1 /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
2 
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4 /*          Greg Joyce <greg@opengridcomputing.com> */
5 /* Copyright (c) 2008-2019, IBM Corporation */
6 /* Copyright (c) 2017, Open Grid Computing, Inc. */
7 
8 #ifndef _SIW_CM_H
9 #define _SIW_CM_H
10 
11 #include <net/sock.h>
12 #include <linux/tcp.h>
13 
14 #include <rdma/iw_cm.h>
15 
16 enum siw_cep_state {
17 	SIW_EPSTATE_IDLE = 1,
18 	SIW_EPSTATE_LISTENING,
19 	SIW_EPSTATE_CONNECTING,
20 	SIW_EPSTATE_AWAIT_MPAREQ,
21 	SIW_EPSTATE_RECVD_MPAREQ,
22 	SIW_EPSTATE_AWAIT_MPAREP,
23 	SIW_EPSTATE_RDMA_MODE,
24 	SIW_EPSTATE_CLOSED
25 };
26 
27 struct siw_mpa_info {
28 	struct mpa_rr hdr; /* peer mpa hdr in host byte order */
29 	struct mpa_v2_data v2_ctrl;
30 	struct mpa_v2_data v2_ctrl_req;
31 	char *pdata;
32 	int bytes_rcvd;
33 };
34 
35 struct siw_device;
36 
37 struct siw_cep {
38 	struct iw_cm_id *cm_id;
39 	struct siw_device *sdev;
40 	struct list_head devq;
41 	spinlock_t lock;
42 	struct kref ref;
43 	int in_use;
44 	wait_queue_head_t waitq;
45 	enum siw_cep_state state;
46 
47 	struct list_head listenq;
48 	struct siw_cep *listen_cep;
49 
50 	struct siw_qp *qp;
51 	struct socket *sock;
52 
53 	struct siw_cm_work *mpa_timer;
54 	struct list_head work_freelist;
55 
56 	struct siw_mpa_info mpa;
57 	int ord;
58 	int ird;
59 	bool enhanced_rdma_conn_est;
60 
61 	/* Saved upcalls of socket */
62 	void (*sk_state_change)(struct sock *sk);
63 	void (*sk_data_ready)(struct sock *sk);
64 	void (*sk_write_space)(struct sock *sk);
65 	void (*sk_error_report)(struct sock *sk);
66 };
67 
68 /*
69  * Connection initiator waits 10 seconds to receive an
70  * MPA reply after sending out MPA request. Reponder waits for
71  * 5 seconds for MPA request to arrive if new TCP connection
72  * was set up.
73  */
74 #define MPAREQ_TIMEOUT (HZ * 10)
75 #define MPAREP_TIMEOUT (HZ * 5)
76 
77 enum siw_work_type {
78 	SIW_CM_WORK_ACCEPT = 1,
79 	SIW_CM_WORK_READ_MPAHDR,
80 	SIW_CM_WORK_CLOSE_LLP, /* close socket */
81 	SIW_CM_WORK_PEER_CLOSE, /* socket indicated peer close */
82 	SIW_CM_WORK_MPATIMEOUT
83 };
84 
85 struct siw_cm_work {
86 	struct delayed_work work;
87 	struct list_head list;
88 	enum siw_work_type type;
89 	struct siw_cep *cep;
90 };
91 
92 #define to_sockaddr_in(a) (*(struct sockaddr_in *)(&(a)))
93 #define to_sockaddr_in6(a) (*(struct sockaddr_in6 *)(&(a)))
94 
getname_peer(struct socket * s,struct sockaddr_storage * a)95 static inline int getname_peer(struct socket *s, struct sockaddr_storage *a)
96 {
97 	return s->ops->getname(s, (struct sockaddr *)a, 1);
98 }
99 
getname_local(struct socket * s,struct sockaddr_storage * a)100 static inline int getname_local(struct socket *s, struct sockaddr_storage *a)
101 {
102 	return s->ops->getname(s, (struct sockaddr *)a, 0);
103 }
104 
ksock_recv(struct socket * sock,char * buf,size_t size,int flags)105 static inline int ksock_recv(struct socket *sock, char *buf, size_t size,
106 			     int flags)
107 {
108 	struct kvec iov = { buf, size };
109 	struct msghdr msg = { .msg_name = NULL, .msg_flags = flags };
110 
111 	return kernel_recvmsg(sock, &msg, &iov, 1, size, flags);
112 }
113 
114 int siw_connect(struct iw_cm_id *id, struct iw_cm_conn_param *parm);
115 int siw_accept(struct iw_cm_id *id, struct iw_cm_conn_param *param);
116 int siw_reject(struct iw_cm_id *id, const void *data, u8 len);
117 int siw_create_listen(struct iw_cm_id *id, int backlog);
118 int siw_destroy_listen(struct iw_cm_id *id);
119 
120 void siw_cep_get(struct siw_cep *cep);
121 void siw_cep_put(struct siw_cep *cep);
122 int siw_cm_queue_work(struct siw_cep *cep, enum siw_work_type type);
123 
124 int siw_cm_init(void);
125 void siw_cm_exit(void);
126 
127 /*
128  * TCP socket interface
129  */
130 #define sk_to_qp(sk) (((struct siw_cep *)((sk)->sk_user_data))->qp)
131 #define sk_to_cep(sk) ((struct siw_cep *)((sk)->sk_user_data))
132 
133 #endif
134