838a160a3bd9c9c28049d93c1c8da7fa65f0620f
[platform/kernel/linux-rpi.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  *  For now, we only support the necessary "confirm link" functionality
8  *  which happens for the first RoCE link after successful CLC handshake.
9  *
10  *  Copyright IBM Corp. 2016
11  *
12  *  Author(s):  Klaus Wacker <Klaus.Wacker@de.ibm.com>
13  *              Ursula Braun <ubraun@linux.vnet.ibm.com>
14  */
15
16 #include <net/tcp.h>
17 #include <rdma/ib_verbs.h>
18
19 #include "smc.h"
20 #include "smc_core.h"
21 #include "smc_clc.h"
22 #include "smc_llc.h"
23
24 #define SMC_LLC_DATA_LEN                40
25
26 struct smc_llc_hdr {
27         struct smc_wr_rx_hdr common;
28         u8 length;      /* 44 */
29         u8 reserved;
30         u8 flags;
31 };
32
33 #define SMC_LLC_FLAG_NO_RMBE_EYEC       0x03
34
35 struct smc_llc_msg_confirm_link {       /* type 0x01 */
36         struct smc_llc_hdr hd;
37         u8 sender_mac[ETH_ALEN];
38         u8 sender_gid[SMC_GID_SIZE];
39         u8 sender_qp_num[3];
40         u8 link_num;
41         u8 link_uid[SMC_LGR_ID_SIZE];
42         u8 max_links;
43         u8 reserved[9];
44 };
45
46 struct smc_llc_msg_test_link {          /* type 0x07 */
47         struct smc_llc_hdr hd;
48         u8 user_data[16];
49         u8 reserved[24];
50 };
51
52 struct smc_rmb_rtoken {
53         union {
54                 u8 num_rkeys;   /* first rtoken byte of CONFIRM LINK msg */
55                                 /* is actually the num of rtokens, first */
56                                 /* rtoken is always for the current link */
57                 u8 link_id;     /* link id of the rtoken */
58         };
59         __be32 rmb_key;
60         __be64 rmb_vaddr;
61 } __packed;                     /* format defined in RFC7609 */
62
63 #define SMC_LLC_RKEYS_PER_MSG   3
64
65 struct smc_llc_msg_confirm_rkey {       /* type 0x06 */
66         struct smc_llc_hdr hd;
67         struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
68         u8 reserved;
69 };
70
71 struct smc_llc_msg_confirm_rkey_cont {  /* type 0x08 */
72         struct smc_llc_hdr hd;
73         u8 num_rkeys;
74         struct smc_rmb_rtoken rtoken[SMC_LLC_RKEYS_PER_MSG];
75 };
76
77 #define SMC_LLC_DEL_RKEY_MAX    8
78 #define SMC_LLC_FLAG_RKEY_NEG   0x20
79
80 struct smc_llc_msg_delete_rkey {        /* type 0x09 */
81         struct smc_llc_hdr hd;
82         u8 num_rkeys;
83         u8 err_mask;
84         u8 reserved[2];
85         __be32 rkey[8];
86         u8 reserved2[4];
87 };
88
89 union smc_llc_msg {
90         struct smc_llc_msg_confirm_link confirm_link;
91
92         struct smc_llc_msg_confirm_rkey confirm_rkey;
93         struct smc_llc_msg_confirm_rkey_cont confirm_rkey_cont;
94         struct smc_llc_msg_delete_rkey delete_rkey;
95
96         struct smc_llc_msg_test_link test_link;
97         struct {
98                 struct smc_llc_hdr hdr;
99                 u8 data[SMC_LLC_DATA_LEN];
100         } raw;
101 };
102
103 #define SMC_LLC_FLAG_RESP               0x80
104
105 /********************************** send *************************************/
106
107 struct smc_llc_tx_pend {
108 };
109
110 /* handler for send/transmission completion of an LLC msg */
111 static void smc_llc_tx_handler(struct smc_wr_tx_pend_priv *pend,
112                                struct smc_link *link,
113                                enum ib_wc_status wc_status)
114 {
115         /* future work: handle wc_status error for recovery and failover */
116 }
117
118 /**
119  * smc_llc_add_pending_send() - add LLC control message to pending WQE transmits
120  * @link: Pointer to SMC link used for sending LLC control message.
121  * @wr_buf: Out variable returning pointer to work request payload buffer.
122  * @pend: Out variable returning pointer to private pending WR tracking.
123  *        It's the context the transmit complete handler will get.
124  *
125  * Reserves and pre-fills an entry for a pending work request send/tx.
126  * Used by mid-level smc_llc_send_msg() to prepare for later actual send/tx.
127  * Can sleep due to smc_get_ctrl_buf (if not in softirq context).
128  *
129  * Return: 0 on success, otherwise an error value.
130  */
131 static int smc_llc_add_pending_send(struct smc_link *link,
132                                     struct smc_wr_buf **wr_buf,
133                                     struct smc_wr_tx_pend_priv **pend)
134 {
135         int rc;
136
137         rc = smc_wr_tx_get_free_slot(link, smc_llc_tx_handler, wr_buf, pend);
138         if (rc < 0)
139                 return rc;
140         BUILD_BUG_ON_MSG(
141                 sizeof(union smc_llc_msg) > SMC_WR_BUF_SIZE,
142                 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_llc_msg)");
143         BUILD_BUG_ON_MSG(
144                 sizeof(union smc_llc_msg) != SMC_WR_TX_SIZE,
145                 "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()");
146         BUILD_BUG_ON_MSG(
147                 sizeof(struct smc_llc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
148                 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_llc_tx_pend)");
149         return 0;
150 }
151
152 /* high-level API to send LLC confirm link */
153 int smc_llc_send_confirm_link(struct smc_link *link, u8 mac[],
154                               union ib_gid *gid,
155                               enum smc_llc_reqresp reqresp)
156 {
157         struct smc_link_group *lgr = container_of(link, struct smc_link_group,
158                                                   lnk[SMC_SINGLE_LINK]);
159         struct smc_llc_msg_confirm_link *confllc;
160         struct smc_wr_tx_pend_priv *pend;
161         struct smc_wr_buf *wr_buf;
162         int rc;
163
164         rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
165         if (rc)
166                 return rc;
167         confllc = (struct smc_llc_msg_confirm_link *)wr_buf;
168         memset(confllc, 0, sizeof(*confllc));
169         confllc->hd.common.type = SMC_LLC_CONFIRM_LINK;
170         confllc->hd.length = sizeof(struct smc_llc_msg_confirm_link);
171         confllc->hd.flags |= SMC_LLC_FLAG_NO_RMBE_EYEC;
172         if (reqresp == SMC_LLC_RESP)
173                 confllc->hd.flags |= SMC_LLC_FLAG_RESP;
174         memcpy(confllc->sender_mac, mac, ETH_ALEN);
175         memcpy(confllc->sender_gid, gid, SMC_GID_SIZE);
176         hton24(confllc->sender_qp_num, link->roce_qp->qp_num);
177         /* confllc->link_num = SMC_SINGLE_LINK; already done by memset above */
178         memcpy(confllc->link_uid, lgr->id, SMC_LGR_ID_SIZE);
179         confllc->max_links = SMC_LINKS_PER_LGR_MAX;
180         /* send llc message */
181         rc = smc_wr_tx_send(link, pend);
182         return rc;
183 }
184
185 /* send LLC test link request or response */
186 int smc_llc_send_test_link(struct smc_link *link, u8 user_data[16],
187                            enum smc_llc_reqresp reqresp)
188 {
189         struct smc_llc_msg_test_link *testllc;
190         struct smc_wr_tx_pend_priv *pend;
191         struct smc_wr_buf *wr_buf;
192         int rc;
193
194         rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
195         if (rc)
196                 return rc;
197         testllc = (struct smc_llc_msg_test_link *)wr_buf;
198         memset(testllc, 0, sizeof(*testllc));
199         testllc->hd.common.type = SMC_LLC_TEST_LINK;
200         testllc->hd.length = sizeof(struct smc_llc_msg_test_link);
201         if (reqresp == SMC_LLC_RESP)
202                 testllc->hd.flags |= SMC_LLC_FLAG_RESP;
203         memcpy(testllc->user_data, user_data, sizeof(testllc->user_data));
204         /* send llc message */
205         rc = smc_wr_tx_send(link, pend);
206         return rc;
207 }
208
209 /* send a prepared message */
210 static int smc_llc_send_message(struct smc_link *link, void *llcbuf, int llclen)
211 {
212         struct smc_wr_tx_pend_priv *pend;
213         struct smc_wr_buf *wr_buf;
214         int rc;
215
216         rc = smc_llc_add_pending_send(link, &wr_buf, &pend);
217         if (rc)
218                 return rc;
219         memcpy(wr_buf, llcbuf, llclen);
220         /* send llc message */
221         rc = smc_wr_tx_send(link, pend);
222         return rc;
223 }
224
225 /********************************* receive ***********************************/
226
227 static void smc_llc_rx_confirm_link(struct smc_link *link,
228                                     struct smc_llc_msg_confirm_link *llc)
229 {
230         struct smc_link_group *lgr;
231         int conf_rc;
232
233         lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
234
235         /* RMBE eyecatchers are not supported */
236         if (llc->hd.flags & SMC_LLC_FLAG_NO_RMBE_EYEC)
237                 conf_rc = 0;
238         else
239                 conf_rc = ENOTSUPP;
240
241         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
242                 if (lgr->role == SMC_SERV) {
243                         link->llc_confirm_resp_rc = conf_rc;
244                         complete(&link->llc_confirm_resp);
245                 }
246         } else {
247                 if (lgr->role == SMC_CLNT) {
248                         link->llc_confirm_rc = conf_rc;
249                         link->link_id = llc->link_num;
250                         complete(&link->llc_confirm);
251                 }
252         }
253 }
254
255 static void smc_llc_rx_test_link(struct smc_link *link,
256                                  struct smc_llc_msg_test_link *llc)
257 {
258         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
259                 /* unused as long as we don't send this type of msg */
260         } else {
261                 smc_llc_send_test_link(link, llc->user_data, SMC_LLC_RESP);
262         }
263 }
264
265 static void smc_llc_rx_confirm_rkey(struct smc_link *link,
266                                     struct smc_llc_msg_confirm_rkey *llc)
267 {
268         struct smc_link_group *lgr;
269         int rc;
270
271         lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
272
273         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
274                 /* unused as long as we don't send this type of msg */
275         } else {
276                 rc = smc_rtoken_add(lgr,
277                                     llc->rtoken[0].rmb_vaddr,
278                                     llc->rtoken[0].rmb_key);
279
280                 /* ignore rtokens for other links, we have only one link */
281
282                 llc->hd.flags |= SMC_LLC_FLAG_RESP;
283                 if (rc < 0)
284                         llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
285                 smc_llc_send_message(link, (void *)llc, sizeof(*llc));
286         }
287 }
288
289 static void smc_llc_rx_confirm_rkey_cont(struct smc_link *link,
290                                       struct smc_llc_msg_confirm_rkey_cont *llc)
291 {
292         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
293                 /* unused as long as we don't send this type of msg */
294         } else {
295                 /* ignore rtokens for other links, we have only one link */
296                 llc->hd.flags |= SMC_LLC_FLAG_RESP;
297                 smc_llc_send_message(link, (void *)llc, sizeof(*llc));
298         }
299 }
300
301 static void smc_llc_rx_delete_rkey(struct smc_link *link,
302                                    struct smc_llc_msg_delete_rkey *llc)
303 {
304         struct smc_link_group *lgr;
305         u8 err_mask = 0;
306         int i, max;
307
308         lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
309
310         if (llc->hd.flags & SMC_LLC_FLAG_RESP) {
311                 /* unused as long as we don't send this type of msg */
312         } else {
313                 max = min_t(u8, llc->num_rkeys, SMC_LLC_DEL_RKEY_MAX);
314                 for (i = 0; i < max; i++) {
315                         if (smc_rtoken_delete(lgr, llc->rkey[i]))
316                                 err_mask |= 1 << (SMC_LLC_DEL_RKEY_MAX - 1 - i);
317                 }
318
319                 if (err_mask) {
320                         llc->hd.flags |= SMC_LLC_FLAG_RKEY_NEG;
321                         llc->err_mask = err_mask;
322                 }
323
324                 llc->hd.flags |= SMC_LLC_FLAG_RESP;
325                 smc_llc_send_message(link, (void *)llc, sizeof(*llc));
326         }
327 }
328
329 static void smc_llc_rx_handler(struct ib_wc *wc, void *buf)
330 {
331         struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
332         union smc_llc_msg *llc = buf;
333
334         if (wc->byte_len < sizeof(*llc))
335                 return; /* short message */
336         if (llc->raw.hdr.length != sizeof(*llc))
337                 return; /* invalid message */
338
339         switch (llc->raw.hdr.common.type) {
340         case SMC_LLC_TEST_LINK:
341                 smc_llc_rx_test_link(link, &llc->test_link);
342                 break;
343         case SMC_LLC_CONFIRM_LINK:
344                 smc_llc_rx_confirm_link(link, &llc->confirm_link);
345                 break;
346         case SMC_LLC_CONFIRM_RKEY:
347                 smc_llc_rx_confirm_rkey(link, &llc->confirm_rkey);
348                 break;
349         case SMC_LLC_CONFIRM_RKEY_CONT:
350                 smc_llc_rx_confirm_rkey_cont(link, &llc->confirm_rkey_cont);
351                 break;
352         case SMC_LLC_DELETE_RKEY:
353                 smc_llc_rx_delete_rkey(link, &llc->delete_rkey);
354                 break;
355         }
356 }
357
358 /***************************** init, exit, misc ******************************/
359
360 static struct smc_wr_rx_handler smc_llc_rx_handlers[] = {
361         {
362                 .handler        = smc_llc_rx_handler,
363                 .type           = SMC_LLC_CONFIRM_LINK
364         },
365         {
366                 .handler        = smc_llc_rx_handler,
367                 .type           = SMC_LLC_TEST_LINK
368         },
369         {
370                 .handler        = smc_llc_rx_handler,
371                 .type           = SMC_LLC_CONFIRM_RKEY
372         },
373         {
374                 .handler        = smc_llc_rx_handler,
375                 .type           = SMC_LLC_CONFIRM_RKEY_CONT
376         },
377         {
378                 .handler        = smc_llc_rx_handler,
379                 .type           = SMC_LLC_DELETE_RKEY
380         },
381         {
382                 .handler        = NULL,
383         }
384 };
385
386 int __init smc_llc_init(void)
387 {
388         struct smc_wr_rx_handler *handler;
389         int rc = 0;
390
391         for (handler = smc_llc_rx_handlers; handler->handler; handler++) {
392                 INIT_HLIST_NODE(&handler->list);
393                 rc = smc_wr_rx_register_handler(handler);
394                 if (rc)
395                         break;
396         }
397         return rc;
398 }