blob: add82b0266f303ac943ecf8786e1b61e401a9b36 [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)
Ursula Braun7f58a1a2018-05-23 16:38:12 +020031#define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
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
Hans Wippel6511aad2018-05-18 09:34:17 +020039static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
40 struct smc_buf_desc *buf_desc);
Karsten Graula6920d12018-05-03 17:57:38 +020041
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;
Karsten Graula6920d12018-05-03 17:57:38 +0200252
253 write_lock_bh(&lgr->rmbs_lock);
254 list_del(&conn->rmb_desc->list);
255 write_unlock_bh(&lgr->rmbs_lock);
256
Hans Wippel6511aad2018-05-18 09:34:17 +0200257 smc_buf_free(lgr, true, conn->rmb_desc);
Karsten Graula6920d12018-05-03 17:57:38 +0200258 }
Ursula Brauncd6851f2017-01-09 16:55:18 +0100259 }
260}
261
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100262/* remove a finished connection from its link group */
263void smc_conn_free(struct smc_connection *conn)
264{
Ursula Braun732720f2018-01-25 11:15:35 +0100265 if (!conn->lgr)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100266 return;
Ursula Braun5f083182017-01-09 16:55:22 +0100267 smc_cdc_tx_dismiss_slots(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100268 smc_lgr_unregister_conn(conn);
Ursula Braun3e034722017-07-28 13:56:20 +0200269 smc_buf_unuse(conn);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100270}
271
272static void smc_link_clear(struct smc_link *lnk)
273{
274 lnk->peer_qpn = 0;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200275 smc_llc_link_clear(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100276 smc_ib_modify_qp_reset(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100277 smc_wr_free_link(lnk);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100278 smc_ib_destroy_queue_pair(lnk);
279 smc_ib_dealloc_protection_domain(lnk);
Ursula Braunf38ba1792017-01-09 16:55:19 +0100280 smc_wr_free_link_mem(lnk);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100281}
282
Hans Wippel6511aad2018-05-18 09:34:17 +0200283static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
284 struct smc_buf_desc *buf_desc)
Ursula Braun3e034722017-07-28 13:56:20 +0200285{
Hans Wippel6511aad2018-05-18 09:34:17 +0200286 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
287
Ursula Braun3e034722017-07-28 13:56:20 +0200288 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 Braun3e034722017-07-28 13:56:20 +0200306 struct smc_buf_desc *buf_desc, *bf_desc;
307 struct list_head *buf_list;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100308 int i;
309
310 for (i = 0; i < SMC_RMBE_SIZES; i++) {
Ursula Braun3e034722017-07-28 13:56:20 +0200311 if (is_rmb)
312 buf_list = &lgr->rmbs[i];
313 else
314 buf_list = &lgr->sndbufs[i];
315 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
Ursula Brauncd6851f2017-01-09 16:55:18 +0100316 list) {
Ursula Braun3e034722017-07-28 13:56:20 +0200317 list_del(&buf_desc->list);
Hans Wippel6511aad2018-05-18 09:34:17 +0200318 smc_buf_free(lgr, is_rmb, buf_desc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100319 }
320 }
321}
322
Ursula Braun3e034722017-07-28 13:56:20 +0200323static void smc_lgr_free_bufs(struct smc_link_group *lgr)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100324{
Ursula Braun3e034722017-07-28 13:56:20 +0200325 /* free send buffers */
326 __smc_lgr_free_bufs(lgr, false);
327 /* free rmbs */
328 __smc_lgr_free_bufs(lgr, true);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100329}
330
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100331/* remove a link group */
332void smc_lgr_free(struct smc_link_group *lgr)
333{
Ursula Braun3e034722017-07-28 13:56:20 +0200334 smc_lgr_free_bufs(lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100335 smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
336 kfree(lgr);
337}
338
Karsten Graul9651b932018-03-01 13:51:33 +0100339void smc_lgr_forget(struct smc_link_group *lgr)
340{
341 spin_lock_bh(&smc_lgr_list.lock);
342 /* do not use this link group for new connections */
343 if (!list_empty(&lgr->list))
344 list_del_init(&lgr->list);
345 spin_unlock_bh(&smc_lgr_list.lock);
346}
347
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100348/* terminate linkgroup abnormally */
Hans Wippelb9f227c2018-05-23 16:38:10 +0200349static void __smc_lgr_terminate(struct smc_link_group *lgr)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100350{
351 struct smc_connection *conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100352 struct smc_sock *smc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100353 struct rb_node *node;
354
Karsten Graul517c3002018-05-15 17:05:03 +0200355 if (lgr->terminating)
356 return; /* lgr already terminating */
357 lgr->terminating = 1;
Hans Wippelb9f227c2018-05-23 16:38:10 +0200358 if (!list_empty(&lgr->list)) /* forget lgr */
359 list_del_init(&lgr->list);
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 Wippelb9f227c2018-05-23 16:38:10 +0200381void smc_lgr_terminate(struct smc_link_group *lgr)
382{
383 spin_lock_bh(&smc_lgr_list.lock);
384 __smc_lgr_terminate(lgr);
385 spin_unlock_bh(&smc_lgr_list.lock);
386}
387
Hans Wippel9fda3512018-05-18 09:34:11 +0200388/* Called when IB port is terminated */
389void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport)
390{
391 struct smc_link_group *lgr, *l;
392
Hans Wippelb9f227c2018-05-23 16:38:10 +0200393 spin_lock_bh(&smc_lgr_list.lock);
Hans Wippel9fda3512018-05-18 09:34:11 +0200394 list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) {
395 if (lgr->lnk[SMC_SINGLE_LINK].smcibdev == smcibdev &&
396 lgr->lnk[SMC_SINGLE_LINK].ibport == ibport)
Hans Wippelb9f227c2018-05-23 16:38:10 +0200397 __smc_lgr_terminate(lgr);
Hans Wippel9fda3512018-05-18 09:34:11 +0200398 }
Hans Wippelb9f227c2018-05-23 16:38:10 +0200399 spin_unlock_bh(&smc_lgr_list.lock);
Hans Wippel9fda3512018-05-18 09:34:11 +0200400}
401
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100402/* Determine vlan of internal TCP socket.
403 * @vlan_id: address to store the determined vlan id into
404 */
405static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
406{
407 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Ursula Brauncb9d43f2018-05-02 16:56:47 +0200408 struct net_device *ndev;
409 int i, nest_lvl, rc = 0;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100410
411 *vlan_id = 0;
412 if (!dst) {
413 rc = -ENOTCONN;
414 goto out;
415 }
416 if (!dst->dev) {
417 rc = -ENODEV;
418 goto out_rel;
419 }
420
Ursula Brauncb9d43f2018-05-02 16:56:47 +0200421 ndev = dst->dev;
422 if (is_vlan_dev(ndev)) {
423 *vlan_id = vlan_dev_vlan_id(ndev);
424 goto out_rel;
425 }
426
427 rtnl_lock();
428 nest_lvl = dev_get_nest_level(ndev);
429 for (i = 0; i < nest_lvl; i++) {
430 struct list_head *lower = &ndev->adj_list.lower;
431
432 if (list_empty(lower))
433 break;
434 lower = lower->next;
435 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
436 if (is_vlan_dev(ndev)) {
437 *vlan_id = vlan_dev_vlan_id(ndev);
438 break;
439 }
440 }
441 rtnl_unlock();
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100442
443out_rel:
444 dst_release(dst);
445out:
446 return rc;
447}
448
449/* determine the link gid matching the vlan id of the link group */
450static int smc_link_determine_gid(struct smc_link_group *lgr)
451{
452 struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
453 struct ib_gid_attr gattr;
454 union ib_gid gid;
455 int i;
456
457 if (!lgr->vlan_id) {
458 lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1];
459 return 0;
460 }
461
462 for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len;
463 i++) {
464 if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid,
465 &gattr))
466 continue;
Ursula Braun43e2ada2017-10-11 13:47:23 +0200467 if (gattr.ndev) {
468 if (is_vlan_dev(gattr.ndev) &&
469 vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) {
470 lnk->gid = gid;
471 dev_put(gattr.ndev);
472 return 0;
473 }
474 dev_put(gattr.ndev);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100475 }
476 }
477 return -ENODEV;
478}
479
480/* create a new SMC connection (and a new link group if necessary) */
Karsten Graulbe6d4672018-03-01 13:51:28 +0100481int smc_conn_create(struct smc_sock *smc,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100482 struct smc_ib_device *smcibdev, u8 ibport,
483 struct smc_clc_msg_local *lcl, int srv_first_contact)
484{
485 struct smc_connection *conn = &smc->conn;
Hans Wippel8437bda2018-05-18 09:34:16 +0200486 int local_contact = SMC_FIRST_CONTACT;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100487 struct smc_link_group *lgr;
488 unsigned short vlan_id;
489 enum smc_lgr_role role;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100490 int rc = 0;
491
492 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
493 rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
494 if (rc)
495 return rc;
496
497 if ((role == SMC_CLNT) && srv_first_contact)
498 /* create new link group as well */
499 goto create;
500
501 /* determine if an existing link group can be reused */
502 spin_lock_bh(&smc_lgr_list.lock);
503 list_for_each_entry(lgr, &smc_lgr_list.list, list) {
504 write_lock_bh(&lgr->conns_lock);
505 if (!memcmp(lgr->peer_systemid, lcl->id_for_peer,
506 SMC_SYSTEMID_LEN) &&
507 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
508 SMC_GID_SIZE) &&
509 !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
510 sizeof(lcl->mac)) &&
511 !lgr->sync_err &&
512 (lgr->role == role) &&
Ursula Brauncd6851f2017-01-09 16:55:18 +0100513 (lgr->vlan_id == vlan_id) &&
514 ((role == SMC_CLNT) ||
515 (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100516 /* link group found */
517 local_contact = SMC_REUSE_CONTACT;
518 conn->lgr = lgr;
519 smc_lgr_register_conn(conn); /* add smc conn to lgr */
520 write_unlock_bh(&lgr->conns_lock);
521 break;
522 }
523 write_unlock_bh(&lgr->conns_lock);
524 }
525 spin_unlock_bh(&smc_lgr_list.lock);
526
527 if (role == SMC_CLNT && !srv_first_contact &&
528 (local_contact == SMC_FIRST_CONTACT)) {
529 /* Server reuses a link group, but Client wants to start
530 * a new one
531 * send out_of_sync decline, reason synchr. error
532 */
533 return -ENOLINK;
534 }
535
536create:
537 if (local_contact == SMC_FIRST_CONTACT) {
Karsten Graulbe6d4672018-03-01 13:51:28 +0100538 rc = smc_lgr_create(smc, smcibdev, ibport,
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100539 lcl->id_for_peer, vlan_id);
540 if (rc)
541 goto out;
542 smc_lgr_register_conn(conn); /* add smc conn to lgr */
543 rc = smc_link_determine_gid(conn->lgr);
544 }
Ursula Braun5f083182017-01-09 16:55:22 +0100545 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
Karsten Graulcbba07a2018-02-28 12:44:07 +0100546 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200547 conn->urg_state = SMC_URG_READ;
Ursula Braun5f083182017-01-09 16:55:22 +0100548#ifndef KERNEL_HAS_ATOMIC64
549 spin_lock_init(&conn->acurs_lock);
550#endif
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100551
552out:
553 return rc ? rc : local_contact;
554}
Ursula Brauncd6851f2017-01-09 16:55:18 +0100555
Hans Wippel2f6beca2018-05-18 09:34:14 +0200556/* convert the RMB size into the compressed notation - minimum 16K.
557 * In contrast to plain ilog2, this rounds towards the next power of 2,
558 * so the socket application gets at least its desired sndbuf / rcvbuf size.
559 */
560static u8 smc_compress_bufsize(int size)
561{
562 u8 compressed;
563
564 if (size <= SMC_BUF_MIN_SIZE)
565 return 0;
566
567 size = (size - 1) >> 14;
568 compressed = ilog2(size) + 1;
569 if (compressed >= SMC_RMBE_SIZES)
570 compressed = SMC_RMBE_SIZES - 1;
571 return compressed;
572}
573
574/* convert the RMB size from compressed notation into integer */
575int smc_uncompress_bufsize(u8 compressed)
576{
577 u32 size;
578
579 size = 0x00000001 << (((int)compressed) + 14);
580 return (int)size;
581}
582
Ursula Braun3e034722017-07-28 13:56:20 +0200583/* try to reuse a sndbuf or rmb description slot for a certain
584 * buffer size; if not available, return NULL
Ursula Brauncd6851f2017-01-09 16:55:18 +0100585 */
Hans Wippel8437bda2018-05-18 09:34:16 +0200586static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
587 rwlock_t *lock,
588 struct list_head *buf_list)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100589{
Ursula Braun3e034722017-07-28 13:56:20 +0200590 struct smc_buf_desc *buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100591
Ursula Braun3e034722017-07-28 13:56:20 +0200592 read_lock_bh(lock);
593 list_for_each_entry(buf_slot, buf_list, list) {
594 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
595 read_unlock_bh(lock);
596 return buf_slot;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100597 }
598 }
Ursula Braun3e034722017-07-28 13:56:20 +0200599 read_unlock_bh(lock);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100600 return NULL;
601}
602
Ursula Braun952310c2017-01-09 16:55:24 +0100603/* one of the conditions for announcing a receiver's current window size is
604 * that it "results in a minimum increase in the window size of 10% of the
605 * receive buffer space" [RFC7609]
606 */
607static inline int smc_rmb_wnd_update_limit(int rmbe_size)
608{
609 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
610}
611
Ursula Braunb33982c2017-07-28 13:56:21 +0200612static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr,
613 bool is_rmb, int bufsize)
614{
615 struct smc_buf_desc *buf_desc;
616 struct smc_link *lnk;
617 int rc;
618
619 /* try to alloc a new buffer */
620 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
621 if (!buf_desc)
622 return ERR_PTR(-ENOMEM);
623
Stefan Raspl2ef4f272018-05-03 18:12:38 +0200624 buf_desc->order = get_order(bufsize);
625 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
626 __GFP_NOMEMALLOC | __GFP_COMP |
627 __GFP_NORETRY | __GFP_ZERO,
628 buf_desc->order);
629 if (!buf_desc->pages) {
Ursula Braunb33982c2017-07-28 13:56:21 +0200630 kfree(buf_desc);
631 return ERR_PTR(-EAGAIN);
632 }
Stefan Raspl2ef4f272018-05-03 18:12:38 +0200633 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
Ursula Braunb33982c2017-07-28 13:56:21 +0200634
635 /* build the sg table from the pages */
636 lnk = &lgr->lnk[SMC_SINGLE_LINK];
637 rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
638 GFP_KERNEL);
639 if (rc) {
Hans Wippel6511aad2018-05-18 09:34:17 +0200640 smc_buf_free(lgr, is_rmb, buf_desc);
Ursula Braunb33982c2017-07-28 13:56:21 +0200641 return ERR_PTR(rc);
642 }
643 sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
644 buf_desc->cpu_addr, bufsize);
645
646 /* map sg table to DMA address */
647 rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
648 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
649 /* SMC protocol depends on mapping to one DMA address only */
650 if (rc != 1) {
Hans Wippel6511aad2018-05-18 09:34:17 +0200651 smc_buf_free(lgr, is_rmb, buf_desc);
Ursula Braunb33982c2017-07-28 13:56:21 +0200652 return ERR_PTR(-EAGAIN);
653 }
654
655 /* create a new memory region for the RMB */
656 if (is_rmb) {
657 rc = smc_ib_get_memory_region(lnk->roce_pd,
658 IB_ACCESS_REMOTE_WRITE |
659 IB_ACCESS_LOCAL_WRITE,
660 buf_desc);
661 if (rc) {
Hans Wippel6511aad2018-05-18 09:34:17 +0200662 smc_buf_free(lgr, is_rmb, buf_desc);
Ursula Braunb33982c2017-07-28 13:56:21 +0200663 return ERR_PTR(rc);
664 }
665 }
666
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200667 buf_desc->len = bufsize;
Ursula Braunb33982c2017-07-28 13:56:21 +0200668 return buf_desc;
669}
670
Ursula Braun3e034722017-07-28 13:56:20 +0200671static int __smc_buf_create(struct smc_sock *smc, bool is_rmb)
Ursula Brauncd6851f2017-01-09 16:55:18 +0100672{
Hans Wippel8437bda2018-05-18 09:34:16 +0200673 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100674 struct smc_connection *conn = &smc->conn;
675 struct smc_link_group *lgr = conn->lgr;
Ursula Braun3e034722017-07-28 13:56:20 +0200676 struct list_head *buf_list;
Ursula Braunc45abf32017-07-28 13:56:14 +0200677 int bufsize, bufsize_short;
Ursula Braun3e034722017-07-28 13:56:20 +0200678 int sk_buf_size;
679 rwlock_t *lock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100680
Ursula Braun3e034722017-07-28 13:56:20 +0200681 if (is_rmb)
682 /* use socket recv buffer size (w/o overhead) as start value */
683 sk_buf_size = smc->sk.sk_rcvbuf / 2;
684 else
685 /* use socket send buffer size (w/o overhead) as start value */
686 sk_buf_size = smc->sk.sk_sndbuf / 2;
687
Ursula Braun4e1061f2017-11-21 13:23:53 +0100688 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
Ursula Braunc45abf32017-07-28 13:56:14 +0200689 bufsize_short >= 0; bufsize_short--) {
Ursula Braun3e034722017-07-28 13:56:20 +0200690
691 if (is_rmb) {
692 lock = &lgr->rmbs_lock;
693 buf_list = &lgr->rmbs[bufsize_short];
694 } else {
695 lock = &lgr->sndbufs_lock;
696 buf_list = &lgr->sndbufs[bufsize_short];
697 }
Ursula Braunc45abf32017-07-28 13:56:14 +0200698 bufsize = smc_uncompress_bufsize(bufsize_short);
Ursula Braun9d8fb612017-07-28 13:56:19 +0200699 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
700 continue;
701
Ursula Braun3e034722017-07-28 13:56:20 +0200702 /* check for reusable slot in the link group */
Hans Wippel8437bda2018-05-18 09:34:16 +0200703 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
Ursula Braun3e034722017-07-28 13:56:20 +0200704 if (buf_desc) {
705 memset(buf_desc->cpu_addr, 0, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100706 break; /* found reusable slot */
707 }
Ursula Braun3e034722017-07-28 13:56:20 +0200708
Ursula Braunb33982c2017-07-28 13:56:21 +0200709 buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize);
710 if (PTR_ERR(buf_desc) == -ENOMEM)
711 break;
712 if (IS_ERR(buf_desc))
Ursula Brauncd6851f2017-01-09 16:55:18 +0100713 continue;
Ursula Braun3e034722017-07-28 13:56:20 +0200714
715 buf_desc->used = 1;
716 write_lock_bh(lock);
717 list_add(&buf_desc->list, buf_list);
718 write_unlock_bh(lock);
719 break; /* found */
Ursula Brauncd6851f2017-01-09 16:55:18 +0100720 }
Ursula Braun3e034722017-07-28 13:56:20 +0200721
Ursula Braunb33982c2017-07-28 13:56:21 +0200722 if (IS_ERR(buf_desc))
Ursula Braun3e034722017-07-28 13:56:20 +0200723 return -ENOMEM;
724
725 if (is_rmb) {
726 conn->rmb_desc = buf_desc;
Ursula Braunc45abf32017-07-28 13:56:14 +0200727 conn->rmbe_size_short = bufsize_short;
728 smc->sk.sk_rcvbuf = bufsize * 2;
Ursula Braun5f083182017-01-09 16:55:22 +0100729 atomic_set(&conn->bytes_to_rcv, 0);
Ursula Braunc45abf32017-07-28 13:56:14 +0200730 conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100731 } else {
Ursula Braun3e034722017-07-28 13:56:20 +0200732 conn->sndbuf_desc = buf_desc;
Ursula Braun3e034722017-07-28 13:56:20 +0200733 smc->sk.sk_sndbuf = bufsize * 2;
734 atomic_set(&conn->sndbuf_space, bufsize);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100735 }
Ursula Braun3e034722017-07-28 13:56:20 +0200736 return 0;
737}
738
Ursula Braun10428dd2017-07-28 13:56:22 +0200739void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
740{
741 struct smc_link_group *lgr = conn->lgr;
742
743 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
744 conn->sndbuf_desc, DMA_TO_DEVICE);
745}
746
747void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
748{
749 struct smc_link_group *lgr = conn->lgr;
750
751 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
752 conn->sndbuf_desc, DMA_TO_DEVICE);
753}
754
755void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
756{
757 struct smc_link_group *lgr = conn->lgr;
758
759 smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
760 conn->rmb_desc, DMA_FROM_DEVICE);
761}
762
763void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
764{
765 struct smc_link_group *lgr = conn->lgr;
766
767 smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
768 conn->rmb_desc, DMA_FROM_DEVICE);
769}
770
Ursula Braun3e034722017-07-28 13:56:20 +0200771/* create the send and receive buffer for an SMC socket;
772 * receive buffers are called RMBs;
773 * (even though the SMC protocol allows more than one RMB-element per RMB,
774 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
775 * extra RMB for every connection in a link group
776 */
777int smc_buf_create(struct smc_sock *smc)
778{
779 int rc;
780
781 /* create send buffer */
782 rc = __smc_buf_create(smc, false);
783 if (rc)
784 return rc;
785 /* create rmb */
786 rc = __smc_buf_create(smc, true);
787 if (rc)
Hans Wippel6511aad2018-05-18 09:34:17 +0200788 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
Ursula Braun3e034722017-07-28 13:56:20 +0200789 return rc;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100790}
Ursula Braunbd4ad572017-01-09 16:55:20 +0100791
792static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
793{
794 int i;
795
796 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
797 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
798 return i;
799 }
800 return -ENOSPC;
801}
802
Karsten Graul4ed75de2018-03-01 13:51:30 +0100803/* add a new rtoken from peer */
804int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey)
Ursula Braunbd4ad572017-01-09 16:55:20 +0100805{
Karsten Graul4ed75de2018-03-01 13:51:30 +0100806 u64 dma_addr = be64_to_cpu(nw_vaddr);
807 u32 rkey = ntohl(nw_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100808 int i;
809
810 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
811 if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
Ursula Braun263eec92017-05-15 17:33:37 +0200812 (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
Ursula Braunbd4ad572017-01-09 16:55:20 +0100813 test_bit(i, lgr->rtokens_used_mask)) {
Karsten Graul4ed75de2018-03-01 13:51:30 +0100814 /* already in list */
815 return i;
816 }
817 }
818 i = smc_rmb_reserve_rtoken_idx(lgr);
819 if (i < 0)
820 return i;
821 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey;
822 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr;
823 return i;
824}
825
826/* delete an rtoken */
827int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey)
828{
829 u32 rkey = ntohl(nw_rkey);
830 int i;
831
832 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
833 if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey &&
834 test_bit(i, lgr->rtokens_used_mask)) {
835 lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0;
836 lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0;
837
838 clear_bit(i, lgr->rtokens_used_mask);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100839 return 0;
840 }
841 }
Karsten Graul4ed75de2018-03-01 13:51:30 +0100842 return -ENOENT;
843}
844
845/* save rkey and dma_addr received from peer during clc handshake */
846int smc_rmb_rtoken_handling(struct smc_connection *conn,
847 struct smc_clc_msg_accept_confirm *clc)
848{
849 conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr,
850 clc->rmb_rkey);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100851 if (conn->rtoken_idx < 0)
852 return conn->rtoken_idx;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100853 return 0;
854}
Hans Wippel9fda3512018-05-18 09:34:11 +0200855
856/* Called (from smc_exit) when module is removed */
857void smc_core_exit(void)
858{
859 struct smc_link_group *lgr, *lg;
860 LIST_HEAD(lgr_freeing_list);
861
862 spin_lock_bh(&smc_lgr_list.lock);
863 if (!list_empty(&smc_lgr_list.list))
864 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
865 spin_unlock_bh(&smc_lgr_list.lock);
866 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
867 list_del_init(&lgr->list);
868 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
869 cancel_delayed_work_sync(&lgr->free_work);
870 smc_lgr_free(lgr); /* free link group */
871 }
872}