Linux Audio

Check our new training course

Loading...
v5.4
  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 <rdma/ib_verbs.h>
 
 17
 18#include "smc.h"
 19#include "smc_ib.h"
 20
 21#define SMC_RMBS_PER_LGR_MAX	255	/* max. # of RMBs per link group */
 22
 23struct smc_lgr_list {			/* list of link group definition */
 24	struct list_head	list;
 25	spinlock_t		lock;	/* protects list of link groups */
 26	u32			num;	/* unique link group number */
 27};
 28
 29enum smc_lgr_role {		/* possible roles of a link group */
 30	SMC_CLNT,	/* client */
 31	SMC_SERV	/* server */
 32};
 33
 34enum smc_link_state {			/* possible states of a link */
 
 35	SMC_LNK_INACTIVE,	/* link is inactive */
 36	SMC_LNK_ACTIVATING,	/* link is being activated */
 37	SMC_LNK_ACTIVE,		/* link is active */
 38	SMC_LNK_DELETING,	/* link is being deleted */
 39};
 40
 41#define SMC_WR_BUF_SIZE		48	/* size of work request buffer */
 42
 43struct smc_wr_buf {
 44	u8	raw[SMC_WR_BUF_SIZE];
 45};
 46
 47#define SMC_WR_REG_MR_WAIT_TIME	(5 * HZ)/* wait time for ib_wr_reg_mr result */
 48
 49enum smc_wr_reg_state {
 50	POSTED,		/* ib_wr_reg_mr request posted */
 51	CONFIRMED,	/* ib_wr_reg_mr response: successful */
 52	FAILED		/* ib_wr_reg_mr response: failure */
 53};
 54
 55struct smc_rdma_sge {				/* sges for RDMA writes */
 56	struct ib_sge		wr_tx_rdma_sge[SMC_IB_MAX_SEND_SGE];
 57};
 58
 59#define SMC_MAX_RDMA_WRITES	2		/* max. # of RDMA writes per
 60						 * message send
 61						 */
 62
 63struct smc_rdma_sges {				/* sges per message send */
 64	struct smc_rdma_sge	tx_rdma_sge[SMC_MAX_RDMA_WRITES];
 65};
 66
 67struct smc_rdma_wr {				/* work requests per message
 68						 * send
 69						 */
 70	struct ib_rdma_wr	wr_tx_rdma[SMC_MAX_RDMA_WRITES];
 71};
 72
 
 
 73struct smc_link {
 74	struct smc_ib_device	*smcibdev;	/* ib-device */
 75	u8			ibport;		/* port - values 1 | 2 */
 76	struct ib_pd		*roce_pd;	/* IB protection domain,
 77						 * unique for every RoCE QP
 78						 */
 79	struct ib_qp		*roce_qp;	/* IB queue pair */
 80	struct ib_qp_attr	qp_attr;	/* IB queue pair attributes */
 81
 82	struct smc_wr_buf	*wr_tx_bufs;	/* WR send payload buffers */
 83	struct ib_send_wr	*wr_tx_ibs;	/* WR send meta data */
 84	struct ib_sge		*wr_tx_sges;	/* WR send gather meta data */
 85	struct smc_rdma_sges	*wr_tx_rdma_sges;/*RDMA WRITE gather meta data*/
 86	struct smc_rdma_wr	*wr_tx_rdmas;	/* WR RDMA WRITE */
 87	struct smc_wr_tx_pend	*wr_tx_pends;	/* WR send waiting for CQE */
 
 88	/* above four vectors have wr_tx_cnt elements and use the same index */
 89	dma_addr_t		wr_tx_dma_addr;	/* DMA address of wr_tx_bufs */
 90	atomic_long_t		wr_tx_id;	/* seq # of last sent WR */
 91	unsigned long		*wr_tx_mask;	/* bit mask of used indexes */
 92	u32			wr_tx_cnt;	/* number of WR send buffers */
 93	wait_queue_head_t	wr_tx_wait;	/* wait for free WR send buf */
 
 94
 95	struct smc_wr_buf	*wr_rx_bufs;	/* WR recv payload buffers */
 96	struct ib_recv_wr	*wr_rx_ibs;	/* WR recv meta data */
 97	struct ib_sge		*wr_rx_sges;	/* WR recv scatter meta data */
 98	/* above three vectors have wr_rx_cnt elements and use the same index */
 99	dma_addr_t		wr_rx_dma_addr;	/* DMA address of wr_rx_bufs */
100	u64			wr_rx_id;	/* seq # of last recv WR */
101	u32			wr_rx_cnt;	/* number of WR recv buffers */
102	unsigned long		wr_rx_tstamp;	/* jiffies when last buf rx */
103
104	struct ib_reg_wr	wr_reg;		/* WR register memory region */
105	wait_queue_head_t	wr_reg_wait;	/* wait for wr_reg result */
 
106	enum smc_wr_reg_state	wr_reg_state;	/* state of wr_reg request */
107
108	u8			gid[SMC_GID_SIZE];/* gid matching used vlan id*/
109	u8			sgid_index;	/* gid index for vlan id      */
110	u32			peer_qpn;	/* QP number of peer */
111	enum ib_mtu		path_mtu;	/* used mtu */
112	enum ib_mtu		peer_mtu;	/* mtu size of peer */
113	u32			psn_initial;	/* QP tx initial packet seqno */
114	u32			peer_psn;	/* QP rx initial packet seqno */
115	u8			peer_mac[ETH_ALEN];	/* = gid[8:10||13:15] */
116	u8			peer_gid[SMC_GID_SIZE];	/* gid of peer*/
117	u8			link_id;	/* unique # within link group */
 
 
 
 
 
 
 
 
118
119	enum smc_link_state	state;		/* state of link */
120	struct workqueue_struct *llc_wq;	/* single thread work queue */
121	struct completion	llc_confirm;	/* wait for rx of conf link */
122	struct completion	llc_confirm_resp; /* wait 4 rx of cnf lnk rsp */
123	int			llc_confirm_rc; /* rc from confirm link msg */
124	int			llc_confirm_resp_rc; /* rc from conf_resp msg */
125	struct completion	llc_add;	/* wait for rx of add link */
126	struct completion	llc_add_resp;	/* wait for rx of add link rsp*/
127	struct delayed_work	llc_testlink_wrk; /* testlink worker */
128	struct completion	llc_testlink_resp; /* wait for rx of testlink */
129	int			llc_testlink_time; /* testlink interval */
130	struct completion	llc_confirm_rkey; /* wait 4 rx of cnf rkey */
131	int			llc_confirm_rkey_rc; /* rc from cnf rkey msg */
132	struct completion	llc_delete_rkey; /* wait 4 rx of del rkey */
133	int			llc_delete_rkey_rc; /* rc from del rkey msg */
134	struct mutex		llc_delete_rkey_mutex; /* serialize usage */
135};
136
137/* For now we just allow one parallel link per link group. The SMC protocol
138 * allows more (up to 8).
139 */
140#define SMC_LINKS_PER_LGR_MAX	1
141#define SMC_SINGLE_LINK		0
142
143#define SMC_FIRST_CONTACT	1		/* first contact to a peer */
144#define SMC_REUSE_CONTACT	0		/* follow-on contact to a peer*/
145
146/* tx/rx buffer list element for sndbufs list and rmbs list of a lgr */
147struct 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	u8			wr_reg	: 1;	/* mem region registered */
154	u8			regerr	: 1;	/* err during registration */
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		struct { /* SMC-D */
166			unsigned short		sba_idx;
167						/* SBA index number */
168			u64			token;
169						/* DMB token number */
170			dma_addr_t		dma_addr;
171						/* DMA address */
172		};
173	};
174};
175
176struct smc_rtoken {				/* address/key of remote RMB */
177	u64			dma_addr;
178	u32			rkey;
179};
180
181#define SMC_LGR_ID_SIZE		4
182#define SMC_BUF_MIN_SIZE	16384	/* minimum size of an RMB */
183#define SMC_RMBE_SIZES		16	/* number of distinct RMBE sizes */
184/* theoretically, the RFC states that largest size would be 512K,
185 * i.e. compressed 5 and thus 6 sizes (0..5), despite
186 * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
187 */
188
189struct smcd_dev;
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191struct smc_link_group {
192	struct list_head	list;
193	struct rb_root		conns_all;	/* connection tree */
194	rwlock_t		conns_lock;	/* protects conns_all */
195	unsigned int		conns_num;	/* current # of connections */
196	unsigned short		vlan_id;	/* vlan id of link group */
197
198	struct list_head	sndbufs[SMC_RMBE_SIZES];/* tx buffers */
199	rwlock_t		sndbufs_lock;	/* protects tx buffers */
200	struct list_head	rmbs[SMC_RMBE_SIZES];	/* rx buffers */
201	rwlock_t		rmbs_lock;	/* protects rx buffers */
202
203	u8			id[SMC_LGR_ID_SIZE];	/* unique lgr id */
204	struct delayed_work	free_work;	/* delayed freeing of an lgr */
 
 
205	u8			sync_err : 1;	/* lgr no longer fits to peer */
206	u8			terminating : 1;/* lgr is terminating */
 
207
208	bool			is_smcd;	/* SMC-R or SMC-D */
 
 
 
 
 
209	union {
210		struct { /* SMC-R */
211			enum smc_lgr_role	role;
212						/* client or server */
213			struct smc_link		lnk[SMC_LINKS_PER_LGR_MAX];
214						/* smc link */
215			char			peer_systemid[SMC_SYSTEMID_LEN];
216						/* unique system_id of peer */
217			struct smc_rtoken	rtokens[SMC_RMBS_PER_LGR_MAX]
218						[SMC_LINKS_PER_LGR_MAX];
219						/* remote addr/key pairs */
220			DECLARE_BITMAP(rtokens_used_mask, SMC_RMBS_PER_LGR_MAX);
221						/* used rtoken elements */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222		};
223		struct { /* SMC-D */
224			u64			peer_gid;
225						/* Peer GID (remote) */
226			struct smcd_dev		*smcd;
227						/* ISM device for VLAN reg. */
 
 
228		};
229	};
230};
231
232struct smc_clc_msg_local;
233
234struct smc_init_info {
235	u8			is_smcd;
 
 
 
 
236	unsigned short		vlan_id;
237	int			srv_first_contact;
238	int			cln_first_contact;
239	/* SMC-R */
240	struct smc_clc_msg_local *ib_lcl;
241	struct smc_ib_device	*ib_dev;
242	u8			ib_gid[SMC_GID_SIZE];
243	u8			ib_port;
244	u32			ib_clcqpn;
245	/* SMC-D */
246	u64			ism_gid;
247	struct smcd_dev		*ism_dev;
 
 
 
 
248};
249
250/* Find the connection associated with the given alert token in the link group.
251 * To use rbtrees we have to implement our own search core.
252 * Requires @conns_lock
253 * @token	alert token to search for
254 * @lgr		 link group to search in
255 * Returns connection associated with token if found, NULL otherwise.
256 */
257static inline struct smc_connection *smc_lgr_find_conn(
258	u32 token, struct smc_link_group *lgr)
259{
260	struct smc_connection *res = NULL;
261	struct rb_node *node;
262
263	node = lgr->conns_all.rb_node;
264	while (node) {
265		struct smc_connection *cur = rb_entry(node,
266					struct smc_connection, alert_node);
267
268		if (cur->alert_token_local > token) {
269			node = node->rb_left;
270		} else {
271			if (cur->alert_token_local < token) {
272				node = node->rb_right;
273			} else {
274				res = cur;
275				break;
276			}
277		}
278	}
279
280	return res;
281}
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283struct smc_sock;
284struct smc_clc_msg_accept_confirm;
285struct smc_clc_msg_local;
286
287void smc_lgr_forget(struct smc_link_group *lgr);
288void smc_lgr_terminate(struct smc_link_group *lgr);
289void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport);
 
