• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
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"
24 #include "smc_wr.h"
25 #include "smc_llc.h"
26 #include "smc_cdc.h"
27 #include "smc_close.h"
28 
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)
32 
33 static u32 smc_lgr_num;			/* unique link group number */
34 
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  */
smc_lgr_add_alert_token(struct smc_connection * conn)41 static 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  */
smc_lgr_register_conn(struct smc_connection * conn)67 static 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  */
__smc_lgr_unregister_conn(struct smc_connection * conn)87 static 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  */
smc_lgr_unregister_conn(struct smc_connection * conn)101 static void smc_lgr_unregister_conn(struct smc_connection *conn)
102 {
103 	struct smc_link_group *lgr = conn->lgr;
104 	int reduced = 0;
105 
106 	if (!lgr)
107 		return;
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);
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);
123 }
124 
smc_lgr_free_work(struct work_struct * work)125 static 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 */
smc_lgr_create(struct smc_sock * smc,__be32 peer_in_addr,struct smc_ib_device * smcibdev,u8 ibport,char * peer_systemid,unsigned short vlan_id)146 static 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;
154 	int i;
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;
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 	}
172 	smc_lgr_num += SMC_LGR_NUM_INCR;
173 	memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE);
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 */
179 	lnk->link_id = SMC_SINGLE_LINK;
180 	lnk->smcibdev = smcibdev;
181 	lnk->ibport = ibport;
182 	lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
183 	if (!smcibdev->initialized)
184 		smc_ib_setup_per_ibdev(smcibdev);
185 	get_random_bytes(rndvec, sizeof(rndvec));
186 	lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16);
187 	rc = smc_wr_alloc_link_mem(lnk);
188 	if (rc)
189 		goto free_lgr;
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;
199 	init_completion(&lnk->llc_confirm);
200 	init_completion(&lnk->llc_confirm_resp);
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);
207 	return 0;
208 
209 destroy_qp:
210 	smc_ib_destroy_queue_pair(lnk);
211 dealloc_pd:
212 	smc_ib_dealloc_protection_domain(lnk);
213 free_link_mem:
214 	smc_wr_free_link_mem(lnk);
215 free_lgr:
216 	kfree(lgr);
217 out:
218 	return rc;
219 }
220 
smc_buf_unuse(struct smc_connection * conn)221 static void smc_buf_unuse(struct smc_connection *conn)
222 {
223 	if (conn->sndbuf_desc) {
224 		conn->sndbuf_desc->used = 0;
225 		conn->sndbuf_size = 0;
226 	}
227 	if (conn->rmb_desc) {
228 		conn->rmb_desc->reused = true;
229 		conn->rmb_desc->used = 0;
230 		conn->rmbe_size = 0;
231 	}
232 }
233 
234 /* remove a finished connection from its link group */
smc_conn_free(struct smc_connection * conn)235 void smc_conn_free(struct smc_connection *conn)
236 {
237 	struct smc_link_group *lgr = conn->lgr;
238 
239 	if (!lgr)
240 		return;
241 	smc_cdc_tx_dismiss_slots(conn);
242 	smc_lgr_unregister_conn(conn);
243 	smc_buf_unuse(conn);
244 }
245 
smc_link_clear(struct smc_link * lnk)246 static void smc_link_clear(struct smc_link *lnk)
247 {
248 	lnk->peer_qpn = 0;
249 	smc_ib_modify_qp_reset(lnk);
250 	smc_wr_free_link(lnk);
251 	smc_ib_destroy_queue_pair(lnk);
252 	smc_ib_dealloc_protection_domain(lnk);
253 	smc_wr_free_link_mem(lnk);
254 }
255 
smc_buf_free(struct smc_buf_desc * buf_desc,struct smc_link * lnk,bool is_rmb)256 static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
257 			 bool is_rmb)
258 {
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);
268 	}
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);
273 }
274 
__smc_lgr_free_bufs(struct smc_link_group * lgr,bool is_rmb)275 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
276 {
277 	struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
278 	struct smc_buf_desc *buf_desc, *bf_desc;
279 	struct list_head *buf_list;
280 	int i;
281 
282 	for (i = 0; i < SMC_RMBE_SIZES; i++) {
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,
288 					 list) {
289 			list_del(&buf_desc->list);
290 			smc_buf_free(buf_desc, lnk, is_rmb);
291 		}
292 	}
293 }
294 
smc_lgr_free_bufs(struct smc_link_group * lgr)295 static 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 
303 /* remove a link group */
smc_lgr_free(struct smc_link_group * lgr)304 void smc_lgr_free(struct smc_link_group *lgr)
305 {
306 	smc_lgr_free_bufs(lgr);
307 	smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
308 	kfree(lgr);
309 }
310 
311 /* terminate linkgroup abnormally */
smc_lgr_terminate(struct smc_link_group * lgr)312 void smc_lgr_terminate(struct smc_link_group *lgr)
313 {
314 	struct smc_connection *conn;
315 	struct smc_sock *smc;
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);
332 		smc = container_of(conn, struct smc_sock, conn);
333 		sock_hold(&smc->sk);
334 		__smc_lgr_unregister_conn(conn);
335 		schedule_work(&conn->close_work);
336 		sock_put(&smc->sk);
337 		node = rb_first(&lgr->conns_all);
338 	}
339 	write_unlock_bh(&lgr->conns_lock);
340 }
341 
342 /* Determine vlan of internal TCP socket.
343  * @vlan_id: address to store the determined vlan id into
344  */
smc_vlan_by_tcpsk(struct socket * clcsock,unsigned short * vlan_id)345 static 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 
363 out_rel:
364 	dst_release(dst);
365 out:
366 	return rc;
367 }
368 
369 /* determine the link gid matching the vlan id of the link group */
smc_link_determine_gid(struct smc_link_group * lgr)370 static 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;
387 		if (gattr.ndev &&
388 		    (vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id)) {
389 			lnk->gid = gid;
390 			return 0;
391 		}
392 	}
393 	return -ENODEV;
394 }
395 
396 /* create a new SMC connection (and a new link group if necessary) */
smc_conn_create(struct smc_sock * smc,__be32 peer_in_addr,struct smc_ib_device * smcibdev,u8 ibport,struct smc_clc_msg_local * lcl,int srv_first_contact)397 int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr,
398 		    struct smc_ib_device *smcibdev, u8 ibport,
399 		    struct smc_clc_msg_local *lcl, int srv_first_contact)
400 {
401 	struct smc_connection *conn = &smc->conn;
402 	struct smc_link_group *lgr;
403 	unsigned short vlan_id;
404 	enum smc_lgr_role role;
405 	int local_contact = SMC_FIRST_CONTACT;
406 	int rc = 0;
407 
408 	role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
409 	rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
410 	if (rc)
411 		return rc;
412 
413 	if ((role == SMC_CLNT) && srv_first_contact)
414 		/* create new link group as well */
415 		goto create;
416 
417 	/* determine if an existing link group can be reused */
418 	spin_lock_bh(&smc_lgr_list.lock);
419 	list_for_each_entry(lgr, &smc_lgr_list.list, list) {
420 		write_lock_bh(&lgr->conns_lock);
421 		if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
422 			    SMC_SYSTEMID_LEN) &&
423 		    !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
424 			    SMC_GID_SIZE) &&
425 		    !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
426 			    sizeof(lcl->mac)) &&
427 		    !lgr->sync_err &&
428 		    (lgr->role == role) &&
429 		    (lgr->vlan_id == vlan_id) &&
430 		    ((role == SMC_CLNT) ||
431 		     (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
432 			/* link group found */
433 			local_contact = SMC_REUSE_CONTACT;
434 			conn->lgr = lgr;
435 			smc_lgr_register_conn(conn); /* add smc conn to lgr */
436 			if (delayed_work_pending(&lgr->free_work))
437 				cancel_delayed_work(&lgr->free_work);
438 			write_unlock_bh(&lgr->conns_lock);
439 			break;
440 		}
441 		write_unlock_bh(&lgr->conns_lock);
442 	}
443 	spin_unlock_bh(&smc_lgr_list.lock);
444 
445 	if (role == SMC_CLNT && !srv_first_contact &&
446 	    (local_contact == SMC_FIRST_CONTACT)) {
447 		/* Server reuses a link group, but Client wants to start
448 		 * a new one
449 		 * send out_of_sync decline, reason synchr. error
450 		 */
451 		return -ENOLINK;
452 	}
453 
454 create:
455 	if (local_contact == SMC_FIRST_CONTACT) {
456 		rc = smc_lgr_create(smc, peer_in_addr, smcibdev, ibport,
457 				    lcl->id_for_peer, vlan_id);
458 		if (rc)
459 			goto out;
460 		smc_lgr_register_conn(conn); /* add smc conn to lgr */
461 		rc = smc_link_determine_gid(conn->lgr);
462 	}
463 	conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
464 	conn->local_tx_ctrl.len = sizeof(struct smc_cdc_msg);
465 #ifndef KERNEL_HAS_ATOMIC64
466 	spin_lock_init(&conn->acurs_lock);
467 #endif
468 
469 out:
470 	return rc ? rc : local_contact;
471 }
472 
473 /* try to reuse a sndbuf or rmb description slot for a certain
474  * buffer size; if not available, return NULL
475  */
476 static inline
smc_buf_get_slot(struct smc_link_group * lgr,int compressed_bufsize,rwlock_t * lock,struct list_head * buf_list)477 struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr,
478 				      int compressed_bufsize,
479 				      rwlock_t *lock,
480 				      struct list_head *buf_list)
481 {
482 	struct smc_buf_desc *buf_slot;
483 
484 	read_lock_bh(lock);
485 	list_for_each_entry(buf_slot, buf_list, list) {
486 		if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
487 			read_unlock_bh(lock);
488 			return buf_slot;
489 		}
490 	}
491 	read_unlock_bh(lock);
492 	return NULL;
493 }
494 
495 /* one of the conditions for announcing a receiver's current window size is
496  * that it "results in a minimum increase in the window size of 10% of the
497  * receive buffer space" [RFC7609]
498  */
smc_rmb_wnd_update_limit(int rmbe_size)499 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
500 {
501 	return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
502 }
503 
smc_new_buf_create(struct smc_link_group * lgr,bool is_rmb,int bufsize)504 static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
505 					       bool is_rmb, int bufsize)
506 {
507 	struct smc_buf_desc *buf_desc;
508 	struct smc_link *lnk;
509 	int rc;
510 
511 	/* try to alloc a new buffer */
512 	buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
513 	if (!buf_desc)
514 		return ERR_PTR(-ENOMEM);
515 
516 	buf_desc->cpu_addr =
517 		(void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN |
518 					 __GFP_NOMEMALLOC |
519 					 __GFP_NORETRY | __GFP_ZERO,
520 					 get_order(bufsize));
521 	if (!buf_desc->cpu_addr) {
522 		kfree(buf_desc);
523 		return ERR_PTR(-EAGAIN);
524 	}
525 	buf_desc->order = get_order(bufsize);
526 
527 	/* build the sg table from the pages */
528 	lnk = &lgr->lnk[SMC_SINGLE_LINK];
529 	rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
530 			    GFP_KERNEL);
531 	if (rc) {
532 		smc_buf_free(buf_desc, lnk, is_rmb);
533 		return ERR_PTR(rc);
534 	}
535 	sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
536 		   buf_desc->cpu_addr, bufsize);
537 
538 	/* map sg table to DMA address */
539 	rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
540 			       is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
541 	/* SMC protocol depends on mapping to one DMA address only */
542 	if (rc != 1)  {
543 		smc_buf_free(buf_desc, lnk, is_rmb);
544 		return ERR_PTR(-EAGAIN);
545 	}
546 
547 	/* create a new memory region for the RMB */
548 	if (is_rmb) {
549 		rc = smc_ib_get_memory_region(lnk->roce_pd,
550 					      IB_ACCESS_REMOTE_WRITE |
551 					      IB_ACCESS_LOCAL_WRITE,
552 					      buf_desc);
553 		if (rc) {
554 			smc_buf_free(buf_desc, lnk, is_rmb);
555 			return ERR_PTR(rc);
556 		}
557 	}
558 
559 	return buf_desc;
560 }
561 
__smc_buf_create(struct smc_sock * smc,bool is_rmb)562 static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
563 {
564 	struct smc_connection *conn = &smc->conn;
565 	struct smc_link_group *lgr = conn->lgr;
566 	struct smc_buf_desc *buf_desc = NULL;
567 	struct list_head *buf_list;
568 	int bufsize, bufsize_short;
569 	int sk_buf_size;
570 	rwlock_t *lock;
571 
572 	if (is_rmb)
573 		/* use socket recv buffer size (w/o overhead) as start value */
574 		sk_buf_size = smc->sk.sk_rcvbuf / 2;
575 	else
576 		/* use socket send buffer size (w/o overhead) as start value */
577 		sk_buf_size = smc->sk.sk_sndbuf / 2;
578 
579 	for (bufsize_short = smc_compress_bufsize(sk_buf_size);
580 	     bufsize_short >= 0; bufsize_short--) {
581 
582 		if (is_rmb) {
583 			lock = &lgr->rmbs_lock;
584 			buf_list = &lgr->rmbs[bufsize_short];
585 		} else {
586 			lock = &lgr->sndbufs_lock;
587 			buf_list = &lgr->sndbufs[bufsize_short];
588 		}
589 		bufsize = smc_uncompress_bufsize(bufsize_short);
590 		if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
591 			continue;
592 
593 		/* check for reusable slot in the link group */
594 		buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list);
595 		if (buf_desc) {
596 			memset(buf_desc->cpu_addr, 0, bufsize);
597 			break; /* found reusable slot */
598 		}
599 
600 		buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
601 		if (PTR_ERR(buf_desc) == -ENOMEM)
602 			break;
603 		if (IS_ERR(buf_desc))
604 			continue;
605 
606 		buf_desc->used = 1;
607 		write_lock_bh(lock);
608 		list_add(&buf_desc->list, buf_list);
609 		write_unlock_bh(lock);
610 		break; /* found */
611 	}
612 
613 	if (IS_ERR(buf_desc))
614 		return -ENOMEM;
615 
616 	if (is_rmb) {
617 		conn->rmb_desc = buf_desc;
618 		conn->rmbe_size = bufsize;
619 		conn->rmbe_size_short = bufsize_short;
620 		smc->sk.sk_rcvbuf = bufsize * 2;
621 		atomic_set(&conn->bytes_to_rcv, 0);
622 		conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
623 	} else {
624 		conn->sndbuf_desc = buf_desc;
625 		conn->sndbuf_size = bufsize;
626 		smc->sk.sk_sndbuf = bufsize * 2;
627 		atomic_set(&conn->sndbuf_space, bufsize);
628 	}
629 	return 0;
630 }
631 
smc_sndbuf_sync_sg_for_cpu(struct smc_connection * conn)632 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
633 {
634 	struct smc_link_group *lgr = conn->lgr;
635 
636 	smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
637 			       conn->sndbuf_desc, DMA_TO_DEVICE);
638 }
639 
smc_sndbuf_sync_sg_for_device(struct smc_connection * conn)640 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
641 {
642 	struct smc_link_group *lgr = conn->lgr;
643 
644 	smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
645 				  conn->sndbuf_desc, DMA_TO_DEVICE);
646 }
647 
smc_rmb_sync_sg_for_cpu(struct smc_connection * conn)648 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
649 {
650 	struct smc_link_group *lgr = conn->lgr;
651 
652 	smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
653 			       conn->rmb_desc, DMA_FROM_DEVICE);
654 }
655 
smc_rmb_sync_sg_for_device(struct smc_connection * conn)656 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
657 {
658 	struct smc_link_group *lgr = conn->lgr;
659 
660 	smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
661 				  conn->rmb_desc, DMA_FROM_DEVICE);
662 }
663 
664 /* create the send and receive buffer for an SMC socket;
665  * receive buffers are called RMBs;
666  * (even though the SMC protocol allows more than one RMB-element per RMB,
667  * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
668  * extra RMB for every connection in a link group
669  */
smc_buf_create(struct smc_sock * smc)670 int smc_buf_create(struct smc_sock *smc)
671 {
672 	int rc;
673 
674 	/* create send buffer */
675 	rc = __smc_buf_create(smc, false);
676 	if (rc)
677 		return rc;
678 	/* create rmb */
679 	rc = __smc_buf_create(smc, true);
680 	if (rc)
681 		smc_buf_free(smc->conn.sndbuf_desc,
682 			     &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false);
683 	return rc;
684 }
685 
smc_rmb_reserve_rtoken_idx(struct smc_link_group * lgr)686 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
687 {
688 	int i;
689 
690 	for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
691 		if (!test_and_set_bit(i, lgr->rtokens_used_mask))
692 			return i;
693 	}
694 	return -ENOSPC;
695 }
696 
697 /* save rkey and dma_addr received from peer during clc handshake */
smc_rmb_rtoken_handling(struct smc_connection * conn,struct smc_clc_msg_accept_confirm * clc)698 int smc_rmb_rtoken_handling(struct smc_connection *conn,
699 			    struct smc_clc_msg_accept_confirm *clc)
700 {
701 	u64 dma_addr = be64_to_cpu(clc->rmb_dma_addr);
702 	struct smc_link_group *lgr = conn->lgr;
703 	u32 rkey = ntohl(clc->rmb_rkey);
704 	int i;
705 
706 	for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
707 		if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
708 		    (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
709 		    test_bit(i, lgr->rtokens_used_mask)) {
710 			conn->rtoken_idx = i;
711 			return 0;
712 		}
713 	}
714 	conn->rtoken_idx = smc_rmb_reserve_rtoken_idx(lgr);
715 	if (conn->rtoken_idx < 0)
716 		return conn->rtoken_idx;
717 	lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey = rkey;
718 	lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr = dma_addr;
719 	return 0;
720 }
721