1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2014-2016 Broadcom Corporation 4 * Copyright (c) 2016-2019 Broadcom Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation. 9 */ 10 11 #include <linux/module.h> 12 13 #include <linux/stringify.h> 14 #include <linux/kernel.h> 15 #include <linux/timer.h> 16 #include <linux/errno.h> 17 #include <linux/ioport.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/interrupt.h> 21 #include <linux/pci.h> 22 #include <linux/netdevice.h> 23 #include <linux/etherdevice.h> 24 #include <linux/skbuff.h> 25 #include <linux/dma-mapping.h> 26 #include <linux/bitops.h> 27 #include <linux/io.h> 28 #include <linux/irq.h> 29 #include <linux/delay.h> 30 #include <asm/byteorder.h> 31 #include <asm/page.h> 32 #include <linux/time.h> 33 #include <linux/mii.h> 34 #include <linux/mdio.h> 35 #include <linux/if.h> 36 #include <linux/if_vlan.h> 37 #include <linux/if_bridge.h> 38 #include <linux/rtc.h> 39 #include <linux/bpf.h> 40 #include <net/gro.h> 41 #include <net/ip.h> 42 #include <net/tcp.h> 43 #include <net/udp.h> 44 #include <net/checksum.h> 45 #include <net/ip6_checksum.h> 46 #include <net/udp_tunnel.h> 47 #include <linux/workqueue.h> 48 #include <linux/prefetch.h> 49 #include <linux/cache.h> 50 #include <linux/log2.h> 51 #include <linux/bitmap.h> 52 #include <linux/cpu_rmap.h> 53 #include <linux/cpumask.h> 54 #include <net/pkt_cls.h> 55 #include <net/page_pool/helpers.h> 56 #include <linux/align.h> 57 #include <net/netdev_queues.h> 58 59 #include "bnxt_hsi.h" 60 #include "bnxt.h" 61 #include "bnxt_hwrm.h" 62 #include "bnxt_ulp.h" 63 #include "bnxt_sriov.h" 64 #include "bnxt_ethtool.h" 65 #include "bnxt_dcb.h" 66 #include "bnxt_xdp.h" 67 #include "bnxt_ptp.h" 68 #include "bnxt_vfr.h" 69 #include "bnxt_tc.h" 70 #include "bnxt_devlink.h" 71 #include "bnxt_debugfs.h" 72 #include "bnxt_hwmon.h" 73 74 #define BNXT_TX_TIMEOUT (5 * HZ) 75 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \ 76 NETIF_MSG_TX_ERR) 77 78 MODULE_LICENSE("GPL"); 79 MODULE_DESCRIPTION("Broadcom NetXtreme network driver"); 80 81 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN) 82 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD 83 #define BNXT_RX_COPY_THRESH 256 84 85 #define BNXT_TX_PUSH_THRESH 164 86 87 /* indexed by enum board_idx */ 88 static const struct { 89 char *name; 90 } board_info[] = { 91 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" }, 92 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" }, 93 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 94 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" }, 95 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" }, 96 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" }, 97 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" }, 98 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" }, 99 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" }, 100 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" }, 101 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" }, 102 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" }, 103 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" }, 104 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" }, 105 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" }, 106 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" }, 107 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" }, 108 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" }, 109 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" }, 110 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" }, 111 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" }, 112 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" }, 113 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" }, 114 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" }, 115 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" }, 116 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" }, 117 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" }, 118 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 119 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" }, 120 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 121 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 122 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" }, 123 [BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 124 [BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" }, 125 [BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" }, 126 [BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" }, 127 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" }, 128 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" }, 129 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" }, 130 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" }, 131 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 132 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" }, 133 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" }, 134 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" }, 135 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" }, 136 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" }, 137 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" }, 138 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" }, 139 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" }, 140 [NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" }, 141 }; 142 143 static const struct pci_device_id bnxt_pci_tbl[] = { 144 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR }, 145 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR }, 146 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 }, 147 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR }, 148 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 }, 149 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 }, 150 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 }, 151 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR }, 152 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 }, 153 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 }, 154 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 }, 155 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 }, 156 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 }, 157 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 }, 158 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR }, 159 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 }, 160 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 }, 161 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 }, 162 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 }, 163 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 }, 164 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR }, 165 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 }, 166 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP }, 167 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP }, 168 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR }, 169 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR }, 170 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP }, 171 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR }, 172 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR }, 173 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR }, 174 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR }, 175 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR }, 176 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR }, 177 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 }, 178 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 }, 179 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 }, 180 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 }, 181 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 }, 182 { PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 }, 183 { PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 }, 184 { PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 }, 185 { PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 }, 186 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR }, 187 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR }, 188 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR }, 189 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR }, 190 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR }, 191 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR }, 192 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 }, 193 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 }, 194 #ifdef CONFIG_BNXT_SRIOV 195 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF }, 196 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV }, 197 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV }, 198 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF }, 199 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV }, 200 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF }, 201 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV }, 202 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV }, 203 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV }, 204 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV }, 205 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF }, 206 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF }, 207 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF }, 208 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF }, 209 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF }, 210 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV }, 211 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF }, 212 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF }, 213 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV }, 214 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV }, 215 { PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF }, 216 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF }, 217 #endif 218 { 0 } 219 }; 220 221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl); 222 223 static const u16 bnxt_vf_req_snif[] = { 224 HWRM_FUNC_CFG, 225 HWRM_FUNC_VF_CFG, 226 HWRM_PORT_PHY_QCFG, 227 HWRM_CFA_L2_FILTER_ALLOC, 228 }; 229 230 static const u16 bnxt_async_events_arr[] = { 231 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE, 232 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE, 233 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD, 234 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED, 235 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE, 236 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE, 237 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE, 238 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY, 239 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY, 240 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION, 241 ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE, 242 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG, 243 ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST, 244 ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP, 245 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT, 246 ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE, 247 }; 248 249 static struct workqueue_struct *bnxt_pf_wq; 250 251 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ 252 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} 253 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}} 254 255 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = { 256 .ports = { 257 .src = 0, 258 .dst = 0, 259 }, 260 .addrs = { 261 .v6addrs = { 262 .src = BNXT_IPV6_MASK_NONE, 263 .dst = BNXT_IPV6_MASK_NONE, 264 }, 265 }, 266 }; 267 268 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = { 269 .ports = { 270 .src = cpu_to_be16(0xffff), 271 .dst = cpu_to_be16(0xffff), 272 }, 273 .addrs = { 274 .v6addrs = { 275 .src = BNXT_IPV6_MASK_ALL, 276 .dst = BNXT_IPV6_MASK_ALL, 277 }, 278 }, 279 }; 280 281 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = { 282 .ports = { 283 .src = cpu_to_be16(0xffff), 284 .dst = cpu_to_be16(0xffff), 285 }, 286 .addrs = { 287 .v4addrs = { 288 .src = cpu_to_be32(0xffffffff), 289 .dst = cpu_to_be32(0xffffffff), 290 }, 291 }, 292 }; 293 294 static bool bnxt_vf_pciid(enum board_idx idx) 295 { 296 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF || 297 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV || 298 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF || 299 idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF); 300 } 301 302 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID) 303 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS) 304 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS) 305 306 #define BNXT_CP_DB_IRQ_DIS(db) \ 307 writel(DB_CP_IRQ_DIS_FLAGS, db) 308 309 #define BNXT_DB_CQ(db, idx) \ 310 writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 311 312 #define BNXT_DB_NQ_P5(db, idx) \ 313 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\ 314 (db)->doorbell) 315 316 #define BNXT_DB_NQ_P7(db, idx) \ 317 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK | \ 318 DB_RING_IDX(db, idx), (db)->doorbell) 319 320 #define BNXT_DB_CQ_ARM(db, idx) \ 321 writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell) 322 323 #define BNXT_DB_NQ_ARM_P5(db, idx) \ 324 bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM | \ 325 DB_RING_IDX(db, idx), (db)->doorbell) 326 327 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 328 { 329 if (bp->flags & BNXT_FLAG_CHIP_P7) 330 BNXT_DB_NQ_P7(db, idx); 331 else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 332 BNXT_DB_NQ_P5(db, idx); 333 else 334 BNXT_DB_CQ(db, idx); 335 } 336 337 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 338 { 339 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 340 BNXT_DB_NQ_ARM_P5(db, idx); 341 else 342 BNXT_DB_CQ_ARM(db, idx); 343 } 344 345 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx) 346 { 347 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 348 bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL | 349 DB_RING_IDX(db, idx), db->doorbell); 350 else 351 BNXT_DB_CQ(db, idx); 352 } 353 354 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay) 355 { 356 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))) 357 return; 358 359 if (BNXT_PF(bp)) 360 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay); 361 else 362 schedule_delayed_work(&bp->fw_reset_task, delay); 363 } 364 365 static void __bnxt_queue_sp_work(struct bnxt *bp) 366 { 367 if (BNXT_PF(bp)) 368 queue_work(bnxt_pf_wq, &bp->sp_task); 369 else 370 schedule_work(&bp->sp_task); 371 } 372 373 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event) 374 { 375 set_bit(event, &bp->sp_event); 376 __bnxt_queue_sp_work(bp); 377 } 378 379 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 380 { 381 if (!rxr->bnapi->in_reset) { 382 rxr->bnapi->in_reset = true; 383 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 384 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event); 385 else 386 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event); 387 __bnxt_queue_sp_work(bp); 388 } 389 rxr->rx_next_cons = 0xffff; 390 } 391 392 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 393 u16 curr) 394 { 395 struct bnxt_napi *bnapi = txr->bnapi; 396 397 if (bnapi->tx_fault) 398 return; 399 400 netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)", 401 txr->txq_index, txr->tx_hw_cons, 402 txr->tx_cons, txr->tx_prod, curr); 403 WARN_ON_ONCE(1); 404 bnapi->tx_fault = 1; 405 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 406 } 407 408 const u16 bnxt_lhint_arr[] = { 409 TX_BD_FLAGS_LHINT_512_AND_SMALLER, 410 TX_BD_FLAGS_LHINT_512_TO_1023, 411 TX_BD_FLAGS_LHINT_1024_TO_2047, 412 TX_BD_FLAGS_LHINT_1024_TO_2047, 413 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 414 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 415 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 416 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 417 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 418 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 419 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 420 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 421 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 422 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 423 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 424 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 425 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 426 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 427 TX_BD_FLAGS_LHINT_2048_AND_LARGER, 428 }; 429 430 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb) 431 { 432 struct metadata_dst *md_dst = skb_metadata_dst(skb); 433 434 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX) 435 return 0; 436 437 return md_dst->u.port_info.port_id; 438 } 439 440 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 441 u16 prod) 442 { 443 /* Sync BD data before updating doorbell */ 444 wmb(); 445 bnxt_db_write(bp, &txr->tx_db, prod); 446 txr->kick_pending = 0; 447 } 448 449 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev) 450 { 451 struct bnxt *bp = netdev_priv(dev); 452 struct tx_bd *txbd, *txbd0; 453 struct tx_bd_ext *txbd1; 454 struct netdev_queue *txq; 455 int i; 456 dma_addr_t mapping; 457 unsigned int length, pad = 0; 458 u32 len, free_size, vlan_tag_flags, cfa_action, flags; 459 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 460 struct pci_dev *pdev = bp->pdev; 461 u16 prod, last_frag, txts_prod; 462 struct bnxt_tx_ring_info *txr; 463 struct bnxt_sw_tx_bd *tx_buf; 464 __le32 lflags = 0; 465 466 i = skb_get_queue_mapping(skb); 467 if (unlikely(i >= bp->tx_nr_rings)) { 468 dev_kfree_skb_any(skb); 469 dev_core_stats_tx_dropped_inc(dev); 470 return NETDEV_TX_OK; 471 } 472 473 txq = netdev_get_tx_queue(dev, i); 474 txr = &bp->tx_ring[bp->tx_ring_map[i]]; 475 prod = txr->tx_prod; 476 477 free_size = bnxt_tx_avail(bp, txr); 478 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) { 479 /* We must have raced with NAPI cleanup */ 480 if (net_ratelimit() && txr->kick_pending) 481 netif_warn(bp, tx_err, dev, 482 "bnxt: ring busy w/ flush pending!\n"); 483 if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 484 bp->tx_wake_thresh)) 485 return NETDEV_TX_BUSY; 486 } 487 488 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 489 goto tx_free; 490 491 length = skb->len; 492 len = skb_headlen(skb); 493 last_frag = skb_shinfo(skb)->nr_frags; 494 495 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 496 497 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 498 tx_buf->skb = skb; 499 tx_buf->nr_frags = last_frag; 500 501 vlan_tag_flags = 0; 502 cfa_action = bnxt_xmit_get_cfa_action(skb); 503 if (skb_vlan_tag_present(skb)) { 504 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN | 505 skb_vlan_tag_get(skb); 506 /* Currently supports 8021Q, 8021AD vlan offloads 507 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated 508 */ 509 if (skb->vlan_proto == htons(ETH_P_8021Q)) 510 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT; 511 } 512 513 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp && 514 ptp->tx_tstamp_en) { 515 if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) { 516 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 517 tx_buf->is_ts_pkt = 1; 518 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 519 } else if (!skb_is_gso(skb)) { 520 u16 seq_id, hdr_off; 521 522 if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) && 523 !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) { 524 if (vlan_tag_flags) 525 hdr_off += VLAN_HLEN; 526 lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP); 527 tx_buf->is_ts_pkt = 1; 528 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 529 530 ptp->txts_req[txts_prod].tx_seqid = seq_id; 531 ptp->txts_req[txts_prod].tx_hdr_off = hdr_off; 532 tx_buf->txts_prod = txts_prod; 533 } 534 } 535 } 536 if (unlikely(skb->no_fcs)) 537 lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC); 538 539 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh && 540 !lflags) { 541 struct tx_push_buffer *tx_push_buf = txr->tx_push; 542 struct tx_push_bd *tx_push = &tx_push_buf->push_bd; 543 struct tx_bd_ext *tx_push1 = &tx_push->txbd2; 544 void __iomem *db = txr->tx_db.doorbell; 545 void *pdata = tx_push_buf->data; 546 u64 *end; 547 int j, push_len; 548 549 /* Set COAL_NOW to be ready quickly for the next push */ 550 tx_push->tx_bd_len_flags_type = 551 cpu_to_le32((length << TX_BD_LEN_SHIFT) | 552 TX_BD_TYPE_LONG_TX_BD | 553 TX_BD_FLAGS_LHINT_512_AND_SMALLER | 554 TX_BD_FLAGS_COAL_NOW | 555 TX_BD_FLAGS_PACKET_END | 556 (2 << TX_BD_FLAGS_BD_CNT_SHIFT)); 557 558 if (skb->ip_summed == CHECKSUM_PARTIAL) 559 tx_push1->tx_bd_hsize_lflags = 560 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 561 else 562 tx_push1->tx_bd_hsize_lflags = 0; 563 564 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 565 tx_push1->tx_bd_cfa_action = 566 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 567 568 end = pdata + length; 569 end = PTR_ALIGN(end, 8) - 1; 570 *end = 0; 571 572 skb_copy_from_linear_data(skb, pdata, len); 573 pdata += len; 574 for (j = 0; j < last_frag; j++) { 575 skb_frag_t *frag = &skb_shinfo(skb)->frags[j]; 576 void *fptr; 577 578 fptr = skb_frag_address_safe(frag); 579 if (!fptr) 580 goto normal_tx; 581 582 memcpy(pdata, fptr, skb_frag_size(frag)); 583 pdata += skb_frag_size(frag); 584 } 585 586 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type; 587 txbd->tx_bd_haddr = txr->data_mapping; 588 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2); 589 prod = NEXT_TX(prod); 590 tx_push->tx_bd_opaque = txbd->tx_bd_opaque; 591 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 592 memcpy(txbd, tx_push1, sizeof(*txbd)); 593 prod = NEXT_TX(prod); 594 tx_push->doorbell = 595 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | 596 DB_RING_IDX(&txr->tx_db, prod)); 597 WRITE_ONCE(txr->tx_prod, prod); 598 599 tx_buf->is_push = 1; 600 netdev_tx_sent_queue(txq, skb->len); 601 wmb(); /* Sync is_push and byte queue before pushing data */ 602 603 push_len = (length + sizeof(*tx_push) + 7) / 8; 604 if (push_len > 16) { 605 __iowrite64_copy(db, tx_push_buf, 16); 606 __iowrite32_copy(db + 4, tx_push_buf + 1, 607 (push_len - 16) << 1); 608 } else { 609 __iowrite64_copy(db, tx_push_buf, push_len); 610 } 611 612 goto tx_done; 613 } 614 615 normal_tx: 616 if (length < BNXT_MIN_PKT_SIZE) { 617 pad = BNXT_MIN_PKT_SIZE - length; 618 if (skb_pad(skb, pad)) 619 /* SKB already freed. */ 620 goto tx_kick_pending; 621 length = BNXT_MIN_PKT_SIZE; 622 } 623 624 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE); 625 626 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 627 goto tx_free; 628 629 dma_unmap_addr_set(tx_buf, mapping, mapping); 630 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD | 631 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT); 632 633 txbd->tx_bd_haddr = cpu_to_le64(mapping); 634 txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag); 635 636 prod = NEXT_TX(prod); 637 txbd1 = (struct tx_bd_ext *) 638 &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 639 640 txbd1->tx_bd_hsize_lflags = lflags; 641 if (skb_is_gso(skb)) { 642 bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4); 643 u32 hdr_len; 644 645 if (skb->encapsulation) { 646 if (udp_gso) 647 hdr_len = skb_inner_transport_offset(skb) + 648 sizeof(struct udphdr); 649 else 650 hdr_len = skb_inner_tcp_all_headers(skb); 651 } else if (udp_gso) { 652 hdr_len = skb_transport_offset(skb) + 653 sizeof(struct udphdr); 654 } else { 655 hdr_len = skb_tcp_all_headers(skb); 656 } 657 658 txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO | 659 TX_BD_FLAGS_T_IPID | 660 (hdr_len << (TX_BD_HSIZE_SHIFT - 1))); 661 length = skb_shinfo(skb)->gso_size; 662 txbd1->tx_bd_mss = cpu_to_le32(length); 663 length += hdr_len; 664 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 665 txbd1->tx_bd_hsize_lflags |= 666 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM); 667 txbd1->tx_bd_mss = 0; 668 } 669 670 length >>= 9; 671 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) { 672 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n", 673 skb->len); 674 i = 0; 675 goto tx_dma_error; 676 } 677 flags |= bnxt_lhint_arr[length]; 678 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 679 680 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags); 681 txbd1->tx_bd_cfa_action = 682 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT); 683 txbd0 = txbd; 684 for (i = 0; i < last_frag; i++) { 685 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 686 687 prod = NEXT_TX(prod); 688 txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)]; 689 690 len = skb_frag_size(frag); 691 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len, 692 DMA_TO_DEVICE); 693 694 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) 695 goto tx_dma_error; 696 697 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 698 dma_unmap_addr_set(tx_buf, mapping, mapping); 699 700 txbd->tx_bd_haddr = cpu_to_le64(mapping); 701 702 flags = len << TX_BD_LEN_SHIFT; 703 txbd->tx_bd_len_flags_type = cpu_to_le32(flags); 704 } 705 706 flags &= ~TX_BD_LEN; 707 txbd->tx_bd_len_flags_type = 708 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags | 709 TX_BD_FLAGS_PACKET_END); 710 711 netdev_tx_sent_queue(txq, skb->len); 712 713 skb_tx_timestamp(skb); 714 715 prod = NEXT_TX(prod); 716 WRITE_ONCE(txr->tx_prod, prod); 717 718 if (!netdev_xmit_more() || netif_xmit_stopped(txq)) { 719 bnxt_txr_db_kick(bp, txr, prod); 720 } else { 721 if (free_size >= bp->tx_wake_thresh) 722 txbd0->tx_bd_len_flags_type |= 723 cpu_to_le32(TX_BD_FLAGS_NO_CMPL); 724 txr->kick_pending = 1; 725 } 726 727 tx_done: 728 729 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) { 730 if (netdev_xmit_more() && !tx_buf->is_push) { 731 txbd0->tx_bd_len_flags_type &= 732 cpu_to_le32(~TX_BD_FLAGS_NO_CMPL); 733 bnxt_txr_db_kick(bp, txr, prod); 734 } 735 736 netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr), 737 bp->tx_wake_thresh); 738 } 739 return NETDEV_TX_OK; 740 741 tx_dma_error: 742 last_frag = i; 743 744 /* start back at beginning and unmap skb */ 745 prod = txr->tx_prod; 746 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 747 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 748 skb_headlen(skb), DMA_TO_DEVICE); 749 prod = NEXT_TX(prod); 750 751 /* unmap remaining mapped pages */ 752 for (i = 0; i < last_frag; i++) { 753 prod = NEXT_TX(prod); 754 tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)]; 755 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 756 skb_frag_size(&skb_shinfo(skb)->frags[i]), 757 DMA_TO_DEVICE); 758 } 759 760 tx_free: 761 dev_kfree_skb_any(skb); 762 tx_kick_pending: 763 if (BNXT_TX_PTP_IS_SET(lflags)) { 764 txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0; 765 atomic64_inc(&bp->ptp_cfg->stats.ts_err); 766 if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 767 /* set SKB to err so PTP worker will clean up */ 768 ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO); 769 } 770 if (txr->kick_pending) 771 bnxt_txr_db_kick(bp, txr, txr->tx_prod); 772 txr->tx_buf_ring[txr->tx_prod].skb = NULL; 773 dev_core_stats_tx_dropped_inc(dev); 774 return NETDEV_TX_OK; 775 } 776 777 /* Returns true if some remaining TX packets not processed. */ 778 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr, 779 int budget) 780 { 781 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index); 782 struct pci_dev *pdev = bp->pdev; 783 u16 hw_cons = txr->tx_hw_cons; 784 unsigned int tx_bytes = 0; 785 u16 cons = txr->tx_cons; 786 int tx_pkts = 0; 787 bool rc = false; 788 789 while (RING_TX(bp, cons) != hw_cons) { 790 struct bnxt_sw_tx_bd *tx_buf; 791 struct sk_buff *skb; 792 bool is_ts_pkt; 793 int j, last; 794 795 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 796 skb = tx_buf->skb; 797 798 if (unlikely(!skb)) { 799 bnxt_sched_reset_txr(bp, txr, cons); 800 return rc; 801 } 802 803 is_ts_pkt = tx_buf->is_ts_pkt; 804 if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) { 805 rc = true; 806 break; 807 } 808 809 cons = NEXT_TX(cons); 810 tx_pkts++; 811 tx_bytes += skb->len; 812 tx_buf->skb = NULL; 813 tx_buf->is_ts_pkt = 0; 814 815 if (tx_buf->is_push) { 816 tx_buf->is_push = 0; 817 goto next_tx_int; 818 } 819 820 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping), 821 skb_headlen(skb), DMA_TO_DEVICE); 822 last = tx_buf->nr_frags; 823 824 for (j = 0; j < last; j++) { 825 cons = NEXT_TX(cons); 826 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)]; 827 dma_unmap_page( 828 &pdev->dev, 829 dma_unmap_addr(tx_buf, mapping), 830 skb_frag_size(&skb_shinfo(skb)->frags[j]), 831 DMA_TO_DEVICE); 832 } 833 if (unlikely(is_ts_pkt)) { 834 if (BNXT_CHIP_P5(bp)) { 835 /* PTP worker takes ownership of the skb */ 836 bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod); 837 skb = NULL; 838 } 839 } 840 841 next_tx_int: 842 cons = NEXT_TX(cons); 843 844 dev_consume_skb_any(skb); 845 } 846 847 WRITE_ONCE(txr->tx_cons, cons); 848 849 __netif_txq_completed_wake(txq, tx_pkts, tx_bytes, 850 bnxt_tx_avail(bp, txr), bp->tx_wake_thresh, 851 READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING); 852 853 return rc; 854 } 855 856 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 857 { 858 struct bnxt_tx_ring_info *txr; 859 bool more = false; 860 int i; 861 862 bnxt_for_each_napi_tx(i, bnapi, txr) { 863 if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons)) 864 more |= __bnxt_tx_int(bp, txr, budget); 865 } 866 if (!more) 867 bnapi->events &= ~BNXT_TX_CMP_EVENT; 868 } 869 870 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, 871 struct bnxt_rx_ring_info *rxr, 872 unsigned int *offset, 873 gfp_t gfp) 874 { 875 struct page *page; 876 877 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) { 878 page = page_pool_dev_alloc_frag(rxr->page_pool, offset, 879 BNXT_RX_PAGE_SIZE); 880 } else { 881 page = page_pool_dev_alloc_pages(rxr->page_pool); 882 *offset = 0; 883 } 884 if (!page) 885 return NULL; 886 887 *mapping = page_pool_get_dma_addr(page) + *offset; 888 return page; 889 } 890 891 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping, 892 gfp_t gfp) 893 { 894 u8 *data; 895 struct pci_dev *pdev = bp->pdev; 896 897 if (gfp == GFP_ATOMIC) 898 data = napi_alloc_frag(bp->rx_buf_size); 899 else 900 data = netdev_alloc_frag(bp->rx_buf_size); 901 if (!data) 902 return NULL; 903 904 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset, 905 bp->rx_buf_use_size, bp->rx_dir, 906 DMA_ATTR_WEAK_ORDERING); 907 908 if (dma_mapping_error(&pdev->dev, *mapping)) { 909 skb_free_frag(data); 910 data = NULL; 911 } 912 return data; 913 } 914 915 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 916 u16 prod, gfp_t gfp) 917 { 918 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 919 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 920 dma_addr_t mapping; 921 922 if (BNXT_RX_PAGE_MODE(bp)) { 923 unsigned int offset; 924 struct page *page = 925 __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 926 927 if (!page) 928 return -ENOMEM; 929 930 mapping += bp->rx_dma_offset; 931 rx_buf->data = page; 932 rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset; 933 } else { 934 u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, gfp); 935 936 if (!data) 937 return -ENOMEM; 938 939 rx_buf->data = data; 940 rx_buf->data_ptr = data + bp->rx_offset; 941 } 942 rx_buf->mapping = mapping; 943 944 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 945 return 0; 946 } 947 948 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data) 949 { 950 u16 prod = rxr->rx_prod; 951 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 952 struct bnxt *bp = rxr->bnapi->bp; 953 struct rx_bd *cons_bd, *prod_bd; 954 955 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 956 cons_rx_buf = &rxr->rx_buf_ring[cons]; 957 958 prod_rx_buf->data = data; 959 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr; 960 961 prod_rx_buf->mapping = cons_rx_buf->mapping; 962 963 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 964 cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)]; 965 966 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr; 967 } 968 969 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 970 { 971 u16 next, max = rxr->rx_agg_bmap_size; 972 973 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx); 974 if (next >= max) 975 next = find_first_zero_bit(rxr->rx_agg_bmap, max); 976 return next; 977 } 978 979 static inline int bnxt_alloc_rx_page(struct bnxt *bp, 980 struct bnxt_rx_ring_info *rxr, 981 u16 prod, gfp_t gfp) 982 { 983 struct rx_bd *rxbd = 984 &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 985 struct bnxt_sw_rx_agg_bd *rx_agg_buf; 986 struct page *page; 987 dma_addr_t mapping; 988 u16 sw_prod = rxr->rx_sw_agg_prod; 989 unsigned int offset = 0; 990 991 page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp); 992 993 if (!page) 994 return -ENOMEM; 995 996 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 997 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 998 999 __set_bit(sw_prod, rxr->rx_agg_bmap); 1000 rx_agg_buf = &rxr->rx_agg_ring[sw_prod]; 1001 rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1002 1003 rx_agg_buf->page = page; 1004 rx_agg_buf->offset = offset; 1005 rx_agg_buf->mapping = mapping; 1006 rxbd->rx_bd_haddr = cpu_to_le64(mapping); 1007 rxbd->rx_bd_opaque = sw_prod; 1008 return 0; 1009 } 1010 1011 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp, 1012 struct bnxt_cp_ring_info *cpr, 1013 u16 cp_cons, u16 curr) 1014 { 1015 struct rx_agg_cmp *agg; 1016 1017 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr)); 1018 agg = (struct rx_agg_cmp *) 1019 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 1020 return agg; 1021 } 1022 1023 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp, 1024 struct bnxt_rx_ring_info *rxr, 1025 u16 agg_id, u16 curr) 1026 { 1027 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id]; 1028 1029 return &tpa_info->agg_arr[curr]; 1030 } 1031 1032 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx, 1033 u16 start, u32 agg_bufs, bool tpa) 1034 { 1035 struct bnxt_napi *bnapi = cpr->bnapi; 1036 struct bnxt *bp = bnapi->bp; 1037 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1038 u16 prod = rxr->rx_agg_prod; 1039 u16 sw_prod = rxr->rx_sw_agg_prod; 1040 bool p5_tpa = false; 1041 u32 i; 1042 1043 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1044 p5_tpa = true; 1045 1046 for (i = 0; i < agg_bufs; i++) { 1047 u16 cons; 1048 struct rx_agg_cmp *agg; 1049 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf; 1050 struct rx_bd *prod_bd; 1051 struct page *page; 1052 1053 if (p5_tpa) 1054 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i); 1055 else 1056 agg = bnxt_get_agg(bp, cpr, idx, start + i); 1057 cons = agg->rx_agg_cmp_opaque; 1058 __clear_bit(cons, rxr->rx_agg_bmap); 1059 1060 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap))) 1061 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod); 1062 1063 __set_bit(sw_prod, rxr->rx_agg_bmap); 1064 prod_rx_buf = &rxr->rx_agg_ring[sw_prod]; 1065 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1066 1067 /* It is possible for sw_prod to be equal to cons, so 1068 * set cons_rx_buf->page to NULL first. 1069 */ 1070 page = cons_rx_buf->page; 1071 cons_rx_buf->page = NULL; 1072 prod_rx_buf->page = page; 1073 prod_rx_buf->offset = cons_rx_buf->offset; 1074 1075 prod_rx_buf->mapping = cons_rx_buf->mapping; 1076 1077 prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)]; 1078 1079 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping); 1080 prod_bd->rx_bd_opaque = sw_prod; 1081 1082 prod = NEXT_RX_AGG(prod); 1083 sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod)); 1084 } 1085 rxr->rx_agg_prod = prod; 1086 rxr->rx_sw_agg_prod = sw_prod; 1087 } 1088 1089 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp, 1090 struct bnxt_rx_ring_info *rxr, 1091 u16 cons, void *data, u8 *data_ptr, 1092 dma_addr_t dma_addr, 1093 unsigned int offset_and_len) 1094 { 1095 unsigned int len = offset_and_len & 0xffff; 1096 struct page *page = data; 1097 u16 prod = rxr->rx_prod; 1098 struct sk_buff *skb; 1099 int err; 1100 1101 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1102 if (unlikely(err)) { 1103 bnxt_reuse_rx_data(rxr, cons, data); 1104 return NULL; 1105 } 1106 dma_addr -= bp->rx_dma_offset; 1107 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1108 bp->rx_dir); 1109 skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE); 1110 if (!skb) { 1111 page_pool_recycle_direct(rxr->page_pool, page); 1112 return NULL; 1113 } 1114 skb_mark_for_recycle(skb); 1115 skb_reserve(skb, bp->rx_offset); 1116 __skb_put(skb, len); 1117 1118 return skb; 1119 } 1120 1121 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp, 1122 struct bnxt_rx_ring_info *rxr, 1123 u16 cons, void *data, u8 *data_ptr, 1124 dma_addr_t dma_addr, 1125 unsigned int offset_and_len) 1126 { 1127 unsigned int payload = offset_and_len >> 16; 1128 unsigned int len = offset_and_len & 0xffff; 1129 skb_frag_t *frag; 1130 struct page *page = data; 1131 u16 prod = rxr->rx_prod; 1132 struct sk_buff *skb; 1133 int off, err; 1134 1135 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1136 if (unlikely(err)) { 1137 bnxt_reuse_rx_data(rxr, cons, data); 1138 return NULL; 1139 } 1140 dma_addr -= bp->rx_dma_offset; 1141 dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE, 1142 bp->rx_dir); 1143 1144 if (unlikely(!payload)) 1145 payload = eth_get_headlen(bp->dev, data_ptr, len); 1146 1147 skb = napi_alloc_skb(&rxr->bnapi->napi, payload); 1148 if (!skb) { 1149 page_pool_recycle_direct(rxr->page_pool, page); 1150 return NULL; 1151 } 1152 1153 skb_mark_for_recycle(skb); 1154 off = (void *)data_ptr - page_address(page); 1155 skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE); 1156 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN, 1157 payload + NET_IP_ALIGN); 1158 1159 frag = &skb_shinfo(skb)->frags[0]; 1160 skb_frag_size_sub(frag, payload); 1161 skb_frag_off_add(frag, payload); 1162 skb->data_len -= payload; 1163 skb->tail += payload; 1164 1165 return skb; 1166 } 1167 1168 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp, 1169 struct bnxt_rx_ring_info *rxr, u16 cons, 1170 void *data, u8 *data_ptr, 1171 dma_addr_t dma_addr, 1172 unsigned int offset_and_len) 1173 { 1174 u16 prod = rxr->rx_prod; 1175 struct sk_buff *skb; 1176 int err; 1177 1178 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC); 1179 if (unlikely(err)) { 1180 bnxt_reuse_rx_data(rxr, cons, data); 1181 return NULL; 1182 } 1183 1184 skb = napi_build_skb(data, bp->rx_buf_size); 1185 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size, 1186 bp->rx_dir, DMA_ATTR_WEAK_ORDERING); 1187 if (!skb) { 1188 skb_free_frag(data); 1189 return NULL; 1190 } 1191 1192 skb_reserve(skb, bp->rx_offset); 1193 skb_put(skb, offset_and_len & 0xffff); 1194 return skb; 1195 } 1196 1197 static u32 __bnxt_rx_agg_pages(struct bnxt *bp, 1198 struct bnxt_cp_ring_info *cpr, 1199 struct skb_shared_info *shinfo, 1200 u16 idx, u32 agg_bufs, bool tpa, 1201 struct xdp_buff *xdp) 1202 { 1203 struct bnxt_napi *bnapi = cpr->bnapi; 1204 struct pci_dev *pdev = bp->pdev; 1205 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1206 u16 prod = rxr->rx_agg_prod; 1207 u32 i, total_frag_len = 0; 1208 bool p5_tpa = false; 1209 1210 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa) 1211 p5_tpa = true; 1212 1213 for (i = 0; i < agg_bufs; i++) { 1214 skb_frag_t *frag = &shinfo->frags[i]; 1215 u16 cons, frag_len; 1216 struct rx_agg_cmp *agg; 1217 struct bnxt_sw_rx_agg_bd *cons_rx_buf; 1218 struct page *page; 1219 dma_addr_t mapping; 1220 1221 if (p5_tpa) 1222 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i); 1223 else 1224 agg = bnxt_get_agg(bp, cpr, idx, i); 1225 cons = agg->rx_agg_cmp_opaque; 1226 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) & 1227 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT; 1228 1229 cons_rx_buf = &rxr->rx_agg_ring[cons]; 1230 skb_frag_fill_page_desc(frag, cons_rx_buf->page, 1231 cons_rx_buf->offset, frag_len); 1232 shinfo->nr_frags = i + 1; 1233 __clear_bit(cons, rxr->rx_agg_bmap); 1234 1235 /* It is possible for bnxt_alloc_rx_page() to allocate 1236 * a sw_prod index that equals the cons index, so we 1237 * need to clear the cons entry now. 1238 */ 1239 mapping = cons_rx_buf->mapping; 1240 page = cons_rx_buf->page; 1241 cons_rx_buf->page = NULL; 1242 1243 if (xdp && page_is_pfmemalloc(page)) 1244 xdp_buff_set_frag_pfmemalloc(xdp); 1245 1246 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) { 1247 --shinfo->nr_frags; 1248 cons_rx_buf->page = page; 1249 1250 /* Update prod since possibly some pages have been 1251 * allocated already. 1252 */ 1253 rxr->rx_agg_prod = prod; 1254 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa); 1255 return 0; 1256 } 1257 1258 dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE, 1259 bp->rx_dir); 1260 1261 total_frag_len += frag_len; 1262 prod = NEXT_RX_AGG(prod); 1263 } 1264 rxr->rx_agg_prod = prod; 1265 return total_frag_len; 1266 } 1267 1268 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp, 1269 struct bnxt_cp_ring_info *cpr, 1270 struct sk_buff *skb, u16 idx, 1271 u32 agg_bufs, bool tpa) 1272 { 1273 struct skb_shared_info *shinfo = skb_shinfo(skb); 1274 u32 total_frag_len = 0; 1275 1276 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx, 1277 agg_bufs, tpa, NULL); 1278 if (!total_frag_len) { 1279 skb_mark_for_recycle(skb); 1280 dev_kfree_skb(skb); 1281 return NULL; 1282 } 1283 1284 skb->data_len += total_frag_len; 1285 skb->len += total_frag_len; 1286 skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs; 1287 return skb; 1288 } 1289 1290 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp, 1291 struct bnxt_cp_ring_info *cpr, 1292 struct xdp_buff *xdp, u16 idx, 1293 u32 agg_bufs, bool tpa) 1294 { 1295 struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp); 1296 u32 total_frag_len = 0; 1297 1298 if (!xdp_buff_has_frags(xdp)) 1299 shinfo->nr_frags = 0; 1300 1301 total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, 1302 idx, agg_bufs, tpa, xdp); 1303 if (total_frag_len) { 1304 xdp_buff_set_frags_flag(xdp); 1305 shinfo->nr_frags = agg_bufs; 1306 shinfo->xdp_frags_size = total_frag_len; 1307 } 1308 return total_frag_len; 1309 } 1310 1311 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1312 u8 agg_bufs, u32 *raw_cons) 1313 { 1314 u16 last; 1315 struct rx_agg_cmp *agg; 1316 1317 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs); 1318 last = RING_CMP(*raw_cons); 1319 agg = (struct rx_agg_cmp *) 1320 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)]; 1321 return RX_AGG_CMP_VALID(agg, *raw_cons); 1322 } 1323 1324 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data, 1325 unsigned int len, 1326 dma_addr_t mapping) 1327 { 1328 struct bnxt *bp = bnapi->bp; 1329 struct pci_dev *pdev = bp->pdev; 1330 struct sk_buff *skb; 1331 1332 skb = napi_alloc_skb(&bnapi->napi, len); 1333 if (!skb) 1334 return NULL; 1335 1336 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh, 1337 bp->rx_dir); 1338 1339 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN, 1340 len + NET_IP_ALIGN); 1341 1342 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh, 1343 bp->rx_dir); 1344 1345 skb_put(skb, len); 1346 1347 return skb; 1348 } 1349 1350 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data, 1351 unsigned int len, 1352 dma_addr_t mapping) 1353 { 1354 return bnxt_copy_data(bnapi, data, len, mapping); 1355 } 1356 1357 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi, 1358 struct xdp_buff *xdp, 1359 unsigned int len, 1360 dma_addr_t mapping) 1361 { 1362 unsigned int metasize = 0; 1363 u8 *data = xdp->data; 1364 struct sk_buff *skb; 1365 1366 len = xdp->data_end - xdp->data_meta; 1367 metasize = xdp->data - xdp->data_meta; 1368 data = xdp->data_meta; 1369 1370 skb = bnxt_copy_data(bnapi, data, len, mapping); 1371 if (!skb) 1372 return skb; 1373 1374 if (metasize) { 1375 skb_metadata_set(skb, metasize); 1376 __skb_pull(skb, metasize); 1377 } 1378 1379 return skb; 1380 } 1381 1382 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 1383 u32 *raw_cons, void *cmp) 1384 { 1385 struct rx_cmp *rxcmp = cmp; 1386 u32 tmp_raw_cons = *raw_cons; 1387 u8 cmp_type, agg_bufs = 0; 1388 1389 cmp_type = RX_CMP_TYPE(rxcmp); 1390 1391 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1392 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & 1393 RX_CMP_AGG_BUFS) >> 1394 RX_CMP_AGG_BUFS_SHIFT; 1395 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 1396 struct rx_tpa_end_cmp *tpa_end = cmp; 1397 1398 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1399 return 0; 1400 1401 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1402 } 1403 1404 if (agg_bufs) { 1405 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 1406 return -EBUSY; 1407 } 1408 *raw_cons = tmp_raw_cons; 1409 return 0; 1410 } 1411 1412 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1413 { 1414 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1415 u16 idx = agg_id & MAX_TPA_P5_MASK; 1416 1417 if (test_bit(idx, map->agg_idx_bmap)) 1418 idx = find_first_zero_bit(map->agg_idx_bmap, 1419 BNXT_AGG_IDX_BMAP_SIZE); 1420 __set_bit(idx, map->agg_idx_bmap); 1421 map->agg_id_tbl[agg_id] = idx; 1422 return idx; 1423 } 1424 1425 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx) 1426 { 1427 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1428 1429 __clear_bit(idx, map->agg_idx_bmap); 1430 } 1431 1432 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id) 1433 { 1434 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map; 1435 1436 return map->agg_id_tbl[agg_id]; 1437 } 1438 1439 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info, 1440 struct rx_tpa_start_cmp *tpa_start, 1441 struct rx_tpa_start_cmp_ext *tpa_start1) 1442 { 1443 tpa_info->cfa_code_valid = 1; 1444 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1); 1445 tpa_info->vlan_valid = 0; 1446 if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) { 1447 tpa_info->vlan_valid = 1; 1448 tpa_info->metadata = 1449 le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata); 1450 } 1451 } 1452 1453 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info, 1454 struct rx_tpa_start_cmp *tpa_start, 1455 struct rx_tpa_start_cmp_ext *tpa_start1) 1456 { 1457 tpa_info->vlan_valid = 0; 1458 if (TPA_START_VLAN_VALID(tpa_start)) { 1459 u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start); 1460 u32 vlan_proto = ETH_P_8021Q; 1461 1462 tpa_info->vlan_valid = 1; 1463 if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD) 1464 vlan_proto = ETH_P_8021AD; 1465 tpa_info->metadata = vlan_proto << 16 | 1466 TPA_START_METADATA0_TCI(tpa_start1); 1467 } 1468 } 1469 1470 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1471 u8 cmp_type, struct rx_tpa_start_cmp *tpa_start, 1472 struct rx_tpa_start_cmp_ext *tpa_start1) 1473 { 1474 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf; 1475 struct bnxt_tpa_info *tpa_info; 1476 u16 cons, prod, agg_id; 1477 struct rx_bd *prod_bd; 1478 dma_addr_t mapping; 1479 1480 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1481 agg_id = TPA_START_AGG_ID_P5(tpa_start); 1482 agg_id = bnxt_alloc_agg_idx(rxr, agg_id); 1483 } else { 1484 agg_id = TPA_START_AGG_ID(tpa_start); 1485 } 1486 cons = tpa_start->rx_tpa_start_cmp_opaque; 1487 prod = rxr->rx_prod; 1488 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1489 prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)]; 1490 tpa_info = &rxr->rx_tpa[agg_id]; 1491 1492 if (unlikely(cons != rxr->rx_next_cons || 1493 TPA_START_ERROR(tpa_start))) { 1494 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n", 1495 cons, rxr->rx_next_cons, 1496 TPA_START_ERROR_CODE(tpa_start1)); 1497 bnxt_sched_reset_rxr(bp, rxr); 1498 return; 1499 } 1500 prod_rx_buf->data = tpa_info->data; 1501 prod_rx_buf->data_ptr = tpa_info->data_ptr; 1502 1503 mapping = tpa_info->mapping; 1504 prod_rx_buf->mapping = mapping; 1505 1506 prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)]; 1507 1508 prod_bd->rx_bd_haddr = cpu_to_le64(mapping); 1509 1510 tpa_info->data = cons_rx_buf->data; 1511 tpa_info->data_ptr = cons_rx_buf->data_ptr; 1512 cons_rx_buf->data = NULL; 1513 tpa_info->mapping = cons_rx_buf->mapping; 1514 1515 tpa_info->len = 1516 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >> 1517 RX_TPA_START_CMP_LEN_SHIFT; 1518 if (likely(TPA_START_HASH_VALID(tpa_start))) { 1519 tpa_info->hash_type = PKT_HASH_TYPE_L4; 1520 tpa_info->gso_type = SKB_GSO_TCPV4; 1521 if (TPA_START_IS_IPV6(tpa_start1)) 1522 tpa_info->gso_type = SKB_GSO_TCPV6; 1523 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */ 1524 else if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP && 1525 TPA_START_HASH_TYPE(tpa_start) == 3) 1526 tpa_info->gso_type = SKB_GSO_TCPV6; 1527 tpa_info->rss_hash = 1528 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash); 1529 } else { 1530 tpa_info->hash_type = PKT_HASH_TYPE_NONE; 1531 tpa_info->gso_type = 0; 1532 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n"); 1533 } 1534 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2); 1535 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info); 1536 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) 1537 bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1); 1538 else 1539 bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1); 1540 tpa_info->agg_count = 0; 1541 1542 rxr->rx_prod = NEXT_RX(prod); 1543 cons = RING_RX(bp, NEXT_RX(cons)); 1544 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 1545 cons_rx_buf = &rxr->rx_buf_ring[cons]; 1546 1547 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data); 1548 rxr->rx_prod = NEXT_RX(rxr->rx_prod); 1549 cons_rx_buf->data = NULL; 1550 } 1551 1552 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs) 1553 { 1554 if (agg_bufs) 1555 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true); 1556 } 1557 1558 #ifdef CONFIG_INET 1559 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto) 1560 { 1561 struct udphdr *uh = NULL; 1562 1563 if (ip_proto == htons(ETH_P_IP)) { 1564 struct iphdr *iph = (struct iphdr *)skb->data; 1565 1566 if (iph->protocol == IPPROTO_UDP) 1567 uh = (struct udphdr *)(iph + 1); 1568 } else { 1569 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data; 1570 1571 if (iph->nexthdr == IPPROTO_UDP) 1572 uh = (struct udphdr *)(iph + 1); 1573 } 1574 if (uh) { 1575 if (uh->check) 1576 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1577 else 1578 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; 1579 } 1580 } 1581 #endif 1582 1583 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info, 1584 int payload_off, int tcp_ts, 1585 struct sk_buff *skb) 1586 { 1587 #ifdef CONFIG_INET 1588 struct tcphdr *th; 1589 int len, nw_off; 1590 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1591 u32 hdr_info = tpa_info->hdr_info; 1592 bool loopback = false; 1593 1594 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1595 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1596 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1597 1598 /* If the packet is an internal loopback packet, the offsets will 1599 * have an extra 4 bytes. 1600 */ 1601 if (inner_mac_off == 4) { 1602 loopback = true; 1603 } else if (inner_mac_off > 4) { 1604 __be16 proto = *((__be16 *)(skb->data + inner_ip_off - 1605 ETH_HLEN - 2)); 1606 1607 /* We only support inner iPv4/ipv6. If we don't see the 1608 * correct protocol ID, it must be a loopback packet where 1609 * the offsets are off by 4. 1610 */ 1611 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6)) 1612 loopback = true; 1613 } 1614 if (loopback) { 1615 /* internal loopback packet, subtract all offsets by 4 */ 1616 inner_ip_off -= 4; 1617 inner_mac_off -= 4; 1618 outer_ip_off -= 4; 1619 } 1620 1621 nw_off = inner_ip_off - ETH_HLEN; 1622 skb_set_network_header(skb, nw_off); 1623 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) { 1624 struct ipv6hdr *iph = ipv6_hdr(skb); 1625 1626 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1627 len = skb->len - skb_transport_offset(skb); 1628 th = tcp_hdr(skb); 1629 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1630 } else { 1631 struct iphdr *iph = ip_hdr(skb); 1632 1633 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1634 len = skb->len - skb_transport_offset(skb); 1635 th = tcp_hdr(skb); 1636 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1637 } 1638 1639 if (inner_mac_off) { /* tunnel */ 1640 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1641 ETH_HLEN - 2)); 1642 1643 bnxt_gro_tunnel(skb, proto); 1644 } 1645 #endif 1646 return skb; 1647 } 1648 1649 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info, 1650 int payload_off, int tcp_ts, 1651 struct sk_buff *skb) 1652 { 1653 #ifdef CONFIG_INET 1654 u16 outer_ip_off, inner_ip_off, inner_mac_off; 1655 u32 hdr_info = tpa_info->hdr_info; 1656 int iphdr_len, nw_off; 1657 1658 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info); 1659 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info); 1660 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info); 1661 1662 nw_off = inner_ip_off - ETH_HLEN; 1663 skb_set_network_header(skb, nw_off); 1664 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ? 1665 sizeof(struct ipv6hdr) : sizeof(struct iphdr); 1666 skb_set_transport_header(skb, nw_off + iphdr_len); 1667 1668 if (inner_mac_off) { /* tunnel */ 1669 __be16 proto = *((__be16 *)(skb->data + outer_ip_off - 1670 ETH_HLEN - 2)); 1671 1672 bnxt_gro_tunnel(skb, proto); 1673 } 1674 #endif 1675 return skb; 1676 } 1677 1678 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr)) 1679 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr)) 1680 1681 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info, 1682 int payload_off, int tcp_ts, 1683 struct sk_buff *skb) 1684 { 1685 #ifdef CONFIG_INET 1686 struct tcphdr *th; 1687 int len, nw_off, tcp_opt_len = 0; 1688 1689 if (tcp_ts) 1690 tcp_opt_len = 12; 1691 1692 if (tpa_info->gso_type == SKB_GSO_TCPV4) { 1693 struct iphdr *iph; 1694 1695 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len - 1696 ETH_HLEN; 1697 skb_set_network_header(skb, nw_off); 1698 iph = ip_hdr(skb); 1699 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr)); 1700 len = skb->len - skb_transport_offset(skb); 1701 th = tcp_hdr(skb); 1702 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0); 1703 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) { 1704 struct ipv6hdr *iph; 1705 1706 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len - 1707 ETH_HLEN; 1708 skb_set_network_header(skb, nw_off); 1709 iph = ipv6_hdr(skb); 1710 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr)); 1711 len = skb->len - skb_transport_offset(skb); 1712 th = tcp_hdr(skb); 1713 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0); 1714 } else { 1715 dev_kfree_skb_any(skb); 1716 return NULL; 1717 } 1718 1719 if (nw_off) /* tunnel */ 1720 bnxt_gro_tunnel(skb, skb->protocol); 1721 #endif 1722 return skb; 1723 } 1724 1725 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp, 1726 struct bnxt_tpa_info *tpa_info, 1727 struct rx_tpa_end_cmp *tpa_end, 1728 struct rx_tpa_end_cmp_ext *tpa_end1, 1729 struct sk_buff *skb) 1730 { 1731 #ifdef CONFIG_INET 1732 int payload_off; 1733 u16 segs; 1734 1735 segs = TPA_END_TPA_SEGS(tpa_end); 1736 if (segs == 1) 1737 return skb; 1738 1739 NAPI_GRO_CB(skb)->count = segs; 1740 skb_shinfo(skb)->gso_size = 1741 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len); 1742 skb_shinfo(skb)->gso_type = tpa_info->gso_type; 1743 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 1744 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1); 1745 else 1746 payload_off = TPA_END_PAYLOAD_OFF(tpa_end); 1747 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb); 1748 if (likely(skb)) 1749 tcp_gro_complete(skb); 1750 #endif 1751 return skb; 1752 } 1753 1754 /* Given the cfa_code of a received packet determine which 1755 * netdev (vf-rep or PF) the packet is destined to. 1756 */ 1757 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code) 1758 { 1759 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code); 1760 1761 /* if vf-rep dev is NULL, the must belongs to the PF */ 1762 return dev ? dev : bp->dev; 1763 } 1764 1765 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp, 1766 struct bnxt_cp_ring_info *cpr, 1767 u32 *raw_cons, 1768 struct rx_tpa_end_cmp *tpa_end, 1769 struct rx_tpa_end_cmp_ext *tpa_end1, 1770 u8 *event) 1771 { 1772 struct bnxt_napi *bnapi = cpr->bnapi; 1773 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 1774 struct net_device *dev = bp->dev; 1775 u8 *data_ptr, agg_bufs; 1776 unsigned int len; 1777 struct bnxt_tpa_info *tpa_info; 1778 dma_addr_t mapping; 1779 struct sk_buff *skb; 1780 u16 idx = 0, agg_id; 1781 void *data; 1782 bool gro; 1783 1784 if (unlikely(bnapi->in_reset)) { 1785 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end); 1786 1787 if (rc < 0) 1788 return ERR_PTR(-EBUSY); 1789 return NULL; 1790 } 1791 1792 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 1793 agg_id = TPA_END_AGG_ID_P5(tpa_end); 1794 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1795 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1); 1796 tpa_info = &rxr->rx_tpa[agg_id]; 1797 if (unlikely(agg_bufs != tpa_info->agg_count)) { 1798 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n", 1799 agg_bufs, tpa_info->agg_count); 1800 agg_bufs = tpa_info->agg_count; 1801 } 1802 tpa_info->agg_count = 0; 1803 *event |= BNXT_AGG_EVENT; 1804 bnxt_free_agg_idx(rxr, agg_id); 1805 idx = agg_id; 1806 gro = !!(bp->flags & BNXT_FLAG_GRO); 1807 } else { 1808 agg_id = TPA_END_AGG_ID(tpa_end); 1809 agg_bufs = TPA_END_AGG_BUFS(tpa_end); 1810 tpa_info = &rxr->rx_tpa[agg_id]; 1811 idx = RING_CMP(*raw_cons); 1812 if (agg_bufs) { 1813 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons)) 1814 return ERR_PTR(-EBUSY); 1815 1816 *event |= BNXT_AGG_EVENT; 1817 idx = NEXT_CMP(idx); 1818 } 1819 gro = !!TPA_END_GRO(tpa_end); 1820 } 1821 data = tpa_info->data; 1822 data_ptr = tpa_info->data_ptr; 1823 prefetch(data_ptr); 1824 len = tpa_info->len; 1825 mapping = tpa_info->mapping; 1826 1827 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) { 1828 bnxt_abort_tpa(cpr, idx, agg_bufs); 1829 if (agg_bufs > MAX_SKB_FRAGS) 1830 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n", 1831 agg_bufs, (int)MAX_SKB_FRAGS); 1832 return NULL; 1833 } 1834 1835 if (len <= bp->rx_copy_thresh) { 1836 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping); 1837 if (!skb) { 1838 bnxt_abort_tpa(cpr, idx, agg_bufs); 1839 cpr->sw_stats->rx.rx_oom_discards += 1; 1840 return NULL; 1841 } 1842 } else { 1843 u8 *new_data; 1844 dma_addr_t new_mapping; 1845 1846 new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, GFP_ATOMIC); 1847 if (!new_data) { 1848 bnxt_abort_tpa(cpr, idx, agg_bufs); 1849 cpr->sw_stats->rx.rx_oom_discards += 1; 1850 return NULL; 1851 } 1852 1853 tpa_info->data = new_data; 1854 tpa_info->data_ptr = new_data + bp->rx_offset; 1855 tpa_info->mapping = new_mapping; 1856 1857 skb = napi_build_skb(data, bp->rx_buf_size); 1858 dma_unmap_single_attrs(&bp->pdev->dev, mapping, 1859 bp->rx_buf_use_size, bp->rx_dir, 1860 DMA_ATTR_WEAK_ORDERING); 1861 1862 if (!skb) { 1863 skb_free_frag(data); 1864 bnxt_abort_tpa(cpr, idx, agg_bufs); 1865 cpr->sw_stats->rx.rx_oom_discards += 1; 1866 return NULL; 1867 } 1868 skb_reserve(skb, bp->rx_offset); 1869 skb_put(skb, len); 1870 } 1871 1872 if (agg_bufs) { 1873 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true); 1874 if (!skb) { 1875 /* Page reuse already handled by bnxt_rx_pages(). */ 1876 cpr->sw_stats->rx.rx_oom_discards += 1; 1877 return NULL; 1878 } 1879 } 1880 1881 if (tpa_info->cfa_code_valid) 1882 dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code); 1883 skb->protocol = eth_type_trans(skb, dev); 1884 1885 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE) 1886 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type); 1887 1888 if (tpa_info->vlan_valid && 1889 (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) { 1890 __be16 vlan_proto = htons(tpa_info->metadata >> 1891 RX_CMP_FLAGS2_METADATA_TPID_SFT); 1892 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1893 1894 if (eth_type_vlan(vlan_proto)) { 1895 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1896 } else { 1897 dev_kfree_skb(skb); 1898 return NULL; 1899 } 1900 } 1901 1902 skb_checksum_none_assert(skb); 1903 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) { 1904 skb->ip_summed = CHECKSUM_UNNECESSARY; 1905 skb->csum_level = 1906 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3; 1907 } 1908 1909 if (gro) 1910 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb); 1911 1912 return skb; 1913 } 1914 1915 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, 1916 struct rx_agg_cmp *rx_agg) 1917 { 1918 u16 agg_id = TPA_AGG_AGG_ID(rx_agg); 1919 struct bnxt_tpa_info *tpa_info; 1920 1921 agg_id = bnxt_lookup_agg_idx(rxr, agg_id); 1922 tpa_info = &rxr->rx_tpa[agg_id]; 1923 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS); 1924 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg; 1925 } 1926 1927 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi, 1928 struct sk_buff *skb) 1929 { 1930 skb_mark_for_recycle(skb); 1931 1932 if (skb->dev != bp->dev) { 1933 /* this packet belongs to a vf-rep */ 1934 bnxt_vf_rep_rx(bp, skb); 1935 return; 1936 } 1937 skb_record_rx_queue(skb, bnapi->index); 1938 napi_gro_receive(&bnapi->napi, skb); 1939 } 1940 1941 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags, 1942 struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts) 1943 { 1944 u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp); 1945 1946 if (BNXT_PTP_RX_TS_VALID(flags)) 1947 goto ts_valid; 1948 if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags)) 1949 return false; 1950 1951 ts_valid: 1952 *cmpl_ts = ts; 1953 return true; 1954 } 1955 1956 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type, 1957 struct rx_cmp *rxcmp, 1958 struct rx_cmp_ext *rxcmp1) 1959 { 1960 __be16 vlan_proto; 1961 u16 vtag; 1962 1963 if (cmp_type == CMP_TYPE_RX_L2_CMP) { 1964 __le32 flags2 = rxcmp1->rx_cmp_flags2; 1965 u32 meta_data; 1966 1967 if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN))) 1968 return skb; 1969 1970 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data); 1971 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK; 1972 vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT); 1973 if (eth_type_vlan(vlan_proto)) 1974 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1975 else 1976 goto vlan_err; 1977 } else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 1978 if (RX_CMP_VLAN_VALID(rxcmp)) { 1979 u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp); 1980 1981 if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q) 1982 vlan_proto = htons(ETH_P_8021Q); 1983 else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD) 1984 vlan_proto = htons(ETH_P_8021AD); 1985 else 1986 goto vlan_err; 1987 vtag = RX_CMP_METADATA0_TCI(rxcmp1); 1988 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag); 1989 } 1990 } 1991 return skb; 1992 vlan_err: 1993 dev_kfree_skb(skb); 1994 return NULL; 1995 } 1996 1997 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp, 1998 struct rx_cmp *rxcmp) 1999 { 2000 u8 ext_op; 2001 2002 ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp); 2003 switch (ext_op) { 2004 case EXT_OP_INNER_4: 2005 case EXT_OP_OUTER_4: 2006 case EXT_OP_INNFL_3: 2007 case EXT_OP_OUTFL_3: 2008 return PKT_HASH_TYPE_L4; 2009 default: 2010 return PKT_HASH_TYPE_L3; 2011 } 2012 } 2013 2014 /* returns the following: 2015 * 1 - 1 packet successfully received 2016 * 0 - successful TPA_START, packet not completed yet 2017 * -EBUSY - completion ring does not have all the agg buffers yet 2018 * -ENOMEM - packet aborted due to out of memory 2019 * -EIO - packet aborted due to hw error indicated in BD 2020 */ 2021 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2022 u32 *raw_cons, u8 *event) 2023 { 2024 struct bnxt_napi *bnapi = cpr->bnapi; 2025 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2026 struct net_device *dev = bp->dev; 2027 struct rx_cmp *rxcmp; 2028 struct rx_cmp_ext *rxcmp1; 2029 u32 tmp_raw_cons = *raw_cons; 2030 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons); 2031 struct bnxt_sw_rx_bd *rx_buf; 2032 unsigned int len; 2033 u8 *data_ptr, agg_bufs, cmp_type; 2034 bool xdp_active = false; 2035 dma_addr_t dma_addr; 2036 struct sk_buff *skb; 2037 struct xdp_buff xdp; 2038 u32 flags, misc; 2039 u32 cmpl_ts; 2040 void *data; 2041 int rc = 0; 2042 2043 rxcmp = (struct rx_cmp *) 2044 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2045 2046 cmp_type = RX_CMP_TYPE(rxcmp); 2047 2048 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) { 2049 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp); 2050 goto next_rx_no_prod_no_len; 2051 } 2052 2053 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2054 cp_cons = RING_CMP(tmp_raw_cons); 2055 rxcmp1 = (struct rx_cmp_ext *) 2056 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2057 2058 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2059 return -EBUSY; 2060 2061 /* The valid test of the entry must be done first before 2062 * reading any further. 2063 */ 2064 dma_rmb(); 2065 prod = rxr->rx_prod; 2066 2067 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP || 2068 cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2069 bnxt_tpa_start(bp, rxr, cmp_type, 2070 (struct rx_tpa_start_cmp *)rxcmp, 2071 (struct rx_tpa_start_cmp_ext *)rxcmp1); 2072 2073 *event |= BNXT_RX_EVENT; 2074 goto next_rx_no_prod_no_len; 2075 2076 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2077 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons, 2078 (struct rx_tpa_end_cmp *)rxcmp, 2079 (struct rx_tpa_end_cmp_ext *)rxcmp1, event); 2080 2081 if (IS_ERR(skb)) 2082 return -EBUSY; 2083 2084 rc = -ENOMEM; 2085 if (likely(skb)) { 2086 bnxt_deliver_skb(bp, bnapi, skb); 2087 rc = 1; 2088 } 2089 *event |= BNXT_RX_EVENT; 2090 goto next_rx_no_prod_no_len; 2091 } 2092 2093 cons = rxcmp->rx_cmp_opaque; 2094 if (unlikely(cons != rxr->rx_next_cons)) { 2095 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp); 2096 2097 /* 0xffff is forced error, don't print it */ 2098 if (rxr->rx_next_cons != 0xffff) 2099 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n", 2100 cons, rxr->rx_next_cons); 2101 bnxt_sched_reset_rxr(bp, rxr); 2102 if (rc1) 2103 return rc1; 2104 goto next_rx_no_prod_no_len; 2105 } 2106 rx_buf = &rxr->rx_buf_ring[cons]; 2107 data = rx_buf->data; 2108 data_ptr = rx_buf->data_ptr; 2109 prefetch(data_ptr); 2110 2111 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1); 2112 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT; 2113 2114 if (agg_bufs) { 2115 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons)) 2116 return -EBUSY; 2117 2118 cp_cons = NEXT_CMP(cp_cons); 2119 *event |= BNXT_AGG_EVENT; 2120 } 2121 *event |= BNXT_RX_EVENT; 2122 2123 rx_buf->data = NULL; 2124 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) { 2125 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2); 2126 2127 bnxt_reuse_rx_data(rxr, cons, data); 2128 if (agg_bufs) 2129 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs, 2130 false); 2131 2132 rc = -EIO; 2133 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) { 2134 bnapi->cp_ring.sw_stats->rx.rx_buf_errors++; 2135 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 2136 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) { 2137 netdev_warn_once(bp->dev, "RX buffer error %x\n", 2138 rx_err); 2139 bnxt_sched_reset_rxr(bp, rxr); 2140 } 2141 } 2142 goto next_rx_no_len; 2143 } 2144 2145 flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type); 2146 len = flags >> RX_CMP_LEN_SHIFT; 2147 dma_addr = rx_buf->mapping; 2148 2149 if (bnxt_xdp_attached(bp, rxr)) { 2150 bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp); 2151 if (agg_bufs) { 2152 u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp, 2153 cp_cons, agg_bufs, 2154 false); 2155 if (!frag_len) 2156 goto oom_next_rx; 2157 } 2158 xdp_active = true; 2159 } 2160 2161 if (xdp_active) { 2162 if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) { 2163 rc = 1; 2164 goto next_rx; 2165 } 2166 } 2167 2168 if (len <= bp->rx_copy_thresh) { 2169 if (!xdp_active) 2170 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr); 2171 else 2172 skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr); 2173 bnxt_reuse_rx_data(rxr, cons, data); 2174 if (!skb) { 2175 if (agg_bufs) { 2176 if (!xdp_active) 2177 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, 2178 agg_bufs, false); 2179 else 2180 bnxt_xdp_buff_frags_free(rxr, &xdp); 2181 } 2182 goto oom_next_rx; 2183 } 2184 } else { 2185 u32 payload; 2186 2187 if (rx_buf->data_ptr == data_ptr) 2188 payload = misc & RX_CMP_PAYLOAD_OFFSET; 2189 else 2190 payload = 0; 2191 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr, 2192 payload | len); 2193 if (!skb) 2194 goto oom_next_rx; 2195 } 2196 2197 if (agg_bufs) { 2198 if (!xdp_active) { 2199 skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false); 2200 if (!skb) 2201 goto oom_next_rx; 2202 } else { 2203 skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1); 2204 if (!skb) { 2205 /* we should be able to free the old skb here */ 2206 bnxt_xdp_buff_frags_free(rxr, &xdp); 2207 goto oom_next_rx; 2208 } 2209 } 2210 } 2211 2212 if (RX_CMP_HASH_VALID(rxcmp)) { 2213 enum pkt_hash_types type; 2214 2215 if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2216 type = bnxt_rss_ext_op(bp, rxcmp); 2217 } else { 2218 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp); 2219 2220 /* RSS profiles 1 and 3 with extract code 0 for inner 2221 * 4-tuple 2222 */ 2223 if (hash_type != 1 && hash_type != 3) 2224 type = PKT_HASH_TYPE_L3; 2225 else 2226 type = PKT_HASH_TYPE_L4; 2227 } 2228 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type); 2229 } 2230 2231 if (cmp_type == CMP_TYPE_RX_L2_CMP) 2232 dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1)); 2233 skb->protocol = eth_type_trans(skb, dev); 2234 2235 if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) { 2236 skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1); 2237 if (!skb) 2238 goto next_rx; 2239 } 2240 2241 skb_checksum_none_assert(skb); 2242 if (RX_CMP_L4_CS_OK(rxcmp1)) { 2243 if (dev->features & NETIF_F_RXCSUM) { 2244 skb->ip_summed = CHECKSUM_UNNECESSARY; 2245 skb->csum_level = RX_CMP_ENCAP(rxcmp1); 2246 } 2247 } else { 2248 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) { 2249 if (dev->features & NETIF_F_RXCSUM) 2250 bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++; 2251 } 2252 } 2253 2254 if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) { 2255 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 2256 u64 ns, ts; 2257 2258 if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) { 2259 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2260 2261 spin_lock_bh(&ptp->ptp_lock); 2262 ns = timecounter_cyc2time(&ptp->tc, ts); 2263 spin_unlock_bh(&ptp->ptp_lock); 2264 memset(skb_hwtstamps(skb), 0, 2265 sizeof(*skb_hwtstamps(skb))); 2266 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns); 2267 } 2268 } 2269 } 2270 bnxt_deliver_skb(bp, bnapi, skb); 2271 rc = 1; 2272 2273 next_rx: 2274 cpr->rx_packets += 1; 2275 cpr->rx_bytes += len; 2276 2277 next_rx_no_len: 2278 rxr->rx_prod = NEXT_RX(prod); 2279 rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons)); 2280 2281 next_rx_no_prod_no_len: 2282 *raw_cons = tmp_raw_cons; 2283 2284 return rc; 2285 2286 oom_next_rx: 2287 cpr->sw_stats->rx.rx_oom_discards += 1; 2288 rc = -ENOMEM; 2289 goto next_rx; 2290 } 2291 2292 /* In netpoll mode, if we are using a combined completion ring, we need to 2293 * discard the rx packets and recycle the buffers. 2294 */ 2295 static int bnxt_force_rx_discard(struct bnxt *bp, 2296 struct bnxt_cp_ring_info *cpr, 2297 u32 *raw_cons, u8 *event) 2298 { 2299 u32 tmp_raw_cons = *raw_cons; 2300 struct rx_cmp_ext *rxcmp1; 2301 struct rx_cmp *rxcmp; 2302 u16 cp_cons; 2303 u8 cmp_type; 2304 int rc; 2305 2306 cp_cons = RING_CMP(tmp_raw_cons); 2307 rxcmp = (struct rx_cmp *) 2308 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2309 2310 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons); 2311 cp_cons = RING_CMP(tmp_raw_cons); 2312 rxcmp1 = (struct rx_cmp_ext *) 2313 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 2314 2315 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 2316 return -EBUSY; 2317 2318 /* The valid test of the entry must be done first before 2319 * reading any further. 2320 */ 2321 dma_rmb(); 2322 cmp_type = RX_CMP_TYPE(rxcmp); 2323 if (cmp_type == CMP_TYPE_RX_L2_CMP || 2324 cmp_type == CMP_TYPE_RX_L2_V3_CMP) { 2325 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 2326 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 2327 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) { 2328 struct rx_tpa_end_cmp_ext *tpa_end1; 2329 2330 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1; 2331 tpa_end1->rx_tpa_end_cmp_errors_v2 |= 2332 cpu_to_le32(RX_TPA_END_CMP_ERRORS); 2333 } 2334 rc = bnxt_rx_pkt(bp, cpr, raw_cons, event); 2335 if (rc && rc != -EBUSY) 2336 cpr->sw_stats->rx.rx_netpoll_discards += 1; 2337 return rc; 2338 } 2339 2340 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx) 2341 { 2342 struct bnxt_fw_health *fw_health = bp->fw_health; 2343 u32 reg = fw_health->regs[reg_idx]; 2344 u32 reg_type, reg_off, val = 0; 2345 2346 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 2347 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 2348 switch (reg_type) { 2349 case BNXT_FW_HEALTH_REG_TYPE_CFG: 2350 pci_read_config_dword(bp->pdev, reg_off, &val); 2351 break; 2352 case BNXT_FW_HEALTH_REG_TYPE_GRC: 2353 reg_off = fw_health->mapped_regs[reg_idx]; 2354 fallthrough; 2355 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 2356 val = readl(bp->bar0 + reg_off); 2357 break; 2358 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 2359 val = readl(bp->bar1 + reg_off); 2360 break; 2361 } 2362 if (reg_idx == BNXT_FW_RESET_INPROG_REG) 2363 val &= fw_health->fw_reset_inprog_reg_mask; 2364 return val; 2365 } 2366 2367 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id) 2368 { 2369 int i; 2370 2371 for (i = 0; i < bp->rx_nr_rings; i++) { 2372 u16 grp_idx = bp->rx_ring[i].bnapi->index; 2373 struct bnxt_ring_grp_info *grp_info; 2374 2375 grp_info = &bp->grp_info[grp_idx]; 2376 if (grp_info->agg_fw_ring_id == ring_id) 2377 return grp_idx; 2378 } 2379 return INVALID_HW_RING_ID; 2380 } 2381 2382 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info) 2383 { 2384 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2385 2386 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 2387 return link_info->force_link_speed2; 2388 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4) 2389 return link_info->force_pam4_link_speed; 2390 return link_info->force_link_speed; 2391 } 2392 2393 static void bnxt_set_force_speed(struct bnxt_link_info *link_info) 2394 { 2395 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2396 2397 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2398 link_info->req_link_speed = link_info->force_link_speed2; 2399 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2400 switch (link_info->req_link_speed) { 2401 case BNXT_LINK_SPEED_50GB_PAM4: 2402 case BNXT_LINK_SPEED_100GB_PAM4: 2403 case BNXT_LINK_SPEED_200GB_PAM4: 2404 case BNXT_LINK_SPEED_400GB_PAM4: 2405 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2406 break; 2407 case BNXT_LINK_SPEED_100GB_PAM4_112: 2408 case BNXT_LINK_SPEED_200GB_PAM4_112: 2409 case BNXT_LINK_SPEED_400GB_PAM4_112: 2410 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112; 2411 break; 2412 default: 2413 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2414 } 2415 return; 2416 } 2417 link_info->req_link_speed = link_info->force_link_speed; 2418 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ; 2419 if (link_info->force_pam4_link_speed) { 2420 link_info->req_link_speed = link_info->force_pam4_link_speed; 2421 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4; 2422 } 2423 } 2424 2425 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info) 2426 { 2427 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2428 2429 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2430 link_info->advertising = link_info->auto_link_speeds2; 2431 return; 2432 } 2433 link_info->advertising = link_info->auto_link_speeds; 2434 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds; 2435 } 2436 2437 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info) 2438 { 2439 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2440 2441 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2442 if (link_info->req_link_speed != link_info->force_link_speed2) 2443 return true; 2444 return false; 2445 } 2446 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ && 2447 link_info->req_link_speed != link_info->force_link_speed) 2448 return true; 2449 if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 && 2450 link_info->req_link_speed != link_info->force_pam4_link_speed) 2451 return true; 2452 return false; 2453 } 2454 2455 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info) 2456 { 2457 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 2458 2459 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 2460 if (link_info->advertising != link_info->auto_link_speeds2) 2461 return true; 2462 return false; 2463 } 2464 if (link_info->advertising != link_info->auto_link_speeds || 2465 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds) 2466 return true; 2467 return false; 2468 } 2469 2470 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2) \ 2471 ((data2) & \ 2472 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK) 2473 2474 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2) \ 2475 (((data2) & \ 2476 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\ 2477 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT) 2478 2479 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1) \ 2480 ((data1) & \ 2481 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK) 2482 2483 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1) \ 2484 (((data1) & \ 2485 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\ 2486 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING) 2487 2488 /* Return true if the workqueue has to be scheduled */ 2489 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2) 2490 { 2491 u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1); 2492 2493 switch (err_type) { 2494 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL: 2495 netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n", 2496 BNXT_EVENT_INVALID_SIGNAL_DATA(data2)); 2497 break; 2498 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM: 2499 netdev_warn(bp->dev, "Pause Storm detected!\n"); 2500 break; 2501 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD: 2502 netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n"); 2503 break; 2504 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: { 2505 u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1); 2506 char *threshold_type; 2507 bool notify = false; 2508 char *dir_str; 2509 2510 switch (type) { 2511 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN: 2512 threshold_type = "warning"; 2513 break; 2514 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL: 2515 threshold_type = "critical"; 2516 break; 2517 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL: 2518 threshold_type = "fatal"; 2519 break; 2520 case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN: 2521 threshold_type = "shutdown"; 2522 break; 2523 default: 2524 netdev_err(bp->dev, "Unknown Thermal threshold type event\n"); 2525 return false; 2526 } 2527 if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) { 2528 dir_str = "above"; 2529 notify = true; 2530 } else { 2531 dir_str = "below"; 2532 } 2533 netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n", 2534 dir_str, threshold_type); 2535 netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n", 2536 BNXT_EVENT_THERMAL_CURRENT_TEMP(data2), 2537 BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)); 2538 if (notify) { 2539 bp->thermal_threshold_type = type; 2540 set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event); 2541 return true; 2542 } 2543 return false; 2544 } 2545 case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED: 2546 netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n"); 2547 break; 2548 default: 2549 netdev_err(bp->dev, "FW reported unknown error type %u\n", 2550 err_type); 2551 break; 2552 } 2553 return false; 2554 } 2555 2556 #define BNXT_GET_EVENT_PORT(data) \ 2557 ((data) & \ 2558 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK) 2559 2560 #define BNXT_EVENT_RING_TYPE(data2) \ 2561 ((data2) & \ 2562 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK) 2563 2564 #define BNXT_EVENT_RING_TYPE_RX(data2) \ 2565 (BNXT_EVENT_RING_TYPE(data2) == \ 2566 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX) 2567 2568 #define BNXT_EVENT_PHC_EVENT_TYPE(data1) \ 2569 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\ 2570 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT) 2571 2572 #define BNXT_EVENT_PHC_RTC_UPDATE(data1) \ 2573 (((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\ 2574 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT) 2575 2576 #define BNXT_PHC_BITS 48 2577 2578 static int bnxt_async_event_process(struct bnxt *bp, 2579 struct hwrm_async_event_cmpl *cmpl) 2580 { 2581 u16 event_id = le16_to_cpu(cmpl->event_id); 2582 u32 data1 = le32_to_cpu(cmpl->event_data1); 2583 u32 data2 = le32_to_cpu(cmpl->event_data2); 2584 2585 netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n", 2586 event_id, data1, data2); 2587 2588 /* TODO CHIMP_FW: Define event id's for link change, error etc */ 2589 switch (event_id) { 2590 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: { 2591 struct bnxt_link_info *link_info = &bp->link_info; 2592 2593 if (BNXT_VF(bp)) 2594 goto async_event_process_exit; 2595 2596 /* print unsupported speed warning in forced speed mode only */ 2597 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) && 2598 (data1 & 0x20000)) { 2599 u16 fw_speed = bnxt_get_force_speed(link_info); 2600 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed); 2601 2602 if (speed != SPEED_UNKNOWN) 2603 netdev_warn(bp->dev, "Link speed %d no longer supported\n", 2604 speed); 2605 } 2606 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event); 2607 } 2608 fallthrough; 2609 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE: 2610 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE: 2611 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event); 2612 fallthrough; 2613 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE: 2614 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event); 2615 break; 2616 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD: 2617 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event); 2618 break; 2619 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: { 2620 u16 port_id = BNXT_GET_EVENT_PORT(data1); 2621 2622 if (BNXT_VF(bp)) 2623 break; 2624 2625 if (bp->pf.port_id != port_id) 2626 break; 2627 2628 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event); 2629 break; 2630 } 2631 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE: 2632 if (BNXT_PF(bp)) 2633 goto async_event_process_exit; 2634 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event); 2635 break; 2636 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: { 2637 char *type_str = "Solicited"; 2638 2639 if (!bp->fw_health) 2640 goto async_event_process_exit; 2641 2642 bp->fw_reset_timestamp = jiffies; 2643 bp->fw_reset_min_dsecs = cmpl->timestamp_lo; 2644 if (!bp->fw_reset_min_dsecs) 2645 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS; 2646 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi); 2647 if (!bp->fw_reset_max_dsecs) 2648 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS; 2649 if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) { 2650 set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state); 2651 } else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) { 2652 type_str = "Fatal"; 2653 bp->fw_health->fatalities++; 2654 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 2655 } else if (data2 && BNXT_FW_STATUS_HEALTHY != 2656 EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) { 2657 type_str = "Non-fatal"; 2658 bp->fw_health->survivals++; 2659 set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 2660 } 2661 netif_warn(bp, hw, bp->dev, 2662 "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n", 2663 type_str, data1, data2, 2664 bp->fw_reset_min_dsecs * 100, 2665 bp->fw_reset_max_dsecs * 100); 2666 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event); 2667 break; 2668 } 2669 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: { 2670 struct bnxt_fw_health *fw_health = bp->fw_health; 2671 char *status_desc = "healthy"; 2672 u32 status; 2673 2674 if (!fw_health) 2675 goto async_event_process_exit; 2676 2677 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) { 2678 fw_health->enabled = false; 2679 netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n"); 2680 break; 2681 } 2682 fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1); 2683 fw_health->tmr_multiplier = 2684 DIV_ROUND_UP(fw_health->polling_dsecs * HZ, 2685 bp->current_interval * 10); 2686 fw_health->tmr_counter = fw_health->tmr_multiplier; 2687 if (!fw_health->enabled) 2688 fw_health->last_fw_heartbeat = 2689 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 2690 fw_health->last_fw_reset_cnt = 2691 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 2692 status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 2693 if (status != BNXT_FW_STATUS_HEALTHY) 2694 status_desc = "unhealthy"; 2695 netif_info(bp, drv, bp->dev, 2696 "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n", 2697 fw_health->primary ? "primary" : "backup", status, 2698 status_desc, fw_health->last_fw_reset_cnt); 2699 if (!fw_health->enabled) { 2700 /* Make sure tmr_counter is set and visible to 2701 * bnxt_health_check() before setting enabled to true. 2702 */ 2703 smp_wmb(); 2704 fw_health->enabled = true; 2705 } 2706 goto async_event_process_exit; 2707 } 2708 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION: 2709 netif_notice(bp, hw, bp->dev, 2710 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n", 2711 data1, data2); 2712 goto async_event_process_exit; 2713 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: { 2714 struct bnxt_rx_ring_info *rxr; 2715 u16 grp_idx; 2716 2717 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 2718 goto async_event_process_exit; 2719 2720 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n", 2721 BNXT_EVENT_RING_TYPE(data2), data1); 2722 if (!BNXT_EVENT_RING_TYPE_RX(data2)) 2723 goto async_event_process_exit; 2724 2725 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1); 2726 if (grp_idx == INVALID_HW_RING_ID) { 2727 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n", 2728 data1); 2729 goto async_event_process_exit; 2730 } 2731 rxr = bp->bnapi[grp_idx]->rx_ring; 2732 bnxt_sched_reset_rxr(bp, rxr); 2733 goto async_event_process_exit; 2734 } 2735 case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: { 2736 struct bnxt_fw_health *fw_health = bp->fw_health; 2737 2738 netif_notice(bp, hw, bp->dev, 2739 "Received firmware echo request, data1: 0x%x, data2: 0x%x\n", 2740 data1, data2); 2741 if (fw_health) { 2742 fw_health->echo_req_data1 = data1; 2743 fw_health->echo_req_data2 = data2; 2744 set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event); 2745 break; 2746 } 2747 goto async_event_process_exit; 2748 } 2749 case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: { 2750 bnxt_ptp_pps_event(bp, data1, data2); 2751 goto async_event_process_exit; 2752 } 2753 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: { 2754 if (bnxt_event_error_report(bp, data1, data2)) 2755 break; 2756 goto async_event_process_exit; 2757 } 2758 case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: { 2759 switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) { 2760 case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE: 2761 if (BNXT_PTP_USE_RTC(bp)) { 2762 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 2763 u64 ns; 2764 2765 if (!ptp) 2766 goto async_event_process_exit; 2767 2768 spin_lock_bh(&ptp->ptp_lock); 2769 bnxt_ptp_update_current_time(bp); 2770 ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) << 2771 BNXT_PHC_BITS) | ptp->current_time); 2772 bnxt_ptp_rtc_timecounter_init(ptp, ns); 2773 spin_unlock_bh(&ptp->ptp_lock); 2774 } 2775 break; 2776 } 2777 goto async_event_process_exit; 2778 } 2779 case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: { 2780 u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff; 2781 2782 hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED); 2783 goto async_event_process_exit; 2784 } 2785 default: 2786 goto async_event_process_exit; 2787 } 2788 __bnxt_queue_sp_work(bp); 2789 async_event_process_exit: 2790 return 0; 2791 } 2792 2793 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp) 2794 { 2795 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id; 2796 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp; 2797 struct hwrm_fwd_req_cmpl *fwd_req_cmpl = 2798 (struct hwrm_fwd_req_cmpl *)txcmp; 2799 2800 switch (cmpl_type) { 2801 case CMPL_BASE_TYPE_HWRM_DONE: 2802 seq_id = le16_to_cpu(h_cmpl->sequence_id); 2803 hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE); 2804 break; 2805 2806 case CMPL_BASE_TYPE_HWRM_FWD_REQ: 2807 vf_id = le16_to_cpu(fwd_req_cmpl->source_id); 2808 2809 if ((vf_id < bp->pf.first_vf_id) || 2810 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) { 2811 netdev_err(bp->dev, "Msg contains invalid VF id %x\n", 2812 vf_id); 2813 return -EINVAL; 2814 } 2815 2816 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap); 2817 bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT); 2818 break; 2819 2820 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT: 2821 bnxt_async_event_process(bp, 2822 (struct hwrm_async_event_cmpl *)txcmp); 2823 break; 2824 2825 default: 2826 break; 2827 } 2828 2829 return 0; 2830 } 2831 2832 static irqreturn_t bnxt_msix(int irq, void *dev_instance) 2833 { 2834 struct bnxt_napi *bnapi = dev_instance; 2835 struct bnxt *bp = bnapi->bp; 2836 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2837 u32 cons = RING_CMP(cpr->cp_raw_cons); 2838 2839 cpr->event_ctr++; 2840 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2841 napi_schedule(&bnapi->napi); 2842 return IRQ_HANDLED; 2843 } 2844 2845 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr) 2846 { 2847 u32 raw_cons = cpr->cp_raw_cons; 2848 u16 cons = RING_CMP(raw_cons); 2849 struct tx_cmp *txcmp; 2850 2851 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2852 2853 return TX_CMP_VALID(txcmp, raw_cons); 2854 } 2855 2856 static irqreturn_t bnxt_inta(int irq, void *dev_instance) 2857 { 2858 struct bnxt_napi *bnapi = dev_instance; 2859 struct bnxt *bp = bnapi->bp; 2860 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 2861 u32 cons = RING_CMP(cpr->cp_raw_cons); 2862 u32 int_status; 2863 2864 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]); 2865 2866 if (!bnxt_has_work(bp, cpr)) { 2867 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS); 2868 /* return if erroneous interrupt */ 2869 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id))) 2870 return IRQ_NONE; 2871 } 2872 2873 /* disable ring IRQ */ 2874 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell); 2875 2876 /* Return here if interrupt is shared and is disabled. */ 2877 if (unlikely(atomic_read(&bp->intr_sem) != 0)) 2878 return IRQ_HANDLED; 2879 2880 napi_schedule(&bnapi->napi); 2881 return IRQ_HANDLED; 2882 } 2883 2884 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 2885 int budget) 2886 { 2887 struct bnxt_napi *bnapi = cpr->bnapi; 2888 u32 raw_cons = cpr->cp_raw_cons; 2889 u32 cons; 2890 int rx_pkts = 0; 2891 u8 event = 0; 2892 struct tx_cmp *txcmp; 2893 2894 cpr->has_more_work = 0; 2895 cpr->had_work_done = 1; 2896 while (1) { 2897 u8 cmp_type; 2898 int rc; 2899 2900 cons = RING_CMP(raw_cons); 2901 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 2902 2903 if (!TX_CMP_VALID(txcmp, raw_cons)) 2904 break; 2905 2906 /* The valid test of the entry must be done first before 2907 * reading any further. 2908 */ 2909 dma_rmb(); 2910 cmp_type = TX_CMP_TYPE(txcmp); 2911 if (cmp_type == CMP_TYPE_TX_L2_CMP || 2912 cmp_type == CMP_TYPE_TX_L2_COAL_CMP) { 2913 u32 opaque = txcmp->tx_cmp_opaque; 2914 struct bnxt_tx_ring_info *txr; 2915 u16 tx_freed; 2916 2917 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)]; 2918 event |= BNXT_TX_CMP_EVENT; 2919 if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP) 2920 txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp); 2921 else 2922 txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque); 2923 tx_freed = (txr->tx_hw_cons - txr->tx_cons) & 2924 bp->tx_ring_mask; 2925 /* return full budget so NAPI will complete. */ 2926 if (unlikely(tx_freed >= bp->tx_wake_thresh)) { 2927 rx_pkts = budget; 2928 raw_cons = NEXT_RAW_CMP(raw_cons); 2929 if (budget) 2930 cpr->has_more_work = 1; 2931 break; 2932 } 2933 } else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) { 2934 bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp); 2935 } else if (cmp_type >= CMP_TYPE_RX_L2_CMP && 2936 cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) { 2937 if (likely(budget)) 2938 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 2939 else 2940 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons, 2941 &event); 2942 if (likely(rc >= 0)) 2943 rx_pkts += rc; 2944 /* Increment rx_pkts when rc is -ENOMEM to count towards 2945 * the NAPI budget. Otherwise, we may potentially loop 2946 * here forever if we consistently cannot allocate 2947 * buffers. 2948 */ 2949 else if (rc == -ENOMEM && budget) 2950 rx_pkts++; 2951 else if (rc == -EBUSY) /* partial completion */ 2952 break; 2953 } else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE || 2954 cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ || 2955 cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) { 2956 bnxt_hwrm_handler(bp, txcmp); 2957 } 2958 raw_cons = NEXT_RAW_CMP(raw_cons); 2959 2960 if (rx_pkts && rx_pkts == budget) { 2961 cpr->has_more_work = 1; 2962 break; 2963 } 2964 } 2965 2966 if (event & BNXT_REDIRECT_EVENT) { 2967 xdp_do_flush(); 2968 event &= ~BNXT_REDIRECT_EVENT; 2969 } 2970 2971 if (event & BNXT_TX_EVENT) { 2972 struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0]; 2973 u16 prod = txr->tx_prod; 2974 2975 /* Sync BD data before updating doorbell */ 2976 wmb(); 2977 2978 bnxt_db_write_relaxed(bp, &txr->tx_db, prod); 2979 event &= ~BNXT_TX_EVENT; 2980 } 2981 2982 cpr->cp_raw_cons = raw_cons; 2983 bnapi->events |= event; 2984 return rx_pkts; 2985 } 2986 2987 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi, 2988 int budget) 2989 { 2990 if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault) 2991 bnapi->tx_int(bp, bnapi, budget); 2992 2993 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) { 2994 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 2995 2996 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 2997 bnapi->events &= ~BNXT_RX_EVENT; 2998 } 2999 if (bnapi->events & BNXT_AGG_EVENT) { 3000 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3001 3002 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3003 bnapi->events &= ~BNXT_AGG_EVENT; 3004 } 3005 } 3006 3007 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr, 3008 int budget) 3009 { 3010 struct bnxt_napi *bnapi = cpr->bnapi; 3011 int rx_pkts; 3012 3013 rx_pkts = __bnxt_poll_work(bp, cpr, budget); 3014 3015 /* ACK completion ring before freeing tx ring and producing new 3016 * buffers in rx/agg rings to prevent overflowing the completion 3017 * ring. 3018 */ 3019 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons); 3020 3021 __bnxt_poll_work_done(bp, bnapi, budget); 3022 return rx_pkts; 3023 } 3024 3025 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget) 3026 { 3027 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3028 struct bnxt *bp = bnapi->bp; 3029 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3030 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 3031 struct tx_cmp *txcmp; 3032 struct rx_cmp_ext *rxcmp1; 3033 u32 cp_cons, tmp_raw_cons; 3034 u32 raw_cons = cpr->cp_raw_cons; 3035 bool flush_xdp = false; 3036 u32 rx_pkts = 0; 3037 u8 event = 0; 3038 3039 while (1) { 3040 int rc; 3041 3042 cp_cons = RING_CMP(raw_cons); 3043 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3044 3045 if (!TX_CMP_VALID(txcmp, raw_cons)) 3046 break; 3047 3048 /* The valid test of the entry must be done first before 3049 * reading any further. 3050 */ 3051 dma_rmb(); 3052 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) { 3053 tmp_raw_cons = NEXT_RAW_CMP(raw_cons); 3054 cp_cons = RING_CMP(tmp_raw_cons); 3055 rxcmp1 = (struct rx_cmp_ext *) 3056 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)]; 3057 3058 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons)) 3059 break; 3060 3061 /* force an error to recycle the buffer */ 3062 rxcmp1->rx_cmp_cfa_code_errors_v2 |= 3063 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR); 3064 3065 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event); 3066 if (likely(rc == -EIO) && budget) 3067 rx_pkts++; 3068 else if (rc == -EBUSY) /* partial completion */ 3069 break; 3070 if (event & BNXT_REDIRECT_EVENT) 3071 flush_xdp = true; 3072 } else if (unlikely(TX_CMP_TYPE(txcmp) == 3073 CMPL_BASE_TYPE_HWRM_DONE)) { 3074 bnxt_hwrm_handler(bp, txcmp); 3075 } else { 3076 netdev_err(bp->dev, 3077 "Invalid completion received on special ring\n"); 3078 } 3079 raw_cons = NEXT_RAW_CMP(raw_cons); 3080 3081 if (rx_pkts == budget) 3082 break; 3083 } 3084 3085 cpr->cp_raw_cons = raw_cons; 3086 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons); 3087 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 3088 3089 if (event & BNXT_AGG_EVENT) 3090 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 3091 if (flush_xdp) 3092 xdp_do_flush(); 3093 3094 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) { 3095 napi_complete_done(napi, rx_pkts); 3096 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3097 } 3098 return rx_pkts; 3099 } 3100 3101 static int bnxt_poll(struct napi_struct *napi, int budget) 3102 { 3103 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3104 struct bnxt *bp = bnapi->bp; 3105 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3106 int work_done = 0; 3107 3108 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3109 napi_complete(napi); 3110 return 0; 3111 } 3112 while (1) { 3113 work_done += bnxt_poll_work(bp, cpr, budget - work_done); 3114 3115 if (work_done >= budget) { 3116 if (!budget) 3117 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3118 break; 3119 } 3120 3121 if (!bnxt_has_work(bp, cpr)) { 3122 if (napi_complete_done(napi, work_done)) 3123 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons); 3124 break; 3125 } 3126 } 3127 if (bp->flags & BNXT_FLAG_DIM) { 3128 struct dim_sample dim_sample = {}; 3129 3130 dim_update_sample(cpr->event_ctr, 3131 cpr->rx_packets, 3132 cpr->rx_bytes, 3133 &dim_sample); 3134 net_dim(&cpr->dim, dim_sample); 3135 } 3136 return work_done; 3137 } 3138 3139 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget) 3140 { 3141 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3142 int i, work_done = 0; 3143 3144 for (i = 0; i < cpr->cp_ring_count; i++) { 3145 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3146 3147 if (cpr2->had_nqe_notify) { 3148 work_done += __bnxt_poll_work(bp, cpr2, 3149 budget - work_done); 3150 cpr->has_more_work |= cpr2->has_more_work; 3151 } 3152 } 3153 return work_done; 3154 } 3155 3156 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi, 3157 u64 dbr_type, int budget) 3158 { 3159 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3160 int i; 3161 3162 for (i = 0; i < cpr->cp_ring_count; i++) { 3163 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i]; 3164 struct bnxt_db_info *db; 3165 3166 if (cpr2->had_work_done) { 3167 u32 tgl = 0; 3168 3169 if (dbr_type == DBR_TYPE_CQ_ARMALL) { 3170 cpr2->had_nqe_notify = 0; 3171 tgl = cpr2->toggle; 3172 } 3173 db = &cpr2->cp_db; 3174 bnxt_writeq(bp, 3175 db->db_key64 | dbr_type | DB_TOGGLE(tgl) | 3176 DB_RING_IDX(db, cpr2->cp_raw_cons), 3177 db->doorbell); 3178 cpr2->had_work_done = 0; 3179 } 3180 } 3181 __bnxt_poll_work_done(bp, bnapi, budget); 3182 } 3183 3184 static int bnxt_poll_p5(struct napi_struct *napi, int budget) 3185 { 3186 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi); 3187 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 3188 struct bnxt_cp_ring_info *cpr_rx; 3189 u32 raw_cons = cpr->cp_raw_cons; 3190 struct bnxt *bp = bnapi->bp; 3191 struct nqe_cn *nqcmp; 3192 int work_done = 0; 3193 u32 cons; 3194 3195 if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) { 3196 napi_complete(napi); 3197 return 0; 3198 } 3199 if (cpr->has_more_work) { 3200 cpr->has_more_work = 0; 3201 work_done = __bnxt_poll_cqs(bp, bnapi, budget); 3202 } 3203 while (1) { 3204 u16 type; 3205 3206 cons = RING_CMP(raw_cons); 3207 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)]; 3208 3209 if (!NQ_CMP_VALID(nqcmp, raw_cons)) { 3210 if (cpr->has_more_work) 3211 break; 3212 3213 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL, 3214 budget); 3215 cpr->cp_raw_cons = raw_cons; 3216 if (napi_complete_done(napi, work_done)) 3217 BNXT_DB_NQ_ARM_P5(&cpr->cp_db, 3218 cpr->cp_raw_cons); 3219 goto poll_done; 3220 } 3221 3222 /* The valid test of the entry must be done first before 3223 * reading any further. 3224 */ 3225 dma_rmb(); 3226 3227 type = le16_to_cpu(nqcmp->type); 3228 if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) { 3229 u32 idx = le32_to_cpu(nqcmp->cq_handle_low); 3230 u32 cq_type = BNXT_NQ_HDL_TYPE(idx); 3231 struct bnxt_cp_ring_info *cpr2; 3232 3233 /* No more budget for RX work */ 3234 if (budget && work_done >= budget && 3235 cq_type == BNXT_NQ_HDL_TYPE_RX) 3236 break; 3237 3238 idx = BNXT_NQ_HDL_IDX(idx); 3239 cpr2 = &cpr->cp_ring_arr[idx]; 3240 cpr2->had_nqe_notify = 1; 3241 cpr2->toggle = NQE_CN_TOGGLE(type); 3242 work_done += __bnxt_poll_work(bp, cpr2, 3243 budget - work_done); 3244 cpr->has_more_work |= cpr2->has_more_work; 3245 } else { 3246 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp); 3247 } 3248 raw_cons = NEXT_RAW_CMP(raw_cons); 3249 } 3250 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget); 3251 if (raw_cons != cpr->cp_raw_cons) { 3252 cpr->cp_raw_cons = raw_cons; 3253 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons); 3254 } 3255 poll_done: 3256 cpr_rx = &cpr->cp_ring_arr[0]; 3257 if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX && 3258 (bp->flags & BNXT_FLAG_DIM)) { 3259 struct dim_sample dim_sample = {}; 3260 3261 dim_update_sample(cpr->event_ctr, 3262 cpr_rx->rx_packets, 3263 cpr_rx->rx_bytes, 3264 &dim_sample); 3265 net_dim(&cpr->dim, dim_sample); 3266 } 3267 return work_done; 3268 } 3269 3270 static void bnxt_free_tx_skbs(struct bnxt *bp) 3271 { 3272 int i, max_idx; 3273 struct pci_dev *pdev = bp->pdev; 3274 3275 if (!bp->tx_ring) 3276 return; 3277 3278 max_idx = bp->tx_nr_pages * TX_DESC_CNT; 3279 for (i = 0; i < bp->tx_nr_rings; i++) { 3280 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3281 int j; 3282 3283 if (!txr->tx_buf_ring) 3284 continue; 3285 3286 for (j = 0; j < max_idx;) { 3287 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j]; 3288 struct sk_buff *skb; 3289 int k, last; 3290 3291 if (i < bp->tx_nr_rings_xdp && 3292 tx_buf->action == XDP_REDIRECT) { 3293 dma_unmap_single(&pdev->dev, 3294 dma_unmap_addr(tx_buf, mapping), 3295 dma_unmap_len(tx_buf, len), 3296 DMA_TO_DEVICE); 3297 xdp_return_frame(tx_buf->xdpf); 3298 tx_buf->action = 0; 3299 tx_buf->xdpf = NULL; 3300 j++; 3301 continue; 3302 } 3303 3304 skb = tx_buf->skb; 3305 if (!skb) { 3306 j++; 3307 continue; 3308 } 3309 3310 tx_buf->skb = NULL; 3311 3312 if (tx_buf->is_push) { 3313 dev_kfree_skb(skb); 3314 j += 2; 3315 continue; 3316 } 3317 3318 dma_unmap_single(&pdev->dev, 3319 dma_unmap_addr(tx_buf, mapping), 3320 skb_headlen(skb), 3321 DMA_TO_DEVICE); 3322 3323 last = tx_buf->nr_frags; 3324 j += 2; 3325 for (k = 0; k < last; k++, j++) { 3326 int ring_idx = j & bp->tx_ring_mask; 3327 skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 3328 3329 tx_buf = &txr->tx_buf_ring[ring_idx]; 3330 dma_unmap_page( 3331 &pdev->dev, 3332 dma_unmap_addr(tx_buf, mapping), 3333 skb_frag_size(frag), DMA_TO_DEVICE); 3334 } 3335 dev_kfree_skb(skb); 3336 } 3337 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i)); 3338 } 3339 } 3340 3341 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3342 { 3343 struct pci_dev *pdev = bp->pdev; 3344 int i, max_idx; 3345 3346 max_idx = bp->rx_nr_pages * RX_DESC_CNT; 3347 3348 for (i = 0; i < max_idx; i++) { 3349 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i]; 3350 dma_addr_t mapping = rx_buf->mapping; 3351 void *data = rx_buf->data; 3352 3353 if (!data) 3354 continue; 3355 3356 rx_buf->data = NULL; 3357 if (BNXT_RX_PAGE_MODE(bp)) { 3358 page_pool_recycle_direct(rxr->page_pool, data); 3359 } else { 3360 dma_unmap_single_attrs(&pdev->dev, mapping, 3361 bp->rx_buf_use_size, bp->rx_dir, 3362 DMA_ATTR_WEAK_ORDERING); 3363 skb_free_frag(data); 3364 } 3365 } 3366 } 3367 3368 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 3369 { 3370 int i, max_idx; 3371 3372 max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT; 3373 3374 for (i = 0; i < max_idx; i++) { 3375 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i]; 3376 struct page *page = rx_agg_buf->page; 3377 3378 if (!page) 3379 continue; 3380 3381 rx_agg_buf->page = NULL; 3382 __clear_bit(i, rxr->rx_agg_bmap); 3383 3384 page_pool_recycle_direct(rxr->page_pool, page); 3385 } 3386 } 3387 3388 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr) 3389 { 3390 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 3391 struct pci_dev *pdev = bp->pdev; 3392 struct bnxt_tpa_idx_map *map; 3393 int i; 3394 3395 if (!rxr->rx_tpa) 3396 goto skip_rx_tpa_free; 3397 3398 for (i = 0; i < bp->max_tpa; i++) { 3399 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i]; 3400 u8 *data = tpa_info->data; 3401 3402 if (!data) 3403 continue; 3404 3405 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping, 3406 bp->rx_buf_use_size, bp->rx_dir, 3407 DMA_ATTR_WEAK_ORDERING); 3408 3409 tpa_info->data = NULL; 3410 3411 skb_free_frag(data); 3412 } 3413 3414 skip_rx_tpa_free: 3415 if (!rxr->rx_buf_ring) 3416 goto skip_rx_buf_free; 3417 3418 bnxt_free_one_rx_ring(bp, rxr); 3419 3420 skip_rx_buf_free: 3421 if (!rxr->rx_agg_ring) 3422 goto skip_rx_agg_free; 3423 3424 bnxt_free_one_rx_agg_ring(bp, rxr); 3425 3426 skip_rx_agg_free: 3427 map = rxr->rx_tpa_idx_map; 3428 if (map) 3429 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap)); 3430 } 3431 3432 static void bnxt_free_rx_skbs(struct bnxt *bp) 3433 { 3434 int i; 3435 3436 if (!bp->rx_ring) 3437 return; 3438 3439 for (i = 0; i < bp->rx_nr_rings; i++) 3440 bnxt_free_one_rx_ring_skbs(bp, i); 3441 } 3442 3443 static void bnxt_free_skbs(struct bnxt *bp) 3444 { 3445 bnxt_free_tx_skbs(bp); 3446 bnxt_free_rx_skbs(bp); 3447 } 3448 3449 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len) 3450 { 3451 u8 init_val = ctxm->init_value; 3452 u16 offset = ctxm->init_offset; 3453 u8 *p2 = p; 3454 int i; 3455 3456 if (!init_val) 3457 return; 3458 if (offset == BNXT_CTX_INIT_INVALID_OFFSET) { 3459 memset(p, init_val, len); 3460 return; 3461 } 3462 for (i = 0; i < len; i += ctxm->entry_size) 3463 *(p2 + i + offset) = init_val; 3464 } 3465 3466 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3467 { 3468 struct pci_dev *pdev = bp->pdev; 3469 int i; 3470 3471 if (!rmem->pg_arr) 3472 goto skip_pages; 3473 3474 for (i = 0; i < rmem->nr_pages; i++) { 3475 if (!rmem->pg_arr[i]) 3476 continue; 3477 3478 dma_free_coherent(&pdev->dev, rmem->page_size, 3479 rmem->pg_arr[i], rmem->dma_arr[i]); 3480 3481 rmem->pg_arr[i] = NULL; 3482 } 3483 skip_pages: 3484 if (rmem->pg_tbl) { 3485 size_t pg_tbl_size = rmem->nr_pages * 8; 3486 3487 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3488 pg_tbl_size = rmem->page_size; 3489 dma_free_coherent(&pdev->dev, pg_tbl_size, 3490 rmem->pg_tbl, rmem->pg_tbl_map); 3491 rmem->pg_tbl = NULL; 3492 } 3493 if (rmem->vmem_size && *rmem->vmem) { 3494 vfree(*rmem->vmem); 3495 *rmem->vmem = NULL; 3496 } 3497 } 3498 3499 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem) 3500 { 3501 struct pci_dev *pdev = bp->pdev; 3502 u64 valid_bit = 0; 3503 int i; 3504 3505 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG)) 3506 valid_bit = PTU_PTE_VALID; 3507 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) { 3508 size_t pg_tbl_size = rmem->nr_pages * 8; 3509 3510 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG) 3511 pg_tbl_size = rmem->page_size; 3512 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size, 3513 &rmem->pg_tbl_map, 3514 GFP_KERNEL); 3515 if (!rmem->pg_tbl) 3516 return -ENOMEM; 3517 } 3518 3519 for (i = 0; i < rmem->nr_pages; i++) { 3520 u64 extra_bits = valid_bit; 3521 3522 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev, 3523 rmem->page_size, 3524 &rmem->dma_arr[i], 3525 GFP_KERNEL); 3526 if (!rmem->pg_arr[i]) 3527 return -ENOMEM; 3528 3529 if (rmem->ctx_mem) 3530 bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i], 3531 rmem->page_size); 3532 if (rmem->nr_pages > 1 || rmem->depth > 0) { 3533 if (i == rmem->nr_pages - 2 && 3534 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3535 extra_bits |= PTU_PTE_NEXT_TO_LAST; 3536 else if (i == rmem->nr_pages - 1 && 3537 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG)) 3538 extra_bits |= PTU_PTE_LAST; 3539 rmem->pg_tbl[i] = 3540 cpu_to_le64(rmem->dma_arr[i] | extra_bits); 3541 } 3542 } 3543 3544 if (rmem->vmem_size) { 3545 *rmem->vmem = vzalloc(rmem->vmem_size); 3546 if (!(*rmem->vmem)) 3547 return -ENOMEM; 3548 } 3549 return 0; 3550 } 3551 3552 static void bnxt_free_tpa_info(struct bnxt *bp) 3553 { 3554 int i, j; 3555 3556 for (i = 0; i < bp->rx_nr_rings; i++) { 3557 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3558 3559 kfree(rxr->rx_tpa_idx_map); 3560 rxr->rx_tpa_idx_map = NULL; 3561 if (rxr->rx_tpa) { 3562 for (j = 0; j < bp->max_tpa; j++) { 3563 kfree(rxr->rx_tpa[j].agg_arr); 3564 rxr->rx_tpa[j].agg_arr = NULL; 3565 } 3566 } 3567 kfree(rxr->rx_tpa); 3568 rxr->rx_tpa = NULL; 3569 } 3570 } 3571 3572 static int bnxt_alloc_tpa_info(struct bnxt *bp) 3573 { 3574 int i, j; 3575 3576 bp->max_tpa = MAX_TPA; 3577 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 3578 if (!bp->max_tpa_v2) 3579 return 0; 3580 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5); 3581 } 3582 3583 for (i = 0; i < bp->rx_nr_rings; i++) { 3584 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3585 struct rx_agg_cmp *agg; 3586 3587 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info), 3588 GFP_KERNEL); 3589 if (!rxr->rx_tpa) 3590 return -ENOMEM; 3591 3592 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3593 continue; 3594 for (j = 0; j < bp->max_tpa; j++) { 3595 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL); 3596 if (!agg) 3597 return -ENOMEM; 3598 rxr->rx_tpa[j].agg_arr = agg; 3599 } 3600 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map), 3601 GFP_KERNEL); 3602 if (!rxr->rx_tpa_idx_map) 3603 return -ENOMEM; 3604 } 3605 return 0; 3606 } 3607 3608 static void bnxt_free_rx_rings(struct bnxt *bp) 3609 { 3610 int i; 3611 3612 if (!bp->rx_ring) 3613 return; 3614 3615 bnxt_free_tpa_info(bp); 3616 for (i = 0; i < bp->rx_nr_rings; i++) { 3617 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3618 struct bnxt_ring_struct *ring; 3619 3620 if (rxr->xdp_prog) 3621 bpf_prog_put(rxr->xdp_prog); 3622 3623 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq)) 3624 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3625 3626 page_pool_destroy(rxr->page_pool); 3627 rxr->page_pool = NULL; 3628 3629 kfree(rxr->rx_agg_bmap); 3630 rxr->rx_agg_bmap = NULL; 3631 3632 ring = &rxr->rx_ring_struct; 3633 bnxt_free_ring(bp, &ring->ring_mem); 3634 3635 ring = &rxr->rx_agg_ring_struct; 3636 bnxt_free_ring(bp, &ring->ring_mem); 3637 } 3638 } 3639 3640 static int bnxt_alloc_rx_page_pool(struct bnxt *bp, 3641 struct bnxt_rx_ring_info *rxr, 3642 int numa_node) 3643 { 3644 struct page_pool_params pp = { 0 }; 3645 3646 pp.pool_size = bp->rx_agg_ring_size; 3647 if (BNXT_RX_PAGE_MODE(bp)) 3648 pp.pool_size += bp->rx_ring_size; 3649 pp.nid = numa_node; 3650 pp.napi = &rxr->bnapi->napi; 3651 pp.netdev = bp->dev; 3652 pp.dev = &bp->pdev->dev; 3653 pp.dma_dir = bp->rx_dir; 3654 pp.max_len = PAGE_SIZE; 3655 pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 3656 3657 rxr->page_pool = page_pool_create(&pp); 3658 if (IS_ERR(rxr->page_pool)) { 3659 int err = PTR_ERR(rxr->page_pool); 3660 3661 rxr->page_pool = NULL; 3662 return err; 3663 } 3664 return 0; 3665 } 3666 3667 static int bnxt_alloc_rx_rings(struct bnxt *bp) 3668 { 3669 int numa_node = dev_to_node(&bp->pdev->dev); 3670 int i, rc = 0, agg_rings = 0, cpu; 3671 3672 if (!bp->rx_ring) 3673 return -ENOMEM; 3674 3675 if (bp->flags & BNXT_FLAG_AGG_RINGS) 3676 agg_rings = 1; 3677 3678 for (i = 0; i < bp->rx_nr_rings; i++) { 3679 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 3680 struct bnxt_ring_struct *ring; 3681 int cpu_node; 3682 3683 ring = &rxr->rx_ring_struct; 3684 3685 cpu = cpumask_local_spread(i, numa_node); 3686 cpu_node = cpu_to_node(cpu); 3687 netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n", 3688 i, cpu_node); 3689 rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node); 3690 if (rc) 3691 return rc; 3692 3693 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0); 3694 if (rc < 0) 3695 return rc; 3696 3697 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq, 3698 MEM_TYPE_PAGE_POOL, 3699 rxr->page_pool); 3700 if (rc) { 3701 xdp_rxq_info_unreg(&rxr->xdp_rxq); 3702 return rc; 3703 } 3704 3705 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3706 if (rc) 3707 return rc; 3708 3709 ring->grp_idx = i; 3710 if (agg_rings) { 3711 u16 mem_size; 3712 3713 ring = &rxr->rx_agg_ring_struct; 3714 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3715 if (rc) 3716 return rc; 3717 3718 ring->grp_idx = i; 3719 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 3720 mem_size = rxr->rx_agg_bmap_size / 8; 3721 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 3722 if (!rxr->rx_agg_bmap) 3723 return -ENOMEM; 3724 } 3725 } 3726 if (bp->flags & BNXT_FLAG_TPA) 3727 rc = bnxt_alloc_tpa_info(bp); 3728 return rc; 3729 } 3730 3731 static void bnxt_free_tx_rings(struct bnxt *bp) 3732 { 3733 int i; 3734 struct pci_dev *pdev = bp->pdev; 3735 3736 if (!bp->tx_ring) 3737 return; 3738 3739 for (i = 0; i < bp->tx_nr_rings; i++) { 3740 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3741 struct bnxt_ring_struct *ring; 3742 3743 if (txr->tx_push) { 3744 dma_free_coherent(&pdev->dev, bp->tx_push_size, 3745 txr->tx_push, txr->tx_push_mapping); 3746 txr->tx_push = NULL; 3747 } 3748 3749 ring = &txr->tx_ring_struct; 3750 3751 bnxt_free_ring(bp, &ring->ring_mem); 3752 } 3753 } 3754 3755 #define BNXT_TC_TO_RING_BASE(bp, tc) \ 3756 ((tc) * (bp)->tx_nr_rings_per_tc) 3757 3758 #define BNXT_RING_TO_TC_OFF(bp, tx) \ 3759 ((tx) % (bp)->tx_nr_rings_per_tc) 3760 3761 #define BNXT_RING_TO_TC(bp, tx) \ 3762 ((tx) / (bp)->tx_nr_rings_per_tc) 3763 3764 static int bnxt_alloc_tx_rings(struct bnxt *bp) 3765 { 3766 int i, j, rc; 3767 struct pci_dev *pdev = bp->pdev; 3768 3769 bp->tx_push_size = 0; 3770 if (bp->tx_push_thresh) { 3771 int push_size; 3772 3773 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) + 3774 bp->tx_push_thresh); 3775 3776 if (push_size > 256) { 3777 push_size = 0; 3778 bp->tx_push_thresh = 0; 3779 } 3780 3781 bp->tx_push_size = push_size; 3782 } 3783 3784 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) { 3785 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 3786 struct bnxt_ring_struct *ring; 3787 u8 qidx; 3788 3789 ring = &txr->tx_ring_struct; 3790 3791 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3792 if (rc) 3793 return rc; 3794 3795 ring->grp_idx = txr->bnapi->index; 3796 if (bp->tx_push_size) { 3797 dma_addr_t mapping; 3798 3799 /* One pre-allocated DMA buffer to backup 3800 * TX push operation 3801 */ 3802 txr->tx_push = dma_alloc_coherent(&pdev->dev, 3803 bp->tx_push_size, 3804 &txr->tx_push_mapping, 3805 GFP_KERNEL); 3806 3807 if (!txr->tx_push) 3808 return -ENOMEM; 3809 3810 mapping = txr->tx_push_mapping + 3811 sizeof(struct tx_push_bd); 3812 txr->data_mapping = cpu_to_le64(mapping); 3813 } 3814 qidx = bp->tc_to_qidx[j]; 3815 ring->queue_id = bp->q_info[qidx].queue_id; 3816 spin_lock_init(&txr->xdp_tx_lock); 3817 if (i < bp->tx_nr_rings_xdp) 3818 continue; 3819 if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1)) 3820 j++; 3821 } 3822 return 0; 3823 } 3824 3825 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr) 3826 { 3827 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 3828 3829 kfree(cpr->cp_desc_ring); 3830 cpr->cp_desc_ring = NULL; 3831 ring->ring_mem.pg_arr = NULL; 3832 kfree(cpr->cp_desc_mapping); 3833 cpr->cp_desc_mapping = NULL; 3834 ring->ring_mem.dma_arr = NULL; 3835 } 3836 3837 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n) 3838 { 3839 cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL); 3840 if (!cpr->cp_desc_ring) 3841 return -ENOMEM; 3842 cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping), 3843 GFP_KERNEL); 3844 if (!cpr->cp_desc_mapping) 3845 return -ENOMEM; 3846 return 0; 3847 } 3848 3849 static void bnxt_free_all_cp_arrays(struct bnxt *bp) 3850 { 3851 int i; 3852 3853 if (!bp->bnapi) 3854 return; 3855 for (i = 0; i < bp->cp_nr_rings; i++) { 3856 struct bnxt_napi *bnapi = bp->bnapi[i]; 3857 3858 if (!bnapi) 3859 continue; 3860 bnxt_free_cp_arrays(&bnapi->cp_ring); 3861 } 3862 } 3863 3864 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp) 3865 { 3866 int i, n = bp->cp_nr_pages; 3867 3868 for (i = 0; i < bp->cp_nr_rings; i++) { 3869 struct bnxt_napi *bnapi = bp->bnapi[i]; 3870 int rc; 3871 3872 if (!bnapi) 3873 continue; 3874 rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n); 3875 if (rc) 3876 return rc; 3877 } 3878 return 0; 3879 } 3880 3881 static void bnxt_free_cp_rings(struct bnxt *bp) 3882 { 3883 int i; 3884 3885 if (!bp->bnapi) 3886 return; 3887 3888 for (i = 0; i < bp->cp_nr_rings; i++) { 3889 struct bnxt_napi *bnapi = bp->bnapi[i]; 3890 struct bnxt_cp_ring_info *cpr; 3891 struct bnxt_ring_struct *ring; 3892 int j; 3893 3894 if (!bnapi) 3895 continue; 3896 3897 cpr = &bnapi->cp_ring; 3898 ring = &cpr->cp_ring_struct; 3899 3900 bnxt_free_ring(bp, &ring->ring_mem); 3901 3902 if (!cpr->cp_ring_arr) 3903 continue; 3904 3905 for (j = 0; j < cpr->cp_ring_count; j++) { 3906 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 3907 3908 ring = &cpr2->cp_ring_struct; 3909 bnxt_free_ring(bp, &ring->ring_mem); 3910 bnxt_free_cp_arrays(cpr2); 3911 } 3912 kfree(cpr->cp_ring_arr); 3913 cpr->cp_ring_arr = NULL; 3914 cpr->cp_ring_count = 0; 3915 } 3916 } 3917 3918 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp, 3919 struct bnxt_cp_ring_info *cpr) 3920 { 3921 struct bnxt_ring_mem_info *rmem; 3922 struct bnxt_ring_struct *ring; 3923 int rc; 3924 3925 rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages); 3926 if (rc) { 3927 bnxt_free_cp_arrays(cpr); 3928 return -ENOMEM; 3929 } 3930 ring = &cpr->cp_ring_struct; 3931 rmem = &ring->ring_mem; 3932 rmem->nr_pages = bp->cp_nr_pages; 3933 rmem->page_size = HW_CMPD_RING_SIZE; 3934 rmem->pg_arr = (void **)cpr->cp_desc_ring; 3935 rmem->dma_arr = cpr->cp_desc_mapping; 3936 rmem->flags = BNXT_RMEM_RING_PTE_FLAG; 3937 rc = bnxt_alloc_ring(bp, rmem); 3938 if (rc) { 3939 bnxt_free_ring(bp, rmem); 3940 bnxt_free_cp_arrays(cpr); 3941 } 3942 return rc; 3943 } 3944 3945 static int bnxt_alloc_cp_rings(struct bnxt *bp) 3946 { 3947 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS); 3948 int i, j, rc, ulp_msix; 3949 int tcs = bp->num_tc; 3950 3951 if (!tcs) 3952 tcs = 1; 3953 ulp_msix = bnxt_get_ulp_msix_num(bp); 3954 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 3955 struct bnxt_napi *bnapi = bp->bnapi[i]; 3956 struct bnxt_cp_ring_info *cpr, *cpr2; 3957 struct bnxt_ring_struct *ring; 3958 int cp_count = 0, k; 3959 int rx = 0, tx = 0; 3960 3961 if (!bnapi) 3962 continue; 3963 3964 cpr = &bnapi->cp_ring; 3965 cpr->bnapi = bnapi; 3966 ring = &cpr->cp_ring_struct; 3967 3968 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 3969 if (rc) 3970 return rc; 3971 3972 ring->map_idx = ulp_msix + i; 3973 3974 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 3975 continue; 3976 3977 if (i < bp->rx_nr_rings) { 3978 cp_count++; 3979 rx = 1; 3980 } 3981 if (i < bp->tx_nr_rings_xdp) { 3982 cp_count++; 3983 tx = 1; 3984 } else if ((sh && i < bp->tx_nr_rings) || 3985 (!sh && i >= bp->rx_nr_rings)) { 3986 cp_count += tcs; 3987 tx = 1; 3988 } 3989 3990 cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr), 3991 GFP_KERNEL); 3992 if (!cpr->cp_ring_arr) 3993 return -ENOMEM; 3994 cpr->cp_ring_count = cp_count; 3995 3996 for (k = 0; k < cp_count; k++) { 3997 cpr2 = &cpr->cp_ring_arr[k]; 3998 rc = bnxt_alloc_cp_sub_ring(bp, cpr2); 3999 if (rc) 4000 return rc; 4001 cpr2->bnapi = bnapi; 4002 cpr2->sw_stats = cpr->sw_stats; 4003 cpr2->cp_idx = k; 4004 if (!k && rx) { 4005 bp->rx_ring[i].rx_cpr = cpr2; 4006 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX; 4007 } else { 4008 int n, tc = k - rx; 4009 4010 n = BNXT_TC_TO_RING_BASE(bp, tc) + j; 4011 bp->tx_ring[n].tx_cpr = cpr2; 4012 cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX; 4013 } 4014 } 4015 if (tx) 4016 j++; 4017 } 4018 return 0; 4019 } 4020 4021 static void bnxt_init_rx_ring_struct(struct bnxt *bp, 4022 struct bnxt_rx_ring_info *rxr) 4023 { 4024 struct bnxt_ring_mem_info *rmem; 4025 struct bnxt_ring_struct *ring; 4026 4027 ring = &rxr->rx_ring_struct; 4028 rmem = &ring->ring_mem; 4029 rmem->nr_pages = bp->rx_nr_pages; 4030 rmem->page_size = HW_RXBD_RING_SIZE; 4031 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4032 rmem->dma_arr = rxr->rx_desc_mapping; 4033 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4034 rmem->vmem = (void **)&rxr->rx_buf_ring; 4035 4036 ring = &rxr->rx_agg_ring_struct; 4037 rmem = &ring->ring_mem; 4038 rmem->nr_pages = bp->rx_agg_nr_pages; 4039 rmem->page_size = HW_RXBD_RING_SIZE; 4040 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4041 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4042 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4043 rmem->vmem = (void **)&rxr->rx_agg_ring; 4044 } 4045 4046 static void bnxt_reset_rx_ring_struct(struct bnxt *bp, 4047 struct bnxt_rx_ring_info *rxr) 4048 { 4049 struct bnxt_ring_mem_info *rmem; 4050 struct bnxt_ring_struct *ring; 4051 int i; 4052 4053 rxr->page_pool->p.napi = NULL; 4054 rxr->page_pool = NULL; 4055 memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info)); 4056 4057 ring = &rxr->rx_ring_struct; 4058 rmem = &ring->ring_mem; 4059 rmem->pg_tbl = NULL; 4060 rmem->pg_tbl_map = 0; 4061 for (i = 0; i < rmem->nr_pages; i++) { 4062 rmem->pg_arr[i] = NULL; 4063 rmem->dma_arr[i] = 0; 4064 } 4065 *rmem->vmem = NULL; 4066 4067 ring = &rxr->rx_agg_ring_struct; 4068 rmem = &ring->ring_mem; 4069 rmem->pg_tbl = NULL; 4070 rmem->pg_tbl_map = 0; 4071 for (i = 0; i < rmem->nr_pages; i++) { 4072 rmem->pg_arr[i] = NULL; 4073 rmem->dma_arr[i] = 0; 4074 } 4075 *rmem->vmem = NULL; 4076 } 4077 4078 static void bnxt_init_ring_struct(struct bnxt *bp) 4079 { 4080 int i, j; 4081 4082 for (i = 0; i < bp->cp_nr_rings; i++) { 4083 struct bnxt_napi *bnapi = bp->bnapi[i]; 4084 struct bnxt_ring_mem_info *rmem; 4085 struct bnxt_cp_ring_info *cpr; 4086 struct bnxt_rx_ring_info *rxr; 4087 struct bnxt_tx_ring_info *txr; 4088 struct bnxt_ring_struct *ring; 4089 4090 if (!bnapi) 4091 continue; 4092 4093 cpr = &bnapi->cp_ring; 4094 ring = &cpr->cp_ring_struct; 4095 rmem = &ring->ring_mem; 4096 rmem->nr_pages = bp->cp_nr_pages; 4097 rmem->page_size = HW_CMPD_RING_SIZE; 4098 rmem->pg_arr = (void **)cpr->cp_desc_ring; 4099 rmem->dma_arr = cpr->cp_desc_mapping; 4100 rmem->vmem_size = 0; 4101 4102 rxr = bnapi->rx_ring; 4103 if (!rxr) 4104 goto skip_rx; 4105 4106 ring = &rxr->rx_ring_struct; 4107 rmem = &ring->ring_mem; 4108 rmem->nr_pages = bp->rx_nr_pages; 4109 rmem->page_size = HW_RXBD_RING_SIZE; 4110 rmem->pg_arr = (void **)rxr->rx_desc_ring; 4111 rmem->dma_arr = rxr->rx_desc_mapping; 4112 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages; 4113 rmem->vmem = (void **)&rxr->rx_buf_ring; 4114 4115 ring = &rxr->rx_agg_ring_struct; 4116 rmem = &ring->ring_mem; 4117 rmem->nr_pages = bp->rx_agg_nr_pages; 4118 rmem->page_size = HW_RXBD_RING_SIZE; 4119 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring; 4120 rmem->dma_arr = rxr->rx_agg_desc_mapping; 4121 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages; 4122 rmem->vmem = (void **)&rxr->rx_agg_ring; 4123 4124 skip_rx: 4125 bnxt_for_each_napi_tx(j, bnapi, txr) { 4126 ring = &txr->tx_ring_struct; 4127 rmem = &ring->ring_mem; 4128 rmem->nr_pages = bp->tx_nr_pages; 4129 rmem->page_size = HW_TXBD_RING_SIZE; 4130 rmem->pg_arr = (void **)txr->tx_desc_ring; 4131 rmem->dma_arr = txr->tx_desc_mapping; 4132 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages; 4133 rmem->vmem = (void **)&txr->tx_buf_ring; 4134 } 4135 } 4136 } 4137 4138 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type) 4139 { 4140 int i; 4141 u32 prod; 4142 struct rx_bd **rx_buf_ring; 4143 4144 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr; 4145 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) { 4146 int j; 4147 struct rx_bd *rxbd; 4148 4149 rxbd = rx_buf_ring[i]; 4150 if (!rxbd) 4151 continue; 4152 4153 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) { 4154 rxbd->rx_bd_len_flags_type = cpu_to_le32(type); 4155 rxbd->rx_bd_opaque = prod; 4156 } 4157 } 4158 } 4159 4160 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp, 4161 struct bnxt_rx_ring_info *rxr, 4162 int ring_nr) 4163 { 4164 u32 prod; 4165 int i; 4166 4167 prod = rxr->rx_prod; 4168 for (i = 0; i < bp->rx_ring_size; i++) { 4169 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) { 4170 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n", 4171 ring_nr, i, bp->rx_ring_size); 4172 break; 4173 } 4174 prod = NEXT_RX(prod); 4175 } 4176 rxr->rx_prod = prod; 4177 } 4178 4179 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp, 4180 struct bnxt_rx_ring_info *rxr, 4181 int ring_nr) 4182 { 4183 u32 prod; 4184 int i; 4185 4186 prod = rxr->rx_agg_prod; 4187 for (i = 0; i < bp->rx_agg_ring_size; i++) { 4188 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) { 4189 netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n", 4190 ring_nr, i, bp->rx_ring_size); 4191 break; 4192 } 4193 prod = NEXT_RX_AGG(prod); 4194 } 4195 rxr->rx_agg_prod = prod; 4196 } 4197 4198 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr) 4199 { 4200 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 4201 int i; 4202 4203 bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr); 4204 4205 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 4206 return 0; 4207 4208 bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr); 4209 4210 if (rxr->rx_tpa) { 4211 dma_addr_t mapping; 4212 u8 *data; 4213 4214 for (i = 0; i < bp->max_tpa; i++) { 4215 data = __bnxt_alloc_rx_frag(bp, &mapping, GFP_KERNEL); 4216 if (!data) 4217 return -ENOMEM; 4218 4219 rxr->rx_tpa[i].data = data; 4220 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset; 4221 rxr->rx_tpa[i].mapping = mapping; 4222 } 4223 } 4224 return 0; 4225 } 4226 4227 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp, 4228 struct bnxt_rx_ring_info *rxr) 4229 { 4230 struct bnxt_ring_struct *ring; 4231 u32 type; 4232 4233 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) | 4234 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP; 4235 4236 if (NET_IP_ALIGN == 2) 4237 type |= RX_BD_FLAGS_SOP; 4238 4239 ring = &rxr->rx_ring_struct; 4240 bnxt_init_rxbd_pages(ring, type); 4241 ring->fw_ring_id = INVALID_HW_RING_ID; 4242 } 4243 4244 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp, 4245 struct bnxt_rx_ring_info *rxr) 4246 { 4247 struct bnxt_ring_struct *ring; 4248 u32 type; 4249 4250 ring = &rxr->rx_agg_ring_struct; 4251 ring->fw_ring_id = INVALID_HW_RING_ID; 4252 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) { 4253 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) | 4254 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP; 4255 4256 bnxt_init_rxbd_pages(ring, type); 4257 } 4258 } 4259 4260 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr) 4261 { 4262 struct bnxt_rx_ring_info *rxr; 4263 4264 rxr = &bp->rx_ring[ring_nr]; 4265 bnxt_init_one_rx_ring_rxbd(bp, rxr); 4266 4267 netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX, 4268 &rxr->bnapi->napi); 4269 4270 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) { 4271 bpf_prog_add(bp->xdp_prog, 1); 4272 rxr->xdp_prog = bp->xdp_prog; 4273 } 4274 4275 bnxt_init_one_rx_agg_ring_rxbd(bp, rxr); 4276 4277 return bnxt_alloc_one_rx_ring(bp, ring_nr); 4278 } 4279 4280 static void bnxt_init_cp_rings(struct bnxt *bp) 4281 { 4282 int i, j; 4283 4284 for (i = 0; i < bp->cp_nr_rings; i++) { 4285 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring; 4286 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 4287 4288 ring->fw_ring_id = INVALID_HW_RING_ID; 4289 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4290 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4291 if (!cpr->cp_ring_arr) 4292 continue; 4293 for (j = 0; j < cpr->cp_ring_count; j++) { 4294 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 4295 4296 ring = &cpr2->cp_ring_struct; 4297 ring->fw_ring_id = INVALID_HW_RING_ID; 4298 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks; 4299 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs; 4300 } 4301 } 4302 } 4303 4304 static int bnxt_init_rx_rings(struct bnxt *bp) 4305 { 4306 int i, rc = 0; 4307 4308 if (BNXT_RX_PAGE_MODE(bp)) { 4309 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM; 4310 bp->rx_dma_offset = XDP_PACKET_HEADROOM; 4311 } else { 4312 bp->rx_offset = BNXT_RX_OFFSET; 4313 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET; 4314 } 4315 4316 for (i = 0; i < bp->rx_nr_rings; i++) { 4317 rc = bnxt_init_one_rx_ring(bp, i); 4318 if (rc) 4319 break; 4320 } 4321 4322 return rc; 4323 } 4324 4325 static int bnxt_init_tx_rings(struct bnxt *bp) 4326 { 4327 u16 i; 4328 4329 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2, 4330 BNXT_MIN_TX_DESC_CNT); 4331 4332 for (i = 0; i < bp->tx_nr_rings; i++) { 4333 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 4334 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 4335 4336 ring->fw_ring_id = INVALID_HW_RING_ID; 4337 4338 if (i >= bp->tx_nr_rings_xdp) 4339 netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp, 4340 NETDEV_QUEUE_TYPE_TX, 4341 &txr->bnapi->napi); 4342 } 4343 4344 return 0; 4345 } 4346 4347 static void bnxt_free_ring_grps(struct bnxt *bp) 4348 { 4349 kfree(bp->grp_info); 4350 bp->grp_info = NULL; 4351 } 4352 4353 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init) 4354 { 4355 int i; 4356 4357 if (irq_re_init) { 4358 bp->grp_info = kcalloc(bp->cp_nr_rings, 4359 sizeof(struct bnxt_ring_grp_info), 4360 GFP_KERNEL); 4361 if (!bp->grp_info) 4362 return -ENOMEM; 4363 } 4364 for (i = 0; i < bp->cp_nr_rings; i++) { 4365 if (irq_re_init) 4366 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID; 4367 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 4368 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID; 4369 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID; 4370 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 4371 } 4372 return 0; 4373 } 4374 4375 static void bnxt_free_vnics(struct bnxt *bp) 4376 { 4377 kfree(bp->vnic_info); 4378 bp->vnic_info = NULL; 4379 bp->nr_vnics = 0; 4380 } 4381 4382 static int bnxt_alloc_vnics(struct bnxt *bp) 4383 { 4384 int num_vnics = 1; 4385 4386 #ifdef CONFIG_RFS_ACCEL 4387 if (bp->flags & BNXT_FLAG_RFS) { 4388 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 4389 num_vnics++; 4390 else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4391 num_vnics += bp->rx_nr_rings; 4392 } 4393 #endif 4394 4395 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 4396 num_vnics++; 4397 4398 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info), 4399 GFP_KERNEL); 4400 if (!bp->vnic_info) 4401 return -ENOMEM; 4402 4403 bp->nr_vnics = num_vnics; 4404 return 0; 4405 } 4406 4407 static void bnxt_init_vnics(struct bnxt *bp) 4408 { 4409 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 4410 int i; 4411 4412 for (i = 0; i < bp->nr_vnics; i++) { 4413 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 4414 int j; 4415 4416 vnic->fw_vnic_id = INVALID_HW_RING_ID; 4417 vnic->vnic_id = i; 4418 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) 4419 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID; 4420 4421 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID; 4422 4423 if (bp->vnic_info[i].rss_hash_key) { 4424 if (i == BNXT_VNIC_DEFAULT) { 4425 u8 *key = (void *)vnic->rss_hash_key; 4426 int k; 4427 4428 if (!bp->rss_hash_key_valid && 4429 !bp->rss_hash_key_updated) { 4430 get_random_bytes(bp->rss_hash_key, 4431 HW_HASH_KEY_SIZE); 4432 bp->rss_hash_key_updated = true; 4433 } 4434 4435 memcpy(vnic->rss_hash_key, bp->rss_hash_key, 4436 HW_HASH_KEY_SIZE); 4437 4438 if (!bp->rss_hash_key_updated) 4439 continue; 4440 4441 bp->rss_hash_key_updated = false; 4442 bp->rss_hash_key_valid = true; 4443 4444 bp->toeplitz_prefix = 0; 4445 for (k = 0; k < 8; k++) { 4446 bp->toeplitz_prefix <<= 8; 4447 bp->toeplitz_prefix |= key[k]; 4448 } 4449 } else { 4450 memcpy(vnic->rss_hash_key, vnic0->rss_hash_key, 4451 HW_HASH_KEY_SIZE); 4452 } 4453 } 4454 } 4455 } 4456 4457 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg) 4458 { 4459 int pages; 4460 4461 pages = ring_size / desc_per_pg; 4462 4463 if (!pages) 4464 return 1; 4465 4466 pages++; 4467 4468 while (pages & (pages - 1)) 4469 pages++; 4470 4471 return pages; 4472 } 4473 4474 void bnxt_set_tpa_flags(struct bnxt *bp) 4475 { 4476 bp->flags &= ~BNXT_FLAG_TPA; 4477 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 4478 return; 4479 if (bp->dev->features & NETIF_F_LRO) 4480 bp->flags |= BNXT_FLAG_LRO; 4481 else if (bp->dev->features & NETIF_F_GRO_HW) 4482 bp->flags |= BNXT_FLAG_GRO; 4483 } 4484 4485 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must 4486 * be set on entry. 4487 */ 4488 void bnxt_set_ring_params(struct bnxt *bp) 4489 { 4490 u32 ring_size, rx_size, rx_space, max_rx_cmpl; 4491 u32 agg_factor = 0, agg_ring_size = 0; 4492 4493 /* 8 for CRC and VLAN */ 4494 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8); 4495 4496 rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) + 4497 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4498 4499 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH; 4500 ring_size = bp->rx_ring_size; 4501 bp->rx_agg_ring_size = 0; 4502 bp->rx_agg_nr_pages = 0; 4503 4504 if (bp->flags & BNXT_FLAG_TPA) 4505 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE); 4506 4507 bp->flags &= ~BNXT_FLAG_JUMBO; 4508 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) { 4509 u32 jumbo_factor; 4510 4511 bp->flags |= BNXT_FLAG_JUMBO; 4512 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT; 4513 if (jumbo_factor > agg_factor) 4514 agg_factor = jumbo_factor; 4515 } 4516 if (agg_factor) { 4517 if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) { 4518 ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA; 4519 netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n", 4520 bp->rx_ring_size, ring_size); 4521 bp->rx_ring_size = ring_size; 4522 } 4523 agg_ring_size = ring_size * agg_factor; 4524 4525 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size, 4526 RX_DESC_CNT); 4527 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) { 4528 u32 tmp = agg_ring_size; 4529 4530 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES; 4531 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1; 4532 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n", 4533 tmp, agg_ring_size); 4534 } 4535 bp->rx_agg_ring_size = agg_ring_size; 4536 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1; 4537 4538 if (BNXT_RX_PAGE_MODE(bp)) { 4539 rx_space = PAGE_SIZE; 4540 rx_size = PAGE_SIZE - 4541 ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) - 4542 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4543 } else { 4544 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN); 4545 rx_space = rx_size + NET_SKB_PAD + 4546 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4547 } 4548 } 4549 4550 bp->rx_buf_use_size = rx_size; 4551 bp->rx_buf_size = rx_space; 4552 4553 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT); 4554 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1; 4555 4556 ring_size = bp->tx_ring_size; 4557 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT); 4558 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1; 4559 4560 max_rx_cmpl = bp->rx_ring_size; 4561 /* MAX TPA needs to be added because TPA_START completions are 4562 * immediately recycled, so the TPA completions are not bound by 4563 * the RX ring size. 4564 */ 4565 if (bp->flags & BNXT_FLAG_TPA) 4566 max_rx_cmpl += bp->max_tpa; 4567 /* RX and TPA completions are 32-byte, all others are 16-byte */ 4568 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size; 4569 bp->cp_ring_size = ring_size; 4570 4571 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT); 4572 if (bp->cp_nr_pages > MAX_CP_PAGES) { 4573 bp->cp_nr_pages = MAX_CP_PAGES; 4574 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1; 4575 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n", 4576 ring_size, bp->cp_ring_size); 4577 } 4578 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT; 4579 bp->cp_ring_mask = bp->cp_bit - 1; 4580 } 4581 4582 /* Changing allocation mode of RX rings. 4583 * TODO: Update when extending xdp_rxq_info to support allocation modes. 4584 */ 4585 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode) 4586 { 4587 struct net_device *dev = bp->dev; 4588 4589 if (page_mode) { 4590 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 4591 bp->flags |= BNXT_FLAG_RX_PAGE_MODE; 4592 4593 if (bp->xdp_prog->aux->xdp_has_frags) 4594 dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU); 4595 else 4596 dev->max_mtu = 4597 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU); 4598 if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 4599 bp->flags |= BNXT_FLAG_JUMBO; 4600 bp->rx_skb_func = bnxt_rx_multi_page_skb; 4601 } else { 4602 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 4603 bp->rx_skb_func = bnxt_rx_page_skb; 4604 } 4605 bp->rx_dir = DMA_BIDIRECTIONAL; 4606 /* Disable LRO or GRO_HW */ 4607 netdev_update_features(dev); 4608 } else { 4609 dev->max_mtu = bp->max_mtu; 4610 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE; 4611 bp->rx_dir = DMA_FROM_DEVICE; 4612 bp->rx_skb_func = bnxt_rx_skb; 4613 } 4614 return 0; 4615 } 4616 4617 static void bnxt_free_vnic_attributes(struct bnxt *bp) 4618 { 4619 int i; 4620 struct bnxt_vnic_info *vnic; 4621 struct pci_dev *pdev = bp->pdev; 4622 4623 if (!bp->vnic_info) 4624 return; 4625 4626 for (i = 0; i < bp->nr_vnics; i++) { 4627 vnic = &bp->vnic_info[i]; 4628 4629 kfree(vnic->fw_grp_ids); 4630 vnic->fw_grp_ids = NULL; 4631 4632 kfree(vnic->uc_list); 4633 vnic->uc_list = NULL; 4634 4635 if (vnic->mc_list) { 4636 dma_free_coherent(&pdev->dev, vnic->mc_list_size, 4637 vnic->mc_list, vnic->mc_list_mapping); 4638 vnic->mc_list = NULL; 4639 } 4640 4641 if (vnic->rss_table) { 4642 dma_free_coherent(&pdev->dev, vnic->rss_table_size, 4643 vnic->rss_table, 4644 vnic->rss_table_dma_addr); 4645 vnic->rss_table = NULL; 4646 } 4647 4648 vnic->rss_hash_key = NULL; 4649 vnic->flags = 0; 4650 } 4651 } 4652 4653 static int bnxt_alloc_vnic_attributes(struct bnxt *bp) 4654 { 4655 int i, rc = 0, size; 4656 struct bnxt_vnic_info *vnic; 4657 struct pci_dev *pdev = bp->pdev; 4658 int max_rings; 4659 4660 for (i = 0; i < bp->nr_vnics; i++) { 4661 vnic = &bp->vnic_info[i]; 4662 4663 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) { 4664 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN; 4665 4666 if (mem_size > 0) { 4667 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL); 4668 if (!vnic->uc_list) { 4669 rc = -ENOMEM; 4670 goto out; 4671 } 4672 } 4673 } 4674 4675 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) { 4676 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN; 4677 vnic->mc_list = 4678 dma_alloc_coherent(&pdev->dev, 4679 vnic->mc_list_size, 4680 &vnic->mc_list_mapping, 4681 GFP_KERNEL); 4682 if (!vnic->mc_list) { 4683 rc = -ENOMEM; 4684 goto out; 4685 } 4686 } 4687 4688 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4689 goto vnic_skip_grps; 4690 4691 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 4692 max_rings = bp->rx_nr_rings; 4693 else 4694 max_rings = 1; 4695 4696 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL); 4697 if (!vnic->fw_grp_ids) { 4698 rc = -ENOMEM; 4699 goto out; 4700 } 4701 vnic_skip_grps: 4702 if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && 4703 !(vnic->flags & BNXT_VNIC_RSS_FLAG)) 4704 continue; 4705 4706 /* Allocate rss table and hash key */ 4707 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16)); 4708 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4709 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5); 4710 4711 vnic->rss_table_size = size + HW_HASH_KEY_SIZE; 4712 vnic->rss_table = dma_alloc_coherent(&pdev->dev, 4713 vnic->rss_table_size, 4714 &vnic->rss_table_dma_addr, 4715 GFP_KERNEL); 4716 if (!vnic->rss_table) { 4717 rc = -ENOMEM; 4718 goto out; 4719 } 4720 4721 vnic->rss_hash_key = ((void *)vnic->rss_table) + size; 4722 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size; 4723 } 4724 return 0; 4725 4726 out: 4727 return rc; 4728 } 4729 4730 static void bnxt_free_hwrm_resources(struct bnxt *bp) 4731 { 4732 struct bnxt_hwrm_wait_token *token; 4733 4734 dma_pool_destroy(bp->hwrm_dma_pool); 4735 bp->hwrm_dma_pool = NULL; 4736 4737 rcu_read_lock(); 4738 hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node) 4739 WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED); 4740 rcu_read_unlock(); 4741 } 4742 4743 static int bnxt_alloc_hwrm_resources(struct bnxt *bp) 4744 { 4745 bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev, 4746 BNXT_HWRM_DMA_SIZE, 4747 BNXT_HWRM_DMA_ALIGN, 0); 4748 if (!bp->hwrm_dma_pool) 4749 return -ENOMEM; 4750 4751 INIT_HLIST_HEAD(&bp->hwrm_pending_list); 4752 4753 return 0; 4754 } 4755 4756 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats) 4757 { 4758 kfree(stats->hw_masks); 4759 stats->hw_masks = NULL; 4760 kfree(stats->sw_stats); 4761 stats->sw_stats = NULL; 4762 if (stats->hw_stats) { 4763 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats, 4764 stats->hw_stats_map); 4765 stats->hw_stats = NULL; 4766 } 4767 } 4768 4769 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats, 4770 bool alloc_masks) 4771 { 4772 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len, 4773 &stats->hw_stats_map, GFP_KERNEL); 4774 if (!stats->hw_stats) 4775 return -ENOMEM; 4776 4777 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL); 4778 if (!stats->sw_stats) 4779 goto stats_mem_err; 4780 4781 if (alloc_masks) { 4782 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL); 4783 if (!stats->hw_masks) 4784 goto stats_mem_err; 4785 } 4786 return 0; 4787 4788 stats_mem_err: 4789 bnxt_free_stats_mem(bp, stats); 4790 return -ENOMEM; 4791 } 4792 4793 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count) 4794 { 4795 int i; 4796 4797 for (i = 0; i < count; i++) 4798 mask_arr[i] = mask; 4799 } 4800 4801 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count) 4802 { 4803 int i; 4804 4805 for (i = 0; i < count; i++) 4806 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]); 4807 } 4808 4809 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp, 4810 struct bnxt_stats_mem *stats) 4811 { 4812 struct hwrm_func_qstats_ext_output *resp; 4813 struct hwrm_func_qstats_ext_input *req; 4814 __le64 *hw_masks; 4815 int rc; 4816 4817 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) || 4818 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 4819 return -EOPNOTSUPP; 4820 4821 rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT); 4822 if (rc) 4823 return rc; 4824 4825 req->fid = cpu_to_le16(0xffff); 4826 req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4827 4828 resp = hwrm_req_hold(bp, req); 4829 rc = hwrm_req_send(bp, req); 4830 if (!rc) { 4831 hw_masks = &resp->rx_ucast_pkts; 4832 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8); 4833 } 4834 hwrm_req_drop(bp, req); 4835 return rc; 4836 } 4837 4838 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags); 4839 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags); 4840 4841 static void bnxt_init_stats(struct bnxt *bp) 4842 { 4843 struct bnxt_napi *bnapi = bp->bnapi[0]; 4844 struct bnxt_cp_ring_info *cpr; 4845 struct bnxt_stats_mem *stats; 4846 __le64 *rx_stats, *tx_stats; 4847 int rc, rx_count, tx_count; 4848 u64 *rx_masks, *tx_masks; 4849 u64 mask; 4850 u8 flags; 4851 4852 cpr = &bnapi->cp_ring; 4853 stats = &cpr->stats; 4854 rc = bnxt_hwrm_func_qstat_ext(bp, stats); 4855 if (rc) { 4856 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 4857 mask = (1ULL << 48) - 1; 4858 else 4859 mask = -1ULL; 4860 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8); 4861 } 4862 if (bp->flags & BNXT_FLAG_PORT_STATS) { 4863 stats = &bp->port_stats; 4864 rx_stats = stats->hw_stats; 4865 rx_masks = stats->hw_masks; 4866 rx_count = sizeof(struct rx_port_stats) / 8; 4867 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4868 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 4869 tx_count = sizeof(struct tx_port_stats) / 8; 4870 4871 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK; 4872 rc = bnxt_hwrm_port_qstats(bp, flags); 4873 if (rc) { 4874 mask = (1ULL << 40) - 1; 4875 4876 bnxt_fill_masks(rx_masks, mask, rx_count); 4877 bnxt_fill_masks(tx_masks, mask, tx_count); 4878 } else { 4879 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4880 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count); 4881 bnxt_hwrm_port_qstats(bp, 0); 4882 } 4883 } 4884 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 4885 stats = &bp->rx_port_stats_ext; 4886 rx_stats = stats->hw_stats; 4887 rx_masks = stats->hw_masks; 4888 rx_count = sizeof(struct rx_port_stats_ext) / 8; 4889 stats = &bp->tx_port_stats_ext; 4890 tx_stats = stats->hw_stats; 4891 tx_masks = stats->hw_masks; 4892 tx_count = sizeof(struct tx_port_stats_ext) / 8; 4893 4894 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK; 4895 rc = bnxt_hwrm_port_qstats_ext(bp, flags); 4896 if (rc) { 4897 mask = (1ULL << 40) - 1; 4898 4899 bnxt_fill_masks(rx_masks, mask, rx_count); 4900 if (tx_stats) 4901 bnxt_fill_masks(tx_masks, mask, tx_count); 4902 } else { 4903 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count); 4904 if (tx_stats) 4905 bnxt_copy_hw_masks(tx_masks, tx_stats, 4906 tx_count); 4907 bnxt_hwrm_port_qstats_ext(bp, 0); 4908 } 4909 } 4910 } 4911 4912 static void bnxt_free_port_stats(struct bnxt *bp) 4913 { 4914 bp->flags &= ~BNXT_FLAG_PORT_STATS; 4915 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT; 4916 4917 bnxt_free_stats_mem(bp, &bp->port_stats); 4918 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext); 4919 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext); 4920 } 4921 4922 static void bnxt_free_ring_stats(struct bnxt *bp) 4923 { 4924 int i; 4925 4926 if (!bp->bnapi) 4927 return; 4928 4929 for (i = 0; i < bp->cp_nr_rings; i++) { 4930 struct bnxt_napi *bnapi = bp->bnapi[i]; 4931 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4932 4933 bnxt_free_stats_mem(bp, &cpr->stats); 4934 4935 kfree(cpr->sw_stats); 4936 cpr->sw_stats = NULL; 4937 } 4938 } 4939 4940 static int bnxt_alloc_stats(struct bnxt *bp) 4941 { 4942 u32 size, i; 4943 int rc; 4944 4945 size = bp->hw_ring_stats_size; 4946 4947 for (i = 0; i < bp->cp_nr_rings; i++) { 4948 struct bnxt_napi *bnapi = bp->bnapi[i]; 4949 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 4950 4951 cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL); 4952 if (!cpr->sw_stats) 4953 return -ENOMEM; 4954 4955 cpr->stats.len = size; 4956 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i); 4957 if (rc) 4958 return rc; 4959 4960 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 4961 } 4962 4963 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700) 4964 return 0; 4965 4966 if (bp->port_stats.hw_stats) 4967 goto alloc_ext_stats; 4968 4969 bp->port_stats.len = BNXT_PORT_STATS_SIZE; 4970 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true); 4971 if (rc) 4972 return rc; 4973 4974 bp->flags |= BNXT_FLAG_PORT_STATS; 4975 4976 alloc_ext_stats: 4977 /* Display extended statistics only if FW supports it */ 4978 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900) 4979 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) 4980 return 0; 4981 4982 if (bp->rx_port_stats_ext.hw_stats) 4983 goto alloc_tx_ext_stats; 4984 4985 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext); 4986 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true); 4987 /* Extended stats are optional */ 4988 if (rc) 4989 return 0; 4990 4991 alloc_tx_ext_stats: 4992 if (bp->tx_port_stats_ext.hw_stats) 4993 return 0; 4994 4995 if (bp->hwrm_spec_code >= 0x10902 || 4996 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) { 4997 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext); 4998 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true); 4999 /* Extended stats are optional */ 5000 if (rc) 5001 return 0; 5002 } 5003 bp->flags |= BNXT_FLAG_PORT_STATS_EXT; 5004 return 0; 5005 } 5006 5007 static void bnxt_clear_ring_indices(struct bnxt *bp) 5008 { 5009 int i, j; 5010 5011 if (!bp->bnapi) 5012 return; 5013 5014 for (i = 0; i < bp->cp_nr_rings; i++) { 5015 struct bnxt_napi *bnapi = bp->bnapi[i]; 5016 struct bnxt_cp_ring_info *cpr; 5017 struct bnxt_rx_ring_info *rxr; 5018 struct bnxt_tx_ring_info *txr; 5019 5020 if (!bnapi) 5021 continue; 5022 5023 cpr = &bnapi->cp_ring; 5024 cpr->cp_raw_cons = 0; 5025 5026 bnxt_for_each_napi_tx(j, bnapi, txr) { 5027 txr->tx_prod = 0; 5028 txr->tx_cons = 0; 5029 txr->tx_hw_cons = 0; 5030 } 5031 5032 rxr = bnapi->rx_ring; 5033 if (rxr) { 5034 rxr->rx_prod = 0; 5035 rxr->rx_agg_prod = 0; 5036 rxr->rx_sw_agg_prod = 0; 5037 rxr->rx_next_cons = 0; 5038 } 5039 bnapi->events = 0; 5040 } 5041 } 5042 5043 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5044 { 5045 u8 type = fltr->type, flags = fltr->flags; 5046 5047 INIT_LIST_HEAD(&fltr->list); 5048 if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) || 5049 (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING)) 5050 list_add_tail(&fltr->list, &bp->usr_fltr_list); 5051 } 5052 5053 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5054 { 5055 if (!list_empty(&fltr->list)) 5056 list_del_init(&fltr->list); 5057 } 5058 5059 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all) 5060 { 5061 struct bnxt_filter_base *usr_fltr, *tmp; 5062 5063 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 5064 if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2) 5065 continue; 5066 bnxt_del_one_usr_fltr(bp, usr_fltr); 5067 } 5068 } 5069 5070 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 5071 { 5072 hlist_del(&fltr->hash); 5073 bnxt_del_one_usr_fltr(bp, fltr); 5074 if (fltr->flags) { 5075 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap); 5076 bp->ntp_fltr_count--; 5077 } 5078 kfree(fltr); 5079 } 5080 5081 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all) 5082 { 5083 int i; 5084 5085 /* Under rtnl_lock and all our NAPIs have been disabled. It's 5086 * safe to delete the hash table. 5087 */ 5088 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 5089 struct hlist_head *head; 5090 struct hlist_node *tmp; 5091 struct bnxt_ntuple_filter *fltr; 5092 5093 head = &bp->ntp_fltr_hash_tbl[i]; 5094 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5095 bnxt_del_l2_filter(bp, fltr->l2_fltr); 5096 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5097 !list_empty(&fltr->base.list))) 5098 continue; 5099 bnxt_del_fltr(bp, &fltr->base); 5100 } 5101 } 5102 if (!all) 5103 return; 5104 5105 bitmap_free(bp->ntp_fltr_bmap); 5106 bp->ntp_fltr_bmap = NULL; 5107 bp->ntp_fltr_count = 0; 5108 } 5109 5110 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp) 5111 { 5112 int i, rc = 0; 5113 5114 if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap) 5115 return 0; 5116 5117 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) 5118 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]); 5119 5120 bp->ntp_fltr_count = 0; 5121 bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL); 5122 5123 if (!bp->ntp_fltr_bmap) 5124 rc = -ENOMEM; 5125 5126 return rc; 5127 } 5128 5129 static void bnxt_free_l2_filters(struct bnxt *bp, bool all) 5130 { 5131 int i; 5132 5133 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) { 5134 struct hlist_head *head; 5135 struct hlist_node *tmp; 5136 struct bnxt_l2_filter *fltr; 5137 5138 head = &bp->l2_fltr_hash_tbl[i]; 5139 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 5140 if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) || 5141 !list_empty(&fltr->base.list))) 5142 continue; 5143 bnxt_del_fltr(bp, &fltr->base); 5144 } 5145 } 5146 } 5147 5148 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp) 5149 { 5150 int i; 5151 5152 for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) 5153 INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]); 5154 get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed)); 5155 } 5156 5157 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init) 5158 { 5159 bnxt_free_vnic_attributes(bp); 5160 bnxt_free_tx_rings(bp); 5161 bnxt_free_rx_rings(bp); 5162 bnxt_free_cp_rings(bp); 5163 bnxt_free_all_cp_arrays(bp); 5164 bnxt_free_ntp_fltrs(bp, false); 5165 bnxt_free_l2_filters(bp, false); 5166 if (irq_re_init) { 5167 bnxt_free_ring_stats(bp); 5168 if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) || 5169 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 5170 bnxt_free_port_stats(bp); 5171 bnxt_free_ring_grps(bp); 5172 bnxt_free_vnics(bp); 5173 kfree(bp->tx_ring_map); 5174 bp->tx_ring_map = NULL; 5175 kfree(bp->tx_ring); 5176 bp->tx_ring = NULL; 5177 kfree(bp->rx_ring); 5178 bp->rx_ring = NULL; 5179 kfree(bp->bnapi); 5180 bp->bnapi = NULL; 5181 } else { 5182 bnxt_clear_ring_indices(bp); 5183 } 5184 } 5185 5186 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) 5187 { 5188 int i, j, rc, size, arr_size; 5189 void *bnapi; 5190 5191 if (irq_re_init) { 5192 /* Allocate bnapi mem pointer array and mem block for 5193 * all queues 5194 */ 5195 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) * 5196 bp->cp_nr_rings); 5197 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi)); 5198 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL); 5199 if (!bnapi) 5200 return -ENOMEM; 5201 5202 bp->bnapi = bnapi; 5203 bnapi += arr_size; 5204 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) { 5205 bp->bnapi[i] = bnapi; 5206 bp->bnapi[i]->index = i; 5207 bp->bnapi[i]->bp = bp; 5208 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5209 struct bnxt_cp_ring_info *cpr = 5210 &bp->bnapi[i]->cp_ring; 5211 5212 cpr->cp_ring_struct.ring_mem.flags = 5213 BNXT_RMEM_RING_PTE_FLAG; 5214 } 5215 } 5216 5217 bp->rx_ring = kcalloc(bp->rx_nr_rings, 5218 sizeof(struct bnxt_rx_ring_info), 5219 GFP_KERNEL); 5220 if (!bp->rx_ring) 5221 return -ENOMEM; 5222 5223 for (i = 0; i < bp->rx_nr_rings; i++) { 5224 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 5225 5226 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 5227 rxr->rx_ring_struct.ring_mem.flags = 5228 BNXT_RMEM_RING_PTE_FLAG; 5229 rxr->rx_agg_ring_struct.ring_mem.flags = 5230 BNXT_RMEM_RING_PTE_FLAG; 5231 } else { 5232 rxr->rx_cpr = &bp->bnapi[i]->cp_ring; 5233 } 5234 rxr->bnapi = bp->bnapi[i]; 5235 bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; 5236 } 5237 5238 bp->tx_ring = kcalloc(bp->tx_nr_rings, 5239 sizeof(struct bnxt_tx_ring_info), 5240 GFP_KERNEL); 5241 if (!bp->tx_ring) 5242 return -ENOMEM; 5243 5244 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16), 5245 GFP_KERNEL); 5246 5247 if (!bp->tx_ring_map) 5248 return -ENOMEM; 5249 5250 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 5251 j = 0; 5252 else 5253 j = bp->rx_nr_rings; 5254 5255 for (i = 0; i < bp->tx_nr_rings; i++) { 5256 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 5257 struct bnxt_napi *bnapi2; 5258 5259 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 5260 txr->tx_ring_struct.ring_mem.flags = 5261 BNXT_RMEM_RING_PTE_FLAG; 5262 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i; 5263 if (i >= bp->tx_nr_rings_xdp) { 5264 int k = j + BNXT_RING_TO_TC_OFF(bp, i); 5265 5266 bnapi2 = bp->bnapi[k]; 5267 txr->txq_index = i - bp->tx_nr_rings_xdp; 5268 txr->tx_napi_idx = 5269 BNXT_RING_TO_TC(bp, txr->txq_index); 5270 bnapi2->tx_ring[txr->tx_napi_idx] = txr; 5271 bnapi2->tx_int = bnxt_tx_int; 5272 } else { 5273 bnapi2 = bp->bnapi[j]; 5274 bnapi2->flags |= BNXT_NAPI_FLAG_XDP; 5275 bnapi2->tx_ring[0] = txr; 5276 bnapi2->tx_int = bnxt_tx_int_xdp; 5277 j++; 5278 } 5279 txr->bnapi = bnapi2; 5280 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 5281 txr->tx_cpr = &bnapi2->cp_ring; 5282 } 5283 5284 rc = bnxt_alloc_stats(bp); 5285 if (rc) 5286 goto alloc_mem_err; 5287 bnxt_init_stats(bp); 5288 5289 rc = bnxt_alloc_ntp_fltrs(bp); 5290 if (rc) 5291 goto alloc_mem_err; 5292 5293 rc = bnxt_alloc_vnics(bp); 5294 if (rc) 5295 goto alloc_mem_err; 5296 } 5297 5298 rc = bnxt_alloc_all_cp_arrays(bp); 5299 if (rc) 5300 goto alloc_mem_err; 5301 5302 bnxt_init_ring_struct(bp); 5303 5304 rc = bnxt_alloc_rx_rings(bp); 5305 if (rc) 5306 goto alloc_mem_err; 5307 5308 rc = bnxt_alloc_tx_rings(bp); 5309 if (rc) 5310 goto alloc_mem_err; 5311 5312 rc = bnxt_alloc_cp_rings(bp); 5313 if (rc) 5314 goto alloc_mem_err; 5315 5316 bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG | 5317 BNXT_VNIC_MCAST_FLAG | 5318 BNXT_VNIC_UCAST_FLAG; 5319 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS)) 5320 bp->vnic_info[BNXT_VNIC_NTUPLE].flags |= 5321 BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG; 5322 5323 rc = bnxt_alloc_vnic_attributes(bp); 5324 if (rc) 5325 goto alloc_mem_err; 5326 return 0; 5327 5328 alloc_mem_err: 5329 bnxt_free_mem(bp, true); 5330 return rc; 5331 } 5332 5333 static void bnxt_disable_int(struct bnxt *bp) 5334 { 5335 int i; 5336 5337 if (!bp->bnapi) 5338 return; 5339 5340 for (i = 0; i < bp->cp_nr_rings; i++) { 5341 struct bnxt_napi *bnapi = bp->bnapi[i]; 5342 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5343 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 5344 5345 if (ring->fw_ring_id != INVALID_HW_RING_ID) 5346 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 5347 } 5348 } 5349 5350 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n) 5351 { 5352 struct bnxt_napi *bnapi = bp->bnapi[n]; 5353 struct bnxt_cp_ring_info *cpr; 5354 5355 cpr = &bnapi->cp_ring; 5356 return cpr->cp_ring_struct.map_idx; 5357 } 5358 5359 static void bnxt_disable_int_sync(struct bnxt *bp) 5360 { 5361 int i; 5362 5363 if (!bp->irq_tbl) 5364 return; 5365 5366 atomic_inc(&bp->intr_sem); 5367 5368 bnxt_disable_int(bp); 5369 for (i = 0; i < bp->cp_nr_rings; i++) { 5370 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 5371 5372 synchronize_irq(bp->irq_tbl[map_idx].vector); 5373 } 5374 } 5375 5376 static void bnxt_enable_int(struct bnxt *bp) 5377 { 5378 int i; 5379 5380 atomic_set(&bp->intr_sem, 0); 5381 for (i = 0; i < bp->cp_nr_rings; i++) { 5382 struct bnxt_napi *bnapi = bp->bnapi[i]; 5383 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 5384 5385 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons); 5386 } 5387 } 5388 5389 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size, 5390 bool async_only) 5391 { 5392 DECLARE_BITMAP(async_events_bmap, 256); 5393 u32 *events = (u32 *)async_events_bmap; 5394 struct hwrm_func_drv_rgtr_output *resp; 5395 struct hwrm_func_drv_rgtr_input *req; 5396 u32 flags; 5397 int rc, i; 5398 5399 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR); 5400 if (rc) 5401 return rc; 5402 5403 req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE | 5404 FUNC_DRV_RGTR_REQ_ENABLES_VER | 5405 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5406 5407 req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX); 5408 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE; 5409 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET) 5410 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT; 5411 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 5412 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT | 5413 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT; 5414 req->flags = cpu_to_le32(flags); 5415 req->ver_maj_8b = DRV_VER_MAJ; 5416 req->ver_min_8b = DRV_VER_MIN; 5417 req->ver_upd_8b = DRV_VER_UPD; 5418 req->ver_maj = cpu_to_le16(DRV_VER_MAJ); 5419 req->ver_min = cpu_to_le16(DRV_VER_MIN); 5420 req->ver_upd = cpu_to_le16(DRV_VER_UPD); 5421 5422 if (BNXT_PF(bp)) { 5423 u32 data[8]; 5424 int i; 5425 5426 memset(data, 0, sizeof(data)); 5427 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) { 5428 u16 cmd = bnxt_vf_req_snif[i]; 5429 unsigned int bit, idx; 5430 5431 idx = cmd / 32; 5432 bit = cmd % 32; 5433 data[idx] |= 1 << bit; 5434 } 5435 5436 for (i = 0; i < 8; i++) 5437 req->vf_req_fwd[i] = cpu_to_le32(data[i]); 5438 5439 req->enables |= 5440 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD); 5441 } 5442 5443 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE) 5444 req->flags |= cpu_to_le32( 5445 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE); 5446 5447 memset(async_events_bmap, 0, sizeof(async_events_bmap)); 5448 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) { 5449 u16 event_id = bnxt_async_events_arr[i]; 5450 5451 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY && 5452 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 5453 continue; 5454 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE && 5455 !bp->ptp_cfg) 5456 continue; 5457 __set_bit(bnxt_async_events_arr[i], async_events_bmap); 5458 } 5459 if (bmap && bmap_size) { 5460 for (i = 0; i < bmap_size; i++) { 5461 if (test_bit(i, bmap)) 5462 __set_bit(i, async_events_bmap); 5463 } 5464 } 5465 for (i = 0; i < 8; i++) 5466 req->async_event_fwd[i] |= cpu_to_le32(events[i]); 5467 5468 if (async_only) 5469 req->enables = 5470 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD); 5471 5472 resp = hwrm_req_hold(bp, req); 5473 rc = hwrm_req_send(bp, req); 5474 if (!rc) { 5475 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state); 5476 if (resp->flags & 5477 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED)) 5478 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE; 5479 } 5480 hwrm_req_drop(bp, req); 5481 return rc; 5482 } 5483 5484 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp) 5485 { 5486 struct hwrm_func_drv_unrgtr_input *req; 5487 int rc; 5488 5489 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state)) 5490 return 0; 5491 5492 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR); 5493 if (rc) 5494 return rc; 5495 return hwrm_req_send(bp, req); 5496 } 5497 5498 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa); 5499 5500 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type) 5501 { 5502 struct hwrm_tunnel_dst_port_free_input *req; 5503 int rc; 5504 5505 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN && 5506 bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID) 5507 return 0; 5508 if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE && 5509 bp->nge_fw_dst_port_id == INVALID_HW_RING_ID) 5510 return 0; 5511 5512 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE); 5513 if (rc) 5514 return rc; 5515 5516 req->tunnel_type = tunnel_type; 5517 5518 switch (tunnel_type) { 5519 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN: 5520 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id); 5521 bp->vxlan_port = 0; 5522 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 5523 break; 5524 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE: 5525 req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id); 5526 bp->nge_port = 0; 5527 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 5528 break; 5529 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE: 5530 req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id); 5531 bp->vxlan_gpe_port = 0; 5532 bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID; 5533 break; 5534 default: 5535 break; 5536 } 5537 5538 rc = hwrm_req_send(bp, req); 5539 if (rc) 5540 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n", 5541 rc); 5542 if (bp->flags & BNXT_FLAG_TPA) 5543 bnxt_set_tpa(bp, true); 5544 return rc; 5545 } 5546 5547 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port, 5548 u8 tunnel_type) 5549 { 5550 struct hwrm_tunnel_dst_port_alloc_output *resp; 5551 struct hwrm_tunnel_dst_port_alloc_input *req; 5552 int rc; 5553 5554 rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC); 5555 if (rc) 5556 return rc; 5557 5558 req->tunnel_type = tunnel_type; 5559 req->tunnel_dst_port_val = port; 5560 5561 resp = hwrm_req_hold(bp, req); 5562 rc = hwrm_req_send(bp, req); 5563 if (rc) { 5564 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n", 5565 rc); 5566 goto err_out; 5567 } 5568 5569 switch (tunnel_type) { 5570 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN: 5571 bp->vxlan_port = port; 5572 bp->vxlan_fw_dst_port_id = 5573 le16_to_cpu(resp->tunnel_dst_port_id); 5574 break; 5575 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE: 5576 bp->nge_port = port; 5577 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id); 5578 break; 5579 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE: 5580 bp->vxlan_gpe_port = port; 5581 bp->vxlan_gpe_fw_dst_port_id = 5582 le16_to_cpu(resp->tunnel_dst_port_id); 5583 break; 5584 default: 5585 break; 5586 } 5587 if (bp->flags & BNXT_FLAG_TPA) 5588 bnxt_set_tpa(bp, true); 5589 5590 err_out: 5591 hwrm_req_drop(bp, req); 5592 return rc; 5593 } 5594 5595 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id) 5596 { 5597 struct hwrm_cfa_l2_set_rx_mask_input *req; 5598 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id]; 5599 int rc; 5600 5601 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK); 5602 if (rc) 5603 return rc; 5604 5605 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 5606 if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) { 5607 req->num_mc_entries = cpu_to_le32(vnic->mc_list_count); 5608 req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping); 5609 } 5610 req->mask = cpu_to_le32(vnic->rx_mask); 5611 return hwrm_req_send_silent(bp, req); 5612 } 5613 5614 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5615 { 5616 if (!atomic_dec_and_test(&fltr->refcnt)) 5617 return; 5618 spin_lock_bh(&bp->ntp_fltr_lock); 5619 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 5620 spin_unlock_bh(&bp->ntp_fltr_lock); 5621 return; 5622 } 5623 hlist_del_rcu(&fltr->base.hash); 5624 bnxt_del_one_usr_fltr(bp, &fltr->base); 5625 if (fltr->base.flags) { 5626 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 5627 bp->ntp_fltr_count--; 5628 } 5629 spin_unlock_bh(&bp->ntp_fltr_lock); 5630 kfree_rcu(fltr, base.rcu); 5631 } 5632 5633 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp, 5634 struct bnxt_l2_key *key, 5635 u32 idx) 5636 { 5637 struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx]; 5638 struct bnxt_l2_filter *fltr; 5639 5640 hlist_for_each_entry_rcu(fltr, head, base.hash) { 5641 struct bnxt_l2_key *l2_key = &fltr->l2_key; 5642 5643 if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) && 5644 l2_key->vlan == key->vlan) 5645 return fltr; 5646 } 5647 return NULL; 5648 } 5649 5650 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp, 5651 struct bnxt_l2_key *key, 5652 u32 idx) 5653 { 5654 struct bnxt_l2_filter *fltr = NULL; 5655 5656 rcu_read_lock(); 5657 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5658 if (fltr) 5659 atomic_inc(&fltr->refcnt); 5660 rcu_read_unlock(); 5661 return fltr; 5662 } 5663 5664 #define BNXT_IPV4_4TUPLE(bp, fkeys) \ 5665 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5666 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) || \ 5667 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5668 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4)) 5669 5670 #define BNXT_IPV6_4TUPLE(bp, fkeys) \ 5671 (((fkeys)->basic.ip_proto == IPPROTO_TCP && \ 5672 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) || \ 5673 ((fkeys)->basic.ip_proto == IPPROTO_UDP && \ 5674 (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6)) 5675 5676 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys) 5677 { 5678 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5679 if (BNXT_IPV4_4TUPLE(bp, fkeys)) 5680 return sizeof(fkeys->addrs.v4addrs) + 5681 sizeof(fkeys->ports); 5682 5683 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4) 5684 return sizeof(fkeys->addrs.v4addrs); 5685 } 5686 5687 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 5688 if (BNXT_IPV6_4TUPLE(bp, fkeys)) 5689 return sizeof(fkeys->addrs.v6addrs) + 5690 sizeof(fkeys->ports); 5691 5692 if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6) 5693 return sizeof(fkeys->addrs.v6addrs); 5694 } 5695 5696 return 0; 5697 } 5698 5699 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys, 5700 const unsigned char *key) 5701 { 5702 u64 prefix = bp->toeplitz_prefix, hash = 0; 5703 struct bnxt_ipv4_tuple tuple4; 5704 struct bnxt_ipv6_tuple tuple6; 5705 int i, j, len = 0; 5706 u8 *four_tuple; 5707 5708 len = bnxt_get_rss_flow_tuple_len(bp, fkeys); 5709 if (!len) 5710 return 0; 5711 5712 if (fkeys->basic.n_proto == htons(ETH_P_IP)) { 5713 tuple4.v4addrs = fkeys->addrs.v4addrs; 5714 tuple4.ports = fkeys->ports; 5715 four_tuple = (unsigned char *)&tuple4; 5716 } else { 5717 tuple6.v6addrs = fkeys->addrs.v6addrs; 5718 tuple6.ports = fkeys->ports; 5719 four_tuple = (unsigned char *)&tuple6; 5720 } 5721 5722 for (i = 0, j = 8; i < len; i++, j++) { 5723 u8 byte = four_tuple[i]; 5724 int bit; 5725 5726 for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) { 5727 if (byte & 0x80) 5728 hash ^= prefix; 5729 } 5730 prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0; 5731 } 5732 5733 /* The valid part of the hash is in the upper 32 bits. */ 5734 return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK; 5735 } 5736 5737 #ifdef CONFIG_RFS_ACCEL 5738 static struct bnxt_l2_filter * 5739 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key) 5740 { 5741 struct bnxt_l2_filter *fltr; 5742 u32 idx; 5743 5744 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5745 BNXT_L2_FLTR_HASH_MASK; 5746 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5747 return fltr; 5748 } 5749 #endif 5750 5751 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr, 5752 struct bnxt_l2_key *key, u32 idx) 5753 { 5754 struct hlist_head *head; 5755 5756 ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr); 5757 fltr->l2_key.vlan = key->vlan; 5758 fltr->base.type = BNXT_FLTR_TYPE_L2; 5759 if (fltr->base.flags) { 5760 int bit_id; 5761 5762 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, 5763 bp->max_fltr, 0); 5764 if (bit_id < 0) 5765 return -ENOMEM; 5766 fltr->base.sw_id = (u16)bit_id; 5767 bp->ntp_fltr_count++; 5768 } 5769 head = &bp->l2_fltr_hash_tbl[idx]; 5770 hlist_add_head_rcu(&fltr->base.hash, head); 5771 bnxt_insert_usr_fltr(bp, &fltr->base); 5772 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 5773 atomic_set(&fltr->refcnt, 1); 5774 return 0; 5775 } 5776 5777 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp, 5778 struct bnxt_l2_key *key, 5779 gfp_t gfp) 5780 { 5781 struct bnxt_l2_filter *fltr; 5782 u32 idx; 5783 int rc; 5784 5785 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5786 BNXT_L2_FLTR_HASH_MASK; 5787 fltr = bnxt_lookup_l2_filter(bp, key, idx); 5788 if (fltr) 5789 return fltr; 5790 5791 fltr = kzalloc(sizeof(*fltr), gfp); 5792 if (!fltr) 5793 return ERR_PTR(-ENOMEM); 5794 spin_lock_bh(&bp->ntp_fltr_lock); 5795 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5796 spin_unlock_bh(&bp->ntp_fltr_lock); 5797 if (rc) { 5798 bnxt_del_l2_filter(bp, fltr); 5799 fltr = ERR_PTR(rc); 5800 } 5801 return fltr; 5802 } 5803 5804 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp, 5805 struct bnxt_l2_key *key, 5806 u16 flags) 5807 { 5808 struct bnxt_l2_filter *fltr; 5809 u32 idx; 5810 int rc; 5811 5812 idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) & 5813 BNXT_L2_FLTR_HASH_MASK; 5814 spin_lock_bh(&bp->ntp_fltr_lock); 5815 fltr = __bnxt_lookup_l2_filter(bp, key, idx); 5816 if (fltr) { 5817 fltr = ERR_PTR(-EEXIST); 5818 goto l2_filter_exit; 5819 } 5820 fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC); 5821 if (!fltr) { 5822 fltr = ERR_PTR(-ENOMEM); 5823 goto l2_filter_exit; 5824 } 5825 fltr->base.flags = flags; 5826 rc = bnxt_init_l2_filter(bp, fltr, key, idx); 5827 if (rc) { 5828 spin_unlock_bh(&bp->ntp_fltr_lock); 5829 bnxt_del_l2_filter(bp, fltr); 5830 return ERR_PTR(rc); 5831 } 5832 5833 l2_filter_exit: 5834 spin_unlock_bh(&bp->ntp_fltr_lock); 5835 return fltr; 5836 } 5837 5838 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx) 5839 { 5840 #ifdef CONFIG_BNXT_SRIOV 5841 struct bnxt_vf_info *vf = &pf->vf[vf_idx]; 5842 5843 return vf->fw_fid; 5844 #else 5845 return INVALID_HW_RING_ID; 5846 #endif 5847 } 5848 5849 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5850 { 5851 struct hwrm_cfa_l2_filter_free_input *req; 5852 u16 target_id = 0xffff; 5853 int rc; 5854 5855 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 5856 struct bnxt_pf_info *pf = &bp->pf; 5857 5858 if (fltr->base.vf_idx >= pf->active_vfs) 5859 return -EINVAL; 5860 5861 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 5862 if (target_id == INVALID_HW_RING_ID) 5863 return -EINVAL; 5864 } 5865 5866 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE); 5867 if (rc) 5868 return rc; 5869 5870 req->target_id = cpu_to_le16(target_id); 5871 req->l2_filter_id = fltr->base.filter_id; 5872 return hwrm_req_send(bp, req); 5873 } 5874 5875 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr) 5876 { 5877 struct hwrm_cfa_l2_filter_alloc_output *resp; 5878 struct hwrm_cfa_l2_filter_alloc_input *req; 5879 u16 target_id = 0xffff; 5880 int rc; 5881 5882 if (fltr->base.flags & BNXT_ACT_FUNC_DST) { 5883 struct bnxt_pf_info *pf = &bp->pf; 5884 5885 if (fltr->base.vf_idx >= pf->active_vfs) 5886 return -EINVAL; 5887 5888 target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx); 5889 } 5890 rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC); 5891 if (rc) 5892 return rc; 5893 5894 req->target_id = cpu_to_le16(target_id); 5895 req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX); 5896 5897 if (!BNXT_CHIP_TYPE_NITRO_A0(bp)) 5898 req->flags |= 5899 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST); 5900 req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id); 5901 req->enables = 5902 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR | 5903 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID | 5904 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK); 5905 ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr); 5906 eth_broadcast_addr(req->l2_addr_mask); 5907 5908 if (fltr->l2_key.vlan) { 5909 req->enables |= 5910 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN | 5911 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK | 5912 CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS); 5913 req->num_vlans = 1; 5914 req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan); 5915 req->l2_ivlan_mask = cpu_to_le16(0xfff); 5916 } 5917 5918 resp = hwrm_req_hold(bp, req); 5919 rc = hwrm_req_send(bp, req); 5920 if (!rc) { 5921 fltr->base.filter_id = resp->l2_filter_id; 5922 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 5923 } 5924 hwrm_req_drop(bp, req); 5925 return rc; 5926 } 5927 5928 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp, 5929 struct bnxt_ntuple_filter *fltr) 5930 { 5931 struct hwrm_cfa_ntuple_filter_free_input *req; 5932 int rc; 5933 5934 set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state); 5935 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE); 5936 if (rc) 5937 return rc; 5938 5939 req->ntuple_filter_id = fltr->base.filter_id; 5940 return hwrm_req_send(bp, req); 5941 } 5942 5943 #define BNXT_NTP_FLTR_FLAGS \ 5944 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \ 5945 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \ 5946 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \ 5947 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \ 5948 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \ 5949 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \ 5950 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \ 5951 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \ 5952 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \ 5953 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \ 5954 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \ 5955 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \ 5956 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID) 5957 5958 #define BNXT_NTP_TUNNEL_FLTR_FLAG \ 5959 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE 5960 5961 void bnxt_fill_ipv6_mask(__be32 mask[4]) 5962 { 5963 int i; 5964 5965 for (i = 0; i < 4; i++) 5966 mask[i] = cpu_to_be32(~0); 5967 } 5968 5969 static void 5970 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp, 5971 struct hwrm_cfa_ntuple_filter_alloc_input *req, 5972 struct bnxt_ntuple_filter *fltr) 5973 { 5974 u16 rxq = fltr->base.rxq; 5975 5976 if (fltr->base.flags & BNXT_ACT_RSS_CTX) { 5977 struct ethtool_rxfh_context *ctx; 5978 struct bnxt_rss_ctx *rss_ctx; 5979 struct bnxt_vnic_info *vnic; 5980 5981 ctx = xa_load(&bp->dev->ethtool->rss_ctx, 5982 fltr->base.fw_vnic_id); 5983 if (ctx) { 5984 rss_ctx = ethtool_rxfh_context_priv(ctx); 5985 vnic = &rss_ctx->vnic; 5986 5987 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 5988 } 5989 return; 5990 } 5991 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 5992 struct bnxt_vnic_info *vnic; 5993 u32 enables; 5994 5995 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 5996 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 5997 enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX; 5998 req->enables |= cpu_to_le32(enables); 5999 req->rfs_ring_tbl_idx = cpu_to_le16(rxq); 6000 } else { 6001 u32 flags; 6002 6003 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX; 6004 req->flags |= cpu_to_le32(flags); 6005 req->dst_id = cpu_to_le16(rxq); 6006 } 6007 } 6008 6009 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp, 6010 struct bnxt_ntuple_filter *fltr) 6011 { 6012 struct hwrm_cfa_ntuple_filter_alloc_output *resp; 6013 struct hwrm_cfa_ntuple_filter_alloc_input *req; 6014 struct bnxt_flow_masks *masks = &fltr->fmasks; 6015 struct flow_keys *keys = &fltr->fkeys; 6016 struct bnxt_l2_filter *l2_fltr; 6017 struct bnxt_vnic_info *vnic; 6018 int rc; 6019 6020 rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC); 6021 if (rc) 6022 return rc; 6023 6024 l2_fltr = fltr->l2_fltr; 6025 req->l2_filter_id = l2_fltr->base.filter_id; 6026 6027 if (fltr->base.flags & BNXT_ACT_DROP) { 6028 req->flags = 6029 cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP); 6030 } else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) { 6031 bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr); 6032 } else { 6033 vnic = &bp->vnic_info[fltr->base.rxq + 1]; 6034 req->dst_id = cpu_to_le16(vnic->fw_vnic_id); 6035 } 6036 req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS); 6037 6038 req->ethertype = htons(ETH_P_IP); 6039 req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4; 6040 req->ip_protocol = keys->basic.ip_proto; 6041 6042 if (keys->basic.n_proto == htons(ETH_P_IPV6)) { 6043 req->ethertype = htons(ETH_P_IPV6); 6044 req->ip_addr_type = 6045 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6; 6046 *(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src; 6047 *(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src; 6048 *(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst; 6049 *(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst; 6050 } else { 6051 req->src_ipaddr[0] = keys->addrs.v4addrs.src; 6052 req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src; 6053 req->dst_ipaddr[0] = keys->addrs.v4addrs.dst; 6054 req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst; 6055 } 6056 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) { 6057 req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG); 6058 req->tunnel_type = 6059 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL; 6060 } 6061 6062 req->src_port = keys->ports.src; 6063 req->src_port_mask = masks->ports.src; 6064 req->dst_port = keys->ports.dst; 6065 req->dst_port_mask = masks->ports.dst; 6066 6067 resp = hwrm_req_hold(bp, req); 6068 rc = hwrm_req_send(bp, req); 6069 if (!rc) 6070 fltr->base.filter_id = resp->ntuple_filter_id; 6071 hwrm_req_drop(bp, req); 6072 return rc; 6073 } 6074 6075 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx, 6076 const u8 *mac_addr) 6077 { 6078 struct bnxt_l2_filter *fltr; 6079 struct bnxt_l2_key key; 6080 int rc; 6081 6082 ether_addr_copy(key.dst_mac_addr, mac_addr); 6083 key.vlan = 0; 6084 fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL); 6085 if (IS_ERR(fltr)) 6086 return PTR_ERR(fltr); 6087 6088 fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id; 6089 rc = bnxt_hwrm_l2_filter_alloc(bp, fltr); 6090 if (rc) 6091 bnxt_del_l2_filter(bp, fltr); 6092 else 6093 bp->vnic_info[vnic_id].l2_filters[idx] = fltr; 6094 return rc; 6095 } 6096 6097 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp) 6098 { 6099 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */ 6100 6101 /* Any associated ntuple filters will also be cleared by firmware. */ 6102 for (i = 0; i < num_of_vnics; i++) { 6103 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6104 6105 for (j = 0; j < vnic->uc_filter_count; j++) { 6106 struct bnxt_l2_filter *fltr = vnic->l2_filters[j]; 6107 6108 bnxt_hwrm_l2_filter_free(bp, fltr); 6109 bnxt_del_l2_filter(bp, fltr); 6110 } 6111 vnic->uc_filter_count = 0; 6112 } 6113 } 6114 6115 #define BNXT_DFLT_TUNL_TPA_BMAP \ 6116 (VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE | \ 6117 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 | \ 6118 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6) 6119 6120 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp, 6121 struct hwrm_vnic_tpa_cfg_input *req) 6122 { 6123 u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP; 6124 6125 if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA)) 6126 return; 6127 6128 if (bp->vxlan_port) 6129 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN; 6130 if (bp->vxlan_gpe_port) 6131 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE; 6132 if (bp->nge_port) 6133 tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE; 6134 6135 req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN); 6136 req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap); 6137 } 6138 6139 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6140 u32 tpa_flags) 6141 { 6142 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX; 6143 struct hwrm_vnic_tpa_cfg_input *req; 6144 int rc; 6145 6146 if (vnic->fw_vnic_id == INVALID_HW_RING_ID) 6147 return 0; 6148 6149 rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG); 6150 if (rc) 6151 return rc; 6152 6153 if (tpa_flags) { 6154 u16 mss = bp->dev->mtu - 40; 6155 u32 nsegs, n, segs = 0, flags; 6156 6157 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA | 6158 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA | 6159 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE | 6160 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN | 6161 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ; 6162 if (tpa_flags & BNXT_FLAG_GRO) 6163 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO; 6164 6165 req->flags = cpu_to_le32(flags); 6166 6167 req->enables = 6168 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS | 6169 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS | 6170 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN); 6171 6172 /* Number of segs are log2 units, and first packet is not 6173 * included as part of this units. 6174 */ 6175 if (mss <= BNXT_RX_PAGE_SIZE) { 6176 n = BNXT_RX_PAGE_SIZE / mss; 6177 nsegs = (MAX_SKB_FRAGS - 1) * n; 6178 } else { 6179 n = mss / BNXT_RX_PAGE_SIZE; 6180 if (mss & (BNXT_RX_PAGE_SIZE - 1)) 6181 n++; 6182 nsegs = (MAX_SKB_FRAGS - n) / n; 6183 } 6184 6185 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6186 segs = MAX_TPA_SEGS_P5; 6187 max_aggs = bp->max_tpa; 6188 } else { 6189 segs = ilog2(nsegs); 6190 } 6191 req->max_agg_segs = cpu_to_le16(segs); 6192 req->max_aggs = cpu_to_le16(max_aggs); 6193 6194 req->min_agg_len = cpu_to_le32(512); 6195 bnxt_hwrm_vnic_update_tunl_tpa(bp, req); 6196 } 6197 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6198 6199 return hwrm_req_send(bp, req); 6200 } 6201 6202 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring) 6203 { 6204 struct bnxt_ring_grp_info *grp_info; 6205 6206 grp_info = &bp->grp_info[ring->grp_idx]; 6207 return grp_info->cp_fw_ring_id; 6208 } 6209 6210 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 6211 { 6212 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6213 return rxr->rx_cpr->cp_ring_struct.fw_ring_id; 6214 else 6215 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct); 6216 } 6217 6218 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr) 6219 { 6220 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6221 return txr->tx_cpr->cp_ring_struct.fw_ring_id; 6222 else 6223 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct); 6224 } 6225 6226 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp) 6227 { 6228 int entries; 6229 6230 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6231 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5; 6232 else 6233 entries = HW_HASH_INDEX_SIZE; 6234 6235 bp->rss_indir_tbl_entries = entries; 6236 bp->rss_indir_tbl = 6237 kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL); 6238 if (!bp->rss_indir_tbl) 6239 return -ENOMEM; 6240 6241 return 0; 6242 } 6243 6244 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp, 6245 struct ethtool_rxfh_context *rss_ctx) 6246 { 6247 u16 max_rings, max_entries, pad, i; 6248 u32 *rss_indir_tbl; 6249 6250 if (!bp->rx_nr_rings) 6251 return; 6252 6253 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6254 max_rings = bp->rx_nr_rings - 1; 6255 else 6256 max_rings = bp->rx_nr_rings; 6257 6258 max_entries = bnxt_get_rxfh_indir_size(bp->dev); 6259 if (rss_ctx) 6260 rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx); 6261 else 6262 rss_indir_tbl = &bp->rss_indir_tbl[0]; 6263 6264 for (i = 0; i < max_entries; i++) 6265 rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings); 6266 6267 pad = bp->rss_indir_tbl_entries - max_entries; 6268 if (pad) 6269 memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl)); 6270 } 6271 6272 static u16 bnxt_get_max_rss_ring(struct bnxt *bp) 6273 { 6274 u32 i, tbl_size, max_ring = 0; 6275 6276 if (!bp->rss_indir_tbl) 6277 return 0; 6278 6279 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6280 for (i = 0; i < tbl_size; i++) 6281 max_ring = max(max_ring, bp->rss_indir_tbl[i]); 6282 return max_ring; 6283 } 6284 6285 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings) 6286 { 6287 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6288 if (!rx_rings) 6289 return 0; 6290 return bnxt_calc_nr_ring_pages(rx_rings - 1, 6291 BNXT_RSS_TABLE_ENTRIES_P5); 6292 } 6293 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 6294 return 2; 6295 return 1; 6296 } 6297 6298 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6299 { 6300 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG); 6301 u16 i, j; 6302 6303 /* Fill the RSS indirection table with ring group ids */ 6304 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) { 6305 if (!no_rss) 6306 j = bp->rss_indir_tbl[i]; 6307 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]); 6308 } 6309 } 6310 6311 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp, 6312 struct bnxt_vnic_info *vnic) 6313 { 6314 __le16 *ring_tbl = vnic->rss_table; 6315 struct bnxt_rx_ring_info *rxr; 6316 u16 tbl_size, i; 6317 6318 tbl_size = bnxt_get_rxfh_indir_size(bp->dev); 6319 6320 for (i = 0; i < tbl_size; i++) { 6321 u16 ring_id, j; 6322 6323 if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG) 6324 j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings); 6325 else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG) 6326 j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i]; 6327 else 6328 j = bp->rss_indir_tbl[i]; 6329 rxr = &bp->rx_ring[j]; 6330 6331 ring_id = rxr->rx_ring_struct.fw_ring_id; 6332 *ring_tbl++ = cpu_to_le16(ring_id); 6333 ring_id = bnxt_cp_ring_for_rx(bp, rxr); 6334 *ring_tbl++ = cpu_to_le16(ring_id); 6335 } 6336 } 6337 6338 static void 6339 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req, 6340 struct bnxt_vnic_info *vnic) 6341 { 6342 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6343 bnxt_fill_hw_rss_tbl_p5(bp, vnic); 6344 if (bp->flags & BNXT_FLAG_CHIP_P7) 6345 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT; 6346 } else { 6347 bnxt_fill_hw_rss_tbl(bp, vnic); 6348 } 6349 6350 if (bp->rss_hash_delta) { 6351 req->hash_type = cpu_to_le32(bp->rss_hash_delta); 6352 if (bp->rss_hash_cfg & bp->rss_hash_delta) 6353 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE; 6354 else 6355 req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE; 6356 } else { 6357 req->hash_type = cpu_to_le32(bp->rss_hash_cfg); 6358 } 6359 req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT; 6360 req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr); 6361 req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr); 6362 } 6363 6364 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6365 bool set_rss) 6366 { 6367 struct hwrm_vnic_rss_cfg_input *req; 6368 int rc; 6369 6370 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) || 6371 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID) 6372 return 0; 6373 6374 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6375 if (rc) 6376 return rc; 6377 6378 if (set_rss) 6379 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6380 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6381 return hwrm_req_send(bp, req); 6382 } 6383 6384 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, 6385 struct bnxt_vnic_info *vnic, bool set_rss) 6386 { 6387 struct hwrm_vnic_rss_cfg_input *req; 6388 dma_addr_t ring_tbl_map; 6389 u32 i, nr_ctxs; 6390 int rc; 6391 6392 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG); 6393 if (rc) 6394 return rc; 6395 6396 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6397 if (!set_rss) 6398 return hwrm_req_send(bp, req); 6399 6400 __bnxt_hwrm_vnic_set_rss(bp, req, vnic); 6401 ring_tbl_map = vnic->rss_table_dma_addr; 6402 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 6403 6404 hwrm_req_hold(bp, req); 6405 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) { 6406 req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map); 6407 req->ring_table_pair_index = i; 6408 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]); 6409 rc = hwrm_req_send(bp, req); 6410 if (rc) 6411 goto exit; 6412 } 6413 6414 exit: 6415 hwrm_req_drop(bp, req); 6416 return rc; 6417 } 6418 6419 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp) 6420 { 6421 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6422 struct hwrm_vnic_rss_qcfg_output *resp; 6423 struct hwrm_vnic_rss_qcfg_input *req; 6424 6425 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG)) 6426 return; 6427 6428 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6429 /* all contexts configured to same hash_type, zero always exists */ 6430 req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6431 resp = hwrm_req_hold(bp, req); 6432 if (!hwrm_req_send(bp, req)) { 6433 bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg; 6434 bp->rss_hash_delta = 0; 6435 } 6436 hwrm_req_drop(bp, req); 6437 } 6438 6439 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6440 { 6441 struct hwrm_vnic_plcmodes_cfg_input *req; 6442 int rc; 6443 6444 rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG); 6445 if (rc) 6446 return rc; 6447 6448 req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT); 6449 req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID); 6450 6451 if (BNXT_RX_PAGE_MODE(bp)) { 6452 req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size); 6453 } else { 6454 req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 | 6455 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6); 6456 req->enables |= 6457 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID); 6458 req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh); 6459 req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh); 6460 } 6461 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6462 return hwrm_req_send(bp, req); 6463 } 6464 6465 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, 6466 struct bnxt_vnic_info *vnic, 6467 u16 ctx_idx) 6468 { 6469 struct hwrm_vnic_rss_cos_lb_ctx_free_input *req; 6470 6471 if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE)) 6472 return; 6473 6474 req->rss_cos_lb_ctx_id = 6475 cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]); 6476 6477 hwrm_req_send(bp, req); 6478 vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID; 6479 } 6480 6481 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp) 6482 { 6483 int i, j; 6484 6485 for (i = 0; i < bp->nr_vnics; i++) { 6486 struct bnxt_vnic_info *vnic = &bp->vnic_info[i]; 6487 6488 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) { 6489 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID) 6490 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j); 6491 } 6492 } 6493 bp->rsscos_nr_ctxs = 0; 6494 } 6495 6496 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, 6497 struct bnxt_vnic_info *vnic, u16 ctx_idx) 6498 { 6499 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp; 6500 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req; 6501 int rc; 6502 6503 rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC); 6504 if (rc) 6505 return rc; 6506 6507 resp = hwrm_req_hold(bp, req); 6508 rc = hwrm_req_send(bp, req); 6509 if (!rc) 6510 vnic->fw_rss_cos_lb_ctx[ctx_idx] = 6511 le16_to_cpu(resp->rss_cos_lb_ctx_id); 6512 hwrm_req_drop(bp, req); 6513 6514 return rc; 6515 } 6516 6517 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp) 6518 { 6519 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP) 6520 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE; 6521 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE; 6522 } 6523 6524 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic) 6525 { 6526 struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 6527 struct hwrm_vnic_cfg_input *req; 6528 unsigned int ring = 0, grp_idx; 6529 u16 def_vlan = 0; 6530 int rc; 6531 6532 rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG); 6533 if (rc) 6534 return rc; 6535 6536 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6537 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0]; 6538 6539 req->default_rx_ring_id = 6540 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id); 6541 req->default_cmpl_ring_id = 6542 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr)); 6543 req->enables = 6544 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID | 6545 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID); 6546 goto vnic_mru; 6547 } 6548 req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP); 6549 /* Only RSS support for now TBD: COS & LB */ 6550 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) { 6551 req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]); 6552 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6553 VNIC_CFG_REQ_ENABLES_MRU); 6554 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) { 6555 req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]); 6556 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE | 6557 VNIC_CFG_REQ_ENABLES_MRU); 6558 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE); 6559 } else { 6560 req->rss_rule = cpu_to_le16(0xffff); 6561 } 6562 6563 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && 6564 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) { 6565 req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]); 6566 req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE); 6567 } else { 6568 req->cos_rule = cpu_to_le16(0xffff); 6569 } 6570 6571 if (vnic->flags & BNXT_VNIC_RSS_FLAG) 6572 ring = 0; 6573 else if (vnic->flags & BNXT_VNIC_RFS_FLAG) 6574 ring = vnic->vnic_id - 1; 6575 else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp)) 6576 ring = bp->rx_nr_rings - 1; 6577 6578 grp_idx = bp->rx_ring[ring].bnapi->index; 6579 req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id); 6580 req->lb_rule = cpu_to_le16(0xffff); 6581 vnic_mru: 6582 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 6583 req->mru = cpu_to_le16(vnic->mru); 6584 6585 req->vnic_id = cpu_to_le16(vnic->fw_vnic_id); 6586 #ifdef CONFIG_BNXT_SRIOV 6587 if (BNXT_VF(bp)) 6588 def_vlan = bp->vf.vlan; 6589 #endif 6590 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan) 6591 req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE); 6592 if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev)) 6593 req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp)); 6594 6595 return hwrm_req_send(bp, req); 6596 } 6597 6598 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, 6599 struct bnxt_vnic_info *vnic) 6600 { 6601 if (vnic->fw_vnic_id != INVALID_HW_RING_ID) { 6602 struct hwrm_vnic_free_input *req; 6603 6604 if (hwrm_req_init(bp, req, HWRM_VNIC_FREE)) 6605 return; 6606 6607 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 6608 6609 hwrm_req_send(bp, req); 6610 vnic->fw_vnic_id = INVALID_HW_RING_ID; 6611 } 6612 } 6613 6614 static void bnxt_hwrm_vnic_free(struct bnxt *bp) 6615 { 6616 u16 i; 6617 6618 for (i = 0; i < bp->nr_vnics; i++) 6619 bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]); 6620 } 6621 6622 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic, 6623 unsigned int start_rx_ring_idx, 6624 unsigned int nr_rings) 6625 { 6626 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings; 6627 struct hwrm_vnic_alloc_output *resp; 6628 struct hwrm_vnic_alloc_input *req; 6629 int rc; 6630 6631 rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC); 6632 if (rc) 6633 return rc; 6634 6635 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6636 goto vnic_no_ring_grps; 6637 6638 /* map ring groups to this vnic */ 6639 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) { 6640 grp_idx = bp->rx_ring[i].bnapi->index; 6641 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) { 6642 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n", 6643 j, nr_rings); 6644 break; 6645 } 6646 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id; 6647 } 6648 6649 vnic_no_ring_grps: 6650 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) 6651 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID; 6652 if (vnic->vnic_id == BNXT_VNIC_DEFAULT) 6653 req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT); 6654 6655 resp = hwrm_req_hold(bp, req); 6656 rc = hwrm_req_send(bp, req); 6657 if (!rc) 6658 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id); 6659 hwrm_req_drop(bp, req); 6660 return rc; 6661 } 6662 6663 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp) 6664 { 6665 struct hwrm_vnic_qcaps_output *resp; 6666 struct hwrm_vnic_qcaps_input *req; 6667 int rc; 6668 6669 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats); 6670 bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP; 6671 bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP; 6672 if (bp->hwrm_spec_code < 0x10600) 6673 return 0; 6674 6675 rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS); 6676 if (rc) 6677 return rc; 6678 6679 resp = hwrm_req_hold(bp, req); 6680 rc = hwrm_req_send(bp, req); 6681 if (!rc) { 6682 u32 flags = le32_to_cpu(resp->flags); 6683 6684 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 6685 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)) 6686 bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP; 6687 if (flags & 6688 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP) 6689 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP; 6690 6691 /* Older P5 fw before EXT_HW_STATS support did not set 6692 * VLAN_STRIP_CAP properly. 6693 */ 6694 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) || 6695 (BNXT_CHIP_P5(bp) && 6696 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))) 6697 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP; 6698 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP) 6699 bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA; 6700 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED) 6701 bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM; 6702 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported); 6703 if (bp->max_tpa_v2) { 6704 if (BNXT_CHIP_P5(bp)) 6705 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5; 6706 else 6707 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7; 6708 } 6709 if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP) 6710 bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA; 6711 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP) 6712 bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP; 6713 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP) 6714 bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP; 6715 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP) 6716 bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP; 6717 if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP) 6718 bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP; 6719 if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP) 6720 bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH; 6721 } 6722 hwrm_req_drop(bp, req); 6723 return rc; 6724 } 6725 6726 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp) 6727 { 6728 struct hwrm_ring_grp_alloc_output *resp; 6729 struct hwrm_ring_grp_alloc_input *req; 6730 int rc; 6731 u16 i; 6732 6733 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 6734 return 0; 6735 6736 rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC); 6737 if (rc) 6738 return rc; 6739 6740 resp = hwrm_req_hold(bp, req); 6741 for (i = 0; i < bp->rx_nr_rings; i++) { 6742 unsigned int grp_idx = bp->rx_ring[i].bnapi->index; 6743 6744 req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id); 6745 req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id); 6746 req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id); 6747 req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx); 6748 6749 rc = hwrm_req_send(bp, req); 6750 6751 if (rc) 6752 break; 6753 6754 bp->grp_info[grp_idx].fw_grp_id = 6755 le32_to_cpu(resp->ring_group_id); 6756 } 6757 hwrm_req_drop(bp, req); 6758 return rc; 6759 } 6760 6761 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp) 6762 { 6763 struct hwrm_ring_grp_free_input *req; 6764 u16 i; 6765 6766 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 6767 return; 6768 6769 if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE)) 6770 return; 6771 6772 hwrm_req_hold(bp, req); 6773 for (i = 0; i < bp->cp_nr_rings; i++) { 6774 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID) 6775 continue; 6776 req->ring_group_id = 6777 cpu_to_le32(bp->grp_info[i].fw_grp_id); 6778 6779 hwrm_req_send(bp, req); 6780 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID; 6781 } 6782 hwrm_req_drop(bp, req); 6783 } 6784 6785 static int hwrm_ring_alloc_send_msg(struct bnxt *bp, 6786 struct bnxt_ring_struct *ring, 6787 u32 ring_type, u32 map_index) 6788 { 6789 struct hwrm_ring_alloc_output *resp; 6790 struct hwrm_ring_alloc_input *req; 6791 struct bnxt_ring_mem_info *rmem = &ring->ring_mem; 6792 struct bnxt_ring_grp_info *grp_info; 6793 int rc, err = 0; 6794 u16 ring_id; 6795 6796 rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC); 6797 if (rc) 6798 goto exit; 6799 6800 req->enables = 0; 6801 if (rmem->nr_pages > 1) { 6802 req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map); 6803 /* Page size is in log2 units */ 6804 req->page_size = BNXT_PAGE_SHIFT; 6805 req->page_tbl_depth = 1; 6806 } else { 6807 req->page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]); 6808 } 6809 req->fbo = 0; 6810 /* Association of ring index with doorbell index and MSIX number */ 6811 req->logical_id = cpu_to_le16(map_index); 6812 6813 switch (ring_type) { 6814 case HWRM_RING_ALLOC_TX: { 6815 struct bnxt_tx_ring_info *txr; 6816 u16 flags = 0; 6817 6818 txr = container_of(ring, struct bnxt_tx_ring_info, 6819 tx_ring_struct); 6820 req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX; 6821 /* Association of transmit ring with completion ring */ 6822 grp_info = &bp->grp_info[ring->grp_idx]; 6823 req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr)); 6824 req->length = cpu_to_le32(bp->tx_ring_mask + 1); 6825 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6826 req->queue_id = cpu_to_le16(ring->queue_id); 6827 if (bp->flags & BNXT_FLAG_TX_COAL_CMPL) 6828 req->cmpl_coal_cnt = 6829 RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64; 6830 if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg) 6831 flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE; 6832 req->flags = cpu_to_le16(flags); 6833 break; 6834 } 6835 case HWRM_RING_ALLOC_RX: 6836 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6837 req->length = cpu_to_le32(bp->rx_ring_mask + 1); 6838 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6839 u16 flags = 0; 6840 6841 /* Association of rx ring with stats context */ 6842 grp_info = &bp->grp_info[ring->grp_idx]; 6843 req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size); 6844 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6845 req->enables |= cpu_to_le32( 6846 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6847 if (NET_IP_ALIGN == 2) 6848 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD; 6849 req->flags = cpu_to_le16(flags); 6850 } 6851 break; 6852 case HWRM_RING_ALLOC_AGG: 6853 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6854 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG; 6855 /* Association of agg ring with rx ring */ 6856 grp_info = &bp->grp_info[ring->grp_idx]; 6857 req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id); 6858 req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE); 6859 req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx); 6860 req->enables |= cpu_to_le32( 6861 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID | 6862 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID); 6863 } else { 6864 req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX; 6865 } 6866 req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1); 6867 break; 6868 case HWRM_RING_ALLOC_CMPL: 6869 req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL; 6870 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 6871 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6872 /* Association of cp ring with nq */ 6873 grp_info = &bp->grp_info[map_index]; 6874 req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id); 6875 req->cq_handle = cpu_to_le64(ring->handle); 6876 req->enables |= cpu_to_le32( 6877 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID); 6878 } else if (bp->flags & BNXT_FLAG_USING_MSIX) { 6879 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 6880 } 6881 break; 6882 case HWRM_RING_ALLOC_NQ: 6883 req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ; 6884 req->length = cpu_to_le32(bp->cp_ring_mask + 1); 6885 if (bp->flags & BNXT_FLAG_USING_MSIX) 6886 req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX; 6887 break; 6888 default: 6889 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n", 6890 ring_type); 6891 return -1; 6892 } 6893 6894 resp = hwrm_req_hold(bp, req); 6895 rc = hwrm_req_send(bp, req); 6896 err = le16_to_cpu(resp->error_code); 6897 ring_id = le16_to_cpu(resp->ring_id); 6898 hwrm_req_drop(bp, req); 6899 6900 exit: 6901 if (rc || err) { 6902 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n", 6903 ring_type, rc, err); 6904 return -EIO; 6905 } 6906 ring->fw_ring_id = ring_id; 6907 return rc; 6908 } 6909 6910 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx) 6911 { 6912 int rc; 6913 6914 if (BNXT_PF(bp)) { 6915 struct hwrm_func_cfg_input *req; 6916 6917 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 6918 if (rc) 6919 return rc; 6920 6921 req->fid = cpu_to_le16(0xffff); 6922 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 6923 req->async_event_cr = cpu_to_le16(idx); 6924 return hwrm_req_send(bp, req); 6925 } else { 6926 struct hwrm_func_vf_cfg_input *req; 6927 6928 rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG); 6929 if (rc) 6930 return rc; 6931 6932 req->enables = 6933 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR); 6934 req->async_event_cr = cpu_to_le16(idx); 6935 return hwrm_req_send(bp, req); 6936 } 6937 } 6938 6939 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db, 6940 u32 ring_type) 6941 { 6942 switch (ring_type) { 6943 case HWRM_RING_ALLOC_TX: 6944 db->db_ring_mask = bp->tx_ring_mask; 6945 break; 6946 case HWRM_RING_ALLOC_RX: 6947 db->db_ring_mask = bp->rx_ring_mask; 6948 break; 6949 case HWRM_RING_ALLOC_AGG: 6950 db->db_ring_mask = bp->rx_agg_ring_mask; 6951 break; 6952 case HWRM_RING_ALLOC_CMPL: 6953 case HWRM_RING_ALLOC_NQ: 6954 db->db_ring_mask = bp->cp_ring_mask; 6955 break; 6956 } 6957 if (bp->flags & BNXT_FLAG_CHIP_P7) { 6958 db->db_epoch_mask = db->db_ring_mask + 1; 6959 db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask); 6960 } 6961 } 6962 6963 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type, 6964 u32 map_idx, u32 xid) 6965 { 6966 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 6967 switch (ring_type) { 6968 case HWRM_RING_ALLOC_TX: 6969 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ; 6970 break; 6971 case HWRM_RING_ALLOC_RX: 6972 case HWRM_RING_ALLOC_AGG: 6973 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ; 6974 break; 6975 case HWRM_RING_ALLOC_CMPL: 6976 db->db_key64 = DBR_PATH_L2; 6977 break; 6978 case HWRM_RING_ALLOC_NQ: 6979 db->db_key64 = DBR_PATH_L2; 6980 break; 6981 } 6982 db->db_key64 |= (u64)xid << DBR_XID_SFT; 6983 6984 if (bp->flags & BNXT_FLAG_CHIP_P7) 6985 db->db_key64 |= DBR_VALID; 6986 6987 db->doorbell = bp->bar1 + bp->db_offset; 6988 } else { 6989 db->doorbell = bp->bar1 + map_idx * 0x80; 6990 switch (ring_type) { 6991 case HWRM_RING_ALLOC_TX: 6992 db->db_key32 = DB_KEY_TX; 6993 break; 6994 case HWRM_RING_ALLOC_RX: 6995 case HWRM_RING_ALLOC_AGG: 6996 db->db_key32 = DB_KEY_RX; 6997 break; 6998 case HWRM_RING_ALLOC_CMPL: 6999 db->db_key32 = DB_KEY_CP; 7000 break; 7001 } 7002 } 7003 bnxt_set_db_mask(bp, db, ring_type); 7004 } 7005 7006 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp, 7007 struct bnxt_rx_ring_info *rxr) 7008 { 7009 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7010 struct bnxt_napi *bnapi = rxr->bnapi; 7011 u32 type = HWRM_RING_ALLOC_RX; 7012 u32 map_idx = bnapi->index; 7013 int rc; 7014 7015 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7016 if (rc) 7017 return rc; 7018 7019 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id); 7020 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id; 7021 7022 return 0; 7023 } 7024 7025 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp, 7026 struct bnxt_rx_ring_info *rxr) 7027 { 7028 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7029 u32 type = HWRM_RING_ALLOC_AGG; 7030 u32 grp_idx = ring->grp_idx; 7031 u32 map_idx; 7032 int rc; 7033 7034 map_idx = grp_idx + bp->rx_nr_rings; 7035 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7036 if (rc) 7037 return rc; 7038 7039 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx, 7040 ring->fw_ring_id); 7041 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 7042 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7043 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id; 7044 7045 return 0; 7046 } 7047 7048 static int bnxt_hwrm_ring_alloc(struct bnxt *bp) 7049 { 7050 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS); 7051 int i, rc = 0; 7052 u32 type; 7053 7054 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7055 type = HWRM_RING_ALLOC_NQ; 7056 else 7057 type = HWRM_RING_ALLOC_CMPL; 7058 for (i = 0; i < bp->cp_nr_rings; i++) { 7059 struct bnxt_napi *bnapi = bp->bnapi[i]; 7060 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7061 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct; 7062 u32 map_idx = ring->map_idx; 7063 unsigned int vector; 7064 7065 vector = bp->irq_tbl[map_idx].vector; 7066 disable_irq_nosync(vector); 7067 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7068 if (rc) { 7069 enable_irq(vector); 7070 goto err_out; 7071 } 7072 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id); 7073 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons); 7074 enable_irq(vector); 7075 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id; 7076 7077 if (!i) { 7078 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id); 7079 if (rc) 7080 netdev_warn(bp->dev, "Failed to set async event completion ring.\n"); 7081 } 7082 } 7083 7084 type = HWRM_RING_ALLOC_TX; 7085 for (i = 0; i < bp->tx_nr_rings; i++) { 7086 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7087 struct bnxt_ring_struct *ring; 7088 u32 map_idx; 7089 7090 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7091 struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr; 7092 struct bnxt_napi *bnapi = txr->bnapi; 7093 u32 type2 = HWRM_RING_ALLOC_CMPL; 7094 7095 ring = &cpr2->cp_ring_struct; 7096 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7097 map_idx = bnapi->index; 7098 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7099 if (rc) 7100 goto err_out; 7101 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7102 ring->fw_ring_id); 7103 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7104 } 7105 ring = &txr->tx_ring_struct; 7106 map_idx = i; 7107 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx); 7108 if (rc) 7109 goto err_out; 7110 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id); 7111 } 7112 7113 for (i = 0; i < bp->rx_nr_rings; i++) { 7114 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 7115 7116 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 7117 if (rc) 7118 goto err_out; 7119 /* If we have agg rings, post agg buffers first. */ 7120 if (!agg_rings) 7121 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 7122 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7123 struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr; 7124 struct bnxt_napi *bnapi = rxr->bnapi; 7125 u32 type2 = HWRM_RING_ALLOC_CMPL; 7126 struct bnxt_ring_struct *ring; 7127 u32 map_idx = bnapi->index; 7128 7129 ring = &cpr2->cp_ring_struct; 7130 ring->handle = BNXT_SET_NQ_HDL(cpr2); 7131 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx); 7132 if (rc) 7133 goto err_out; 7134 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx, 7135 ring->fw_ring_id); 7136 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons); 7137 } 7138 } 7139 7140 if (agg_rings) { 7141 for (i = 0; i < bp->rx_nr_rings; i++) { 7142 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]); 7143 if (rc) 7144 goto err_out; 7145 } 7146 } 7147 err_out: 7148 return rc; 7149 } 7150 7151 static int hwrm_ring_free_send_msg(struct bnxt *bp, 7152 struct bnxt_ring_struct *ring, 7153 u32 ring_type, int cmpl_ring_id) 7154 { 7155 struct hwrm_ring_free_output *resp; 7156 struct hwrm_ring_free_input *req; 7157 u16 error_code = 0; 7158 int rc; 7159 7160 if (BNXT_NO_FW_ACCESS(bp)) 7161 return 0; 7162 7163 rc = hwrm_req_init(bp, req, HWRM_RING_FREE); 7164 if (rc) 7165 goto exit; 7166 7167 req->cmpl_ring = cpu_to_le16(cmpl_ring_id); 7168 req->ring_type = ring_type; 7169 req->ring_id = cpu_to_le16(ring->fw_ring_id); 7170 7171 resp = hwrm_req_hold(bp, req); 7172 rc = hwrm_req_send(bp, req); 7173 error_code = le16_to_cpu(resp->error_code); 7174 hwrm_req_drop(bp, req); 7175 exit: 7176 if (rc || error_code) { 7177 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n", 7178 ring_type, rc, error_code); 7179 return -EIO; 7180 } 7181 return 0; 7182 } 7183 7184 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp, 7185 struct bnxt_rx_ring_info *rxr, 7186 bool close_path) 7187 { 7188 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct; 7189 u32 grp_idx = rxr->bnapi->index; 7190 u32 cmpl_ring_id; 7191 7192 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7193 return; 7194 7195 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7196 hwrm_ring_free_send_msg(bp, ring, 7197 RING_FREE_REQ_RING_TYPE_RX, 7198 close_path ? cmpl_ring_id : 7199 INVALID_HW_RING_ID); 7200 ring->fw_ring_id = INVALID_HW_RING_ID; 7201 bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID; 7202 } 7203 7204 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp, 7205 struct bnxt_rx_ring_info *rxr, 7206 bool close_path) 7207 { 7208 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct; 7209 u32 grp_idx = rxr->bnapi->index; 7210 u32 type, cmpl_ring_id; 7211 7212 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7213 type = RING_FREE_REQ_RING_TYPE_RX_AGG; 7214 else 7215 type = RING_FREE_REQ_RING_TYPE_RX; 7216 7217 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7218 return; 7219 7220 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr); 7221 hwrm_ring_free_send_msg(bp, ring, type, 7222 close_path ? cmpl_ring_id : 7223 INVALID_HW_RING_ID); 7224 ring->fw_ring_id = INVALID_HW_RING_ID; 7225 bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID; 7226 } 7227 7228 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path) 7229 { 7230 u32 type; 7231 int i; 7232 7233 if (!bp->bnapi) 7234 return; 7235 7236 for (i = 0; i < bp->tx_nr_rings; i++) { 7237 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i]; 7238 struct bnxt_ring_struct *ring = &txr->tx_ring_struct; 7239 7240 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7241 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr); 7242 7243 hwrm_ring_free_send_msg(bp, ring, 7244 RING_FREE_REQ_RING_TYPE_TX, 7245 close_path ? cmpl_ring_id : 7246 INVALID_HW_RING_ID); 7247 ring->fw_ring_id = INVALID_HW_RING_ID; 7248 } 7249 } 7250 7251 for (i = 0; i < bp->rx_nr_rings; i++) { 7252 bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path); 7253 bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path); 7254 } 7255 7256 /* The completion rings are about to be freed. After that the 7257 * IRQ doorbell will not work anymore. So we need to disable 7258 * IRQ here. 7259 */ 7260 bnxt_disable_int_sync(bp); 7261 7262 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7263 type = RING_FREE_REQ_RING_TYPE_NQ; 7264 else 7265 type = RING_FREE_REQ_RING_TYPE_L2_CMPL; 7266 for (i = 0; i < bp->cp_nr_rings; i++) { 7267 struct bnxt_napi *bnapi = bp->bnapi[i]; 7268 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7269 struct bnxt_ring_struct *ring; 7270 int j; 7271 7272 for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) { 7273 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 7274 7275 ring = &cpr2->cp_ring_struct; 7276 if (ring->fw_ring_id == INVALID_HW_RING_ID) 7277 continue; 7278 hwrm_ring_free_send_msg(bp, ring, 7279 RING_FREE_REQ_RING_TYPE_L2_CMPL, 7280 INVALID_HW_RING_ID); 7281 ring->fw_ring_id = INVALID_HW_RING_ID; 7282 } 7283 ring = &cpr->cp_ring_struct; 7284 if (ring->fw_ring_id != INVALID_HW_RING_ID) { 7285 hwrm_ring_free_send_msg(bp, ring, type, 7286 INVALID_HW_RING_ID); 7287 ring->fw_ring_id = INVALID_HW_RING_ID; 7288 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID; 7289 } 7290 } 7291 } 7292 7293 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7294 bool shared); 7295 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 7296 bool shared); 7297 7298 static int bnxt_hwrm_get_rings(struct bnxt *bp) 7299 { 7300 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7301 struct hwrm_func_qcfg_output *resp; 7302 struct hwrm_func_qcfg_input *req; 7303 int rc; 7304 7305 if (bp->hwrm_spec_code < 0x10601) 7306 return 0; 7307 7308 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7309 if (rc) 7310 return rc; 7311 7312 req->fid = cpu_to_le16(0xffff); 7313 resp = hwrm_req_hold(bp, req); 7314 rc = hwrm_req_send(bp, req); 7315 if (rc) { 7316 hwrm_req_drop(bp, req); 7317 return rc; 7318 } 7319 7320 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7321 if (BNXT_NEW_RM(bp)) { 7322 u16 cp, stats; 7323 7324 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings); 7325 hw_resc->resv_hw_ring_grps = 7326 le32_to_cpu(resp->alloc_hw_ring_grps); 7327 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics); 7328 hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx); 7329 cp = le16_to_cpu(resp->alloc_cmpl_rings); 7330 stats = le16_to_cpu(resp->alloc_stat_ctx); 7331 hw_resc->resv_irqs = cp; 7332 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7333 int rx = hw_resc->resv_rx_rings; 7334 int tx = hw_resc->resv_tx_rings; 7335 7336 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7337 rx >>= 1; 7338 if (cp < (rx + tx)) { 7339 rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false); 7340 if (rc) 7341 goto get_rings_exit; 7342 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7343 rx <<= 1; 7344 hw_resc->resv_rx_rings = rx; 7345 hw_resc->resv_tx_rings = tx; 7346 } 7347 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix); 7348 hw_resc->resv_hw_ring_grps = rx; 7349 } 7350 hw_resc->resv_cp_rings = cp; 7351 hw_resc->resv_stat_ctxs = stats; 7352 } 7353 get_rings_exit: 7354 hwrm_req_drop(bp, req); 7355 return rc; 7356 } 7357 7358 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings) 7359 { 7360 struct hwrm_func_qcfg_output *resp; 7361 struct hwrm_func_qcfg_input *req; 7362 int rc; 7363 7364 if (bp->hwrm_spec_code < 0x10601) 7365 return 0; 7366 7367 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 7368 if (rc) 7369 return rc; 7370 7371 req->fid = cpu_to_le16(fid); 7372 resp = hwrm_req_hold(bp, req); 7373 rc = hwrm_req_send(bp, req); 7374 if (!rc) 7375 *tx_rings = le16_to_cpu(resp->alloc_tx_rings); 7376 7377 hwrm_req_drop(bp, req); 7378 return rc; 7379 } 7380 7381 static bool bnxt_rfs_supported(struct bnxt *bp); 7382 7383 static struct hwrm_func_cfg_input * 7384 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7385 { 7386 struct hwrm_func_cfg_input *req; 7387 u32 enables = 0; 7388 7389 if (bnxt_hwrm_func_cfg_short_req_init(bp, &req)) 7390 return NULL; 7391 7392 req->fid = cpu_to_le16(0xffff); 7393 enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7394 req->num_tx_rings = cpu_to_le16(hwr->tx); 7395 if (BNXT_NEW_RM(bp)) { 7396 enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0; 7397 enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7398 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7399 enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0; 7400 enables |= hwr->cp_p5 ? 7401 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7402 } else { 7403 enables |= hwr->cp ? 7404 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7405 enables |= hwr->grp ? 7406 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7407 } 7408 enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0; 7409 enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 7410 0; 7411 req->num_rx_rings = cpu_to_le16(hwr->rx); 7412 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7413 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7414 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7415 req->num_msix = cpu_to_le16(hwr->cp); 7416 } else { 7417 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7418 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7419 } 7420 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7421 req->num_vnics = cpu_to_le16(hwr->vnic); 7422 } 7423 req->enables = cpu_to_le32(enables); 7424 return req; 7425 } 7426 7427 static struct hwrm_func_vf_cfg_input * 7428 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7429 { 7430 struct hwrm_func_vf_cfg_input *req; 7431 u32 enables = 0; 7432 7433 if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG)) 7434 return NULL; 7435 7436 enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0; 7437 enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS | 7438 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7439 enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0; 7440 enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0; 7441 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7442 enables |= hwr->cp_p5 ? 7443 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7444 } else { 7445 enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0; 7446 enables |= hwr->grp ? 7447 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0; 7448 } 7449 enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0; 7450 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS; 7451 7452 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX); 7453 req->num_tx_rings = cpu_to_le16(hwr->tx); 7454 req->num_rx_rings = cpu_to_le16(hwr->rx); 7455 req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx); 7456 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7457 req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5); 7458 } else { 7459 req->num_cmpl_rings = cpu_to_le16(hwr->cp); 7460 req->num_hw_ring_grps = cpu_to_le16(hwr->grp); 7461 } 7462 req->num_stat_ctxs = cpu_to_le16(hwr->stat); 7463 req->num_vnics = cpu_to_le16(hwr->vnic); 7464 7465 req->enables = cpu_to_le32(enables); 7466 return req; 7467 } 7468 7469 static int 7470 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7471 { 7472 struct hwrm_func_cfg_input *req; 7473 int rc; 7474 7475 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7476 if (!req) 7477 return -ENOMEM; 7478 7479 if (!req->enables) { 7480 hwrm_req_drop(bp, req); 7481 return 0; 7482 } 7483 7484 rc = hwrm_req_send(bp, req); 7485 if (rc) 7486 return rc; 7487 7488 if (bp->hwrm_spec_code < 0x10601) 7489 bp->hw_resc.resv_tx_rings = hwr->tx; 7490 7491 return bnxt_hwrm_get_rings(bp); 7492 } 7493 7494 static int 7495 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7496 { 7497 struct hwrm_func_vf_cfg_input *req; 7498 int rc; 7499 7500 if (!BNXT_NEW_RM(bp)) { 7501 bp->hw_resc.resv_tx_rings = hwr->tx; 7502 return 0; 7503 } 7504 7505 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7506 if (!req) 7507 return -ENOMEM; 7508 7509 rc = hwrm_req_send(bp, req); 7510 if (rc) 7511 return rc; 7512 7513 return bnxt_hwrm_get_rings(bp); 7514 } 7515 7516 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7517 { 7518 if (BNXT_PF(bp)) 7519 return bnxt_hwrm_reserve_pf_rings(bp, hwr); 7520 else 7521 return bnxt_hwrm_reserve_vf_rings(bp, hwr); 7522 } 7523 7524 int bnxt_nq_rings_in_use(struct bnxt *bp) 7525 { 7526 return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp); 7527 } 7528 7529 static int bnxt_cp_rings_in_use(struct bnxt *bp) 7530 { 7531 int cp; 7532 7533 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7534 return bnxt_nq_rings_in_use(bp); 7535 7536 cp = bp->tx_nr_rings + bp->rx_nr_rings; 7537 return cp; 7538 } 7539 7540 static int bnxt_get_func_stat_ctxs(struct bnxt *bp) 7541 { 7542 return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp); 7543 } 7544 7545 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7546 { 7547 if (!hwr->grp) 7548 return 0; 7549 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 7550 int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp); 7551 7552 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7553 rss_ctx *= hwr->vnic; 7554 return rss_ctx; 7555 } 7556 if (BNXT_VF(bp)) 7557 return BNXT_VF_MAX_RSS_CTX; 7558 if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp)) 7559 return hwr->grp + 1; 7560 return 1; 7561 } 7562 7563 /* Check if a default RSS map needs to be setup. This function is only 7564 * used on older firmware that does not require reserving RX rings. 7565 */ 7566 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp) 7567 { 7568 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7569 7570 /* The RSS map is valid for RX rings set to resv_rx_rings */ 7571 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) { 7572 hw_resc->resv_rx_rings = bp->rx_nr_rings; 7573 if (!netif_is_rxfh_configured(bp->dev)) 7574 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7575 } 7576 } 7577 7578 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings) 7579 { 7580 if (bp->flags & BNXT_FLAG_RFS) { 7581 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 7582 return 2 + bp->num_rss_ctx; 7583 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7584 return rx_rings + 1; 7585 } 7586 return 1; 7587 } 7588 7589 static bool bnxt_need_reserve_rings(struct bnxt *bp) 7590 { 7591 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7592 int cp = bnxt_cp_rings_in_use(bp); 7593 int nq = bnxt_nq_rings_in_use(bp); 7594 int rx = bp->rx_nr_rings, stat; 7595 int vnic, grp = rx; 7596 7597 /* Old firmware does not need RX ring reservations but we still 7598 * need to setup a default RSS map when needed. With new firmware 7599 * we go through RX ring reservations first and then set up the 7600 * RSS map for the successfully reserved RX rings when needed. 7601 */ 7602 if (!BNXT_NEW_RM(bp)) 7603 bnxt_check_rss_tbl_no_rmgr(bp); 7604 7605 if (hw_resc->resv_tx_rings != bp->tx_nr_rings && 7606 bp->hwrm_spec_code >= 0x10601) 7607 return true; 7608 7609 if (!BNXT_NEW_RM(bp)) 7610 return false; 7611 7612 vnic = bnxt_get_total_vnics(bp, rx); 7613 7614 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7615 rx <<= 1; 7616 stat = bnxt_get_func_stat_ctxs(bp); 7617 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp || 7618 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat || 7619 (hw_resc->resv_hw_ring_grps != grp && 7620 !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))) 7621 return true; 7622 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) && 7623 hw_resc->resv_irqs != nq) 7624 return true; 7625 return false; 7626 } 7627 7628 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7629 { 7630 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 7631 7632 hwr->tx = hw_resc->resv_tx_rings; 7633 if (BNXT_NEW_RM(bp)) { 7634 hwr->rx = hw_resc->resv_rx_rings; 7635 hwr->cp = hw_resc->resv_irqs; 7636 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7637 hwr->cp_p5 = hw_resc->resv_cp_rings; 7638 hwr->grp = hw_resc->resv_hw_ring_grps; 7639 hwr->vnic = hw_resc->resv_vnics; 7640 hwr->stat = hw_resc->resv_stat_ctxs; 7641 hwr->rss_ctx = hw_resc->resv_rsscos_ctxs; 7642 } 7643 } 7644 7645 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7646 { 7647 return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic && 7648 hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)); 7649 } 7650 7651 static int bnxt_get_avail_msix(struct bnxt *bp, int num); 7652 7653 static int __bnxt_reserve_rings(struct bnxt *bp) 7654 { 7655 struct bnxt_hw_rings hwr = {0}; 7656 int rx_rings, old_rx_rings, rc; 7657 int cp = bp->cp_nr_rings; 7658 int ulp_msix = 0; 7659 bool sh = false; 7660 int tx_cp; 7661 7662 if (!bnxt_need_reserve_rings(bp)) 7663 return 0; 7664 7665 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 7666 ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 7667 if (!ulp_msix) 7668 bnxt_set_ulp_stat_ctxs(bp, 0); 7669 7670 if (ulp_msix > bp->ulp_num_msix_want) 7671 ulp_msix = bp->ulp_num_msix_want; 7672 hwr.cp = cp + ulp_msix; 7673 } else { 7674 hwr.cp = bnxt_nq_rings_in_use(bp); 7675 } 7676 7677 hwr.tx = bp->tx_nr_rings; 7678 hwr.rx = bp->rx_nr_rings; 7679 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 7680 sh = true; 7681 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7682 hwr.cp_p5 = hwr.rx + hwr.tx; 7683 7684 hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx); 7685 7686 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7687 hwr.rx <<= 1; 7688 hwr.grp = bp->rx_nr_rings; 7689 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 7690 hwr.stat = bnxt_get_func_stat_ctxs(bp); 7691 old_rx_rings = bp->hw_resc.resv_rx_rings; 7692 7693 rc = bnxt_hwrm_reserve_rings(bp, &hwr); 7694 if (rc) 7695 return rc; 7696 7697 bnxt_copy_reserved_rings(bp, &hwr); 7698 7699 rx_rings = hwr.rx; 7700 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 7701 if (hwr.rx >= 2) { 7702 rx_rings = hwr.rx >> 1; 7703 } else { 7704 if (netif_running(bp->dev)) 7705 return -ENOMEM; 7706 7707 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 7708 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 7709 bp->dev->hw_features &= ~NETIF_F_LRO; 7710 bp->dev->features &= ~NETIF_F_LRO; 7711 bnxt_set_ring_params(bp); 7712 } 7713 } 7714 rx_rings = min_t(int, rx_rings, hwr.grp); 7715 hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings); 7716 if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp)) 7717 hwr.stat -= bnxt_get_ulp_stat_ctxs(bp); 7718 hwr.cp = min_t(int, hwr.cp, hwr.stat); 7719 rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh); 7720 if (bp->flags & BNXT_FLAG_AGG_RINGS) 7721 hwr.rx = rx_rings << 1; 7722 tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx); 7723 hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings; 7724 bp->tx_nr_rings = hwr.tx; 7725 7726 /* If we cannot reserve all the RX rings, reset the RSS map only 7727 * if absolutely necessary 7728 */ 7729 if (rx_rings != bp->rx_nr_rings) { 7730 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n", 7731 rx_rings, bp->rx_nr_rings); 7732 if (netif_is_rxfh_configured(bp->dev) && 7733 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) != 7734 bnxt_get_nr_rss_ctxs(bp, rx_rings) || 7735 bnxt_get_max_rss_ring(bp) >= rx_rings)) { 7736 netdev_warn(bp->dev, "RSS table entries reverting to default\n"); 7737 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED; 7738 } 7739 } 7740 bp->rx_nr_rings = rx_rings; 7741 bp->cp_nr_rings = hwr.cp; 7742 7743 if (!bnxt_rings_ok(bp, &hwr)) 7744 return -ENOMEM; 7745 7746 if (old_rx_rings != bp->hw_resc.resv_rx_rings && 7747 !netif_is_rxfh_configured(bp->dev)) 7748 bnxt_set_dflt_rss_indir_tbl(bp, NULL); 7749 7750 if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) { 7751 int resv_msix, resv_ctx, ulp_ctxs; 7752 struct bnxt_hw_resc *hw_resc; 7753 7754 hw_resc = &bp->hw_resc; 7755 resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings; 7756 ulp_msix = min_t(int, resv_msix, ulp_msix); 7757 bnxt_set_ulp_msix_num(bp, ulp_msix); 7758 resv_ctx = hw_resc->resv_stat_ctxs - bp->cp_nr_rings; 7759 ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp)); 7760 bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs); 7761 } 7762 7763 return rc; 7764 } 7765 7766 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7767 { 7768 struct hwrm_func_vf_cfg_input *req; 7769 u32 flags; 7770 7771 if (!BNXT_NEW_RM(bp)) 7772 return 0; 7773 7774 req = __bnxt_hwrm_reserve_vf_rings(bp, hwr); 7775 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST | 7776 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7777 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7778 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7779 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST | 7780 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST; 7781 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 7782 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7783 7784 req->flags = cpu_to_le32(flags); 7785 return hwrm_req_send_silent(bp, req); 7786 } 7787 7788 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7789 { 7790 struct hwrm_func_cfg_input *req; 7791 u32 flags; 7792 7793 req = __bnxt_hwrm_reserve_pf_rings(bp, hwr); 7794 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST; 7795 if (BNXT_NEW_RM(bp)) { 7796 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST | 7797 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST | 7798 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST | 7799 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST; 7800 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 7801 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST | 7802 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST; 7803 else 7804 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST; 7805 } 7806 7807 req->flags = cpu_to_le32(flags); 7808 return hwrm_req_send_silent(bp, req); 7809 } 7810 7811 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr) 7812 { 7813 if (bp->hwrm_spec_code < 0x10801) 7814 return 0; 7815 7816 if (BNXT_PF(bp)) 7817 return bnxt_hwrm_check_pf_rings(bp, hwr); 7818 7819 return bnxt_hwrm_check_vf_rings(bp, hwr); 7820 } 7821 7822 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp) 7823 { 7824 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7825 struct hwrm_ring_aggint_qcaps_output *resp; 7826 struct hwrm_ring_aggint_qcaps_input *req; 7827 int rc; 7828 7829 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS; 7830 coal_cap->num_cmpl_dma_aggr_max = 63; 7831 coal_cap->num_cmpl_dma_aggr_during_int_max = 63; 7832 coal_cap->cmpl_aggr_dma_tmr_max = 65535; 7833 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535; 7834 coal_cap->int_lat_tmr_min_max = 65535; 7835 coal_cap->int_lat_tmr_max_max = 65535; 7836 coal_cap->num_cmpl_aggr_int_max = 65535; 7837 coal_cap->timer_units = 80; 7838 7839 if (bp->hwrm_spec_code < 0x10902) 7840 return; 7841 7842 if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS)) 7843 return; 7844 7845 resp = hwrm_req_hold(bp, req); 7846 rc = hwrm_req_send_silent(bp, req); 7847 if (!rc) { 7848 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params); 7849 coal_cap->nq_params = le32_to_cpu(resp->nq_params); 7850 coal_cap->num_cmpl_dma_aggr_max = 7851 le16_to_cpu(resp->num_cmpl_dma_aggr_max); 7852 coal_cap->num_cmpl_dma_aggr_during_int_max = 7853 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max); 7854 coal_cap->cmpl_aggr_dma_tmr_max = 7855 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max); 7856 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 7857 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max); 7858 coal_cap->int_lat_tmr_min_max = 7859 le16_to_cpu(resp->int_lat_tmr_min_max); 7860 coal_cap->int_lat_tmr_max_max = 7861 le16_to_cpu(resp->int_lat_tmr_max_max); 7862 coal_cap->num_cmpl_aggr_int_max = 7863 le16_to_cpu(resp->num_cmpl_aggr_int_max); 7864 coal_cap->timer_units = le16_to_cpu(resp->timer_units); 7865 } 7866 hwrm_req_drop(bp, req); 7867 } 7868 7869 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec) 7870 { 7871 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7872 7873 return usec * 1000 / coal_cap->timer_units; 7874 } 7875 7876 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, 7877 struct bnxt_coal *hw_coal, 7878 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 7879 { 7880 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7881 u16 val, tmr, max, flags = hw_coal->flags; 7882 u32 cmpl_params = coal_cap->cmpl_params; 7883 7884 max = hw_coal->bufs_per_record * 128; 7885 if (hw_coal->budget) 7886 max = hw_coal->bufs_per_record * hw_coal->budget; 7887 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max); 7888 7889 val = clamp_t(u16, hw_coal->coal_bufs, 1, max); 7890 req->num_cmpl_aggr_int = cpu_to_le16(val); 7891 7892 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max); 7893 req->num_cmpl_dma_aggr = cpu_to_le16(val); 7894 7895 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 7896 coal_cap->num_cmpl_dma_aggr_during_int_max); 7897 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val); 7898 7899 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks); 7900 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max); 7901 req->int_lat_tmr_max = cpu_to_le16(tmr); 7902 7903 /* min timer set to 1/2 of interrupt timer */ 7904 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) { 7905 val = tmr / 2; 7906 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max); 7907 req->int_lat_tmr_min = cpu_to_le16(val); 7908 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 7909 } 7910 7911 /* buf timer set to 1/4 of interrupt timer */ 7912 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max); 7913 req->cmpl_aggr_dma_tmr = cpu_to_le16(val); 7914 7915 if (cmpl_params & 7916 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) { 7917 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq); 7918 val = clamp_t(u16, tmr, 1, 7919 coal_cap->cmpl_aggr_dma_tmr_during_int_max); 7920 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val); 7921 req->enables |= 7922 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE); 7923 } 7924 7925 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) && 7926 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh) 7927 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE; 7928 req->flags = cpu_to_le16(flags); 7929 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES); 7930 } 7931 7932 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi, 7933 struct bnxt_coal *hw_coal) 7934 { 7935 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req; 7936 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7937 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 7938 u32 nq_params = coal_cap->nq_params; 7939 u16 tmr; 7940 int rc; 7941 7942 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN)) 7943 return 0; 7944 7945 rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7946 if (rc) 7947 return rc; 7948 7949 req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id); 7950 req->flags = 7951 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ); 7952 7953 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2; 7954 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max); 7955 req->int_lat_tmr_min = cpu_to_le16(tmr); 7956 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE); 7957 return hwrm_req_send(bp, req); 7958 } 7959 7960 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi) 7961 { 7962 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx; 7963 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 7964 struct bnxt_coal coal; 7965 int rc; 7966 7967 /* Tick values in micro seconds. 7968 * 1 coal_buf x bufs_per_record = 1 completion record. 7969 */ 7970 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal)); 7971 7972 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks; 7973 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs; 7974 7975 if (!bnapi->rx_ring) 7976 return -ENODEV; 7977 7978 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 7979 if (rc) 7980 return rc; 7981 7982 bnxt_hwrm_set_coal_params(bp, &coal, req_rx); 7983 7984 req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring)); 7985 7986 return hwrm_req_send(bp, req_rx); 7987 } 7988 7989 static int 7990 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 7991 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 7992 { 7993 u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring); 7994 7995 req->ring_id = cpu_to_le16(ring_id); 7996 return hwrm_req_send(bp, req); 7997 } 7998 7999 static int 8000 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi, 8001 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req) 8002 { 8003 struct bnxt_tx_ring_info *txr; 8004 int i, rc; 8005 8006 bnxt_for_each_napi_tx(i, bnapi, txr) { 8007 u16 ring_id; 8008 8009 ring_id = bnxt_cp_ring_for_tx(bp, txr); 8010 req->ring_id = cpu_to_le16(ring_id); 8011 rc = hwrm_req_send(bp, req); 8012 if (rc) 8013 return rc; 8014 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8015 return 0; 8016 } 8017 return 0; 8018 } 8019 8020 int bnxt_hwrm_set_coal(struct bnxt *bp) 8021 { 8022 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx; 8023 int i, rc; 8024 8025 rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8026 if (rc) 8027 return rc; 8028 8029 rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS); 8030 if (rc) { 8031 hwrm_req_drop(bp, req_rx); 8032 return rc; 8033 } 8034 8035 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx); 8036 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx); 8037 8038 hwrm_req_hold(bp, req_rx); 8039 hwrm_req_hold(bp, req_tx); 8040 for (i = 0; i < bp->cp_nr_rings; i++) { 8041 struct bnxt_napi *bnapi = bp->bnapi[i]; 8042 struct bnxt_coal *hw_coal; 8043 8044 if (!bnapi->rx_ring) 8045 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8046 else 8047 rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx); 8048 if (rc) 8049 break; 8050 8051 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 8052 continue; 8053 8054 if (bnapi->rx_ring && bnapi->tx_ring[0]) { 8055 rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx); 8056 if (rc) 8057 break; 8058 } 8059 if (bnapi->rx_ring) 8060 hw_coal = &bp->rx_coal; 8061 else 8062 hw_coal = &bp->tx_coal; 8063 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal); 8064 } 8065 hwrm_req_drop(bp, req_rx); 8066 hwrm_req_drop(bp, req_tx); 8067 return rc; 8068 } 8069 8070 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp) 8071 { 8072 struct hwrm_stat_ctx_clr_stats_input *req0 = NULL; 8073 struct hwrm_stat_ctx_free_input *req; 8074 int i; 8075 8076 if (!bp->bnapi) 8077 return; 8078 8079 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8080 return; 8081 8082 if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE)) 8083 return; 8084 if (BNXT_FW_MAJ(bp) <= 20) { 8085 if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) { 8086 hwrm_req_drop(bp, req); 8087 return; 8088 } 8089 hwrm_req_hold(bp, req0); 8090 } 8091 hwrm_req_hold(bp, req); 8092 for (i = 0; i < bp->cp_nr_rings; i++) { 8093 struct bnxt_napi *bnapi = bp->bnapi[i]; 8094 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8095 8096 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) { 8097 req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id); 8098 if (req0) { 8099 req0->stat_ctx_id = req->stat_ctx_id; 8100 hwrm_req_send(bp, req0); 8101 } 8102 hwrm_req_send(bp, req); 8103 8104 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID; 8105 } 8106 } 8107 hwrm_req_drop(bp, req); 8108 if (req0) 8109 hwrm_req_drop(bp, req0); 8110 } 8111 8112 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp) 8113 { 8114 struct hwrm_stat_ctx_alloc_output *resp; 8115 struct hwrm_stat_ctx_alloc_input *req; 8116 int rc, i; 8117 8118 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 8119 return 0; 8120 8121 rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC); 8122 if (rc) 8123 return rc; 8124 8125 req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size); 8126 req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000); 8127 8128 resp = hwrm_req_hold(bp, req); 8129 for (i = 0; i < bp->cp_nr_rings; i++) { 8130 struct bnxt_napi *bnapi = bp->bnapi[i]; 8131 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 8132 8133 req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map); 8134 8135 rc = hwrm_req_send(bp, req); 8136 if (rc) 8137 break; 8138 8139 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id); 8140 8141 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id; 8142 } 8143 hwrm_req_drop(bp, req); 8144 return rc; 8145 } 8146 8147 static int bnxt_hwrm_func_qcfg(struct bnxt *bp) 8148 { 8149 struct hwrm_func_qcfg_output *resp; 8150 struct hwrm_func_qcfg_input *req; 8151 u16 flags; 8152 int rc; 8153 8154 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 8155 if (rc) 8156 return rc; 8157 8158 req->fid = cpu_to_le16(0xffff); 8159 resp = hwrm_req_hold(bp, req); 8160 rc = hwrm_req_send(bp, req); 8161 if (rc) 8162 goto func_qcfg_exit; 8163 8164 #ifdef CONFIG_BNXT_SRIOV 8165 if (BNXT_VF(bp)) { 8166 struct bnxt_vf_info *vf = &bp->vf; 8167 8168 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK; 8169 } else { 8170 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs); 8171 } 8172 #endif 8173 flags = le16_to_cpu(resp->flags); 8174 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED | 8175 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) { 8176 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT; 8177 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED) 8178 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT; 8179 } 8180 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)) 8181 bp->flags |= BNXT_FLAG_MULTI_HOST; 8182 8183 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED) 8184 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR; 8185 8186 switch (resp->port_partition_type) { 8187 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0: 8188 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5: 8189 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0: 8190 bp->port_partition_type = resp->port_partition_type; 8191 break; 8192 } 8193 if (bp->hwrm_spec_code < 0x10707 || 8194 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB) 8195 bp->br_mode = BRIDGE_MODE_VEB; 8196 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA) 8197 bp->br_mode = BRIDGE_MODE_VEPA; 8198 else 8199 bp->br_mode = BRIDGE_MODE_UNDEF; 8200 8201 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured); 8202 if (!bp->max_mtu) 8203 bp->max_mtu = BNXT_MAX_MTU; 8204 8205 if (bp->db_size) 8206 goto func_qcfg_exit; 8207 8208 bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024; 8209 if (BNXT_CHIP_P5(bp)) { 8210 if (BNXT_PF(bp)) 8211 bp->db_offset = DB_PF_OFFSET_P5; 8212 else 8213 bp->db_offset = DB_VF_OFFSET_P5; 8214 } 8215 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) * 8216 1024); 8217 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) || 8218 bp->db_size <= bp->db_offset) 8219 bp->db_size = pci_resource_len(bp->pdev, 2); 8220 8221 func_qcfg_exit: 8222 hwrm_req_drop(bp, req); 8223 return rc; 8224 } 8225 8226 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm, 8227 u8 init_val, u8 init_offset, 8228 bool init_mask_set) 8229 { 8230 ctxm->init_value = init_val; 8231 ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET; 8232 if (init_mask_set) 8233 ctxm->init_offset = init_offset * 4; 8234 else 8235 ctxm->init_value = 0; 8236 } 8237 8238 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max) 8239 { 8240 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8241 u16 type; 8242 8243 for (type = 0; type < ctx_max; type++) { 8244 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8245 int n = 1; 8246 8247 if (!ctxm->max_entries) 8248 continue; 8249 8250 if (ctxm->instance_bmap) 8251 n = hweight32(ctxm->instance_bmap); 8252 ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL); 8253 if (!ctxm->pg_info) 8254 return -ENOMEM; 8255 } 8256 return 0; 8257 } 8258 8259 #define BNXT_CTX_INIT_VALID(flags) \ 8260 (!!((flags) & \ 8261 FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT)) 8262 8263 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp) 8264 { 8265 struct hwrm_func_backing_store_qcaps_v2_output *resp; 8266 struct hwrm_func_backing_store_qcaps_v2_input *req; 8267 struct bnxt_ctx_mem_info *ctx; 8268 u16 type; 8269 int rc; 8270 8271 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2); 8272 if (rc) 8273 return rc; 8274 8275 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8276 if (!ctx) 8277 return -ENOMEM; 8278 bp->ctx = ctx; 8279 8280 resp = hwrm_req_hold(bp, req); 8281 8282 for (type = 0; type < BNXT_CTX_V2_MAX; ) { 8283 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8284 u8 init_val, init_off, i; 8285 __le32 *p; 8286 u32 flags; 8287 8288 req->type = cpu_to_le16(type); 8289 rc = hwrm_req_send(bp, req); 8290 if (rc) 8291 goto ctx_done; 8292 flags = le32_to_cpu(resp->flags); 8293 type = le16_to_cpu(resp->next_valid_type); 8294 if (!(flags & FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_TYPE_VALID)) 8295 continue; 8296 8297 ctxm->type = le16_to_cpu(resp->type); 8298 ctxm->entry_size = le16_to_cpu(resp->entry_size); 8299 ctxm->flags = flags; 8300 ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map); 8301 ctxm->entry_multiple = resp->entry_multiple; 8302 ctxm->max_entries = le32_to_cpu(resp->max_num_entries); 8303 ctxm->min_entries = le32_to_cpu(resp->min_num_entries); 8304 init_val = resp->ctx_init_value; 8305 init_off = resp->ctx_init_offset; 8306 bnxt_init_ctx_initializer(ctxm, init_val, init_off, 8307 BNXT_CTX_INIT_VALID(flags)); 8308 ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt, 8309 BNXT_MAX_SPLIT_ENTRY); 8310 for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt; 8311 i++, p++) 8312 ctxm->split[i] = le32_to_cpu(*p); 8313 } 8314 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX); 8315 8316 ctx_done: 8317 hwrm_req_drop(bp, req); 8318 return rc; 8319 } 8320 8321 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp) 8322 { 8323 struct hwrm_func_backing_store_qcaps_output *resp; 8324 struct hwrm_func_backing_store_qcaps_input *req; 8325 int rc; 8326 8327 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx) 8328 return 0; 8329 8330 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8331 return bnxt_hwrm_func_backing_store_qcaps_v2(bp); 8332 8333 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS); 8334 if (rc) 8335 return rc; 8336 8337 resp = hwrm_req_hold(bp, req); 8338 rc = hwrm_req_send_silent(bp, req); 8339 if (!rc) { 8340 struct bnxt_ctx_mem_type *ctxm; 8341 struct bnxt_ctx_mem_info *ctx; 8342 u8 init_val, init_idx = 0; 8343 u16 init_mask; 8344 8345 ctx = bp->ctx; 8346 if (!ctx) { 8347 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 8348 if (!ctx) { 8349 rc = -ENOMEM; 8350 goto ctx_err; 8351 } 8352 bp->ctx = ctx; 8353 } 8354 init_val = resp->ctx_kind_initializer; 8355 init_mask = le16_to_cpu(resp->ctx_init_mask); 8356 8357 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8358 ctxm->max_entries = le32_to_cpu(resp->qp_max_entries); 8359 ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries); 8360 ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries); 8361 ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries); 8362 ctxm->entry_size = le16_to_cpu(resp->qp_entry_size); 8363 bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset, 8364 (init_mask & (1 << init_idx++)) != 0); 8365 8366 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8367 ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries); 8368 ctxm->max_entries = le32_to_cpu(resp->srq_max_entries); 8369 ctxm->entry_size = le16_to_cpu(resp->srq_entry_size); 8370 bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset, 8371 (init_mask & (1 << init_idx++)) != 0); 8372 8373 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8374 ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries); 8375 ctxm->max_entries = le32_to_cpu(resp->cq_max_entries); 8376 ctxm->entry_size = le16_to_cpu(resp->cq_entry_size); 8377 bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset, 8378 (init_mask & (1 << init_idx++)) != 0); 8379 8380 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8381 ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries); 8382 ctxm->max_entries = ctxm->vnic_entries + 8383 le16_to_cpu(resp->vnic_max_ring_table_entries); 8384 ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size); 8385 bnxt_init_ctx_initializer(ctxm, init_val, 8386 resp->vnic_init_offset, 8387 (init_mask & (1 << init_idx++)) != 0); 8388 8389 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8390 ctxm->max_entries = le32_to_cpu(resp->stat_max_entries); 8391 ctxm->entry_size = le16_to_cpu(resp->stat_entry_size); 8392 bnxt_init_ctx_initializer(ctxm, init_val, 8393 resp->stat_init_offset, 8394 (init_mask & (1 << init_idx++)) != 0); 8395 8396 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8397 ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size); 8398 ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring); 8399 ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring); 8400 ctxm->entry_multiple = resp->tqm_entries_multiple; 8401 if (!ctxm->entry_multiple) 8402 ctxm->entry_multiple = 1; 8403 8404 memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm)); 8405 8406 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8407 ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries); 8408 ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size); 8409 ctxm->mrav_num_entries_units = 8410 le16_to_cpu(resp->mrav_num_entries_units); 8411 bnxt_init_ctx_initializer(ctxm, init_val, 8412 resp->mrav_init_offset, 8413 (init_mask & (1 << init_idx++)) != 0); 8414 8415 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8416 ctxm->entry_size = le16_to_cpu(resp->tim_entry_size); 8417 ctxm->max_entries = le32_to_cpu(resp->tim_max_entries); 8418 8419 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count; 8420 if (!ctx->tqm_fp_rings_count) 8421 ctx->tqm_fp_rings_count = bp->max_q; 8422 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS) 8423 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS; 8424 8425 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8426 memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm)); 8427 ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1; 8428 8429 rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX); 8430 } else { 8431 rc = 0; 8432 } 8433 ctx_err: 8434 hwrm_req_drop(bp, req); 8435 return rc; 8436 } 8437 8438 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr, 8439 __le64 *pg_dir) 8440 { 8441 if (!rmem->nr_pages) 8442 return; 8443 8444 BNXT_SET_CTX_PAGE_ATTR(*pg_attr); 8445 if (rmem->depth >= 1) { 8446 if (rmem->depth == 2) 8447 *pg_attr |= 2; 8448 else 8449 *pg_attr |= 1; 8450 *pg_dir = cpu_to_le64(rmem->pg_tbl_map); 8451 } else { 8452 *pg_dir = cpu_to_le64(rmem->dma_arr[0]); 8453 } 8454 } 8455 8456 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \ 8457 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \ 8458 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \ 8459 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \ 8460 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \ 8461 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) 8462 8463 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables) 8464 { 8465 struct hwrm_func_backing_store_cfg_input *req; 8466 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8467 struct bnxt_ctx_pg_info *ctx_pg; 8468 struct bnxt_ctx_mem_type *ctxm; 8469 void **__req = (void **)&req; 8470 u32 req_len = sizeof(*req); 8471 __le32 *num_entries; 8472 __le64 *pg_dir; 8473 u32 flags = 0; 8474 u8 *pg_attr; 8475 u32 ena; 8476 int rc; 8477 int i; 8478 8479 if (!ctx) 8480 return 0; 8481 8482 if (req_len > bp->hwrm_max_ext_req_len) 8483 req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN; 8484 rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len); 8485 if (rc) 8486 return rc; 8487 8488 req->enables = cpu_to_le32(enables); 8489 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) { 8490 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8491 ctx_pg = ctxm->pg_info; 8492 req->qp_num_entries = cpu_to_le32(ctx_pg->entries); 8493 req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries); 8494 req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries); 8495 req->qp_entry_size = cpu_to_le16(ctxm->entry_size); 8496 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8497 &req->qpc_pg_size_qpc_lvl, 8498 &req->qpc_page_dir); 8499 8500 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD) 8501 req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries); 8502 } 8503 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) { 8504 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8505 ctx_pg = ctxm->pg_info; 8506 req->srq_num_entries = cpu_to_le32(ctx_pg->entries); 8507 req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries); 8508 req->srq_entry_size = cpu_to_le16(ctxm->entry_size); 8509 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8510 &req->srq_pg_size_srq_lvl, 8511 &req->srq_page_dir); 8512 } 8513 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) { 8514 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8515 ctx_pg = ctxm->pg_info; 8516 req->cq_num_entries = cpu_to_le32(ctx_pg->entries); 8517 req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries); 8518 req->cq_entry_size = cpu_to_le16(ctxm->entry_size); 8519 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8520 &req->cq_pg_size_cq_lvl, 8521 &req->cq_page_dir); 8522 } 8523 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) { 8524 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8525 ctx_pg = ctxm->pg_info; 8526 req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries); 8527 req->vnic_num_ring_table_entries = 8528 cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries); 8529 req->vnic_entry_size = cpu_to_le16(ctxm->entry_size); 8530 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8531 &req->vnic_pg_size_vnic_lvl, 8532 &req->vnic_page_dir); 8533 } 8534 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) { 8535 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8536 ctx_pg = ctxm->pg_info; 8537 req->stat_num_entries = cpu_to_le32(ctxm->max_entries); 8538 req->stat_entry_size = cpu_to_le16(ctxm->entry_size); 8539 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8540 &req->stat_pg_size_stat_lvl, 8541 &req->stat_page_dir); 8542 } 8543 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) { 8544 u32 units; 8545 8546 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8547 ctx_pg = ctxm->pg_info; 8548 req->mrav_num_entries = cpu_to_le32(ctx_pg->entries); 8549 units = ctxm->mrav_num_entries_units; 8550 if (units) { 8551 u32 num_mr, num_ah = ctxm->mrav_av_entries; 8552 u32 entries; 8553 8554 num_mr = ctx_pg->entries - num_ah; 8555 entries = ((num_mr / units) << 16) | (num_ah / units); 8556 req->mrav_num_entries = cpu_to_le32(entries); 8557 flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT; 8558 } 8559 req->mrav_entry_size = cpu_to_le16(ctxm->entry_size); 8560 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8561 &req->mrav_pg_size_mrav_lvl, 8562 &req->mrav_page_dir); 8563 } 8564 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) { 8565 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8566 ctx_pg = ctxm->pg_info; 8567 req->tim_num_entries = cpu_to_le32(ctx_pg->entries); 8568 req->tim_entry_size = cpu_to_le16(ctxm->entry_size); 8569 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8570 &req->tim_pg_size_tim_lvl, 8571 &req->tim_page_dir); 8572 } 8573 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8574 for (i = 0, num_entries = &req->tqm_sp_num_entries, 8575 pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl, 8576 pg_dir = &req->tqm_sp_page_dir, 8577 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP, 8578 ctx_pg = ctxm->pg_info; 8579 i < BNXT_MAX_TQM_RINGS; 8580 ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i], 8581 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) { 8582 if (!(enables & ena)) 8583 continue; 8584 8585 req->tqm_entry_size = cpu_to_le16(ctxm->entry_size); 8586 *num_entries = cpu_to_le32(ctx_pg->entries); 8587 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir); 8588 } 8589 req->flags = cpu_to_le32(flags); 8590 return hwrm_req_send(bp, req); 8591 } 8592 8593 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp, 8594 struct bnxt_ctx_pg_info *ctx_pg) 8595 { 8596 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8597 8598 rmem->page_size = BNXT_PAGE_SIZE; 8599 rmem->pg_arr = ctx_pg->ctx_pg_arr; 8600 rmem->dma_arr = ctx_pg->ctx_dma_arr; 8601 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG; 8602 if (rmem->depth >= 1) 8603 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG; 8604 return bnxt_alloc_ring(bp, rmem); 8605 } 8606 8607 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp, 8608 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size, 8609 u8 depth, struct bnxt_ctx_mem_type *ctxm) 8610 { 8611 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8612 int rc; 8613 8614 if (!mem_size) 8615 return -EINVAL; 8616 8617 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8618 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) { 8619 ctx_pg->nr_pages = 0; 8620 return -EINVAL; 8621 } 8622 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) { 8623 int nr_tbls, i; 8624 8625 rmem->depth = 2; 8626 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg), 8627 GFP_KERNEL); 8628 if (!ctx_pg->ctx_pg_tbl) 8629 return -ENOMEM; 8630 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES); 8631 rmem->nr_pages = nr_tbls; 8632 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8633 if (rc) 8634 return rc; 8635 for (i = 0; i < nr_tbls; i++) { 8636 struct bnxt_ctx_pg_info *pg_tbl; 8637 8638 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL); 8639 if (!pg_tbl) 8640 return -ENOMEM; 8641 ctx_pg->ctx_pg_tbl[i] = pg_tbl; 8642 rmem = &pg_tbl->ring_mem; 8643 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i]; 8644 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i]; 8645 rmem->depth = 1; 8646 rmem->nr_pages = MAX_CTX_PAGES; 8647 rmem->ctx_mem = ctxm; 8648 if (i == (nr_tbls - 1)) { 8649 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES; 8650 8651 if (rem) 8652 rmem->nr_pages = rem; 8653 } 8654 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl); 8655 if (rc) 8656 break; 8657 } 8658 } else { 8659 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE); 8660 if (rmem->nr_pages > 1 || depth) 8661 rmem->depth = 1; 8662 rmem->ctx_mem = ctxm; 8663 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg); 8664 } 8665 return rc; 8666 } 8667 8668 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp, 8669 struct bnxt_ctx_pg_info *ctx_pg) 8670 { 8671 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem; 8672 8673 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES || 8674 ctx_pg->ctx_pg_tbl) { 8675 int i, nr_tbls = rmem->nr_pages; 8676 8677 for (i = 0; i < nr_tbls; i++) { 8678 struct bnxt_ctx_pg_info *pg_tbl; 8679 struct bnxt_ring_mem_info *rmem2; 8680 8681 pg_tbl = ctx_pg->ctx_pg_tbl[i]; 8682 if (!pg_tbl) 8683 continue; 8684 rmem2 = &pg_tbl->ring_mem; 8685 bnxt_free_ring(bp, rmem2); 8686 ctx_pg->ctx_pg_arr[i] = NULL; 8687 kfree(pg_tbl); 8688 ctx_pg->ctx_pg_tbl[i] = NULL; 8689 } 8690 kfree(ctx_pg->ctx_pg_tbl); 8691 ctx_pg->ctx_pg_tbl = NULL; 8692 } 8693 bnxt_free_ring(bp, rmem); 8694 ctx_pg->nr_pages = 0; 8695 } 8696 8697 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp, 8698 struct bnxt_ctx_mem_type *ctxm, u32 entries, 8699 u8 pg_lvl) 8700 { 8701 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 8702 int i, rc = 0, n = 1; 8703 u32 mem_size; 8704 8705 if (!ctxm->entry_size || !ctx_pg) 8706 return -EINVAL; 8707 if (ctxm->instance_bmap) 8708 n = hweight32(ctxm->instance_bmap); 8709 if (ctxm->entry_multiple) 8710 entries = roundup(entries, ctxm->entry_multiple); 8711 entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries); 8712 mem_size = entries * ctxm->entry_size; 8713 for (i = 0; i < n && !rc; i++) { 8714 ctx_pg[i].entries = entries; 8715 rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl, 8716 ctxm->init_value ? ctxm : NULL); 8717 } 8718 return rc; 8719 } 8720 8721 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp, 8722 struct bnxt_ctx_mem_type *ctxm, 8723 bool last) 8724 { 8725 struct hwrm_func_backing_store_cfg_v2_input *req; 8726 u32 instance_bmap = ctxm->instance_bmap; 8727 int i, j, rc = 0, n = 1; 8728 __le32 *p; 8729 8730 if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info) 8731 return 0; 8732 8733 if (instance_bmap) 8734 n = hweight32(ctxm->instance_bmap); 8735 else 8736 instance_bmap = 1; 8737 8738 rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2); 8739 if (rc) 8740 return rc; 8741 hwrm_req_hold(bp, req); 8742 req->type = cpu_to_le16(ctxm->type); 8743 req->entry_size = cpu_to_le16(ctxm->entry_size); 8744 req->subtype_valid_cnt = ctxm->split_entry_cnt; 8745 for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++) 8746 p[i] = cpu_to_le32(ctxm->split[i]); 8747 for (i = 0, j = 0; j < n && !rc; i++) { 8748 struct bnxt_ctx_pg_info *ctx_pg; 8749 8750 if (!(instance_bmap & (1 << i))) 8751 continue; 8752 req->instance = cpu_to_le16(i); 8753 ctx_pg = &ctxm->pg_info[j++]; 8754 if (!ctx_pg->entries) 8755 continue; 8756 req->num_entries = cpu_to_le32(ctx_pg->entries); 8757 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, 8758 &req->page_size_pbl_level, 8759 &req->page_dir); 8760 if (last && j == n) 8761 req->flags = 8762 cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE); 8763 rc = hwrm_req_send(bp, req); 8764 } 8765 hwrm_req_drop(bp, req); 8766 return rc; 8767 } 8768 8769 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena) 8770 { 8771 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8772 struct bnxt_ctx_mem_type *ctxm; 8773 u16 last_type; 8774 int rc = 0; 8775 u16 type; 8776 8777 if (!ena) 8778 return 0; 8779 else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) 8780 last_type = BNXT_CTX_MAX - 1; 8781 else 8782 last_type = BNXT_CTX_L2_MAX - 1; 8783 ctx->ctx_arr[last_type].last = 1; 8784 8785 for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) { 8786 ctxm = &ctx->ctx_arr[type]; 8787 8788 rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last); 8789 if (rc) 8790 return rc; 8791 } 8792 return 0; 8793 } 8794 8795 void bnxt_free_ctx_mem(struct bnxt *bp) 8796 { 8797 struct bnxt_ctx_mem_info *ctx = bp->ctx; 8798 u16 type; 8799 8800 if (!ctx) 8801 return; 8802 8803 for (type = 0; type < BNXT_CTX_V2_MAX; type++) { 8804 struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type]; 8805 struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info; 8806 int i, n = 1; 8807 8808 if (!ctx_pg) 8809 continue; 8810 if (ctxm->instance_bmap) 8811 n = hweight32(ctxm->instance_bmap); 8812 for (i = 0; i < n; i++) 8813 bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]); 8814 8815 kfree(ctx_pg); 8816 ctxm->pg_info = NULL; 8817 } 8818 8819 ctx->flags &= ~BNXT_CTX_FLAG_INITED; 8820 kfree(ctx); 8821 bp->ctx = NULL; 8822 } 8823 8824 static int bnxt_alloc_ctx_mem(struct bnxt *bp) 8825 { 8826 struct bnxt_ctx_mem_type *ctxm; 8827 struct bnxt_ctx_mem_info *ctx; 8828 u32 l2_qps, qp1_qps, max_qps; 8829 u32 ena, entries_sp, entries; 8830 u32 srqs, max_srqs, min; 8831 u32 num_mr, num_ah; 8832 u32 extra_srqs = 0; 8833 u32 extra_qps = 0; 8834 u32 fast_qpmd_qps; 8835 u8 pg_lvl = 1; 8836 int i, rc; 8837 8838 rc = bnxt_hwrm_func_backing_store_qcaps(bp); 8839 if (rc) { 8840 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n", 8841 rc); 8842 return rc; 8843 } 8844 ctx = bp->ctx; 8845 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED)) 8846 return 0; 8847 8848 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8849 l2_qps = ctxm->qp_l2_entries; 8850 qp1_qps = ctxm->qp_qp1_entries; 8851 fast_qpmd_qps = ctxm->qp_fast_qpmd_entries; 8852 max_qps = ctxm->max_entries; 8853 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8854 srqs = ctxm->srq_l2_entries; 8855 max_srqs = ctxm->max_entries; 8856 ena = 0; 8857 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) { 8858 pg_lvl = 2; 8859 extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps); 8860 /* allocate extra qps if fw supports RoCE fast qp destroy feature */ 8861 extra_qps += fast_qpmd_qps; 8862 extra_srqs = min_t(u32, 8192, max_srqs - srqs); 8863 if (fast_qpmd_qps) 8864 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD; 8865 } 8866 8867 ctxm = &ctx->ctx_arr[BNXT_CTX_QP]; 8868 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 8869 pg_lvl); 8870 if (rc) 8871 return rc; 8872 8873 ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ]; 8874 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl); 8875 if (rc) 8876 return rc; 8877 8878 ctxm = &ctx->ctx_arr[BNXT_CTX_CQ]; 8879 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries + 8880 extra_qps * 2, pg_lvl); 8881 if (rc) 8882 return rc; 8883 8884 ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC]; 8885 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 8886 if (rc) 8887 return rc; 8888 8889 ctxm = &ctx->ctx_arr[BNXT_CTX_STAT]; 8890 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1); 8891 if (rc) 8892 return rc; 8893 8894 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 8895 goto skip_rdma; 8896 8897 ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV]; 8898 /* 128K extra is needed to accommodate static AH context 8899 * allocation by f/w. 8900 */ 8901 num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256); 8902 num_ah = min_t(u32, num_mr, 1024 * 128); 8903 ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1; 8904 if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah) 8905 ctxm->mrav_av_entries = num_ah; 8906 8907 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2); 8908 if (rc) 8909 return rc; 8910 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV; 8911 8912 ctxm = &ctx->ctx_arr[BNXT_CTX_TIM]; 8913 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1); 8914 if (rc) 8915 return rc; 8916 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM; 8917 8918 skip_rdma: 8919 ctxm = &ctx->ctx_arr[BNXT_CTX_STQM]; 8920 min = ctxm->min_entries; 8921 entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps + 8922 2 * (extra_qps + qp1_qps) + min; 8923 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2); 8924 if (rc) 8925 return rc; 8926 8927 ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM]; 8928 entries = l2_qps + 2 * (extra_qps + qp1_qps); 8929 rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2); 8930 if (rc) 8931 return rc; 8932 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) 8933 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i; 8934 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES; 8935 8936 if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2) 8937 rc = bnxt_backing_store_cfg_v2(bp, ena); 8938 else 8939 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena); 8940 if (rc) { 8941 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n", 8942 rc); 8943 return rc; 8944 } 8945 ctx->flags |= BNXT_CTX_FLAG_INITED; 8946 return 0; 8947 } 8948 8949 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all) 8950 { 8951 struct hwrm_func_resource_qcaps_output *resp; 8952 struct hwrm_func_resource_qcaps_input *req; 8953 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 8954 int rc; 8955 8956 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS); 8957 if (rc) 8958 return rc; 8959 8960 req->fid = cpu_to_le16(0xffff); 8961 resp = hwrm_req_hold(bp, req); 8962 rc = hwrm_req_send_silent(bp, req); 8963 if (rc) 8964 goto hwrm_func_resc_qcaps_exit; 8965 8966 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs); 8967 if (!all) 8968 goto hwrm_func_resc_qcaps_exit; 8969 8970 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx); 8971 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 8972 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings); 8973 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 8974 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings); 8975 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 8976 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings); 8977 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 8978 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps); 8979 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps); 8980 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs); 8981 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 8982 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics); 8983 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 8984 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx); 8985 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 8986 8987 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 8988 u16 max_msix = le16_to_cpu(resp->max_msix); 8989 8990 hw_resc->max_nqs = max_msix; 8991 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings; 8992 } 8993 8994 if (BNXT_PF(bp)) { 8995 struct bnxt_pf_info *pf = &bp->pf; 8996 8997 pf->vf_resv_strategy = 8998 le16_to_cpu(resp->vf_reservation_strategy); 8999 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) 9000 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL; 9001 } 9002 hwrm_func_resc_qcaps_exit: 9003 hwrm_req_drop(bp, req); 9004 return rc; 9005 } 9006 9007 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp) 9008 { 9009 struct hwrm_port_mac_ptp_qcfg_output *resp; 9010 struct hwrm_port_mac_ptp_qcfg_input *req; 9011 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 9012 bool phc_cfg; 9013 u8 flags; 9014 int rc; 9015 9016 if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) { 9017 rc = -ENODEV; 9018 goto no_ptp; 9019 } 9020 9021 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG); 9022 if (rc) 9023 goto no_ptp; 9024 9025 req->port_id = cpu_to_le16(bp->pf.port_id); 9026 resp = hwrm_req_hold(bp, req); 9027 rc = hwrm_req_send(bp, req); 9028 if (rc) 9029 goto exit; 9030 9031 flags = resp->flags; 9032 if (BNXT_CHIP_P5_AND_MINUS(bp) && 9033 !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) { 9034 rc = -ENODEV; 9035 goto exit; 9036 } 9037 if (!ptp) { 9038 ptp = kzalloc(sizeof(*ptp), GFP_KERNEL); 9039 if (!ptp) { 9040 rc = -ENOMEM; 9041 goto exit; 9042 } 9043 ptp->bp = bp; 9044 bp->ptp_cfg = ptp; 9045 } 9046 9047 if (flags & 9048 (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK | 9049 PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) { 9050 ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower); 9051 ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper); 9052 } else if (BNXT_CHIP_P5(bp)) { 9053 ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER; 9054 ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER; 9055 } else { 9056 rc = -ENODEV; 9057 goto exit; 9058 } 9059 phc_cfg = (flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0; 9060 rc = bnxt_ptp_init(bp, phc_cfg); 9061 if (rc) 9062 netdev_warn(bp->dev, "PTP initialization failed.\n"); 9063 exit: 9064 hwrm_req_drop(bp, req); 9065 if (!rc) 9066 return 0; 9067 9068 no_ptp: 9069 bnxt_ptp_clear(bp); 9070 kfree(ptp); 9071 bp->ptp_cfg = NULL; 9072 return rc; 9073 } 9074 9075 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp) 9076 { 9077 struct hwrm_func_qcaps_output *resp; 9078 struct hwrm_func_qcaps_input *req; 9079 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 9080 u32 flags, flags_ext, flags_ext2; 9081 int rc; 9082 9083 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS); 9084 if (rc) 9085 return rc; 9086 9087 req->fid = cpu_to_le16(0xffff); 9088 resp = hwrm_req_hold(bp, req); 9089 rc = hwrm_req_send(bp, req); 9090 if (rc) 9091 goto hwrm_func_qcaps_exit; 9092 9093 flags = le32_to_cpu(resp->flags); 9094 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED) 9095 bp->flags |= BNXT_FLAG_ROCEV1_CAP; 9096 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED) 9097 bp->flags |= BNXT_FLAG_ROCEV2_CAP; 9098 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED) 9099 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED; 9100 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE) 9101 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET; 9102 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED) 9103 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED; 9104 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE) 9105 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY; 9106 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD) 9107 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD; 9108 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED)) 9109 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT; 9110 if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED) 9111 bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS; 9112 9113 flags_ext = le32_to_cpu(resp->flags_ext); 9114 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED) 9115 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED; 9116 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED)) 9117 bp->fw_cap |= BNXT_FW_CAP_PTP_PPS; 9118 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED) 9119 bp->fw_cap |= BNXT_FW_CAP_PTP_RTC; 9120 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT)) 9121 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF; 9122 if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED)) 9123 bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH; 9124 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED) 9125 bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2; 9126 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP) 9127 bp->flags |= BNXT_FLAG_TX_COAL_CMPL; 9128 9129 flags_ext2 = le32_to_cpu(resp->flags_ext2); 9130 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED) 9131 bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS; 9132 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED) 9133 bp->flags |= BNXT_FLAG_UDP_GSO_CAP; 9134 if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED) 9135 bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP; 9136 9137 bp->tx_push_thresh = 0; 9138 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) && 9139 BNXT_FW_MAJ(bp) > 217) 9140 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH; 9141 9142 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx); 9143 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings); 9144 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings); 9145 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings); 9146 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps); 9147 if (!hw_resc->max_hw_ring_grps) 9148 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings; 9149 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs); 9150 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics); 9151 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx); 9152 9153 hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records); 9154 hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records); 9155 hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows); 9156 hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows); 9157 hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows); 9158 hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows); 9159 9160 if (BNXT_PF(bp)) { 9161 struct bnxt_pf_info *pf = &bp->pf; 9162 9163 pf->fw_fid = le16_to_cpu(resp->fid); 9164 pf->port_id = le16_to_cpu(resp->port_id); 9165 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN); 9166 pf->first_vf_id = le16_to_cpu(resp->first_vf_id); 9167 pf->max_vfs = le16_to_cpu(resp->max_vfs); 9168 bp->flags &= ~BNXT_FLAG_WOL_CAP; 9169 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED) 9170 bp->flags |= BNXT_FLAG_WOL_CAP; 9171 if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) { 9172 bp->fw_cap |= BNXT_FW_CAP_PTP; 9173 } else { 9174 bnxt_ptp_clear(bp); 9175 kfree(bp->ptp_cfg); 9176 bp->ptp_cfg = NULL; 9177 } 9178 } else { 9179 #ifdef CONFIG_BNXT_SRIOV 9180 struct bnxt_vf_info *vf = &bp->vf; 9181 9182 vf->fw_fid = le16_to_cpu(resp->fid); 9183 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN); 9184 #endif 9185 } 9186 bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs); 9187 9188 hwrm_func_qcaps_exit: 9189 hwrm_req_drop(bp, req); 9190 return rc; 9191 } 9192 9193 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp) 9194 { 9195 struct hwrm_dbg_qcaps_output *resp; 9196 struct hwrm_dbg_qcaps_input *req; 9197 int rc; 9198 9199 bp->fw_dbg_cap = 0; 9200 if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS)) 9201 return; 9202 9203 rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS); 9204 if (rc) 9205 return; 9206 9207 req->fid = cpu_to_le16(0xffff); 9208 resp = hwrm_req_hold(bp, req); 9209 rc = hwrm_req_send(bp, req); 9210 if (rc) 9211 goto hwrm_dbg_qcaps_exit; 9212 9213 bp->fw_dbg_cap = le32_to_cpu(resp->flags); 9214 9215 hwrm_dbg_qcaps_exit: 9216 hwrm_req_drop(bp, req); 9217 } 9218 9219 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp); 9220 9221 int bnxt_hwrm_func_qcaps(struct bnxt *bp) 9222 { 9223 int rc; 9224 9225 rc = __bnxt_hwrm_func_qcaps(bp); 9226 if (rc) 9227 return rc; 9228 9229 bnxt_hwrm_dbg_qcaps(bp); 9230 9231 rc = bnxt_hwrm_queue_qportcfg(bp); 9232 if (rc) { 9233 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc); 9234 return rc; 9235 } 9236 if (bp->hwrm_spec_code >= 0x10803) { 9237 rc = bnxt_alloc_ctx_mem(bp); 9238 if (rc) 9239 return rc; 9240 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 9241 if (!rc) 9242 bp->fw_cap |= BNXT_FW_CAP_NEW_RM; 9243 } 9244 return 0; 9245 } 9246 9247 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp) 9248 { 9249 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp; 9250 struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req; 9251 u32 flags; 9252 int rc; 9253 9254 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW)) 9255 return 0; 9256 9257 rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS); 9258 if (rc) 9259 return rc; 9260 9261 resp = hwrm_req_hold(bp, req); 9262 rc = hwrm_req_send(bp, req); 9263 if (rc) 9264 goto hwrm_cfa_adv_qcaps_exit; 9265 9266 flags = le32_to_cpu(resp->flags); 9267 if (flags & 9268 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED) 9269 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2; 9270 9271 if (flags & 9272 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED) 9273 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3; 9274 9275 if (flags & 9276 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED) 9277 bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO; 9278 9279 hwrm_cfa_adv_qcaps_exit: 9280 hwrm_req_drop(bp, req); 9281 return rc; 9282 } 9283 9284 static int __bnxt_alloc_fw_health(struct bnxt *bp) 9285 { 9286 if (bp->fw_health) 9287 return 0; 9288 9289 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL); 9290 if (!bp->fw_health) 9291 return -ENOMEM; 9292 9293 mutex_init(&bp->fw_health->lock); 9294 return 0; 9295 } 9296 9297 static int bnxt_alloc_fw_health(struct bnxt *bp) 9298 { 9299 int rc; 9300 9301 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) && 9302 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9303 return 0; 9304 9305 rc = __bnxt_alloc_fw_health(bp); 9306 if (rc) { 9307 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET; 9308 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9309 return rc; 9310 } 9311 9312 return 0; 9313 } 9314 9315 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg) 9316 { 9317 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 + 9318 BNXT_GRCPF_REG_WINDOW_BASE_OUT + 9319 BNXT_FW_HEALTH_WIN_MAP_OFF); 9320 } 9321 9322 static void bnxt_inv_fw_health_reg(struct bnxt *bp) 9323 { 9324 struct bnxt_fw_health *fw_health = bp->fw_health; 9325 u32 reg_type; 9326 9327 if (!fw_health) 9328 return; 9329 9330 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]); 9331 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9332 fw_health->status_reliable = false; 9333 9334 reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]); 9335 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) 9336 fw_health->resets_reliable = false; 9337 } 9338 9339 static void bnxt_try_map_fw_health_reg(struct bnxt *bp) 9340 { 9341 void __iomem *hs; 9342 u32 status_loc; 9343 u32 reg_type; 9344 u32 sig; 9345 9346 if (bp->fw_health) 9347 bp->fw_health->status_reliable = false; 9348 9349 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC); 9350 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC); 9351 9352 sig = readl(hs + offsetof(struct hcomm_status, sig_ver)); 9353 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) { 9354 if (!bp->chip_num) { 9355 __bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE); 9356 bp->chip_num = readl(bp->bar0 + 9357 BNXT_FW_HEALTH_WIN_BASE + 9358 BNXT_GRC_REG_CHIP_NUM); 9359 } 9360 if (!BNXT_CHIP_P5_PLUS(bp)) 9361 return; 9362 9363 status_loc = BNXT_GRC_REG_STATUS_P5 | 9364 BNXT_FW_HEALTH_REG_TYPE_BAR0; 9365 } else { 9366 status_loc = readl(hs + offsetof(struct hcomm_status, 9367 fw_status_loc)); 9368 } 9369 9370 if (__bnxt_alloc_fw_health(bp)) { 9371 netdev_warn(bp->dev, "no memory for firmware status checks\n"); 9372 return; 9373 } 9374 9375 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc; 9376 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc); 9377 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) { 9378 __bnxt_map_fw_health_reg(bp, status_loc); 9379 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] = 9380 BNXT_FW_HEALTH_WIN_OFF(status_loc); 9381 } 9382 9383 bp->fw_health->status_reliable = true; 9384 } 9385 9386 static int bnxt_map_fw_health_regs(struct bnxt *bp) 9387 { 9388 struct bnxt_fw_health *fw_health = bp->fw_health; 9389 u32 reg_base = 0xffffffff; 9390 int i; 9391 9392 bp->fw_health->status_reliable = false; 9393 bp->fw_health->resets_reliable = false; 9394 /* Only pre-map the monitoring GRC registers using window 3 */ 9395 for (i = 0; i < 4; i++) { 9396 u32 reg = fw_health->regs[i]; 9397 9398 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC) 9399 continue; 9400 if (reg_base == 0xffffffff) 9401 reg_base = reg & BNXT_GRC_BASE_MASK; 9402 if ((reg & BNXT_GRC_BASE_MASK) != reg_base) 9403 return -ERANGE; 9404 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg); 9405 } 9406 bp->fw_health->status_reliable = true; 9407 bp->fw_health->resets_reliable = true; 9408 if (reg_base == 0xffffffff) 9409 return 0; 9410 9411 __bnxt_map_fw_health_reg(bp, reg_base); 9412 return 0; 9413 } 9414 9415 static void bnxt_remap_fw_health_regs(struct bnxt *bp) 9416 { 9417 if (!bp->fw_health) 9418 return; 9419 9420 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) { 9421 bp->fw_health->status_reliable = true; 9422 bp->fw_health->resets_reliable = true; 9423 } else { 9424 bnxt_try_map_fw_health_reg(bp); 9425 } 9426 } 9427 9428 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp) 9429 { 9430 struct bnxt_fw_health *fw_health = bp->fw_health; 9431 struct hwrm_error_recovery_qcfg_output *resp; 9432 struct hwrm_error_recovery_qcfg_input *req; 9433 int rc, i; 9434 9435 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) 9436 return 0; 9437 9438 rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG); 9439 if (rc) 9440 return rc; 9441 9442 resp = hwrm_req_hold(bp, req); 9443 rc = hwrm_req_send(bp, req); 9444 if (rc) 9445 goto err_recovery_out; 9446 fw_health->flags = le32_to_cpu(resp->flags); 9447 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) && 9448 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) { 9449 rc = -EINVAL; 9450 goto err_recovery_out; 9451 } 9452 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq); 9453 fw_health->master_func_wait_dsecs = 9454 le32_to_cpu(resp->master_func_wait_period); 9455 fw_health->normal_func_wait_dsecs = 9456 le32_to_cpu(resp->normal_func_wait_period); 9457 fw_health->post_reset_wait_dsecs = 9458 le32_to_cpu(resp->master_func_wait_period_after_reset); 9459 fw_health->post_reset_max_wait_dsecs = 9460 le32_to_cpu(resp->max_bailout_time_after_reset); 9461 fw_health->regs[BNXT_FW_HEALTH_REG] = 9462 le32_to_cpu(resp->fw_health_status_reg); 9463 fw_health->regs[BNXT_FW_HEARTBEAT_REG] = 9464 le32_to_cpu(resp->fw_heartbeat_reg); 9465 fw_health->regs[BNXT_FW_RESET_CNT_REG] = 9466 le32_to_cpu(resp->fw_reset_cnt_reg); 9467 fw_health->regs[BNXT_FW_RESET_INPROG_REG] = 9468 le32_to_cpu(resp->reset_inprogress_reg); 9469 fw_health->fw_reset_inprog_reg_mask = 9470 le32_to_cpu(resp->reset_inprogress_reg_mask); 9471 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt; 9472 if (fw_health->fw_reset_seq_cnt >= 16) { 9473 rc = -EINVAL; 9474 goto err_recovery_out; 9475 } 9476 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) { 9477 fw_health->fw_reset_seq_regs[i] = 9478 le32_to_cpu(resp->reset_reg[i]); 9479 fw_health->fw_reset_seq_vals[i] = 9480 le32_to_cpu(resp->reset_reg_val[i]); 9481 fw_health->fw_reset_seq_delay_msec[i] = 9482 resp->delay_after_reset[i]; 9483 } 9484 err_recovery_out: 9485 hwrm_req_drop(bp, req); 9486 if (!rc) 9487 rc = bnxt_map_fw_health_regs(bp); 9488 if (rc) 9489 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY; 9490 return rc; 9491 } 9492 9493 static int bnxt_hwrm_func_reset(struct bnxt *bp) 9494 { 9495 struct hwrm_func_reset_input *req; 9496 int rc; 9497 9498 rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET); 9499 if (rc) 9500 return rc; 9501 9502 req->enables = 0; 9503 hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT); 9504 return hwrm_req_send(bp, req); 9505 } 9506 9507 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp) 9508 { 9509 struct hwrm_nvm_get_dev_info_output nvm_info; 9510 9511 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info)) 9512 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d", 9513 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min, 9514 nvm_info.nvm_cfg_ver_upd); 9515 } 9516 9517 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp) 9518 { 9519 struct hwrm_queue_qportcfg_output *resp; 9520 struct hwrm_queue_qportcfg_input *req; 9521 u8 i, j, *qptr; 9522 bool no_rdma; 9523 int rc = 0; 9524 9525 rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG); 9526 if (rc) 9527 return rc; 9528 9529 resp = hwrm_req_hold(bp, req); 9530 rc = hwrm_req_send(bp, req); 9531 if (rc) 9532 goto qportcfg_exit; 9533 9534 if (!resp->max_configurable_queues) { 9535 rc = -EINVAL; 9536 goto qportcfg_exit; 9537 } 9538 bp->max_tc = resp->max_configurable_queues; 9539 bp->max_lltc = resp->max_configurable_lossless_queues; 9540 if (bp->max_tc > BNXT_MAX_QUEUE) 9541 bp->max_tc = BNXT_MAX_QUEUE; 9542 9543 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP); 9544 qptr = &resp->queue_id0; 9545 for (i = 0, j = 0; i < bp->max_tc; i++) { 9546 bp->q_info[j].queue_id = *qptr; 9547 bp->q_ids[i] = *qptr++; 9548 bp->q_info[j].queue_profile = *qptr++; 9549 bp->tc_to_qidx[j] = j; 9550 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) || 9551 (no_rdma && BNXT_PF(bp))) 9552 j++; 9553 } 9554 bp->max_q = bp->max_tc; 9555 bp->max_tc = max_t(u8, j, 1); 9556 9557 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG) 9558 bp->max_tc = 1; 9559 9560 if (bp->max_lltc > bp->max_tc) 9561 bp->max_lltc = bp->max_tc; 9562 9563 qportcfg_exit: 9564 hwrm_req_drop(bp, req); 9565 return rc; 9566 } 9567 9568 static int bnxt_hwrm_poll(struct bnxt *bp) 9569 { 9570 struct hwrm_ver_get_input *req; 9571 int rc; 9572 9573 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9574 if (rc) 9575 return rc; 9576 9577 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9578 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9579 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9580 9581 hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT); 9582 rc = hwrm_req_send(bp, req); 9583 return rc; 9584 } 9585 9586 static int bnxt_hwrm_ver_get(struct bnxt *bp) 9587 { 9588 struct hwrm_ver_get_output *resp; 9589 struct hwrm_ver_get_input *req; 9590 u16 fw_maj, fw_min, fw_bld, fw_rsv; 9591 u32 dev_caps_cfg, hwrm_ver; 9592 int rc, len; 9593 9594 rc = hwrm_req_init(bp, req, HWRM_VER_GET); 9595 if (rc) 9596 return rc; 9597 9598 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 9599 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN; 9600 req->hwrm_intf_maj = HWRM_VERSION_MAJOR; 9601 req->hwrm_intf_min = HWRM_VERSION_MINOR; 9602 req->hwrm_intf_upd = HWRM_VERSION_UPDATE; 9603 9604 resp = hwrm_req_hold(bp, req); 9605 rc = hwrm_req_send(bp, req); 9606 if (rc) 9607 goto hwrm_ver_get_exit; 9608 9609 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output)); 9610 9611 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 | 9612 resp->hwrm_intf_min_8b << 8 | 9613 resp->hwrm_intf_upd_8b; 9614 if (resp->hwrm_intf_maj_8b < 1) { 9615 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n", 9616 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 9617 resp->hwrm_intf_upd_8b); 9618 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n"); 9619 } 9620 9621 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 | 9622 HWRM_VERSION_UPDATE; 9623 9624 if (bp->hwrm_spec_code > hwrm_ver) 9625 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 9626 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR, 9627 HWRM_VERSION_UPDATE); 9628 else 9629 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d", 9630 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b, 9631 resp->hwrm_intf_upd_8b); 9632 9633 fw_maj = le16_to_cpu(resp->hwrm_fw_major); 9634 if (bp->hwrm_spec_code > 0x10803 && fw_maj) { 9635 fw_min = le16_to_cpu(resp->hwrm_fw_minor); 9636 fw_bld = le16_to_cpu(resp->hwrm_fw_build); 9637 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch); 9638 len = FW_VER_STR_LEN; 9639 } else { 9640 fw_maj = resp->hwrm_fw_maj_8b; 9641 fw_min = resp->hwrm_fw_min_8b; 9642 fw_bld = resp->hwrm_fw_bld_8b; 9643 fw_rsv = resp->hwrm_fw_rsvd_8b; 9644 len = BC_HWRM_STR_LEN; 9645 } 9646 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv); 9647 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld, 9648 fw_rsv); 9649 9650 if (strlen(resp->active_pkg_name)) { 9651 int fw_ver_len = strlen(bp->fw_ver_str); 9652 9653 snprintf(bp->fw_ver_str + fw_ver_len, 9654 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s", 9655 resp->active_pkg_name); 9656 bp->fw_cap |= BNXT_FW_CAP_PKG_VER; 9657 } 9658 9659 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout); 9660 if (!bp->hwrm_cmd_timeout) 9661 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 9662 bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000; 9663 if (!bp->hwrm_cmd_max_timeout) 9664 bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT; 9665 else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT) 9666 netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n", 9667 bp->hwrm_cmd_max_timeout / 1000); 9668 9669 if (resp->hwrm_intf_maj_8b >= 1) { 9670 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len); 9671 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len); 9672 } 9673 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN) 9674 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN; 9675 9676 bp->chip_num = le16_to_cpu(resp->chip_num); 9677 bp->chip_rev = resp->chip_rev; 9678 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev && 9679 !resp->chip_metal) 9680 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0; 9681 9682 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg); 9683 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) && 9684 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED)) 9685 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD; 9686 9687 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED) 9688 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL; 9689 9690 if (dev_caps_cfg & 9691 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED) 9692 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE; 9693 9694 if (dev_caps_cfg & 9695 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED) 9696 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF; 9697 9698 if (dev_caps_cfg & 9699 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED) 9700 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW; 9701 9702 hwrm_ver_get_exit: 9703 hwrm_req_drop(bp, req); 9704 return rc; 9705 } 9706 9707 int bnxt_hwrm_fw_set_time(struct bnxt *bp) 9708 { 9709 struct hwrm_fw_set_time_input *req; 9710 struct tm tm; 9711 time64_t now = ktime_get_real_seconds(); 9712 int rc; 9713 9714 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) || 9715 bp->hwrm_spec_code < 0x10400) 9716 return -EOPNOTSUPP; 9717 9718 time64_to_tm(now, 0, &tm); 9719 rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME); 9720 if (rc) 9721 return rc; 9722 9723 req->year = cpu_to_le16(1900 + tm.tm_year); 9724 req->month = 1 + tm.tm_mon; 9725 req->day = tm.tm_mday; 9726 req->hour = tm.tm_hour; 9727 req->minute = tm.tm_min; 9728 req->second = tm.tm_sec; 9729 return hwrm_req_send(bp, req); 9730 } 9731 9732 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask) 9733 { 9734 u64 sw_tmp; 9735 9736 hw &= mask; 9737 sw_tmp = (*sw & ~mask) | hw; 9738 if (hw < (*sw & mask)) 9739 sw_tmp += mask + 1; 9740 WRITE_ONCE(*sw, sw_tmp); 9741 } 9742 9743 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks, 9744 int count, bool ignore_zero) 9745 { 9746 int i; 9747 9748 for (i = 0; i < count; i++) { 9749 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i])); 9750 9751 if (ignore_zero && !hw) 9752 continue; 9753 9754 if (masks[i] == -1ULL) 9755 sw_stats[i] = hw; 9756 else 9757 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]); 9758 } 9759 } 9760 9761 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats) 9762 { 9763 if (!stats->hw_stats) 9764 return; 9765 9766 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 9767 stats->hw_masks, stats->len / 8, false); 9768 } 9769 9770 static void bnxt_accumulate_all_stats(struct bnxt *bp) 9771 { 9772 struct bnxt_stats_mem *ring0_stats; 9773 bool ignore_zero = false; 9774 int i; 9775 9776 /* Chip bug. Counter intermittently becomes 0. */ 9777 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 9778 ignore_zero = true; 9779 9780 for (i = 0; i < bp->cp_nr_rings; i++) { 9781 struct bnxt_napi *bnapi = bp->bnapi[i]; 9782 struct bnxt_cp_ring_info *cpr; 9783 struct bnxt_stats_mem *stats; 9784 9785 cpr = &bnapi->cp_ring; 9786 stats = &cpr->stats; 9787 if (!i) 9788 ring0_stats = stats; 9789 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats, 9790 ring0_stats->hw_masks, 9791 ring0_stats->len / 8, ignore_zero); 9792 } 9793 if (bp->flags & BNXT_FLAG_PORT_STATS) { 9794 struct bnxt_stats_mem *stats = &bp->port_stats; 9795 __le64 *hw_stats = stats->hw_stats; 9796 u64 *sw_stats = stats->sw_stats; 9797 u64 *masks = stats->hw_masks; 9798 int cnt; 9799 9800 cnt = sizeof(struct rx_port_stats) / 8; 9801 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 9802 9803 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9804 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9805 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 9806 cnt = sizeof(struct tx_port_stats) / 8; 9807 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false); 9808 } 9809 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) { 9810 bnxt_accumulate_stats(&bp->rx_port_stats_ext); 9811 bnxt_accumulate_stats(&bp->tx_port_stats_ext); 9812 } 9813 } 9814 9815 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags) 9816 { 9817 struct hwrm_port_qstats_input *req; 9818 struct bnxt_pf_info *pf = &bp->pf; 9819 int rc; 9820 9821 if (!(bp->flags & BNXT_FLAG_PORT_STATS)) 9822 return 0; 9823 9824 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 9825 return -EOPNOTSUPP; 9826 9827 rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS); 9828 if (rc) 9829 return rc; 9830 9831 req->flags = flags; 9832 req->port_id = cpu_to_le16(pf->port_id); 9833 req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map + 9834 BNXT_TX_PORT_STATS_BYTE_OFFSET); 9835 req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map); 9836 return hwrm_req_send(bp, req); 9837 } 9838 9839 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags) 9840 { 9841 struct hwrm_queue_pri2cos_qcfg_output *resp_qc; 9842 struct hwrm_queue_pri2cos_qcfg_input *req_qc; 9843 struct hwrm_port_qstats_ext_output *resp_qs; 9844 struct hwrm_port_qstats_ext_input *req_qs; 9845 struct bnxt_pf_info *pf = &bp->pf; 9846 u32 tx_stat_size; 9847 int rc; 9848 9849 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 9850 return 0; 9851 9852 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)) 9853 return -EOPNOTSUPP; 9854 9855 rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT); 9856 if (rc) 9857 return rc; 9858 9859 req_qs->flags = flags; 9860 req_qs->port_id = cpu_to_le16(pf->port_id); 9861 req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext)); 9862 req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map); 9863 tx_stat_size = bp->tx_port_stats_ext.hw_stats ? 9864 sizeof(struct tx_port_stats_ext) : 0; 9865 req_qs->tx_stat_size = cpu_to_le16(tx_stat_size); 9866 req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map); 9867 resp_qs = hwrm_req_hold(bp, req_qs); 9868 rc = hwrm_req_send(bp, req_qs); 9869 if (!rc) { 9870 bp->fw_rx_stats_ext_size = 9871 le16_to_cpu(resp_qs->rx_stat_size) / 8; 9872 if (BNXT_FW_MAJ(bp) < 220 && 9873 bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY) 9874 bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY; 9875 9876 bp->fw_tx_stats_ext_size = tx_stat_size ? 9877 le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0; 9878 } else { 9879 bp->fw_rx_stats_ext_size = 0; 9880 bp->fw_tx_stats_ext_size = 0; 9881 } 9882 hwrm_req_drop(bp, req_qs); 9883 9884 if (flags) 9885 return rc; 9886 9887 if (bp->fw_tx_stats_ext_size <= 9888 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) { 9889 bp->pri2cos_valid = 0; 9890 return rc; 9891 } 9892 9893 rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG); 9894 if (rc) 9895 return rc; 9896 9897 req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN); 9898 9899 resp_qc = hwrm_req_hold(bp, req_qc); 9900 rc = hwrm_req_send(bp, req_qc); 9901 if (!rc) { 9902 u8 *pri2cos; 9903 int i, j; 9904 9905 pri2cos = &resp_qc->pri0_cos_queue_id; 9906 for (i = 0; i < 8; i++) { 9907 u8 queue_id = pri2cos[i]; 9908 u8 queue_idx; 9909 9910 /* Per port queue IDs start from 0, 10, 20, etc */ 9911 queue_idx = queue_id % 10; 9912 if (queue_idx > BNXT_MAX_QUEUE) { 9913 bp->pri2cos_valid = false; 9914 hwrm_req_drop(bp, req_qc); 9915 return rc; 9916 } 9917 for (j = 0; j < bp->max_q; j++) { 9918 if (bp->q_ids[j] == queue_id) 9919 bp->pri2cos_idx[i] = queue_idx; 9920 } 9921 } 9922 bp->pri2cos_valid = true; 9923 } 9924 hwrm_req_drop(bp, req_qc); 9925 9926 return rc; 9927 } 9928 9929 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp) 9930 { 9931 bnxt_hwrm_tunnel_dst_port_free(bp, 9932 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN); 9933 bnxt_hwrm_tunnel_dst_port_free(bp, 9934 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE); 9935 } 9936 9937 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa) 9938 { 9939 int rc, i; 9940 u32 tpa_flags = 0; 9941 9942 if (set_tpa) 9943 tpa_flags = bp->flags & BNXT_FLAG_TPA; 9944 else if (BNXT_NO_FW_ACCESS(bp)) 9945 return 0; 9946 for (i = 0; i < bp->nr_vnics; i++) { 9947 rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags); 9948 if (rc) { 9949 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n", 9950 i, rc); 9951 return rc; 9952 } 9953 } 9954 return 0; 9955 } 9956 9957 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp) 9958 { 9959 int i; 9960 9961 for (i = 0; i < bp->nr_vnics; i++) 9962 bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false); 9963 } 9964 9965 static void bnxt_clear_vnic(struct bnxt *bp) 9966 { 9967 if (!bp->vnic_info) 9968 return; 9969 9970 bnxt_hwrm_clear_vnic_filter(bp); 9971 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) { 9972 /* clear all RSS setting before free vnic ctx */ 9973 bnxt_hwrm_clear_vnic_rss(bp); 9974 bnxt_hwrm_vnic_ctx_free(bp); 9975 } 9976 /* before free the vnic, undo the vnic tpa settings */ 9977 if (bp->flags & BNXT_FLAG_TPA) 9978 bnxt_set_tpa(bp, false); 9979 bnxt_hwrm_vnic_free(bp); 9980 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 9981 bnxt_hwrm_vnic_ctx_free(bp); 9982 } 9983 9984 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path, 9985 bool irq_re_init) 9986 { 9987 bnxt_clear_vnic(bp); 9988 bnxt_hwrm_ring_free(bp, close_path); 9989 bnxt_hwrm_ring_grp_free(bp); 9990 if (irq_re_init) { 9991 bnxt_hwrm_stat_ctx_free(bp); 9992 bnxt_hwrm_free_tunnel_ports(bp); 9993 } 9994 } 9995 9996 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode) 9997 { 9998 struct hwrm_func_cfg_input *req; 9999 u8 evb_mode; 10000 int rc; 10001 10002 if (br_mode == BRIDGE_MODE_VEB) 10003 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB; 10004 else if (br_mode == BRIDGE_MODE_VEPA) 10005 evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA; 10006 else 10007 return -EINVAL; 10008 10009 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10010 if (rc) 10011 return rc; 10012 10013 req->fid = cpu_to_le16(0xffff); 10014 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE); 10015 req->evb_mode = evb_mode; 10016 return hwrm_req_send(bp, req); 10017 } 10018 10019 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size) 10020 { 10021 struct hwrm_func_cfg_input *req; 10022 int rc; 10023 10024 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803) 10025 return 0; 10026 10027 rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req); 10028 if (rc) 10029 return rc; 10030 10031 req->fid = cpu_to_le16(0xffff); 10032 req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE); 10033 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64; 10034 if (size == 128) 10035 req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128; 10036 10037 return hwrm_req_send(bp, req); 10038 } 10039 10040 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10041 { 10042 int rc; 10043 10044 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) 10045 goto skip_rss_ctx; 10046 10047 /* allocate context for vnic */ 10048 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0); 10049 if (rc) { 10050 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10051 vnic->vnic_id, rc); 10052 goto vnic_setup_err; 10053 } 10054 bp->rsscos_nr_ctxs++; 10055 10056 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10057 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1); 10058 if (rc) { 10059 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n", 10060 vnic->vnic_id, rc); 10061 goto vnic_setup_err; 10062 } 10063 bp->rsscos_nr_ctxs++; 10064 } 10065 10066 skip_rss_ctx: 10067 /* configure default vnic, ring grp */ 10068 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10069 if (rc) { 10070 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10071 vnic->vnic_id, rc); 10072 goto vnic_setup_err; 10073 } 10074 10075 /* Enable RSS hashing on vnic */ 10076 rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true); 10077 if (rc) { 10078 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n", 10079 vnic->vnic_id, rc); 10080 goto vnic_setup_err; 10081 } 10082 10083 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10084 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10085 if (rc) { 10086 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10087 vnic->vnic_id, rc); 10088 } 10089 } 10090 10091 vnic_setup_err: 10092 return rc; 10093 } 10094 10095 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic, 10096 u8 valid) 10097 { 10098 struct hwrm_vnic_update_input *req; 10099 int rc; 10100 10101 rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE); 10102 if (rc) 10103 return rc; 10104 10105 req->vnic_id = cpu_to_le32(vnic->fw_vnic_id); 10106 10107 if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID) 10108 req->mru = cpu_to_le16(vnic->mru); 10109 10110 req->enables = cpu_to_le32(valid); 10111 10112 return hwrm_req_send(bp, req); 10113 } 10114 10115 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10116 { 10117 int rc; 10118 10119 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true); 10120 if (rc) { 10121 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n", 10122 vnic->vnic_id, rc); 10123 return rc; 10124 } 10125 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10126 if (rc) 10127 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n", 10128 vnic->vnic_id, rc); 10129 return rc; 10130 } 10131 10132 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10133 { 10134 int rc, i, nr_ctxs; 10135 10136 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings); 10137 for (i = 0; i < nr_ctxs; i++) { 10138 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i); 10139 if (rc) { 10140 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n", 10141 vnic->vnic_id, i, rc); 10142 break; 10143 } 10144 bp->rsscos_nr_ctxs++; 10145 } 10146 if (i < nr_ctxs) 10147 return -ENOMEM; 10148 10149 rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic); 10150 if (rc) 10151 return rc; 10152 10153 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 10154 rc = bnxt_hwrm_vnic_set_hds(bp, vnic); 10155 if (rc) { 10156 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n", 10157 vnic->vnic_id, rc); 10158 } 10159 } 10160 return rc; 10161 } 10162 10163 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic) 10164 { 10165 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10166 return __bnxt_setup_vnic_p5(bp, vnic); 10167 else 10168 return __bnxt_setup_vnic(bp, vnic); 10169 } 10170 10171 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp, 10172 struct bnxt_vnic_info *vnic, 10173 u16 start_rx_ring_idx, int rx_rings) 10174 { 10175 int rc; 10176 10177 rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings); 10178 if (rc) { 10179 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n", 10180 vnic->vnic_id, rc); 10181 return rc; 10182 } 10183 return bnxt_setup_vnic(bp, vnic); 10184 } 10185 10186 static int bnxt_alloc_rfs_vnics(struct bnxt *bp) 10187 { 10188 struct bnxt_vnic_info *vnic; 10189 int i, rc = 0; 10190 10191 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) { 10192 vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE]; 10193 return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings); 10194 } 10195 10196 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10197 return 0; 10198 10199 for (i = 0; i < bp->rx_nr_rings; i++) { 10200 u16 vnic_id = i + 1; 10201 u16 ring_id = i; 10202 10203 if (vnic_id >= bp->nr_vnics) 10204 break; 10205 10206 vnic = &bp->vnic_info[vnic_id]; 10207 vnic->flags |= BNXT_VNIC_RFS_FLAG; 10208 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 10209 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG; 10210 if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1)) 10211 break; 10212 } 10213 return rc; 10214 } 10215 10216 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx, 10217 bool all) 10218 { 10219 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10220 struct bnxt_filter_base *usr_fltr, *tmp; 10221 struct bnxt_ntuple_filter *ntp_fltr; 10222 int i; 10223 10224 if (netif_running(bp->dev)) { 10225 bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic); 10226 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) { 10227 if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID) 10228 bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i); 10229 } 10230 } 10231 if (!all) 10232 return; 10233 10234 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) { 10235 if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) && 10236 usr_fltr->fw_vnic_id == rss_ctx->index) { 10237 ntp_fltr = container_of(usr_fltr, 10238 struct bnxt_ntuple_filter, 10239 base); 10240 bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr); 10241 bnxt_del_ntp_filter(bp, ntp_fltr); 10242 bnxt_del_one_usr_fltr(bp, usr_fltr); 10243 } 10244 } 10245 10246 if (vnic->rss_table) 10247 dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size, 10248 vnic->rss_table, 10249 vnic->rss_table_dma_addr); 10250 bp->num_rss_ctx--; 10251 } 10252 10253 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp) 10254 { 10255 bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA); 10256 struct ethtool_rxfh_context *ctx; 10257 unsigned long context; 10258 10259 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10260 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10261 struct bnxt_vnic_info *vnic = &rss_ctx->vnic; 10262 10263 if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) || 10264 bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) || 10265 __bnxt_setup_vnic_p5(bp, vnic)) { 10266 netdev_err(bp->dev, "Failed to restore RSS ctx %d\n", 10267 rss_ctx->index); 10268 bnxt_del_one_rss_ctx(bp, rss_ctx, true); 10269 ethtool_rxfh_context_lost(bp->dev, rss_ctx->index); 10270 } 10271 } 10272 } 10273 10274 static void bnxt_clear_rss_ctxs(struct bnxt *bp) 10275 { 10276 struct ethtool_rxfh_context *ctx; 10277 unsigned long context; 10278 10279 xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) { 10280 struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx); 10281 10282 bnxt_del_one_rss_ctx(bp, rss_ctx, false); 10283 } 10284 } 10285 10286 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */ 10287 static bool bnxt_promisc_ok(struct bnxt *bp) 10288 { 10289 #ifdef CONFIG_BNXT_SRIOV 10290 if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf)) 10291 return false; 10292 #endif 10293 return true; 10294 } 10295 10296 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp) 10297 { 10298 struct bnxt_vnic_info *vnic = &bp->vnic_info[1]; 10299 unsigned int rc = 0; 10300 10301 rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1); 10302 if (rc) { 10303 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10304 rc); 10305 return rc; 10306 } 10307 10308 rc = bnxt_hwrm_vnic_cfg(bp, vnic); 10309 if (rc) { 10310 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n", 10311 rc); 10312 return rc; 10313 } 10314 return rc; 10315 } 10316 10317 static int bnxt_cfg_rx_mode(struct bnxt *); 10318 static bool bnxt_mc_list_updated(struct bnxt *, u32 *); 10319 10320 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init) 10321 { 10322 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 10323 int rc = 0; 10324 unsigned int rx_nr_rings = bp->rx_nr_rings; 10325 10326 if (irq_re_init) { 10327 rc = bnxt_hwrm_stat_ctx_alloc(bp); 10328 if (rc) { 10329 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n", 10330 rc); 10331 goto err_out; 10332 } 10333 } 10334 10335 rc = bnxt_hwrm_ring_alloc(bp); 10336 if (rc) { 10337 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc); 10338 goto err_out; 10339 } 10340 10341 rc = bnxt_hwrm_ring_grp_alloc(bp); 10342 if (rc) { 10343 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc); 10344 goto err_out; 10345 } 10346 10347 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10348 rx_nr_rings--; 10349 10350 /* default vnic 0 */ 10351 rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings); 10352 if (rc) { 10353 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc); 10354 goto err_out; 10355 } 10356 10357 if (BNXT_VF(bp)) 10358 bnxt_hwrm_func_qcfg(bp); 10359 10360 rc = bnxt_setup_vnic(bp, vnic); 10361 if (rc) 10362 goto err_out; 10363 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 10364 bnxt_hwrm_update_rss_hash_cfg(bp); 10365 10366 if (bp->flags & BNXT_FLAG_RFS) { 10367 rc = bnxt_alloc_rfs_vnics(bp); 10368 if (rc) 10369 goto err_out; 10370 } 10371 10372 if (bp->flags & BNXT_FLAG_TPA) { 10373 rc = bnxt_set_tpa(bp, true); 10374 if (rc) 10375 goto err_out; 10376 } 10377 10378 if (BNXT_VF(bp)) 10379 bnxt_update_vf_mac(bp); 10380 10381 /* Filter for default vnic 0 */ 10382 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr); 10383 if (rc) { 10384 if (BNXT_VF(bp) && rc == -ENODEV) 10385 netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n"); 10386 else 10387 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 10388 goto err_out; 10389 } 10390 vnic->uc_filter_count = 1; 10391 10392 vnic->rx_mask = 0; 10393 if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state)) 10394 goto skip_rx_mask; 10395 10396 if (bp->dev->flags & IFF_BROADCAST) 10397 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 10398 10399 if (bp->dev->flags & IFF_PROMISC) 10400 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 10401 10402 if (bp->dev->flags & IFF_ALLMULTI) { 10403 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 10404 vnic->mc_list_count = 0; 10405 } else if (bp->dev->flags & IFF_MULTICAST) { 10406 u32 mask = 0; 10407 10408 bnxt_mc_list_updated(bp, &mask); 10409 vnic->rx_mask |= mask; 10410 } 10411 10412 rc = bnxt_cfg_rx_mode(bp); 10413 if (rc) 10414 goto err_out; 10415 10416 skip_rx_mask: 10417 rc = bnxt_hwrm_set_coal(bp); 10418 if (rc) 10419 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n", 10420 rc); 10421 10422 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10423 rc = bnxt_setup_nitroa0_vnic(bp); 10424 if (rc) 10425 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n", 10426 rc); 10427 } 10428 10429 if (BNXT_VF(bp)) { 10430 bnxt_hwrm_func_qcfg(bp); 10431 netdev_update_features(bp->dev); 10432 } 10433 10434 return 0; 10435 10436 err_out: 10437 bnxt_hwrm_resource_free(bp, 0, true); 10438 10439 return rc; 10440 } 10441 10442 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init) 10443 { 10444 bnxt_hwrm_resource_free(bp, 1, irq_re_init); 10445 return 0; 10446 } 10447 10448 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init) 10449 { 10450 bnxt_init_cp_rings(bp); 10451 bnxt_init_rx_rings(bp); 10452 bnxt_init_tx_rings(bp); 10453 bnxt_init_ring_grps(bp, irq_re_init); 10454 bnxt_init_vnics(bp); 10455 10456 return bnxt_init_chip(bp, irq_re_init); 10457 } 10458 10459 static int bnxt_set_real_num_queues(struct bnxt *bp) 10460 { 10461 int rc; 10462 struct net_device *dev = bp->dev; 10463 10464 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings - 10465 bp->tx_nr_rings_xdp); 10466 if (rc) 10467 return rc; 10468 10469 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings); 10470 if (rc) 10471 return rc; 10472 10473 #ifdef CONFIG_RFS_ACCEL 10474 if (bp->flags & BNXT_FLAG_RFS) 10475 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings); 10476 #endif 10477 10478 return rc; 10479 } 10480 10481 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10482 bool shared) 10483 { 10484 int _rx = *rx, _tx = *tx; 10485 10486 if (shared) { 10487 *rx = min_t(int, _rx, max); 10488 *tx = min_t(int, _tx, max); 10489 } else { 10490 if (max < 2) 10491 return -ENOMEM; 10492 10493 while (_rx + _tx > max) { 10494 if (_rx > _tx && _rx > 1) 10495 _rx--; 10496 else if (_tx > 1) 10497 _tx--; 10498 } 10499 *rx = _rx; 10500 *tx = _tx; 10501 } 10502 return 0; 10503 } 10504 10505 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp) 10506 { 10507 return (tx - tx_xdp) / tx_sets + tx_xdp; 10508 } 10509 10510 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx) 10511 { 10512 int tcs = bp->num_tc; 10513 10514 if (!tcs) 10515 tcs = 1; 10516 return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp); 10517 } 10518 10519 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp) 10520 { 10521 int tcs = bp->num_tc; 10522 10523 return (tx_cp - bp->tx_nr_rings_xdp) * tcs + 10524 bp->tx_nr_rings_xdp; 10525 } 10526 10527 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max, 10528 bool sh) 10529 { 10530 int tx_cp = bnxt_num_tx_to_cp(bp, *tx); 10531 10532 if (tx_cp != *tx) { 10533 int tx_saved = tx_cp, rc; 10534 10535 rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh); 10536 if (rc) 10537 return rc; 10538 if (tx_cp != tx_saved) 10539 *tx = bnxt_num_cp_to_tx(bp, tx_cp); 10540 return 0; 10541 } 10542 return __bnxt_trim_rings(bp, rx, tx, max, sh); 10543 } 10544 10545 static void bnxt_setup_msix(struct bnxt *bp) 10546 { 10547 const int len = sizeof(bp->irq_tbl[0].name); 10548 struct net_device *dev = bp->dev; 10549 int tcs, i; 10550 10551 tcs = bp->num_tc; 10552 if (tcs) { 10553 int i, off, count; 10554 10555 for (i = 0; i < tcs; i++) { 10556 count = bp->tx_nr_rings_per_tc; 10557 off = BNXT_TC_TO_RING_BASE(bp, i); 10558 netdev_set_tc_queue(dev, i, count, off); 10559 } 10560 } 10561 10562 for (i = 0; i < bp->cp_nr_rings; i++) { 10563 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10564 char *attr; 10565 10566 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 10567 attr = "TxRx"; 10568 else if (i < bp->rx_nr_rings) 10569 attr = "rx"; 10570 else 10571 attr = "tx"; 10572 10573 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name, 10574 attr, i); 10575 bp->irq_tbl[map_idx].handler = bnxt_msix; 10576 } 10577 } 10578 10579 static void bnxt_setup_inta(struct bnxt *bp) 10580 { 10581 const int len = sizeof(bp->irq_tbl[0].name); 10582 10583 if (bp->num_tc) { 10584 netdev_reset_tc(bp->dev); 10585 bp->num_tc = 0; 10586 } 10587 10588 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx", 10589 0); 10590 bp->irq_tbl[0].handler = bnxt_inta; 10591 } 10592 10593 static int bnxt_init_int_mode(struct bnxt *bp); 10594 10595 static int bnxt_setup_int_mode(struct bnxt *bp) 10596 { 10597 int rc; 10598 10599 if (!bp->irq_tbl) { 10600 rc = bnxt_init_int_mode(bp); 10601 if (rc || !bp->irq_tbl) 10602 return rc ?: -ENODEV; 10603 } 10604 10605 if (bp->flags & BNXT_FLAG_USING_MSIX) 10606 bnxt_setup_msix(bp); 10607 else 10608 bnxt_setup_inta(bp); 10609 10610 rc = bnxt_set_real_num_queues(bp); 10611 return rc; 10612 } 10613 10614 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp) 10615 { 10616 return bp->hw_resc.max_rsscos_ctxs; 10617 } 10618 10619 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp) 10620 { 10621 return bp->hw_resc.max_vnics; 10622 } 10623 10624 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp) 10625 { 10626 return bp->hw_resc.max_stat_ctxs; 10627 } 10628 10629 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp) 10630 { 10631 return bp->hw_resc.max_cp_rings; 10632 } 10633 10634 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp) 10635 { 10636 unsigned int cp = bp->hw_resc.max_cp_rings; 10637 10638 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 10639 cp -= bnxt_get_ulp_msix_num(bp); 10640 10641 return cp; 10642 } 10643 10644 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp) 10645 { 10646 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 10647 10648 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10649 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs); 10650 10651 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings); 10652 } 10653 10654 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs) 10655 { 10656 bp->hw_resc.max_irqs = max_irqs; 10657 } 10658 10659 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp) 10660 { 10661 unsigned int cp; 10662 10663 cp = bnxt_get_max_func_cp_rings_for_en(bp); 10664 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10665 return cp - bp->rx_nr_rings - bp->tx_nr_rings; 10666 else 10667 return cp - bp->cp_nr_rings; 10668 } 10669 10670 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp) 10671 { 10672 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp); 10673 } 10674 10675 static int bnxt_get_avail_msix(struct bnxt *bp, int num) 10676 { 10677 int max_irq = bnxt_get_max_func_irqs(bp); 10678 int total_req = bp->cp_nr_rings + num; 10679 10680 if (max_irq < total_req) { 10681 num = max_irq - bp->cp_nr_rings; 10682 if (num <= 0) 10683 return 0; 10684 } 10685 return num; 10686 } 10687 10688 static int bnxt_get_num_msix(struct bnxt *bp) 10689 { 10690 if (!BNXT_NEW_RM(bp)) 10691 return bnxt_get_max_func_irqs(bp); 10692 10693 return bnxt_nq_rings_in_use(bp); 10694 } 10695 10696 static int bnxt_init_msix(struct bnxt *bp) 10697 { 10698 int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp; 10699 struct msix_entry *msix_ent; 10700 10701 total_vecs = bnxt_get_num_msix(bp); 10702 max = bnxt_get_max_func_irqs(bp); 10703 if (total_vecs > max) 10704 total_vecs = max; 10705 10706 if (!total_vecs) 10707 return 0; 10708 10709 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL); 10710 if (!msix_ent) 10711 return -ENOMEM; 10712 10713 for (i = 0; i < total_vecs; i++) { 10714 msix_ent[i].entry = i; 10715 msix_ent[i].vector = 0; 10716 } 10717 10718 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) 10719 min = 2; 10720 10721 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs); 10722 ulp_msix = bnxt_get_ulp_msix_num(bp); 10723 if (total_vecs < 0 || total_vecs < ulp_msix) { 10724 rc = -ENODEV; 10725 goto msix_setup_exit; 10726 } 10727 10728 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL); 10729 if (bp->irq_tbl) { 10730 for (i = 0; i < total_vecs; i++) 10731 bp->irq_tbl[i].vector = msix_ent[i].vector; 10732 10733 bp->total_irqs = total_vecs; 10734 /* Trim rings based upon num of vectors allocated */ 10735 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings, 10736 total_vecs - ulp_msix, min == 1); 10737 if (rc) 10738 goto msix_setup_exit; 10739 10740 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 10741 bp->cp_nr_rings = (min == 1) ? 10742 max_t(int, tx_cp, bp->rx_nr_rings) : 10743 tx_cp + bp->rx_nr_rings; 10744 10745 } else { 10746 rc = -ENOMEM; 10747 goto msix_setup_exit; 10748 } 10749 bp->flags |= BNXT_FLAG_USING_MSIX; 10750 kfree(msix_ent); 10751 return 0; 10752 10753 msix_setup_exit: 10754 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc); 10755 kfree(bp->irq_tbl); 10756 bp->irq_tbl = NULL; 10757 pci_disable_msix(bp->pdev); 10758 kfree(msix_ent); 10759 return rc; 10760 } 10761 10762 static int bnxt_init_inta(struct bnxt *bp) 10763 { 10764 bp->irq_tbl = kzalloc(sizeof(struct bnxt_irq), GFP_KERNEL); 10765 if (!bp->irq_tbl) 10766 return -ENOMEM; 10767 10768 bp->total_irqs = 1; 10769 bp->rx_nr_rings = 1; 10770 bp->tx_nr_rings = 1; 10771 bp->cp_nr_rings = 1; 10772 bp->flags |= BNXT_FLAG_SHARED_RINGS; 10773 bp->irq_tbl[0].vector = bp->pdev->irq; 10774 return 0; 10775 } 10776 10777 static int bnxt_init_int_mode(struct bnxt *bp) 10778 { 10779 int rc = -ENODEV; 10780 10781 if (bp->flags & BNXT_FLAG_MSIX_CAP) 10782 rc = bnxt_init_msix(bp); 10783 10784 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) { 10785 /* fallback to INTA */ 10786 rc = bnxt_init_inta(bp); 10787 } 10788 return rc; 10789 } 10790 10791 static void bnxt_clear_int_mode(struct bnxt *bp) 10792 { 10793 if (bp->flags & BNXT_FLAG_USING_MSIX) 10794 pci_disable_msix(bp->pdev); 10795 10796 kfree(bp->irq_tbl); 10797 bp->irq_tbl = NULL; 10798 bp->flags &= ~BNXT_FLAG_USING_MSIX; 10799 } 10800 10801 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init) 10802 { 10803 bool irq_cleared = false; 10804 int tcs = bp->num_tc; 10805 int irqs_required; 10806 int rc; 10807 10808 if (!bnxt_need_reserve_rings(bp)) 10809 return 0; 10810 10811 if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) { 10812 int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want); 10813 10814 if (ulp_msix > bp->ulp_num_msix_want) 10815 ulp_msix = bp->ulp_num_msix_want; 10816 irqs_required = ulp_msix + bp->cp_nr_rings; 10817 } else { 10818 irqs_required = bnxt_get_num_msix(bp); 10819 } 10820 10821 if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) { 10822 bnxt_ulp_irq_stop(bp); 10823 bnxt_clear_int_mode(bp); 10824 irq_cleared = true; 10825 } 10826 rc = __bnxt_reserve_rings(bp); 10827 if (irq_cleared) { 10828 if (!rc) 10829 rc = bnxt_init_int_mode(bp); 10830 bnxt_ulp_irq_restart(bp, rc); 10831 } 10832 if (rc) { 10833 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc); 10834 return rc; 10835 } 10836 if (tcs && (bp->tx_nr_rings_per_tc * tcs != 10837 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) { 10838 netdev_err(bp->dev, "tx ring reservation failure\n"); 10839 netdev_reset_tc(bp->dev); 10840 bp->num_tc = 0; 10841 if (bp->tx_nr_rings_xdp) 10842 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp; 10843 else 10844 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 10845 return -ENOMEM; 10846 } 10847 return 0; 10848 } 10849 10850 static void bnxt_free_irq(struct bnxt *bp) 10851 { 10852 struct bnxt_irq *irq; 10853 int i; 10854 10855 #ifdef CONFIG_RFS_ACCEL 10856 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap); 10857 bp->dev->rx_cpu_rmap = NULL; 10858 #endif 10859 if (!bp->irq_tbl || !bp->bnapi) 10860 return; 10861 10862 for (i = 0; i < bp->cp_nr_rings; i++) { 10863 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10864 10865 irq = &bp->irq_tbl[map_idx]; 10866 if (irq->requested) { 10867 if (irq->have_cpumask) { 10868 irq_set_affinity_hint(irq->vector, NULL); 10869 free_cpumask_var(irq->cpu_mask); 10870 irq->have_cpumask = 0; 10871 } 10872 free_irq(irq->vector, bp->bnapi[i]); 10873 } 10874 10875 irq->requested = 0; 10876 } 10877 } 10878 10879 static int bnxt_request_irq(struct bnxt *bp) 10880 { 10881 int i, j, rc = 0; 10882 unsigned long flags = 0; 10883 #ifdef CONFIG_RFS_ACCEL 10884 struct cpu_rmap *rmap; 10885 #endif 10886 10887 rc = bnxt_setup_int_mode(bp); 10888 if (rc) { 10889 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n", 10890 rc); 10891 return rc; 10892 } 10893 #ifdef CONFIG_RFS_ACCEL 10894 rmap = bp->dev->rx_cpu_rmap; 10895 #endif 10896 if (!(bp->flags & BNXT_FLAG_USING_MSIX)) 10897 flags = IRQF_SHARED; 10898 10899 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) { 10900 int map_idx = bnxt_cp_num_to_irq_num(bp, i); 10901 struct bnxt_irq *irq = &bp->irq_tbl[map_idx]; 10902 10903 #ifdef CONFIG_RFS_ACCEL 10904 if (rmap && bp->bnapi[i]->rx_ring) { 10905 rc = irq_cpu_rmap_add(rmap, irq->vector); 10906 if (rc) 10907 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n", 10908 j); 10909 j++; 10910 } 10911 #endif 10912 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 10913 bp->bnapi[i]); 10914 if (rc) 10915 break; 10916 10917 netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector); 10918 irq->requested = 1; 10919 10920 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) { 10921 int numa_node = dev_to_node(&bp->pdev->dev); 10922 10923 irq->have_cpumask = 1; 10924 cpumask_set_cpu(cpumask_local_spread(i, numa_node), 10925 irq->cpu_mask); 10926 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask); 10927 if (rc) { 10928 netdev_warn(bp->dev, 10929 "Set affinity failed, IRQ = %d\n", 10930 irq->vector); 10931 break; 10932 } 10933 } 10934 } 10935 return rc; 10936 } 10937 10938 static void bnxt_del_napi(struct bnxt *bp) 10939 { 10940 int i; 10941 10942 if (!bp->bnapi) 10943 return; 10944 10945 for (i = 0; i < bp->rx_nr_rings; i++) 10946 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL); 10947 for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++) 10948 netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL); 10949 10950 for (i = 0; i < bp->cp_nr_rings; i++) { 10951 struct bnxt_napi *bnapi = bp->bnapi[i]; 10952 10953 __netif_napi_del(&bnapi->napi); 10954 } 10955 /* We called __netif_napi_del(), we need 10956 * to respect an RCU grace period before freeing napi structures. 10957 */ 10958 synchronize_net(); 10959 } 10960 10961 static void bnxt_init_napi(struct bnxt *bp) 10962 { 10963 int i; 10964 unsigned int cp_nr_rings = bp->cp_nr_rings; 10965 struct bnxt_napi *bnapi; 10966 10967 if (bp->flags & BNXT_FLAG_USING_MSIX) { 10968 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll; 10969 10970 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 10971 poll_fn = bnxt_poll_p5; 10972 else if (BNXT_CHIP_TYPE_NITRO_A0(bp)) 10973 cp_nr_rings--; 10974 for (i = 0; i < cp_nr_rings; i++) { 10975 bnapi = bp->bnapi[i]; 10976 netif_napi_add(bp->dev, &bnapi->napi, poll_fn); 10977 } 10978 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 10979 bnapi = bp->bnapi[cp_nr_rings]; 10980 netif_napi_add(bp->dev, &bnapi->napi, 10981 bnxt_poll_nitroa0); 10982 } 10983 } else { 10984 bnapi = bp->bnapi[0]; 10985 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll); 10986 } 10987 } 10988 10989 static void bnxt_disable_napi(struct bnxt *bp) 10990 { 10991 int i; 10992 10993 if (!bp->bnapi || 10994 test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state)) 10995 return; 10996 10997 for (i = 0; i < bp->cp_nr_rings; i++) { 10998 struct bnxt_napi *bnapi = bp->bnapi[i]; 10999 struct bnxt_cp_ring_info *cpr; 11000 11001 cpr = &bnapi->cp_ring; 11002 if (bnapi->tx_fault) 11003 cpr->sw_stats->tx.tx_resets++; 11004 if (bnapi->in_reset) 11005 cpr->sw_stats->rx.rx_resets++; 11006 napi_disable(&bnapi->napi); 11007 if (bnapi->rx_ring) 11008 cancel_work_sync(&cpr->dim.work); 11009 } 11010 } 11011 11012 static void bnxt_enable_napi(struct bnxt *bp) 11013 { 11014 int i; 11015 11016 clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state); 11017 for (i = 0; i < bp->cp_nr_rings; i++) { 11018 struct bnxt_napi *bnapi = bp->bnapi[i]; 11019 struct bnxt_cp_ring_info *cpr; 11020 11021 bnapi->tx_fault = 0; 11022 11023 cpr = &bnapi->cp_ring; 11024 bnapi->in_reset = false; 11025 11026 if (bnapi->rx_ring) { 11027 INIT_WORK(&cpr->dim.work, bnxt_dim_work); 11028 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 11029 } 11030 napi_enable(&bnapi->napi); 11031 } 11032 } 11033 11034 void bnxt_tx_disable(struct bnxt *bp) 11035 { 11036 int i; 11037 struct bnxt_tx_ring_info *txr; 11038 11039 if (bp->tx_ring) { 11040 for (i = 0; i < bp->tx_nr_rings; i++) { 11041 txr = &bp->tx_ring[i]; 11042 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING); 11043 } 11044 } 11045 /* Make sure napi polls see @dev_state change */ 11046 synchronize_net(); 11047 /* Drop carrier first to prevent TX timeout */ 11048 netif_carrier_off(bp->dev); 11049 /* Stop all TX queues */ 11050 netif_tx_disable(bp->dev); 11051 } 11052 11053 void bnxt_tx_enable(struct bnxt *bp) 11054 { 11055 int i; 11056 struct bnxt_tx_ring_info *txr; 11057 11058 for (i = 0; i < bp->tx_nr_rings; i++) { 11059 txr = &bp->tx_ring[i]; 11060 WRITE_ONCE(txr->dev_state, 0); 11061 } 11062 /* Make sure napi polls see @dev_state change */ 11063 synchronize_net(); 11064 netif_tx_wake_all_queues(bp->dev); 11065 if (BNXT_LINK_IS_UP(bp)) 11066 netif_carrier_on(bp->dev); 11067 } 11068 11069 static char *bnxt_report_fec(struct bnxt_link_info *link_info) 11070 { 11071 u8 active_fec = link_info->active_fec_sig_mode & 11072 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK; 11073 11074 switch (active_fec) { 11075 default: 11076 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE: 11077 return "None"; 11078 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE: 11079 return "Clause 74 BaseR"; 11080 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE: 11081 return "Clause 91 RS(528,514)"; 11082 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE: 11083 return "Clause 91 RS544_1XN"; 11084 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE: 11085 return "Clause 91 RS(544,514)"; 11086 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE: 11087 return "Clause 91 RS272_1XN"; 11088 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE: 11089 return "Clause 91 RS(272,257)"; 11090 } 11091 } 11092 11093 void bnxt_report_link(struct bnxt *bp) 11094 { 11095 if (BNXT_LINK_IS_UP(bp)) { 11096 const char *signal = ""; 11097 const char *flow_ctrl; 11098 const char *duplex; 11099 u32 speed; 11100 u16 fec; 11101 11102 netif_carrier_on(bp->dev); 11103 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed); 11104 if (speed == SPEED_UNKNOWN) { 11105 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n"); 11106 return; 11107 } 11108 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL) 11109 duplex = "full"; 11110 else 11111 duplex = "half"; 11112 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH) 11113 flow_ctrl = "ON - receive & transmit"; 11114 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX) 11115 flow_ctrl = "ON - transmit"; 11116 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX) 11117 flow_ctrl = "ON - receive"; 11118 else 11119 flow_ctrl = "none"; 11120 if (bp->link_info.phy_qcfg_resp.option_flags & 11121 PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) { 11122 u8 sig_mode = bp->link_info.active_fec_sig_mode & 11123 PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK; 11124 switch (sig_mode) { 11125 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ: 11126 signal = "(NRZ) "; 11127 break; 11128 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4: 11129 signal = "(PAM4 56Gbps) "; 11130 break; 11131 case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112: 11132 signal = "(PAM4 112Gbps) "; 11133 break; 11134 default: 11135 break; 11136 } 11137 } 11138 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n", 11139 speed, signal, duplex, flow_ctrl); 11140 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) 11141 netdev_info(bp->dev, "EEE is %s\n", 11142 bp->eee.eee_active ? "active" : 11143 "not active"); 11144 fec = bp->link_info.fec_cfg; 11145 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED)) 11146 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n", 11147 (fec & BNXT_FEC_AUTONEG) ? "on" : "off", 11148 bnxt_report_fec(&bp->link_info)); 11149 } else { 11150 netif_carrier_off(bp->dev); 11151 netdev_err(bp->dev, "NIC Link is Down\n"); 11152 } 11153 } 11154 11155 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp) 11156 { 11157 if (!resp->supported_speeds_auto_mode && 11158 !resp->supported_speeds_force_mode && 11159 !resp->supported_pam4_speeds_auto_mode && 11160 !resp->supported_pam4_speeds_force_mode && 11161 !resp->supported_speeds2_auto_mode && 11162 !resp->supported_speeds2_force_mode) 11163 return true; 11164 return false; 11165 } 11166 11167 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp) 11168 { 11169 struct bnxt_link_info *link_info = &bp->link_info; 11170 struct hwrm_port_phy_qcaps_output *resp; 11171 struct hwrm_port_phy_qcaps_input *req; 11172 int rc = 0; 11173 11174 if (bp->hwrm_spec_code < 0x10201) 11175 return 0; 11176 11177 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS); 11178 if (rc) 11179 return rc; 11180 11181 resp = hwrm_req_hold(bp, req); 11182 rc = hwrm_req_send(bp, req); 11183 if (rc) 11184 goto hwrm_phy_qcaps_exit; 11185 11186 bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8); 11187 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) { 11188 struct ethtool_keee *eee = &bp->eee; 11189 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode); 11190 11191 _bnxt_fw_to_linkmode(eee->supported, fw_speeds); 11192 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) & 11193 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK; 11194 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) & 11195 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK; 11196 } 11197 11198 if (bp->hwrm_spec_code >= 0x10a01) { 11199 if (bnxt_phy_qcaps_no_speed(resp)) { 11200 link_info->phy_state = BNXT_PHY_STATE_DISABLED; 11201 netdev_warn(bp->dev, "Ethernet link disabled\n"); 11202 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) { 11203 link_info->phy_state = BNXT_PHY_STATE_ENABLED; 11204 netdev_info(bp->dev, "Ethernet link enabled\n"); 11205 /* Phy re-enabled, reprobe the speeds */ 11206 link_info->support_auto_speeds = 0; 11207 link_info->support_pam4_auto_speeds = 0; 11208 link_info->support_auto_speeds2 = 0; 11209 } 11210 } 11211 if (resp->supported_speeds_auto_mode) 11212 link_info->support_auto_speeds = 11213 le16_to_cpu(resp->supported_speeds_auto_mode); 11214 if (resp->supported_pam4_speeds_auto_mode) 11215 link_info->support_pam4_auto_speeds = 11216 le16_to_cpu(resp->supported_pam4_speeds_auto_mode); 11217 if (resp->supported_speeds2_auto_mode) 11218 link_info->support_auto_speeds2 = 11219 le16_to_cpu(resp->supported_speeds2_auto_mode); 11220 11221 bp->port_count = resp->port_cnt; 11222 11223 hwrm_phy_qcaps_exit: 11224 hwrm_req_drop(bp, req); 11225 return rc; 11226 } 11227 11228 static bool bnxt_support_dropped(u16 advertising, u16 supported) 11229 { 11230 u16 diff = advertising ^ supported; 11231 11232 return ((supported | diff) != supported); 11233 } 11234 11235 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info) 11236 { 11237 struct bnxt *bp = container_of(link_info, struct bnxt, link_info); 11238 11239 /* Check if any advertised speeds are no longer supported. The caller 11240 * holds the link_lock mutex, so we can modify link_info settings. 11241 */ 11242 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11243 if (bnxt_support_dropped(link_info->advertising, 11244 link_info->support_auto_speeds2)) { 11245 link_info->advertising = link_info->support_auto_speeds2; 11246 return true; 11247 } 11248 return false; 11249 } 11250 if (bnxt_support_dropped(link_info->advertising, 11251 link_info->support_auto_speeds)) { 11252 link_info->advertising = link_info->support_auto_speeds; 11253 return true; 11254 } 11255 if (bnxt_support_dropped(link_info->advertising_pam4, 11256 link_info->support_pam4_auto_speeds)) { 11257 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds; 11258 return true; 11259 } 11260 return false; 11261 } 11262 11263 int bnxt_update_link(struct bnxt *bp, bool chng_link_state) 11264 { 11265 struct bnxt_link_info *link_info = &bp->link_info; 11266 struct hwrm_port_phy_qcfg_output *resp; 11267 struct hwrm_port_phy_qcfg_input *req; 11268 u8 link_state = link_info->link_state; 11269 bool support_changed; 11270 int rc; 11271 11272 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG); 11273 if (rc) 11274 return rc; 11275 11276 resp = hwrm_req_hold(bp, req); 11277 rc = hwrm_req_send(bp, req); 11278 if (rc) { 11279 hwrm_req_drop(bp, req); 11280 if (BNXT_VF(bp) && rc == -ENODEV) { 11281 netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n"); 11282 rc = 0; 11283 } 11284 return rc; 11285 } 11286 11287 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp)); 11288 link_info->phy_link_status = resp->link; 11289 link_info->duplex = resp->duplex_cfg; 11290 if (bp->hwrm_spec_code >= 0x10800) 11291 link_info->duplex = resp->duplex_state; 11292 link_info->pause = resp->pause; 11293 link_info->auto_mode = resp->auto_mode; 11294 link_info->auto_pause_setting = resp->auto_pause; 11295 link_info->lp_pause = resp->link_partner_adv_pause; 11296 link_info->force_pause_setting = resp->force_pause; 11297 link_info->duplex_setting = resp->duplex_cfg; 11298 if (link_info->phy_link_status == BNXT_LINK_LINK) { 11299 link_info->link_speed = le16_to_cpu(resp->link_speed); 11300 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) 11301 link_info->active_lanes = resp->active_lanes; 11302 } else { 11303 link_info->link_speed = 0; 11304 link_info->active_lanes = 0; 11305 } 11306 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed); 11307 link_info->force_pam4_link_speed = 11308 le16_to_cpu(resp->force_pam4_link_speed); 11309 link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2); 11310 link_info->support_speeds = le16_to_cpu(resp->support_speeds); 11311 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds); 11312 link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2); 11313 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask); 11314 link_info->auto_pam4_link_speeds = 11315 le16_to_cpu(resp->auto_pam4_link_speed_mask); 11316 link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2); 11317 link_info->lp_auto_link_speeds = 11318 le16_to_cpu(resp->link_partner_adv_speeds); 11319 link_info->lp_auto_pam4_link_speeds = 11320 resp->link_partner_pam4_adv_speeds; 11321 link_info->preemphasis = le32_to_cpu(resp->preemphasis); 11322 link_info->phy_ver[0] = resp->phy_maj; 11323 link_info->phy_ver[1] = resp->phy_min; 11324 link_info->phy_ver[2] = resp->phy_bld; 11325 link_info->media_type = resp->media_type; 11326 link_info->phy_type = resp->phy_type; 11327 link_info->transceiver = resp->xcvr_pkg_type; 11328 link_info->phy_addr = resp->eee_config_phy_addr & 11329 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK; 11330 link_info->module_status = resp->module_status; 11331 11332 if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) { 11333 struct ethtool_keee *eee = &bp->eee; 11334 u16 fw_speeds; 11335 11336 eee->eee_active = 0; 11337 if (resp->eee_config_phy_addr & 11338 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) { 11339 eee->eee_active = 1; 11340 fw_speeds = le16_to_cpu( 11341 resp->link_partner_adv_eee_link_speed_mask); 11342 _bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds); 11343 } 11344 11345 /* Pull initial EEE config */ 11346 if (!chng_link_state) { 11347 if (resp->eee_config_phy_addr & 11348 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED) 11349 eee->eee_enabled = 1; 11350 11351 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask); 11352 _bnxt_fw_to_linkmode(eee->advertised, fw_speeds); 11353 11354 if (resp->eee_config_phy_addr & 11355 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) { 11356 __le32 tmr; 11357 11358 eee->tx_lpi_enabled = 1; 11359 tmr = resp->xcvr_identifier_type_tx_lpi_timer; 11360 eee->tx_lpi_timer = le32_to_cpu(tmr) & 11361 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK; 11362 } 11363 } 11364 } 11365 11366 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED; 11367 if (bp->hwrm_spec_code >= 0x10504) { 11368 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg); 11369 link_info->active_fec_sig_mode = resp->active_fec_signal_mode; 11370 } 11371 /* TODO: need to add more logic to report VF link */ 11372 if (chng_link_state) { 11373 if (link_info->phy_link_status == BNXT_LINK_LINK) 11374 link_info->link_state = BNXT_LINK_STATE_UP; 11375 else 11376 link_info->link_state = BNXT_LINK_STATE_DOWN; 11377 if (link_state != link_info->link_state) 11378 bnxt_report_link(bp); 11379 } else { 11380 /* always link down if not require to update link state */ 11381 link_info->link_state = BNXT_LINK_STATE_DOWN; 11382 } 11383 hwrm_req_drop(bp, req); 11384 11385 if (!BNXT_PHY_CFG_ABLE(bp)) 11386 return 0; 11387 11388 support_changed = bnxt_support_speed_dropped(link_info); 11389 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED)) 11390 bnxt_hwrm_set_link_setting(bp, true, false); 11391 return 0; 11392 } 11393 11394 static void bnxt_get_port_module_status(struct bnxt *bp) 11395 { 11396 struct bnxt_link_info *link_info = &bp->link_info; 11397 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp; 11398 u8 module_status; 11399 11400 if (bnxt_update_link(bp, true)) 11401 return; 11402 11403 module_status = link_info->module_status; 11404 switch (module_status) { 11405 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX: 11406 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN: 11407 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG: 11408 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n", 11409 bp->pf.port_id); 11410 if (bp->hwrm_spec_code >= 0x10201) { 11411 netdev_warn(bp->dev, "Module part number %s\n", 11412 resp->phy_vendor_partnumber); 11413 } 11414 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX) 11415 netdev_warn(bp->dev, "TX is disabled\n"); 11416 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN) 11417 netdev_warn(bp->dev, "SFP+ module is shutdown\n"); 11418 } 11419 } 11420 11421 static void 11422 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11423 { 11424 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) { 11425 if (bp->hwrm_spec_code >= 0x10201) 11426 req->auto_pause = 11427 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE; 11428 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11429 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX; 11430 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11431 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX; 11432 req->enables |= 11433 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11434 } else { 11435 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX) 11436 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX; 11437 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX) 11438 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX; 11439 req->enables |= 11440 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE); 11441 if (bp->hwrm_spec_code >= 0x10201) { 11442 req->auto_pause = req->force_pause; 11443 req->enables |= cpu_to_le32( 11444 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE); 11445 } 11446 } 11447 } 11448 11449 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req) 11450 { 11451 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) { 11452 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK; 11453 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11454 req->enables |= 11455 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK); 11456 req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising); 11457 } else if (bp->link_info.advertising) { 11458 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK); 11459 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising); 11460 } 11461 if (bp->link_info.advertising_pam4) { 11462 req->enables |= 11463 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK); 11464 req->auto_link_pam4_speed_mask = 11465 cpu_to_le16(bp->link_info.advertising_pam4); 11466 } 11467 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE); 11468 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG); 11469 } else { 11470 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE); 11471 if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) { 11472 req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed); 11473 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2); 11474 netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n", 11475 (u32)bp->link_info.req_link_speed); 11476 } else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) { 11477 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11478 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED); 11479 } else { 11480 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed); 11481 } 11482 } 11483 11484 /* tell chimp that the setting takes effect immediately */ 11485 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY); 11486 } 11487 11488 int bnxt_hwrm_set_pause(struct bnxt *bp) 11489 { 11490 struct hwrm_port_phy_cfg_input *req; 11491 int rc; 11492 11493 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11494 if (rc) 11495 return rc; 11496 11497 bnxt_hwrm_set_pause_common(bp, req); 11498 11499 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) || 11500 bp->link_info.force_link_chng) 11501 bnxt_hwrm_set_link_common(bp, req); 11502 11503 rc = hwrm_req_send(bp, req); 11504 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) { 11505 /* since changing of pause setting doesn't trigger any link 11506 * change event, the driver needs to update the current pause 11507 * result upon successfully return of the phy_cfg command 11508 */ 11509 bp->link_info.pause = 11510 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl; 11511 bp->link_info.auto_pause_setting = 0; 11512 if (!bp->link_info.force_link_chng) 11513 bnxt_report_link(bp); 11514 } 11515 bp->link_info.force_link_chng = false; 11516 return rc; 11517 } 11518 11519 static void bnxt_hwrm_set_eee(struct bnxt *bp, 11520 struct hwrm_port_phy_cfg_input *req) 11521 { 11522 struct ethtool_keee *eee = &bp->eee; 11523 11524 if (eee->eee_enabled) { 11525 u16 eee_speeds; 11526 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE; 11527 11528 if (eee->tx_lpi_enabled) 11529 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE; 11530 else 11531 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE; 11532 11533 req->flags |= cpu_to_le32(flags); 11534 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised); 11535 req->eee_link_speed_mask = cpu_to_le16(eee_speeds); 11536 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer); 11537 } else { 11538 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE); 11539 } 11540 } 11541 11542 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee) 11543 { 11544 struct hwrm_port_phy_cfg_input *req; 11545 int rc; 11546 11547 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11548 if (rc) 11549 return rc; 11550 11551 if (set_pause) 11552 bnxt_hwrm_set_pause_common(bp, req); 11553 11554 bnxt_hwrm_set_link_common(bp, req); 11555 11556 if (set_eee) 11557 bnxt_hwrm_set_eee(bp, req); 11558 return hwrm_req_send(bp, req); 11559 } 11560 11561 static int bnxt_hwrm_shutdown_link(struct bnxt *bp) 11562 { 11563 struct hwrm_port_phy_cfg_input *req; 11564 int rc; 11565 11566 if (!BNXT_SINGLE_PF(bp)) 11567 return 0; 11568 11569 if (pci_num_vf(bp->pdev) && 11570 !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN)) 11571 return 0; 11572 11573 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG); 11574 if (rc) 11575 return rc; 11576 11577 req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN); 11578 rc = hwrm_req_send(bp, req); 11579 if (!rc) { 11580 mutex_lock(&bp->link_lock); 11581 /* Device is not obliged link down in certain scenarios, even 11582 * when forced. Setting the state unknown is consistent with 11583 * driver startup and will force link state to be reported 11584 * during subsequent open based on PORT_PHY_QCFG. 11585 */ 11586 bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN; 11587 mutex_unlock(&bp->link_lock); 11588 } 11589 return rc; 11590 } 11591 11592 static int bnxt_fw_reset_via_optee(struct bnxt *bp) 11593 { 11594 #ifdef CONFIG_TEE_BNXT_FW 11595 int rc = tee_bnxt_fw_load(); 11596 11597 if (rc) 11598 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc); 11599 11600 return rc; 11601 #else 11602 netdev_err(bp->dev, "OP-TEE not supported\n"); 11603 return -ENODEV; 11604 #endif 11605 } 11606 11607 static int bnxt_try_recover_fw(struct bnxt *bp) 11608 { 11609 if (bp->fw_health && bp->fw_health->status_reliable) { 11610 int retry = 0, rc; 11611 u32 sts; 11612 11613 do { 11614 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 11615 rc = bnxt_hwrm_poll(bp); 11616 if (!BNXT_FW_IS_BOOTING(sts) && 11617 !BNXT_FW_IS_RECOVERING(sts)) 11618 break; 11619 retry++; 11620 } while (rc == -EBUSY && retry < BNXT_FW_RETRY); 11621 11622 if (!BNXT_FW_IS_HEALTHY(sts)) { 11623 netdev_err(bp->dev, 11624 "Firmware not responding, status: 0x%x\n", 11625 sts); 11626 rc = -ENODEV; 11627 } 11628 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) { 11629 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n"); 11630 return bnxt_fw_reset_via_optee(bp); 11631 } 11632 return rc; 11633 } 11634 11635 return -ENODEV; 11636 } 11637 11638 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset) 11639 { 11640 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 11641 11642 if (!BNXT_NEW_RM(bp)) 11643 return; /* no resource reservations required */ 11644 11645 hw_resc->resv_cp_rings = 0; 11646 hw_resc->resv_stat_ctxs = 0; 11647 hw_resc->resv_irqs = 0; 11648 hw_resc->resv_tx_rings = 0; 11649 hw_resc->resv_rx_rings = 0; 11650 hw_resc->resv_hw_ring_grps = 0; 11651 hw_resc->resv_vnics = 0; 11652 hw_resc->resv_rsscos_ctxs = 0; 11653 if (!fw_reset) { 11654 bp->tx_nr_rings = 0; 11655 bp->rx_nr_rings = 0; 11656 } 11657 } 11658 11659 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset) 11660 { 11661 int rc; 11662 11663 if (!BNXT_NEW_RM(bp)) 11664 return 0; /* no resource reservations required */ 11665 11666 rc = bnxt_hwrm_func_resc_qcaps(bp, true); 11667 if (rc) 11668 netdev_err(bp->dev, "resc_qcaps failed\n"); 11669 11670 bnxt_clear_reservations(bp, fw_reset); 11671 11672 return rc; 11673 } 11674 11675 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up) 11676 { 11677 struct hwrm_func_drv_if_change_output *resp; 11678 struct hwrm_func_drv_if_change_input *req; 11679 bool fw_reset = !bp->irq_tbl; 11680 bool resc_reinit = false; 11681 int rc, retry = 0; 11682 u32 flags = 0; 11683 11684 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE)) 11685 return 0; 11686 11687 rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE); 11688 if (rc) 11689 return rc; 11690 11691 if (up) 11692 req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP); 11693 resp = hwrm_req_hold(bp, req); 11694 11695 hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT); 11696 while (retry < BNXT_FW_IF_RETRY) { 11697 rc = hwrm_req_send(bp, req); 11698 if (rc != -EAGAIN) 11699 break; 11700 11701 msleep(50); 11702 retry++; 11703 } 11704 11705 if (rc == -EAGAIN) { 11706 hwrm_req_drop(bp, req); 11707 return rc; 11708 } else if (!rc) { 11709 flags = le32_to_cpu(resp->flags); 11710 } else if (up) { 11711 rc = bnxt_try_recover_fw(bp); 11712 fw_reset = true; 11713 } 11714 hwrm_req_drop(bp, req); 11715 if (rc) 11716 return rc; 11717 11718 if (!up) { 11719 bnxt_inv_fw_health_reg(bp); 11720 return 0; 11721 } 11722 11723 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE) 11724 resc_reinit = true; 11725 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE || 11726 test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 11727 fw_reset = true; 11728 else 11729 bnxt_remap_fw_health_regs(bp); 11730 11731 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) { 11732 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n"); 11733 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 11734 return -ENODEV; 11735 } 11736 if (resc_reinit || fw_reset) { 11737 if (fw_reset) { 11738 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11739 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11740 bnxt_ulp_irq_stop(bp); 11741 bnxt_free_ctx_mem(bp); 11742 bnxt_dcb_free(bp); 11743 rc = bnxt_fw_init_one(bp); 11744 if (rc) { 11745 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11746 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 11747 return rc; 11748 } 11749 bnxt_clear_int_mode(bp); 11750 rc = bnxt_init_int_mode(bp); 11751 if (rc) { 11752 clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 11753 netdev_err(bp->dev, "init int mode failed\n"); 11754 return rc; 11755 } 11756 } 11757 rc = bnxt_cancel_reservations(bp, fw_reset); 11758 } 11759 return rc; 11760 } 11761 11762 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp) 11763 { 11764 struct hwrm_port_led_qcaps_output *resp; 11765 struct hwrm_port_led_qcaps_input *req; 11766 struct bnxt_pf_info *pf = &bp->pf; 11767 int rc; 11768 11769 bp->num_leds = 0; 11770 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601) 11771 return 0; 11772 11773 rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS); 11774 if (rc) 11775 return rc; 11776 11777 req->port_id = cpu_to_le16(pf->port_id); 11778 resp = hwrm_req_hold(bp, req); 11779 rc = hwrm_req_send(bp, req); 11780 if (rc) { 11781 hwrm_req_drop(bp, req); 11782 return rc; 11783 } 11784 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) { 11785 int i; 11786 11787 bp->num_leds = resp->num_leds; 11788 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) * 11789 bp->num_leds); 11790 for (i = 0; i < bp->num_leds; i++) { 11791 struct bnxt_led_info *led = &bp->leds[i]; 11792 __le16 caps = led->led_state_caps; 11793 11794 if (!led->led_group_id || 11795 !BNXT_LED_ALT_BLINK_CAP(caps)) { 11796 bp->num_leds = 0; 11797 break; 11798 } 11799 } 11800 } 11801 hwrm_req_drop(bp, req); 11802 return 0; 11803 } 11804 11805 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp) 11806 { 11807 struct hwrm_wol_filter_alloc_output *resp; 11808 struct hwrm_wol_filter_alloc_input *req; 11809 int rc; 11810 11811 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC); 11812 if (rc) 11813 return rc; 11814 11815 req->port_id = cpu_to_le16(bp->pf.port_id); 11816 req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT; 11817 req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS); 11818 memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN); 11819 11820 resp = hwrm_req_hold(bp, req); 11821 rc = hwrm_req_send(bp, req); 11822 if (!rc) 11823 bp->wol_filter_id = resp->wol_filter_id; 11824 hwrm_req_drop(bp, req); 11825 return rc; 11826 } 11827 11828 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp) 11829 { 11830 struct hwrm_wol_filter_free_input *req; 11831 int rc; 11832 11833 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE); 11834 if (rc) 11835 return rc; 11836 11837 req->port_id = cpu_to_le16(bp->pf.port_id); 11838 req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID); 11839 req->wol_filter_id = bp->wol_filter_id; 11840 11841 return hwrm_req_send(bp, req); 11842 } 11843 11844 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle) 11845 { 11846 struct hwrm_wol_filter_qcfg_output *resp; 11847 struct hwrm_wol_filter_qcfg_input *req; 11848 u16 next_handle = 0; 11849 int rc; 11850 11851 rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG); 11852 if (rc) 11853 return rc; 11854 11855 req->port_id = cpu_to_le16(bp->pf.port_id); 11856 req->handle = cpu_to_le16(handle); 11857 resp = hwrm_req_hold(bp, req); 11858 rc = hwrm_req_send(bp, req); 11859 if (!rc) { 11860 next_handle = le16_to_cpu(resp->next_handle); 11861 if (next_handle != 0) { 11862 if (resp->wol_type == 11863 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) { 11864 bp->wol = 1; 11865 bp->wol_filter_id = resp->wol_filter_id; 11866 } 11867 } 11868 } 11869 hwrm_req_drop(bp, req); 11870 return next_handle; 11871 } 11872 11873 static void bnxt_get_wol_settings(struct bnxt *bp) 11874 { 11875 u16 handle = 0; 11876 11877 bp->wol = 0; 11878 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP)) 11879 return; 11880 11881 do { 11882 handle = bnxt_hwrm_get_wol_fltrs(bp, handle); 11883 } while (handle && handle != 0xffff); 11884 } 11885 11886 static bool bnxt_eee_config_ok(struct bnxt *bp) 11887 { 11888 struct ethtool_keee *eee = &bp->eee; 11889 struct bnxt_link_info *link_info = &bp->link_info; 11890 11891 if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP)) 11892 return true; 11893 11894 if (eee->eee_enabled) { 11895 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 11896 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp); 11897 11898 _bnxt_fw_to_linkmode(advertising, link_info->advertising); 11899 11900 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 11901 eee->eee_enabled = 0; 11902 return false; 11903 } 11904 if (linkmode_andnot(tmp, eee->advertised, advertising)) { 11905 linkmode_and(eee->advertised, advertising, 11906 eee->supported); 11907 return false; 11908 } 11909 } 11910 return true; 11911 } 11912 11913 static int bnxt_update_phy_setting(struct bnxt *bp) 11914 { 11915 int rc; 11916 bool update_link = false; 11917 bool update_pause = false; 11918 bool update_eee = false; 11919 struct bnxt_link_info *link_info = &bp->link_info; 11920 11921 rc = bnxt_update_link(bp, true); 11922 if (rc) { 11923 netdev_err(bp->dev, "failed to update link (rc: %x)\n", 11924 rc); 11925 return rc; 11926 } 11927 if (!BNXT_SINGLE_PF(bp)) 11928 return 0; 11929 11930 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 11931 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) != 11932 link_info->req_flow_ctrl) 11933 update_pause = true; 11934 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) && 11935 link_info->force_pause_setting != link_info->req_flow_ctrl) 11936 update_pause = true; 11937 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) { 11938 if (BNXT_AUTO_MODE(link_info->auto_mode)) 11939 update_link = true; 11940 if (bnxt_force_speed_updated(link_info)) 11941 update_link = true; 11942 if (link_info->req_duplex != link_info->duplex_setting) 11943 update_link = true; 11944 } else { 11945 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE) 11946 update_link = true; 11947 if (bnxt_auto_speed_updated(link_info)) 11948 update_link = true; 11949 } 11950 11951 /* The last close may have shutdown the link, so need to call 11952 * PHY_CFG to bring it back up. 11953 */ 11954 if (!BNXT_LINK_IS_UP(bp)) 11955 update_link = true; 11956 11957 if (!bnxt_eee_config_ok(bp)) 11958 update_eee = true; 11959 11960 if (update_link) 11961 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee); 11962 else if (update_pause) 11963 rc = bnxt_hwrm_set_pause(bp); 11964 if (rc) { 11965 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n", 11966 rc); 11967 return rc; 11968 } 11969 11970 return rc; 11971 } 11972 11973 /* Common routine to pre-map certain register block to different GRC window. 11974 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows 11975 * in PF and 3 windows in VF that can be customized to map in different 11976 * register blocks. 11977 */ 11978 static void bnxt_preset_reg_win(struct bnxt *bp) 11979 { 11980 if (BNXT_PF(bp)) { 11981 /* CAG registers map to GRC window #4 */ 11982 writel(BNXT_CAG_REG_BASE, 11983 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12); 11984 } 11985 } 11986 11987 static int bnxt_init_dflt_ring_mode(struct bnxt *bp); 11988 11989 static int bnxt_reinit_after_abort(struct bnxt *bp) 11990 { 11991 int rc; 11992 11993 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 11994 return -EBUSY; 11995 11996 if (bp->dev->reg_state == NETREG_UNREGISTERED) 11997 return -ENODEV; 11998 11999 rc = bnxt_fw_init_one(bp); 12000 if (!rc) { 12001 bnxt_clear_int_mode(bp); 12002 rc = bnxt_init_int_mode(bp); 12003 if (!rc) { 12004 clear_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12005 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state); 12006 } 12007 } 12008 return rc; 12009 } 12010 12011 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr) 12012 { 12013 struct bnxt_ntuple_filter *ntp_fltr; 12014 struct bnxt_l2_filter *l2_fltr; 12015 12016 if (list_empty(&fltr->list)) 12017 return; 12018 12019 if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) { 12020 ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base); 12021 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 12022 atomic_inc(&l2_fltr->refcnt); 12023 ntp_fltr->l2_fltr = l2_fltr; 12024 if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) { 12025 bnxt_del_ntp_filter(bp, ntp_fltr); 12026 netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n", 12027 fltr->sw_id); 12028 } 12029 } else if (fltr->type == BNXT_FLTR_TYPE_L2) { 12030 l2_fltr = container_of(fltr, struct bnxt_l2_filter, base); 12031 if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) { 12032 bnxt_del_l2_filter(bp, l2_fltr); 12033 netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n", 12034 fltr->sw_id); 12035 } 12036 } 12037 } 12038 12039 static void bnxt_cfg_usr_fltrs(struct bnxt *bp) 12040 { 12041 struct bnxt_filter_base *usr_fltr, *tmp; 12042 12043 list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) 12044 bnxt_cfg_one_usr_fltr(bp, usr_fltr); 12045 } 12046 12047 static int bnxt_set_xps_mapping(struct bnxt *bp) 12048 { 12049 int numa_node = dev_to_node(&bp->pdev->dev); 12050 unsigned int q_idx, map_idx, cpu, i; 12051 const struct cpumask *cpu_mask_ptr; 12052 int nr_cpus = num_online_cpus(); 12053 cpumask_t *q_map; 12054 int rc = 0; 12055 12056 q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL); 12057 if (!q_map) 12058 return -ENOMEM; 12059 12060 /* Create CPU mask for all TX queues across MQPRIO traffic classes. 12061 * Each TC has the same number of TX queues. The nth TX queue for each 12062 * TC will have the same CPU mask. 12063 */ 12064 for (i = 0; i < nr_cpus; i++) { 12065 map_idx = i % bp->tx_nr_rings_per_tc; 12066 cpu = cpumask_local_spread(i, numa_node); 12067 cpu_mask_ptr = get_cpu_mask(cpu); 12068 cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr); 12069 } 12070 12071 /* Register CPU mask for each TX queue except the ones marked for XDP */ 12072 for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) { 12073 map_idx = q_idx % bp->tx_nr_rings_per_tc; 12074 rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx); 12075 if (rc) { 12076 netdev_warn(bp->dev, "Error setting XPS for q:%d\n", 12077 q_idx); 12078 break; 12079 } 12080 } 12081 12082 kfree(q_map); 12083 12084 return rc; 12085 } 12086 12087 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12088 { 12089 int rc = 0; 12090 12091 bnxt_preset_reg_win(bp); 12092 netif_carrier_off(bp->dev); 12093 if (irq_re_init) { 12094 /* Reserve rings now if none were reserved at driver probe. */ 12095 rc = bnxt_init_dflt_ring_mode(bp); 12096 if (rc) { 12097 netdev_err(bp->dev, "Failed to reserve default rings at open\n"); 12098 return rc; 12099 } 12100 } 12101 rc = bnxt_reserve_rings(bp, irq_re_init); 12102 if (rc) 12103 return rc; 12104 if ((bp->flags & BNXT_FLAG_RFS) && 12105 !(bp->flags & BNXT_FLAG_USING_MSIX)) { 12106 /* disable RFS if falling back to INTA */ 12107 bp->dev->hw_features &= ~NETIF_F_NTUPLE; 12108 bp->flags &= ~BNXT_FLAG_RFS; 12109 } 12110 12111 rc = bnxt_alloc_mem(bp, irq_re_init); 12112 if (rc) { 12113 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12114 goto open_err_free_mem; 12115 } 12116 12117 if (irq_re_init) { 12118 bnxt_init_napi(bp); 12119 rc = bnxt_request_irq(bp); 12120 if (rc) { 12121 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc); 12122 goto open_err_irq; 12123 } 12124 } 12125 12126 rc = bnxt_init_nic(bp, irq_re_init); 12127 if (rc) { 12128 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12129 goto open_err_irq; 12130 } 12131 12132 bnxt_enable_napi(bp); 12133 bnxt_debug_dev_init(bp); 12134 12135 if (link_re_init) { 12136 mutex_lock(&bp->link_lock); 12137 rc = bnxt_update_phy_setting(bp); 12138 mutex_unlock(&bp->link_lock); 12139 if (rc) { 12140 netdev_warn(bp->dev, "failed to update phy settings\n"); 12141 if (BNXT_SINGLE_PF(bp)) { 12142 bp->link_info.phy_retry = true; 12143 bp->link_info.phy_retry_expires = 12144 jiffies + 5 * HZ; 12145 } 12146 } 12147 } 12148 12149 if (irq_re_init) { 12150 udp_tunnel_nic_reset_ntf(bp->dev); 12151 rc = bnxt_set_xps_mapping(bp); 12152 if (rc) 12153 netdev_warn(bp->dev, "failed to set xps mapping\n"); 12154 } 12155 12156 if (bp->tx_nr_rings_xdp < num_possible_cpus()) { 12157 if (!static_key_enabled(&bnxt_xdp_locking_key)) 12158 static_branch_enable(&bnxt_xdp_locking_key); 12159 } else if (static_key_enabled(&bnxt_xdp_locking_key)) { 12160 static_branch_disable(&bnxt_xdp_locking_key); 12161 } 12162 set_bit(BNXT_STATE_OPEN, &bp->state); 12163 bnxt_enable_int(bp); 12164 /* Enable TX queues */ 12165 bnxt_tx_enable(bp); 12166 mod_timer(&bp->timer, jiffies + bp->current_interval); 12167 /* Poll link status and check for SFP+ module status */ 12168 mutex_lock(&bp->link_lock); 12169 bnxt_get_port_module_status(bp); 12170 mutex_unlock(&bp->link_lock); 12171 12172 /* VF-reps may need to be re-opened after the PF is re-opened */ 12173 if (BNXT_PF(bp)) 12174 bnxt_vf_reps_open(bp); 12175 if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) 12176 WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS); 12177 bnxt_ptp_init_rtc(bp, true); 12178 bnxt_ptp_cfg_tstamp_filters(bp); 12179 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12180 bnxt_hwrm_realloc_rss_ctx_vnic(bp); 12181 bnxt_cfg_usr_fltrs(bp); 12182 return 0; 12183 12184 open_err_irq: 12185 bnxt_del_napi(bp); 12186 12187 open_err_free_mem: 12188 bnxt_free_skbs(bp); 12189 bnxt_free_irq(bp); 12190 bnxt_free_mem(bp, true); 12191 return rc; 12192 } 12193 12194 /* rtnl_lock held */ 12195 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12196 { 12197 int rc = 0; 12198 12199 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) 12200 rc = -EIO; 12201 if (!rc) 12202 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init); 12203 if (rc) { 12204 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc); 12205 dev_close(bp->dev); 12206 } 12207 return rc; 12208 } 12209 12210 /* rtnl_lock held, open the NIC half way by allocating all resources, but 12211 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline 12212 * self tests. 12213 */ 12214 int bnxt_half_open_nic(struct bnxt *bp) 12215 { 12216 int rc = 0; 12217 12218 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12219 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n"); 12220 rc = -ENODEV; 12221 goto half_open_err; 12222 } 12223 12224 rc = bnxt_alloc_mem(bp, true); 12225 if (rc) { 12226 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc); 12227 goto half_open_err; 12228 } 12229 bnxt_init_napi(bp); 12230 set_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12231 rc = bnxt_init_nic(bp, true); 12232 if (rc) { 12233 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12234 bnxt_del_napi(bp); 12235 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc); 12236 goto half_open_err; 12237 } 12238 return 0; 12239 12240 half_open_err: 12241 bnxt_free_skbs(bp); 12242 bnxt_free_mem(bp, true); 12243 dev_close(bp->dev); 12244 return rc; 12245 } 12246 12247 /* rtnl_lock held, this call can only be made after a previous successful 12248 * call to bnxt_half_open_nic(). 12249 */ 12250 void bnxt_half_close_nic(struct bnxt *bp) 12251 { 12252 bnxt_hwrm_resource_free(bp, false, true); 12253 bnxt_del_napi(bp); 12254 bnxt_free_skbs(bp); 12255 bnxt_free_mem(bp, true); 12256 clear_bit(BNXT_STATE_HALF_OPEN, &bp->state); 12257 } 12258 12259 void bnxt_reenable_sriov(struct bnxt *bp) 12260 { 12261 if (BNXT_PF(bp)) { 12262 struct bnxt_pf_info *pf = &bp->pf; 12263 int n = pf->active_vfs; 12264 12265 if (n) 12266 bnxt_cfg_hw_sriov(bp, &n, true); 12267 } 12268 } 12269 12270 static int bnxt_open(struct net_device *dev) 12271 { 12272 struct bnxt *bp = netdev_priv(dev); 12273 int rc; 12274 12275 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 12276 rc = bnxt_reinit_after_abort(bp); 12277 if (rc) { 12278 if (rc == -EBUSY) 12279 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n"); 12280 else 12281 netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n"); 12282 return -ENODEV; 12283 } 12284 } 12285 12286 rc = bnxt_hwrm_if_change(bp, true); 12287 if (rc) 12288 return rc; 12289 12290 rc = __bnxt_open_nic(bp, true, true); 12291 if (rc) { 12292 bnxt_hwrm_if_change(bp, false); 12293 } else { 12294 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) { 12295 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 12296 bnxt_queue_sp_work(bp, 12297 BNXT_RESTART_ULP_SP_EVENT); 12298 } 12299 } 12300 12301 return rc; 12302 } 12303 12304 static bool bnxt_drv_busy(struct bnxt *bp) 12305 { 12306 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) || 12307 test_bit(BNXT_STATE_READ_STATS, &bp->state)); 12308 } 12309 12310 static void bnxt_get_ring_stats(struct bnxt *bp, 12311 struct rtnl_link_stats64 *stats); 12312 12313 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init, 12314 bool link_re_init) 12315 { 12316 /* Close the VF-reps before closing PF */ 12317 if (BNXT_PF(bp)) 12318 bnxt_vf_reps_close(bp); 12319 12320 /* Change device state to avoid TX queue wake up's */ 12321 bnxt_tx_disable(bp); 12322 12323 clear_bit(BNXT_STATE_OPEN, &bp->state); 12324 smp_mb__after_atomic(); 12325 while (bnxt_drv_busy(bp)) 12326 msleep(20); 12327 12328 if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp)) 12329 bnxt_clear_rss_ctxs(bp); 12330 /* Flush rings and disable interrupts */ 12331 bnxt_shutdown_nic(bp, irq_re_init); 12332 12333 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */ 12334 12335 bnxt_debug_dev_exit(bp); 12336 bnxt_disable_napi(bp); 12337 del_timer_sync(&bp->timer); 12338 bnxt_free_skbs(bp); 12339 12340 /* Save ring stats before shutdown */ 12341 if (bp->bnapi && irq_re_init) { 12342 bnxt_get_ring_stats(bp, &bp->net_stats_prev); 12343 bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev); 12344 } 12345 if (irq_re_init) { 12346 bnxt_free_irq(bp); 12347 bnxt_del_napi(bp); 12348 } 12349 bnxt_free_mem(bp, irq_re_init); 12350 } 12351 12352 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init) 12353 { 12354 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 12355 /* If we get here, it means firmware reset is in progress 12356 * while we are trying to close. We can safely proceed with 12357 * the close because we are holding rtnl_lock(). Some firmware 12358 * messages may fail as we proceed to close. We set the 12359 * ABORT_ERR flag here so that the FW reset thread will later 12360 * abort when it gets the rtnl_lock() and sees the flag. 12361 */ 12362 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n"); 12363 set_bit(BNXT_STATE_ABORT_ERR, &bp->state); 12364 } 12365 12366 #ifdef CONFIG_BNXT_SRIOV 12367 if (bp->sriov_cfg) { 12368 int rc; 12369 12370 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait, 12371 !bp->sriov_cfg, 12372 BNXT_SRIOV_CFG_WAIT_TMO); 12373 if (!rc) 12374 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n"); 12375 else if (rc < 0) 12376 netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n"); 12377 } 12378 #endif 12379 __bnxt_close_nic(bp, irq_re_init, link_re_init); 12380 } 12381 12382 static int bnxt_close(struct net_device *dev) 12383 { 12384 struct bnxt *bp = netdev_priv(dev); 12385 12386 bnxt_close_nic(bp, true, true); 12387 bnxt_hwrm_shutdown_link(bp); 12388 bnxt_hwrm_if_change(bp, false); 12389 return 0; 12390 } 12391 12392 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg, 12393 u16 *val) 12394 { 12395 struct hwrm_port_phy_mdio_read_output *resp; 12396 struct hwrm_port_phy_mdio_read_input *req; 12397 int rc; 12398 12399 if (bp->hwrm_spec_code < 0x10a00) 12400 return -EOPNOTSUPP; 12401 12402 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ); 12403 if (rc) 12404 return rc; 12405 12406 req->port_id = cpu_to_le16(bp->pf.port_id); 12407 req->phy_addr = phy_addr; 12408 req->reg_addr = cpu_to_le16(reg & 0x1f); 12409 if (mdio_phy_id_is_c45(phy_addr)) { 12410 req->cl45_mdio = 1; 12411 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12412 req->dev_addr = mdio_phy_id_devad(phy_addr); 12413 req->reg_addr = cpu_to_le16(reg); 12414 } 12415 12416 resp = hwrm_req_hold(bp, req); 12417 rc = hwrm_req_send(bp, req); 12418 if (!rc) 12419 *val = le16_to_cpu(resp->reg_data); 12420 hwrm_req_drop(bp, req); 12421 return rc; 12422 } 12423 12424 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg, 12425 u16 val) 12426 { 12427 struct hwrm_port_phy_mdio_write_input *req; 12428 int rc; 12429 12430 if (bp->hwrm_spec_code < 0x10a00) 12431 return -EOPNOTSUPP; 12432 12433 rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE); 12434 if (rc) 12435 return rc; 12436 12437 req->port_id = cpu_to_le16(bp->pf.port_id); 12438 req->phy_addr = phy_addr; 12439 req->reg_addr = cpu_to_le16(reg & 0x1f); 12440 if (mdio_phy_id_is_c45(phy_addr)) { 12441 req->cl45_mdio = 1; 12442 req->phy_addr = mdio_phy_id_prtad(phy_addr); 12443 req->dev_addr = mdio_phy_id_devad(phy_addr); 12444 req->reg_addr = cpu_to_le16(reg); 12445 } 12446 req->reg_data = cpu_to_le16(val); 12447 12448 return hwrm_req_send(bp, req); 12449 } 12450 12451 /* rtnl_lock held */ 12452 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 12453 { 12454 struct mii_ioctl_data *mdio = if_mii(ifr); 12455 struct bnxt *bp = netdev_priv(dev); 12456 int rc; 12457 12458 switch (cmd) { 12459 case SIOCGMIIPHY: 12460 mdio->phy_id = bp->link_info.phy_addr; 12461 12462 fallthrough; 12463 case SIOCGMIIREG: { 12464 u16 mii_regval = 0; 12465 12466 if (!netif_running(dev)) 12467 return -EAGAIN; 12468 12469 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num, 12470 &mii_regval); 12471 mdio->val_out = mii_regval; 12472 return rc; 12473 } 12474 12475 case SIOCSMIIREG: 12476 if (!netif_running(dev)) 12477 return -EAGAIN; 12478 12479 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num, 12480 mdio->val_in); 12481 12482 case SIOCSHWTSTAMP: 12483 return bnxt_hwtstamp_set(dev, ifr); 12484 12485 case SIOCGHWTSTAMP: 12486 return bnxt_hwtstamp_get(dev, ifr); 12487 12488 default: 12489 /* do nothing */ 12490 break; 12491 } 12492 return -EOPNOTSUPP; 12493 } 12494 12495 static void bnxt_get_ring_stats(struct bnxt *bp, 12496 struct rtnl_link_stats64 *stats) 12497 { 12498 int i; 12499 12500 for (i = 0; i < bp->cp_nr_rings; i++) { 12501 struct bnxt_napi *bnapi = bp->bnapi[i]; 12502 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 12503 u64 *sw = cpr->stats.sw_stats; 12504 12505 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 12506 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12507 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 12508 12509 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 12510 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 12511 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 12512 12513 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 12514 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 12515 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 12516 12517 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 12518 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 12519 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 12520 12521 stats->rx_missed_errors += 12522 BNXT_GET_RING_STATS64(sw, rx_discard_pkts); 12523 12524 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 12525 12526 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts); 12527 12528 stats->rx_dropped += 12529 cpr->sw_stats->rx.rx_netpoll_discards + 12530 cpr->sw_stats->rx.rx_oom_discards; 12531 } 12532 } 12533 12534 static void bnxt_add_prev_stats(struct bnxt *bp, 12535 struct rtnl_link_stats64 *stats) 12536 { 12537 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev; 12538 12539 stats->rx_packets += prev_stats->rx_packets; 12540 stats->tx_packets += prev_stats->tx_packets; 12541 stats->rx_bytes += prev_stats->rx_bytes; 12542 stats->tx_bytes += prev_stats->tx_bytes; 12543 stats->rx_missed_errors += prev_stats->rx_missed_errors; 12544 stats->multicast += prev_stats->multicast; 12545 stats->rx_dropped += prev_stats->rx_dropped; 12546 stats->tx_dropped += prev_stats->tx_dropped; 12547 } 12548 12549 static void 12550 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 12551 { 12552 struct bnxt *bp = netdev_priv(dev); 12553 12554 set_bit(BNXT_STATE_READ_STATS, &bp->state); 12555 /* Make sure bnxt_close_nic() sees that we are reading stats before 12556 * we check the BNXT_STATE_OPEN flag. 12557 */ 12558 smp_mb__after_atomic(); 12559 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 12560 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12561 *stats = bp->net_stats_prev; 12562 return; 12563 } 12564 12565 bnxt_get_ring_stats(bp, stats); 12566 bnxt_add_prev_stats(bp, stats); 12567 12568 if (bp->flags & BNXT_FLAG_PORT_STATS) { 12569 u64 *rx = bp->port_stats.sw_stats; 12570 u64 *tx = bp->port_stats.sw_stats + 12571 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8; 12572 12573 stats->rx_crc_errors = 12574 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames); 12575 stats->rx_frame_errors = 12576 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames); 12577 stats->rx_length_errors = 12578 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) + 12579 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) + 12580 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames); 12581 stats->rx_errors = 12582 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) + 12583 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames); 12584 stats->collisions = 12585 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions); 12586 stats->tx_fifo_errors = 12587 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns); 12588 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err); 12589 } 12590 clear_bit(BNXT_STATE_READ_STATS, &bp->state); 12591 } 12592 12593 static void bnxt_get_one_ring_err_stats(struct bnxt *bp, 12594 struct bnxt_total_ring_err_stats *stats, 12595 struct bnxt_cp_ring_info *cpr) 12596 { 12597 struct bnxt_sw_stats *sw_stats = cpr->sw_stats; 12598 u64 *hw_stats = cpr->stats.sw_stats; 12599 12600 stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors; 12601 stats->rx_total_resets += sw_stats->rx.rx_resets; 12602 stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors; 12603 stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards; 12604 stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards; 12605 stats->rx_total_ring_discards += 12606 BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts); 12607 stats->tx_total_resets += sw_stats->tx.tx_resets; 12608 stats->tx_total_ring_discards += 12609 BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts); 12610 stats->total_missed_irqs += sw_stats->cmn.missed_irqs; 12611 } 12612 12613 void bnxt_get_ring_err_stats(struct bnxt *bp, 12614 struct bnxt_total_ring_err_stats *stats) 12615 { 12616 int i; 12617 12618 for (i = 0; i < bp->cp_nr_rings; i++) 12619 bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring); 12620 } 12621 12622 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask) 12623 { 12624 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12625 struct net_device *dev = bp->dev; 12626 struct netdev_hw_addr *ha; 12627 u8 *haddr; 12628 int mc_count = 0; 12629 bool update = false; 12630 int off = 0; 12631 12632 netdev_for_each_mc_addr(ha, dev) { 12633 if (mc_count >= BNXT_MAX_MC_ADDRS) { 12634 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12635 vnic->mc_list_count = 0; 12636 return false; 12637 } 12638 haddr = ha->addr; 12639 if (!ether_addr_equal(haddr, vnic->mc_list + off)) { 12640 memcpy(vnic->mc_list + off, haddr, ETH_ALEN); 12641 update = true; 12642 } 12643 off += ETH_ALEN; 12644 mc_count++; 12645 } 12646 if (mc_count) 12647 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 12648 12649 if (mc_count != vnic->mc_list_count) { 12650 vnic->mc_list_count = mc_count; 12651 update = true; 12652 } 12653 return update; 12654 } 12655 12656 static bool bnxt_uc_list_updated(struct bnxt *bp) 12657 { 12658 struct net_device *dev = bp->dev; 12659 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12660 struct netdev_hw_addr *ha; 12661 int off = 0; 12662 12663 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1)) 12664 return true; 12665 12666 netdev_for_each_uc_addr(ha, dev) { 12667 if (!ether_addr_equal(ha->addr, vnic->uc_list + off)) 12668 return true; 12669 12670 off += ETH_ALEN; 12671 } 12672 return false; 12673 } 12674 12675 static void bnxt_set_rx_mode(struct net_device *dev) 12676 { 12677 struct bnxt *bp = netdev_priv(dev); 12678 struct bnxt_vnic_info *vnic; 12679 bool mc_update = false; 12680 bool uc_update; 12681 u32 mask; 12682 12683 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) 12684 return; 12685 12686 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12687 mask = vnic->rx_mask; 12688 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS | 12689 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST | 12690 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST | 12691 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST); 12692 12693 if (dev->flags & IFF_PROMISC) 12694 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12695 12696 uc_update = bnxt_uc_list_updated(bp); 12697 12698 if (dev->flags & IFF_BROADCAST) 12699 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST; 12700 if (dev->flags & IFF_ALLMULTI) { 12701 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12702 vnic->mc_list_count = 0; 12703 } else if (dev->flags & IFF_MULTICAST) { 12704 mc_update = bnxt_mc_list_updated(bp, &mask); 12705 } 12706 12707 if (mask != vnic->rx_mask || uc_update || mc_update) { 12708 vnic->rx_mask = mask; 12709 12710 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 12711 } 12712 } 12713 12714 static int bnxt_cfg_rx_mode(struct bnxt *bp) 12715 { 12716 struct net_device *dev = bp->dev; 12717 struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 12718 struct netdev_hw_addr *ha; 12719 int i, off = 0, rc; 12720 bool uc_update; 12721 12722 netif_addr_lock_bh(dev); 12723 uc_update = bnxt_uc_list_updated(bp); 12724 netif_addr_unlock_bh(dev); 12725 12726 if (!uc_update) 12727 goto skip_uc; 12728 12729 for (i = 1; i < vnic->uc_filter_count; i++) { 12730 struct bnxt_l2_filter *fltr = vnic->l2_filters[i]; 12731 12732 bnxt_hwrm_l2_filter_free(bp, fltr); 12733 bnxt_del_l2_filter(bp, fltr); 12734 } 12735 12736 vnic->uc_filter_count = 1; 12737 12738 netif_addr_lock_bh(dev); 12739 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) { 12740 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12741 } else { 12742 netdev_for_each_uc_addr(ha, dev) { 12743 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN); 12744 off += ETH_ALEN; 12745 vnic->uc_filter_count++; 12746 } 12747 } 12748 netif_addr_unlock_bh(dev); 12749 12750 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) { 12751 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off); 12752 if (rc) { 12753 if (BNXT_VF(bp) && rc == -ENODEV) { 12754 if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 12755 netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n"); 12756 else 12757 netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n"); 12758 rc = 0; 12759 } else { 12760 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc); 12761 } 12762 vnic->uc_filter_count = i; 12763 return rc; 12764 } 12765 } 12766 if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 12767 netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n"); 12768 12769 skip_uc: 12770 if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) && 12771 !bnxt_promisc_ok(bp)) 12772 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS; 12773 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 12774 if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) { 12775 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n", 12776 rc); 12777 vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST; 12778 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST; 12779 vnic->mc_list_count = 0; 12780 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0); 12781 } 12782 if (rc) 12783 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n", 12784 rc); 12785 12786 return rc; 12787 } 12788 12789 static bool bnxt_can_reserve_rings(struct bnxt *bp) 12790 { 12791 #ifdef CONFIG_BNXT_SRIOV 12792 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) { 12793 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 12794 12795 /* No minimum rings were provisioned by the PF. Don't 12796 * reserve rings by default when device is down. 12797 */ 12798 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings) 12799 return true; 12800 12801 if (!netif_running(bp->dev)) 12802 return false; 12803 } 12804 #endif 12805 return true; 12806 } 12807 12808 /* If the chip and firmware supports RFS */ 12809 static bool bnxt_rfs_supported(struct bnxt *bp) 12810 { 12811 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 12812 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) 12813 return true; 12814 return false; 12815 } 12816 /* 212 firmware is broken for aRFS */ 12817 if (BNXT_FW_MAJ(bp) == 212) 12818 return false; 12819 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) 12820 return true; 12821 if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) 12822 return true; 12823 return false; 12824 } 12825 12826 /* If runtime conditions support RFS */ 12827 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx) 12828 { 12829 struct bnxt_hw_rings hwr = {0}; 12830 int max_vnics, max_rss_ctxs; 12831 12832 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 12833 !BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 12834 return bnxt_rfs_supported(bp); 12835 12836 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings) 12837 return false; 12838 12839 hwr.grp = bp->rx_nr_rings; 12840 hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings); 12841 if (new_rss_ctx) 12842 hwr.vnic++; 12843 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 12844 max_vnics = bnxt_get_max_func_vnics(bp); 12845 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp); 12846 12847 if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) { 12848 if (bp->rx_nr_rings > 1) 12849 netdev_warn(bp->dev, 12850 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n", 12851 min(max_rss_ctxs - 1, max_vnics - 1)); 12852 return false; 12853 } 12854 12855 if (!BNXT_NEW_RM(bp)) 12856 return true; 12857 12858 /* Do not reduce VNIC and RSS ctx reservations. There is a FW 12859 * issue that will mess up the default VNIC if we reduce the 12860 * reservations. 12861 */ 12862 if (hwr.vnic <= bp->hw_resc.resv_vnics && 12863 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 12864 return true; 12865 12866 bnxt_hwrm_reserve_rings(bp, &hwr); 12867 if (hwr.vnic <= bp->hw_resc.resv_vnics && 12868 hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs) 12869 return true; 12870 12871 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n"); 12872 hwr.vnic = 1; 12873 hwr.rss_ctx = 0; 12874 bnxt_hwrm_reserve_rings(bp, &hwr); 12875 return false; 12876 } 12877 12878 static netdev_features_t bnxt_fix_features(struct net_device *dev, 12879 netdev_features_t features) 12880 { 12881 struct bnxt *bp = netdev_priv(dev); 12882 netdev_features_t vlan_features; 12883 12884 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false)) 12885 features &= ~NETIF_F_NTUPLE; 12886 12887 if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog) 12888 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 12889 12890 if (!(features & NETIF_F_GRO)) 12891 features &= ~NETIF_F_GRO_HW; 12892 12893 if (features & NETIF_F_GRO_HW) 12894 features &= ~NETIF_F_LRO; 12895 12896 /* Both CTAG and STAG VLAN accelaration on the RX side have to be 12897 * turned on or off together. 12898 */ 12899 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX; 12900 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) { 12901 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) 12902 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 12903 else if (vlan_features) 12904 features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 12905 } 12906 #ifdef CONFIG_BNXT_SRIOV 12907 if (BNXT_VF(bp) && bp->vf.vlan) 12908 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX; 12909 #endif 12910 return features; 12911 } 12912 12913 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init, 12914 bool link_re_init, u32 flags, bool update_tpa) 12915 { 12916 bnxt_close_nic(bp, irq_re_init, link_re_init); 12917 bp->flags = flags; 12918 if (update_tpa) 12919 bnxt_set_ring_params(bp); 12920 return bnxt_open_nic(bp, irq_re_init, link_re_init); 12921 } 12922 12923 static int bnxt_set_features(struct net_device *dev, netdev_features_t features) 12924 { 12925 bool update_tpa = false, update_ntuple = false; 12926 struct bnxt *bp = netdev_priv(dev); 12927 u32 flags = bp->flags; 12928 u32 changes; 12929 int rc = 0; 12930 bool re_init = false; 12931 12932 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS; 12933 if (features & NETIF_F_GRO_HW) 12934 flags |= BNXT_FLAG_GRO; 12935 else if (features & NETIF_F_LRO) 12936 flags |= BNXT_FLAG_LRO; 12937 12938 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS) 12939 flags &= ~BNXT_FLAG_TPA; 12940 12941 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX) 12942 flags |= BNXT_FLAG_STRIP_VLAN; 12943 12944 if (features & NETIF_F_NTUPLE) 12945 flags |= BNXT_FLAG_RFS; 12946 else 12947 bnxt_clear_usr_fltrs(bp, true); 12948 12949 changes = flags ^ bp->flags; 12950 if (changes & BNXT_FLAG_TPA) { 12951 update_tpa = true; 12952 if ((bp->flags & BNXT_FLAG_TPA) == 0 || 12953 (flags & BNXT_FLAG_TPA) == 0 || 12954 (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 12955 re_init = true; 12956 } 12957 12958 if (changes & ~BNXT_FLAG_TPA) 12959 re_init = true; 12960 12961 if (changes & BNXT_FLAG_RFS) 12962 update_ntuple = true; 12963 12964 if (flags != bp->flags) { 12965 u32 old_flags = bp->flags; 12966 12967 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 12968 bp->flags = flags; 12969 if (update_tpa) 12970 bnxt_set_ring_params(bp); 12971 return rc; 12972 } 12973 12974 if (update_ntuple) 12975 return bnxt_reinit_features(bp, true, false, flags, update_tpa); 12976 12977 if (re_init) 12978 return bnxt_reinit_features(bp, false, false, flags, update_tpa); 12979 12980 if (update_tpa) { 12981 bp->flags = flags; 12982 rc = bnxt_set_tpa(bp, 12983 (flags & BNXT_FLAG_TPA) ? 12984 true : false); 12985 if (rc) 12986 bp->flags = old_flags; 12987 } 12988 } 12989 return rc; 12990 } 12991 12992 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off, 12993 u8 **nextp) 12994 { 12995 struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off); 12996 struct hop_jumbo_hdr *jhdr; 12997 int hdr_count = 0; 12998 u8 *nexthdr; 12999 int start; 13000 13001 /* Check that there are at most 2 IPv6 extension headers, no 13002 * fragment header, and each is <= 64 bytes. 13003 */ 13004 start = nw_off + sizeof(*ip6h); 13005 nexthdr = &ip6h->nexthdr; 13006 while (ipv6_ext_hdr(*nexthdr)) { 13007 struct ipv6_opt_hdr *hp; 13008 int hdrlen; 13009 13010 if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE || 13011 *nexthdr == NEXTHDR_FRAGMENT) 13012 return false; 13013 hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data, 13014 skb_headlen(skb), NULL); 13015 if (!hp) 13016 return false; 13017 if (*nexthdr == NEXTHDR_AUTH) 13018 hdrlen = ipv6_authlen(hp); 13019 else 13020 hdrlen = ipv6_optlen(hp); 13021 13022 if (hdrlen > 64) 13023 return false; 13024 13025 /* The ext header may be a hop-by-hop header inserted for 13026 * big TCP purposes. This will be removed before sending 13027 * from NIC, so do not count it. 13028 */ 13029 if (*nexthdr == NEXTHDR_HOP) { 13030 if (likely(skb->len <= GRO_LEGACY_MAX_SIZE)) 13031 goto increment_hdr; 13032 13033 jhdr = (struct hop_jumbo_hdr *)hp; 13034 if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 || 13035 jhdr->nexthdr != IPPROTO_TCP) 13036 goto increment_hdr; 13037 13038 goto next_hdr; 13039 } 13040 increment_hdr: 13041 hdr_count++; 13042 next_hdr: 13043 nexthdr = &hp->nexthdr; 13044 start += hdrlen; 13045 } 13046 if (nextp) { 13047 /* Caller will check inner protocol */ 13048 if (skb->encapsulation) { 13049 *nextp = nexthdr; 13050 return true; 13051 } 13052 *nextp = NULL; 13053 } 13054 /* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */ 13055 return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP; 13056 } 13057 13058 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */ 13059 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb) 13060 { 13061 struct udphdr *uh = udp_hdr(skb); 13062 __be16 udp_port = uh->dest; 13063 13064 if (udp_port != bp->vxlan_port && udp_port != bp->nge_port && 13065 udp_port != bp->vxlan_gpe_port) 13066 return false; 13067 if (skb->inner_protocol == htons(ETH_P_TEB)) { 13068 struct ethhdr *eh = inner_eth_hdr(skb); 13069 13070 switch (eh->h_proto) { 13071 case htons(ETH_P_IP): 13072 return true; 13073 case htons(ETH_P_IPV6): 13074 return bnxt_exthdr_check(bp, skb, 13075 skb_inner_network_offset(skb), 13076 NULL); 13077 } 13078 } else if (skb->inner_protocol == htons(ETH_P_IP)) { 13079 return true; 13080 } else if (skb->inner_protocol == htons(ETH_P_IPV6)) { 13081 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13082 NULL); 13083 } 13084 return false; 13085 } 13086 13087 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto) 13088 { 13089 switch (l4_proto) { 13090 case IPPROTO_UDP: 13091 return bnxt_udp_tunl_check(bp, skb); 13092 case IPPROTO_IPIP: 13093 return true; 13094 case IPPROTO_GRE: { 13095 switch (skb->inner_protocol) { 13096 default: 13097 return false; 13098 case htons(ETH_P_IP): 13099 return true; 13100 case htons(ETH_P_IPV6): 13101 fallthrough; 13102 } 13103 } 13104 case IPPROTO_IPV6: 13105 /* Check ext headers of inner ipv6 */ 13106 return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb), 13107 NULL); 13108 } 13109 return false; 13110 } 13111 13112 static netdev_features_t bnxt_features_check(struct sk_buff *skb, 13113 struct net_device *dev, 13114 netdev_features_t features) 13115 { 13116 struct bnxt *bp = netdev_priv(dev); 13117 u8 *l4_proto; 13118 13119 features = vlan_features_check(skb, features); 13120 switch (vlan_get_protocol(skb)) { 13121 case htons(ETH_P_IP): 13122 if (!skb->encapsulation) 13123 return features; 13124 l4_proto = &ip_hdr(skb)->protocol; 13125 if (bnxt_tunl_check(bp, skb, *l4_proto)) 13126 return features; 13127 break; 13128 case htons(ETH_P_IPV6): 13129 if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb), 13130 &l4_proto)) 13131 break; 13132 if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto)) 13133 return features; 13134 break; 13135 } 13136 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 13137 } 13138 13139 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words, 13140 u32 *reg_buf) 13141 { 13142 struct hwrm_dbg_read_direct_output *resp; 13143 struct hwrm_dbg_read_direct_input *req; 13144 __le32 *dbg_reg_buf; 13145 dma_addr_t mapping; 13146 int rc, i; 13147 13148 rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT); 13149 if (rc) 13150 return rc; 13151 13152 dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4, 13153 &mapping); 13154 if (!dbg_reg_buf) { 13155 rc = -ENOMEM; 13156 goto dbg_rd_reg_exit; 13157 } 13158 13159 req->host_dest_addr = cpu_to_le64(mapping); 13160 13161 resp = hwrm_req_hold(bp, req); 13162 req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR); 13163 req->read_len32 = cpu_to_le32(num_words); 13164 13165 rc = hwrm_req_send(bp, req); 13166 if (rc || resp->error_code) { 13167 rc = -EIO; 13168 goto dbg_rd_reg_exit; 13169 } 13170 for (i = 0; i < num_words; i++) 13171 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]); 13172 13173 dbg_rd_reg_exit: 13174 hwrm_req_drop(bp, req); 13175 return rc; 13176 } 13177 13178 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type, 13179 u32 ring_id, u32 *prod, u32 *cons) 13180 { 13181 struct hwrm_dbg_ring_info_get_output *resp; 13182 struct hwrm_dbg_ring_info_get_input *req; 13183 int rc; 13184 13185 rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET); 13186 if (rc) 13187 return rc; 13188 13189 req->ring_type = ring_type; 13190 req->fw_ring_id = cpu_to_le32(ring_id); 13191 resp = hwrm_req_hold(bp, req); 13192 rc = hwrm_req_send(bp, req); 13193 if (!rc) { 13194 *prod = le32_to_cpu(resp->producer_index); 13195 *cons = le32_to_cpu(resp->consumer_index); 13196 } 13197 hwrm_req_drop(bp, req); 13198 return rc; 13199 } 13200 13201 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi) 13202 { 13203 struct bnxt_tx_ring_info *txr; 13204 int i = bnapi->index, j; 13205 13206 bnxt_for_each_napi_tx(j, bnapi, txr) 13207 netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n", 13208 i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod, 13209 txr->tx_cons); 13210 } 13211 13212 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi) 13213 { 13214 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring; 13215 int i = bnapi->index; 13216 13217 if (!rxr) 13218 return; 13219 13220 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", 13221 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod, 13222 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod, 13223 rxr->rx_sw_agg_prod); 13224 } 13225 13226 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi) 13227 { 13228 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring; 13229 int i = bnapi->index; 13230 13231 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n", 13232 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons); 13233 } 13234 13235 static void bnxt_dbg_dump_states(struct bnxt *bp) 13236 { 13237 int i; 13238 struct bnxt_napi *bnapi; 13239 13240 for (i = 0; i < bp->cp_nr_rings; i++) { 13241 bnapi = bp->bnapi[i]; 13242 if (netif_msg_drv(bp)) { 13243 bnxt_dump_tx_sw_state(bnapi); 13244 bnxt_dump_rx_sw_state(bnapi); 13245 bnxt_dump_cp_sw_state(bnapi); 13246 } 13247 } 13248 } 13249 13250 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr) 13251 { 13252 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr]; 13253 struct hwrm_ring_reset_input *req; 13254 struct bnxt_napi *bnapi = rxr->bnapi; 13255 struct bnxt_cp_ring_info *cpr; 13256 u16 cp_ring_id; 13257 int rc; 13258 13259 rc = hwrm_req_init(bp, req, HWRM_RING_RESET); 13260 if (rc) 13261 return rc; 13262 13263 cpr = &bnapi->cp_ring; 13264 cp_ring_id = cpr->cp_ring_struct.fw_ring_id; 13265 req->cmpl_ring = cpu_to_le16(cp_ring_id); 13266 req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP; 13267 req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id); 13268 return hwrm_req_send_silent(bp, req); 13269 } 13270 13271 static void bnxt_reset_task(struct bnxt *bp, bool silent) 13272 { 13273 if (!silent) 13274 bnxt_dbg_dump_states(bp); 13275 if (netif_running(bp->dev)) { 13276 bnxt_close_nic(bp, !silent, false); 13277 bnxt_open_nic(bp, !silent, false); 13278 } 13279 } 13280 13281 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue) 13282 { 13283 struct bnxt *bp = netdev_priv(dev); 13284 13285 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n"); 13286 bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT); 13287 } 13288 13289 static void bnxt_fw_health_check(struct bnxt *bp) 13290 { 13291 struct bnxt_fw_health *fw_health = bp->fw_health; 13292 struct pci_dev *pdev = bp->pdev; 13293 u32 val; 13294 13295 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13296 return; 13297 13298 /* Make sure it is enabled before checking the tmr_counter. */ 13299 smp_rmb(); 13300 if (fw_health->tmr_counter) { 13301 fw_health->tmr_counter--; 13302 return; 13303 } 13304 13305 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13306 if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) { 13307 fw_health->arrests++; 13308 goto fw_reset; 13309 } 13310 13311 fw_health->last_fw_heartbeat = val; 13312 13313 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13314 if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) { 13315 fw_health->discoveries++; 13316 goto fw_reset; 13317 } 13318 13319 fw_health->tmr_counter = fw_health->tmr_multiplier; 13320 return; 13321 13322 fw_reset: 13323 bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT); 13324 } 13325 13326 static void bnxt_timer(struct timer_list *t) 13327 { 13328 struct bnxt *bp = from_timer(bp, t, timer); 13329 struct net_device *dev = bp->dev; 13330 13331 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state)) 13332 return; 13333 13334 if (atomic_read(&bp->intr_sem) != 0) 13335 goto bnxt_restart_timer; 13336 13337 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) 13338 bnxt_fw_health_check(bp); 13339 13340 if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks) 13341 bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT); 13342 13343 if (bnxt_tc_flower_enabled(bp)) 13344 bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT); 13345 13346 #ifdef CONFIG_RFS_ACCEL 13347 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) 13348 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 13349 #endif /*CONFIG_RFS_ACCEL*/ 13350 13351 if (bp->link_info.phy_retry) { 13352 if (time_after(jiffies, bp->link_info.phy_retry_expires)) { 13353 bp->link_info.phy_retry = false; 13354 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n"); 13355 } else { 13356 bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT); 13357 } 13358 } 13359 13360 if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state)) 13361 bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT); 13362 13363 if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev)) 13364 bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT); 13365 13366 bnxt_restart_timer: 13367 mod_timer(&bp->timer, jiffies + bp->current_interval); 13368 } 13369 13370 static void bnxt_rtnl_lock_sp(struct bnxt *bp) 13371 { 13372 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK 13373 * set. If the device is being closed, bnxt_close() may be holding 13374 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we 13375 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl(). 13376 */ 13377 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13378 rtnl_lock(); 13379 } 13380 13381 static void bnxt_rtnl_unlock_sp(struct bnxt *bp) 13382 { 13383 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13384 rtnl_unlock(); 13385 } 13386 13387 /* Only called from bnxt_sp_task() */ 13388 static void bnxt_reset(struct bnxt *bp, bool silent) 13389 { 13390 bnxt_rtnl_lock_sp(bp); 13391 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 13392 bnxt_reset_task(bp, silent); 13393 bnxt_rtnl_unlock_sp(bp); 13394 } 13395 13396 /* Only called from bnxt_sp_task() */ 13397 static void bnxt_rx_ring_reset(struct bnxt *bp) 13398 { 13399 int i; 13400 13401 bnxt_rtnl_lock_sp(bp); 13402 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13403 bnxt_rtnl_unlock_sp(bp); 13404 return; 13405 } 13406 /* Disable and flush TPA before resetting the RX ring */ 13407 if (bp->flags & BNXT_FLAG_TPA) 13408 bnxt_set_tpa(bp, false); 13409 for (i = 0; i < bp->rx_nr_rings; i++) { 13410 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i]; 13411 struct bnxt_cp_ring_info *cpr; 13412 int rc; 13413 13414 if (!rxr->bnapi->in_reset) 13415 continue; 13416 13417 rc = bnxt_hwrm_rx_ring_reset(bp, i); 13418 if (rc) { 13419 if (rc == -EINVAL || rc == -EOPNOTSUPP) 13420 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n"); 13421 else 13422 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n", 13423 rc); 13424 bnxt_reset_task(bp, true); 13425 break; 13426 } 13427 bnxt_free_one_rx_ring_skbs(bp, i); 13428 rxr->rx_prod = 0; 13429 rxr->rx_agg_prod = 0; 13430 rxr->rx_sw_agg_prod = 0; 13431 rxr->rx_next_cons = 0; 13432 rxr->bnapi->in_reset = false; 13433 bnxt_alloc_one_rx_ring(bp, i); 13434 cpr = &rxr->bnapi->cp_ring; 13435 cpr->sw_stats->rx.rx_resets++; 13436 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13437 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 13438 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 13439 } 13440 if (bp->flags & BNXT_FLAG_TPA) 13441 bnxt_set_tpa(bp, true); 13442 bnxt_rtnl_unlock_sp(bp); 13443 } 13444 13445 static void bnxt_fw_fatal_close(struct bnxt *bp) 13446 { 13447 bnxt_tx_disable(bp); 13448 bnxt_disable_napi(bp); 13449 bnxt_disable_int_sync(bp); 13450 bnxt_free_irq(bp); 13451 bnxt_clear_int_mode(bp); 13452 pci_disable_device(bp->pdev); 13453 } 13454 13455 static void bnxt_fw_reset_close(struct bnxt *bp) 13456 { 13457 /* When firmware is in fatal state, quiesce device and disable 13458 * bus master to prevent any potential bad DMAs before freeing 13459 * kernel memory. 13460 */ 13461 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) { 13462 u16 val = 0; 13463 13464 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 13465 if (val == 0xffff) 13466 bp->fw_reset_min_dsecs = 0; 13467 bnxt_fw_fatal_close(bp); 13468 } 13469 __bnxt_close_nic(bp, true, false); 13470 bnxt_vf_reps_free(bp); 13471 bnxt_clear_int_mode(bp); 13472 bnxt_hwrm_func_drv_unrgtr(bp); 13473 if (pci_is_enabled(bp->pdev)) 13474 pci_disable_device(bp->pdev); 13475 bnxt_free_ctx_mem(bp); 13476 } 13477 13478 static bool is_bnxt_fw_ok(struct bnxt *bp) 13479 { 13480 struct bnxt_fw_health *fw_health = bp->fw_health; 13481 bool no_heartbeat = false, has_reset = false; 13482 u32 val; 13483 13484 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG); 13485 if (val == fw_health->last_fw_heartbeat) 13486 no_heartbeat = true; 13487 13488 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 13489 if (val != fw_health->last_fw_reset_cnt) 13490 has_reset = true; 13491 13492 if (!no_heartbeat && has_reset) 13493 return true; 13494 13495 return false; 13496 } 13497 13498 /* rtnl_lock is acquired before calling this function */ 13499 static void bnxt_force_fw_reset(struct bnxt *bp) 13500 { 13501 struct bnxt_fw_health *fw_health = bp->fw_health; 13502 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13503 u32 wait_dsecs; 13504 13505 if (!test_bit(BNXT_STATE_OPEN, &bp->state) || 13506 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 13507 return; 13508 13509 if (ptp) { 13510 spin_lock_bh(&ptp->ptp_lock); 13511 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13512 spin_unlock_bh(&ptp->ptp_lock); 13513 } else { 13514 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13515 } 13516 bnxt_fw_reset_close(bp); 13517 wait_dsecs = fw_health->master_func_wait_dsecs; 13518 if (fw_health->primary) { 13519 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) 13520 wait_dsecs = 0; 13521 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 13522 } else { 13523 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10; 13524 wait_dsecs = fw_health->normal_func_wait_dsecs; 13525 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13526 } 13527 13528 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs; 13529 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs; 13530 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 13531 } 13532 13533 void bnxt_fw_exception(struct bnxt *bp) 13534 { 13535 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n"); 13536 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 13537 bnxt_ulp_stop(bp); 13538 bnxt_rtnl_lock_sp(bp); 13539 bnxt_force_fw_reset(bp); 13540 bnxt_rtnl_unlock_sp(bp); 13541 } 13542 13543 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or 13544 * < 0 on error. 13545 */ 13546 static int bnxt_get_registered_vfs(struct bnxt *bp) 13547 { 13548 #ifdef CONFIG_BNXT_SRIOV 13549 int rc; 13550 13551 if (!BNXT_PF(bp)) 13552 return 0; 13553 13554 rc = bnxt_hwrm_func_qcfg(bp); 13555 if (rc) { 13556 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc); 13557 return rc; 13558 } 13559 if (bp->pf.registered_vfs) 13560 return bp->pf.registered_vfs; 13561 if (bp->sriov_cfg) 13562 return 1; 13563 #endif 13564 return 0; 13565 } 13566 13567 void bnxt_fw_reset(struct bnxt *bp) 13568 { 13569 bnxt_ulp_stop(bp); 13570 bnxt_rtnl_lock_sp(bp); 13571 if (test_bit(BNXT_STATE_OPEN, &bp->state) && 13572 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 13573 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 13574 int n = 0, tmo; 13575 13576 if (ptp) { 13577 spin_lock_bh(&ptp->ptp_lock); 13578 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13579 spin_unlock_bh(&ptp->ptp_lock); 13580 } else { 13581 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13582 } 13583 if (bp->pf.active_vfs && 13584 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) 13585 n = bnxt_get_registered_vfs(bp); 13586 if (n < 0) { 13587 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n", 13588 n); 13589 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 13590 dev_close(bp->dev); 13591 goto fw_reset_exit; 13592 } else if (n > 0) { 13593 u16 vf_tmo_dsecs = n * 10; 13594 13595 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs) 13596 bp->fw_reset_max_dsecs = vf_tmo_dsecs; 13597 bp->fw_reset_state = 13598 BNXT_FW_RESET_STATE_POLL_VF; 13599 bnxt_queue_fw_reset_work(bp, HZ / 10); 13600 goto fw_reset_exit; 13601 } 13602 bnxt_fw_reset_close(bp); 13603 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 13604 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 13605 tmo = HZ / 10; 13606 } else { 13607 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 13608 tmo = bp->fw_reset_min_dsecs * HZ / 10; 13609 } 13610 bnxt_queue_fw_reset_work(bp, tmo); 13611 } 13612 fw_reset_exit: 13613 bnxt_rtnl_unlock_sp(bp); 13614 } 13615 13616 static void bnxt_chk_missed_irq(struct bnxt *bp) 13617 { 13618 int i; 13619 13620 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 13621 return; 13622 13623 for (i = 0; i < bp->cp_nr_rings; i++) { 13624 struct bnxt_napi *bnapi = bp->bnapi[i]; 13625 struct bnxt_cp_ring_info *cpr; 13626 u32 fw_ring_id; 13627 int j; 13628 13629 if (!bnapi) 13630 continue; 13631 13632 cpr = &bnapi->cp_ring; 13633 for (j = 0; j < cpr->cp_ring_count; j++) { 13634 struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j]; 13635 u32 val[2]; 13636 13637 if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2)) 13638 continue; 13639 13640 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) { 13641 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons; 13642 continue; 13643 } 13644 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id; 13645 bnxt_dbg_hwrm_ring_info_get(bp, 13646 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL, 13647 fw_ring_id, &val[0], &val[1]); 13648 cpr->sw_stats->cmn.missed_irqs++; 13649 } 13650 } 13651 } 13652 13653 static void bnxt_cfg_ntp_filters(struct bnxt *); 13654 13655 static void bnxt_init_ethtool_link_settings(struct bnxt *bp) 13656 { 13657 struct bnxt_link_info *link_info = &bp->link_info; 13658 13659 if (BNXT_AUTO_MODE(link_info->auto_mode)) { 13660 link_info->autoneg = BNXT_AUTONEG_SPEED; 13661 if (bp->hwrm_spec_code >= 0x10201) { 13662 if (link_info->auto_pause_setting & 13663 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE) 13664 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 13665 } else { 13666 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL; 13667 } 13668 bnxt_set_auto_speed(link_info); 13669 } else { 13670 bnxt_set_force_speed(link_info); 13671 link_info->req_duplex = link_info->duplex_setting; 13672 } 13673 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) 13674 link_info->req_flow_ctrl = 13675 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH; 13676 else 13677 link_info->req_flow_ctrl = link_info->force_pause_setting; 13678 } 13679 13680 static void bnxt_fw_echo_reply(struct bnxt *bp) 13681 { 13682 struct bnxt_fw_health *fw_health = bp->fw_health; 13683 struct hwrm_func_echo_response_input *req; 13684 int rc; 13685 13686 rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE); 13687 if (rc) 13688 return; 13689 req->event_data1 = cpu_to_le32(fw_health->echo_req_data1); 13690 req->event_data2 = cpu_to_le32(fw_health->echo_req_data2); 13691 hwrm_req_send(bp, req); 13692 } 13693 13694 static void bnxt_ulp_restart(struct bnxt *bp) 13695 { 13696 bnxt_ulp_stop(bp); 13697 bnxt_ulp_start(bp, 0); 13698 } 13699 13700 static void bnxt_sp_task(struct work_struct *work) 13701 { 13702 struct bnxt *bp = container_of(work, struct bnxt, sp_task); 13703 13704 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13705 smp_mb__after_atomic(); 13706 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) { 13707 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13708 return; 13709 } 13710 13711 if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) { 13712 bnxt_ulp_restart(bp); 13713 bnxt_reenable_sriov(bp); 13714 } 13715 13716 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event)) 13717 bnxt_cfg_rx_mode(bp); 13718 13719 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event)) 13720 bnxt_cfg_ntp_filters(bp); 13721 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event)) 13722 bnxt_hwrm_exec_fwd_req(bp); 13723 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event)) 13724 netdev_info(bp->dev, "Receive PF driver unload event!\n"); 13725 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) { 13726 bnxt_hwrm_port_qstats(bp, 0); 13727 bnxt_hwrm_port_qstats_ext(bp, 0); 13728 bnxt_accumulate_all_stats(bp); 13729 } 13730 13731 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) { 13732 int rc; 13733 13734 mutex_lock(&bp->link_lock); 13735 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, 13736 &bp->sp_event)) 13737 bnxt_hwrm_phy_qcaps(bp); 13738 13739 rc = bnxt_update_link(bp, true); 13740 if (rc) 13741 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n", 13742 rc); 13743 13744 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, 13745 &bp->sp_event)) 13746 bnxt_init_ethtool_link_settings(bp); 13747 mutex_unlock(&bp->link_lock); 13748 } 13749 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) { 13750 int rc; 13751 13752 mutex_lock(&bp->link_lock); 13753 rc = bnxt_update_phy_setting(bp); 13754 mutex_unlock(&bp->link_lock); 13755 if (rc) { 13756 netdev_warn(bp->dev, "update phy settings retry failed\n"); 13757 } else { 13758 bp->link_info.phy_retry = false; 13759 netdev_info(bp->dev, "update phy settings retry succeeded\n"); 13760 } 13761 } 13762 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) { 13763 mutex_lock(&bp->link_lock); 13764 bnxt_get_port_module_status(bp); 13765 mutex_unlock(&bp->link_lock); 13766 } 13767 13768 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event)) 13769 bnxt_tc_flow_stats_work(bp); 13770 13771 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event)) 13772 bnxt_chk_missed_irq(bp); 13773 13774 if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event)) 13775 bnxt_fw_echo_reply(bp); 13776 13777 if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event)) 13778 bnxt_hwmon_notify_event(bp); 13779 13780 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They 13781 * must be the last functions to be called before exiting. 13782 */ 13783 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) 13784 bnxt_reset(bp, false); 13785 13786 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event)) 13787 bnxt_reset(bp, true); 13788 13789 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event)) 13790 bnxt_rx_ring_reset(bp); 13791 13792 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) { 13793 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) || 13794 test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state)) 13795 bnxt_devlink_health_fw_report(bp); 13796 else 13797 bnxt_fw_reset(bp); 13798 } 13799 13800 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) { 13801 if (!is_bnxt_fw_ok(bp)) 13802 bnxt_devlink_health_fw_report(bp); 13803 } 13804 13805 smp_mb__before_atomic(); 13806 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state); 13807 } 13808 13809 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 13810 int *max_cp); 13811 13812 /* Under rtnl_lock */ 13813 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs, 13814 int tx_xdp) 13815 { 13816 int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp; 13817 struct bnxt_hw_rings hwr = {0}; 13818 int rx_rings = rx; 13819 13820 if (tcs) 13821 tx_sets = tcs; 13822 13823 _bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp); 13824 13825 if (max_rx < rx_rings) 13826 return -ENOMEM; 13827 13828 if (bp->flags & BNXT_FLAG_AGG_RINGS) 13829 rx_rings <<= 1; 13830 13831 hwr.rx = rx_rings; 13832 hwr.tx = tx * tx_sets + tx_xdp; 13833 if (max_tx < hwr.tx) 13834 return -ENOMEM; 13835 13836 hwr.vnic = bnxt_get_total_vnics(bp, rx); 13837 13838 tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp); 13839 hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx; 13840 if (max_cp < hwr.cp) 13841 return -ENOMEM; 13842 hwr.stat = hwr.cp; 13843 if (BNXT_NEW_RM(bp)) { 13844 hwr.cp += bnxt_get_ulp_msix_num_in_use(bp); 13845 hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp); 13846 hwr.grp = rx; 13847 hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr); 13848 } 13849 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 13850 hwr.cp_p5 = hwr.tx + rx; 13851 return bnxt_hwrm_check_rings(bp, &hwr); 13852 } 13853 13854 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev) 13855 { 13856 if (bp->bar2) { 13857 pci_iounmap(pdev, bp->bar2); 13858 bp->bar2 = NULL; 13859 } 13860 13861 if (bp->bar1) { 13862 pci_iounmap(pdev, bp->bar1); 13863 bp->bar1 = NULL; 13864 } 13865 13866 if (bp->bar0) { 13867 pci_iounmap(pdev, bp->bar0); 13868 bp->bar0 = NULL; 13869 } 13870 } 13871 13872 static void bnxt_cleanup_pci(struct bnxt *bp) 13873 { 13874 bnxt_unmap_bars(bp, bp->pdev); 13875 pci_release_regions(bp->pdev); 13876 if (pci_is_enabled(bp->pdev)) 13877 pci_disable_device(bp->pdev); 13878 } 13879 13880 static void bnxt_init_dflt_coal(struct bnxt *bp) 13881 { 13882 struct bnxt_coal_cap *coal_cap = &bp->coal_cap; 13883 struct bnxt_coal *coal; 13884 u16 flags = 0; 13885 13886 if (coal_cap->cmpl_params & 13887 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET) 13888 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET; 13889 13890 /* Tick values in micro seconds. 13891 * 1 coal_buf x bufs_per_record = 1 completion record. 13892 */ 13893 coal = &bp->rx_coal; 13894 coal->coal_ticks = 10; 13895 coal->coal_bufs = 30; 13896 coal->coal_ticks_irq = 1; 13897 coal->coal_bufs_irq = 2; 13898 coal->idle_thresh = 50; 13899 coal->bufs_per_record = 2; 13900 coal->budget = 64; /* NAPI budget */ 13901 coal->flags = flags; 13902 13903 coal = &bp->tx_coal; 13904 coal->coal_ticks = 28; 13905 coal->coal_bufs = 30; 13906 coal->coal_ticks_irq = 2; 13907 coal->coal_bufs_irq = 2; 13908 coal->bufs_per_record = 1; 13909 coal->flags = flags; 13910 13911 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS; 13912 } 13913 13914 /* FW that pre-reserves 1 VNIC per function */ 13915 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp) 13916 { 13917 u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp); 13918 13919 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13920 (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18))) 13921 return true; 13922 if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 13923 (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172))) 13924 return true; 13925 return false; 13926 } 13927 13928 static int bnxt_fw_init_one_p1(struct bnxt *bp) 13929 { 13930 int rc; 13931 13932 bp->fw_cap = 0; 13933 rc = bnxt_hwrm_ver_get(bp); 13934 /* FW may be unresponsive after FLR. FLR must complete within 100 msec 13935 * so wait before continuing with recovery. 13936 */ 13937 if (rc) 13938 msleep(100); 13939 bnxt_try_map_fw_health_reg(bp); 13940 if (rc) { 13941 rc = bnxt_try_recover_fw(bp); 13942 if (rc) 13943 return rc; 13944 rc = bnxt_hwrm_ver_get(bp); 13945 if (rc) 13946 return rc; 13947 } 13948 13949 bnxt_nvm_cfg_ver_get(bp); 13950 13951 rc = bnxt_hwrm_func_reset(bp); 13952 if (rc) 13953 return -ENODEV; 13954 13955 bnxt_hwrm_fw_set_time(bp); 13956 return 0; 13957 } 13958 13959 static int bnxt_fw_init_one_p2(struct bnxt *bp) 13960 { 13961 int rc; 13962 13963 /* Get the MAX capabilities for this function */ 13964 rc = bnxt_hwrm_func_qcaps(bp); 13965 if (rc) { 13966 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n", 13967 rc); 13968 return -ENODEV; 13969 } 13970 13971 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp); 13972 if (rc) 13973 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n", 13974 rc); 13975 13976 if (bnxt_alloc_fw_health(bp)) { 13977 netdev_warn(bp->dev, "no memory for firmware error recovery\n"); 13978 } else { 13979 rc = bnxt_hwrm_error_recovery_qcfg(bp); 13980 if (rc) 13981 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n", 13982 rc); 13983 } 13984 13985 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false); 13986 if (rc) 13987 return -ENODEV; 13988 13989 if (bnxt_fw_pre_resv_vnics(bp)) 13990 bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS; 13991 13992 bnxt_hwrm_func_qcfg(bp); 13993 bnxt_hwrm_vnic_qcaps(bp); 13994 bnxt_hwrm_port_led_qcaps(bp); 13995 bnxt_ethtool_init(bp); 13996 if (bp->fw_cap & BNXT_FW_CAP_PTP) 13997 __bnxt_hwrm_ptp_qcfg(bp); 13998 bnxt_dcb_init(bp); 13999 bnxt_hwmon_init(bp); 14000 return 0; 14001 } 14002 14003 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp) 14004 { 14005 bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP; 14006 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 | 14007 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 | 14008 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 | 14009 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6; 14010 if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA) 14011 bp->rss_hash_delta = bp->rss_hash_cfg; 14012 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) { 14013 bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP; 14014 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 | 14015 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6; 14016 } 14017 } 14018 14019 static void bnxt_set_dflt_rfs(struct bnxt *bp) 14020 { 14021 struct net_device *dev = bp->dev; 14022 14023 dev->hw_features &= ~NETIF_F_NTUPLE; 14024 dev->features &= ~NETIF_F_NTUPLE; 14025 bp->flags &= ~BNXT_FLAG_RFS; 14026 if (bnxt_rfs_supported(bp)) { 14027 dev->hw_features |= NETIF_F_NTUPLE; 14028 if (bnxt_rfs_capable(bp, false)) { 14029 bp->flags |= BNXT_FLAG_RFS; 14030 dev->features |= NETIF_F_NTUPLE; 14031 } 14032 } 14033 } 14034 14035 static void bnxt_fw_init_one_p3(struct bnxt *bp) 14036 { 14037 struct pci_dev *pdev = bp->pdev; 14038 14039 bnxt_set_dflt_rss_hash_type(bp); 14040 bnxt_set_dflt_rfs(bp); 14041 14042 bnxt_get_wol_settings(bp); 14043 if (bp->flags & BNXT_FLAG_WOL_CAP) 14044 device_set_wakeup_enable(&pdev->dev, bp->wol); 14045 else 14046 device_set_wakeup_capable(&pdev->dev, false); 14047 14048 bnxt_hwrm_set_cache_line_size(bp, cache_line_size()); 14049 bnxt_hwrm_coal_params_qcaps(bp); 14050 } 14051 14052 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt); 14053 14054 int bnxt_fw_init_one(struct bnxt *bp) 14055 { 14056 int rc; 14057 14058 rc = bnxt_fw_init_one_p1(bp); 14059 if (rc) { 14060 netdev_err(bp->dev, "Firmware init phase 1 failed\n"); 14061 return rc; 14062 } 14063 rc = bnxt_fw_init_one_p2(bp); 14064 if (rc) { 14065 netdev_err(bp->dev, "Firmware init phase 2 failed\n"); 14066 return rc; 14067 } 14068 rc = bnxt_probe_phy(bp, false); 14069 if (rc) 14070 return rc; 14071 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false); 14072 if (rc) 14073 return rc; 14074 14075 bnxt_fw_init_one_p3(bp); 14076 return 0; 14077 } 14078 14079 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx) 14080 { 14081 struct bnxt_fw_health *fw_health = bp->fw_health; 14082 u32 reg = fw_health->fw_reset_seq_regs[reg_idx]; 14083 u32 val = fw_health->fw_reset_seq_vals[reg_idx]; 14084 u32 reg_type, reg_off, delay_msecs; 14085 14086 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx]; 14087 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg); 14088 reg_off = BNXT_FW_HEALTH_REG_OFF(reg); 14089 switch (reg_type) { 14090 case BNXT_FW_HEALTH_REG_TYPE_CFG: 14091 pci_write_config_dword(bp->pdev, reg_off, val); 14092 break; 14093 case BNXT_FW_HEALTH_REG_TYPE_GRC: 14094 writel(reg_off & BNXT_GRC_BASE_MASK, 14095 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4); 14096 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000; 14097 fallthrough; 14098 case BNXT_FW_HEALTH_REG_TYPE_BAR0: 14099 writel(val, bp->bar0 + reg_off); 14100 break; 14101 case BNXT_FW_HEALTH_REG_TYPE_BAR1: 14102 writel(val, bp->bar1 + reg_off); 14103 break; 14104 } 14105 if (delay_msecs) { 14106 pci_read_config_dword(bp->pdev, 0, &val); 14107 msleep(delay_msecs); 14108 } 14109 } 14110 14111 bool bnxt_hwrm_reset_permitted(struct bnxt *bp) 14112 { 14113 struct hwrm_func_qcfg_output *resp; 14114 struct hwrm_func_qcfg_input *req; 14115 bool result = true; /* firmware will enforce if unknown */ 14116 14117 if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF) 14118 return result; 14119 14120 if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG)) 14121 return result; 14122 14123 req->fid = cpu_to_le16(0xffff); 14124 resp = hwrm_req_hold(bp, req); 14125 if (!hwrm_req_send(bp, req)) 14126 result = !!(le16_to_cpu(resp->flags) & 14127 FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED); 14128 hwrm_req_drop(bp, req); 14129 return result; 14130 } 14131 14132 static void bnxt_reset_all(struct bnxt *bp) 14133 { 14134 struct bnxt_fw_health *fw_health = bp->fw_health; 14135 int i, rc; 14136 14137 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14138 bnxt_fw_reset_via_optee(bp); 14139 bp->fw_reset_timestamp = jiffies; 14140 return; 14141 } 14142 14143 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) { 14144 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) 14145 bnxt_fw_reset_writel(bp, i); 14146 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) { 14147 struct hwrm_fw_reset_input *req; 14148 14149 rc = hwrm_req_init(bp, req, HWRM_FW_RESET); 14150 if (!rc) { 14151 req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG); 14152 req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP; 14153 req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP; 14154 req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL; 14155 rc = hwrm_req_send(bp, req); 14156 } 14157 if (rc != -ENODEV) 14158 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc); 14159 } 14160 bp->fw_reset_timestamp = jiffies; 14161 } 14162 14163 static bool bnxt_fw_reset_timeout(struct bnxt *bp) 14164 { 14165 return time_after(jiffies, bp->fw_reset_timestamp + 14166 (bp->fw_reset_max_dsecs * HZ / 10)); 14167 } 14168 14169 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc) 14170 { 14171 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14172 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF) 14173 bnxt_dl_health_fw_status_update(bp, false); 14174 bp->fw_reset_state = 0; 14175 dev_close(bp->dev); 14176 } 14177 14178 static void bnxt_fw_reset_task(struct work_struct *work) 14179 { 14180 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work); 14181 int rc = 0; 14182 14183 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 14184 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n"); 14185 return; 14186 } 14187 14188 switch (bp->fw_reset_state) { 14189 case BNXT_FW_RESET_STATE_POLL_VF: { 14190 int n = bnxt_get_registered_vfs(bp); 14191 int tmo; 14192 14193 if (n < 0) { 14194 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n", 14195 n, jiffies_to_msecs(jiffies - 14196 bp->fw_reset_timestamp)); 14197 goto fw_reset_abort; 14198 } else if (n > 0) { 14199 if (bnxt_fw_reset_timeout(bp)) { 14200 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14201 bp->fw_reset_state = 0; 14202 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n", 14203 n); 14204 goto ulp_start; 14205 } 14206 bnxt_queue_fw_reset_work(bp, HZ / 10); 14207 return; 14208 } 14209 bp->fw_reset_timestamp = jiffies; 14210 rtnl_lock(); 14211 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) { 14212 bnxt_fw_reset_abort(bp, rc); 14213 rtnl_unlock(); 14214 goto ulp_start; 14215 } 14216 bnxt_fw_reset_close(bp); 14217 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) { 14218 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN; 14219 tmo = HZ / 10; 14220 } else { 14221 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14222 tmo = bp->fw_reset_min_dsecs * HZ / 10; 14223 } 14224 rtnl_unlock(); 14225 bnxt_queue_fw_reset_work(bp, tmo); 14226 return; 14227 } 14228 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: { 14229 u32 val; 14230 14231 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14232 if (!(val & BNXT_FW_STATUS_SHUTDOWN) && 14233 !bnxt_fw_reset_timeout(bp)) { 14234 bnxt_queue_fw_reset_work(bp, HZ / 5); 14235 return; 14236 } 14237 14238 if (!bp->fw_health->primary) { 14239 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs; 14240 14241 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14242 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10); 14243 return; 14244 } 14245 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW; 14246 } 14247 fallthrough; 14248 case BNXT_FW_RESET_STATE_RESET_FW: 14249 bnxt_reset_all(bp); 14250 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV; 14251 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10); 14252 return; 14253 case BNXT_FW_RESET_STATE_ENABLE_DEV: 14254 bnxt_inv_fw_health_reg(bp); 14255 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) && 14256 !bp->fw_reset_min_dsecs) { 14257 u16 val; 14258 14259 pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val); 14260 if (val == 0xffff) { 14261 if (bnxt_fw_reset_timeout(bp)) { 14262 netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n"); 14263 rc = -ETIMEDOUT; 14264 goto fw_reset_abort; 14265 } 14266 bnxt_queue_fw_reset_work(bp, HZ / 1000); 14267 return; 14268 } 14269 } 14270 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state); 14271 clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state); 14272 if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) && 14273 !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) 14274 bnxt_dl_remote_reload(bp); 14275 if (pci_enable_device(bp->pdev)) { 14276 netdev_err(bp->dev, "Cannot re-enable PCI device\n"); 14277 rc = -ENODEV; 14278 goto fw_reset_abort; 14279 } 14280 pci_set_master(bp->pdev); 14281 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW; 14282 fallthrough; 14283 case BNXT_FW_RESET_STATE_POLL_FW: 14284 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT; 14285 rc = bnxt_hwrm_poll(bp); 14286 if (rc) { 14287 if (bnxt_fw_reset_timeout(bp)) { 14288 netdev_err(bp->dev, "Firmware reset aborted\n"); 14289 goto fw_reset_abort_status; 14290 } 14291 bnxt_queue_fw_reset_work(bp, HZ / 5); 14292 return; 14293 } 14294 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT; 14295 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING; 14296 fallthrough; 14297 case BNXT_FW_RESET_STATE_OPENING: 14298 while (!rtnl_trylock()) { 14299 bnxt_queue_fw_reset_work(bp, HZ / 10); 14300 return; 14301 } 14302 rc = bnxt_open(bp->dev); 14303 if (rc) { 14304 netdev_err(bp->dev, "bnxt_open() failed during FW reset\n"); 14305 bnxt_fw_reset_abort(bp, rc); 14306 rtnl_unlock(); 14307 goto ulp_start; 14308 } 14309 14310 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && 14311 bp->fw_health->enabled) { 14312 bp->fw_health->last_fw_reset_cnt = 14313 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG); 14314 } 14315 bp->fw_reset_state = 0; 14316 /* Make sure fw_reset_state is 0 before clearing the flag */ 14317 smp_mb__before_atomic(); 14318 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 14319 bnxt_ptp_reapply_pps(bp); 14320 clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state); 14321 if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) { 14322 bnxt_dl_health_fw_recovery_done(bp); 14323 bnxt_dl_health_fw_status_update(bp, true); 14324 } 14325 rtnl_unlock(); 14326 bnxt_ulp_start(bp, 0); 14327 bnxt_reenable_sriov(bp); 14328 rtnl_lock(); 14329 bnxt_vf_reps_alloc(bp); 14330 bnxt_vf_reps_open(bp); 14331 rtnl_unlock(); 14332 break; 14333 } 14334 return; 14335 14336 fw_reset_abort_status: 14337 if (bp->fw_health->status_reliable || 14338 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) { 14339 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG); 14340 14341 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts); 14342 } 14343 fw_reset_abort: 14344 rtnl_lock(); 14345 bnxt_fw_reset_abort(bp, rc); 14346 rtnl_unlock(); 14347 ulp_start: 14348 bnxt_ulp_start(bp, rc); 14349 } 14350 14351 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev) 14352 { 14353 int rc; 14354 struct bnxt *bp = netdev_priv(dev); 14355 14356 SET_NETDEV_DEV(dev, &pdev->dev); 14357 14358 /* enable device (incl. PCI PM wakeup), and bus-mastering */ 14359 rc = pci_enable_device(pdev); 14360 if (rc) { 14361 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n"); 14362 goto init_err; 14363 } 14364 14365 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 14366 dev_err(&pdev->dev, 14367 "Cannot find PCI device base address, aborting\n"); 14368 rc = -ENODEV; 14369 goto init_err_disable; 14370 } 14371 14372 rc = pci_request_regions(pdev, DRV_MODULE_NAME); 14373 if (rc) { 14374 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n"); 14375 goto init_err_disable; 14376 } 14377 14378 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 && 14379 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) { 14380 dev_err(&pdev->dev, "System does not support DMA, aborting\n"); 14381 rc = -EIO; 14382 goto init_err_release; 14383 } 14384 14385 pci_set_master(pdev); 14386 14387 bp->dev = dev; 14388 bp->pdev = pdev; 14389 14390 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2() 14391 * determines the BAR size. 14392 */ 14393 bp->bar0 = pci_ioremap_bar(pdev, 0); 14394 if (!bp->bar0) { 14395 dev_err(&pdev->dev, "Cannot map device registers, aborting\n"); 14396 rc = -ENOMEM; 14397 goto init_err_release; 14398 } 14399 14400 bp->bar2 = pci_ioremap_bar(pdev, 4); 14401 if (!bp->bar2) { 14402 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n"); 14403 rc = -ENOMEM; 14404 goto init_err_release; 14405 } 14406 14407 INIT_WORK(&bp->sp_task, bnxt_sp_task); 14408 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task); 14409 14410 spin_lock_init(&bp->ntp_fltr_lock); 14411 #if BITS_PER_LONG == 32 14412 spin_lock_init(&bp->db_lock); 14413 #endif 14414 14415 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE; 14416 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE; 14417 14418 timer_setup(&bp->timer, bnxt_timer, 0); 14419 bp->current_interval = BNXT_TIMER_INTERVAL; 14420 14421 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID; 14422 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID; 14423 14424 clear_bit(BNXT_STATE_OPEN, &bp->state); 14425 return 0; 14426 14427 init_err_release: 14428 bnxt_unmap_bars(bp, pdev); 14429 pci_release_regions(pdev); 14430 14431 init_err_disable: 14432 pci_disable_device(pdev); 14433 14434 init_err: 14435 return rc; 14436 } 14437 14438 /* rtnl_lock held */ 14439 static int bnxt_change_mac_addr(struct net_device *dev, void *p) 14440 { 14441 struct sockaddr *addr = p; 14442 struct bnxt *bp = netdev_priv(dev); 14443 int rc = 0; 14444 14445 if (!is_valid_ether_addr(addr->sa_data)) 14446 return -EADDRNOTAVAIL; 14447 14448 if (ether_addr_equal(addr->sa_data, dev->dev_addr)) 14449 return 0; 14450 14451 rc = bnxt_approve_mac(bp, addr->sa_data, true); 14452 if (rc) 14453 return rc; 14454 14455 eth_hw_addr_set(dev, addr->sa_data); 14456 bnxt_clear_usr_fltrs(bp, true); 14457 if (netif_running(dev)) { 14458 bnxt_close_nic(bp, false, false); 14459 rc = bnxt_open_nic(bp, false, false); 14460 } 14461 14462 return rc; 14463 } 14464 14465 /* rtnl_lock held */ 14466 static int bnxt_change_mtu(struct net_device *dev, int new_mtu) 14467 { 14468 struct bnxt *bp = netdev_priv(dev); 14469 14470 if (netif_running(dev)) 14471 bnxt_close_nic(bp, true, false); 14472 14473 WRITE_ONCE(dev->mtu, new_mtu); 14474 bnxt_set_ring_params(bp); 14475 14476 if (netif_running(dev)) 14477 return bnxt_open_nic(bp, true, false); 14478 14479 return 0; 14480 } 14481 14482 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc) 14483 { 14484 struct bnxt *bp = netdev_priv(dev); 14485 bool sh = false; 14486 int rc, tx_cp; 14487 14488 if (tc > bp->max_tc) { 14489 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n", 14490 tc, bp->max_tc); 14491 return -EINVAL; 14492 } 14493 14494 if (bp->num_tc == tc) 14495 return 0; 14496 14497 if (bp->flags & BNXT_FLAG_SHARED_RINGS) 14498 sh = true; 14499 14500 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 14501 sh, tc, bp->tx_nr_rings_xdp); 14502 if (rc) 14503 return rc; 14504 14505 /* Needs to close the device and do hw resource re-allocations */ 14506 if (netif_running(bp->dev)) 14507 bnxt_close_nic(bp, true, false); 14508 14509 if (tc) { 14510 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc; 14511 netdev_set_num_tc(dev, tc); 14512 bp->num_tc = tc; 14513 } else { 14514 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 14515 netdev_reset_tc(dev); 14516 bp->num_tc = 0; 14517 } 14518 bp->tx_nr_rings += bp->tx_nr_rings_xdp; 14519 tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings); 14520 bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) : 14521 tx_cp + bp->rx_nr_rings; 14522 14523 if (netif_running(bp->dev)) 14524 return bnxt_open_nic(bp, true, false); 14525 14526 return 0; 14527 } 14528 14529 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 14530 void *cb_priv) 14531 { 14532 struct bnxt *bp = cb_priv; 14533 14534 if (!bnxt_tc_flower_enabled(bp) || 14535 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 14536 return -EOPNOTSUPP; 14537 14538 switch (type) { 14539 case TC_SETUP_CLSFLOWER: 14540 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data); 14541 default: 14542 return -EOPNOTSUPP; 14543 } 14544 } 14545 14546 LIST_HEAD(bnxt_block_cb_list); 14547 14548 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type, 14549 void *type_data) 14550 { 14551 struct bnxt *bp = netdev_priv(dev); 14552 14553 switch (type) { 14554 case TC_SETUP_BLOCK: 14555 return flow_block_cb_setup_simple(type_data, 14556 &bnxt_block_cb_list, 14557 bnxt_setup_tc_block_cb, 14558 bp, bp, true); 14559 case TC_SETUP_QDISC_MQPRIO: { 14560 struct tc_mqprio_qopt *mqprio = type_data; 14561 14562 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 14563 14564 return bnxt_setup_mq_tc(dev, mqprio->num_tc); 14565 } 14566 default: 14567 return -EOPNOTSUPP; 14568 } 14569 } 14570 14571 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys, 14572 const struct sk_buff *skb) 14573 { 14574 struct bnxt_vnic_info *vnic; 14575 14576 if (skb) 14577 return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK; 14578 14579 vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT]; 14580 return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key); 14581 } 14582 14583 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr, 14584 u32 idx) 14585 { 14586 struct hlist_head *head; 14587 int bit_id; 14588 14589 spin_lock_bh(&bp->ntp_fltr_lock); 14590 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0); 14591 if (bit_id < 0) { 14592 spin_unlock_bh(&bp->ntp_fltr_lock); 14593 return -ENOMEM; 14594 } 14595 14596 fltr->base.sw_id = (u16)bit_id; 14597 fltr->base.type = BNXT_FLTR_TYPE_NTUPLE; 14598 fltr->base.flags |= BNXT_ACT_RING_DST; 14599 head = &bp->ntp_fltr_hash_tbl[idx]; 14600 hlist_add_head_rcu(&fltr->base.hash, head); 14601 set_bit(BNXT_FLTR_INSERTED, &fltr->base.state); 14602 bnxt_insert_usr_fltr(bp, &fltr->base); 14603 bp->ntp_fltr_count++; 14604 spin_unlock_bh(&bp->ntp_fltr_lock); 14605 return 0; 14606 } 14607 14608 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1, 14609 struct bnxt_ntuple_filter *f2) 14610 { 14611 struct bnxt_flow_masks *masks1 = &f1->fmasks; 14612 struct bnxt_flow_masks *masks2 = &f2->fmasks; 14613 struct flow_keys *keys1 = &f1->fkeys; 14614 struct flow_keys *keys2 = &f2->fkeys; 14615 14616 if (keys1->basic.n_proto != keys2->basic.n_proto || 14617 keys1->basic.ip_proto != keys2->basic.ip_proto) 14618 return false; 14619 14620 if (keys1->basic.n_proto == htons(ETH_P_IP)) { 14621 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src || 14622 masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src || 14623 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst || 14624 masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst) 14625 return false; 14626 } else { 14627 if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src, 14628 &keys2->addrs.v6addrs.src) || 14629 !ipv6_addr_equal(&masks1->addrs.v6addrs.src, 14630 &masks2->addrs.v6addrs.src) || 14631 !ipv6_addr_equal(&keys1->addrs.v6addrs.dst, 14632 &keys2->addrs.v6addrs.dst) || 14633 !ipv6_addr_equal(&masks1->addrs.v6addrs.dst, 14634 &masks2->addrs.v6addrs.dst)) 14635 return false; 14636 } 14637 14638 return keys1->ports.src == keys2->ports.src && 14639 masks1->ports.src == masks2->ports.src && 14640 keys1->ports.dst == keys2->ports.dst && 14641 masks1->ports.dst == masks2->ports.dst && 14642 keys1->control.flags == keys2->control.flags && 14643 f1->l2_fltr == f2->l2_fltr; 14644 } 14645 14646 struct bnxt_ntuple_filter * 14647 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp, 14648 struct bnxt_ntuple_filter *fltr, u32 idx) 14649 { 14650 struct bnxt_ntuple_filter *f; 14651 struct hlist_head *head; 14652 14653 head = &bp->ntp_fltr_hash_tbl[idx]; 14654 hlist_for_each_entry_rcu(f, head, base.hash) { 14655 if (bnxt_fltr_match(f, fltr)) 14656 return f; 14657 } 14658 return NULL; 14659 } 14660 14661 #ifdef CONFIG_RFS_ACCEL 14662 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 14663 u16 rxq_index, u32 flow_id) 14664 { 14665 struct bnxt *bp = netdev_priv(dev); 14666 struct bnxt_ntuple_filter *fltr, *new_fltr; 14667 struct flow_keys *fkeys; 14668 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 14669 struct bnxt_l2_filter *l2_fltr; 14670 int rc = 0, idx; 14671 u32 flags; 14672 14673 if (ether_addr_equal(dev->dev_addr, eth->h_dest)) { 14674 l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0]; 14675 atomic_inc(&l2_fltr->refcnt); 14676 } else { 14677 struct bnxt_l2_key key; 14678 14679 ether_addr_copy(key.dst_mac_addr, eth->h_dest); 14680 key.vlan = 0; 14681 l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key); 14682 if (!l2_fltr) 14683 return -EINVAL; 14684 if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) { 14685 bnxt_del_l2_filter(bp, l2_fltr); 14686 return -EINVAL; 14687 } 14688 } 14689 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC); 14690 if (!new_fltr) { 14691 bnxt_del_l2_filter(bp, l2_fltr); 14692 return -ENOMEM; 14693 } 14694 14695 fkeys = &new_fltr->fkeys; 14696 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) { 14697 rc = -EPROTONOSUPPORT; 14698 goto err_free; 14699 } 14700 14701 if ((fkeys->basic.n_proto != htons(ETH_P_IP) && 14702 fkeys->basic.n_proto != htons(ETH_P_IPV6)) || 14703 ((fkeys->basic.ip_proto != IPPROTO_TCP) && 14704 (fkeys->basic.ip_proto != IPPROTO_UDP))) { 14705 rc = -EPROTONOSUPPORT; 14706 goto err_free; 14707 } 14708 new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL; 14709 if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) { 14710 if (bp->hwrm_spec_code < 0x10601) { 14711 rc = -EPROTONOSUPPORT; 14712 goto err_free; 14713 } 14714 new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL; 14715 } 14716 flags = fkeys->control.flags; 14717 if (((flags & FLOW_DIS_ENCAPSULATION) && 14718 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) { 14719 rc = -EPROTONOSUPPORT; 14720 goto err_free; 14721 } 14722 new_fltr->l2_fltr = l2_fltr; 14723 14724 idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb); 14725 rcu_read_lock(); 14726 fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx); 14727 if (fltr) { 14728 rc = fltr->base.sw_id; 14729 rcu_read_unlock(); 14730 goto err_free; 14731 } 14732 rcu_read_unlock(); 14733 14734 new_fltr->flow_id = flow_id; 14735 new_fltr->base.rxq = rxq_index; 14736 rc = bnxt_insert_ntp_filter(bp, new_fltr, idx); 14737 if (!rc) { 14738 bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT); 14739 return new_fltr->base.sw_id; 14740 } 14741 14742 err_free: 14743 bnxt_del_l2_filter(bp, l2_fltr); 14744 kfree(new_fltr); 14745 return rc; 14746 } 14747 #endif 14748 14749 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr) 14750 { 14751 spin_lock_bh(&bp->ntp_fltr_lock); 14752 if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) { 14753 spin_unlock_bh(&bp->ntp_fltr_lock); 14754 return; 14755 } 14756 hlist_del_rcu(&fltr->base.hash); 14757 bnxt_del_one_usr_fltr(bp, &fltr->base); 14758 bp->ntp_fltr_count--; 14759 spin_unlock_bh(&bp->ntp_fltr_lock); 14760 bnxt_del_l2_filter(bp, fltr->l2_fltr); 14761 clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap); 14762 kfree_rcu(fltr, base.rcu); 14763 } 14764 14765 static void bnxt_cfg_ntp_filters(struct bnxt *bp) 14766 { 14767 #ifdef CONFIG_RFS_ACCEL 14768 int i; 14769 14770 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) { 14771 struct hlist_head *head; 14772 struct hlist_node *tmp; 14773 struct bnxt_ntuple_filter *fltr; 14774 int rc; 14775 14776 head = &bp->ntp_fltr_hash_tbl[i]; 14777 hlist_for_each_entry_safe(fltr, tmp, head, base.hash) { 14778 bool del = false; 14779 14780 if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) { 14781 if (fltr->base.flags & BNXT_ACT_NO_AGING) 14782 continue; 14783 if (rps_may_expire_flow(bp->dev, fltr->base.rxq, 14784 fltr->flow_id, 14785 fltr->base.sw_id)) { 14786 bnxt_hwrm_cfa_ntuple_filter_free(bp, 14787 fltr); 14788 del = true; 14789 } 14790 } else { 14791 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp, 14792 fltr); 14793 if (rc) 14794 del = true; 14795 else 14796 set_bit(BNXT_FLTR_VALID, &fltr->base.state); 14797 } 14798 14799 if (del) 14800 bnxt_del_ntp_filter(bp, fltr); 14801 } 14802 } 14803 #endif 14804 } 14805 14806 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table, 14807 unsigned int entry, struct udp_tunnel_info *ti) 14808 { 14809 struct bnxt *bp = netdev_priv(netdev); 14810 unsigned int cmd; 14811 14812 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 14813 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN; 14814 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 14815 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE; 14816 else 14817 cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE; 14818 14819 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd); 14820 } 14821 14822 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table, 14823 unsigned int entry, struct udp_tunnel_info *ti) 14824 { 14825 struct bnxt *bp = netdev_priv(netdev); 14826 unsigned int cmd; 14827 14828 if (ti->type == UDP_TUNNEL_TYPE_VXLAN) 14829 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN; 14830 else if (ti->type == UDP_TUNNEL_TYPE_GENEVE) 14831 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE; 14832 else 14833 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE; 14834 14835 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd); 14836 } 14837 14838 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = { 14839 .set_port = bnxt_udp_tunnel_set_port, 14840 .unset_port = bnxt_udp_tunnel_unset_port, 14841 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 14842 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 14843 .tables = { 14844 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 14845 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 14846 }, 14847 }, bnxt_udp_tunnels_p7 = { 14848 .set_port = bnxt_udp_tunnel_set_port, 14849 .unset_port = bnxt_udp_tunnel_unset_port, 14850 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP | 14851 UDP_TUNNEL_NIC_INFO_OPEN_ONLY, 14852 .tables = { 14853 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, }, 14854 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, }, 14855 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, }, 14856 }, 14857 }; 14858 14859 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 14860 struct net_device *dev, u32 filter_mask, 14861 int nlflags) 14862 { 14863 struct bnxt *bp = netdev_priv(dev); 14864 14865 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0, 14866 nlflags, filter_mask, NULL); 14867 } 14868 14869 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 14870 u16 flags, struct netlink_ext_ack *extack) 14871 { 14872 struct bnxt *bp = netdev_priv(dev); 14873 struct nlattr *attr, *br_spec; 14874 int rem, rc = 0; 14875 14876 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp)) 14877 return -EOPNOTSUPP; 14878 14879 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 14880 if (!br_spec) 14881 return -EINVAL; 14882 14883 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 14884 u16 mode; 14885 14886 mode = nla_get_u16(attr); 14887 if (mode == bp->br_mode) 14888 break; 14889 14890 rc = bnxt_hwrm_set_br_mode(bp, mode); 14891 if (!rc) 14892 bp->br_mode = mode; 14893 break; 14894 } 14895 return rc; 14896 } 14897 14898 int bnxt_get_port_parent_id(struct net_device *dev, 14899 struct netdev_phys_item_id *ppid) 14900 { 14901 struct bnxt *bp = netdev_priv(dev); 14902 14903 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 14904 return -EOPNOTSUPP; 14905 14906 /* The PF and it's VF-reps only support the switchdev framework */ 14907 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID)) 14908 return -EOPNOTSUPP; 14909 14910 ppid->id_len = sizeof(bp->dsn); 14911 memcpy(ppid->id, bp->dsn, ppid->id_len); 14912 14913 return 0; 14914 } 14915 14916 static const struct net_device_ops bnxt_netdev_ops = { 14917 .ndo_open = bnxt_open, 14918 .ndo_start_xmit = bnxt_start_xmit, 14919 .ndo_stop = bnxt_close, 14920 .ndo_get_stats64 = bnxt_get_stats64, 14921 .ndo_set_rx_mode = bnxt_set_rx_mode, 14922 .ndo_eth_ioctl = bnxt_ioctl, 14923 .ndo_validate_addr = eth_validate_addr, 14924 .ndo_set_mac_address = bnxt_change_mac_addr, 14925 .ndo_change_mtu = bnxt_change_mtu, 14926 .ndo_fix_features = bnxt_fix_features, 14927 .ndo_set_features = bnxt_set_features, 14928 .ndo_features_check = bnxt_features_check, 14929 .ndo_tx_timeout = bnxt_tx_timeout, 14930 #ifdef CONFIG_BNXT_SRIOV 14931 .ndo_get_vf_config = bnxt_get_vf_config, 14932 .ndo_set_vf_mac = bnxt_set_vf_mac, 14933 .ndo_set_vf_vlan = bnxt_set_vf_vlan, 14934 .ndo_set_vf_rate = bnxt_set_vf_bw, 14935 .ndo_set_vf_link_state = bnxt_set_vf_link_state, 14936 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk, 14937 .ndo_set_vf_trust = bnxt_set_vf_trust, 14938 #endif 14939 .ndo_setup_tc = bnxt_setup_tc, 14940 #ifdef CONFIG_RFS_ACCEL 14941 .ndo_rx_flow_steer = bnxt_rx_flow_steer, 14942 #endif 14943 .ndo_bpf = bnxt_xdp, 14944 .ndo_xdp_xmit = bnxt_xdp_xmit, 14945 .ndo_bridge_getlink = bnxt_bridge_getlink, 14946 .ndo_bridge_setlink = bnxt_bridge_setlink, 14947 }; 14948 14949 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i, 14950 struct netdev_queue_stats_rx *stats) 14951 { 14952 struct bnxt *bp = netdev_priv(dev); 14953 struct bnxt_cp_ring_info *cpr; 14954 u64 *sw; 14955 14956 cpr = &bp->bnapi[i]->cp_ring; 14957 sw = cpr->stats.sw_stats; 14958 14959 stats->packets = 0; 14960 stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts); 14961 stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts); 14962 stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts); 14963 14964 stats->bytes = 0; 14965 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes); 14966 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes); 14967 stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes); 14968 14969 stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards; 14970 } 14971 14972 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i, 14973 struct netdev_queue_stats_tx *stats) 14974 { 14975 struct bnxt *bp = netdev_priv(dev); 14976 struct bnxt_napi *bnapi; 14977 u64 *sw; 14978 14979 bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi; 14980 sw = bnapi->cp_ring.stats.sw_stats; 14981 14982 stats->packets = 0; 14983 stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts); 14984 stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts); 14985 stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts); 14986 14987 stats->bytes = 0; 14988 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes); 14989 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes); 14990 stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes); 14991 } 14992 14993 static void bnxt_get_base_stats(struct net_device *dev, 14994 struct netdev_queue_stats_rx *rx, 14995 struct netdev_queue_stats_tx *tx) 14996 { 14997 struct bnxt *bp = netdev_priv(dev); 14998 14999 rx->packets = bp->net_stats_prev.rx_packets; 15000 rx->bytes = bp->net_stats_prev.rx_bytes; 15001 rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards; 15002 15003 tx->packets = bp->net_stats_prev.tx_packets; 15004 tx->bytes = bp->net_stats_prev.tx_bytes; 15005 } 15006 15007 static const struct netdev_stat_ops bnxt_stat_ops = { 15008 .get_queue_stats_rx = bnxt_get_queue_stats_rx, 15009 .get_queue_stats_tx = bnxt_get_queue_stats_tx, 15010 .get_base_stats = bnxt_get_base_stats, 15011 }; 15012 15013 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr) 15014 { 15015 u16 mem_size; 15016 15017 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1; 15018 mem_size = rxr->rx_agg_bmap_size / 8; 15019 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL); 15020 if (!rxr->rx_agg_bmap) 15021 return -ENOMEM; 15022 15023 return 0; 15024 } 15025 15026 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 15027 { 15028 struct bnxt_rx_ring_info *rxr, *clone; 15029 struct bnxt *bp = netdev_priv(dev); 15030 struct bnxt_ring_struct *ring; 15031 int rc; 15032 15033 rxr = &bp->rx_ring[idx]; 15034 clone = qmem; 15035 memcpy(clone, rxr, sizeof(*rxr)); 15036 bnxt_init_rx_ring_struct(bp, clone); 15037 bnxt_reset_rx_ring_struct(bp, clone); 15038 15039 clone->rx_prod = 0; 15040 clone->rx_agg_prod = 0; 15041 clone->rx_sw_agg_prod = 0; 15042 clone->rx_next_cons = 0; 15043 15044 rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid); 15045 if (rc) 15046 return rc; 15047 15048 rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0); 15049 if (rc < 0) 15050 goto err_page_pool_destroy; 15051 15052 rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq, 15053 MEM_TYPE_PAGE_POOL, 15054 clone->page_pool); 15055 if (rc) 15056 goto err_rxq_info_unreg; 15057 15058 ring = &clone->rx_ring_struct; 15059 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15060 if (rc) 15061 goto err_free_rx_ring; 15062 15063 if (bp->flags & BNXT_FLAG_AGG_RINGS) { 15064 ring = &clone->rx_agg_ring_struct; 15065 rc = bnxt_alloc_ring(bp, &ring->ring_mem); 15066 if (rc) 15067 goto err_free_rx_agg_ring; 15068 15069 rc = bnxt_alloc_rx_agg_bmap(bp, clone); 15070 if (rc) 15071 goto err_free_rx_agg_ring; 15072 } 15073 15074 bnxt_init_one_rx_ring_rxbd(bp, clone); 15075 bnxt_init_one_rx_agg_ring_rxbd(bp, clone); 15076 15077 bnxt_alloc_one_rx_ring_skb(bp, clone, idx); 15078 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15079 bnxt_alloc_one_rx_ring_page(bp, clone, idx); 15080 15081 return 0; 15082 15083 err_free_rx_agg_ring: 15084 bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem); 15085 err_free_rx_ring: 15086 bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem); 15087 err_rxq_info_unreg: 15088 xdp_rxq_info_unreg(&clone->xdp_rxq); 15089 err_page_pool_destroy: 15090 clone->page_pool->p.napi = NULL; 15091 page_pool_destroy(clone->page_pool); 15092 clone->page_pool = NULL; 15093 return rc; 15094 } 15095 15096 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem) 15097 { 15098 struct bnxt_rx_ring_info *rxr = qmem; 15099 struct bnxt *bp = netdev_priv(dev); 15100 struct bnxt_ring_struct *ring; 15101 15102 bnxt_free_one_rx_ring(bp, rxr); 15103 bnxt_free_one_rx_agg_ring(bp, rxr); 15104 15105 xdp_rxq_info_unreg(&rxr->xdp_rxq); 15106 15107 page_pool_destroy(rxr->page_pool); 15108 rxr->page_pool = NULL; 15109 15110 ring = &rxr->rx_ring_struct; 15111 bnxt_free_ring(bp, &ring->ring_mem); 15112 15113 ring = &rxr->rx_agg_ring_struct; 15114 bnxt_free_ring(bp, &ring->ring_mem); 15115 15116 kfree(rxr->rx_agg_bmap); 15117 rxr->rx_agg_bmap = NULL; 15118 } 15119 15120 static void bnxt_copy_rx_ring(struct bnxt *bp, 15121 struct bnxt_rx_ring_info *dst, 15122 struct bnxt_rx_ring_info *src) 15123 { 15124 struct bnxt_ring_mem_info *dst_rmem, *src_rmem; 15125 struct bnxt_ring_struct *dst_ring, *src_ring; 15126 int i; 15127 15128 dst_ring = &dst->rx_ring_struct; 15129 dst_rmem = &dst_ring->ring_mem; 15130 src_ring = &src->rx_ring_struct; 15131 src_rmem = &src_ring->ring_mem; 15132 15133 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15134 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15135 WARN_ON(dst_rmem->flags != src_rmem->flags); 15136 WARN_ON(dst_rmem->depth != src_rmem->depth); 15137 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15138 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15139 15140 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15141 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15142 *dst_rmem->vmem = *src_rmem->vmem; 15143 for (i = 0; i < dst_rmem->nr_pages; i++) { 15144 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15145 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15146 } 15147 15148 if (!(bp->flags & BNXT_FLAG_AGG_RINGS)) 15149 return; 15150 15151 dst_ring = &dst->rx_agg_ring_struct; 15152 dst_rmem = &dst_ring->ring_mem; 15153 src_ring = &src->rx_agg_ring_struct; 15154 src_rmem = &src_ring->ring_mem; 15155 15156 WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages); 15157 WARN_ON(dst_rmem->page_size != src_rmem->page_size); 15158 WARN_ON(dst_rmem->flags != src_rmem->flags); 15159 WARN_ON(dst_rmem->depth != src_rmem->depth); 15160 WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size); 15161 WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem); 15162 WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size); 15163 15164 dst_rmem->pg_tbl = src_rmem->pg_tbl; 15165 dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map; 15166 *dst_rmem->vmem = *src_rmem->vmem; 15167 for (i = 0; i < dst_rmem->nr_pages; i++) { 15168 dst_rmem->pg_arr[i] = src_rmem->pg_arr[i]; 15169 dst_rmem->dma_arr[i] = src_rmem->dma_arr[i]; 15170 } 15171 15172 dst->rx_agg_bmap = src->rx_agg_bmap; 15173 } 15174 15175 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx) 15176 { 15177 struct bnxt *bp = netdev_priv(dev); 15178 struct bnxt_rx_ring_info *rxr, *clone; 15179 struct bnxt_cp_ring_info *cpr; 15180 struct bnxt_vnic_info *vnic; 15181 int i, rc; 15182 15183 rxr = &bp->rx_ring[idx]; 15184 clone = qmem; 15185 15186 rxr->rx_prod = clone->rx_prod; 15187 rxr->rx_agg_prod = clone->rx_agg_prod; 15188 rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod; 15189 rxr->rx_next_cons = clone->rx_next_cons; 15190 rxr->page_pool = clone->page_pool; 15191 rxr->xdp_rxq = clone->xdp_rxq; 15192 15193 bnxt_copy_rx_ring(bp, rxr, clone); 15194 15195 rc = bnxt_hwrm_rx_ring_alloc(bp, rxr); 15196 if (rc) 15197 return rc; 15198 rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr); 15199 if (rc) 15200 goto err_free_hwrm_rx_ring; 15201 15202 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod); 15203 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15204 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod); 15205 15206 cpr = &rxr->bnapi->cp_ring; 15207 cpr->sw_stats->rx.rx_resets++; 15208 15209 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15210 vnic = &bp->vnic_info[i]; 15211 vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN; 15212 bnxt_hwrm_vnic_update(bp, vnic, 15213 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15214 } 15215 15216 return 0; 15217 15218 err_free_hwrm_rx_ring: 15219 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15220 return rc; 15221 } 15222 15223 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx) 15224 { 15225 struct bnxt *bp = netdev_priv(dev); 15226 struct bnxt_rx_ring_info *rxr; 15227 struct bnxt_vnic_info *vnic; 15228 int i; 15229 15230 for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) { 15231 vnic = &bp->vnic_info[i]; 15232 vnic->mru = 0; 15233 bnxt_hwrm_vnic_update(bp, vnic, 15234 VNIC_UPDATE_REQ_ENABLES_MRU_VALID); 15235 } 15236 15237 rxr = &bp->rx_ring[idx]; 15238 bnxt_hwrm_rx_ring_free(bp, rxr, false); 15239 bnxt_hwrm_rx_agg_ring_free(bp, rxr, false); 15240 rxr->rx_next_cons = 0; 15241 page_pool_disable_direct_recycling(rxr->page_pool); 15242 15243 memcpy(qmem, rxr, sizeof(*rxr)); 15244 bnxt_init_rx_ring_struct(bp, qmem); 15245 15246 return 0; 15247 } 15248 15249 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = { 15250 .ndo_queue_mem_size = sizeof(struct bnxt_rx_ring_info), 15251 .ndo_queue_mem_alloc = bnxt_queue_mem_alloc, 15252 .ndo_queue_mem_free = bnxt_queue_mem_free, 15253 .ndo_queue_start = bnxt_queue_start, 15254 .ndo_queue_stop = bnxt_queue_stop, 15255 }; 15256 15257 static void bnxt_remove_one(struct pci_dev *pdev) 15258 { 15259 struct net_device *dev = pci_get_drvdata(pdev); 15260 struct bnxt *bp = netdev_priv(dev); 15261 15262 if (BNXT_PF(bp)) 15263 bnxt_sriov_disable(bp); 15264 15265 bnxt_rdma_aux_device_del(bp); 15266 15267 bnxt_ptp_clear(bp); 15268 unregister_netdev(dev); 15269 15270 bnxt_rdma_aux_device_uninit(bp); 15271 15272 bnxt_free_l2_filters(bp, true); 15273 bnxt_free_ntp_fltrs(bp, true); 15274 WARN_ON(bp->num_rss_ctx); 15275 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 15276 /* Flush any pending tasks */ 15277 cancel_work_sync(&bp->sp_task); 15278 cancel_delayed_work_sync(&bp->fw_reset_task); 15279 bp->sp_event = 0; 15280 15281 bnxt_dl_fw_reporters_destroy(bp); 15282 bnxt_dl_unregister(bp); 15283 bnxt_shutdown_tc(bp); 15284 15285 bnxt_clear_int_mode(bp); 15286 bnxt_hwrm_func_drv_unrgtr(bp); 15287 bnxt_free_hwrm_resources(bp); 15288 bnxt_hwmon_uninit(bp); 15289 bnxt_ethtool_free(bp); 15290 bnxt_dcb_free(bp); 15291 kfree(bp->ptp_cfg); 15292 bp->ptp_cfg = NULL; 15293 kfree(bp->fw_health); 15294 bp->fw_health = NULL; 15295 bnxt_cleanup_pci(bp); 15296 bnxt_free_ctx_mem(bp); 15297 kfree(bp->rss_indir_tbl); 15298 bp->rss_indir_tbl = NULL; 15299 bnxt_free_port_stats(bp); 15300 free_netdev(dev); 15301 } 15302 15303 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt) 15304 { 15305 int rc = 0; 15306 struct bnxt_link_info *link_info = &bp->link_info; 15307 15308 bp->phy_flags = 0; 15309 rc = bnxt_hwrm_phy_qcaps(bp); 15310 if (rc) { 15311 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n", 15312 rc); 15313 return rc; 15314 } 15315 if (bp->phy_flags & BNXT_PHY_FL_NO_FCS) 15316 bp->dev->priv_flags |= IFF_SUPP_NOFCS; 15317 else 15318 bp->dev->priv_flags &= ~IFF_SUPP_NOFCS; 15319 if (!fw_dflt) 15320 return 0; 15321 15322 mutex_lock(&bp->link_lock); 15323 rc = bnxt_update_link(bp, false); 15324 if (rc) { 15325 mutex_unlock(&bp->link_lock); 15326 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n", 15327 rc); 15328 return rc; 15329 } 15330 15331 /* Older firmware does not have supported_auto_speeds, so assume 15332 * that all supported speeds can be autonegotiated. 15333 */ 15334 if (link_info->auto_link_speeds && !link_info->support_auto_speeds) 15335 link_info->support_auto_speeds = link_info->support_speeds; 15336 15337 bnxt_init_ethtool_link_settings(bp); 15338 mutex_unlock(&bp->link_lock); 15339 return 0; 15340 } 15341 15342 static int bnxt_get_max_irq(struct pci_dev *pdev) 15343 { 15344 u16 ctrl; 15345 15346 if (!pdev->msix_cap) 15347 return 1; 15348 15349 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 15350 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1; 15351 } 15352 15353 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15354 int *max_cp) 15355 { 15356 struct bnxt_hw_resc *hw_resc = &bp->hw_resc; 15357 int max_ring_grps = 0, max_irq; 15358 15359 *max_tx = hw_resc->max_tx_rings; 15360 *max_rx = hw_resc->max_rx_rings; 15361 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp); 15362 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) - 15363 bnxt_get_ulp_msix_num_in_use(bp), 15364 hw_resc->max_stat_ctxs - 15365 bnxt_get_ulp_stat_ctxs_in_use(bp)); 15366 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) 15367 *max_cp = min_t(int, *max_cp, max_irq); 15368 max_ring_grps = hw_resc->max_hw_ring_grps; 15369 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) { 15370 *max_cp -= 1; 15371 *max_rx -= 2; 15372 } 15373 if (bp->flags & BNXT_FLAG_AGG_RINGS) 15374 *max_rx >>= 1; 15375 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) { 15376 int rc; 15377 15378 rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false); 15379 if (rc) { 15380 *max_rx = 0; 15381 *max_tx = 0; 15382 } 15383 /* On P5 chips, max_cp output param should be available NQs */ 15384 *max_cp = max_irq; 15385 } 15386 *max_rx = min_t(int, *max_rx, max_ring_grps); 15387 } 15388 15389 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared) 15390 { 15391 int rx, tx, cp; 15392 15393 _bnxt_get_max_rings(bp, &rx, &tx, &cp); 15394 *max_rx = rx; 15395 *max_tx = tx; 15396 if (!rx || !tx || !cp) 15397 return -ENOMEM; 15398 15399 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared); 15400 } 15401 15402 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx, 15403 bool shared) 15404 { 15405 int rc; 15406 15407 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15408 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) { 15409 /* Not enough rings, try disabling agg rings. */ 15410 bp->flags &= ~BNXT_FLAG_AGG_RINGS; 15411 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared); 15412 if (rc) { 15413 /* set BNXT_FLAG_AGG_RINGS back for consistency */ 15414 bp->flags |= BNXT_FLAG_AGG_RINGS; 15415 return rc; 15416 } 15417 bp->flags |= BNXT_FLAG_NO_AGG_RINGS; 15418 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15419 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW); 15420 bnxt_set_ring_params(bp); 15421 } 15422 15423 if (bp->flags & BNXT_FLAG_ROCE_CAP) { 15424 int max_cp, max_stat, max_irq; 15425 15426 /* Reserve minimum resources for RoCE */ 15427 max_cp = bnxt_get_max_func_cp_rings(bp); 15428 max_stat = bnxt_get_max_func_stat_ctxs(bp); 15429 max_irq = bnxt_get_max_func_irqs(bp); 15430 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS || 15431 max_irq <= BNXT_MIN_ROCE_CP_RINGS || 15432 max_stat <= BNXT_MIN_ROCE_STAT_CTXS) 15433 return 0; 15434 15435 max_cp -= BNXT_MIN_ROCE_CP_RINGS; 15436 max_irq -= BNXT_MIN_ROCE_CP_RINGS; 15437 max_stat -= BNXT_MIN_ROCE_STAT_CTXS; 15438 max_cp = min_t(int, max_cp, max_irq); 15439 max_cp = min_t(int, max_cp, max_stat); 15440 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared); 15441 if (rc) 15442 rc = 0; 15443 } 15444 return rc; 15445 } 15446 15447 /* In initial default shared ring setting, each shared ring must have a 15448 * RX/TX ring pair. 15449 */ 15450 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp) 15451 { 15452 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings); 15453 bp->rx_nr_rings = bp->cp_nr_rings; 15454 bp->tx_nr_rings_per_tc = bp->cp_nr_rings; 15455 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15456 } 15457 15458 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh) 15459 { 15460 int dflt_rings, max_rx_rings, max_tx_rings, rc; 15461 int avail_msix; 15462 15463 if (!bnxt_can_reserve_rings(bp)) 15464 return 0; 15465 15466 if (sh) 15467 bp->flags |= BNXT_FLAG_SHARED_RINGS; 15468 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues(); 15469 /* Reduce default rings on multi-port cards so that total default 15470 * rings do not exceed CPU count. 15471 */ 15472 if (bp->port_count > 1) { 15473 int max_rings = 15474 max_t(int, num_online_cpus() / bp->port_count, 1); 15475 15476 dflt_rings = min_t(int, dflt_rings, max_rings); 15477 } 15478 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh); 15479 if (rc) 15480 return rc; 15481 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings); 15482 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings); 15483 if (sh) 15484 bnxt_trim_dflt_sh_rings(bp); 15485 else 15486 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings; 15487 bp->tx_nr_rings = bp->tx_nr_rings_per_tc; 15488 15489 avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings; 15490 if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) { 15491 int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want); 15492 15493 bnxt_set_ulp_msix_num(bp, ulp_num_msix); 15494 bnxt_set_dflt_ulp_stat_ctxs(bp); 15495 } 15496 15497 rc = __bnxt_reserve_rings(bp); 15498 if (rc && rc != -ENODEV) 15499 netdev_warn(bp->dev, "Unable to reserve tx rings\n"); 15500 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15501 if (sh) 15502 bnxt_trim_dflt_sh_rings(bp); 15503 15504 /* Rings may have been trimmed, re-reserve the trimmed rings. */ 15505 if (bnxt_need_reserve_rings(bp)) { 15506 rc = __bnxt_reserve_rings(bp); 15507 if (rc && rc != -ENODEV) 15508 netdev_warn(bp->dev, "2nd rings reservation failed.\n"); 15509 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15510 } 15511 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) { 15512 bp->rx_nr_rings++; 15513 bp->cp_nr_rings++; 15514 } 15515 if (rc) { 15516 bp->tx_nr_rings = 0; 15517 bp->rx_nr_rings = 0; 15518 } 15519 return rc; 15520 } 15521 15522 static int bnxt_init_dflt_ring_mode(struct bnxt *bp) 15523 { 15524 int rc; 15525 15526 if (bp->tx_nr_rings) 15527 return 0; 15528 15529 bnxt_ulp_irq_stop(bp); 15530 bnxt_clear_int_mode(bp); 15531 rc = bnxt_set_dflt_rings(bp, true); 15532 if (rc) { 15533 if (BNXT_VF(bp) && rc == -ENODEV) 15534 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 15535 else 15536 netdev_err(bp->dev, "Not enough rings available.\n"); 15537 goto init_dflt_ring_err; 15538 } 15539 rc = bnxt_init_int_mode(bp); 15540 if (rc) 15541 goto init_dflt_ring_err; 15542 15543 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15544 15545 bnxt_set_dflt_rfs(bp); 15546 15547 init_dflt_ring_err: 15548 bnxt_ulp_irq_restart(bp, rc); 15549 return rc; 15550 } 15551 15552 int bnxt_restore_pf_fw_resources(struct bnxt *bp) 15553 { 15554 int rc; 15555 15556 ASSERT_RTNL(); 15557 bnxt_hwrm_func_qcaps(bp); 15558 15559 if (netif_running(bp->dev)) 15560 __bnxt_close_nic(bp, true, false); 15561 15562 bnxt_ulp_irq_stop(bp); 15563 bnxt_clear_int_mode(bp); 15564 rc = bnxt_init_int_mode(bp); 15565 bnxt_ulp_irq_restart(bp, rc); 15566 15567 if (netif_running(bp->dev)) { 15568 if (rc) 15569 dev_close(bp->dev); 15570 else 15571 rc = bnxt_open_nic(bp, true, false); 15572 } 15573 15574 return rc; 15575 } 15576 15577 static int bnxt_init_mac_addr(struct bnxt *bp) 15578 { 15579 int rc = 0; 15580 15581 if (BNXT_PF(bp)) { 15582 eth_hw_addr_set(bp->dev, bp->pf.mac_addr); 15583 } else { 15584 #ifdef CONFIG_BNXT_SRIOV 15585 struct bnxt_vf_info *vf = &bp->vf; 15586 bool strict_approval = true; 15587 15588 if (is_valid_ether_addr(vf->mac_addr)) { 15589 /* overwrite netdev dev_addr with admin VF MAC */ 15590 eth_hw_addr_set(bp->dev, vf->mac_addr); 15591 /* Older PF driver or firmware may not approve this 15592 * correctly. 15593 */ 15594 strict_approval = false; 15595 } else { 15596 eth_hw_addr_random(bp->dev); 15597 } 15598 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval); 15599 #endif 15600 } 15601 return rc; 15602 } 15603 15604 static void bnxt_vpd_read_info(struct bnxt *bp) 15605 { 15606 struct pci_dev *pdev = bp->pdev; 15607 unsigned int vpd_size, kw_len; 15608 int pos, size; 15609 u8 *vpd_data; 15610 15611 vpd_data = pci_vpd_alloc(pdev, &vpd_size); 15612 if (IS_ERR(vpd_data)) { 15613 pci_warn(pdev, "Unable to read VPD\n"); 15614 return; 15615 } 15616 15617 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 15618 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 15619 if (pos < 0) 15620 goto read_sn; 15621 15622 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 15623 memcpy(bp->board_partno, &vpd_data[pos], size); 15624 15625 read_sn: 15626 pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 15627 PCI_VPD_RO_KEYWORD_SERIALNO, 15628 &kw_len); 15629 if (pos < 0) 15630 goto exit; 15631 15632 size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1); 15633 memcpy(bp->board_serialno, &vpd_data[pos], size); 15634 exit: 15635 kfree(vpd_data); 15636 } 15637 15638 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 15639 { 15640 struct pci_dev *pdev = bp->pdev; 15641 u64 qword; 15642 15643 qword = pci_get_dsn(pdev); 15644 if (!qword) { 15645 netdev_info(bp->dev, "Unable to read adapter's DSN\n"); 15646 return -EOPNOTSUPP; 15647 } 15648 15649 put_unaligned_le64(qword, dsn); 15650 15651 bp->flags |= BNXT_FLAG_DSN_VALID; 15652 return 0; 15653 } 15654 15655 static int bnxt_map_db_bar(struct bnxt *bp) 15656 { 15657 if (!bp->db_size) 15658 return -ENODEV; 15659 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size); 15660 if (!bp->bar1) 15661 return -ENOMEM; 15662 return 0; 15663 } 15664 15665 void bnxt_print_device_info(struct bnxt *bp) 15666 { 15667 netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n", 15668 board_info[bp->board_idx].name, 15669 (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr); 15670 15671 pcie_print_link_status(bp->pdev); 15672 } 15673 15674 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 15675 { 15676 struct bnxt_hw_resc *hw_resc; 15677 struct net_device *dev; 15678 struct bnxt *bp; 15679 int rc, max_irqs; 15680 15681 if (pci_is_bridge(pdev)) 15682 return -ENODEV; 15683 15684 /* Clear any pending DMA transactions from crash kernel 15685 * while loading driver in capture kernel. 15686 */ 15687 if (is_kdump_kernel()) { 15688 pci_clear_master(pdev); 15689 pcie_flr(pdev); 15690 } 15691 15692 max_irqs = bnxt_get_max_irq(pdev); 15693 dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE, 15694 max_irqs); 15695 if (!dev) 15696 return -ENOMEM; 15697 15698 bp = netdev_priv(dev); 15699 bp->board_idx = ent->driver_data; 15700 bp->msg_enable = BNXT_DEF_MSG_ENABLE; 15701 bnxt_set_max_func_irqs(bp, max_irqs); 15702 15703 if (bnxt_vf_pciid(bp->board_idx)) 15704 bp->flags |= BNXT_FLAG_VF; 15705 15706 /* No devlink port registration in case of a VF */ 15707 if (BNXT_PF(bp)) 15708 SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port); 15709 15710 if (pdev->msix_cap) 15711 bp->flags |= BNXT_FLAG_MSIX_CAP; 15712 15713 rc = bnxt_init_board(pdev, dev); 15714 if (rc < 0) 15715 goto init_err_free; 15716 15717 dev->netdev_ops = &bnxt_netdev_ops; 15718 dev->stat_ops = &bnxt_stat_ops; 15719 dev->watchdog_timeo = BNXT_TX_TIMEOUT; 15720 dev->ethtool_ops = &bnxt_ethtool_ops; 15721 pci_set_drvdata(pdev, dev); 15722 15723 rc = bnxt_alloc_hwrm_resources(bp); 15724 if (rc) 15725 goto init_err_pci_clean; 15726 15727 mutex_init(&bp->hwrm_cmd_lock); 15728 mutex_init(&bp->link_lock); 15729 15730 rc = bnxt_fw_init_one_p1(bp); 15731 if (rc) 15732 goto init_err_pci_clean; 15733 15734 if (BNXT_PF(bp)) 15735 bnxt_vpd_read_info(bp); 15736 15737 if (BNXT_CHIP_P5_PLUS(bp)) { 15738 bp->flags |= BNXT_FLAG_CHIP_P5_PLUS; 15739 if (BNXT_CHIP_P7(bp)) 15740 bp->flags |= BNXT_FLAG_CHIP_P7; 15741 } 15742 15743 rc = bnxt_alloc_rss_indir_tbl(bp); 15744 if (rc) 15745 goto init_err_pci_clean; 15746 15747 rc = bnxt_fw_init_one_p2(bp); 15748 if (rc) 15749 goto init_err_pci_clean; 15750 15751 rc = bnxt_map_db_bar(bp); 15752 if (rc) { 15753 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n", 15754 rc); 15755 goto init_err_pci_clean; 15756 } 15757 15758 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 15759 NETIF_F_TSO | NETIF_F_TSO6 | 15760 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 15761 NETIF_F_GSO_IPXIP4 | 15762 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 15763 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH | 15764 NETIF_F_RXCSUM | NETIF_F_GRO; 15765 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 15766 dev->hw_features |= NETIF_F_GSO_UDP_L4; 15767 15768 if (BNXT_SUPPORTS_TPA(bp)) 15769 dev->hw_features |= NETIF_F_LRO; 15770 15771 dev->hw_enc_features = 15772 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG | 15773 NETIF_F_TSO | NETIF_F_TSO6 | 15774 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE | 15775 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM | 15776 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL; 15777 if (bp->flags & BNXT_FLAG_UDP_GSO_CAP) 15778 dev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 15779 if (bp->flags & BNXT_FLAG_CHIP_P7) 15780 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7; 15781 else 15782 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels; 15783 15784 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM | 15785 NETIF_F_GSO_GRE_CSUM; 15786 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA; 15787 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP) 15788 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX; 15789 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT) 15790 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX; 15791 if (BNXT_SUPPORTS_TPA(bp)) 15792 dev->hw_features |= NETIF_F_GRO_HW; 15793 dev->features |= dev->hw_features | NETIF_F_HIGHDMA; 15794 if (dev->features & NETIF_F_GRO_HW) 15795 dev->features &= ~NETIF_F_LRO; 15796 dev->priv_flags |= IFF_UNICAST_FLT; 15797 15798 netif_set_tso_max_size(dev, GSO_MAX_SIZE); 15799 if (bp->tso_max_segs) 15800 netif_set_tso_max_segs(dev, bp->tso_max_segs); 15801 15802 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 15803 NETDEV_XDP_ACT_RX_SG; 15804 15805 #ifdef CONFIG_BNXT_SRIOV 15806 init_waitqueue_head(&bp->sriov_cfg_wait); 15807 #endif 15808 if (BNXT_SUPPORTS_TPA(bp)) { 15809 bp->gro_func = bnxt_gro_func_5730x; 15810 if (BNXT_CHIP_P4(bp)) 15811 bp->gro_func = bnxt_gro_func_5731x; 15812 else if (BNXT_CHIP_P5_PLUS(bp)) 15813 bp->gro_func = bnxt_gro_func_5750x; 15814 } 15815 if (!BNXT_CHIP_P4_PLUS(bp)) 15816 bp->flags |= BNXT_FLAG_DOUBLE_DB; 15817 15818 rc = bnxt_init_mac_addr(bp); 15819 if (rc) { 15820 dev_err(&pdev->dev, "Unable to initialize mac address.\n"); 15821 rc = -EADDRNOTAVAIL; 15822 goto init_err_pci_clean; 15823 } 15824 15825 if (BNXT_PF(bp)) { 15826 /* Read the adapter's DSN to use as the eswitch switch_id */ 15827 rc = bnxt_pcie_dsn_get(bp, bp->dsn); 15828 } 15829 15830 /* MTU range: 60 - FW defined max */ 15831 dev->min_mtu = ETH_ZLEN; 15832 dev->max_mtu = bp->max_mtu; 15833 15834 rc = bnxt_probe_phy(bp, true); 15835 if (rc) 15836 goto init_err_pci_clean; 15837 15838 hw_resc = &bp->hw_resc; 15839 bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows + 15840 BNXT_L2_FLTR_MAX_FLTR; 15841 /* Older firmware may not report these filters properly */ 15842 if (bp->max_fltr < BNXT_MAX_FLTR) 15843 bp->max_fltr = BNXT_MAX_FLTR; 15844 bnxt_init_l2_fltr_tbl(bp); 15845 bnxt_set_rx_skb_mode(bp, false); 15846 bnxt_set_tpa_flags(bp); 15847 bnxt_set_ring_params(bp); 15848 bnxt_rdma_aux_device_init(bp); 15849 rc = bnxt_set_dflt_rings(bp, true); 15850 if (rc) { 15851 if (BNXT_VF(bp) && rc == -ENODEV) { 15852 netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n"); 15853 } else { 15854 netdev_err(bp->dev, "Not enough rings available.\n"); 15855 rc = -ENOMEM; 15856 } 15857 goto init_err_pci_clean; 15858 } 15859 15860 bnxt_fw_init_one_p3(bp); 15861 15862 bnxt_init_dflt_coal(bp); 15863 15864 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX) 15865 bp->flags |= BNXT_FLAG_STRIP_VLAN; 15866 15867 rc = bnxt_init_int_mode(bp); 15868 if (rc) 15869 goto init_err_pci_clean; 15870 15871 /* No TC has been set yet and rings may have been trimmed due to 15872 * limited MSIX, so we re-initialize the TX rings per TC. 15873 */ 15874 bp->tx_nr_rings_per_tc = bp->tx_nr_rings; 15875 15876 if (BNXT_PF(bp)) { 15877 if (!bnxt_pf_wq) { 15878 bnxt_pf_wq = 15879 create_singlethread_workqueue("bnxt_pf_wq"); 15880 if (!bnxt_pf_wq) { 15881 dev_err(&pdev->dev, "Unable to create workqueue.\n"); 15882 rc = -ENOMEM; 15883 goto init_err_pci_clean; 15884 } 15885 } 15886 rc = bnxt_init_tc(bp); 15887 if (rc) 15888 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n", 15889 rc); 15890 } 15891 15892 bnxt_inv_fw_health_reg(bp); 15893 rc = bnxt_dl_register(bp); 15894 if (rc) 15895 goto init_err_dl; 15896 15897 INIT_LIST_HEAD(&bp->usr_fltr_list); 15898 15899 if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) 15900 bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX; 15901 if (BNXT_SUPPORTS_QUEUE_API(bp)) 15902 dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops; 15903 15904 rc = register_netdev(dev); 15905 if (rc) 15906 goto init_err_cleanup; 15907 15908 bnxt_dl_fw_reporters_create(bp); 15909 15910 bnxt_rdma_aux_device_add(bp); 15911 15912 bnxt_print_device_info(bp); 15913 15914 pci_save_state(pdev); 15915 15916 return 0; 15917 init_err_cleanup: 15918 bnxt_rdma_aux_device_uninit(bp); 15919 bnxt_dl_unregister(bp); 15920 init_err_dl: 15921 bnxt_shutdown_tc(bp); 15922 bnxt_clear_int_mode(bp); 15923 15924 init_err_pci_clean: 15925 bnxt_hwrm_func_drv_unrgtr(bp); 15926 bnxt_free_hwrm_resources(bp); 15927 bnxt_hwmon_uninit(bp); 15928 bnxt_ethtool_free(bp); 15929 bnxt_ptp_clear(bp); 15930 kfree(bp->ptp_cfg); 15931 bp->ptp_cfg = NULL; 15932 kfree(bp->fw_health); 15933 bp->fw_health = NULL; 15934 bnxt_cleanup_pci(bp); 15935 bnxt_free_ctx_mem(bp); 15936 kfree(bp->rss_indir_tbl); 15937 bp->rss_indir_tbl = NULL; 15938 15939 init_err_free: 15940 free_netdev(dev); 15941 return rc; 15942 } 15943 15944 static void bnxt_shutdown(struct pci_dev *pdev) 15945 { 15946 struct net_device *dev = pci_get_drvdata(pdev); 15947 struct bnxt *bp; 15948 15949 if (!dev) 15950 return; 15951 15952 rtnl_lock(); 15953 bp = netdev_priv(dev); 15954 if (!bp) 15955 goto shutdown_exit; 15956 15957 if (netif_running(dev)) 15958 dev_close(dev); 15959 15960 bnxt_clear_int_mode(bp); 15961 pci_disable_device(pdev); 15962 15963 if (system_state == SYSTEM_POWER_OFF) { 15964 pci_wake_from_d3(pdev, bp->wol); 15965 pci_set_power_state(pdev, PCI_D3hot); 15966 } 15967 15968 shutdown_exit: 15969 rtnl_unlock(); 15970 } 15971 15972 #ifdef CONFIG_PM_SLEEP 15973 static int bnxt_suspend(struct device *device) 15974 { 15975 struct net_device *dev = dev_get_drvdata(device); 15976 struct bnxt *bp = netdev_priv(dev); 15977 int rc = 0; 15978 15979 bnxt_ulp_stop(bp); 15980 15981 rtnl_lock(); 15982 if (netif_running(dev)) { 15983 netif_device_detach(dev); 15984 rc = bnxt_close(dev); 15985 } 15986 bnxt_hwrm_func_drv_unrgtr(bp); 15987 pci_disable_device(bp->pdev); 15988 bnxt_free_ctx_mem(bp); 15989 rtnl_unlock(); 15990 return rc; 15991 } 15992 15993 static int bnxt_resume(struct device *device) 15994 { 15995 struct net_device *dev = dev_get_drvdata(device); 15996 struct bnxt *bp = netdev_priv(dev); 15997 int rc = 0; 15998 15999 rtnl_lock(); 16000 rc = pci_enable_device(bp->pdev); 16001 if (rc) { 16002 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n", 16003 rc); 16004 goto resume_exit; 16005 } 16006 pci_set_master(bp->pdev); 16007 if (bnxt_hwrm_ver_get(bp)) { 16008 rc = -ENODEV; 16009 goto resume_exit; 16010 } 16011 rc = bnxt_hwrm_func_reset(bp); 16012 if (rc) { 16013 rc = -EBUSY; 16014 goto resume_exit; 16015 } 16016 16017 rc = bnxt_hwrm_func_qcaps(bp); 16018 if (rc) 16019 goto resume_exit; 16020 16021 bnxt_clear_reservations(bp, true); 16022 16023 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) { 16024 rc = -ENODEV; 16025 goto resume_exit; 16026 } 16027 16028 bnxt_get_wol_settings(bp); 16029 if (netif_running(dev)) { 16030 rc = bnxt_open(dev); 16031 if (!rc) 16032 netif_device_attach(dev); 16033 } 16034 16035 resume_exit: 16036 rtnl_unlock(); 16037 bnxt_ulp_start(bp, rc); 16038 if (!rc) 16039 bnxt_reenable_sriov(bp); 16040 return rc; 16041 } 16042 16043 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume); 16044 #define BNXT_PM_OPS (&bnxt_pm_ops) 16045 16046 #else 16047 16048 #define BNXT_PM_OPS NULL 16049 16050 #endif /* CONFIG_PM_SLEEP */ 16051 16052 /** 16053 * bnxt_io_error_detected - called when PCI error is detected 16054 * @pdev: Pointer to PCI device 16055 * @state: The current pci connection state 16056 * 16057 * This function is called after a PCI bus error affecting 16058 * this device has been detected. 16059 */ 16060 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev, 16061 pci_channel_state_t state) 16062 { 16063 struct net_device *netdev = pci_get_drvdata(pdev); 16064 struct bnxt *bp = netdev_priv(netdev); 16065 bool abort = false; 16066 16067 netdev_info(netdev, "PCI I/O error detected\n"); 16068 16069 bnxt_ulp_stop(bp); 16070 16071 rtnl_lock(); 16072 netif_device_detach(netdev); 16073 16074 if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) { 16075 netdev_err(bp->dev, "Firmware reset already in progress\n"); 16076 abort = true; 16077 } 16078 16079 if (abort || state == pci_channel_io_perm_failure) { 16080 rtnl_unlock(); 16081 return PCI_ERS_RESULT_DISCONNECT; 16082 } 16083 16084 /* Link is not reliable anymore if state is pci_channel_io_frozen 16085 * so we disable bus master to prevent any potential bad DMAs before 16086 * freeing kernel memory. 16087 */ 16088 if (state == pci_channel_io_frozen) { 16089 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state); 16090 bnxt_fw_fatal_close(bp); 16091 } 16092 16093 if (netif_running(netdev)) 16094 __bnxt_close_nic(bp, true, true); 16095 16096 if (pci_is_enabled(pdev)) 16097 pci_disable_device(pdev); 16098 bnxt_free_ctx_mem(bp); 16099 rtnl_unlock(); 16100 16101 /* Request a slot slot reset. */ 16102 return PCI_ERS_RESULT_NEED_RESET; 16103 } 16104 16105 /** 16106 * bnxt_io_slot_reset - called after the pci bus has been reset. 16107 * @pdev: Pointer to PCI device 16108 * 16109 * Restart the card from scratch, as if from a cold-boot. 16110 * At this point, the card has exprienced a hard reset, 16111 * followed by fixups by BIOS, and has its config space 16112 * set up identically to what it was at cold boot. 16113 */ 16114 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev) 16115 { 16116 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT; 16117 struct net_device *netdev = pci_get_drvdata(pdev); 16118 struct bnxt *bp = netdev_priv(netdev); 16119 int retry = 0; 16120 int err = 0; 16121 int off; 16122 16123 netdev_info(bp->dev, "PCI Slot Reset\n"); 16124 16125 if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && 16126 test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) 16127 msleep(900); 16128 16129 rtnl_lock(); 16130 16131 if (pci_enable_device(pdev)) { 16132 dev_err(&pdev->dev, 16133 "Cannot re-enable PCI device after reset.\n"); 16134 } else { 16135 pci_set_master(pdev); 16136 /* Upon fatal error, our device internal logic that latches to 16137 * BAR value is getting reset and will restore only upon 16138 * rewritting the BARs. 16139 * 16140 * As pci_restore_state() does not re-write the BARs if the 16141 * value is same as saved value earlier, driver needs to 16142 * write the BARs to 0 to force restore, in case of fatal error. 16143 */ 16144 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, 16145 &bp->state)) { 16146 for (off = PCI_BASE_ADDRESS_0; 16147 off <= PCI_BASE_ADDRESS_5; off += 4) 16148 pci_write_config_dword(bp->pdev, off, 0); 16149 } 16150 pci_restore_state(pdev); 16151 pci_save_state(pdev); 16152 16153 bnxt_inv_fw_health_reg(bp); 16154 bnxt_try_map_fw_health_reg(bp); 16155 16156 /* In some PCIe AER scenarios, firmware may take up to 16157 * 10 seconds to become ready in the worst case. 16158 */ 16159 do { 16160 err = bnxt_try_recover_fw(bp); 16161 if (!err) 16162 break; 16163 retry++; 16164 } while (retry < BNXT_FW_SLOT_RESET_RETRY); 16165 16166 if (err) { 16167 dev_err(&pdev->dev, "Firmware not ready\n"); 16168 goto reset_exit; 16169 } 16170 16171 err = bnxt_hwrm_func_reset(bp); 16172 if (!err) 16173 result = PCI_ERS_RESULT_RECOVERED; 16174 16175 bnxt_ulp_irq_stop(bp); 16176 bnxt_clear_int_mode(bp); 16177 err = bnxt_init_int_mode(bp); 16178 bnxt_ulp_irq_restart(bp, err); 16179 } 16180 16181 reset_exit: 16182 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state); 16183 bnxt_clear_reservations(bp, true); 16184 rtnl_unlock(); 16185 16186 return result; 16187 } 16188 16189 /** 16190 * bnxt_io_resume - called when traffic can start flowing again. 16191 * @pdev: Pointer to PCI device 16192 * 16193 * This callback is called when the error recovery driver tells 16194 * us that its OK to resume normal operation. 16195 */ 16196 static void bnxt_io_resume(struct pci_dev *pdev) 16197 { 16198 struct net_device *netdev = pci_get_drvdata(pdev); 16199 struct bnxt *bp = netdev_priv(netdev); 16200 int err; 16201 16202 netdev_info(bp->dev, "PCI Slot Resume\n"); 16203 rtnl_lock(); 16204 16205 err = bnxt_hwrm_func_qcaps(bp); 16206 if (!err && netif_running(netdev)) 16207 err = bnxt_open(netdev); 16208 16209 if (!err) 16210 netif_device_attach(netdev); 16211 16212 rtnl_unlock(); 16213 bnxt_ulp_start(bp, err); 16214 if (!err) 16215 bnxt_reenable_sriov(bp); 16216 } 16217 16218 static const struct pci_error_handlers bnxt_err_handler = { 16219 .error_detected = bnxt_io_error_detected, 16220 .slot_reset = bnxt_io_slot_reset, 16221 .resume = bnxt_io_resume 16222 }; 16223 16224 static struct pci_driver bnxt_pci_driver = { 16225 .name = DRV_MODULE_NAME, 16226 .id_table = bnxt_pci_tbl, 16227 .probe = bnxt_init_one, 16228 .remove = bnxt_remove_one, 16229 .shutdown = bnxt_shutdown, 16230 .driver.pm = BNXT_PM_OPS, 16231 .err_handler = &bnxt_err_handler, 16232 #if defined(CONFIG_BNXT_SRIOV) 16233 .sriov_configure = bnxt_sriov_configure, 16234 #endif 16235 }; 16236 16237 static int __init bnxt_init(void) 16238 { 16239 int err; 16240 16241 bnxt_debug_init(); 16242 err = pci_register_driver(&bnxt_pci_driver); 16243 if (err) { 16244 bnxt_debug_exit(); 16245 return err; 16246 } 16247 16248 return 0; 16249 } 16250 16251 static void __exit bnxt_exit(void) 16252 { 16253 pci_unregister_driver(&bnxt_pci_driver); 16254 if (bnxt_pf_wq) 16255 destroy_workqueue(bnxt_pf_wq); 16256 bnxt_debug_exit(); 16257 } 16258 16259 module_init(bnxt_init); 16260 module_exit(bnxt_exit); 16261