290void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid,
291			unsigned short vlan);
 
 
292int smc_buf_create(struct smc_sock *smc, bool is_smcd);
293int smc_uncompress_bufsize(u8 compressed);
294int smc_rmb_rtoken_handling(struct smc_connection *conn,
295			    struct smc_clc_msg_accept_confirm *clc);
296int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey);
297int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey);
 
 
 
 
298void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
299void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
300void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
301void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
302int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
303
304void smc_conn_free(struct smc_connection *conn);
305int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
306void smcd_conn_free(struct smc_connection *conn);
307void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr);
 
308void smc_core_exit(void);
309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310static inline struct smc_link_group *smc_get_lgr(struct smc_link *link)
311{
312	return container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
313}
314#endif
v5.14.15
  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
 26struct 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
 32enum smc_lgr_role {		/* possible roles of a link group */
 33	SMC_CLNT,	/* client */
 34	SMC_SERV	/* server */
 35};
 36
 37enum 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
 46struct 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
 52enum 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
 58struct 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
 66struct smc_rdma_sges {				/* sges per message send */
 67	struct smc_rdma_sge	tx_rdma_sge[SMC_MAX_RDMA_WRITES];
 68};
 69
 70struct 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
 78struct 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 */
149struct 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
185struct 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
197struct smcd_dev;
198
199enum 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
207enum 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
214struct smc_llc_qentry;
215
216struct smc_llc_flow {
217	enum smc_llc_flowtype type;
218	struct smc_llc_qentry *qentry;
219};
220
221struct 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
303struct smc_clc_msg_local;
304
305struct 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 */
335static 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 */
362static 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
369static inline bool smc_link_active(struct smc_link *lnk)
370{
371	return lnk->state == SMC_LNK_ACTIVE;
372}
373
374static 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
387struct 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
395static 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
413struct smc_sock;
414struct smc_clc_msg_accept_confirm;
 
