1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3
4 #ifndef _FBNIC_H_
5 #define _FBNIC_H_
6
7 #include <linux/interrupt.h>
8 #include <linux/io.h>
9 #include <linux/ptp_clock_kernel.h>
10 #include <linux/types.h>
11 #include <linux/workqueue.h>
12
13 #include "fbnic_csr.h"
14 #include "fbnic_fw.h"
15 #include "fbnic_hw_stats.h"
16 #include "fbnic_mac.h"
17 #include "fbnic_rpc.h"
18
19 struct fbnic_dev {
20 struct device *dev;
21 struct net_device *netdev;
22 struct dentry *dbg_fbd;
23
24 u32 __iomem *uc_addr0;
25 u32 __iomem *uc_addr4;
26 const struct fbnic_mac *mac;
27 unsigned int fw_msix_vector;
28 unsigned int pcs_msix_vector;
29 unsigned short num_irqs;
30
31 struct delayed_work service_task;
32
33 struct fbnic_fw_mbx mbx[FBNIC_IPC_MBX_INDICES];
34 struct fbnic_fw_cap fw_cap;
35 /* Lock protecting Tx Mailbox queue to prevent possible races */
36 spinlock_t fw_tx_lock;
37
38 unsigned long last_heartbeat_request;
39 unsigned long last_heartbeat_response;
40 u8 fw_heartbeat_enabled;
41
42 u64 dsn;
43 u32 mps;
44 u32 readrq;
45
46 /* Local copy of the devices TCAM */
47 struct fbnic_act_tcam act_tcam[FBNIC_RPC_TCAM_ACT_NUM_ENTRIES];
48 struct fbnic_mac_addr mac_addr[FBNIC_RPC_TCAM_MACDA_NUM_ENTRIES];
49 u8 mac_addr_boundary;
50 u8 tce_tcam_last;
51
52 /* Number of TCQs/RCQs available on hardware */
53 u16 max_num_queues;
54
55 /* Lock protecting writes to @time_high, @time_offset of fbnic_netdev,
56 * and the HW time CSR machinery.
57 */
58 spinlock_t time_lock;
59 /* Externally accessible PTP clock, may be NULL */
60 struct ptp_clock *ptp;
61 struct ptp_clock_info ptp_info;
62 /* Last @time_high refresh time in jiffies (to catch stalls) */
63 unsigned long last_read;
64
65 /* Local copy of hardware statistics */
66 struct fbnic_hw_stats hw_stats;
67 };
68
69 /* Reserve entry 0 in the MSI-X "others" array until we have filled all
70 * 32 of the possible interrupt slots. By doing this we can avoid any
71 * potential conflicts should we need to enable one of the debug interrupt
72 * causes later.
73 */
74 enum {
75 FBNIC_FW_MSIX_ENTRY,
76 FBNIC_PCS_MSIX_ENTRY,
77 FBNIC_NON_NAPI_VECTORS
78 };
79
fbnic_present(struct fbnic_dev * fbd)80 static inline bool fbnic_present(struct fbnic_dev *fbd)
81 {
82 return !!READ_ONCE(fbd->uc_addr0);
83 }
84
fbnic_wr32(struct fbnic_dev * fbd,u32 reg,u32 val)85 static inline void fbnic_wr32(struct fbnic_dev *fbd, u32 reg, u32 val)
86 {
87 u32 __iomem *csr = READ_ONCE(fbd->uc_addr0);
88
89 if (csr)
90 writel(val, csr + reg);
91 }
92
93 u32 fbnic_rd32(struct fbnic_dev *fbd, u32 reg);
94
fbnic_wrfl(struct fbnic_dev * fbd)95 static inline void fbnic_wrfl(struct fbnic_dev *fbd)
96 {
97 fbnic_rd32(fbd, FBNIC_MASTER_SPARE_0);
98 }
99
100 static inline void
fbnic_rmw32(struct fbnic_dev * fbd,u32 reg,u32 mask,u32 val)101 fbnic_rmw32(struct fbnic_dev *fbd, u32 reg, u32 mask, u32 val)
102 {
103 u32 v;
104
105 v = fbnic_rd32(fbd, reg);
106 v &= ~mask;
107 v |= val;
108 fbnic_wr32(fbd, reg, v);
109 }
110
111 #define wr32(_f, _r, _v) fbnic_wr32(_f, _r, _v)
112 #define rd32(_f, _r) fbnic_rd32(_f, _r)
113 #define wrfl(_f) fbnic_wrfl(_f)
114
115 bool fbnic_fw_present(struct fbnic_dev *fbd);
116 u32 fbnic_fw_rd32(struct fbnic_dev *fbd, u32 reg);
117 void fbnic_fw_wr32(struct fbnic_dev *fbd, u32 reg, u32 val);
118
119 #define fw_rd32(_f, _r) fbnic_fw_rd32(_f, _r)
120 #define fw_wr32(_f, _r, _v) fbnic_fw_wr32(_f, _r, _v)
121 #define fw_wrfl(_f) fbnic_fw_rd32(_f, FBNIC_FW_ZERO_REG)
122
fbnic_bmc_present(struct fbnic_dev * fbd)123 static inline bool fbnic_bmc_present(struct fbnic_dev *fbd)
124 {
125 return fbd->fw_cap.bmc_present;
126 }
127
fbnic_init_failure(struct fbnic_dev * fbd)128 static inline bool fbnic_init_failure(struct fbnic_dev *fbd)
129 {
130 return !fbd->netdev;
131 }
132
133 extern char fbnic_driver_name[];
134
135 void fbnic_devlink_free(struct fbnic_dev *fbd);
136 struct fbnic_dev *fbnic_devlink_alloc(struct pci_dev *pdev);
137 void fbnic_devlink_register(struct fbnic_dev *fbd);
138 void fbnic_devlink_unregister(struct fbnic_dev *fbd);
139
140 int fbnic_fw_enable_mbx(struct fbnic_dev *fbd);
141 void fbnic_fw_disable_mbx(struct fbnic_dev *fbd);
142
143 int fbnic_pcs_irq_enable(struct fbnic_dev *fbd);
144 void fbnic_pcs_irq_disable(struct fbnic_dev *fbd);
145
146 int fbnic_request_irq(struct fbnic_dev *dev, int nr, irq_handler_t handler,
147 unsigned long flags, const char *name, void *data);
148 void fbnic_free_irq(struct fbnic_dev *dev, int nr, void *data);
149 void fbnic_free_irqs(struct fbnic_dev *fbd);
150 int fbnic_alloc_irqs(struct fbnic_dev *fbd);
151
152 void fbnic_get_fw_ver_commit_str(struct fbnic_dev *fbd, char *fw_version,
153 const size_t str_sz);
154
155 void fbnic_dbg_fbd_init(struct fbnic_dev *fbd);
156 void fbnic_dbg_fbd_exit(struct fbnic_dev *fbd);
157 void fbnic_dbg_init(void);
158 void fbnic_dbg_exit(void);
159
160 void fbnic_csr_get_regs(struct fbnic_dev *fbd, u32 *data, u32 *regs_version);
161 int fbnic_csr_regs_len(struct fbnic_dev *fbd);
162
163 enum fbnic_boards {
164 fbnic_board_asic
165 };
166
167 struct fbnic_info {
168 unsigned int max_num_queues;
169 unsigned int bar_mask;
170 };
171
172 #endif /* _FBNIC_H_ */
173