Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 4 | * |
| 5 | * Connection Data Control (CDC) |
| 6 | * |
| 7 | * Copyright IBM Corp. 2016 |
| 8 | * |
| 9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 10 | */ |
| 11 | |
| 12 | #ifndef SMC_CDC_H |
| 13 | #define SMC_CDC_H |
| 14 | |
| 15 | #include <linux/kernel.h> /* max_t */ |
| 16 | #include <linux/atomic.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/compiler.h> |
| 19 | |
| 20 | #include "smc.h" |
| 21 | #include "smc_core.h" |
| 22 | #include "smc_wr.h" |
| 23 | |
| 24 | #define SMC_CDC_MSG_TYPE 0xFE |
| 25 | |
| 26 | /* in network byte order */ |
| 27 | union smc_cdc_cursor { /* SMC cursor */ |
| 28 | struct { |
| 29 | __be16 reserved; |
| 30 | __be16 wrap; |
| 31 | __be32 count; |
| 32 | }; |
| 33 | #ifdef KERNEL_HAS_ATOMIC64 |
| 34 | atomic64_t acurs; /* for atomic processing */ |
| 35 | #else |
| 36 | u64 acurs; /* for atomic processing */ |
| 37 | #endif |
| 38 | } __aligned(8); |
| 39 | |
| 40 | /* in network byte order */ |
| 41 | struct smc_cdc_msg { |
| 42 | struct smc_wr_rx_hdr common; /* .type = 0xFE */ |
| 43 | u8 len; /* 44 */ |
| 44 | __be16 seqno; |
| 45 | __be32 token; |
| 46 | union smc_cdc_cursor prod; |
| 47 | union smc_cdc_cursor cons; /* piggy backed "ack" */ |
| 48 | struct smc_cdc_producer_flags prod_flags; |
| 49 | struct smc_cdc_conn_state_flags conn_state_flags; |
| 50 | u8 reserved[18]; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* SMC-D cursor format */ |
| 54 | union smcd_cdc_cursor { |
| 55 | struct { |
| 56 | u16 wrap; |
| 57 | u32 count; |
| 58 | struct smc_cdc_producer_flags prod_flags; |
| 59 | struct smc_cdc_conn_state_flags conn_state_flags; |
| 60 | } __packed; |
| 61 | #ifdef KERNEL_HAS_ATOMIC64 |
| 62 | atomic64_t acurs; /* for atomic processing */ |
| 63 | #else |
| 64 | u64 acurs; /* for atomic processing */ |
| 65 | #endif |
| 66 | } __aligned(8); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 67 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 68 | /* CDC message for SMC-D */ |
| 69 | struct smcd_cdc_msg { |
| 70 | struct smc_wr_rx_hdr common; /* Type = 0xFE */ |
| 71 | u8 res1[7]; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 72 | union smcd_cdc_cursor prod; |
| 73 | union smcd_cdc_cursor cons; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 74 | u8 res3[8]; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 75 | } __aligned(8); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 76 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 77 | static inline bool smc_cdc_rxed_any_close(struct smc_connection *conn) |
| 78 | { |
| 79 | return conn->local_rx_ctrl.conn_state_flags.peer_conn_abort || |
| 80 | conn->local_rx_ctrl.conn_state_flags.peer_conn_closed; |
| 81 | } |
| 82 | |
| 83 | static inline bool smc_cdc_rxed_any_close_or_senddone( |
| 84 | struct smc_connection *conn) |
| 85 | { |
| 86 | return smc_cdc_rxed_any_close(conn) || |
| 87 | conn->local_rx_ctrl.conn_state_flags.peer_done_writing; |
| 88 | } |
| 89 | |
| 90 | static inline void smc_curs_add(int size, union smc_host_cursor *curs, |
| 91 | int value) |
| 92 | { |
| 93 | curs->count += value; |
| 94 | if (curs->count >= size) { |
| 95 | curs->wrap++; |
| 96 | curs->count -= size; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* SMC cursors are 8 bytes long and require atomic reading and writing */ |
| 101 | static inline u64 smc_curs_read(union smc_host_cursor *curs, |
| 102 | struct smc_connection *conn) |
| 103 | { |
| 104 | #ifndef KERNEL_HAS_ATOMIC64 |
| 105 | unsigned long flags; |
| 106 | u64 ret; |
| 107 | |
| 108 | spin_lock_irqsave(&conn->acurs_lock, flags); |
| 109 | ret = curs->acurs; |
| 110 | spin_unlock_irqrestore(&conn->acurs_lock, flags); |
| 111 | return ret; |
| 112 | #else |
| 113 | return atomic64_read(&curs->acurs); |
| 114 | #endif |
| 115 | } |
| 116 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 117 | /* Copy cursor src into tgt */ |
| 118 | static inline void smc_curs_copy(union smc_host_cursor *tgt, |
| 119 | union smc_host_cursor *src, |
| 120 | struct smc_connection *conn) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 121 | { |
| 122 | #ifndef KERNEL_HAS_ATOMIC64 |
| 123 | unsigned long flags; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 124 | |
| 125 | spin_lock_irqsave(&conn->acurs_lock, flags); |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 126 | tgt->acurs = src->acurs; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 127 | spin_unlock_irqrestore(&conn->acurs_lock, flags); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 128 | #else |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 129 | atomic64_set(&tgt->acurs, atomic64_read(&src->acurs)); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 133 | static inline void smc_curs_copy_net(union smc_cdc_cursor *tgt, |
| 134 | union smc_cdc_cursor *src, |
| 135 | struct smc_connection *conn) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 136 | { |
| 137 | #ifndef KERNEL_HAS_ATOMIC64 |
| 138 | unsigned long flags; |
| 139 | |
| 140 | spin_lock_irqsave(&conn->acurs_lock, flags); |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 141 | tgt->acurs = src->acurs; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 142 | spin_unlock_irqrestore(&conn->acurs_lock, flags); |
| 143 | #else |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 144 | atomic64_set(&tgt->acurs, atomic64_read(&src->acurs)); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 145 | #endif |
| 146 | } |
| 147 | |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 148 | static inline void smcd_curs_copy(union smcd_cdc_cursor *tgt, |
| 149 | union smcd_cdc_cursor *src, |
| 150 | struct smc_connection *conn) |
| 151 | { |
| 152 | #ifndef KERNEL_HAS_ATOMIC64 |
| 153 | unsigned long flags; |
| 154 | |
| 155 | spin_lock_irqsave(&conn->acurs_lock, flags); |
| 156 | tgt->acurs = src->acurs; |
| 157 | spin_unlock_irqrestore(&conn->acurs_lock, flags); |
| 158 | #else |
| 159 | atomic64_set(&tgt->acurs, atomic64_read(&src->acurs)); |
| 160 | #endif |
| 161 | } |
| 162 | |
Ursula Braun | b8649ef | 2019-02-04 13:44:45 +0100 | [diff] [blame^] | 163 | /* calculate cursor difference between old and new, where old <= new and |
| 164 | * difference cannot exceed size |
| 165 | */ |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 166 | static inline int smc_curs_diff(unsigned int size, |
| 167 | union smc_host_cursor *old, |
| 168 | union smc_host_cursor *new) |
| 169 | { |
| 170 | if (old->wrap != new->wrap) |
| 171 | return max_t(int, 0, |
| 172 | ((size - old->count) + new->count)); |
| 173 | |
| 174 | return max_t(int, 0, (new->count - old->count)); |
| 175 | } |
| 176 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 177 | /* calculate cursor difference between old and new - returns negative |
| 178 | * value in case old > new |
| 179 | */ |
| 180 | static inline int smc_curs_comp(unsigned int size, |
| 181 | union smc_host_cursor *old, |
| 182 | union smc_host_cursor *new) |
| 183 | { |
| 184 | if (old->wrap > new->wrap || |
| 185 | (old->wrap == new->wrap && old->count > new->count)) |
| 186 | return -smc_curs_diff(size, new, old); |
| 187 | return smc_curs_diff(size, old, new); |
| 188 | } |
| 189 | |
Ursula Braun | b8649ef | 2019-02-04 13:44:45 +0100 | [diff] [blame^] | 190 | /* calculate cursor difference between old and new, where old <= new and |
| 191 | * difference may exceed size |
| 192 | */ |
| 193 | static inline int smc_curs_diff_large(unsigned int size, |
| 194 | union smc_host_cursor *old, |
| 195 | union smc_host_cursor *new) |
| 196 | { |
| 197 | if (old->wrap < new->wrap) |
| 198 | return min_t(int, |
| 199 | (size - old->count) + new->count + |
| 200 | (new->wrap - old->wrap - 1) * size, |
| 201 | size); |
| 202 | |
| 203 | if (old->wrap > new->wrap) /* wrap has switched from 0xffff to 0x0000 */ |
| 204 | return min_t(int, |
| 205 | (size - old->count) + new->count + |
| 206 | (new->wrap + 0xffff - old->wrap) * size, |
| 207 | size); |
| 208 | |
| 209 | return max_t(int, 0, (new->count - old->count)); |
| 210 | } |
| 211 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 212 | static inline void smc_host_cursor_to_cdc(union smc_cdc_cursor *peer, |
| 213 | union smc_host_cursor *local, |
| 214 | struct smc_connection *conn) |
| 215 | { |
| 216 | union smc_host_cursor temp; |
| 217 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 218 | smc_curs_copy(&temp, local, conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 219 | peer->count = htonl(temp.count); |
| 220 | peer->wrap = htons(temp.wrap); |
| 221 | /* peer->reserved = htons(0); must be ensured by caller */ |
| 222 | } |
| 223 | |
| 224 | static inline void smc_host_msg_to_cdc(struct smc_cdc_msg *peer, |
| 225 | struct smc_host_cdc_msg *local, |
| 226 | struct smc_connection *conn) |
| 227 | { |
| 228 | peer->common.type = local->common.type; |
| 229 | peer->len = local->len; |
| 230 | peer->seqno = htons(local->seqno); |
| 231 | peer->token = htonl(local->token); |
| 232 | smc_host_cursor_to_cdc(&peer->prod, &local->prod, conn); |
| 233 | smc_host_cursor_to_cdc(&peer->cons, &local->cons, conn); |
| 234 | peer->prod_flags = local->prod_flags; |
| 235 | peer->conn_state_flags = local->conn_state_flags; |
| 236 | } |
| 237 | |
| 238 | static inline void smc_cdc_cursor_to_host(union smc_host_cursor *local, |
| 239 | union smc_cdc_cursor *peer, |
| 240 | struct smc_connection *conn) |
| 241 | { |
| 242 | union smc_host_cursor temp, old; |
| 243 | union smc_cdc_cursor net; |
| 244 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 245 | smc_curs_copy(&old, local, conn); |
| 246 | smc_curs_copy_net(&net, peer, conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 247 | temp.count = ntohl(net.count); |
| 248 | temp.wrap = ntohs(net.wrap); |
| 249 | if ((old.wrap > temp.wrap) && temp.wrap) |
| 250 | return; |
| 251 | if ((old.wrap == temp.wrap) && |
| 252 | (old.count > temp.count)) |
| 253 | return; |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 254 | smc_curs_copy(local, &temp, conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 257 | static inline void smcr_cdc_msg_to_host(struct smc_host_cdc_msg *local, |
| 258 | struct smc_cdc_msg *peer, |
| 259 | struct smc_connection *conn) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 260 | { |
| 261 | local->common.type = peer->common.type; |
| 262 | local->len = peer->len; |
| 263 | local->seqno = ntohs(peer->seqno); |
| 264 | local->token = ntohl(peer->token); |
| 265 | smc_cdc_cursor_to_host(&local->prod, &peer->prod, conn); |
| 266 | smc_cdc_cursor_to_host(&local->cons, &peer->cons, conn); |
| 267 | local->prod_flags = peer->prod_flags; |
| 268 | local->conn_state_flags = peer->conn_state_flags; |
| 269 | } |
| 270 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 271 | static inline void smcd_cdc_msg_to_host(struct smc_host_cdc_msg *local, |
| 272 | struct smcd_cdc_msg *peer) |
| 273 | { |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 274 | union smc_host_cursor temp; |
| 275 | |
| 276 | temp.wrap = peer->prod.wrap; |
| 277 | temp.count = peer->prod.count; |
| 278 | atomic64_set(&local->prod.acurs, atomic64_read(&temp.acurs)); |
| 279 | |
| 280 | temp.wrap = peer->cons.wrap; |
| 281 | temp.count = peer->cons.count; |
| 282 | atomic64_set(&local->cons.acurs, atomic64_read(&temp.acurs)); |
| 283 | local->prod_flags = peer->cons.prod_flags; |
| 284 | local->conn_state_flags = peer->cons.conn_state_flags; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static inline void smc_cdc_msg_to_host(struct smc_host_cdc_msg *local, |
| 288 | struct smc_cdc_msg *peer, |
| 289 | struct smc_connection *conn) |
| 290 | { |
| 291 | if (conn->lgr->is_smcd) |
| 292 | smcd_cdc_msg_to_host(local, (struct smcd_cdc_msg *)peer); |
| 293 | else |
| 294 | smcr_cdc_msg_to_host(local, peer, conn); |
| 295 | } |
| 296 | |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 297 | struct smc_cdc_tx_pend { |
| 298 | struct smc_connection *conn; /* socket connection */ |
| 299 | union smc_host_cursor cursor; /* tx sndbuf cursor sent */ |
| 300 | union smc_host_cursor p_cursor; /* rx RMBE cursor produced */ |
| 301 | u16 ctrl_seq; /* conn. tx sequence # */ |
| 302 | }; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 303 | |
Ursula Braun | 51957bc | 2017-09-21 09:17:34 +0200 | [diff] [blame] | 304 | int smc_cdc_get_free_slot(struct smc_connection *conn, |
| 305 | struct smc_wr_buf **wr_buf, |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 306 | struct smc_rdma_wr **wr_rdma_buf, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 307 | struct smc_cdc_tx_pend **pend); |
| 308 | void smc_cdc_tx_dismiss_slots(struct smc_connection *conn); |
| 309 | int smc_cdc_msg_send(struct smc_connection *conn, struct smc_wr_buf *wr_buf, |
| 310 | struct smc_cdc_tx_pend *pend); |
| 311 | int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 312 | int smcd_cdc_msg_send(struct smc_connection *conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 313 | int smc_cdc_init(void) __init; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 314 | void smcd_cdc_rx_init(struct smc_connection *conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 315 | |
| 316 | #endif /* SMC_CDC_H */ |