1 /* 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> 15 #include <linux/compiler.h> /* __aligned */ 16 #include <net/sock.h> 17 18 #include "smc_ib.h" 19 20 #define SMCPROTO_SMC 0 /* SMC protocol */ 21 22 #define SMC_MAX_PORTS 2 /* Max # of ports */ 23 24 extern struct proto smc_proto; 25 26 #ifdef ATOMIC64_INIT 27 #define KERNEL_HAS_ATOMIC64 28 #endif 29 30 enum smc_state { /* possible states of an SMC socket */ 31 SMC_ACTIVE = 1, 32 SMC_INIT = 2, 33 SMC_CLOSED = 7, 34 SMC_LISTEN = 10, 35 /* 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, 45 }; 46 47 struct smc_link_group; 48 49 struct smc_wr_rx_hdr { /* common prefix part of LLC and CDC to demultiplex */ 50 u8 type; 51 } __aligned(1); 52 53 struct 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 67 struct 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 */ 86 union 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 */ 100 struct 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 114 struct 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 */ 119 int peer_rmbe_size; /* size of peer rx buffer */ 120 atomic_t peer_rmbe_space;/* remaining free bytes in peer 121 * rmbe 122 */ 123 int rtoken_idx; /* idx to peer RMB rkey/addr */ 124 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 */ 130 int rmbe_update_limit; 131 /* lower limit for consumer 132 * cursor update 133 */ 134 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 */ 152 struct work_struct tx_work; /* retry of smc_cdc_msg_send */ 153 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 167 }; 168 169 struct smc_sock { /* smc sock container */ 170 struct sock sk; 171 struct socket *clcsock; /* internal tcp socket */ 172 struct smc_connection conn; /* smc connection */ 173 struct sockaddr *addr; /* inet connect address */ 174 struct smc_sock *listen_smc; /* listen parent */ 175 struct work_struct tcp_listen_work;/* handle tcp socket accepts */ 176 struct work_struct smc_listen_work;/* prepare new accept socket */ 177 struct list_head accept_q; /* sockets to be accepted */ 178 spinlock_t accept_q_lock; /* protects accept_q */ 179 struct delayed_work sock_put_work; /* final socket freeing */ 180 bool use_fallback; /* fallback to tcp */ 181 u8 wait_close_tx_prepared : 1; 182 /* shutdown wr or close 183 * started, waiting for unsent 184 * data to be sent 185 */ 186 }; 187 188 static inline struct smc_sock *smc_sk(const struct sock *sk) 189 { 190 return (struct smc_sock *)sk; 191 } 192 193 #define SMC_SYSTEMID_LEN 8 194 195 extern u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */ 196 197 /* convert an u32 value into network byte order, store it into a 3 byte field */ 198 static inline void hton24(u8 *net, u32 host) 199 { 200 __be32 t; 201 202 t = cpu_to_be32(host); 203 memcpy(net, ((u8 *)&t) + 1, 3); 204 } 205 206 /* convert a received 3 byte field into host byte order*/ 207 static inline u32 ntoh24(u8 *net) 208 { 209 __be32 t = 0; 210 211 memcpy(((u8 *)&t) + 1, net, 3); 212 return be32_to_cpu(t); 213 } 214 215 #define SMC_BUF_MIN_SIZE 16384 /* minimum size of an RMB */ 216 217 #define SMC_RMBE_SIZES 16 /* number of distinct sizes for an RMBE */ 218 /* theoretically, the RFC states that largest size would be 512K, 219 * i.e. compressed 5 and thus 6 sizes (0..5), despite 220 * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15) 221 */ 222 223 /* convert the RMB size into the compressed notation - minimum 16K. 224 * In contrast to plain ilog2, this rounds towards the next power of 2, 225 * so the socket application gets at least its desired sndbuf / rcvbuf size. 226 */ 227 static inline u8 smc_compress_bufsize(int size) 228 { 229 u8 compressed; 230 231 if (size <= SMC_BUF_MIN_SIZE) 232 return 0; 233 234 size = (size - 1) >> 14; 235 compressed = ilog2(size) + 1; 236 if (compressed >= SMC_RMBE_SIZES) 237 compressed = SMC_RMBE_SIZES - 1; 238 return compressed; 239 } 240 241 /* convert the RMB size from compressed notation into integer */ 242 static inline int smc_uncompress_bufsize(u8 compressed) 243 { 244 u32 size; 245 246 size = 0x00000001 << (((int)compressed) + 14); 247 return (int)size; 248 } 249 250 #ifdef CONFIG_XFRM 251 static inline bool using_ipsec(struct smc_sock *smc) 252 { 253 return (smc->clcsock->sk->sk_policy[0] || 254 smc->clcsock->sk->sk_policy[1]) ? 1 : 0; 255 } 256 #else 257 static inline bool using_ipsec(struct smc_sock *smc) 258 { 259 return 0; 260 } 261 #endif 262 263 struct smc_clc_msg_local; 264 265 int smc_netinfo_by_tcpsk(struct socket *clcsock, __be32 *subnet, 266 u8 *prefix_len); 267 void smc_conn_free(struct smc_connection *conn); 268 int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr, 269 struct smc_ib_device *smcibdev, u8 ibport, 270 struct smc_clc_msg_local *lcl, int srv_first_contact); 271 struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock); 272 void smc_close_non_accepted(struct sock *sk); 273 274 #endif /* __SMC_H */ 275