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 | * handles flow control |
| 7 | * |
| 8 | * Copyright IBM Corp. 2016 |
| 9 | * |
| 10 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/spinlock.h> |
| 14 | |
| 15 | #include "smc.h" |
| 16 | #include "smc_wr.h" |
| 17 | #include "smc_cdc.h" |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 18 | #include "smc_tx.h" |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 19 | #include "smc_rx.h" |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 20 | #include "smc_close.h" |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 21 | |
| 22 | /********************************** send *************************************/ |
| 23 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 24 | /* handler for send/transmission completion of a CDC msg */ |
| 25 | static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, |
| 26 | struct smc_link *link, |
| 27 | enum ib_wc_status wc_status) |
| 28 | { |
| 29 | struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd; |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 30 | struct smc_connection *conn = cdcpend->conn; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 31 | struct smc_sock *smc; |
| 32 | int diff; |
| 33 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 34 | if (!conn) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 35 | /* already dismissed */ |
| 36 | return; |
| 37 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 38 | smc = container_of(conn, struct smc_sock, conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 39 | bh_lock_sock(&smc->sk); |
| 40 | if (!wc_status) { |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 41 | diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 42 | &cdcpend->conn->tx_curs_fin, |
| 43 | &cdcpend->cursor); |
| 44 | /* sndbuf_space is decreased in smc_sendmsg */ |
| 45 | smp_mb__before_atomic(); |
| 46 | atomic_add(diff, &cdcpend->conn->sndbuf_space); |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 47 | /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 48 | smp_mb__after_atomic(); |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 49 | smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn); |
Karsten Graul | f0ec4f1 | 2020-05-04 14:18:37 +0200 | [diff] [blame] | 50 | smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor, |
| 51 | conn); |
| 52 | conn->tx_cdc_seq_fin = cdcpend->ctrl_seq; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 53 | } |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 54 | smc_tx_sndbuf_nonfull(smc); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 55 | bh_unlock_sock(&smc->sk); |
| 56 | } |
| 57 | |
Ursula Braun | 51957bc | 2017-09-21 09:17:34 +0200 | [diff] [blame] | 58 | int smc_cdc_get_free_slot(struct smc_connection *conn, |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame^] | 59 | struct smc_link *link, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 60 | struct smc_wr_buf **wr_buf, |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 61 | struct smc_rdma_wr **wr_rdma_buf, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 62 | struct smc_cdc_tx_pend **pend) |
| 63 | { |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 64 | int rc; |
Ursula Braun | 51957bc | 2017-09-21 09:17:34 +0200 | [diff] [blame] | 65 | |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 66 | rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf, |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 67 | wr_rdma_buf, |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 68 | (struct smc_wr_tx_pend_priv **)pend); |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 69 | if (conn->killed) |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 70 | /* abnormal termination */ |
| 71 | rc = -EPIPE; |
| 72 | return rc; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static inline void smc_cdc_add_pending_send(struct smc_connection *conn, |
| 76 | struct smc_cdc_tx_pend *pend) |
| 77 | { |
| 78 | BUILD_BUG_ON_MSG( |
| 79 | sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE, |
| 80 | "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)"); |
| 81 | BUILD_BUG_ON_MSG( |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 82 | offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 83 | "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_cdc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()"); |
| 84 | BUILD_BUG_ON_MSG( |
| 85 | sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE, |
| 86 | "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)"); |
| 87 | pend->conn = conn; |
| 88 | pend->cursor = conn->tx_curs_sent; |
| 89 | pend->p_cursor = conn->local_tx_ctrl.prod; |
| 90 | pend->ctrl_seq = conn->tx_cdc_seq; |
| 91 | } |
| 92 | |
| 93 | int smc_cdc_msg_send(struct smc_connection *conn, |
| 94 | struct smc_wr_buf *wr_buf, |
| 95 | struct smc_cdc_tx_pend *pend) |
| 96 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 97 | struct smc_link *link = conn->lnk; |
Ursula Braun | b8649ef | 2019-02-04 13:44:45 +0100 | [diff] [blame] | 98 | union smc_host_cursor cfed; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 99 | int rc; |
| 100 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 101 | smc_cdc_add_pending_send(conn, pend); |
| 102 | |
| 103 | conn->tx_cdc_seq++; |
| 104 | conn->local_tx_ctrl.seqno = conn->tx_cdc_seq; |
Ursula Braun | ccc8ca9 | 2019-02-07 14:52:54 +0100 | [diff] [blame] | 105 | smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 106 | rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend); |
Karsten Graul | 4dff63c | 2019-02-12 16:29:50 +0100 | [diff] [blame] | 107 | if (!rc) { |
Ursula Braun | b8649ef | 2019-02-04 13:44:45 +0100 | [diff] [blame] | 108 | smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn); |
Karsten Graul | 4dff63c | 2019-02-12 16:29:50 +0100 | [diff] [blame] | 109 | conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0; |
Karsten Graul | f0ec4f1 | 2020-05-04 14:18:37 +0200 | [diff] [blame] | 110 | } else { |
| 111 | conn->tx_cdc_seq--; |
| 112 | conn->local_tx_ctrl.seqno = conn->tx_cdc_seq; |
Karsten Graul | 4dff63c | 2019-02-12 16:29:50 +0100 | [diff] [blame] | 113 | } |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 114 | |
| 115 | return rc; |
| 116 | } |
| 117 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 118 | static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 119 | { |
| 120 | struct smc_cdc_tx_pend *pend; |
| 121 | struct smc_wr_buf *wr_buf; |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame^] | 122 | struct smc_link *link; |
| 123 | bool again = false; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 124 | int rc; |
| 125 | |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame^] | 126 | again: |
| 127 | link = conn->lnk; |
| 128 | rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 129 | if (rc) |
| 130 | return rc; |
| 131 | |
Karsten Graul | 2dee25a | 2019-01-30 18:51:06 +0100 | [diff] [blame] | 132 | spin_lock_bh(&conn->send_lock); |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame^] | 133 | if (link != conn->lnk) { |
| 134 | /* link of connection changed, try again one time*/ |
| 135 | spin_unlock_bh(&conn->send_lock); |
| 136 | smc_wr_tx_put_slot(link, |
| 137 | (struct smc_wr_tx_pend_priv *)pend); |
| 138 | if (again) |
| 139 | return -ENOLINK; |
| 140 | again = true; |
| 141 | goto again; |
| 142 | } |
Karsten Graul | 2dee25a | 2019-01-30 18:51:06 +0100 | [diff] [blame] | 143 | rc = smc_cdc_msg_send(conn, wr_buf, pend); |
| 144 | spin_unlock_bh(&conn->send_lock); |
| 145 | return rc; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 148 | int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn) |
| 149 | { |
| 150 | int rc; |
| 151 | |
Ursula Braun | 50c6b20 | 2019-11-14 13:02:40 +0100 | [diff] [blame] | 152 | if (!conn->lgr || (conn->lgr->is_smcd && conn->lgr->peer_shutdown)) |
| 153 | return -EPIPE; |
| 154 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 155 | if (conn->lgr->is_smcd) { |
| 156 | spin_lock_bh(&conn->send_lock); |
| 157 | rc = smcd_cdc_msg_send(conn); |
| 158 | spin_unlock_bh(&conn->send_lock); |
| 159 | } else { |
| 160 | rc = smcr_cdc_get_slot_and_msg_send(conn); |
| 161 | } |
| 162 | |
| 163 | return rc; |
| 164 | } |
| 165 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 166 | static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend, |
| 167 | unsigned long data) |
| 168 | { |
| 169 | struct smc_connection *conn = (struct smc_connection *)data; |
| 170 | struct smc_cdc_tx_pend *cdc_pend = |
| 171 | (struct smc_cdc_tx_pend *)tx_pend; |
| 172 | |
| 173 | return cdc_pend->conn == conn; |
| 174 | } |
| 175 | |
| 176 | static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend) |
| 177 | { |
| 178 | struct smc_cdc_tx_pend *cdc_pend = |
| 179 | (struct smc_cdc_tx_pend *)tx_pend; |
| 180 | |
| 181 | cdc_pend->conn = NULL; |
| 182 | } |
| 183 | |
| 184 | void smc_cdc_tx_dismiss_slots(struct smc_connection *conn) |
| 185 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 186 | struct smc_link *link = conn->lnk; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 187 | |
| 188 | smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE, |
| 189 | smc_cdc_tx_filter, smc_cdc_tx_dismisser, |
| 190 | (unsigned long)conn); |
| 191 | } |
| 192 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 193 | /* Send a SMC-D CDC header. |
| 194 | * This increments the free space available in our send buffer. |
| 195 | * Also update the confirmed receive buffer with what was sent to the peer. |
| 196 | */ |
| 197 | int smcd_cdc_msg_send(struct smc_connection *conn) |
| 198 | { |
| 199 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 200 | union smc_host_cursor curs; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 201 | struct smcd_cdc_msg cdc; |
| 202 | int rc, diff; |
| 203 | |
| 204 | memset(&cdc, 0, sizeof(cdc)); |
| 205 | cdc.common.type = SMC_CDC_MSG_TYPE; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 206 | curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs); |
| 207 | cdc.prod.wrap = curs.wrap; |
| 208 | cdc.prod.count = curs.count; |
| 209 | curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs); |
| 210 | cdc.cons.wrap = curs.wrap; |
| 211 | cdc.cons.count = curs.count; |
| 212 | cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags; |
| 213 | cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 214 | rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1); |
| 215 | if (rc) |
| 216 | return rc; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 217 | smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn); |
Karsten Graul | 4dff63c | 2019-02-12 16:29:50 +0100 | [diff] [blame] | 218 | conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 219 | /* Calculate transmitted data and increment free send buffer space */ |
| 220 | diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin, |
| 221 | &conn->tx_curs_sent); |
| 222 | /* increased by confirmed number of bytes */ |
| 223 | smp_mb__before_atomic(); |
| 224 | atomic_add(diff, &conn->sndbuf_space); |
| 225 | /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ |
| 226 | smp_mb__after_atomic(); |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 227 | smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 228 | |
| 229 | smc_tx_sndbuf_nonfull(smc); |
| 230 | return rc; |
| 231 | } |
| 232 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 233 | /********************************* receive ***********************************/ |
| 234 | |
| 235 | static inline bool smc_cdc_before(u16 seq1, u16 seq2) |
| 236 | { |
| 237 | return (s16)(seq1 - seq2) < 0; |
| 238 | } |
| 239 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 240 | static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc, |
| 241 | int *diff_prod) |
| 242 | { |
| 243 | struct smc_connection *conn = &smc->conn; |
| 244 | char *base; |
| 245 | |
| 246 | /* new data included urgent business */ |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 247 | smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn); |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 248 | conn->urg_state = SMC_URG_VALID; |
| 249 | if (!sock_flag(&smc->sk, SOCK_URGINLINE)) |
| 250 | /* we'll skip the urgent byte, so don't account for it */ |
| 251 | (*diff_prod)--; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 252 | base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off; |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 253 | if (conn->urg_curs.count) |
| 254 | conn->urg_rx_byte = *(base + conn->urg_curs.count - 1); |
| 255 | else |
| 256 | conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1); |
| 257 | sk_send_sigurg(&smc->sk); |
| 258 | } |
| 259 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 260 | static void smc_cdc_msg_recv_action(struct smc_sock *smc, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 261 | struct smc_cdc_msg *cdc) |
| 262 | { |
| 263 | union smc_host_cursor cons_old, prod_old; |
| 264 | struct smc_connection *conn = &smc->conn; |
| 265 | int diff_cons, diff_prod; |
| 266 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 267 | smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn); |
| 268 | smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 269 | smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn); |
| 270 | |
| 271 | diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old, |
| 272 | &conn->local_rx_ctrl.cons); |
| 273 | if (diff_cons) { |
| 274 | /* peer_rmbe_space is decreased during data transfer with RDMA |
| 275 | * write |
| 276 | */ |
| 277 | smp_mb__before_atomic(); |
| 278 | atomic_add(diff_cons, &conn->peer_rmbe_space); |
| 279 | /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */ |
| 280 | smp_mb__after_atomic(); |
| 281 | } |
| 282 | |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 283 | diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old, |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 284 | &conn->local_rx_ctrl.prod); |
| 285 | if (diff_prod) { |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 286 | if (conn->local_rx_ctrl.prod_flags.urg_data_present) |
| 287 | smc_cdc_handle_urg_data_arrival(smc, &diff_prod); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 288 | /* bytes_to_rcv is decreased in smc_recvmsg */ |
| 289 | smp_mb__before_atomic(); |
| 290 | atomic_add(diff_prod, &conn->bytes_to_rcv); |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 291 | /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */ |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 292 | smp_mb__after_atomic(); |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 293 | smc->sk.sk_data_ready(&smc->sk); |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 294 | } else { |
Karsten Graul | cf0cfe5 | 2019-02-12 16:29:53 +0100 | [diff] [blame] | 295 | if (conn->local_rx_ctrl.prod_flags.write_blocked) |
| 296 | smc->sk.sk_data_ready(&smc->sk); |
| 297 | if (conn->local_rx_ctrl.prod_flags.urg_data_pending) |
| 298 | conn->urg_state = SMC_URG_NOTYET; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 301 | /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */ |
Karsten Graul | cf0cfe5 | 2019-02-12 16:29:53 +0100 | [diff] [blame] | 302 | if ((diff_cons && smc_tx_prepared_sends(conn)) || |
| 303 | conn->local_rx_ctrl.prod_flags.cons_curs_upd_req || |
| 304 | conn->local_rx_ctrl.prod_flags.urg_data_pending) |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 305 | smc_tx_sndbuf_nonempty(conn); |
Karsten Graul | cf0cfe5 | 2019-02-12 16:29:53 +0100 | [diff] [blame] | 306 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 307 | if (diff_cons && conn->urg_tx_pend && |
| 308 | atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) { |
| 309 | /* urg data confirmed by peer, indicate we're ready for more */ |
| 310 | conn->urg_tx_pend = false; |
| 311 | smc->sk.sk_write_space(&smc->sk); |
| 312 | } |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 313 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 314 | if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) { |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 315 | smc->sk.sk_err = ECONNRESET; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 316 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; |
| 317 | } |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 318 | if (smc_cdc_rxed_any_close_or_senddone(conn)) { |
| 319 | smc->sk.sk_shutdown |= RCV_SHUTDOWN; |
| 320 | if (smc->clcsock && smc->clcsock->sk) |
| 321 | smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN; |
| 322 | sock_set_flag(&smc->sk, SOCK_DONE); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 323 | sock_hold(&smc->sk); /* sock_put in close_work */ |
| 324 | if (!schedule_work(&conn->close_work)) |
| 325 | sock_put(&smc->sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 326 | } |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* called under tasklet context */ |
Hans Wippel | d7b0e37 | 2018-05-18 09:34:15 +0200 | [diff] [blame] | 330 | static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 331 | { |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 332 | sock_hold(&smc->sk); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 333 | bh_lock_sock(&smc->sk); |
Hans Wippel | d7b0e37 | 2018-05-18 09:34:15 +0200 | [diff] [blame] | 334 | smc_cdc_msg_recv_action(smc, cdc); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 335 | bh_unlock_sock(&smc->sk); |
| 336 | sock_put(&smc->sk); /* no free sk in softirq-context */ |
| 337 | } |
| 338 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 339 | /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ |
| 340 | * handler to indicate update in the DMBE. |
| 341 | * |
| 342 | * Context: |
| 343 | * - tasklet context |
| 344 | */ |
| 345 | static void smcd_cdc_rx_tsklet(unsigned long data) |
| 346 | { |
| 347 | struct smc_connection *conn = (struct smc_connection *)data; |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 348 | struct smcd_cdc_msg *data_cdc; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 349 | struct smcd_cdc_msg cdc; |
| 350 | struct smc_sock *smc; |
| 351 | |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 352 | if (!conn || conn->killed) |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 353 | return; |
| 354 | |
Ursula Braun | b9a22dd | 2018-11-20 16:46:42 +0100 | [diff] [blame] | 355 | data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr; |
| 356 | smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn); |
| 357 | smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 358 | smc = container_of(conn, struct smc_sock, conn); |
| 359 | smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc); |
| 360 | } |
| 361 | |
| 362 | /* Initialize receive tasklet. Called from ISM device IRQ handler to start |
| 363 | * receiver side. |
| 364 | */ |
| 365 | void smcd_cdc_rx_init(struct smc_connection *conn) |
| 366 | { |
| 367 | tasklet_init(&conn->rx_tsklet, smcd_cdc_rx_tsklet, (unsigned long)conn); |
| 368 | } |
| 369 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 370 | /***************************** init, exit, misc ******************************/ |
| 371 | |
| 372 | static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf) |
| 373 | { |
| 374 | struct smc_link *link = (struct smc_link *)wc->qp->qp_context; |
| 375 | struct smc_cdc_msg *cdc = buf; |
Hans Wippel | d7b0e37 | 2018-05-18 09:34:15 +0200 | [diff] [blame] | 376 | struct smc_connection *conn; |
| 377 | struct smc_link_group *lgr; |
| 378 | struct smc_sock *smc; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 379 | |
| 380 | if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved)) |
| 381 | return; /* short message */ |
Karsten Graul | cbba07a | 2018-02-28 12:44:07 +0100 | [diff] [blame] | 382 | if (cdc->len != SMC_WR_TX_SIZE) |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 383 | return; /* invalid message */ |
Hans Wippel | d7b0e37 | 2018-05-18 09:34:15 +0200 | [diff] [blame] | 384 | |
| 385 | /* lookup connection */ |
Stefan Raspl | 00e5fb2 | 2018-07-23 13:53:10 +0200 | [diff] [blame] | 386 | lgr = smc_get_lgr(link); |
Hans Wippel | d7b0e37 | 2018-05-18 09:34:15 +0200 | [diff] [blame] | 387 | read_lock_bh(&lgr->conns_lock); |
| 388 | conn = smc_lgr_find_conn(ntohl(cdc->token), lgr); |
| 389 | read_unlock_bh(&lgr->conns_lock); |
| 390 | if (!conn) |
| 391 | return; |
| 392 | smc = container_of(conn, struct smc_sock, conn); |
| 393 | |
| 394 | if (!cdc->prod_flags.failover_validation) { |
| 395 | if (smc_cdc_before(ntohs(cdc->seqno), |
| 396 | conn->local_rx_ctrl.seqno)) |
| 397 | /* received seqno is old */ |
| 398 | return; |
| 399 | } |
| 400 | smc_cdc_msg_recv(smc, cdc); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = { |
| 404 | { |
| 405 | .handler = smc_cdc_rx_handler, |
| 406 | .type = SMC_CDC_MSG_TYPE |
| 407 | }, |
| 408 | { |
| 409 | .handler = NULL, |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | int __init smc_cdc_init(void) |
| 414 | { |
| 415 | struct smc_wr_rx_handler *handler; |
| 416 | int rc = 0; |
| 417 | |
| 418 | for (handler = smc_cdc_rx_handlers; handler->handler; handler++) { |
| 419 | INIT_HLIST_NODE(&handler->list); |
| 420 | rc = smc_wr_rx_register_handler(handler); |
| 421 | if (rc) |
| 422 | break; |
| 423 | } |
| 424 | return rc; |
| 425 | } |