Merge branch 'for-4.19/wiimote' into for-linus
[linux-2.6-microblaze.git] / net / smc / smc_llc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  Link Layer Control (LLC)
6  *
7  *  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
21 #define SMC_LLC_DATA_LEN                40
22
23 struct smc_llc_hdr {
24         struct smc_wr_rx_hdr common;
25         u8 length;      /* 44 */
26 #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
33         u8 flags;
34 };
35
36 #define SMC_LLC_FLAG_NO_RMBE_EYEC       0x03
37
38 struct 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
49 #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
54 struct 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
69 struct 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
76 struct smc_llc_msg_test_link {          /* type 0x07 */
77         struct smc_llc_hdr hd;
78         u8 user_data[16];
79         u8 reserved[24];
80 };
81
82 struct 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
95 struct 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
101 struct 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
110 struct 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
119 union smc_llc_msg {
120         struct smc_llc_msg_confirm_link confirm_link;
121         struct smc_llc_msg_add_link add_link;
122         struct smc_llc_msg_del_link delete_link;
123
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
128         struct smc_llc_msg_test_link test_link;
129         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
137 /********************************** send *************************************/
138
139 struct smc_llc_tx_pend {
140 };
141
142 /* handler for send/transmission completion of an LLC msg */
143 static 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  */
163 static 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 */
185 int 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);
203         confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
204         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);
209         confllc->link_num = link->link_id;
210         memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
211         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
217 /* send LLC confirm rkey request */
218 static 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
242 /* prepare an add link message */
243 static 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
261 /* send ADD LINK request or response */
262 int 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;
275         smc_llc_prep_add_link(addllc, link, mac, gid, reqresp);
276         /* send llc message */
277         rc = smc_wr_tx_send(link, pend);
278         return rc;
279 }
280
281 /* prepare a delete link message */
282 static 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
297 /* send DELETE LINK request or response */
298 int 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;
310         smc_llc_prep_delete_link(delllc, link, reqresp);
311         /* send llc message */
312         rc = smc_wr_tx_send(link, pend);
313         return rc;
314 }
315
316 /* send LLC test link request */
317 static int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16])
318 {
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);
331         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
337 struct 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 */
345 static void smc_llc_send_message_work(struct work_struct *work)
346 {
347         struct smc_llc_send_work *llcwrk = container_of(work,
348                                                 struct smc_llc_send_work, work);
349         struct smc_wr_tx_pend_priv *pend;
350         struct smc_wr_buf *wr_buf;
351         int rc;
352
353         if (llcwrk->link->state == SMC_LNK_INACTIVE)
354                 goto out;
355         rc = smc_llc_add_pending_send(llcwrk->link, &wr_buf, &pend);
356         if (rc)
357                 goto out;
358         memcpy(wr_buf, &llcwrk->llcbuf, llcwrk->llclen);
359         smc_wr_tx_send(llcwrk->link, pend);
360 out:
361         kfree(llcwrk);
362 }
363
364 /* copy llcbuf and schedule an llc send on link */
365 static 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;
377 }
378
379 /********************************* receive ***********************************/
380
381 static 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;
385         int conf_rc;
386
387         lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
388
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
395         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
396                 if (lgr->role == SMC_SERV &&
397                     link->state == SMC_LNK_ACTIVATING) {
398                         link->llc_confirm_resp_rc = conf_rc;
399                         complete(&link->llc_confirm_resp);
400                 }
401         } else {
402                 if (lgr->role == SMC_CLNT &&
403                     link->state == SMC_LNK_ACTIVATING) {
404                         link->llc_confirm_rc = conf_rc;
405                         link->link_id = llc->link_num;
406                         complete(&link->llc_confirm);
407                 }
408         }
409 }
410
411 static 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) {
427                         smc_llc_prep_add_link(llc, link,
428                                         link->smcibdev->mac[link->ibport - 1],
429                                         &link->smcibdev->gid[link->ibport - 1],
430                                         SMC_LLC_REQ);
431
432                 } else {
433                         smc_llc_prep_add_link(llc, link,
434                                         link->smcibdev->mac[link->ibport - 1],
435                                         &link->smcibdev->gid[link->ibport - 1],
436                                         SMC_LLC_RESP);
437                 }
438                 smc_llc_send_message(link, llc, sizeof(*llc));
439         }
440 }
441
442 static 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) {
453                         smc_lgr_forget(lgr);
454                         smc_llc_prep_delete_link(llc, link, SMC_LLC_REQ);
455                         smc_llc_send_message(link, llc, sizeof(*llc));
456                 } else {
457                         smc_llc_prep_delete_link(llc, link, SMC_LLC_RESP);
458                         smc_llc_send_message(link, llc, sizeof(*llc));
459                         smc_lgr_terminate(lgr);
460                 }
461         }
462 }
463
464 static 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) {
468                 if (link->state == SMC_LNK_ACTIVE)
469                         complete(&link->llc_testlink_resp);
470         } else {
471                 llc->hd.flags |= SMC_LLC_FLAG_RESP;
472                 smc_llc_send_message(link, llc, sizeof(*llc));
473         }
474 }
475
476 static 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) {
485                 link->llc_confirm_rkey_rc = llc->hd.flags &
486                                             SMC_LLC_FLAG_RKEY_NEG;
487                 complete(&link->llc_confirm_rkey);
488         } 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;
498                 smc_llc_send_message(link, llc, sizeof(*llc));
499         }
500 }
501
502 static 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;
510                 smc_llc_send_message(link, llc, sizeof(*llc));
511         }
512 }
513
514 static 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;
538                 smc_llc_send_message(link, llc, sizeof(*llc));
539         }
540 }
541
542 static 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 */
551         if (link->state == SMC_LNK_INACTIVE)
552                 return; /* link not active, drop msg */
553
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:
559                 smc_llc_rx_confirm_link(link, &llc->confirm_link);
560                 break;
561         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;
567         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;
576         }
577 }
578
579 /***************************** worker, utils *********************************/
580
581 static 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);
600         smc_llc_send_test_link(link, user_data);
601         /* 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;
609 out:
610         queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
611                            next_interval);
612 }
613
614 int smc_llc_link_init(struct smc_link *link)
615 {
616         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;
623         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);
628         init_completion(&link->llc_testlink_resp);
629         INIT_DELAYED_WORK(&link->llc_testlink_wrk, smc_llc_testlink_work);
630         return 0;
631 }
632
633 void smc_llc_link_active(struct smc_link *link, int testlink_time)
634 {
635         link->state = SMC_LNK_ACTIVE;
636         if (testlink_time) {
637                 link->llc_testlink_time = testlink_time * HZ;
638                 queue_delayed_work(link->llc_wq, &link->llc_testlink_wrk,
639                                    link->llc_testlink_time);
640         }
641 }
642
643 /* called in tasklet context */
644 void 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 */
651 void smc_llc_link_clear(struct smc_link *link)
652 {
653         flush_workqueue(link->llc_wq);
654         destroy_workqueue(link->llc_wq);
655 }
656
657 /* register a new rtoken at the remote peer */
658 int 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
673 /***************************** init, exit, misc ******************************/
674
675 static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
676         {
677                 .handler        = smc_llc_rx_handler,
678                 .type           = SMC_LLC_CONFIRM_LINK
679         },
680         {
681                 .handler        = smc_llc_rx_handler,
682                 .type           = SMC_LLC_TEST_LINK
683         },
684         {
685                 .handler        = smc_llc_rx_handler,
686                 .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,
694                 .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         {
705                 .handler        = NULL,
706         }
707 };
708
709 int __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 }