blob: d9247765aff328a9bfeb9038569dbe00a2304bad [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 Graul97cdbc42018-03-14 11:01:02 +010035static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
36{
37 /* client link group creation always follows the server link group
38 * creation. For client use a somewhat higher removal delay time,
39 * otherwise there is a risk of out-of-sync link groups.
40 */
41 mod_delayed_work(system_wq, &lgr->free_work,
42 lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT :
43 SMC_LGR_FREE_DELAY_SERV);
44}
45
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010046/* Register connection's alert token in our lookup structure.
47 * To use rbtrees we have to implement our own insert core.
48 * Requires @conns_lock
49 * @smc connection to register
50 * Returns 0 on success, != otherwise.
51 */
52static void smc_lgr_add_alert_token(struct smc_connection *conn)
53{
54 struct rb_node **link, *parent = NULL;
55 u32 token = conn->alert_token_local;
56
57 link = &conn->lgr->conns_all.rb_node;
58 while (*link) {
59 struct smc_connection *cur = rb_entry(*link,
60 struct smc_connection, alert_node);
61
62 parent = *link;
63 if (cur->alert_token_local > token)
64 link = &parent->rb_left;
65 else
66 link = &parent->rb_right;
67 }
68 /* Put the new node there */
69 rb_link_node(&conn->alert_node, parent, link);
70 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
71}
72
73/* Register connection in link group by assigning an alert token
74 * registered in a search tree.
75 * Requires @conns_lock
76 * Note that '0' is a reserved value and not assigned.
77 */
78static void smc_lgr_register_conn(struct smc_connection *conn)
79{
80 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
81 static atomic_t nexttoken = ATOMIC_INIT(0);
82
83 /* find a new alert_token_local value not yet used by some connection
84 * in this link group
85 */
86 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
87 while (!conn->alert_token_local) {
88 conn->alert_token_local = atomic_inc_return(&nexttoken);
89 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
90 conn->alert_token_local = 0;
91 }
92 smc_lgr_add_alert_token(conn);
93 conn->lgr->conns_num++;
94}
95
96/* Unregister connection and reset the alert token of the given connection<
97 */
98static void __smc_lgr_unregister_conn(struct smc_connection *conn)
99{
100 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
101 struct smc_link_group *lgr = conn->lgr;
102
103 rb_erase(&conn->alert_node, &lgr->conns_all);
104 lgr->conns_num--;
105 conn->alert_token_local = 0;
106 conn->lgr = NULL;
107 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
108}
109
110/* Unregister connection and trigger lgr freeing if applicable
111 */
112static void smc_lgr_unregister_conn(struct smc_connection *conn)
113{
114 struct smc_link_group *lgr = conn->lgr;
115 int reduced = 0;
116
117 write_lock_bh(&lgr->conns_lock);
118 if (conn->alert_token_local) {
119 reduced = 1;
120 __smc_lgr_unregister_conn(conn);
121 }
122 write_unlock_bh(&lgr->conns_lock);
Ursula Braun5bc11dd2017-09-21 09:16:31 +0200123 if (!reduced || lgr->conns_num)
124 return;
Karsten Graul97cdbc42018-03-14 11:01:02 +0100125 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100126}
127
128static void smc_lgr_free_work(struct work_struct *work)
129{
130 struct smc_link_group *lgr = container_of(to_delayed_work(work),
131 struct smc_link_group,
132 free_work);
133 bool conns;
134
135 spin_lock_bh(&smc_lgr_list.lock);
Ursula Braun610db662018-01-25 11:15:34 +0100136 if (list_empty(&lgr->list))
137 goto free;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100138 read_lock_bh(&lgr->conns_lock);
139 conns = RB_EMPTY_ROOT(&lgr->conns_all);
140 read_unlock_bh(&lgr->conns_lock);
141 if (!conns) { /* number of lgr connections is no longer zero */
142 spin_unlock_bh(&smc_lgr_list.lock);
143 return;
144 }
145 list_del_init(&lgr->list); /* remove from smc_lgr_list */
Ursula Braun610db662018-01-25 11:15:34 +0100146free:
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100147 spin_unlock_bh(&smc_lgr_list.lock);
Ursula Braun268ffcc2018-03-14 11:01:01 +0100148 if (!delayed_work_pending(&lgr->free_work))
149 smc_lgr_free(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100150}
151
152/* create a new SMC link group */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100153static int smc_lgr_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100154 struct smc_ib_device *smcibdev, u8 ibport,
155 char *peer_systemid, unsigned short vlan_id)
156{
157 struct smc_link_group *lgr;
158 struct smc_link *lnk;
159 u8 rndvec[3];
160 int rc = 0;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100161 int i;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100162
163 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
164 if (!lgr) {
165 rc = -ENOMEM;
166 goto out;
167 }
168 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
169 lgr->sync_err = false;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100170 memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN);
171 lgr->vlan_id = vlan_id;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100172 rwlock_init(&lgr->sndbufs_lock);
173 rwlock_init(&lgr->rmbs_lock);
174 for (i = 0; i < SMC_RMBE_SIZES; i++) {
175 INIT_LIST_HEAD(&lgr->sndbufs[i]);
176 INIT_LIST_HEAD(&lgr->rmbs[i]);
177 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100178 smc_lgr_num += SMC_LGR_NUM_INCR;
179 memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100180 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
181 lgr->conns_all = RB_ROOT;
182
183 lnk = &lgr->lnk[SMC_SINGLE_LINK];
184 /* initialize link */
Karsten Graul52bedf32018-03-01 13:51:32 +0100185 lnk->state = SMC_LNK_ACTIVATING;
Karsten Graul2be922f2018-02-28 12:44:08 +0100186 lnk->link_id = SMC_SINGLE_LINK;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100187 lnk->smcibdev = smcibdev;
188 lnk->ibport = ibport;
189 lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100190 if (!smcibdev->initialized)
191 smc_ib_setup_per_ibdev(smcibdev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100192 get_random_bytes(rndvec, sizeof(rndvec));
193 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100194 rc = smc_wr_alloc_link_mem(lnk);
195 if (rc)
196 goto free_lgr;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100197 rc = smc_ib_create_protection_domain(lnk);
198 if (rc)
199 goto free_link_mem;
200 rc = smc_ib_create_queue_pair(lnk);
201 if (rc)
202 goto dealloc_pd;
203 rc = smc_wr_create_link(lnk);
204 if (rc)
205 goto destroy_qp;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100206 init_completion(&lnk->llc_confirm);
207 init_completion(&lnk->llc_confirm_resp);
Karsten Graul52bedf32018-03-01 13:51:32 +0100208 init_completion(&lnk->llc_add);
209 init_completion(&lnk->llc_add_resp);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100210
211 smc->conn.lgr = lgr;
212 rwlock_init(&lgr->conns_lock);
213 spin_lock_bh(&smc_lgr_list.lock);
214 list_add(&lgr->list, &smc_lgr_list.list);
215 spin_unlock_bh(&smc_lgr_list.lock);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100216 return 0;
217
Ursula Braunbd4ad572017-01-09 16:55:20 +0100218destroy_qp:
219 smc_ib_destroy_queue_pair(lnk);
220dealloc_pd:
221 smc_ib_dealloc_protection_domain(lnk);
222free_link_mem:
223 smc_wr_free_link_mem(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100224free_lgr:
225 kfree(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100226out:
227 return rc;
228}
229
Ursula Braun3e034722017-07-28 13:56:20 +0200230static void smc_buf_unuse(struct smc_connection *conn)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100231{
232 if (conn->sndbuf_desc) {
233 conn->sndbuf_desc->used = 0;
234 conn->sndbuf_size = 0;
235 }
Ursula Brauncd6851f2017-01-09 16:55:18 +0100236 if (conn->rmb_desc) {
Ursula Braun897e1c22017-07-28 13:56:16 +0200237 conn->rmb_desc->reused = true;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100238 conn->rmb_desc->used = 0;
239 conn->rmbe_size = 0;
240 }
241}
242
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100243/* remove a finished connection from its link group */
244void smc_conn_free(struct smc_connection *conn)
245{
Ursula Braun732720f2018-01-25 11:15:35 +0100246 if (!conn->lgr)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100247 return;
Ursula Braun5f083182017-01-09 16:55:22 +0100248 smc_cdc_tx_dismiss_slots(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100249 smc_lgr_unregister_conn(conn);
Ursula Braun3e034722017-07-28 13:56:20 +0200250 smc_buf_unuse(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100251}
252
253static void smc_link_clear(struct smc_link *lnk)
254{
255 lnk->peer_qpn = 0;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100256 smc_ib_modify_qp_reset(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100257 smc_wr_free_link(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100258 smc_ib_destroy_queue_pair(lnk);
259 smc_ib_dealloc_protection_domain(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100260 smc_wr_free_link_mem(lnk);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100261}
262
Ursula Braun3e034722017-07-28 13:56:20 +0200263static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
264 bool is_rmb)
265{
266 if (is_rmb) {
267 if (buf_desc->mr_rx[SMC_SINGLE_LINK])
268 smc_ib_put_memory_region(
269 buf_desc->mr_rx[SMC_SINGLE_LINK]);
270 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
271 DMA_FROM_DEVICE);
272 } else {
273 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
274 DMA_TO_DEVICE);
275 }
276 sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]);
277 if (buf_desc->cpu_addr)
278 free_pages((unsigned long)buf_desc->cpu_addr, buf_desc->order);
279 kfree(buf_desc);
280}
281
282static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100283{
Ursula Braun9d8fb612017-07-28 13:56:19 +0200284 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun3e034722017-07-28 13:56:20 +0200285 struct smc_buf_desc *buf_desc, *bf_desc;
286 struct list_head *buf_list;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100287 int i;
288
289 for (i = 0; i < SMC_RMBE_SIZES; i++) {
Ursula Braun3e034722017-07-28 13:56:20 +0200290 if (is_rmb)
291 buf_list = &lgr->rmbs[i];
292 else
293 buf_list = &lgr->sndbufs[i];
294 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
Ursula Brauncd6851f2017-01-09 16:55:18 +0100295 list) {
Ursula Braun3e034722017-07-28 13:56:20 +0200296 list_del(&buf_desc->list);
297 smc_buf_free(buf_desc, lnk, is_rmb);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100298 }
299 }
300}
301
Ursula Braun3e034722017-07-28 13:56:20 +0200302static void smc_lgr_free_bufs(struct smc_link_group *lgr)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100303{
Ursula Braun3e034722017-07-28 13:56:20 +0200304 /* free send buffers */
305 __smc_lgr_free_bufs(lgr, false);
306 /* free rmbs */
307 __smc_lgr_free_bufs(lgr, true);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100308}
309
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100310/* remove a link group */
311void smc_lgr_free(struct smc_link_group *lgr)
312{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200313 smc_llc_link_flush(&lgr->lnk[SMC_SINGLE_LINK]);
Ursula Braun3e034722017-07-28 13:56:20 +0200314 smc_lgr_free_bufs(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100315 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
316 kfree(lgr);
317}
318
Karsten Graul9651b932018-03-01 13:51:33 +0100319void smc_lgr_forget(struct smc_link_group *lgr)
320{
321 spin_lock_bh(&smc_lgr_list.lock);
322 /* do not use this link group for new connections */
323 if (!list_empty(&lgr->list))
324 list_del_init(&lgr->list);
325 spin_unlock_bh(&smc_lgr_list.lock);
326}
327
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100328/* terminate linkgroup abnormally */
329void smc_lgr_terminate(struct smc_link_group *lgr)
330{
331 struct smc_connection *conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100332 struct smc_sock *smc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100333 struct rb_node *node;
334
Karsten Graul9651b932018-03-01 13:51:33 +0100335 smc_lgr_forget(lgr);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200336 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100337
338 write_lock_bh(&lgr->conns_lock);
339 node = rb_first(&lgr->conns_all);
340 while (node) {
341 conn = rb_entry(node, struct smc_connection, alert_node);
Ursula Braunb38d7322017-01-09 16:55:25 +0100342 smc = container_of(conn, struct smc_sock, conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100343 sock_hold(&smc->sk); /* sock_put in close work */
Ursula Braun732720f2018-01-25 11:15:35 +0100344 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100345 __smc_lgr_unregister_conn(conn);
Ursula Braun732720f2018-01-25 11:15:35 +0100346 write_unlock_bh(&lgr->conns_lock);
Ursula Braun51f1de72018-01-26 09:28:48 +0100347 if (!schedule_work(&conn->close_work))
348 sock_put(&smc->sk);
Ursula Braun732720f2018-01-25 11:15:35 +0100349 write_lock_bh(&lgr->conns_lock);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100350 node = rb_first(&lgr->conns_all);
351 }
352 write_unlock_bh(&lgr->conns_lock);
Ursula Braun732720f2018-01-25 11:15:35 +0100353 wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait);
Karsten Graul97cdbc42018-03-14 11:01:02 +0100354 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100355}
356
357/* Determine vlan of internal TCP socket.
358 * @vlan_id: address to store the determined vlan id into
359 */
360static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
361{
362 struct dst_entry *dst = sk_dst_get(clcsock->sk);
363 int rc = 0;
364
365 *vlan_id = 0;
366 if (!dst) {
367 rc = -ENOTCONN;
368 goto out;
369 }
370 if (!dst->dev) {
371 rc = -ENODEV;
372 goto out_rel;
373 }
374
375 if (is_vlan_dev(dst->dev))
376 *vlan_id = vlan_dev_vlan_id(dst->dev);
377
378out_rel:
379 dst_release(dst);
380out:
381 return rc;
382}
383
384/* determine the link gid matching the vlan id of the link group */
385static int smc_link_determine_gid(struct smc_link_group *lgr)
386{
387 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
388 struct ib_gid_attr gattr;
389 union ib_gid gid;
390 int i;
391
392 if (!lgr->vlan_id) {
393 lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1];
394 return 0;
395 }
396
397 for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len;
398 i++) {
399 if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid,
400 &gattr))
401 continue;
Ursula Braun43e2ada2017-10-11 13:47:23 +0200402 if (gattr.ndev) {
403 if (is_vlan_dev(gattr.ndev) &&
404 vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) {
405 lnk->gid = gid;
406 dev_put(gattr.ndev);
407 return 0;
408 }
409 dev_put(gattr.ndev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100410 }
411 }
412 return -ENODEV;
413}
414
415/* create a new SMC connection (and a new link group if necessary) */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100416int smc_conn_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100417 struct smc_ib_device *smcibdev, u8 ibport,
418 struct smc_clc_msg_local *lcl, int srv_first_contact)
419{
420 struct smc_connection *conn = &smc->conn;
421 struct smc_link_group *lgr;
422 unsigned short vlan_id;
423 enum smc_lgr_role role;
424 int local_contact = SMC_FIRST_CONTACT;
425 int rc = 0;
426
427 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
428 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
429 if (rc)
430 return rc;
431
432 if ((role == SMC_CLNT) && srv_first_contact)
433 /* create new link group as well */
434 goto create;
435
436 /* determine if an existing link group can be reused */
437 spin_lock_bh(&smc_lgr_list.lock);
438 list_for_each_entry(lgr, &smc_lgr_list.list, list) {
439 write_lock_bh(&lgr->conns_lock);
440 if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
441 SMC_SYSTEMID_LEN) &&
442 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
443 SMC_GID_SIZE) &&
444 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
445 sizeof(lcl->mac)) &&
446 !lgr->sync_err &&
447 (lgr->role == role) &&
Ursula Brauncd6851f2017-01-09 16:55:18 +0100448 (lgr->vlan_id == vlan_id) &&
449 ((role == SMC_CLNT) ||
450 (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100451 /* link group found */
452 local_contact = SMC_REUSE_CONTACT;
453 conn->lgr = lgr;
454 smc_lgr_register_conn(conn); /* add smc conn to lgr */
455 write_unlock_bh(&lgr->conns_lock);
456 break;
457 }
458 write_unlock_bh(&lgr->conns_lock);
459 }
460 spin_unlock_bh(&smc_lgr_list.lock);
461
462 if (role == SMC_CLNT && !srv_first_contact &&
463 (local_contact == SMC_FIRST_CONTACT)) {
464 /* Server reuses a link group, but Client wants to start
465 * a new one
466 * send out_of_sync decline, reason synchr. error
467 */
468 return -ENOLINK;
469 }
470
471create:
472 if (local_contact == SMC_FIRST_CONTACT) {
Karsten Graulbe6d4672018-03-01 13:51:28 +0100473 rc = smc_lgr_create(smc, smcibdev, ibport,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100474 lcl->id_for_peer, vlan_id);
475 if (rc)
476 goto out;
477 smc_lgr_register_conn(conn); /* add smc conn to lgr */
478 rc = smc_link_determine_gid(conn->lgr);
479 }
Ursula Braun5f083182017-01-09 16:55:22 +0100480 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
Karsten Graulcbba07a2018-02-28 12:44:07 +0100481 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
Ursula Braun5f083182017-01-09 16:55:22 +0100482#ifndef KERNEL_HAS_ATOMIC64
483 spin_lock_init(&conn->acurs_lock);
484#endif
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100485
486out:
487 return rc ? rc : local_contact;
488}
Ursula Brauncd6851f2017-01-09 16:55:18 +0100489
Ursula Braun3e034722017-07-28 13:56:20 +0200490/* try to reuse a sndbuf or rmb description slot for a certain
491 * buffer size; if not available, return NULL
Ursula Brauncd6851f2017-01-09 16:55:18 +0100492 */
493static inline
Ursula Braun3e034722017-07-28 13:56:20 +0200494struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr,
495 int compressed_bufsize,
496 rwlock_t *lock,
497 struct list_head *buf_list)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100498{
Ursula Braun3e034722017-07-28 13:56:20 +0200499 struct smc_buf_desc *buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100500
Ursula Braun3e034722017-07-28 13:56:20 +0200501 read_lock_bh(lock);
502 list_for_each_entry(buf_slot, buf_list, list) {
503 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
504 read_unlock_bh(lock);
505 return buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100506 }
507 }
Ursula Braun3e034722017-07-28 13:56:20 +0200508 read_unlock_bh(lock);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100509 return NULL;
510}
511
Ursula Braun952310c2017-01-09 16:55:24 +0100512/* one of the conditions for announcing a receiver's current window size is
513 * that it "results in a minimum increase in the window size of 10% of the
514 * receive buffer space" [RFC7609]
515 */
516static inline int smc_rmb_wnd_update_limit(int rmbe_size)
517{
518 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
519}
520
Ursula Braunb33982c2017-07-28 13:56:21 +0200521static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
522 bool is_rmb, int bufsize)
523{
524 struct smc_buf_desc *buf_desc;
525 struct smc_link *lnk;
526 int rc;
527
528 /* try to alloc a new buffer */
529 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
530 if (!buf_desc)
531 return ERR_PTR(-ENOMEM);
532
533 buf_desc->cpu_addr =
534 (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN |
535 __GFP_NOMEMALLOC |
536 __GFP_NORETRY | __GFP_ZERO,
537 get_order(bufsize));
538 if (!buf_desc->cpu_addr) {
539 kfree(buf_desc);
540 return ERR_PTR(-EAGAIN);
541 }
542 buf_desc->order = get_order(bufsize);
543
544 /* build the sg table from the pages */
545 lnk = &lgr->lnk[SMC_SINGLE_LINK];
546 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
547 GFP_KERNEL);
548 if (rc) {
549 smc_buf_free(buf_desc, lnk, is_rmb);
550 return ERR_PTR(rc);
551 }
552 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
553 buf_desc->cpu_addr, bufsize);
554
555 /* map sg table to DMA address */
556 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
557 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
558 /* SMC protocol depends on mapping to one DMA address only */
559 if (rc != 1) {
560 smc_buf_free(buf_desc, lnk, is_rmb);
561 return ERR_PTR(-EAGAIN);
562 }
563
564 /* create a new memory region for the RMB */
565 if (is_rmb) {
566 rc = smc_ib_get_memory_region(lnk->roce_pd,
567 IB_ACCESS_REMOTE_WRITE |
568 IB_ACCESS_LOCAL_WRITE,
569 buf_desc);
570 if (rc) {
571 smc_buf_free(buf_desc, lnk, is_rmb);
572 return ERR_PTR(rc);
573 }
574 }
575
576 return buf_desc;
577}
578
Ursula Braun3e034722017-07-28 13:56:20 +0200579static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100580{
581 struct smc_connection *conn = &smc->conn;
582 struct smc_link_group *lgr = conn->lgr;
Geert Uytterhoeven68870372017-11-21 13:23:54 +0100583 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
Ursula Braun3e034722017-07-28 13:56:20 +0200584 struct list_head *buf_list;
Ursula Braunc45abf32017-07-28 13:56:14 +0200585 int bufsize, bufsize_short;
Ursula Braun3e034722017-07-28 13:56:20 +0200586 int sk_buf_size;
587 rwlock_t *lock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100588
Ursula Braun3e034722017-07-28 13:56:20 +0200589 if (is_rmb)
590 /* use socket recv buffer size (w/o overhead) as start value */
591 sk_buf_size = smc->sk.sk_rcvbuf / 2;
592 else
593 /* use socket send buffer size (w/o overhead) as start value */
594 sk_buf_size = smc->sk.sk_sndbuf / 2;
595
Ursula Braun4e1061f2017-11-21 13:23:53 +0100596 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
Ursula Braunc45abf32017-07-28 13:56:14 +0200597 bufsize_short >= 0; bufsize_short--) {
Ursula Braun3e034722017-07-28 13:56:20 +0200598
599 if (is_rmb) {
600 lock = &lgr->rmbs_lock;
601 buf_list = &lgr->rmbs[bufsize_short];
602 } else {
603 lock = &lgr->sndbufs_lock;
604 buf_list = &lgr->sndbufs[bufsize_short];
605 }
Ursula Braunc45abf32017-07-28 13:56:14 +0200606 bufsize = smc_uncompress_bufsize(bufsize_short);
Ursula Braun9d8fb612017-07-28 13:56:19 +0200607 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
608 continue;
609
Ursula Braun3e034722017-07-28 13:56:20 +0200610 /* check for reusable slot in the link group */
611 buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list);
612 if (buf_desc) {
613 memset(buf_desc->cpu_addr, 0, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100614 break; /* found reusable slot */
615 }
Ursula Braun3e034722017-07-28 13:56:20 +0200616
Ursula Braunb33982c2017-07-28 13:56:21 +0200617 buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
618 if (PTR_ERR(buf_desc) == -ENOMEM)
619 break;
620 if (IS_ERR(buf_desc))
Ursula Brauncd6851f2017-01-09 16:55:18 +0100621 continue;
Ursula Braun3e034722017-07-28 13:56:20 +0200622
623 buf_desc->used = 1;
624 write_lock_bh(lock);
625 list_add(&buf_desc->list, buf_list);
626 write_unlock_bh(lock);
627 break; /* found */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100628 }
Ursula Braun3e034722017-07-28 13:56:20 +0200629
Ursula Braunb33982c2017-07-28 13:56:21 +0200630 if (IS_ERR(buf_desc))
Ursula Braun3e034722017-07-28 13:56:20 +0200631 return -ENOMEM;
632
633 if (is_rmb) {
634 conn->rmb_desc = buf_desc;
Ursula Braunc45abf32017-07-28 13:56:14 +0200635 conn->rmbe_size = bufsize;
636 conn->rmbe_size_short = bufsize_short;
637 smc->sk.sk_rcvbuf = bufsize * 2;
Ursula Braun5f083182017-01-09 16:55:22 +0100638 atomic_set(&conn->bytes_to_rcv, 0);
Ursula Braunc45abf32017-07-28 13:56:14 +0200639 conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100640 } else {
Ursula Braun3e034722017-07-28 13:56:20 +0200641 conn->sndbuf_desc = buf_desc;
642 conn->sndbuf_size = bufsize;
643 smc->sk.sk_sndbuf = bufsize * 2;
644 atomic_set(&conn->sndbuf_space, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100645 }
Ursula Braun3e034722017-07-28 13:56:20 +0200646 return 0;
647}
648
Ursula Braun10428dd2017-07-28 13:56:22 +0200649void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
650{
651 struct smc_link_group *lgr = conn->lgr;
652
653 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
654 conn->sndbuf_desc, DMA_TO_DEVICE);
655}
656
657void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
658{
659 struct smc_link_group *lgr = conn->lgr;
660
661 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
662 conn->sndbuf_desc, DMA_TO_DEVICE);
663}
664
665void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
666{
667 struct smc_link_group *lgr = conn->lgr;
668
669 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
670 conn->rmb_desc, DMA_FROM_DEVICE);
671}
672
673void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
674{
675 struct smc_link_group *lgr = conn->lgr;
676
677 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
678 conn->rmb_desc, DMA_FROM_DEVICE);
679}
680
Ursula Braun3e034722017-07-28 13:56:20 +0200681/* create the send and receive buffer for an SMC socket;
682 * receive buffers are called RMBs;
683 * (even though the SMC protocol allows more than one RMB-element per RMB,
684 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
685 * extra RMB for every connection in a link group
686 */
687int smc_buf_create(struct smc_sock *smc)
688{
689 int rc;
690
691 /* create send buffer */
692 rc = __smc_buf_create(smc, false);
693 if (rc)
694 return rc;
695 /* create rmb */
696 rc = __smc_buf_create(smc, true);
697 if (rc)
698 smc_buf_free(smc->conn.sndbuf_desc,
699 &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false);
700 return rc;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100701}
Ursula Braunbd4ad572017-01-09 16:55:20 +0100702
703static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
704{
705 int i;
706
707 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
708 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
709 return i;
710 }
711 return -ENOSPC;
712}
713
Karsten Graul4ed75de2018-03-01 13:51:30 +0100714/* add a new rtoken from peer */
715int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey)
Ursula Braunbd4ad572017-01-09 16:55:20 +0100716{
Karsten Graul4ed75de2018-03-01 13:51:30 +0100717 u64 dma_addr = be64_to_cpu(nw_vaddr);
718 u32 rkey = ntohl(nw_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100719 int i;
720
721 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
722 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
Ursula Braun263eec92017-05-15 17:33:37 +0200723 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
Ursula Braunbd4ad572017-01-09 16:55:20 +0100724 test_bit(i, lgr->rtokens_used_mask)) {
Karsten Graul4ed75de2018-03-01 13:51:30 +0100725 /* already in list */
726 return i;
727 }
728 }
729 i = smc_rmb_reserve_rtoken_idx(lgr);
730 if (i < 0)
731 return i;
732 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey;
733 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr;
734 return i;
735}
736
737/* delete an rtoken */
738int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey)
739{
740 u32 rkey = ntohl(nw_rkey);
741 int i;
742
743 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
744 if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey &&
745 test_bit(i, lgr->rtokens_used_mask)) {
746 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0;
747 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0;
748
749 clear_bit(i, lgr->rtokens_used_mask);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100750 return 0;
751 }
752 }
Karsten Graul4ed75de2018-03-01 13:51:30 +0100753 return -ENOENT;
754}
755
756/* save rkey and dma_addr received from peer during clc handshake */
757int smc_rmb_rtoken_handling(struct smc_connection *conn,
758 struct smc_clc_msg_accept_confirm *clc)
759{
760 conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr,
761 clc->rmb_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100762 if (conn->rtoken_idx < 0)
763 return conn->rtoken_idx;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100764 return 0;
765}