]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
0cfdd8f9 UB |
2 | /* |
3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE | |
4 | * | |
5 | * Basic Transport Functions exploiting Infiniband API | |
6 | * | |
7 | * Copyright IBM Corp. 2016 | |
8 | * | |
9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> | |
10 | */ | |
11 | ||
12 | #include <linux/socket.h> | |
13 | #include <linux/if_vlan.h> | |
14 | #include <linux/random.h> | |
15 | #include <linux/workqueue.h> | |
16 | #include <net/tcp.h> | |
17 | #include <net/sock.h> | |
18 | #include <rdma/ib_verbs.h> | |
19 | ||
20 | #include "smc.h" | |
21 | #include "smc_clc.h" | |
22 | #include "smc_core.h" | |
23 | #include "smc_ib.h" | |
f38ba179 | 24 | #include "smc_wr.h" |
9bf9abea | 25 | #include "smc_llc.h" |
5f08318f | 26 | #include "smc_cdc.h" |
b38d7324 | 27 | #include "smc_close.h" |
0cfdd8f9 | 28 | |
5bc11ddb UB |
29 | #define SMC_LGR_NUM_INCR 256 |
30 | #define SMC_LGR_FREE_DELAY_SERV (600 * HZ) | |
31 | #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10) | |
0cfdd8f9 | 32 | |
9bf9abea UB |
33 | static u32 smc_lgr_num; /* unique link group number */ |
34 | ||
0cfdd8f9 UB |
35 | /* Register connection's alert token in our lookup structure. |
36 | * To use rbtrees we have to implement our own insert core. | |
37 | * Requires @conns_lock | |
38 | * @smc connection to register | |
39 | * Returns 0 on success, != otherwise. | |
40 | */ | |
41 | static void smc_lgr_add_alert_token(struct smc_connection *conn) | |
42 | { | |
43 | struct rb_node **link, *parent = NULL; | |
44 | u32 token = conn->alert_token_local; | |
45 | ||
46 | link = &conn->lgr->conns_all.rb_node; | |
47 | while (*link) { | |
48 | struct smc_connection *cur = rb_entry(*link, | |
49 | struct smc_connection, alert_node); | |
50 | ||
51 | parent = *link; | |
52 | if (cur->alert_token_local > token) | |
53 | link = &parent->rb_left; | |
54 | else | |
55 | link = &parent->rb_right; | |
56 | } | |
57 | /* Put the new node there */ | |
58 | rb_link_node(&conn->alert_node, parent, link); | |
59 | rb_insert_color(&conn->alert_node, &conn->lgr->conns_all); | |
60 | } | |
61 | ||
62 | /* Register connection in link group by assigning an alert token | |
63 | * registered in a search tree. | |
64 | * Requires @conns_lock | |
65 | * Note that '0' is a reserved value and not assigned. | |
66 | */ | |
67 | static void smc_lgr_register_conn(struct smc_connection *conn) | |
68 | { | |
69 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); | |
70 | static atomic_t nexttoken = ATOMIC_INIT(0); | |
71 | ||
72 | /* find a new alert_token_local value not yet used by some connection | |
73 | * in this link group | |
74 | */ | |
75 | sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */ | |
76 | while (!conn->alert_token_local) { | |
77 | conn->alert_token_local = atomic_inc_return(&nexttoken); | |
78 | if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr)) | |
79 | conn->alert_token_local = 0; | |
80 | } | |
81 | smc_lgr_add_alert_token(conn); | |
82 | conn->lgr->conns_num++; | |
83 | } | |
84 | ||
85 | /* Unregister connection and reset the alert token of the given connection< | |
86 | */ | |
87 | static void __smc_lgr_unregister_conn(struct smc_connection *conn) | |
88 | { | |
89 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); | |
90 | struct smc_link_group *lgr = conn->lgr; | |
91 | ||
92 | rb_erase(&conn->alert_node, &lgr->conns_all); | |
93 | lgr->conns_num--; | |
94 | conn->alert_token_local = 0; | |
95 | conn->lgr = NULL; | |
96 | sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */ | |
97 | } | |
98 | ||
99 | /* Unregister connection and trigger lgr freeing if applicable | |
100 | */ | |
101 | static void smc_lgr_unregister_conn(struct smc_connection *conn) | |
102 | { | |
103 | struct smc_link_group *lgr = conn->lgr; | |
104 | int reduced = 0; | |
105 | ||
106 | write_lock_bh(&lgr->conns_lock); | |
107 | if (conn->alert_token_local) { | |
108 | reduced = 1; | |
109 | __smc_lgr_unregister_conn(conn); | |
110 | } | |
111 | write_unlock_bh(&lgr->conns_lock); | |
5bc11ddb UB |
112 | if (!reduced || lgr->conns_num) |
113 | return; | |
114 | /* client link group creation always follows the server link group | |
115 | * creation. For client use a somewhat higher removal delay time, | |
116 | * otherwise there is a risk of out-of-sync link groups. | |
117 | */ | |
118 | mod_delayed_work(system_wq, &lgr->free_work, | |
119 | lgr->role == SMC_CLNT ? SMC_LGR_FREE_DELAY_CLNT : | |
120 | SMC_LGR_FREE_DELAY_SERV); | |
0cfdd8f9 UB |
121 | } |
122 | ||
123 | static void smc_lgr_free_work(struct work_struct *work) | |
124 | { | |
125 | struct smc_link_group *lgr = container_of(to_delayed_work(work), | |
126 | struct smc_link_group, | |
127 | free_work); | |
128 | bool conns; | |
129 | ||
130 | spin_lock_bh(&smc_lgr_list.lock); | |
131 | read_lock_bh(&lgr->conns_lock); | |
132 | conns = RB_EMPTY_ROOT(&lgr->conns_all); | |
133 | read_unlock_bh(&lgr->conns_lock); | |
134 | if (!conns) { /* number of lgr connections is no longer zero */ | |
135 | spin_unlock_bh(&smc_lgr_list.lock); | |
136 | return; | |
137 | } | |
138 | list_del_init(&lgr->list); /* remove from smc_lgr_list */ | |
139 | spin_unlock_bh(&smc_lgr_list.lock); | |
140 | smc_lgr_free(lgr); | |
141 | } | |
142 | ||
143 | /* create a new SMC link group */ | |
144 | static int smc_lgr_create(struct smc_sock *smc, __be32 peer_in_addr, | |
145 | struct smc_ib_device *smcibdev, u8 ibport, | |
146 | char *peer_systemid, unsigned short vlan_id) | |
147 | { | |
148 | struct smc_link_group *lgr; | |
149 | struct smc_link *lnk; | |
150 | u8 rndvec[3]; | |
151 | int rc = 0; | |
cd6851f3 | 152 | int i; |
0cfdd8f9 UB |
153 | |
154 | lgr = kzalloc(sizeof(*lgr), GFP_KERNEL); | |
155 | if (!lgr) { | |
156 | rc = -ENOMEM; | |
157 | goto out; | |
158 | } | |
159 | lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT; | |
160 | lgr->sync_err = false; | |
161 | lgr->daddr = peer_in_addr; | |
162 | memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN); | |
163 | lgr->vlan_id = vlan_id; | |
cd6851f3 UB |
164 | rwlock_init(&lgr->sndbufs_lock); |
165 | rwlock_init(&lgr->rmbs_lock); | |
166 | for (i = 0; i < SMC_RMBE_SIZES; i++) { | |
167 | INIT_LIST_HEAD(&lgr->sndbufs[i]); | |
168 | INIT_LIST_HEAD(&lgr->rmbs[i]); | |
169 | } | |
9bf9abea UB |
170 | smc_lgr_num += SMC_LGR_NUM_INCR; |
171 | memcpy(&lgr->id, (u8 *)&smc_lgr_num, SMC_LGR_ID_SIZE); | |
0cfdd8f9 UB |
172 | INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work); |
173 | lgr->conns_all = RB_ROOT; | |
174 | ||
175 | lnk = &lgr->lnk[SMC_SINGLE_LINK]; | |
176 | /* initialize link */ | |
d42cd4df | 177 | lnk->link_id = SMC_SINGLE_LINK; |
0cfdd8f9 UB |
178 | lnk->smcibdev = smcibdev; |
179 | lnk->ibport = ibport; | |
180 | lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu; | |
bd4ad577 UB |
181 | if (!smcibdev->initialized) |
182 | smc_ib_setup_per_ibdev(smcibdev); | |
0cfdd8f9 UB |
183 | get_random_bytes(rndvec, sizeof(rndvec)); |
184 | lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) + (rndvec[2] << 16); | |
f38ba179 UB |
185 | rc = smc_wr_alloc_link_mem(lnk); |
186 | if (rc) | |
187 | goto free_lgr; | |
bd4ad577 UB |
188 | rc = smc_ib_create_protection_domain(lnk); |
189 | if (rc) | |
190 | goto free_link_mem; | |
191 | rc = smc_ib_create_queue_pair(lnk); | |
192 | if (rc) | |
193 | goto dealloc_pd; | |
194 | rc = smc_wr_create_link(lnk); | |
195 | if (rc) | |
196 | goto destroy_qp; | |
9bf9abea UB |
197 | init_completion(&lnk->llc_confirm); |
198 | init_completion(&lnk->llc_confirm_resp); | |
0cfdd8f9 UB |
199 | |
200 | smc->conn.lgr = lgr; | |
201 | rwlock_init(&lgr->conns_lock); | |
202 | spin_lock_bh(&smc_lgr_list.lock); | |
203 | list_add(&lgr->list, &smc_lgr_list.list); | |
204 | spin_unlock_bh(&smc_lgr_list.lock); | |
f38ba179 UB |
205 | return 0; |
206 | ||
bd4ad577 UB |
207 | destroy_qp: |
208 | smc_ib_destroy_queue_pair(lnk); | |
209 | dealloc_pd: | |
210 | smc_ib_dealloc_protection_domain(lnk); | |
211 | free_link_mem: | |
212 | smc_wr_free_link_mem(lnk); | |
f38ba179 UB |
213 | free_lgr: |
214 | kfree(lgr); | |
0cfdd8f9 UB |
215 | out: |
216 | return rc; | |
217 | } | |
218 | ||
3e034725 | 219 | static void smc_buf_unuse(struct smc_connection *conn) |
cd6851f3 UB |
220 | { |
221 | if (conn->sndbuf_desc) { | |
222 | conn->sndbuf_desc->used = 0; | |
223 | conn->sndbuf_size = 0; | |
224 | } | |
cd6851f3 | 225 | if (conn->rmb_desc) { |
897e1c24 | 226 | conn->rmb_desc->reused = true; |
cd6851f3 UB |
227 | conn->rmb_desc->used = 0; |
228 | conn->rmbe_size = 0; | |
229 | } | |
230 | } | |
231 | ||
0cfdd8f9 UB |
232 | /* remove a finished connection from its link group */ |
233 | void smc_conn_free(struct smc_connection *conn) | |
234 | { | |
235 | struct smc_link_group *lgr = conn->lgr; | |
236 | ||
237 | if (!lgr) | |
238 | return; | |
5f08318f | 239 | smc_cdc_tx_dismiss_slots(conn); |
0cfdd8f9 | 240 | smc_lgr_unregister_conn(conn); |
3e034725 | 241 | smc_buf_unuse(conn); |
0cfdd8f9 UB |
242 | } |
243 | ||
244 | static void smc_link_clear(struct smc_link *lnk) | |
245 | { | |
246 | lnk->peer_qpn = 0; | |
bd4ad577 | 247 | smc_ib_modify_qp_reset(lnk); |
f38ba179 | 248 | smc_wr_free_link(lnk); |
bd4ad577 UB |
249 | smc_ib_destroy_queue_pair(lnk); |
250 | smc_ib_dealloc_protection_domain(lnk); | |
f38ba179 | 251 | smc_wr_free_link_mem(lnk); |
0cfdd8f9 UB |
252 | } |
253 | ||
3e034725 UB |
254 | static void smc_buf_free(struct smc_buf_desc *buf_desc, struct smc_link *lnk, |
255 | bool is_rmb) | |
cd6851f3 | 256 | { |
3e034725 UB |
257 | if (is_rmb) { |
258 | if (buf_desc->mr_rx[SMC_SINGLE_LINK]) | |
259 | smc_ib_put_memory_region( | |
260 | buf_desc->mr_rx[SMC_SINGLE_LINK]); | |
261 | smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, | |
262 | DMA_FROM_DEVICE); | |
263 | } else { | |
264 | smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc, | |
265 | DMA_TO_DEVICE); | |
cd6851f3 | 266 | } |
3e034725 UB |
267 | sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]); |
268 | if (buf_desc->cpu_addr) | |
269 | free_pages((unsigned long)buf_desc->cpu_addr, buf_desc->order); | |
270 | kfree(buf_desc); | |
cd6851f3 UB |
271 | } |
272 | ||
3e034725 | 273 | static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb) |
cd6851f3 | 274 | { |
bd4ad577 | 275 | struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; |
3e034725 UB |
276 | struct smc_buf_desc *buf_desc, *bf_desc; |
277 | struct list_head *buf_list; | |
cd6851f3 UB |
278 | int i; |
279 | ||
280 | for (i = 0; i < SMC_RMBE_SIZES; i++) { | |
3e034725 UB |
281 | if (is_rmb) |
282 | buf_list = &lgr->rmbs[i]; | |
283 | else | |
284 | buf_list = &lgr->sndbufs[i]; | |
285 | list_for_each_entry_safe(buf_desc, bf_desc, buf_list, | |
cd6851f3 | 286 | list) { |
3e034725 UB |
287 | list_del(&buf_desc->list); |
288 | smc_buf_free(buf_desc, lnk, is_rmb); | |
cd6851f3 UB |
289 | } |
290 | } | |
291 | } | |
292 | ||
3e034725 UB |
293 | static void smc_lgr_free_bufs(struct smc_link_group *lgr) |
294 | { | |
295 | /* free send buffers */ | |
296 | __smc_lgr_free_bufs(lgr, false); | |
297 | /* free rmbs */ | |
298 | __smc_lgr_free_bufs(lgr, true); | |
299 | } | |
300 | ||
0cfdd8f9 UB |
301 | /* remove a link group */ |
302 | void smc_lgr_free(struct smc_link_group *lgr) | |
303 | { | |
3e034725 | 304 | smc_lgr_free_bufs(lgr); |
0cfdd8f9 UB |
305 | smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]); |
306 | kfree(lgr); | |
307 | } | |
308 | ||
309 | /* terminate linkgroup abnormally */ | |
310 | void smc_lgr_terminate(struct smc_link_group *lgr) | |
311 | { | |
312 | struct smc_connection *conn; | |
b38d7324 | 313 | struct smc_sock *smc; |
0cfdd8f9 UB |
314 | struct rb_node *node; |
315 | ||
316 | spin_lock_bh(&smc_lgr_list.lock); | |
317 | if (list_empty(&lgr->list)) { | |
318 | /* termination already triggered */ | |
319 | spin_unlock_bh(&smc_lgr_list.lock); | |
320 | return; | |
321 | } | |
322 | /* do not use this link group for new connections */ | |
323 | list_del_init(&lgr->list); | |
324 | spin_unlock_bh(&smc_lgr_list.lock); | |
325 | ||
326 | write_lock_bh(&lgr->conns_lock); | |
327 | node = rb_first(&lgr->conns_all); | |
328 | while (node) { | |
329 | conn = rb_entry(node, struct smc_connection, alert_node); | |
b38d7324 UB |
330 | smc = container_of(conn, struct smc_sock, conn); |
331 | sock_hold(&smc->sk); | |
0cfdd8f9 | 332 | __smc_lgr_unregister_conn(conn); |
46c28dbd | 333 | schedule_work(&conn->close_work); |
b38d7324 | 334 | sock_put(&smc->sk); |
0cfdd8f9 UB |
335 | node = rb_first(&lgr->conns_all); |
336 | } | |
337 | write_unlock_bh(&lgr->conns_lock); | |
0cfdd8f9 UB |
338 | } |
339 | ||
340 | /* Determine vlan of internal TCP socket. | |
341 | * @vlan_id: address to store the determined vlan id into | |
342 | */ | |
343 | static int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id) | |
344 | { | |
345 | struct dst_entry *dst = sk_dst_get(clcsock->sk); | |
346 | int rc = 0; | |
347 | ||
348 | *vlan_id = 0; | |
349 | if (!dst) { | |
350 | rc = -ENOTCONN; | |
351 | goto out; | |
352 | } | |
353 | if (!dst->dev) { | |
354 | rc = -ENODEV; | |
355 | goto out_rel; | |
356 | } | |
357 | ||
358 | if (is_vlan_dev(dst->dev)) | |
359 | *vlan_id = vlan_dev_vlan_id(dst->dev); | |
360 | ||
361 | out_rel: | |
362 | dst_release(dst); | |
363 | out: | |
364 | return rc; | |
365 | } | |
366 | ||
367 | /* determine the link gid matching the vlan id of the link group */ | |
368 | static int smc_link_determine_gid(struct smc_link_group *lgr) | |
369 | { | |
370 | struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK]; | |
371 | struct ib_gid_attr gattr; | |
372 | union ib_gid gid; | |
373 | int i; | |
374 | ||
375 | if (!lgr->vlan_id) { | |
376 | lnk->gid = lnk->smcibdev->gid[lnk->ibport - 1]; | |
377 | return 0; | |
378 | } | |
379 | ||
380 | for (i = 0; i < lnk->smcibdev->pattr[lnk->ibport - 1].gid_tbl_len; | |
381 | i++) { | |
382 | if (ib_query_gid(lnk->smcibdev->ibdev, lnk->ibport, i, &gid, | |
383 | &gattr)) | |
384 | continue; | |
43e2ada3 UB |
385 | if (gattr.ndev) { |
386 | if (is_vlan_dev(gattr.ndev) && | |
387 | vlan_dev_vlan_id(gattr.ndev) == lgr->vlan_id) { | |
388 | lnk->gid = gid; | |
389 | dev_put(gattr.ndev); | |
390 | return 0; | |
391 | } | |
392 | dev_put(gattr.ndev); | |
0cfdd8f9 UB |
393 | } |
394 | } | |
395 | return -ENODEV; | |
396 | } | |
397 | ||
398 | /* create a new SMC connection (and a new link group if necessary) */ | |
399 | int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr, | |
400 | struct smc_ib_device *smcibdev, u8 ibport, | |
401 | struct smc_clc_msg_local *lcl, int srv_first_contact) | |
402 | { | |
403 | struct smc_connection *conn = &smc->conn; | |
404 | struct smc_link_group *lgr; | |
405 | unsigned short vlan_id; | |
406 | enum smc_lgr_role role; | |
407 | int local_contact = SMC_FIRST_CONTACT; | |
408 | int rc = 0; | |
409 | ||
410 | role = smc->listen_smc ? SMC_SERV : SMC_CLNT; | |
411 | rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id); | |
412 | if (rc) | |
413 | return rc; | |
414 | ||
415 | if ((role == SMC_CLNT) && srv_first_contact) | |
416 | /* create new link group as well */ | |
417 | goto create; | |
418 | ||
419 | /* determine if an existing link group can be reused */ | |
420 | spin_lock_bh(&smc_lgr_list.lock); | |
421 | list_for_each_entry(lgr, &smc_lgr_list.list, list) { | |
422 | write_lock_bh(&lgr->conns_lock); | |
423 | if (!memcmp(lgr->peer_systemid, lcl->id_for_peer, | |
424 | SMC_SYSTEMID_LEN) && | |
425 | !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid, | |
426 | SMC_GID_SIZE) && | |
427 | !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac, | |
428 | sizeof(lcl->mac)) && | |
429 | !lgr->sync_err && | |
430 | (lgr->role == role) && | |
cd6851f3 UB |
431 | (lgr->vlan_id == vlan_id) && |
432 | ((role == SMC_CLNT) || | |
433 | (lgr->conns_num < SMC_RMBS_PER_LGR_MAX))) { | |
0cfdd8f9 UB |
434 | /* link group found */ |
435 | local_contact = SMC_REUSE_CONTACT; | |
436 | conn->lgr = lgr; | |
437 | smc_lgr_register_conn(conn); /* add smc conn to lgr */ | |
438 | write_unlock_bh(&lgr->conns_lock); | |
439 | break; | |
440 | } | |
441 | write_unlock_bh(&lgr->conns_lock); | |
442 | } | |
443 | spin_unlock_bh(&smc_lgr_list.lock); | |
444 | ||
445 | if (role == SMC_CLNT && !srv_first_contact && | |
446 | (local_contact == SMC_FIRST_CONTACT)) { | |
447 | /* Server reuses a link group, but Client wants to start | |
448 | * a new one | |
449 | * send out_of_sync decline, reason synchr. error | |
450 | */ | |
451 | return -ENOLINK; | |
452 | } | |
453 | ||
454 | create: | |
455 | if (local_contact == SMC_FIRST_CONTACT) { | |
456 | rc = smc_lgr_create(smc, peer_in_addr, smcibdev, ibport, | |
457 | lcl->id_for_peer, vlan_id); | |
458 | if (rc) | |
459 | goto out; | |
460 | smc_lgr_register_conn(conn); /* add smc conn to lgr */ | |
461 | rc = smc_link_determine_gid(conn->lgr); | |
462 | } | |
5f08318f UB |
463 | conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE; |
464 | conn->local_tx_ctrl.len = sizeof(struct smc_cdc_msg); | |
465 | #ifndef KERNEL_HAS_ATOMIC64 | |
466 | spin_lock_init(&conn->acurs_lock); | |
467 | #endif | |
0cfdd8f9 UB |
468 | |
469 | out: | |
470 | return rc ? rc : local_contact; | |
471 | } | |
cd6851f3 | 472 | |
3e034725 UB |
473 | /* try to reuse a sndbuf or rmb description slot for a certain |
474 | * buffer size; if not available, return NULL | |
cd6851f3 UB |
475 | */ |
476 | static inline | |
3e034725 UB |
477 | struct smc_buf_desc *smc_buf_get_slot(struct smc_link_group *lgr, |
478 | int compressed_bufsize, | |
479 | rwlock_t *lock, | |
480 | struct list_head *buf_list) | |
cd6851f3 | 481 | { |
3e034725 | 482 | struct smc_buf_desc *buf_slot; |
cd6851f3 | 483 | |
3e034725 UB |
484 | read_lock_bh(lock); |
485 | list_for_each_entry(buf_slot, buf_list, list) { | |
486 | if (cmpxchg(&buf_slot->used, 0, 1) == 0) { | |
487 | read_unlock_bh(lock); | |
488 | return buf_slot; | |
cd6851f3 UB |
489 | } |
490 | } | |
3e034725 | 491 | read_unlock_bh(lock); |
cd6851f3 UB |
492 | return NULL; |
493 | } | |
494 | ||
952310cc UB |
495 | /* one of the conditions for announcing a receiver's current window size is |
496 | * that it "results in a minimum increase in the window size of 10% of the | |
497 | * receive buffer space" [RFC7609] | |
498 | */ | |
499 | static inline int smc_rmb_wnd_update_limit(int rmbe_size) | |
500 | { | |
501 | return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2); | |
502 | } | |
503 | ||
b33982c3 UB |
504 | static struct smc_buf_desc *smc_new_buf_create(struct smc_link_group *lgr, |
505 | bool is_rmb, int bufsize) | |
506 | { | |
507 | struct smc_buf_desc *buf_desc; | |
508 | struct smc_link *lnk; | |
509 | int rc; | |
510 | ||
511 | /* try to alloc a new buffer */ | |
512 | buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL); | |
513 | if (!buf_desc) | |
514 | return ERR_PTR(-ENOMEM); | |
515 | ||
516 | buf_desc->cpu_addr = | |
517 | (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | | |
518 | __GFP_NOMEMALLOC | | |
519 | __GFP_NORETRY | __GFP_ZERO, | |
520 | get_order(bufsize)); | |
521 | if (!buf_desc->cpu_addr) { | |
522 | kfree(buf_desc); | |
523 | return ERR_PTR(-EAGAIN); | |
524 | } | |
525 | buf_desc->order = get_order(bufsize); | |
526 | ||
527 | /* build the sg table from the pages */ | |
528 | lnk = &lgr->lnk[SMC_SINGLE_LINK]; | |
529 | rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1, | |
530 | GFP_KERNEL); | |
531 | if (rc) { | |
532 | smc_buf_free(buf_desc, lnk, is_rmb); | |
533 | return ERR_PTR(rc); | |
534 | } | |
535 | sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl, | |
536 | buf_desc->cpu_addr, bufsize); | |
537 | ||
538 | /* map sg table to DMA address */ | |
539 | rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc, | |
540 | is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
541 | /* SMC protocol depends on mapping to one DMA address only */ | |
542 | if (rc != 1) { | |
543 | smc_buf_free(buf_desc, lnk, is_rmb); | |
544 | return ERR_PTR(-EAGAIN); | |
545 | } | |
546 | ||
547 | /* create a new memory region for the RMB */ | |
548 | if (is_rmb) { | |
549 | rc = smc_ib_get_memory_region(lnk->roce_pd, | |
550 | IB_ACCESS_REMOTE_WRITE | | |
551 | IB_ACCESS_LOCAL_WRITE, | |
552 | buf_desc); | |
553 | if (rc) { | |
554 | smc_buf_free(buf_desc, lnk, is_rmb); | |
555 | return ERR_PTR(rc); | |
556 | } | |
557 | } | |
558 | ||
559 | return buf_desc; | |
560 | } | |
561 | ||
3e034725 | 562 | static int __smc_buf_create(struct smc_sock *smc, bool is_rmb) |
cd6851f3 UB |
563 | { |
564 | struct smc_connection *conn = &smc->conn; | |
565 | struct smc_link_group *lgr = conn->lgr; | |
68870370 | 566 | struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM); |
3e034725 | 567 | struct list_head *buf_list; |
c45abf31 | 568 | int bufsize, bufsize_short; |
3e034725 UB |
569 | int sk_buf_size; |
570 | rwlock_t *lock; | |
cd6851f3 | 571 | |
3e034725 UB |
572 | if (is_rmb) |
573 | /* use socket recv buffer size (w/o overhead) as start value */ | |
574 | sk_buf_size = smc->sk.sk_rcvbuf / 2; | |
575 | else | |
576 | /* use socket send buffer size (w/o overhead) as start value */ | |
577 | sk_buf_size = smc->sk.sk_sndbuf / 2; | |
578 | ||
4e1061f4 | 579 | for (bufsize_short = smc_compress_bufsize(sk_buf_size); |
c45abf31 | 580 | bufsize_short >= 0; bufsize_short--) { |
9d8fb617 | 581 | |
3e034725 UB |
582 | if (is_rmb) { |
583 | lock = &lgr->rmbs_lock; | |
584 | buf_list = &lgr->rmbs[bufsize_short]; | |
585 | } else { | |
586 | lock = &lgr->sndbufs_lock; | |
587 | buf_list = &lgr->sndbufs[bufsize_short]; | |
9d8fb617 | 588 | } |
c45abf31 | 589 | bufsize = smc_uncompress_bufsize(bufsize_short); |
a3fe3d01 UB |
590 | if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC) |
591 | continue; | |
592 | ||
3e034725 UB |
593 | /* check for reusable slot in the link group */ |
594 | buf_desc = smc_buf_get_slot(lgr, bufsize_short, lock, buf_list); | |
595 | if (buf_desc) { | |
596 | memset(buf_desc->cpu_addr, 0, bufsize); | |
cd6851f3 UB |
597 | break; /* found reusable slot */ |
598 | } | |
a3fe3d01 | 599 | |
b33982c3 UB |
600 | buf_desc = smc_new_buf_create(lgr, is_rmb, bufsize); |
601 | if (PTR_ERR(buf_desc) == -ENOMEM) | |
602 | break; | |
603 | if (IS_ERR(buf_desc)) | |
a3fe3d01 | 604 | continue; |
897e1c24 | 605 | |
3e034725 UB |
606 | buf_desc->used = 1; |
607 | write_lock_bh(lock); | |
608 | list_add(&buf_desc->list, buf_list); | |
609 | write_unlock_bh(lock); | |
610 | break; /* found */ | |
cd6851f3 | 611 | } |
3e034725 | 612 | |
b33982c3 | 613 | if (IS_ERR(buf_desc)) |
3e034725 UB |
614 | return -ENOMEM; |
615 | ||
616 | if (is_rmb) { | |
617 | conn->rmb_desc = buf_desc; | |
c45abf31 UB |
618 | conn->rmbe_size = bufsize; |
619 | conn->rmbe_size_short = bufsize_short; | |
620 | smc->sk.sk_rcvbuf = bufsize * 2; | |
5f08318f | 621 | atomic_set(&conn->bytes_to_rcv, 0); |
c45abf31 | 622 | conn->rmbe_update_limit = smc_rmb_wnd_update_limit(bufsize); |
cd6851f3 | 623 | } else { |
3e034725 UB |
624 | conn->sndbuf_desc = buf_desc; |
625 | conn->sndbuf_size = bufsize; | |
626 | smc->sk.sk_sndbuf = bufsize * 2; | |
627 | atomic_set(&conn->sndbuf_space, bufsize); | |
cd6851f3 | 628 | } |
3e034725 UB |
629 | return 0; |
630 | } | |
631 | ||
10428dd8 UB |
632 | void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn) |
633 | { | |
634 | struct smc_link_group *lgr = conn->lgr; | |
635 | ||
636 | smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, | |
637 | conn->sndbuf_desc, DMA_TO_DEVICE); | |
638 | } | |
639 | ||
640 | void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn) | |
641 | { | |
642 | struct smc_link_group *lgr = conn->lgr; | |
643 | ||
644 | smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, | |
645 | conn->sndbuf_desc, DMA_TO_DEVICE); | |
646 | } | |
647 | ||
648 | void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn) | |
649 | { | |
650 | struct smc_link_group *lgr = conn->lgr; | |
651 | ||
652 | smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev, | |
653 | conn->rmb_desc, DMA_FROM_DEVICE); | |
654 | } | |
655 | ||
656 | void smc_rmb_sync_sg_for_device(struct smc_connection *conn) | |
657 | { | |
658 | struct smc_link_group *lgr = conn->lgr; | |
659 | ||
660 | smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev, | |
661 | conn->rmb_desc, DMA_FROM_DEVICE); | |
662 | } | |
663 | ||
3e034725 UB |
664 | /* create the send and receive buffer for an SMC socket; |
665 | * receive buffers are called RMBs; | |
666 | * (even though the SMC protocol allows more than one RMB-element per RMB, | |
667 | * the Linux implementation uses just one RMB-element per RMB, i.e. uses an | |
668 | * extra RMB for every connection in a link group | |
669 | */ | |
670 | int smc_buf_create(struct smc_sock *smc) | |
671 | { | |
672 | int rc; | |
673 | ||
674 | /* create send buffer */ | |
675 | rc = __smc_buf_create(smc, false); | |
676 | if (rc) | |
677 | return rc; | |
678 | /* create rmb */ | |
679 | rc = __smc_buf_create(smc, true); | |
680 | if (rc) | |
681 | smc_buf_free(smc->conn.sndbuf_desc, | |
682 | &smc->conn.lgr->lnk[SMC_SINGLE_LINK], false); | |
683 | return rc; | |
cd6851f3 | 684 | } |
bd4ad577 UB |
685 | |
686 | static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr) | |
687 | { | |
688 | int i; | |
689 | ||
690 | for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) { | |
691 | if (!test_and_set_bit(i, lgr->rtokens_used_mask)) | |
692 | return i; | |
693 | } | |
694 | return -ENOSPC; | |
695 | } | |
696 | ||
697 | /* save rkey and dma_addr received from peer during clc handshake */ | |
698 | int smc_rmb_rtoken_handling(struct smc_connection *conn, | |
699 | struct smc_clc_msg_accept_confirm *clc) | |
700 | { | |
701 | u64 dma_addr = be64_to_cpu(clc->rmb_dma_addr); | |
702 | struct smc_link_group *lgr = conn->lgr; | |
703 | u32 rkey = ntohl(clc->rmb_rkey); | |
704 | int i; | |
705 | ||
706 | for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) { | |
707 | if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) && | |
263eec9b | 708 | (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) && |
bd4ad577 UB |
709 | test_bit(i, lgr->rtokens_used_mask)) { |
710 | conn->rtoken_idx = i; | |
711 | return 0; | |
712 | } | |
713 | } | |
714 | conn->rtoken_idx = smc_rmb_reserve_rtoken_idx(lgr); | |
715 | if (conn->rtoken_idx < 0) | |
716 | return conn->rtoken_idx; | |
717 | lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey = rkey; | |
718 | lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr = dma_addr; | |
719 | return 0; | |
720 | } |