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