xref: /openbmc/linux/net/smc/smc_core.h (revision 31e67366)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  Definitions for SMC Connections, Link Groups and Links
6  *
7  *  Copyright IBM Corp. 2016
8  *
9  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
10  */
11 
12 #ifndef _SMC_CORE_H
13 #define _SMC_CORE_H
14 
15 #include <linux/atomic.h>
16 #include <linux/smc.h>
17 #include <linux/pci.h>
18 #include <rdma/ib_verbs.h>
19 #include <net/genetlink.h>
20 
21 #include "smc.h"
22 #include "smc_ib.h"
23 
24 #define SMC_RMBS_PER_LGR_MAX	255	/* max. # of RMBs per link group */
25 
26 struct smc_lgr_list {			/* list of link group definition */
27 	struct list_head	list;
28 	spinlock_t		lock;	/* protects list of link groups */
29 	u32			num;	/* unique link group number */
30 };
31 
32 enum smc_lgr_role {		/* possible roles of a link group */
33 	SMC_CLNT,	/* client */
34 	SMC_SERV	/* server */
35 };
36 
37 enum smc_link_state {			/* possible states of a link */
38 	SMC_LNK_UNUSED,		/* link is unused */
39 	SMC_LNK_INACTIVE,	/* link is inactive */
40 	SMC_LNK_ACTIVATING,	/* link is being activated */
41 	SMC_LNK_ACTIVE,		/* link is active */
42 };
43 
44 #define SMC_WR_BUF_SIZE		48	/* size of work request buffer */
45 
46 struct smc_wr_buf {
47 	u8	raw[SMC_WR_BUF_SIZE];
48 };
49 
50 #define SMC_WR_REG_MR_WAIT_TIME	(5 * HZ)/* wait time for ib_wr_reg_mr result */
51 
52 enum smc_wr_reg_state {
53 	POSTED,		/* ib_wr_reg_mr request posted */
54 	CONFIRMED,	/* ib_wr_reg_mr response: successful */
55 	FAILED		/* ib_wr_reg_mr response: failure */
56 };
57 
58 struct smc_rdma_sge {				/* sges for RDMA writes */
59 	struct ib_sge		wr_tx_rdma_sge[SMC_IB_MAX_SEND_SGE];
60 };
61 
62 #define SMC_MAX_RDMA_WRITES	2		/* max. # of RDMA writes per
63 						 * message send
64 						 */
65 
66 struct smc_rdma_sges {				/* sges per message send */
67 	struct smc_rdma_sge	tx_rdma_sge[SMC_MAX_RDMA_WRITES];
68 };
69 
70 struct smc_rdma_wr {				/* work requests per message
71 						 * send
72 						 */
73 	struct ib_rdma_wr	wr_tx_rdma[SMC_MAX_RDMA_WRITES];
74 };
75 
76 #define SMC_LGR_ID_SIZE		4
77 
78 struct smc_link {
79 	struct smc_ib_device	*smcibdev;	/* ib-device */
80 	u8			ibport;		/* port - values 1 | 2 */
81 	struct ib_pd		*roce_pd;	/* IB protection domain,
82 						 * unique for every RoCE QP
83 						 */
84 	struct ib_qp		*roce_qp;	/* IB queue pair */
85 	struct ib_qp_attr	qp_attr;	/* IB queue pair attributes */
86 
87 	struct smc_wr_buf	*wr_tx_bufs;	/* WR send payload buffers */
88 	struct ib_send_wr	*wr_tx_ibs;	/* WR send meta data */
89 	struct ib_sge		*wr_tx_sges;	/* WR send gather meta data */
90 	struct smc_rdma_sges	*wr_tx_rdma_sges;/*RDMA WRITE gather meta data*/
91 	struct smc_rdma_wr	*wr_tx_rdmas;	/* WR RDMA WRITE */
92 	struct smc_wr_tx_pend	*wr_tx_pends;	/* WR send waiting for CQE */
93 	struct completion	*wr_tx_compl;	/* WR send CQE completion */
94 	/* above four vectors have wr_tx_cnt elements and use the same index */
95 	dma_addr_t		wr_tx_dma_addr;	/* DMA address of wr_tx_bufs */
96 	atomic_long_t		wr_tx_id;	/* seq # of last sent WR */
97 	unsigned long		*wr_tx_mask;	/* bit mask of used indexes */
98 	u32			wr_tx_cnt;	/* number of WR send buffers */
99 	wait_queue_head_t	wr_tx_wait;	/* wait for free WR send buf */
100 
101 	struct smc_wr_buf	*wr_rx_bufs;	/* WR recv payload buffers */
102 	struct ib_recv_wr	*wr_rx_ibs;	/* WR recv meta data */
103 	struct ib_sge		*wr_rx_sges;	/* WR recv scatter meta data */
104 	/* above three vectors have wr_rx_cnt elements and use the same index */
105 	dma_addr_t		wr_rx_dma_addr;	/* DMA address of wr_rx_bufs */
106 	u64			wr_rx_id;	/* seq # of last recv WR */
107 	u32			wr_rx_cnt;	/* number of WR recv buffers */
108 	unsigned long		wr_rx_tstamp;	/* jiffies when last buf rx */
109 
110 	struct ib_reg_wr	wr_reg;		/* WR register memory region */
111 	wait_queue_head_t	wr_reg_wait;	/* wait for wr_reg result */
112 	enum smc_wr_reg_state	wr_reg_state;	/* state of wr_reg request */
113 
114 	u8			gid[SMC_GID_SIZE];/* gid matching used vlan id*/
115 	u8			sgid_index;	/* gid index for vlan id      */
116 	u32			peer_qpn;	/* QP number of peer */
117 	enum ib_mtu		path_mtu;	/* used mtu */
118 	enum ib_mtu		peer_mtu;	/* mtu size of peer */
119 	u32			psn_initial;	/* QP tx initial packet seqno */
120 	u32			peer_psn;	/* QP rx initial packet seqno */
121 	u8			peer_mac[ETH_ALEN];	/* = gid[8:10||13:15] */
122 	u8			peer_gid[SMC_GID_SIZE];	/* gid of peer*/
123 	u8			link_id;	/* unique # within link group */
124 	u8			link_uid[SMC_LGR_ID_SIZE]; /* unique lnk id */
125 	u8			peer_link_uid[SMC_LGR_ID_SIZE]; /* peer uid */
126 	u8			link_idx;	/* index in lgr link array */
127 	u8			link_is_asym;	/* is link asymmetric? */
128 	struct smc_link_group	*lgr;		/* parent link group */
129 	struct work_struct	link_down_wrk;	/* wrk to bring link down */
130 	char			ibname[IB_DEVICE_NAME_MAX]; /* ib device name */
131 	int			ndev_ifidx; /* network device ifindex */
132 
133 	enum smc_link_state	state;		/* state of link */
134 	struct delayed_work	llc_testlink_wrk; /* testlink worker */
135 	struct completion	llc_testlink_resp; /* wait for rx of testlink */
136 	int			llc_testlink_time; /* testlink interval */
137 	atomic_t		conn_cnt; /* connections on this link */
138 };
139 
140 /* For now we just allow one parallel link per link group. The SMC protocol
141  * allows more (up to 8).
142  */
143 #define SMC_LINKS_PER_LGR_MAX	3
144 #define SMC_SINGLE_LINK		0
145 
146 /* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
147 struct smc_buf_desc {
148 	struct list_head	list;
149 	void			*cpu_addr;	/* virtual address of buffer */
150 	struct page		*pages;
151 	int			len;		/* length of buffer */
152 	u32			used;		/* currently used / unused */
153 	union {
154 		struct { /* SMC-R */
155 			struct sg_table	sgt[SMC_LINKS_PER_LGR_MAX];
156 					/* virtual buffer */
157 			struct ib_mr	*mr_rx[SMC_LINKS_PER_LGR_MAX];
158 					/* for rmb only: memory region
159 					 * incl. rkey provided to peer
160 					 */
161 			u32		order;	/* allocation order */
162 
163 			u8		is_conf_rkey;
164 					/* confirm_rkey done */
165 			u8		is_reg_mr[SMC_LINKS_PER_LGR_MAX];
166 					/* mem region registered */
167 			u8		is_map_ib[SMC_LINKS_PER_LGR_MAX];
168 					/* mem region mapped to lnk */
169 			u8		is_reg_err;
170 					/* buffer registration err */
171 		};
172 		struct { /* SMC-D */
173 			unsigned short	sba_idx;
174 					/* SBA index number */
175 			u64		token;
176 					/* DMB token number */
177 			dma_addr_t	dma_addr;
178 					/* DMA address */
179 		};
180 	};
181 };
182 
183 struct smc_rtoken {				/* address/key of remote RMB */
184 	u64			dma_addr;
185 	u32			rkey;
186 };
187 
188 #define SMC_BUF_MIN_SIZE	16384	/* minimum size of an RMB */
189 #define SMC_RMBE_SIZES		16	/* number of distinct RMBE sizes */
190 /* theoretically, the RFC states that largest size would be 512K,
191  * i.e. compressed 5 and thus 6 sizes (0..5), despite
192  * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
193  */
194 
195 struct smcd_dev;
196 
197 enum smc_lgr_type {				/* redundancy state of lgr */
198 	SMC_LGR_NONE,			/* no active links, lgr to be deleted */
199 	SMC_LGR_SINGLE,			/* 1 active RNIC on each peer */
200 	SMC_LGR_SYMMETRIC,		/* 2 active RNICs on each peer */
201 	SMC_LGR_ASYMMETRIC_PEER,	/* local has 2, peer 1 active RNICs */
202 	SMC_LGR_ASYMMETRIC_LOCAL,	/* local has 1, peer 2 active RNICs */
203 };
204 
205 enum smc_llc_flowtype {
206 	SMC_LLC_FLOW_NONE	= 0,
207 	SMC_LLC_FLOW_ADD_LINK	= 2,
208 	SMC_LLC_FLOW_DEL_LINK	= 4,
209 	SMC_LLC_FLOW_RKEY	= 6,
210 };
211 
212 struct smc_llc_qentry;
213 
214 struct smc_llc_flow {
215 	enum smc_llc_flowtype type;
216 	struct smc_llc_qentry *qentry;
217 };
218 
219 struct smc_link_group {
220 	struct list_head	list;
221 	struct rb_root		conns_all;	/* connection tree */
222 	rwlock_t		conns_lock;	/* protects conns_all */
223 	unsigned int		conns_num;	/* current # of connections */
224 	unsigned short		vlan_id;	/* vlan id of link group */
225 
226 	struct list_head	sndbufs[SMC_RMBE_SIZES];/* tx buffers */
227 	struct mutex		sndbufs_lock;	/* protects tx buffers */
228 	struct list_head	rmbs[SMC_RMBE_SIZES];	/* rx buffers */
229 	struct mutex		rmbs_lock;	/* protects rx buffers */
230 
231 	u8			id[SMC_LGR_ID_SIZE];	/* unique lgr id */
232 	struct delayed_work	free_work;	/* delayed freeing of an lgr */
233 	struct work_struct	terminate_work;	/* abnormal lgr termination */
234 	struct workqueue_struct	*tx_wq;		/* wq for conn. tx workers */
235 	u8			sync_err : 1;	/* lgr no longer fits to peer */
236 	u8			terminating : 1;/* lgr is terminating */
237 	u8			freeing : 1;	/* lgr is being freed */
238 
239 	bool			is_smcd;	/* SMC-R or SMC-D */
240 	u8			smc_version;
241 	u8			negotiated_eid[SMC_MAX_EID_LEN];
242 	u8			peer_os;	/* peer operating system */
243 	u8			peer_smc_release;
244 	u8			peer_hostname[SMC_MAX_HOSTNAME_LEN];
245 	union {
246 		struct { /* SMC-R */
247 			enum smc_lgr_role	role;
248 						/* client or server */
249 			struct smc_link		lnk[SMC_LINKS_PER_LGR_MAX];
250 						/* smc link */
251 			char			peer_systemid[SMC_SYSTEMID_LEN];
252 						/* unique system_id of peer */
253 			struct smc_rtoken	rtokens[SMC_RMBS_PER_LGR_MAX]
254 						[SMC_LINKS_PER_LGR_MAX];
255 						/* remote addr/key pairs */
256 			DECLARE_BITMAP(rtokens_used_mask, SMC_RMBS_PER_LGR_MAX);
257 						/* used rtoken elements */
258 			u8			next_link_id;
259 			enum smc_lgr_type	type;
260 						/* redundancy state */
261 			u8			pnet_id[SMC_MAX_PNETID_LEN + 1];
262 						/* pnet id of this lgr */
263 			struct list_head	llc_event_q;
264 						/* queue for llc events */
265 			spinlock_t		llc_event_q_lock;
266 						/* protects llc_event_q */
267 			struct mutex		llc_conf_mutex;
268 						/* protects lgr reconfig. */
269 			struct work_struct	llc_add_link_work;
270 			struct work_struct	llc_del_link_work;
271 			struct work_struct	llc_event_work;
272 						/* llc event worker */
273 			wait_queue_head_t	llc_flow_waiter;
274 						/* w4 next llc event */
275 			wait_queue_head_t	llc_msg_waiter;
276 						/* w4 next llc msg */
277 			struct smc_llc_flow	llc_flow_lcl;
278 						/* llc local control field */
279 			struct smc_llc_flow	llc_flow_rmt;
280 						/* llc remote control field */
281 			struct smc_llc_qentry	*delayed_event;
282 						/* arrived when flow active */
283 			spinlock_t		llc_flow_lock;
284 						/* protects llc flow */
285 			int			llc_testlink_time;
286 						/* link keep alive time */
287 			u32			llc_termination_rsn;
288 						/* rsn code for termination */
289 		};
290 		struct { /* SMC-D */
291 			u64			peer_gid;
292 						/* Peer GID (remote) */
293 			struct smcd_dev		*smcd;
294 						/* ISM device for VLAN reg. */
295 			u8			peer_shutdown : 1;
296 						/* peer triggered shutdownn */
297 		};
298 	};
299 };
300 
301 struct smc_clc_msg_local;
302 
303 struct smc_init_info {
304 	u8			is_smcd;
305 	u8			smc_type_v1;
306 	u8			smc_type_v2;
307 	u8			first_contact_peer;
308 	u8			first_contact_local;
309 	unsigned short		vlan_id;
310 	u32			rc;
311 	/* SMC-R */
312 	struct smc_clc_msg_local *ib_lcl;
313 	struct smc_ib_device	*ib_dev;
314 	u8			ib_gid[SMC_GID_SIZE];
315 	u8			ib_port;
316 	u32			ib_clcqpn;
317 	/* SMC-D */
318 	u64			ism_peer_gid[SMC_MAX_ISM_DEVS + 1];
319 	struct smcd_dev		*ism_dev[SMC_MAX_ISM_DEVS + 1];
320 	u16			ism_chid[SMC_MAX_ISM_DEVS + 1];
321 	u8			ism_offered_cnt; /* # of ISM devices offered */
322 	u8			ism_selected;    /* index of selected ISM dev*/
323 	u8			smcd_version;
324 };
325 
326 /* Find the connection associated with the given alert token in the link group.
327  * To use rbtrees we have to implement our own search core.
328  * Requires @conns_lock
329  * @token	alert token to search for
330  * @lgr		 link group to search in
331  * Returns connection associated with token if found, NULL otherwise.
332  */
333 static inline struct smc_connection *smc_lgr_find_conn(
334 	u32 token, struct smc_link_group *lgr)
335 {
336 	struct smc_connection *res = NULL;
337 	struct rb_node *node;
338 
339 	node = lgr->conns_all.rb_node;
340 	while (node) {
341 		struct smc_connection *cur = rb_entry(node,
342 					struct smc_connection, alert_node);
343 
344 		if (cur->alert_token_local > token) {
345 			node = node->rb_left;
346 		} else {
347 			if (cur->alert_token_local < token) {
348 				node = node->rb_right;
349 			} else {
350 				res = cur;
351 				break;
352 			}
353 		}
354 	}
355 
356 	return res;
357 }
358 
359 /* returns true if the specified link is usable */
360 static inline bool smc_link_usable(struct smc_link *lnk)
361 {
362 	if (lnk->state == SMC_LNK_UNUSED || lnk->state == SMC_LNK_INACTIVE)
363 		return false;
364 	return true;
365 }
366 
367 static inline bool smc_link_active(struct smc_link *lnk)
368 {
369 	return lnk->state == SMC_LNK_ACTIVE;
370 }
371 
372 static inline void smc_gid_be16_convert(__u8 *buf, u8 *gid_raw)
373 {
374 	sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
375 		be16_to_cpu(((__be16 *)gid_raw)[0]),
376 		be16_to_cpu(((__be16 *)gid_raw)[1]),
377 		be16_to_cpu(((__be16 *)gid_raw)[2]),
378 		be16_to_cpu(((__be16 *)gid_raw)[3]),
379 		be16_to_cpu(((__be16 *)gid_raw)[4]),
380 		be16_to_cpu(((__be16 *)gid_raw)[5]),
381 		be16_to_cpu(((__be16 *)gid_raw)[6]),
382 		be16_to_cpu(((__be16 *)gid_raw)[7]));
383 }
384 
385 struct smc_pci_dev {
386 	__u32		pci_fid;
387 	__u16		pci_pchid;
388 	__u16		pci_vendor;
389 	__u16		pci_device;
390 	__u8		pci_id[SMC_PCI_ID_STR_LEN];
391 };
392 
393 static inline void smc_set_pci_values(struct pci_dev *pci_dev,
394 				      struct smc_pci_dev *smc_dev)
395 {
396 	smc_dev->pci_vendor = pci_dev->vendor;
397 	smc_dev->pci_device = pci_dev->device;
398 	snprintf(smc_dev->pci_id, sizeof(smc_dev->pci_id), "%s",
399 		 pci_name(pci_dev));
400 #if IS_ENABLED(CONFIG_S390)
401 	{ /* Set s390 specific PCI information */
402 	struct zpci_dev *zdev;
403 
404 	zdev = to_zpci(pci_dev);
405 	smc_dev->pci_fid = zdev->fid;
406 	smc_dev->pci_pchid = zdev->pchid;
407 	}
408 #endif
409 }
410 
411 struct smc_sock;
412 struct smc_clc_msg_accept_confirm;
413 struct smc_clc_msg_local;
414 
415 void smc_lgr_cleanup_early(struct smc_connection *conn);
416 void smc_lgr_terminate_sched(struct smc_link_group *lgr);
417 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport);
418 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport);
419 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid,
420 			unsigned short vlan);
421 void smc_smcd_terminate_all(struct smcd_dev *dev);
422 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev);
423 int smc_buf_create(struct smc_sock *smc, bool is_smcd);
424 int smc_uncompress_bufsize(u8 compressed);
425 int smc_rmb_rtoken_handling(struct smc_connection *conn, struct smc_link *link,
426 			    struct smc_clc_msg_accept_confirm *clc);
427 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey);
428 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey);
429 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
430 		    __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey);
431 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
432 		     __be64 nw_vaddr, __be32 nw_rkey);
433 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
434 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
435 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
436 void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
437 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
438 
439 void smc_conn_free(struct smc_connection *conn);
440 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
441 void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr);
442 int smc_core_init(void);
443 void smc_core_exit(void);
444 
445 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
446 		   u8 link_idx, struct smc_init_info *ini);
447 void smcr_link_clear(struct smc_link *lnk, bool log);
448 int smcr_buf_map_lgr(struct smc_link *lnk);
449 int smcr_buf_reg_lgr(struct smc_link *lnk);
450 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type);
451 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
452 			    enum smc_lgr_type new_type, int asym_lnk_idx);
453 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc);
454 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
455 				  struct smc_link *from_lnk, bool is_dev_err);
456 void smcr_link_down_cond(struct smc_link *lnk);
457 void smcr_link_down_cond_sched(struct smc_link *lnk);
458 int smc_nl_get_sys_info(struct sk_buff *skb, struct netlink_callback *cb);
459 int smcr_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
460 int smcr_nl_get_link(struct sk_buff *skb, struct netlink_callback *cb);
461 int smcd_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
462 
463 static inline struct smc_link_group *smc_get_lgr(struct smc_link *link)
464 {
465 	return link->lgr;
466 }
467 #endif
468