]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/smc/smc_core.c
net/smc: prevent races between smc_lgr_terminate() and smc_conn_free()
[mirror_ubuntu-bionic-kernel.git] / net / smc / smc_core.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
0cfdd8f9
UB
2/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Basic Transport Functions exploiting Infiniband API
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
10 */
11
12#include <linux/socket.h>
13#include <linux/if_vlan.h>
14#include <linux/random.h>
15#include <linux/workqueue.h>
16#include <net/tcp.h>
17#include <net/sock.h>
18#include <rdma/ib_verbs.h>
19
20#include "smc.h"
21#include "smc_clc.h"
22#include "smc_core.h"
23#include "smc_ib.h"
f38ba179 24#include "smc_wr.h"
9bf9abea 25#include "smc_llc.h"
5f08318f 26#include "smc_cdc.h"
b38d7324 27#include "smc_close.h"
0cfdd8f9 28
5bc11ddb
UB
29#define SMC_LGR_NUM_INCR 256
30#define SMC_LGR_FREE_DELAY_SERV (600 * HZ)
31#define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10)
0cfdd8f9 32
9bf9abea
UB
33static u32 smc_lgr_num; /* unique link group number */
34
0cfdd8f9
UB
35/* Register connection's alert token in our lookup structure.
36 * To use rbtrees we have to implement our own insert core.
37 * Requires @conns_lock
38 * @smc connection to register
39 * Returns 0 on success, != otherwise.
40 */
41static void smc_lgr_add_alert_token(struct smc_connection *conn)
42{
43 struct rb_node **link, *parent = NULL;
44 u32 token = conn->alert_token_local;
45
46 link = &conn->lgr->conns_all.rb_node;
47 while (*link) {
48 struct smc_connection *cur = rb_entry(*link,
49 struct smc_connection, alert_node);
50
51 parent = *link;
52 if (cur->alert_token_local > token)
53 link = &parent->rb_left;
54 else
55 link = &parent->rb_right;
56 }
57 /* Put the new node there */
58 rb_link_node(&conn->alert_node, parent, link);
59 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
60}
61
62/* Register connection in link group by assigning an alert token
63 * registered in a search tree.
64 * Requires @conns_lock
65 * Note that '0' is a reserved value and not assigned.
66 */
67static void smc_lgr_register_conn(struct smc_connection *conn)
68{
69 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
70 static atomic_t nexttoken = ATOMIC_INIT(0);
71
72 /* find a new alert_token_local value not yet used by some connection
73 * in this link group
74 */
75 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
76 while (!conn->alert_token_local) {
77 conn->alert_token_local = atomic_inc_return(&nexttoken);
78 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
79 conn->alert_token_local = 0;
80 }
81 smc_lgr_add_alert_token(conn);
82 conn->lgr->conns_num++;
83}
84
85/* Unregister connection and reset the alert token of the given connection<
86 */
87static void __smc_lgr_unregister_conn(struct smc_connection *conn)
88{
89 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
90 struct smc_link_group *lgr = conn->lgr;
91
92 rb_erase(&conn->alert_node, &lgr->conns_all);
93 lgr->conns_num--;
94 conn->alert_token_local = 0;
95 conn->lgr = NULL;
96 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
97}
98
99/* Unregister connection and trigger lgr freeing if applicable
100 */
101static void smc_lgr_unregister_conn(struct smc_connection *conn)
102{
103 struct smc_link_group *lgr = conn->lgr;
104 int reduced = 0;
105
33a47b9e
KG
106 if (!lgr)
107 return;
0cfdd8f9
UB
108 write_lock_bh(&lgr->conns_lock);
109 if (conn->alert_token_local) {
110 reduced = 1;
111 __smc_lgr_unregister_conn(conn);
112 }
113 write_unlock_bh(&lgr->conns_lock);
5bc11ddb
UB
114 if (!reduced || lgr->conns_num)
115 return;
116 /* client link group creation always follows the server link group
117 * creation. For client use a somewhat higher removal delay time,
118 * otherwise there is a risk of out-of-sync link groups.
119 */
120 mod_delayed_work(system_wq, &lgr->free_work,
121 lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT :
122 SMC_LGR_FREE_DELAY_SERV);
0cfdd8f9
UB
123}
124
125static void smc_lgr_free_work(struct work_struct *work)
126{
127 struct smc_link_group *lgr = container_of(to_delayed_work(work),
128 struct smc_link_group,
129 free_work);
130 bool conns;
131
132 spin_lock_bh(&smc_lgr_list.lock);
133 read_lock_bh(&lgr->conns_lock);
134 conns = RB_EMPTY_ROOT(&lgr->conns_all);
135 read_unlock_bh(&lgr->conns_lock);
136 if (!conns) { /* number of lgr connections is no longer zero */
137 spin_unlock_bh(&smc_lgr_list.lock);
138 return;
139 }
140 list_del_init(&lgr->list); /* remove from smc_lgr_list */
141 spin_unlock_bh(&smc_lgr_list.lock);
142 smc_lgr_free(lgr);
143}
144
145/* create a new SMC link group */
146static int smc_lgr_create(struct smc_sock *smc, __be32 peer_in_addr,
147 struct smc_ib_device *smcibdev, u8 ibport,
148 char *peer_systemid, unsigned short vlan_id)
149{
150 struct smc_link_group *lgr;
151 struct smc_link *lnk;
152 u8 rndvec[3];
153 int rc = 0;
cd6851f3 154 int i;
0cfdd8f9
UB
155
156 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
157 if (!lgr) {
158 rc = -ENOMEM;
159 goto out;
160 }
161 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
162 lgr->sync_err = false;
163 lgr->daddr = peer_in_addr;
164 memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN);
165 lgr->vlan_id = vlan_id;
cd6851f3
UB
166 rwlock_init(&lgr->sndbufs_lock);
167 rwlock_init(&lgr->rmbs_lock);
168 for (i = 0; i < SMC_RMBE_SIZES; i++) {
169 INIT_LIST_HEAD(&lgr->sndbufs[i]);
170 INIT_LIST_HEAD(&lgr->rmbs[i]);
171 }
9bf9abea
UB
172 smc_lgr_num += SMC_LGR_NUM_INCR;
173 memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE);
0cfdd8f9
UB
174 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
175 lgr->conns_all = RB_ROOT;
176
177 lnk = &lgr->lnk[SMC_SINGLE_LINK];
178 /* initialize link */
d42cd4df 179 lnk->link_id = SMC_SINGLE_LINK;
0cfdd8f9
UB
180 lnk->smcibdev = smcibdev;
181 lnk->ibport = ibport;
182 lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
bd4ad577
UB
183 if (!smcibdev->initialized)
184 smc_ib_setup_per_ibdev(smcibdev);
0cfdd8f9
UB
185 get_random_bytes(rndvec, sizeof(rndvec));
186 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16);
f38ba179
UB
187 rc = smc_wr_alloc_link_mem(lnk);
188 if (rc)
189 goto free_lgr;
bd4ad577
UB
190 rc = smc_ib_create_protection_domain(lnk);
191 if (rc)
192 goto free_link_mem;
193 rc = smc_ib_create_queue_pair(lnk);
194 if (rc)
195 goto dealloc_pd;
196 rc = smc_wr_create_link(lnk);
197 if (rc)
198 goto destroy_qp;
9bf9abea
UB
199 init_completion(&lnk->llc_confirm);
200 init_completion(&lnk->llc_confirm_resp);
0cfdd8f9
UB
201
202 smc->conn.lgr = lgr;
203 rwlock_init(&lgr->conns_lock);
204 spin_lock_bh(&smc_lgr_list.lock);
205 list_add(&lgr->list, &smc_lgr_list.list);
206 spin_unlock_bh(&smc_lgr_list.lock);
f38ba179
UB
207 return 0;
208
bd4ad577
UB
209destroy_qp:
210 smc_ib_destroy_queue_pair(lnk);
211dealloc_pd:
212 smc_ib_dealloc_protection_domain(lnk);
213free_link_mem:
214 smc_wr_free_link_mem(lnk);
f38ba179
UB
215free_lgr:
216 kfree(lgr);
0cfdd8f9
UB
217out:
218 return rc;
219}
220
3e034725 221static void smc_buf_unuse(struct smc_connection *conn)
cd6851f3
UB
222{
223 if (conn->sndbuf_desc) {
224 conn->sndbuf_desc->used = 0;
225 conn->sndbuf_size = 0;
226 }
cd6851f3 227 if (conn->rmb_desc) {
897e1c24 228 conn->rmb_desc->reused = true;
cd6851f3
UB
229 conn->rmb_desc->used = 0;
230 conn->rmbe_size = 0;
231 }
232}
233
0cfdd8f9
UB
234/* remove a finished connection from its link group */
235void smc_conn_free(struct smc_connection *conn)
236{
237 struct smc_link_group *lgr = conn->lgr;
238
239 if (!lgr)
240 return;
5f08318f 241 smc_cdc_tx_dismiss_slots(conn);
0cfdd8f9 242 smc_lgr_unregister_conn(conn);
3e034725 243 smc_buf_unuse(conn);
0cfdd8f9
UB
244}
245
246static void smc_link_clear(struct smc_link *lnk)
247{
248 lnk->peer_qpn = 0;
bd4ad577 249 smc_ib_modify_qp_reset(lnk);
f38ba179 250 smc_wr_free_link(lnk);
bd4ad577
UB
251 smc_ib_destroy_queue_pair(lnk);
252 smc_ib_dealloc_protection_domain(lnk);
f38ba179 253 smc_wr_free_link_mem(lnk);
0cfdd8f9
UB
254}
255
3e034725
UB
256static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
257 bool is_rmb)
cd6851f3 258{
3e034725
UB
259 if (is_rmb) {
260 if (buf_desc->mr_rx[SMC_SINGLE_LINK])
261 smc_ib_put_memory_region(
262 buf_desc->mr_rx[SMC_SINGLE_LINK]);
263 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
264 DMA_FROM_DEVICE);
265 } else {
266 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
267 DMA_TO_DEVICE);
cd6851f3 268 }
3e034725
UB
269 sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]);
270 if (buf_desc->cpu_addr)
271 free_pages((unsigned long)buf_desc->cpu_addr, buf_desc->order);
272 kfree(buf_desc);
cd6851f3
UB
273}
274
3e034725 275static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
cd6851f3 276{
bd4ad577 277 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
3e034725
UB
278 struct smc_buf_desc *buf_desc, *bf_desc;
279 struct list_head *buf_list;
cd6851f3
UB
280 int i;
281
282 for (i = 0; i < SMC_RMBE_SIZES; i++) {
3e034725
UB
283 if (is_rmb)
284 buf_list = &lgr->rmbs[i];
285 else
286 buf_list = &lgr->sndbufs[i];
287 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
cd6851f3 288 list) {
3e034725
UB
289 list_del(&buf_desc->list);
290 smc_buf_free(buf_desc, lnk, is_rmb);
cd6851f3
UB
291 }
292 }
293}
294
3e034725
UB
295static void smc_lgr_free_bufs(struct smc_link_group *lgr)
296{
297 /* free send buffers */
298 __smc_lgr_free_bufs(lgr, false);
299 /* free rmbs */
300 __smc_lgr_free_bufs(lgr, true);
301}
302
0cfdd8f9
UB
303/* remove a link group */
304void smc_lgr_free(struct smc_link_group *lgr)
305{
3e034725 306 smc_lgr_free_bufs(lgr);
0cfdd8f9
UB
307 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
308 kfree(lgr);
309}
310
311/* terminate linkgroup abnormally */
312void smc_lgr_terminate(struct smc_link_group *lgr)
313{
314 struct smc_connection *conn;
b38d7324 315 struct smc_sock *smc;
0cfdd8f9
UB
316 struct rb_node *node;
317
318 spin_lock_bh(&smc_lgr_list.lock);
319 if (list_empty(&lgr->list)) {
320 /* termination already triggered */
321 spin_unlock_bh(&smc_lgr_list.lock);
322 return;
323 }
324 /* do not use this link group for new connections */
325 list_del_init(&lgr->list);
326 spin_unlock_bh(&smc_lgr_list.lock);
327
328 write_lock_bh(&lgr->conns_lock);
329 node = rb_first(&lgr->conns_all);
330 while (node) {
331 conn = rb_entry(node, struct smc_connection, alert_node);
b38d7324
UB
332 smc = container_of(conn, struct smc_sock, conn);
333 sock_hold(&smc->sk);
0cfdd8f9 334 __smc_lgr_unregister_conn(conn);
46c28dbd 335 schedule_work(&conn->close_work);
b38d7324 336 sock_put(&smc->sk);
0cfdd8f9
UB
337 node = rb_first(&lgr->conns_all);
338 }
339 write_unlock_bh(&lgr->conns_lock);
0cfdd8f9
UB
340}
341
342/* Determine vlan of internal TCP socket.
343 * @vlan_id: address to store the determined vlan id into
344 */
345static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
346{
347 struct dst_entry *dst = sk_dst_get(clcsock->sk);
348 int rc = 0;
349
350 *vlan_id = 0;
351 if (!dst) {
352 rc = -ENOTCONN;
353 goto out;
354 }
355 if (!dst->dev) {
356 rc = -ENODEV;
357 goto out_rel;
358 }
359
360 if (is_vlan_dev(dst->dev))
361 *vlan_id = vlan_dev_vlan_id(dst->dev);
362
363out_rel:
364 dst_release(dst);
365out:
366 return rc;
367}
368
369/* determine the link gid matching the vlan id of the link group */
370static int smc_link_determine_gid(struct smc_link_group *lgr)
371{
372 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
373 struct ib_gid_attr gattr;
374 union ib_gid gid;
375 int i;
376
377 if (!lgr->vlan_id) {
378 lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1];
379 return 0;
380 }
381
382 for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len;
383 i++) {
384 if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid,
385 &gattr))
386 continue;
43e2ada3
UB
387 if (gattr.ndev) {
388 if (is_vlan_dev(gattr.ndev) &&
389 vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) {
390 lnk->gid = gid;
391 dev_put(gattr.ndev);
392 return 0;
393 }
394 dev_put(gattr.ndev);
0cfdd8f9
UB
395 }
396 }
397 return -ENODEV;
398}
399
400/* create a new SMC connection (and a new link group if necessary) */
401int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr,
402 struct smc_ib_device *smcibdev, u8 ibport,
403 struct smc_clc_msg_local *lcl, int srv_first_contact)
404{
405 struct smc_connection *conn = &smc->conn;
406 struct smc_link_group *lgr;
407 unsigned short vlan_id;
408 enum smc_lgr_role role;
409 int local_contact = SMC_FIRST_CONTACT;
410 int rc = 0;
411
412 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
413 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
414 if (rc)
415 return rc;
416
417 if ((role == SMC_CLNT) && srv_first_contact)
418 /* create new link group as well */
419 goto create;
420
421 /* determine if an existing link group can be reused */
422 spin_lock_bh(&smc_lgr_list.lock);
423 list_for_each_entry(lgr, &smc_lgr_list.list, list) {
424 write_lock_bh(&lgr->conns_lock);
425 if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
426 SMC_SYSTEMID_LEN) &&
427 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
428 SMC_GID_SIZE) &&
429 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
430 sizeof(lcl->mac)) &&
431 !lgr->sync_err &&
432 (lgr->role == role) &&
cd6851f3
UB
433 (lgr->vlan_id == vlan_id) &&
434 ((role == SMC_CLNT) ||
435 (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
0cfdd8f9
UB
436 /* link group found */
437 local_contact = SMC_REUSE_CONTACT;
438 conn->lgr = lgr;
439 smc_lgr_register_conn(conn); /* add smc conn to lgr */
33a47b9e
KG
440 if (delayed_work_pending(&lgr->free_work))
441 cancel_delayed_work(&lgr->free_work);
0cfdd8f9
UB
442 write_unlock_bh(&lgr->conns_lock);
443 break;
444 }
445 write_unlock_bh(&lgr->conns_lock);
446 }
447 spin_unlock_bh(&smc_lgr_list.lock);
448
449 if (role == SMC_CLNT && !srv_first_contact &&
450 (local_contact == SMC_FIRST_CONTACT)) {
451 /* Server reuses a link group, but Client wants to start
452 * a new one
453 * send out_of_sync decline, reason synchr. error
454 */
455 return -ENOLINK;
456 }
457
458create:
459 if (local_contact == SMC_FIRST_CONTACT) {
460 rc = smc_lgr_create(smc, peer_in_addr, smcibdev, ibport,
461 lcl->id_for_peer, vlan_id);
462 if (rc)
463 goto out;
464 smc_lgr_register_conn(conn); /* add smc conn to lgr */
465 rc = smc_link_determine_gid(conn->lgr);
466 }
5f08318f
UB
467 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
468 conn->local_tx_ctrl.len = sizeof(struct smc_cdc_msg);
469#ifndef KERNEL_HAS_ATOMIC64
470 spin_lock_init(&conn->acurs_lock);
471#endif
0cfdd8f9
UB
472
473out:
474 return rc ? rc : local_contact;
475}
cd6851f3 476
3e034725
UB
477/* try to reuse a sndbuf or rmb description slot for a certain
478 * buffer size; if not available, return NULL
cd6851f3
UB
479 */
480static inline
3e034725
UB
481struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr,
482 int compressed_bufsize,
483 rwlock_t *lock,
484 struct list_head *buf_list)
cd6851f3 485{
3e034725 486 struct smc_buf_desc *buf_slot;
cd6851f3 487
3e034725
UB
488 read_lock_bh(lock);
489 list_for_each_entry(buf_slot, buf_list, list) {
490 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
491 read_unlock_bh(lock);
492 return buf_slot;
cd6851f3
UB
493 }
494 }
3e034725 495 read_unlock_bh(lock);
cd6851f3
UB
496 return NULL;
497}
498
952310cc
UB
499/* one of the conditions for announcing a receiver's current window size is
500 * that it "results in a minimum increase in the window size of 10% of the
501 * receive buffer space" [RFC7609]
502 */
503static inline int smc_rmb_wnd_update_limit(int rmbe_size)
504{
505 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
506}
507
b33982c3
UB
508static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
509 bool is_rmb, int bufsize)
510{
511 struct smc_buf_desc *buf_desc;
512 struct smc_link *lnk;
513 int rc;
514
515 /* try to alloc a new buffer */
516 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
517 if (!buf_desc)
518 return ERR_PTR(-ENOMEM);
519
520 buf_desc->cpu_addr =
521 (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN |
522 __GFP_NOMEMALLOC |
523 __GFP_NORETRY | __GFP_ZERO,
524 get_order(bufsize));
525 if (!buf_desc->cpu_addr) {
526 kfree(buf_desc);
527 return ERR_PTR(-EAGAIN);
528 }
529 buf_desc->order = get_order(bufsize);
530
531 /* build the sg table from the pages */
532 lnk = &lgr->lnk[SMC_SINGLE_LINK];
533 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
534 GFP_KERNEL);
535 if (rc) {
536 smc_buf_free(buf_desc, lnk, is_rmb);
537 return ERR_PTR(rc);
538 }
539 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
540 buf_desc->cpu_addr, bufsize);
541
542 /* map sg table to DMA address */
543 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
544 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
545 /* SMC protocol depends on mapping to one DMA address only */
546 if (rc != 1) {
547 smc_buf_free(buf_desc, lnk, is_rmb);
548 return ERR_PTR(-EAGAIN);
549 }
550
551 /* create a new memory region for the RMB */
552 if (is_rmb) {
553 rc = smc_ib_get_memory_region(lnk->roce_pd,
554 IB_ACCESS_REMOTE_WRITE |
555 IB_ACCESS_LOCAL_WRITE,
556 buf_desc);
557 if (rc) {
558 smc_buf_free(buf_desc, lnk, is_rmb);
559 return ERR_PTR(rc);
560 }
561 }
562
563 return buf_desc;
564}
565
3e034725 566static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
cd6851f3
UB
567{
568 struct smc_connection *conn = &smc->conn;
569 struct smc_link_group *lgr = conn->lgr;
68870370 570 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
3e034725 571 struct list_head *buf_list;
c45abf31 572 int bufsize, bufsize_short;
3e034725
UB
573 int sk_buf_size;
574 rwlock_t *lock;
cd6851f3 575
3e034725
UB
576 if (is_rmb)
577 /* use socket recv buffer size (w/o overhead) as start value */
578 sk_buf_size = smc->sk.sk_rcvbuf / 2;
579 else
580 /* use socket send buffer size (w/o overhead) as start value */
581 sk_buf_size = smc->sk.sk_sndbuf / 2;
582
4e1061f4 583 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
c45abf31 584 bufsize_short >= 0; bufsize_short--) {
9d8fb617 585
3e034725
UB
586 if (is_rmb) {
587 lock = &lgr->rmbs_lock;
588 buf_list = &lgr->rmbs[bufsize_short];
589 } else {
590 lock = &lgr->sndbufs_lock;
591 buf_list = &lgr->sndbufs[bufsize_short];
9d8fb617 592 }
c45abf31 593 bufsize = smc_uncompress_bufsize(bufsize_short);
a3fe3d01
UB
594 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
595 continue;
596
3e034725
UB
597 /* check for reusable slot in the link group */
598 buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list);
599 if (buf_desc) {
600 memset(buf_desc->cpu_addr, 0, bufsize);
cd6851f3
UB
601 break; /* found reusable slot */
602 }
a3fe3d01 603
b33982c3
UB
604 buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
605 if (PTR_ERR(buf_desc) == -ENOMEM)
606 break;
607 if (IS_ERR(buf_desc))
a3fe3d01 608 continue;
897e1c24 609
3e034725
UB
610 buf_desc->used = 1;
611 write_lock_bh(lock);
612 list_add(&buf_desc->list, buf_list);
613 write_unlock_bh(lock);
614 break; /* found */
cd6851f3 615 }
3e034725 616
b33982c3 617 if (IS_ERR(buf_desc))
3e034725
UB
618 return -ENOMEM;
619
620 if (is_rmb) {
621 conn->rmb_desc = buf_desc;
c45abf31
UB
622 conn->rmbe_size = bufsize;
623 conn->rmbe_size_short = bufsize_short;
624 smc->sk.sk_rcvbuf = bufsize * 2;
5f08318f 625 atomic_set(&conn->bytes_to_rcv, 0);
c45abf31 626 conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
cd6851f3 627 } else {
3e034725
UB
628 conn->sndbuf_desc = buf_desc;
629 conn->sndbuf_size = bufsize;
630 smc->sk.sk_sndbuf = bufsize * 2;
631 atomic_set(&conn->sndbuf_space, bufsize);
cd6851f3 632 }
3e034725
UB
633 return 0;
634}
635
10428dd8
UB
636void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
637{
638 struct smc_link_group *lgr = conn->lgr;
639
640 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
641 conn->sndbuf_desc, DMA_TO_DEVICE);
642}
643
644void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
645{
646 struct smc_link_group *lgr = conn->lgr;
647
648 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
649 conn->sndbuf_desc, DMA_TO_DEVICE);
650}
651
652void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
653{
654 struct smc_link_group *lgr = conn->lgr;
655
656 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
657 conn->rmb_desc, DMA_FROM_DEVICE);
658}
659
660void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
661{
662 struct smc_link_group *lgr = conn->lgr;
663
664 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
665 conn->rmb_desc, DMA_FROM_DEVICE);
666}
667
3e034725
UB
668/* create the send and receive buffer for an SMC socket;
669 * receive buffers are called RMBs;
670 * (even though the SMC protocol allows more than one RMB-element per RMB,
671 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
672 * extra RMB for every connection in a link group
673 */
674int smc_buf_create(struct smc_sock *smc)
675{
676 int rc;
677
678 /* create send buffer */
679 rc = __smc_buf_create(smc, false);
680 if (rc)
681 return rc;
682 /* create rmb */
683 rc = __smc_buf_create(smc, true);
684 if (rc)
685 smc_buf_free(smc->conn.sndbuf_desc,
686 &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false);
687 return rc;
cd6851f3 688}
bd4ad577
UB
689
690static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
691{
692 int i;
693
694 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
695 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
696 return i;
697 }
698 return -ENOSPC;
699}
700
701/* save rkey and dma_addr received from peer during clc handshake */
702int smc_rmb_rtoken_handling(struct smc_connection *conn,
703 struct smc_clc_msg_accept_confirm *clc)
704{
705 u64 dma_addr = be64_to_cpu(clc->rmb_dma_addr);
706 struct smc_link_group *lgr = conn->lgr;
707 u32 rkey = ntohl(clc->rmb_rkey);
708 int i;
709
710 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
711 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
263eec9b 712 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
bd4ad577
UB
713 test_bit(i, lgr->rtokens_used_mask)) {
714 conn->rtoken_idx = i;
715 return 0;
716 }
717 }
718 conn->rtoken_idx = smc_rmb_reserve_rtoken_idx(lgr);
719 if (conn->rtoken_idx < 0)
720 return conn->rtoken_idx;
721 lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey = rkey;
722 lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr = dma_addr;
723 return 0;
724}