blob: 6e44313e4467d01fbdc77f07ee7d14c50ab92ce2 [file] [log] [blame]
Ursula Braunac713872017-01-09 16:55:13 +01001/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * Definitions for the SMC module (socket related)
5 *
6 * Copyright IBM Corp. 2016
7 *
8 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
9 */
10#ifndef __SMC_H
11#define __SMC_H
12
13#include <linux/socket.h>
14#include <linux/types.h>
Ursula Braunf38ba1792017-01-09 16:55:19 +010015#include <linux/compiler.h> /* __aligned */
Ursula Braunac713872017-01-09 16:55:13 +010016#include <net/sock.h>
17
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010018#include "smc_ib.h"
19
Ursula Braunac713872017-01-09 16:55:13 +010020#define SMCPROTO_SMC 0 /* SMC protocol */
21
Ursula Brauna4cf0442017-01-09 16:55:14 +010022#define SMC_MAX_PORTS 2 /* Max # of ports */
23
Ursula Braunf16a7dd2017-01-09 16:55:26 +010024extern struct proto smc_proto;
25
Ursula Braun5f083182017-01-09 16:55:22 +010026#ifdef ATOMIC64_INIT
27#define KERNEL_HAS_ATOMIC64
28#endif
29
Ursula Braunac713872017-01-09 16:55:13 +010030enum smc_state { /* possible states of an SMC socket */
31 SMC_ACTIVE = 1,
32 SMC_INIT = 2,
33 SMC_CLOSED = 7,
34 SMC_LISTEN = 10,
Ursula Braunb38d7322017-01-09 16:55:25 +010035 /* normal close */
36 SMC_PEERCLOSEWAIT1 = 20,
37 SMC_PEERCLOSEWAIT2 = 21,
38 SMC_APPFINCLOSEWAIT = 24,
39 SMC_APPCLOSEWAIT1 = 22,
40 SMC_APPCLOSEWAIT2 = 23,
41 SMC_PEERFINCLOSEWAIT = 25,
42 /* abnormal close */
43 SMC_PEERABORTWAIT = 26,
44 SMC_PROCESSABORT = 27,
Ursula Braunac713872017-01-09 16:55:13 +010045};
46
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010047struct smc_link_group;
48
Ursula Braunf38ba1792017-01-09 16:55:19 +010049struct smc_wr_rx_hdr { /* common prefix part of LLC and CDC to demultiplex */
50 u8 type;
51} __aligned(1);
52
Ursula Braun5f083182017-01-09 16:55:22 +010053struct smc_cdc_conn_state_flags {
54#if defined(__BIG_ENDIAN_BITFIELD)
55 u8 peer_done_writing : 1; /* Sending done indicator */
56 u8 peer_conn_closed : 1; /* Peer connection closed indicator */
57 u8 peer_conn_abort : 1; /* Abnormal close indicator */
58 u8 reserved : 5;
59#elif defined(__LITTLE_ENDIAN_BITFIELD)
60 u8 reserved : 5;
61 u8 peer_conn_abort : 1;
62 u8 peer_conn_closed : 1;
63 u8 peer_done_writing : 1;
64#endif
65};
66
67struct smc_cdc_producer_flags {
68#if defined(__BIG_ENDIAN_BITFIELD)
69 u8 write_blocked : 1; /* Writing Blocked, no rx buf space */
70 u8 urg_data_pending : 1; /* Urgent Data Pending */
71 u8 urg_data_present : 1; /* Urgent Data Present */
72 u8 cons_curs_upd_req : 1; /* cursor update requested */
73 u8 failover_validation : 1;/* message replay due to failover */
74 u8 reserved : 3;
75#elif defined(__LITTLE_ENDIAN_BITFIELD)
76 u8 reserved : 3;
77 u8 failover_validation : 1;
78 u8 cons_curs_upd_req : 1;
79 u8 urg_data_present : 1;
80 u8 urg_data_pending : 1;
81 u8 write_blocked : 1;
82#endif
83};
84
85/* in host byte order */
86union smc_host_cursor { /* SMC cursor - an offset in an RMBE */
87 struct {
88 u16 reserved;
89 u16 wrap; /* window wrap sequence number */
90 u32 count; /* cursor (= offset) part */
91 };
92#ifdef KERNEL_HAS_ATOMIC64
93 atomic64_t acurs; /* for atomic processing */
94#else
95 u64 acurs; /* for atomic processing */
96#endif
97} __aligned(8);
98
99/* in host byte order, except for flag bitfields in network byte order */
100struct smc_host_cdc_msg { /* Connection Data Control message */
101 struct smc_wr_rx_hdr common; /* .type = 0xFE */
102 u8 len; /* length = 44 */
103 u16 seqno; /* connection seq # */
104 u32 token; /* alert_token */
105 union smc_host_cursor prod; /* producer cursor */
106 union smc_host_cursor cons; /* consumer cursor,
107 * piggy backed "ack"
108 */
109 struct smc_cdc_producer_flags prod_flags; /* conn. tx/rx status */
110 struct smc_cdc_conn_state_flags conn_state_flags; /* peer conn. status*/
111 u8 reserved[18];
112} __aligned(8);
113
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100114struct smc_connection {
115 struct rb_node alert_node;
116 struct smc_link_group *lgr; /* link group of connection */
117 u32 alert_token_local; /* unique conn. id */
118 u8 peer_conn_idx; /* from tcp handshake */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100119 int peer_rmbe_size; /* size of peer rx buffer */
120 atomic_t peer_rmbe_space;/* remaining free bytes in peer
121 * rmbe
122 */
Ursula Braunbd4ad572017-01-09 16:55:20 +0100123 int rtoken_idx; /* idx to peer RMB rkey/addr */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100124
125 struct smc_buf_desc *sndbuf_desc; /* send buffer descriptor */
126 int sndbuf_size; /* sndbuf size <== sock wmem */
127 struct smc_buf_desc *rmb_desc; /* RMBE descriptor */
128 int rmbe_size; /* RMBE size <== sock rmem */
129 int rmbe_size_short;/* compressed notation */
Ursula Braun952310c2017-01-09 16:55:24 +0100130 int rmbe_update_limit;
131 /* lower limit for consumer
132 * cursor update
133 */
Ursula Braun5f083182017-01-09 16:55:22 +0100134
135 struct smc_host_cdc_msg local_tx_ctrl; /* host byte order staging
136 * buffer for CDC msg send
137 * .prod cf. TCP snd_nxt
138 * .cons cf. TCP sends ack
139 */
140 union smc_host_cursor tx_curs_prep; /* tx - prepared data
141 * snd_max..wmem_alloc
142 */
143 union smc_host_cursor tx_curs_sent; /* tx - sent data
144 * snd_nxt ?
145 */
146 union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer
147 * snd-wnd-begin ?
148 */
149 atomic_t sndbuf_space; /* remaining space in sndbuf */
150 u16 tx_cdc_seq; /* sequence # for CDC send */
151 spinlock_t send_lock; /* protect wr_sends */
Ursula Braune6727f32017-01-09 16:55:23 +0100152 struct work_struct tx_work; /* retry of smc_cdc_msg_send */
Ursula Braun5f083182017-01-09 16:55:22 +0100153
154 struct smc_host_cdc_msg local_rx_ctrl; /* filled during event_handl.
155 * .prod cf. TCP rcv_nxt
156 * .cons cf. TCP snd_una
157 */
158 union smc_host_cursor rx_curs_confirmed; /* confirmed to peer
159 * source of snd_una ?
160 */
161 atomic_t bytes_to_rcv; /* arrived data,
162 * not yet received
163 */
164#ifndef KERNEL_HAS_ATOMIC64
165 spinlock_t acurs_lock; /* protect cursors */
166#endif
Ursula Braun46c28db2017-04-10 14:58:01 +0200167 struct work_struct close_work; /* peer sent some closing */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100168};
169
Ursula Braunac713872017-01-09 16:55:13 +0100170struct smc_sock { /* smc sock container */
171 struct sock sk;
172 struct socket *clcsock; /* internal tcp socket */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100173 struct smc_connection conn; /* smc connection */
Ursula Brauna046d572017-01-09 16:55:16 +0100174 struct sockaddr *addr; /* inet connect address */
175 struct smc_sock *listen_smc; /* listen parent */
176 struct work_struct tcp_listen_work;/* handle tcp socket accepts */
177 struct work_struct smc_listen_work;/* prepare new accept socket */
178 struct list_head accept_q; /* sockets to be accepted */
179 spinlock_t accept_q_lock; /* protects accept_q */
Ursula Braunb38d7322017-01-09 16:55:25 +0100180 struct delayed_work sock_put_work; /* final socket freeing */
Ursula Braunac713872017-01-09 16:55:13 +0100181 bool use_fallback; /* fallback to tcp */
Ursula Braunb38d7322017-01-09 16:55:25 +0100182 u8 wait_close_tx_prepared : 1;
183 /* shutdown wr or close
184 * started, waiting for unsent
185 * data to be sent
186 */
Ursula Braunac713872017-01-09 16:55:13 +0100187};
188
189static inline struct smc_sock *smc_sk(const struct sock *sk)
190{
191 return (struct smc_sock *)sk;
192}
193
Ursula Brauna4cf0442017-01-09 16:55:14 +0100194#define SMC_SYSTEMID_LEN 8
195
196extern u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
197
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100198/* convert an u32 value into network byte order, store it into a 3 byte field */
199static inline void hton24(u8 *net, u32 host)
200{
201 __be32 t;
202
203 t = cpu_to_be32(host);
204 memcpy(net, ((u8 *)&t) + 1, 3);
205}
206
207/* convert a received 3 byte field into host byte order*/
208static inline u32 ntoh24(u8 *net)
209{
210 __be32 t = 0;
211
212 memcpy(((u8 *)&t) + 1, net, 3);
213 return be32_to_cpu(t);
214}
215
Ursula Brauncd6851f2017-01-09 16:55:18 +0100216#define SMC_BUF_MIN_SIZE 16384 /* minimum size of an RMB */
217
218#define SMC_RMBE_SIZES 16 /* number of distinct sizes for an RMBE */
219/* theoretically, the RFC states that largest size would be 512K,
220 * i.e. compressed 5 and thus 6 sizes (0..5), despite
221 * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
222 */
223
224/* convert the RMB size into the compressed notation - minimum 16K.
225 * In contrast to plain ilog2, this rounds towards the next power of 2,
226 * so the socket application gets at least its desired sndbuf / rcvbuf size.
227 */
228static inline u8 smc_compress_bufsize(int size)
229{
230 u8 compressed;
231
232 if (size <= SMC_BUF_MIN_SIZE)
233 return 0;
234
235 size = (size - 1) >> 14;
236 compressed = ilog2(size) + 1;
237 if (compressed >= SMC_RMBE_SIZES)
238 compressed = SMC_RMBE_SIZES - 1;
239 return compressed;
240}
241
242/* convert the RMB size from compressed notation into integer */
243static inline int smc_uncompress_bufsize(u8 compressed)
244{
245 u32 size;
246
247 size = 0x00000001 << (((int)compressed) + 14);
248 return (int)size;
249}
250
Ursula Brauna046d572017-01-09 16:55:16 +0100251#ifdef CONFIG_XFRM
252static inline bool using_ipsec(struct smc_sock *smc)
253{
254 return (smc->clcsock->sk->sk_policy[0] ||
255 smc->clcsock->sk->sk_policy[1]) ? 1 : 0;
256}
257#else
258static inline bool using_ipsec(struct smc_sock *smc)
259{
260 return 0;
261}
262#endif
263
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100264struct smc_clc_msg_local;
265
Ursula Brauna046d572017-01-09 16:55:16 +0100266int smc_netinfo_by_tcpsk(struct socket *clcsock, __be32 *subnet,
267 u8 *prefix_len);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100268void smc_conn_free(struct smc_connection *conn);
269int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr,
270 struct smc_ib_device *smcibdev, u8 ibport,
271 struct smc_clc_msg_local *lcl, int srv_first_contact);
Ursula Braunb38d7322017-01-09 16:55:25 +0100272struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
273void smc_close_non_accepted(struct sock *sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100274
Ursula Braunac713872017-01-09 16:55:13 +0100275#endif /* __SMC_H */