1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * CLC (connection layer control) handshake over initial TCP socket to
6 * prepare for RDMA traffic
8 * Copyright IBM Corp. 2016
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 #include <rdma/ib_verbs.h>
17 #include <linux/smc.h>
20 #include "smc_netlink.h"
22 #define SMC_CLC_PROPOSAL 0x01
23 #define SMC_CLC_ACCEPT 0x02
24 #define SMC_CLC_CONFIRM 0x03
25 #define SMC_CLC_DECLINE 0x04
27 #define SMC_TYPE_R 0 /* SMC-R only */
28 #define SMC_TYPE_D 1 /* SMC-D only */
29 #define SMC_TYPE_N 2 /* neither SMC-R nor SMC-D */
30 #define SMC_TYPE_B 3 /* SMC-R and SMC-D */
31 #define CLC_WAIT_TIME (6 * HZ) /* max. wait time on clcsock */
32 #define CLC_WAIT_TIME_SHORT HZ /* short wait time on clcsock */
33 #define SMC_CLC_DECL_MEM 0x01010000 /* insufficient memory resources */
34 #define SMC_CLC_DECL_TIMEOUT_CL 0x02010000 /* timeout w4 QP confirm link */
35 #define SMC_CLC_DECL_TIMEOUT_AL 0x02020000 /* timeout w4 QP add link */
36 #define SMC_CLC_DECL_CNFERR 0x03000000 /* configuration error */
37 #define SMC_CLC_DECL_PEERNOSMC 0x03010000 /* peer did not indicate SMC */
38 #define SMC_CLC_DECL_IPSEC 0x03020000 /* IPsec usage */
39 #define SMC_CLC_DECL_NOSMCDEV 0x03030000 /* no SMC device found (R or D) */
40 #define SMC_CLC_DECL_NOSMCDDEV 0x03030001 /* no SMC-D device found */
41 #define SMC_CLC_DECL_NOSMCRDEV 0x03030002 /* no SMC-R device found */
42 #define SMC_CLC_DECL_NOISM2SUPP 0x03030003 /* hardware has no ISMv2 support */
43 #define SMC_CLC_DECL_NOV2EXT 0x03030004 /* peer sent no clc v2 extension */
44 #define SMC_CLC_DECL_NOV2DEXT 0x03030005 /* peer sent no clc SMC-Dv2 ext. */
45 #define SMC_CLC_DECL_NOSEID 0x03030006 /* peer sent no SEID */
46 #define SMC_CLC_DECL_NOSMCD2DEV 0x03030007 /* no SMC-Dv2 device found */
47 #define SMC_CLC_DECL_NOUEID 0x03030008 /* peer sent no UEID */
48 #define SMC_CLC_DECL_RELEASEERR 0x03030009 /* release version negotiate failed */
49 #define SMC_CLC_DECL_MAXCONNERR 0x0303000a /* max connections negotiate failed */
50 #define SMC_CLC_DECL_MAXLINKERR 0x0303000b /* max links negotiate failed */
51 #define SMC_CLC_DECL_MODEUNSUPP 0x03040000 /* smc modes do not match (R or D)*/
52 #define SMC_CLC_DECL_RMBE_EC 0x03050000 /* peer has eyecatcher in RMBE */
53 #define SMC_CLC_DECL_OPTUNSUPP 0x03060000 /* fastopen sockopt not supported */
54 #define SMC_CLC_DECL_DIFFPREFIX 0x03070000 /* IP prefix / subnet mismatch */
55 #define SMC_CLC_DECL_GETVLANERR 0x03080000 /* err to get vlan id of ip device*/
56 #define SMC_CLC_DECL_ISMVLANERR 0x03090000 /* err to reg vlan id on ism dev */
57 #define SMC_CLC_DECL_NOACTLINK 0x030a0000 /* no active smc-r link in lgr */
58 #define SMC_CLC_DECL_NOSRVLINK 0x030b0000 /* SMC-R link from srv not found */
59 #define SMC_CLC_DECL_VERSMISMAT 0x030c0000 /* SMC version mismatch */
60 #define SMC_CLC_DECL_MAX_DMB 0x030d0000 /* SMC-D DMB limit exceeded */
61 #define SMC_CLC_DECL_NOROUTE 0x030e0000 /* SMC-Rv2 conn. no route to peer */
62 #define SMC_CLC_DECL_NOINDIRECT 0x030f0000 /* SMC-Rv2 conn. indirect mismatch*/
63 #define SMC_CLC_DECL_SYNCERR 0x04000000 /* synchronization error */
64 #define SMC_CLC_DECL_PEERDECL 0x05000000 /* peer declined during handshake */
65 #define SMC_CLC_DECL_INTERR 0x09990000 /* internal error */
66 #define SMC_CLC_DECL_ERR_RTOK 0x09990001 /* rtoken handling failed */
67 #define SMC_CLC_DECL_ERR_RDYLNK 0x09990002 /* ib ready link failed */
68 #define SMC_CLC_DECL_ERR_REGBUF 0x09990003 /* reg rdma bufs failed */
70 #define SMC_FIRST_CONTACT_MASK 0b10 /* first contact bit within typev2 */
72 struct smc_clc_msg_hdr { /* header1 of clc messages */
73 u8 eyecatcher[4]; /* eye catcher */
74 u8 type; /* proposal / accept / confirm / decline */
76 #if defined(__BIG_ENDIAN_BITFIELD)
80 #elif defined(__LITTLE_ENDIAN_BITFIELD)
85 } __packed; /* format defined in RFC7609 */
87 struct smc_clc_msg_trail { /* trailer of clc messages */
91 struct smc_clc_msg_local { /* header2 of clc messages */
92 u8 id_for_peer[SMC_SYSTEMID_LEN]; /* unique system id */
93 u8 gid[16]; /* gid of ib_device port */
94 u8 mac[6]; /* mac of ib_device port */
97 /* Struct would be 4 byte aligned, but it is used in an array that is sent
98 * to peers and must conform to RFC7609, hence we need to use packed here.
100 struct smc_clc_ipv6_prefix {
101 struct in6_addr prefix;
103 } __packed; /* format defined in RFC7609 */
105 #if defined(__BIG_ENDIAN_BITFIELD)
106 struct smc_clc_v2_flag {
111 #elif defined(__LITTLE_ENDIAN_BITFIELD)
112 struct smc_clc_v2_flag {
119 struct smc_clnt_opts_area_hdr {
120 u8 eid_cnt; /* number of user defined EIDs */
121 u8 ism_gid_cnt; /* number of ISMv2 GIDs */
123 struct smc_clc_v2_flag flag;
125 __be16 smcd_v2_ext_offset; /* SMC-Dv2 Extension Offset */
128 struct smc_clc_smcd_gid_chid {
129 __be64 gid; /* ISM GID */
130 __be16 chid; /* ISMv2 CHID */
131 } __packed; /* format defined in
132 * IBM Shared Memory Communications Version 2
133 * (https://www.ibm.com/support/pages/node/6326337)
136 struct smc_clc_v2_extension {
137 struct smc_clnt_opts_area_hdr hdr;
138 u8 roce[16]; /* RoCEv2 GID */
142 u8 user_eids[][SMC_MAX_EID_LEN];
145 struct smc_clc_msg_proposal_prefix { /* prefix part of clc proposal message*/
146 __be32 outgoing_subnet; /* subnet mask */
147 u8 prefix_len; /* number of significant bits in mask */
149 u8 ipv6_prefixes_cnt; /* number of IPv6 prefixes in prefix array */
152 struct smc_clc_msg_smcd { /* SMC-D GID information */
153 struct smc_clc_smcd_gid_chid ism; /* ISM native GID+CHID of requestor */
154 __be16 v2_ext_offset; /* SMC Version 2 Extension Offset */
155 u8 vendor_oui[3]; /* vendor organizationally unique identifier */
156 u8 vendor_exp_options[5];
160 struct smc_clc_smcd_v2_extension {
161 u8 system_eid[SMC_MAX_EID_LEN];
163 struct smc_clc_smcd_gid_chid gidchid[];
166 struct smc_clc_msg_proposal { /* clc proposal message sent by Linux */
167 struct smc_clc_msg_hdr hdr;
168 struct smc_clc_msg_local lcl;
169 __be16 iparea_offset; /* offset to IP address information area */
172 #define SMC_CLC_MAX_V6_PREFIX 8
173 #define SMC_CLC_MAX_UEID 8
175 struct smc_clc_msg_proposal_area {
176 struct smc_clc_msg_proposal pclc_base;
177 struct smc_clc_msg_smcd pclc_smcd;
178 struct smc_clc_msg_proposal_prefix pclc_prfx;
179 struct smc_clc_ipv6_prefix pclc_prfx_ipv6[SMC_CLC_MAX_V6_PREFIX];
180 struct smc_clc_v2_extension pclc_v2_ext;
181 u8 user_eids[SMC_CLC_MAX_UEID][SMC_MAX_EID_LEN];
182 struct smc_clc_smcd_v2_extension pclc_smcd_v2_ext;
183 struct smc_clc_smcd_gid_chid pclc_gidchids[SMC_MAX_ISM_DEVS];
184 struct smc_clc_msg_trail pclc_trl;
187 struct smcr_clc_msg_accept_confirm { /* SMCR accept/confirm */
188 struct smc_clc_msg_local lcl;
189 u8 qpn[3]; /* QP number */
190 __be32 rmb_rkey; /* RMB rkey */
191 u8 rmbe_idx; /* Index of RMBE in RMB */
192 __be32 rmbe_alert_token; /* unique connection id */
193 #if defined(__BIG_ENDIAN_BITFIELD)
194 u8 rmbe_size : 4, /* buf size (compressed) */
195 qp_mtu : 4; /* QP mtu */
196 #elif defined(__LITTLE_ENDIAN_BITFIELD)
201 __be64 rmb_dma_addr; /* RMB virtual address */
203 u8 psn[3]; /* packet sequence number */
206 struct smcd_clc_msg_accept_confirm_common { /* SMCD accept/confirm */
207 u64 gid; /* Sender GID */
208 u64 token; /* DMB token */
209 u8 dmbe_idx; /* DMBE index */
210 #if defined(__BIG_ENDIAN_BITFIELD)
211 u8 dmbe_size : 4, /* buf size (compressed) */
213 #elif defined(__LITTLE_ENDIAN_BITFIELD)
218 __be32 linkid; /* Link identifier */
221 #define SMC_CLC_OS_ZOS 1
222 #define SMC_CLC_OS_LINUX 2
223 #define SMC_CLC_OS_AIX 3
225 struct smc_clc_first_contact_ext {
226 #if defined(__BIG_ENDIAN_BITFIELD)
231 #elif defined(__LITTLE_ENDIAN_BITFIELD)
238 u8 hostname[SMC_MAX_HOSTNAME_LEN];
241 struct smc_clc_first_contact_ext_v2x {
242 struct smc_clc_first_contact_ext fce_v2_base;
243 u8 max_conns; /* for SMC-R only */
244 u8 max_links; /* for SMC-R only */
246 __be32 vendor_exp_options;
248 } __packed; /* format defined in
249 * IBM Shared Memory Communications Version 2 (Third Edition)
250 * (https://www.ibm.com/support/pages/node/7009315)
253 struct smc_clc_fce_gid_ext {
256 u8 gid[][SMC_GID_SIZE];
259 struct smc_clc_msg_accept_confirm { /* clc accept / confirm message */
260 struct smc_clc_msg_hdr hdr;
262 struct smcr_clc_msg_accept_confirm r0; /* SMC-R */
264 struct smcd_clc_msg_accept_confirm_common d0;
268 } __packed; /* format defined in RFC7609 */
270 struct smc_clc_msg_accept_confirm_v2 { /* clc accept / confirm message */
271 struct smc_clc_msg_hdr hdr;
274 struct smcr_clc_msg_accept_confirm r0;
275 u8 eid[SMC_MAX_EID_LEN];
279 struct smcd_clc_msg_accept_confirm_common d0;
281 u8 eid[SMC_MAX_EID_LEN];
287 struct smc_clc_msg_decline { /* clc decline message */
288 struct smc_clc_msg_hdr hdr;
289 u8 id_for_peer[SMC_SYSTEMID_LEN]; /* sender peer_id */
290 __be32 peer_diagnosis; /* diagnosis information */
291 #if defined(__BIG_ENDIAN_BITFIELD)
294 #elif defined(__LITTLE_ENDIAN_BITFIELD)
299 struct smc_clc_msg_trail trl; /* eye catcher "SMCD" or "SMCR" EBCDIC */
302 #define SMC_DECL_DIAG_COUNT_V2 4 /* no. of additional peer diagnosis codes */
304 struct smc_clc_msg_decline_v2 { /* clc decline message */
305 struct smc_clc_msg_hdr hdr;
306 u8 id_for_peer[SMC_SYSTEMID_LEN]; /* sender peer_id */
307 __be32 peer_diagnosis; /* diagnosis information */
308 #if defined(__BIG_ENDIAN_BITFIELD)
311 #elif defined(__LITTLE_ENDIAN_BITFIELD)
316 __be32 peer_diagnosis_v2[SMC_DECL_DIAG_COUNT_V2];
317 struct smc_clc_msg_trail trl; /* eye catcher "SMCD" or "SMCR" EBCDIC */
320 /* determine start of the prefix area within the proposal message */
321 static inline struct smc_clc_msg_proposal_prefix *
322 smc_clc_proposal_get_prefix(struct smc_clc_msg_proposal *pclc)
324 return (struct smc_clc_msg_proposal_prefix *)
325 ((u8 *)pclc + sizeof(*pclc) + ntohs(pclc->iparea_offset));
328 static inline bool smcr_indicated(int smc_type)
330 return smc_type == SMC_TYPE_R || smc_type == SMC_TYPE_B;
333 static inline bool smcd_indicated(int smc_type)
335 return smc_type == SMC_TYPE_D || smc_type == SMC_TYPE_B;
338 static inline u8 smc_indicated_type(int is_smcd, int is_smcr)
340 if (is_smcd && is_smcr)
349 /* get SMC-D info from proposal message */
350 static inline struct smc_clc_msg_smcd *
351 smc_get_clc_msg_smcd(struct smc_clc_msg_proposal *prop)
353 if (smcd_indicated(prop->hdr.typev1) &&
354 ntohs(prop->iparea_offset) != sizeof(struct smc_clc_msg_smcd))
357 return (struct smc_clc_msg_smcd *)(prop + 1);
360 static inline struct smc_clc_v2_extension *
361 smc_get_clc_v2_ext(struct smc_clc_msg_proposal *prop)
363 struct smc_clc_msg_smcd *prop_smcd = smc_get_clc_msg_smcd(prop);
365 if (!prop_smcd || !ntohs(prop_smcd->v2_ext_offset))
368 return (struct smc_clc_v2_extension *)
370 offsetof(struct smc_clc_msg_smcd, v2_ext_offset) +
371 sizeof(prop_smcd->v2_ext_offset) +
372 ntohs(prop_smcd->v2_ext_offset));
375 static inline struct smc_clc_smcd_v2_extension *
376 smc_get_clc_smcd_v2_ext(struct smc_clc_v2_extension *prop_v2ext)
380 if (!ntohs(prop_v2ext->hdr.smcd_v2_ext_offset))
383 return (struct smc_clc_smcd_v2_extension *)
385 offsetof(struct smc_clc_v2_extension, hdr) +
386 offsetof(struct smc_clnt_opts_area_hdr, smcd_v2_ext_offset) +
387 sizeof(prop_v2ext->hdr.smcd_v2_ext_offset) +
388 ntohs(prop_v2ext->hdr.smcd_v2_ext_offset));
391 static inline struct smc_clc_first_contact_ext *
392 smc_get_clc_first_contact_ext(struct smc_clc_msg_accept_confirm_v2 *clc_v2,
397 if (clc_v2->hdr.version == SMC_V1 ||
398 !(clc_v2->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
403 offsetofend(struct smc_clc_msg_accept_confirm_v2, d1);
406 offsetofend(struct smc_clc_msg_accept_confirm_v2, r1);
408 return (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) +
413 struct smc_init_info;
415 int smc_clc_prfx_match(struct socket *clcsock,
416 struct smc_clc_msg_proposal_prefix *prop);
417 int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
418 u8 expected_type, unsigned long timeout);
419 int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info, u8 version);
420 int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini);
421 int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
422 u8 version, u8 *eid, struct smc_init_info *ini);
423 int smc_clc_send_accept(struct smc_sock *smc, bool srv_first_contact,
424 u8 version, u8 *negotiated_eid, struct smc_init_info *ini);
425 int smc_clc_srv_v2x_features_validate(struct smc_clc_msg_proposal *pclc,
426 struct smc_init_info *ini);
427 int smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext *fce,
428 struct smc_init_info *ini);
429 int smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm *cclc,
430 struct smc_init_info *ini);
431 void smc_clc_init(void) __init;
432 void smc_clc_exit(void);
433 void smc_clc_get_hostname(u8 **host);
434 bool smc_clc_match_eid(u8 *negotiated_eid,
435 struct smc_clc_v2_extension *smc_v2_ext,
436 u8 *peer_eid, u8 *local_eid);
437 int smc_clc_ueid_count(void);
438 int smc_nl_dump_ueid(struct sk_buff *skb, struct netlink_callback *cb);
439 int smc_nl_add_ueid(struct sk_buff *skb, struct genl_info *info);
440 int smc_nl_remove_ueid(struct sk_buff *skb, struct genl_info *info);
441 int smc_nl_flush_ueid(struct sk_buff *skb, struct genl_info *info);
442 int smc_nl_dump_seid(struct sk_buff *skb, struct netlink_callback *cb);
443 int smc_nl_enable_seid(struct sk_buff *skb, struct genl_info *info);
444 int smc_nl_disable_seid(struct sk_buff *skb, struct genl_info *info);