415
416void smc_lgr_cleanup_early(struct smc_connection *conn);
417void smc_lgr_terminate_sched(struct smc_link_group *lgr);
418void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport);
419void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport);
420void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid,
421			unsigned short vlan);
422void smc_smcd_terminate_all(struct smcd_dev *dev);
423void smc_smcr_terminate_all(struct smc_ib_device *smcibdev);
424int smc_buf_create(struct smc_sock *smc, bool is_smcd);
425int smc_uncompress_bufsize(u8 compressed);
426int smc_rmb_rtoken_handling(struct smc_connection *conn, struct smc_link *link,
427			    struct smc_clc_msg_accept_confirm *clc);
428int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey);
429int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey);
430void 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);
432void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
433		     __be64 nw_vaddr, __be32 nw_rkey);
434void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn);
435void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn);
436void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn);
437void smc_rmb_sync_sg_for_device(struct smc_connection *conn);
438int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
439
440void smc_conn_free(struct smc_connection *conn);
441int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
 
442void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr);
443int smc_core_init(void);
444void smc_core_exit(void);
445
446int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
447		   u8 link_idx, struct smc_init_info *ini);
448void smcr_link_clear(struct smc_link *lnk, bool log);
449void smc_switch_link_and_count(struct smc_connection *conn,
450			       struct smc_link *to_lnk);
451int smcr_buf_map_lgr(struct smc_link *lnk);
452int smcr_buf_reg_lgr(struct smc_link *lnk);
453void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type);
454void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
455			    enum smc_lgr_type new_type, int asym_lnk_idx);
456int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc);
457struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
458				  struct smc_link *from_lnk, bool is_dev_err);
459void smcr_link_down_cond(struct smc_link *lnk);
460void smcr_link_down_cond_sched(struct smc_link *lnk);
461int smc_nl_get_sys_info(struct sk_buff *skb, struct netlink_callback *cb);
462int smcr_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
463int smcr_nl_get_link(struct sk_buff *skb, struct netlink_callback *cb);
464int smcd_nl_get_lgr(struct sk_buff *skb, struct netlink_callback *cb);
465
466static inline struct smc_link_group *smc_get_lgr(struct smc_link *link)
467{
468	return link->lgr;
469}
470#endif