blob: c63cf7b07ccccaf9e4f6a4e5bab89a373c21d23f [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
Hans Wippel9fda3512018-05-18 09:34:11 +020033static struct smc_lgr_list smc_lgr_list = { /* established link groups */
34 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
35 .list = LIST_HEAD_INIT(smc_lgr_list.list),
36 .num = 0,
37};
Ursula Braun9bf9abe2017-01-09 16:55:21 +010038
Karsten Graula6920d12018-05-03 17:57:38 +020039static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
40 bool is_rmb);
41
Karsten Graul97cdbc42018-03-14 11:01:02 +010042static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
43{
44 /* client link group creation always follows the server link group
45 * creation. For client use a somewhat higher removal delay time,
46 * otherwise there is a risk of out-of-sync link groups.
47 */
48 mod_delayed_work(system_wq, &lgr->free_work,
49 lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT :
50 SMC_LGR_FREE_DELAY_SERV);
51}
52
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010053/* Register connection's alert token in our lookup structure.
54 * To use rbtrees we have to implement our own insert core.
55 * Requires @conns_lock
56 * @smc connection to register
57 * Returns 0 on success, != otherwise.
58 */
59static void smc_lgr_add_alert_token(struct smc_connection *conn)
60{
61 struct rb_node **link, *parent = NULL;
62 u32 token = conn->alert_token_local;
63
64 link = &conn->lgr->conns_all.rb_node;
65 while (*link) {
66 struct smc_connection *cur = rb_entry(*link,
67 struct smc_connection, alert_node);
68
69 parent = *link;
70 if (cur->alert_token_local > token)
71 link = &parent->rb_left;
72 else
73 link = &parent->rb_right;
74 }
75 /* Put the new node there */
76 rb_link_node(&conn->alert_node, parent, link);
77 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
78}
79
80/* Register connection in link group by assigning an alert token
81 * registered in a search tree.
82 * Requires @conns_lock
83 * Note that '0' is a reserved value and not assigned.
84 */
85static void smc_lgr_register_conn(struct smc_connection *conn)
86{
87 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
88 static atomic_t nexttoken = ATOMIC_INIT(0);
89
90 /* find a new alert_token_local value not yet used by some connection
91 * in this link group
92 */
93 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
94 while (!conn->alert_token_local) {
95 conn->alert_token_local = atomic_inc_return(&nexttoken);
96 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
97 conn->alert_token_local = 0;
98 }
99 smc_lgr_add_alert_token(conn);
100 conn->lgr->conns_num++;
101}
102
103/* Unregister connection and reset the alert token of the given connection<
104 */
105static void __smc_lgr_unregister_conn(struct smc_connection *conn)
106{
107 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
108 struct smc_link_group *lgr = conn->lgr;
109
110 rb_erase(&conn->alert_node, &lgr->conns_all);
111 lgr->conns_num--;
112 conn->alert_token_local = 0;
113 conn->lgr = NULL;
114 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
115}
116
117/* Unregister connection and trigger lgr freeing if applicable
118 */
119static void smc_lgr_unregister_conn(struct smc_connection *conn)
120{
121 struct smc_link_group *lgr = conn->lgr;
122 int reduced = 0;
123
124 write_lock_bh(&lgr->conns_lock);
125 if (conn->alert_token_local) {
126 reduced = 1;
127 __smc_lgr_unregister_conn(conn);
128 }
129 write_unlock_bh(&lgr->conns_lock);
Ursula Braun5bc11dd2017-09-21 09:16:31 +0200130 if (!reduced || lgr->conns_num)
131 return;
Karsten Graul97cdbc42018-03-14 11:01:02 +0100132 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100133}
134
135static void smc_lgr_free_work(struct work_struct *work)
136{
137 struct smc_link_group *lgr = container_of(to_delayed_work(work),
138 struct smc_link_group,
139 free_work);
140 bool conns;
141
142 spin_lock_bh(&smc_lgr_list.lock);
Ursula Braun610db662018-01-25 11:15:34 +0100143 if (list_empty(&lgr->list))
144 goto free;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100145 read_lock_bh(&lgr->conns_lock);
146 conns = RB_EMPTY_ROOT(&lgr->conns_all);
147 read_unlock_bh(&lgr->conns_lock);
148 if (!conns) { /* number of lgr connections is no longer zero */
149 spin_unlock_bh(&smc_lgr_list.lock);
150 return;
151 }
152 list_del_init(&lgr->list); /* remove from smc_lgr_list */
Ursula Braun610db662018-01-25 11:15:34 +0100153free:
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100154 spin_unlock_bh(&smc_lgr_list.lock);
Karsten Graul3cf52eb2018-05-15 17:05:01 +0200155 if (!delayed_work_pending(&lgr->free_work)) {
156 if (lgr->lnk[SMC_SINGLE_LINK].state != SMC_LNK_INACTIVE)
157 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
Ursula Braun268ffcc2018-03-14 11:01:01 +0100158 smc_lgr_free(lgr);
Karsten Graul3cf52eb2018-05-15 17:05:01 +0200159 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100160}
161
162/* create a new SMC link group */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100163static int smc_lgr_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100164 struct smc_ib_device *smcibdev, u8 ibport,
165 char *peer_systemid, unsigned short vlan_id)
166{
167 struct smc_link_group *lgr;
168 struct smc_link *lnk;
169 u8 rndvec[3];
170 int rc = 0;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100171 int i;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100172
173 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
174 if (!lgr) {
175 rc = -ENOMEM;
176 goto out;
177 }
178 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
Karsten Graul517c3002018-05-15 17:05:03 +0200179 lgr->sync_err = 0;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100180 memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN);
181 lgr->vlan_id = vlan_id;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100182 rwlock_init(&lgr->sndbufs_lock);
183 rwlock_init(&lgr->rmbs_lock);
184 for (i = 0; i < SMC_RMBE_SIZES; i++) {
185 INIT_LIST_HEAD(&lgr->sndbufs[i]);
186 INIT_LIST_HEAD(&lgr->rmbs[i]);
187 }
Hans Wippel9fda3512018-05-18 09:34:11 +0200188 smc_lgr_list.num += SMC_LGR_NUM_INCR;
189 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100190 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
191 lgr->conns_all = RB_ROOT;
192
193 lnk = &lgr->lnk[SMC_SINGLE_LINK];
194 /* initialize link */
Karsten Graul52bedf32018-03-01 13:51:32 +0100195 lnk->state = SMC_LNK_ACTIVATING;
Karsten Graul2be922f2018-02-28 12:44:08 +0100196 lnk->link_id = SMC_SINGLE_LINK;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100197 lnk->smcibdev = smcibdev;
198 lnk->ibport = ibport;
199 lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100200 if (!smcibdev->initialized)
201 smc_ib_setup_per_ibdev(smcibdev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100202 get_random_bytes(rndvec, sizeof(rndvec));
203 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16);
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200204 rc = smc_llc_link_init(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100205 if (rc)
206 goto free_lgr;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200207 rc = smc_wr_alloc_link_mem(lnk);
208 if (rc)
209 goto clear_llc_lnk;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100210 rc = smc_ib_create_protection_domain(lnk);
211 if (rc)
212 goto free_link_mem;
213 rc = smc_ib_create_queue_pair(lnk);
214 if (rc)
215 goto dealloc_pd;
216 rc = smc_wr_create_link(lnk);
217 if (rc)
218 goto destroy_qp;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100219
220 smc->conn.lgr = lgr;
221 rwlock_init(&lgr->conns_lock);
222 spin_lock_bh(&smc_lgr_list.lock);
223 list_add(&lgr->list, &smc_lgr_list.list);
224 spin_unlock_bh(&smc_lgr_list.lock);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100225 return 0;
226
Ursula Braunbd4ad572017-01-09 16:55:20 +0100227destroy_qp:
228 smc_ib_destroy_queue_pair(lnk);
229dealloc_pd:
230 smc_ib_dealloc_protection_domain(lnk);
231free_link_mem:
232 smc_wr_free_link_mem(lnk);
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200233clear_llc_lnk:
234 smc_llc_link_clear(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100235free_lgr:
236 kfree(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100237out:
238 return rc;
239}
240
Ursula Braun3e034722017-07-28 13:56:20 +0200241static void smc_buf_unuse(struct smc_connection *conn)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100242{
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200243 if (conn->sndbuf_desc)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100244 conn->sndbuf_desc->used = 0;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100245 if (conn->rmb_desc) {
Karsten Graula6920d12018-05-03 17:57:38 +0200246 if (!conn->rmb_desc->regerr) {
247 conn->rmb_desc->reused = 1;
248 conn->rmb_desc->used = 0;
Karsten Graula6920d12018-05-03 17:57:38 +0200249 } else {
250 /* buf registration failed, reuse not possible */
251 struct smc_link_group *lgr = conn->lgr;
252 struct smc_link *lnk;
253
254 write_lock_bh(&lgr->rmbs_lock);
255 list_del(&conn->rmb_desc->list);
256 write_unlock_bh(&lgr->rmbs_lock);
257
258 lnk = &lgr->lnk[SMC_SINGLE_LINK];
259 smc_buf_free(conn->rmb_desc, lnk, true);
260 }
Ursula Brauncd6851f2017-01-09 16:55:18 +0100261 }
262}
263
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100264/* remove a finished connection from its link group */
265void smc_conn_free(struct smc_connection *conn)
266{
Ursula Braun732720f2018-01-25 11:15:35 +0100267 if (!conn->lgr)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100268 return;
Ursula Braun5f083182017-01-09 16:55:22 +0100269 smc_cdc_tx_dismiss_slots(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100270 smc_lgr_unregister_conn(conn);
Ursula Braun3e034722017-07-28 13:56:20 +0200271 smc_buf_unuse(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100272}
273
274static void smc_link_clear(struct smc_link *lnk)
275{
276 lnk->peer_qpn = 0;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200277 smc_llc_link_clear(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100278 smc_ib_modify_qp_reset(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100279 smc_wr_free_link(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100280 smc_ib_destroy_queue_pair(lnk);
281 smc_ib_dealloc_protection_domain(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100282 smc_wr_free_link_mem(lnk);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100283}
284
Ursula Braun3e034722017-07-28 13:56:20 +0200285static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk,
286 bool is_rmb)
287{
288 if (is_rmb) {
289 if (buf_desc->mr_rx[SMC_SINGLE_LINK])
290 smc_ib_put_memory_region(
291 buf_desc->mr_rx[SMC_SINGLE_LINK]);
292 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
293 DMA_FROM_DEVICE);
294 } else {
295 smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
296 DMA_TO_DEVICE);
297 }
298 sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]);
Stefan Raspl2ef4f272018-05-03 18:12:38 +0200299 if (buf_desc->pages)
300 __free_pages(buf_desc->pages, buf_desc->order);
Ursula Braun3e034722017-07-28 13:56:20 +0200301 kfree(buf_desc);
302}
303
304static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100305{
Ursula Braun9d8fb612017-07-28 13:56:19 +0200306 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun3e034722017-07-28 13:56:20 +0200307 struct smc_buf_desc *buf_desc, *bf_desc;
308 struct list_head *buf_list;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100309 int i;
310
311 for (i = 0; i < SMC_RMBE_SIZES; i++) {
Ursula Braun3e034722017-07-28 13:56:20 +0200312 if (is_rmb)
313 buf_list = &lgr->rmbs[i];
314 else
315 buf_list = &lgr->sndbufs[i];
316 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
Ursula Brauncd6851f2017-01-09 16:55:18 +0100317 list) {
Ursula Braun3e034722017-07-28 13:56:20 +0200318 list_del(&buf_desc->list);
319 smc_buf_free(buf_desc, lnk, is_rmb);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100320 }
321 }
322}
323
Ursula Braun3e034722017-07-28 13:56:20 +0200324static void smc_lgr_free_bufs(struct smc_link_group *lgr)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100325{
Ursula Braun3e034722017-07-28 13:56:20 +0200326 /* free send buffers */
327 __smc_lgr_free_bufs(lgr, false);
328 /* free rmbs */
329 __smc_lgr_free_bufs(lgr, true);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100330}
331
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100332/* remove a link group */
333void smc_lgr_free(struct smc_link_group *lgr)
334{
Ursula Braun3e034722017-07-28 13:56:20 +0200335 smc_lgr_free_bufs(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100336 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
337 kfree(lgr);
338}
339
Karsten Graul9651b932018-03-01 13:51:33 +0100340void smc_lgr_forget(struct smc_link_group *lgr)
341{
342 spin_lock_bh(&smc_lgr_list.lock);
343 /* do not use this link group for new connections */
344 if (!list_empty(&lgr->list))
345 list_del_init(&lgr->list);
346 spin_unlock_bh(&smc_lgr_list.lock);
347}
348
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100349/* terminate linkgroup abnormally */
350void smc_lgr_terminate(struct smc_link_group *lgr)
351{
352 struct smc_connection *conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100353 struct smc_sock *smc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100354 struct rb_node *node;
355
Karsten Graul517c3002018-05-15 17:05:03 +0200356 if (lgr->terminating)
357 return; /* lgr already terminating */
358 lgr->terminating = 1;
Karsten Graul9651b932018-03-01 13:51:33 +0100359 smc_lgr_forget(lgr);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200360 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100361
362 write_lock_bh(&lgr->conns_lock);
363 node = rb_first(&lgr->conns_all);
364 while (node) {
365 conn = rb_entry(node, struct smc_connection, alert_node);
Ursula Braunb38d7322017-01-09 16:55:25 +0100366 smc = container_of(conn, struct smc_sock, conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100367 sock_hold(&smc->sk); /* sock_put in close work */
Ursula Braun732720f2018-01-25 11:15:35 +0100368 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100369 __smc_lgr_unregister_conn(conn);
Ursula Braun732720f2018-01-25 11:15:35 +0100370 write_unlock_bh(&lgr->conns_lock);
Ursula Braun51f1de72018-01-26 09:28:48 +0100371 if (!schedule_work(&conn->close_work))
372 sock_put(&smc->sk);
Ursula Braun732720f2018-01-25 11:15:35 +0100373 write_lock_bh(&lgr->conns_lock);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100374 node = rb_first(&lgr->conns_all);
375 }
376 write_unlock_bh(&lgr->conns_lock);
Ursula Braun732720f2018-01-25 11:15:35 +0100377 wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait);
Karsten Graul97cdbc42018-03-14 11:01:02 +0100378 smc_lgr_schedule_free_work(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100379}
380
Hans Wippel9fda3512018-05-18 09:34:11 +0200381/* Called when IB port is terminated */
382void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport)
383{
384 struct smc_link_group *lgr, *l;
385
386 list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) {
387 if (lgr->lnk[SMC_SINGLE_LINK].smcibdev == smcibdev &&
388 lgr->lnk[SMC_SINGLE_LINK].ibport == ibport)
389 smc_lgr_terminate(lgr);
390 }
391}
392
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100393/* Determine vlan of internal TCP socket.
394 * @vlan_id: address to store the determined vlan id into
395 */
396static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
397{
398 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Ursula Brauncb9d43f2018-05-02 16:56:47 +0200399 struct net_device *ndev;
400 int i, nest_lvl, rc = 0;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100401
402 *vlan_id = 0;
403 if (!dst) {
404 rc = -ENOTCONN;
405 goto out;
406 }
407 if (!dst->dev) {
408 rc = -ENODEV;
409 goto out_rel;
410 }
411
Ursula Brauncb9d43f2018-05-02 16:56:47 +0200412 ndev = dst->dev;
413 if (is_vlan_dev(ndev)) {
414 *vlan_id = vlan_dev_vlan_id(ndev);
415 goto out_rel;
416 }
417
418 rtnl_lock();
419 nest_lvl = dev_get_nest_level(ndev);
420 for (i = 0; i < nest_lvl; i++) {
421 struct list_head *lower = &ndev->adj_list.lower;
422
423 if (list_empty(lower))
424 break;
425 lower = lower->next;
426 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
427 if (is_vlan_dev(ndev)) {
428 *vlan_id = vlan_dev_vlan_id(ndev);
429 break;
430 }
431 }
432 rtnl_unlock();
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100433
434out_rel:
435 dst_release(dst);
436out:
437 return rc;
438}
439
440/* determine the link gid matching the vlan id of the link group */
441static int smc_link_determine_gid(struct smc_link_group *lgr)
442{
443 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
444 struct ib_gid_attr gattr;
445 union ib_gid gid;
446 int i;
447
448 if (!lgr->vlan_id) {
449 lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1];
450 return 0;
451 }
452
453 for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len;
454 i++) {
455 if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid,
456 &gattr))
457 continue;
Ursula Braun43e2ada2017-10-11 13:47:23 +0200458 if (gattr.ndev) {
459 if (is_vlan_dev(gattr.ndev) &&
460 vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) {
461 lnk->gid = gid;
462 dev_put(gattr.ndev);
463 return 0;
464 }
465 dev_put(gattr.ndev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100466 }
467 }
468 return -ENODEV;
469}
470
471/* create a new SMC connection (and a new link group if necessary) */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100472int smc_conn_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100473 struct smc_ib_device *smcibdev, u8 ibport,
474 struct smc_clc_msg_local *lcl, int srv_first_contact)
475{
476 struct smc_connection *conn = &smc->conn;
Hans Wippel8437bda2018-05-18 09:34:16 +0200477 int local_contact = SMC_FIRST_CONTACT;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100478 struct smc_link_group *lgr;
479 unsigned short vlan_id;
480 enum smc_lgr_role role;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100481 int rc = 0;
482
483 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
484 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
485 if (rc)
486 return rc;
487
488 if ((role == SMC_CLNT) && srv_first_contact)
489 /* create new link group as well */
490 goto create;
491
492 /* determine if an existing link group can be reused */
493 spin_lock_bh(&smc_lgr_list.lock);
494 list_for_each_entry(lgr, &smc_lgr_list.list, list) {
495 write_lock_bh(&lgr->conns_lock);
496 if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
497 SMC_SYSTEMID_LEN) &&
498 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
499 SMC_GID_SIZE) &&
500 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
501 sizeof(lcl->mac)) &&
502 !lgr->sync_err &&
503 (lgr->role == role) &&
Ursula Brauncd6851f2017-01-09 16:55:18 +0100504 (lgr->vlan_id == vlan_id) &&
505 ((role == SMC_CLNT) ||
506 (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100507 /* link group found */
508 local_contact = SMC_REUSE_CONTACT;
509 conn->lgr = lgr;
510 smc_lgr_register_conn(conn); /* add smc conn to lgr */
511 write_unlock_bh(&lgr->conns_lock);
512 break;
513 }
514 write_unlock_bh(&lgr->conns_lock);
515 }
516 spin_unlock_bh(&smc_lgr_list.lock);
517
518 if (role == SMC_CLNT && !srv_first_contact &&
519 (local_contact == SMC_FIRST_CONTACT)) {
520 /* Server reuses a link group, but Client wants to start
521 * a new one
522 * send out_of_sync decline, reason synchr. error
523 */
524 return -ENOLINK;
525 }
526
527create:
528 if (local_contact == SMC_FIRST_CONTACT) {
Karsten Graulbe6d4672018-03-01 13:51:28 +0100529 rc = smc_lgr_create(smc, smcibdev, ibport,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100530 lcl->id_for_peer, vlan_id);
531 if (rc)
532 goto out;
533 smc_lgr_register_conn(conn); /* add smc conn to lgr */
534 rc = smc_link_determine_gid(conn->lgr);
535 }
Ursula Braun5f083182017-01-09 16:55:22 +0100536 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
Karsten Graulcbba07a2018-02-28 12:44:07 +0100537 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
Ursula Braun5f083182017-01-09 16:55:22 +0100538#ifndef KERNEL_HAS_ATOMIC64
539 spin_lock_init(&conn->acurs_lock);
540#endif
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100541
542out:
543 return rc ? rc : local_contact;
544}
Ursula Brauncd6851f2017-01-09 16:55:18 +0100545
Hans Wippel2f6beca2018-05-18 09:34:14 +0200546/* convert the RMB size into the compressed notation - minimum 16K.
547 * In contrast to plain ilog2, this rounds towards the next power of 2,
548 * so the socket application gets at least its desired sndbuf / rcvbuf size.
549 */
550static u8 smc_compress_bufsize(int size)
551{
552 u8 compressed;
553
554 if (size <= SMC_BUF_MIN_SIZE)
555 return 0;
556
557 size = (size - 1) >> 14;
558 compressed = ilog2(size) + 1;
559 if (compressed >= SMC_RMBE_SIZES)
560 compressed = SMC_RMBE_SIZES - 1;
561 return compressed;
562}
563
564/* convert the RMB size from compressed notation into integer */
565int smc_uncompress_bufsize(u8 compressed)
566{
567 u32 size;
568
569 size = 0x00000001 << (((int)compressed) + 14);
570 return (int)size;
571}
572
Ursula Braun3e034722017-07-28 13:56:20 +0200573/* try to reuse a sndbuf or rmb description slot for a certain
574 * buffer size; if not available, return NULL
Ursula Brauncd6851f2017-01-09 16:55:18 +0100575 */
Hans Wippel8437bda2018-05-18 09:34:16 +0200576static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
577 rwlock_t *lock,
578 struct list_head *buf_list)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100579{
Ursula Braun3e034722017-07-28 13:56:20 +0200580 struct smc_buf_desc *buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100581
Ursula Braun3e034722017-07-28 13:56:20 +0200582 read_lock_bh(lock);
583 list_for_each_entry(buf_slot, buf_list, list) {
584 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
585 read_unlock_bh(lock);
586 return buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100587 }
588 }
Ursula Braun3e034722017-07-28 13:56:20 +0200589 read_unlock_bh(lock);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100590 return NULL;
591}
592
Ursula Braun952310c2017-01-09 16:55:24 +0100593/* one of the conditions for announcing a receiver's current window size is
594 * that it "results in a minimum increase in the window size of 10% of the
595 * receive buffer space" [RFC7609]
596 */
597static inline int smc_rmb_wnd_update_limit(int rmbe_size)
598{
599 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
600}
601
Ursula Braunb33982c2017-07-28 13:56:21 +0200602static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
603 bool is_rmb, int bufsize)
604{
605 struct smc_buf_desc *buf_desc;
606 struct smc_link *lnk;
607 int rc;
608
609 /* try to alloc a new buffer */
610 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
611 if (!buf_desc)
612 return ERR_PTR(-ENOMEM);
613
Stefan Raspl2ef4f272018-05-03 18:12:38 +0200614 buf_desc->order = get_order(bufsize);
615 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
616 __GFP_NOMEMALLOC | __GFP_COMP |
617 __GFP_NORETRY | __GFP_ZERO,
618 buf_desc->order);
619 if (!buf_desc->pages) {
Ursula Braunb33982c2017-07-28 13:56:21 +0200620 kfree(buf_desc);
621 return ERR_PTR(-EAGAIN);
622 }
Stefan Raspl2ef4f272018-05-03 18:12:38 +0200623 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
Ursula Braunb33982c2017-07-28 13:56:21 +0200624
625 /* build the sg table from the pages */
626 lnk = &lgr->lnk[SMC_SINGLE_LINK];
627 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
628 GFP_KERNEL);
629 if (rc) {
630 smc_buf_free(buf_desc, lnk, is_rmb);
631 return ERR_PTR(rc);
632 }
633 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
634 buf_desc->cpu_addr, bufsize);
635
636 /* map sg table to DMA address */
637 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
638 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
639 /* SMC protocol depends on mapping to one DMA address only */
640 if (rc != 1) {
641 smc_buf_free(buf_desc, lnk, is_rmb);
642 return ERR_PTR(-EAGAIN);
643 }
644
645 /* create a new memory region for the RMB */
646 if (is_rmb) {
647 rc = smc_ib_get_memory_region(lnk->roce_pd,
648 IB_ACCESS_REMOTE_WRITE |
649 IB_ACCESS_LOCAL_WRITE,
650 buf_desc);
651 if (rc) {
652 smc_buf_free(buf_desc, lnk, is_rmb);
653 return ERR_PTR(rc);
654 }
655 }
656
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200657 buf_desc->len = bufsize;
Ursula Braunb33982c2017-07-28 13:56:21 +0200658 return buf_desc;
659}
660
Ursula Braun3e034722017-07-28 13:56:20 +0200661static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100662{
Hans Wippel8437bda2018-05-18 09:34:16 +0200663 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100664 struct smc_connection *conn = &smc->conn;
665 struct smc_link_group *lgr = conn->lgr;
Ursula Braun3e034722017-07-28 13:56:20 +0200666 struct list_head *buf_list;
Ursula Braunc45abf32017-07-28 13:56:14 +0200667 int bufsize, bufsize_short;
Ursula Braun3e034722017-07-28 13:56:20 +0200668 int sk_buf_size;
669 rwlock_t *lock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100670
Ursula Braun3e034722017-07-28 13:56:20 +0200671 if (is_rmb)
672 /* use socket recv buffer size (w/o overhead) as start value */
673 sk_buf_size = smc->sk.sk_rcvbuf / 2;
674 else
675 /* use socket send buffer size (w/o overhead) as start value */
676 sk_buf_size = smc->sk.sk_sndbuf / 2;
677
Ursula Braun4e1061f2017-11-21 13:23:53 +0100678 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
Ursula Braunc45abf32017-07-28 13:56:14 +0200679 bufsize_short >= 0; bufsize_short--) {
Ursula Braun3e034722017-07-28 13:56:20 +0200680
681 if (is_rmb) {
682 lock = &lgr->rmbs_lock;
683 buf_list = &lgr->rmbs[bufsize_short];
684 } else {
685 lock = &lgr->sndbufs_lock;
686 buf_list = &lgr->sndbufs[bufsize_short];
687 }
Ursula Braunc45abf32017-07-28 13:56:14 +0200688 bufsize = smc_uncompress_bufsize(bufsize_short);
Ursula Braun9d8fb612017-07-28 13:56:19 +0200689 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
690 continue;
691
Ursula Braun3e034722017-07-28 13:56:20 +0200692 /* check for reusable slot in the link group */
Hans Wippel8437bda2018-05-18 09:34:16 +0200693 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
Ursula Braun3e034722017-07-28 13:56:20 +0200694 if (buf_desc) {
695 memset(buf_desc->cpu_addr, 0, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100696 break; /* found reusable slot */
697 }
Ursula Braun3e034722017-07-28 13:56:20 +0200698
Ursula Braunb33982c2017-07-28 13:56:21 +0200699 buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
700 if (PTR_ERR(buf_desc) == -ENOMEM)
701 break;
702 if (IS_ERR(buf_desc))
Ursula Brauncd6851f2017-01-09 16:55:18 +0100703 continue;
Ursula Braun3e034722017-07-28 13:56:20 +0200704
705 buf_desc->used = 1;
706 write_lock_bh(lock);
707 list_add(&buf_desc->list, buf_list);
708 write_unlock_bh(lock);
709 break; /* found */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100710 }
Ursula Braun3e034722017-07-28 13:56:20 +0200711
Ursula Braunb33982c2017-07-28 13:56:21 +0200712 if (IS_ERR(buf_desc))
Ursula Braun3e034722017-07-28 13:56:20 +0200713 return -ENOMEM;
714
715 if (is_rmb) {
716 conn->rmb_desc = buf_desc;
Ursula Braunc45abf32017-07-28 13:56:14 +0200717 conn->rmbe_size_short = bufsize_short;
718 smc->sk.sk_rcvbuf = bufsize * 2;
Ursula Braun5f083182017-01-09 16:55:22 +0100719 atomic_set(&conn->bytes_to_rcv, 0);
Ursula Braunc45abf32017-07-28 13:56:14 +0200720 conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100721 } else {
Ursula Braun3e034722017-07-28 13:56:20 +0200722 conn->sndbuf_desc = buf_desc;
Ursula Braun3e034722017-07-28 13:56:20 +0200723 smc->sk.sk_sndbuf = bufsize * 2;
724 atomic_set(&conn->sndbuf_space, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100725 }
Ursula Braun3e034722017-07-28 13:56:20 +0200726 return 0;
727}
728
Ursula Braun10428dd2017-07-28 13:56:22 +0200729void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
730{
731 struct smc_link_group *lgr = conn->lgr;
732
733 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
734 conn->sndbuf_desc, DMA_TO_DEVICE);
735}
736
737void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
738{
739 struct smc_link_group *lgr = conn->lgr;
740
741 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
742 conn->sndbuf_desc, DMA_TO_DEVICE);
743}
744
745void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
746{
747 struct smc_link_group *lgr = conn->lgr;
748
749 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
750 conn->rmb_desc, DMA_FROM_DEVICE);
751}
752
753void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
754{
755 struct smc_link_group *lgr = conn->lgr;
756
757 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
758 conn->rmb_desc, DMA_FROM_DEVICE);
759}
760
Ursula Braun3e034722017-07-28 13:56:20 +0200761/* create the send and receive buffer for an SMC socket;
762 * receive buffers are called RMBs;
763 * (even though the SMC protocol allows more than one RMB-element per RMB,
764 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
765 * extra RMB for every connection in a link group
766 */
767int smc_buf_create(struct smc_sock *smc)
768{
769 int rc;
770
771 /* create send buffer */
772 rc = __smc_buf_create(smc, false);
773 if (rc)
774 return rc;
775 /* create rmb */
776 rc = __smc_buf_create(smc, true);
777 if (rc)
778 smc_buf_free(smc->conn.sndbuf_desc,
779 &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false);
780 return rc;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100781}
Ursula Braunbd4ad572017-01-09 16:55:20 +0100782
783static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
784{
785 int i;
786
787 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
788 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
789 return i;
790 }
791 return -ENOSPC;
792}
793
Karsten Graul4ed75de2018-03-01 13:51:30 +0100794/* add a new rtoken from peer */
795int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey)
Ursula Braunbd4ad572017-01-09 16:55:20 +0100796{
Karsten Graul4ed75de2018-03-01 13:51:30 +0100797 u64 dma_addr = be64_to_cpu(nw_vaddr);
798 u32 rkey = ntohl(nw_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100799 int i;
800
801 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
802 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
Ursula Braun263eec92017-05-15 17:33:37 +0200803 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
Ursula Braunbd4ad572017-01-09 16:55:20 +0100804 test_bit(i, lgr->rtokens_used_mask)) {
Karsten Graul4ed75de2018-03-01 13:51:30 +0100805 /* already in list */
806 return i;
807 }
808 }
809 i = smc_rmb_reserve_rtoken_idx(lgr);
810 if (i < 0)
811 return i;
812 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey;
813 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr;
814 return i;
815}
816
817/* delete an rtoken */
818int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey)
819{
820 u32 rkey = ntohl(nw_rkey);
821 int i;
822
823 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
824 if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey &&
825 test_bit(i, lgr->rtokens_used_mask)) {
826 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0;
827 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0;
828
829 clear_bit(i, lgr->rtokens_used_mask);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100830 return 0;
831 }
832 }
Karsten Graul4ed75de2018-03-01 13:51:30 +0100833 return -ENOENT;
834}
835
836/* save rkey and dma_addr received from peer during clc handshake */
837int smc_rmb_rtoken_handling(struct smc_connection *conn,
838 struct smc_clc_msg_accept_confirm *clc)
839{
840 conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr,
841 clc->rmb_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100842 if (conn->rtoken_idx < 0)
843 return conn->rtoken_idx;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100844 return 0;
845}
Hans Wippel9fda3512018-05-18 09:34:11 +0200846
847/* Called (from smc_exit) when module is removed */
848void smc_core_exit(void)
849{
850 struct smc_link_group *lgr, *lg;
851 LIST_HEAD(lgr_freeing_list);
852
853 spin_lock_bh(&smc_lgr_list.lock);
854 if (!list_empty(&smc_lgr_list.list))
855 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
856 spin_unlock_bh(&smc_lgr_list.lock);
857 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
858 list_del_init(&lgr->list);
859 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
860 cancel_delayed_work_sync(&lgr->free_work);
861 smc_lgr_free(lgr); /* free link group */
862 }
863}