blob: d4bd01bb44e1738dd7ea1a562b8f4be3295a324b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01002/*
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"
Ursula Braunf38ba1792017-01-09 16:55:19 +010024#include "smc_wr.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010025#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010026#include "smc_cdc.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010027#include "smc_close.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010028
Ursula Braun5bc11dd2017-09-21 09:16:31 +020029#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)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010032
Ursula Braun9bf9abe2017-01-09 16:55:21 +010033static u32 smc_lgr_num; /* unique link group number */
34
Karsten Graula6920d12018-05-03 17:57:38 +020035static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
36 bool is_rmb);
37
Karsten Graul97cdbc42018-03-14 11:01:02 +010038static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
39{
40 /* client link group creation always follows the server link group
41 * creation. For client use a somewhat higher removal delay time,
42 * otherwise there is a risk of out-of-sync link groups.
43 */
44 mod_delayed_work(system_wq, &lgr->free_work,
45 lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT :
46 SMC_LGR_FREE_DELAY_SERV);
47}
48
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010049/* Register connection's alert token in our lookup structure.
50 * To use rbtrees we have to implement our own insert core.
51 * Requires @conns_lock
52 * @smc connection to register
53 * Returns 0 on success, != otherwise.
54 */
55static void smc_lgr_add_alert_token(struct smc_connection *conn)
56{
57 struct rb_node **link, *parent = NULL;
58 u32 token = conn->alert_token_local;
59
60 link = &conn->lgr->conns_all.rb_node;
61 while (*link) {
62 struct smc_connection *cur = rb_entry(*link,
63 struct smc_connection, alert_node);
64
65 parent = *link;
66 if (cur->alert_token_local > token)
67 link = &parent->rb_left;
68 else
69 link = &parent->rb_right;
70 }
71 /* Put the new node there */
72 rb_link_node(&conn->alert_node, parent, link);
73 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
74}
75
76/* Register connection in link group by assigning an alert token
77 * registered in a search tree.
78 * Requires @conns_lock
79 * Note that '0' is a reserved value and not assigned.
80 */
81static void smc_lgr_register_conn(struct smc_connection *conn)
82{
83 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
84 static atomic_t nexttoken = ATOMIC_INIT(0);
85
86 /* find a new alert_token_local value not yet used by some connection
87 * in this link group
88 */
89 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
90 while (!conn->alert_token_local) {
91 conn->alert_token_local = atomic_inc_return(&nexttoken);
92 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
93 conn->alert_token_local = 0;
94 }
95 smc_lgr_add_alert_token(conn);
96 conn->lgr->conns_num++;
97}
98
99/* Unregister connection and reset the alert token of the given connection<
100 */
101static void __smc_lgr_unregister_conn(struct smc_connection *conn)
102{
103 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
104 struct smc_link_group *lgr = conn->lgr;
105
106 rb_erase(&conn->alert_node, &lgr->conns_all);
107 lgr->conns_num--;
108 conn->alert_token_local = 0;
109 conn->lgr = NULL;
110 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
111}
112
113/* Unregister connection and trigger lgr freeing if applicable
114 */
115static void smc_lgr_unregister_conn(struct smc_connection *conn)
116{
117 struct smc_link_group *lgr = conn->lgr;
118 int reduced = 0;
119
120 write_lock_bh(&lgr->conns_lock);
121 if (conn->alert_token_local) {
122 reduced = 1;
123 __smc_lgr_unregister_conn(conn);
124 }
125 write_unlock_bh(&lgr->conns_lock);
Ursula Braun5bc11dd2017-09-21 09:16:31 +0200126 if (!reduced || lgr->conns_num)
127 return;
Karsten Graul97cdbc42018-03-14 11:01:02 +0100128 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100129}
130
131static void smc_lgr_free_work(struct work_struct *work)
132{
133 struct smc_link_group *lgr = container_of(to_delayed_work(work),
134 struct smc_link_group,
135 free_work);
136 bool conns;
137
138 spin_lock_bh(&smc_lgr_list.lock);
Ursula Braun610db662018-01-25 11:15:34 +0100139 if (list_empty(&lgr->list))
140 goto free;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100141 read_lock_bh(&lgr->conns_lock);
142 conns = RB_EMPTY_ROOT(&lgr->conns_all);
143 read_unlock_bh(&lgr->conns_lock);
144 if (!conns) { /* number of lgr connections is no longer zero */
145 spin_unlock_bh(&smc_lgr_list.lock);
146 return;
147 }
148 list_del_init(&lgr->list); /* remove from smc_lgr_list */
Ursula Braun610db662018-01-25 11:15:34 +0100149free:
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100150 spin_unlock_bh(&smc_lgr_list.lock);
Ursula Braun268ffcc2018-03-14 11:01:01 +0100151 if (!delayed_work_pending(&lgr->free_work))
152 smc_lgr_free(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100153}
154
155/* create a new SMC link group */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100156static int smc_lgr_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100157 struct smc_ib_device *smcibdev, u8 ibport,
158 char *peer_systemid, unsigned short vlan_id)
159{
160 struct smc_link_group *lgr;
161 struct smc_link *lnk;
162 u8 rndvec[3];
163 int rc = 0;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100164 int i;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100165
166 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
167 if (!lgr) {
168 rc = -ENOMEM;
169 goto out;
170 }
171 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
172 lgr->sync_err = false;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100173 memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN);
174 lgr->vlan_id = vlan_id;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100175 rwlock_init(&lgr->sndbufs_lock);
176 rwlock_init(&lgr->rmbs_lock);
177 for (i = 0; i < SMC_RMBE_SIZES; i++) {
178 INIT_LIST_HEAD(&lgr->sndbufs[i]);
179 INIT_LIST_HEAD(&lgr->rmbs[i]);
180 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100181 smc_lgr_num += SMC_LGR_NUM_INCR;
182 memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100183 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
184 lgr->conns_all = RB_ROOT;
185
186 lnk = &lgr->lnk[SMC_SINGLE_LINK];
187 /* initialize link */
Karsten Graul52bedf32018-03-01 13:51:32 +0100188 lnk->state = SMC_LNK_ACTIVATING;
Karsten Graul2be922f2018-02-28 12:44:08 +0100189 lnk->link_id = SMC_SINGLE_LINK;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100190 lnk->smcibdev = smcibdev;
191 lnk->ibport = ibport;
192 lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100193 if (!smcibdev->initialized)
194 smc_ib_setup_per_ibdev(smcibdev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100195 get_random_bytes(rndvec, sizeof(rndvec));
196 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100197 rc = smc_wr_alloc_link_mem(lnk);
198 if (rc)
199 goto free_lgr;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100200 rc = smc_ib_create_protection_domain(lnk);
201 if (rc)
202 goto free_link_mem;
203 rc = smc_ib_create_queue_pair(lnk);
204 if (rc)
205 goto dealloc_pd;
206 rc = smc_wr_create_link(lnk);
207 if (rc)
208 goto destroy_qp;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100209 init_completion(&lnk->llc_confirm);
210 init_completion(&lnk->llc_confirm_resp);
Karsten Graul52bedf32018-03-01 13:51:32 +0100211 init_completion(&lnk->llc_add);
212 init_completion(&lnk->llc_add_resp);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100213
214 smc->conn.lgr = lgr;
215 rwlock_init(&lgr->conns_lock);
216 spin_lock_bh(&smc_lgr_list.lock);
217 list_add(&lgr->list, &smc_lgr_list.list);
218 spin_unlock_bh(&smc_lgr_list.lock);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100219 return 0;
220
Ursula Braunbd4ad572017-01-09 16:55:20 +0100221destroy_qp:
222 smc_ib_destroy_queue_pair(lnk);
223dealloc_pd:
224 smc_ib_dealloc_protection_domain(lnk);
225free_link_mem:
226 smc_wr_free_link_mem(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100227free_lgr:
228 kfree(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100229out:
230 return rc;
231}
232
Ursula Braun3e034722017-07-28 13:56:20 +0200233static void smc_buf_unuse(struct smc_connection *conn)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100234{
235 if (conn->sndbuf_desc) {
236 conn->sndbuf_desc->used = 0;
237 conn->sndbuf_size = 0;
238 }
Ursula Brauncd6851f2017-01-09 16:55:18 +0100239 if (conn->rmb_desc) {
Karsten Graula6920d12018-05-03 17:57:38 +0200240 if (!conn->rmb_desc->regerr) {
241 conn->rmb_desc->reused = 1;
242 conn->rmb_desc->used = 0;
243 conn->rmbe_size = 0;
244 } else {
245 /* buf registration failed, reuse not possible */
246 struct smc_link_group *lgr = conn->lgr;
247 struct smc_link *lnk;
248
249 write_lock_bh(&lgr->rmbs_lock);
250 list_del(&conn->rmb_desc->list);
251 write_unlock_bh(&lgr->rmbs_lock);
252
253 lnk = &lgr->lnk[SMC_SINGLE_LINK];
254 smc_buf_free(conn->rmb_desc, lnk, true);
255 }
Ursula Brauncd6851f2017-01-09 16:55:18 +0100256 }
257}
258
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100259/* remove a finished connection from its link group */
260void smc_conn_free(struct smc_connection *conn)
261{
Ursula Braun732720f2018-01-25 11:15:35 +0100262 if (!conn->lgr)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100263 return;
Ursula Braun5f083182017-01-09 16:55:22 +0100264 smc_cdc_tx_dismiss_slots(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100265 smc_lgr_unregister_conn(conn);
Ursula Braun3e034722017-07-28 13:56:20 +0200266 smc_buf_unuse(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100267}
268
269static void smc_link_clear(struct smc_link *lnk)
270{
271 lnk->peer_qpn = 0;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100272 smc_ib_modify_qp_reset(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100273 smc_wr_free_link(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100274 smc_ib_destroy_queue_pair(lnk);
275 smc_ib_dealloc_protection_domain(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100276 smc_wr_free_link_mem(lnk);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100277}
278
Ursula Braun3e034722017-07-28 13:56:20 +0200279static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
280 bool is_rmb)
281{
282 if (is_rmb) {
283 if (buf_desc->mr_rx[SMC_SINGLE_LINK])
284 smc_ib_put_memory_region(
285 buf_desc->mr_rx[SMC_SINGLE_LINK]);
286 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
287 DMA_FROM_DEVICE);
288 } else {
289 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
290 DMA_TO_DEVICE);
291 }
292 sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]);
293 if (buf_desc->cpu_addr)
294 free_pages((unsigned long)buf_desc->cpu_addr, buf_desc->order);
295 kfree(buf_desc);
296}
297
298static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100299{
Ursula Braun9d8fb612017-07-28 13:56:19 +0200300 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun3e034722017-07-28 13:56:20 +0200301 struct smc_buf_desc *buf_desc, *bf_desc;
302 struct list_head *buf_list;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100303 int i;
304
305 for (i = 0; i < SMC_RMBE_SIZES; i++) {
Ursula Braun3e034722017-07-28 13:56:20 +0200306 if (is_rmb)
307 buf_list = &lgr->rmbs[i];
308 else
309 buf_list = &lgr->sndbufs[i];
310 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
Ursula Brauncd6851f2017-01-09 16:55:18 +0100311 list) {
Ursula Braun3e034722017-07-28 13:56:20 +0200312 list_del(&buf_desc->list);
313 smc_buf_free(buf_desc, lnk, is_rmb);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100314 }
315 }
316}
317
Ursula Braun3e034722017-07-28 13:56:20 +0200318static void smc_lgr_free_bufs(struct smc_link_group *lgr)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100319{
Ursula Braun3e034722017-07-28 13:56:20 +0200320 /* free send buffers */
321 __smc_lgr_free_bufs(lgr, false);
322 /* free rmbs */
323 __smc_lgr_free_bufs(lgr, true);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100324}
325
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100326/* remove a link group */
327void smc_lgr_free(struct smc_link_group *lgr)
328{
Ursula Braun3e034722017-07-28 13:56:20 +0200329 smc_lgr_free_bufs(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100330 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
331 kfree(lgr);
332}
333
Karsten Graul9651b932018-03-01 13:51:33 +0100334void smc_lgr_forget(struct smc_link_group *lgr)
335{
336 spin_lock_bh(&smc_lgr_list.lock);
337 /* do not use this link group for new connections */
338 if (!list_empty(&lgr->list))
339 list_del_init(&lgr->list);
340 spin_unlock_bh(&smc_lgr_list.lock);
341}
342
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100343/* terminate linkgroup abnormally */
344void smc_lgr_terminate(struct smc_link_group *lgr)
345{
346 struct smc_connection *conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100347 struct smc_sock *smc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100348 struct rb_node *node;
349
Karsten Graul9651b932018-03-01 13:51:33 +0100350 smc_lgr_forget(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100351
352 write_lock_bh(&lgr->conns_lock);
353 node = rb_first(&lgr->conns_all);
354 while (node) {
355 conn = rb_entry(node, struct smc_connection, alert_node);
Ursula Braunb38d7322017-01-09 16:55:25 +0100356 smc = container_of(conn, struct smc_sock, conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100357 sock_hold(&smc->sk); /* sock_put in close work */
Ursula Braun732720f2018-01-25 11:15:35 +0100358 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100359 __smc_lgr_unregister_conn(conn);
Ursula Braun732720f2018-01-25 11:15:35 +0100360 write_unlock_bh(&lgr->conns_lock);
Ursula Braun51f1de72018-01-26 09:28:48 +0100361 if (!schedule_work(&conn->close_work))
362 sock_put(&smc->sk);
Ursula Braun732720f2018-01-25 11:15:35 +0100363 write_lock_bh(&lgr->conns_lock);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100364 node = rb_first(&lgr->conns_all);
365 }
366 write_unlock_bh(&lgr->conns_lock);
Ursula Braun732720f2018-01-25 11:15:35 +0100367 wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait);
Karsten Graul97cdbc42018-03-14 11:01:02 +0100368 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100369}
370
371/* Determine vlan of internal TCP socket.
372 * @vlan_id: address to store the determined vlan id into
373 */
374static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
375{
376 struct dst_entry *dst = sk_dst_get(clcsock->sk);
377 int rc = 0;
378
379 *vlan_id = 0;
380 if (!dst) {
381 rc = -ENOTCONN;
382 goto out;
383 }
384 if (!dst->dev) {
385 rc = -ENODEV;
386 goto out_rel;
387 }
388
389 if (is_vlan_dev(dst->dev))
390 *vlan_id = vlan_dev_vlan_id(dst->dev);
391
392out_rel:
393 dst_release(dst);
394out:
395 return rc;
396}
397
398/* determine the link gid matching the vlan id of the link group */
399static int smc_link_determine_gid(struct smc_link_group *lgr)
400{
401 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
402 struct ib_gid_attr gattr;
403 union ib_gid gid;
404 int i;
405
406 if (!lgr->vlan_id) {
407 lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1];
408 return 0;
409 }
410
411 for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len;
412 i++) {
413 if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid,
414 &gattr))
415 continue;
Ursula Braun43e2ada2017-10-11 13:47:23 +0200416 if (gattr.ndev) {
417 if (is_vlan_dev(gattr.ndev) &&
418 vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) {
419 lnk->gid = gid;
420 dev_put(gattr.ndev);
421 return 0;
422 }
423 dev_put(gattr.ndev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100424 }
425 }
426 return -ENODEV;
427}
428
429/* create a new SMC connection (and a new link group if necessary) */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100430int smc_conn_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100431 struct smc_ib_device *smcibdev, u8 ibport,
432 struct smc_clc_msg_local *lcl, int srv_first_contact)
433{
434 struct smc_connection *conn = &smc->conn;
435 struct smc_link_group *lgr;
436 unsigned short vlan_id;
437 enum smc_lgr_role role;
438 int local_contact = SMC_FIRST_CONTACT;
439 int rc = 0;
440
441 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
442 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
443 if (rc)
444 return rc;
445
446 if ((role == SMC_CLNT) && srv_first_contact)
447 /* create new link group as well */
448 goto create;
449
450 /* determine if an existing link group can be reused */
451 spin_lock_bh(&smc_lgr_list.lock);
452 list_for_each_entry(lgr, &smc_lgr_list.list, list) {
453 write_lock_bh(&lgr->conns_lock);
454 if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
455 SMC_SYSTEMID_LEN) &&
456 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
457 SMC_GID_SIZE) &&
458 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
459 sizeof(lcl->mac)) &&
460 !lgr->sync_err &&
461 (lgr->role == role) &&
Ursula Brauncd6851f2017-01-09 16:55:18 +0100462 (lgr->vlan_id == vlan_id) &&
463 ((role == SMC_CLNT) ||
464 (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100465 /* link group found */
466 local_contact = SMC_REUSE_CONTACT;
467 conn->lgr = lgr;
468 smc_lgr_register_conn(conn); /* add smc conn to lgr */
469 write_unlock_bh(&lgr->conns_lock);
470 break;
471 }
472 write_unlock_bh(&lgr->conns_lock);
473 }
474 spin_unlock_bh(&smc_lgr_list.lock);
475
476 if (role == SMC_CLNT && !srv_first_contact &&
477 (local_contact == SMC_FIRST_CONTACT)) {
478 /* Server reuses a link group, but Client wants to start
479 * a new one
480 * send out_of_sync decline, reason synchr. error
481 */
482 return -ENOLINK;
483 }
484
485create:
486 if (local_contact == SMC_FIRST_CONTACT) {
Karsten Graulbe6d4672018-03-01 13:51:28 +0100487 rc = smc_lgr_create(smc, smcibdev, ibport,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100488 lcl->id_for_peer, vlan_id);
489 if (rc)
490 goto out;
491 smc_lgr_register_conn(conn); /* add smc conn to lgr */
492 rc = smc_link_determine_gid(conn->lgr);
493 }
Ursula Braun5f083182017-01-09 16:55:22 +0100494 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
Karsten Graulcbba07a2018-02-28 12:44:07 +0100495 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
Ursula Braun5f083182017-01-09 16:55:22 +0100496#ifndef KERNEL_HAS_ATOMIC64
497 spin_lock_init(&conn->acurs_lock);
498#endif
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100499
500out:
501 return rc ? rc : local_contact;
502}
Ursula Brauncd6851f2017-01-09 16:55:18 +0100503
Ursula Braun3e034722017-07-28 13:56:20 +0200504/* try to reuse a sndbuf or rmb description slot for a certain
505 * buffer size; if not available, return NULL
Ursula Brauncd6851f2017-01-09 16:55:18 +0100506 */
507static inline
Ursula Braun3e034722017-07-28 13:56:20 +0200508struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr,
509 int compressed_bufsize,
510 rwlock_t *lock,
511 struct list_head *buf_list)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100512{
Ursula Braun3e034722017-07-28 13:56:20 +0200513 struct smc_buf_desc *buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100514
Ursula Braun3e034722017-07-28 13:56:20 +0200515 read_lock_bh(lock);
516 list_for_each_entry(buf_slot, buf_list, list) {
517 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
518 read_unlock_bh(lock);
519 return buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100520 }
521 }
Ursula Braun3e034722017-07-28 13:56:20 +0200522 read_unlock_bh(lock);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100523 return NULL;
524}
525
Ursula Braun952310c2017-01-09 16:55:24 +0100526/* one of the conditions for announcing a receiver's current window size is
527 * that it "results in a minimum increase in the window size of 10% of the
528 * receive buffer space" [RFC7609]
529 */
530static inline int smc_rmb_wnd_update_limit(int rmbe_size)
531{
532 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
533}
534
Ursula Braunb33982c2017-07-28 13:56:21 +0200535static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
536 bool is_rmb, int bufsize)
537{
538 struct smc_buf_desc *buf_desc;
539 struct smc_link *lnk;
540 int rc;
541
542 /* try to alloc a new buffer */
543 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
544 if (!buf_desc)
545 return ERR_PTR(-ENOMEM);
546
547 buf_desc->cpu_addr =
548 (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN |
549 __GFP_NOMEMALLOC |
550 __GFP_NORETRY | __GFP_ZERO,
551 get_order(bufsize));
552 if (!buf_desc->cpu_addr) {
553 kfree(buf_desc);
554 return ERR_PTR(-EAGAIN);
555 }
556 buf_desc->order = get_order(bufsize);
557
558 /* build the sg table from the pages */
559 lnk = &lgr->lnk[SMC_SINGLE_LINK];
560 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
561 GFP_KERNEL);
562 if (rc) {
563 smc_buf_free(buf_desc, lnk, is_rmb);
564 return ERR_PTR(rc);
565 }
566 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
567 buf_desc->cpu_addr, bufsize);
568
569 /* map sg table to DMA address */
570 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
571 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
572 /* SMC protocol depends on mapping to one DMA address only */
573 if (rc != 1) {
574 smc_buf_free(buf_desc, lnk, is_rmb);
575 return ERR_PTR(-EAGAIN);
576 }
577
578 /* create a new memory region for the RMB */
579 if (is_rmb) {
580 rc = smc_ib_get_memory_region(lnk->roce_pd,
581 IB_ACCESS_REMOTE_WRITE |
582 IB_ACCESS_LOCAL_WRITE,
583 buf_desc);
584 if (rc) {
585 smc_buf_free(buf_desc, lnk, is_rmb);
586 return ERR_PTR(rc);
587 }
588 }
589
590 return buf_desc;
591}
592
Ursula Braun3e034722017-07-28 13:56:20 +0200593static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100594{
595 struct smc_connection *conn = &smc->conn;
596 struct smc_link_group *lgr = conn->lgr;
Geert Uytterhoeven68870372017-11-21 13:23:54 +0100597 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
Ursula Braun3e034722017-07-28 13:56:20 +0200598 struct list_head *buf_list;
Ursula Braunc45abf32017-07-28 13:56:14 +0200599 int bufsize, bufsize_short;
Ursula Braun3e034722017-07-28 13:56:20 +0200600 int sk_buf_size;
601 rwlock_t *lock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100602
Ursula Braun3e034722017-07-28 13:56:20 +0200603 if (is_rmb)
604 /* use socket recv buffer size (w/o overhead) as start value */
605 sk_buf_size = smc->sk.sk_rcvbuf / 2;
606 else
607 /* use socket send buffer size (w/o overhead) as start value */
608 sk_buf_size = smc->sk.sk_sndbuf / 2;
609
Ursula Braun4e1061f2017-11-21 13:23:53 +0100610 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
Ursula Braunc45abf32017-07-28 13:56:14 +0200611 bufsize_short >= 0; bufsize_short--) {
Ursula Braun3e034722017-07-28 13:56:20 +0200612
613 if (is_rmb) {
614 lock = &lgr->rmbs_lock;
615 buf_list = &lgr->rmbs[bufsize_short];
616 } else {
617 lock = &lgr->sndbufs_lock;
618 buf_list = &lgr->sndbufs[bufsize_short];
619 }
Ursula Braunc45abf32017-07-28 13:56:14 +0200620 bufsize = smc_uncompress_bufsize(bufsize_short);
Ursula Braun9d8fb612017-07-28 13:56:19 +0200621 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
622 continue;
623
Ursula Braun3e034722017-07-28 13:56:20 +0200624 /* check for reusable slot in the link group */
625 buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list);
626 if (buf_desc) {
627 memset(buf_desc->cpu_addr, 0, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100628 break; /* found reusable slot */
629 }
Ursula Braun3e034722017-07-28 13:56:20 +0200630
Ursula Braunb33982c2017-07-28 13:56:21 +0200631 buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
632 if (PTR_ERR(buf_desc) == -ENOMEM)
633 break;
634 if (IS_ERR(buf_desc))
Ursula Brauncd6851f2017-01-09 16:55:18 +0100635 continue;
Ursula Braun3e034722017-07-28 13:56:20 +0200636
637 buf_desc->used = 1;
638 write_lock_bh(lock);
639 list_add(&buf_desc->list, buf_list);
640 write_unlock_bh(lock);
641 break; /* found */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100642 }
Ursula Braun3e034722017-07-28 13:56:20 +0200643
Ursula Braunb33982c2017-07-28 13:56:21 +0200644 if (IS_ERR(buf_desc))
Ursula Braun3e034722017-07-28 13:56:20 +0200645 return -ENOMEM;
646
647 if (is_rmb) {
648 conn->rmb_desc = buf_desc;
Ursula Braunc45abf32017-07-28 13:56:14 +0200649 conn->rmbe_size = bufsize;
650 conn->rmbe_size_short = bufsize_short;
651 smc->sk.sk_rcvbuf = bufsize * 2;
Ursula Braun5f083182017-01-09 16:55:22 +0100652 atomic_set(&conn->bytes_to_rcv, 0);
Ursula Braunc45abf32017-07-28 13:56:14 +0200653 conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100654 } else {
Ursula Braun3e034722017-07-28 13:56:20 +0200655 conn->sndbuf_desc = buf_desc;
656 conn->sndbuf_size = bufsize;
657 smc->sk.sk_sndbuf = bufsize * 2;
658 atomic_set(&conn->sndbuf_space, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100659 }
Ursula Braun3e034722017-07-28 13:56:20 +0200660 return 0;
661}
662
Ursula Braun10428dd2017-07-28 13:56:22 +0200663void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
664{
665 struct smc_link_group *lgr = conn->lgr;
666
667 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
668 conn->sndbuf_desc, DMA_TO_DEVICE);
669}
670
671void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
672{
673 struct smc_link_group *lgr = conn->lgr;
674
675 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
676 conn->sndbuf_desc, DMA_TO_DEVICE);
677}
678
679void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
680{
681 struct smc_link_group *lgr = conn->lgr;
682
683 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
684 conn->rmb_desc, DMA_FROM_DEVICE);
685}
686
687void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
688{
689 struct smc_link_group *lgr = conn->lgr;
690
691 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
692 conn->rmb_desc, DMA_FROM_DEVICE);
693}
694
Ursula Braun3e034722017-07-28 13:56:20 +0200695/* create the send and receive buffer for an SMC socket;
696 * receive buffers are called RMBs;
697 * (even though the SMC protocol allows more than one RMB-element per RMB,
698 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
699 * extra RMB for every connection in a link group
700 */
701int smc_buf_create(struct smc_sock *smc)
702{
703 int rc;
704
705 /* create send buffer */
706 rc = __smc_buf_create(smc, false);
707 if (rc)
708 return rc;
709 /* create rmb */
710 rc = __smc_buf_create(smc, true);
711 if (rc)
712 smc_buf_free(smc->conn.sndbuf_desc,
713 &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false);
714 return rc;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100715}
Ursula Braunbd4ad572017-01-09 16:55:20 +0100716
717static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
718{
719 int i;
720
721 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
722 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
723 return i;
724 }
725 return -ENOSPC;
726}
727
Karsten Graul4ed75de2018-03-01 13:51:30 +0100728/* add a new rtoken from peer */
729int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey)
Ursula Braunbd4ad572017-01-09 16:55:20 +0100730{
Karsten Graul4ed75de2018-03-01 13:51:30 +0100731 u64 dma_addr = be64_to_cpu(nw_vaddr);
732 u32 rkey = ntohl(nw_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100733 int i;
734
735 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
736 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
Ursula Braun263eec92017-05-15 17:33:37 +0200737 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
Ursula Braunbd4ad572017-01-09 16:55:20 +0100738 test_bit(i, lgr->rtokens_used_mask)) {
Karsten Graul4ed75de2018-03-01 13:51:30 +0100739 /* already in list */
740 return i;
741 }
742 }
743 i = smc_rmb_reserve_rtoken_idx(lgr);
744 if (i < 0)
745 return i;
746 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey;
747 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr;
748 return i;
749}
750
751/* delete an rtoken */
752int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey)
753{
754 u32 rkey = ntohl(nw_rkey);
755 int i;
756
757 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
758 if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey &&
759 test_bit(i, lgr->rtokens_used_mask)) {
760 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0;
761 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0;
762
763 clear_bit(i, lgr->rtokens_used_mask);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100764 return 0;
765 }
766 }
Karsten Graul4ed75de2018-03-01 13:51:30 +0100767 return -ENOENT;
768}
769
770/* save rkey and dma_addr received from peer during clc handshake */
771int smc_rmb_rtoken_handling(struct smc_connection *conn,
772 struct smc_clc_msg_accept_confirm *clc)
773{
774 conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr,
775 clc->rmb_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100776 if (conn->rtoken_idx < 0)
777 return conn->rtoken_idx;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100778 return 0;
779}