0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/spinlock.h>
0014
0015 #include "smc.h"
0016 #include "smc_wr.h"
0017 #include "smc_cdc.h"
0018 #include "smc_tx.h"
0019 #include "smc_rx.h"
0020 #include "smc_close.h"
0021
0022
0023
0024
0025 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
0026 struct smc_link *link,
0027 enum ib_wc_status wc_status)
0028 {
0029 struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
0030 struct smc_connection *conn = cdcpend->conn;
0031 struct smc_sock *smc;
0032 int diff;
0033
0034 smc = container_of(conn, struct smc_sock, conn);
0035 bh_lock_sock(&smc->sk);
0036 if (!wc_status) {
0037 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
0038 &cdcpend->conn->tx_curs_fin,
0039 &cdcpend->cursor);
0040
0041 smp_mb__before_atomic();
0042 atomic_add(diff, &cdcpend->conn->sndbuf_space);
0043
0044 smp_mb__after_atomic();
0045 smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
0046 smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,
0047 conn);
0048 conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
0049 }
0050
0051 if (atomic_dec_and_test(&conn->cdc_pend_tx_wr)) {
0052
0053
0054
0055
0056 if (sock_owned_by_user(&smc->sk))
0057 conn->tx_in_release_sock = true;
0058 else
0059 smc_tx_pending(conn);
0060
0061 if (unlikely(wq_has_sleeper(&conn->cdc_pend_tx_wq)))
0062 wake_up(&conn->cdc_pend_tx_wq);
0063 }
0064 WARN_ON(atomic_read(&conn->cdc_pend_tx_wr) < 0);
0065
0066 smc_tx_sndbuf_nonfull(smc);
0067 bh_unlock_sock(&smc->sk);
0068 }
0069
0070 int smc_cdc_get_free_slot(struct smc_connection *conn,
0071 struct smc_link *link,
0072 struct smc_wr_buf **wr_buf,
0073 struct smc_rdma_wr **wr_rdma_buf,
0074 struct smc_cdc_tx_pend **pend)
0075 {
0076 int rc;
0077
0078 rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
0079 wr_rdma_buf,
0080 (struct smc_wr_tx_pend_priv **)pend);
0081 if (conn->killed) {
0082
0083 if (!rc)
0084 smc_wr_tx_put_slot(link,
0085 (struct smc_wr_tx_pend_priv *)(*pend));
0086 rc = -EPIPE;
0087 }
0088 return rc;
0089 }
0090
0091 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
0092 struct smc_cdc_tx_pend *pend)
0093 {
0094 BUILD_BUG_ON_MSG(
0095 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
0096 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
0097 BUILD_BUG_ON_MSG(
0098 offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
0099 "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()");
0100 BUILD_BUG_ON_MSG(
0101 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
0102 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
0103 pend->conn = conn;
0104 pend->cursor = conn->tx_curs_sent;
0105 pend->p_cursor = conn->local_tx_ctrl.prod;
0106 pend->ctrl_seq = conn->tx_cdc_seq;
0107 }
0108
0109 int smc_cdc_msg_send(struct smc_connection *conn,
0110 struct smc_wr_buf *wr_buf,
0111 struct smc_cdc_tx_pend *pend)
0112 {
0113 struct smc_link *link = conn->lnk;
0114 union smc_host_cursor cfed;
0115 int rc;
0116
0117 smc_cdc_add_pending_send(conn, pend);
0118
0119 conn->tx_cdc_seq++;
0120 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
0121 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
0122
0123 atomic_inc(&conn->cdc_pend_tx_wr);
0124 smp_mb__after_atomic();
0125
0126 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
0127 if (!rc) {
0128 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
0129 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
0130 } else {
0131 conn->tx_cdc_seq--;
0132 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
0133 atomic_dec(&conn->cdc_pend_tx_wr);
0134 }
0135
0136 return rc;
0137 }
0138
0139
0140 int smcr_cdc_msg_send_validation(struct smc_connection *conn,
0141 struct smc_cdc_tx_pend *pend,
0142 struct smc_wr_buf *wr_buf)
0143 {
0144 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
0145 struct smc_link *link = conn->lnk;
0146 struct smc_cdc_msg *peer;
0147 int rc;
0148
0149 peer = (struct smc_cdc_msg *)wr_buf;
0150 peer->common.type = local->common.type;
0151 peer->len = local->len;
0152 peer->seqno = htons(conn->tx_cdc_seq_fin);
0153 peer->token = htonl(local->token);
0154 peer->prod_flags.failover_validation = 1;
0155
0156
0157
0158
0159 smc_cdc_add_pending_send(conn, pend);
0160
0161 atomic_inc(&conn->cdc_pend_tx_wr);
0162 smp_mb__after_atomic();
0163
0164 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
0165 if (unlikely(rc))
0166 atomic_dec(&conn->cdc_pend_tx_wr);
0167
0168 return rc;
0169 }
0170
0171 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
0172 {
0173 struct smc_cdc_tx_pend *pend;
0174 struct smc_wr_buf *wr_buf;
0175 struct smc_link *link;
0176 bool again = false;
0177 int rc;
0178
0179 again:
0180 link = conn->lnk;
0181 if (!smc_wr_tx_link_hold(link))
0182 return -ENOLINK;
0183 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
0184 if (rc)
0185 goto put_out;
0186
0187 spin_lock_bh(&conn->send_lock);
0188 if (link != conn->lnk) {
0189
0190 spin_unlock_bh(&conn->send_lock);
0191 smc_wr_tx_put_slot(link,
0192 (struct smc_wr_tx_pend_priv *)pend);
0193 smc_wr_tx_link_put(link);
0194 if (again)
0195 return -ENOLINK;
0196 again = true;
0197 goto again;
0198 }
0199 rc = smc_cdc_msg_send(conn, wr_buf, pend);
0200 spin_unlock_bh(&conn->send_lock);
0201 put_out:
0202 smc_wr_tx_link_put(link);
0203 return rc;
0204 }
0205
0206 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
0207 {
0208 int rc;
0209
0210 if (!smc_conn_lgr_valid(conn) ||
0211 (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
0212 return -EPIPE;
0213
0214 if (conn->lgr->is_smcd) {
0215 spin_lock_bh(&conn->send_lock);
0216 rc = smcd_cdc_msg_send(conn);
0217 spin_unlock_bh(&conn->send_lock);
0218 } else {
0219 rc = smcr_cdc_get_slot_and_msg_send(conn);
0220 }
0221
0222 return rc;
0223 }
0224
0225 void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn)
0226 {
0227 wait_event(conn->cdc_pend_tx_wq, !atomic_read(&conn->cdc_pend_tx_wr));
0228 }
0229
0230
0231
0232
0233
0234 int smcd_cdc_msg_send(struct smc_connection *conn)
0235 {
0236 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
0237 union smc_host_cursor curs;
0238 struct smcd_cdc_msg cdc;
0239 int rc, diff;
0240
0241 memset(&cdc, 0, sizeof(cdc));
0242 cdc.common.type = SMC_CDC_MSG_TYPE;
0243 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
0244 cdc.prod.wrap = curs.wrap;
0245 cdc.prod.count = curs.count;
0246 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
0247 cdc.cons.wrap = curs.wrap;
0248 cdc.cons.count = curs.count;
0249 cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
0250 cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
0251 rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
0252 if (rc)
0253 return rc;
0254 smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
0255 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
0256
0257 diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
0258 &conn->tx_curs_sent);
0259
0260 smp_mb__before_atomic();
0261 atomic_add(diff, &conn->sndbuf_space);
0262
0263 smp_mb__after_atomic();
0264 smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
0265
0266 smc_tx_sndbuf_nonfull(smc);
0267 return rc;
0268 }
0269
0270
0271
0272 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
0273 {
0274 return (s16)(seq1 - seq2) < 0;
0275 }
0276
0277 static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
0278 int *diff_prod)
0279 {
0280 struct smc_connection *conn = &smc->conn;
0281 char *base;
0282
0283
0284 smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
0285 conn->urg_state = SMC_URG_VALID;
0286 if (!sock_flag(&smc->sk, SOCK_URGINLINE))
0287
0288 (*diff_prod)--;
0289 base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
0290 if (conn->urg_curs.count)
0291 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
0292 else
0293 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
0294 sk_send_sigurg(&smc->sk);
0295 }
0296
0297 static void smc_cdc_msg_validate(struct smc_sock *smc, struct smc_cdc_msg *cdc,
0298 struct smc_link *link)
0299 {
0300 struct smc_connection *conn = &smc->conn;
0301 u16 recv_seq = ntohs(cdc->seqno);
0302 s16 diff;
0303
0304
0305 diff = conn->local_rx_ctrl.seqno - recv_seq;
0306 if (diff < 0) {
0307
0308 conn->out_of_sync = 1;
0309 spin_lock_bh(&conn->send_lock);
0310 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
0311 conn->lnk = link;
0312 spin_unlock_bh(&conn->send_lock);
0313 sock_hold(&smc->sk);
0314 if (!queue_work(smc_close_wq, &conn->abort_work))
0315 sock_put(&smc->sk);
0316 }
0317 }
0318
0319 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
0320 struct smc_cdc_msg *cdc)
0321 {
0322 union smc_host_cursor cons_old, prod_old;
0323 struct smc_connection *conn = &smc->conn;
0324 int diff_cons, diff_prod;
0325
0326 smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
0327 smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
0328 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
0329
0330 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
0331 &conn->local_rx_ctrl.cons);
0332 if (diff_cons) {
0333
0334
0335
0336 smp_mb__before_atomic();
0337 atomic_add(diff_cons, &conn->peer_rmbe_space);
0338
0339 smp_mb__after_atomic();
0340 }
0341
0342 diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
0343 &conn->local_rx_ctrl.prod);
0344 if (diff_prod) {
0345 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
0346 smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
0347
0348 smp_mb__before_atomic();
0349 atomic_add(diff_prod, &conn->bytes_to_rcv);
0350
0351 smp_mb__after_atomic();
0352 smc->sk.sk_data_ready(&smc->sk);
0353 } else {
0354 if (conn->local_rx_ctrl.prod_flags.write_blocked)
0355 smc->sk.sk_data_ready(&smc->sk);
0356 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
0357 conn->urg_state = SMC_URG_NOTYET;
0358 }
0359
0360
0361 if ((diff_cons && smc_tx_prepared_sends(conn)) ||
0362 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
0363 conn->local_rx_ctrl.prod_flags.urg_data_pending) {
0364 if (!sock_owned_by_user(&smc->sk))
0365 smc_tx_pending(conn);
0366 else
0367 conn->tx_in_release_sock = true;
0368 }
0369
0370 if (diff_cons && conn->urg_tx_pend &&
0371 atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
0372
0373 conn->urg_tx_pend = false;
0374 smc->sk.sk_write_space(&smc->sk);
0375 }
0376
0377 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
0378 smc->sk.sk_err = ECONNRESET;
0379 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
0380 }
0381 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
0382 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
0383 if (smc->clcsock && smc->clcsock->sk)
0384 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
0385 sock_set_flag(&smc->sk, SOCK_DONE);
0386 sock_hold(&smc->sk);
0387 if (!queue_work(smc_close_wq, &conn->close_work))
0388 sock_put(&smc->sk);
0389 }
0390 }
0391
0392
0393 static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
0394 {
0395 sock_hold(&smc->sk);
0396 bh_lock_sock(&smc->sk);
0397 smc_cdc_msg_recv_action(smc, cdc);
0398 bh_unlock_sock(&smc->sk);
0399 sock_put(&smc->sk);
0400 }
0401
0402
0403
0404
0405
0406
0407
0408 static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
0409 {
0410 struct smc_connection *conn = from_tasklet(conn, t, rx_tsklet);
0411 struct smcd_cdc_msg *data_cdc;
0412 struct smcd_cdc_msg cdc;
0413 struct smc_sock *smc;
0414
0415 if (!conn || conn->killed)
0416 return;
0417
0418 data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
0419 smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
0420 smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
0421 smc = container_of(conn, struct smc_sock, conn);
0422 smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
0423 }
0424
0425
0426
0427
0428 void smcd_cdc_rx_init(struct smc_connection *conn)
0429 {
0430 tasklet_setup(&conn->rx_tsklet, smcd_cdc_rx_tsklet);
0431 }
0432
0433
0434
0435 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
0436 {
0437 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
0438 struct smc_cdc_msg *cdc = buf;
0439 struct smc_connection *conn;
0440 struct smc_link_group *lgr;
0441 struct smc_sock *smc;
0442
0443 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
0444 return;
0445 if (cdc->len != SMC_WR_TX_SIZE)
0446 return;
0447
0448
0449 lgr = smc_get_lgr(link);
0450 read_lock_bh(&lgr->conns_lock);
0451 conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
0452 read_unlock_bh(&lgr->conns_lock);
0453 if (!conn || conn->out_of_sync)
0454 return;
0455 smc = container_of(conn, struct smc_sock, conn);
0456
0457 if (cdc->prod_flags.failover_validation) {
0458 smc_cdc_msg_validate(smc, cdc, link);
0459 return;
0460 }
0461 if (smc_cdc_before(ntohs(cdc->seqno),
0462 conn->local_rx_ctrl.seqno))
0463
0464 return;
0465
0466 smc_cdc_msg_recv(smc, cdc);
0467 }
0468
0469 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
0470 {
0471 .handler = smc_cdc_rx_handler,
0472 .type = SMC_CDC_MSG_TYPE
0473 },
0474 {
0475 .handler = NULL,
0476 }
0477 };
0478
0479 int __init smc_cdc_init(void)
0480 {
0481 struct smc_wr_rx_handler *handler;
0482 int rc = 0;
0483
0484 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
0485 INIT_HLIST_NODE(&handler->list);
0486 rc = smc_wr_rx_register_handler(handler);
0487 if (rc)
0488 break;
0489 }
0490 return rc;
0491 }