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