blob: df5b0a6ea8488ffb53fb5a4aa5b564d557ee4400 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Link Layer Control (LLC)
6 *
Ursula Braun9bf9abe2017-01-09 16:55:21 +01007 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Klaus Wacker <Klaus.Wacker@de.ibm.com>
10 * Ursula Braun <ubraun@linux.vnet.ibm.com>
11 */
12
13#include <net/tcp.h>
14#include <rdma/ib_verbs.h>
15
16#include "smc.h"
17#include "smc_core.h"
18#include "smc_clc.h"
19#include "smc_llc.h"
Karsten Graul336ba092020-05-03 14:38:40 +020020#include "smc_pnet.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010021
Stefan Raspl0f627122018-03-01 13:51:26 +010022#define SMC_LLC_DATA_LEN 40
23
24struct smc_llc_hdr {
25 struct smc_wr_rx_hdr common;
26 u8 length; /* 44 */
Karsten Graul52bedf32018-03-01 13:51:32 +010027#if defined(__BIG_ENDIAN_BITFIELD)
28 u8 reserved:4,
29 add_link_rej_rsn:4;
30#elif defined(__LITTLE_ENDIAN_BITFIELD)
31 u8 add_link_rej_rsn:4,
32 reserved:4;
33#endif
Stefan Raspl0f627122018-03-01 13:51:26 +010034 u8 flags;
35};
36
Karsten Graul75d320d2018-03-01 13:51:31 +010037#define SMC_LLC_FLAG_NO_RMBE_EYEC 0x03
38
Stefan Raspl0f627122018-03-01 13:51:26 +010039struct smc_llc_msg_confirm_link { /* type 0x01 */
40 struct smc_llc_hdr hd;
41 u8 sender_mac[ETH_ALEN];
42 u8 sender_gid[SMC_GID_SIZE];
43 u8 sender_qp_num[3];
44 u8 link_num;
45 u8 link_uid[SMC_LGR_ID_SIZE];
46 u8 max_links;
47 u8 reserved[9];
48};
49
Karsten Graul52bedf32018-03-01 13:51:32 +010050#define SMC_LLC_FLAG_ADD_LNK_REJ 0x40
51#define SMC_LLC_REJ_RSN_NO_ALT_PATH 1
52
53#define SMC_LLC_ADD_LNK_MAX_LINKS 2
54
55struct smc_llc_msg_add_link { /* type 0x02 */
56 struct smc_llc_hdr hd;
57 u8 sender_mac[ETH_ALEN];
58 u8 reserved2[2];
59 u8 sender_gid[SMC_GID_SIZE];
60 u8 sender_qp_num[3];
61 u8 link_num;
Karsten Graulfbed3b32020-05-01 12:48:04 +020062#if defined(__BIG_ENDIAN_BITFIELD)
63 u8 reserved3 : 4,
64 qp_mtu : 4;
65#elif defined(__LITTLE_ENDIAN_BITFIELD)
66 u8 qp_mtu : 4,
67 reserved3 : 4;
68#endif
Karsten Graul52bedf32018-03-01 13:51:32 +010069 u8 initial_psn[3];
70 u8 reserved[8];
71};
72
Karsten Graul87f88cd2020-05-03 14:38:41 +020073struct smc_llc_msg_add_link_cont_rt {
74 __be32 rmb_key;
75 __be32 rmb_key_new;
76 __be64 rmb_vaddr_new;
77};
78
79#define SMC_LLC_RKEYS_PER_CONT_MSG 2
80
81struct smc_llc_msg_add_link_cont { /* type 0x03 */
82 struct smc_llc_hdr hd;
83 u8 link_num;
84 u8 num_rkeys;
85 u8 reserved2[2];
86 struct smc_llc_msg_add_link_cont_rt rt[SMC_LLC_RKEYS_PER_CONT_MSG];
87 u8 reserved[4];
88} __packed; /* format defined in RFC7609 */
89
Karsten Graul52bedf32018-03-01 13:51:32 +010090#define SMC_LLC_FLAG_DEL_LINK_ALL 0x40
91#define SMC_LLC_FLAG_DEL_LINK_ORDERLY 0x20
92
93struct smc_llc_msg_del_link { /* type 0x04 */
94 struct smc_llc_hdr hd;
95 u8 link_num;
96 __be32 reason;
97 u8 reserved[35];
98} __packed; /* format defined in RFC7609 */
99
Karsten Graul313164d2018-03-01 13:51:29 +0100100struct smc_llc_msg_test_link { /* type 0x07 */
101 struct smc_llc_hdr hd;
102 u8 user_data[16];
103 u8 reserved[24];
104};
105
Karsten Graul4ed75de2018-03-01 13:51:30 +0100106struct smc_rmb_rtoken {
107 union {
108 u8 num_rkeys; /* first rtoken byte of CONFIRM LINK msg */
109 /* is actually the num of rtokens, first */
110 /* rtoken is always for the current link */
111 u8 link_id; /* link id of the rtoken */
112 };
113 __be32 rmb_key;
114 __be64 rmb_vaddr;
115} __packed; /* format defined in RFC7609 */
116
117#define SMC_LLC_RKEYS_PER_MSG 3
118
119struct smc_llc_msg_confirm_rkey { /* type 0x06 */
120 struct smc_llc_hdr hd;
121 struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
122 u8 reserved;
123};
124
Karsten Graul4ed75de2018-03-01 13:51:30 +0100125#define SMC_LLC_DEL_RKEY_MAX 8
Karsten Graul3bc67e02020-04-30 15:55:48 +0200126#define SMC_LLC_FLAG_RKEY_RETRY 0x10
Karsten Graul4ed75de2018-03-01 13:51:30 +0100127#define SMC_LLC_FLAG_RKEY_NEG 0x20
128
129struct smc_llc_msg_delete_rkey { /* type 0x09 */
130 struct smc_llc_hdr hd;
131 u8 num_rkeys;
132 u8 err_mask;
133 u8 reserved[2];
134 __be32 rkey[8];
135 u8 reserved2[4];
136};
137
Stefan Raspl0f627122018-03-01 13:51:26 +0100138union smc_llc_msg {
139 struct smc_llc_msg_confirm_link confirm_link;
Karsten Graul52bedf32018-03-01 13:51:32 +0100140 struct smc_llc_msg_add_link add_link;
Karsten Graul87f88cd2020-05-03 14:38:41 +0200141 struct smc_llc_msg_add_link_cont add_link_cont;
Karsten Graul52bedf32018-03-01 13:51:32 +0100142 struct smc_llc_msg_del_link delete_link;
Karsten Graul4ed75de2018-03-01 13:51:30 +0100143
144 struct smc_llc_msg_confirm_rkey confirm_rkey;
Karsten Graul4ed75de2018-03-01 13:51:30 +0100145 struct smc_llc_msg_delete_rkey delete_rkey;
146
Karsten Graul313164d2018-03-01 13:51:29 +0100147 struct smc_llc_msg_test_link test_link;
Stefan Raspl0f627122018-03-01 13:51:26 +0100148 struct {
149 struct smc_llc_hdr hdr;
150 u8 data[SMC_LLC_DATA_LEN];
151 } raw;
152};
153
154#define SMC_LLC_FLAG_RESP 0x80
155
Karsten Graul6c8968c2020-04-29 17:10:46 +0200156struct smc_llc_qentry {
157 struct list_head list;
158 struct smc_link *link;
159 union smc_llc_msg msg;
160};
161
Karsten Graul4dadd152020-05-03 14:38:50 +0200162static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc);
163
Karsten Graul555da9a2020-04-30 15:55:38 +0200164struct smc_llc_qentry *smc_llc_flow_qentry_clr(struct smc_llc_flow *flow)
165{
166 struct smc_llc_qentry *qentry = flow->qentry;
167
168 flow->qentry = NULL;
169 return qentry;
170}
171
172void smc_llc_flow_qentry_del(struct smc_llc_flow *flow)
173{
174 struct smc_llc_qentry *qentry;
175
176 if (flow->qentry) {
177 qentry = flow->qentry;
178 flow->qentry = NULL;
179 kfree(qentry);
180 }
181}
182
183static inline void smc_llc_flow_qentry_set(struct smc_llc_flow *flow,
184 struct smc_llc_qentry *qentry)
185{
186 flow->qentry = qentry;
187}
188
Karsten Graul6778a6b2020-07-08 17:05:11 +0200189static void smc_llc_flow_parallel(struct smc_link_group *lgr, u8 flow_type,
190 struct smc_llc_qentry *qentry)
191{
192 u8 msg_type = qentry->msg.raw.hdr.common.type;
193
194 if ((msg_type == SMC_LLC_ADD_LINK || msg_type == SMC_LLC_DELETE_LINK) &&
195 flow_type != msg_type && !lgr->delayed_event) {
196 lgr->delayed_event = qentry;
197 return;
198 }
199 /* drop parallel or already-in-progress llc requests */
200 if (flow_type != msg_type)
201 pr_warn_once("smc: SMC-R lg %*phN dropped parallel "
202 "LLC msg: msg %d flow %d role %d\n",
203 SMC_LGR_ID_SIZE, &lgr->id,
204 qentry->msg.raw.hdr.common.type,
205 flow_type, lgr->role);
206 kfree(qentry);
207}
208
Karsten Graul555da9a2020-04-30 15:55:38 +0200209/* try to start a new llc flow, initiated by an incoming llc msg */
210static bool smc_llc_flow_start(struct smc_llc_flow *flow,
211 struct smc_llc_qentry *qentry)
212{
213 struct smc_link_group *lgr = qentry->link->lgr;
214
215 spin_lock_bh(&lgr->llc_flow_lock);
216 if (flow->type) {
217 /* a flow is already active */
Karsten Graul6778a6b2020-07-08 17:05:11 +0200218 smc_llc_flow_parallel(lgr, flow->type, qentry);
Karsten Graul555da9a2020-04-30 15:55:38 +0200219 spin_unlock_bh(&lgr->llc_flow_lock);
220 return false;
221 }
222 switch (qentry->msg.raw.hdr.common.type) {
223 case SMC_LLC_ADD_LINK:
224 flow->type = SMC_LLC_FLOW_ADD_LINK;
225 break;
226 case SMC_LLC_DELETE_LINK:
227 flow->type = SMC_LLC_FLOW_DEL_LINK;
228 break;
229 case SMC_LLC_CONFIRM_RKEY:
230 case SMC_LLC_DELETE_RKEY:
231 flow->type = SMC_LLC_FLOW_RKEY;
232 break;
233 default:
234 flow->type = SMC_LLC_FLOW_NONE;
235 }
236 if (qentry == lgr->delayed_event)
237 lgr->delayed_event = NULL;
Karsten Graul555da9a2020-04-30 15:55:38 +0200238 smc_llc_flow_qentry_set(flow, qentry);
Karsten Graul6778a6b2020-07-08 17:05:11 +0200239 spin_unlock_bh(&lgr->llc_flow_lock);
Karsten Graul555da9a2020-04-30 15:55:38 +0200240 return true;
241}
242
243/* start a new local llc flow, wait till current flow finished */
244int smc_llc_flow_initiate(struct smc_link_group *lgr,
245 enum smc_llc_flowtype type)
246{
247 enum smc_llc_flowtype allowed_remote = SMC_LLC_FLOW_NONE;
248 int rc;
249
250 /* all flows except confirm_rkey and delete_rkey are exclusive,
251 * confirm/delete rkey flows can run concurrently (local and remote)
252 */
253 if (type == SMC_LLC_FLOW_RKEY)
254 allowed_remote = SMC_LLC_FLOW_RKEY;
255again:
256 if (list_empty(&lgr->list))
257 return -ENODEV;
258 spin_lock_bh(&lgr->llc_flow_lock);
259 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
260 (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
261 lgr->llc_flow_rmt.type == allowed_remote)) {
262 lgr->llc_flow_lcl.type = type;
263 spin_unlock_bh(&lgr->llc_flow_lock);
264 return 0;
265 }
266 spin_unlock_bh(&lgr->llc_flow_lock);
Karsten Graul6778a6b2020-07-08 17:05:11 +0200267 rc = wait_event_timeout(lgr->llc_flow_waiter, (list_empty(&lgr->list) ||
268 (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE &&
269 (lgr->llc_flow_rmt.type == SMC_LLC_FLOW_NONE ||
270 lgr->llc_flow_rmt.type == allowed_remote))),
271 SMC_LLC_WAIT_TIME * 10);
Karsten Graul555da9a2020-04-30 15:55:38 +0200272 if (!rc)
273 return -ETIMEDOUT;
274 goto again;
275}
276
277/* finish the current llc flow */
278void smc_llc_flow_stop(struct smc_link_group *lgr, struct smc_llc_flow *flow)
279{
280 spin_lock_bh(&lgr->llc_flow_lock);
281 memset(flow, 0, sizeof(*flow));
282 flow->type = SMC_LLC_FLOW_NONE;
283 spin_unlock_bh(&lgr->llc_flow_lock);
284 if (!list_empty(&lgr->list) && lgr->delayed_event &&
285 flow == &lgr->llc_flow_lcl)
286 schedule_work(&lgr->llc_event_work);
287 else
Karsten Graul6778a6b2020-07-08 17:05:11 +0200288 wake_up(&lgr->llc_flow_waiter);
Karsten Graul555da9a2020-04-30 15:55:38 +0200289}
290
291/* lnk is optional and used for early wakeup when link goes down, useful in
292 * cases where we wait for a response on the link after we sent a request
293 */
294struct smc_llc_qentry *smc_llc_wait(struct smc_link_group *lgr,
295 struct smc_link *lnk,
296 int time_out, u8 exp_msg)
297{
298 struct smc_llc_flow *flow = &lgr->llc_flow_lcl;
Karsten Graul6778a6b2020-07-08 17:05:11 +0200299 u8 rcv_msg;
Karsten Graul555da9a2020-04-30 15:55:38 +0200300
Karsten Graul6778a6b2020-07-08 17:05:11 +0200301 wait_event_timeout(lgr->llc_msg_waiter,
302 (flow->qentry ||
303 (lnk && !smc_link_usable(lnk)) ||
304 list_empty(&lgr->list)),
305 time_out);
Karsten Graul555da9a2020-04-30 15:55:38 +0200306 if (!flow->qentry ||
307 (lnk && !smc_link_usable(lnk)) || list_empty(&lgr->list)) {
308 smc_llc_flow_qentry_del(flow);
309 goto out;
310 }
Karsten Graul6778a6b2020-07-08 17:05:11 +0200311 rcv_msg = flow->qentry->msg.raw.hdr.common.type;
312 if (exp_msg && rcv_msg != exp_msg) {
Karsten Graul555da9a2020-04-30 15:55:38 +0200313 if (exp_msg == SMC_LLC_ADD_LINK &&
Karsten Graul6778a6b2020-07-08 17:05:11 +0200314 rcv_msg == SMC_LLC_DELETE_LINK) {
Karsten Graul555da9a2020-04-30 15:55:38 +0200315 /* flow_start will delay the unexpected msg */
316 smc_llc_flow_start(&lgr->llc_flow_lcl,
317 smc_llc_flow_qentry_clr(flow));
318 return NULL;
319 }
Karsten Graul6778a6b2020-07-08 17:05:11 +0200320 pr_warn_once("smc: SMC-R lg %*phN dropped unexpected LLC msg: "
321 "msg %d exp %d flow %d role %d flags %x\n",
322 SMC_LGR_ID_SIZE, &lgr->id, rcv_msg, exp_msg,
323 flow->type, lgr->role,
324 flow->qentry->msg.raw.hdr.flags);
Karsten Graul555da9a2020-04-30 15:55:38 +0200325 smc_llc_flow_qentry_del(flow);
326 }
327out:
328 return flow->qentry;
329}
330
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100331/********************************** send *************************************/
332
333struct smc_llc_tx_pend {
334};
335
336/* handler for send/transmission completion of an LLC msg */
337static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
338 struct smc_link *link,
339 enum ib_wc_status wc_status)
340{
341 /* future work: handle wc_status error for recovery and failover */
342}
343
344/**
345 * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
346 * @link: Pointer to SMC link used for sending LLC control message.
347 * @wr_buf: Out variable returning pointer to work request payload buffer.
348 * @pend: Out variable returning pointer to private pending WR tracking.
349 * It's the context the transmit complete handler will get.
350 *
351 * Reserves and pre-fills an entry for a pending work request send/tx.
352 * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
353 * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
354 *
355 * Return: 0 on success, otherwise an error value.
356 */
357static int smc_llc_add_pending_send(struct smc_link *link,
358 struct smc_wr_buf **wr_buf,
359 struct smc_wr_tx_pend_priv **pend)
360{
361 int rc;
362
Ursula Braunad6f3172019-02-04 13:44:44 +0100363 rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, NULL,
364 pend);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100365 if (rc < 0)
366 return rc;
367 BUILD_BUG_ON_MSG(
368 sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
369 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
370 BUILD_BUG_ON_MSG(
371 sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
372 "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_llc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()");
373 BUILD_BUG_ON_MSG(
374 sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
375 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
376 return 0;
377}
378
379/* high-level API to send LLC confirm link */
Ursula Braun947541f2018-07-25 16:35:30 +0200380int smc_llc_send_confirm_link(struct smc_link *link,
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381 enum smc_llc_reqresp reqresp)
382{
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383 struct smc_llc_msg_confirm_link *confllc;
384 struct smc_wr_tx_pend_priv *pend;
385 struct smc_wr_buf *wr_buf;
386 int rc;
387
388 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
389 if (rc)
390 return rc;
391 confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
392 memset(confllc, 0, sizeof(*confllc));
393 confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
394 confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
Karsten Graul75d320d2018-03-01 13:51:31 +0100395 confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100396 if (reqresp == SMC_LLC_RESP)
397 confllc->hd.flags |= SMC_LLC_FLAG_RESP;
Ursula Braun947541f2018-07-25 16:35:30 +0200398 memcpy(confllc->sender_mac, link->smcibdev->mac[link->ibport - 1],
399 ETH_ALEN);
Ursula Braun7005ada2018-07-25 16:35:31 +0200400 memcpy(confllc->sender_gid, link->gid, SMC_GID_SIZE);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100401 hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
Karsten Graul2be922f2018-02-28 12:44:08 +0100402 confllc->link_num = link->link_id;
Karsten Graul45fa8da2020-05-04 14:18:47 +0200403 memcpy(confllc->link_uid, link->link_uid, SMC_LGR_ID_SIZE);
Karsten Graulb1570a82020-05-03 14:38:42 +0200404 confllc->max_links = SMC_LLC_ADD_LNK_MAX_LINKS;
Karsten Graul52bedf32018-03-01 13:51:32 +0100405 /* send llc message */
406 rc = smc_wr_tx_send(link, pend);
407 return rc;
408}
409
Karsten Graul44aa81c2018-05-15 17:04:55 +0200410/* send LLC confirm rkey request */
Karsten Graul3d88a212020-04-30 15:55:44 +0200411static int smc_llc_send_confirm_rkey(struct smc_link *send_link,
Karsten Graul44aa81c2018-05-15 17:04:55 +0200412 struct smc_buf_desc *rmb_desc)
413{
414 struct smc_llc_msg_confirm_rkey *rkeyllc;
415 struct smc_wr_tx_pend_priv *pend;
416 struct smc_wr_buf *wr_buf;
Karsten Graul3d88a212020-04-30 15:55:44 +0200417 struct smc_link *link;
418 int i, rc, rtok_ix;
Karsten Graul44aa81c2018-05-15 17:04:55 +0200419
Karsten Graul3d88a212020-04-30 15:55:44 +0200420 rc = smc_llc_add_pending_send(send_link, &wr_buf, &pend);
Karsten Graul44aa81c2018-05-15 17:04:55 +0200421 if (rc)
422 return rc;
423 rkeyllc = (struct smc_llc_msg_confirm_rkey *)wr_buf;
424 memset(rkeyllc, 0, sizeof(*rkeyllc));
425 rkeyllc->hd.common.type = SMC_LLC_CONFIRM_RKEY;
426 rkeyllc->hd.length = sizeof(struct smc_llc_msg_confirm_rkey);
Karsten Graul3d88a212020-04-30 15:55:44 +0200427
428 rtok_ix = 1;
429 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
430 link = &send_link->lgr->lnk[i];
Karsten Graul741a49a2020-07-18 15:06:16 +0200431 if (smc_link_active(link) && link != send_link) {
Karsten Graul3d88a212020-04-30 15:55:44 +0200432 rkeyllc->rtoken[rtok_ix].link_id = link->link_id;
433 rkeyllc->rtoken[rtok_ix].rmb_key =
434 htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
435 rkeyllc->rtoken[rtok_ix].rmb_vaddr = cpu_to_be64(
436 (u64)sg_dma_address(
437 rmb_desc->sgt[link->link_idx].sgl));
438 rtok_ix++;
439 }
440 }
441 /* rkey of send_link is in rtoken[0] */
442 rkeyllc->rtoken[0].num_rkeys = rtok_ix - 1;
Karsten Graul44aa81c2018-05-15 17:04:55 +0200443 rkeyllc->rtoken[0].rmb_key =
Karsten Graul3d88a212020-04-30 15:55:44 +0200444 htonl(rmb_desc->mr_rx[send_link->link_idx]->rkey);
Karsten Graul44aa81c2018-05-15 17:04:55 +0200445 rkeyllc->rtoken[0].rmb_vaddr = cpu_to_be64(
Karsten Graul3d88a212020-04-30 15:55:44 +0200446 (u64)sg_dma_address(rmb_desc->sgt[send_link->link_idx].sgl));
Karsten Graul44aa81c2018-05-15 17:04:55 +0200447 /* send llc message */
Karsten Graul3d88a212020-04-30 15:55:44 +0200448 rc = smc_wr_tx_send(send_link, pend);
Karsten Graul44aa81c2018-05-15 17:04:55 +0200449 return rc;
450}
451
Karsten Graul60e03c62018-11-22 10:26:42 +0100452/* send LLC delete rkey request */
453static int smc_llc_send_delete_rkey(struct smc_link *link,
454 struct smc_buf_desc *rmb_desc)
455{
456 struct smc_llc_msg_delete_rkey *rkeyllc;
457 struct smc_wr_tx_pend_priv *pend;
458 struct smc_wr_buf *wr_buf;
459 int rc;
460
461 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
462 if (rc)
463 return rc;
464 rkeyllc = (struct smc_llc_msg_delete_rkey *)wr_buf;
465 memset(rkeyllc, 0, sizeof(*rkeyllc));
466 rkeyllc->hd.common.type = SMC_LLC_DELETE_RKEY;
467 rkeyllc->hd.length = sizeof(struct smc_llc_msg_delete_rkey);
468 rkeyllc->num_rkeys = 1;
Karsten Graul387707f2020-04-29 17:10:40 +0200469 rkeyllc->rkey[0] = htonl(rmb_desc->mr_rx[link->link_idx]->rkey);
Karsten Graul60e03c62018-11-22 10:26:42 +0100470 /* send llc message */
471 rc = smc_wr_tx_send(link, pend);
472 return rc;
473}
474
Karsten Graul52bedf32018-03-01 13:51:32 +0100475/* send ADD LINK request or response */
Ursula Braun7005ada2018-07-25 16:35:31 +0200476int smc_llc_send_add_link(struct smc_link *link, u8 mac[], u8 gid[],
Karsten Graulfbed3b32020-05-01 12:48:04 +0200477 struct smc_link *link_new,
Karsten Graul52bedf32018-03-01 13:51:32 +0100478 enum smc_llc_reqresp reqresp)
479{
480 struct smc_llc_msg_add_link *addllc;
481 struct smc_wr_tx_pend_priv *pend;
482 struct smc_wr_buf *wr_buf;
483 int rc;
484
485 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
486 if (rc)
487 return rc;
488 addllc = (struct smc_llc_msg_add_link *)wr_buf;
Karsten Graulfbed3b32020-05-01 12:48:04 +0200489
490 memset(addllc, 0, sizeof(*addllc));
491 addllc->hd.common.type = SMC_LLC_ADD_LINK;
492 addllc->hd.length = sizeof(struct smc_llc_msg_add_link);
493 if (reqresp == SMC_LLC_RESP)
494 addllc->hd.flags |= SMC_LLC_FLAG_RESP;
495 memcpy(addllc->sender_mac, mac, ETH_ALEN);
496 memcpy(addllc->sender_gid, gid, SMC_GID_SIZE);
497 if (link_new) {
498 addllc->link_num = link_new->link_id;
499 hton24(addllc->sender_qp_num, link_new->roce_qp->qp_num);
500 hton24(addllc->initial_psn, link_new->psn_initial);
501 if (reqresp == SMC_LLC_REQ)
502 addllc->qp_mtu = link_new->path_mtu;
503 else
504 addllc->qp_mtu = min(link_new->path_mtu,
505 link_new->peer_mtu);
506 }
Karsten Graul52bedf32018-03-01 13:51:32 +0100507 /* send llc message */
508 rc = smc_wr_tx_send(link, pend);
509 return rc;
510}
511
512/* send DELETE LINK request or response */
Karsten Graulfbed3b32020-05-01 12:48:04 +0200513int smc_llc_send_delete_link(struct smc_link *link, u8 link_del_id,
514 enum smc_llc_reqresp reqresp, bool orderly,
515 u32 reason)
Karsten Graul52bedf32018-03-01 13:51:32 +0100516{
517 struct smc_llc_msg_del_link *delllc;
518 struct smc_wr_tx_pend_priv *pend;
519 struct smc_wr_buf *wr_buf;
520 int rc;
521
522 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
523 if (rc)
524 return rc;
525 delllc = (struct smc_llc_msg_del_link *)wr_buf;
Karsten Graulfbed3b32020-05-01 12:48:04 +0200526
527 memset(delllc, 0, sizeof(*delllc));
528 delllc->hd.common.type = SMC_LLC_DELETE_LINK;
529 delllc->hd.length = sizeof(struct smc_llc_msg_del_link);
530 if (reqresp == SMC_LLC_RESP)
531 delllc->hd.flags |= SMC_LLC_FLAG_RESP;
532 if (orderly)
533 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
534 if (link_del_id)
535 delllc->link_num = link_del_id;
536 else
537 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
538 delllc->reason = htonl(reason);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100539 /* send llc message */
540 rc = smc_wr_tx_send(link, pend);
541 return rc;
542}
543
Karsten Grauld97935f2018-05-15 17:04:57 +0200544/* send LLC test link request */
545static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
Karsten Graul313164d2018-03-01 13:51:29 +0100546{
547 struct smc_llc_msg_test_link *testllc;
548 struct smc_wr_tx_pend_priv *pend;
549 struct smc_wr_buf *wr_buf;
550 int rc;
551
552 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
553 if (rc)
554 return rc;
555 testllc = (struct smc_llc_msg_test_link *)wr_buf;
556 memset(testllc, 0, sizeof(*testllc));
557 testllc->hd.common.type = SMC_LLC_TEST_LINK;
558 testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
Karsten Graul313164d2018-03-01 13:51:29 +0100559 memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
560 /* send llc message */
561 rc = smc_wr_tx_send(link, pend);
562 return rc;
563}
564
Karsten Graul6c8968c2020-04-29 17:10:46 +0200565/* schedule an llc send on link, may wait for buffers */
566static int smc_llc_send_message(struct smc_link *link, void *llcbuf)
Karsten Graul4ed75de2018-03-01 13:51:30 +0100567{
568 struct smc_wr_tx_pend_priv *pend;
569 struct smc_wr_buf *wr_buf;
570 int rc;
571
Karsten Graul6c8968c2020-04-29 17:10:46 +0200572 if (!smc_link_usable(link))
573 return -ENOLINK;
574 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
Karsten Graul4ed75de2018-03-01 13:51:30 +0100575 if (rc)
Karsten Graul6c8968c2020-04-29 17:10:46 +0200576 return rc;
577 memcpy(wr_buf, llcbuf, sizeof(union smc_llc_msg));
578 return smc_wr_tx_send(link, pend);
Karsten Graul4ed75de2018-03-01 13:51:30 +0100579}
580
Karsten Graulf3811fd2020-05-04 14:18:42 +0200581/* schedule an llc send on link, may wait for buffers,
582 * and wait for send completion notification.
583 * @return 0 on success
584 */
585static int smc_llc_send_message_wait(struct smc_link *link, void *llcbuf)
586{
587 struct smc_wr_tx_pend_priv *pend;
588 struct smc_wr_buf *wr_buf;
589 int rc;
590
591 if (!smc_link_usable(link))
592 return -ENOLINK;
593 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
594 if (rc)
595 return rc;
596 memcpy(wr_buf, llcbuf, sizeof(union smc_llc_msg));
597 return smc_wr_tx_send_wait(link, pend, SMC_LLC_WAIT_TIME);
598}
599
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100600/********************************* receive ***********************************/
601
Karsten Graul336ba092020-05-03 14:38:40 +0200602static int smc_llc_alloc_alt_link(struct smc_link_group *lgr,
603 enum smc_lgr_type lgr_new_t)
604{
605 int i;
606
607 if (lgr->type == SMC_LGR_SYMMETRIC ||
608 (lgr->type != SMC_LGR_SINGLE &&
609 (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
610 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)))
611 return -EMLINK;
612
613 if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
614 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER) {
615 for (i = SMC_LINKS_PER_LGR_MAX - 1; i >= 0; i--)
616 if (lgr->lnk[i].state == SMC_LNK_UNUSED)
617 return i;
618 } else {
619 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
620 if (lgr->lnk[i].state == SMC_LNK_UNUSED)
621 return i;
622 }
623 return -EMLINK;
624}
625
Karsten Graul87f88cd2020-05-03 14:38:41 +0200626/* return first buffer from any of the next buf lists */
627static struct smc_buf_desc *_smc_llc_get_next_rmb(struct smc_link_group *lgr,
628 int *buf_lst)
629{
630 struct smc_buf_desc *buf_pos;
631
632 while (*buf_lst < SMC_RMBE_SIZES) {
633 buf_pos = list_first_entry_or_null(&lgr->rmbs[*buf_lst],
634 struct smc_buf_desc, list);
635 if (buf_pos)
636 return buf_pos;
637 (*buf_lst)++;
638 }
639 return NULL;
640}
641
642/* return next rmb from buffer lists */
643static struct smc_buf_desc *smc_llc_get_next_rmb(struct smc_link_group *lgr,
644 int *buf_lst,
645 struct smc_buf_desc *buf_pos)
646{
647 struct smc_buf_desc *buf_next;
648
649 if (!buf_pos || list_is_last(&buf_pos->list, &lgr->rmbs[*buf_lst])) {
650 (*buf_lst)++;
651 return _smc_llc_get_next_rmb(lgr, buf_lst);
652 }
653 buf_next = list_next_entry(buf_pos, list);
654 return buf_next;
655}
656
657static struct smc_buf_desc *smc_llc_get_first_rmb(struct smc_link_group *lgr,
658 int *buf_lst)
659{
660 *buf_lst = 0;
661 return smc_llc_get_next_rmb(lgr, buf_lst, NULL);
662}
663
664/* send one add_link_continue msg */
665static int smc_llc_add_link_cont(struct smc_link *link,
666 struct smc_link *link_new, u8 *num_rkeys_todo,
667 int *buf_lst, struct smc_buf_desc **buf_pos)
668{
669 struct smc_llc_msg_add_link_cont *addc_llc;
670 struct smc_link_group *lgr = link->lgr;
671 int prim_lnk_idx, lnk_idx, i, rc;
672 struct smc_wr_tx_pend_priv *pend;
673 struct smc_wr_buf *wr_buf;
674 struct smc_buf_desc *rmb;
675 u8 n;
676
677 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
678 if (rc)
679 return rc;
680 addc_llc = (struct smc_llc_msg_add_link_cont *)wr_buf;
681 memset(addc_llc, 0, sizeof(*addc_llc));
682
683 prim_lnk_idx = link->link_idx;
684 lnk_idx = link_new->link_idx;
685 addc_llc->link_num = link_new->link_id;
686 addc_llc->num_rkeys = *num_rkeys_todo;
687 n = *num_rkeys_todo;
688 for (i = 0; i < min_t(u8, n, SMC_LLC_RKEYS_PER_CONT_MSG); i++) {
689 if (!*buf_pos) {
690 addc_llc->num_rkeys = addc_llc->num_rkeys -
691 *num_rkeys_todo;
692 *num_rkeys_todo = 0;
693 break;
694 }
695 rmb = *buf_pos;
696
697 addc_llc->rt[i].rmb_key = htonl(rmb->mr_rx[prim_lnk_idx]->rkey);
698 addc_llc->rt[i].rmb_key_new = htonl(rmb->mr_rx[lnk_idx]->rkey);
699 addc_llc->rt[i].rmb_vaddr_new =
700 cpu_to_be64((u64)sg_dma_address(rmb->sgt[lnk_idx].sgl));
701
702 (*num_rkeys_todo)--;
703 *buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
704 while (*buf_pos && !(*buf_pos)->used)
705 *buf_pos = smc_llc_get_next_rmb(lgr, buf_lst, *buf_pos);
706 }
707 addc_llc->hd.common.type = SMC_LLC_ADD_LINK_CONT;
708 addc_llc->hd.length = sizeof(struct smc_llc_msg_add_link_cont);
709 if (lgr->role == SMC_CLNT)
710 addc_llc->hd.flags |= SMC_LLC_FLAG_RESP;
711 return smc_wr_tx_send(link, pend);
712}
713
714static int smc_llc_cli_rkey_exchange(struct smc_link *link,
715 struct smc_link *link_new)
716{
717 struct smc_llc_msg_add_link_cont *addc_llc;
718 struct smc_link_group *lgr = link->lgr;
719 u8 max, num_rkeys_send, num_rkeys_recv;
720 struct smc_llc_qentry *qentry;
721 struct smc_buf_desc *buf_pos;
722 int buf_lst;
723 int rc = 0;
724 int i;
725
726 mutex_lock(&lgr->rmbs_lock);
727 num_rkeys_send = lgr->conns_num;
728 buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
729 do {
730 qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_TIME,
731 SMC_LLC_ADD_LINK_CONT);
732 if (!qentry) {
733 rc = -ETIMEDOUT;
734 break;
735 }
736 addc_llc = &qentry->msg.add_link_cont;
737 num_rkeys_recv = addc_llc->num_rkeys;
738 max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
739 for (i = 0; i < max; i++) {
740 smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
741 addc_llc->rt[i].rmb_key,
742 addc_llc->rt[i].rmb_vaddr_new,
743 addc_llc->rt[i].rmb_key_new);
744 num_rkeys_recv--;
745 }
746 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
747 rc = smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
748 &buf_lst, &buf_pos);
749 if (rc)
750 break;
751 } while (num_rkeys_send || num_rkeys_recv);
752
753 mutex_unlock(&lgr->rmbs_lock);
754 return rc;
755}
756
Karsten Graul336ba092020-05-03 14:38:40 +0200757/* prepare and send an add link reject response */
758static int smc_llc_cli_add_link_reject(struct smc_llc_qentry *qentry)
759{
760 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
761 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_ADD_LNK_REJ;
762 qentry->msg.raw.hdr.add_link_rej_rsn = SMC_LLC_REJ_RSN_NO_ALT_PATH;
763 return smc_llc_send_message(qentry->link, &qentry->msg);
764}
765
Karsten Graulb1570a82020-05-03 14:38:42 +0200766static int smc_llc_cli_conf_link(struct smc_link *link,
767 struct smc_init_info *ini,
768 struct smc_link *link_new,
769 enum smc_lgr_type lgr_new_t)
770{
771 struct smc_link_group *lgr = link->lgr;
Karsten Graulb1570a82020-05-03 14:38:42 +0200772 struct smc_llc_qentry *qentry = NULL;
773 int rc = 0;
774
775 /* receive CONFIRM LINK request over RoCE fabric */
776 qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_FIRST_TIME, 0);
777 if (!qentry) {
778 rc = smc_llc_send_delete_link(link, link_new->link_id,
779 SMC_LLC_REQ, false,
780 SMC_LLC_DEL_LOST_PATH);
781 return -ENOLINK;
782 }
783 if (qentry->msg.raw.hdr.common.type != SMC_LLC_CONFIRM_LINK) {
784 /* received DELETE_LINK instead */
Karsten Graulb1570a82020-05-03 14:38:42 +0200785 qentry->msg.raw.hdr.flags |= SMC_LLC_FLAG_RESP;
786 smc_llc_send_message(link, &qentry->msg);
787 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
788 return -ENOLINK;
789 }
Karsten Graul649758f2020-05-04 14:18:48 +0200790 smc_llc_save_peer_uid(qentry);
Karsten Graulb1570a82020-05-03 14:38:42 +0200791 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
792
793 rc = smc_ib_modify_qp_rts(link_new);
794 if (rc) {
795 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
796 false, SMC_LLC_DEL_LOST_PATH);
797 return -ENOLINK;
798 }
799 smc_wr_remember_qp_attr(link_new);
800
801 rc = smcr_buf_reg_lgr(link_new);
802 if (rc) {
803 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
804 false, SMC_LLC_DEL_LOST_PATH);
805 return -ENOLINK;
806 }
807
808 /* send CONFIRM LINK response over RoCE fabric */
809 rc = smc_llc_send_confirm_link(link_new, SMC_LLC_RESP);
810 if (rc) {
811 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
812 false, SMC_LLC_DEL_LOST_PATH);
813 return -ENOLINK;
814 }
815 smc_llc_link_active(link_new);
Karsten Graulad6c1112020-05-04 14:18:44 +0200816 if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
817 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)
818 smcr_lgr_set_type_asym(lgr, lgr_new_t, link_new->link_idx);
819 else
820 smcr_lgr_set_type(lgr, lgr_new_t);
Karsten Graulb1570a82020-05-03 14:38:42 +0200821 return 0;
822}
823
Karsten Graul336ba092020-05-03 14:38:40 +0200824static void smc_llc_save_add_link_info(struct smc_link *link,
825 struct smc_llc_msg_add_link *add_llc)
826{
827 link->peer_qpn = ntoh24(add_llc->sender_qp_num);
828 memcpy(link->peer_gid, add_llc->sender_gid, SMC_GID_SIZE);
829 memcpy(link->peer_mac, add_llc->sender_mac, ETH_ALEN);
830 link->peer_psn = ntoh24(add_llc->initial_psn);
831 link->peer_mtu = add_llc->qp_mtu;
832}
833
834/* as an SMC client, process an add link request */
835int smc_llc_cli_add_link(struct smc_link *link, struct smc_llc_qentry *qentry)
836{
837 struct smc_llc_msg_add_link *llc = &qentry->msg.add_link;
838 enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
839 struct smc_link_group *lgr = smc_get_lgr(link);
840 struct smc_link *lnk_new = NULL;
841 struct smc_init_info ini;
842 int lnk_idx, rc = 0;
843
844 ini.vlan_id = lgr->vlan_id;
845 smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
846 if (!memcmp(llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
847 !memcmp(llc->sender_mac, link->peer_mac, ETH_ALEN)) {
848 if (!ini.ib_dev)
849 goto out_reject;
850 lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
851 }
852 if (!ini.ib_dev) {
853 lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
854 ini.ib_dev = link->smcibdev;
855 ini.ib_port = link->ibport;
856 }
857 lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
858 if (lnk_idx < 0)
859 goto out_reject;
860 lnk_new = &lgr->lnk[lnk_idx];
861 rc = smcr_link_init(lgr, lnk_new, lnk_idx, &ini);
862 if (rc)
863 goto out_reject;
864 smc_llc_save_add_link_info(lnk_new, llc);
Karsten Graul45fa8da2020-05-04 14:18:47 +0200865 lnk_new->link_id = llc->link_num; /* SMC server assigns link id */
866 smc_llc_link_set_uid(lnk_new);
Karsten Graul336ba092020-05-03 14:38:40 +0200867
868 rc = smc_ib_ready_link(lnk_new);
869 if (rc)
870 goto out_clear_lnk;
871
872 rc = smcr_buf_map_lgr(lnk_new);
873 if (rc)
874 goto out_clear_lnk;
875
876 rc = smc_llc_send_add_link(link,
877 lnk_new->smcibdev->mac[ini.ib_port - 1],
878 lnk_new->gid, lnk_new, SMC_LLC_RESP);
879 if (rc)
880 goto out_clear_lnk;
Karsten Graul87f88cd2020-05-03 14:38:41 +0200881 rc = smc_llc_cli_rkey_exchange(link, lnk_new);
Karsten Graul336ba092020-05-03 14:38:40 +0200882 if (rc) {
883 rc = 0;
884 goto out_clear_lnk;
885 }
Karsten Graulb1570a82020-05-03 14:38:42 +0200886 rc = smc_llc_cli_conf_link(link, &ini, lnk_new, lgr_new_t);
Karsten Graul336ba092020-05-03 14:38:40 +0200887 if (!rc)
888 goto out;
889out_clear_lnk:
Karsten Graul0a99be42020-05-05 15:01:20 +0200890 smcr_link_clear(lnk_new, false);
Karsten Graul336ba092020-05-03 14:38:40 +0200891out_reject:
892 smc_llc_cli_add_link_reject(qentry);
893out:
894 kfree(qentry);
895 return rc;
896}
897
Karsten Graulc48254f2020-07-18 15:06:14 +0200898/* as an SMC client, invite server to start the add_link processing */
899static void smc_llc_cli_add_link_invite(struct smc_link *link,
900 struct smc_llc_qentry *qentry)
901{
902 struct smc_link_group *lgr = smc_get_lgr(link);
903 struct smc_init_info ini;
904
905 if (lgr->type == SMC_LGR_SYMMETRIC ||
906 lgr->type == SMC_LGR_ASYMMETRIC_PEER)
907 goto out;
908
909 ini.vlan_id = lgr->vlan_id;
910 smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
911 if (!ini.ib_dev)
912 goto out;
913
914 smc_llc_send_add_link(link, ini.ib_dev->mac[ini.ib_port - 1],
915 ini.ib_gid, NULL, SMC_LLC_REQ);
916out:
917 kfree(qentry);
918}
919
920static bool smc_llc_is_local_add_link(union smc_llc_msg *llc)
921{
922 if (llc->raw.hdr.common.type == SMC_LLC_ADD_LINK &&
923 !llc->add_link.qp_mtu && !llc->add_link.link_num)
924 return true;
925 return false;
926}
927
Karsten Graulb1570a82020-05-03 14:38:42 +0200928static void smc_llc_process_cli_add_link(struct smc_link_group *lgr)
929{
930 struct smc_llc_qentry *qentry;
931
932 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
933
934 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulc48254f2020-07-18 15:06:14 +0200935 if (smc_llc_is_local_add_link(&qentry->msg))
936 smc_llc_cli_add_link_invite(qentry->link, qentry);
937 else
938 smc_llc_cli_add_link(qentry->link, qentry);
Karsten Graulb1570a82020-05-03 14:38:42 +0200939 mutex_unlock(&lgr->llc_conf_mutex);
940}
941
Karsten Graul9c416872020-05-03 14:38:48 +0200942static int smc_llc_active_link_count(struct smc_link_group *lgr)
943{
944 int i, link_count = 0;
945
946 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200947 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graul9c416872020-05-03 14:38:48 +0200948 continue;
949 link_count++;
950 }
951 return link_count;
952}
953
Karsten Graulc9a5d242020-05-03 14:38:46 +0200954/* find the asymmetric link when 3 links are established */
955static struct smc_link *smc_llc_find_asym_link(struct smc_link_group *lgr)
956{
957 int asym_idx = -ENOENT;
958 int i, j, k;
959 bool found;
960
961 /* determine asymmetric link */
962 found = false;
963 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
964 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
965 if (!smc_link_usable(&lgr->lnk[i]) ||
966 !smc_link_usable(&lgr->lnk[j]))
967 continue;
968 if (!memcmp(lgr->lnk[i].gid, lgr->lnk[j].gid,
969 SMC_GID_SIZE)) {
970 found = true; /* asym_lnk is i or j */
971 break;
972 }
973 }
974 if (found)
975 break;
976 }
977 if (!found)
978 goto out; /* no asymmetric link */
979 for (k = 0; k < SMC_LINKS_PER_LGR_MAX; k++) {
980 if (!smc_link_usable(&lgr->lnk[k]))
981 continue;
982 if (k != i &&
983 !memcmp(lgr->lnk[i].peer_gid, lgr->lnk[k].peer_gid,
984 SMC_GID_SIZE)) {
985 asym_idx = i;
986 break;
987 }
988 if (k != j &&
989 !memcmp(lgr->lnk[j].peer_gid, lgr->lnk[k].peer_gid,
990 SMC_GID_SIZE)) {
991 asym_idx = j;
992 break;
993 }
994 }
995out:
996 return (asym_idx < 0) ? NULL : &lgr->lnk[asym_idx];
997}
998
999static void smc_llc_delete_asym_link(struct smc_link_group *lgr)
1000{
1001 struct smc_link *lnk_new = NULL, *lnk_asym;
1002 struct smc_llc_qentry *qentry;
1003 int rc;
1004
1005 lnk_asym = smc_llc_find_asym_link(lgr);
1006 if (!lnk_asym)
1007 return; /* no asymmetric link */
1008 if (!smc_link_downing(&lnk_asym->state))
1009 return;
Karsten Graulc6f02eb2020-05-04 14:18:38 +02001010 lnk_new = smc_switch_conns(lgr, lnk_asym, false);
Karsten Graulc9a5d242020-05-03 14:38:46 +02001011 smc_wr_tx_wait_no_pending_sends(lnk_asym);
1012 if (!lnk_new)
1013 goto out_free;
1014 /* change flow type from ADD_LINK into DEL_LINK */
1015 lgr->llc_flow_lcl.type = SMC_LLC_FLOW_DEL_LINK;
1016 rc = smc_llc_send_delete_link(lnk_new, lnk_asym->link_id, SMC_LLC_REQ,
1017 true, SMC_LLC_DEL_NO_ASYM_NEEDED);
1018 if (rc) {
1019 smcr_link_down_cond(lnk_new);
1020 goto out_free;
1021 }
1022 qentry = smc_llc_wait(lgr, lnk_new, SMC_LLC_WAIT_TIME,
1023 SMC_LLC_DELETE_LINK);
1024 if (!qentry) {
1025 smcr_link_down_cond(lnk_new);
1026 goto out_free;
1027 }
1028 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1029out_free:
Karsten Graul0a99be42020-05-05 15:01:20 +02001030 smcr_link_clear(lnk_asym, true);
Karsten Graulc9a5d242020-05-03 14:38:46 +02001031}
1032
Karsten Graul57b49922020-05-03 14:38:44 +02001033static int smc_llc_srv_rkey_exchange(struct smc_link *link,
1034 struct smc_link *link_new)
1035{
1036 struct smc_llc_msg_add_link_cont *addc_llc;
1037 struct smc_link_group *lgr = link->lgr;
1038 u8 max, num_rkeys_send, num_rkeys_recv;
1039 struct smc_llc_qentry *qentry = NULL;
1040 struct smc_buf_desc *buf_pos;
1041 int buf_lst;
1042 int rc = 0;
1043 int i;
1044
1045 mutex_lock(&lgr->rmbs_lock);
1046 num_rkeys_send = lgr->conns_num;
1047 buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
1048 do {
1049 smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
1050 &buf_lst, &buf_pos);
1051 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME,
1052 SMC_LLC_ADD_LINK_CONT);
1053 if (!qentry) {
1054 rc = -ETIMEDOUT;
1055 goto out;
1056 }
1057 addc_llc = &qentry->msg.add_link_cont;
1058 num_rkeys_recv = addc_llc->num_rkeys;
1059 max = min_t(u8, num_rkeys_recv, SMC_LLC_RKEYS_PER_CONT_MSG);
1060 for (i = 0; i < max; i++) {
1061 smc_rtoken_set(lgr, link->link_idx, link_new->link_idx,
1062 addc_llc->rt[i].rmb_key,
1063 addc_llc->rt[i].rmb_vaddr_new,
1064 addc_llc->rt[i].rmb_key_new);
1065 num_rkeys_recv--;
1066 }
1067 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1068 } while (num_rkeys_send || num_rkeys_recv);
1069out:
1070 mutex_unlock(&lgr->rmbs_lock);
1071 return rc;
1072}
1073
Karsten Graul1551c952020-05-03 14:38:45 +02001074static int smc_llc_srv_conf_link(struct smc_link *link,
1075 struct smc_link *link_new,
1076 enum smc_lgr_type lgr_new_t)
1077{
1078 struct smc_link_group *lgr = link->lgr;
1079 struct smc_llc_qentry *qentry = NULL;
1080 int rc;
1081
1082 /* send CONFIRM LINK request over the RoCE fabric */
1083 rc = smc_llc_send_confirm_link(link_new, SMC_LLC_REQ);
1084 if (rc)
1085 return -ENOLINK;
1086 /* receive CONFIRM LINK response over the RoCE fabric */
Karsten Graula35fffb2020-07-18 15:06:09 +02001087 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_FIRST_TIME, 0);
1088 if (!qentry ||
1089 qentry->msg.raw.hdr.common.type != SMC_LLC_CONFIRM_LINK) {
Karsten Graul1551c952020-05-03 14:38:45 +02001090 /* send DELETE LINK */
1091 smc_llc_send_delete_link(link, link_new->link_id, SMC_LLC_REQ,
1092 false, SMC_LLC_DEL_LOST_PATH);
Karsten Graula35fffb2020-07-18 15:06:09 +02001093 if (qentry)
1094 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
Karsten Graul1551c952020-05-03 14:38:45 +02001095 return -ENOLINK;
1096 }
Karsten Graul649758f2020-05-04 14:18:48 +02001097 smc_llc_save_peer_uid(qentry);
Karsten Graul1551c952020-05-03 14:38:45 +02001098 smc_llc_link_active(link_new);
Karsten Graulad6c1112020-05-04 14:18:44 +02001099 if (lgr_new_t == SMC_LGR_ASYMMETRIC_LOCAL ||
1100 lgr_new_t == SMC_LGR_ASYMMETRIC_PEER)
1101 smcr_lgr_set_type_asym(lgr, lgr_new_t, link_new->link_idx);
1102 else
1103 smcr_lgr_set_type(lgr, lgr_new_t);
Karsten Graul1551c952020-05-03 14:38:45 +02001104 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1105 return 0;
1106}
1107
Karsten Graul2d2209f2020-05-03 14:38:43 +02001108int smc_llc_srv_add_link(struct smc_link *link)
1109{
1110 enum smc_lgr_type lgr_new_t = SMC_LGR_SYMMETRIC;
1111 struct smc_link_group *lgr = link->lgr;
1112 struct smc_llc_msg_add_link *add_llc;
1113 struct smc_llc_qentry *qentry = NULL;
1114 struct smc_link *link_new;
1115 struct smc_init_info ini;
1116 int lnk_idx, rc = 0;
1117
1118 /* ignore client add link recommendation, start new flow */
1119 ini.vlan_id = lgr->vlan_id;
1120 smc_pnet_find_alt_roce(lgr, &ini, link->smcibdev);
1121 if (!ini.ib_dev) {
1122 lgr_new_t = SMC_LGR_ASYMMETRIC_LOCAL;
1123 ini.ib_dev = link->smcibdev;
1124 ini.ib_port = link->ibport;
1125 }
1126 lnk_idx = smc_llc_alloc_alt_link(lgr, lgr_new_t);
1127 if (lnk_idx < 0)
1128 return 0;
1129
1130 rc = smcr_link_init(lgr, &lgr->lnk[lnk_idx], lnk_idx, &ini);
1131 if (rc)
1132 return rc;
1133 link_new = &lgr->lnk[lnk_idx];
1134 rc = smc_llc_send_add_link(link,
1135 link_new->smcibdev->mac[ini.ib_port - 1],
1136 link_new->gid, link_new, SMC_LLC_REQ);
1137 if (rc)
1138 goto out_err;
1139 /* receive ADD LINK response over the RoCE fabric */
1140 qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME, SMC_LLC_ADD_LINK);
1141 if (!qentry) {
1142 rc = -ETIMEDOUT;
1143 goto out_err;
1144 }
1145 add_llc = &qentry->msg.add_link;
1146 if (add_llc->hd.flags & SMC_LLC_FLAG_ADD_LNK_REJ) {
1147 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1148 rc = -ENOLINK;
1149 goto out_err;
1150 }
1151 if (lgr->type == SMC_LGR_SINGLE &&
1152 (!memcmp(add_llc->sender_gid, link->peer_gid, SMC_GID_SIZE) &&
1153 !memcmp(add_llc->sender_mac, link->peer_mac, ETH_ALEN))) {
1154 lgr_new_t = SMC_LGR_ASYMMETRIC_PEER;
1155 }
1156 smc_llc_save_add_link_info(link_new, add_llc);
1157 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1158
1159 rc = smc_ib_ready_link(link_new);
1160 if (rc)
1161 goto out_err;
1162 rc = smcr_buf_map_lgr(link_new);
1163 if (rc)
1164 goto out_err;
1165 rc = smcr_buf_reg_lgr(link_new);
1166 if (rc)
1167 goto out_err;
Karsten Graul57b49922020-05-03 14:38:44 +02001168 rc = smc_llc_srv_rkey_exchange(link, link_new);
Karsten Graul2d2209f2020-05-03 14:38:43 +02001169 if (rc)
1170 goto out_err;
Karsten Graul1551c952020-05-03 14:38:45 +02001171 rc = smc_llc_srv_conf_link(link, link_new, lgr_new_t);
Karsten Graul2d2209f2020-05-03 14:38:43 +02001172 if (rc)
1173 goto out_err;
1174 return 0;
1175out_err:
Karsten Graul0a99be42020-05-05 15:01:20 +02001176 smcr_link_clear(link_new, false);
Karsten Graul2d2209f2020-05-03 14:38:43 +02001177 return rc;
1178}
1179
1180static void smc_llc_process_srv_add_link(struct smc_link_group *lgr)
1181{
1182 struct smc_link *link = lgr->llc_flow_lcl.qentry->link;
1183 int rc;
1184
1185 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1186
1187 mutex_lock(&lgr->llc_conf_mutex);
1188 rc = smc_llc_srv_add_link(link);
1189 if (!rc && lgr->type == SMC_LGR_SYMMETRIC) {
1190 /* delete any asymmetric link */
Karsten Graulc9a5d242020-05-03 14:38:46 +02001191 smc_llc_delete_asym_link(lgr);
Karsten Graul2d2209f2020-05-03 14:38:43 +02001192 }
1193 mutex_unlock(&lgr->llc_conf_mutex);
1194}
1195
Karsten Graulc48254f2020-07-18 15:06:14 +02001196/* enqueue a local add_link req to trigger a new add_link flow */
1197void smc_llc_add_link_local(struct smc_link *link)
Karsten Graul4dadd152020-05-03 14:38:50 +02001198{
1199 struct smc_llc_msg_add_link add_llc = {0};
1200
1201 add_llc.hd.length = sizeof(add_llc);
1202 add_llc.hd.common.type = SMC_LLC_ADD_LINK;
Karsten Graulc48254f2020-07-18 15:06:14 +02001203 /* no dev and port needed */
Karsten Graul4dadd152020-05-03 14:38:50 +02001204 smc_llc_enqueue(link, (union smc_llc_msg *)&add_llc);
1205}
1206
Karsten Graulb45e7f92020-05-01 12:48:13 +02001207/* worker to process an add link message */
1208static void smc_llc_add_link_work(struct work_struct *work)
1209{
1210 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1211 llc_add_link_work);
1212
1213 if (list_empty(&lgr->list)) {
1214 /* link group is terminating */
1215 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1216 goto out;
1217 }
1218
Karsten Graulb1570a82020-05-03 14:38:42 +02001219 if (lgr->role == SMC_CLNT)
1220 smc_llc_process_cli_add_link(lgr);
Karsten Graul2d2209f2020-05-03 14:38:43 +02001221 else
1222 smc_llc_process_srv_add_link(lgr);
Karsten Graulb45e7f92020-05-01 12:48:13 +02001223out:
1224 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
1225}
1226
Karsten Graul4dadd152020-05-03 14:38:50 +02001227/* enqueue a local del_link msg to trigger a new del_link flow,
1228 * called only for role SMC_SERV
1229 */
1230void smc_llc_srv_delete_link_local(struct smc_link *link, u8 del_link_id)
1231{
1232 struct smc_llc_msg_del_link del_llc = {0};
1233
1234 del_llc.hd.length = sizeof(del_llc);
1235 del_llc.hd.common.type = SMC_LLC_DELETE_LINK;
1236 del_llc.link_num = del_link_id;
1237 del_llc.reason = htonl(SMC_LLC_DEL_LOST_PATH);
1238 del_llc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
1239 smc_llc_enqueue(link, (union smc_llc_msg *)&del_llc);
1240}
1241
Karsten Graul9c416872020-05-03 14:38:48 +02001242static void smc_llc_process_cli_delete_link(struct smc_link_group *lgr)
1243{
1244 struct smc_link *lnk_del = NULL, *lnk_asym, *lnk;
1245 struct smc_llc_msg_del_link *del_llc;
1246 struct smc_llc_qentry *qentry;
1247 int active_links;
1248 int lnk_idx;
1249
1250 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1251 lnk = qentry->link;
1252 del_llc = &qentry->msg.delete_link;
1253
1254 if (del_llc->hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1255 smc_lgr_terminate_sched(lgr);
1256 goto out;
1257 }
1258 mutex_lock(&lgr->llc_conf_mutex);
1259 /* delete single link */
1260 for (lnk_idx = 0; lnk_idx < SMC_LINKS_PER_LGR_MAX; lnk_idx++) {
1261 if (lgr->lnk[lnk_idx].link_id != del_llc->link_num)
1262 continue;
1263 lnk_del = &lgr->lnk[lnk_idx];
1264 break;
1265 }
1266 del_llc->hd.flags |= SMC_LLC_FLAG_RESP;
1267 if (!lnk_del) {
1268 /* link was not found */
1269 del_llc->reason = htonl(SMC_LLC_DEL_NOLNK);
1270 smc_llc_send_message(lnk, &qentry->msg);
1271 goto out_unlock;
1272 }
1273 lnk_asym = smc_llc_find_asym_link(lgr);
1274
1275 del_llc->reason = 0;
1276 smc_llc_send_message(lnk, &qentry->msg); /* response */
1277
1278 if (smc_link_downing(&lnk_del->state)) {
Karsten Graulb7eede72020-07-08 17:05:12 +02001279 if (smc_switch_conns(lgr, lnk_del, false))
1280 smc_wr_tx_wait_no_pending_sends(lnk_del);
Karsten Graul9c416872020-05-03 14:38:48 +02001281 }
Karsten Graul0a99be42020-05-05 15:01:20 +02001282 smcr_link_clear(lnk_del, true);
Karsten Graul9c416872020-05-03 14:38:48 +02001283
1284 active_links = smc_llc_active_link_count(lgr);
1285 if (lnk_del == lnk_asym) {
1286 /* expected deletion of asym link, don't change lgr state */
1287 } else if (active_links == 1) {
Karsten Graulad6c1112020-05-04 14:18:44 +02001288 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
Karsten Graul9c416872020-05-03 14:38:48 +02001289 } else if (!active_links) {
Karsten Graulad6c1112020-05-04 14:18:44 +02001290 smcr_lgr_set_type(lgr, SMC_LGR_NONE);
Karsten Graul9c416872020-05-03 14:38:48 +02001291 smc_lgr_terminate_sched(lgr);
1292 }
1293out_unlock:
1294 mutex_unlock(&lgr->llc_conf_mutex);
1295out:
1296 kfree(qentry);
1297}
1298
Karsten Graulf3811fd2020-05-04 14:18:42 +02001299/* try to send a DELETE LINK ALL request on any active link,
1300 * waiting for send completion
1301 */
1302void smc_llc_send_link_delete_all(struct smc_link_group *lgr, bool ord, u32 rsn)
1303{
1304 struct smc_llc_msg_del_link delllc = {0};
1305 int i;
1306
1307 delllc.hd.common.type = SMC_LLC_DELETE_LINK;
1308 delllc.hd.length = sizeof(delllc);
1309 if (ord)
1310 delllc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
1311 delllc.hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
1312 delllc.reason = htonl(rsn);
1313
1314 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1315 if (!smc_link_usable(&lgr->lnk[i]))
1316 continue;
1317 if (!smc_llc_send_message_wait(&lgr->lnk[i], &delllc))
1318 break;
1319 }
1320}
1321
Karsten Graul08ae27d2020-05-03 14:38:49 +02001322static void smc_llc_process_srv_delete_link(struct smc_link_group *lgr)
1323{
1324 struct smc_llc_msg_del_link *del_llc;
1325 struct smc_link *lnk, *lnk_del;
1326 struct smc_llc_qentry *qentry;
1327 int active_links;
1328 int i;
1329
1330 mutex_lock(&lgr->llc_conf_mutex);
1331 qentry = smc_llc_flow_qentry_clr(&lgr->llc_flow_lcl);
1332 lnk = qentry->link;
1333 del_llc = &qentry->msg.delete_link;
1334
1335 if (qentry->msg.delete_link.hd.flags & SMC_LLC_FLAG_DEL_LINK_ALL) {
1336 /* delete entire lgr */
Karsten Graulf3811fd2020-05-04 14:18:42 +02001337 smc_llc_send_link_delete_all(lgr, true, ntohl(
1338 qentry->msg.delete_link.reason));
Karsten Graul08ae27d2020-05-03 14:38:49 +02001339 smc_lgr_terminate_sched(lgr);
1340 goto out;
1341 }
1342 /* delete single link */
1343 lnk_del = NULL;
1344 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1345 if (lgr->lnk[i].link_id == del_llc->link_num) {
1346 lnk_del = &lgr->lnk[i];
1347 break;
1348 }
1349 }
1350 if (!lnk_del)
1351 goto out; /* asymmetric link already deleted */
1352
1353 if (smc_link_downing(&lnk_del->state)) {
Karsten Graulb7eede72020-07-08 17:05:12 +02001354 if (smc_switch_conns(lgr, lnk_del, false))
1355 smc_wr_tx_wait_no_pending_sends(lnk_del);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001356 }
1357 if (!list_empty(&lgr->list)) {
1358 /* qentry is either a request from peer (send it back to
1359 * initiate the DELETE_LINK processing), or a locally
1360 * enqueued DELETE_LINK request (forward it)
1361 */
1362 if (!smc_llc_send_message(lnk, &qentry->msg)) {
Karsten Graul08ae27d2020-05-03 14:38:49 +02001363 struct smc_llc_qentry *qentry2;
1364
1365 qentry2 = smc_llc_wait(lgr, lnk, SMC_LLC_WAIT_TIME,
1366 SMC_LLC_DELETE_LINK);
YueHaibingca7e3ed2020-05-07 16:24:06 +02001367 if (qentry2)
Karsten Graul08ae27d2020-05-03 14:38:49 +02001368 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001369 }
1370 }
Karsten Graul0a99be42020-05-05 15:01:20 +02001371 smcr_link_clear(lnk_del, true);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001372
1373 active_links = smc_llc_active_link_count(lgr);
1374 if (active_links == 1) {
Karsten Graulad6c1112020-05-04 14:18:44 +02001375 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001376 } else if (!active_links) {
Karsten Graulad6c1112020-05-04 14:18:44 +02001377 smcr_lgr_set_type(lgr, SMC_LGR_NONE);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001378 smc_lgr_terminate_sched(lgr);
1379 }
1380
1381 if (lgr->type == SMC_LGR_SINGLE && !list_empty(&lgr->list)) {
1382 /* trigger setup of asymm alt link */
Karsten Graulc48254f2020-07-18 15:06:14 +02001383 smc_llc_add_link_local(lnk);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001384 }
1385out:
1386 mutex_unlock(&lgr->llc_conf_mutex);
1387 kfree(qentry);
1388}
1389
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001390static void smc_llc_delete_link_work(struct work_struct *work)
Karsten Graul52bedf32018-03-01 13:51:32 +01001391{
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001392 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1393 llc_del_link_work);
Karsten Graul52bedf32018-03-01 13:51:32 +01001394
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001395 if (list_empty(&lgr->list)) {
1396 /* link group is terminating */
1397 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
1398 goto out;
Karsten Graul52bedf32018-03-01 13:51:32 +01001399 }
Karsten Graul9c416872020-05-03 14:38:48 +02001400
1401 if (lgr->role == SMC_CLNT)
1402 smc_llc_process_cli_delete_link(lgr);
Karsten Graul08ae27d2020-05-03 14:38:49 +02001403 else
1404 smc_llc_process_srv_delete_link(lgr);
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001405out:
1406 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul52bedf32018-03-01 13:51:32 +01001407}
1408
Karsten Graul3bc67e02020-04-30 15:55:48 +02001409/* process a confirm_rkey request from peer, remote flow */
1410static void smc_llc_rmt_conf_rkey(struct smc_link_group *lgr)
Karsten Graul4ed75de2018-03-01 13:51:30 +01001411{
Karsten Graul3bc67e02020-04-30 15:55:48 +02001412 struct smc_llc_msg_confirm_rkey *llc;
1413 struct smc_llc_qentry *qentry;
1414 struct smc_link *link;
1415 int num_entries;
1416 int rk_idx;
1417 int i;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001418
Karsten Graul3bc67e02020-04-30 15:55:48 +02001419 qentry = lgr->llc_flow_rmt.qentry;
1420 llc = &qentry->msg.confirm_rkey;
1421 link = qentry->link;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001422
Karsten Graul3bc67e02020-04-30 15:55:48 +02001423 num_entries = llc->rtoken[0].num_rkeys;
1424 /* first rkey entry is for receiving link */
1425 rk_idx = smc_rtoken_add(link,
1426 llc->rtoken[0].rmb_vaddr,
1427 llc->rtoken[0].rmb_key);
1428 if (rk_idx < 0)
1429 goto out_err;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001430
Karsten Graul3bc67e02020-04-30 15:55:48 +02001431 for (i = 1; i <= min_t(u8, num_entries, SMC_LLC_RKEYS_PER_MSG - 1); i++)
1432 smc_rtoken_set2(lgr, rk_idx, llc->rtoken[i].link_id,
1433 llc->rtoken[i].rmb_vaddr,
1434 llc->rtoken[i].rmb_key);
1435 /* max links is 3 so there is no need to support conf_rkey_cont msgs */
1436 goto out;
1437out_err:
1438 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1439 llc->hd.flags |= SMC_LLC_FLAG_RKEY_RETRY;
1440out:
Karsten Graulef79d432020-04-29 17:10:47 +02001441 llc->hd.flags |= SMC_LLC_FLAG_RESP;
Karsten Graul3bc67e02020-04-30 15:55:48 +02001442 smc_llc_send_message(link, &qentry->msg);
1443 smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
Karsten Graul4ed75de2018-03-01 13:51:30 +01001444}
1445
Karsten Graul218b24f2020-04-30 15:55:49 +02001446/* process a delete_rkey request from peer, remote flow */
1447static void smc_llc_rmt_delete_rkey(struct smc_link_group *lgr)
Karsten Graul4ed75de2018-03-01 13:51:30 +01001448{
Karsten Graul218b24f2020-04-30 15:55:49 +02001449 struct smc_llc_msg_delete_rkey *llc;
1450 struct smc_llc_qentry *qentry;
1451 struct smc_link *link;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001452 u8 err_mask = 0;
1453 int i, max;
1454
Karsten Graul218b24f2020-04-30 15:55:49 +02001455 qentry = lgr->llc_flow_rmt.qentry;
1456 llc = &qentry->msg.delete_rkey;
1457 link = qentry->link;
1458
Karsten Graulef79d432020-04-29 17:10:47 +02001459 max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
1460 for (i = 0; i < max; i++) {
1461 if (smc_rtoken_delete(link, llc->rkey[i]))
1462 err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
Karsten Graul4ed75de2018-03-01 13:51:30 +01001463 }
Karsten Graulef79d432020-04-29 17:10:47 +02001464 if (err_mask) {
1465 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
1466 llc->err_mask = err_mask;
1467 }
Karsten Graul218b24f2020-04-30 15:55:49 +02001468 llc->hd.flags |= SMC_LLC_FLAG_RESP;
1469 smc_llc_send_message(link, &qentry->msg);
1470 smc_llc_flow_qentry_del(&lgr->llc_flow_rmt);
1471}
Karsten Graulef79d432020-04-29 17:10:47 +02001472
Karsten Graul3e0c40a2020-05-04 14:18:45 +02001473static void smc_llc_protocol_violation(struct smc_link_group *lgr, u8 type)
1474{
1475 pr_warn_ratelimited("smc: SMC-R lg %*phN LLC protocol violation: "
1476 "llc_type %d\n", SMC_LGR_ID_SIZE, &lgr->id, type);
1477 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_PROT_VIOL);
1478 smc_lgr_terminate_sched(lgr);
1479}
1480
Karsten Graul6c8968c2020-04-29 17:10:46 +02001481/* flush the llc event queue */
Karsten Graul00a049c2020-04-29 17:10:49 +02001482static void smc_llc_event_flush(struct smc_link_group *lgr)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001483{
Karsten Graul6c8968c2020-04-29 17:10:46 +02001484 struct smc_llc_qentry *qentry, *q;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001485
Karsten Graul6c8968c2020-04-29 17:10:46 +02001486 spin_lock_bh(&lgr->llc_event_q_lock);
1487 list_for_each_entry_safe(qentry, q, &lgr->llc_event_q, list) {
1488 list_del_init(&qentry->list);
1489 kfree(qentry);
1490 }
1491 spin_unlock_bh(&lgr->llc_event_q_lock);
1492}
1493
1494static void smc_llc_event_handler(struct smc_llc_qentry *qentry)
1495{
1496 union smc_llc_msg *llc = &qentry->msg;
1497 struct smc_link *link = qentry->link;
Karsten Graul0fb0b022020-04-30 15:55:43 +02001498 struct smc_link_group *lgr = link->lgr;
Karsten Graul6c8968c2020-04-29 17:10:46 +02001499
Karsten Grauld854fcb2020-04-29 17:10:43 +02001500 if (!smc_link_usable(link))
Karsten Graul6c8968c2020-04-29 17:10:46 +02001501 goto out;
Karsten Graul313164d2018-03-01 13:51:29 +01001502
1503 switch (llc->raw.hdr.common.type) {
1504 case SMC_LLC_TEST_LINK:
Karsten Graul56e80912020-04-30 15:55:46 +02001505 llc->test_link.hd.flags |= SMC_LLC_FLAG_RESP;
1506 smc_llc_send_message(link, llc);
Karsten Graul313164d2018-03-01 13:51:29 +01001507 break;
Karsten Graul52bedf32018-03-01 13:51:32 +01001508 case SMC_LLC_ADD_LINK:
Karsten Graul0fb0b022020-04-30 15:55:43 +02001509 if (list_empty(&lgr->list))
1510 goto out; /* lgr is terminating */
1511 if (lgr->role == SMC_CLNT) {
Karsten Graulc48254f2020-07-18 15:06:14 +02001512 if (smc_llc_is_local_add_link(llc)) {
1513 if (lgr->llc_flow_lcl.type ==
1514 SMC_LLC_FLOW_ADD_LINK)
1515 break; /* add_link in progress */
1516 if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1517 qentry)) {
1518 schedule_work(&lgr->llc_add_link_work);
1519 }
1520 return;
1521 }
1522 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK &&
1523 !lgr->llc_flow_lcl.qentry) {
Karsten Graul0fb0b022020-04-30 15:55:43 +02001524 /* a flow is waiting for this message */
1525 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl,
1526 qentry);
Karsten Graul6778a6b2020-07-08 17:05:11 +02001527 wake_up(&lgr->llc_msg_waiter);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001528 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl,
1529 qentry)) {
Karsten Graulb45e7f92020-05-01 12:48:13 +02001530 schedule_work(&lgr->llc_add_link_work);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001531 }
1532 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl, qentry)) {
1533 /* as smc server, handle client suggestion */
Karsten Graulb45e7f92020-05-01 12:48:13 +02001534 schedule_work(&lgr->llc_add_link_work);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001535 }
1536 return;
1537 case SMC_LLC_CONFIRM_LINK:
Karsten Graul87f88cd2020-05-03 14:38:41 +02001538 case SMC_LLC_ADD_LINK_CONT:
Karsten Graul0fb0b022020-04-30 15:55:43 +02001539 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1540 /* a flow is waiting for this message */
1541 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl, qentry);
Karsten Graul6778a6b2020-07-08 17:05:11 +02001542 wake_up(&lgr->llc_msg_waiter);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001543 return;
1544 }
Karsten Graul52bedf32018-03-01 13:51:32 +01001545 break;
1546 case SMC_LLC_DELETE_LINK:
Karsten Graulb9979c22020-07-18 15:06:15 +02001547 if (lgr->llc_flow_lcl.type == SMC_LLC_FLOW_ADD_LINK &&
1548 !lgr->llc_flow_lcl.qentry) {
1549 /* DEL LINK REQ during ADD LINK SEQ */
1550 smc_llc_flow_qentry_set(&lgr->llc_flow_lcl, qentry);
1551 wake_up(&lgr->llc_msg_waiter);
1552 } else if (smc_llc_flow_start(&lgr->llc_flow_lcl, qentry)) {
1553 schedule_work(&lgr->llc_del_link_work);
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001554 }
1555 return;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001556 case SMC_LLC_CONFIRM_RKEY:
Karsten Graul3bc67e02020-04-30 15:55:48 +02001557 /* new request from remote, assign to remote flow */
1558 if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1559 /* process here, does not wait for more llc msgs */
1560 smc_llc_rmt_conf_rkey(lgr);
1561 smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1562 }
1563 return;
Karsten Graul4ed75de2018-03-01 13:51:30 +01001564 case SMC_LLC_CONFIRM_RKEY_CONT:
Karsten Graul42d18ac2020-04-30 15:55:50 +02001565 /* not used because max links is 3, and 3 rkeys fit into
1566 * one CONFIRM_RKEY message
1567 */
Karsten Graul4ed75de2018-03-01 13:51:30 +01001568 break;
1569 case SMC_LLC_DELETE_RKEY:
Karsten Graul218b24f2020-04-30 15:55:49 +02001570 /* new request from remote, assign to remote flow */
1571 if (smc_llc_flow_start(&lgr->llc_flow_rmt, qentry)) {
1572 /* process here, does not wait for more llc msgs */
1573 smc_llc_rmt_delete_rkey(lgr);
1574 smc_llc_flow_stop(lgr, &lgr->llc_flow_rmt);
1575 }
1576 return;
Karsten Graul3e0c40a2020-05-04 14:18:45 +02001577 default:
1578 smc_llc_protocol_violation(lgr, llc->raw.hdr.common.type);
1579 break;
Karsten Graul313164d2018-03-01 13:51:29 +01001580 }
Karsten Graul6c8968c2020-04-29 17:10:46 +02001581out:
1582 kfree(qentry);
1583}
1584
1585/* worker to process llc messages on the event queue */
1586static void smc_llc_event_work(struct work_struct *work)
1587{
1588 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
1589 llc_event_work);
1590 struct smc_llc_qentry *qentry;
1591
Karsten Graul555da9a2020-04-30 15:55:38 +02001592 if (!lgr->llc_flow_lcl.type && lgr->delayed_event) {
1593 if (smc_link_usable(lgr->delayed_event->link)) {
1594 smc_llc_event_handler(lgr->delayed_event);
1595 } else {
1596 qentry = lgr->delayed_event;
1597 lgr->delayed_event = NULL;
1598 kfree(qentry);
1599 }
1600 }
1601
Karsten Graul6c8968c2020-04-29 17:10:46 +02001602again:
1603 spin_lock_bh(&lgr->llc_event_q_lock);
1604 if (!list_empty(&lgr->llc_event_q)) {
1605 qentry = list_first_entry(&lgr->llc_event_q,
1606 struct smc_llc_qentry, list);
1607 list_del_init(&qentry->list);
1608 spin_unlock_bh(&lgr->llc_event_q_lock);
1609 smc_llc_event_handler(qentry);
1610 goto again;
1611 }
1612 spin_unlock_bh(&lgr->llc_event_q_lock);
1613}
1614
Karsten Graulef79d432020-04-29 17:10:47 +02001615/* process llc responses in tasklet context */
Karsten Graula6688d92020-04-30 15:55:39 +02001616static void smc_llc_rx_response(struct smc_link *link,
1617 struct smc_llc_qentry *qentry)
Karsten Graulef79d432020-04-29 17:10:47 +02001618{
Karsten Graul2ff08672020-07-18 15:06:13 +02001619 enum smc_llc_flowtype flowtype = link->lgr->llc_flow_lcl.type;
1620 struct smc_llc_flow *flow = &link->lgr->llc_flow_lcl;
Karsten Graula6688d92020-04-30 15:55:39 +02001621 u8 llc_type = qentry->msg.raw.hdr.common.type;
Karsten Graulef79d432020-04-29 17:10:47 +02001622
Karsten Graula6688d92020-04-30 15:55:39 +02001623 switch (llc_type) {
Karsten Graulef79d432020-04-29 17:10:47 +02001624 case SMC_LLC_TEST_LINK:
Karsten Graul741a49a2020-07-18 15:06:16 +02001625 if (smc_link_active(link))
Karsten Graulef79d432020-04-29 17:10:47 +02001626 complete(&link->llc_testlink_resp);
1627 break;
Karsten Graulef79d432020-04-29 17:10:47 +02001628 case SMC_LLC_ADD_LINK:
Karsten Graul87f88cd2020-05-03 14:38:41 +02001629 case SMC_LLC_ADD_LINK_CONT:
Karsten Graul2ff08672020-07-18 15:06:13 +02001630 case SMC_LLC_CONFIRM_LINK:
1631 if (flowtype != SMC_LLC_FLOW_ADD_LINK || flow->qentry)
1632 break; /* drop out-of-flow response */
1633 goto assign;
1634 case SMC_LLC_DELETE_LINK:
1635 if (flowtype != SMC_LLC_FLOW_DEL_LINK || flow->qentry)
1636 break; /* drop out-of-flow response */
1637 goto assign;
Karsten Graul3d88a212020-04-30 15:55:44 +02001638 case SMC_LLC_CONFIRM_RKEY:
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001639 case SMC_LLC_DELETE_RKEY:
Karsten Graul2ff08672020-07-18 15:06:13 +02001640 if (flowtype != SMC_LLC_FLOW_RKEY || flow->qentry)
1641 break; /* drop out-of-flow response */
1642 goto assign;
Karsten Graulef79d432020-04-29 17:10:47 +02001643 case SMC_LLC_CONFIRM_RKEY_CONT:
Karsten Graul42d18ac2020-04-30 15:55:50 +02001644 /* not used because max links is 3 */
Karsten Graulef79d432020-04-29 17:10:47 +02001645 break;
Karsten Graul3e0c40a2020-05-04 14:18:45 +02001646 default:
1647 smc_llc_protocol_violation(link->lgr, llc_type);
1648 break;
Karsten Graulef79d432020-04-29 17:10:47 +02001649 }
Karsten Graula6688d92020-04-30 15:55:39 +02001650 kfree(qentry);
Karsten Graul2ff08672020-07-18 15:06:13 +02001651 return;
1652assign:
1653 /* assign responses to the local flow, we requested them */
1654 smc_llc_flow_qentry_set(&link->lgr->llc_flow_lcl, qentry);
1655 wake_up(&link->lgr->llc_msg_waiter);
Karsten Graulef79d432020-04-29 17:10:47 +02001656}
1657
Karsten Graula6688d92020-04-30 15:55:39 +02001658static void smc_llc_enqueue(struct smc_link *link, union smc_llc_msg *llc)
Karsten Graul6c8968c2020-04-29 17:10:46 +02001659{
Karsten Graul6c8968c2020-04-29 17:10:46 +02001660 struct smc_link_group *lgr = link->lgr;
1661 struct smc_llc_qentry *qentry;
Karsten Graul6c8968c2020-04-29 17:10:46 +02001662 unsigned long flags;
1663
Karsten Graul6c8968c2020-04-29 17:10:46 +02001664 qentry = kmalloc(sizeof(*qentry), GFP_ATOMIC);
1665 if (!qentry)
1666 return;
1667 qentry->link = link;
1668 INIT_LIST_HEAD(&qentry->list);
1669 memcpy(&qentry->msg, llc, sizeof(union smc_llc_msg));
Karsten Graula6688d92020-04-30 15:55:39 +02001670
1671 /* process responses immediately */
1672 if (llc->raw.hdr.flags & SMC_LLC_FLAG_RESP) {
1673 smc_llc_rx_response(link, qentry);
1674 return;
1675 }
1676
1677 /* add requests to event queue */
Karsten Graul6c8968c2020-04-29 17:10:46 +02001678 spin_lock_irqsave(&lgr->llc_event_q_lock, flags);
1679 list_add_tail(&qentry->list, &lgr->llc_event_q);
1680 spin_unlock_irqrestore(&lgr->llc_event_q_lock, flags);
Karsten Graul6778a6b2020-07-08 17:05:11 +02001681 schedule_work(&lgr->llc_event_work);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001682}
1683
Karsten Graula6688d92020-04-30 15:55:39 +02001684/* copy received msg and add it to the event queue */
1685static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
1686{
1687 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
1688 union smc_llc_msg *llc = buf;
1689
1690 if (wc->byte_len < sizeof(*llc))
1691 return; /* short message */
1692 if (llc->raw.hdr.length != sizeof(*llc))
1693 return; /* invalid message */
1694
1695 smc_llc_enqueue(link, llc);
1696}
1697
Karsten Graul44aa81c2018-05-15 17:04:55 +02001698/***************************** worker, utils *********************************/
Karsten Graul877ae5b2018-05-02 16:56:44 +02001699
1700static void smc_llc_testlink_work(struct work_struct *work)
1701{
1702 struct smc_link *link = container_of(to_delayed_work(work),
1703 struct smc_link, llc_testlink_wrk);
1704 unsigned long next_interval;
Karsten Graul877ae5b2018-05-02 16:56:44 +02001705 unsigned long expire_time;
1706 u8 user_data[16] = { 0 };
1707 int rc;
1708
Karsten Graul741a49a2020-07-18 15:06:16 +02001709 if (!smc_link_active(link))
Karsten Graul877ae5b2018-05-02 16:56:44 +02001710 return; /* don't reschedule worker */
1711 expire_time = link->wr_rx_tstamp + link->llc_testlink_time;
1712 if (time_is_after_jiffies(expire_time)) {
1713 next_interval = expire_time - jiffies;
1714 goto out;
1715 }
1716 reinit_completion(&link->llc_testlink_resp);
Karsten Grauld97935f2018-05-15 17:04:57 +02001717 smc_llc_send_test_link(link, user_data);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001718 /* receive TEST LINK response over RoCE fabric */
1719 rc = wait_for_completion_interruptible_timeout(&link->llc_testlink_resp,
1720 SMC_LLC_WAIT_TIME);
Karsten Graul741a49a2020-07-18 15:06:16 +02001721 if (!smc_link_active(link))
Karsten Graul1020e1e2020-04-29 17:10:44 +02001722 return; /* link state changed */
Karsten Graul877ae5b2018-05-02 16:56:44 +02001723 if (rc <= 0) {
Karsten Graul87523932020-05-01 12:48:09 +02001724 smcr_link_down_cond_sched(link);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001725 return;
1726 }
1727 next_interval = link->llc_testlink_time;
1728out:
Karsten Graul1020e1e2020-04-29 17:10:44 +02001729 schedule_delayed_work(&link->llc_testlink_wrk, next_interval);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001730}
1731
Karsten Graul00a049c2020-04-29 17:10:49 +02001732void smc_llc_lgr_init(struct smc_link_group *lgr, struct smc_sock *smc)
1733{
1734 struct net *net = sock_net(smc->clcsock->sk);
1735
1736 INIT_WORK(&lgr->llc_event_work, smc_llc_event_work);
Karsten Graulb45e7f92020-05-01 12:48:13 +02001737 INIT_WORK(&lgr->llc_add_link_work, smc_llc_add_link_work);
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001738 INIT_WORK(&lgr->llc_del_link_work, smc_llc_delete_link_work);
Karsten Graul00a049c2020-04-29 17:10:49 +02001739 INIT_LIST_HEAD(&lgr->llc_event_q);
1740 spin_lock_init(&lgr->llc_event_q_lock);
Karsten Graul555da9a2020-04-30 15:55:38 +02001741 spin_lock_init(&lgr->llc_flow_lock);
Karsten Graul6778a6b2020-07-08 17:05:11 +02001742 init_waitqueue_head(&lgr->llc_flow_waiter);
1743 init_waitqueue_head(&lgr->llc_msg_waiter);
Karsten Grauld5500662020-05-01 12:48:05 +02001744 mutex_init(&lgr->llc_conf_mutex);
Karsten Graul00a049c2020-04-29 17:10:49 +02001745 lgr->llc_testlink_time = net->ipv4.sysctl_tcp_keepalive_time;
1746}
1747
1748/* called after lgr was removed from lgr_list */
1749void smc_llc_lgr_clear(struct smc_link_group *lgr)
1750{
1751 smc_llc_event_flush(lgr);
Karsten Graul6778a6b2020-07-08 17:05:11 +02001752 wake_up_all(&lgr->llc_flow_waiter);
1753 wake_up_all(&lgr->llc_msg_waiter);
Karsten Graul00a049c2020-04-29 17:10:49 +02001754 cancel_work_sync(&lgr->llc_event_work);
Karsten Graulb45e7f92020-05-01 12:48:13 +02001755 cancel_work_sync(&lgr->llc_add_link_work);
Karsten Graul9ec6bf12020-05-03 14:38:47 +02001756 cancel_work_sync(&lgr->llc_del_link_work);
Karsten Graul555da9a2020-04-30 15:55:38 +02001757 if (lgr->delayed_event) {
1758 kfree(lgr->delayed_event);
1759 lgr->delayed_event = NULL;
1760 }
Karsten Graul00a049c2020-04-29 17:10:49 +02001761}
1762
Karsten Graul2a4c57a2018-05-15 17:04:59 +02001763int smc_llc_link_init(struct smc_link *link)
Karsten Graul877ae5b2018-05-02 16:56:44 +02001764{
1765 init_completion(&link->llc_testlink_resp);
1766 INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
Karsten Graul2a4c57a2018-05-15 17:04:59 +02001767 return 0;
Karsten Graulb32cf4a2018-05-15 17:04:58 +02001768}
1769
Karsten Graul00a049c2020-04-29 17:10:49 +02001770void smc_llc_link_active(struct smc_link *link)
Karsten Graulb32cf4a2018-05-15 17:04:58 +02001771{
Karsten Graul0a99be42020-05-05 15:01:20 +02001772 pr_warn_ratelimited("smc: SMC-R lg %*phN link added: id %*phN, "
1773 "peerid %*phN, ibdev %s, ibport %d\n",
1774 SMC_LGR_ID_SIZE, &link->lgr->id,
1775 SMC_LGR_ID_SIZE, &link->link_uid,
1776 SMC_LGR_ID_SIZE, &link->peer_link_uid,
1777 link->smcibdev->ibdev->name, link->ibport);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001778 link->state = SMC_LNK_ACTIVE;
Karsten Graul00a049c2020-04-29 17:10:49 +02001779 if (link->lgr->llc_testlink_time) {
1780 link->llc_testlink_time = link->lgr->llc_testlink_time * HZ;
Karsten Graul1020e1e2020-04-29 17:10:44 +02001781 schedule_delayed_work(&link->llc_testlink_wrk,
1782 link->llc_testlink_time);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001783 }
1784}
1785
Karsten Graul877ae5b2018-05-02 16:56:44 +02001786/* called in worker context */
Karsten Graul0a99be42020-05-05 15:01:20 +02001787void smc_llc_link_clear(struct smc_link *link, bool log)
Karsten Graul877ae5b2018-05-02 16:56:44 +02001788{
Karsten Graul0a99be42020-05-05 15:01:20 +02001789 if (log)
1790 pr_warn_ratelimited("smc: SMC-R lg %*phN link removed: id %*phN"
1791 ", peerid %*phN, ibdev %s, ibport %d\n",
1792 SMC_LGR_ID_SIZE, &link->lgr->id,
1793 SMC_LGR_ID_SIZE, &link->link_uid,
1794 SMC_LGR_ID_SIZE, &link->peer_link_uid,
1795 link->smcibdev->ibdev->name, link->ibport);
Karsten Graul2140ac22020-04-29 17:10:45 +02001796 complete(&link->llc_testlink_resp);
1797 cancel_delayed_work_sync(&link->llc_testlink_wrk);
1798 smc_wr_wakeup_reg_wait(link);
1799 smc_wr_wakeup_tx_wait(link);
Karsten Graul877ae5b2018-05-02 16:56:44 +02001800}
1801
Karsten Graul3d88a212020-04-30 15:55:44 +02001802/* register a new rtoken at the remote peer (for all links) */
1803int smc_llc_do_confirm_rkey(struct smc_link *send_link,
Karsten Graul44aa81c2018-05-15 17:04:55 +02001804 struct smc_buf_desc *rmb_desc)
1805{
Karsten Graul3d88a212020-04-30 15:55:44 +02001806 struct smc_link_group *lgr = send_link->lgr;
1807 struct smc_llc_qentry *qentry = NULL;
1808 int rc = 0;
Karsten Graul44aa81c2018-05-15 17:04:55 +02001809
Karsten Graul3d88a212020-04-30 15:55:44 +02001810 rc = smc_llc_send_confirm_rkey(send_link, rmb_desc);
1811 if (rc)
1812 goto out;
Karsten Graul44aa81c2018-05-15 17:04:55 +02001813 /* receive CONFIRM RKEY response from server over RoCE fabric */
Karsten Graul3d88a212020-04-30 15:55:44 +02001814 qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1815 SMC_LLC_CONFIRM_RKEY);
1816 if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
1817 rc = -EFAULT;
1818out:
1819 if (qentry)
1820 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
Karsten Graul3d88a212020-04-30 15:55:44 +02001821 return rc;
Karsten Graul44aa81c2018-05-15 17:04:55 +02001822}
1823
Karsten Graul60e03c62018-11-22 10:26:42 +01001824/* unregister an rtoken at the remote peer */
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001825int smc_llc_do_delete_rkey(struct smc_link_group *lgr,
Karsten Graul60e03c62018-11-22 10:26:42 +01001826 struct smc_buf_desc *rmb_desc)
1827{
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001828 struct smc_llc_qentry *qentry = NULL;
1829 struct smc_link *send_link;
Ursula Braun0b29ec62019-11-14 13:02:47 +01001830 int rc = 0;
Karsten Graul60e03c62018-11-22 10:26:42 +01001831
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001832 send_link = smc_llc_usable_link(lgr);
1833 if (!send_link)
1834 return -ENOLINK;
1835
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001836 /* protected by llc_flow control */
1837 rc = smc_llc_send_delete_rkey(send_link, rmb_desc);
Karsten Graul60e03c62018-11-22 10:26:42 +01001838 if (rc)
1839 goto out;
1840 /* receive DELETE RKEY response from server over RoCE fabric */
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001841 qentry = smc_llc_wait(lgr, send_link, SMC_LLC_WAIT_TIME,
1842 SMC_LLC_DELETE_RKEY);
1843 if (!qentry || (qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_RKEY_NEG))
Karsten Graul60e03c62018-11-22 10:26:42 +01001844 rc = -EFAULT;
Karsten Graul60e03c62018-11-22 10:26:42 +01001845out:
Karsten Graul6d74c3a2020-04-30 15:55:45 +02001846 if (qentry)
1847 smc_llc_flow_qentry_del(&lgr->llc_flow_lcl);
Karsten Graul60e03c62018-11-22 10:26:42 +01001848 return rc;
1849}
1850
Karsten Graul45fa8da2020-05-04 14:18:47 +02001851void smc_llc_link_set_uid(struct smc_link *link)
1852{
1853 __be32 link_uid;
1854
1855 link_uid = htonl(*((u32 *)link->lgr->id) + link->link_id);
1856 memcpy(link->link_uid, &link_uid, SMC_LGR_ID_SIZE);
1857}
1858
Karsten Graul649758f2020-05-04 14:18:48 +02001859/* save peers link user id, used for debug purposes */
1860void smc_llc_save_peer_uid(struct smc_llc_qentry *qentry)
1861{
1862 memcpy(qentry->link->peer_link_uid, qentry->msg.confirm_link.link_uid,
1863 SMC_LGR_ID_SIZE);
1864}
1865
Karsten Graul92334cf2020-04-30 15:55:41 +02001866/* evaluate confirm link request or response */
1867int smc_llc_eval_conf_link(struct smc_llc_qentry *qentry,
1868 enum smc_llc_reqresp type)
1869{
Karsten Graul45fa8da2020-05-04 14:18:47 +02001870 if (type == SMC_LLC_REQ) { /* SMC server assigns link_id */
Karsten Graul92334cf2020-04-30 15:55:41 +02001871 qentry->link->link_id = qentry->msg.confirm_link.link_num;
Karsten Graul45fa8da2020-05-04 14:18:47 +02001872 smc_llc_link_set_uid(qentry->link);
1873 }
Karsten Graul92334cf2020-04-30 15:55:41 +02001874 if (!(qentry->msg.raw.hdr.flags & SMC_LLC_FLAG_NO_RMBE_EYEC))
1875 return -ENOTSUPP;
1876 return 0;
1877}
1878
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001879/***************************** init, exit, misc ******************************/
1880
1881static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
1882 {
1883 .handler = smc_llc_rx_handler,
1884 .type = SMC_LLC_CONFIRM_LINK
1885 },
1886 {
Karsten Graul313164d2018-03-01 13:51:29 +01001887 .handler = smc_llc_rx_handler,
1888 .type = SMC_LLC_TEST_LINK
1889 },
1890 {
Karsten Graul4ed75de2018-03-01 13:51:30 +01001891 .handler = smc_llc_rx_handler,
Karsten Graul52bedf32018-03-01 13:51:32 +01001892 .type = SMC_LLC_ADD_LINK
1893 },
1894 {
1895 .handler = smc_llc_rx_handler,
Karsten Graul87f88cd2020-05-03 14:38:41 +02001896 .type = SMC_LLC_ADD_LINK_CONT
1897 },
1898 {
1899 .handler = smc_llc_rx_handler,
Karsten Graul52bedf32018-03-01 13:51:32 +01001900 .type = SMC_LLC_DELETE_LINK
1901 },
1902 {
1903 .handler = smc_llc_rx_handler,
Karsten Graul4ed75de2018-03-01 13:51:30 +01001904 .type = SMC_LLC_CONFIRM_RKEY
1905 },
1906 {
1907 .handler = smc_llc_rx_handler,
1908 .type = SMC_LLC_CONFIRM_RKEY_CONT
1909 },
1910 {
1911 .handler = smc_llc_rx_handler,
1912 .type = SMC_LLC_DELETE_RKEY
1913 },
1914 {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001915 .handler = NULL,
1916 }
1917};
1918
1919int __init smc_llc_init(void)
1920{
1921 struct smc_wr_rx_handler *handler;
1922 int rc = 0;
1923
1924 for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
1925 INIT_HLIST_NODE(&handler->list);
1926 rc = smc_wr_rx_register_handler(handler);
1927 if (rc)
1928 break;
1929 }
1930 return rc;
1931}