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 dev_consume_skb_any(skb); 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, 1487 BNXT_AGG_IDX_BMAP_SIZE); 1488 __set_bit(idx, map->agg_idx_bmap); 1489 map->agg_id_tbl[agg_id] = idx; 1490 return idx; 1491 } 1492 1493 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1494 { 1495 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1496 1497 __clear_bit(idx, map->agg_idx_bmap); 1498 } 1499 1500 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1501 { 1502 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1503 1504 return map->agg_id_tbl[agg_id]; 1505 } 1506 1507 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1508 struct rx_tpa_start_cmp *tpa_start, 1509 struct rx_tpa_start_cmp_ext *tpa_start1) 1510 { 1511 tpa_info->cfa_code_valid = 1; 1512 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1513 tpa_info->vlan_valid = 0; 1514 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1515 tpa_info->vlan_valid = 1; 1516 tpa_info->metadata = 1517 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1518 } 1519 } 1520 1521 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1522 struct rx_tpa_start_cmp *tpa_start, 1523 struct rx_tpa_start_cmp_ext *tpa_start1) 1524 { 1525 tpa_info->vlan_valid = 0; 1526 if (TPA_START_VLAN_VALID(tpa_start)) { 1527 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1528 u32 vlan_proto = ETH_P_8021Q; 1529 1530 tpa_info->vlan_valid = 1; 1531 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1532 vlan_proto = ETH_P_8021AD; 1533 tpa_info->metadata = vlan_proto << 16 | 1534 TPA_START_METADATA0_TCI(tpa_start1); 1535 } 1536 } 1537 1538 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1539 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1540 struct rx_tpa_start_cmp_ext *tpa_start1) 1541 { 1542 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1543 struct bnxt_tpa_info *tpa_info; 1544 u16 cons, prod, agg_id; 1545 struct rx_bd *prod_bd; 1546 dma_addr_t mapping; 1547 1548 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1549 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1550 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1551 } else { 1552 agg_id = TPA_START_AGG_ID(tpa_start); 1553 } 1554 cons = tpa_start->rx_tpa_start_cmp_opaque; 1555 prod = rxr->rx_prod; 1556 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1557 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1558 tpa_info = &rxr->rx_tpa[agg_id]; 1559 1560 if (unlikely(cons != rxr->rx_next_cons || 1561 TPA_START_ERROR(tpa_start))) { 1562 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1563 cons, rxr->rx_next_cons, 1564 TPA_START_ERROR_CODE(tpa_start1)); 1565 bnxt_sched_reset_rxr(bp, rxr); 1566 return; 1567 } 1568 prod_rx_buf->data = tpa_info->data; 1569 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1570 1571 mapping = tpa_info->mapping; 1572 prod_rx_buf->mapping = mapping; 1573 1574 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1575 1576 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1577 1578 tpa_info->data = cons_rx_buf->data; 1579 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1580 cons_rx_buf->data = NULL; 1581 tpa_info->mapping = cons_rx_buf->mapping; 1582 1583 tpa_info->len = 1584 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1585 RX_TPA_START_CMP_LEN_SHIFT; 1586 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1587 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1588 tpa_info->gso_type = SKB_GSO_TCPV4; 1589 if (TPA_START_IS_IPV6(tpa_start1)) 1590 tpa_info->gso_type = SKB_GSO_TCPV6; 1591 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1592 else if (!BNXT_CHIP_P4_PLUS(bp) && 1593 TPA_START_HASH_TYPE(tpa_start) == 3) 1594 tpa_info->gso_type = SKB_GSO_TCPV6; 1595 tpa_info->rss_hash = 1596 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1597 } else { 1598 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1599 tpa_info->gso_type = 0; 1600 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1601 } 1602 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1603 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1604 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1605 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1606 else 1607 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1608 tpa_info->agg_count = 0; 1609 1610 rxr->rx_prod = NEXT_RX(prod); 1611 cons = RING_RX(bp, NEXT_RX(cons)); 1612 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1613 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1614 1615 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1616 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1617 cons_rx_buf->data = NULL; 1618 } 1619 1620 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1621 { 1622 if (agg_bufs) 1623 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1624 } 1625 1626 #ifdef CONFIG_INET 1627 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1628 { 1629 struct udphdr *uh = NULL; 1630 1631 if (ip_proto == htons(ETH_P_IP)) { 1632 struct iphdr *iph = (struct iphdr *)skb->data; 1633 1634 if (iph->protocol == IPPROTO_UDP) 1635 uh = (struct udphdr *)(iph + 1); 1636 } else { 1637 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1638 1639 if (iph->nexthdr == IPPROTO_UDP) 1640 uh = (struct udphdr *)(iph + 1); 1641 } 1642 if (uh) { 1643 if (uh->check) 1644 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1645 else 1646 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1647 } 1648 } 1649 #endif 1650 1651 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1652 int payload_off, int tcp_ts, 1653 struct sk_buff *skb) 1654 { 1655 #ifdef CONFIG_INET 1656 struct tcphdr *th; 1657 int len, nw_off; 1658 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1659 u32 hdr_info = tpa_info->hdr_info; 1660 bool loopback = false; 1661 1662 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1663 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1664 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1665 1666 /* If the packet is an internal loopback packet, the offsets will 1667 * have an extra 4 bytes. 1668 */ 1669 if (inner_mac_off == 4) { 1670 loopback = true; 1671 } else if (inner_mac_off > 4) { 1672 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1673 ETH_HLEN - 2)); 1674 1675 /* We only support inner iPv4/ipv6. If we don't see the 1676 * correct protocol ID, it must be a loopback packet where 1677 * the offsets are off by 4. 1678 */ 1679 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1680 loopback = true; 1681 } 1682 if (loopback) { 1683 /* internal loopback packet, subtract all offsets by 4 */ 1684 inner_ip_off -= 4; 1685 inner_mac_off -= 4; 1686 outer_ip_off -= 4; 1687 } 1688 1689 nw_off = inner_ip_off - ETH_HLEN; 1690 skb_set_network_header(skb, nw_off); 1691 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1692 struct ipv6hdr *iph = ipv6_hdr(skb); 1693 1694 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1695 len = skb->len - skb_transport_offset(skb); 1696 th = tcp_hdr(skb); 1697 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1698 } else { 1699 struct iphdr *iph = ip_hdr(skb); 1700 1701 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1702 len = skb->len - skb_transport_offset(skb); 1703 th = tcp_hdr(skb); 1704 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1705 } 1706 1707 if (inner_mac_off) { /* tunnel */ 1708 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1709 ETH_HLEN - 2)); 1710 1711 bnxt_gro_tunnel(skb, proto); 1712 } 1713 #endif 1714 return skb; 1715 } 1716 1717 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1718 int payload_off, int tcp_ts, 1719 struct sk_buff *skb) 1720 { 1721 #ifdef CONFIG_INET 1722 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1723 u32 hdr_info = tpa_info->hdr_info; 1724 int iphdr_len, nw_off; 1725 1726 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1727 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1728 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1729 1730 nw_off = inner_ip_off - ETH_HLEN; 1731 skb_set_network_header(skb, nw_off); 1732 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1733 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1734 skb_set_transport_header(skb, nw_off + iphdr_len); 1735 1736 if (inner_mac_off) { /* tunnel */ 1737 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1738 ETH_HLEN - 2)); 1739 1740 bnxt_gro_tunnel(skb, proto); 1741 } 1742 #endif 1743 return skb; 1744 } 1745 1746 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1747 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1748 1749 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1750 int payload_off, int tcp_ts, 1751 struct sk_buff *skb) 1752 { 1753 #ifdef CONFIG_INET 1754 struct tcphdr *th; 1755 int len, nw_off, tcp_opt_len = 0; 1756 1757 if (tcp_ts) 1758 tcp_opt_len = 12; 1759 1760 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1761 struct iphdr *iph; 1762 1763 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1764 ETH_HLEN; 1765 skb_set_network_header(skb, nw_off); 1766 iph = ip_hdr(skb); 1767 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1768 len = skb->len - skb_transport_offset(skb); 1769 th = tcp_hdr(skb); 1770 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1771 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1772 struct ipv6hdr *iph; 1773 1774 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1775 ETH_HLEN; 1776 skb_set_network_header(skb, nw_off); 1777 iph = ipv6_hdr(skb); 1778 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1779 len = skb->len - skb_transport_offset(skb); 1780 th = tcp_hdr(skb); 1781 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1782 } else { 1783 dev_kfree_skb_any(skb); 1784 return NULL; 1785 } 1786 1787 if (nw_off) /* tunnel */ 1788 bnxt_gro_tunnel(skb, skb->protocol); 1789 #endif 1790 return skb; 1791 } 1792 1793 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1794 struct bnxt_tpa_info *tpa_info, 1795 struct rx_tpa_end_cmp *tpa_end, 1796 struct rx_tpa_end_cmp_ext *tpa_end1, 1797 struct sk_buff *skb) 1798 { 1799 #ifdef CONFIG_INET 1800 int payload_off; 1801 u16 segs; 1802 1803 segs = TPA_END_TPA_SEGS(tpa_end); 1804 if (segs == 1) 1805 return skb; 1806 1807 NAPI_GRO_CB(skb)->count = segs; 1808 skb_shinfo(skb)->gso_size = 1809 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1810 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1811 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1812 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1813 else 1814 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1815 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1816 if (likely(skb)) 1817 tcp_gro_complete(skb); 1818 #endif 1819 return skb; 1820 } 1821 1822 /* Given the cfa_code of a received packet determine which 1823 * netdev (vf-rep or PF) the packet is destined to. 1824 */ 1825 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1826 { 1827 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1828 1829 /* if vf-rep dev is NULL, it must belong to the PF */ 1830 return dev ? dev : bp->dev; 1831 } 1832 1833 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1834 struct bnxt_cp_ring_info *cpr, 1835 u32 *raw_cons, 1836 struct rx_tpa_end_cmp *tpa_end, 1837 struct rx_tpa_end_cmp_ext *tpa_end1, 1838 u8 *event) 1839 { 1840 struct bnxt_napi *bnapi = cpr->bnapi; 1841 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1842 struct net_device *dev = bp->dev; 1843 u8 *data_ptr, agg_bufs; 1844 unsigned int len; 1845 struct bnxt_tpa_info *tpa_info; 1846 dma_addr_t mapping; 1847 struct sk_buff *skb; 1848 u16 idx = 0, agg_id; 1849 void *data; 1850 bool gro; 1851 1852 if (unlikely(bnapi->in_reset)) { 1853 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1854 1855 if (rc < 0) 1856 return ERR_PTR(-EBUSY); 1857 return NULL; 1858 } 1859 1860 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1861 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1862 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1863 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1864 tpa_info = &rxr->rx_tpa[agg_id]; 1865 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1866 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1867 agg_bufs, tpa_info->agg_count); 1868 agg_bufs = tpa_info->agg_count; 1869 } 1870 tpa_info->agg_count = 0; 1871 *event |= BNXT_AGG_EVENT; 1872 bnxt_free_agg_idx(rxr, agg_id); 1873 idx = agg_id; 1874 gro = !!(bp->flags & BNXT_FLAG_GRO); 1875 } else { 1876 agg_id = TPA_END_AGG_ID(tpa_end); 1877 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1878 tpa_info = &rxr->rx_tpa[agg_id]; 1879 idx = RING_CMP(*raw_cons); 1880 if (agg_bufs) { 1881 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1882 return ERR_PTR(-EBUSY); 1883 1884 *event |= BNXT_AGG_EVENT; 1885 idx = NEXT_CMP(idx); 1886 } 1887 gro = !!TPA_END_GRO(tpa_end); 1888 } 1889 data = tpa_info->data; 1890 data_ptr = tpa_info->data_ptr; 1891 prefetch(data_ptr); 1892 len = tpa_info->len; 1893 mapping = tpa_info->mapping; 1894 1895 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1896 bnxt_abort_tpa(cpr, idx, agg_bufs); 1897 if (agg_bufs > MAX_SKB_FRAGS) 1898 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1899 agg_bufs, (int)MAX_SKB_FRAGS); 1900 return NULL; 1901 } 1902 1903 if (len <= bp->rx_copybreak) { 1904 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1905 if (!skb) { 1906 bnxt_abort_tpa(cpr, idx, agg_bufs); 1907 cpr->sw_stats->rx.rx_oom_discards += 1; 1908 return NULL; 1909 } 1910 } else { 1911 u8 *new_data; 1912 dma_addr_t new_mapping; 1913 1914 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr, 1915 GFP_ATOMIC); 1916 if (!new_data) { 1917 bnxt_abort_tpa(cpr, idx, agg_bufs); 1918 cpr->sw_stats->rx.rx_oom_discards += 1; 1919 return NULL; 1920 } 1921 1922 tpa_info->data = new_data; 1923 tpa_info->data_ptr = new_data + bp->rx_offset; 1924 tpa_info->mapping = new_mapping; 1925 1926 skb = napi_build_skb(data, bp->rx_buf_size); 1927 dma_sync_single_for_cpu(&bp->pdev->dev, mapping, 1928 bp->rx_buf_use_size, bp->rx_dir); 1929 1930 if (!skb) { 1931 page_pool_free_va(rxr->head_pool, data, true); 1932 bnxt_abort_tpa(cpr, idx, agg_bufs); 1933 cpr->sw_stats->rx.rx_oom_discards += 1; 1934 return NULL; 1935 } 1936 skb_mark_for_recycle(skb); 1937 skb_reserve(skb, bp->rx_offset); 1938 skb_put(skb, len); 1939 } 1940 1941 if (agg_bufs) { 1942 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs, 1943 true); 1944 if (!skb) { 1945 /* Page reuse already handled by bnxt_rx_pages(). */ 1946 cpr->sw_stats->rx.rx_oom_discards += 1; 1947 return NULL; 1948 } 1949 } 1950 1951 if (tpa_info->cfa_code_valid) 1952 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1953 skb->protocol = eth_type_trans(skb, dev); 1954 1955 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1956 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1957 1958 if (tpa_info->vlan_valid && 1959 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1960 __be16 vlan_proto = htons(tpa_info->metadata >> 1961 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1962 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1963 1964 if (eth_type_vlan(vlan_proto)) { 1965 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1966 } else { 1967 dev_kfree_skb(skb); 1968 return NULL; 1969 } 1970 } 1971 1972 skb_checksum_none_assert(skb); 1973 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1974 skb->ip_summed = CHECKSUM_UNNECESSARY; 1975 skb->csum_level = 1976 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1977 } 1978 1979 if (gro) 1980 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1981 1982 return skb; 1983 } 1984 1985 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1986 struct rx_agg_cmp *rx_agg) 1987 { 1988 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1989 struct bnxt_tpa_info *tpa_info; 1990 1991 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1992 tpa_info = &rxr->rx_tpa[agg_id]; 1993 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1994 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1995 } 1996 1997 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1998 struct sk_buff *skb) 1999 { 2000 skb_mark_for_recycle(skb); 2001 2002 if (skb->dev != bp->dev) { 2003 /* this packet belongs to a vf-rep */ 2004 bnxt_vf_rep_rx(bp, skb); 2005 return; 2006 } 2007 skb_record_rx_queue(skb, bnapi->index); 2008 napi_gro_receive(&bnapi->napi, skb); 2009 } 2010 2011 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 2012 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 2013 { 2014 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 2015 2016 if (BNXT_PTP_RX_TS_VALID(flags)) 2017 goto ts_valid; 2018 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 2019 return false; 2020 2021 ts_valid: 2022 *cmpl_ts = ts; 2023 return true; 2024 } 2025 2026 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 2027 struct rx_cmp *rxcmp, 2028 struct rx_cmp_ext *rxcmp1) 2029 { 2030 __be16 vlan_proto; 2031 u16 vtag; 2032 2033 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 2034 __le32 flags2 = rxcmp1->rx_cmp_flags2; 2035 u32 meta_data; 2036 2037 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 2038 return skb; 2039 2040 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 2041 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 2042 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 2043 if (eth_type_vlan(vlan_proto)) 2044 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2045 else 2046 goto vlan_err; 2047 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2048 if (RX_CMP_VLAN_VALID(rxcmp)) { 2049 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 2050 2051 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 2052 vlan_proto = htons(ETH_P_8021Q); 2053 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 2054 vlan_proto = htons(ETH_P_8021AD); 2055 else 2056 goto vlan_err; 2057 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 2058 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 2059 } 2060 } 2061 return skb; 2062 vlan_err: 2063 skb_mark_for_recycle(skb); 2064 dev_kfree_skb(skb); 2065 return NULL; 2066 } 2067 2068 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 2069 struct rx_cmp *rxcmp) 2070 { 2071 u8 ext_op; 2072 2073 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2074 switch (ext_op) { 2075 case EXT_OP_INNER_4: 2076 case EXT_OP_OUTER_4: 2077 case EXT_OP_INNFL_3: 2078 case EXT_OP_OUTFL_3: 2079 return PKT_HASH_TYPE_L4; 2080 default: 2081 return PKT_HASH_TYPE_L3; 2082 } 2083 } 2084 2085 /* returns the following: 2086 * 1 - 1 packet successfully received 2087 * 0 - successful TPA_START, packet not completed yet 2088 * -EBUSY - completion ring does not have all the agg buffers yet 2089 * -ENOMEM - packet aborted due to out of memory 2090 * -EIO - packet aborted due to hw error indicated in BD 2091 */ 2092 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2093 u32 *raw_cons, u8 *event) 2094 { 2095 struct bnxt_napi *bnapi = cpr->bnapi; 2096 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2097 struct net_device *dev = bp->dev; 2098 struct rx_cmp *rxcmp; 2099 struct rx_cmp_ext *rxcmp1; 2100 u32 tmp_raw_cons = *raw_cons; 2101 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2102 struct skb_shared_info *sinfo; 2103 struct bnxt_sw_rx_bd *rx_buf; 2104 unsigned int len; 2105 u8 *data_ptr, agg_bufs, cmp_type; 2106 bool xdp_active = false; 2107 dma_addr_t dma_addr; 2108 struct sk_buff *skb; 2109 struct xdp_buff xdp; 2110 u32 flags, misc; 2111 u32 cmpl_ts; 2112 void *data; 2113 int rc = 0; 2114 2115 rxcmp = (struct rx_cmp *) 2116 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2117 2118 cmp_type = RX_CMP_TYPE(rxcmp); 2119 2120 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2121 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2122 goto next_rx_no_prod_no_len; 2123 } 2124 2125 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2126 cp_cons = RING_CMP(tmp_raw_cons); 2127 rxcmp1 = (struct rx_cmp_ext *) 2128 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2129 2130 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2131 return -EBUSY; 2132 2133 /* The valid test of the entry must be done first before 2134 * reading any further. 2135 */ 2136 dma_rmb(); 2137 prod = rxr->rx_prod; 2138 2139 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2140 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2141 bnxt_tpa_start(bp, rxr, cmp_type, 2142 (struct rx_tpa_start_cmp *)rxcmp, 2143 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2144 2145 *event |= BNXT_RX_EVENT; 2146 goto next_rx_no_prod_no_len; 2147 2148 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2149 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2150 (struct rx_tpa_end_cmp *)rxcmp, 2151 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2152 2153 if (IS_ERR(skb)) 2154 return -EBUSY; 2155 2156 rc = -ENOMEM; 2157 if (likely(skb)) { 2158 bnxt_deliver_skb(bp, bnapi, skb); 2159 rc = 1; 2160 } 2161 *event |= BNXT_RX_EVENT; 2162 goto next_rx_no_prod_no_len; 2163 } 2164 2165 cons = rxcmp->rx_cmp_opaque; 2166 if (unlikely(cons != rxr->rx_next_cons)) { 2167 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2168 2169 /* 0xffff is forced error, don't print it */ 2170 if (rxr->rx_next_cons != 0xffff) 2171 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2172 cons, rxr->rx_next_cons); 2173 bnxt_sched_reset_rxr(bp, rxr); 2174 if (rc1) 2175 return rc1; 2176 goto next_rx_no_prod_no_len; 2177 } 2178 rx_buf = &rxr->rx_buf_ring[cons]; 2179 data = rx_buf->data; 2180 data_ptr = rx_buf->data_ptr; 2181 prefetch(data_ptr); 2182 2183 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2184 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2185 2186 if (agg_bufs) { 2187 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2188 return -EBUSY; 2189 2190 cp_cons = NEXT_CMP(cp_cons); 2191 *event |= BNXT_AGG_EVENT; 2192 } 2193 *event |= BNXT_RX_EVENT; 2194 2195 rx_buf->data = NULL; 2196 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2197 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2198 2199 bnxt_reuse_rx_data(rxr, cons, data); 2200 if (agg_bufs) 2201 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2202 false); 2203 2204 rc = -EIO; 2205 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2206 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2207 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2208 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2209 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2210 rx_err); 2211 bnxt_sched_reset_rxr(bp, rxr); 2212 } 2213 } 2214 goto next_rx_no_len; 2215 } 2216 2217 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2218 len = flags >> RX_CMP_LEN_SHIFT; 2219 dma_addr = rx_buf->mapping; 2220 2221 if (bnxt_xdp_attached(bp, rxr)) { 2222 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2223 if (agg_bufs) { 2224 u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp, 2225 cp_cons, 2226 agg_bufs, 2227 false); 2228 if (!frag_len) 2229 goto oom_next_rx; 2230 2231 } 2232 xdp_active = true; 2233 } 2234 2235 if (xdp_active) { 2236 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2237 rc = 1; 2238 goto next_rx; 2239 } 2240 if (xdp_buff_has_frags(&xdp)) { 2241 sinfo = xdp_get_shared_info_from_buff(&xdp); 2242 agg_bufs = sinfo->nr_frags; 2243 } else { 2244 agg_bufs = 0; 2245 } 2246 } 2247 2248 if (len <= bp->rx_copybreak) { 2249 if (!xdp_active) 2250 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2251 else 2252 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2253 bnxt_reuse_rx_data(rxr, cons, data); 2254 if (!skb) { 2255 if (agg_bufs) { 2256 if (!xdp_active) 2257 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2258 agg_bufs, false); 2259 else 2260 bnxt_xdp_buff_frags_free(rxr, &xdp); 2261 } 2262 goto oom_next_rx; 2263 } 2264 } else { 2265 u32 payload; 2266 2267 if (rx_buf->data_ptr == data_ptr) 2268 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2269 else 2270 payload = 0; 2271 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2272 payload | len); 2273 if (!skb) 2274 goto oom_next_rx; 2275 } 2276 2277 if (agg_bufs) { 2278 if (!xdp_active) { 2279 skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons, 2280 agg_bufs, false); 2281 if (!skb) 2282 goto oom_next_rx; 2283 } else { 2284 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, 2285 rxr->page_pool, &xdp); 2286 if (!skb) { 2287 /* we should be able to free the old skb here */ 2288 bnxt_xdp_buff_frags_free(rxr, &xdp); 2289 goto oom_next_rx; 2290 } 2291 } 2292 } 2293 2294 if (RX_CMP_HASH_VALID(rxcmp)) { 2295 enum pkt_hash_types type; 2296 2297 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2298 type = bnxt_rss_ext_op(bp, rxcmp); 2299 } else { 2300 u32 itypes = RX_CMP_ITYPES(rxcmp); 2301 2302 if (itypes == RX_CMP_FLAGS_ITYPE_TCP || 2303 itypes == RX_CMP_FLAGS_ITYPE_UDP) 2304 type = PKT_HASH_TYPE_L4; 2305 else 2306 type = PKT_HASH_TYPE_L3; 2307 } 2308 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2309 } 2310 2311 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2312 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2313 skb->protocol = eth_type_trans(skb, dev); 2314 2315 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2316 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2317 if (!skb) 2318 goto next_rx; 2319 } 2320 2321 skb_checksum_none_assert(skb); 2322 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2323 if (dev->features & NETIF_F_RXCSUM) { 2324 skb->ip_summed = CHECKSUM_UNNECESSARY; 2325 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2326 } 2327 } else { 2328 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2329 if (dev->features & NETIF_F_RXCSUM) 2330 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2331 } 2332 } 2333 2334 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2335 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2336 u64 ns, ts; 2337 2338 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2339 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2340 2341 ns = bnxt_timecounter_cyc2time(ptp, ts); 2342 memset(skb_hwtstamps(skb), 0, 2343 sizeof(*skb_hwtstamps(skb))); 2344 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2345 } 2346 } 2347 } 2348 bnxt_deliver_skb(bp, bnapi, skb); 2349 rc = 1; 2350 2351 next_rx: 2352 cpr->rx_packets += 1; 2353 cpr->rx_bytes += len; 2354 2355 next_rx_no_len: 2356 rxr->rx_prod = NEXT_RX(prod); 2357 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2358 2359 next_rx_no_prod_no_len: 2360 *raw_cons = tmp_raw_cons; 2361 2362 return rc; 2363 2364 oom_next_rx: 2365 cpr->sw_stats->rx.rx_oom_discards += 1; 2366 rc = -ENOMEM; 2367 goto next_rx; 2368 } 2369 2370 /* In netpoll mode, if we are using a combined completion ring, we need to 2371 * discard the rx packets and recycle the buffers. 2372 */ 2373 static int bnxt_force_rx_discard(struct bnxt *bp, 2374 struct bnxt_cp_ring_info *cpr, 2375 u32 *raw_cons, u8 *event) 2376 { 2377 u32 tmp_raw_cons = *raw_cons; 2378 struct rx_cmp_ext *rxcmp1; 2379 struct rx_cmp *rxcmp; 2380 u16 cp_cons; 2381 u8 cmp_type; 2382 int rc; 2383 2384 cp_cons = RING_CMP(tmp_raw_cons); 2385 rxcmp = (struct rx_cmp *) 2386 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2387 2388 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2389 cp_cons = RING_CMP(tmp_raw_cons); 2390 rxcmp1 = (struct rx_cmp_ext *) 2391 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2392 2393 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2394 return -EBUSY; 2395 2396 /* The valid test of the entry must be done first before 2397 * reading any further. 2398 */ 2399 dma_rmb(); 2400 cmp_type = RX_CMP_TYPE(rxcmp); 2401 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2402 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2403 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2404 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2405 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2406 struct rx_tpa_end_cmp_ext *tpa_end1; 2407 2408 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2409 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2410 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2411 } 2412 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2413 if (rc && rc != -EBUSY) 2414 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2415 return rc; 2416 } 2417 2418 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2419 { 2420 struct bnxt_fw_health *fw_health = bp->fw_health; 2421 u32 reg = fw_health->regs[reg_idx]; 2422 u32 reg_type, reg_off, val = 0; 2423 2424 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2425 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2426 switch (reg_type) { 2427 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2428 pci_read_config_dword(bp->pdev, reg_off, &val); 2429 break; 2430 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2431 reg_off = fw_health->mapped_regs[reg_idx]; 2432 fallthrough; 2433 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2434 val = readl(bp->bar0 + reg_off); 2435 break; 2436 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2437 val = readl(bp->bar1 + reg_off); 2438 break; 2439 } 2440 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2441 val &= fw_health->fw_reset_inprog_reg_mask; 2442 return val; 2443 } 2444 2445 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2446 { 2447 int i; 2448 2449 for (i = 0; i < bp->rx_nr_rings; i++) { 2450 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2451 struct bnxt_ring_grp_info *grp_info; 2452 2453 grp_info = &bp->grp_info[grp_idx]; 2454 if (grp_info->agg_fw_ring_id == ring_id) 2455 return grp_idx; 2456 } 2457 return INVALID_HW_RING_ID; 2458 } 2459 2460 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2461 { 2462 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2463 2464 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2465 return link_info->force_link_speed2; 2466 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2467 return link_info->force_pam4_link_speed; 2468 return link_info->force_link_speed; 2469 } 2470 2471 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2472 { 2473 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2474 2475 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2476 link_info->req_link_speed = link_info->force_link_speed2; 2477 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2478 switch (link_info->req_link_speed) { 2479 case BNXT_LINK_SPEED_50GB_PAM4: 2480 case BNXT_LINK_SPEED_100GB_PAM4: 2481 case BNXT_LINK_SPEED_200GB_PAM4: 2482 case BNXT_LINK_SPEED_400GB_PAM4: 2483 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2484 break; 2485 case BNXT_LINK_SPEED_100GB_PAM4_112: 2486 case BNXT_LINK_SPEED_200GB_PAM4_112: 2487 case BNXT_LINK_SPEED_400GB_PAM4_112: 2488 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2489 break; 2490 default: 2491 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2492 } 2493 return; 2494 } 2495 link_info->req_link_speed = link_info->force_link_speed; 2496 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2497 if (link_info->force_pam4_link_speed) { 2498 link_info->req_link_speed = link_info->force_pam4_link_speed; 2499 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2500 } 2501 } 2502 2503 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2504 { 2505 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2506 2507 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2508 link_info->advertising = link_info->auto_link_speeds2; 2509 return; 2510 } 2511 link_info->advertising = link_info->auto_link_speeds; 2512 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2513 } 2514 2515 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2516 { 2517 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2518 2519 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2520 if (link_info->req_link_speed != link_info->force_link_speed2) 2521 return true; 2522 return false; 2523 } 2524 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2525 link_info->req_link_speed != link_info->force_link_speed) 2526 return true; 2527 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2528 link_info->req_link_speed != link_info->force_pam4_link_speed) 2529 return true; 2530 return false; 2531 } 2532 2533 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2534 { 2535 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2536 2537 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2538 if (link_info->advertising != link_info->auto_link_speeds2) 2539 return true; 2540 return false; 2541 } 2542 if (link_info->advertising != link_info->auto_link_speeds || 2543 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2544 return true; 2545 return false; 2546 } 2547 2548 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type) 2549 { 2550 u32 flags = bp->ctx->ctx_arr[type].flags; 2551 2552 return (flags & BNXT_CTX_MEM_TYPE_VALID) && 2553 ((flags & BNXT_CTX_MEM_FW_TRACE) || 2554 (flags & BNXT_CTX_MEM_FW_BIN_TRACE)); 2555 } 2556 2557 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm) 2558 { 2559 u32 mem_size, pages, rem_bytes, magic_byte_offset; 2560 u16 trace_type = bnxt_bstore_to_trace[ctxm->type]; 2561 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 2562 struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl; 2563 struct bnxt_bs_trace_info *bs_trace; 2564 int last_pg; 2565 2566 if (ctxm->instance_bmap && ctxm->instance_bmap > 1) 2567 return; 2568 2569 mem_size = ctxm->max_entries * ctxm->entry_size; 2570 rem_bytes = mem_size % BNXT_PAGE_SIZE; 2571 pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 2572 2573 last_pg = (pages - 1) & (MAX_CTX_PAGES - 1); 2574 magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1; 2575 2576 rmem = &ctx_pg[0].ring_mem; 2577 bs_trace = &bp->bs_trace[trace_type]; 2578 bs_trace->ctx_type = ctxm->type; 2579 bs_trace->trace_type = trace_type; 2580 if (pages > MAX_CTX_PAGES) { 2581 int last_pg_dir = rmem->nr_pages - 1; 2582 2583 rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem; 2584 bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg]; 2585 } else { 2586 bs_trace->magic_byte = rmem->pg_arr[last_pg]; 2587 } 2588 bs_trace->magic_byte += magic_byte_offset; 2589 *bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE; 2590 } 2591 2592 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1) \ 2593 (((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\ 2594 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT) 2595 2596 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2) \ 2597 (((data2) & \ 2598 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\ 2599 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT) 2600 2601 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2602 ((data2) & \ 2603 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2604 2605 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2606 (((data2) & \ 2607 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2608 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2609 2610 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2611 ((data1) & \ 2612 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2613 2614 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2615 (((data1) & \ 2616 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2617 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2618 2619 /* Return true if the workqueue has to be scheduled */ 2620 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2621 { 2622 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2623 2624 switch (err_type) { 2625 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2626 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2627 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2628 break; 2629 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2630 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2631 break; 2632 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2633 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2634 break; 2635 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2636 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2637 char *threshold_type; 2638 bool notify = false; 2639 char *dir_str; 2640 2641 switch (type) { 2642 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2643 threshold_type = "warning"; 2644 break; 2645 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2646 threshold_type = "critical"; 2647 break; 2648 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2649 threshold_type = "fatal"; 2650 break; 2651 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2652 threshold_type = "shutdown"; 2653 break; 2654 default: 2655 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2656 return false; 2657 } 2658 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2659 dir_str = "above"; 2660 notify = true; 2661 } else { 2662 dir_str = "below"; 2663 } 2664 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2665 dir_str, threshold_type); 2666 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2667 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2668 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2669 if (notify) { 2670 bp->thermal_threshold_type = type; 2671 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2672 return true; 2673 } 2674 return false; 2675 } 2676 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2677 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2678 break; 2679 default: 2680 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2681 err_type); 2682 break; 2683 } 2684 return false; 2685 } 2686 2687 #define BNXT_GET_EVENT_PORT(data) \ 2688 ((data) & \ 2689 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2690 2691 #define BNXT_EVENT_RING_TYPE(data2) \ 2692 ((data2) & \ 2693 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2694 2695 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2696 (BNXT_EVENT_RING_TYPE(data2) == \ 2697 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2698 2699 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2700 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2701 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2702 2703 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2704 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2705 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2706 2707 #define BNXT_PHC_BITS 48 2708 2709 static int bnxt_async_event_process(struct bnxt *bp, 2710 struct hwrm_async_event_cmpl *cmpl) 2711 { 2712 u16 event_id = le16_to_cpu(cmpl->event_id); 2713 u32 data1 = le32_to_cpu(cmpl->event_data1); 2714 u32 data2 = le32_to_cpu(cmpl->event_data2); 2715 2716 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2717 event_id, data1, data2); 2718 2719 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2720 switch (event_id) { 2721 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2722 struct bnxt_link_info *link_info = &bp->link_info; 2723 2724 if (BNXT_VF(bp)) 2725 goto async_event_process_exit; 2726 2727 /* print unsupported speed warning in forced speed mode only */ 2728 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2729 (data1 & 0x20000)) { 2730 u16 fw_speed = bnxt_get_force_speed(link_info); 2731 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2732 2733 if (speed != SPEED_UNKNOWN) 2734 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2735 speed); 2736 } 2737 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2738 } 2739 fallthrough; 2740 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2741 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2742 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2743 fallthrough; 2744 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2745 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2746 break; 2747 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2748 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2749 break; 2750 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2751 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2752 2753 if (BNXT_VF(bp)) 2754 break; 2755 2756 if (bp->pf.port_id != port_id) 2757 break; 2758 2759 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2760 break; 2761 } 2762 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2763 if (BNXT_PF(bp)) 2764 goto async_event_process_exit; 2765 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2766 break; 2767 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2768 char *type_str = "Solicited"; 2769 2770 if (!bp->fw_health) 2771 goto async_event_process_exit; 2772 2773 bp->fw_reset_timestamp = jiffies; 2774 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2775 if (!bp->fw_reset_min_dsecs) 2776 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2777 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2778 if (!bp->fw_reset_max_dsecs) 2779 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2780 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2781 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2782 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2783 type_str = "Fatal"; 2784 bp->fw_health->fatalities++; 2785 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2786 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2787 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2788 type_str = "Non-fatal"; 2789 bp->fw_health->survivals++; 2790 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2791 } 2792 netif_warn(bp, hw, bp->dev, 2793 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2794 type_str, data1, data2, 2795 bp->fw_reset_min_dsecs * 100, 2796 bp->fw_reset_max_dsecs * 100); 2797 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2798 break; 2799 } 2800 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2801 struct bnxt_fw_health *fw_health = bp->fw_health; 2802 char *status_desc = "healthy"; 2803 u32 status; 2804 2805 if (!fw_health) 2806 goto async_event_process_exit; 2807 2808 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2809 fw_health->enabled = false; 2810 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2811 break; 2812 } 2813 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2814 fw_health->tmr_multiplier = 2815 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2816 bp->current_interval * 10); 2817 fw_health->tmr_counter = fw_health->tmr_multiplier; 2818 if (!fw_health->enabled) 2819 fw_health->last_fw_heartbeat = 2820 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2821 fw_health->last_fw_reset_cnt = 2822 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2823 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2824 if (status != BNXT_FW_STATUS_HEALTHY) 2825 status_desc = "unhealthy"; 2826 netif_info(bp, drv, bp->dev, 2827 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2828 fw_health->primary ? "primary" : "backup", status, 2829 status_desc, fw_health->last_fw_reset_cnt); 2830 if (!fw_health->enabled) { 2831 /* Make sure tmr_counter is set and visible to 2832 * bnxt_health_check() before setting enabled to true. 2833 */ 2834 smp_wmb(); 2835 fw_health->enabled = true; 2836 } 2837 goto async_event_process_exit; 2838 } 2839 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2840 netif_notice(bp, hw, bp->dev, 2841 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2842 data1, data2); 2843 goto async_event_process_exit; 2844 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2845 struct bnxt_rx_ring_info *rxr; 2846 u16 grp_idx; 2847 2848 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2849 goto async_event_process_exit; 2850 2851 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2852 BNXT_EVENT_RING_TYPE(data2), data1); 2853 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2854 goto async_event_process_exit; 2855 2856 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2857 if (grp_idx == INVALID_HW_RING_ID) { 2858 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2859 data1); 2860 goto async_event_process_exit; 2861 } 2862 rxr = bp->bnapi[grp_idx]->rx_ring; 2863 bnxt_sched_reset_rxr(bp, rxr); 2864 goto async_event_process_exit; 2865 } 2866 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2867 struct bnxt_fw_health *fw_health = bp->fw_health; 2868 2869 netif_notice(bp, hw, bp->dev, 2870 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2871 data1, data2); 2872 if (fw_health) { 2873 fw_health->echo_req_data1 = data1; 2874 fw_health->echo_req_data2 = data2; 2875 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2876 break; 2877 } 2878 goto async_event_process_exit; 2879 } 2880 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2881 bnxt_ptp_pps_event(bp, data1, data2); 2882 goto async_event_process_exit; 2883 } 2884 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2885 if (bnxt_event_error_report(bp, data1, data2)) 2886 break; 2887 goto async_event_process_exit; 2888 } 2889 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2890 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2891 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2892 if (BNXT_PTP_USE_RTC(bp)) { 2893 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2894 unsigned long flags; 2895 u64 ns; 2896 2897 if (!ptp) 2898 goto async_event_process_exit; 2899 2900 bnxt_ptp_update_current_time(bp); 2901 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2902 BNXT_PHC_BITS) | ptp->current_time); 2903 write_seqlock_irqsave(&ptp->ptp_lock, flags); 2904 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2905 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 2906 } 2907 break; 2908 } 2909 goto async_event_process_exit; 2910 } 2911 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2912 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2913 2914 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2915 goto async_event_process_exit; 2916 } 2917 case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: { 2918 u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1); 2919 u32 offset = BNXT_EVENT_BUF_PRODUCER_OFFSET(data2); 2920 2921 bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset); 2922 goto async_event_process_exit; 2923 } 2924 default: 2925 goto async_event_process_exit; 2926 } 2927 __bnxt_queue_sp_work(bp); 2928 async_event_process_exit: 2929 bnxt_ulp_async_events(bp, cmpl); 2930 return 0; 2931 } 2932 2933 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2934 { 2935 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2936 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2937 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2938 (struct hwrm_fwd_req_cmpl *)txcmp; 2939 2940 switch (cmpl_type) { 2941 case CMPL_BASE_TYPE_HWRM_DONE: 2942 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2943 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2944 break; 2945 2946 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2947 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2948 2949 if ((vf_id < bp->pf.first_vf_id) || 2950 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2951 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2952 vf_id); 2953 return -EINVAL; 2954 } 2955 2956 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2957 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2958 break; 2959 2960 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2961 bnxt_async_event_process(bp, 2962 (struct hwrm_async_event_cmpl *)txcmp); 2963 break; 2964 2965 default: 2966 break; 2967 } 2968 2969 return 0; 2970 } 2971 2972 static bool bnxt_vnic_is_active(struct bnxt *bp) 2973 { 2974 struct bnxt_vnic_info *vnic = &bp->vnic_info[0]; 2975 2976 return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0; 2977 } 2978 2979 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2980 { 2981 struct bnxt_napi *bnapi = dev_instance; 2982 struct bnxt *bp = bnapi->bp; 2983 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2984 u32 cons = RING_CMP(cpr->cp_raw_cons); 2985 2986 cpr->event_ctr++; 2987 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2988 napi_schedule(&bnapi->napi); 2989 return IRQ_HANDLED; 2990 } 2991 2992 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2993 { 2994 u32 raw_cons = cpr->cp_raw_cons; 2995 u16 cons = RING_CMP(raw_cons); 2996 struct tx_cmp *txcmp; 2997 2998 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2999 3000 return TX_CMP_VALID(txcmp, raw_cons); 3001 } 3002 3003 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3004 int budget) 3005 { 3006 struct bnxt_napi *bnapi = cpr->bnapi; 3007 u32 raw_cons = cpr->cp_raw_cons; 3008 bool flush_xdp = false; 3009 u32 cons; 3010 int rx_pkts = 0; 3011 u8 event = 0; 3012 struct tx_cmp *txcmp; 3013 3014 cpr->has_more_work = 0; 3015 cpr->had_work_done = 1; 3016 while (1) { 3017 u8 cmp_type; 3018 int rc; 3019 3020 cons = RING_CMP(raw_cons); 3021 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3022 3023 if (!TX_CMP_VALID(txcmp, raw_cons)) 3024 break; 3025 3026 /* The valid test of the entry must be done first before 3027 * reading any further. 3028 */ 3029 dma_rmb(); 3030 cmp_type = TX_CMP_TYPE(txcmp); 3031 if (cmp_type == CMP_TYPE_TX_L2_CMP || 3032 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 3033 u32 opaque = txcmp->tx_cmp_opaque; 3034 struct bnxt_tx_ring_info *txr; 3035 u16 tx_freed; 3036 3037 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 3038 event |= BNXT_TX_CMP_EVENT; 3039 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 3040 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 3041 else 3042 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 3043 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 3044 bp->tx_ring_mask; 3045 /* return full budget so NAPI will complete. */ 3046 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 3047 rx_pkts = budget; 3048 raw_cons = NEXT_RAW_CMP(raw_cons); 3049 if (budget) 3050 cpr->has_more_work = 1; 3051 break; 3052 } 3053 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 3054 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 3055 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 3056 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 3057 if (likely(budget)) 3058 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3059 else 3060 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 3061 &event); 3062 if (event & BNXT_REDIRECT_EVENT) 3063 flush_xdp = true; 3064 if (likely(rc >= 0)) 3065 rx_pkts += rc; 3066 /* Increment rx_pkts when rc is -ENOMEM to count towards 3067 * the NAPI budget. Otherwise, we may potentially loop 3068 * here forever if we consistently cannot allocate 3069 * buffers. 3070 */ 3071 else if (rc == -ENOMEM && budget) 3072 rx_pkts++; 3073 else if (rc == -EBUSY) /* partial completion */ 3074 break; 3075 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 3076 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 3077 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 3078 bnxt_hwrm_handler(bp, txcmp); 3079 } 3080 raw_cons = NEXT_RAW_CMP(raw_cons); 3081 3082 if (rx_pkts && rx_pkts == budget) { 3083 cpr->has_more_work = 1; 3084 break; 3085 } 3086 } 3087 3088 if (flush_xdp) { 3089 xdp_do_flush(); 3090 event &= ~BNXT_REDIRECT_EVENT; 3091 } 3092 3093 if (event & BNXT_TX_EVENT) { 3094 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 3095 u16 prod = txr->tx_prod; 3096 3097 /* Sync BD data before updating doorbell */ 3098 wmb(); 3099 3100 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 3101 event &= ~BNXT_TX_EVENT; 3102 } 3103 3104 cpr->cp_raw_cons = raw_cons; 3105 bnapi->events |= event; 3106 return rx_pkts; 3107 } 3108 3109 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3110 int budget) 3111 { 3112 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 3113 bnapi->tx_int(bp, bnapi, budget); 3114 3115 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 3116 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3117 3118 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3119 bnapi->events &= ~BNXT_RX_EVENT; 3120 } 3121 if (bnapi->events & BNXT_AGG_EVENT) { 3122 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3123 3124 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3125 bnapi->events &= ~BNXT_AGG_EVENT; 3126 } 3127 } 3128 3129 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3130 int budget) 3131 { 3132 struct bnxt_napi *bnapi = cpr->bnapi; 3133 int rx_pkts; 3134 3135 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3136 3137 /* ACK completion ring before freeing tx ring and producing new 3138 * buffers in rx/agg rings to prevent overflowing the completion 3139 * ring. 3140 */ 3141 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3142 3143 __bnxt_poll_work_done(bp, bnapi, budget); 3144 return rx_pkts; 3145 } 3146 3147 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3148 { 3149 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3150 struct bnxt *bp = bnapi->bp; 3151 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3152 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3153 struct tx_cmp *txcmp; 3154 struct rx_cmp_ext *rxcmp1; 3155 u32 cp_cons, tmp_raw_cons; 3156 u32 raw_cons = cpr->cp_raw_cons; 3157 bool flush_xdp = false; 3158 u32 rx_pkts = 0; 3159 u8 event = 0; 3160 3161 while (1) { 3162 int rc; 3163 3164 cp_cons = RING_CMP(raw_cons); 3165 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3166 3167 if (!TX_CMP_VALID(txcmp, raw_cons)) 3168 break; 3169 3170 /* The valid test of the entry must be done first before 3171 * reading any further. 3172 */ 3173 dma_rmb(); 3174 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3175 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3176 cp_cons = RING_CMP(tmp_raw_cons); 3177 rxcmp1 = (struct rx_cmp_ext *) 3178 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3179 3180 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3181 break; 3182 3183 /* force an error to recycle the buffer */ 3184 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3185 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3186 3187 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3188 if (likely(rc == -EIO) && budget) 3189 rx_pkts++; 3190 else if (rc == -EBUSY) /* partial completion */ 3191 break; 3192 if (event & BNXT_REDIRECT_EVENT) 3193 flush_xdp = true; 3194 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3195 CMPL_BASE_TYPE_HWRM_DONE)) { 3196 bnxt_hwrm_handler(bp, txcmp); 3197 } else { 3198 netdev_err(bp->dev, 3199 "Invalid completion received on special ring\n"); 3200 } 3201 raw_cons = NEXT_RAW_CMP(raw_cons); 3202 3203 if (rx_pkts == budget) 3204 break; 3205 } 3206 3207 cpr->cp_raw_cons = raw_cons; 3208 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3209 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3210 3211 if (event & BNXT_AGG_EVENT) 3212 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3213 if (flush_xdp) 3214 xdp_do_flush(); 3215 3216 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3217 napi_complete_done(napi, rx_pkts); 3218 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3219 } 3220 return rx_pkts; 3221 } 3222 3223 static int bnxt_poll(struct napi_struct *napi, int budget) 3224 { 3225 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3226 struct bnxt *bp = bnapi->bp; 3227 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3228 int work_done = 0; 3229 3230 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3231 napi_complete(napi); 3232 return 0; 3233 } 3234 while (1) { 3235 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3236 3237 if (work_done >= budget) { 3238 if (!budget) 3239 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3240 break; 3241 } 3242 3243 if (!bnxt_has_work(bp, cpr)) { 3244 if (napi_complete_done(napi, work_done)) 3245 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3246 break; 3247 } 3248 } 3249 if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3250 struct dim_sample dim_sample = {}; 3251 3252 dim_update_sample(cpr->event_ctr, 3253 cpr->rx_packets, 3254 cpr->rx_bytes, 3255 &dim_sample); 3256 net_dim(&cpr->dim, &dim_sample); 3257 } 3258 return work_done; 3259 } 3260 3261 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3262 { 3263 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3264 int i, work_done = 0; 3265 3266 for (i = 0; i < cpr->cp_ring_count; i++) { 3267 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3268 3269 if (cpr2->had_nqe_notify) { 3270 work_done += __bnxt_poll_work(bp, cpr2, 3271 budget - work_done); 3272 cpr->has_more_work |= cpr2->has_more_work; 3273 } 3274 } 3275 return work_done; 3276 } 3277 3278 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3279 u64 dbr_type, int budget) 3280 { 3281 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3282 int i; 3283 3284 for (i = 0; i < cpr->cp_ring_count; i++) { 3285 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3286 struct bnxt_db_info *db; 3287 3288 if (cpr2->had_work_done) { 3289 u32 tgl = 0; 3290 3291 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3292 cpr2->had_nqe_notify = 0; 3293 tgl = cpr2->toggle; 3294 } 3295 db = &cpr2->cp_db; 3296 bnxt_writeq(bp, 3297 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3298 DB_RING_IDX(db, cpr2->cp_raw_cons), 3299 db->doorbell); 3300 cpr2->had_work_done = 0; 3301 } 3302 } 3303 __bnxt_poll_work_done(bp, bnapi, budget); 3304 } 3305 3306 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3307 { 3308 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3309 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3310 struct bnxt_cp_ring_info *cpr_rx; 3311 u32 raw_cons = cpr->cp_raw_cons; 3312 struct bnxt *bp = bnapi->bp; 3313 struct nqe_cn *nqcmp; 3314 int work_done = 0; 3315 u32 cons; 3316 3317 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3318 napi_complete(napi); 3319 return 0; 3320 } 3321 if (cpr->has_more_work) { 3322 cpr->has_more_work = 0; 3323 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3324 } 3325 while (1) { 3326 u16 type; 3327 3328 cons = RING_CMP(raw_cons); 3329 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3330 3331 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3332 if (cpr->has_more_work) 3333 break; 3334 3335 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3336 budget); 3337 cpr->cp_raw_cons = raw_cons; 3338 if (napi_complete_done(napi, work_done)) 3339 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3340 cpr->cp_raw_cons); 3341 goto poll_done; 3342 } 3343 3344 /* The valid test of the entry must be done first before 3345 * reading any further. 3346 */ 3347 dma_rmb(); 3348 3349 type = le16_to_cpu(nqcmp->type); 3350 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3351 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3352 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3353 struct bnxt_cp_ring_info *cpr2; 3354 3355 /* No more budget for RX work */ 3356 if (budget && work_done >= budget && 3357 cq_type == BNXT_NQ_HDL_TYPE_RX) 3358 break; 3359 3360 idx = BNXT_NQ_HDL_IDX(idx); 3361 cpr2 = &cpr->cp_ring_arr[idx]; 3362 cpr2->had_nqe_notify = 1; 3363 cpr2->toggle = NQE_CN_TOGGLE(type); 3364 work_done += __bnxt_poll_work(bp, cpr2, 3365 budget - work_done); 3366 cpr->has_more_work |= cpr2->has_more_work; 3367 } else { 3368 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3369 } 3370 raw_cons = NEXT_RAW_CMP(raw_cons); 3371 } 3372 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3373 if (raw_cons != cpr->cp_raw_cons) { 3374 cpr->cp_raw_cons = raw_cons; 3375 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3376 } 3377 poll_done: 3378 cpr_rx = &cpr->cp_ring_arr[0]; 3379 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3380 (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) { 3381 struct dim_sample dim_sample = {}; 3382 3383 dim_update_sample(cpr->event_ctr, 3384 cpr_rx->rx_packets, 3385 cpr_rx->rx_bytes, 3386 &dim_sample); 3387 net_dim(&cpr->dim, &dim_sample); 3388 } 3389 return work_done; 3390 } 3391 3392 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp, 3393 struct bnxt_tx_ring_info *txr, int idx) 3394 { 3395 int i, max_idx; 3396 struct pci_dev *pdev = bp->pdev; 3397 3398 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3399 3400 for (i = 0; i < max_idx;) { 3401 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i]; 3402 struct sk_buff *skb; 3403 int j, last; 3404 3405 if (idx < bp->tx_nr_rings_xdp && 3406 tx_buf->action == XDP_REDIRECT) { 3407 dma_unmap_single(&pdev->dev, 3408 dma_unmap_addr(tx_buf, mapping), 3409 dma_unmap_len(tx_buf, len), 3410 DMA_TO_DEVICE); 3411 xdp_return_frame(tx_buf->xdpf); 3412 tx_buf->action = 0; 3413 tx_buf->xdpf = NULL; 3414 i++; 3415 continue; 3416 } 3417 3418 skb = tx_buf->skb; 3419 if (!skb) { 3420 i++; 3421 continue; 3422 } 3423 3424 tx_buf->skb = NULL; 3425 3426 if (tx_buf->is_push) { 3427 dev_kfree_skb(skb); 3428 i += 2; 3429 continue; 3430 } 3431 3432 dma_unmap_single(&pdev->dev, 3433 dma_unmap_addr(tx_buf, mapping), 3434 skb_headlen(skb), 3435 DMA_TO_DEVICE); 3436 3437 last = tx_buf->nr_frags; 3438 i += 2; 3439 for (j = 0; j < last; j++, i++) { 3440 int ring_idx = i & bp->tx_ring_mask; 3441 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 3442 3443 tx_buf = &txr->tx_buf_ring[ring_idx]; 3444 netmem_dma_unmap_page_attrs(&pdev->dev, 3445 dma_unmap_addr(tx_buf, 3446 mapping), 3447 skb_frag_size(frag), 3448 DMA_TO_DEVICE, 0); 3449 } 3450 dev_kfree_skb(skb); 3451 } 3452 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx)); 3453 } 3454 3455 static void bnxt_free_tx_skbs(struct bnxt *bp) 3456 { 3457 int i; 3458 3459 if (!bp->tx_ring) 3460 return; 3461 3462 for (i = 0; i < bp->tx_nr_rings; i++) { 3463 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3464 3465 if (!txr->tx_buf_ring) 3466 continue; 3467 3468 bnxt_free_one_tx_ring_skbs(bp, txr, i); 3469 } 3470 3471 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 3472 bnxt_ptp_free_txts_skbs(bp->ptp_cfg); 3473 } 3474 3475 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3476 { 3477 int i, max_idx; 3478 3479 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3480 3481 for (i = 0; i < max_idx; i++) { 3482 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3483 void *data = rx_buf->data; 3484 3485 if (!data) 3486 continue; 3487 3488 rx_buf->data = NULL; 3489 if (BNXT_RX_PAGE_MODE(bp)) 3490 page_pool_recycle_direct(rxr->page_pool, data); 3491 else 3492 page_pool_free_va(rxr->head_pool, data, true); 3493 } 3494 } 3495 3496 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3497 { 3498 int i, max_idx; 3499 3500 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3501 3502 for (i = 0; i < max_idx; i++) { 3503 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3504 netmem_ref netmem = rx_agg_buf->netmem; 3505 3506 if (!netmem) 3507 continue; 3508 3509 rx_agg_buf->netmem = 0; 3510 __clear_bit(i, rxr->rx_agg_bmap); 3511 3512 page_pool_recycle_direct_netmem(rxr->page_pool, netmem); 3513 } 3514 } 3515 3516 static void bnxt_free_one_tpa_info_data(struct bnxt *bp, 3517 struct bnxt_rx_ring_info *rxr) 3518 { 3519 int i; 3520 3521 for (i = 0; i < bp->max_tpa; i++) { 3522 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3523 u8 *data = tpa_info->data; 3524 3525 if (!data) 3526 continue; 3527 3528 tpa_info->data = NULL; 3529 page_pool_free_va(rxr->head_pool, data, false); 3530 } 3531 } 3532 3533 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, 3534 struct bnxt_rx_ring_info *rxr) 3535 { 3536 struct bnxt_tpa_idx_map *map; 3537 3538 if (!rxr->rx_tpa) 3539 goto skip_rx_tpa_free; 3540 3541 bnxt_free_one_tpa_info_data(bp, rxr); 3542 3543 skip_rx_tpa_free: 3544 if (!rxr->rx_buf_ring) 3545 goto skip_rx_buf_free; 3546 3547 bnxt_free_one_rx_ring(bp, rxr); 3548 3549 skip_rx_buf_free: 3550 if (!rxr->rx_agg_ring) 3551 goto skip_rx_agg_free; 3552 3553 bnxt_free_one_rx_agg_ring(bp, rxr); 3554 3555 skip_rx_agg_free: 3556 map = rxr->rx_tpa_idx_map; 3557 if (map) 3558 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3559 } 3560 3561 static void bnxt_free_rx_skbs(struct bnxt *bp) 3562 { 3563 int i; 3564 3565 if (!bp->rx_ring) 3566 return; 3567 3568 for (i = 0; i < bp->rx_nr_rings; i++) 3569 bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]); 3570 } 3571 3572 static void bnxt_free_skbs(struct bnxt *bp) 3573 { 3574 bnxt_free_tx_skbs(bp); 3575 bnxt_free_rx_skbs(bp); 3576 } 3577 3578 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3579 { 3580 u8 init_val = ctxm->init_value; 3581 u16 offset = ctxm->init_offset; 3582 u8 *p2 = p; 3583 int i; 3584 3585 if (!init_val) 3586 return; 3587 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3588 memset(p, init_val, len); 3589 return; 3590 } 3591 for (i = 0; i < len; i += ctxm->entry_size) 3592 *(p2 + i + offset) = init_val; 3593 } 3594 3595 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem, 3596 void *buf, size_t offset, size_t head, 3597 size_t tail) 3598 { 3599 int i, head_page, start_idx, source_offset; 3600 size_t len, rem_len, total_len, max_bytes; 3601 3602 head_page = head / rmem->page_size; 3603 source_offset = head % rmem->page_size; 3604 total_len = (tail - head) & MAX_CTX_BYTES_MASK; 3605 if (!total_len) 3606 total_len = MAX_CTX_BYTES; 3607 start_idx = head_page % MAX_CTX_PAGES; 3608 max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size - 3609 source_offset; 3610 total_len = min(total_len, max_bytes); 3611 rem_len = total_len; 3612 3613 for (i = start_idx; rem_len; i++, source_offset = 0) { 3614 len = min((size_t)(rmem->page_size - source_offset), rem_len); 3615 if (buf) 3616 memcpy(buf + offset, rmem->pg_arr[i] + source_offset, 3617 len); 3618 offset += len; 3619 rem_len -= len; 3620 } 3621 return total_len; 3622 } 3623 3624 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3625 { 3626 struct pci_dev *pdev = bp->pdev; 3627 int i; 3628 3629 if (!rmem->pg_arr) 3630 goto skip_pages; 3631 3632 for (i = 0; i < rmem->nr_pages; i++) { 3633 if (!rmem->pg_arr[i]) 3634 continue; 3635 3636 dma_free_coherent(&pdev->dev, rmem->page_size, 3637 rmem->pg_arr[i], rmem->dma_arr[i]); 3638 3639 rmem->pg_arr[i] = NULL; 3640 } 3641 skip_pages: 3642 if (rmem->pg_tbl) { 3643 size_t pg_tbl_size = rmem->nr_pages * 8; 3644 3645 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3646 pg_tbl_size = rmem->page_size; 3647 dma_free_coherent(&pdev->dev, pg_tbl_size, 3648 rmem->pg_tbl, rmem->pg_tbl_map); 3649 rmem->pg_tbl = NULL; 3650 } 3651 if (rmem->vmem_size && *rmem->vmem) { 3652 vfree(*rmem->vmem); 3653 *rmem->vmem = NULL; 3654 } 3655 } 3656 3657 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3658 { 3659 struct pci_dev *pdev = bp->pdev; 3660 u64 valid_bit = 0; 3661 int i; 3662 3663 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3664 valid_bit = PTU_PTE_VALID; 3665 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3666 size_t pg_tbl_size = rmem->nr_pages * 8; 3667 3668 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3669 pg_tbl_size = rmem->page_size; 3670 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3671 &rmem->pg_tbl_map, 3672 GFP_KERNEL); 3673 if (!rmem->pg_tbl) 3674 return -ENOMEM; 3675 } 3676 3677 for (i = 0; i < rmem->nr_pages; i++) { 3678 u64 extra_bits = valid_bit; 3679 3680 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3681 rmem->page_size, 3682 &rmem->dma_arr[i], 3683 GFP_KERNEL); 3684 if (!rmem->pg_arr[i]) 3685 return -ENOMEM; 3686 3687 if (rmem->ctx_mem) 3688 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3689 rmem->page_size); 3690 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3691 if (i == rmem->nr_pages - 2 && 3692 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3693 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3694 else if (i == rmem->nr_pages - 1 && 3695 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3696 extra_bits |= PTU_PTE_LAST; 3697 rmem->pg_tbl[i] = 3698 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3699 } 3700 } 3701 3702 if (rmem->vmem_size) { 3703 *rmem->vmem = vzalloc(rmem->vmem_size); 3704 if (!(*rmem->vmem)) 3705 return -ENOMEM; 3706 } 3707 return 0; 3708 } 3709 3710 static void bnxt_free_one_tpa_info(struct bnxt *bp, 3711 struct bnxt_rx_ring_info *rxr) 3712 { 3713 int i; 3714 3715 kfree(rxr->rx_tpa_idx_map); 3716 rxr->rx_tpa_idx_map = NULL; 3717 if (rxr->rx_tpa) { 3718 for (i = 0; i < bp->max_tpa; i++) { 3719 kfree(rxr->rx_tpa[i].agg_arr); 3720 rxr->rx_tpa[i].agg_arr = NULL; 3721 } 3722 } 3723 kfree(rxr->rx_tpa); 3724 rxr->rx_tpa = NULL; 3725 } 3726 3727 static void bnxt_free_tpa_info(struct bnxt *bp) 3728 { 3729 int i; 3730 3731 for (i = 0; i < bp->rx_nr_rings; i++) { 3732 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3733 3734 bnxt_free_one_tpa_info(bp, rxr); 3735 } 3736 } 3737 3738 static int bnxt_alloc_one_tpa_info(struct bnxt *bp, 3739 struct bnxt_rx_ring_info *rxr) 3740 { 3741 struct rx_agg_cmp *agg; 3742 int i; 3743 3744 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3745 GFP_KERNEL); 3746 if (!rxr->rx_tpa) 3747 return -ENOMEM; 3748 3749 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3750 return 0; 3751 for (i = 0; i < bp->max_tpa; i++) { 3752 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3753 if (!agg) 3754 return -ENOMEM; 3755 rxr->rx_tpa[i].agg_arr = agg; 3756 } 3757 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3758 GFP_KERNEL); 3759 if (!rxr->rx_tpa_idx_map) 3760 return -ENOMEM; 3761 3762 return 0; 3763 } 3764 3765 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3766 { 3767 int i, rc; 3768 3769 bp->max_tpa = MAX_TPA; 3770 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3771 if (!bp->max_tpa_v2) 3772 return 0; 3773 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3774 } 3775 3776 for (i = 0; i < bp->rx_nr_rings; i++) { 3777 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3778 3779 rc = bnxt_alloc_one_tpa_info(bp, rxr); 3780 if (rc) 3781 return rc; 3782 } 3783 return 0; 3784 } 3785 3786 static void bnxt_free_rx_rings(struct bnxt *bp) 3787 { 3788 int i; 3789 3790 if (!bp->rx_ring) 3791 return; 3792 3793 bnxt_free_tpa_info(bp); 3794 for (i = 0; i < bp->rx_nr_rings; i++) { 3795 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3796 struct bnxt_ring_struct *ring; 3797 3798 if (rxr->xdp_prog) 3799 bpf_prog_put(rxr->xdp_prog); 3800 3801 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3802 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3803 3804 page_pool_destroy(rxr->page_pool); 3805 page_pool_destroy(rxr->head_pool); 3806 rxr->page_pool = rxr->head_pool = NULL; 3807 3808 kfree(rxr->rx_agg_bmap); 3809 rxr->rx_agg_bmap = NULL; 3810 3811 ring = &rxr->rx_ring_struct; 3812 bnxt_free_ring(bp, &ring->ring_mem); 3813 3814 ring = &rxr->rx_agg_ring_struct; 3815 bnxt_free_ring(bp, &ring->ring_mem); 3816 } 3817 } 3818 3819 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3820 struct bnxt_rx_ring_info *rxr, 3821 int numa_node) 3822 { 3823 const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE; 3824 const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K; 3825 struct page_pool_params pp = { 0 }; 3826 struct page_pool *pool; 3827 3828 pp.pool_size = bp->rx_agg_ring_size / agg_size_fac; 3829 if (BNXT_RX_PAGE_MODE(bp)) 3830 pp.pool_size += bp->rx_ring_size / rx_size_fac; 3831 pp.nid = numa_node; 3832 pp.netdev = bp->dev; 3833 pp.dev = &bp->pdev->dev; 3834 pp.dma_dir = bp->rx_dir; 3835 pp.max_len = PAGE_SIZE; 3836 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | 3837 PP_FLAG_ALLOW_UNREADABLE_NETMEM; 3838 pp.queue_idx = rxr->bnapi->index; 3839 3840 pool = page_pool_create(&pp); 3841 if (IS_ERR(pool)) 3842 return PTR_ERR(pool); 3843 rxr->page_pool = pool; 3844 3845 rxr->need_head_pool = page_pool_is_unreadable(pool); 3846 if (bnxt_separate_head_pool(rxr)) { 3847 pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024); 3848 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3849 pool = page_pool_create(&pp); 3850 if (IS_ERR(pool)) 3851 goto err_destroy_pp; 3852 } else { 3853 page_pool_get(pool); 3854 } 3855 rxr->head_pool = pool; 3856 3857 return 0; 3858 3859 err_destroy_pp: 3860 page_pool_destroy(rxr->page_pool); 3861 rxr->page_pool = NULL; 3862 return PTR_ERR(pool); 3863 } 3864 3865 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr) 3866 { 3867 page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi); 3868 page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi); 3869 } 3870 3871 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3872 { 3873 u16 mem_size; 3874 3875 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3876 mem_size = rxr->rx_agg_bmap_size / 8; 3877 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3878 if (!rxr->rx_agg_bmap) 3879 return -ENOMEM; 3880 3881 return 0; 3882 } 3883 3884 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3885 { 3886 int numa_node = dev_to_node(&bp->pdev->dev); 3887 int i, rc = 0, agg_rings = 0, cpu; 3888 3889 if (!bp->rx_ring) 3890 return -ENOMEM; 3891 3892 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3893 agg_rings = 1; 3894 3895 for (i = 0; i < bp->rx_nr_rings; i++) { 3896 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3897 struct bnxt_ring_struct *ring; 3898 int cpu_node; 3899 3900 ring = &rxr->rx_ring_struct; 3901 3902 cpu = cpumask_local_spread(i, numa_node); 3903 cpu_node = cpu_to_node(cpu); 3904 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3905 i, cpu_node); 3906 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3907 if (rc) 3908 return rc; 3909 bnxt_enable_rx_page_pool(rxr); 3910 3911 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3912 if (rc < 0) 3913 return rc; 3914 3915 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3916 MEM_TYPE_PAGE_POOL, 3917 rxr->page_pool); 3918 if (rc) { 3919 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3920 return rc; 3921 } 3922 3923 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3924 if (rc) 3925 return rc; 3926 3927 ring->grp_idx = i; 3928 if (agg_rings) { 3929 ring = &rxr->rx_agg_ring_struct; 3930 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3931 if (rc) 3932 return rc; 3933 3934 ring->grp_idx = i; 3935 rc = bnxt_alloc_rx_agg_bmap(bp, rxr); 3936 if (rc) 3937 return rc; 3938 } 3939 } 3940 if (bp->flags & BNXT_FLAG_TPA) 3941 rc = bnxt_alloc_tpa_info(bp); 3942 return rc; 3943 } 3944 3945 static void bnxt_free_tx_rings(struct bnxt *bp) 3946 { 3947 int i; 3948 struct pci_dev *pdev = bp->pdev; 3949 3950 if (!bp->tx_ring) 3951 return; 3952 3953 for (i = 0; i < bp->tx_nr_rings; i++) { 3954 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3955 struct bnxt_ring_struct *ring; 3956 3957 if (txr->tx_push) { 3958 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3959 txr->tx_push, txr->tx_push_mapping); 3960 txr->tx_push = NULL; 3961 } 3962 3963 ring = &txr->tx_ring_struct; 3964 3965 bnxt_free_ring(bp, &ring->ring_mem); 3966 } 3967 } 3968 3969 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3970 ((tc) * (bp)->tx_nr_rings_per_tc) 3971 3972 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3973 ((tx) % (bp)->tx_nr_rings_per_tc) 3974 3975 #define BNXT_RING_TO_TC(bp, tx) \ 3976 ((tx) / (bp)->tx_nr_rings_per_tc) 3977 3978 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3979 { 3980 int i, j, rc; 3981 struct pci_dev *pdev = bp->pdev; 3982 3983 bp->tx_push_size = 0; 3984 if (bp->tx_push_thresh) { 3985 int push_size; 3986 3987 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3988 bp->tx_push_thresh); 3989 3990 if (push_size > 256) { 3991 push_size = 0; 3992 bp->tx_push_thresh = 0; 3993 } 3994 3995 bp->tx_push_size = push_size; 3996 } 3997 3998 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3999 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4000 struct bnxt_ring_struct *ring; 4001 u8 qidx; 4002 4003 ring = &txr->tx_ring_struct; 4004 4005 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4006 if (rc) 4007 return rc; 4008 4009 ring->grp_idx = txr->bnapi->index; 4010 if (bp->tx_push_size) { 4011 dma_addr_t mapping; 4012 4013 /* One pre-allocated DMA buffer to backup 4014 * TX push operation 4015 */ 4016 txr->tx_push = dma_alloc_coherent(&pdev->dev, 4017 bp->tx_push_size, 4018 &txr->tx_push_mapping, 4019 GFP_KERNEL); 4020 4021 if (!txr->tx_push) 4022 return -ENOMEM; 4023 4024 mapping = txr->tx_push_mapping + 4025 sizeof(struct tx_push_bd); 4026 txr->data_mapping = cpu_to_le64(mapping); 4027 } 4028 qidx = bp->tc_to_qidx[j]; 4029 ring->queue_id = bp->q_info[qidx].queue_id; 4030 spin_lock_init(&txr->xdp_tx_lock); 4031 if (i < bp->tx_nr_rings_xdp) 4032 continue; 4033 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 4034 j++; 4035 } 4036 return 0; 4037 } 4038 4039 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 4040 { 4041 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4042 4043 kfree(cpr->cp_desc_ring); 4044 cpr->cp_desc_ring = NULL; 4045 ring->ring_mem.pg_arr = NULL; 4046 kfree(cpr->cp_desc_mapping); 4047 cpr->cp_desc_mapping = NULL; 4048 ring->ring_mem.dma_arr = NULL; 4049 } 4050 4051 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 4052 { 4053 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 4054 if (!cpr->cp_desc_ring) 4055 return -ENOMEM; 4056 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 4057 GFP_KERNEL); 4058 if (!cpr->cp_desc_mapping) 4059 return -ENOMEM; 4060 return 0; 4061 } 4062 4063 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 4064 { 4065 int i; 4066 4067 if (!bp->bnapi) 4068 return; 4069 for (i = 0; i < bp->cp_nr_rings; i++) { 4070 struct bnxt_napi *bnapi = bp->bnapi[i]; 4071 4072 if (!bnapi) 4073 continue; 4074 bnxt_free_cp_arrays(&bnapi->cp_ring); 4075 } 4076 } 4077 4078 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 4079 { 4080 int i, n = bp->cp_nr_pages; 4081 4082 for (i = 0; i < bp->cp_nr_rings; i++) { 4083 struct bnxt_napi *bnapi = bp->bnapi[i]; 4084 int rc; 4085 4086 if (!bnapi) 4087 continue; 4088 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 4089 if (rc) 4090 return rc; 4091 } 4092 return 0; 4093 } 4094 4095 static void bnxt_free_cp_rings(struct bnxt *bp) 4096 { 4097 int i; 4098 4099 if (!bp->bnapi) 4100 return; 4101 4102 for (i = 0; i < bp->cp_nr_rings; i++) { 4103 struct bnxt_napi *bnapi = bp->bnapi[i]; 4104 struct bnxt_cp_ring_info *cpr; 4105 struct bnxt_ring_struct *ring; 4106 int j; 4107 4108 if (!bnapi) 4109 continue; 4110 4111 cpr = &bnapi->cp_ring; 4112 ring = &cpr->cp_ring_struct; 4113 4114 bnxt_free_ring(bp, &ring->ring_mem); 4115 4116 if (!cpr->cp_ring_arr) 4117 continue; 4118 4119 for (j = 0; j < cpr->cp_ring_count; j++) { 4120 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4121 4122 ring = &cpr2->cp_ring_struct; 4123 bnxt_free_ring(bp, &ring->ring_mem); 4124 bnxt_free_cp_arrays(cpr2); 4125 } 4126 kfree(cpr->cp_ring_arr); 4127 cpr->cp_ring_arr = NULL; 4128 cpr->cp_ring_count = 0; 4129 } 4130 } 4131 4132 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 4133 struct bnxt_cp_ring_info *cpr) 4134 { 4135 struct bnxt_ring_mem_info *rmem; 4136 struct bnxt_ring_struct *ring; 4137 int rc; 4138 4139 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 4140 if (rc) { 4141 bnxt_free_cp_arrays(cpr); 4142 return -ENOMEM; 4143 } 4144 ring = &cpr->cp_ring_struct; 4145 rmem = &ring->ring_mem; 4146 rmem->nr_pages = bp->cp_nr_pages; 4147 rmem->page_size = HW_CMPD_RING_SIZE; 4148 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4149 rmem->dma_arr = cpr->cp_desc_mapping; 4150 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 4151 rc = bnxt_alloc_ring(bp, rmem); 4152 if (rc) { 4153 bnxt_free_ring(bp, rmem); 4154 bnxt_free_cp_arrays(cpr); 4155 } 4156 return rc; 4157 } 4158 4159 static int bnxt_alloc_cp_rings(struct bnxt *bp) 4160 { 4161 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 4162 int i, j, rc, ulp_msix; 4163 int tcs = bp->num_tc; 4164 4165 if (!tcs) 4166 tcs = 1; 4167 ulp_msix = bnxt_get_ulp_msix_num(bp); 4168 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 4169 struct bnxt_napi *bnapi = bp->bnapi[i]; 4170 struct bnxt_cp_ring_info *cpr, *cpr2; 4171 struct bnxt_ring_struct *ring; 4172 int cp_count = 0, k; 4173 int rx = 0, tx = 0; 4174 4175 if (!bnapi) 4176 continue; 4177 4178 cpr = &bnapi->cp_ring; 4179 cpr->bnapi = bnapi; 4180 ring = &cpr->cp_ring_struct; 4181 4182 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 4183 if (rc) 4184 return rc; 4185 4186 ring->map_idx = ulp_msix + i; 4187 4188 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4189 continue; 4190 4191 if (i < bp->rx_nr_rings) { 4192 cp_count++; 4193 rx = 1; 4194 } 4195 if (i < bp->tx_nr_rings_xdp) { 4196 cp_count++; 4197 tx = 1; 4198 } else if ((sh && i < bp->tx_nr_rings) || 4199 (!sh && i >= bp->rx_nr_rings)) { 4200 cp_count += tcs; 4201 tx = 1; 4202 } 4203 4204 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 4205 GFP_KERNEL); 4206 if (!cpr->cp_ring_arr) 4207 return -ENOMEM; 4208 cpr->cp_ring_count = cp_count; 4209 4210 for (k = 0; k < cp_count; k++) { 4211 cpr2 = &cpr->cp_ring_arr[k]; 4212 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 4213 if (rc) 4214 return rc; 4215 cpr2->bnapi = bnapi; 4216 cpr2->sw_stats = cpr->sw_stats; 4217 cpr2->cp_idx = k; 4218 if (!k && rx) { 4219 bp->rx_ring[i].rx_cpr = cpr2; 4220 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4221 } else { 4222 int n, tc = k - rx; 4223 4224 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4225 bp->tx_ring[n].tx_cpr = cpr2; 4226 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4227 } 4228 } 4229 if (tx) 4230 j++; 4231 } 4232 return 0; 4233 } 4234 4235 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4236 struct bnxt_rx_ring_info *rxr) 4237 { 4238 struct bnxt_ring_mem_info *rmem; 4239 struct bnxt_ring_struct *ring; 4240 4241 ring = &rxr->rx_ring_struct; 4242 rmem = &ring->ring_mem; 4243 rmem->nr_pages = bp->rx_nr_pages; 4244 rmem->page_size = HW_RXBD_RING_SIZE; 4245 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4246 rmem->dma_arr = rxr->rx_desc_mapping; 4247 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4248 rmem->vmem = (void **)&rxr->rx_buf_ring; 4249 4250 ring = &rxr->rx_agg_ring_struct; 4251 rmem = &ring->ring_mem; 4252 rmem->nr_pages = bp->rx_agg_nr_pages; 4253 rmem->page_size = HW_RXBD_RING_SIZE; 4254 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4255 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4256 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4257 rmem->vmem = (void **)&rxr->rx_agg_ring; 4258 } 4259 4260 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4261 struct bnxt_rx_ring_info *rxr) 4262 { 4263 struct bnxt_ring_mem_info *rmem; 4264 struct bnxt_ring_struct *ring; 4265 int i; 4266 4267 rxr->page_pool->p.napi = NULL; 4268 rxr->page_pool = NULL; 4269 rxr->head_pool->p.napi = NULL; 4270 rxr->head_pool = NULL; 4271 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4272 4273 ring = &rxr->rx_ring_struct; 4274 rmem = &ring->ring_mem; 4275 rmem->pg_tbl = NULL; 4276 rmem->pg_tbl_map = 0; 4277 for (i = 0; i < rmem->nr_pages; i++) { 4278 rmem->pg_arr[i] = NULL; 4279 rmem->dma_arr[i] = 0; 4280 } 4281 *rmem->vmem = NULL; 4282 4283 ring = &rxr->rx_agg_ring_struct; 4284 rmem = &ring->ring_mem; 4285 rmem->pg_tbl = NULL; 4286 rmem->pg_tbl_map = 0; 4287 for (i = 0; i < rmem->nr_pages; i++) { 4288 rmem->pg_arr[i] = NULL; 4289 rmem->dma_arr[i] = 0; 4290 } 4291 *rmem->vmem = NULL; 4292 } 4293 4294 static void bnxt_init_ring_struct(struct bnxt *bp) 4295 { 4296 int i, j; 4297 4298 for (i = 0; i < bp->cp_nr_rings; i++) { 4299 struct bnxt_napi *bnapi = bp->bnapi[i]; 4300 struct bnxt_ring_mem_info *rmem; 4301 struct bnxt_cp_ring_info *cpr; 4302 struct bnxt_rx_ring_info *rxr; 4303 struct bnxt_tx_ring_info *txr; 4304 struct bnxt_ring_struct *ring; 4305 4306 if (!bnapi) 4307 continue; 4308 4309 cpr = &bnapi->cp_ring; 4310 ring = &cpr->cp_ring_struct; 4311 rmem = &ring->ring_mem; 4312 rmem->nr_pages = bp->cp_nr_pages; 4313 rmem->page_size = HW_CMPD_RING_SIZE; 4314 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4315 rmem->dma_arr = cpr->cp_desc_mapping; 4316 rmem->vmem_size = 0; 4317 4318 rxr = bnapi->rx_ring; 4319 if (!rxr) 4320 goto skip_rx; 4321 4322 ring = &rxr->rx_ring_struct; 4323 rmem = &ring->ring_mem; 4324 rmem->nr_pages = bp->rx_nr_pages; 4325 rmem->page_size = HW_RXBD_RING_SIZE; 4326 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4327 rmem->dma_arr = rxr->rx_desc_mapping; 4328 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4329 rmem->vmem = (void **)&rxr->rx_buf_ring; 4330 4331 ring = &rxr->rx_agg_ring_struct; 4332 rmem = &ring->ring_mem; 4333 rmem->nr_pages = bp->rx_agg_nr_pages; 4334 rmem->page_size = HW_RXBD_RING_SIZE; 4335 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4336 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4337 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4338 rmem->vmem = (void **)&rxr->rx_agg_ring; 4339 4340 skip_rx: 4341 bnxt_for_each_napi_tx(j, bnapi, txr) { 4342 ring = &txr->tx_ring_struct; 4343 rmem = &ring->ring_mem; 4344 rmem->nr_pages = bp->tx_nr_pages; 4345 rmem->page_size = HW_TXBD_RING_SIZE; 4346 rmem->pg_arr = (void **)txr->tx_desc_ring; 4347 rmem->dma_arr = txr->tx_desc_mapping; 4348 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4349 rmem->vmem = (void **)&txr->tx_buf_ring; 4350 } 4351 } 4352 } 4353 4354 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4355 { 4356 int i; 4357 u32 prod; 4358 struct rx_bd **rx_buf_ring; 4359 4360 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4361 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4362 int j; 4363 struct rx_bd *rxbd; 4364 4365 rxbd = rx_buf_ring[i]; 4366 if (!rxbd) 4367 continue; 4368 4369 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4370 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4371 rxbd->rx_bd_opaque = prod; 4372 } 4373 } 4374 } 4375 4376 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4377 struct bnxt_rx_ring_info *rxr, 4378 int ring_nr) 4379 { 4380 u32 prod; 4381 int i; 4382 4383 prod = rxr->rx_prod; 4384 for (i = 0; i < bp->rx_ring_size; i++) { 4385 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4386 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4387 ring_nr, i, bp->rx_ring_size); 4388 break; 4389 } 4390 prod = NEXT_RX(prod); 4391 } 4392 rxr->rx_prod = prod; 4393 } 4394 4395 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp, 4396 struct bnxt_rx_ring_info *rxr, 4397 int ring_nr) 4398 { 4399 u32 prod; 4400 int i; 4401 4402 prod = rxr->rx_agg_prod; 4403 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4404 if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) { 4405 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4406 ring_nr, i, bp->rx_agg_ring_size); 4407 break; 4408 } 4409 prod = NEXT_RX_AGG(prod); 4410 } 4411 rxr->rx_agg_prod = prod; 4412 } 4413 4414 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp, 4415 struct bnxt_rx_ring_info *rxr) 4416 { 4417 dma_addr_t mapping; 4418 u8 *data; 4419 int i; 4420 4421 for (i = 0; i < bp->max_tpa; i++) { 4422 data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, 4423 GFP_KERNEL); 4424 if (!data) 4425 return -ENOMEM; 4426 4427 rxr->rx_tpa[i].data = data; 4428 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4429 rxr->rx_tpa[i].mapping = mapping; 4430 } 4431 4432 return 0; 4433 } 4434 4435 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4436 { 4437 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4438 int rc; 4439 4440 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4441 4442 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4443 return 0; 4444 4445 bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr); 4446 4447 if (rxr->rx_tpa) { 4448 rc = bnxt_alloc_one_tpa_info_data(bp, rxr); 4449 if (rc) 4450 return rc; 4451 } 4452 return 0; 4453 } 4454 4455 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4456 struct bnxt_rx_ring_info *rxr) 4457 { 4458 struct bnxt_ring_struct *ring; 4459 u32 type; 4460 4461 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4462 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4463 4464 if (NET_IP_ALIGN == 2) 4465 type |= RX_BD_FLAGS_SOP; 4466 4467 ring = &rxr->rx_ring_struct; 4468 bnxt_init_rxbd_pages(ring, type); 4469 ring->fw_ring_id = INVALID_HW_RING_ID; 4470 } 4471 4472 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4473 struct bnxt_rx_ring_info *rxr) 4474 { 4475 struct bnxt_ring_struct *ring; 4476 u32 type; 4477 4478 ring = &rxr->rx_agg_ring_struct; 4479 ring->fw_ring_id = INVALID_HW_RING_ID; 4480 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4481 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4482 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4483 4484 bnxt_init_rxbd_pages(ring, type); 4485 } 4486 } 4487 4488 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4489 { 4490 struct bnxt_rx_ring_info *rxr; 4491 4492 rxr = &bp->rx_ring[ring_nr]; 4493 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4494 4495 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4496 &rxr->bnapi->napi); 4497 4498 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4499 bpf_prog_add(bp->xdp_prog, 1); 4500 rxr->xdp_prog = bp->xdp_prog; 4501 } 4502 4503 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4504 4505 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4506 } 4507 4508 static void bnxt_init_cp_rings(struct bnxt *bp) 4509 { 4510 int i, j; 4511 4512 for (i = 0; i < bp->cp_nr_rings; i++) { 4513 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4514 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4515 4516 ring->fw_ring_id = INVALID_HW_RING_ID; 4517 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4518 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4519 if (!cpr->cp_ring_arr) 4520 continue; 4521 for (j = 0; j < cpr->cp_ring_count; j++) { 4522 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4523 4524 ring = &cpr2->cp_ring_struct; 4525 ring->fw_ring_id = INVALID_HW_RING_ID; 4526 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4527 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4528 } 4529 } 4530 } 4531 4532 static int bnxt_init_rx_rings(struct bnxt *bp) 4533 { 4534 int i, rc = 0; 4535 4536 if (BNXT_RX_PAGE_MODE(bp)) { 4537 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4538 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4539 } else { 4540 bp->rx_offset = BNXT_RX_OFFSET; 4541 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4542 } 4543 4544 for (i = 0; i < bp->rx_nr_rings; i++) { 4545 rc = bnxt_init_one_rx_ring(bp, i); 4546 if (rc) 4547 break; 4548 } 4549 4550 return rc; 4551 } 4552 4553 static int bnxt_init_tx_rings(struct bnxt *bp) 4554 { 4555 u16 i; 4556 4557 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4558 BNXT_MIN_TX_DESC_CNT); 4559 4560 for (i = 0; i < bp->tx_nr_rings; i++) { 4561 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4562 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4563 4564 ring->fw_ring_id = INVALID_HW_RING_ID; 4565 4566 if (i >= bp->tx_nr_rings_xdp) 4567 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4568 NETDEV_QUEUE_TYPE_TX, 4569 &txr->bnapi->napi); 4570 } 4571 4572 return 0; 4573 } 4574 4575 static void bnxt_free_ring_grps(struct bnxt *bp) 4576 { 4577 kfree(bp->grp_info); 4578 bp->grp_info = NULL; 4579 } 4580 4581 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4582 { 4583 int i; 4584 4585 if (irq_re_init) { 4586 bp->grp_info = kcalloc(bp->cp_nr_rings, 4587 sizeof(struct bnxt_ring_grp_info), 4588 GFP_KERNEL); 4589 if (!bp->grp_info) 4590 return -ENOMEM; 4591 } 4592 for (i = 0; i < bp->cp_nr_rings; i++) { 4593 if (irq_re_init) 4594 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4595 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4596 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4597 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4598 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4599 } 4600 return 0; 4601 } 4602 4603 static void bnxt_free_vnics(struct bnxt *bp) 4604 { 4605 kfree(bp->vnic_info); 4606 bp->vnic_info = NULL; 4607 bp->nr_vnics = 0; 4608 } 4609 4610 static int bnxt_alloc_vnics(struct bnxt *bp) 4611 { 4612 int num_vnics = 1; 4613 4614 #ifdef CONFIG_RFS_ACCEL 4615 if (bp->flags & BNXT_FLAG_RFS) { 4616 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4617 num_vnics++; 4618 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4619 num_vnics += bp->rx_nr_rings; 4620 } 4621 #endif 4622 4623 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4624 num_vnics++; 4625 4626 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4627 GFP_KERNEL); 4628 if (!bp->vnic_info) 4629 return -ENOMEM; 4630 4631 bp->nr_vnics = num_vnics; 4632 return 0; 4633 } 4634 4635 static void bnxt_init_vnics(struct bnxt *bp) 4636 { 4637 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4638 int i; 4639 4640 for (i = 0; i < bp->nr_vnics; i++) { 4641 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4642 int j; 4643 4644 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4645 vnic->vnic_id = i; 4646 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4647 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4648 4649 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4650 4651 if (bp->vnic_info[i].rss_hash_key) { 4652 if (i == BNXT_VNIC_DEFAULT) { 4653 u8 *key = (void *)vnic->rss_hash_key; 4654 int k; 4655 4656 if (!bp->rss_hash_key_valid && 4657 !bp->rss_hash_key_updated) { 4658 get_random_bytes(bp->rss_hash_key, 4659 HW_HASH_KEY_SIZE); 4660 bp->rss_hash_key_updated = true; 4661 } 4662 4663 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4664 HW_HASH_KEY_SIZE); 4665 4666 if (!bp->rss_hash_key_updated) 4667 continue; 4668 4669 bp->rss_hash_key_updated = false; 4670 bp->rss_hash_key_valid = true; 4671 4672 bp->toeplitz_prefix = 0; 4673 for (k = 0; k < 8; k++) { 4674 bp->toeplitz_prefix <<= 8; 4675 bp->toeplitz_prefix |= key[k]; 4676 } 4677 } else { 4678 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4679 HW_HASH_KEY_SIZE); 4680 } 4681 } 4682 } 4683 } 4684 4685 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4686 { 4687 int pages; 4688 4689 pages = ring_size / desc_per_pg; 4690 4691 if (!pages) 4692 return 1; 4693 4694 pages++; 4695 4696 while (pages & (pages - 1)) 4697 pages++; 4698 4699 return pages; 4700 } 4701 4702 void bnxt_set_tpa_flags(struct bnxt *bp) 4703 { 4704 bp->flags &= ~BNXT_FLAG_TPA; 4705 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4706 return; 4707 if (bp->dev->features & NETIF_F_LRO) 4708 bp->flags |= BNXT_FLAG_LRO; 4709 else if (bp->dev->features & NETIF_F_GRO_HW) 4710 bp->flags |= BNXT_FLAG_GRO; 4711 } 4712 4713 static void bnxt_init_ring_params(struct bnxt *bp) 4714 { 4715 unsigned int rx_size; 4716 4717 bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK; 4718 /* Try to fit 4 chunks into a 4k page */ 4719 rx_size = SZ_1K - 4720 NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4721 bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size); 4722 } 4723 4724 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4725 * be set on entry. 4726 */ 4727 void bnxt_set_ring_params(struct bnxt *bp) 4728 { 4729 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4730 u32 agg_factor = 0, agg_ring_size = 0; 4731 4732 /* 8 for CRC and VLAN */ 4733 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4734 4735 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4736 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4737 4738 ring_size = bp->rx_ring_size; 4739 bp->rx_agg_ring_size = 0; 4740 bp->rx_agg_nr_pages = 0; 4741 4742 if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS) 4743 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4744 4745 bp->flags &= ~BNXT_FLAG_JUMBO; 4746 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4747 u32 jumbo_factor; 4748 4749 bp->flags |= BNXT_FLAG_JUMBO; 4750 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4751 if (jumbo_factor > agg_factor) 4752 agg_factor = jumbo_factor; 4753 } 4754 if (agg_factor) { 4755 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4756 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4757 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4758 bp->rx_ring_size, ring_size); 4759 bp->rx_ring_size = ring_size; 4760 } 4761 agg_ring_size = ring_size * agg_factor; 4762 4763 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4764 RX_DESC_CNT); 4765 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4766 u32 tmp = agg_ring_size; 4767 4768 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4769 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4770 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4771 tmp, agg_ring_size); 4772 } 4773 bp->rx_agg_ring_size = agg_ring_size; 4774 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4775 4776 if (BNXT_RX_PAGE_MODE(bp)) { 4777 rx_space = PAGE_SIZE; 4778 rx_size = PAGE_SIZE - 4779 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4780 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4781 } else { 4782 rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK, 4783 bp->rx_copybreak, 4784 bp->dev->cfg_pending->hds_thresh); 4785 rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN); 4786 rx_space = rx_size + NET_SKB_PAD + 4787 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4788 } 4789 } 4790 4791 bp->rx_buf_use_size = rx_size; 4792 bp->rx_buf_size = rx_space; 4793 4794 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4795 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4796 4797 ring_size = bp->tx_ring_size; 4798 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4799 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4800 4801 max_rx_cmpl = bp->rx_ring_size; 4802 /* MAX TPA needs to be added because TPA_START completions are 4803 * immediately recycled, so the TPA completions are not bound by 4804 * the RX ring size. 4805 */ 4806 if (bp->flags & BNXT_FLAG_TPA) 4807 max_rx_cmpl += bp->max_tpa; 4808 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4809 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4810 bp->cp_ring_size = ring_size; 4811 4812 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4813 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4814 bp->cp_nr_pages = MAX_CP_PAGES; 4815 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4816 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4817 ring_size, bp->cp_ring_size); 4818 } 4819 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4820 bp->cp_ring_mask = bp->cp_bit - 1; 4821 } 4822 4823 /* Changing allocation mode of RX rings. 4824 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4825 */ 4826 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4827 { 4828 struct net_device *dev = bp->dev; 4829 4830 if (page_mode) { 4831 bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS); 4832 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4833 4834 if (bp->xdp_prog->aux->xdp_has_frags) 4835 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4836 else 4837 dev->max_mtu = 4838 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4839 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4840 bp->flags |= BNXT_FLAG_JUMBO; 4841 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4842 } else { 4843 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4844 bp->rx_skb_func = bnxt_rx_page_skb; 4845 } 4846 bp->rx_dir = DMA_BIDIRECTIONAL; 4847 } else { 4848 dev->max_mtu = bp->max_mtu; 4849 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4850 bp->rx_dir = DMA_FROM_DEVICE; 4851 bp->rx_skb_func = bnxt_rx_skb; 4852 } 4853 } 4854 4855 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4856 { 4857 __bnxt_set_rx_skb_mode(bp, page_mode); 4858 4859 if (!page_mode) { 4860 int rx, tx; 4861 4862 bnxt_get_max_rings(bp, &rx, &tx, true); 4863 if (rx > 1) { 4864 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 4865 bp->dev->hw_features |= NETIF_F_LRO; 4866 } 4867 } 4868 4869 /* Update LRO and GRO_HW availability */ 4870 netdev_update_features(bp->dev); 4871 } 4872 4873 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4874 { 4875 int i; 4876 struct bnxt_vnic_info *vnic; 4877 struct pci_dev *pdev = bp->pdev; 4878 4879 if (!bp->vnic_info) 4880 return; 4881 4882 for (i = 0; i < bp->nr_vnics; i++) { 4883 vnic = &bp->vnic_info[i]; 4884 4885 kfree(vnic->fw_grp_ids); 4886 vnic->fw_grp_ids = NULL; 4887 4888 kfree(vnic->uc_list); 4889 vnic->uc_list = NULL; 4890 4891 if (vnic->mc_list) { 4892 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4893 vnic->mc_list, vnic->mc_list_mapping); 4894 vnic->mc_list = NULL; 4895 } 4896 4897 if (vnic->rss_table) { 4898 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4899 vnic->rss_table, 4900 vnic->rss_table_dma_addr); 4901 vnic->rss_table = NULL; 4902 } 4903 4904 vnic->rss_hash_key = NULL; 4905 vnic->flags = 0; 4906 } 4907 } 4908 4909 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4910 { 4911 int i, rc = 0, size; 4912 struct bnxt_vnic_info *vnic; 4913 struct pci_dev *pdev = bp->pdev; 4914 int max_rings; 4915 4916 for (i = 0; i < bp->nr_vnics; i++) { 4917 vnic = &bp->vnic_info[i]; 4918 4919 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4920 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4921 4922 if (mem_size > 0) { 4923 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4924 if (!vnic->uc_list) { 4925 rc = -ENOMEM; 4926 goto out; 4927 } 4928 } 4929 } 4930 4931 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4932 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4933 vnic->mc_list = 4934 dma_alloc_coherent(&pdev->dev, 4935 vnic->mc_list_size, 4936 &vnic->mc_list_mapping, 4937 GFP_KERNEL); 4938 if (!vnic->mc_list) { 4939 rc = -ENOMEM; 4940 goto out; 4941 } 4942 } 4943 4944 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4945 goto vnic_skip_grps; 4946 4947 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4948 max_rings = bp->rx_nr_rings; 4949 else 4950 max_rings = 1; 4951 4952 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4953 if (!vnic->fw_grp_ids) { 4954 rc = -ENOMEM; 4955 goto out; 4956 } 4957 vnic_skip_grps: 4958 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4959 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4960 continue; 4961 4962 /* Allocate rss table and hash key */ 4963 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4964 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4965 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4966 4967 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4968 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4969 vnic->rss_table_size, 4970 &vnic->rss_table_dma_addr, 4971 GFP_KERNEL); 4972 if (!vnic->rss_table) { 4973 rc = -ENOMEM; 4974 goto out; 4975 } 4976 4977 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4978 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4979 } 4980 return 0; 4981 4982 out: 4983 return rc; 4984 } 4985 4986 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4987 { 4988 struct bnxt_hwrm_wait_token *token; 4989 4990 dma_pool_destroy(bp->hwrm_dma_pool); 4991 bp->hwrm_dma_pool = NULL; 4992 4993 rcu_read_lock(); 4994 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4995 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4996 rcu_read_unlock(); 4997 } 4998 4999 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 5000 { 5001 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 5002 BNXT_HWRM_DMA_SIZE, 5003 BNXT_HWRM_DMA_ALIGN, 0); 5004 if (!bp->hwrm_dma_pool) 5005 return -ENOMEM; 5006 5007 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 5008 5009 return 0; 5010 } 5011 5012 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 5013 { 5014 kfree(stats->hw_masks); 5015 stats->hw_masks = NULL; 5016 kfree(stats->sw_stats); 5017 stats->sw_stats = NULL; 5018 if (stats->hw_stats) { 5019 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 5020 stats->hw_stats_map); 5021 stats->hw_stats = NULL; 5022 } 5023 } 5024 5025 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 5026 bool alloc_masks) 5027 { 5028 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 5029 &stats->hw_stats_map, GFP_KERNEL); 5030 if (!stats->hw_stats) 5031 return -ENOMEM; 5032 5033 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 5034 if (!stats->sw_stats) 5035 goto stats_mem_err; 5036 5037 if (alloc_masks) { 5038 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 5039 if (!stats->hw_masks) 5040 goto stats_mem_err; 5041 } 5042 return 0; 5043 5044 stats_mem_err: 5045 bnxt_free_stats_mem(bp, stats); 5046 return -ENOMEM; 5047 } 5048 5049 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 5050 { 5051 int i; 5052 5053 for (i = 0; i < count; i++) 5054 mask_arr[i] = mask; 5055 } 5056 5057 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 5058 { 5059 int i; 5060 5061 for (i = 0; i < count; i++) 5062 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 5063 } 5064 5065 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 5066 struct bnxt_stats_mem *stats) 5067 { 5068 struct hwrm_func_qstats_ext_output *resp; 5069 struct hwrm_func_qstats_ext_input *req; 5070 __le64 *hw_masks; 5071 int rc; 5072 5073 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 5074 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5075 return -EOPNOTSUPP; 5076 5077 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 5078 if (rc) 5079 return rc; 5080 5081 req->fid = cpu_to_le16(0xffff); 5082 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5083 5084 resp = hwrm_req_hold(bp, req); 5085 rc = hwrm_req_send(bp, req); 5086 if (!rc) { 5087 hw_masks = &resp->rx_ucast_pkts; 5088 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 5089 } 5090 hwrm_req_drop(bp, req); 5091 return rc; 5092 } 5093 5094 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 5095 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 5096 5097 static void bnxt_init_stats(struct bnxt *bp) 5098 { 5099 struct bnxt_napi *bnapi = bp->bnapi[0]; 5100 struct bnxt_cp_ring_info *cpr; 5101 struct bnxt_stats_mem *stats; 5102 __le64 *rx_stats, *tx_stats; 5103 int rc, rx_count, tx_count; 5104 u64 *rx_masks, *tx_masks; 5105 u64 mask; 5106 u8 flags; 5107 5108 cpr = &bnapi->cp_ring; 5109 stats = &cpr->stats; 5110 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 5111 if (rc) { 5112 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5113 mask = (1ULL << 48) - 1; 5114 else 5115 mask = -1ULL; 5116 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 5117 } 5118 if (bp->flags & BNXT_FLAG_PORT_STATS) { 5119 stats = &bp->port_stats; 5120 rx_stats = stats->hw_stats; 5121 rx_masks = stats->hw_masks; 5122 rx_count = sizeof(struct rx_port_stats) / 8; 5123 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5124 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 5125 tx_count = sizeof(struct tx_port_stats) / 8; 5126 5127 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 5128 rc = bnxt_hwrm_port_qstats(bp, flags); 5129 if (rc) { 5130 mask = (1ULL << 40) - 1; 5131 5132 bnxt_fill_masks(rx_masks, mask, rx_count); 5133 bnxt_fill_masks(tx_masks, mask, tx_count); 5134 } else { 5135 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5136 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 5137 bnxt_hwrm_port_qstats(bp, 0); 5138 } 5139 } 5140 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 5141 stats = &bp->rx_port_stats_ext; 5142 rx_stats = stats->hw_stats; 5143 rx_masks = stats->hw_masks; 5144 rx_count = sizeof(struct rx_port_stats_ext) / 8; 5145 stats = &bp->tx_port_stats_ext; 5146 tx_stats = stats->hw_stats; 5147 tx_masks = stats->hw_masks; 5148 tx_count = sizeof(struct tx_port_stats_ext) / 8; 5149 5150 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 5151 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 5152 if (rc) { 5153 mask = (1ULL << 40) - 1; 5154 5155 bnxt_fill_masks(rx_masks, mask, rx_count); 5156 if (tx_stats) 5157 bnxt_fill_masks(tx_masks, mask, tx_count); 5158 } else { 5159 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 5160 if (tx_stats) 5161 bnxt_copy_hw_masks(tx_masks, tx_stats, 5162 tx_count); 5163 bnxt_hwrm_port_qstats_ext(bp, 0); 5164 } 5165 } 5166 } 5167 5168 static void bnxt_free_port_stats(struct bnxt *bp) 5169 { 5170 bp->flags &= ~BNXT_FLAG_PORT_STATS; 5171 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 5172 5173 bnxt_free_stats_mem(bp, &bp->port_stats); 5174 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 5175 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 5176 } 5177 5178 static void bnxt_free_ring_stats(struct bnxt *bp) 5179 { 5180 int i; 5181 5182 if (!bp->bnapi) 5183 return; 5184 5185 for (i = 0; i < bp->cp_nr_rings; i++) { 5186 struct bnxt_napi *bnapi = bp->bnapi[i]; 5187 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5188 5189 bnxt_free_stats_mem(bp, &cpr->stats); 5190 5191 kfree(cpr->sw_stats); 5192 cpr->sw_stats = NULL; 5193 } 5194 } 5195 5196 static int bnxt_alloc_stats(struct bnxt *bp) 5197 { 5198 u32 size, i; 5199 int rc; 5200 5201 size = bp->hw_ring_stats_size; 5202 5203 for (i = 0; i < bp->cp_nr_rings; i++) { 5204 struct bnxt_napi *bnapi = bp->bnapi[i]; 5205 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5206 5207 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 5208 if (!cpr->sw_stats) 5209 return -ENOMEM; 5210 5211 cpr->stats.len = size; 5212 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 5213 if (rc) 5214 return rc; 5215 5216 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 5217 } 5218 5219 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 5220 return 0; 5221 5222 if (bp->port_stats.hw_stats) 5223 goto alloc_ext_stats; 5224 5225 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 5226 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 5227 if (rc) 5228 return rc; 5229 5230 bp->flags |= BNXT_FLAG_PORT_STATS; 5231 5232 alloc_ext_stats: 5233 /* Display extended statistics only if FW supports it */ 5234 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 5235 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 5236 return 0; 5237 5238 if (bp->rx_port_stats_ext.hw_stats) 5239 goto alloc_tx_ext_stats; 5240 5241 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 5242 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 5243 /* Extended stats are optional */ 5244 if (rc) 5245 return 0; 5246 5247 alloc_tx_ext_stats: 5248 if (bp->tx_port_stats_ext.hw_stats) 5249 return 0; 5250 5251 if (bp->hwrm_spec_code >= 0x10902 || 5252 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 5253 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 5254 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 5255 /* Extended stats are optional */ 5256 if (rc) 5257 return 0; 5258 } 5259 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5260 return 0; 5261 } 5262 5263 static void bnxt_clear_ring_indices(struct bnxt *bp) 5264 { 5265 int i, j; 5266 5267 if (!bp->bnapi) 5268 return; 5269 5270 for (i = 0; i < bp->cp_nr_rings; i++) { 5271 struct bnxt_napi *bnapi = bp->bnapi[i]; 5272 struct bnxt_cp_ring_info *cpr; 5273 struct bnxt_rx_ring_info *rxr; 5274 struct bnxt_tx_ring_info *txr; 5275 5276 if (!bnapi) 5277 continue; 5278 5279 cpr = &bnapi->cp_ring; 5280 cpr->cp_raw_cons = 0; 5281 5282 bnxt_for_each_napi_tx(j, bnapi, txr) { 5283 txr->tx_prod = 0; 5284 txr->tx_cons = 0; 5285 txr->tx_hw_cons = 0; 5286 } 5287 5288 rxr = bnapi->rx_ring; 5289 if (rxr) { 5290 rxr->rx_prod = 0; 5291 rxr->rx_agg_prod = 0; 5292 rxr->rx_sw_agg_prod = 0; 5293 rxr->rx_next_cons = 0; 5294 } 5295 bnapi->events = 0; 5296 } 5297 } 5298 5299 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5300 { 5301 u8 type = fltr->type, flags = fltr->flags; 5302 5303 INIT_LIST_HEAD(&fltr->list); 5304 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5305 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5306 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5307 } 5308 5309 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5310 { 5311 if (!list_empty(&fltr->list)) 5312 list_del_init(&fltr->list); 5313 } 5314 5315 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5316 { 5317 struct bnxt_filter_base *usr_fltr, *tmp; 5318 5319 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5320 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5321 continue; 5322 bnxt_del_one_usr_fltr(bp, usr_fltr); 5323 } 5324 } 5325 5326 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5327 { 5328 hlist_del(&fltr->hash); 5329 bnxt_del_one_usr_fltr(bp, fltr); 5330 if (fltr->flags) { 5331 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5332 bp->ntp_fltr_count--; 5333 } 5334 kfree(fltr); 5335 } 5336 5337 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5338 { 5339 int i; 5340 5341 netdev_assert_locked_or_invisible(bp->dev); 5342 5343 /* Under netdev instance lock and all our NAPIs have been disabled. 5344 * It's safe to delete the hash table. 5345 */ 5346 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5347 struct hlist_head *head; 5348 struct hlist_node *tmp; 5349 struct bnxt_ntuple_filter *fltr; 5350 5351 head = &bp->ntp_fltr_hash_tbl[i]; 5352 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5353 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5354 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5355 !list_empty(&fltr->base.list))) 5356 continue; 5357 bnxt_del_fltr(bp, &fltr->base); 5358 } 5359 } 5360 if (!all) 5361 return; 5362 5363 bitmap_free(bp->ntp_fltr_bmap); 5364 bp->ntp_fltr_bmap = NULL; 5365 bp->ntp_fltr_count = 0; 5366 } 5367 5368 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5369 { 5370 int i, rc = 0; 5371 5372 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5373 return 0; 5374 5375 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5376 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5377 5378 bp->ntp_fltr_count = 0; 5379 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5380 5381 if (!bp->ntp_fltr_bmap) 5382 rc = -ENOMEM; 5383 5384 return rc; 5385 } 5386 5387 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5388 { 5389 int i; 5390 5391 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5392 struct hlist_head *head; 5393 struct hlist_node *tmp; 5394 struct bnxt_l2_filter *fltr; 5395 5396 head = &bp->l2_fltr_hash_tbl[i]; 5397 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5398 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5399 !list_empty(&fltr->base.list))) 5400 continue; 5401 bnxt_del_fltr(bp, &fltr->base); 5402 } 5403 } 5404 } 5405 5406 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5407 { 5408 int i; 5409 5410 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5411 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5412 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5413 } 5414 5415 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5416 { 5417 bnxt_free_vnic_attributes(bp); 5418 bnxt_free_tx_rings(bp); 5419 bnxt_free_rx_rings(bp); 5420 bnxt_free_cp_rings(bp); 5421 bnxt_free_all_cp_arrays(bp); 5422 bnxt_free_ntp_fltrs(bp, false); 5423 bnxt_free_l2_filters(bp, false); 5424 if (irq_re_init) { 5425 bnxt_free_ring_stats(bp); 5426 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5427 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5428 bnxt_free_port_stats(bp); 5429 bnxt_free_ring_grps(bp); 5430 bnxt_free_vnics(bp); 5431 kfree(bp->tx_ring_map); 5432 bp->tx_ring_map = NULL; 5433 kfree(bp->tx_ring); 5434 bp->tx_ring = NULL; 5435 kfree(bp->rx_ring); 5436 bp->rx_ring = NULL; 5437 kfree(bp->bnapi); 5438 bp->bnapi = NULL; 5439 } else { 5440 bnxt_clear_ring_indices(bp); 5441 } 5442 } 5443 5444 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5445 { 5446 int i, j, rc, size, arr_size; 5447 void *bnapi; 5448 5449 if (irq_re_init) { 5450 /* Allocate bnapi mem pointer array and mem block for 5451 * all queues 5452 */ 5453 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5454 bp->cp_nr_rings); 5455 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5456 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5457 if (!bnapi) 5458 return -ENOMEM; 5459 5460 bp->bnapi = bnapi; 5461 bnapi += arr_size; 5462 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5463 bp->bnapi[i] = bnapi; 5464 bp->bnapi[i]->index = i; 5465 bp->bnapi[i]->bp = bp; 5466 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5467 struct bnxt_cp_ring_info *cpr = 5468 &bp->bnapi[i]->cp_ring; 5469 5470 cpr->cp_ring_struct.ring_mem.flags = 5471 BNXT_RMEM_RING_PTE_FLAG; 5472 } 5473 } 5474 5475 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5476 sizeof(struct bnxt_rx_ring_info), 5477 GFP_KERNEL); 5478 if (!bp->rx_ring) 5479 return -ENOMEM; 5480 5481 for (i = 0; i < bp->rx_nr_rings; i++) { 5482 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5483 5484 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5485 rxr->rx_ring_struct.ring_mem.flags = 5486 BNXT_RMEM_RING_PTE_FLAG; 5487 rxr->rx_agg_ring_struct.ring_mem.flags = 5488 BNXT_RMEM_RING_PTE_FLAG; 5489 } else { 5490 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5491 } 5492 rxr->bnapi = bp->bnapi[i]; 5493 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5494 } 5495 5496 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5497 sizeof(struct bnxt_tx_ring_info), 5498 GFP_KERNEL); 5499 if (!bp->tx_ring) 5500 return -ENOMEM; 5501 5502 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5503 GFP_KERNEL); 5504 5505 if (!bp->tx_ring_map) 5506 return -ENOMEM; 5507 5508 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5509 j = 0; 5510 else 5511 j = bp->rx_nr_rings; 5512 5513 for (i = 0; i < bp->tx_nr_rings; i++) { 5514 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5515 struct bnxt_napi *bnapi2; 5516 5517 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5518 txr->tx_ring_struct.ring_mem.flags = 5519 BNXT_RMEM_RING_PTE_FLAG; 5520 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5521 if (i >= bp->tx_nr_rings_xdp) { 5522 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5523 5524 bnapi2 = bp->bnapi[k]; 5525 txr->txq_index = i - bp->tx_nr_rings_xdp; 5526 txr->tx_napi_idx = 5527 BNXT_RING_TO_TC(bp, txr->txq_index); 5528 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5529 bnapi2->tx_int = bnxt_tx_int; 5530 } else { 5531 bnapi2 = bp->bnapi[j]; 5532 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5533 bnapi2->tx_ring[0] = txr; 5534 bnapi2->tx_int = bnxt_tx_int_xdp; 5535 j++; 5536 } 5537 txr->bnapi = bnapi2; 5538 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5539 txr->tx_cpr = &bnapi2->cp_ring; 5540 } 5541 5542 rc = bnxt_alloc_stats(bp); 5543 if (rc) 5544 goto alloc_mem_err; 5545 bnxt_init_stats(bp); 5546 5547 rc = bnxt_alloc_ntp_fltrs(bp); 5548 if (rc) 5549 goto alloc_mem_err; 5550 5551 rc = bnxt_alloc_vnics(bp); 5552 if (rc) 5553 goto alloc_mem_err; 5554 } 5555 5556 rc = bnxt_alloc_all_cp_arrays(bp); 5557 if (rc) 5558 goto alloc_mem_err; 5559 5560 bnxt_init_ring_struct(bp); 5561 5562 rc = bnxt_alloc_rx_rings(bp); 5563 if (rc) 5564 goto alloc_mem_err; 5565 5566 rc = bnxt_alloc_tx_rings(bp); 5567 if (rc) 5568 goto alloc_mem_err; 5569 5570 rc = bnxt_alloc_cp_rings(bp); 5571 if (rc) 5572 goto alloc_mem_err; 5573 5574 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5575 BNXT_VNIC_MCAST_FLAG | 5576 BNXT_VNIC_UCAST_FLAG; 5577 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5578 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5579 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5580 5581 rc = bnxt_alloc_vnic_attributes(bp); 5582 if (rc) 5583 goto alloc_mem_err; 5584 return 0; 5585 5586 alloc_mem_err: 5587 bnxt_free_mem(bp, true); 5588 return rc; 5589 } 5590 5591 static void bnxt_disable_int(struct bnxt *bp) 5592 { 5593 int i; 5594 5595 if (!bp->bnapi) 5596 return; 5597 5598 for (i = 0; i < bp->cp_nr_rings; i++) { 5599 struct bnxt_napi *bnapi = bp->bnapi[i]; 5600 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5601 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5602 5603 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5604 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5605 } 5606 } 5607 5608 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5609 { 5610 struct bnxt_napi *bnapi = bp->bnapi[n]; 5611 struct bnxt_cp_ring_info *cpr; 5612 5613 cpr = &bnapi->cp_ring; 5614 return cpr->cp_ring_struct.map_idx; 5615 } 5616 5617 static void bnxt_disable_int_sync(struct bnxt *bp) 5618 { 5619 int i; 5620 5621 if (!bp->irq_tbl) 5622 return; 5623 5624 atomic_inc(&bp->intr_sem); 5625 5626 bnxt_disable_int(bp); 5627 for (i = 0; i < bp->cp_nr_rings; i++) { 5628 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5629 5630 synchronize_irq(bp->irq_tbl[map_idx].vector); 5631 } 5632 } 5633 5634 static void bnxt_enable_int(struct bnxt *bp) 5635 { 5636 int i; 5637 5638 atomic_set(&bp->intr_sem, 0); 5639 for (i = 0; i < bp->cp_nr_rings; i++) { 5640 struct bnxt_napi *bnapi = bp->bnapi[i]; 5641 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5642 5643 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5644 } 5645 } 5646 5647 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5648 bool async_only) 5649 { 5650 DECLARE_BITMAP(async_events_bmap, 256); 5651 u32 *events = (u32 *)async_events_bmap; 5652 struct hwrm_func_drv_rgtr_output *resp; 5653 struct hwrm_func_drv_rgtr_input *req; 5654 u32 flags; 5655 int rc, i; 5656 5657 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5658 if (rc) 5659 return rc; 5660 5661 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5662 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5663 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5664 5665 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5666 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5667 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5668 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5669 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5670 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5671 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5672 if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2) 5673 flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT; 5674 req->flags = cpu_to_le32(flags); 5675 req->ver_maj_8b = DRV_VER_MAJ; 5676 req->ver_min_8b = DRV_VER_MIN; 5677 req->ver_upd_8b = DRV_VER_UPD; 5678 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5679 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5680 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5681 5682 if (BNXT_PF(bp)) { 5683 u32 data[8]; 5684 int i; 5685 5686 memset(data, 0, sizeof(data)); 5687 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5688 u16 cmd = bnxt_vf_req_snif[i]; 5689 unsigned int bit, idx; 5690 5691 idx = cmd / 32; 5692 bit = cmd % 32; 5693 data[idx] |= 1 << bit; 5694 } 5695 5696 for (i = 0; i < 8; i++) 5697 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5698 5699 req->enables |= 5700 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5701 } 5702 5703 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5704 req->flags |= cpu_to_le32( 5705 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5706 5707 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5708 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5709 u16 event_id = bnxt_async_events_arr[i]; 5710 5711 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5712 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5713 continue; 5714 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5715 !bp->ptp_cfg) 5716 continue; 5717 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5718 } 5719 if (bmap && bmap_size) { 5720 for (i = 0; i < bmap_size; i++) { 5721 if (test_bit(i, bmap)) 5722 __set_bit(i, async_events_bmap); 5723 } 5724 } 5725 for (i = 0; i < 8; i++) 5726 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5727 5728 if (async_only) 5729 req->enables = 5730 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5731 5732 resp = hwrm_req_hold(bp, req); 5733 rc = hwrm_req_send(bp, req); 5734 if (!rc) { 5735 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5736 if (resp->flags & 5737 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5738 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5739 } 5740 hwrm_req_drop(bp, req); 5741 return rc; 5742 } 5743 5744 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5745 { 5746 struct hwrm_func_drv_unrgtr_input *req; 5747 int rc; 5748 5749 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5750 return 0; 5751 5752 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5753 if (rc) 5754 return rc; 5755 return hwrm_req_send(bp, req); 5756 } 5757 5758 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5759 5760 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5761 { 5762 struct hwrm_tunnel_dst_port_free_input *req; 5763 int rc; 5764 5765 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5766 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5767 return 0; 5768 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5769 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5770 return 0; 5771 5772 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5773 if (rc) 5774 return rc; 5775 5776 req->tunnel_type = tunnel_type; 5777 5778 switch (tunnel_type) { 5779 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5780 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5781 bp->vxlan_port = 0; 5782 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5783 break; 5784 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5785 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5786 bp->nge_port = 0; 5787 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5788 break; 5789 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5790 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5791 bp->vxlan_gpe_port = 0; 5792 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5793 break; 5794 default: 5795 break; 5796 } 5797 5798 rc = hwrm_req_send(bp, req); 5799 if (rc) 5800 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5801 rc); 5802 if (bp->flags & BNXT_FLAG_TPA) 5803 bnxt_set_tpa(bp, true); 5804 return rc; 5805 } 5806 5807 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5808 u8 tunnel_type) 5809 { 5810 struct hwrm_tunnel_dst_port_alloc_output *resp; 5811 struct hwrm_tunnel_dst_port_alloc_input *req; 5812 int rc; 5813 5814 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5815 if (rc) 5816 return rc; 5817 5818 req->tunnel_type = tunnel_type; 5819 req->tunnel_dst_port_val = port; 5820 5821 resp = hwrm_req_hold(bp, req); 5822 rc = hwrm_req_send(bp, req); 5823 if (rc) { 5824 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5825 rc); 5826 goto err_out; 5827 } 5828 5829 switch (tunnel_type) { 5830 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5831 bp->vxlan_port = port; 5832 bp->vxlan_fw_dst_port_id = 5833 le16_to_cpu(resp->tunnel_dst_port_id); 5834 break; 5835 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5836 bp->nge_port = port; 5837 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5838 break; 5839 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5840 bp->vxlan_gpe_port = port; 5841 bp->vxlan_gpe_fw_dst_port_id = 5842 le16_to_cpu(resp->tunnel_dst_port_id); 5843 break; 5844 default: 5845 break; 5846 } 5847 if (bp->flags & BNXT_FLAG_TPA) 5848 bnxt_set_tpa(bp, true); 5849 5850 err_out: 5851 hwrm_req_drop(bp, req); 5852 return rc; 5853 } 5854 5855 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5856 { 5857 struct hwrm_cfa_l2_set_rx_mask_input *req; 5858 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5859 int rc; 5860 5861 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5862 if (rc) 5863 return rc; 5864 5865 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5866 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5867 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5868 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5869 } 5870 req->mask = cpu_to_le32(vnic->rx_mask); 5871 return hwrm_req_send_silent(bp, req); 5872 } 5873 5874 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5875 { 5876 if (!atomic_dec_and_test(&fltr->refcnt)) 5877 return; 5878 spin_lock_bh(&bp->ntp_fltr_lock); 5879 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5880 spin_unlock_bh(&bp->ntp_fltr_lock); 5881 return; 5882 } 5883 hlist_del_rcu(&fltr->base.hash); 5884 bnxt_del_one_usr_fltr(bp, &fltr->base); 5885 if (fltr->base.flags) { 5886 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5887 bp->ntp_fltr_count--; 5888 } 5889 spin_unlock_bh(&bp->ntp_fltr_lock); 5890 kfree_rcu(fltr, base.rcu); 5891 } 5892 5893 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5894 struct bnxt_l2_key *key, 5895 u32 idx) 5896 { 5897 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5898 struct bnxt_l2_filter *fltr; 5899 5900 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5901 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5902 5903 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5904 l2_key->vlan == key->vlan) 5905 return fltr; 5906 } 5907 return NULL; 5908 } 5909 5910 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5911 struct bnxt_l2_key *key, 5912 u32 idx) 5913 { 5914 struct bnxt_l2_filter *fltr = NULL; 5915 5916 rcu_read_lock(); 5917 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5918 if (fltr) 5919 atomic_inc(&fltr->refcnt); 5920 rcu_read_unlock(); 5921 return fltr; 5922 } 5923 5924 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5925 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5926 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5927 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5928 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5929 5930 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5931 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5932 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5933 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5934 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5935 5936 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5937 { 5938 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5939 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5940 return sizeof(fkeys->addrs.v4addrs) + 5941 sizeof(fkeys->ports); 5942 5943 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5944 return sizeof(fkeys->addrs.v4addrs); 5945 } 5946 5947 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5948 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5949 return sizeof(fkeys->addrs.v6addrs) + 5950 sizeof(fkeys->ports); 5951 5952 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5953 return sizeof(fkeys->addrs.v6addrs); 5954 } 5955 5956 return 0; 5957 } 5958 5959 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5960 const unsigned char *key) 5961 { 5962 u64 prefix = bp->toeplitz_prefix, hash = 0; 5963 struct bnxt_ipv4_tuple tuple4; 5964 struct bnxt_ipv6_tuple tuple6; 5965 int i, j, len = 0; 5966 u8 *four_tuple; 5967 5968 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5969 if (!len) 5970 return 0; 5971 5972 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5973 tuple4.v4addrs = fkeys->addrs.v4addrs; 5974 tuple4.ports = fkeys->ports; 5975 four_tuple = (unsigned char *)&tuple4; 5976 } else { 5977 tuple6.v6addrs = fkeys->addrs.v6addrs; 5978 tuple6.ports = fkeys->ports; 5979 four_tuple = (unsigned char *)&tuple6; 5980 } 5981 5982 for (i = 0, j = 8; i < len; i++, j++) { 5983 u8 byte = four_tuple[i]; 5984 int bit; 5985 5986 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5987 if (byte & 0x80) 5988 hash ^= prefix; 5989 } 5990 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5991 } 5992 5993 /* The valid part of the hash is in the upper 32 bits. */ 5994 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5995 } 5996 5997 #ifdef CONFIG_RFS_ACCEL 5998 static struct bnxt_l2_filter * 5999 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 6000 { 6001 struct bnxt_l2_filter *fltr; 6002 u32 idx; 6003 6004 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6005 BNXT_L2_FLTR_HASH_MASK; 6006 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6007 return fltr; 6008 } 6009 #endif 6010 6011 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 6012 struct bnxt_l2_key *key, u32 idx) 6013 { 6014 struct hlist_head *head; 6015 6016 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 6017 fltr->l2_key.vlan = key->vlan; 6018 fltr->base.type = BNXT_FLTR_TYPE_L2; 6019 if (fltr->base.flags) { 6020 int bit_id; 6021 6022 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 6023 bp->max_fltr, 0); 6024 if (bit_id < 0) 6025 return -ENOMEM; 6026 fltr->base.sw_id = (u16)bit_id; 6027 bp->ntp_fltr_count++; 6028 } 6029 head = &bp->l2_fltr_hash_tbl[idx]; 6030 hlist_add_head_rcu(&fltr->base.hash, head); 6031 bnxt_insert_usr_fltr(bp, &fltr->base); 6032 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 6033 atomic_set(&fltr->refcnt, 1); 6034 return 0; 6035 } 6036 6037 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 6038 struct bnxt_l2_key *key, 6039 gfp_t gfp) 6040 { 6041 struct bnxt_l2_filter *fltr; 6042 u32 idx; 6043 int rc; 6044 6045 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6046 BNXT_L2_FLTR_HASH_MASK; 6047 fltr = bnxt_lookup_l2_filter(bp, key, idx); 6048 if (fltr) 6049 return fltr; 6050 6051 fltr = kzalloc(sizeof(*fltr), gfp); 6052 if (!fltr) 6053 return ERR_PTR(-ENOMEM); 6054 spin_lock_bh(&bp->ntp_fltr_lock); 6055 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6056 spin_unlock_bh(&bp->ntp_fltr_lock); 6057 if (rc) { 6058 bnxt_del_l2_filter(bp, fltr); 6059 fltr = ERR_PTR(rc); 6060 } 6061 return fltr; 6062 } 6063 6064 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 6065 struct bnxt_l2_key *key, 6066 u16 flags) 6067 { 6068 struct bnxt_l2_filter *fltr; 6069 u32 idx; 6070 int rc; 6071 6072 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 6073 BNXT_L2_FLTR_HASH_MASK; 6074 spin_lock_bh(&bp->ntp_fltr_lock); 6075 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 6076 if (fltr) { 6077 fltr = ERR_PTR(-EEXIST); 6078 goto l2_filter_exit; 6079 } 6080 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 6081 if (!fltr) { 6082 fltr = ERR_PTR(-ENOMEM); 6083 goto l2_filter_exit; 6084 } 6085 fltr->base.flags = flags; 6086 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 6087 if (rc) { 6088 spin_unlock_bh(&bp->ntp_fltr_lock); 6089 bnxt_del_l2_filter(bp, fltr); 6090 return ERR_PTR(rc); 6091 } 6092 6093 l2_filter_exit: 6094 spin_unlock_bh(&bp->ntp_fltr_lock); 6095 return fltr; 6096 } 6097 6098 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 6099 { 6100 #ifdef CONFIG_BNXT_SRIOV 6101 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 6102 6103 return vf->fw_fid; 6104 #else 6105 return INVALID_HW_RING_ID; 6106 #endif 6107 } 6108 6109 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6110 { 6111 struct hwrm_cfa_l2_filter_free_input *req; 6112 u16 target_id = 0xffff; 6113 int rc; 6114 6115 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6116 struct bnxt_pf_info *pf = &bp->pf; 6117 6118 if (fltr->base.vf_idx >= pf->active_vfs) 6119 return -EINVAL; 6120 6121 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6122 if (target_id == INVALID_HW_RING_ID) 6123 return -EINVAL; 6124 } 6125 6126 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 6127 if (rc) 6128 return rc; 6129 6130 req->target_id = cpu_to_le16(target_id); 6131 req->l2_filter_id = fltr->base.filter_id; 6132 return hwrm_req_send(bp, req); 6133 } 6134 6135 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 6136 { 6137 struct hwrm_cfa_l2_filter_alloc_output *resp; 6138 struct hwrm_cfa_l2_filter_alloc_input *req; 6139 u16 target_id = 0xffff; 6140 int rc; 6141 6142 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 6143 struct bnxt_pf_info *pf = &bp->pf; 6144 6145 if (fltr->base.vf_idx >= pf->active_vfs) 6146 return -EINVAL; 6147 6148 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 6149 } 6150 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 6151 if (rc) 6152 return rc; 6153 6154 req->target_id = cpu_to_le16(target_id); 6155 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 6156 6157 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 6158 req->flags |= 6159 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 6160 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 6161 req->enables = 6162 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 6163 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 6164 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 6165 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 6166 eth_broadcast_addr(req->l2_addr_mask); 6167 6168 if (fltr->l2_key.vlan) { 6169 req->enables |= 6170 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 6171 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 6172 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 6173 req->num_vlans = 1; 6174 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 6175 req->l2_ivlan_mask = cpu_to_le16(0xfff); 6176 } 6177 6178 resp = hwrm_req_hold(bp, req); 6179 rc = hwrm_req_send(bp, req); 6180 if (!rc) { 6181 fltr->base.filter_id = resp->l2_filter_id; 6182 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 6183 } 6184 hwrm_req_drop(bp, req); 6185 return rc; 6186 } 6187 6188 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 6189 struct bnxt_ntuple_filter *fltr) 6190 { 6191 struct hwrm_cfa_ntuple_filter_free_input *req; 6192 int rc; 6193 6194 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 6195 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 6196 if (rc) 6197 return rc; 6198 6199 req->ntuple_filter_id = fltr->base.filter_id; 6200 return hwrm_req_send(bp, req); 6201 } 6202 6203 #define BNXT_NTP_FLTR_FLAGS \ 6204 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 6205 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 6206 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 6207 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 6208 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 6209 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 6210 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 6211 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 6212 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 6213 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 6214 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 6215 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 6216 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 6217 6218 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 6219 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 6220 6221 void bnxt_fill_ipv6_mask(__be32 mask[4]) 6222 { 6223 int i; 6224 6225 for (i = 0; i < 4; i++) 6226 mask[i] = cpu_to_be32(~0); 6227 } 6228 6229 static void 6230 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 6231 struct hwrm_cfa_ntuple_filter_alloc_input *req, 6232 struct bnxt_ntuple_filter *fltr) 6233 { 6234 u16 rxq = fltr->base.rxq; 6235 6236 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 6237 struct ethtool_rxfh_context *ctx; 6238 struct bnxt_rss_ctx *rss_ctx; 6239 struct bnxt_vnic_info *vnic; 6240 6241 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 6242 fltr->base.fw_vnic_id); 6243 if (ctx) { 6244 rss_ctx = ethtool_rxfh_context_priv(ctx); 6245 vnic = &rss_ctx->vnic; 6246 6247 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6248 } 6249 return; 6250 } 6251 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 6252 struct bnxt_vnic_info *vnic; 6253 u32 enables; 6254 6255 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 6256 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6257 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 6258 req->enables |= cpu_to_le32(enables); 6259 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6260 } else { 6261 u32 flags; 6262 6263 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6264 req->flags |= cpu_to_le32(flags); 6265 req->dst_id = cpu_to_le16(rxq); 6266 } 6267 } 6268 6269 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6270 struct bnxt_ntuple_filter *fltr) 6271 { 6272 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6273 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6274 struct bnxt_flow_masks *masks = &fltr->fmasks; 6275 struct flow_keys *keys = &fltr->fkeys; 6276 struct bnxt_l2_filter *l2_fltr; 6277 struct bnxt_vnic_info *vnic; 6278 int rc; 6279 6280 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6281 if (rc) 6282 return rc; 6283 6284 l2_fltr = fltr->l2_fltr; 6285 req->l2_filter_id = l2_fltr->base.filter_id; 6286 6287 if (fltr->base.flags & BNXT_ACT_DROP) { 6288 req->flags = 6289 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6290 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6291 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6292 } else { 6293 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6294 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6295 } 6296 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6297 6298 req->ethertype = htons(ETH_P_IP); 6299 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6300 req->ip_protocol = keys->basic.ip_proto; 6301 6302 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6303 req->ethertype = htons(ETH_P_IPV6); 6304 req->ip_addr_type = 6305 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6306 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6307 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6308 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6309 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6310 } else { 6311 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6312 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6313 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6314 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6315 } 6316 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6317 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6318 req->tunnel_type = 6319 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6320 } 6321 6322 req->src_port = keys->ports.src; 6323 req->src_port_mask = masks->ports.src; 6324 req->dst_port = keys->ports.dst; 6325 req->dst_port_mask = masks->ports.dst; 6326 6327 resp = hwrm_req_hold(bp, req); 6328 rc = hwrm_req_send(bp, req); 6329 if (!rc) 6330 fltr->base.filter_id = resp->ntuple_filter_id; 6331 hwrm_req_drop(bp, req); 6332 return rc; 6333 } 6334 6335 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6336 const u8 *mac_addr) 6337 { 6338 struct bnxt_l2_filter *fltr; 6339 struct bnxt_l2_key key; 6340 int rc; 6341 6342 ether_addr_copy(key.dst_mac_addr, mac_addr); 6343 key.vlan = 0; 6344 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6345 if (IS_ERR(fltr)) 6346 return PTR_ERR(fltr); 6347 6348 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6349 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6350 if (rc) 6351 bnxt_del_l2_filter(bp, fltr); 6352 else 6353 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6354 return rc; 6355 } 6356 6357 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6358 { 6359 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6360 6361 /* Any associated ntuple filters will also be cleared by firmware. */ 6362 for (i = 0; i < num_of_vnics; i++) { 6363 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6364 6365 for (j = 0; j < vnic->uc_filter_count; j++) { 6366 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6367 6368 bnxt_hwrm_l2_filter_free(bp, fltr); 6369 bnxt_del_l2_filter(bp, fltr); 6370 } 6371 vnic->uc_filter_count = 0; 6372 } 6373 } 6374 6375 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6376 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6377 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6378 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6379 6380 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6381 struct hwrm_vnic_tpa_cfg_input *req) 6382 { 6383 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6384 6385 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6386 return; 6387 6388 if (bp->vxlan_port) 6389 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6390 if (bp->vxlan_gpe_port) 6391 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6392 if (bp->nge_port) 6393 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6394 6395 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6396 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6397 } 6398 6399 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6400 u32 tpa_flags) 6401 { 6402 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6403 struct hwrm_vnic_tpa_cfg_input *req; 6404 int rc; 6405 6406 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6407 return 0; 6408 6409 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6410 if (rc) 6411 return rc; 6412 6413 if (tpa_flags) { 6414 u16 mss = bp->dev->mtu - 40; 6415 u32 nsegs, n, segs = 0, flags; 6416 6417 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6418 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6419 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6420 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6421 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6422 if (tpa_flags & BNXT_FLAG_GRO) 6423 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6424 6425 req->flags = cpu_to_le32(flags); 6426 6427 req->enables = 6428 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6429 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6430 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6431 6432 /* Number of segs are log2 units, and first packet is not 6433 * included as part of this units. 6434 */ 6435 if (mss <= BNXT_RX_PAGE_SIZE) { 6436 n = BNXT_RX_PAGE_SIZE / mss; 6437 nsegs = (MAX_SKB_FRAGS - 1) * n; 6438 } else { 6439 n = mss / BNXT_RX_PAGE_SIZE; 6440 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6441 n++; 6442 nsegs = (MAX_SKB_FRAGS - n) / n; 6443 } 6444 6445 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6446 segs = MAX_TPA_SEGS_P5; 6447 max_aggs = bp->max_tpa; 6448 } else { 6449 segs = ilog2(nsegs); 6450 } 6451 req->max_agg_segs = cpu_to_le16(segs); 6452 req->max_aggs = cpu_to_le16(max_aggs); 6453 6454 req->min_agg_len = cpu_to_le32(512); 6455 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6456 } 6457 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6458 6459 return hwrm_req_send(bp, req); 6460 } 6461 6462 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6463 { 6464 struct bnxt_ring_grp_info *grp_info; 6465 6466 grp_info = &bp->grp_info[ring->grp_idx]; 6467 return grp_info->cp_fw_ring_id; 6468 } 6469 6470 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6471 { 6472 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6473 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6474 else 6475 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6476 } 6477 6478 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6479 { 6480 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6481 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6482 else 6483 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6484 } 6485 6486 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6487 { 6488 int entries; 6489 6490 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6491 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6492 else 6493 entries = HW_HASH_INDEX_SIZE; 6494 6495 bp->rss_indir_tbl_entries = entries; 6496 bp->rss_indir_tbl = 6497 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6498 if (!bp->rss_indir_tbl) 6499 return -ENOMEM; 6500 6501 return 0; 6502 } 6503 6504 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6505 struct ethtool_rxfh_context *rss_ctx) 6506 { 6507 u16 max_rings, max_entries, pad, i; 6508 u32 *rss_indir_tbl; 6509 6510 if (!bp->rx_nr_rings) 6511 return; 6512 6513 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6514 max_rings = bp->rx_nr_rings - 1; 6515 else 6516 max_rings = bp->rx_nr_rings; 6517 6518 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6519 if (rss_ctx) 6520 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6521 else 6522 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6523 6524 for (i = 0; i < max_entries; i++) 6525 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6526 6527 pad = bp->rss_indir_tbl_entries - max_entries; 6528 if (pad) 6529 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6530 } 6531 6532 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6533 { 6534 u32 i, tbl_size, max_ring = 0; 6535 6536 if (!bp->rss_indir_tbl) 6537 return 0; 6538 6539 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6540 for (i = 0; i < tbl_size; i++) 6541 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6542 return max_ring; 6543 } 6544 6545 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6546 { 6547 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6548 if (!rx_rings) 6549 return 0; 6550 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6551 BNXT_RSS_TABLE_ENTRIES_P5); 6552 } 6553 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6554 return 2; 6555 return 1; 6556 } 6557 6558 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6559 { 6560 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6561 u16 i, j; 6562 6563 /* Fill the RSS indirection table with ring group ids */ 6564 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6565 if (!no_rss) 6566 j = bp->rss_indir_tbl[i]; 6567 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6568 } 6569 } 6570 6571 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6572 struct bnxt_vnic_info *vnic) 6573 { 6574 __le16 *ring_tbl = vnic->rss_table; 6575 struct bnxt_rx_ring_info *rxr; 6576 u16 tbl_size, i; 6577 6578 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6579 6580 for (i = 0; i < tbl_size; i++) { 6581 u16 ring_id, j; 6582 6583 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6584 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6585 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6586 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6587 else 6588 j = bp->rss_indir_tbl[i]; 6589 rxr = &bp->rx_ring[j]; 6590 6591 ring_id = rxr->rx_ring_struct.fw_ring_id; 6592 *ring_tbl++ = cpu_to_le16(ring_id); 6593 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6594 *ring_tbl++ = cpu_to_le16(ring_id); 6595 } 6596 } 6597 6598 static void 6599 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6600 struct bnxt_vnic_info *vnic) 6601 { 6602 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6603 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6604 if (bp->flags & BNXT_FLAG_CHIP_P7) 6605 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6606 } else { 6607 bnxt_fill_hw_rss_tbl(bp, vnic); 6608 } 6609 6610 if (bp->rss_hash_delta) { 6611 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6612 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6613 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6614 else 6615 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6616 } else { 6617 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6618 } 6619 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6620 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6621 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6622 } 6623 6624 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6625 bool set_rss) 6626 { 6627 struct hwrm_vnic_rss_cfg_input *req; 6628 int rc; 6629 6630 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6631 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6632 return 0; 6633 6634 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6635 if (rc) 6636 return rc; 6637 6638 if (set_rss) 6639 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6640 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6641 return hwrm_req_send(bp, req); 6642 } 6643 6644 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6645 struct bnxt_vnic_info *vnic, bool set_rss) 6646 { 6647 struct hwrm_vnic_rss_cfg_input *req; 6648 dma_addr_t ring_tbl_map; 6649 u32 i, nr_ctxs; 6650 int rc; 6651 6652 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6653 if (rc) 6654 return rc; 6655 6656 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6657 if (!set_rss) 6658 return hwrm_req_send(bp, req); 6659 6660 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6661 ring_tbl_map = vnic->rss_table_dma_addr; 6662 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6663 6664 hwrm_req_hold(bp, req); 6665 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6666 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6667 req->ring_table_pair_index = i; 6668 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6669 rc = hwrm_req_send(bp, req); 6670 if (rc) 6671 goto exit; 6672 } 6673 6674 exit: 6675 hwrm_req_drop(bp, req); 6676 return rc; 6677 } 6678 6679 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6680 { 6681 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6682 struct hwrm_vnic_rss_qcfg_output *resp; 6683 struct hwrm_vnic_rss_qcfg_input *req; 6684 6685 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6686 return; 6687 6688 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6689 /* all contexts configured to same hash_type, zero always exists */ 6690 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6691 resp = hwrm_req_hold(bp, req); 6692 if (!hwrm_req_send(bp, req)) { 6693 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6694 bp->rss_hash_delta = 0; 6695 } 6696 hwrm_req_drop(bp, req); 6697 } 6698 6699 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6700 { 6701 u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh; 6702 struct hwrm_vnic_plcmodes_cfg_input *req; 6703 int rc; 6704 6705 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6706 if (rc) 6707 return rc; 6708 6709 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6710 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6711 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6712 6713 if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 6714 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6715 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6716 req->enables |= 6717 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6718 req->hds_threshold = cpu_to_le16(hds_thresh); 6719 } 6720 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6721 return hwrm_req_send(bp, req); 6722 } 6723 6724 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6725 struct bnxt_vnic_info *vnic, 6726 u16 ctx_idx) 6727 { 6728 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6729 6730 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6731 return; 6732 6733 req->rss_cos_lb_ctx_id = 6734 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6735 6736 hwrm_req_send(bp, req); 6737 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6738 } 6739 6740 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6741 { 6742 int i, j; 6743 6744 for (i = 0; i < bp->nr_vnics; i++) { 6745 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6746 6747 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6748 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6749 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6750 } 6751 } 6752 bp->rsscos_nr_ctxs = 0; 6753 } 6754 6755 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6756 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6757 { 6758 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6759 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6760 int rc; 6761 6762 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6763 if (rc) 6764 return rc; 6765 6766 resp = hwrm_req_hold(bp, req); 6767 rc = hwrm_req_send(bp, req); 6768 if (!rc) 6769 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6770 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6771 hwrm_req_drop(bp, req); 6772 6773 return rc; 6774 } 6775 6776 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6777 { 6778 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6779 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6780 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6781 } 6782 6783 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6784 { 6785 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6786 struct hwrm_vnic_cfg_input *req; 6787 unsigned int ring = 0, grp_idx; 6788 u16 def_vlan = 0; 6789 int rc; 6790 6791 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6792 if (rc) 6793 return rc; 6794 6795 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6796 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6797 6798 req->default_rx_ring_id = 6799 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6800 req->default_cmpl_ring_id = 6801 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6802 req->enables = 6803 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6804 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6805 goto vnic_mru; 6806 } 6807 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6808 /* Only RSS support for now TBD: COS & LB */ 6809 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6810 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6811 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6812 VNIC_CFG_REQ_ENABLES_MRU); 6813 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6814 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6815 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6816 VNIC_CFG_REQ_ENABLES_MRU); 6817 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6818 } else { 6819 req->rss_rule = cpu_to_le16(0xffff); 6820 } 6821 6822 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6823 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6824 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6825 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6826 } else { 6827 req->cos_rule = cpu_to_le16(0xffff); 6828 } 6829 6830 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6831 ring = 0; 6832 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6833 ring = vnic->vnic_id - 1; 6834 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6835 ring = bp->rx_nr_rings - 1; 6836 6837 grp_idx = bp->rx_ring[ring].bnapi->index; 6838 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6839 req->lb_rule = cpu_to_le16(0xffff); 6840 vnic_mru: 6841 vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN; 6842 req->mru = cpu_to_le16(vnic->mru); 6843 6844 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6845 #ifdef CONFIG_BNXT_SRIOV 6846 if (BNXT_VF(bp)) 6847 def_vlan = bp->vf.vlan; 6848 #endif 6849 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6850 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6851 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6852 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6853 6854 return hwrm_req_send(bp, req); 6855 } 6856 6857 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6858 struct bnxt_vnic_info *vnic) 6859 { 6860 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6861 struct hwrm_vnic_free_input *req; 6862 6863 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6864 return; 6865 6866 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6867 6868 hwrm_req_send(bp, req); 6869 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6870 } 6871 } 6872 6873 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6874 { 6875 u16 i; 6876 6877 for (i = 0; i < bp->nr_vnics; i++) 6878 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6879 } 6880 6881 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6882 unsigned int start_rx_ring_idx, 6883 unsigned int nr_rings) 6884 { 6885 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6886 struct hwrm_vnic_alloc_output *resp; 6887 struct hwrm_vnic_alloc_input *req; 6888 int rc; 6889 6890 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6891 if (rc) 6892 return rc; 6893 6894 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6895 goto vnic_no_ring_grps; 6896 6897 /* map ring groups to this vnic */ 6898 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6899 grp_idx = bp->rx_ring[i].bnapi->index; 6900 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6901 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6902 j, nr_rings); 6903 break; 6904 } 6905 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6906 } 6907 6908 vnic_no_ring_grps: 6909 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6910 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6911 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6912 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6913 6914 resp = hwrm_req_hold(bp, req); 6915 rc = hwrm_req_send(bp, req); 6916 if (!rc) 6917 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6918 hwrm_req_drop(bp, req); 6919 return rc; 6920 } 6921 6922 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6923 { 6924 struct hwrm_vnic_qcaps_output *resp; 6925 struct hwrm_vnic_qcaps_input *req; 6926 int rc; 6927 6928 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6929 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6930 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6931 if (bp->hwrm_spec_code < 0x10600) 6932 return 0; 6933 6934 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6935 if (rc) 6936 return rc; 6937 6938 resp = hwrm_req_hold(bp, req); 6939 rc = hwrm_req_send(bp, req); 6940 if (!rc) { 6941 u32 flags = le32_to_cpu(resp->flags); 6942 6943 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6944 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6945 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6946 if (flags & 6947 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6948 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6949 6950 /* Older P5 fw before EXT_HW_STATS support did not set 6951 * VLAN_STRIP_CAP properly. 6952 */ 6953 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6954 (BNXT_CHIP_P5(bp) && 6955 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6956 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6957 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6958 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6959 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6960 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6961 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6962 if (bp->max_tpa_v2) { 6963 if (BNXT_CHIP_P5(bp)) 6964 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6965 else 6966 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6967 } 6968 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6969 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6970 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6971 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6972 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6973 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6974 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6975 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6976 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6977 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6978 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP) 6979 bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP; 6980 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6981 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6982 } 6983 hwrm_req_drop(bp, req); 6984 return rc; 6985 } 6986 6987 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6988 { 6989 struct hwrm_ring_grp_alloc_output *resp; 6990 struct hwrm_ring_grp_alloc_input *req; 6991 int rc; 6992 u16 i; 6993 6994 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6995 return 0; 6996 6997 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6998 if (rc) 6999 return rc; 7000 7001 resp = hwrm_req_hold(bp, req); 7002 for (i = 0; i < bp->rx_nr_rings; i++) { 7003 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 7004 7005 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 7006 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 7007 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 7008 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 7009 7010 rc = hwrm_req_send(bp, req); 7011 7012 if (rc) 7013 break; 7014 7015 bp->grp_info[grp_idx].fw_grp_id = 7016 le32_to_cpu(resp->ring_group_id); 7017 } 7018 hwrm_req_drop(bp, req); 7019 return rc; 7020 } 7021 7022 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 7023 { 7024 struct hwrm_ring_grp_free_input *req; 7025 u16 i; 7026 7027 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7028 return; 7029 7030 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 7031 return; 7032 7033 hwrm_req_hold(bp, req); 7034 for (i = 0; i < bp->cp_nr_rings; i++) { 7035 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 7036 continue; 7037 req->ring_group_id = 7038 cpu_to_le32(bp->grp_info[i].fw_grp_id); 7039 7040 hwrm_req_send(bp, req); 7041 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 7042 } 7043 hwrm_req_drop(bp, req); 7044 } 7045 7046 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type, 7047 struct hwrm_ring_alloc_input *req, 7048 struct bnxt_ring_struct *ring) 7049 { 7050 struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx]; 7051 u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID | 7052 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID; 7053 7054 if (ring_type == HWRM_RING_ALLOC_AGG) { 7055 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 7056 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 7057 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 7058 enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID; 7059 } else { 7060 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 7061 if (NET_IP_ALIGN == 2) 7062 req->flags = 7063 cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD); 7064 } 7065 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7066 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7067 req->enables |= cpu_to_le32(enables); 7068 } 7069 7070 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 7071 struct bnxt_ring_struct *ring, 7072 u32 ring_type, u32 map_index) 7073 { 7074 struct hwrm_ring_alloc_output *resp; 7075 struct hwrm_ring_alloc_input *req; 7076 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 7077 struct bnxt_ring_grp_info *grp_info; 7078 int rc, err = 0; 7079 u16 ring_id; 7080 7081 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 7082 if (rc) 7083 goto exit; 7084 7085 req->enables = 0; 7086 if (rmem->nr_pages > 1) { 7087 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 7088 /* Page size is in log2 units */ 7089 req->page_size = BNXT_PAGE_SHIFT; 7090 req->page_tbl_depth = 1; 7091 } else { 7092 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 7093 } 7094 req->fbo = 0; 7095 /* Association of ring index with doorbell index and MSIX number */ 7096 req->logical_id = cpu_to_le16(map_index); 7097 7098 switch (ring_type) { 7099 case HWRM_RING_ALLOC_TX: { 7100 struct bnxt_tx_ring_info *txr; 7101 u16 flags = 0; 7102 7103 txr = container_of(ring, struct bnxt_tx_ring_info, 7104 tx_ring_struct); 7105 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 7106 /* Association of transmit ring with completion ring */ 7107 grp_info = &bp->grp_info[ring->grp_idx]; 7108 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 7109 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 7110 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 7111 req->queue_id = cpu_to_le16(ring->queue_id); 7112 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 7113 req->cmpl_coal_cnt = 7114 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 7115 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 7116 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 7117 req->flags = cpu_to_le16(flags); 7118 break; 7119 } 7120 case HWRM_RING_ALLOC_RX: 7121 case HWRM_RING_ALLOC_AGG: 7122 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 7123 req->length = (ring_type == HWRM_RING_ALLOC_RX) ? 7124 cpu_to_le32(bp->rx_ring_mask + 1) : 7125 cpu_to_le32(bp->rx_agg_ring_mask + 1); 7126 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7127 bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring); 7128 break; 7129 case HWRM_RING_ALLOC_CMPL: 7130 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 7131 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7132 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7133 /* Association of cp ring with nq */ 7134 grp_info = &bp->grp_info[map_index]; 7135 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 7136 req->cq_handle = cpu_to_le64(ring->handle); 7137 req->enables |= cpu_to_le32( 7138 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 7139 } else { 7140 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7141 } 7142 break; 7143 case HWRM_RING_ALLOC_NQ: 7144 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 7145 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 7146 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 7147 break; 7148 default: 7149 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 7150 ring_type); 7151 return -EINVAL; 7152 } 7153 7154 resp = hwrm_req_hold(bp, req); 7155 rc = hwrm_req_send(bp, req); 7156 err = le16_to_cpu(resp->error_code); 7157 ring_id = le16_to_cpu(resp->ring_id); 7158 hwrm_req_drop(bp, req); 7159 7160 exit: 7161 if (rc || err) { 7162 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 7163 ring_type, rc, err); 7164 return -EIO; 7165 } 7166 ring->fw_ring_id = ring_id; 7167 return rc; 7168 } 7169 7170 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 7171 { 7172 int rc; 7173 7174 if (BNXT_PF(bp)) { 7175 struct hwrm_func_cfg_input *req; 7176 7177 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 7178 if (rc) 7179 return rc; 7180 7181 req->fid = cpu_to_le16(0xffff); 7182 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7183 req->async_event_cr = cpu_to_le16(idx); 7184 return hwrm_req_send(bp, req); 7185 } else { 7186 struct hwrm_func_vf_cfg_input *req; 7187 7188 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 7189 if (rc) 7190 return rc; 7191 7192 req->enables = 7193 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 7194 req->async_event_cr = cpu_to_le16(idx); 7195 return hwrm_req_send(bp, req); 7196 } 7197 } 7198 7199 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 7200 u32 ring_type) 7201 { 7202 switch (ring_type) { 7203 case HWRM_RING_ALLOC_TX: 7204 db->db_ring_mask = bp->tx_ring_mask; 7205 break; 7206 case HWRM_RING_ALLOC_RX: 7207 db->db_ring_mask = bp->rx_ring_mask; 7208 break; 7209 case HWRM_RING_ALLOC_AGG: 7210 db->db_ring_mask = bp->rx_agg_ring_mask; 7211 break; 7212 case HWRM_RING_ALLOC_CMPL: 7213 case HWRM_RING_ALLOC_NQ: 7214 db->db_ring_mask = bp->cp_ring_mask; 7215 break; 7216 } 7217 if (bp->flags & BNXT_FLAG_CHIP_P7) { 7218 db->db_epoch_mask = db->db_ring_mask + 1; 7219 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 7220 } 7221 } 7222 7223 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 7224 u32 map_idx, u32 xid) 7225 { 7226 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7227 switch (ring_type) { 7228 case HWRM_RING_ALLOC_TX: 7229 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 7230 break; 7231 case HWRM_RING_ALLOC_RX: 7232 case HWRM_RING_ALLOC_AGG: 7233 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 7234 break; 7235 case HWRM_RING_ALLOC_CMPL: 7236 db->db_key64 = DBR_PATH_L2; 7237 break; 7238 case HWRM_RING_ALLOC_NQ: 7239 db->db_key64 = DBR_PATH_L2; 7240 break; 7241 } 7242 db->db_key64 |= (u64)xid << DBR_XID_SFT; 7243 7244 if (bp->flags & BNXT_FLAG_CHIP_P7) 7245 db->db_key64 |= DBR_VALID; 7246 7247 db->doorbell = bp->bar1 + bp->db_offset; 7248 } else { 7249 db->doorbell = bp->bar1 + map_idx * 0x80; 7250 switch (ring_type) { 7251 case HWRM_RING_ALLOC_TX: 7252 db->db_key32 = DB_KEY_TX; 7253 break; 7254 case HWRM_RING_ALLOC_RX: 7255 case HWRM_RING_ALLOC_AGG: 7256 db->db_key32 = DB_KEY_RX; 7257 break; 7258 case HWRM_RING_ALLOC_CMPL: 7259 db->db_key32 = DB_KEY_CP; 7260 break; 7261 } 7262 } 7263 bnxt_set_db_mask(bp, db, ring_type); 7264 } 7265 7266 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7267 struct bnxt_rx_ring_info *rxr) 7268 { 7269 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7270 struct bnxt_napi *bnapi = rxr->bnapi; 7271 u32 type = HWRM_RING_ALLOC_RX; 7272 u32 map_idx = bnapi->index; 7273 int rc; 7274 7275 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7276 if (rc) 7277 return rc; 7278 7279 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7280 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7281 7282 return 0; 7283 } 7284 7285 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7286 struct bnxt_rx_ring_info *rxr) 7287 { 7288 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7289 u32 type = HWRM_RING_ALLOC_AGG; 7290 u32 grp_idx = ring->grp_idx; 7291 u32 map_idx; 7292 int rc; 7293 7294 map_idx = grp_idx + bp->rx_nr_rings; 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_agg_db, type, map_idx, 7300 ring->fw_ring_id); 7301 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7302 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7303 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7304 7305 return 0; 7306 } 7307 7308 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp, 7309 struct bnxt_cp_ring_info *cpr) 7310 { 7311 const u32 type = HWRM_RING_ALLOC_CMPL; 7312 struct bnxt_napi *bnapi = cpr->bnapi; 7313 struct bnxt_ring_struct *ring; 7314 u32 map_idx = bnapi->index; 7315 int rc; 7316 7317 ring = &cpr->cp_ring_struct; 7318 ring->handle = BNXT_SET_NQ_HDL(cpr); 7319 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7320 if (rc) 7321 return rc; 7322 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7323 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7324 return 0; 7325 } 7326 7327 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp, 7328 struct bnxt_tx_ring_info *txr, u32 tx_idx) 7329 { 7330 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7331 const u32 type = HWRM_RING_ALLOC_TX; 7332 int rc; 7333 7334 rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx); 7335 if (rc) 7336 return rc; 7337 bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id); 7338 return 0; 7339 } 7340 7341 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7342 { 7343 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7344 int i, rc = 0; 7345 u32 type; 7346 7347 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7348 type = HWRM_RING_ALLOC_NQ; 7349 else 7350 type = HWRM_RING_ALLOC_CMPL; 7351 for (i = 0; i < bp->cp_nr_rings; i++) { 7352 struct bnxt_napi *bnapi = bp->bnapi[i]; 7353 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7354 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7355 u32 map_idx = ring->map_idx; 7356 unsigned int vector; 7357 7358 vector = bp->irq_tbl[map_idx].vector; 7359 disable_irq_nosync(vector); 7360 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7361 if (rc) { 7362 enable_irq(vector); 7363 goto err_out; 7364 } 7365 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7366 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7367 enable_irq(vector); 7368 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7369 7370 if (!i) { 7371 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7372 if (rc) 7373 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7374 } 7375 } 7376 7377 for (i = 0; i < bp->tx_nr_rings; i++) { 7378 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7379 7380 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7381 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 7382 if (rc) 7383 goto err_out; 7384 } 7385 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i); 7386 if (rc) 7387 goto err_out; 7388 } 7389 7390 for (i = 0; i < bp->rx_nr_rings; i++) { 7391 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7392 7393 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7394 if (rc) 7395 goto err_out; 7396 /* If we have agg rings, post agg buffers first. */ 7397 if (!agg_rings) 7398 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7399 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7400 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 7401 if (rc) 7402 goto err_out; 7403 } 7404 } 7405 7406 if (agg_rings) { 7407 for (i = 0; i < bp->rx_nr_rings; i++) { 7408 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7409 if (rc) 7410 goto err_out; 7411 } 7412 } 7413 err_out: 7414 return rc; 7415 } 7416 7417 static void bnxt_cancel_dim(struct bnxt *bp) 7418 { 7419 int i; 7420 7421 /* DIM work is initialized in bnxt_enable_napi(). Proceed only 7422 * if NAPI is enabled. 7423 */ 7424 if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 7425 return; 7426 7427 /* Make sure NAPI sees that the VNIC is disabled */ 7428 synchronize_net(); 7429 for (i = 0; i < bp->rx_nr_rings; i++) { 7430 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7431 struct bnxt_napi *bnapi = rxr->bnapi; 7432 7433 cancel_work_sync(&bnapi->cp_ring.dim.work); 7434 } 7435 } 7436 7437 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7438 struct bnxt_ring_struct *ring, 7439 u32 ring_type, int cmpl_ring_id) 7440 { 7441 struct hwrm_ring_free_output *resp; 7442 struct hwrm_ring_free_input *req; 7443 u16 error_code = 0; 7444 int rc; 7445 7446 if (BNXT_NO_FW_ACCESS(bp)) 7447 return 0; 7448 7449 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7450 if (rc) 7451 goto exit; 7452 7453 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7454 req->ring_type = ring_type; 7455 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7456 7457 resp = hwrm_req_hold(bp, req); 7458 rc = hwrm_req_send(bp, req); 7459 error_code = le16_to_cpu(resp->error_code); 7460 hwrm_req_drop(bp, req); 7461 exit: 7462 if (rc || error_code) { 7463 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7464 ring_type, rc, error_code); 7465 return -EIO; 7466 } 7467 return 0; 7468 } 7469 7470 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp, 7471 struct bnxt_tx_ring_info *txr, 7472 bool close_path) 7473 { 7474 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7475 u32 cmpl_ring_id; 7476 7477 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7478 return; 7479 7480 cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) : 7481 INVALID_HW_RING_ID; 7482 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX, 7483 cmpl_ring_id); 7484 ring->fw_ring_id = INVALID_HW_RING_ID; 7485 } 7486 7487 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7488 struct bnxt_rx_ring_info *rxr, 7489 bool close_path) 7490 { 7491 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7492 u32 grp_idx = rxr->bnapi->index; 7493 u32 cmpl_ring_id; 7494 7495 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7496 return; 7497 7498 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7499 hwrm_ring_free_send_msg(bp, ring, 7500 RING_FREE_REQ_RING_TYPE_RX, 7501 close_path ? cmpl_ring_id : 7502 INVALID_HW_RING_ID); 7503 ring->fw_ring_id = INVALID_HW_RING_ID; 7504 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7505 } 7506 7507 static void bnxt_hwrm_rx_agg_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_agg_ring_struct; 7512 u32 grp_idx = rxr->bnapi->index; 7513 u32 type, cmpl_ring_id; 7514 7515 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7516 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7517 else 7518 type = RING_FREE_REQ_RING_TYPE_RX; 7519 7520 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7521 return; 7522 7523 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7524 hwrm_ring_free_send_msg(bp, ring, type, 7525 close_path ? cmpl_ring_id : 7526 INVALID_HW_RING_ID); 7527 ring->fw_ring_id = INVALID_HW_RING_ID; 7528 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7529 } 7530 7531 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp, 7532 struct bnxt_cp_ring_info *cpr) 7533 { 7534 struct bnxt_ring_struct *ring; 7535 7536 ring = &cpr->cp_ring_struct; 7537 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7538 return; 7539 7540 hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL, 7541 INVALID_HW_RING_ID); 7542 ring->fw_ring_id = INVALID_HW_RING_ID; 7543 } 7544 7545 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 7546 { 7547 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7548 int i, size = ring->ring_mem.page_size; 7549 7550 cpr->cp_raw_cons = 0; 7551 cpr->toggle = 0; 7552 7553 for (i = 0; i < bp->cp_nr_pages; i++) 7554 if (cpr->cp_desc_ring[i]) 7555 memset(cpr->cp_desc_ring[i], 0, size); 7556 } 7557 7558 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7559 { 7560 u32 type; 7561 int i; 7562 7563 if (!bp->bnapi) 7564 return; 7565 7566 for (i = 0; i < bp->tx_nr_rings; i++) 7567 bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path); 7568 7569 bnxt_cancel_dim(bp); 7570 for (i = 0; i < bp->rx_nr_rings; i++) { 7571 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7572 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7573 } 7574 7575 /* The completion rings are about to be freed. After that the 7576 * IRQ doorbell will not work anymore. So we need to disable 7577 * IRQ here. 7578 */ 7579 bnxt_disable_int_sync(bp); 7580 7581 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7582 type = RING_FREE_REQ_RING_TYPE_NQ; 7583 else 7584 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7585 for (i = 0; i < bp->cp_nr_rings; i++) { 7586 struct bnxt_napi *bnapi = bp->bnapi[i]; 7587 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7588 struct bnxt_ring_struct *ring; 7589 int j; 7590 7591 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) 7592 bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]); 7593 7594 ring = &cpr->cp_ring_struct; 7595 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7596 hwrm_ring_free_send_msg(bp, ring, type, 7597 INVALID_HW_RING_ID); 7598 ring->fw_ring_id = INVALID_HW_RING_ID; 7599 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7600 } 7601 } 7602 } 7603 7604 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7605 bool shared); 7606 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7607 bool shared); 7608 7609 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7610 { 7611 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7612 struct hwrm_func_qcfg_output *resp; 7613 struct hwrm_func_qcfg_input *req; 7614 int rc; 7615 7616 if (bp->hwrm_spec_code < 0x10601) 7617 return 0; 7618 7619 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7620 if (rc) 7621 return rc; 7622 7623 req->fid = cpu_to_le16(0xffff); 7624 resp = hwrm_req_hold(bp, req); 7625 rc = hwrm_req_send(bp, req); 7626 if (rc) { 7627 hwrm_req_drop(bp, req); 7628 return rc; 7629 } 7630 7631 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7632 if (BNXT_NEW_RM(bp)) { 7633 u16 cp, stats; 7634 7635 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7636 hw_resc->resv_hw_ring_grps = 7637 le32_to_cpu(resp->alloc_hw_ring_grps); 7638 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7639 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7640 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7641 stats = le16_to_cpu(resp->alloc_stat_ctx); 7642 hw_resc->resv_irqs = cp; 7643 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7644 int rx = hw_resc->resv_rx_rings; 7645 int tx = hw_resc->resv_tx_rings; 7646 7647 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7648 rx >>= 1; 7649 if (cp < (rx + tx)) { 7650 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7651 if (rc) 7652 goto get_rings_exit; 7653 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7654 rx <<= 1; 7655 hw_resc->resv_rx_rings = rx; 7656 hw_resc->resv_tx_rings = tx; 7657 } 7658 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7659 hw_resc->resv_hw_ring_grps = rx; 7660 } 7661 hw_resc->resv_cp_rings = cp; 7662 hw_resc->resv_stat_ctxs = stats; 7663 } 7664 get_rings_exit: 7665 hwrm_req_drop(bp, req); 7666 return rc; 7667 } 7668 7669 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7670 { 7671 struct hwrm_func_qcfg_output *resp; 7672 struct hwrm_func_qcfg_input *req; 7673 int rc; 7674 7675 if (bp->hwrm_spec_code < 0x10601) 7676 return 0; 7677 7678 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7679 if (rc) 7680 return rc; 7681 7682 req->fid = cpu_to_le16(fid); 7683 resp = hwrm_req_hold(bp, req); 7684 rc = hwrm_req_send(bp, req); 7685 if (!rc) 7686 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7687 7688 hwrm_req_drop(bp, req); 7689 return rc; 7690 } 7691 7692 static bool bnxt_rfs_supported(struct bnxt *bp); 7693 7694 static struct hwrm_func_cfg_input * 7695 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7696 { 7697 struct hwrm_func_cfg_input *req; 7698 u32 enables = 0; 7699 7700 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7701 return NULL; 7702 7703 req->fid = cpu_to_le16(0xffff); 7704 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7705 req->num_tx_rings = cpu_to_le16(hwr->tx); 7706 if (BNXT_NEW_RM(bp)) { 7707 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7708 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7709 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7710 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7711 enables |= hwr->cp_p5 ? 7712 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7713 } else { 7714 enables |= hwr->cp ? 7715 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7716 enables |= hwr->grp ? 7717 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7718 } 7719 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7720 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7721 0; 7722 req->num_rx_rings = cpu_to_le16(hwr->rx); 7723 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7724 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7725 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7726 req->num_msix = cpu_to_le16(hwr->cp); 7727 } else { 7728 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7729 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7730 } 7731 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7732 req->num_vnics = cpu_to_le16(hwr->vnic); 7733 } 7734 req->enables = cpu_to_le32(enables); 7735 return req; 7736 } 7737 7738 static struct hwrm_func_vf_cfg_input * 7739 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7740 { 7741 struct hwrm_func_vf_cfg_input *req; 7742 u32 enables = 0; 7743 7744 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7745 return NULL; 7746 7747 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7748 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7749 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7750 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7751 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7752 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7753 enables |= hwr->cp_p5 ? 7754 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7755 } else { 7756 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7757 enables |= hwr->grp ? 7758 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7759 } 7760 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7761 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7762 7763 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7764 req->num_tx_rings = cpu_to_le16(hwr->tx); 7765 req->num_rx_rings = cpu_to_le16(hwr->rx); 7766 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7767 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7768 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7769 } else { 7770 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7771 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7772 } 7773 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7774 req->num_vnics = cpu_to_le16(hwr->vnic); 7775 7776 req->enables = cpu_to_le32(enables); 7777 return req; 7778 } 7779 7780 static int 7781 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7782 { 7783 struct hwrm_func_cfg_input *req; 7784 int rc; 7785 7786 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7787 if (!req) 7788 return -ENOMEM; 7789 7790 if (!req->enables) { 7791 hwrm_req_drop(bp, req); 7792 return 0; 7793 } 7794 7795 rc = hwrm_req_send(bp, req); 7796 if (rc) 7797 return rc; 7798 7799 if (bp->hwrm_spec_code < 0x10601) 7800 bp->hw_resc.resv_tx_rings = hwr->tx; 7801 7802 return bnxt_hwrm_get_rings(bp); 7803 } 7804 7805 static int 7806 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7807 { 7808 struct hwrm_func_vf_cfg_input *req; 7809 int rc; 7810 7811 if (!BNXT_NEW_RM(bp)) { 7812 bp->hw_resc.resv_tx_rings = hwr->tx; 7813 return 0; 7814 } 7815 7816 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7817 if (!req) 7818 return -ENOMEM; 7819 7820 rc = hwrm_req_send(bp, req); 7821 if (rc) 7822 return rc; 7823 7824 return bnxt_hwrm_get_rings(bp); 7825 } 7826 7827 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7828 { 7829 if (BNXT_PF(bp)) 7830 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7831 else 7832 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7833 } 7834 7835 int bnxt_nq_rings_in_use(struct bnxt *bp) 7836 { 7837 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7838 } 7839 7840 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7841 { 7842 int cp; 7843 7844 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7845 return bnxt_nq_rings_in_use(bp); 7846 7847 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7848 return cp; 7849 } 7850 7851 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7852 { 7853 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7854 } 7855 7856 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7857 { 7858 if (!hwr->grp) 7859 return 0; 7860 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7861 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7862 7863 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7864 rss_ctx *= hwr->vnic; 7865 return rss_ctx; 7866 } 7867 if (BNXT_VF(bp)) 7868 return BNXT_VF_MAX_RSS_CTX; 7869 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7870 return hwr->grp + 1; 7871 return 1; 7872 } 7873 7874 /* Check if a default RSS map needs to be setup. This function is only 7875 * used on older firmware that does not require reserving RX rings. 7876 */ 7877 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7878 { 7879 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7880 7881 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7882 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7883 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7884 if (!netif_is_rxfh_configured(bp->dev)) 7885 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7886 } 7887 } 7888 7889 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7890 { 7891 if (bp->flags & BNXT_FLAG_RFS) { 7892 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7893 return 2 + bp->num_rss_ctx; 7894 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7895 return rx_rings + 1; 7896 } 7897 return 1; 7898 } 7899 7900 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7901 { 7902 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7903 int cp = bnxt_cp_rings_in_use(bp); 7904 int nq = bnxt_nq_rings_in_use(bp); 7905 int rx = bp->rx_nr_rings, stat; 7906 int vnic, grp = rx; 7907 7908 /* Old firmware does not need RX ring reservations but we still 7909 * need to setup a default RSS map when needed. With new firmware 7910 * we go through RX ring reservations first and then set up the 7911 * RSS map for the successfully reserved RX rings when needed. 7912 */ 7913 if (!BNXT_NEW_RM(bp)) 7914 bnxt_check_rss_tbl_no_rmgr(bp); 7915 7916 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7917 bp->hwrm_spec_code >= 0x10601) 7918 return true; 7919 7920 if (!BNXT_NEW_RM(bp)) 7921 return false; 7922 7923 vnic = bnxt_get_total_vnics(bp, rx); 7924 7925 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7926 rx <<= 1; 7927 stat = bnxt_get_func_stat_ctxs(bp); 7928 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7929 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7930 (hw_resc->resv_hw_ring_grps != grp && 7931 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7932 return true; 7933 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7934 hw_resc->resv_irqs != nq) 7935 return true; 7936 return false; 7937 } 7938 7939 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7940 { 7941 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7942 7943 hwr->tx = hw_resc->resv_tx_rings; 7944 if (BNXT_NEW_RM(bp)) { 7945 hwr->rx = hw_resc->resv_rx_rings; 7946 hwr->cp = hw_resc->resv_irqs; 7947 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7948 hwr->cp_p5 = hw_resc->resv_cp_rings; 7949 hwr->grp = hw_resc->resv_hw_ring_grps; 7950 hwr->vnic = hw_resc->resv_vnics; 7951 hwr->stat = hw_resc->resv_stat_ctxs; 7952 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7953 } 7954 } 7955 7956 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7957 { 7958 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7959 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7960 } 7961 7962 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7963 7964 static int __bnxt_reserve_rings(struct bnxt *bp) 7965 { 7966 struct bnxt_hw_rings hwr = {0}; 7967 int rx_rings, old_rx_rings, rc; 7968 int cp = bp->cp_nr_rings; 7969 int ulp_msix = 0; 7970 bool sh = false; 7971 int tx_cp; 7972 7973 if (!bnxt_need_reserve_rings(bp)) 7974 return 0; 7975 7976 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7977 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7978 if (!ulp_msix) 7979 bnxt_set_ulp_stat_ctxs(bp, 0); 7980 7981 if (ulp_msix > bp->ulp_num_msix_want) 7982 ulp_msix = bp->ulp_num_msix_want; 7983 hwr.cp = cp + ulp_msix; 7984 } else { 7985 hwr.cp = bnxt_nq_rings_in_use(bp); 7986 } 7987 7988 hwr.tx = bp->tx_nr_rings; 7989 hwr.rx = bp->rx_nr_rings; 7990 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7991 sh = true; 7992 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7993 hwr.cp_p5 = hwr.rx + hwr.tx; 7994 7995 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7996 7997 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7998 hwr.rx <<= 1; 7999 hwr.grp = bp->rx_nr_rings; 8000 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 8001 hwr.stat = bnxt_get_func_stat_ctxs(bp); 8002 old_rx_rings = bp->hw_resc.resv_rx_rings; 8003 8004 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 8005 if (rc) 8006 return rc; 8007 8008 bnxt_copy_reserved_rings(bp, &hwr); 8009 8010 rx_rings = hwr.rx; 8011 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 8012 if (hwr.rx >= 2) { 8013 rx_rings = hwr.rx >> 1; 8014 } else { 8015 if (netif_running(bp->dev)) 8016 return -ENOMEM; 8017 8018 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 8019 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 8020 bp->dev->hw_features &= ~NETIF_F_LRO; 8021 bp->dev->features &= ~NETIF_F_LRO; 8022 bnxt_set_ring_params(bp); 8023 } 8024 } 8025 rx_rings = min_t(int, rx_rings, hwr.grp); 8026 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 8027 if (bnxt_ulp_registered(bp->edev) && 8028 hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 8029 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 8030 hwr.cp = min_t(int, hwr.cp, hwr.stat); 8031 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 8032 if (bp->flags & BNXT_FLAG_AGG_RINGS) 8033 hwr.rx = rx_rings << 1; 8034 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 8035 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 8036 if (hwr.tx != bp->tx_nr_rings) { 8037 netdev_warn(bp->dev, 8038 "Able to reserve only %d out of %d requested TX rings\n", 8039 hwr.tx, bp->tx_nr_rings); 8040 } 8041 bp->tx_nr_rings = hwr.tx; 8042 8043 /* If we cannot reserve all the RX rings, reset the RSS map only 8044 * if absolutely necessary 8045 */ 8046 if (rx_rings != bp->rx_nr_rings) { 8047 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 8048 rx_rings, bp->rx_nr_rings); 8049 if (netif_is_rxfh_configured(bp->dev) && 8050 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 8051 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 8052 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 8053 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 8054 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 8055 } 8056 } 8057 bp->rx_nr_rings = rx_rings; 8058 bp->cp_nr_rings = hwr.cp; 8059 8060 if (!bnxt_rings_ok(bp, &hwr)) 8061 return -ENOMEM; 8062 8063 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 8064 !netif_is_rxfh_configured(bp->dev)) 8065 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 8066 8067 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 8068 int resv_msix, resv_ctx, ulp_ctxs; 8069 struct bnxt_hw_resc *hw_resc; 8070 8071 hw_resc = &bp->hw_resc; 8072 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 8073 ulp_msix = min_t(int, resv_msix, ulp_msix); 8074 bnxt_set_ulp_msix_num(bp, ulp_msix); 8075 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 8076 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 8077 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 8078 } 8079 8080 return rc; 8081 } 8082 8083 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8084 { 8085 struct hwrm_func_vf_cfg_input *req; 8086 u32 flags; 8087 8088 if (!BNXT_NEW_RM(bp)) 8089 return 0; 8090 8091 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 8092 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 8093 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8094 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8095 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8096 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 8097 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 8098 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8099 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8100 8101 req->flags = cpu_to_le32(flags); 8102 return hwrm_req_send_silent(bp, req); 8103 } 8104 8105 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8106 { 8107 struct hwrm_func_cfg_input *req; 8108 u32 flags; 8109 8110 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 8111 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 8112 if (BNXT_NEW_RM(bp)) { 8113 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 8114 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 8115 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 8116 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 8117 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 8118 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 8119 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 8120 else 8121 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 8122 } 8123 8124 req->flags = cpu_to_le32(flags); 8125 return hwrm_req_send_silent(bp, req); 8126 } 8127 8128 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 8129 { 8130 if (bp->hwrm_spec_code < 0x10801) 8131 return 0; 8132 8133 if (BNXT_PF(bp)) 8134 return bnxt_hwrm_check_pf_rings(bp, hwr); 8135 8136 return bnxt_hwrm_check_vf_rings(bp, hwr); 8137 } 8138 8139 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 8140 { 8141 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8142 struct hwrm_ring_aggint_qcaps_output *resp; 8143 struct hwrm_ring_aggint_qcaps_input *req; 8144 int rc; 8145 8146 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 8147 coal_cap->num_cmpl_dma_aggr_max = 63; 8148 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 8149 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 8150 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 8151 coal_cap->int_lat_tmr_min_max = 65535; 8152 coal_cap->int_lat_tmr_max_max = 65535; 8153 coal_cap->num_cmpl_aggr_int_max = 65535; 8154 coal_cap->timer_units = 80; 8155 8156 if (bp->hwrm_spec_code < 0x10902) 8157 return; 8158 8159 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 8160 return; 8161 8162 resp = hwrm_req_hold(bp, req); 8163 rc = hwrm_req_send_silent(bp, req); 8164 if (!rc) { 8165 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 8166 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 8167 coal_cap->num_cmpl_dma_aggr_max = 8168 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 8169 coal_cap->num_cmpl_dma_aggr_during_int_max = 8170 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 8171 coal_cap->cmpl_aggr_dma_tmr_max = 8172 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 8173 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 8174 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 8175 coal_cap->int_lat_tmr_min_max = 8176 le16_to_cpu(resp->int_lat_tmr_min_max); 8177 coal_cap->int_lat_tmr_max_max = 8178 le16_to_cpu(resp->int_lat_tmr_max_max); 8179 coal_cap->num_cmpl_aggr_int_max = 8180 le16_to_cpu(resp->num_cmpl_aggr_int_max); 8181 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 8182 } 8183 hwrm_req_drop(bp, req); 8184 } 8185 8186 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 8187 { 8188 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8189 8190 return usec * 1000 / coal_cap->timer_units; 8191 } 8192 8193 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 8194 struct bnxt_coal *hw_coal, 8195 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8196 { 8197 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8198 u16 val, tmr, max, flags = hw_coal->flags; 8199 u32 cmpl_params = coal_cap->cmpl_params; 8200 8201 max = hw_coal->bufs_per_record * 128; 8202 if (hw_coal->budget) 8203 max = hw_coal->bufs_per_record * hw_coal->budget; 8204 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 8205 8206 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 8207 req->num_cmpl_aggr_int = cpu_to_le16(val); 8208 8209 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 8210 req->num_cmpl_dma_aggr = cpu_to_le16(val); 8211 8212 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 8213 coal_cap->num_cmpl_dma_aggr_during_int_max); 8214 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 8215 8216 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 8217 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 8218 req->int_lat_tmr_max = cpu_to_le16(tmr); 8219 8220 /* min timer set to 1/2 of interrupt timer */ 8221 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 8222 val = tmr / 2; 8223 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 8224 req->int_lat_tmr_min = cpu_to_le16(val); 8225 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8226 } 8227 8228 /* buf timer set to 1/4 of interrupt timer */ 8229 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 8230 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 8231 8232 if (cmpl_params & 8233 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 8234 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 8235 val = clamp_t(u16, tmr, 1, 8236 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 8237 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 8238 req->enables |= 8239 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 8240 } 8241 8242 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 8243 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 8244 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 8245 req->flags = cpu_to_le16(flags); 8246 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 8247 } 8248 8249 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 8250 struct bnxt_coal *hw_coal) 8251 { 8252 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 8253 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8254 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 8255 u32 nq_params = coal_cap->nq_params; 8256 u16 tmr; 8257 int rc; 8258 8259 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 8260 return 0; 8261 8262 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8263 if (rc) 8264 return rc; 8265 8266 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 8267 req->flags = 8268 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 8269 8270 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 8271 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 8272 req->int_lat_tmr_min = cpu_to_le16(tmr); 8273 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 8274 return hwrm_req_send(bp, req); 8275 } 8276 8277 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 8278 { 8279 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 8280 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8281 struct bnxt_coal coal; 8282 int rc; 8283 8284 /* Tick values in micro seconds. 8285 * 1 coal_buf x bufs_per_record = 1 completion record. 8286 */ 8287 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 8288 8289 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 8290 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 8291 8292 if (!bnapi->rx_ring) 8293 return -ENODEV; 8294 8295 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8296 if (rc) 8297 return rc; 8298 8299 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 8300 8301 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 8302 8303 return hwrm_req_send(bp, req_rx); 8304 } 8305 8306 static int 8307 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8308 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8309 { 8310 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 8311 8312 req->ring_id = cpu_to_le16(ring_id); 8313 return hwrm_req_send(bp, req); 8314 } 8315 8316 static int 8317 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8318 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8319 { 8320 struct bnxt_tx_ring_info *txr; 8321 int i, rc; 8322 8323 bnxt_for_each_napi_tx(i, bnapi, txr) { 8324 u16 ring_id; 8325 8326 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8327 req->ring_id = cpu_to_le16(ring_id); 8328 rc = hwrm_req_send(bp, req); 8329 if (rc) 8330 return rc; 8331 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8332 return 0; 8333 } 8334 return 0; 8335 } 8336 8337 int bnxt_hwrm_set_coal(struct bnxt *bp) 8338 { 8339 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8340 int i, rc; 8341 8342 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8343 if (rc) 8344 return rc; 8345 8346 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8347 if (rc) { 8348 hwrm_req_drop(bp, req_rx); 8349 return rc; 8350 } 8351 8352 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8353 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8354 8355 hwrm_req_hold(bp, req_rx); 8356 hwrm_req_hold(bp, req_tx); 8357 for (i = 0; i < bp->cp_nr_rings; i++) { 8358 struct bnxt_napi *bnapi = bp->bnapi[i]; 8359 struct bnxt_coal *hw_coal; 8360 8361 if (!bnapi->rx_ring) 8362 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8363 else 8364 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8365 if (rc) 8366 break; 8367 8368 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8369 continue; 8370 8371 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8372 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8373 if (rc) 8374 break; 8375 } 8376 if (bnapi->rx_ring) 8377 hw_coal = &bp->rx_coal; 8378 else 8379 hw_coal = &bp->tx_coal; 8380 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8381 } 8382 hwrm_req_drop(bp, req_rx); 8383 hwrm_req_drop(bp, req_tx); 8384 return rc; 8385 } 8386 8387 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8388 { 8389 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8390 struct hwrm_stat_ctx_free_input *req; 8391 int i; 8392 8393 if (!bp->bnapi) 8394 return; 8395 8396 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8397 return; 8398 8399 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8400 return; 8401 if (BNXT_FW_MAJ(bp) <= 20) { 8402 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8403 hwrm_req_drop(bp, req); 8404 return; 8405 } 8406 hwrm_req_hold(bp, req0); 8407 } 8408 hwrm_req_hold(bp, req); 8409 for (i = 0; i < bp->cp_nr_rings; i++) { 8410 struct bnxt_napi *bnapi = bp->bnapi[i]; 8411 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8412 8413 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8414 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8415 if (req0) { 8416 req0->stat_ctx_id = req->stat_ctx_id; 8417 hwrm_req_send(bp, req0); 8418 } 8419 hwrm_req_send(bp, req); 8420 8421 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8422 } 8423 } 8424 hwrm_req_drop(bp, req); 8425 if (req0) 8426 hwrm_req_drop(bp, req0); 8427 } 8428 8429 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8430 { 8431 struct hwrm_stat_ctx_alloc_output *resp; 8432 struct hwrm_stat_ctx_alloc_input *req; 8433 int rc, i; 8434 8435 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8436 return 0; 8437 8438 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8439 if (rc) 8440 return rc; 8441 8442 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8443 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8444 8445 resp = hwrm_req_hold(bp, req); 8446 for (i = 0; i < bp->cp_nr_rings; i++) { 8447 struct bnxt_napi *bnapi = bp->bnapi[i]; 8448 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8449 8450 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8451 8452 rc = hwrm_req_send(bp, req); 8453 if (rc) 8454 break; 8455 8456 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8457 8458 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8459 } 8460 hwrm_req_drop(bp, req); 8461 return rc; 8462 } 8463 8464 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8465 { 8466 struct hwrm_func_qcfg_output *resp; 8467 struct hwrm_func_qcfg_input *req; 8468 u16 flags; 8469 int rc; 8470 8471 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8472 if (rc) 8473 return rc; 8474 8475 req->fid = cpu_to_le16(0xffff); 8476 resp = hwrm_req_hold(bp, req); 8477 rc = hwrm_req_send(bp, req); 8478 if (rc) 8479 goto func_qcfg_exit; 8480 8481 flags = le16_to_cpu(resp->flags); 8482 #ifdef CONFIG_BNXT_SRIOV 8483 if (BNXT_VF(bp)) { 8484 struct bnxt_vf_info *vf = &bp->vf; 8485 8486 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8487 if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF) 8488 vf->flags |= BNXT_VF_TRUST; 8489 else 8490 vf->flags &= ~BNXT_VF_TRUST; 8491 } else { 8492 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8493 } 8494 #endif 8495 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8496 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8497 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8498 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8499 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8500 } 8501 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8502 bp->flags |= BNXT_FLAG_MULTI_HOST; 8503 8504 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8505 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8506 8507 if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV) 8508 bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV; 8509 8510 switch (resp->port_partition_type) { 8511 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8512 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2: 8513 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8514 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8515 bp->port_partition_type = resp->port_partition_type; 8516 break; 8517 } 8518 if (bp->hwrm_spec_code < 0x10707 || 8519 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8520 bp->br_mode = BRIDGE_MODE_VEB; 8521 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8522 bp->br_mode = BRIDGE_MODE_VEPA; 8523 else 8524 bp->br_mode = BRIDGE_MODE_UNDEF; 8525 8526 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8527 if (!bp->max_mtu) 8528 bp->max_mtu = BNXT_MAX_MTU; 8529 8530 if (bp->db_size) 8531 goto func_qcfg_exit; 8532 8533 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8534 if (BNXT_CHIP_P5(bp)) { 8535 if (BNXT_PF(bp)) 8536 bp->db_offset = DB_PF_OFFSET_P5; 8537 else 8538 bp->db_offset = DB_VF_OFFSET_P5; 8539 } 8540 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8541 1024); 8542 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8543 bp->db_size <= bp->db_offset) 8544 bp->db_size = pci_resource_len(bp->pdev, 2); 8545 8546 func_qcfg_exit: 8547 hwrm_req_drop(bp, req); 8548 return rc; 8549 } 8550 8551 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8552 u8 init_val, u8 init_offset, 8553 bool init_mask_set) 8554 { 8555 ctxm->init_value = init_val; 8556 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8557 if (init_mask_set) 8558 ctxm->init_offset = init_offset * 4; 8559 else 8560 ctxm->init_value = 0; 8561 } 8562 8563 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8564 { 8565 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8566 u16 type; 8567 8568 for (type = 0; type < ctx_max; type++) { 8569 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8570 int n = 1; 8571 8572 if (!ctxm->max_entries || ctxm->pg_info) 8573 continue; 8574 8575 if (ctxm->instance_bmap) 8576 n = hweight32(ctxm->instance_bmap); 8577 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8578 if (!ctxm->pg_info) 8579 return -ENOMEM; 8580 } 8581 return 0; 8582 } 8583 8584 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 8585 struct bnxt_ctx_mem_type *ctxm, bool force); 8586 8587 #define BNXT_CTX_INIT_VALID(flags) \ 8588 (!!((flags) & \ 8589 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8590 8591 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8592 { 8593 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8594 struct hwrm_func_backing_store_qcaps_v2_input *req; 8595 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8596 u16 type; 8597 int rc; 8598 8599 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8600 if (rc) 8601 return rc; 8602 8603 if (!ctx) { 8604 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8605 if (!ctx) 8606 return -ENOMEM; 8607 bp->ctx = ctx; 8608 } 8609 8610 resp = hwrm_req_hold(bp, req); 8611 8612 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8613 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8614 u8 init_val, init_off, i; 8615 u32 max_entries; 8616 u16 entry_size; 8617 __le32 *p; 8618 u32 flags; 8619 8620 req->type = cpu_to_le16(type); 8621 rc = hwrm_req_send(bp, req); 8622 if (rc) 8623 goto ctx_done; 8624 flags = le32_to_cpu(resp->flags); 8625 type = le16_to_cpu(resp->next_valid_type); 8626 if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) { 8627 bnxt_free_one_ctx_mem(bp, ctxm, true); 8628 continue; 8629 } 8630 entry_size = le16_to_cpu(resp->entry_size); 8631 max_entries = le32_to_cpu(resp->max_num_entries); 8632 if (ctxm->mem_valid) { 8633 if (!(flags & BNXT_CTX_MEM_PERSIST) || 8634 ctxm->entry_size != entry_size || 8635 ctxm->max_entries != max_entries) 8636 bnxt_free_one_ctx_mem(bp, ctxm, true); 8637 else 8638 continue; 8639 } 8640 ctxm->type = le16_to_cpu(resp->type); 8641 ctxm->entry_size = entry_size; 8642 ctxm->flags = flags; 8643 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8644 ctxm->entry_multiple = resp->entry_multiple; 8645 ctxm->max_entries = max_entries; 8646 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8647 init_val = resp->ctx_init_value; 8648 init_off = resp->ctx_init_offset; 8649 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8650 BNXT_CTX_INIT_VALID(flags)); 8651 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8652 BNXT_MAX_SPLIT_ENTRY); 8653 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8654 i++, p++) 8655 ctxm->split[i] = le32_to_cpu(*p); 8656 } 8657 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8658 8659 ctx_done: 8660 hwrm_req_drop(bp, req); 8661 return rc; 8662 } 8663 8664 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8665 { 8666 struct hwrm_func_backing_store_qcaps_output *resp; 8667 struct hwrm_func_backing_store_qcaps_input *req; 8668 int rc; 8669 8670 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || 8671 (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED)) 8672 return 0; 8673 8674 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8675 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8676 8677 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8678 if (rc) 8679 return rc; 8680 8681 resp = hwrm_req_hold(bp, req); 8682 rc = hwrm_req_send_silent(bp, req); 8683 if (!rc) { 8684 struct bnxt_ctx_mem_type *ctxm; 8685 struct bnxt_ctx_mem_info *ctx; 8686 u8 init_val, init_idx = 0; 8687 u16 init_mask; 8688 8689 ctx = bp->ctx; 8690 if (!ctx) { 8691 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8692 if (!ctx) { 8693 rc = -ENOMEM; 8694 goto ctx_err; 8695 } 8696 bp->ctx = ctx; 8697 } 8698 init_val = resp->ctx_kind_initializer; 8699 init_mask = le16_to_cpu(resp->ctx_init_mask); 8700 8701 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8702 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8703 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8704 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8705 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8706 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8707 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8708 (init_mask & (1 << init_idx++)) != 0); 8709 8710 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8711 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8712 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8713 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8714 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8715 (init_mask & (1 << init_idx++)) != 0); 8716 8717 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8718 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8719 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8720 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8721 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8722 (init_mask & (1 << init_idx++)) != 0); 8723 8724 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8725 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8726 ctxm->max_entries = ctxm->vnic_entries + 8727 le16_to_cpu(resp->vnic_max_ring_table_entries); 8728 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8729 bnxt_init_ctx_initializer(ctxm, init_val, 8730 resp->vnic_init_offset, 8731 (init_mask & (1 << init_idx++)) != 0); 8732 8733 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8734 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8735 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8736 bnxt_init_ctx_initializer(ctxm, init_val, 8737 resp->stat_init_offset, 8738 (init_mask & (1 << init_idx++)) != 0); 8739 8740 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8741 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8742 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8743 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8744 ctxm->entry_multiple = resp->tqm_entries_multiple; 8745 if (!ctxm->entry_multiple) 8746 ctxm->entry_multiple = 1; 8747 8748 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8749 8750 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8751 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8752 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8753 ctxm->mrav_num_entries_units = 8754 le16_to_cpu(resp->mrav_num_entries_units); 8755 bnxt_init_ctx_initializer(ctxm, init_val, 8756 resp->mrav_init_offset, 8757 (init_mask & (1 << init_idx++)) != 0); 8758 8759 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8760 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8761 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8762 8763 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8764 if (!ctx->tqm_fp_rings_count) 8765 ctx->tqm_fp_rings_count = bp->max_q; 8766 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8767 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8768 8769 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8770 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8771 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8772 8773 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8774 } else { 8775 rc = 0; 8776 } 8777 ctx_err: 8778 hwrm_req_drop(bp, req); 8779 return rc; 8780 } 8781 8782 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8783 __le64 *pg_dir) 8784 { 8785 if (!rmem->nr_pages) 8786 return; 8787 8788 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8789 if (rmem->depth >= 1) { 8790 if (rmem->depth == 2) 8791 *pg_attr |= 2; 8792 else 8793 *pg_attr |= 1; 8794 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8795 } else { 8796 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8797 } 8798 } 8799 8800 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8801 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8802 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8803 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8804 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8805 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8806 8807 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8808 { 8809 struct hwrm_func_backing_store_cfg_input *req; 8810 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8811 struct bnxt_ctx_pg_info *ctx_pg; 8812 struct bnxt_ctx_mem_type *ctxm; 8813 void **__req = (void **)&req; 8814 u32 req_len = sizeof(*req); 8815 __le32 *num_entries; 8816 __le64 *pg_dir; 8817 u32 flags = 0; 8818 u8 *pg_attr; 8819 u32 ena; 8820 int rc; 8821 int i; 8822 8823 if (!ctx) 8824 return 0; 8825 8826 if (req_len > bp->hwrm_max_ext_req_len) 8827 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8828 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8829 if (rc) 8830 return rc; 8831 8832 req->enables = cpu_to_le32(enables); 8833 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8834 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8835 ctx_pg = ctxm->pg_info; 8836 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8837 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8838 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8839 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8840 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8841 &req->qpc_pg_size_qpc_lvl, 8842 &req->qpc_page_dir); 8843 8844 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8845 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8846 } 8847 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8848 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8849 ctx_pg = ctxm->pg_info; 8850 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8851 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8852 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8853 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8854 &req->srq_pg_size_srq_lvl, 8855 &req->srq_page_dir); 8856 } 8857 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8858 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8859 ctx_pg = ctxm->pg_info; 8860 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8861 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8862 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8863 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8864 &req->cq_pg_size_cq_lvl, 8865 &req->cq_page_dir); 8866 } 8867 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8868 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8869 ctx_pg = ctxm->pg_info; 8870 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8871 req->vnic_num_ring_table_entries = 8872 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8873 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8874 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8875 &req->vnic_pg_size_vnic_lvl, 8876 &req->vnic_page_dir); 8877 } 8878 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8879 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8880 ctx_pg = ctxm->pg_info; 8881 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8882 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8883 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8884 &req->stat_pg_size_stat_lvl, 8885 &req->stat_page_dir); 8886 } 8887 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8888 u32 units; 8889 8890 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8891 ctx_pg = ctxm->pg_info; 8892 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8893 units = ctxm->mrav_num_entries_units; 8894 if (units) { 8895 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8896 u32 entries; 8897 8898 num_mr = ctx_pg->entries - num_ah; 8899 entries = ((num_mr / units) << 16) | (num_ah / units); 8900 req->mrav_num_entries = cpu_to_le32(entries); 8901 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8902 } 8903 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8904 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8905 &req->mrav_pg_size_mrav_lvl, 8906 &req->mrav_page_dir); 8907 } 8908 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8909 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8910 ctx_pg = ctxm->pg_info; 8911 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8912 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8913 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8914 &req->tim_pg_size_tim_lvl, 8915 &req->tim_page_dir); 8916 } 8917 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8918 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8919 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8920 pg_dir = &req->tqm_sp_page_dir, 8921 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8922 ctx_pg = ctxm->pg_info; 8923 i < BNXT_MAX_TQM_RINGS; 8924 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8925 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8926 if (!(enables & ena)) 8927 continue; 8928 8929 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8930 *num_entries = cpu_to_le32(ctx_pg->entries); 8931 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8932 } 8933 req->flags = cpu_to_le32(flags); 8934 return hwrm_req_send(bp, req); 8935 } 8936 8937 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8938 struct bnxt_ctx_pg_info *ctx_pg) 8939 { 8940 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8941 8942 rmem->page_size = BNXT_PAGE_SIZE; 8943 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8944 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8945 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8946 if (rmem->depth >= 1) 8947 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8948 return bnxt_alloc_ring(bp, rmem); 8949 } 8950 8951 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8952 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8953 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8954 { 8955 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8956 int rc; 8957 8958 if (!mem_size) 8959 return -EINVAL; 8960 8961 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8962 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8963 ctx_pg->nr_pages = 0; 8964 return -EINVAL; 8965 } 8966 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8967 int nr_tbls, i; 8968 8969 rmem->depth = 2; 8970 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8971 GFP_KERNEL); 8972 if (!ctx_pg->ctx_pg_tbl) 8973 return -ENOMEM; 8974 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8975 rmem->nr_pages = nr_tbls; 8976 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8977 if (rc) 8978 return rc; 8979 for (i = 0; i < nr_tbls; i++) { 8980 struct bnxt_ctx_pg_info *pg_tbl; 8981 8982 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8983 if (!pg_tbl) 8984 return -ENOMEM; 8985 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8986 rmem = &pg_tbl->ring_mem; 8987 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8988 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8989 rmem->depth = 1; 8990 rmem->nr_pages = MAX_CTX_PAGES; 8991 rmem->ctx_mem = ctxm; 8992 if (i == (nr_tbls - 1)) { 8993 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8994 8995 if (rem) 8996 rmem->nr_pages = rem; 8997 } 8998 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8999 if (rc) 9000 break; 9001 } 9002 } else { 9003 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 9004 if (rmem->nr_pages > 1 || depth) 9005 rmem->depth = 1; 9006 rmem->ctx_mem = ctxm; 9007 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 9008 } 9009 return rc; 9010 } 9011 9012 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp, 9013 struct bnxt_ctx_pg_info *ctx_pg, 9014 void *buf, size_t offset, size_t head, 9015 size_t tail) 9016 { 9017 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9018 size_t nr_pages = ctx_pg->nr_pages; 9019 int page_size = rmem->page_size; 9020 size_t len = 0, total_len = 0; 9021 u16 depth = rmem->depth; 9022 9023 tail %= nr_pages * page_size; 9024 do { 9025 if (depth > 1) { 9026 int i = head / (page_size * MAX_CTX_PAGES); 9027 struct bnxt_ctx_pg_info *pg_tbl; 9028 9029 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9030 rmem = &pg_tbl->ring_mem; 9031 } 9032 len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail); 9033 head += len; 9034 offset += len; 9035 total_len += len; 9036 if (head >= nr_pages * page_size) 9037 head = 0; 9038 } while (head != tail); 9039 return total_len; 9040 } 9041 9042 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 9043 struct bnxt_ctx_pg_info *ctx_pg) 9044 { 9045 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 9046 9047 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 9048 ctx_pg->ctx_pg_tbl) { 9049 int i, nr_tbls = rmem->nr_pages; 9050 9051 for (i = 0; i < nr_tbls; i++) { 9052 struct bnxt_ctx_pg_info *pg_tbl; 9053 struct bnxt_ring_mem_info *rmem2; 9054 9055 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 9056 if (!pg_tbl) 9057 continue; 9058 rmem2 = &pg_tbl->ring_mem; 9059 bnxt_free_ring(bp, rmem2); 9060 ctx_pg->ctx_pg_arr[i] = NULL; 9061 kfree(pg_tbl); 9062 ctx_pg->ctx_pg_tbl[i] = NULL; 9063 } 9064 kfree(ctx_pg->ctx_pg_tbl); 9065 ctx_pg->ctx_pg_tbl = NULL; 9066 } 9067 bnxt_free_ring(bp, rmem); 9068 ctx_pg->nr_pages = 0; 9069 } 9070 9071 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 9072 struct bnxt_ctx_mem_type *ctxm, u32 entries, 9073 u8 pg_lvl) 9074 { 9075 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9076 int i, rc = 0, n = 1; 9077 u32 mem_size; 9078 9079 if (!ctxm->entry_size || !ctx_pg) 9080 return -EINVAL; 9081 if (ctxm->instance_bmap) 9082 n = hweight32(ctxm->instance_bmap); 9083 if (ctxm->entry_multiple) 9084 entries = roundup(entries, ctxm->entry_multiple); 9085 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 9086 mem_size = entries * ctxm->entry_size; 9087 for (i = 0; i < n && !rc; i++) { 9088 ctx_pg[i].entries = entries; 9089 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 9090 ctxm->init_value ? ctxm : NULL); 9091 } 9092 if (!rc) 9093 ctxm->mem_valid = 1; 9094 return rc; 9095 } 9096 9097 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 9098 struct bnxt_ctx_mem_type *ctxm, 9099 bool last) 9100 { 9101 struct hwrm_func_backing_store_cfg_v2_input *req; 9102 u32 instance_bmap = ctxm->instance_bmap; 9103 int i, j, rc = 0, n = 1; 9104 __le32 *p; 9105 9106 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 9107 return 0; 9108 9109 if (instance_bmap) 9110 n = hweight32(ctxm->instance_bmap); 9111 else 9112 instance_bmap = 1; 9113 9114 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 9115 if (rc) 9116 return rc; 9117 hwrm_req_hold(bp, req); 9118 req->type = cpu_to_le16(ctxm->type); 9119 req->entry_size = cpu_to_le16(ctxm->entry_size); 9120 if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) && 9121 bnxt_bs_trace_avail(bp, ctxm->type)) { 9122 struct bnxt_bs_trace_info *bs_trace; 9123 u32 enables; 9124 9125 enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET; 9126 req->enables = cpu_to_le32(enables); 9127 bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]]; 9128 req->next_bs_offset = cpu_to_le32(bs_trace->last_offset); 9129 } 9130 req->subtype_valid_cnt = ctxm->split_entry_cnt; 9131 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 9132 p[i] = cpu_to_le32(ctxm->split[i]); 9133 for (i = 0, j = 0; j < n && !rc; i++) { 9134 struct bnxt_ctx_pg_info *ctx_pg; 9135 9136 if (!(instance_bmap & (1 << i))) 9137 continue; 9138 req->instance = cpu_to_le16(i); 9139 ctx_pg = &ctxm->pg_info[j++]; 9140 if (!ctx_pg->entries) 9141 continue; 9142 req->num_entries = cpu_to_le32(ctx_pg->entries); 9143 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 9144 &req->page_size_pbl_level, 9145 &req->page_dir); 9146 if (last && j == n) 9147 req->flags = 9148 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 9149 rc = hwrm_req_send(bp, req); 9150 } 9151 hwrm_req_drop(bp, req); 9152 return rc; 9153 } 9154 9155 static int bnxt_backing_store_cfg_v2(struct bnxt *bp) 9156 { 9157 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9158 struct bnxt_ctx_mem_type *ctxm; 9159 u16 last_type = BNXT_CTX_INV; 9160 int rc = 0; 9161 u16 type; 9162 9163 for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) { 9164 ctxm = &ctx->ctx_arr[type]; 9165 if (!bnxt_bs_trace_avail(bp, type)) 9166 continue; 9167 if (!ctxm->mem_valid) { 9168 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, 9169 ctxm->max_entries, 1); 9170 if (rc) { 9171 netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n", 9172 type); 9173 continue; 9174 } 9175 bnxt_bs_trace_init(bp, ctxm); 9176 } 9177 last_type = type; 9178 } 9179 9180 if (last_type == BNXT_CTX_INV) { 9181 for (type = 0; type < BNXT_CTX_MAX; type++) { 9182 ctxm = &ctx->ctx_arr[type]; 9183 if (ctxm->mem_valid) 9184 last_type = type; 9185 } 9186 if (last_type == BNXT_CTX_INV) 9187 return 0; 9188 } 9189 ctx->ctx_arr[last_type].last = 1; 9190 9191 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 9192 ctxm = &ctx->ctx_arr[type]; 9193 9194 if (!ctxm->mem_valid) 9195 continue; 9196 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 9197 if (rc) 9198 return rc; 9199 } 9200 return 0; 9201 } 9202 9203 /** 9204 * __bnxt_copy_ctx_mem - copy host context memory 9205 * @bp: The driver context 9206 * @ctxm: The pointer to the context memory type 9207 * @buf: The destination buffer or NULL to just obtain the length 9208 * @offset: The buffer offset to copy the data to 9209 * @head: The head offset of context memory to copy from 9210 * @tail: The tail offset (last byte + 1) of context memory to end the copy 9211 * 9212 * This function is called for debugging purposes to dump the host context 9213 * used by the chip. 9214 * 9215 * Return: Length of memory copied 9216 */ 9217 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp, 9218 struct bnxt_ctx_mem_type *ctxm, void *buf, 9219 size_t offset, size_t head, size_t tail) 9220 { 9221 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 9222 size_t len = 0, total_len = 0; 9223 int i, n = 1; 9224 9225 if (!ctx_pg) 9226 return 0; 9227 9228 if (ctxm->instance_bmap) 9229 n = hweight32(ctxm->instance_bmap); 9230 for (i = 0; i < n; i++) { 9231 len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head, 9232 tail); 9233 offset += len; 9234 total_len += len; 9235 } 9236 return total_len; 9237 } 9238 9239 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm, 9240 void *buf, size_t offset) 9241 { 9242 size_t tail = ctxm->max_entries * ctxm->entry_size; 9243 9244 return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail); 9245 } 9246 9247 static void bnxt_free_one_ctx_mem(struct bnxt *bp, 9248 struct bnxt_ctx_mem_type *ctxm, bool force) 9249 { 9250 struct bnxt_ctx_pg_info *ctx_pg; 9251 int i, n = 1; 9252 9253 ctxm->last = 0; 9254 9255 if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST)) 9256 return; 9257 9258 ctx_pg = ctxm->pg_info; 9259 if (ctx_pg) { 9260 if (ctxm->instance_bmap) 9261 n = hweight32(ctxm->instance_bmap); 9262 for (i = 0; i < n; i++) 9263 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 9264 9265 kfree(ctx_pg); 9266 ctxm->pg_info = NULL; 9267 ctxm->mem_valid = 0; 9268 } 9269 memset(ctxm, 0, sizeof(*ctxm)); 9270 } 9271 9272 void bnxt_free_ctx_mem(struct bnxt *bp, bool force) 9273 { 9274 struct bnxt_ctx_mem_info *ctx = bp->ctx; 9275 u16 type; 9276 9277 if (!ctx) 9278 return; 9279 9280 for (type = 0; type < BNXT_CTX_V2_MAX; type++) 9281 bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force); 9282 9283 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 9284 if (force) { 9285 kfree(ctx); 9286 bp->ctx = NULL; 9287 } 9288 } 9289 9290 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 9291 { 9292 struct bnxt_ctx_mem_type *ctxm; 9293 struct bnxt_ctx_mem_info *ctx; 9294 u32 l2_qps, qp1_qps, max_qps; 9295 u32 ena, entries_sp, entries; 9296 u32 srqs, max_srqs, min; 9297 u32 num_mr, num_ah; 9298 u32 extra_srqs = 0; 9299 u32 extra_qps = 0; 9300 u32 fast_qpmd_qps; 9301 u8 pg_lvl = 1; 9302 int i, rc; 9303 9304 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 9305 if (rc) { 9306 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 9307 rc); 9308 return rc; 9309 } 9310 ctx = bp->ctx; 9311 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 9312 return 0; 9313 9314 ena = 0; 9315 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 9316 goto skip_legacy; 9317 9318 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9319 l2_qps = ctxm->qp_l2_entries; 9320 qp1_qps = ctxm->qp_qp1_entries; 9321 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 9322 max_qps = ctxm->max_entries; 9323 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9324 srqs = ctxm->srq_l2_entries; 9325 max_srqs = ctxm->max_entries; 9326 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 9327 pg_lvl = 2; 9328 if (BNXT_SW_RES_LMT(bp)) { 9329 extra_qps = max_qps - l2_qps - qp1_qps; 9330 extra_srqs = max_srqs - srqs; 9331 } else { 9332 extra_qps = min_t(u32, 65536, 9333 max_qps - l2_qps - qp1_qps); 9334 /* allocate extra qps if fw supports RoCE fast qp 9335 * destroy feature 9336 */ 9337 extra_qps += fast_qpmd_qps; 9338 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 9339 } 9340 if (fast_qpmd_qps) 9341 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 9342 } 9343 9344 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 9345 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 9346 pg_lvl); 9347 if (rc) 9348 return rc; 9349 9350 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 9351 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 9352 if (rc) 9353 return rc; 9354 9355 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 9356 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 9357 extra_qps * 2, pg_lvl); 9358 if (rc) 9359 return rc; 9360 9361 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 9362 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9363 if (rc) 9364 return rc; 9365 9366 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 9367 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 9368 if (rc) 9369 return rc; 9370 9371 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 9372 goto skip_rdma; 9373 9374 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 9375 if (BNXT_SW_RES_LMT(bp) && 9376 ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) { 9377 num_ah = ctxm->mrav_av_entries; 9378 num_mr = ctxm->max_entries - num_ah; 9379 } else { 9380 /* 128K extra is needed to accommodate static AH context 9381 * allocation by f/w. 9382 */ 9383 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 9384 num_ah = min_t(u32, num_mr, 1024 * 128); 9385 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 9386 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 9387 ctxm->mrav_av_entries = num_ah; 9388 } 9389 9390 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 9391 if (rc) 9392 return rc; 9393 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 9394 9395 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 9396 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 9397 if (rc) 9398 return rc; 9399 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 9400 9401 skip_rdma: 9402 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 9403 min = ctxm->min_entries; 9404 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 9405 2 * (extra_qps + qp1_qps) + min; 9406 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 9407 if (rc) 9408 return rc; 9409 9410 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 9411 entries = l2_qps + 2 * (extra_qps + qp1_qps); 9412 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 9413 if (rc) 9414 return rc; 9415 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 9416 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 9417 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 9418 9419 skip_legacy: 9420 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 9421 rc = bnxt_backing_store_cfg_v2(bp); 9422 else 9423 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 9424 if (rc) { 9425 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 9426 rc); 9427 return rc; 9428 } 9429 ctx->flags |= BNXT_CTX_FLAG_INITED; 9430 return 0; 9431 } 9432 9433 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp) 9434 { 9435 struct hwrm_dbg_crashdump_medium_cfg_input *req; 9436 u16 page_attr; 9437 int rc; 9438 9439 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9440 return 0; 9441 9442 rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG); 9443 if (rc) 9444 return rc; 9445 9446 if (BNXT_PAGE_SIZE == 0x2000) 9447 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K; 9448 else if (BNXT_PAGE_SIZE == 0x10000) 9449 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K; 9450 else 9451 page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K; 9452 req->pg_size_lvl = cpu_to_le16(page_attr | 9453 bp->fw_crash_mem->ring_mem.depth); 9454 req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map); 9455 req->size = cpu_to_le32(bp->fw_crash_len); 9456 req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR); 9457 return hwrm_req_send(bp, req); 9458 } 9459 9460 static void bnxt_free_crash_dump_mem(struct bnxt *bp) 9461 { 9462 if (bp->fw_crash_mem) { 9463 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9464 kfree(bp->fw_crash_mem); 9465 bp->fw_crash_mem = NULL; 9466 } 9467 } 9468 9469 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp) 9470 { 9471 u32 mem_size = 0; 9472 int rc; 9473 9474 if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR)) 9475 return 0; 9476 9477 rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size); 9478 if (rc) 9479 return rc; 9480 9481 mem_size = round_up(mem_size, 4); 9482 9483 /* keep and use the existing pages */ 9484 if (bp->fw_crash_mem && 9485 mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE) 9486 goto alloc_done; 9487 9488 if (bp->fw_crash_mem) 9489 bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem); 9490 else 9491 bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem), 9492 GFP_KERNEL); 9493 if (!bp->fw_crash_mem) 9494 return -ENOMEM; 9495 9496 rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL); 9497 if (rc) { 9498 bnxt_free_crash_dump_mem(bp); 9499 return rc; 9500 } 9501 9502 alloc_done: 9503 bp->fw_crash_len = mem_size; 9504 return 0; 9505 } 9506 9507 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 9508 { 9509 struct hwrm_func_resource_qcaps_output *resp; 9510 struct hwrm_func_resource_qcaps_input *req; 9511 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9512 int rc; 9513 9514 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 9515 if (rc) 9516 return rc; 9517 9518 req->fid = cpu_to_le16(0xffff); 9519 resp = hwrm_req_hold(bp, req); 9520 rc = hwrm_req_send_silent(bp, req); 9521 if (rc) 9522 goto hwrm_func_resc_qcaps_exit; 9523 9524 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 9525 if (!all) 9526 goto hwrm_func_resc_qcaps_exit; 9527 9528 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 9529 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9530 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 9531 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9532 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 9533 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9534 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 9535 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9536 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 9537 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 9538 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 9539 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9540 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 9541 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9542 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 9543 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9544 9545 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 9546 u16 max_msix = le16_to_cpu(resp->max_msix); 9547 9548 hw_resc->max_nqs = max_msix; 9549 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 9550 } 9551 9552 if (BNXT_PF(bp)) { 9553 struct bnxt_pf_info *pf = &bp->pf; 9554 9555 pf->vf_resv_strategy = 9556 le16_to_cpu(resp->vf_reservation_strategy); 9557 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9558 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9559 } 9560 hwrm_func_resc_qcaps_exit: 9561 hwrm_req_drop(bp, req); 9562 return rc; 9563 } 9564 9565 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9566 { 9567 struct hwrm_port_mac_ptp_qcfg_output *resp; 9568 struct hwrm_port_mac_ptp_qcfg_input *req; 9569 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9570 u8 flags; 9571 int rc; 9572 9573 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9574 rc = -ENODEV; 9575 goto no_ptp; 9576 } 9577 9578 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9579 if (rc) 9580 goto no_ptp; 9581 9582 req->port_id = cpu_to_le16(bp->pf.port_id); 9583 resp = hwrm_req_hold(bp, req); 9584 rc = hwrm_req_send(bp, req); 9585 if (rc) 9586 goto exit; 9587 9588 flags = resp->flags; 9589 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9590 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9591 rc = -ENODEV; 9592 goto exit; 9593 } 9594 if (!ptp) { 9595 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9596 if (!ptp) { 9597 rc = -ENOMEM; 9598 goto exit; 9599 } 9600 ptp->bp = bp; 9601 bp->ptp_cfg = ptp; 9602 } 9603 9604 if (flags & 9605 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9606 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9607 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9608 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9609 } else if (BNXT_CHIP_P5(bp)) { 9610 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9611 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9612 } else { 9613 rc = -ENODEV; 9614 goto exit; 9615 } 9616 ptp->rtc_configured = 9617 (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9618 rc = bnxt_ptp_init(bp); 9619 if (rc) 9620 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9621 exit: 9622 hwrm_req_drop(bp, req); 9623 if (!rc) 9624 return 0; 9625 9626 no_ptp: 9627 bnxt_ptp_clear(bp); 9628 kfree(ptp); 9629 bp->ptp_cfg = NULL; 9630 return rc; 9631 } 9632 9633 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9634 { 9635 u32 flags, flags_ext, flags_ext2, flags_ext3; 9636 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9637 struct hwrm_func_qcaps_output *resp; 9638 struct hwrm_func_qcaps_input *req; 9639 int rc; 9640 9641 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9642 if (rc) 9643 return rc; 9644 9645 req->fid = cpu_to_le16(0xffff); 9646 resp = hwrm_req_hold(bp, req); 9647 rc = hwrm_req_send(bp, req); 9648 if (rc) 9649 goto hwrm_func_qcaps_exit; 9650 9651 flags = le32_to_cpu(resp->flags); 9652 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9653 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9654 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9655 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9656 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9657 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9658 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9659 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9660 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9661 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9662 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9663 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9664 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9665 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9666 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9667 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9668 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9669 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9670 9671 flags_ext = le32_to_cpu(resp->flags_ext); 9672 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9673 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9674 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9675 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9676 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9677 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9678 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9679 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9680 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9681 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9682 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED) 9683 bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2; 9684 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED)) 9685 bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP; 9686 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9687 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9688 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9689 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9690 9691 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9692 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9693 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9694 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9695 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9696 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9697 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9698 if (flags_ext2 & 9699 FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED) 9700 bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS; 9701 if (BNXT_PF(bp) && 9702 (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED)) 9703 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED; 9704 9705 flags_ext3 = le32_to_cpu(resp->flags_ext3); 9706 if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT) 9707 bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT; 9708 9709 bp->tx_push_thresh = 0; 9710 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9711 BNXT_FW_MAJ(bp) > 217) 9712 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9713 9714 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9715 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9716 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9717 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9718 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9719 if (!hw_resc->max_hw_ring_grps) 9720 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9721 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9722 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9723 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9724 9725 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9726 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9727 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9728 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9729 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9730 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9731 9732 if (BNXT_PF(bp)) { 9733 struct bnxt_pf_info *pf = &bp->pf; 9734 9735 pf->fw_fid = le16_to_cpu(resp->fid); 9736 pf->port_id = le16_to_cpu(resp->port_id); 9737 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9738 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9739 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9740 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9741 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9742 bp->flags |= BNXT_FLAG_WOL_CAP; 9743 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9744 bp->fw_cap |= BNXT_FW_CAP_PTP; 9745 } else { 9746 bnxt_ptp_clear(bp); 9747 kfree(bp->ptp_cfg); 9748 bp->ptp_cfg = NULL; 9749 } 9750 } else { 9751 #ifdef CONFIG_BNXT_SRIOV 9752 struct bnxt_vf_info *vf = &bp->vf; 9753 9754 vf->fw_fid = le16_to_cpu(resp->fid); 9755 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9756 #endif 9757 } 9758 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9759 9760 hwrm_func_qcaps_exit: 9761 hwrm_req_drop(bp, req); 9762 return rc; 9763 } 9764 9765 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9766 { 9767 struct hwrm_dbg_qcaps_output *resp; 9768 struct hwrm_dbg_qcaps_input *req; 9769 int rc; 9770 9771 bp->fw_dbg_cap = 0; 9772 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9773 return; 9774 9775 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9776 if (rc) 9777 return; 9778 9779 req->fid = cpu_to_le16(0xffff); 9780 resp = hwrm_req_hold(bp, req); 9781 rc = hwrm_req_send(bp, req); 9782 if (rc) 9783 goto hwrm_dbg_qcaps_exit; 9784 9785 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9786 9787 hwrm_dbg_qcaps_exit: 9788 hwrm_req_drop(bp, req); 9789 } 9790 9791 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9792 9793 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9794 { 9795 int rc; 9796 9797 rc = __bnxt_hwrm_func_qcaps(bp); 9798 if (rc) 9799 return rc; 9800 9801 bnxt_hwrm_dbg_qcaps(bp); 9802 9803 rc = bnxt_hwrm_queue_qportcfg(bp); 9804 if (rc) { 9805 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9806 return rc; 9807 } 9808 if (bp->hwrm_spec_code >= 0x10803) { 9809 rc = bnxt_alloc_ctx_mem(bp); 9810 if (rc) 9811 return rc; 9812 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9813 if (!rc) 9814 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9815 } 9816 return 0; 9817 } 9818 9819 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9820 { 9821 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9822 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9823 u32 flags; 9824 int rc; 9825 9826 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9827 return 0; 9828 9829 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9830 if (rc) 9831 return rc; 9832 9833 resp = hwrm_req_hold(bp, req); 9834 rc = hwrm_req_send(bp, req); 9835 if (rc) 9836 goto hwrm_cfa_adv_qcaps_exit; 9837 9838 flags = le32_to_cpu(resp->flags); 9839 if (flags & 9840 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9841 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9842 9843 if (flags & 9844 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9845 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9846 9847 if (flags & 9848 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9849 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9850 9851 hwrm_cfa_adv_qcaps_exit: 9852 hwrm_req_drop(bp, req); 9853 return rc; 9854 } 9855 9856 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9857 { 9858 if (bp->fw_health) 9859 return 0; 9860 9861 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9862 if (!bp->fw_health) 9863 return -ENOMEM; 9864 9865 mutex_init(&bp->fw_health->lock); 9866 return 0; 9867 } 9868 9869 static int bnxt_alloc_fw_health(struct bnxt *bp) 9870 { 9871 int rc; 9872 9873 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9874 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9875 return 0; 9876 9877 rc = __bnxt_alloc_fw_health(bp); 9878 if (rc) { 9879 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9880 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9881 return rc; 9882 } 9883 9884 return 0; 9885 } 9886 9887 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9888 { 9889 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9890 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9891 BNXT_FW_HEALTH_WIN_MAP_OFF); 9892 } 9893 9894 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9895 { 9896 struct bnxt_fw_health *fw_health = bp->fw_health; 9897 u32 reg_type; 9898 9899 if (!fw_health) 9900 return; 9901 9902 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9903 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9904 fw_health->status_reliable = false; 9905 9906 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9907 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9908 fw_health->resets_reliable = false; 9909 } 9910 9911 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9912 { 9913 void __iomem *hs; 9914 u32 status_loc; 9915 u32 reg_type; 9916 u32 sig; 9917 9918 if (bp->fw_health) 9919 bp->fw_health->status_reliable = false; 9920 9921 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9922 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9923 9924 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9925 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9926 if (!bp->chip_num) { 9927 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9928 bp->chip_num = readl(bp->bar0 + 9929 BNXT_FW_HEALTH_WIN_BASE + 9930 BNXT_GRC_REG_CHIP_NUM); 9931 } 9932 if (!BNXT_CHIP_P5_PLUS(bp)) 9933 return; 9934 9935 status_loc = BNXT_GRC_REG_STATUS_P5 | 9936 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9937 } else { 9938 status_loc = readl(hs + offsetof(struct hcomm_status, 9939 fw_status_loc)); 9940 } 9941 9942 if (__bnxt_alloc_fw_health(bp)) { 9943 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9944 return; 9945 } 9946 9947 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9948 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9949 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9950 __bnxt_map_fw_health_reg(bp, status_loc); 9951 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9952 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9953 } 9954 9955 bp->fw_health->status_reliable = true; 9956 } 9957 9958 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9959 { 9960 struct bnxt_fw_health *fw_health = bp->fw_health; 9961 u32 reg_base = 0xffffffff; 9962 int i; 9963 9964 bp->fw_health->status_reliable = false; 9965 bp->fw_health->resets_reliable = false; 9966 /* Only pre-map the monitoring GRC registers using window 3 */ 9967 for (i = 0; i < 4; i++) { 9968 u32 reg = fw_health->regs[i]; 9969 9970 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9971 continue; 9972 if (reg_base == 0xffffffff) 9973 reg_base = reg & BNXT_GRC_BASE_MASK; 9974 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9975 return -ERANGE; 9976 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9977 } 9978 bp->fw_health->status_reliable = true; 9979 bp->fw_health->resets_reliable = true; 9980 if (reg_base == 0xffffffff) 9981 return 0; 9982 9983 __bnxt_map_fw_health_reg(bp, reg_base); 9984 return 0; 9985 } 9986 9987 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9988 { 9989 if (!bp->fw_health) 9990 return; 9991 9992 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9993 bp->fw_health->status_reliable = true; 9994 bp->fw_health->resets_reliable = true; 9995 } else { 9996 bnxt_try_map_fw_health_reg(bp); 9997 } 9998 } 9999 10000 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 10001 { 10002 struct bnxt_fw_health *fw_health = bp->fw_health; 10003 struct hwrm_error_recovery_qcfg_output *resp; 10004 struct hwrm_error_recovery_qcfg_input *req; 10005 int rc, i; 10006 10007 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 10008 return 0; 10009 10010 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 10011 if (rc) 10012 return rc; 10013 10014 resp = hwrm_req_hold(bp, req); 10015 rc = hwrm_req_send(bp, req); 10016 if (rc) 10017 goto err_recovery_out; 10018 fw_health->flags = le32_to_cpu(resp->flags); 10019 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 10020 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 10021 rc = -EINVAL; 10022 goto err_recovery_out; 10023 } 10024 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 10025 fw_health->master_func_wait_dsecs = 10026 le32_to_cpu(resp->master_func_wait_period); 10027 fw_health->normal_func_wait_dsecs = 10028 le32_to_cpu(resp->normal_func_wait_period); 10029 fw_health->post_reset_wait_dsecs = 10030 le32_to_cpu(resp->master_func_wait_period_after_reset); 10031 fw_health->post_reset_max_wait_dsecs = 10032 le32_to_cpu(resp->max_bailout_time_after_reset); 10033 fw_health->regs[BNXT_FW_HEALTH_REG] = 10034 le32_to_cpu(resp->fw_health_status_reg); 10035 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 10036 le32_to_cpu(resp->fw_heartbeat_reg); 10037 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 10038 le32_to_cpu(resp->fw_reset_cnt_reg); 10039 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 10040 le32_to_cpu(resp->reset_inprogress_reg); 10041 fw_health->fw_reset_inprog_reg_mask = 10042 le32_to_cpu(resp->reset_inprogress_reg_mask); 10043 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 10044 if (fw_health->fw_reset_seq_cnt >= 16) { 10045 rc = -EINVAL; 10046 goto err_recovery_out; 10047 } 10048 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 10049 fw_health->fw_reset_seq_regs[i] = 10050 le32_to_cpu(resp->reset_reg[i]); 10051 fw_health->fw_reset_seq_vals[i] = 10052 le32_to_cpu(resp->reset_reg_val[i]); 10053 fw_health->fw_reset_seq_delay_msec[i] = 10054 resp->delay_after_reset[i]; 10055 } 10056 err_recovery_out: 10057 hwrm_req_drop(bp, req); 10058 if (!rc) 10059 rc = bnxt_map_fw_health_regs(bp); 10060 if (rc) 10061 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 10062 return rc; 10063 } 10064 10065 static int bnxt_hwrm_func_reset(struct bnxt *bp) 10066 { 10067 struct hwrm_func_reset_input *req; 10068 int rc; 10069 10070 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 10071 if (rc) 10072 return rc; 10073 10074 req->enables = 0; 10075 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 10076 return hwrm_req_send(bp, req); 10077 } 10078 10079 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 10080 { 10081 struct hwrm_nvm_get_dev_info_output nvm_info; 10082 10083 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 10084 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 10085 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 10086 nvm_info.nvm_cfg_ver_upd); 10087 } 10088 10089 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 10090 { 10091 struct hwrm_queue_qportcfg_output *resp; 10092 struct hwrm_queue_qportcfg_input *req; 10093 u8 i, j, *qptr; 10094 bool no_rdma; 10095 int rc = 0; 10096 10097 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 10098 if (rc) 10099 return rc; 10100 10101 resp = hwrm_req_hold(bp, req); 10102 rc = hwrm_req_send(bp, req); 10103 if (rc) 10104 goto qportcfg_exit; 10105 10106 if (!resp->max_configurable_queues) { 10107 rc = -EINVAL; 10108 goto qportcfg_exit; 10109 } 10110 bp->max_tc = resp->max_configurable_queues; 10111 bp->max_lltc = resp->max_configurable_lossless_queues; 10112 if (bp->max_tc > BNXT_MAX_QUEUE) 10113 bp->max_tc = BNXT_MAX_QUEUE; 10114 10115 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 10116 qptr = &resp->queue_id0; 10117 for (i = 0, j = 0; i < bp->max_tc; i++) { 10118 bp->q_info[j].queue_id = *qptr; 10119 bp->q_ids[i] = *qptr++; 10120 bp->q_info[j].queue_profile = *qptr++; 10121 bp->tc_to_qidx[j] = j; 10122 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 10123 (no_rdma && BNXT_PF(bp))) 10124 j++; 10125 } 10126 bp->max_q = bp->max_tc; 10127 bp->max_tc = max_t(u8, j, 1); 10128 10129 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 10130 bp->max_tc = 1; 10131 10132 if (bp->max_lltc > bp->max_tc) 10133 bp->max_lltc = bp->max_tc; 10134 10135 qportcfg_exit: 10136 hwrm_req_drop(bp, req); 10137 return rc; 10138 } 10139 10140 static int bnxt_hwrm_poll(struct bnxt *bp) 10141 { 10142 struct hwrm_ver_get_input *req; 10143 int rc; 10144 10145 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10146 if (rc) 10147 return rc; 10148 10149 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10150 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10151 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10152 10153 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 10154 rc = hwrm_req_send(bp, req); 10155 return rc; 10156 } 10157 10158 static int bnxt_hwrm_ver_get(struct bnxt *bp) 10159 { 10160 struct hwrm_ver_get_output *resp; 10161 struct hwrm_ver_get_input *req; 10162 u16 fw_maj, fw_min, fw_bld, fw_rsv; 10163 u32 dev_caps_cfg, hwrm_ver; 10164 int rc, len, max_tmo_secs; 10165 10166 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 10167 if (rc) 10168 return rc; 10169 10170 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 10171 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 10172 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 10173 req->hwrm_intf_min = HWRM_VERSION_MINOR; 10174 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 10175 10176 resp = hwrm_req_hold(bp, req); 10177 rc = hwrm_req_send(bp, req); 10178 if (rc) 10179 goto hwrm_ver_get_exit; 10180 10181 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 10182 10183 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 10184 resp->hwrm_intf_min_8b << 8 | 10185 resp->hwrm_intf_upd_8b; 10186 if (resp->hwrm_intf_maj_8b < 1) { 10187 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 10188 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10189 resp->hwrm_intf_upd_8b); 10190 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 10191 } 10192 10193 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 10194 HWRM_VERSION_UPDATE; 10195 10196 if (bp->hwrm_spec_code > hwrm_ver) 10197 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10198 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 10199 HWRM_VERSION_UPDATE); 10200 else 10201 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 10202 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 10203 resp->hwrm_intf_upd_8b); 10204 10205 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 10206 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 10207 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 10208 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 10209 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 10210 len = FW_VER_STR_LEN; 10211 } else { 10212 fw_maj = resp->hwrm_fw_maj_8b; 10213 fw_min = resp->hwrm_fw_min_8b; 10214 fw_bld = resp->hwrm_fw_bld_8b; 10215 fw_rsv = resp->hwrm_fw_rsvd_8b; 10216 len = BC_HWRM_STR_LEN; 10217 } 10218 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 10219 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 10220 fw_rsv); 10221 10222 if (strlen(resp->active_pkg_name)) { 10223 int fw_ver_len = strlen(bp->fw_ver_str); 10224 10225 snprintf(bp->fw_ver_str + fw_ver_len, 10226 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 10227 resp->active_pkg_name); 10228 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 10229 } 10230 10231 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 10232 if (!bp->hwrm_cmd_timeout) 10233 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 10234 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 10235 if (!bp->hwrm_cmd_max_timeout) 10236 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 10237 max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000; 10238 #ifdef CONFIG_DETECT_HUNG_TASK 10239 if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT || 10240 max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) { 10241 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n", 10242 max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT); 10243 } 10244 #endif 10245 10246 if (resp->hwrm_intf_maj_8b >= 1) { 10247 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 10248 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 10249 } 10250 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 10251 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 10252 10253 bp->chip_num = le16_to_cpu(resp->chip_num); 10254 bp->chip_rev = resp->chip_rev; 10255 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 10256 !resp->chip_metal) 10257 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 10258 10259 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 10260 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 10261 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 10262 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 10263 10264 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 10265 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 10266 10267 if (dev_caps_cfg & 10268 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 10269 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 10270 10271 if (dev_caps_cfg & 10272 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 10273 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 10274 10275 if (dev_caps_cfg & 10276 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 10277 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 10278 10279 hwrm_ver_get_exit: 10280 hwrm_req_drop(bp, req); 10281 return rc; 10282 } 10283 10284 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 10285 { 10286 struct hwrm_fw_set_time_input *req; 10287 struct tm tm; 10288 time64_t now = ktime_get_real_seconds(); 10289 int rc; 10290 10291 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 10292 bp->hwrm_spec_code < 0x10400) 10293 return -EOPNOTSUPP; 10294 10295 time64_to_tm(now, 0, &tm); 10296 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 10297 if (rc) 10298 return rc; 10299 10300 req->year = cpu_to_le16(1900 + tm.tm_year); 10301 req->month = 1 + tm.tm_mon; 10302 req->day = tm.tm_mday; 10303 req->hour = tm.tm_hour; 10304 req->minute = tm.tm_min; 10305 req->second = tm.tm_sec; 10306 return hwrm_req_send(bp, req); 10307 } 10308 10309 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 10310 { 10311 u64 sw_tmp; 10312 10313 hw &= mask; 10314 sw_tmp = (*sw & ~mask) | hw; 10315 if (hw < (*sw & mask)) 10316 sw_tmp += mask + 1; 10317 WRITE_ONCE(*sw, sw_tmp); 10318 } 10319 10320 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 10321 int count, bool ignore_zero) 10322 { 10323 int i; 10324 10325 for (i = 0; i < count; i++) { 10326 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 10327 10328 if (ignore_zero && !hw) 10329 continue; 10330 10331 if (masks[i] == -1ULL) 10332 sw_stats[i] = hw; 10333 else 10334 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 10335 } 10336 } 10337 10338 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 10339 { 10340 if (!stats->hw_stats) 10341 return; 10342 10343 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10344 stats->hw_masks, stats->len / 8, false); 10345 } 10346 10347 static void bnxt_accumulate_all_stats(struct bnxt *bp) 10348 { 10349 struct bnxt_stats_mem *ring0_stats; 10350 bool ignore_zero = false; 10351 int i; 10352 10353 /* Chip bug. Counter intermittently becomes 0. */ 10354 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10355 ignore_zero = true; 10356 10357 for (i = 0; i < bp->cp_nr_rings; i++) { 10358 struct bnxt_napi *bnapi = bp->bnapi[i]; 10359 struct bnxt_cp_ring_info *cpr; 10360 struct bnxt_stats_mem *stats; 10361 10362 cpr = &bnapi->cp_ring; 10363 stats = &cpr->stats; 10364 if (!i) 10365 ring0_stats = stats; 10366 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 10367 ring0_stats->hw_masks, 10368 ring0_stats->len / 8, ignore_zero); 10369 } 10370 if (bp->flags & BNXT_FLAG_PORT_STATS) { 10371 struct bnxt_stats_mem *stats = &bp->port_stats; 10372 __le64 *hw_stats = stats->hw_stats; 10373 u64 *sw_stats = stats->sw_stats; 10374 u64 *masks = stats->hw_masks; 10375 int cnt; 10376 10377 cnt = sizeof(struct rx_port_stats) / 8; 10378 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10379 10380 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10381 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10382 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 10383 cnt = sizeof(struct tx_port_stats) / 8; 10384 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 10385 } 10386 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 10387 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 10388 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 10389 } 10390 } 10391 10392 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 10393 { 10394 struct hwrm_port_qstats_input *req; 10395 struct bnxt_pf_info *pf = &bp->pf; 10396 int rc; 10397 10398 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 10399 return 0; 10400 10401 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10402 return -EOPNOTSUPP; 10403 10404 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 10405 if (rc) 10406 return rc; 10407 10408 req->flags = flags; 10409 req->port_id = cpu_to_le16(pf->port_id); 10410 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 10411 BNXT_TX_PORT_STATS_BYTE_OFFSET); 10412 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 10413 return hwrm_req_send(bp, req); 10414 } 10415 10416 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 10417 { 10418 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 10419 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 10420 struct hwrm_port_qstats_ext_output *resp_qs; 10421 struct hwrm_port_qstats_ext_input *req_qs; 10422 struct bnxt_pf_info *pf = &bp->pf; 10423 u32 tx_stat_size; 10424 int rc; 10425 10426 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 10427 return 0; 10428 10429 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 10430 return -EOPNOTSUPP; 10431 10432 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 10433 if (rc) 10434 return rc; 10435 10436 req_qs->flags = flags; 10437 req_qs->port_id = cpu_to_le16(pf->port_id); 10438 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 10439 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 10440 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 10441 sizeof(struct tx_port_stats_ext) : 0; 10442 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 10443 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 10444 resp_qs = hwrm_req_hold(bp, req_qs); 10445 rc = hwrm_req_send(bp, req_qs); 10446 if (!rc) { 10447 bp->fw_rx_stats_ext_size = 10448 le16_to_cpu(resp_qs->rx_stat_size) / 8; 10449 if (BNXT_FW_MAJ(bp) < 220 && 10450 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 10451 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 10452 10453 bp->fw_tx_stats_ext_size = tx_stat_size ? 10454 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 10455 } else { 10456 bp->fw_rx_stats_ext_size = 0; 10457 bp->fw_tx_stats_ext_size = 0; 10458 } 10459 hwrm_req_drop(bp, req_qs); 10460 10461 if (flags) 10462 return rc; 10463 10464 if (bp->fw_tx_stats_ext_size <= 10465 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 10466 bp->pri2cos_valid = 0; 10467 return rc; 10468 } 10469 10470 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 10471 if (rc) 10472 return rc; 10473 10474 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 10475 10476 resp_qc = hwrm_req_hold(bp, req_qc); 10477 rc = hwrm_req_send(bp, req_qc); 10478 if (!rc) { 10479 u8 *pri2cos; 10480 int i, j; 10481 10482 pri2cos = &resp_qc->pri0_cos_queue_id; 10483 for (i = 0; i < 8; i++) { 10484 u8 queue_id = pri2cos[i]; 10485 u8 queue_idx; 10486 10487 /* Per port queue IDs start from 0, 10, 20, etc */ 10488 queue_idx = queue_id % 10; 10489 if (queue_idx > BNXT_MAX_QUEUE) { 10490 bp->pri2cos_valid = false; 10491 hwrm_req_drop(bp, req_qc); 10492 return rc; 10493 } 10494 for (j = 0; j < bp->max_q; j++) { 10495 if (bp->q_ids[j] == queue_id) 10496 bp->pri2cos_idx[i] = queue_idx; 10497 } 10498 } 10499 bp->pri2cos_valid = true; 10500 } 10501 hwrm_req_drop(bp, req_qc); 10502 10503 return rc; 10504 } 10505 10506 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 10507 { 10508 bnxt_hwrm_tunnel_dst_port_free(bp, 10509 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 10510 bnxt_hwrm_tunnel_dst_port_free(bp, 10511 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 10512 } 10513 10514 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 10515 { 10516 int rc, i; 10517 u32 tpa_flags = 0; 10518 10519 if (set_tpa) 10520 tpa_flags = bp->flags & BNXT_FLAG_TPA; 10521 else if (BNXT_NO_FW_ACCESS(bp)) 10522 return 0; 10523 for (i = 0; i < bp->nr_vnics; i++) { 10524 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 10525 if (rc) { 10526 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 10527 i, rc); 10528 return rc; 10529 } 10530 } 10531 return 0; 10532 } 10533 10534 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 10535 { 10536 int i; 10537 10538 for (i = 0; i < bp->nr_vnics; i++) 10539 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 10540 } 10541 10542 static void bnxt_clear_vnic(struct bnxt *bp) 10543 { 10544 if (!bp->vnic_info) 10545 return; 10546 10547 bnxt_hwrm_clear_vnic_filter(bp); 10548 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 10549 /* clear all RSS setting before free vnic ctx */ 10550 bnxt_hwrm_clear_vnic_rss(bp); 10551 bnxt_hwrm_vnic_ctx_free(bp); 10552 } 10553 /* before free the vnic, undo the vnic tpa settings */ 10554 if (bp->flags & BNXT_FLAG_TPA) 10555 bnxt_set_tpa(bp, false); 10556 bnxt_hwrm_vnic_free(bp); 10557 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10558 bnxt_hwrm_vnic_ctx_free(bp); 10559 } 10560 10561 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 10562 bool irq_re_init) 10563 { 10564 bnxt_clear_vnic(bp); 10565 bnxt_hwrm_ring_free(bp, close_path); 10566 bnxt_hwrm_ring_grp_free(bp); 10567 if (irq_re_init) { 10568 bnxt_hwrm_stat_ctx_free(bp); 10569 bnxt_hwrm_free_tunnel_ports(bp); 10570 } 10571 } 10572 10573 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 10574 { 10575 struct hwrm_func_cfg_input *req; 10576 u8 evb_mode; 10577 int rc; 10578 10579 if (br_mode == BRIDGE_MODE_VEB) 10580 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10581 else if (br_mode == BRIDGE_MODE_VEPA) 10582 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10583 else 10584 return -EINVAL; 10585 10586 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10587 if (rc) 10588 return rc; 10589 10590 req->fid = cpu_to_le16(0xffff); 10591 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10592 req->evb_mode = evb_mode; 10593 return hwrm_req_send(bp, req); 10594 } 10595 10596 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10597 { 10598 struct hwrm_func_cfg_input *req; 10599 int rc; 10600 10601 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10602 return 0; 10603 10604 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10605 if (rc) 10606 return rc; 10607 10608 req->fid = cpu_to_le16(0xffff); 10609 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10610 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10611 if (size == 128) 10612 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10613 10614 return hwrm_req_send(bp, req); 10615 } 10616 10617 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10618 { 10619 int rc; 10620 10621 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10622 goto skip_rss_ctx; 10623 10624 /* allocate context for vnic */ 10625 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10626 if (rc) { 10627 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10628 vnic->vnic_id, rc); 10629 goto vnic_setup_err; 10630 } 10631 bp->rsscos_nr_ctxs++; 10632 10633 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10634 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10635 if (rc) { 10636 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10637 vnic->vnic_id, rc); 10638 goto vnic_setup_err; 10639 } 10640 bp->rsscos_nr_ctxs++; 10641 } 10642 10643 skip_rss_ctx: 10644 /* configure default vnic, ring grp */ 10645 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10646 if (rc) { 10647 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10648 vnic->vnic_id, rc); 10649 goto vnic_setup_err; 10650 } 10651 10652 /* Enable RSS hashing on vnic */ 10653 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10654 if (rc) { 10655 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10656 vnic->vnic_id, rc); 10657 goto vnic_setup_err; 10658 } 10659 10660 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10661 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10662 if (rc) { 10663 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10664 vnic->vnic_id, rc); 10665 } 10666 } 10667 10668 vnic_setup_err: 10669 return rc; 10670 } 10671 10672 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10673 u8 valid) 10674 { 10675 struct hwrm_vnic_update_input *req; 10676 int rc; 10677 10678 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10679 if (rc) 10680 return rc; 10681 10682 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10683 10684 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10685 req->mru = cpu_to_le16(vnic->mru); 10686 10687 req->enables = cpu_to_le32(valid); 10688 10689 return hwrm_req_send(bp, req); 10690 } 10691 10692 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10693 { 10694 int rc; 10695 10696 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10697 if (rc) { 10698 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10699 vnic->vnic_id, rc); 10700 return rc; 10701 } 10702 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10703 if (rc) 10704 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10705 vnic->vnic_id, rc); 10706 return rc; 10707 } 10708 10709 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10710 { 10711 int rc, i, nr_ctxs; 10712 10713 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10714 for (i = 0; i < nr_ctxs; i++) { 10715 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10716 if (rc) { 10717 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10718 vnic->vnic_id, i, rc); 10719 break; 10720 } 10721 bp->rsscos_nr_ctxs++; 10722 } 10723 if (i < nr_ctxs) 10724 return -ENOMEM; 10725 10726 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10727 if (rc) 10728 return rc; 10729 10730 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10731 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10732 if (rc) { 10733 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10734 vnic->vnic_id, rc); 10735 } 10736 } 10737 return rc; 10738 } 10739 10740 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10741 { 10742 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10743 return __bnxt_setup_vnic_p5(bp, vnic); 10744 else 10745 return __bnxt_setup_vnic(bp, vnic); 10746 } 10747 10748 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10749 struct bnxt_vnic_info *vnic, 10750 u16 start_rx_ring_idx, int rx_rings) 10751 { 10752 int rc; 10753 10754 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10755 if (rc) { 10756 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10757 vnic->vnic_id, rc); 10758 return rc; 10759 } 10760 return bnxt_setup_vnic(bp, vnic); 10761 } 10762 10763 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10764 { 10765 struct bnxt_vnic_info *vnic; 10766 int i, rc = 0; 10767 10768 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10769 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10770 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10771 } 10772 10773 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10774 return 0; 10775 10776 for (i = 0; i < bp->rx_nr_rings; i++) { 10777 u16 vnic_id = i + 1; 10778 u16 ring_id = i; 10779 10780 if (vnic_id >= bp->nr_vnics) 10781 break; 10782 10783 vnic = &bp->vnic_info[vnic_id]; 10784 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10785 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10786 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10787 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10788 break; 10789 } 10790 return rc; 10791 } 10792 10793 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10794 bool all) 10795 { 10796 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10797 struct bnxt_filter_base *usr_fltr, *tmp; 10798 struct bnxt_ntuple_filter *ntp_fltr; 10799 int i; 10800 10801 if (netif_running(bp->dev)) { 10802 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10803 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10804 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10805 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10806 } 10807 } 10808 if (!all) 10809 return; 10810 10811 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10812 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10813 usr_fltr->fw_vnic_id == rss_ctx->index) { 10814 ntp_fltr = container_of(usr_fltr, 10815 struct bnxt_ntuple_filter, 10816 base); 10817 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10818 bnxt_del_ntp_filter(bp, ntp_fltr); 10819 bnxt_del_one_usr_fltr(bp, usr_fltr); 10820 } 10821 } 10822 10823 if (vnic->rss_table) 10824 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10825 vnic->rss_table, 10826 vnic->rss_table_dma_addr); 10827 bp->num_rss_ctx--; 10828 } 10829 10830 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10831 int rxr_id) 10832 { 10833 u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 10834 int i, vnic_rx; 10835 10836 /* Ntuple VNIC always has all the rx rings. Any change of ring id 10837 * must be updated because a future filter may use it. 10838 */ 10839 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 10840 return true; 10841 10842 for (i = 0; i < tbl_size; i++) { 10843 if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 10844 vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 10845 else 10846 vnic_rx = bp->rss_indir_tbl[i]; 10847 10848 if (rxr_id == vnic_rx) 10849 return true; 10850 } 10851 10852 return false; 10853 } 10854 10855 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10856 u16 mru, int rxr_id) 10857 { 10858 int rc; 10859 10860 if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id)) 10861 return 0; 10862 10863 if (mru) { 10864 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10865 if (rc) { 10866 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10867 vnic->vnic_id, rc); 10868 return rc; 10869 } 10870 } 10871 vnic->mru = mru; 10872 bnxt_hwrm_vnic_update(bp, vnic, 10873 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 10874 10875 return 0; 10876 } 10877 10878 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id) 10879 { 10880 struct ethtool_rxfh_context *ctx; 10881 unsigned long context; 10882 int rc; 10883 10884 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10885 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10886 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10887 10888 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id); 10889 if (rc) 10890 return rc; 10891 } 10892 10893 return 0; 10894 } 10895 10896 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10897 { 10898 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10899 struct ethtool_rxfh_context *ctx; 10900 unsigned long context; 10901 10902 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10903 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10904 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10905 10906 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10907 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10908 __bnxt_setup_vnic_p5(bp, vnic)) { 10909 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10910 rss_ctx->index); 10911 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10912 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10913 } 10914 } 10915 } 10916 10917 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10918 { 10919 struct ethtool_rxfh_context *ctx; 10920 unsigned long context; 10921 10922 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10923 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10924 10925 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10926 } 10927 } 10928 10929 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10930 static bool bnxt_promisc_ok(struct bnxt *bp) 10931 { 10932 #ifdef CONFIG_BNXT_SRIOV 10933 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10934 return false; 10935 #endif 10936 return true; 10937 } 10938 10939 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10940 { 10941 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10942 unsigned int rc = 0; 10943 10944 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10945 if (rc) { 10946 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10947 rc); 10948 return rc; 10949 } 10950 10951 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10952 if (rc) { 10953 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10954 rc); 10955 return rc; 10956 } 10957 return rc; 10958 } 10959 10960 static int bnxt_cfg_rx_mode(struct bnxt *); 10961 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10962 10963 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10964 { 10965 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10966 int rc = 0; 10967 unsigned int rx_nr_rings = bp->rx_nr_rings; 10968 10969 if (irq_re_init) { 10970 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10971 if (rc) { 10972 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10973 rc); 10974 goto err_out; 10975 } 10976 } 10977 10978 rc = bnxt_hwrm_ring_alloc(bp); 10979 if (rc) { 10980 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10981 goto err_out; 10982 } 10983 10984 rc = bnxt_hwrm_ring_grp_alloc(bp); 10985 if (rc) { 10986 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10987 goto err_out; 10988 } 10989 10990 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10991 rx_nr_rings--; 10992 10993 /* default vnic 0 */ 10994 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10995 if (rc) { 10996 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10997 goto err_out; 10998 } 10999 11000 if (BNXT_VF(bp)) 11001 bnxt_hwrm_func_qcfg(bp); 11002 11003 rc = bnxt_setup_vnic(bp, vnic); 11004 if (rc) 11005 goto err_out; 11006 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 11007 bnxt_hwrm_update_rss_hash_cfg(bp); 11008 11009 if (bp->flags & BNXT_FLAG_RFS) { 11010 rc = bnxt_alloc_rfs_vnics(bp); 11011 if (rc) 11012 goto err_out; 11013 } 11014 11015 if (bp->flags & BNXT_FLAG_TPA) { 11016 rc = bnxt_set_tpa(bp, true); 11017 if (rc) 11018 goto err_out; 11019 } 11020 11021 if (BNXT_VF(bp)) 11022 bnxt_update_vf_mac(bp); 11023 11024 /* Filter for default vnic 0 */ 11025 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 11026 if (rc) { 11027 if (BNXT_VF(bp) && rc == -ENODEV) 11028 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 11029 else 11030 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 11031 goto err_out; 11032 } 11033 vnic->uc_filter_count = 1; 11034 11035 vnic->rx_mask = 0; 11036 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 11037 goto skip_rx_mask; 11038 11039 if (bp->dev->flags & IFF_BROADCAST) 11040 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 11041 11042 if (bp->dev->flags & IFF_PROMISC) 11043 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 11044 11045 if (bp->dev->flags & IFF_ALLMULTI) { 11046 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 11047 vnic->mc_list_count = 0; 11048 } else if (bp->dev->flags & IFF_MULTICAST) { 11049 u32 mask = 0; 11050 11051 bnxt_mc_list_updated(bp, &mask); 11052 vnic->rx_mask |= mask; 11053 } 11054 11055 rc = bnxt_cfg_rx_mode(bp); 11056 if (rc) 11057 goto err_out; 11058 11059 skip_rx_mask: 11060 rc = bnxt_hwrm_set_coal(bp); 11061 if (rc) 11062 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 11063 rc); 11064 11065 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11066 rc = bnxt_setup_nitroa0_vnic(bp); 11067 if (rc) 11068 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 11069 rc); 11070 } 11071 11072 if (BNXT_VF(bp)) { 11073 bnxt_hwrm_func_qcfg(bp); 11074 netdev_update_features(bp->dev); 11075 } 11076 11077 return 0; 11078 11079 err_out: 11080 bnxt_hwrm_resource_free(bp, 0, true); 11081 11082 return rc; 11083 } 11084 11085 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 11086 { 11087 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 11088 return 0; 11089 } 11090 11091 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 11092 { 11093 bnxt_init_cp_rings(bp); 11094 bnxt_init_rx_rings(bp); 11095 bnxt_init_tx_rings(bp); 11096 bnxt_init_ring_grps(bp, irq_re_init); 11097 bnxt_init_vnics(bp); 11098 11099 return bnxt_init_chip(bp, irq_re_init); 11100 } 11101 11102 static int bnxt_set_real_num_queues(struct bnxt *bp) 11103 { 11104 int rc; 11105 struct net_device *dev = bp->dev; 11106 11107 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 11108 bp->tx_nr_rings_xdp); 11109 if (rc) 11110 return rc; 11111 11112 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 11113 if (rc) 11114 return rc; 11115 11116 #ifdef CONFIG_RFS_ACCEL 11117 if (bp->flags & BNXT_FLAG_RFS) 11118 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 11119 #endif 11120 11121 return rc; 11122 } 11123 11124 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11125 bool shared) 11126 { 11127 int _rx = *rx, _tx = *tx; 11128 11129 if (shared) { 11130 *rx = min_t(int, _rx, max); 11131 *tx = min_t(int, _tx, max); 11132 } else { 11133 if (max < 2) 11134 return -ENOMEM; 11135 11136 while (_rx + _tx > max) { 11137 if (_rx > _tx && _rx > 1) 11138 _rx--; 11139 else if (_tx > 1) 11140 _tx--; 11141 } 11142 *rx = _rx; 11143 *tx = _tx; 11144 } 11145 return 0; 11146 } 11147 11148 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 11149 { 11150 return (tx - tx_xdp) / tx_sets + tx_xdp; 11151 } 11152 11153 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 11154 { 11155 int tcs = bp->num_tc; 11156 11157 if (!tcs) 11158 tcs = 1; 11159 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 11160 } 11161 11162 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 11163 { 11164 int tcs = bp->num_tc; 11165 11166 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 11167 bp->tx_nr_rings_xdp; 11168 } 11169 11170 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 11171 bool sh) 11172 { 11173 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 11174 11175 if (tx_cp != *tx) { 11176 int tx_saved = tx_cp, rc; 11177 11178 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 11179 if (rc) 11180 return rc; 11181 if (tx_cp != tx_saved) 11182 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 11183 return 0; 11184 } 11185 return __bnxt_trim_rings(bp, rx, tx, max, sh); 11186 } 11187 11188 static void bnxt_setup_msix(struct bnxt *bp) 11189 { 11190 const int len = sizeof(bp->irq_tbl[0].name); 11191 struct net_device *dev = bp->dev; 11192 int tcs, i; 11193 11194 tcs = bp->num_tc; 11195 if (tcs) { 11196 int i, off, count; 11197 11198 for (i = 0; i < tcs; i++) { 11199 count = bp->tx_nr_rings_per_tc; 11200 off = BNXT_TC_TO_RING_BASE(bp, i); 11201 netdev_set_tc_queue(dev, i, count, off); 11202 } 11203 } 11204 11205 for (i = 0; i < bp->cp_nr_rings; i++) { 11206 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11207 char *attr; 11208 11209 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 11210 attr = "TxRx"; 11211 else if (i < bp->rx_nr_rings) 11212 attr = "rx"; 11213 else 11214 attr = "tx"; 11215 11216 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 11217 attr, i); 11218 bp->irq_tbl[map_idx].handler = bnxt_msix; 11219 } 11220 } 11221 11222 static int bnxt_init_int_mode(struct bnxt *bp); 11223 11224 static int bnxt_change_msix(struct bnxt *bp, int total) 11225 { 11226 struct msi_map map; 11227 int i; 11228 11229 /* add MSIX to the end if needed */ 11230 for (i = bp->total_irqs; i < total; i++) { 11231 map = pci_msix_alloc_irq_at(bp->pdev, i, NULL); 11232 if (map.index < 0) 11233 return bp->total_irqs; 11234 bp->irq_tbl[i].vector = map.virq; 11235 bp->total_irqs++; 11236 } 11237 11238 /* trim MSIX from the end if needed */ 11239 for (i = bp->total_irqs; i > total; i--) { 11240 map.index = i - 1; 11241 map.virq = bp->irq_tbl[i - 1].vector; 11242 pci_msix_free_irq(bp->pdev, map); 11243 bp->total_irqs--; 11244 } 11245 return bp->total_irqs; 11246 } 11247 11248 static int bnxt_setup_int_mode(struct bnxt *bp) 11249 { 11250 int rc; 11251 11252 if (!bp->irq_tbl) { 11253 rc = bnxt_init_int_mode(bp); 11254 if (rc || !bp->irq_tbl) 11255 return rc ?: -ENODEV; 11256 } 11257 11258 bnxt_setup_msix(bp); 11259 11260 rc = bnxt_set_real_num_queues(bp); 11261 return rc; 11262 } 11263 11264 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 11265 { 11266 return bp->hw_resc.max_rsscos_ctxs; 11267 } 11268 11269 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 11270 { 11271 return bp->hw_resc.max_vnics; 11272 } 11273 11274 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 11275 { 11276 return bp->hw_resc.max_stat_ctxs; 11277 } 11278 11279 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 11280 { 11281 return bp->hw_resc.max_cp_rings; 11282 } 11283 11284 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 11285 { 11286 unsigned int cp = bp->hw_resc.max_cp_rings; 11287 11288 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 11289 cp -= bnxt_get_ulp_msix_num(bp); 11290 11291 return cp; 11292 } 11293 11294 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 11295 { 11296 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11297 11298 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11299 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 11300 11301 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 11302 } 11303 11304 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 11305 { 11306 bp->hw_resc.max_irqs = max_irqs; 11307 } 11308 11309 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 11310 { 11311 unsigned int cp; 11312 11313 cp = bnxt_get_max_func_cp_rings_for_en(bp); 11314 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11315 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 11316 else 11317 return cp - bp->cp_nr_rings; 11318 } 11319 11320 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 11321 { 11322 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 11323 } 11324 11325 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 11326 { 11327 int max_irq = bnxt_get_max_func_irqs(bp); 11328 int total_req = bp->cp_nr_rings + num; 11329 11330 if (max_irq < total_req) { 11331 num = max_irq - bp->cp_nr_rings; 11332 if (num <= 0) 11333 return 0; 11334 } 11335 return num; 11336 } 11337 11338 static int bnxt_get_num_msix(struct bnxt *bp) 11339 { 11340 if (!BNXT_NEW_RM(bp)) 11341 return bnxt_get_max_func_irqs(bp); 11342 11343 return bnxt_nq_rings_in_use(bp); 11344 } 11345 11346 static int bnxt_init_int_mode(struct bnxt *bp) 11347 { 11348 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size; 11349 11350 total_vecs = bnxt_get_num_msix(bp); 11351 max = bnxt_get_max_func_irqs(bp); 11352 if (total_vecs > max) 11353 total_vecs = max; 11354 11355 if (!total_vecs) 11356 return 0; 11357 11358 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 11359 min = 2; 11360 11361 total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs, 11362 PCI_IRQ_MSIX); 11363 ulp_msix = bnxt_get_ulp_msix_num(bp); 11364 if (total_vecs < 0 || total_vecs < ulp_msix) { 11365 rc = -ENODEV; 11366 goto msix_setup_exit; 11367 } 11368 11369 tbl_size = total_vecs; 11370 if (pci_msix_can_alloc_dyn(bp->pdev)) 11371 tbl_size = max; 11372 bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL); 11373 if (bp->irq_tbl) { 11374 for (i = 0; i < total_vecs; i++) 11375 bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i); 11376 11377 bp->total_irqs = total_vecs; 11378 /* Trim rings based upon num of vectors allocated */ 11379 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 11380 total_vecs - ulp_msix, min == 1); 11381 if (rc) 11382 goto msix_setup_exit; 11383 11384 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 11385 bp->cp_nr_rings = (min == 1) ? 11386 max_t(int, tx_cp, bp->rx_nr_rings) : 11387 tx_cp + bp->rx_nr_rings; 11388 11389 } else { 11390 rc = -ENOMEM; 11391 goto msix_setup_exit; 11392 } 11393 return 0; 11394 11395 msix_setup_exit: 11396 netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc); 11397 kfree(bp->irq_tbl); 11398 bp->irq_tbl = NULL; 11399 pci_free_irq_vectors(bp->pdev); 11400 return rc; 11401 } 11402 11403 static void bnxt_clear_int_mode(struct bnxt *bp) 11404 { 11405 pci_free_irq_vectors(bp->pdev); 11406 11407 kfree(bp->irq_tbl); 11408 bp->irq_tbl = NULL; 11409 } 11410 11411 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 11412 { 11413 bool irq_cleared = false; 11414 bool irq_change = false; 11415 int tcs = bp->num_tc; 11416 int irqs_required; 11417 int rc; 11418 11419 if (!bnxt_need_reserve_rings(bp)) 11420 return 0; 11421 11422 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 11423 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 11424 11425 if (ulp_msix > bp->ulp_num_msix_want) 11426 ulp_msix = bp->ulp_num_msix_want; 11427 irqs_required = ulp_msix + bp->cp_nr_rings; 11428 } else { 11429 irqs_required = bnxt_get_num_msix(bp); 11430 } 11431 11432 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 11433 irq_change = true; 11434 if (!pci_msix_can_alloc_dyn(bp->pdev)) { 11435 bnxt_ulp_irq_stop(bp); 11436 bnxt_clear_int_mode(bp); 11437 irq_cleared = true; 11438 } 11439 } 11440 rc = __bnxt_reserve_rings(bp); 11441 if (irq_cleared) { 11442 if (!rc) 11443 rc = bnxt_init_int_mode(bp); 11444 bnxt_ulp_irq_restart(bp, rc); 11445 } else if (irq_change && !rc) { 11446 if (bnxt_change_msix(bp, irqs_required) != irqs_required) 11447 rc = -ENOSPC; 11448 } 11449 if (rc) { 11450 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 11451 return rc; 11452 } 11453 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 11454 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 11455 netdev_err(bp->dev, "tx ring reservation failure\n"); 11456 netdev_reset_tc(bp->dev); 11457 bp->num_tc = 0; 11458 if (bp->tx_nr_rings_xdp) 11459 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 11460 else 11461 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 11462 return -ENOMEM; 11463 } 11464 return 0; 11465 } 11466 11467 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx) 11468 { 11469 struct bnxt_tx_ring_info *txr; 11470 struct netdev_queue *txq; 11471 struct bnxt_napi *bnapi; 11472 int i; 11473 11474 bnapi = bp->bnapi[idx]; 11475 bnxt_for_each_napi_tx(i, bnapi, txr) { 11476 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11477 synchronize_net(); 11478 11479 if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) { 11480 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11481 if (txq) { 11482 __netif_tx_lock_bh(txq); 11483 netif_tx_stop_queue(txq); 11484 __netif_tx_unlock_bh(txq); 11485 } 11486 } 11487 11488 if (!bp->tph_mode) 11489 continue; 11490 11491 bnxt_hwrm_tx_ring_free(bp, txr, true); 11492 bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr); 11493 bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index); 11494 bnxt_clear_one_cp_ring(bp, txr->tx_cpr); 11495 } 11496 } 11497 11498 static int bnxt_tx_queue_start(struct bnxt *bp, int idx) 11499 { 11500 struct bnxt_tx_ring_info *txr; 11501 struct netdev_queue *txq; 11502 struct bnxt_napi *bnapi; 11503 int rc, i; 11504 11505 bnapi = bp->bnapi[idx]; 11506 /* All rings have been reserved and previously allocated. 11507 * Reallocating with the same parameters should never fail. 11508 */ 11509 bnxt_for_each_napi_tx(i, bnapi, txr) { 11510 if (!bp->tph_mode) 11511 goto start_tx; 11512 11513 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr); 11514 if (rc) 11515 return rc; 11516 11517 rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false); 11518 if (rc) 11519 return rc; 11520 11521 txr->tx_prod = 0; 11522 txr->tx_cons = 0; 11523 txr->tx_hw_cons = 0; 11524 start_tx: 11525 WRITE_ONCE(txr->dev_state, 0); 11526 synchronize_net(); 11527 11528 if (bnapi->flags & BNXT_NAPI_FLAG_XDP) 11529 continue; 11530 11531 txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 11532 if (txq) 11533 netif_tx_start_queue(txq); 11534 } 11535 11536 return 0; 11537 } 11538 11539 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify, 11540 const cpumask_t *mask) 11541 { 11542 struct bnxt_irq *irq; 11543 u16 tag; 11544 int err; 11545 11546 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11547 11548 if (!irq->bp->tph_mode) 11549 return; 11550 11551 cpumask_copy(irq->cpu_mask, mask); 11552 11553 if (irq->ring_nr >= irq->bp->rx_nr_rings) 11554 return; 11555 11556 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11557 cpumask_first(irq->cpu_mask), &tag)) 11558 return; 11559 11560 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag)) 11561 return; 11562 11563 netdev_lock(irq->bp->dev); 11564 if (netif_running(irq->bp->dev)) { 11565 err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr); 11566 if (err) 11567 netdev_err(irq->bp->dev, 11568 "RX queue restart failed: err=%d\n", err); 11569 } 11570 netdev_unlock(irq->bp->dev); 11571 } 11572 11573 static void bnxt_irq_affinity_release(struct kref *ref) 11574 { 11575 struct irq_affinity_notify *notify = 11576 container_of(ref, struct irq_affinity_notify, kref); 11577 struct bnxt_irq *irq; 11578 11579 irq = container_of(notify, struct bnxt_irq, affinity_notify); 11580 11581 if (!irq->bp->tph_mode) 11582 return; 11583 11584 if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) { 11585 netdev_err(irq->bp->dev, 11586 "Setting ST=0 for MSIX entry %d failed\n", 11587 irq->msix_nr); 11588 return; 11589 } 11590 } 11591 11592 static void bnxt_release_irq_notifier(struct bnxt_irq *irq) 11593 { 11594 irq_set_affinity_notifier(irq->vector, NULL); 11595 } 11596 11597 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq) 11598 { 11599 struct irq_affinity_notify *notify; 11600 11601 irq->bp = bp; 11602 11603 /* Nothing to do if TPH is not enabled */ 11604 if (!bp->tph_mode) 11605 return; 11606 11607 /* Register IRQ affinity notifier */ 11608 notify = &irq->affinity_notify; 11609 notify->irq = irq->vector; 11610 notify->notify = bnxt_irq_affinity_notify; 11611 notify->release = bnxt_irq_affinity_release; 11612 11613 irq_set_affinity_notifier(irq->vector, notify); 11614 } 11615 11616 static void bnxt_free_irq(struct bnxt *bp) 11617 { 11618 struct bnxt_irq *irq; 11619 int i; 11620 11621 #ifdef CONFIG_RFS_ACCEL 11622 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 11623 bp->dev->rx_cpu_rmap = NULL; 11624 #endif 11625 if (!bp->irq_tbl || !bp->bnapi) 11626 return; 11627 11628 for (i = 0; i < bp->cp_nr_rings; i++) { 11629 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11630 11631 irq = &bp->irq_tbl[map_idx]; 11632 if (irq->requested) { 11633 if (irq->have_cpumask) { 11634 irq_update_affinity_hint(irq->vector, NULL); 11635 free_cpumask_var(irq->cpu_mask); 11636 irq->have_cpumask = 0; 11637 } 11638 11639 bnxt_release_irq_notifier(irq); 11640 11641 free_irq(irq->vector, bp->bnapi[i]); 11642 } 11643 11644 irq->requested = 0; 11645 } 11646 11647 /* Disable TPH support */ 11648 pcie_disable_tph(bp->pdev); 11649 bp->tph_mode = 0; 11650 } 11651 11652 static int bnxt_request_irq(struct bnxt *bp) 11653 { 11654 struct cpu_rmap *rmap = NULL; 11655 int i, j, rc = 0; 11656 unsigned long flags = 0; 11657 11658 rc = bnxt_setup_int_mode(bp); 11659 if (rc) { 11660 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 11661 rc); 11662 return rc; 11663 } 11664 #ifdef CONFIG_RFS_ACCEL 11665 rmap = bp->dev->rx_cpu_rmap; 11666 #endif 11667 11668 /* Enable TPH support as part of IRQ request */ 11669 rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE); 11670 if (!rc) 11671 bp->tph_mode = PCI_TPH_ST_IV_MODE; 11672 11673 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 11674 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 11675 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 11676 11677 if (IS_ENABLED(CONFIG_RFS_ACCEL) && 11678 rmap && bp->bnapi[i]->rx_ring) { 11679 rc = irq_cpu_rmap_add(rmap, irq->vector); 11680 if (rc) 11681 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 11682 j); 11683 j++; 11684 } 11685 11686 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 11687 bp->bnapi[i]); 11688 if (rc) 11689 break; 11690 11691 netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector); 11692 irq->requested = 1; 11693 11694 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 11695 int numa_node = dev_to_node(&bp->pdev->dev); 11696 u16 tag; 11697 11698 irq->have_cpumask = 1; 11699 irq->msix_nr = map_idx; 11700 irq->ring_nr = i; 11701 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 11702 irq->cpu_mask); 11703 rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask); 11704 if (rc) { 11705 netdev_warn(bp->dev, 11706 "Update affinity hint failed, IRQ = %d\n", 11707 irq->vector); 11708 break; 11709 } 11710 11711 bnxt_register_irq_notifier(bp, irq); 11712 11713 /* Init ST table entry */ 11714 if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM, 11715 cpumask_first(irq->cpu_mask), 11716 &tag)) 11717 continue; 11718 11719 pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag); 11720 } 11721 } 11722 return rc; 11723 } 11724 11725 static void bnxt_del_napi(struct bnxt *bp) 11726 { 11727 int i; 11728 11729 if (!bp->bnapi) 11730 return; 11731 11732 for (i = 0; i < bp->rx_nr_rings; i++) 11733 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 11734 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 11735 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 11736 11737 for (i = 0; i < bp->cp_nr_rings; i++) { 11738 struct bnxt_napi *bnapi = bp->bnapi[i]; 11739 11740 __netif_napi_del_locked(&bnapi->napi); 11741 } 11742 /* We called __netif_napi_del_locked(), we need 11743 * to respect an RCU grace period before freeing napi structures. 11744 */ 11745 synchronize_net(); 11746 } 11747 11748 static void bnxt_init_napi(struct bnxt *bp) 11749 { 11750 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 11751 unsigned int cp_nr_rings = bp->cp_nr_rings; 11752 struct bnxt_napi *bnapi; 11753 int i; 11754 11755 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 11756 poll_fn = bnxt_poll_p5; 11757 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 11758 cp_nr_rings--; 11759 11760 set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11761 11762 for (i = 0; i < cp_nr_rings; i++) { 11763 bnapi = bp->bnapi[i]; 11764 netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn, 11765 bnapi->index); 11766 } 11767 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 11768 bnapi = bp->bnapi[cp_nr_rings]; 11769 netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0); 11770 } 11771 } 11772 11773 static void bnxt_disable_napi(struct bnxt *bp) 11774 { 11775 int i; 11776 11777 if (!bp->bnapi || 11778 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 11779 return; 11780 11781 for (i = 0; i < bp->cp_nr_rings; i++) { 11782 struct bnxt_napi *bnapi = bp->bnapi[i]; 11783 struct bnxt_cp_ring_info *cpr; 11784 11785 cpr = &bnapi->cp_ring; 11786 if (bnapi->tx_fault) 11787 cpr->sw_stats->tx.tx_resets++; 11788 if (bnapi->in_reset) 11789 cpr->sw_stats->rx.rx_resets++; 11790 napi_disable_locked(&bnapi->napi); 11791 } 11792 } 11793 11794 static void bnxt_enable_napi(struct bnxt *bp) 11795 { 11796 int i; 11797 11798 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11799 for (i = 0; i < bp->cp_nr_rings; i++) { 11800 struct bnxt_napi *bnapi = bp->bnapi[i]; 11801 struct bnxt_cp_ring_info *cpr; 11802 11803 bnapi->tx_fault = 0; 11804 11805 cpr = &bnapi->cp_ring; 11806 bnapi->in_reset = false; 11807 11808 if (bnapi->rx_ring) { 11809 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11810 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11811 } 11812 napi_enable_locked(&bnapi->napi); 11813 } 11814 } 11815 11816 void bnxt_tx_disable(struct bnxt *bp) 11817 { 11818 int i; 11819 struct bnxt_tx_ring_info *txr; 11820 11821 if (bp->tx_ring) { 11822 for (i = 0; i < bp->tx_nr_rings; i++) { 11823 txr = &bp->tx_ring[i]; 11824 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11825 } 11826 } 11827 /* Make sure napi polls see @dev_state change */ 11828 synchronize_net(); 11829 /* Drop carrier first to prevent TX timeout */ 11830 netif_carrier_off(bp->dev); 11831 /* Stop all TX queues */ 11832 netif_tx_disable(bp->dev); 11833 } 11834 11835 void bnxt_tx_enable(struct bnxt *bp) 11836 { 11837 int i; 11838 struct bnxt_tx_ring_info *txr; 11839 11840 for (i = 0; i < bp->tx_nr_rings; i++) { 11841 txr = &bp->tx_ring[i]; 11842 WRITE_ONCE(txr->dev_state, 0); 11843 } 11844 /* Make sure napi polls see @dev_state change */ 11845 synchronize_net(); 11846 netif_tx_wake_all_queues(bp->dev); 11847 if (BNXT_LINK_IS_UP(bp)) 11848 netif_carrier_on(bp->dev); 11849 } 11850 11851 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11852 { 11853 u8 active_fec = link_info->active_fec_sig_mode & 11854 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11855 11856 switch (active_fec) { 11857 default: 11858 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11859 return "None"; 11860 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11861 return "Clause 74 BaseR"; 11862 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11863 return "Clause 91 RS(528,514)"; 11864 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11865 return "Clause 91 RS544_1XN"; 11866 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11867 return "Clause 91 RS(544,514)"; 11868 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11869 return "Clause 91 RS272_1XN"; 11870 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11871 return "Clause 91 RS(272,257)"; 11872 } 11873 } 11874 11875 void bnxt_report_link(struct bnxt *bp) 11876 { 11877 if (BNXT_LINK_IS_UP(bp)) { 11878 const char *signal = ""; 11879 const char *flow_ctrl; 11880 const char *duplex; 11881 u32 speed; 11882 u16 fec; 11883 11884 netif_carrier_on(bp->dev); 11885 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11886 if (speed == SPEED_UNKNOWN) { 11887 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11888 return; 11889 } 11890 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11891 duplex = "full"; 11892 else 11893 duplex = "half"; 11894 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11895 flow_ctrl = "ON - receive & transmit"; 11896 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11897 flow_ctrl = "ON - transmit"; 11898 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11899 flow_ctrl = "ON - receive"; 11900 else 11901 flow_ctrl = "none"; 11902 if (bp->link_info.phy_qcfg_resp.option_flags & 11903 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11904 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11905 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11906 switch (sig_mode) { 11907 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11908 signal = "(NRZ) "; 11909 break; 11910 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11911 signal = "(PAM4 56Gbps) "; 11912 break; 11913 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11914 signal = "(PAM4 112Gbps) "; 11915 break; 11916 default: 11917 break; 11918 } 11919 } 11920 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11921 speed, signal, duplex, flow_ctrl); 11922 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11923 netdev_info(bp->dev, "EEE is %s\n", 11924 bp->eee.eee_active ? "active" : 11925 "not active"); 11926 fec = bp->link_info.fec_cfg; 11927 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11928 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11929 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11930 bnxt_report_fec(&bp->link_info)); 11931 } else { 11932 netif_carrier_off(bp->dev); 11933 netdev_err(bp->dev, "NIC Link is Down\n"); 11934 } 11935 } 11936 11937 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11938 { 11939 if (!resp->supported_speeds_auto_mode && 11940 !resp->supported_speeds_force_mode && 11941 !resp->supported_pam4_speeds_auto_mode && 11942 !resp->supported_pam4_speeds_force_mode && 11943 !resp->supported_speeds2_auto_mode && 11944 !resp->supported_speeds2_force_mode) 11945 return true; 11946 return false; 11947 } 11948 11949 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11950 { 11951 struct bnxt_link_info *link_info = &bp->link_info; 11952 struct hwrm_port_phy_qcaps_output *resp; 11953 struct hwrm_port_phy_qcaps_input *req; 11954 int rc = 0; 11955 11956 if (bp->hwrm_spec_code < 0x10201) 11957 return 0; 11958 11959 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11960 if (rc) 11961 return rc; 11962 11963 resp = hwrm_req_hold(bp, req); 11964 rc = hwrm_req_send(bp, req); 11965 if (rc) 11966 goto hwrm_phy_qcaps_exit; 11967 11968 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11969 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11970 struct ethtool_keee *eee = &bp->eee; 11971 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11972 11973 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11974 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11975 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11976 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11977 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11978 } 11979 11980 if (bp->hwrm_spec_code >= 0x10a01) { 11981 if (bnxt_phy_qcaps_no_speed(resp)) { 11982 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11983 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11984 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11985 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11986 netdev_info(bp->dev, "Ethernet link enabled\n"); 11987 /* Phy re-enabled, reprobe the speeds */ 11988 link_info->support_auto_speeds = 0; 11989 link_info->support_pam4_auto_speeds = 0; 11990 link_info->support_auto_speeds2 = 0; 11991 } 11992 } 11993 if (resp->supported_speeds_auto_mode) 11994 link_info->support_auto_speeds = 11995 le16_to_cpu(resp->supported_speeds_auto_mode); 11996 if (resp->supported_pam4_speeds_auto_mode) 11997 link_info->support_pam4_auto_speeds = 11998 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11999 if (resp->supported_speeds2_auto_mode) 12000 link_info->support_auto_speeds2 = 12001 le16_to_cpu(resp->supported_speeds2_auto_mode); 12002 12003 bp->port_count = resp->port_cnt; 12004 12005 hwrm_phy_qcaps_exit: 12006 hwrm_req_drop(bp, req); 12007 return rc; 12008 } 12009 12010 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp) 12011 { 12012 struct hwrm_port_mac_qcaps_output *resp; 12013 struct hwrm_port_mac_qcaps_input *req; 12014 int rc; 12015 12016 if (bp->hwrm_spec_code < 0x10a03) 12017 return; 12018 12019 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS); 12020 if (rc) 12021 return; 12022 12023 resp = hwrm_req_hold(bp, req); 12024 rc = hwrm_req_send_silent(bp, req); 12025 if (!rc) 12026 bp->mac_flags = resp->flags; 12027 hwrm_req_drop(bp, req); 12028 } 12029 12030 static bool bnxt_support_dropped(u16 advertising, u16 supported) 12031 { 12032 u16 diff = advertising ^ supported; 12033 12034 return ((supported | diff) != supported); 12035 } 12036 12037 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 12038 { 12039 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 12040 12041 /* Check if any advertised speeds are no longer supported. The caller 12042 * holds the link_lock mutex, so we can modify link_info settings. 12043 */ 12044 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12045 if (bnxt_support_dropped(link_info->advertising, 12046 link_info->support_auto_speeds2)) { 12047 link_info->advertising = link_info->support_auto_speeds2; 12048 return true; 12049 } 12050 return false; 12051 } 12052 if (bnxt_support_dropped(link_info->advertising, 12053 link_info->support_auto_speeds)) { 12054 link_info->advertising = link_info->support_auto_speeds; 12055 return true; 12056 } 12057 if (bnxt_support_dropped(link_info->advertising_pam4, 12058 link_info->support_pam4_auto_speeds)) { 12059 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 12060 return true; 12061 } 12062 return false; 12063 } 12064 12065 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 12066 { 12067 struct bnxt_link_info *link_info = &bp->link_info; 12068 struct hwrm_port_phy_qcfg_output *resp; 12069 struct hwrm_port_phy_qcfg_input *req; 12070 u8 link_state = link_info->link_state; 12071 bool support_changed; 12072 int rc; 12073 12074 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 12075 if (rc) 12076 return rc; 12077 12078 resp = hwrm_req_hold(bp, req); 12079 rc = hwrm_req_send(bp, req); 12080 if (rc) { 12081 hwrm_req_drop(bp, req); 12082 if (BNXT_VF(bp) && rc == -ENODEV) { 12083 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 12084 rc = 0; 12085 } 12086 return rc; 12087 } 12088 12089 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 12090 link_info->phy_link_status = resp->link; 12091 link_info->duplex = resp->duplex_cfg; 12092 if (bp->hwrm_spec_code >= 0x10800) 12093 link_info->duplex = resp->duplex_state; 12094 link_info->pause = resp->pause; 12095 link_info->auto_mode = resp->auto_mode; 12096 link_info->auto_pause_setting = resp->auto_pause; 12097 link_info->lp_pause = resp->link_partner_adv_pause; 12098 link_info->force_pause_setting = resp->force_pause; 12099 link_info->duplex_setting = resp->duplex_cfg; 12100 if (link_info->phy_link_status == BNXT_LINK_LINK) { 12101 link_info->link_speed = le16_to_cpu(resp->link_speed); 12102 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 12103 link_info->active_lanes = resp->active_lanes; 12104 } else { 12105 link_info->link_speed = 0; 12106 link_info->active_lanes = 0; 12107 } 12108 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 12109 link_info->force_pam4_link_speed = 12110 le16_to_cpu(resp->force_pam4_link_speed); 12111 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 12112 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 12113 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 12114 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 12115 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 12116 link_info->auto_pam4_link_speeds = 12117 le16_to_cpu(resp->auto_pam4_link_speed_mask); 12118 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 12119 link_info->lp_auto_link_speeds = 12120 le16_to_cpu(resp->link_partner_adv_speeds); 12121 link_info->lp_auto_pam4_link_speeds = 12122 resp->link_partner_pam4_adv_speeds; 12123 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 12124 link_info->phy_ver[0] = resp->phy_maj; 12125 link_info->phy_ver[1] = resp->phy_min; 12126 link_info->phy_ver[2] = resp->phy_bld; 12127 link_info->media_type = resp->media_type; 12128 link_info->phy_type = resp->phy_type; 12129 link_info->transceiver = resp->xcvr_pkg_type; 12130 link_info->phy_addr = resp->eee_config_phy_addr & 12131 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 12132 link_info->module_status = resp->module_status; 12133 12134 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 12135 struct ethtool_keee *eee = &bp->eee; 12136 u16 fw_speeds; 12137 12138 eee->eee_active = 0; 12139 if (resp->eee_config_phy_addr & 12140 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 12141 eee->eee_active = 1; 12142 fw_speeds = le16_to_cpu( 12143 resp->link_partner_adv_eee_link_speed_mask); 12144 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 12145 } 12146 12147 /* Pull initial EEE config */ 12148 if (!chng_link_state) { 12149 if (resp->eee_config_phy_addr & 12150 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 12151 eee->eee_enabled = 1; 12152 12153 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 12154 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 12155 12156 if (resp->eee_config_phy_addr & 12157 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 12158 __le32 tmr; 12159 12160 eee->tx_lpi_enabled = 1; 12161 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 12162 eee->tx_lpi_timer = le32_to_cpu(tmr) & 12163 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 12164 } 12165 } 12166 } 12167 12168 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 12169 if (bp->hwrm_spec_code >= 0x10504) { 12170 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 12171 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 12172 } 12173 /* TODO: need to add more logic to report VF link */ 12174 if (chng_link_state) { 12175 if (link_info->phy_link_status == BNXT_LINK_LINK) 12176 link_info->link_state = BNXT_LINK_STATE_UP; 12177 else 12178 link_info->link_state = BNXT_LINK_STATE_DOWN; 12179 if (link_state != link_info->link_state) 12180 bnxt_report_link(bp); 12181 } else { 12182 /* always link down if not require to update link state */ 12183 link_info->link_state = BNXT_LINK_STATE_DOWN; 12184 } 12185 hwrm_req_drop(bp, req); 12186 12187 if (!BNXT_PHY_CFG_ABLE(bp)) 12188 return 0; 12189 12190 support_changed = bnxt_support_speed_dropped(link_info); 12191 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 12192 bnxt_hwrm_set_link_setting(bp, true, false); 12193 return 0; 12194 } 12195 12196 static void bnxt_get_port_module_status(struct bnxt *bp) 12197 { 12198 struct bnxt_link_info *link_info = &bp->link_info; 12199 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 12200 u8 module_status; 12201 12202 if (bnxt_update_link(bp, true)) 12203 return; 12204 12205 module_status = link_info->module_status; 12206 switch (module_status) { 12207 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 12208 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 12209 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 12210 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 12211 bp->pf.port_id); 12212 if (bp->hwrm_spec_code >= 0x10201) { 12213 netdev_warn(bp->dev, "Module part number %s\n", 12214 resp->phy_vendor_partnumber); 12215 } 12216 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 12217 netdev_warn(bp->dev, "TX is disabled\n"); 12218 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 12219 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 12220 } 12221 } 12222 12223 static void 12224 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12225 { 12226 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 12227 if (bp->hwrm_spec_code >= 0x10201) 12228 req->auto_pause = 12229 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 12230 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12231 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 12232 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12233 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 12234 req->enables |= 12235 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12236 } else { 12237 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 12238 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 12239 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 12240 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 12241 req->enables |= 12242 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 12243 if (bp->hwrm_spec_code >= 0x10201) { 12244 req->auto_pause = req->force_pause; 12245 req->enables |= cpu_to_le32( 12246 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 12247 } 12248 } 12249 } 12250 12251 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 12252 { 12253 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 12254 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 12255 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12256 req->enables |= 12257 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 12258 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 12259 } else if (bp->link_info.advertising) { 12260 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 12261 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 12262 } 12263 if (bp->link_info.advertising_pam4) { 12264 req->enables |= 12265 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 12266 req->auto_link_pam4_speed_mask = 12267 cpu_to_le16(bp->link_info.advertising_pam4); 12268 } 12269 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 12270 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 12271 } else { 12272 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 12273 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 12274 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 12275 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 12276 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 12277 (u32)bp->link_info.req_link_speed); 12278 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 12279 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12280 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 12281 } else { 12282 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 12283 } 12284 } 12285 12286 /* tell chimp that the setting takes effect immediately */ 12287 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 12288 } 12289 12290 int bnxt_hwrm_set_pause(struct bnxt *bp) 12291 { 12292 struct hwrm_port_phy_cfg_input *req; 12293 int rc; 12294 12295 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12296 if (rc) 12297 return rc; 12298 12299 bnxt_hwrm_set_pause_common(bp, req); 12300 12301 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 12302 bp->link_info.force_link_chng) 12303 bnxt_hwrm_set_link_common(bp, req); 12304 12305 rc = hwrm_req_send(bp, req); 12306 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 12307 /* since changing of pause setting doesn't trigger any link 12308 * change event, the driver needs to update the current pause 12309 * result upon successfully return of the phy_cfg command 12310 */ 12311 bp->link_info.pause = 12312 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 12313 bp->link_info.auto_pause_setting = 0; 12314 if (!bp->link_info.force_link_chng) 12315 bnxt_report_link(bp); 12316 } 12317 bp->link_info.force_link_chng = false; 12318 return rc; 12319 } 12320 12321 static void bnxt_hwrm_set_eee(struct bnxt *bp, 12322 struct hwrm_port_phy_cfg_input *req) 12323 { 12324 struct ethtool_keee *eee = &bp->eee; 12325 12326 if (eee->eee_enabled) { 12327 u16 eee_speeds; 12328 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 12329 12330 if (eee->tx_lpi_enabled) 12331 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 12332 else 12333 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 12334 12335 req->flags |= cpu_to_le32(flags); 12336 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 12337 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 12338 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 12339 } else { 12340 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 12341 } 12342 } 12343 12344 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 12345 { 12346 struct hwrm_port_phy_cfg_input *req; 12347 int rc; 12348 12349 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12350 if (rc) 12351 return rc; 12352 12353 if (set_pause) 12354 bnxt_hwrm_set_pause_common(bp, req); 12355 12356 bnxt_hwrm_set_link_common(bp, req); 12357 12358 if (set_eee) 12359 bnxt_hwrm_set_eee(bp, req); 12360 return hwrm_req_send(bp, req); 12361 } 12362 12363 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 12364 { 12365 struct hwrm_port_phy_cfg_input *req; 12366 int rc; 12367 12368 if (!BNXT_SINGLE_PF(bp)) 12369 return 0; 12370 12371 if (pci_num_vf(bp->pdev) && 12372 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 12373 return 0; 12374 12375 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 12376 if (rc) 12377 return rc; 12378 12379 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 12380 rc = hwrm_req_send(bp, req); 12381 if (!rc) { 12382 mutex_lock(&bp->link_lock); 12383 /* Device is not obliged link down in certain scenarios, even 12384 * when forced. Setting the state unknown is consistent with 12385 * driver startup and will force link state to be reported 12386 * during subsequent open based on PORT_PHY_QCFG. 12387 */ 12388 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 12389 mutex_unlock(&bp->link_lock); 12390 } 12391 return rc; 12392 } 12393 12394 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 12395 { 12396 #ifdef CONFIG_TEE_BNXT_FW 12397 int rc = tee_bnxt_fw_load(); 12398 12399 if (rc) 12400 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 12401 12402 return rc; 12403 #else 12404 netdev_err(bp->dev, "OP-TEE not supported\n"); 12405 return -ENODEV; 12406 #endif 12407 } 12408 12409 static int bnxt_try_recover_fw(struct bnxt *bp) 12410 { 12411 if (bp->fw_health && bp->fw_health->status_reliable) { 12412 int retry = 0, rc; 12413 u32 sts; 12414 12415 do { 12416 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 12417 rc = bnxt_hwrm_poll(bp); 12418 if (!BNXT_FW_IS_BOOTING(sts) && 12419 !BNXT_FW_IS_RECOVERING(sts)) 12420 break; 12421 retry++; 12422 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 12423 12424 if (!BNXT_FW_IS_HEALTHY(sts)) { 12425 netdev_err(bp->dev, 12426 "Firmware not responding, status: 0x%x\n", 12427 sts); 12428 rc = -ENODEV; 12429 } 12430 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 12431 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 12432 return bnxt_fw_reset_via_optee(bp); 12433 } 12434 return rc; 12435 } 12436 12437 return -ENODEV; 12438 } 12439 12440 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 12441 { 12442 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12443 12444 if (!BNXT_NEW_RM(bp)) 12445 return; /* no resource reservations required */ 12446 12447 hw_resc->resv_cp_rings = 0; 12448 hw_resc->resv_stat_ctxs = 0; 12449 hw_resc->resv_irqs = 0; 12450 hw_resc->resv_tx_rings = 0; 12451 hw_resc->resv_rx_rings = 0; 12452 hw_resc->resv_hw_ring_grps = 0; 12453 hw_resc->resv_vnics = 0; 12454 hw_resc->resv_rsscos_ctxs = 0; 12455 if (!fw_reset) { 12456 bp->tx_nr_rings = 0; 12457 bp->rx_nr_rings = 0; 12458 } 12459 } 12460 12461 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 12462 { 12463 int rc; 12464 12465 if (!BNXT_NEW_RM(bp)) 12466 return 0; /* no resource reservations required */ 12467 12468 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 12469 if (rc) 12470 netdev_err(bp->dev, "resc_qcaps failed\n"); 12471 12472 bnxt_clear_reservations(bp, fw_reset); 12473 12474 return rc; 12475 } 12476 12477 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 12478 { 12479 struct hwrm_func_drv_if_change_output *resp; 12480 struct hwrm_func_drv_if_change_input *req; 12481 bool resc_reinit = false; 12482 bool caps_change = false; 12483 int rc, retry = 0; 12484 bool fw_reset; 12485 u32 flags = 0; 12486 12487 fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT); 12488 bp->fw_reset_state = 0; 12489 12490 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 12491 return 0; 12492 12493 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 12494 if (rc) 12495 return rc; 12496 12497 if (up) 12498 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 12499 resp = hwrm_req_hold(bp, req); 12500 12501 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 12502 while (retry < BNXT_FW_IF_RETRY) { 12503 rc = hwrm_req_send(bp, req); 12504 if (rc != -EAGAIN) 12505 break; 12506 12507 msleep(50); 12508 retry++; 12509 } 12510 12511 if (rc == -EAGAIN) { 12512 hwrm_req_drop(bp, req); 12513 return rc; 12514 } else if (!rc) { 12515 flags = le32_to_cpu(resp->flags); 12516 } else if (up) { 12517 rc = bnxt_try_recover_fw(bp); 12518 fw_reset = true; 12519 } 12520 hwrm_req_drop(bp, req); 12521 if (rc) 12522 return rc; 12523 12524 if (!up) { 12525 bnxt_inv_fw_health_reg(bp); 12526 return 0; 12527 } 12528 12529 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 12530 resc_reinit = true; 12531 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 12532 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 12533 fw_reset = true; 12534 else 12535 bnxt_remap_fw_health_regs(bp); 12536 12537 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 12538 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 12539 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12540 return -ENODEV; 12541 } 12542 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE) 12543 caps_change = true; 12544 12545 if (resc_reinit || fw_reset || caps_change) { 12546 if (fw_reset || caps_change) { 12547 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12548 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12549 bnxt_ulp_irq_stop(bp); 12550 bnxt_free_ctx_mem(bp, false); 12551 bnxt_dcb_free(bp); 12552 rc = bnxt_fw_init_one(bp); 12553 if (rc) { 12554 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12555 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12556 return rc; 12557 } 12558 /* IRQ will be initialized later in bnxt_request_irq()*/ 12559 bnxt_clear_int_mode(bp); 12560 } 12561 rc = bnxt_cancel_reservations(bp, fw_reset); 12562 } 12563 return rc; 12564 } 12565 12566 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 12567 { 12568 struct hwrm_port_led_qcaps_output *resp; 12569 struct hwrm_port_led_qcaps_input *req; 12570 struct bnxt_pf_info *pf = &bp->pf; 12571 int rc; 12572 12573 bp->num_leds = 0; 12574 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 12575 return 0; 12576 12577 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 12578 if (rc) 12579 return rc; 12580 12581 req->port_id = cpu_to_le16(pf->port_id); 12582 resp = hwrm_req_hold(bp, req); 12583 rc = hwrm_req_send(bp, req); 12584 if (rc) { 12585 hwrm_req_drop(bp, req); 12586 return rc; 12587 } 12588 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 12589 int i; 12590 12591 bp->num_leds = resp->num_leds; 12592 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 12593 bp->num_leds); 12594 for (i = 0; i < bp->num_leds; i++) { 12595 struct bnxt_led_info *led = &bp->leds[i]; 12596 __le16 caps = led->led_state_caps; 12597 12598 if (!led->led_group_id || 12599 !BNXT_LED_ALT_BLINK_CAP(caps)) { 12600 bp->num_leds = 0; 12601 break; 12602 } 12603 } 12604 } 12605 hwrm_req_drop(bp, req); 12606 return 0; 12607 } 12608 12609 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 12610 { 12611 struct hwrm_wol_filter_alloc_output *resp; 12612 struct hwrm_wol_filter_alloc_input *req; 12613 int rc; 12614 12615 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 12616 if (rc) 12617 return rc; 12618 12619 req->port_id = cpu_to_le16(bp->pf.port_id); 12620 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 12621 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 12622 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 12623 12624 resp = hwrm_req_hold(bp, req); 12625 rc = hwrm_req_send(bp, req); 12626 if (!rc) 12627 bp->wol_filter_id = resp->wol_filter_id; 12628 hwrm_req_drop(bp, req); 12629 return rc; 12630 } 12631 12632 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 12633 { 12634 struct hwrm_wol_filter_free_input *req; 12635 int rc; 12636 12637 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 12638 if (rc) 12639 return rc; 12640 12641 req->port_id = cpu_to_le16(bp->pf.port_id); 12642 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 12643 req->wol_filter_id = bp->wol_filter_id; 12644 12645 return hwrm_req_send(bp, req); 12646 } 12647 12648 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 12649 { 12650 struct hwrm_wol_filter_qcfg_output *resp; 12651 struct hwrm_wol_filter_qcfg_input *req; 12652 u16 next_handle = 0; 12653 int rc; 12654 12655 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 12656 if (rc) 12657 return rc; 12658 12659 req->port_id = cpu_to_le16(bp->pf.port_id); 12660 req->handle = cpu_to_le16(handle); 12661 resp = hwrm_req_hold(bp, req); 12662 rc = hwrm_req_send(bp, req); 12663 if (!rc) { 12664 next_handle = le16_to_cpu(resp->next_handle); 12665 if (next_handle != 0) { 12666 if (resp->wol_type == 12667 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 12668 bp->wol = 1; 12669 bp->wol_filter_id = resp->wol_filter_id; 12670 } 12671 } 12672 } 12673 hwrm_req_drop(bp, req); 12674 return next_handle; 12675 } 12676 12677 static void bnxt_get_wol_settings(struct bnxt *bp) 12678 { 12679 u16 handle = 0; 12680 12681 bp->wol = 0; 12682 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 12683 return; 12684 12685 do { 12686 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 12687 } while (handle && handle != 0xffff); 12688 } 12689 12690 static bool bnxt_eee_config_ok(struct bnxt *bp) 12691 { 12692 struct ethtool_keee *eee = &bp->eee; 12693 struct bnxt_link_info *link_info = &bp->link_info; 12694 12695 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 12696 return true; 12697 12698 if (eee->eee_enabled) { 12699 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 12700 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 12701 12702 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 12703 12704 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12705 eee->eee_enabled = 0; 12706 return false; 12707 } 12708 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 12709 linkmode_and(eee->advertised, advertising, 12710 eee->supported); 12711 return false; 12712 } 12713 } 12714 return true; 12715 } 12716 12717 static int bnxt_update_phy_setting(struct bnxt *bp) 12718 { 12719 int rc; 12720 bool update_link = false; 12721 bool update_pause = false; 12722 bool update_eee = false; 12723 struct bnxt_link_info *link_info = &bp->link_info; 12724 12725 rc = bnxt_update_link(bp, true); 12726 if (rc) { 12727 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 12728 rc); 12729 return rc; 12730 } 12731 if (!BNXT_SINGLE_PF(bp)) 12732 return 0; 12733 12734 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12735 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 12736 link_info->req_flow_ctrl) 12737 update_pause = true; 12738 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 12739 link_info->force_pause_setting != link_info->req_flow_ctrl) 12740 update_pause = true; 12741 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 12742 if (BNXT_AUTO_MODE(link_info->auto_mode)) 12743 update_link = true; 12744 if (bnxt_force_speed_updated(link_info)) 12745 update_link = true; 12746 if (link_info->req_duplex != link_info->duplex_setting) 12747 update_link = true; 12748 } else { 12749 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 12750 update_link = true; 12751 if (bnxt_auto_speed_updated(link_info)) 12752 update_link = true; 12753 } 12754 12755 /* The last close may have shutdown the link, so need to call 12756 * PHY_CFG to bring it back up. 12757 */ 12758 if (!BNXT_LINK_IS_UP(bp)) 12759 update_link = true; 12760 12761 if (!bnxt_eee_config_ok(bp)) 12762 update_eee = true; 12763 12764 if (update_link) 12765 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 12766 else if (update_pause) 12767 rc = bnxt_hwrm_set_pause(bp); 12768 if (rc) { 12769 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 12770 rc); 12771 return rc; 12772 } 12773 12774 return rc; 12775 } 12776 12777 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 12778 12779 static int bnxt_reinit_after_abort(struct bnxt *bp) 12780 { 12781 int rc; 12782 12783 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12784 return -EBUSY; 12785 12786 if (bp->dev->reg_state == NETREG_UNREGISTERED) 12787 return -ENODEV; 12788 12789 rc = bnxt_fw_init_one(bp); 12790 if (!rc) { 12791 bnxt_clear_int_mode(bp); 12792 rc = bnxt_init_int_mode(bp); 12793 if (!rc) { 12794 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12795 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12796 } 12797 } 12798 return rc; 12799 } 12800 12801 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12802 { 12803 struct bnxt_ntuple_filter *ntp_fltr; 12804 struct bnxt_l2_filter *l2_fltr; 12805 12806 if (list_empty(&fltr->list)) 12807 return; 12808 12809 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12810 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12811 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12812 atomic_inc(&l2_fltr->refcnt); 12813 ntp_fltr->l2_fltr = l2_fltr; 12814 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12815 bnxt_del_ntp_filter(bp, ntp_fltr); 12816 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12817 fltr->sw_id); 12818 } 12819 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12820 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12821 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12822 bnxt_del_l2_filter(bp, l2_fltr); 12823 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12824 fltr->sw_id); 12825 } 12826 } 12827 } 12828 12829 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12830 { 12831 struct bnxt_filter_base *usr_fltr, *tmp; 12832 12833 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12834 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12835 } 12836 12837 static int bnxt_set_xps_mapping(struct bnxt *bp) 12838 { 12839 int numa_node = dev_to_node(&bp->pdev->dev); 12840 unsigned int q_idx, map_idx, cpu, i; 12841 const struct cpumask *cpu_mask_ptr; 12842 int nr_cpus = num_online_cpus(); 12843 cpumask_t *q_map; 12844 int rc = 0; 12845 12846 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12847 if (!q_map) 12848 return -ENOMEM; 12849 12850 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12851 * Each TC has the same number of TX queues. The nth TX queue for each 12852 * TC will have the same CPU mask. 12853 */ 12854 for (i = 0; i < nr_cpus; i++) { 12855 map_idx = i % bp->tx_nr_rings_per_tc; 12856 cpu = cpumask_local_spread(i, numa_node); 12857 cpu_mask_ptr = get_cpu_mask(cpu); 12858 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12859 } 12860 12861 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12862 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12863 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12864 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12865 if (rc) { 12866 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12867 q_idx); 12868 break; 12869 } 12870 } 12871 12872 kfree(q_map); 12873 12874 return rc; 12875 } 12876 12877 static int bnxt_tx_nr_rings(struct bnxt *bp) 12878 { 12879 return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc : 12880 bp->tx_nr_rings_per_tc; 12881 } 12882 12883 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp) 12884 { 12885 return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings; 12886 } 12887 12888 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12889 { 12890 int rc = 0; 12891 12892 netif_carrier_off(bp->dev); 12893 if (irq_re_init) { 12894 /* Reserve rings now if none were reserved at driver probe. */ 12895 rc = bnxt_init_dflt_ring_mode(bp); 12896 if (rc) { 12897 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12898 return rc; 12899 } 12900 } 12901 rc = bnxt_reserve_rings(bp, irq_re_init); 12902 if (rc) 12903 return rc; 12904 12905 /* Make adjustments if reserved TX rings are less than requested */ 12906 bp->tx_nr_rings -= bp->tx_nr_rings_xdp; 12907 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 12908 if (bp->tx_nr_rings_xdp) { 12909 bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc; 12910 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 12911 } 12912 rc = bnxt_alloc_mem(bp, irq_re_init); 12913 if (rc) { 12914 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12915 goto open_err_free_mem; 12916 } 12917 12918 if (irq_re_init) { 12919 bnxt_init_napi(bp); 12920 rc = bnxt_request_irq(bp); 12921 if (rc) { 12922 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12923 goto open_err_irq; 12924 } 12925 } 12926 12927 rc = bnxt_init_nic(bp, irq_re_init); 12928 if (rc) { 12929 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12930 goto open_err_irq; 12931 } 12932 12933 bnxt_enable_napi(bp); 12934 bnxt_debug_dev_init(bp); 12935 12936 if (link_re_init) { 12937 mutex_lock(&bp->link_lock); 12938 rc = bnxt_update_phy_setting(bp); 12939 mutex_unlock(&bp->link_lock); 12940 if (rc) { 12941 netdev_warn(bp->dev, "failed to update phy settings\n"); 12942 if (BNXT_SINGLE_PF(bp)) { 12943 bp->link_info.phy_retry = true; 12944 bp->link_info.phy_retry_expires = 12945 jiffies + 5 * HZ; 12946 } 12947 } 12948 } 12949 12950 if (irq_re_init) { 12951 udp_tunnel_nic_reset_ntf(bp->dev); 12952 rc = bnxt_set_xps_mapping(bp); 12953 if (rc) 12954 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12955 } 12956 12957 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12958 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12959 static_branch_enable(&bnxt_xdp_locking_key); 12960 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12961 static_branch_disable(&bnxt_xdp_locking_key); 12962 } 12963 set_bit(BNXT_STATE_OPEN, &bp->state); 12964 bnxt_enable_int(bp); 12965 /* Enable TX queues */ 12966 bnxt_tx_enable(bp); 12967 mod_timer(&bp->timer, jiffies + bp->current_interval); 12968 /* Poll link status and check for SFP+ module status */ 12969 mutex_lock(&bp->link_lock); 12970 bnxt_get_port_module_status(bp); 12971 mutex_unlock(&bp->link_lock); 12972 12973 /* VF-reps may need to be re-opened after the PF is re-opened */ 12974 if (BNXT_PF(bp)) 12975 bnxt_vf_reps_open(bp); 12976 bnxt_ptp_init_rtc(bp, true); 12977 bnxt_ptp_cfg_tstamp_filters(bp); 12978 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12979 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12980 bnxt_cfg_usr_fltrs(bp); 12981 return 0; 12982 12983 open_err_irq: 12984 bnxt_del_napi(bp); 12985 12986 open_err_free_mem: 12987 bnxt_free_skbs(bp); 12988 bnxt_free_irq(bp); 12989 bnxt_free_mem(bp, true); 12990 return rc; 12991 } 12992 12993 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12994 { 12995 int rc = 0; 12996 12997 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12998 rc = -EIO; 12999 if (!rc) 13000 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 13001 if (rc) { 13002 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 13003 netif_close(bp->dev); 13004 } 13005 return rc; 13006 } 13007 13008 /* netdev instance lock held, open the NIC half way by allocating all 13009 * resources, but NAPI, IRQ, and TX are not enabled. This is mainly used 13010 * for offline self tests. 13011 */ 13012 int bnxt_half_open_nic(struct bnxt *bp) 13013 { 13014 int rc = 0; 13015 13016 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13017 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 13018 rc = -ENODEV; 13019 goto half_open_err; 13020 } 13021 13022 rc = bnxt_alloc_mem(bp, true); 13023 if (rc) { 13024 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 13025 goto half_open_err; 13026 } 13027 bnxt_init_napi(bp); 13028 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13029 rc = bnxt_init_nic(bp, true); 13030 if (rc) { 13031 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13032 bnxt_del_napi(bp); 13033 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 13034 goto half_open_err; 13035 } 13036 return 0; 13037 13038 half_open_err: 13039 bnxt_free_skbs(bp); 13040 bnxt_free_mem(bp, true); 13041 netif_close(bp->dev); 13042 return rc; 13043 } 13044 13045 /* netdev instance lock held, this call can only be made after a previous 13046 * successful call to bnxt_half_open_nic(). 13047 */ 13048 void bnxt_half_close_nic(struct bnxt *bp) 13049 { 13050 bnxt_hwrm_resource_free(bp, false, true); 13051 bnxt_del_napi(bp); 13052 bnxt_free_skbs(bp); 13053 bnxt_free_mem(bp, true); 13054 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 13055 } 13056 13057 void bnxt_reenable_sriov(struct bnxt *bp) 13058 { 13059 if (BNXT_PF(bp)) { 13060 struct bnxt_pf_info *pf = &bp->pf; 13061 int n = pf->active_vfs; 13062 13063 if (n) 13064 bnxt_cfg_hw_sriov(bp, &n, true); 13065 } 13066 } 13067 13068 static int bnxt_open(struct net_device *dev) 13069 { 13070 struct bnxt *bp = netdev_priv(dev); 13071 int rc; 13072 13073 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 13074 rc = bnxt_reinit_after_abort(bp); 13075 if (rc) { 13076 if (rc == -EBUSY) 13077 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 13078 else 13079 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 13080 return -ENODEV; 13081 } 13082 } 13083 13084 rc = bnxt_hwrm_if_change(bp, true); 13085 if (rc) 13086 return rc; 13087 13088 rc = __bnxt_open_nic(bp, true, true); 13089 if (rc) { 13090 bnxt_hwrm_if_change(bp, false); 13091 } else { 13092 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 13093 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13094 bnxt_queue_sp_work(bp, 13095 BNXT_RESTART_ULP_SP_EVENT); 13096 } 13097 } 13098 13099 return rc; 13100 } 13101 13102 static bool bnxt_drv_busy(struct bnxt *bp) 13103 { 13104 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 13105 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 13106 } 13107 13108 static void bnxt_get_ring_stats(struct bnxt *bp, 13109 struct rtnl_link_stats64 *stats); 13110 13111 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 13112 bool link_re_init) 13113 { 13114 /* Close the VF-reps before closing PF */ 13115 if (BNXT_PF(bp)) 13116 bnxt_vf_reps_close(bp); 13117 13118 /* Change device state to avoid TX queue wake up's */ 13119 bnxt_tx_disable(bp); 13120 13121 clear_bit(BNXT_STATE_OPEN, &bp->state); 13122 smp_mb__after_atomic(); 13123 while (bnxt_drv_busy(bp)) 13124 msleep(20); 13125 13126 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 13127 bnxt_clear_rss_ctxs(bp); 13128 /* Flush rings and disable interrupts */ 13129 bnxt_shutdown_nic(bp, irq_re_init); 13130 13131 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 13132 13133 bnxt_debug_dev_exit(bp); 13134 bnxt_disable_napi(bp); 13135 timer_delete_sync(&bp->timer); 13136 bnxt_free_skbs(bp); 13137 13138 /* Save ring stats before shutdown */ 13139 if (bp->bnapi && irq_re_init) { 13140 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 13141 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 13142 } 13143 if (irq_re_init) { 13144 bnxt_free_irq(bp); 13145 bnxt_del_napi(bp); 13146 } 13147 bnxt_free_mem(bp, irq_re_init); 13148 } 13149 13150 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 13151 { 13152 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13153 /* If we get here, it means firmware reset is in progress 13154 * while we are trying to close. We can safely proceed with 13155 * the close because we are holding netdev instance lock. 13156 * Some firmware messages may fail as we proceed to close. 13157 * We set the ABORT_ERR flag here so that the FW reset thread 13158 * will later abort when it gets the netdev instance lock 13159 * and sees the flag. 13160 */ 13161 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 13162 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 13163 } 13164 13165 #ifdef CONFIG_BNXT_SRIOV 13166 if (bp->sriov_cfg) { 13167 int rc; 13168 13169 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 13170 !bp->sriov_cfg, 13171 BNXT_SRIOV_CFG_WAIT_TMO); 13172 if (!rc) 13173 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 13174 else if (rc < 0) 13175 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 13176 } 13177 #endif 13178 __bnxt_close_nic(bp, irq_re_init, link_re_init); 13179 } 13180 13181 static int bnxt_close(struct net_device *dev) 13182 { 13183 struct bnxt *bp = netdev_priv(dev); 13184 13185 bnxt_close_nic(bp, true, true); 13186 bnxt_hwrm_shutdown_link(bp); 13187 bnxt_hwrm_if_change(bp, false); 13188 return 0; 13189 } 13190 13191 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 13192 u16 *val) 13193 { 13194 struct hwrm_port_phy_mdio_read_output *resp; 13195 struct hwrm_port_phy_mdio_read_input *req; 13196 int rc; 13197 13198 if (bp->hwrm_spec_code < 0x10a00) 13199 return -EOPNOTSUPP; 13200 13201 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 13202 if (rc) 13203 return rc; 13204 13205 req->port_id = cpu_to_le16(bp->pf.port_id); 13206 req->phy_addr = phy_addr; 13207 req->reg_addr = cpu_to_le16(reg & 0x1f); 13208 if (mdio_phy_id_is_c45(phy_addr)) { 13209 req->cl45_mdio = 1; 13210 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13211 req->dev_addr = mdio_phy_id_devad(phy_addr); 13212 req->reg_addr = cpu_to_le16(reg); 13213 } 13214 13215 resp = hwrm_req_hold(bp, req); 13216 rc = hwrm_req_send(bp, req); 13217 if (!rc) 13218 *val = le16_to_cpu(resp->reg_data); 13219 hwrm_req_drop(bp, req); 13220 return rc; 13221 } 13222 13223 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 13224 u16 val) 13225 { 13226 struct hwrm_port_phy_mdio_write_input *req; 13227 int rc; 13228 13229 if (bp->hwrm_spec_code < 0x10a00) 13230 return -EOPNOTSUPP; 13231 13232 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 13233 if (rc) 13234 return rc; 13235 13236 req->port_id = cpu_to_le16(bp->pf.port_id); 13237 req->phy_addr = phy_addr; 13238 req->reg_addr = cpu_to_le16(reg & 0x1f); 13239 if (mdio_phy_id_is_c45(phy_addr)) { 13240 req->cl45_mdio = 1; 13241 req->phy_addr = mdio_phy_id_prtad(phy_addr); 13242 req->dev_addr = mdio_phy_id_devad(phy_addr); 13243 req->reg_addr = cpu_to_le16(reg); 13244 } 13245 req->reg_data = cpu_to_le16(val); 13246 13247 return hwrm_req_send(bp, req); 13248 } 13249 13250 /* netdev instance lock held */ 13251 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 13252 { 13253 struct mii_ioctl_data *mdio = if_mii(ifr); 13254 struct bnxt *bp = netdev_priv(dev); 13255 int rc; 13256 13257 switch (cmd) { 13258 case SIOCGMIIPHY: 13259 mdio->phy_id = bp->link_info.phy_addr; 13260 13261 fallthrough; 13262 case SIOCGMIIREG: { 13263 u16 mii_regval = 0; 13264 13265 if (!netif_running(dev)) 13266 return -EAGAIN; 13267 13268 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 13269 &mii_regval); 13270 mdio->val_out = mii_regval; 13271 return rc; 13272 } 13273 13274 case SIOCSMIIREG: 13275 if (!netif_running(dev)) 13276 return -EAGAIN; 13277 13278 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 13279 mdio->val_in); 13280 13281 case SIOCSHWTSTAMP: 13282 return bnxt_hwtstamp_set(dev, ifr); 13283 13284 case SIOCGHWTSTAMP: 13285 return bnxt_hwtstamp_get(dev, ifr); 13286 13287 default: 13288 /* do nothing */ 13289 break; 13290 } 13291 return -EOPNOTSUPP; 13292 } 13293 13294 static void bnxt_get_ring_stats(struct bnxt *bp, 13295 struct rtnl_link_stats64 *stats) 13296 { 13297 int i; 13298 13299 for (i = 0; i < bp->cp_nr_rings; i++) { 13300 struct bnxt_napi *bnapi = bp->bnapi[i]; 13301 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13302 u64 *sw = cpr->stats.sw_stats; 13303 13304 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 13305 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13306 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 13307 13308 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 13309 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 13310 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 13311 13312 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 13313 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 13314 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 13315 13316 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 13317 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 13318 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 13319 13320 stats->rx_missed_errors += 13321 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 13322 13323 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 13324 13325 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 13326 13327 stats->rx_dropped += 13328 cpr->sw_stats->rx.rx_netpoll_discards + 13329 cpr->sw_stats->rx.rx_oom_discards; 13330 } 13331 } 13332 13333 static void bnxt_add_prev_stats(struct bnxt *bp, 13334 struct rtnl_link_stats64 *stats) 13335 { 13336 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 13337 13338 stats->rx_packets += prev_stats->rx_packets; 13339 stats->tx_packets += prev_stats->tx_packets; 13340 stats->rx_bytes += prev_stats->rx_bytes; 13341 stats->tx_bytes += prev_stats->tx_bytes; 13342 stats->rx_missed_errors += prev_stats->rx_missed_errors; 13343 stats->multicast += prev_stats->multicast; 13344 stats->rx_dropped += prev_stats->rx_dropped; 13345 stats->tx_dropped += prev_stats->tx_dropped; 13346 } 13347 13348 static void 13349 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 13350 { 13351 struct bnxt *bp = netdev_priv(dev); 13352 13353 set_bit(BNXT_STATE_READ_STATS, &bp->state); 13354 /* Make sure bnxt_close_nic() sees that we are reading stats before 13355 * we check the BNXT_STATE_OPEN flag. 13356 */ 13357 smp_mb__after_atomic(); 13358 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13359 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13360 *stats = bp->net_stats_prev; 13361 return; 13362 } 13363 13364 bnxt_get_ring_stats(bp, stats); 13365 bnxt_add_prev_stats(bp, stats); 13366 13367 if (bp->flags & BNXT_FLAG_PORT_STATS) { 13368 u64 *rx = bp->port_stats.sw_stats; 13369 u64 *tx = bp->port_stats.sw_stats + 13370 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 13371 13372 stats->rx_crc_errors = 13373 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 13374 stats->rx_frame_errors = 13375 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 13376 stats->rx_length_errors = 13377 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 13378 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 13379 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 13380 stats->rx_errors = 13381 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 13382 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 13383 stats->collisions = 13384 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 13385 stats->tx_fifo_errors = 13386 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 13387 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 13388 } 13389 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 13390 } 13391 13392 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 13393 struct bnxt_total_ring_err_stats *stats, 13394 struct bnxt_cp_ring_info *cpr) 13395 { 13396 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 13397 u64 *hw_stats = cpr->stats.sw_stats; 13398 13399 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 13400 stats->rx_total_resets += sw_stats->rx.rx_resets; 13401 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 13402 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 13403 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 13404 stats->rx_total_ring_discards += 13405 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 13406 stats->tx_total_resets += sw_stats->tx.tx_resets; 13407 stats->tx_total_ring_discards += 13408 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 13409 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 13410 } 13411 13412 void bnxt_get_ring_err_stats(struct bnxt *bp, 13413 struct bnxt_total_ring_err_stats *stats) 13414 { 13415 int i; 13416 13417 for (i = 0; i < bp->cp_nr_rings; i++) 13418 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 13419 } 13420 13421 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 13422 { 13423 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13424 struct net_device *dev = bp->dev; 13425 struct netdev_hw_addr *ha; 13426 u8 *haddr; 13427 int mc_count = 0; 13428 bool update = false; 13429 int off = 0; 13430 13431 netdev_for_each_mc_addr(ha, dev) { 13432 if (mc_count >= BNXT_MAX_MC_ADDRS) { 13433 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13434 vnic->mc_list_count = 0; 13435 return false; 13436 } 13437 haddr = ha->addr; 13438 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 13439 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 13440 update = true; 13441 } 13442 off += ETH_ALEN; 13443 mc_count++; 13444 } 13445 if (mc_count) 13446 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13447 13448 if (mc_count != vnic->mc_list_count) { 13449 vnic->mc_list_count = mc_count; 13450 update = true; 13451 } 13452 return update; 13453 } 13454 13455 static bool bnxt_uc_list_updated(struct bnxt *bp) 13456 { 13457 struct net_device *dev = bp->dev; 13458 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13459 struct netdev_hw_addr *ha; 13460 int off = 0; 13461 13462 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 13463 return true; 13464 13465 netdev_for_each_uc_addr(ha, dev) { 13466 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 13467 return true; 13468 13469 off += ETH_ALEN; 13470 } 13471 return false; 13472 } 13473 13474 static void bnxt_set_rx_mode(struct net_device *dev) 13475 { 13476 struct bnxt *bp = netdev_priv(dev); 13477 struct bnxt_vnic_info *vnic; 13478 bool mc_update = false; 13479 bool uc_update; 13480 u32 mask; 13481 13482 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 13483 return; 13484 13485 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13486 mask = vnic->rx_mask; 13487 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 13488 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 13489 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 13490 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 13491 13492 if (dev->flags & IFF_PROMISC) 13493 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13494 13495 uc_update = bnxt_uc_list_updated(bp); 13496 13497 if (dev->flags & IFF_BROADCAST) 13498 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 13499 if (dev->flags & IFF_ALLMULTI) { 13500 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13501 vnic->mc_list_count = 0; 13502 } else if (dev->flags & IFF_MULTICAST) { 13503 mc_update = bnxt_mc_list_updated(bp, &mask); 13504 } 13505 13506 if (mask != vnic->rx_mask || uc_update || mc_update) { 13507 vnic->rx_mask = mask; 13508 13509 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13510 } 13511 } 13512 13513 static int bnxt_cfg_rx_mode(struct bnxt *bp) 13514 { 13515 struct net_device *dev = bp->dev; 13516 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 13517 struct netdev_hw_addr *ha; 13518 int i, off = 0, rc; 13519 bool uc_update; 13520 13521 netif_addr_lock_bh(dev); 13522 uc_update = bnxt_uc_list_updated(bp); 13523 netif_addr_unlock_bh(dev); 13524 13525 if (!uc_update) 13526 goto skip_uc; 13527 13528 for (i = 1; i < vnic->uc_filter_count; i++) { 13529 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 13530 13531 bnxt_hwrm_l2_filter_free(bp, fltr); 13532 bnxt_del_l2_filter(bp, fltr); 13533 } 13534 13535 vnic->uc_filter_count = 1; 13536 13537 netif_addr_lock_bh(dev); 13538 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 13539 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13540 } else { 13541 netdev_for_each_uc_addr(ha, dev) { 13542 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 13543 off += ETH_ALEN; 13544 vnic->uc_filter_count++; 13545 } 13546 } 13547 netif_addr_unlock_bh(dev); 13548 13549 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 13550 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 13551 if (rc) { 13552 if (BNXT_VF(bp) && rc == -ENODEV) { 13553 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13554 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 13555 else 13556 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 13557 rc = 0; 13558 } else { 13559 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 13560 } 13561 vnic->uc_filter_count = i; 13562 return rc; 13563 } 13564 } 13565 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13566 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 13567 13568 skip_uc: 13569 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 13570 !bnxt_promisc_ok(bp)) 13571 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 13572 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13573 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 13574 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 13575 rc); 13576 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 13577 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 13578 vnic->mc_list_count = 0; 13579 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 13580 } 13581 if (rc) 13582 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 13583 rc); 13584 13585 return rc; 13586 } 13587 13588 static bool bnxt_can_reserve_rings(struct bnxt *bp) 13589 { 13590 #ifdef CONFIG_BNXT_SRIOV 13591 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 13592 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 13593 13594 /* No minimum rings were provisioned by the PF. Don't 13595 * reserve rings by default when device is down. 13596 */ 13597 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 13598 return true; 13599 13600 if (!netif_running(bp->dev)) 13601 return false; 13602 } 13603 #endif 13604 return true; 13605 } 13606 13607 /* If the chip and firmware supports RFS */ 13608 static bool bnxt_rfs_supported(struct bnxt *bp) 13609 { 13610 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 13611 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 13612 return true; 13613 return false; 13614 } 13615 /* 212 firmware is broken for aRFS */ 13616 if (BNXT_FW_MAJ(bp) == 212) 13617 return false; 13618 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 13619 return true; 13620 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 13621 return true; 13622 return false; 13623 } 13624 13625 /* If runtime conditions support RFS */ 13626 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 13627 { 13628 struct bnxt_hw_rings hwr = {0}; 13629 int max_vnics, max_rss_ctxs; 13630 13631 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13632 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 13633 return bnxt_rfs_supported(bp); 13634 13635 if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 13636 return false; 13637 13638 hwr.grp = bp->rx_nr_rings; 13639 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 13640 if (new_rss_ctx) 13641 hwr.vnic++; 13642 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13643 max_vnics = bnxt_get_max_func_vnics(bp); 13644 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 13645 13646 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 13647 if (bp->rx_nr_rings > 1) 13648 netdev_warn(bp->dev, 13649 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 13650 min(max_rss_ctxs - 1, max_vnics - 1)); 13651 return false; 13652 } 13653 13654 if (!BNXT_NEW_RM(bp)) 13655 return true; 13656 13657 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 13658 * issue that will mess up the default VNIC if we reduce the 13659 * reservations. 13660 */ 13661 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13662 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13663 return true; 13664 13665 bnxt_hwrm_reserve_rings(bp, &hwr); 13666 if (hwr.vnic <= bp->hw_resc.resv_vnics && 13667 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 13668 return true; 13669 13670 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 13671 hwr.vnic = 1; 13672 hwr.rss_ctx = 0; 13673 bnxt_hwrm_reserve_rings(bp, &hwr); 13674 return false; 13675 } 13676 13677 static netdev_features_t bnxt_fix_features(struct net_device *dev, 13678 netdev_features_t features) 13679 { 13680 struct bnxt *bp = netdev_priv(dev); 13681 netdev_features_t vlan_features; 13682 13683 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 13684 features &= ~NETIF_F_NTUPLE; 13685 13686 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 13687 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 13688 13689 if (!(features & NETIF_F_GRO)) 13690 features &= ~NETIF_F_GRO_HW; 13691 13692 if (features & NETIF_F_GRO_HW) 13693 features &= ~NETIF_F_LRO; 13694 13695 /* Both CTAG and STAG VLAN acceleration on the RX side have to be 13696 * turned on or off together. 13697 */ 13698 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 13699 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 13700 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13701 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13702 else if (vlan_features) 13703 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 13704 } 13705 #ifdef CONFIG_BNXT_SRIOV 13706 if (BNXT_VF(bp) && bp->vf.vlan) 13707 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 13708 #endif 13709 return features; 13710 } 13711 13712 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 13713 bool link_re_init, u32 flags, bool update_tpa) 13714 { 13715 bnxt_close_nic(bp, irq_re_init, link_re_init); 13716 bp->flags = flags; 13717 if (update_tpa) 13718 bnxt_set_ring_params(bp); 13719 return bnxt_open_nic(bp, irq_re_init, link_re_init); 13720 } 13721 13722 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 13723 { 13724 bool update_tpa = false, update_ntuple = false; 13725 struct bnxt *bp = netdev_priv(dev); 13726 u32 flags = bp->flags; 13727 u32 changes; 13728 int rc = 0; 13729 bool re_init = false; 13730 13731 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 13732 if (features & NETIF_F_GRO_HW) 13733 flags |= BNXT_FLAG_GRO; 13734 else if (features & NETIF_F_LRO) 13735 flags |= BNXT_FLAG_LRO; 13736 13737 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 13738 flags &= ~BNXT_FLAG_TPA; 13739 13740 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 13741 flags |= BNXT_FLAG_STRIP_VLAN; 13742 13743 if (features & NETIF_F_NTUPLE) 13744 flags |= BNXT_FLAG_RFS; 13745 else 13746 bnxt_clear_usr_fltrs(bp, true); 13747 13748 changes = flags ^ bp->flags; 13749 if (changes & BNXT_FLAG_TPA) { 13750 update_tpa = true; 13751 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 13752 (flags & BNXT_FLAG_TPA) == 0 || 13753 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13754 re_init = true; 13755 } 13756 13757 if (changes & ~BNXT_FLAG_TPA) 13758 re_init = true; 13759 13760 if (changes & BNXT_FLAG_RFS) 13761 update_ntuple = true; 13762 13763 if (flags != bp->flags) { 13764 u32 old_flags = bp->flags; 13765 13766 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13767 bp->flags = flags; 13768 if (update_tpa) 13769 bnxt_set_ring_params(bp); 13770 return rc; 13771 } 13772 13773 if (update_ntuple) 13774 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 13775 13776 if (re_init) 13777 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 13778 13779 if (update_tpa) { 13780 bp->flags = flags; 13781 rc = bnxt_set_tpa(bp, 13782 (flags & BNXT_FLAG_TPA) ? 13783 true : false); 13784 if (rc) 13785 bp->flags = old_flags; 13786 } 13787 } 13788 return rc; 13789 } 13790 13791 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 13792 u8 **nextp) 13793 { 13794 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 13795 struct hop_jumbo_hdr *jhdr; 13796 int hdr_count = 0; 13797 u8 *nexthdr; 13798 int start; 13799 13800 /* Check that there are at most 2 IPv6 extension headers, no 13801 * fragment header, and each is <= 64 bytes. 13802 */ 13803 start = nw_off + sizeof(*ip6h); 13804 nexthdr = &ip6h->nexthdr; 13805 while (ipv6_ext_hdr(*nexthdr)) { 13806 struct ipv6_opt_hdr *hp; 13807 int hdrlen; 13808 13809 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13810 *nexthdr == NEXTHDR_FRAGMENT) 13811 return false; 13812 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13813 skb_headlen(skb), NULL); 13814 if (!hp) 13815 return false; 13816 if (*nexthdr == NEXTHDR_AUTH) 13817 hdrlen = ipv6_authlen(hp); 13818 else 13819 hdrlen = ipv6_optlen(hp); 13820 13821 if (hdrlen > 64) 13822 return false; 13823 13824 /* The ext header may be a hop-by-hop header inserted for 13825 * big TCP purposes. This will be removed before sending 13826 * from NIC, so do not count it. 13827 */ 13828 if (*nexthdr == NEXTHDR_HOP) { 13829 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13830 goto increment_hdr; 13831 13832 jhdr = (struct hop_jumbo_hdr *)hp; 13833 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13834 jhdr->nexthdr != IPPROTO_TCP) 13835 goto increment_hdr; 13836 13837 goto next_hdr; 13838 } 13839 increment_hdr: 13840 hdr_count++; 13841 next_hdr: 13842 nexthdr = &hp->nexthdr; 13843 start += hdrlen; 13844 } 13845 if (nextp) { 13846 /* Caller will check inner protocol */ 13847 if (skb->encapsulation) { 13848 *nextp = nexthdr; 13849 return true; 13850 } 13851 *nextp = NULL; 13852 } 13853 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13854 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13855 } 13856 13857 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13858 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13859 { 13860 struct udphdr *uh = udp_hdr(skb); 13861 __be16 udp_port = uh->dest; 13862 13863 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13864 udp_port != bp->vxlan_gpe_port) 13865 return false; 13866 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13867 struct ethhdr *eh = inner_eth_hdr(skb); 13868 13869 switch (eh->h_proto) { 13870 case htons(ETH_P_IP): 13871 return true; 13872 case htons(ETH_P_IPV6): 13873 return bnxt_exthdr_check(bp, skb, 13874 skb_inner_network_offset(skb), 13875 NULL); 13876 } 13877 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13878 return true; 13879 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13880 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13881 NULL); 13882 } 13883 return false; 13884 } 13885 13886 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13887 { 13888 switch (l4_proto) { 13889 case IPPROTO_UDP: 13890 return bnxt_udp_tunl_check(bp, skb); 13891 case IPPROTO_IPIP: 13892 return true; 13893 case IPPROTO_GRE: { 13894 switch (skb->inner_protocol) { 13895 default: 13896 return false; 13897 case htons(ETH_P_IP): 13898 return true; 13899 case htons(ETH_P_IPV6): 13900 fallthrough; 13901 } 13902 } 13903 case IPPROTO_IPV6: 13904 /* Check ext headers of inner ipv6 */ 13905 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13906 NULL); 13907 } 13908 return false; 13909 } 13910 13911 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13912 struct net_device *dev, 13913 netdev_features_t features) 13914 { 13915 struct bnxt *bp = netdev_priv(dev); 13916 u8 *l4_proto; 13917 13918 features = vlan_features_check(skb, features); 13919 switch (vlan_get_protocol(skb)) { 13920 case htons(ETH_P_IP): 13921 if (!skb->encapsulation) 13922 return features; 13923 l4_proto = &ip_hdr(skb)->protocol; 13924 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13925 return features; 13926 break; 13927 case htons(ETH_P_IPV6): 13928 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13929 &l4_proto)) 13930 break; 13931 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13932 return features; 13933 break; 13934 } 13935 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13936 } 13937 13938 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13939 u32 *reg_buf) 13940 { 13941 struct hwrm_dbg_read_direct_output *resp; 13942 struct hwrm_dbg_read_direct_input *req; 13943 __le32 *dbg_reg_buf; 13944 dma_addr_t mapping; 13945 int rc, i; 13946 13947 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13948 if (rc) 13949 return rc; 13950 13951 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13952 &mapping); 13953 if (!dbg_reg_buf) { 13954 rc = -ENOMEM; 13955 goto dbg_rd_reg_exit; 13956 } 13957 13958 req->host_dest_addr = cpu_to_le64(mapping); 13959 13960 resp = hwrm_req_hold(bp, req); 13961 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13962 req->read_len32 = cpu_to_le32(num_words); 13963 13964 rc = hwrm_req_send(bp, req); 13965 if (rc || resp->error_code) { 13966 rc = -EIO; 13967 goto dbg_rd_reg_exit; 13968 } 13969 for (i = 0; i < num_words; i++) 13970 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13971 13972 dbg_rd_reg_exit: 13973 hwrm_req_drop(bp, req); 13974 return rc; 13975 } 13976 13977 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13978 u32 ring_id, u32 *prod, u32 *cons) 13979 { 13980 struct hwrm_dbg_ring_info_get_output *resp; 13981 struct hwrm_dbg_ring_info_get_input *req; 13982 int rc; 13983 13984 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13985 if (rc) 13986 return rc; 13987 13988 req->ring_type = ring_type; 13989 req->fw_ring_id = cpu_to_le32(ring_id); 13990 resp = hwrm_req_hold(bp, req); 13991 rc = hwrm_req_send(bp, req); 13992 if (!rc) { 13993 *prod = le32_to_cpu(resp->producer_index); 13994 *cons = le32_to_cpu(resp->consumer_index); 13995 } 13996 hwrm_req_drop(bp, req); 13997 return rc; 13998 } 13999 14000 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 14001 { 14002 struct bnxt_tx_ring_info *txr; 14003 int i = bnapi->index, j; 14004 14005 bnxt_for_each_napi_tx(j, bnapi, txr) 14006 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 14007 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 14008 txr->tx_cons); 14009 } 14010 14011 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 14012 { 14013 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 14014 int i = bnapi->index; 14015 14016 if (!rxr) 14017 return; 14018 14019 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", 14020 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 14021 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 14022 rxr->rx_sw_agg_prod); 14023 } 14024 14025 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 14026 { 14027 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 14028 int i = bnapi->index; 14029 14030 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 14031 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 14032 } 14033 14034 static void bnxt_dbg_dump_states(struct bnxt *bp) 14035 { 14036 int i; 14037 struct bnxt_napi *bnapi; 14038 14039 for (i = 0; i < bp->cp_nr_rings; i++) { 14040 bnapi = bp->bnapi[i]; 14041 if (netif_msg_drv(bp)) { 14042 bnxt_dump_tx_sw_state(bnapi); 14043 bnxt_dump_rx_sw_state(bnapi); 14044 bnxt_dump_cp_sw_state(bnapi); 14045 } 14046 } 14047 } 14048 14049 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 14050 { 14051 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 14052 struct hwrm_ring_reset_input *req; 14053 struct bnxt_napi *bnapi = rxr->bnapi; 14054 struct bnxt_cp_ring_info *cpr; 14055 u16 cp_ring_id; 14056 int rc; 14057 14058 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 14059 if (rc) 14060 return rc; 14061 14062 cpr = &bnapi->cp_ring; 14063 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 14064 req->cmpl_ring = cpu_to_le16(cp_ring_id); 14065 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 14066 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 14067 return hwrm_req_send_silent(bp, req); 14068 } 14069 14070 static void bnxt_reset_task(struct bnxt *bp, bool silent) 14071 { 14072 if (!silent) 14073 bnxt_dbg_dump_states(bp); 14074 if (netif_running(bp->dev)) { 14075 bnxt_close_nic(bp, !silent, false); 14076 bnxt_open_nic(bp, !silent, false); 14077 } 14078 } 14079 14080 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 14081 { 14082 struct bnxt *bp = netdev_priv(dev); 14083 14084 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 14085 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 14086 } 14087 14088 static void bnxt_fw_health_check(struct bnxt *bp) 14089 { 14090 struct bnxt_fw_health *fw_health = bp->fw_health; 14091 struct pci_dev *pdev = bp->pdev; 14092 u32 val; 14093 14094 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14095 return; 14096 14097 /* Make sure it is enabled before checking the tmr_counter. */ 14098 smp_rmb(); 14099 if (fw_health->tmr_counter) { 14100 fw_health->tmr_counter--; 14101 return; 14102 } 14103 14104 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14105 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 14106 fw_health->arrests++; 14107 goto fw_reset; 14108 } 14109 14110 fw_health->last_fw_heartbeat = val; 14111 14112 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14113 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 14114 fw_health->discoveries++; 14115 goto fw_reset; 14116 } 14117 14118 fw_health->tmr_counter = fw_health->tmr_multiplier; 14119 return; 14120 14121 fw_reset: 14122 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 14123 } 14124 14125 static void bnxt_timer(struct timer_list *t) 14126 { 14127 struct bnxt *bp = timer_container_of(bp, t, timer); 14128 struct net_device *dev = bp->dev; 14129 14130 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 14131 return; 14132 14133 if (atomic_read(&bp->intr_sem) != 0) 14134 goto bnxt_restart_timer; 14135 14136 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 14137 bnxt_fw_health_check(bp); 14138 14139 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 14140 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 14141 14142 if (bnxt_tc_flower_enabled(bp)) 14143 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 14144 14145 #ifdef CONFIG_RFS_ACCEL 14146 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 14147 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14148 #endif /*CONFIG_RFS_ACCEL*/ 14149 14150 if (bp->link_info.phy_retry) { 14151 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 14152 bp->link_info.phy_retry = false; 14153 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 14154 } else { 14155 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 14156 } 14157 } 14158 14159 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 14160 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 14161 14162 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 14163 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 14164 14165 bnxt_restart_timer: 14166 mod_timer(&bp->timer, jiffies + bp->current_interval); 14167 } 14168 14169 static void bnxt_lock_sp(struct bnxt *bp) 14170 { 14171 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 14172 * set. If the device is being closed, bnxt_close() may be holding 14173 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear. 14174 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev 14175 * instance lock. 14176 */ 14177 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14178 netdev_lock(bp->dev); 14179 } 14180 14181 static void bnxt_unlock_sp(struct bnxt *bp) 14182 { 14183 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14184 netdev_unlock(bp->dev); 14185 } 14186 14187 /* Only called from bnxt_sp_task() */ 14188 static void bnxt_reset(struct bnxt *bp, bool silent) 14189 { 14190 bnxt_lock_sp(bp); 14191 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 14192 bnxt_reset_task(bp, silent); 14193 bnxt_unlock_sp(bp); 14194 } 14195 14196 /* Only called from bnxt_sp_task() */ 14197 static void bnxt_rx_ring_reset(struct bnxt *bp) 14198 { 14199 int i; 14200 14201 bnxt_lock_sp(bp); 14202 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14203 bnxt_unlock_sp(bp); 14204 return; 14205 } 14206 /* Disable and flush TPA before resetting the RX ring */ 14207 if (bp->flags & BNXT_FLAG_TPA) 14208 bnxt_set_tpa(bp, false); 14209 for (i = 0; i < bp->rx_nr_rings; i++) { 14210 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 14211 struct bnxt_cp_ring_info *cpr; 14212 int rc; 14213 14214 if (!rxr->bnapi->in_reset) 14215 continue; 14216 14217 rc = bnxt_hwrm_rx_ring_reset(bp, i); 14218 if (rc) { 14219 if (rc == -EINVAL || rc == -EOPNOTSUPP) 14220 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 14221 else 14222 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 14223 rc); 14224 bnxt_reset_task(bp, true); 14225 break; 14226 } 14227 bnxt_free_one_rx_ring_skbs(bp, rxr); 14228 rxr->rx_prod = 0; 14229 rxr->rx_agg_prod = 0; 14230 rxr->rx_sw_agg_prod = 0; 14231 rxr->rx_next_cons = 0; 14232 rxr->bnapi->in_reset = false; 14233 bnxt_alloc_one_rx_ring(bp, i); 14234 cpr = &rxr->bnapi->cp_ring; 14235 cpr->sw_stats->rx.rx_resets++; 14236 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14237 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 14238 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 14239 } 14240 if (bp->flags & BNXT_FLAG_TPA) 14241 bnxt_set_tpa(bp, true); 14242 bnxt_unlock_sp(bp); 14243 } 14244 14245 static void bnxt_fw_fatal_close(struct bnxt *bp) 14246 { 14247 bnxt_tx_disable(bp); 14248 bnxt_disable_napi(bp); 14249 bnxt_disable_int_sync(bp); 14250 bnxt_free_irq(bp); 14251 bnxt_clear_int_mode(bp); 14252 pci_disable_device(bp->pdev); 14253 } 14254 14255 static void bnxt_fw_reset_close(struct bnxt *bp) 14256 { 14257 /* When firmware is in fatal state, quiesce device and disable 14258 * bus master to prevent any potential bad DMAs before freeing 14259 * kernel memory. 14260 */ 14261 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 14262 u16 val = 0; 14263 14264 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14265 if (val == 0xffff) 14266 bp->fw_reset_min_dsecs = 0; 14267 bnxt_fw_fatal_close(bp); 14268 } 14269 __bnxt_close_nic(bp, true, false); 14270 bnxt_vf_reps_free(bp); 14271 bnxt_clear_int_mode(bp); 14272 bnxt_hwrm_func_drv_unrgtr(bp); 14273 if (pci_is_enabled(bp->pdev)) 14274 pci_disable_device(bp->pdev); 14275 bnxt_free_ctx_mem(bp, false); 14276 } 14277 14278 static bool is_bnxt_fw_ok(struct bnxt *bp) 14279 { 14280 struct bnxt_fw_health *fw_health = bp->fw_health; 14281 bool no_heartbeat = false, has_reset = false; 14282 u32 val; 14283 14284 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 14285 if (val == fw_health->last_fw_heartbeat) 14286 no_heartbeat = true; 14287 14288 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14289 if (val != fw_health->last_fw_reset_cnt) 14290 has_reset = true; 14291 14292 if (!no_heartbeat && has_reset) 14293 return true; 14294 14295 return false; 14296 } 14297 14298 /* netdev instance lock is acquired before calling this function */ 14299 static void bnxt_force_fw_reset(struct bnxt *bp) 14300 { 14301 struct bnxt_fw_health *fw_health = bp->fw_health; 14302 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14303 u32 wait_dsecs; 14304 14305 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 14306 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 14307 return; 14308 14309 /* we have to serialize with bnxt_refclk_read()*/ 14310 if (ptp) { 14311 unsigned long flags; 14312 14313 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14314 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14315 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14316 } else { 14317 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14318 } 14319 bnxt_fw_reset_close(bp); 14320 wait_dsecs = fw_health->master_func_wait_dsecs; 14321 if (fw_health->primary) { 14322 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 14323 wait_dsecs = 0; 14324 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14325 } else { 14326 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 14327 wait_dsecs = fw_health->normal_func_wait_dsecs; 14328 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14329 } 14330 14331 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 14332 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 14333 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14334 } 14335 14336 void bnxt_fw_exception(struct bnxt *bp) 14337 { 14338 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 14339 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14340 bnxt_ulp_stop(bp); 14341 bnxt_lock_sp(bp); 14342 bnxt_force_fw_reset(bp); 14343 bnxt_unlock_sp(bp); 14344 } 14345 14346 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 14347 * < 0 on error. 14348 */ 14349 static int bnxt_get_registered_vfs(struct bnxt *bp) 14350 { 14351 #ifdef CONFIG_BNXT_SRIOV 14352 int rc; 14353 14354 if (!BNXT_PF(bp)) 14355 return 0; 14356 14357 rc = bnxt_hwrm_func_qcfg(bp); 14358 if (rc) { 14359 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 14360 return rc; 14361 } 14362 if (bp->pf.registered_vfs) 14363 return bp->pf.registered_vfs; 14364 if (bp->sriov_cfg) 14365 return 1; 14366 #endif 14367 return 0; 14368 } 14369 14370 void bnxt_fw_reset(struct bnxt *bp) 14371 { 14372 bnxt_ulp_stop(bp); 14373 bnxt_lock_sp(bp); 14374 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 14375 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14376 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 14377 int n = 0, tmo; 14378 14379 /* we have to serialize with bnxt_refclk_read()*/ 14380 if (ptp) { 14381 unsigned long flags; 14382 14383 write_seqlock_irqsave(&ptp->ptp_lock, flags); 14384 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14385 write_sequnlock_irqrestore(&ptp->ptp_lock, flags); 14386 } else { 14387 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14388 } 14389 if (bp->pf.active_vfs && 14390 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 14391 n = bnxt_get_registered_vfs(bp); 14392 if (n < 0) { 14393 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 14394 n); 14395 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14396 netif_close(bp->dev); 14397 goto fw_reset_exit; 14398 } else if (n > 0) { 14399 u16 vf_tmo_dsecs = n * 10; 14400 14401 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 14402 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 14403 bp->fw_reset_state = 14404 BNXT_FW_RESET_STATE_POLL_VF; 14405 bnxt_queue_fw_reset_work(bp, HZ / 10); 14406 goto fw_reset_exit; 14407 } 14408 bnxt_fw_reset_close(bp); 14409 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14410 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14411 tmo = HZ / 10; 14412 } else { 14413 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14414 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14415 } 14416 bnxt_queue_fw_reset_work(bp, tmo); 14417 } 14418 fw_reset_exit: 14419 bnxt_unlock_sp(bp); 14420 } 14421 14422 static void bnxt_chk_missed_irq(struct bnxt *bp) 14423 { 14424 int i; 14425 14426 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 14427 return; 14428 14429 for (i = 0; i < bp->cp_nr_rings; i++) { 14430 struct bnxt_napi *bnapi = bp->bnapi[i]; 14431 struct bnxt_cp_ring_info *cpr; 14432 u32 fw_ring_id; 14433 int j; 14434 14435 if (!bnapi) 14436 continue; 14437 14438 cpr = &bnapi->cp_ring; 14439 for (j = 0; j < cpr->cp_ring_count; j++) { 14440 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 14441 u32 val[2]; 14442 14443 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 14444 continue; 14445 14446 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 14447 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 14448 continue; 14449 } 14450 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 14451 bnxt_dbg_hwrm_ring_info_get(bp, 14452 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 14453 fw_ring_id, &val[0], &val[1]); 14454 cpr->sw_stats->cmn.missed_irqs++; 14455 } 14456 } 14457 } 14458 14459 static void bnxt_cfg_ntp_filters(struct bnxt *); 14460 14461 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 14462 { 14463 struct bnxt_link_info *link_info = &bp->link_info; 14464 14465 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 14466 link_info->autoneg = BNXT_AUTONEG_SPEED; 14467 if (bp->hwrm_spec_code >= 0x10201) { 14468 if (link_info->auto_pause_setting & 14469 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 14470 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14471 } else { 14472 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 14473 } 14474 bnxt_set_auto_speed(link_info); 14475 } else { 14476 bnxt_set_force_speed(link_info); 14477 link_info->req_duplex = link_info->duplex_setting; 14478 } 14479 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 14480 link_info->req_flow_ctrl = 14481 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 14482 else 14483 link_info->req_flow_ctrl = link_info->force_pause_setting; 14484 } 14485 14486 static void bnxt_fw_echo_reply(struct bnxt *bp) 14487 { 14488 struct bnxt_fw_health *fw_health = bp->fw_health; 14489 struct hwrm_func_echo_response_input *req; 14490 int rc; 14491 14492 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 14493 if (rc) 14494 return; 14495 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 14496 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 14497 hwrm_req_send(bp, req); 14498 } 14499 14500 static void bnxt_ulp_restart(struct bnxt *bp) 14501 { 14502 bnxt_ulp_stop(bp); 14503 bnxt_ulp_start(bp, 0); 14504 } 14505 14506 static void bnxt_sp_task(struct work_struct *work) 14507 { 14508 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 14509 14510 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14511 smp_mb__after_atomic(); 14512 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 14513 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14514 return; 14515 } 14516 14517 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 14518 bnxt_ulp_restart(bp); 14519 bnxt_reenable_sriov(bp); 14520 } 14521 14522 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 14523 bnxt_cfg_rx_mode(bp); 14524 14525 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 14526 bnxt_cfg_ntp_filters(bp); 14527 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 14528 bnxt_hwrm_exec_fwd_req(bp); 14529 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 14530 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 14531 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 14532 bnxt_hwrm_port_qstats(bp, 0); 14533 bnxt_hwrm_port_qstats_ext(bp, 0); 14534 bnxt_accumulate_all_stats(bp); 14535 } 14536 14537 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 14538 int rc; 14539 14540 mutex_lock(&bp->link_lock); 14541 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 14542 &bp->sp_event)) 14543 bnxt_hwrm_phy_qcaps(bp); 14544 14545 rc = bnxt_update_link(bp, true); 14546 if (rc) 14547 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 14548 rc); 14549 14550 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 14551 &bp->sp_event)) 14552 bnxt_init_ethtool_link_settings(bp); 14553 mutex_unlock(&bp->link_lock); 14554 } 14555 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 14556 int rc; 14557 14558 mutex_lock(&bp->link_lock); 14559 rc = bnxt_update_phy_setting(bp); 14560 mutex_unlock(&bp->link_lock); 14561 if (rc) { 14562 netdev_warn(bp->dev, "update phy settings retry failed\n"); 14563 } else { 14564 bp->link_info.phy_retry = false; 14565 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 14566 } 14567 } 14568 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 14569 mutex_lock(&bp->link_lock); 14570 bnxt_get_port_module_status(bp); 14571 mutex_unlock(&bp->link_lock); 14572 } 14573 14574 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 14575 bnxt_tc_flow_stats_work(bp); 14576 14577 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 14578 bnxt_chk_missed_irq(bp); 14579 14580 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 14581 bnxt_fw_echo_reply(bp); 14582 14583 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 14584 bnxt_hwmon_notify_event(bp); 14585 14586 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 14587 * must be the last functions to be called before exiting. 14588 */ 14589 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 14590 bnxt_reset(bp, false); 14591 14592 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 14593 bnxt_reset(bp, true); 14594 14595 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 14596 bnxt_rx_ring_reset(bp); 14597 14598 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 14599 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 14600 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 14601 bnxt_devlink_health_fw_report(bp); 14602 else 14603 bnxt_fw_reset(bp); 14604 } 14605 14606 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 14607 if (!is_bnxt_fw_ok(bp)) 14608 bnxt_devlink_health_fw_report(bp); 14609 } 14610 14611 smp_mb__before_atomic(); 14612 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 14613 } 14614 14615 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 14616 int *max_cp); 14617 14618 /* Under netdev instance lock */ 14619 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 14620 int tx_xdp) 14621 { 14622 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 14623 struct bnxt_hw_rings hwr = {0}; 14624 int rx_rings = rx; 14625 int rc; 14626 14627 if (tcs) 14628 tx_sets = tcs; 14629 14630 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 14631 14632 if (max_rx < rx_rings) 14633 return -ENOMEM; 14634 14635 if (bp->flags & BNXT_FLAG_AGG_RINGS) 14636 rx_rings <<= 1; 14637 14638 hwr.rx = rx_rings; 14639 hwr.tx = tx * tx_sets + tx_xdp; 14640 if (max_tx < hwr.tx) 14641 return -ENOMEM; 14642 14643 hwr.vnic = bnxt_get_total_vnics(bp, rx); 14644 14645 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 14646 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 14647 if (max_cp < hwr.cp) 14648 return -ENOMEM; 14649 hwr.stat = hwr.cp; 14650 if (BNXT_NEW_RM(bp)) { 14651 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 14652 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 14653 hwr.grp = rx; 14654 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 14655 } 14656 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 14657 hwr.cp_p5 = hwr.tx + rx; 14658 rc = bnxt_hwrm_check_rings(bp, &hwr); 14659 if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) { 14660 if (!bnxt_ulp_registered(bp->edev)) { 14661 hwr.cp += bnxt_get_ulp_msix_num(bp); 14662 hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp)); 14663 } 14664 if (hwr.cp > bp->total_irqs) { 14665 int total_msix = bnxt_change_msix(bp, hwr.cp); 14666 14667 if (total_msix < hwr.cp) { 14668 netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n", 14669 hwr.cp, total_msix); 14670 rc = -ENOSPC; 14671 } 14672 } 14673 } 14674 return rc; 14675 } 14676 14677 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 14678 { 14679 if (bp->bar2) { 14680 pci_iounmap(pdev, bp->bar2); 14681 bp->bar2 = NULL; 14682 } 14683 14684 if (bp->bar1) { 14685 pci_iounmap(pdev, bp->bar1); 14686 bp->bar1 = NULL; 14687 } 14688 14689 if (bp->bar0) { 14690 pci_iounmap(pdev, bp->bar0); 14691 bp->bar0 = NULL; 14692 } 14693 } 14694 14695 static void bnxt_cleanup_pci(struct bnxt *bp) 14696 { 14697 bnxt_unmap_bars(bp, bp->pdev); 14698 pci_release_regions(bp->pdev); 14699 if (pci_is_enabled(bp->pdev)) 14700 pci_disable_device(bp->pdev); 14701 } 14702 14703 static void bnxt_init_dflt_coal(struct bnxt *bp) 14704 { 14705 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 14706 struct bnxt_coal *coal; 14707 u16 flags = 0; 14708 14709 if (coal_cap->cmpl_params & 14710 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 14711 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 14712 14713 /* Tick values in micro seconds. 14714 * 1 coal_buf x bufs_per_record = 1 completion record. 14715 */ 14716 coal = &bp->rx_coal; 14717 coal->coal_ticks = 10; 14718 coal->coal_bufs = 30; 14719 coal->coal_ticks_irq = 1; 14720 coal->coal_bufs_irq = 2; 14721 coal->idle_thresh = 50; 14722 coal->bufs_per_record = 2; 14723 coal->budget = 64; /* NAPI budget */ 14724 coal->flags = flags; 14725 14726 coal = &bp->tx_coal; 14727 coal->coal_ticks = 28; 14728 coal->coal_bufs = 30; 14729 coal->coal_ticks_irq = 2; 14730 coal->coal_bufs_irq = 2; 14731 coal->bufs_per_record = 1; 14732 coal->flags = flags; 14733 14734 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 14735 } 14736 14737 /* FW that pre-reserves 1 VNIC per function */ 14738 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 14739 { 14740 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 14741 14742 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14743 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 14744 return true; 14745 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 14746 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 14747 return true; 14748 return false; 14749 } 14750 14751 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp) 14752 { 14753 struct hwrm_queue_pfcwd_timeout_qcaps_output *resp; 14754 struct hwrm_queue_pfcwd_timeout_qcaps_input *req; 14755 int rc; 14756 14757 bp->max_pfcwd_tmo_ms = 0; 14758 rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS); 14759 if (rc) 14760 return; 14761 resp = hwrm_req_hold(bp, req); 14762 rc = hwrm_req_send_silent(bp, req); 14763 if (!rc) 14764 bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout); 14765 hwrm_req_drop(bp, req); 14766 } 14767 14768 static int bnxt_fw_init_one_p1(struct bnxt *bp) 14769 { 14770 int rc; 14771 14772 bp->fw_cap = 0; 14773 rc = bnxt_hwrm_ver_get(bp); 14774 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 14775 * so wait before continuing with recovery. 14776 */ 14777 if (rc) 14778 msleep(100); 14779 bnxt_try_map_fw_health_reg(bp); 14780 if (rc) { 14781 rc = bnxt_try_recover_fw(bp); 14782 if (rc) 14783 return rc; 14784 rc = bnxt_hwrm_ver_get(bp); 14785 if (rc) 14786 return rc; 14787 } 14788 14789 bnxt_nvm_cfg_ver_get(bp); 14790 14791 rc = bnxt_hwrm_func_reset(bp); 14792 if (rc) 14793 return -ENODEV; 14794 14795 bnxt_hwrm_fw_set_time(bp); 14796 return 0; 14797 } 14798 14799 static int bnxt_fw_init_one_p2(struct bnxt *bp) 14800 { 14801 int rc; 14802 14803 /* Get the MAX capabilities for this function */ 14804 rc = bnxt_hwrm_func_qcaps(bp); 14805 if (rc) { 14806 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 14807 rc); 14808 return -ENODEV; 14809 } 14810 14811 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 14812 if (rc) 14813 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 14814 rc); 14815 14816 if (bnxt_alloc_fw_health(bp)) { 14817 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 14818 } else { 14819 rc = bnxt_hwrm_error_recovery_qcfg(bp); 14820 if (rc) 14821 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 14822 rc); 14823 } 14824 14825 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 14826 if (rc) 14827 return -ENODEV; 14828 14829 rc = bnxt_alloc_crash_dump_mem(bp); 14830 if (rc) 14831 netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n", 14832 rc); 14833 if (!rc) { 14834 rc = bnxt_hwrm_crash_dump_mem_cfg(bp); 14835 if (rc) { 14836 bnxt_free_crash_dump_mem(bp); 14837 netdev_warn(bp->dev, 14838 "hwrm crash dump mem failure rc: %d\n", rc); 14839 } 14840 } 14841 14842 if (bnxt_fw_pre_resv_vnics(bp)) 14843 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 14844 14845 bnxt_hwrm_pfcwd_qcaps(bp); 14846 bnxt_hwrm_func_qcfg(bp); 14847 bnxt_hwrm_vnic_qcaps(bp); 14848 bnxt_hwrm_port_led_qcaps(bp); 14849 bnxt_ethtool_init(bp); 14850 if (bp->fw_cap & BNXT_FW_CAP_PTP) 14851 __bnxt_hwrm_ptp_qcfg(bp); 14852 bnxt_dcb_init(bp); 14853 bnxt_hwmon_init(bp); 14854 return 0; 14855 } 14856 14857 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14858 { 14859 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14860 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14861 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14862 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14863 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14864 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14865 bp->rss_hash_delta = bp->rss_hash_cfg; 14866 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14867 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14868 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14869 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14870 } 14871 } 14872 14873 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14874 { 14875 struct net_device *dev = bp->dev; 14876 14877 dev->hw_features &= ~NETIF_F_NTUPLE; 14878 dev->features &= ~NETIF_F_NTUPLE; 14879 bp->flags &= ~BNXT_FLAG_RFS; 14880 if (bnxt_rfs_supported(bp)) { 14881 dev->hw_features |= NETIF_F_NTUPLE; 14882 if (bnxt_rfs_capable(bp, false)) { 14883 bp->flags |= BNXT_FLAG_RFS; 14884 dev->features |= NETIF_F_NTUPLE; 14885 } 14886 } 14887 } 14888 14889 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14890 { 14891 struct pci_dev *pdev = bp->pdev; 14892 14893 bnxt_set_dflt_rss_hash_type(bp); 14894 bnxt_set_dflt_rfs(bp); 14895 14896 bnxt_get_wol_settings(bp); 14897 if (bp->flags & BNXT_FLAG_WOL_CAP) 14898 device_set_wakeup_enable(&pdev->dev, bp->wol); 14899 else 14900 device_set_wakeup_capable(&pdev->dev, false); 14901 14902 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14903 bnxt_hwrm_coal_params_qcaps(bp); 14904 } 14905 14906 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14907 14908 int bnxt_fw_init_one(struct bnxt *bp) 14909 { 14910 int rc; 14911 14912 rc = bnxt_fw_init_one_p1(bp); 14913 if (rc) { 14914 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14915 return rc; 14916 } 14917 rc = bnxt_fw_init_one_p2(bp); 14918 if (rc) { 14919 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14920 return rc; 14921 } 14922 rc = bnxt_probe_phy(bp, false); 14923 if (rc) 14924 return rc; 14925 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14926 if (rc) 14927 return rc; 14928 14929 bnxt_fw_init_one_p3(bp); 14930 return 0; 14931 } 14932 14933 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14934 { 14935 struct bnxt_fw_health *fw_health = bp->fw_health; 14936 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14937 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14938 u32 reg_type, reg_off, delay_msecs; 14939 14940 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14941 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14942 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14943 switch (reg_type) { 14944 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14945 pci_write_config_dword(bp->pdev, reg_off, val); 14946 break; 14947 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14948 writel(reg_off & BNXT_GRC_BASE_MASK, 14949 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14950 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14951 fallthrough; 14952 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14953 writel(val, bp->bar0 + reg_off); 14954 break; 14955 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14956 writel(val, bp->bar1 + reg_off); 14957 break; 14958 } 14959 if (delay_msecs) { 14960 pci_read_config_dword(bp->pdev, 0, &val); 14961 msleep(delay_msecs); 14962 } 14963 } 14964 14965 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14966 { 14967 struct hwrm_func_qcfg_output *resp; 14968 struct hwrm_func_qcfg_input *req; 14969 bool result = true; /* firmware will enforce if unknown */ 14970 14971 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14972 return result; 14973 14974 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14975 return result; 14976 14977 req->fid = cpu_to_le16(0xffff); 14978 resp = hwrm_req_hold(bp, req); 14979 if (!hwrm_req_send(bp, req)) 14980 result = !!(le16_to_cpu(resp->flags) & 14981 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14982 hwrm_req_drop(bp, req); 14983 return result; 14984 } 14985 14986 static void bnxt_reset_all(struct bnxt *bp) 14987 { 14988 struct bnxt_fw_health *fw_health = bp->fw_health; 14989 int i, rc; 14990 14991 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14992 bnxt_fw_reset_via_optee(bp); 14993 bp->fw_reset_timestamp = jiffies; 14994 return; 14995 } 14996 14997 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14998 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14999 bnxt_fw_reset_writel(bp, i); 15000 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 15001 struct hwrm_fw_reset_input *req; 15002 15003 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 15004 if (!rc) { 15005 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 15006 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 15007 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 15008 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 15009 rc = hwrm_req_send(bp, req); 15010 } 15011 if (rc != -ENODEV) 15012 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 15013 } 15014 bp->fw_reset_timestamp = jiffies; 15015 } 15016 15017 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 15018 { 15019 return time_after(jiffies, bp->fw_reset_timestamp + 15020 (bp->fw_reset_max_dsecs * HZ / 10)); 15021 } 15022 15023 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 15024 { 15025 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15026 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 15027 bnxt_dl_health_fw_status_update(bp, false); 15028 bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT; 15029 netif_close(bp->dev); 15030 } 15031 15032 static void bnxt_fw_reset_task(struct work_struct *work) 15033 { 15034 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 15035 int rc = 0; 15036 15037 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 15038 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 15039 return; 15040 } 15041 15042 switch (bp->fw_reset_state) { 15043 case BNXT_FW_RESET_STATE_POLL_VF: { 15044 int n = bnxt_get_registered_vfs(bp); 15045 int tmo; 15046 15047 if (n < 0) { 15048 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 15049 n, jiffies_to_msecs(jiffies - 15050 bp->fw_reset_timestamp)); 15051 goto fw_reset_abort; 15052 } else if (n > 0) { 15053 if (bnxt_fw_reset_timeout(bp)) { 15054 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15055 bp->fw_reset_state = 0; 15056 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 15057 n); 15058 goto ulp_start; 15059 } 15060 bnxt_queue_fw_reset_work(bp, HZ / 10); 15061 return; 15062 } 15063 bp->fw_reset_timestamp = jiffies; 15064 netdev_lock(bp->dev); 15065 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 15066 bnxt_fw_reset_abort(bp, rc); 15067 netdev_unlock(bp->dev); 15068 goto ulp_start; 15069 } 15070 bnxt_fw_reset_close(bp); 15071 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 15072 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 15073 tmo = HZ / 10; 15074 } else { 15075 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15076 tmo = bp->fw_reset_min_dsecs * HZ / 10; 15077 } 15078 netdev_unlock(bp->dev); 15079 bnxt_queue_fw_reset_work(bp, tmo); 15080 return; 15081 } 15082 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 15083 u32 val; 15084 15085 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15086 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 15087 !bnxt_fw_reset_timeout(bp)) { 15088 bnxt_queue_fw_reset_work(bp, HZ / 5); 15089 return; 15090 } 15091 15092 if (!bp->fw_health->primary) { 15093 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 15094 15095 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15096 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 15097 return; 15098 } 15099 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 15100 } 15101 fallthrough; 15102 case BNXT_FW_RESET_STATE_RESET_FW: 15103 bnxt_reset_all(bp); 15104 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 15105 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 15106 return; 15107 case BNXT_FW_RESET_STATE_ENABLE_DEV: 15108 bnxt_inv_fw_health_reg(bp); 15109 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 15110 !bp->fw_reset_min_dsecs) { 15111 u16 val; 15112 15113 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 15114 if (val == 0xffff) { 15115 if (bnxt_fw_reset_timeout(bp)) { 15116 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 15117 rc = -ETIMEDOUT; 15118 goto fw_reset_abort; 15119 } 15120 bnxt_queue_fw_reset_work(bp, HZ / 1000); 15121 return; 15122 } 15123 } 15124 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 15125 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 15126 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 15127 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 15128 bnxt_dl_remote_reload(bp); 15129 if (pci_enable_device(bp->pdev)) { 15130 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 15131 rc = -ENODEV; 15132 goto fw_reset_abort; 15133 } 15134 pci_set_master(bp->pdev); 15135 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 15136 fallthrough; 15137 case BNXT_FW_RESET_STATE_POLL_FW: 15138 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 15139 rc = bnxt_hwrm_poll(bp); 15140 if (rc) { 15141 if (bnxt_fw_reset_timeout(bp)) { 15142 netdev_err(bp->dev, "Firmware reset aborted\n"); 15143 goto fw_reset_abort_status; 15144 } 15145 bnxt_queue_fw_reset_work(bp, HZ / 5); 15146 return; 15147 } 15148 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 15149 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 15150 fallthrough; 15151 case BNXT_FW_RESET_STATE_OPENING: 15152 while (!netdev_trylock(bp->dev)) { 15153 bnxt_queue_fw_reset_work(bp, HZ / 10); 15154 return; 15155 } 15156 rc = bnxt_open(bp->dev); 15157 if (rc) { 15158 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 15159 bnxt_fw_reset_abort(bp, rc); 15160 netdev_unlock(bp->dev); 15161 goto ulp_start; 15162 } 15163 15164 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 15165 bp->fw_health->enabled) { 15166 bp->fw_health->last_fw_reset_cnt = 15167 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 15168 } 15169 bp->fw_reset_state = 0; 15170 /* Make sure fw_reset_state is 0 before clearing the flag */ 15171 smp_mb__before_atomic(); 15172 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15173 bnxt_ptp_reapply_pps(bp); 15174 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 15175 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 15176 bnxt_dl_health_fw_recovery_done(bp); 15177 bnxt_dl_health_fw_status_update(bp, true); 15178 } 15179 netdev_unlock(bp->dev); 15180 bnxt_ulp_start(bp, 0); 15181 bnxt_reenable_sriov(bp); 15182 netdev_lock(bp->dev); 15183 bnxt_vf_reps_alloc(bp); 15184 bnxt_vf_reps_open(bp); 15185 netdev_unlock(bp->dev); 15186 break; 15187 } 15188 return; 15189 15190 fw_reset_abort_status: 15191 if (bp->fw_health->status_reliable || 15192 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 15193 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 15194 15195 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 15196 } 15197 fw_reset_abort: 15198 netdev_lock(bp->dev); 15199 bnxt_fw_reset_abort(bp, rc); 15200 netdev_unlock(bp->dev); 15201 ulp_start: 15202 bnxt_ulp_start(bp, rc); 15203 } 15204 15205 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 15206 { 15207 int rc; 15208 struct bnxt *bp = netdev_priv(dev); 15209 15210 SET_NETDEV_DEV(dev, &pdev->dev); 15211 15212 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 15213 rc = pci_enable_device(pdev); 15214 if (rc) { 15215 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 15216 goto init_err; 15217 } 15218 15219 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 15220 dev_err(&pdev->dev, 15221 "Cannot find PCI device base address, aborting\n"); 15222 rc = -ENODEV; 15223 goto init_err_disable; 15224 } 15225 15226 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 15227 if (rc) { 15228 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 15229 goto init_err_disable; 15230 } 15231 15232 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 15233 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 15234 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 15235 rc = -EIO; 15236 goto init_err_release; 15237 } 15238 15239 pci_set_master(pdev); 15240 15241 bp->dev = dev; 15242 bp->pdev = pdev; 15243 15244 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 15245 * determines the BAR size. 15246 */ 15247 bp->bar0 = pci_ioremap_bar(pdev, 0); 15248 if (!bp->bar0) { 15249 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 15250 rc = -ENOMEM; 15251 goto init_err_release; 15252 } 15253 15254 bp->bar2 = pci_ioremap_bar(pdev, 4); 15255 if (!bp->bar2) { 15256 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 15257 rc = -ENOMEM; 15258 goto init_err_release; 15259 } 15260 15261 INIT_WORK(&bp->sp_task, bnxt_sp_task); 15262 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 15263 15264 spin_lock_init(&bp->ntp_fltr_lock); 15265 #if BITS_PER_LONG == 32 15266 spin_lock_init(&bp->db_lock); 15267 #endif 15268 15269 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 15270 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 15271 15272 timer_setup(&bp->timer, bnxt_timer, 0); 15273 bp->current_interval = BNXT_TIMER_INTERVAL; 15274 15275 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 15276 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 15277 15278 clear_bit(BNXT_STATE_OPEN, &bp->state); 15279 return 0; 15280 15281 init_err_release: 15282 bnxt_unmap_bars(bp, pdev); 15283 pci_release_regions(pdev); 15284 15285 init_err_disable: 15286 pci_disable_device(pdev); 15287 15288 init_err: 15289 return rc; 15290 } 15291 15292 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 15293 { 15294 struct sockaddr *addr = p; 15295 struct bnxt *bp = netdev_priv(dev); 15296 int rc = 0; 15297 15298 netdev_assert_locked(dev); 15299 15300 if (!is_valid_ether_addr(addr->sa_data)) 15301 return -EADDRNOTAVAIL; 15302 15303 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 15304 return 0; 15305 15306 rc = bnxt_approve_mac(bp, addr->sa_data, true); 15307 if (rc) 15308 return rc; 15309 15310 eth_hw_addr_set(dev, addr->sa_data); 15311 bnxt_clear_usr_fltrs(bp, true); 15312 if (netif_running(dev)) { 15313 bnxt_close_nic(bp, false, false); 15314 rc = bnxt_open_nic(bp, false, false); 15315 } 15316 15317 return rc; 15318 } 15319 15320 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 15321 { 15322 struct bnxt *bp = netdev_priv(dev); 15323 15324 netdev_assert_locked(dev); 15325 15326 if (netif_running(dev)) 15327 bnxt_close_nic(bp, true, false); 15328 15329 WRITE_ONCE(dev->mtu, new_mtu); 15330 15331 /* MTU change may change the AGG ring settings if an XDP multi-buffer 15332 * program is attached. We need to set the AGG rings settings and 15333 * rx_skb_func accordingly. 15334 */ 15335 if (READ_ONCE(bp->xdp_prog)) 15336 bnxt_set_rx_skb_mode(bp, true); 15337 15338 bnxt_set_ring_params(bp); 15339 15340 if (netif_running(dev)) 15341 return bnxt_open_nic(bp, true, false); 15342 15343 return 0; 15344 } 15345 15346 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 15347 { 15348 struct bnxt *bp = netdev_priv(dev); 15349 bool sh = false; 15350 int rc, tx_cp; 15351 15352 if (tc > bp->max_tc) { 15353 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 15354 tc, bp->max_tc); 15355 return -EINVAL; 15356 } 15357 15358 if (bp->num_tc == tc) 15359 return 0; 15360 15361 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 15362 sh = true; 15363 15364 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 15365 sh, tc, bp->tx_nr_rings_xdp); 15366 if (rc) 15367 return rc; 15368 15369 /* Needs to close the device and do hw resource re-allocations */ 15370 if (netif_running(bp->dev)) 15371 bnxt_close_nic(bp, true, false); 15372 15373 if (tc) { 15374 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 15375 netdev_set_num_tc(dev, tc); 15376 bp->num_tc = tc; 15377 } else { 15378 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15379 netdev_reset_tc(dev); 15380 bp->num_tc = 0; 15381 } 15382 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 15383 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 15384 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 15385 tx_cp + bp->rx_nr_rings; 15386 15387 if (netif_running(bp->dev)) 15388 return bnxt_open_nic(bp, true, false); 15389 15390 return 0; 15391 } 15392 15393 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 15394 void *cb_priv) 15395 { 15396 struct bnxt *bp = cb_priv; 15397 15398 if (!bnxt_tc_flower_enabled(bp) || 15399 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 15400 return -EOPNOTSUPP; 15401 15402 switch (type) { 15403 case TC_SETUP_CLSFLOWER: 15404 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 15405 default: 15406 return -EOPNOTSUPP; 15407 } 15408 } 15409 15410 LIST_HEAD(bnxt_block_cb_list); 15411 15412 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 15413 void *type_data) 15414 { 15415 struct bnxt *bp = netdev_priv(dev); 15416 15417 switch (type) { 15418 case TC_SETUP_BLOCK: 15419 return flow_block_cb_setup_simple(type_data, 15420 &bnxt_block_cb_list, 15421 bnxt_setup_tc_block_cb, 15422 bp, bp, true); 15423 case TC_SETUP_QDISC_MQPRIO: { 15424 struct tc_mqprio_qopt *mqprio = type_data; 15425 15426 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 15427 15428 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 15429 } 15430 default: 15431 return -EOPNOTSUPP; 15432 } 15433 } 15434 15435 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 15436 const struct sk_buff *skb) 15437 { 15438 struct bnxt_vnic_info *vnic; 15439 15440 if (skb) 15441 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 15442 15443 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 15444 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 15445 } 15446 15447 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 15448 u32 idx) 15449 { 15450 struct hlist_head *head; 15451 int bit_id; 15452 15453 spin_lock_bh(&bp->ntp_fltr_lock); 15454 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 15455 if (bit_id < 0) { 15456 spin_unlock_bh(&bp->ntp_fltr_lock); 15457 return -ENOMEM; 15458 } 15459 15460 fltr->base.sw_id = (u16)bit_id; 15461 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 15462 fltr->base.flags |= BNXT_ACT_RING_DST; 15463 head = &bp->ntp_fltr_hash_tbl[idx]; 15464 hlist_add_head_rcu(&fltr->base.hash, head); 15465 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 15466 bnxt_insert_usr_fltr(bp, &fltr->base); 15467 bp->ntp_fltr_count++; 15468 spin_unlock_bh(&bp->ntp_fltr_lock); 15469 return 0; 15470 } 15471 15472 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 15473 struct bnxt_ntuple_filter *f2) 15474 { 15475 struct bnxt_flow_masks *masks1 = &f1->fmasks; 15476 struct bnxt_flow_masks *masks2 = &f2->fmasks; 15477 struct flow_keys *keys1 = &f1->fkeys; 15478 struct flow_keys *keys2 = &f2->fkeys; 15479 15480 if (keys1->basic.n_proto != keys2->basic.n_proto || 15481 keys1->basic.ip_proto != keys2->basic.ip_proto) 15482 return false; 15483 15484 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 15485 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 15486 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 15487 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 15488 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 15489 return false; 15490 } else { 15491 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 15492 &keys2->addrs.v6addrs.src) || 15493 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 15494 &masks2->addrs.v6addrs.src) || 15495 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 15496 &keys2->addrs.v6addrs.dst) || 15497 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 15498 &masks2->addrs.v6addrs.dst)) 15499 return false; 15500 } 15501 15502 return keys1->ports.src == keys2->ports.src && 15503 masks1->ports.src == masks2->ports.src && 15504 keys1->ports.dst == keys2->ports.dst && 15505 masks1->ports.dst == masks2->ports.dst && 15506 keys1->control.flags == keys2->control.flags && 15507 f1->l2_fltr == f2->l2_fltr; 15508 } 15509 15510 struct bnxt_ntuple_filter * 15511 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 15512 struct bnxt_ntuple_filter *fltr, u32 idx) 15513 { 15514 struct bnxt_ntuple_filter *f; 15515 struct hlist_head *head; 15516 15517 head = &bp->ntp_fltr_hash_tbl[idx]; 15518 hlist_for_each_entry_rcu(f, head, base.hash) { 15519 if (bnxt_fltr_match(f, fltr)) 15520 return f; 15521 } 15522 return NULL; 15523 } 15524 15525 #ifdef CONFIG_RFS_ACCEL 15526 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 15527 u16 rxq_index, u32 flow_id) 15528 { 15529 struct bnxt *bp = netdev_priv(dev); 15530 struct bnxt_ntuple_filter *fltr, *new_fltr; 15531 struct flow_keys *fkeys; 15532 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 15533 struct bnxt_l2_filter *l2_fltr; 15534 int rc = 0, idx; 15535 u32 flags; 15536 15537 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 15538 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 15539 atomic_inc(&l2_fltr->refcnt); 15540 } else { 15541 struct bnxt_l2_key key; 15542 15543 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 15544 key.vlan = 0; 15545 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 15546 if (!l2_fltr) 15547 return -EINVAL; 15548 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 15549 bnxt_del_l2_filter(bp, l2_fltr); 15550 return -EINVAL; 15551 } 15552 } 15553 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 15554 if (!new_fltr) { 15555 bnxt_del_l2_filter(bp, l2_fltr); 15556 return -ENOMEM; 15557 } 15558 15559 fkeys = &new_fltr->fkeys; 15560 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 15561 rc = -EPROTONOSUPPORT; 15562 goto err_free; 15563 } 15564 15565 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 15566 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 15567 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 15568 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 15569 rc = -EPROTONOSUPPORT; 15570 goto err_free; 15571 } 15572 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 15573 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 15574 if (bp->hwrm_spec_code < 0x10601) { 15575 rc = -EPROTONOSUPPORT; 15576 goto err_free; 15577 } 15578 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 15579 } 15580 flags = fkeys->control.flags; 15581 if (((flags & FLOW_DIS_ENCAPSULATION) && 15582 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 15583 rc = -EPROTONOSUPPORT; 15584 goto err_free; 15585 } 15586 new_fltr->l2_fltr = l2_fltr; 15587 15588 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 15589 rcu_read_lock(); 15590 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 15591 if (fltr) { 15592 rc = fltr->base.sw_id; 15593 rcu_read_unlock(); 15594 goto err_free; 15595 } 15596 rcu_read_unlock(); 15597 15598 new_fltr->flow_id = flow_id; 15599 new_fltr->base.rxq = rxq_index; 15600 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 15601 if (!rc) { 15602 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 15603 return new_fltr->base.sw_id; 15604 } 15605 15606 err_free: 15607 bnxt_del_l2_filter(bp, l2_fltr); 15608 kfree(new_fltr); 15609 return rc; 15610 } 15611 #endif 15612 15613 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 15614 { 15615 spin_lock_bh(&bp->ntp_fltr_lock); 15616 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 15617 spin_unlock_bh(&bp->ntp_fltr_lock); 15618 return; 15619 } 15620 hlist_del_rcu(&fltr->base.hash); 15621 bnxt_del_one_usr_fltr(bp, &fltr->base); 15622 bp->ntp_fltr_count--; 15623 spin_unlock_bh(&bp->ntp_fltr_lock); 15624 bnxt_del_l2_filter(bp, fltr->l2_fltr); 15625 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 15626 kfree_rcu(fltr, base.rcu); 15627 } 15628 15629 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 15630 { 15631 #ifdef CONFIG_RFS_ACCEL 15632 int i; 15633 15634 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 15635 struct hlist_head *head; 15636 struct hlist_node *tmp; 15637 struct bnxt_ntuple_filter *fltr; 15638 int rc; 15639 15640 head = &bp->ntp_fltr_hash_tbl[i]; 15641 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 15642 bool del = false; 15643 15644 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 15645 if (fltr->base.flags & BNXT_ACT_NO_AGING) 15646 continue; 15647 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 15648 fltr->flow_id, 15649 fltr->base.sw_id)) { 15650 bnxt_hwrm_cfa_ntuple_filter_free(bp, 15651 fltr); 15652 del = true; 15653 } 15654 } else { 15655 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 15656 fltr); 15657 if (rc) 15658 del = true; 15659 else 15660 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 15661 } 15662 15663 if (del) 15664 bnxt_del_ntp_filter(bp, fltr); 15665 } 15666 } 15667 #endif 15668 } 15669 15670 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 15671 unsigned int entry, struct udp_tunnel_info *ti) 15672 { 15673 struct bnxt *bp = netdev_priv(netdev); 15674 unsigned int cmd; 15675 15676 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15677 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 15678 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15679 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 15680 else 15681 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 15682 15683 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 15684 } 15685 15686 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 15687 unsigned int entry, struct udp_tunnel_info *ti) 15688 { 15689 struct bnxt *bp = netdev_priv(netdev); 15690 unsigned int cmd; 15691 15692 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 15693 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 15694 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 15695 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 15696 else 15697 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 15698 15699 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 15700 } 15701 15702 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 15703 .set_port = bnxt_udp_tunnel_set_port, 15704 .unset_port = bnxt_udp_tunnel_unset_port, 15705 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15706 .tables = { 15707 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15708 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15709 }, 15710 }, bnxt_udp_tunnels_p7 = { 15711 .set_port = bnxt_udp_tunnel_set_port, 15712 .unset_port = bnxt_udp_tunnel_unset_port, 15713 .flags = UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 15714 .tables = { 15715 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 15716 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 15717 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 15718 }, 15719 }; 15720 15721 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 15722 struct net_device *dev, u32 filter_mask, 15723 int nlflags) 15724 { 15725 struct bnxt *bp = netdev_priv(dev); 15726 15727 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 15728 nlflags, filter_mask, NULL); 15729 } 15730 15731 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 15732 u16 flags, struct netlink_ext_ack *extack) 15733 { 15734 struct bnxt *bp = netdev_priv(dev); 15735 struct nlattr *attr, *br_spec; 15736 int rem, rc = 0; 15737 15738 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 15739 return -EOPNOTSUPP; 15740 15741 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 15742 if (!br_spec) 15743 return -EINVAL; 15744 15745 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 15746 u16 mode; 15747 15748 mode = nla_get_u16(attr); 15749 if (mode == bp->br_mode) 15750 break; 15751 15752 rc = bnxt_hwrm_set_br_mode(bp, mode); 15753 if (!rc) 15754 bp->br_mode = mode; 15755 break; 15756 } 15757 return rc; 15758 } 15759 15760 int bnxt_get_port_parent_id(struct net_device *dev, 15761 struct netdev_phys_item_id *ppid) 15762 { 15763 struct bnxt *bp = netdev_priv(dev); 15764 15765 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 15766 return -EOPNOTSUPP; 15767 15768 /* The PF and it's VF-reps only support the switchdev framework */ 15769 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 15770 return -EOPNOTSUPP; 15771 15772 ppid->id_len = sizeof(bp->dsn); 15773 memcpy(ppid->id, bp->dsn, ppid->id_len); 15774 15775 return 0; 15776 } 15777 15778 static const struct net_device_ops bnxt_netdev_ops = { 15779 .ndo_open = bnxt_open, 15780 .ndo_start_xmit = bnxt_start_xmit, 15781 .ndo_stop = bnxt_close, 15782 .ndo_get_stats64 = bnxt_get_stats64, 15783 .ndo_set_rx_mode = bnxt_set_rx_mode, 15784 .ndo_eth_ioctl = bnxt_ioctl, 15785 .ndo_validate_addr = eth_validate_addr, 15786 .ndo_set_mac_address = bnxt_change_mac_addr, 15787 .ndo_change_mtu = bnxt_change_mtu, 15788 .ndo_fix_features = bnxt_fix_features, 15789 .ndo_set_features = bnxt_set_features, 15790 .ndo_features_check = bnxt_features_check, 15791 .ndo_tx_timeout = bnxt_tx_timeout, 15792 #ifdef CONFIG_BNXT_SRIOV 15793 .ndo_get_vf_config = bnxt_get_vf_config, 15794 .ndo_set_vf_mac = bnxt_set_vf_mac, 15795 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 15796 .ndo_set_vf_rate = bnxt_set_vf_bw, 15797 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 15798 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 15799 .ndo_set_vf_trust = bnxt_set_vf_trust, 15800 #endif 15801 .ndo_setup_tc = bnxt_setup_tc, 15802 #ifdef CONFIG_RFS_ACCEL 15803 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 15804 #endif 15805 .ndo_bpf = bnxt_xdp, 15806 .ndo_xdp_xmit = bnxt_xdp_xmit, 15807 .ndo_bridge_getlink = bnxt_bridge_getlink, 15808 .ndo_bridge_setlink = bnxt_bridge_setlink, 15809 }; 15810 15811 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 15812 struct netdev_queue_stats_rx *stats) 15813 { 15814 struct bnxt *bp = netdev_priv(dev); 15815 struct bnxt_cp_ring_info *cpr; 15816 u64 *sw; 15817 15818 if (!bp->bnapi) 15819 return; 15820 15821 cpr = &bp->bnapi[i]->cp_ring; 15822 sw = cpr->stats.sw_stats; 15823 15824 stats->packets = 0; 15825 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 15826 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 15827 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 15828 15829 stats->bytes = 0; 15830 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 15831 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 15832 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 15833 15834 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 15835 } 15836 15837 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 15838 struct netdev_queue_stats_tx *stats) 15839 { 15840 struct bnxt *bp = netdev_priv(dev); 15841 struct bnxt_napi *bnapi; 15842 u64 *sw; 15843 15844 if (!bp->tx_ring) 15845 return; 15846 15847 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 15848 sw = bnapi->cp_ring.stats.sw_stats; 15849 15850 stats->packets = 0; 15851 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 15852 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 15853 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 15854 15855 stats->bytes = 0; 15856 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 15857 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 15858 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 15859 } 15860 15861 static void bnxt_get_base_stats(struct net_device *dev, 15862 struct netdev_queue_stats_rx *rx, 15863 struct netdev_queue_stats_tx *tx) 15864 { 15865 struct bnxt *bp = netdev_priv(dev); 15866 15867 rx->packets = bp->net_stats_prev.rx_packets; 15868 rx->bytes = bp->net_stats_prev.rx_bytes; 15869 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15870 15871 tx->packets = bp->net_stats_prev.tx_packets; 15872 tx->bytes = bp->net_stats_prev.tx_bytes; 15873 } 15874 15875 static const struct netdev_stat_ops bnxt_stat_ops = { 15876 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15877 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15878 .get_base_stats = bnxt_get_base_stats, 15879 }; 15880 15881 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15882 { 15883 struct bnxt_rx_ring_info *rxr, *clone; 15884 struct bnxt *bp = netdev_priv(dev); 15885 struct bnxt_ring_struct *ring; 15886 int rc; 15887 15888 if (!bp->rx_ring) 15889 return -ENETDOWN; 15890 15891 rxr = &bp->rx_ring[idx]; 15892 clone = qmem; 15893 memcpy(clone, rxr, sizeof(*rxr)); 15894 bnxt_init_rx_ring_struct(bp, clone); 15895 bnxt_reset_rx_ring_struct(bp, clone); 15896 15897 clone->rx_prod = 0; 15898 clone->rx_agg_prod = 0; 15899 clone->rx_sw_agg_prod = 0; 15900 clone->rx_next_cons = 0; 15901 clone->need_head_pool = false; 15902 15903 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15904 if (rc) 15905 return rc; 15906 15907 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15908 if (rc < 0) 15909 goto err_page_pool_destroy; 15910 15911 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15912 MEM_TYPE_PAGE_POOL, 15913 clone->page_pool); 15914 if (rc) 15915 goto err_rxq_info_unreg; 15916 15917 ring = &clone->rx_ring_struct; 15918 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15919 if (rc) 15920 goto err_free_rx_ring; 15921 15922 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15923 ring = &clone->rx_agg_ring_struct; 15924 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15925 if (rc) 15926 goto err_free_rx_agg_ring; 15927 15928 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15929 if (rc) 15930 goto err_free_rx_agg_ring; 15931 } 15932 15933 if (bp->flags & BNXT_FLAG_TPA) { 15934 rc = bnxt_alloc_one_tpa_info(bp, clone); 15935 if (rc) 15936 goto err_free_tpa_info; 15937 } 15938 15939 bnxt_init_one_rx_ring_rxbd(bp, clone); 15940 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15941 15942 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15943 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15944 bnxt_alloc_one_rx_ring_netmem(bp, clone, idx); 15945 if (bp->flags & BNXT_FLAG_TPA) 15946 bnxt_alloc_one_tpa_info_data(bp, clone); 15947 15948 return 0; 15949 15950 err_free_tpa_info: 15951 bnxt_free_one_tpa_info(bp, clone); 15952 err_free_rx_agg_ring: 15953 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15954 err_free_rx_ring: 15955 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15956 err_rxq_info_unreg: 15957 xdp_rxq_info_unreg(&clone->xdp_rxq); 15958 err_page_pool_destroy: 15959 page_pool_destroy(clone->page_pool); 15960 page_pool_destroy(clone->head_pool); 15961 clone->page_pool = NULL; 15962 clone->head_pool = NULL; 15963 return rc; 15964 } 15965 15966 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15967 { 15968 struct bnxt_rx_ring_info *rxr = qmem; 15969 struct bnxt *bp = netdev_priv(dev); 15970 struct bnxt_ring_struct *ring; 15971 15972 bnxt_free_one_rx_ring_skbs(bp, rxr); 15973 bnxt_free_one_tpa_info(bp, rxr); 15974 15975 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15976 15977 page_pool_destroy(rxr->page_pool); 15978 page_pool_destroy(rxr->head_pool); 15979 rxr->page_pool = NULL; 15980 rxr->head_pool = NULL; 15981 15982 ring = &rxr->rx_ring_struct; 15983 bnxt_free_ring(bp, &ring->ring_mem); 15984 15985 ring = &rxr->rx_agg_ring_struct; 15986 bnxt_free_ring(bp, &ring->ring_mem); 15987 15988 kfree(rxr->rx_agg_bmap); 15989 rxr->rx_agg_bmap = NULL; 15990 } 15991 15992 static void bnxt_copy_rx_ring(struct bnxt *bp, 15993 struct bnxt_rx_ring_info *dst, 15994 struct bnxt_rx_ring_info *src) 15995 { 15996 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15997 struct bnxt_ring_struct *dst_ring, *src_ring; 15998 int i; 15999 16000 dst_ring = &dst->rx_ring_struct; 16001 dst_rmem = &dst_ring->ring_mem; 16002 src_ring = &src->rx_ring_struct; 16003 src_rmem = &src_ring->ring_mem; 16004 16005 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16006 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16007 WARN_ON(dst_rmem->flags != src_rmem->flags); 16008 WARN_ON(dst_rmem->depth != src_rmem->depth); 16009 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16010 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16011 16012 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16013 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16014 *dst_rmem->vmem = *src_rmem->vmem; 16015 for (i = 0; i < dst_rmem->nr_pages; i++) { 16016 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16017 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16018 } 16019 16020 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 16021 return; 16022 16023 dst_ring = &dst->rx_agg_ring_struct; 16024 dst_rmem = &dst_ring->ring_mem; 16025 src_ring = &src->rx_agg_ring_struct; 16026 src_rmem = &src_ring->ring_mem; 16027 16028 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 16029 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 16030 WARN_ON(dst_rmem->flags != src_rmem->flags); 16031 WARN_ON(dst_rmem->depth != src_rmem->depth); 16032 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 16033 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 16034 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 16035 16036 dst_rmem->pg_tbl = src_rmem->pg_tbl; 16037 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 16038 *dst_rmem->vmem = *src_rmem->vmem; 16039 for (i = 0; i < dst_rmem->nr_pages; i++) { 16040 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 16041 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 16042 } 16043 16044 dst->rx_agg_bmap = src->rx_agg_bmap; 16045 } 16046 16047 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 16048 { 16049 struct bnxt *bp = netdev_priv(dev); 16050 struct bnxt_rx_ring_info *rxr, *clone; 16051 struct bnxt_cp_ring_info *cpr; 16052 struct bnxt_vnic_info *vnic; 16053 struct bnxt_napi *bnapi; 16054 int i, rc; 16055 u16 mru; 16056 16057 rxr = &bp->rx_ring[idx]; 16058 clone = qmem; 16059 16060 rxr->rx_prod = clone->rx_prod; 16061 rxr->rx_agg_prod = clone->rx_agg_prod; 16062 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 16063 rxr->rx_next_cons = clone->rx_next_cons; 16064 rxr->rx_tpa = clone->rx_tpa; 16065 rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map; 16066 rxr->page_pool = clone->page_pool; 16067 rxr->head_pool = clone->head_pool; 16068 rxr->xdp_rxq = clone->xdp_rxq; 16069 rxr->need_head_pool = clone->need_head_pool; 16070 16071 bnxt_copy_rx_ring(bp, rxr, clone); 16072 16073 bnapi = rxr->bnapi; 16074 cpr = &bnapi->cp_ring; 16075 16076 /* All rings have been reserved and previously allocated. 16077 * Reallocating with the same parameters should never fail. 16078 */ 16079 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 16080 if (rc) 16081 goto err_reset; 16082 16083 if (bp->tph_mode) { 16084 rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr); 16085 if (rc) 16086 goto err_reset; 16087 } 16088 16089 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 16090 if (rc) 16091 goto err_reset; 16092 16093 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 16094 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16095 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 16096 16097 if (bp->flags & BNXT_FLAG_SHARED_RINGS) { 16098 rc = bnxt_tx_queue_start(bp, idx); 16099 if (rc) 16100 goto err_reset; 16101 } 16102 16103 bnxt_enable_rx_page_pool(rxr); 16104 napi_enable_locked(&bnapi->napi); 16105 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16106 16107 mru = bp->dev->mtu + VLAN_ETH_HLEN; 16108 for (i = 0; i < bp->nr_vnics; i++) { 16109 vnic = &bp->vnic_info[i]; 16110 16111 rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx); 16112 if (rc) 16113 return rc; 16114 } 16115 return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx); 16116 16117 err_reset: 16118 netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n", 16119 rc); 16120 napi_enable_locked(&bnapi->napi); 16121 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 16122 bnxt_reset_task(bp, true); 16123 return rc; 16124 } 16125 16126 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 16127 { 16128 struct bnxt *bp = netdev_priv(dev); 16129 struct bnxt_rx_ring_info *rxr; 16130 struct bnxt_cp_ring_info *cpr; 16131 struct bnxt_vnic_info *vnic; 16132 struct bnxt_napi *bnapi; 16133 int i; 16134 16135 for (i = 0; i < bp->nr_vnics; i++) { 16136 vnic = &bp->vnic_info[i]; 16137 16138 bnxt_set_vnic_mru_p5(bp, vnic, 0, idx); 16139 } 16140 bnxt_set_rss_ctx_vnic_mru(bp, 0, idx); 16141 /* Make sure NAPI sees that the VNIC is disabled */ 16142 synchronize_net(); 16143 rxr = &bp->rx_ring[idx]; 16144 bnapi = rxr->bnapi; 16145 cpr = &bnapi->cp_ring; 16146 cancel_work_sync(&cpr->dim.work); 16147 bnxt_hwrm_rx_ring_free(bp, rxr, false); 16148 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 16149 page_pool_disable_direct_recycling(rxr->page_pool); 16150 if (bnxt_separate_head_pool(rxr)) 16151 page_pool_disable_direct_recycling(rxr->head_pool); 16152 16153 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 16154 bnxt_tx_queue_stop(bp, idx); 16155 16156 /* Disable NAPI now after freeing the rings because HWRM_RING_FREE 16157 * completion is handled in NAPI to guarantee no more DMA on that ring 16158 * after seeing the completion. 16159 */ 16160 napi_disable_locked(&bnapi->napi); 16161 16162 if (bp->tph_mode) { 16163 bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr); 16164 bnxt_clear_one_cp_ring(bp, rxr->rx_cpr); 16165 } 16166 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 16167 16168 memcpy(qmem, rxr, sizeof(*rxr)); 16169 bnxt_init_rx_ring_struct(bp, qmem); 16170 16171 return 0; 16172 } 16173 16174 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 16175 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 16176 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 16177 .ndo_queue_mem_free = bnxt_queue_mem_free, 16178 .ndo_queue_start = bnxt_queue_start, 16179 .ndo_queue_stop = bnxt_queue_stop, 16180 }; 16181 16182 static void bnxt_remove_one(struct pci_dev *pdev) 16183 { 16184 struct net_device *dev = pci_get_drvdata(pdev); 16185 struct bnxt *bp = netdev_priv(dev); 16186 16187 if (BNXT_PF(bp)) 16188 __bnxt_sriov_disable(bp); 16189 16190 bnxt_rdma_aux_device_del(bp); 16191 16192 unregister_netdev(dev); 16193 bnxt_ptp_clear(bp); 16194 16195 bnxt_rdma_aux_device_uninit(bp); 16196 16197 bnxt_free_l2_filters(bp, true); 16198 bnxt_free_ntp_fltrs(bp, true); 16199 WARN_ON(bp->num_rss_ctx); 16200 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16201 /* Flush any pending tasks */ 16202 cancel_work_sync(&bp->sp_task); 16203 cancel_delayed_work_sync(&bp->fw_reset_task); 16204 bp->sp_event = 0; 16205 16206 bnxt_dl_fw_reporters_destroy(bp); 16207 bnxt_dl_unregister(bp); 16208 bnxt_shutdown_tc(bp); 16209 16210 bnxt_clear_int_mode(bp); 16211 bnxt_hwrm_func_drv_unrgtr(bp); 16212 bnxt_free_hwrm_resources(bp); 16213 bnxt_hwmon_uninit(bp); 16214 bnxt_ethtool_free(bp); 16215 bnxt_dcb_free(bp); 16216 kfree(bp->ptp_cfg); 16217 bp->ptp_cfg = NULL; 16218 kfree(bp->fw_health); 16219 bp->fw_health = NULL; 16220 bnxt_cleanup_pci(bp); 16221 bnxt_free_ctx_mem(bp, true); 16222 bnxt_free_crash_dump_mem(bp); 16223 kfree(bp->rss_indir_tbl); 16224 bp->rss_indir_tbl = NULL; 16225 bnxt_free_port_stats(bp); 16226 free_netdev(dev); 16227 } 16228 16229 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 16230 { 16231 int rc = 0; 16232 struct bnxt_link_info *link_info = &bp->link_info; 16233 16234 bp->phy_flags = 0; 16235 rc = bnxt_hwrm_phy_qcaps(bp); 16236 if (rc) { 16237 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 16238 rc); 16239 return rc; 16240 } 16241 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 16242 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 16243 else 16244 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 16245 16246 bp->mac_flags = 0; 16247 bnxt_hwrm_mac_qcaps(bp); 16248 16249 if (!fw_dflt) 16250 return 0; 16251 16252 mutex_lock(&bp->link_lock); 16253 rc = bnxt_update_link(bp, false); 16254 if (rc) { 16255 mutex_unlock(&bp->link_lock); 16256 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 16257 rc); 16258 return rc; 16259 } 16260 16261 /* Older firmware does not have supported_auto_speeds, so assume 16262 * that all supported speeds can be autonegotiated. 16263 */ 16264 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 16265 link_info->support_auto_speeds = link_info->support_speeds; 16266 16267 bnxt_init_ethtool_link_settings(bp); 16268 mutex_unlock(&bp->link_lock); 16269 return 0; 16270 } 16271 16272 static int bnxt_get_max_irq(struct pci_dev *pdev) 16273 { 16274 u16 ctrl; 16275 16276 if (!pdev->msix_cap) 16277 return 1; 16278 16279 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 16280 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 16281 } 16282 16283 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16284 int *max_cp) 16285 { 16286 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 16287 int max_ring_grps = 0, max_irq; 16288 16289 *max_tx = hw_resc->max_tx_rings; 16290 *max_rx = hw_resc->max_rx_rings; 16291 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 16292 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 16293 bnxt_get_ulp_msix_num_in_use(bp), 16294 hw_resc->max_stat_ctxs - 16295 bnxt_get_ulp_stat_ctxs_in_use(bp)); 16296 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 16297 *max_cp = min_t(int, *max_cp, max_irq); 16298 max_ring_grps = hw_resc->max_hw_ring_grps; 16299 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 16300 *max_cp -= 1; 16301 *max_rx -= 2; 16302 } 16303 if (bp->flags & BNXT_FLAG_AGG_RINGS) 16304 *max_rx >>= 1; 16305 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 16306 int rc; 16307 16308 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 16309 if (rc) { 16310 *max_rx = 0; 16311 *max_tx = 0; 16312 } 16313 /* On P5 chips, max_cp output param should be available NQs */ 16314 *max_cp = max_irq; 16315 } 16316 *max_rx = min_t(int, *max_rx, max_ring_grps); 16317 } 16318 16319 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 16320 { 16321 int rx, tx, cp; 16322 16323 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 16324 *max_rx = rx; 16325 *max_tx = tx; 16326 if (!rx || !tx || !cp) 16327 return -ENOMEM; 16328 16329 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 16330 } 16331 16332 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 16333 bool shared) 16334 { 16335 int rc; 16336 16337 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16338 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 16339 /* Not enough rings, try disabling agg rings. */ 16340 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 16341 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 16342 if (rc) { 16343 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 16344 bp->flags |= BNXT_FLAG_AGG_RINGS; 16345 return rc; 16346 } 16347 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 16348 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16349 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 16350 bnxt_set_ring_params(bp); 16351 } 16352 16353 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 16354 int max_cp, max_stat, max_irq; 16355 16356 /* Reserve minimum resources for RoCE */ 16357 max_cp = bnxt_get_max_func_cp_rings(bp); 16358 max_stat = bnxt_get_max_func_stat_ctxs(bp); 16359 max_irq = bnxt_get_max_func_irqs(bp); 16360 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 16361 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 16362 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 16363 return 0; 16364 16365 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 16366 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 16367 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 16368 max_cp = min_t(int, max_cp, max_irq); 16369 max_cp = min_t(int, max_cp, max_stat); 16370 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 16371 if (rc) 16372 rc = 0; 16373 } 16374 return rc; 16375 } 16376 16377 /* In initial default shared ring setting, each shared ring must have a 16378 * RX/TX ring pair. 16379 */ 16380 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 16381 { 16382 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 16383 bp->rx_nr_rings = bp->cp_nr_rings; 16384 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 16385 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16386 } 16387 16388 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 16389 { 16390 int dflt_rings, max_rx_rings, max_tx_rings, rc; 16391 int avail_msix; 16392 16393 if (!bnxt_can_reserve_rings(bp)) 16394 return 0; 16395 16396 if (sh) 16397 bp->flags |= BNXT_FLAG_SHARED_RINGS; 16398 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 16399 /* Reduce default rings on multi-port cards so that total default 16400 * rings do not exceed CPU count. 16401 */ 16402 if (bp->port_count > 1) { 16403 int max_rings = 16404 max_t(int, num_online_cpus() / bp->port_count, 1); 16405 16406 dflt_rings = min_t(int, dflt_rings, max_rings); 16407 } 16408 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 16409 if (rc) 16410 return rc; 16411 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 16412 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 16413 if (sh) 16414 bnxt_trim_dflt_sh_rings(bp); 16415 else 16416 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 16417 bp->tx_nr_rings = bnxt_tx_nr_rings(bp); 16418 16419 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 16420 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 16421 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 16422 16423 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 16424 bnxt_set_dflt_ulp_stat_ctxs(bp); 16425 } 16426 16427 rc = __bnxt_reserve_rings(bp); 16428 if (rc && rc != -ENODEV) 16429 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 16430 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16431 if (sh) 16432 bnxt_trim_dflt_sh_rings(bp); 16433 16434 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 16435 if (bnxt_need_reserve_rings(bp)) { 16436 rc = __bnxt_reserve_rings(bp); 16437 if (rc && rc != -ENODEV) 16438 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 16439 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16440 } 16441 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 16442 bp->rx_nr_rings++; 16443 bp->cp_nr_rings++; 16444 } 16445 if (rc) { 16446 bp->tx_nr_rings = 0; 16447 bp->rx_nr_rings = 0; 16448 } 16449 return rc; 16450 } 16451 16452 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 16453 { 16454 int rc; 16455 16456 if (bp->tx_nr_rings) 16457 return 0; 16458 16459 bnxt_ulp_irq_stop(bp); 16460 bnxt_clear_int_mode(bp); 16461 rc = bnxt_set_dflt_rings(bp, true); 16462 if (rc) { 16463 if (BNXT_VF(bp) && rc == -ENODEV) 16464 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16465 else 16466 netdev_err(bp->dev, "Not enough rings available.\n"); 16467 goto init_dflt_ring_err; 16468 } 16469 rc = bnxt_init_int_mode(bp); 16470 if (rc) 16471 goto init_dflt_ring_err; 16472 16473 bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp); 16474 16475 bnxt_set_dflt_rfs(bp); 16476 16477 init_dflt_ring_err: 16478 bnxt_ulp_irq_restart(bp, rc); 16479 return rc; 16480 } 16481 16482 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 16483 { 16484 int rc; 16485 16486 netdev_ops_assert_locked(bp->dev); 16487 bnxt_hwrm_func_qcaps(bp); 16488 16489 if (netif_running(bp->dev)) 16490 __bnxt_close_nic(bp, true, false); 16491 16492 bnxt_ulp_irq_stop(bp); 16493 bnxt_clear_int_mode(bp); 16494 rc = bnxt_init_int_mode(bp); 16495 bnxt_ulp_irq_restart(bp, rc); 16496 16497 if (netif_running(bp->dev)) { 16498 if (rc) 16499 netif_close(bp->dev); 16500 else 16501 rc = bnxt_open_nic(bp, true, false); 16502 } 16503 16504 return rc; 16505 } 16506 16507 static int bnxt_init_mac_addr(struct bnxt *bp) 16508 { 16509 int rc = 0; 16510 16511 if (BNXT_PF(bp)) { 16512 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 16513 } else { 16514 #ifdef CONFIG_BNXT_SRIOV 16515 struct bnxt_vf_info *vf = &bp->vf; 16516 bool strict_approval = true; 16517 16518 if (is_valid_ether_addr(vf->mac_addr)) { 16519 /* overwrite netdev dev_addr with admin VF MAC */ 16520 eth_hw_addr_set(bp->dev, vf->mac_addr); 16521 /* Older PF driver or firmware may not approve this 16522 * correctly. 16523 */ 16524 strict_approval = false; 16525 } else { 16526 eth_hw_addr_random(bp->dev); 16527 } 16528 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 16529 #endif 16530 } 16531 return rc; 16532 } 16533 16534 static void bnxt_vpd_read_info(struct bnxt *bp) 16535 { 16536 struct pci_dev *pdev = bp->pdev; 16537 unsigned int vpd_size, kw_len; 16538 int pos, size; 16539 u8 *vpd_data; 16540 16541 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 16542 if (IS_ERR(vpd_data)) { 16543 pci_warn(pdev, "Unable to read VPD\n"); 16544 return; 16545 } 16546 16547 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16548 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 16549 if (pos < 0) 16550 goto read_sn; 16551 16552 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16553 memcpy(bp->board_partno, &vpd_data[pos], size); 16554 16555 read_sn: 16556 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 16557 PCI_VPD_RO_KEYWORD_SERIALNO, 16558 &kw_len); 16559 if (pos < 0) 16560 goto exit; 16561 16562 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 16563 memcpy(bp->board_serialno, &vpd_data[pos], size); 16564 exit: 16565 kfree(vpd_data); 16566 } 16567 16568 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 16569 { 16570 struct pci_dev *pdev = bp->pdev; 16571 u64 qword; 16572 16573 qword = pci_get_dsn(pdev); 16574 if (!qword) { 16575 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 16576 return -EOPNOTSUPP; 16577 } 16578 16579 put_unaligned_le64(qword, dsn); 16580 16581 bp->flags |= BNXT_FLAG_DSN_VALID; 16582 return 0; 16583 } 16584 16585 static int bnxt_map_db_bar(struct bnxt *bp) 16586 { 16587 if (!bp->db_size) 16588 return -ENODEV; 16589 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 16590 if (!bp->bar1) 16591 return -ENOMEM; 16592 return 0; 16593 } 16594 16595 void bnxt_print_device_info(struct bnxt *bp) 16596 { 16597 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 16598 board_info[bp->board_idx].name, 16599 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 16600 16601 pcie_print_link_status(bp->pdev); 16602 } 16603 16604 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 16605 { 16606 struct bnxt_hw_resc *hw_resc; 16607 struct net_device *dev; 16608 struct bnxt *bp; 16609 int rc, max_irqs; 16610 16611 if (pci_is_bridge(pdev)) 16612 return -ENODEV; 16613 16614 if (!pdev->msix_cap) { 16615 dev_err(&pdev->dev, "MSIX capability not found, aborting\n"); 16616 return -ENODEV; 16617 } 16618 16619 /* Clear any pending DMA transactions from crash kernel 16620 * while loading driver in capture kernel. 16621 */ 16622 if (is_kdump_kernel()) { 16623 pci_clear_master(pdev); 16624 pcie_flr(pdev); 16625 } 16626 16627 max_irqs = bnxt_get_max_irq(pdev); 16628 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 16629 max_irqs); 16630 if (!dev) 16631 return -ENOMEM; 16632 16633 bp = netdev_priv(dev); 16634 bp->board_idx = ent->driver_data; 16635 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 16636 bnxt_set_max_func_irqs(bp, max_irqs); 16637 16638 if (bnxt_vf_pciid(bp->board_idx)) 16639 bp->flags |= BNXT_FLAG_VF; 16640 16641 /* No devlink port registration in case of a VF */ 16642 if (BNXT_PF(bp)) 16643 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 16644 16645 rc = bnxt_init_board(pdev, dev); 16646 if (rc < 0) 16647 goto init_err_free; 16648 16649 dev->netdev_ops = &bnxt_netdev_ops; 16650 dev->stat_ops = &bnxt_stat_ops; 16651 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 16652 dev->ethtool_ops = &bnxt_ethtool_ops; 16653 pci_set_drvdata(pdev, dev); 16654 16655 rc = bnxt_alloc_hwrm_resources(bp); 16656 if (rc) 16657 goto init_err_pci_clean; 16658 16659 mutex_init(&bp->hwrm_cmd_lock); 16660 mutex_init(&bp->link_lock); 16661 16662 rc = bnxt_fw_init_one_p1(bp); 16663 if (rc) 16664 goto init_err_pci_clean; 16665 16666 if (BNXT_PF(bp)) 16667 bnxt_vpd_read_info(bp); 16668 16669 if (BNXT_CHIP_P5_PLUS(bp)) { 16670 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 16671 if (BNXT_CHIP_P7(bp)) 16672 bp->flags |= BNXT_FLAG_CHIP_P7; 16673 } 16674 16675 rc = bnxt_alloc_rss_indir_tbl(bp); 16676 if (rc) 16677 goto init_err_pci_clean; 16678 16679 rc = bnxt_fw_init_one_p2(bp); 16680 if (rc) 16681 goto init_err_pci_clean; 16682 16683 rc = bnxt_map_db_bar(bp); 16684 if (rc) { 16685 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 16686 rc); 16687 goto init_err_pci_clean; 16688 } 16689 16690 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16691 NETIF_F_TSO | NETIF_F_TSO6 | 16692 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16693 NETIF_F_GSO_IPXIP4 | 16694 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16695 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 16696 NETIF_F_RXCSUM | NETIF_F_GRO; 16697 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16698 dev->hw_features |= NETIF_F_GSO_UDP_L4; 16699 16700 if (BNXT_SUPPORTS_TPA(bp)) 16701 dev->hw_features |= NETIF_F_LRO; 16702 16703 dev->hw_enc_features = 16704 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 16705 NETIF_F_TSO | NETIF_F_TSO6 | 16706 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 16707 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 16708 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 16709 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 16710 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 16711 if (bp->flags & BNXT_FLAG_CHIP_P7) 16712 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 16713 else 16714 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 16715 16716 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 16717 NETIF_F_GSO_GRE_CSUM; 16718 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 16719 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 16720 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 16721 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 16722 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 16723 if (BNXT_SUPPORTS_TPA(bp)) 16724 dev->hw_features |= NETIF_F_GRO_HW; 16725 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 16726 if (dev->features & NETIF_F_GRO_HW) 16727 dev->features &= ~NETIF_F_LRO; 16728 dev->priv_flags |= IFF_UNICAST_FLT; 16729 16730 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 16731 if (bp->tso_max_segs) 16732 netif_set_tso_max_segs(dev, bp->tso_max_segs); 16733 16734 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 16735 NETDEV_XDP_ACT_RX_SG; 16736 16737 #ifdef CONFIG_BNXT_SRIOV 16738 init_waitqueue_head(&bp->sriov_cfg_wait); 16739 #endif 16740 if (BNXT_SUPPORTS_TPA(bp)) { 16741 bp->gro_func = bnxt_gro_func_5730x; 16742 if (BNXT_CHIP_P4(bp)) 16743 bp->gro_func = bnxt_gro_func_5731x; 16744 else if (BNXT_CHIP_P5_PLUS(bp)) 16745 bp->gro_func = bnxt_gro_func_5750x; 16746 } 16747 if (!BNXT_CHIP_P4_PLUS(bp)) 16748 bp->flags |= BNXT_FLAG_DOUBLE_DB; 16749 16750 rc = bnxt_init_mac_addr(bp); 16751 if (rc) { 16752 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 16753 rc = -EADDRNOTAVAIL; 16754 goto init_err_pci_clean; 16755 } 16756 16757 if (BNXT_PF(bp)) { 16758 /* Read the adapter's DSN to use as the eswitch switch_id */ 16759 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 16760 } 16761 16762 /* MTU range: 60 - FW defined max */ 16763 dev->min_mtu = ETH_ZLEN; 16764 dev->max_mtu = bp->max_mtu; 16765 16766 rc = bnxt_probe_phy(bp, true); 16767 if (rc) 16768 goto init_err_pci_clean; 16769 16770 hw_resc = &bp->hw_resc; 16771 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 16772 BNXT_L2_FLTR_MAX_FLTR; 16773 /* Older firmware may not report these filters properly */ 16774 if (bp->max_fltr < BNXT_MAX_FLTR) 16775 bp->max_fltr = BNXT_MAX_FLTR; 16776 bnxt_init_l2_fltr_tbl(bp); 16777 __bnxt_set_rx_skb_mode(bp, false); 16778 bnxt_set_tpa_flags(bp); 16779 bnxt_init_ring_params(bp); 16780 bnxt_set_ring_params(bp); 16781 bnxt_rdma_aux_device_init(bp); 16782 rc = bnxt_set_dflt_rings(bp, true); 16783 if (rc) { 16784 if (BNXT_VF(bp) && rc == -ENODEV) { 16785 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 16786 } else { 16787 netdev_err(bp->dev, "Not enough rings available.\n"); 16788 rc = -ENOMEM; 16789 } 16790 goto init_err_pci_clean; 16791 } 16792 16793 bnxt_fw_init_one_p3(bp); 16794 16795 bnxt_init_dflt_coal(bp); 16796 16797 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 16798 bp->flags |= BNXT_FLAG_STRIP_VLAN; 16799 16800 rc = bnxt_init_int_mode(bp); 16801 if (rc) 16802 goto init_err_pci_clean; 16803 16804 /* No TC has been set yet and rings may have been trimmed due to 16805 * limited MSIX, so we re-initialize the TX rings per TC. 16806 */ 16807 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 16808 16809 if (BNXT_PF(bp)) { 16810 if (!bnxt_pf_wq) { 16811 bnxt_pf_wq = 16812 create_singlethread_workqueue("bnxt_pf_wq"); 16813 if (!bnxt_pf_wq) { 16814 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 16815 rc = -ENOMEM; 16816 goto init_err_pci_clean; 16817 } 16818 } 16819 rc = bnxt_init_tc(bp); 16820 if (rc) 16821 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 16822 rc); 16823 } 16824 16825 bnxt_inv_fw_health_reg(bp); 16826 rc = bnxt_dl_register(bp); 16827 if (rc) 16828 goto init_err_dl; 16829 16830 INIT_LIST_HEAD(&bp->usr_fltr_list); 16831 16832 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 16833 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 16834 if (BNXT_SUPPORTS_QUEUE_API(bp)) 16835 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 16836 dev->request_ops_lock = true; 16837 dev->netmem_tx = true; 16838 16839 rc = register_netdev(dev); 16840 if (rc) 16841 goto init_err_cleanup; 16842 16843 bnxt_dl_fw_reporters_create(bp); 16844 16845 bnxt_rdma_aux_device_add(bp); 16846 16847 bnxt_print_device_info(bp); 16848 16849 pci_save_state(pdev); 16850 16851 return 0; 16852 init_err_cleanup: 16853 bnxt_rdma_aux_device_uninit(bp); 16854 bnxt_dl_unregister(bp); 16855 init_err_dl: 16856 bnxt_shutdown_tc(bp); 16857 bnxt_clear_int_mode(bp); 16858 16859 init_err_pci_clean: 16860 bnxt_hwrm_func_drv_unrgtr(bp); 16861 bnxt_free_hwrm_resources(bp); 16862 bnxt_hwmon_uninit(bp); 16863 bnxt_ethtool_free(bp); 16864 bnxt_ptp_clear(bp); 16865 kfree(bp->ptp_cfg); 16866 bp->ptp_cfg = NULL; 16867 kfree(bp->fw_health); 16868 bp->fw_health = NULL; 16869 bnxt_cleanup_pci(bp); 16870 bnxt_free_ctx_mem(bp, true); 16871 bnxt_free_crash_dump_mem(bp); 16872 kfree(bp->rss_indir_tbl); 16873 bp->rss_indir_tbl = NULL; 16874 16875 init_err_free: 16876 free_netdev(dev); 16877 return rc; 16878 } 16879 16880 static void bnxt_shutdown(struct pci_dev *pdev) 16881 { 16882 struct net_device *dev = pci_get_drvdata(pdev); 16883 struct bnxt *bp; 16884 16885 if (!dev) 16886 return; 16887 16888 rtnl_lock(); 16889 netdev_lock(dev); 16890 bp = netdev_priv(dev); 16891 if (!bp) 16892 goto shutdown_exit; 16893 16894 if (netif_running(dev)) 16895 netif_close(dev); 16896 16897 bnxt_ptp_clear(bp); 16898 bnxt_clear_int_mode(bp); 16899 pci_disable_device(pdev); 16900 16901 if (system_state == SYSTEM_POWER_OFF) { 16902 pci_wake_from_d3(pdev, bp->wol); 16903 pci_set_power_state(pdev, PCI_D3hot); 16904 } 16905 16906 shutdown_exit: 16907 netdev_unlock(dev); 16908 rtnl_unlock(); 16909 } 16910 16911 #ifdef CONFIG_PM_SLEEP 16912 static int bnxt_suspend(struct device *device) 16913 { 16914 struct net_device *dev = dev_get_drvdata(device); 16915 struct bnxt *bp = netdev_priv(dev); 16916 int rc = 0; 16917 16918 bnxt_ulp_stop(bp); 16919 16920 netdev_lock(dev); 16921 if (netif_running(dev)) { 16922 netif_device_detach(dev); 16923 rc = bnxt_close(dev); 16924 } 16925 bnxt_hwrm_func_drv_unrgtr(bp); 16926 bnxt_ptp_clear(bp); 16927 pci_disable_device(bp->pdev); 16928 bnxt_free_ctx_mem(bp, false); 16929 netdev_unlock(dev); 16930 return rc; 16931 } 16932 16933 static int bnxt_resume(struct device *device) 16934 { 16935 struct net_device *dev = dev_get_drvdata(device); 16936 struct bnxt *bp = netdev_priv(dev); 16937 int rc = 0; 16938 16939 netdev_lock(dev); 16940 rc = pci_enable_device(bp->pdev); 16941 if (rc) { 16942 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16943 rc); 16944 goto resume_exit; 16945 } 16946 pci_set_master(bp->pdev); 16947 if (bnxt_hwrm_ver_get(bp)) { 16948 rc = -ENODEV; 16949 goto resume_exit; 16950 } 16951 rc = bnxt_hwrm_func_reset(bp); 16952 if (rc) { 16953 rc = -EBUSY; 16954 goto resume_exit; 16955 } 16956 16957 rc = bnxt_hwrm_func_qcaps(bp); 16958 if (rc) 16959 goto resume_exit; 16960 16961 bnxt_clear_reservations(bp, true); 16962 16963 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16964 rc = -ENODEV; 16965 goto resume_exit; 16966 } 16967 if (bp->fw_crash_mem) 16968 bnxt_hwrm_crash_dump_mem_cfg(bp); 16969 16970 if (bnxt_ptp_init(bp)) { 16971 kfree(bp->ptp_cfg); 16972 bp->ptp_cfg = NULL; 16973 } 16974 bnxt_get_wol_settings(bp); 16975 if (netif_running(dev)) { 16976 rc = bnxt_open(dev); 16977 if (!rc) 16978 netif_device_attach(dev); 16979 } 16980 16981 resume_exit: 16982 netdev_unlock(bp->dev); 16983 bnxt_ulp_start(bp, rc); 16984 if (!rc) 16985 bnxt_reenable_sriov(bp); 16986 return rc; 16987 } 16988 16989 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16990 #define BNXT_PM_OPS (&bnxt_pm_ops) 16991 16992 #else 16993 16994 #define BNXT_PM_OPS NULL 16995 16996 #endif /* CONFIG_PM_SLEEP */ 16997 16998 /** 16999 * bnxt_io_error_detected - called when PCI error is detected 17000 * @pdev: Pointer to PCI device 17001 * @state: The current pci connection state 17002 * 17003 * This function is called after a PCI bus error affecting 17004 * this device has been detected. 17005 */ 17006 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 17007 pci_channel_state_t state) 17008 { 17009 struct net_device *netdev = pci_get_drvdata(pdev); 17010 struct bnxt *bp = netdev_priv(netdev); 17011 bool abort = false; 17012 17013 netdev_info(netdev, "PCI I/O error detected\n"); 17014 17015 bnxt_ulp_stop(bp); 17016 17017 netdev_lock(netdev); 17018 netif_device_detach(netdev); 17019 17020 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 17021 netdev_err(bp->dev, "Firmware reset already in progress\n"); 17022 abort = true; 17023 } 17024 17025 if (abort || state == pci_channel_io_perm_failure) { 17026 netdev_unlock(netdev); 17027 return PCI_ERS_RESULT_DISCONNECT; 17028 } 17029 17030 /* Link is not reliable anymore if state is pci_channel_io_frozen 17031 * so we disable bus master to prevent any potential bad DMAs before 17032 * freeing kernel memory. 17033 */ 17034 if (state == pci_channel_io_frozen) { 17035 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 17036 bnxt_fw_fatal_close(bp); 17037 } 17038 17039 if (netif_running(netdev)) 17040 __bnxt_close_nic(bp, true, true); 17041 17042 if (pci_is_enabled(pdev)) 17043 pci_disable_device(pdev); 17044 bnxt_free_ctx_mem(bp, false); 17045 netdev_unlock(netdev); 17046 17047 /* Request a slot reset. */ 17048 return PCI_ERS_RESULT_NEED_RESET; 17049 } 17050 17051 /** 17052 * bnxt_io_slot_reset - called after the pci bus has been reset. 17053 * @pdev: Pointer to PCI device 17054 * 17055 * Restart the card from scratch, as if from a cold-boot. 17056 * At this point, the card has experienced a hard reset, 17057 * followed by fixups by BIOS, and has its config space 17058 * set up identically to what it was at cold boot. 17059 */ 17060 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 17061 { 17062 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 17063 struct net_device *netdev = pci_get_drvdata(pdev); 17064 struct bnxt *bp = netdev_priv(netdev); 17065 int retry = 0; 17066 int err = 0; 17067 int off; 17068 17069 netdev_info(bp->dev, "PCI Slot Reset\n"); 17070 17071 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 17072 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 17073 msleep(900); 17074 17075 netdev_lock(netdev); 17076 17077 if (pci_enable_device(pdev)) { 17078 dev_err(&pdev->dev, 17079 "Cannot re-enable PCI device after reset.\n"); 17080 } else { 17081 pci_set_master(pdev); 17082 /* Upon fatal error, our device internal logic that latches to 17083 * BAR value is getting reset and will restore only upon 17084 * rewriting the BARs. 17085 * 17086 * As pci_restore_state() does not re-write the BARs if the 17087 * value is same as saved value earlier, driver needs to 17088 * write the BARs to 0 to force restore, in case of fatal error. 17089 */ 17090 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 17091 &bp->state)) { 17092 for (off = PCI_BASE_ADDRESS_0; 17093 off <= PCI_BASE_ADDRESS_5; off += 4) 17094 pci_write_config_dword(bp->pdev, off, 0); 17095 } 17096 pci_restore_state(pdev); 17097 pci_save_state(pdev); 17098 17099 bnxt_inv_fw_health_reg(bp); 17100 bnxt_try_map_fw_health_reg(bp); 17101 17102 /* In some PCIe AER scenarios, firmware may take up to 17103 * 10 seconds to become ready in the worst case. 17104 */ 17105 do { 17106 err = bnxt_try_recover_fw(bp); 17107 if (!err) 17108 break; 17109 retry++; 17110 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 17111 17112 if (err) { 17113 dev_err(&pdev->dev, "Firmware not ready\n"); 17114 goto reset_exit; 17115 } 17116 17117 err = bnxt_hwrm_func_reset(bp); 17118 if (!err) 17119 result = PCI_ERS_RESULT_RECOVERED; 17120 17121 /* IRQ will be initialized later in bnxt_io_resume */ 17122 bnxt_ulp_irq_stop(bp); 17123 bnxt_clear_int_mode(bp); 17124 } 17125 17126 reset_exit: 17127 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 17128 bnxt_clear_reservations(bp, true); 17129 netdev_unlock(netdev); 17130 17131 return result; 17132 } 17133 17134 /** 17135 * bnxt_io_resume - called when traffic can start flowing again. 17136 * @pdev: Pointer to PCI device 17137 * 17138 * This callback is called when the error recovery driver tells 17139 * us that its OK to resume normal operation. 17140 */ 17141 static void bnxt_io_resume(struct pci_dev *pdev) 17142 { 17143 struct net_device *netdev = pci_get_drvdata(pdev); 17144 struct bnxt *bp = netdev_priv(netdev); 17145 int err; 17146 17147 netdev_info(bp->dev, "PCI Slot Resume\n"); 17148 netdev_lock(netdev); 17149 17150 err = bnxt_hwrm_func_qcaps(bp); 17151 if (!err) { 17152 if (netif_running(netdev)) { 17153 err = bnxt_open(netdev); 17154 } else { 17155 err = bnxt_reserve_rings(bp, true); 17156 if (!err) 17157 err = bnxt_init_int_mode(bp); 17158 } 17159 } 17160 17161 if (!err) 17162 netif_device_attach(netdev); 17163 17164 netdev_unlock(netdev); 17165 bnxt_ulp_start(bp, err); 17166 if (!err) 17167 bnxt_reenable_sriov(bp); 17168 } 17169 17170 static const struct pci_error_handlers bnxt_err_handler = { 17171 .error_detected = bnxt_io_error_detected, 17172 .slot_reset = bnxt_io_slot_reset, 17173 .resume = bnxt_io_resume 17174 }; 17175 17176 static struct pci_driver bnxt_pci_driver = { 17177 .name = DRV_MODULE_NAME, 17178 .id_table = bnxt_pci_tbl, 17179 .probe = bnxt_init_one, 17180 .remove = bnxt_remove_one, 17181 .shutdown = bnxt_shutdown, 17182 .driver.pm = BNXT_PM_OPS, 17183 .err_handler = &bnxt_err_handler, 17184 #if defined(CONFIG_BNXT_SRIOV) 17185 .sriov_configure = bnxt_sriov_configure, 17186 #endif 17187 }; 17188 17189 static int __init bnxt_init(void) 17190 { 17191 int err; 17192 17193 bnxt_debug_init(); 17194 err = pci_register_driver(&bnxt_pci_driver); 17195 if (err) { 17196 bnxt_debug_exit(); 17197 return err; 17198 } 17199 17200 return 0; 17201 } 17202 17203 static void __exit bnxt_exit(void) 17204 { 17205 pci_unregister_driver(&bnxt_pci_driver); 17206 if (bnxt_pf_wq) 17207 destroy_workqueue(bnxt_pf_wq); 17208 bnxt_debug_exit(); 17209 } 17210 17211 module_init(bnxt_init); 17212 module_exit(bnxt_exit); 17213