blob: 5800a6b43d830018ffc91a4272190508e8c9f233 [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"
20
Stefan Raspl0f627122018-03-01 13:51:26 +010021#define SMC_LLC_DATA_LEN 40
22
23struct smc_llc_hdr {
24 struct smc_wr_rx_hdr common;
25 u8 length; /* 44 */
Karsten Graul52bedf32018-03-01 13:51:32 +010026#if defined(__BIG_ENDIAN_BITFIELD)
27 u8 reserved:4,
28 add_link_rej_rsn:4;
29#elif defined(__LITTLE_ENDIAN_BITFIELD)
30 u8 add_link_rej_rsn:4,
31 reserved:4;
32#endif
Stefan Raspl0f627122018-03-01 13:51:26 +010033 u8 flags;
34};
35
Karsten Graul75d320d2018-03-01 13:51:31 +010036#define SMC_LLC_FLAG_NO_RMBE_EYEC 0x03
37
Stefan Raspl0f627122018-03-01 13:51:26 +010038struct smc_llc_msg_confirm_link { /* type 0x01 */
39 struct smc_llc_hdr hd;
40 u8 sender_mac[ETH_ALEN];
41 u8 sender_gid[SMC_GID_SIZE];
42 u8 sender_qp_num[3];
43 u8 link_num;
44 u8 link_uid[SMC_LGR_ID_SIZE];
45 u8 max_links;
46 u8 reserved[9];
47};
48
Karsten Graul52bedf32018-03-01 13:51:32 +010049#define SMC_LLC_FLAG_ADD_LNK_REJ 0x40
50#define SMC_LLC_REJ_RSN_NO_ALT_PATH 1
51
52#define SMC_LLC_ADD_LNK_MAX_LINKS 2
53
54struct smc_llc_msg_add_link { /* type 0x02 */
55 struct smc_llc_hdr hd;
56 u8 sender_mac[ETH_ALEN];
57 u8 reserved2[2];
58 u8 sender_gid[SMC_GID_SIZE];
59 u8 sender_qp_num[3];
60 u8 link_num;
61 u8 flags2; /* QP mtu */
62 u8 initial_psn[3];
63 u8 reserved[8];
64};
65
66#define SMC_LLC_FLAG_DEL_LINK_ALL 0x40
67#define SMC_LLC_FLAG_DEL_LINK_ORDERLY 0x20
68
69struct smc_llc_msg_del_link { /* type 0x04 */
70 struct smc_llc_hdr hd;
71 u8 link_num;
72 __be32 reason;
73 u8 reserved[35];
74} __packed; /* format defined in RFC7609 */
75
Karsten Graul313164d2018-03-01 13:51:29 +010076struct smc_llc_msg_test_link { /* type 0x07 */
77 struct smc_llc_hdr hd;
78 u8 user_data[16];
79 u8 reserved[24];
80};
81
Karsten Graul4ed75de2018-03-01 13:51:30 +010082struct smc_rmb_rtoken {
83 union {
84 u8 num_rkeys; /* first rtoken byte of CONFIRM LINK msg */
85 /* is actually the num of rtokens, first */
86 /* rtoken is always for the current link */
87 u8 link_id; /* link id of the rtoken */
88 };
89 __be32 rmb_key;
90 __be64 rmb_vaddr;
91} __packed; /* format defined in RFC7609 */
92
93#define SMC_LLC_RKEYS_PER_MSG 3
94
95struct smc_llc_msg_confirm_rkey { /* type 0x06 */
96 struct smc_llc_hdr hd;
97 struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
98 u8 reserved;
99};
100
101struct smc_llc_msg_confirm_rkey_cont { /* type 0x08 */
102 struct smc_llc_hdr hd;
103 u8 num_rkeys;
104 struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
105};
106
107#define SMC_LLC_DEL_RKEY_MAX 8
108#define SMC_LLC_FLAG_RKEY_NEG 0x20
109
110struct smc_llc_msg_delete_rkey { /* type 0x09 */
111 struct smc_llc_hdr hd;
112 u8 num_rkeys;
113 u8 err_mask;
114 u8 reserved[2];
115 __be32 rkey[8];
116 u8 reserved2[4];
117};
118
Stefan Raspl0f627122018-03-01 13:51:26 +0100119union smc_llc_msg {
120 struct smc_llc_msg_confirm_link confirm_link;
Karsten Graul52bedf32018-03-01 13:51:32 +0100121 struct smc_llc_msg_add_link add_link;
122 struct smc_llc_msg_del_link delete_link;
Karsten Graul4ed75de2018-03-01 13:51:30 +0100123
124 struct smc_llc_msg_confirm_rkey confirm_rkey;
125 struct smc_llc_msg_confirm_rkey_cont confirm_rkey_cont;
126 struct smc_llc_msg_delete_rkey delete_rkey;
127
Karsten Graul313164d2018-03-01 13:51:29 +0100128 struct smc_llc_msg_test_link test_link;
Stefan Raspl0f627122018-03-01 13:51:26 +0100129 struct {
130 struct smc_llc_hdr hdr;
131 u8 data[SMC_LLC_DATA_LEN];
132 } raw;
133};
134
135#define SMC_LLC_FLAG_RESP 0x80
136
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100137/********************************** send *************************************/
138
139struct smc_llc_tx_pend {
140};
141
142/* handler for send/transmission completion of an LLC msg */
143static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
144 struct smc_link *link,
145 enum ib_wc_status wc_status)
146{
147 /* future work: handle wc_status error for recovery and failover */
148}
149
150/**
151 * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
152 * @link: Pointer to SMC link used for sending LLC control message.
153 * @wr_buf: Out variable returning pointer to work request payload buffer.
154 * @pend: Out variable returning pointer to private pending WR tracking.
155 * It's the context the transmit complete handler will get.
156 *
157 * Reserves and pre-fills an entry for a pending work request send/tx.
158 * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
159 * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
160 *
161 * Return: 0 on success, otherwise an error value.
162 */
163static int smc_llc_add_pending_send(struct smc_link *link,
164 struct smc_wr_buf **wr_buf,
165 struct smc_wr_tx_pend_priv **pend)
166{
167 int rc;
168
169 rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, pend);
170 if (rc < 0)
171 return rc;
172 BUILD_BUG_ON_MSG(
173 sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
174 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
175 BUILD_BUG_ON_MSG(
176 sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
177 "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()");
178 BUILD_BUG_ON_MSG(
179 sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
180 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
181 return 0;
182}
183
184/* high-level API to send LLC confirm link */
185int smc_llc_send_confirm_link(struct smc_link *link, u8 mac[],
186 union ib_gid *gid,
187 enum smc_llc_reqresp reqresp)
188{
189 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
190 lnk[SMC_SINGLE_LINK]);
191 struct smc_llc_msg_confirm_link *confllc;
192 struct smc_wr_tx_pend_priv *pend;
193 struct smc_wr_buf *wr_buf;
194 int rc;
195
196 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
197 if (rc)
198 return rc;
199 confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
200 memset(confllc, 0, sizeof(*confllc));
201 confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
202 confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
Karsten Graul75d320d2018-03-01 13:51:31 +0100203 confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100204 if (reqresp == SMC_LLC_RESP)
205 confllc->hd.flags |= SMC_LLC_FLAG_RESP;
206 memcpy(confllc->sender_mac, mac, ETH_ALEN);
207 memcpy(confllc->sender_gid, gid, SMC_GID_SIZE);
208 hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
Karsten Graul2be922f2018-02-28 12:44:08 +0100209 confllc->link_num = link->link_id;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100210 memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
Karsten Graul52bedf32018-03-01 13:51:32 +0100211 confllc->max_links = SMC_LLC_ADD_LNK_MAX_LINKS; /* enforce peer resp. */
212 /* send llc message */
213 rc = smc_wr_tx_send(link, pend);
214 return rc;
215}
216
Karsten Graul44aa81c2018-05-15 17:04:55 +0200217/* send LLC confirm rkey request */
218static int smc_llc_send_confirm_rkey(struct smc_link *link,
219 struct smc_buf_desc *rmb_desc)
220{
221 struct smc_llc_msg_confirm_rkey *rkeyllc;
222 struct smc_wr_tx_pend_priv *pend;
223 struct smc_wr_buf *wr_buf;
224 int rc;
225
226 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
227 if (rc)
228 return rc;
229 rkeyllc = (struct smc_llc_msg_confirm_rkey *)wr_buf;
230 memset(rkeyllc, 0, sizeof(*rkeyllc));
231 rkeyllc->hd.common.type = SMC_LLC_CONFIRM_RKEY;
232 rkeyllc->hd.length = sizeof(struct smc_llc_msg_confirm_rkey);
233 rkeyllc->rtoken[0].rmb_key =
234 htonl(rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
235 rkeyllc->rtoken[0].rmb_vaddr = cpu_to_be64(
236 (u64)sg_dma_address(rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
237 /* send llc message */
238 rc = smc_wr_tx_send(link, pend);
239 return rc;
240}
241
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200242/* prepare an add link message */
243static void smc_llc_prep_add_link(struct smc_llc_msg_add_link *addllc,
244 struct smc_link *link, u8 mac[],
245 union ib_gid *gid,
246 enum smc_llc_reqresp reqresp)
247{
248 memset(addllc, 0, sizeof(*addllc));
249 addllc->hd.common.type = SMC_LLC_ADD_LINK;
250 addllc->hd.length = sizeof(struct smc_llc_msg_add_link);
251 if (reqresp == SMC_LLC_RESP) {
252 addllc->hd.flags |= SMC_LLC_FLAG_RESP;
253 /* always reject more links for now */
254 addllc->hd.flags |= SMC_LLC_FLAG_ADD_LNK_REJ;
255 addllc->hd.add_link_rej_rsn = SMC_LLC_REJ_RSN_NO_ALT_PATH;
256 }
257 memcpy(addllc->sender_mac, mac, ETH_ALEN);
258 memcpy(addllc->sender_gid, gid, SMC_GID_SIZE);
259}
260
Karsten Graul52bedf32018-03-01 13:51:32 +0100261/* send ADD LINK request or response */
262int smc_llc_send_add_link(struct smc_link *link, u8 mac[],
263 union ib_gid *gid,
264 enum smc_llc_reqresp reqresp)
265{
266 struct smc_llc_msg_add_link *addllc;
267 struct smc_wr_tx_pend_priv *pend;
268 struct smc_wr_buf *wr_buf;
269 int rc;
270
271 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
272 if (rc)
273 return rc;
274 addllc = (struct smc_llc_msg_add_link *)wr_buf;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200275 smc_llc_prep_add_link(addllc, link, mac, gid, reqresp);
Karsten Graul52bedf32018-03-01 13:51:32 +0100276 /* send llc message */
277 rc = smc_wr_tx_send(link, pend);
278 return rc;
279}
280
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200281/* prepare a delete link message */
282static void smc_llc_prep_delete_link(struct smc_llc_msg_del_link *delllc,
283 struct smc_link *link,
284 enum smc_llc_reqresp reqresp)
285{
286 memset(delllc, 0, sizeof(*delllc));
287 delllc->hd.common.type = SMC_LLC_DELETE_LINK;
288 delllc->hd.length = sizeof(struct smc_llc_msg_add_link);
289 if (reqresp == SMC_LLC_RESP)
290 delllc->hd.flags |= SMC_LLC_FLAG_RESP;
291 /* DEL_LINK_ALL because only 1 link supported */
292 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ALL;
293 delllc->hd.flags |= SMC_LLC_FLAG_DEL_LINK_ORDERLY;
294 delllc->link_num = link->link_id;
295}
296
Karsten Graul52bedf32018-03-01 13:51:32 +0100297/* send DELETE LINK request or response */
298int smc_llc_send_delete_link(struct smc_link *link,
299 enum smc_llc_reqresp reqresp)
300{
301 struct smc_llc_msg_del_link *delllc;
302 struct smc_wr_tx_pend_priv *pend;
303 struct smc_wr_buf *wr_buf;
304 int rc;
305
306 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
307 if (rc)
308 return rc;
309 delllc = (struct smc_llc_msg_del_link *)wr_buf;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200310 smc_llc_prep_delete_link(delllc, link, reqresp);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100311 /* send llc message */
312 rc = smc_wr_tx_send(link, pend);
313 return rc;
314}
315
Karsten Grauld97935f2018-05-15 17:04:57 +0200316/* send LLC test link request */
317static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
Karsten Graul313164d2018-03-01 13:51:29 +0100318{
319 struct smc_llc_msg_test_link *testllc;
320 struct smc_wr_tx_pend_priv *pend;
321 struct smc_wr_buf *wr_buf;
322 int rc;
323
324 rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
325 if (rc)
326 return rc;
327 testllc = (struct smc_llc_msg_test_link *)wr_buf;
328 memset(testllc, 0, sizeof(*testllc));
329 testllc->hd.common.type = SMC_LLC_TEST_LINK;
330 testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
Karsten Graul313164d2018-03-01 13:51:29 +0100331 memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
332 /* send llc message */
333 rc = smc_wr_tx_send(link, pend);
334 return rc;
335}
336
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200337struct smc_llc_send_work {
338 struct work_struct work;
339 struct smc_link *link;
340 int llclen;
341 union smc_llc_msg llcbuf;
342};
343
344/* worker that sends a prepared message */
345static void smc_llc_send_message_work(struct work_struct *work)
Karsten Graul4ed75de2018-03-01 13:51:30 +0100346{
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200347 struct smc_llc_send_work *llcwrk = container_of(work,
348 struct smc_llc_send_work, work);
Karsten Graul4ed75de2018-03-01 13:51:30 +0100349 struct smc_wr_tx_pend_priv *pend;
350 struct smc_wr_buf *wr_buf;
351 int rc;
352
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200353 if (llcwrk->link->state == SMC_LNK_INACTIVE)
354 goto out;
355 rc = smc_llc_add_pending_send(llcwrk->link, &wr_buf, &pend);
Karsten Graul4ed75de2018-03-01 13:51:30 +0100356 if (rc)
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200357 goto out;
358 memcpy(wr_buf, &llcwrk->llcbuf, llcwrk->llclen);
359 smc_wr_tx_send(llcwrk->link, pend);
360out:
361 kfree(llcwrk);
362}
363
364/* copy llcbuf and schedule an llc send on link */
365static int smc_llc_send_message(struct smc_link *link, void *llcbuf, int llclen)
366{
367 struct smc_llc_send_work *wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
368
369 if (!wrk)
370 return -ENOMEM;
371 INIT_WORK(&wrk->work, smc_llc_send_message_work);
372 wrk->link = link;
373 wrk->llclen = llclen;
374 memcpy(&wrk->llcbuf, llcbuf, llclen);
375 queue_work(link->llc_wq, &wrk->work);
376 return 0;
Karsten Graul4ed75de2018-03-01 13:51:30 +0100377}
378
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379/********************************* receive ***********************************/
380
381static void smc_llc_rx_confirm_link(struct smc_link *link,
382 struct smc_llc_msg_confirm_link *llc)
383{
384 struct smc_link_group *lgr;
Karsten Graul75d320d2018-03-01 13:51:31 +0100385 int conf_rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386
387 lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
Karsten Graul75d320d2018-03-01 13:51:31 +0100388
389 /* RMBE eyecatchers are not supported */
390 if (llc->hd.flags & SMC_LLC_FLAG_NO_RMBE_EYEC)
391 conf_rc = 0;
392 else
393 conf_rc = ENOTSUPP;
394
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100396 if (lgr->role == SMC_SERV &&
397 link->state == SMC_LNK_ACTIVATING) {
Karsten Graul75d320d2018-03-01 13:51:31 +0100398 link->llc_confirm_resp_rc = conf_rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100399 complete(&link->llc_confirm_resp);
Karsten Graul75d320d2018-03-01 13:51:31 +0100400 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100401 } else {
Karsten Graul52bedf32018-03-01 13:51:32 +0100402 if (lgr->role == SMC_CLNT &&
403 link->state == SMC_LNK_ACTIVATING) {
Karsten Graul75d320d2018-03-01 13:51:31 +0100404 link->llc_confirm_rc = conf_rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100405 link->link_id = llc->link_num;
406 complete(&link->llc_confirm);
407 }
408 }
409}
410
Karsten Graul52bedf32018-03-01 13:51:32 +0100411static void smc_llc_rx_add_link(struct smc_link *link,
412 struct smc_llc_msg_add_link *llc)
413{
414 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
415 lnk[SMC_SINGLE_LINK]);
416
417 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
418 if (link->state == SMC_LNK_ACTIVATING)
419 complete(&link->llc_add_resp);
420 } else {
421 if (link->state == SMC_LNK_ACTIVATING) {
422 complete(&link->llc_add);
423 return;
424 }
425
426 if (lgr->role == SMC_SERV) {
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200427 smc_llc_prep_add_link(llc, link,
Karsten Graul52bedf32018-03-01 13:51:32 +0100428 link->smcibdev->mac[link->ibport - 1],
429 &link->smcibdev->gid[link->ibport - 1],
430 SMC_LLC_REQ);
431
432 } else {
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200433 smc_llc_prep_add_link(llc, link,
Karsten Graul52bedf32018-03-01 13:51:32 +0100434 link->smcibdev->mac[link->ibport - 1],
435 &link->smcibdev->gid[link->ibport - 1],
436 SMC_LLC_RESP);
437 }
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200438 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul52bedf32018-03-01 13:51:32 +0100439 }
440}
441
442static void smc_llc_rx_delete_link(struct smc_link *link,
443 struct smc_llc_msg_del_link *llc)
444{
445 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
446 lnk[SMC_SINGLE_LINK]);
447
448 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
449 if (lgr->role == SMC_SERV)
450 smc_lgr_terminate(lgr);
451 } else {
452 if (lgr->role == SMC_SERV) {
Karsten Graul9651b932018-03-01 13:51:33 +0100453 smc_lgr_forget(lgr);
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200454 smc_llc_prep_delete_link(llc, link, SMC_LLC_REQ);
455 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul52bedf32018-03-01 13:51:32 +0100456 } else {
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200457 smc_llc_prep_delete_link(llc, link, SMC_LLC_RESP);
458 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul52bedf32018-03-01 13:51:32 +0100459 smc_lgr_terminate(lgr);
460 }
461 }
462}
463
Karsten Graul313164d2018-03-01 13:51:29 +0100464static void smc_llc_rx_test_link(struct smc_link *link,
465 struct smc_llc_msg_test_link *llc)
466{
467 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
Karsten Graul877ae5b2018-05-02 16:56:44 +0200468 if (link->state == SMC_LNK_ACTIVE)
469 complete(&link->llc_testlink_resp);
Karsten Graul313164d2018-03-01 13:51:29 +0100470 } else {
Karsten Grauld97935f2018-05-15 17:04:57 +0200471 llc->hd.flags |= SMC_LLC_FLAG_RESP;
472 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul313164d2018-03-01 13:51:29 +0100473 }
474}
475
Karsten Graul4ed75de2018-03-01 13:51:30 +0100476static void smc_llc_rx_confirm_rkey(struct smc_link *link,
477 struct smc_llc_msg_confirm_rkey *llc)
478{
479 struct smc_link_group *lgr;
480 int rc;
481
482 lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
483
484 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
Karsten Graul44aa81c2018-05-15 17:04:55 +0200485 link->llc_confirm_rkey_rc = llc->hd.flags &
486 SMC_LLC_FLAG_RKEY_NEG;
487 complete(&link->llc_confirm_rkey);
Karsten Graul4ed75de2018-03-01 13:51:30 +0100488 } else {
489 rc = smc_rtoken_add(lgr,
490 llc->rtoken[0].rmb_vaddr,
491 llc->rtoken[0].rmb_key);
492
493 /* ignore rtokens for other links, we have only one link */
494
495 llc->hd.flags |= SMC_LLC_FLAG_RESP;
496 if (rc < 0)
497 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
Karsten Graul9fcdf8e2018-05-15 17:04:56 +0200498 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul4ed75de2018-03-01 13:51:30 +0100499 }
500}
501
502static void smc_llc_rx_confirm_rkey_cont(struct smc_link *link,
503 struct smc_llc_msg_confirm_rkey_cont *llc)
504{
505 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
506 /* unused as long as we don't send this type of msg */
507 } else {
508 /* ignore rtokens for other links, we have only one link */
509 llc->hd.flags |= SMC_LLC_FLAG_RESP;
Karsten Graul9fcdf8e2018-05-15 17:04:56 +0200510 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul4ed75de2018-03-01 13:51:30 +0100511 }
512}
513
514static void smc_llc_rx_delete_rkey(struct smc_link *link,
515 struct smc_llc_msg_delete_rkey *llc)
516{
517 struct smc_link_group *lgr;
518 u8 err_mask = 0;
519 int i, max;
520
521 lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
522
523 if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
524 /* unused as long as we don't send this type of msg */
525 } else {
526 max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
527 for (i = 0; i < max; i++) {
528 if (smc_rtoken_delete(lgr, llc->rkey[i]))
529 err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
530 }
531
532 if (err_mask) {
533 llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
534 llc->err_mask = err_mask;
535 }
536
537 llc->hd.flags |= SMC_LLC_FLAG_RESP;
Karsten Graul9fcdf8e2018-05-15 17:04:56 +0200538 smc_llc_send_message(link, llc, sizeof(*llc));
Karsten Graul4ed75de2018-03-01 13:51:30 +0100539 }
540}
541
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100542static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
543{
544 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
545 union smc_llc_msg *llc = buf;
546
547 if (wc->byte_len < sizeof(*llc))
548 return; /* short message */
549 if (llc->raw.hdr.length != sizeof(*llc))
550 return; /* invalid message */
Karsten Graul8f332a72018-05-15 17:05:02 +0200551 if (link->state == SMC_LNK_INACTIVE)
552 return; /* link not active, drop msg */
Karsten Graul313164d2018-03-01 13:51:29 +0100553
554 switch (llc->raw.hdr.common.type) {
555 case SMC_LLC_TEST_LINK:
556 smc_llc_rx_test_link(link, &llc->test_link);
557 break;
558 case SMC_LLC_CONFIRM_LINK:
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100559 smc_llc_rx_confirm_link(link, &llc->confirm_link);
Karsten Graul313164d2018-03-01 13:51:29 +0100560 break;
Karsten Graul52bedf32018-03-01 13:51:32 +0100561 case SMC_LLC_ADD_LINK:
562 smc_llc_rx_add_link(link, &llc->add_link);
563 break;
564 case SMC_LLC_DELETE_LINK:
565 smc_llc_rx_delete_link(link, &llc->delete_link);
566 break;
Karsten Graul4ed75de2018-03-01 13:51:30 +0100567 case SMC_LLC_CONFIRM_RKEY:
568 smc_llc_rx_confirm_rkey(link, &llc->confirm_rkey);
569 break;
570 case SMC_LLC_CONFIRM_RKEY_CONT:
571 smc_llc_rx_confirm_rkey_cont(link, &llc->confirm_rkey_cont);
572 break;
573 case SMC_LLC_DELETE_RKEY:
574 smc_llc_rx_delete_rkey(link, &llc->delete_rkey);
575 break;
Karsten Graul313164d2018-03-01 13:51:29 +0100576 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100577}
578
Karsten Graul44aa81c2018-05-15 17:04:55 +0200579/***************************** worker, utils *********************************/
Karsten Graul877ae5b2018-05-02 16:56:44 +0200580
581static void smc_llc_testlink_work(struct work_struct *work)
582{
583 struct smc_link *link = container_of(to_delayed_work(work),
584 struct smc_link, llc_testlink_wrk);
585 unsigned long next_interval;
586 struct smc_link_group *lgr;
587 unsigned long expire_time;
588 u8 user_data[16] = { 0 };
589 int rc;
590
591 lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
592 if (link->state != SMC_LNK_ACTIVE)
593 return; /* don't reschedule worker */
594 expire_time = link->wr_rx_tstamp + link->llc_testlink_time;
595 if (time_is_after_jiffies(expire_time)) {
596 next_interval = expire_time - jiffies;
597 goto out;
598 }
599 reinit_completion(&link->llc_testlink_resp);
Karsten Grauld97935f2018-05-15 17:04:57 +0200600 smc_llc_send_test_link(link, user_data);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200601 /* receive TEST LINK response over RoCE fabric */
602 rc = wait_for_completion_interruptible_timeout(&link->llc_testlink_resp,
603 SMC_LLC_WAIT_TIME);
604 if (rc <= 0) {
605 smc_lgr_terminate(lgr);
606 return;
607 }
608 next_interval = link->llc_testlink_time;
609out:
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200610 queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
611 next_interval);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200612}
613
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200614int smc_llc_link_init(struct smc_link *link)
Karsten Graul877ae5b2018-05-02 16:56:44 +0200615{
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200616 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
617 lnk[SMC_SINGLE_LINK]);
618 link->llc_wq = alloc_ordered_workqueue("llc_wq-%x:%x)", WQ_MEM_RECLAIM,
619 *((u32 *)lgr->id),
620 link->link_id);
621 if (!link->llc_wq)
622 return -ENOMEM;
Karsten Graulb32cf4a2018-05-15 17:04:58 +0200623 init_completion(&link->llc_confirm);
624 init_completion(&link->llc_confirm_resp);
625 init_completion(&link->llc_add);
626 init_completion(&link->llc_add_resp);
627 init_completion(&link->llc_confirm_rkey);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200628 init_completion(&link->llc_testlink_resp);
629 INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200630 return 0;
Karsten Graulb32cf4a2018-05-15 17:04:58 +0200631}
632
633void smc_llc_link_active(struct smc_link *link, int testlink_time)
634{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200635 link->state = SMC_LNK_ACTIVE;
636 if (testlink_time) {
637 link->llc_testlink_time = testlink_time * HZ;
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200638 queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
639 link->llc_testlink_time);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200640 }
641}
642
643/* called in tasklet context */
644void smc_llc_link_inactive(struct smc_link *link)
645{
646 link->state = SMC_LNK_INACTIVE;
647 cancel_delayed_work(&link->llc_testlink_wrk);
648}
649
650/* called in worker context */
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200651void smc_llc_link_clear(struct smc_link *link)
Karsten Graul877ae5b2018-05-02 16:56:44 +0200652{
Karsten Graul2a4c57a2018-05-15 17:04:59 +0200653 flush_workqueue(link->llc_wq);
654 destroy_workqueue(link->llc_wq);
Karsten Graul877ae5b2018-05-02 16:56:44 +0200655}
656
Karsten Graul44aa81c2018-05-15 17:04:55 +0200657/* register a new rtoken at the remote peer */
658int smc_llc_do_confirm_rkey(struct smc_link *link,
659 struct smc_buf_desc *rmb_desc)
660{
661 int rc;
662
663 reinit_completion(&link->llc_confirm_rkey);
664 smc_llc_send_confirm_rkey(link, rmb_desc);
665 /* receive CONFIRM RKEY response from server over RoCE fabric */
666 rc = wait_for_completion_interruptible_timeout(&link->llc_confirm_rkey,
667 SMC_LLC_WAIT_TIME);
668 if (rc <= 0 || link->llc_confirm_rkey_rc)
669 return -EFAULT;
670 return 0;
671}
672
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100673/***************************** init, exit, misc ******************************/
674
675static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
676 {
677 .handler = smc_llc_rx_handler,
678 .type = SMC_LLC_CONFIRM_LINK
679 },
680 {
Karsten Graul313164d2018-03-01 13:51:29 +0100681 .handler = smc_llc_rx_handler,
682 .type = SMC_LLC_TEST_LINK
683 },
684 {
Karsten Graul4ed75de2018-03-01 13:51:30 +0100685 .handler = smc_llc_rx_handler,
Karsten Graul52bedf32018-03-01 13:51:32 +0100686 .type = SMC_LLC_ADD_LINK
687 },
688 {
689 .handler = smc_llc_rx_handler,
690 .type = SMC_LLC_DELETE_LINK
691 },
692 {
693 .handler = smc_llc_rx_handler,
Karsten Graul4ed75de2018-03-01 13:51:30 +0100694 .type = SMC_LLC_CONFIRM_RKEY
695 },
696 {
697 .handler = smc_llc_rx_handler,
698 .type = SMC_LLC_CONFIRM_RKEY_CONT
699 },
700 {
701 .handler = smc_llc_rx_handler,
702 .type = SMC_LLC_DELETE_RKEY
703 },
704 {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100705 .handler = NULL,
706 }
707};
708
709int __init smc_llc_init(void)
710{
711 struct smc_wr_rx_handler *handler;
712 int rc = 0;
713
714 for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
715 INIT_HLIST_NODE(&handler->list);
716 rc = smc_wr_rx_register_handler(handler);
717 if (rc)
718 break;
719 }
720 return rc;
721}