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