1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 
3 /* Authors: Cheng Xu <chengyou@linux.alibaba.com> */
4 /*          Kai Shen <kaishen@linux.alibaba.com> */
5 /* Copyright (c) 2020-2022, Alibaba Group. */
6 
7 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
8 /*          Greg Joyce <greg@opengridcomputing.com> */
9 /* Copyright (c) 2008-2019, IBM Corporation */
10 /* Copyright (c) 2017, Open Grid Computing, Inc. */
11 
12 #ifndef __ERDMA_CM_H__
13 #define __ERDMA_CM_H__
14 
15 #include <linux/tcp.h>
16 #include <net/sock.h>
17 #include <rdma/iw_cm.h>
18 
19 /* iWarp MPA protocol defs */
20 #define MPA_REVISION_EXT_1 129
21 #define MPA_MAX_PRIVDATA RDMA_MAX_PRIVATE_DATA
22 #define MPA_KEY_REQ "MPA ID Req Frame"
23 #define MPA_KEY_REP "MPA ID Rep Frame"
24 #define MPA_KEY_SIZE 16
25 #define MPA_DEFAULT_HDR_LEN 28
26 
27 struct mpa_rr_params {
28 	__be16 bits;
29 	__be16 pd_len;
30 };
31 
32 /*
33  * MPA request/response Hdr bits & fields
34  */
35 enum {
36 	MPA_RR_FLAG_MARKERS = __cpu_to_be16(0x8000),
37 	MPA_RR_FLAG_CRC = __cpu_to_be16(0x4000),
38 	MPA_RR_FLAG_REJECT = __cpu_to_be16(0x2000),
39 	MPA_RR_RESERVED = __cpu_to_be16(0x1f00),
40 	MPA_RR_MASK_REVISION = __cpu_to_be16(0x00ff)
41 };
42 
43 /*
44  * MPA request/reply header
45  */
46 struct mpa_rr {
47 	u8 key[16];
48 	struct mpa_rr_params params;
49 };
50 
51 struct erdma_mpa_ext {
52 	__be32 cookie;
53 	__be32 bits;
54 };
55 
56 enum {
57 	MPA_EXT_FLAG_CC = cpu_to_be32(0x0000000f),
58 };
59 
60 struct erdma_mpa_info {
61 	struct mpa_rr hdr; /* peer mpa hdr in host byte order */
62 	struct erdma_mpa_ext ext_data;
63 	char *pdata;
64 	int bytes_rcvd;
65 };
66 
67 struct erdma_sk_upcalls {
68 	void (*sk_state_change)(struct sock *sk);
69 	void (*sk_data_ready)(struct sock *sk, int bytes);
70 	void (*sk_error_report)(struct sock *sk);
71 };
72 
73 struct erdma_dev;
74 
75 enum erdma_cep_state {
76 	ERDMA_EPSTATE_IDLE = 1,
77 	ERDMA_EPSTATE_LISTENING,
78 	ERDMA_EPSTATE_CONNECTING,
79 	ERDMA_EPSTATE_AWAIT_MPAREQ,
80 	ERDMA_EPSTATE_RECVD_MPAREQ,
81 	ERDMA_EPSTATE_AWAIT_MPAREP,
82 	ERDMA_EPSTATE_RDMA_MODE,
83 	ERDMA_EPSTATE_CLOSED
84 };
85 
86 struct erdma_cep {
87 	struct iw_cm_id *cm_id;
88 	struct erdma_dev *dev;
89 	struct list_head devq;
90 	spinlock_t lock;
91 	struct kref ref;
92 	int in_use;
93 	wait_queue_head_t waitq;
94 	enum erdma_cep_state state;
95 
96 	struct list_head listenq;
97 	struct erdma_cep *listen_cep;
98 
99 	struct erdma_qp *qp;
100 	struct socket *sock;
101 
102 	struct erdma_cm_work *mpa_timer;
103 	struct list_head work_freelist;
104 
105 	struct erdma_mpa_info mpa;
106 	int ord;
107 	int ird;
108 
109 	int pd_len;
110 	/* hold user's private data. */
111 	void *private_data;
112 
113 	/* Saved upcalls of socket llp.sock */
114 	void (*sk_state_change)(struct sock *sk);
115 	void (*sk_data_ready)(struct sock *sk);
116 	void (*sk_error_report)(struct sock *sk);
117 };
118 
119 #define MPAREQ_TIMEOUT (HZ * 20)
120 #define MPAREP_TIMEOUT (HZ * 10)
121 #define CONNECT_TIMEOUT (HZ * 10)
122 
123 enum erdma_work_type {
124 	ERDMA_CM_WORK_ACCEPT = 1,
125 	ERDMA_CM_WORK_READ_MPAHDR,
126 	ERDMA_CM_WORK_CLOSE_LLP, /* close socket */
127 	ERDMA_CM_WORK_PEER_CLOSE, /* socket indicated peer close */
128 	ERDMA_CM_WORK_MPATIMEOUT,
129 	ERDMA_CM_WORK_CONNECTED,
130 	ERDMA_CM_WORK_CONNECTTIMEOUT
131 };
132 
133 struct erdma_cm_work {
134 	struct delayed_work work;
135 	struct list_head list;
136 	enum erdma_work_type type;
137 	struct erdma_cep *cep;
138 };
139 
140 #define to_sockaddr_in(a) (*(struct sockaddr_in *)(&(a)))
141 
142 static inline int getname_peer(struct socket *s, struct sockaddr_storage *a)
143 {
144 	return s->ops->getname(s, (struct sockaddr *)a, 1);
145 }
146 
147 static inline int getname_local(struct socket *s, struct sockaddr_storage *a)
148 {
149 	return s->ops->getname(s, (struct sockaddr *)a, 0);
150 }
151 
152 int erdma_connect(struct iw_cm_id *id, struct iw_cm_conn_param *param);
153 int erdma_accept(struct iw_cm_id *id, struct iw_cm_conn_param *param);
154 int erdma_reject(struct iw_cm_id *id, const void *pdata, u8 plen);
155 int erdma_create_listen(struct iw_cm_id *id, int backlog);
156 int erdma_destroy_listen(struct iw_cm_id *id);
157 
158 void erdma_cep_get(struct erdma_cep *ceq);
159 void erdma_cep_put(struct erdma_cep *ceq);
160 int erdma_cm_queue_work(struct erdma_cep *ceq, enum erdma_work_type type);
161 
162 int erdma_cm_init(void);
163 void erdma_cm_exit(void);
164 
165 #define sk_to_cep(sk) ((struct erdma_cep *)((sk)->sk_user_data))
166 
167 #endif
168