1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2014 Intel Corporation.
6  *
7  * Intel SCIF driver.
8  */
9 #ifndef SCIF_MAIN_H
10 #define SCIF_MAIN_H
11 
12 #include <linux/sched/signal.h>
13 #include <linux/pci.h>
14 #include <linux/miscdevice.h>
15 #include <linux/dmaengine.h>
16 #include <linux/iova.h>
17 #include <linux/anon_inodes.h>
18 #include <linux/file.h>
19 #include <linux/vmalloc.h>
20 #include <linux/scif.h>
21 #include "../common/mic_dev.h"
22 
23 #define SCIF_MGMT_NODE 0
24 #define SCIF_DEFAULT_WATCHDOG_TO 30
25 #define SCIF_NODE_ACCEPT_TIMEOUT (3 * HZ)
26 #define SCIF_NODE_ALIVE_TIMEOUT (SCIF_DEFAULT_WATCHDOG_TO * HZ)
27 #define SCIF_RMA_TEMP_CACHE_LIMIT 0x20000
28 
29 /*
30  * Generic state used for certain node QP message exchanges
31  * like Unregister, Alloc etc.
32  */
33 enum scif_msg_state {
34 	OP_IDLE = 1,
35 	OP_IN_PROGRESS,
36 	OP_COMPLETED,
37 	OP_FAILED
38 };
39 
40 /*
41  * struct scif_info - Global SCIF information
42  *
43  * @nodeid: Node ID this node is to others
44  * @maxid: Max known node ID
45  * @total: Total number of SCIF nodes
46  * @nr_zombies: number of zombie endpoints
47  * @eplock: Lock to synchronize listening, zombie endpoint lists
48  * @connlock: Lock to synchronize connected and disconnected lists
49  * @nb_connect_lock: Synchronize non blocking connect operations
50  * @port_lock: Synchronize access to SCIF ports
51  * @uaccept: List of user acceptreq waiting for acceptreg
52  * @listen: List of listening end points
53  * @zombie: List of zombie end points with pending RMA's
54  * @connected: List of end points in connected state
55  * @disconnected: List of end points in disconnected state
56  * @nb_connect_list: List for non blocking connections
57  * @misc_work: miscellaneous SCIF tasks
58  * @conflock: Lock to synchronize SCIF node configuration changes
59  * @en_msg_log: Enable debug message logging
60  * @p2p_enable: Enable P2P SCIF network
61  * @mdev: The MISC device
62  * @conn_work: Work for workqueue handling all connections
63  * @exitwq: Wait queue for waiting for an EXIT node QP message response
64  * @loopb_dev: Dummy SCIF device used for loopback
65  * @loopb_wq: Workqueue used for handling loopback messages
66  * @loopb_wqname[16]: Name of loopback workqueue
67  * @loopb_work: Used for submitting work to loopb_wq
68  * @loopb_recv_q: List of messages received on the loopb_wq
69  * @card_initiated_exit: set when the card has initiated the exit
70  * @rmalock: Synchronize access to RMA operations
71  * @fencelock: Synchronize access to list of remote fences requested.
72  * @rma: List of temporary registered windows to be destroyed.
73  * @rma_tc: List of temporary registered & cached Windows to be destroyed
74  * @fence: List of remote fence requests
75  * @mmu_notif_work: Work for registration caching MMU notifier workqueue
76  * @mmu_notif_cleanup: List of temporary cached windows for reg cache
77  * @rma_tc_limit: RMA temporary cache limit
78  */
79 struct scif_info {
80 	u8 nodeid;
81 	u8 maxid;
82 	u8 total;
83 	u32 nr_zombies;
84 	struct mutex eplock;
85 	struct mutex connlock;
86 	spinlock_t nb_connect_lock;
87 	spinlock_t port_lock;
88 	struct list_head uaccept;
89 	struct list_head listen;
90 	struct list_head zombie;
91 	struct list_head connected;
92 	struct list_head disconnected;
93 	struct list_head nb_connect_list;
94 	struct work_struct misc_work;
95 	struct mutex conflock;
96 	u8 en_msg_log;
97 	u8 p2p_enable;
98 	struct miscdevice mdev;
99 	struct work_struct conn_work;
100 	wait_queue_head_t exitwq;
101 	struct scif_dev *loopb_dev;
102 	struct workqueue_struct *loopb_wq;
103 	char loopb_wqname[16];
104 	struct work_struct loopb_work;
105 	struct list_head loopb_recv_q;
106 	bool card_initiated_exit;
107 	spinlock_t rmalock;
108 	struct mutex fencelock;
109 	struct list_head rma;
110 	struct list_head rma_tc;
111 	struct list_head fence;
112 	struct work_struct mmu_notif_work;
113 	struct list_head mmu_notif_cleanup;
114 	unsigned long rma_tc_limit;
115 };
116 
117 /*
118  * struct scif_p2p_info - SCIF mapping information used for P2P
119  *
120  * @ppi_peer_id - SCIF peer node id
121  * @ppi_sg - Scatter list for bar information (One for mmio and one for aper)
122  * @sg_nentries - Number of entries in the scatterlist
123  * @ppi_da: DMA address for MMIO and APER bars
124  * @ppi_len: Length of MMIO and APER bars
125  * @ppi_list: Link in list of mapping information
126  */
127 struct scif_p2p_info {
128 	u8 ppi_peer_id;
129 	struct scatterlist *ppi_sg[2];
130 	u64 sg_nentries[2];
131 	dma_addr_t ppi_da[2];
132 	u64 ppi_len[2];
133 #define SCIF_PPI_MMIO 0
134 #define SCIF_PPI_APER 1
135 	struct list_head ppi_list;
136 };
137 
138 /*
139  * struct scif_dev - SCIF remote device specific fields
140  *
141  * @node: Node id
142  * @p2p: List of P2P mapping information
143  * @qpairs: The node queue pair for exchanging control messages
144  * @intr_wq: Workqueue for handling Node QP messages
145  * @intr_wqname: Name of node QP workqueue for handling interrupts
146  * @intr_bh: Used for submitting work to intr_wq
147  * @lock: Lock used for synchronizing access to the scif device
148  * @sdev: SCIF hardware device on the SCIF hardware bus
149  * @db: doorbell the peer will trigger to generate an interrupt on self
150  * @rdb: Doorbell to trigger on the peer to generate an interrupt on the peer
151  * @cookie: Cookie received while registering the interrupt handler
152  * @peer_add_work: Work for handling device_add for peer devices
153  * @p2p_dwork: Delayed work to enable polling for P2P state
154  * @qp_dwork: Delayed work for enabling polling for remote QP information
155  * @p2p_retry: Number of times to retry polling of P2P state
156  * @base_addr: P2P aperture bar base address
157  * @mic_mw mmio: The peer MMIO information used for P2P
158  * @spdev: SCIF peer device on the SCIF peer bus
159  * @node_remove_ack_pending: True if a node_remove_ack is pending
160  * @exit_ack_pending: true if an exit_ack is pending
161  * @disconn_wq: Used while waiting for a node remove response
162  * @disconn_rescnt: Keeps track of number of node remove requests sent
163  * @exit: Status of exit message
164  * @qp_dma_addr: Queue pair DMA address passed to the peer
165  * @dma_ch_idx: Round robin index for DMA channels
166  * @signal_pool: DMA pool used for scheduling scif_fence_signal DMA's
167 */
168 struct scif_dev {
169 	u8 node;
170 	struct list_head p2p;
171 	struct scif_qp *qpairs;
172 	struct workqueue_struct *intr_wq;
173 	char intr_wqname[16];
174 	struct work_struct intr_bh;
175 	struct mutex lock;
176 	struct scif_hw_dev *sdev;
177 	int db;
178 	int rdb;
179 	struct mic_irq *cookie;
180 	struct work_struct peer_add_work;
181 	struct delayed_work p2p_dwork;
182 	struct delayed_work qp_dwork;
183 	int p2p_retry;
184 	dma_addr_t base_addr;
185 	struct mic_mw mmio;
186 	struct scif_peer_dev __rcu *spdev;
187 	bool node_remove_ack_pending;
188 	bool exit_ack_pending;
189 	wait_queue_head_t disconn_wq;
190 	atomic_t disconn_rescnt;
191 	enum scif_msg_state exit;
192 	dma_addr_t qp_dma_addr;
193 	int dma_ch_idx;
194 	struct dma_pool *signal_pool;
195 };
196 
197 extern bool scif_reg_cache_enable;
198 extern bool scif_ulimit_check;
199 extern struct scif_info scif_info;
200 extern struct idr scif_ports;
201 extern struct bus_type scif_peer_bus;
202 extern struct scif_dev *scif_dev;
203 extern const struct file_operations scif_fops;
204 extern const struct file_operations scif_anon_fops;
205 
206 /* Size of the RB for the Node QP */
207 #define SCIF_NODE_QP_SIZE 0x10000
208 
209 #include "scif_nodeqp.h"
210 #include "scif_rma.h"
211 #include "scif_rma_list.h"
212 
213 /*
214  * scifdev_self:
215  * @dev: The remote SCIF Device
216  *
217  * Returns true if the SCIF Device passed is the self aka Loopback SCIF device.
218  */
scifdev_self(struct scif_dev * dev)219 static inline int scifdev_self(struct scif_dev *dev)
220 {
221 	return dev->node == scif_info.nodeid;
222 }
223 
scif_is_mgmt_node(void)224 static inline bool scif_is_mgmt_node(void)
225 {
226 	return !scif_info.nodeid;
227 }
228 
229 /*
230  * scifdev_is_p2p:
231  * @dev: The remote SCIF Device
232  *
233  * Returns true if the SCIF Device is a MIC Peer to Peer SCIF device.
234  */
scifdev_is_p2p(struct scif_dev * dev)235 static inline bool scifdev_is_p2p(struct scif_dev *dev)
236 {
237 	if (scif_is_mgmt_node())
238 		return false;
239 	else
240 		return dev != &scif_dev[SCIF_MGMT_NODE] &&
241 			!scifdev_self(dev);
242 }
243 
244 /*
245  * scifdev_alive:
246  * @scifdev: The remote SCIF Device
247  *
248  * Returns true if the remote SCIF Device is running or sleeping for
249  * this endpoint.
250  */
_scifdev_alive(struct scif_dev * scifdev)251 static inline int _scifdev_alive(struct scif_dev *scifdev)
252 {
253 	struct scif_peer_dev *spdev;
254 
255 	rcu_read_lock();
256 	spdev = rcu_dereference(scifdev->spdev);
257 	rcu_read_unlock();
258 	return !!spdev;
259 }
260 
261 #include "scif_epd.h"
262 
263 void __init scif_init_debugfs(void);
264 void scif_exit_debugfs(void);
265 int scif_setup_intr_wq(struct scif_dev *scifdev);
266 void scif_destroy_intr_wq(struct scif_dev *scifdev);
267 void scif_cleanup_scifdev(struct scif_dev *dev);
268 void scif_handle_remove_node(int node);
269 void scif_disconnect_node(u32 node_id, bool mgmt_initiated);
270 void scif_free_qp(struct scif_dev *dev);
271 void scif_misc_handler(struct work_struct *work);
272 void scif_stop(struct scif_dev *scifdev);
273 irqreturn_t scif_intr_handler(int irq, void *data);
274 #endif /* SCIF_MAIN_H */
275