smc_tx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Manage send buffer.
  6. * Producer:
  7. * Copy user space data into send buffer, if send buffer space available.
  8. * Consumer:
  9. * Trigger RDMA write into RMBE of peer and send CDC, if RMBE space available.
  10. *
  11. * Copyright IBM Corp. 2016
  12. *
  13. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  14. */
  15. #include <linux/net.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/sched/signal.h>
  19. #include <net/sock.h>
  20. #include <net/tcp.h>
  21. #include "smc.h"
  22. #include "smc_wr.h"
  23. #include "smc_cdc.h"
  24. #include "smc_ism.h"
  25. #include "smc_tx.h"
  26. #define SMC_TX_WORK_DELAY HZ
  27. #define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */
  28. /***************************** sndbuf producer *******************************/
  29. /* callback implementation for sk.sk_write_space()
  30. * to wakeup sndbuf producers that blocked with smc_tx_wait().
  31. * called under sk_socket lock.
  32. */
  33. static void smc_tx_write_space(struct sock *sk)
  34. {
  35. struct socket *sock = sk->sk_socket;
  36. struct smc_sock *smc = smc_sk(sk);
  37. struct socket_wq *wq;
  38. /* similar to sk_stream_write_space */
  39. if (atomic_read(&smc->conn.sndbuf_space) && sock) {
  40. clear_bit(SOCK_NOSPACE, &sock->flags);
  41. rcu_read_lock();
  42. wq = rcu_dereference(sk->sk_wq);
  43. if (skwq_has_sleeper(wq))
  44. wake_up_interruptible_poll(&wq->wait,
  45. EPOLLOUT | EPOLLWRNORM |
  46. EPOLLWRBAND);
  47. if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
  48. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  49. rcu_read_unlock();
  50. }
  51. }
  52. /* Wakeup sndbuf producers that blocked with smc_tx_wait().
  53. * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
  54. */
  55. void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
  56. {
  57. if (smc->sk.sk_socket &&
  58. test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
  59. smc->sk.sk_write_space(&smc->sk);
  60. }
  61. /* blocks sndbuf producer until at least one byte of free space available
  62. * or urgent Byte was consumed
  63. */
  64. static int smc_tx_wait(struct smc_sock *smc, int flags)
  65. {
  66. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  67. struct smc_connection *conn = &smc->conn;
  68. struct sock *sk = &smc->sk;
  69. long timeo;
  70. int rc = 0;
  71. /* similar to sk_stream_wait_memory */
  72. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  73. add_wait_queue(sk_sleep(sk), &wait);
  74. while (1) {
  75. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  76. if (sk->sk_err ||
  77. (sk->sk_shutdown & SEND_SHUTDOWN) ||
  78. conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
  79. rc = -EPIPE;
  80. break;
  81. }
  82. if (smc_cdc_rxed_any_close(conn)) {
  83. rc = -ECONNRESET;
  84. break;
  85. }
  86. if (!timeo) {
  87. /* ensure EPOLLOUT is subsequently generated */
  88. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  89. rc = -EAGAIN;
  90. break;
  91. }
  92. if (signal_pending(current)) {
  93. rc = sock_intr_errno(timeo);
  94. break;
  95. }
  96. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  97. if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend)
  98. break; /* at least 1 byte of free & no urgent data */
  99. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  100. sk_wait_event(sk, &timeo,
  101. sk->sk_err ||
  102. (sk->sk_shutdown & SEND_SHUTDOWN) ||
  103. smc_cdc_rxed_any_close(conn) ||
  104. (atomic_read(&conn->sndbuf_space) &&
  105. !conn->urg_tx_pend),
  106. &wait);
  107. }
  108. remove_wait_queue(sk_sleep(sk), &wait);
  109. return rc;
  110. }
  111. static bool smc_tx_is_corked(struct smc_sock *smc)
  112. {
  113. struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
  114. return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
  115. }
  116. /* sndbuf producer: main API called by socket layer.
  117. * called under sock lock.
  118. */
  119. int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
  120. {
  121. size_t copylen, send_done = 0, send_remaining = len;
  122. size_t chunk_len, chunk_off, chunk_len_sum;
  123. struct smc_connection *conn = &smc->conn;
  124. union smc_host_cursor prep;
  125. struct sock *sk = &smc->sk;
  126. char *sndbuf_base;
  127. int tx_cnt_prep;
  128. int writespace;
  129. int rc, chunk;
  130. /* This should be in poll */
  131. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  132. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
  133. rc = -EPIPE;
  134. goto out_err;
  135. }
  136. while (msg_data_left(msg)) {
  137. if (sk->sk_state == SMC_INIT)
  138. return -ENOTCONN;
  139. if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
  140. (smc->sk.sk_err == ECONNABORTED) ||
  141. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort)
  142. return -EPIPE;
  143. if (smc_cdc_rxed_any_close(conn))
  144. return send_done ?: -ECONNRESET;
  145. if (msg->msg_flags & MSG_OOB)
  146. conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
  147. if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
  148. if (send_done)
  149. return send_done;
  150. rc = smc_tx_wait(smc, msg->msg_flags);
  151. if (rc)
  152. goto out_err;
  153. continue;
  154. }
  155. /* initialize variables for 1st iteration of subsequent loop */
  156. /* could be just 1 byte, even after smc_tx_wait above */
  157. writespace = atomic_read(&conn->sndbuf_space);
  158. /* not more than what user space asked for */
  159. copylen = min_t(size_t, send_remaining, writespace);
  160. /* determine start of sndbuf */
  161. sndbuf_base = conn->sndbuf_desc->cpu_addr;
  162. smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
  163. tx_cnt_prep = prep.count;
  164. /* determine chunks where to write into sndbuf */
  165. /* either unwrapped case, or 1st chunk of wrapped case */
  166. chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
  167. tx_cnt_prep);
  168. chunk_len_sum = chunk_len;
  169. chunk_off = tx_cnt_prep;
  170. smc_sndbuf_sync_sg_for_cpu(conn);
  171. for (chunk = 0; chunk < 2; chunk++) {
  172. rc = memcpy_from_msg(sndbuf_base + chunk_off,
  173. msg, chunk_len);
  174. if (rc) {
  175. smc_sndbuf_sync_sg_for_device(conn);
  176. if (send_done)
  177. return send_done;
  178. goto out_err;
  179. }
  180. send_done += chunk_len;
  181. send_remaining -= chunk_len;
  182. if (chunk_len_sum == copylen)
  183. break; /* either on 1st or 2nd iteration */
  184. /* prepare next (== 2nd) iteration */
  185. chunk_len = copylen - chunk_len; /* remainder */
  186. chunk_len_sum += chunk_len;
  187. chunk_off = 0; /* modulo offset in send ring buffer */
  188. }
  189. smc_sndbuf_sync_sg_for_device(conn);
  190. /* update cursors */
  191. smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
  192. smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
  193. /* increased in send tasklet smc_cdc_tx_handler() */
  194. smp_mb__before_atomic();
  195. atomic_sub(copylen, &conn->sndbuf_space);
  196. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  197. smp_mb__after_atomic();
  198. /* since we just produced more new data into sndbuf,
  199. * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
  200. */
  201. if ((msg->msg_flags & MSG_OOB) && !send_remaining)
  202. conn->urg_tx_pend = true;
  203. if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) &&
  204. (atomic_read(&conn->sndbuf_space) >
  205. (conn->sndbuf_desc->len >> 1)))
  206. /* for a corked socket defer the RDMA writes if there
  207. * is still sufficient sndbuf_space available
  208. */
  209. schedule_delayed_work(&conn->tx_work,
  210. SMC_TX_CORK_DELAY);
  211. else
  212. smc_tx_sndbuf_nonempty(conn);
  213. } /* while (msg_data_left(msg)) */
  214. return send_done;
  215. out_err:
  216. rc = sk_stream_error(sk, msg->msg_flags, rc);
  217. /* make sure we wake any epoll edge trigger waiter */
  218. if (unlikely(rc == -EAGAIN))
  219. sk->sk_write_space(sk);
  220. return rc;
  221. }
  222. /***************************** sndbuf consumer *******************************/
  223. /* sndbuf consumer: actual data transfer of one target chunk with ISM write */
  224. int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
  225. u32 offset, int signal)
  226. {
  227. struct smc_ism_position pos;
  228. int rc;
  229. memset(&pos, 0, sizeof(pos));
  230. pos.token = conn->peer_token;
  231. pos.index = conn->peer_rmbe_idx;
  232. pos.offset = conn->tx_off + offset;
  233. pos.signal = signal;
  234. rc = smc_ism_write(conn->lgr->smcd, &pos, data, len);
  235. if (rc)
  236. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  237. return rc;
  238. }
  239. /* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
  240. static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
  241. int num_sges, struct ib_sge sges[])
  242. {
  243. struct smc_link_group *lgr = conn->lgr;
  244. struct ib_rdma_wr rdma_wr;
  245. struct smc_link *link;
  246. int rc;
  247. memset(&rdma_wr, 0, sizeof(rdma_wr));
  248. link = &lgr->lnk[SMC_SINGLE_LINK];
  249. rdma_wr.wr.wr_id = smc_wr_tx_get_next_wr_id(link);
  250. rdma_wr.wr.sg_list = sges;
  251. rdma_wr.wr.num_sge = num_sges;
  252. rdma_wr.wr.opcode = IB_WR_RDMA_WRITE;
  253. rdma_wr.remote_addr =
  254. lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr +
  255. /* RMBE within RMB */
  256. conn->tx_off +
  257. /* offset within RMBE */
  258. peer_rmbe_offset;
  259. rdma_wr.rkey = lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey;
  260. rc = ib_post_send(link->roce_qp, &rdma_wr.wr, NULL);
  261. if (rc) {
  262. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  263. smc_lgr_terminate(lgr);
  264. }
  265. return rc;
  266. }
  267. /* sndbuf consumer */
  268. static inline void smc_tx_advance_cursors(struct smc_connection *conn,
  269. union smc_host_cursor *prod,
  270. union smc_host_cursor *sent,
  271. size_t len)
  272. {
  273. smc_curs_add(conn->peer_rmbe_size, prod, len);
  274. /* increased in recv tasklet smc_cdc_msg_rcv() */
  275. smp_mb__before_atomic();
  276. /* data in flight reduces usable snd_wnd */
  277. atomic_sub(len, &conn->peer_rmbe_space);
  278. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  279. smp_mb__after_atomic();
  280. smc_curs_add(conn->sndbuf_desc->len, sent, len);
  281. }
  282. /* SMC-R helper for smc_tx_rdma_writes() */
  283. static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
  284. size_t src_off, size_t src_len,
  285. size_t dst_off, size_t dst_len)
  286. {
  287. dma_addr_t dma_addr =
  288. sg_dma_address(conn->sndbuf_desc->sgt[SMC_SINGLE_LINK].sgl);
  289. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  290. int src_len_sum = src_len, dst_len_sum = dst_len;
  291. struct ib_sge sges[SMC_IB_MAX_SEND_SGE];
  292. int sent_count = src_off;
  293. int srcchunk, dstchunk;
  294. int num_sges;
  295. int rc;
  296. for (dstchunk = 0; dstchunk < 2; dstchunk++) {
  297. num_sges = 0;
  298. for (srcchunk = 0; srcchunk < 2; srcchunk++) {
  299. sges[srcchunk].addr = dma_addr + src_off;
  300. sges[srcchunk].length = src_len;
  301. sges[srcchunk].lkey = link->roce_pd->local_dma_lkey;
  302. num_sges++;
  303. src_off += src_len;
  304. if (src_off >= conn->sndbuf_desc->len)
  305. src_off -= conn->sndbuf_desc->len;
  306. /* modulo in send ring */
  307. if (src_len_sum == dst_len)
  308. break; /* either on 1st or 2nd iteration */
  309. /* prepare next (== 2nd) iteration */
  310. src_len = dst_len - src_len; /* remainder */
  311. src_len_sum += src_len;
  312. }
  313. rc = smc_tx_rdma_write(conn, dst_off, num_sges, sges);
  314. if (rc)
  315. return rc;
  316. if (dst_len_sum == len)
  317. break; /* either on 1st or 2nd iteration */
  318. /* prepare next (== 2nd) iteration */
  319. dst_off = 0; /* modulo offset in RMBE ring buffer */
  320. dst_len = len - dst_len; /* remainder */
  321. dst_len_sum += dst_len;
  322. src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
  323. sent_count);
  324. src_len_sum = src_len;
  325. }
  326. return 0;
  327. }
  328. /* SMC-D helper for smc_tx_rdma_writes() */
  329. static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
  330. size_t src_off, size_t src_len,
  331. size_t dst_off, size_t dst_len)
  332. {
  333. int src_len_sum = src_len, dst_len_sum = dst_len;
  334. int srcchunk, dstchunk;
  335. int rc;
  336. for (dstchunk = 0; dstchunk < 2; dstchunk++) {
  337. for (srcchunk = 0; srcchunk < 2; srcchunk++) {
  338. void *data = conn->sndbuf_desc->cpu_addr + src_off;
  339. rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
  340. sizeof(struct smcd_cdc_msg), 0);
  341. if (rc)
  342. return rc;
  343. dst_off += src_len;
  344. src_off += src_len;
  345. if (src_off >= conn->sndbuf_desc->len)
  346. src_off -= conn->sndbuf_desc->len;
  347. /* modulo in send ring */
  348. if (src_len_sum == dst_len)
  349. break; /* either on 1st or 2nd iteration */
  350. /* prepare next (== 2nd) iteration */
  351. src_len = dst_len - src_len; /* remainder */
  352. src_len_sum += src_len;
  353. }
  354. if (dst_len_sum == len)
  355. break; /* either on 1st or 2nd iteration */
  356. /* prepare next (== 2nd) iteration */
  357. dst_off = 0; /* modulo offset in RMBE ring buffer */
  358. dst_len = len - dst_len; /* remainder */
  359. dst_len_sum += dst_len;
  360. src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
  361. src_len_sum = src_len;
  362. }
  363. return 0;
  364. }
  365. /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
  366. * usable snd_wnd as max transmit
  367. */
  368. static int smc_tx_rdma_writes(struct smc_connection *conn)
  369. {
  370. size_t len, src_len, dst_off, dst_len; /* current chunk values */
  371. union smc_host_cursor sent, prep, prod, cons;
  372. struct smc_cdc_producer_flags *pflags;
  373. int to_send, rmbespace;
  374. int rc;
  375. /* source: sndbuf */
  376. smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
  377. smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
  378. /* cf. wmem_alloc - (snd_max - snd_una) */
  379. to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
  380. if (to_send <= 0)
  381. return 0;
  382. /* destination: RMBE */
  383. /* cf. snd_wnd */
  384. rmbespace = atomic_read(&conn->peer_rmbe_space);
  385. if (rmbespace <= 0)
  386. return 0;
  387. smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
  388. smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
  389. /* if usable snd_wnd closes ask peer to advertise once it opens again */
  390. pflags = &conn->local_tx_ctrl.prod_flags;
  391. pflags->write_blocked = (to_send >= rmbespace);
  392. /* cf. usable snd_wnd */
  393. len = min(to_send, rmbespace);
  394. /* initialize variables for first iteration of subsequent nested loop */
  395. dst_off = prod.count;
  396. if (prod.wrap == cons.wrap) {
  397. /* the filled destination area is unwrapped,
  398. * hence the available free destination space is wrapped
  399. * and we need 2 destination chunks of sum len; start with 1st
  400. * which is limited by what's available in sndbuf
  401. */
  402. dst_len = min_t(size_t,
  403. conn->peer_rmbe_size - prod.count, len);
  404. } else {
  405. /* the filled destination area is wrapped,
  406. * hence the available free destination space is unwrapped
  407. * and we need a single destination chunk of entire len
  408. */
  409. dst_len = len;
  410. }
  411. /* dst_len determines the maximum src_len */
  412. if (sent.count + dst_len <= conn->sndbuf_desc->len) {
  413. /* unwrapped src case: single chunk of entire dst_len */
  414. src_len = dst_len;
  415. } else {
  416. /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
  417. src_len = conn->sndbuf_desc->len - sent.count;
  418. }
  419. if (conn->lgr->is_smcd)
  420. rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
  421. dst_off, dst_len);
  422. else
  423. rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
  424. dst_off, dst_len);
  425. if (rc)
  426. return rc;
  427. if (conn->urg_tx_pend && len == to_send)
  428. pflags->urg_data_present = 1;
  429. smc_tx_advance_cursors(conn, &prod, &sent, len);
  430. /* update connection's cursors with advanced local cursors */
  431. smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
  432. /* dst: peer RMBE */
  433. smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
  434. return 0;
  435. }
  436. /* Wakeup sndbuf consumers from any context (IRQ or process)
  437. * since there is more data to transmit; usable snd_wnd as max transmit
  438. */
  439. static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
  440. {
  441. struct smc_cdc_producer_flags *pflags;
  442. struct smc_cdc_tx_pend *pend;
  443. struct smc_wr_buf *wr_buf;
  444. int rc;
  445. rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend);
  446. if (rc < 0) {
  447. if (rc == -EBUSY) {
  448. struct smc_sock *smc =
  449. container_of(conn, struct smc_sock, conn);
  450. if (smc->sk.sk_err == ECONNABORTED)
  451. return sock_error(&smc->sk);
  452. rc = 0;
  453. if (conn->alert_token_local) /* connection healthy */
  454. mod_delayed_work(system_wq, &conn->tx_work,
  455. SMC_TX_WORK_DELAY);
  456. }
  457. return rc;
  458. }
  459. spin_lock_bh(&conn->send_lock);
  460. if (!conn->local_tx_ctrl.prod_flags.urg_data_present) {
  461. rc = smc_tx_rdma_writes(conn);
  462. if (rc) {
  463. smc_wr_tx_put_slot(&conn->lgr->lnk[SMC_SINGLE_LINK],
  464. (struct smc_wr_tx_pend_priv *)pend);
  465. goto out_unlock;
  466. }
  467. }
  468. rc = smc_cdc_msg_send(conn, wr_buf, pend);
  469. pflags = &conn->local_tx_ctrl.prod_flags;
  470. if (!rc && pflags->urg_data_present) {
  471. pflags->urg_data_pending = 0;
  472. pflags->urg_data_present = 0;
  473. }
  474. out_unlock:
  475. spin_unlock_bh(&conn->send_lock);
  476. return rc;
  477. }
  478. static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
  479. {
  480. struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
  481. int rc = 0;
  482. spin_lock_bh(&conn->send_lock);
  483. if (!pflags->urg_data_present)
  484. rc = smc_tx_rdma_writes(conn);
  485. if (!rc)
  486. rc = smcd_cdc_msg_send(conn);
  487. if (!rc && pflags->urg_data_present) {
  488. pflags->urg_data_pending = 0;
  489. pflags->urg_data_present = 0;
  490. }
  491. spin_unlock_bh(&conn->send_lock);
  492. return rc;
  493. }
  494. int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
  495. {
  496. int rc;
  497. if (conn->lgr->is_smcd)
  498. rc = smcd_tx_sndbuf_nonempty(conn);
  499. else
  500. rc = smcr_tx_sndbuf_nonempty(conn);
  501. return rc;
  502. }
  503. /* Wakeup sndbuf consumers from process context
  504. * since there is more data to transmit
  505. */
  506. void smc_tx_work(struct work_struct *work)
  507. {
  508. struct smc_connection *conn = container_of(to_delayed_work(work),
  509. struct smc_connection,
  510. tx_work);
  511. struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
  512. int rc;
  513. lock_sock(&smc->sk);
  514. if (smc->sk.sk_err ||
  515. !conn->alert_token_local ||
  516. conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
  517. goto out;
  518. rc = smc_tx_sndbuf_nonempty(conn);
  519. if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
  520. !atomic_read(&conn->bytes_to_rcv))
  521. conn->local_rx_ctrl.prod_flags.write_blocked = 0;
  522. out:
  523. release_sock(&smc->sk);
  524. }
  525. void smc_tx_consumer_update(struct smc_connection *conn, bool force)
  526. {
  527. union smc_host_cursor cfed, cons, prod;
  528. int sender_free = conn->rmb_desc->len;
  529. int to_confirm;
  530. smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
  531. smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
  532. to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
  533. if (to_confirm > conn->rmbe_update_limit) {
  534. smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
  535. sender_free = conn->rmb_desc->len -
  536. smc_curs_diff_large(conn->rmb_desc->len,
  537. &cfed, &prod);
  538. }
  539. if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
  540. force ||
  541. ((to_confirm > conn->rmbe_update_limit) &&
  542. ((sender_free <= (conn->rmb_desc->len / 2)) ||
  543. conn->local_rx_ctrl.prod_flags.write_blocked))) {
  544. if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
  545. conn->alert_token_local) { /* connection healthy */
  546. schedule_delayed_work(&conn->tx_work,
  547. SMC_TX_WORK_DELAY);
  548. return;
  549. }
  550. smc_curs_copy(&conn->rx_curs_confirmed,
  551. &conn->local_tx_ctrl.cons, conn);
  552. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
  553. }
  554. if (conn->local_rx_ctrl.prod_flags.write_blocked &&
  555. !atomic_read(&conn->bytes_to_rcv))
  556. conn->local_rx_ctrl.prod_flags.write_blocked = 0;
  557. }
  558. /***************************** send initialize *******************************/
  559. /* Initialize send properties on connection establishment. NB: not __init! */
  560. void smc_tx_init(struct smc_sock *smc)
  561. {
  562. smc->sk.sk_write_space = smc_tx_write_space;
  563. }