1 // SPDX-License-Identifier: GPL-2.0
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 <linux/wait.h>
17 #include <linux/reboot.h>
18 #include <linux/mutex.h>
19 #include <net/tcp.h>
20 #include <net/sock.h>
21 #include <rdma/ib_verbs.h>
22 #include <rdma/ib_cache.h>
23
24 #include "smc.h"
25 #include "smc_clc.h"
26 #include "smc_core.h"
27 #include "smc_ib.h"
28 #include "smc_wr.h"
29 #include "smc_llc.h"
30 #include "smc_cdc.h"
31 #include "smc_close.h"
32 #include "smc_ism.h"
33
34 #define SMC_LGR_NUM_INCR 256
35 #define SMC_LGR_FREE_DELAY_SERV (600 * HZ)
36 #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
37
38 static struct smc_lgr_list smc_lgr_list = { /* established link groups */
39 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
40 .list = LIST_HEAD_INIT(smc_lgr_list.list),
41 .num = 0,
42 };
43
44 static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */
45 static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted);
46
47 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
48 struct smc_buf_desc *buf_desc);
49 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft);
50
51 static void smc_link_down_work(struct work_struct *work);
52
53 /* return head of link group list and its lock for a given link group */
smc_lgr_list_head(struct smc_link_group * lgr,spinlock_t ** lgr_lock)54 static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr,
55 spinlock_t **lgr_lock)
56 {
57 if (lgr->is_smcd) {
58 *lgr_lock = &lgr->smcd->lgr_lock;
59 return &lgr->smcd->lgr_list;
60 }
61
62 *lgr_lock = &smc_lgr_list.lock;
63 return &smc_lgr_list.list;
64 }
65
smc_lgr_schedule_free_work(struct smc_link_group * lgr)66 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
67 {
68 /* client link group creation always follows the server link group
69 * creation. For client use a somewhat higher removal delay time,
70 * otherwise there is a risk of out-of-sync link groups.
71 */
72 if (!lgr->freeing) {
73 mod_delayed_work(system_wq, &lgr->free_work,
74 (!lgr->is_smcd && lgr->role == SMC_CLNT) ?
75 SMC_LGR_FREE_DELAY_CLNT :
76 SMC_LGR_FREE_DELAY_SERV);
77 }
78 }
79
80 /* Register connection's alert token in our lookup structure.
81 * To use rbtrees we have to implement our own insert core.
82 * Requires @conns_lock
83 * @smc connection to register
84 * Returns 0 on success, != otherwise.
85 */
smc_lgr_add_alert_token(struct smc_connection * conn)86 static void smc_lgr_add_alert_token(struct smc_connection *conn)
87 {
88 struct rb_node **link, *parent = NULL;
89 u32 token = conn->alert_token_local;
90
91 link = &conn->lgr->conns_all.rb_node;
92 while (*link) {
93 struct smc_connection *cur = rb_entry(*link,
94 struct smc_connection, alert_node);
95
96 parent = *link;
97 if (cur->alert_token_local > token)
98 link = &parent->rb_left;
99 else
100 link = &parent->rb_right;
101 }
102 /* Put the new node there */
103 rb_link_node(&conn->alert_node, parent, link);
104 rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
105 }
106
107 /* assign an SMC-R link to the connection */
smcr_lgr_conn_assign_link(struct smc_connection * conn,bool first)108 static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
109 {
110 enum smc_link_state expected = first ? SMC_LNK_ACTIVATING :
111 SMC_LNK_ACTIVE;
112 int i, j;
113
114 /* do link balancing */
115 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
116 struct smc_link *lnk = &conn->lgr->lnk[i];
117
118 if (lnk->state != expected || lnk->link_is_asym)
119 continue;
120 if (conn->lgr->role == SMC_CLNT) {
121 conn->lnk = lnk; /* temporary, SMC server assigns link*/
122 break;
123 }
124 if (conn->lgr->conns_num % 2) {
125 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
126 struct smc_link *lnk2;
127
128 lnk2 = &conn->lgr->lnk[j];
129 if (lnk2->state == expected &&
130 !lnk2->link_is_asym) {
131 conn->lnk = lnk2;
132 break;
133 }
134 }
135 }
136 if (!conn->lnk)
137 conn->lnk = lnk;
138 break;
139 }
140 if (!conn->lnk)
141 return SMC_CLC_DECL_NOACTLINK;
142 return 0;
143 }
144
145 /* Register connection in link group by assigning an alert token
146 * registered in a search tree.
147 * Requires @conns_lock
148 * Note that '0' is a reserved value and not assigned.
149 */
smc_lgr_register_conn(struct smc_connection * conn,bool first)150 static int smc_lgr_register_conn(struct smc_connection *conn, bool first)
151 {
152 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
153 static atomic_t nexttoken = ATOMIC_INIT(0);
154 int rc;
155
156 if (!conn->lgr->is_smcd) {
157 rc = smcr_lgr_conn_assign_link(conn, first);
158 if (rc)
159 return rc;
160 }
161 /* find a new alert_token_local value not yet used by some connection
162 * in this link group
163 */
164 sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
165 while (!conn->alert_token_local) {
166 conn->alert_token_local = atomic_inc_return(&nexttoken);
167 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
168 conn->alert_token_local = 0;
169 }
170 smc_lgr_add_alert_token(conn);
171 conn->lgr->conns_num++;
172 return 0;
173 }
174
175 /* Unregister connection and reset the alert token of the given connection<
176 */
__smc_lgr_unregister_conn(struct smc_connection * conn)177 static void __smc_lgr_unregister_conn(struct smc_connection *conn)
178 {
179 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
180 struct smc_link_group *lgr = conn->lgr;
181
182 rb_erase(&conn->alert_node, &lgr->conns_all);
183 lgr->conns_num--;
184 conn->alert_token_local = 0;
185 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
186 }
187
188 /* Unregister connection from lgr
189 */
smc_lgr_unregister_conn(struct smc_connection * conn)190 static void smc_lgr_unregister_conn(struct smc_connection *conn)
191 {
192 struct smc_link_group *lgr = conn->lgr;
193
194 if (!lgr)
195 return;
196 write_lock_bh(&lgr->conns_lock);
197 if (conn->alert_token_local) {
198 __smc_lgr_unregister_conn(conn);
199 }
200 write_unlock_bh(&lgr->conns_lock);
201 conn->lgr = NULL;
202 }
203
smc_lgr_cleanup_early(struct smc_connection * conn)204 void smc_lgr_cleanup_early(struct smc_connection *conn)
205 {
206 struct smc_link_group *lgr = conn->lgr;
207 spinlock_t *lgr_lock;
208
209 if (!lgr)
210 return;
211
212 smc_conn_free(conn);
213 smc_lgr_list_head(lgr, &lgr_lock);
214 spin_lock_bh(lgr_lock);
215 /* do not use this link group for new connections */
216 if (!list_empty(&lgr->list))
217 list_del_init(&lgr->list);
218 spin_unlock_bh(lgr_lock);
219 __smc_lgr_terminate(lgr, true);
220 }
221
smcr_lgr_link_deactivate_all(struct smc_link_group * lgr)222 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)
223 {
224 int i;
225
226 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
227 struct smc_link *lnk = &lgr->lnk[i];
228
229 if (smc_link_sendable(lnk))
230 lnk->state = SMC_LNK_INACTIVE;
231 }
232 wake_up_all(&lgr->llc_msg_waiter);
233 wake_up_all(&lgr->llc_flow_waiter);
234 }
235
236 static void smc_lgr_free(struct smc_link_group *lgr);
237
smc_lgr_free_work(struct work_struct * work)238 static void smc_lgr_free_work(struct work_struct *work)
239 {
240 struct smc_link_group *lgr = container_of(to_delayed_work(work),
241 struct smc_link_group,
242 free_work);
243 spinlock_t *lgr_lock;
244 bool conns;
245
246 smc_lgr_list_head(lgr, &lgr_lock);
247 spin_lock_bh(lgr_lock);
248 if (lgr->freeing) {
249 spin_unlock_bh(lgr_lock);
250 return;
251 }
252 read_lock_bh(&lgr->conns_lock);
253 conns = RB_EMPTY_ROOT(&lgr->conns_all);
254 read_unlock_bh(&lgr->conns_lock);
255 if (!conns) { /* number of lgr connections is no longer zero */
256 spin_unlock_bh(lgr_lock);
257 return;
258 }
259 list_del_init(&lgr->list); /* remove from smc_lgr_list */
260 lgr->freeing = 1; /* this instance does the freeing, no new schedule */
261 spin_unlock_bh(lgr_lock);
262 cancel_delayed_work(&lgr->free_work);
263
264 if (!lgr->is_smcd && !lgr->terminating)
265 smc_llc_send_link_delete_all(lgr, true,
266 SMC_LLC_DEL_PROG_INIT_TERM);
267 if (lgr->is_smcd && !lgr->terminating)
268 smc_ism_signal_shutdown(lgr);
269 if (!lgr->is_smcd)
270 smcr_lgr_link_deactivate_all(lgr);
271 smc_lgr_free(lgr);
272 }
273
smc_lgr_terminate_work(struct work_struct * work)274 static void smc_lgr_terminate_work(struct work_struct *work)
275 {
276 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
277 terminate_work);
278
279 __smc_lgr_terminate(lgr, true);
280 }
281
282 /* return next unique link id for the lgr */
smcr_next_link_id(struct smc_link_group * lgr)283 static u8 smcr_next_link_id(struct smc_link_group *lgr)
284 {
285 u8 link_id;
286 int i;
287
288 while (1) {
289 again:
290 link_id = ++lgr->next_link_id;
291 if (!link_id) /* skip zero as link_id */
292 link_id = ++lgr->next_link_id;
293 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
294 if (smc_link_usable(&lgr->lnk[i]) &&
295 lgr->lnk[i].link_id == link_id)
296 goto again;
297 }
298 break;
299 }
300 return link_id;
301 }
302
smcr_link_init(struct smc_link_group * lgr,struct smc_link * lnk,u8 link_idx,struct smc_init_info * ini)303 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
304 u8 link_idx, struct smc_init_info *ini)
305 {
306 u8 rndvec[3];
307 int rc;
308
309 get_device(&ini->ib_dev->ibdev->dev);
310 atomic_inc(&ini->ib_dev->lnk_cnt);
311 lnk->link_id = smcr_next_link_id(lgr);
312 lnk->lgr = lgr;
313 lnk->link_idx = link_idx;
314 lnk->smcibdev = ini->ib_dev;
315 lnk->ibport = ini->ib_port;
316 lnk->path_mtu = ini->ib_dev->pattr[ini->ib_port - 1].active_mtu;
317 smc_llc_link_set_uid(lnk);
318 INIT_WORK(&lnk->link_down_wrk, smc_link_down_work);
319 if (!ini->ib_dev->initialized) {
320 rc = (int)smc_ib_setup_per_ibdev(ini->ib_dev);
321 if (rc)
322 goto out;
323 }
324 get_random_bytes(rndvec, sizeof(rndvec));
325 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
326 (rndvec[2] << 16);
327 rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
328 ini->vlan_id, lnk->gid, &lnk->sgid_index);
329 if (rc)
330 goto out;
331 rc = smc_llc_link_init(lnk);
332 if (rc)
333 goto out;
334 rc = smc_wr_alloc_link_mem(lnk);
335 if (rc)
336 goto clear_llc_lnk;
337 rc = smc_ib_create_protection_domain(lnk);
338 if (rc)
339 goto free_link_mem;
340 rc = smc_ib_create_queue_pair(lnk);
341 if (rc)
342 goto dealloc_pd;
343 rc = smc_wr_create_link(lnk);
344 if (rc)
345 goto destroy_qp;
346 lnk->state = SMC_LNK_ACTIVATING;
347 return 0;
348
349 destroy_qp:
350 smc_ib_destroy_queue_pair(lnk);
351 dealloc_pd:
352 smc_ib_dealloc_protection_domain(lnk);
353 free_link_mem:
354 smc_wr_free_link_mem(lnk);
355 clear_llc_lnk:
356 smc_llc_link_clear(lnk, false);
357 out:
358 put_device(&ini->ib_dev->ibdev->dev);
359 memset(lnk, 0, sizeof(struct smc_link));
360 lnk->state = SMC_LNK_UNUSED;
361 if (!atomic_dec_return(&ini->ib_dev->lnk_cnt))
362 wake_up(&ini->ib_dev->lnks_deleted);
363 return rc;
364 }
365
366 /* create a new SMC link group */
smc_lgr_create(struct smc_sock * smc,struct smc_init_info * ini)367 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
368 {
369 struct smc_link_group *lgr;
370 struct list_head *lgr_list;
371 struct smc_link *lnk;
372 spinlock_t *lgr_lock;
373 u8 link_idx;
374 int rc = 0;
375 int i;
376
377 if (ini->is_smcd && ini->vlan_id) {
378 if (smc_ism_get_vlan(ini->ism_dev[ini->ism_selected],
379 ini->vlan_id)) {
380 rc = SMC_CLC_DECL_ISMVLANERR;
381 goto out;
382 }
383 }
384
385 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
386 if (!lgr) {
387 rc = SMC_CLC_DECL_MEM;
388 goto ism_put_vlan;
389 }
390 lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0,
391 SMC_LGR_ID_SIZE, &lgr->id);
392 if (!lgr->tx_wq) {
393 rc = -ENOMEM;
394 goto free_lgr;
395 }
396 lgr->is_smcd = ini->is_smcd;
397 lgr->sync_err = 0;
398 lgr->terminating = 0;
399 lgr->freeing = 0;
400 lgr->vlan_id = ini->vlan_id;
401 mutex_init(&lgr->sndbufs_lock);
402 mutex_init(&lgr->rmbs_lock);
403 rwlock_init(&lgr->conns_lock);
404 for (i = 0; i < SMC_RMBE_SIZES; i++) {
405 INIT_LIST_HEAD(&lgr->sndbufs[i]);
406 INIT_LIST_HEAD(&lgr->rmbs[i]);
407 }
408 lgr->next_link_id = 0;
409 smc_lgr_list.num += SMC_LGR_NUM_INCR;
410 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
411 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
412 INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
413 lgr->conns_all = RB_ROOT;
414 if (ini->is_smcd) {
415 /* SMC-D specific settings */
416 get_device(&ini->ism_dev[ini->ism_selected]->dev);
417 lgr->peer_gid = ini->ism_peer_gid[ini->ism_selected];
418 lgr->smcd = ini->ism_dev[ini->ism_selected];
419 lgr_list = &ini->ism_dev[ini->ism_selected]->lgr_list;
420 lgr_lock = &lgr->smcd->lgr_lock;
421 lgr->smc_version = ini->smcd_version;
422 lgr->peer_shutdown = 0;
423 atomic_inc(&ini->ism_dev[ini->ism_selected]->lgr_cnt);
424 } else {
425 /* SMC-R specific settings */
426 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
427 memcpy(lgr->peer_systemid, ini->ib_lcl->id_for_peer,
428 SMC_SYSTEMID_LEN);
429 memcpy(lgr->pnet_id, ini->ib_dev->pnetid[ini->ib_port - 1],
430 SMC_MAX_PNETID_LEN);
431 smc_llc_lgr_init(lgr, smc);
432
433 link_idx = SMC_SINGLE_LINK;
434 lnk = &lgr->lnk[link_idx];
435 rc = smcr_link_init(lgr, lnk, link_idx, ini);
436 if (rc)
437 goto free_wq;
438 lgr_list = &smc_lgr_list.list;
439 lgr_lock = &smc_lgr_list.lock;
440 atomic_inc(&lgr_cnt);
441 }
442 smc->conn.lgr = lgr;
443 spin_lock_bh(lgr_lock);
444 list_add_tail(&lgr->list, lgr_list);
445 spin_unlock_bh(lgr_lock);
446 return 0;
447
448 free_wq:
449 destroy_workqueue(lgr->tx_wq);
450 free_lgr:
451 kfree(lgr);
452 ism_put_vlan:
453 if (ini->is_smcd && ini->vlan_id)
454 smc_ism_put_vlan(ini->ism_dev[ini->ism_selected], ini->vlan_id);
455 out:
456 if (rc < 0) {
457 if (rc == -ENOMEM)
458 rc = SMC_CLC_DECL_MEM;
459 else
460 rc = SMC_CLC_DECL_INTERR;
461 }
462 return rc;
463 }
464
smc_write_space(struct smc_connection * conn)465 static int smc_write_space(struct smc_connection *conn)
466 {
467 int buffer_len = conn->peer_rmbe_size;
468 union smc_host_cursor prod;
469 union smc_host_cursor cons;
470 int space;
471
472 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
473 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
474 /* determine rx_buf space */
475 space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
476 return space;
477 }
478
smc_switch_cursor(struct smc_sock * smc,struct smc_cdc_tx_pend * pend,struct smc_wr_buf * wr_buf)479 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
480 struct smc_wr_buf *wr_buf)
481 {
482 struct smc_connection *conn = &smc->conn;
483 union smc_host_cursor cons, fin;
484 int rc = 0;
485 int diff;
486
487 smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
488 smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
489 /* set prod cursor to old state, enforce tx_rdma_writes() */
490 smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
491 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
492
493 if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
494 /* cons cursor advanced more than fin, and prod was set
495 * fin above, so now prod is smaller than cons. Fix that.
496 */
497 diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
498 smc_curs_add(conn->sndbuf_desc->len,
499 &conn->tx_curs_sent, diff);
500 smc_curs_add(conn->sndbuf_desc->len,
501 &conn->tx_curs_fin, diff);
502
503 smp_mb__before_atomic();
504 atomic_add(diff, &conn->sndbuf_space);
505 smp_mb__after_atomic();
506
507 smc_curs_add(conn->peer_rmbe_size,
508 &conn->local_tx_ctrl.prod, diff);
509 smc_curs_add(conn->peer_rmbe_size,
510 &conn->local_tx_ctrl_fin, diff);
511 }
512 /* recalculate, value is used by tx_rdma_writes() */
513 atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
514
515 if (smc->sk.sk_state != SMC_INIT &&
516 smc->sk.sk_state != SMC_CLOSED) {
517 rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf);
518 if (!rc) {
519 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0);
520 smc->sk.sk_data_ready(&smc->sk);
521 }
522 } else {
523 smc_wr_tx_put_slot(conn->lnk,
524 (struct smc_wr_tx_pend_priv *)pend);
525 }
526 return rc;
527 }
528
smc_switch_conns(struct smc_link_group * lgr,struct smc_link * from_lnk,bool is_dev_err)529 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
530 struct smc_link *from_lnk, bool is_dev_err)
531 {
532 struct smc_link *to_lnk = NULL;
533 struct smc_cdc_tx_pend *pend;
534 struct smc_connection *conn;
535 struct smc_wr_buf *wr_buf;
536 struct smc_sock *smc;
537 struct rb_node *node;
538 int i, rc = 0;
539
540 /* link is inactive, wake up tx waiters */
541 smc_wr_wakeup_tx_wait(from_lnk);
542
543 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
544 if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx)
545 continue;
546 if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev &&
547 from_lnk->ibport == lgr->lnk[i].ibport) {
548 continue;
549 }
550 to_lnk = &lgr->lnk[i];
551 break;
552 }
553 if (!to_lnk || !smc_wr_tx_link_hold(to_lnk)) {
554 smc_lgr_terminate_sched(lgr);
555 return NULL;
556 }
557 again:
558 read_lock_bh(&lgr->conns_lock);
559 for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
560 conn = rb_entry(node, struct smc_connection, alert_node);
561 if (conn->lnk != from_lnk)
562 continue;
563 smc = container_of(conn, struct smc_sock, conn);
564 /* conn->lnk not yet set in SMC_INIT state */
565 if (smc->sk.sk_state == SMC_INIT)
566 continue;
567 if (smc->sk.sk_state == SMC_CLOSED ||
568 smc->sk.sk_state == SMC_PEERCLOSEWAIT1 ||
569 smc->sk.sk_state == SMC_PEERCLOSEWAIT2 ||
570 smc->sk.sk_state == SMC_APPFINCLOSEWAIT ||
571 smc->sk.sk_state == SMC_APPCLOSEWAIT1 ||
572 smc->sk.sk_state == SMC_APPCLOSEWAIT2 ||
573 smc->sk.sk_state == SMC_PEERFINCLOSEWAIT ||
574 smc->sk.sk_state == SMC_PEERABORTWAIT ||
575 smc->sk.sk_state == SMC_PROCESSABORT) {
576 spin_lock_bh(&conn->send_lock);
577 conn->lnk = to_lnk;
578 spin_unlock_bh(&conn->send_lock);
579 continue;
580 }
581 sock_hold(&smc->sk);
582 read_unlock_bh(&lgr->conns_lock);
583 /* pre-fetch buffer outside of send_lock, might sleep */
584 rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend);
585 if (rc)
586 goto err_out;
587 /* avoid race with smcr_tx_sndbuf_nonempty() */
588 spin_lock_bh(&conn->send_lock);
589 conn->lnk = to_lnk;
590 rc = smc_switch_cursor(smc, pend, wr_buf);
591 spin_unlock_bh(&conn->send_lock);
592 sock_put(&smc->sk);
593 if (rc)
594 goto err_out;
595 goto again;
596 }
597 read_unlock_bh(&lgr->conns_lock);
598 smc_wr_tx_link_put(to_lnk);
599 return to_lnk;
600
601 err_out:
602 smcr_link_down_cond_sched(to_lnk);
603 smc_wr_tx_link_put(to_lnk);
604 return NULL;
605 }
606
smcr_buf_unuse(struct smc_buf_desc * rmb_desc,struct smc_link_group * lgr)607 static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc,
608 struct smc_link_group *lgr)
609 {
610 int rc;
611
612 if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) {
613 /* unregister rmb with peer */
614 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
615 if (!rc) {
616 /* protect against smc_llc_cli_rkey_exchange() */
617 mutex_lock(&lgr->llc_conf_mutex);
618 smc_llc_do_delete_rkey(lgr, rmb_desc);
619 rmb_desc->is_conf_rkey = false;
620 mutex_unlock(&lgr->llc_conf_mutex);
621 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
622 }
623 }
624
625 if (rmb_desc->is_reg_err) {
626 /* buf registration failed, reuse not possible */
627 mutex_lock(&lgr->rmbs_lock);
628 list_del(&rmb_desc->list);
629 mutex_unlock(&lgr->rmbs_lock);
630
631 smc_buf_free(lgr, true, rmb_desc);
632 } else {
633 rmb_desc->used = 0;
634 }
635 }
636
smc_buf_unuse(struct smc_connection * conn,struct smc_link_group * lgr)637 static void smc_buf_unuse(struct smc_connection *conn,
638 struct smc_link_group *lgr)
639 {
640 if (conn->sndbuf_desc)
641 conn->sndbuf_desc->used = 0;
642 if (conn->rmb_desc && lgr->is_smcd)
643 conn->rmb_desc->used = 0;
644 else if (conn->rmb_desc)
645 smcr_buf_unuse(conn->rmb_desc, lgr);
646 }
647
648 /* remove a finished connection from its link group */
smc_conn_free(struct smc_connection * conn)649 void smc_conn_free(struct smc_connection *conn)
650 {
651 struct smc_link_group *lgr = conn->lgr;
652
653 if (!lgr)
654 return;
655 if (lgr->is_smcd) {
656 if (!list_empty(&lgr->list))
657 smc_ism_unset_conn(conn);
658 tasklet_kill(&conn->rx_tsklet);
659 } else {
660 smc_cdc_wait_pend_tx_wr(conn);
661 if (current_work() != &conn->abort_work)
662 cancel_work_sync(&conn->abort_work);
663 }
664 if (!list_empty(&lgr->list)) {
665 smc_lgr_unregister_conn(conn);
666 smc_buf_unuse(conn, lgr); /* allow buffer reuse */
667 }
668
669 if (!lgr->conns_num)
670 smc_lgr_schedule_free_work(lgr);
671 }
672
673 /* unregister a link from a buf_desc */
smcr_buf_unmap_link(struct smc_buf_desc * buf_desc,bool is_rmb,struct smc_link * lnk)674 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb,
675 struct smc_link *lnk)
676 {
677 if (is_rmb)
678 buf_desc->is_reg_mr[lnk->link_idx] = false;
679 if (!buf_desc->is_map_ib[lnk->link_idx])
680 return;
681 if (is_rmb) {
682 if (buf_desc->mr_rx[lnk->link_idx]) {
683 smc_ib_put_memory_region(
684 buf_desc->mr_rx[lnk->link_idx]);
685 buf_desc->mr_rx[lnk->link_idx] = NULL;
686 }
687 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
688 } else {
689 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
690 }
691 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
692 buf_desc->is_map_ib[lnk->link_idx] = false;
693 }
694
695 /* unmap all buffers of lgr for a deleted link */
smcr_buf_unmap_lgr(struct smc_link * lnk)696 static void smcr_buf_unmap_lgr(struct smc_link *lnk)
697 {
698 struct smc_link_group *lgr = lnk->lgr;
699 struct smc_buf_desc *buf_desc, *bf;
700 int i;
701
702 for (i = 0; i < SMC_RMBE_SIZES; i++) {
703 mutex_lock(&lgr->rmbs_lock);
704 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list)
705 smcr_buf_unmap_link(buf_desc, true, lnk);
706 mutex_unlock(&lgr->rmbs_lock);
707 mutex_lock(&lgr->sndbufs_lock);
708 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i],
709 list)
710 smcr_buf_unmap_link(buf_desc, false, lnk);
711 mutex_unlock(&lgr->sndbufs_lock);
712 }
713 }
714
smcr_rtoken_clear_link(struct smc_link * lnk)715 static void smcr_rtoken_clear_link(struct smc_link *lnk)
716 {
717 struct smc_link_group *lgr = lnk->lgr;
718 int i;
719
720 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
721 lgr->rtokens[i][lnk->link_idx].rkey = 0;
722 lgr->rtokens[i][lnk->link_idx].dma_addr = 0;
723 }
724 }
725
726 /* must be called under lgr->llc_conf_mutex lock */
smcr_link_clear(struct smc_link * lnk,bool log)727 void smcr_link_clear(struct smc_link *lnk, bool log)
728 {
729 struct smc_ib_device *smcibdev;
730
731 if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
732 return;
733 lnk->peer_qpn = 0;
734 smc_llc_link_clear(lnk, log);
735 smcr_buf_unmap_lgr(lnk);
736 smcr_rtoken_clear_link(lnk);
737 smc_ib_modify_qp_error(lnk);
738 smc_wr_free_link(lnk);
739 smc_ib_destroy_queue_pair(lnk);
740 smc_ib_dealloc_protection_domain(lnk);
741 smc_wr_free_link_mem(lnk);
742 put_device(&lnk->smcibdev->ibdev->dev);
743 smcibdev = lnk->smcibdev;
744 memset(lnk, 0, sizeof(struct smc_link));
745 lnk->state = SMC_LNK_UNUSED;
746 if (!atomic_dec_return(&smcibdev->lnk_cnt))
747 wake_up(&smcibdev->lnks_deleted);
748 }
749
smcr_buf_free(struct smc_link_group * lgr,bool is_rmb,struct smc_buf_desc * buf_desc)750 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
751 struct smc_buf_desc *buf_desc)
752 {
753 int i;
754
755 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
756 smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]);
757
758 if (buf_desc->pages)
759 __free_pages(buf_desc->pages, buf_desc->order);
760 kfree(buf_desc);
761 }
762
smcd_buf_free(struct smc_link_group * lgr,bool is_dmb,struct smc_buf_desc * buf_desc)763 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
764 struct smc_buf_desc *buf_desc)
765 {
766 if (is_dmb) {
767 /* restore original buf len */
768 buf_desc->len += sizeof(struct smcd_cdc_msg);
769 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
770 } else {
771 kfree(buf_desc->cpu_addr);
772 }
773 kfree(buf_desc);
774 }
775
smc_buf_free(struct smc_link_group * lgr,bool is_rmb,struct smc_buf_desc * buf_desc)776 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
777 struct smc_buf_desc *buf_desc)
778 {
779 if (lgr->is_smcd)
780 smcd_buf_free(lgr, is_rmb, buf_desc);
781 else
782 smcr_buf_free(lgr, is_rmb, buf_desc);
783 }
784
__smc_lgr_free_bufs(struct smc_link_group * lgr,bool is_rmb)785 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
786 {
787 struct smc_buf_desc *buf_desc, *bf_desc;
788 struct list_head *buf_list;
789 int i;
790
791 for (i = 0; i < SMC_RMBE_SIZES; i++) {
792 if (is_rmb)
793 buf_list = &lgr->rmbs[i];
794 else
795 buf_list = &lgr->sndbufs[i];
796 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
797 list) {
798 list_del(&buf_desc->list);
799 smc_buf_free(lgr, is_rmb, buf_desc);
800 }
801 }
802 }
803
smc_lgr_free_bufs(struct smc_link_group * lgr)804 static void smc_lgr_free_bufs(struct smc_link_group *lgr)
805 {
806 /* free send buffers */
807 __smc_lgr_free_bufs(lgr, false);
808 /* free rmbs */
809 __smc_lgr_free_bufs(lgr, true);
810 }
811
812 /* remove a link group */
smc_lgr_free(struct smc_link_group * lgr)813 static void smc_lgr_free(struct smc_link_group *lgr)
814 {
815 int i;
816
817 if (!lgr->is_smcd) {
818 mutex_lock(&lgr->llc_conf_mutex);
819 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
820 if (lgr->lnk[i].state != SMC_LNK_UNUSED)
821 smcr_link_clear(&lgr->lnk[i], false);
822 }
823 mutex_unlock(&lgr->llc_conf_mutex);
824 smc_llc_lgr_clear(lgr);
825 }
826
827 smc_lgr_free_bufs(lgr);
828 destroy_workqueue(lgr->tx_wq);
829 if (lgr->is_smcd) {
830 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
831 put_device(&lgr->smcd->dev);
832 if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
833 wake_up(&lgr->smcd->lgrs_deleted);
834 } else {
835 if (!atomic_dec_return(&lgr_cnt))
836 wake_up(&lgrs_deleted);
837 }
838 kfree(lgr);
839 }
840
smcd_unregister_all_dmbs(struct smc_link_group * lgr)841 static void smcd_unregister_all_dmbs(struct smc_link_group *lgr)
842 {
843 int i;
844
845 for (i = 0; i < SMC_RMBE_SIZES; i++) {
846 struct smc_buf_desc *buf_desc;
847
848 list_for_each_entry(buf_desc, &lgr->rmbs[i], list) {
849 buf_desc->len += sizeof(struct smcd_cdc_msg);
850 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
851 }
852 }
853 }
854
smc_sk_wake_ups(struct smc_sock * smc)855 static void smc_sk_wake_ups(struct smc_sock *smc)
856 {
857 smc->sk.sk_write_space(&smc->sk);
858 smc->sk.sk_data_ready(&smc->sk);
859 smc->sk.sk_state_change(&smc->sk);
860 }
861
862 /* kill a connection */
smc_conn_kill(struct smc_connection * conn,bool soft)863 static void smc_conn_kill(struct smc_connection *conn, bool soft)
864 {
865 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
866
867 if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
868 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
869 else
870 smc_close_abort(conn);
871 conn->killed = 1;
872 smc->sk.sk_err = ECONNABORTED;
873 smc_sk_wake_ups(smc);
874 if (conn->lgr->is_smcd) {
875 smc_ism_unset_conn(conn);
876 if (soft)
877 tasklet_kill(&conn->rx_tsklet);
878 else
879 tasklet_unlock_wait(&conn->rx_tsklet);
880 } else {
881 smc_cdc_wait_pend_tx_wr(conn);
882 }
883 smc_lgr_unregister_conn(conn);
884 smc_close_active_abort(smc);
885 }
886
smc_lgr_cleanup(struct smc_link_group * lgr)887 static void smc_lgr_cleanup(struct smc_link_group *lgr)
888 {
889 if (lgr->is_smcd) {
890 smc_ism_signal_shutdown(lgr);
891 smcd_unregister_all_dmbs(lgr);
892 } else {
893 u32 rsn = lgr->llc_termination_rsn;
894
895 if (!rsn)
896 rsn = SMC_LLC_DEL_PROG_INIT_TERM;
897 smc_llc_send_link_delete_all(lgr, false, rsn);
898 smcr_lgr_link_deactivate_all(lgr);
899 }
900 }
901
902 /* terminate link group
903 * @soft: true if link group shutdown can take its time
904 * false if immediate link group shutdown is required
905 */
__smc_lgr_terminate(struct smc_link_group * lgr,bool soft)906 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
907 {
908 struct smc_connection *conn;
909 struct smc_sock *smc;
910 struct rb_node *node;
911
912 if (lgr->terminating)
913 return; /* lgr already terminating */
914 /* cancel free_work sync, will terminate when lgr->freeing is set */
915 cancel_delayed_work_sync(&lgr->free_work);
916 lgr->terminating = 1;
917
918 /* kill remaining link group connections */
919 read_lock_bh(&lgr->conns_lock);
920 node = rb_first(&lgr->conns_all);
921 while (node) {
922 read_unlock_bh(&lgr->conns_lock);
923 conn = rb_entry(node, struct smc_connection, alert_node);
924 smc = container_of(conn, struct smc_sock, conn);
925 sock_hold(&smc->sk); /* sock_put below */
926 lock_sock(&smc->sk);
927 smc_conn_kill(conn, soft);
928 release_sock(&smc->sk);
929 sock_put(&smc->sk); /* sock_hold above */
930 read_lock_bh(&lgr->conns_lock);
931 node = rb_first(&lgr->conns_all);
932 }
933 read_unlock_bh(&lgr->conns_lock);
934 smc_lgr_cleanup(lgr);
935 smc_lgr_free(lgr);
936 }
937
938 /* unlink link group and schedule termination */
smc_lgr_terminate_sched(struct smc_link_group * lgr)939 void smc_lgr_terminate_sched(struct smc_link_group *lgr)
940 {
941 spinlock_t *lgr_lock;
942
943 smc_lgr_list_head(lgr, &lgr_lock);
944 spin_lock_bh(lgr_lock);
945 if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
946 spin_unlock_bh(lgr_lock);
947 return; /* lgr already terminating */
948 }
949 list_del_init(&lgr->list);
950 lgr->freeing = 1;
951 spin_unlock_bh(lgr_lock);
952 schedule_work(&lgr->terminate_work);
953 }
954
955 /* Called when peer lgr shutdown (regularly or abnormally) is received */
smc_smcd_terminate(struct smcd_dev * dev,u64 peer_gid,unsigned short vlan)956 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
957 {
958 struct smc_link_group *lgr, *l;
959 LIST_HEAD(lgr_free_list);
960
961 /* run common cleanup function and build free list */
962 spin_lock_bh(&dev->lgr_lock);
963 list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
964 if ((!peer_gid || lgr->peer_gid == peer_gid) &&
965 (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
966 if (peer_gid) /* peer triggered termination */
967 lgr->peer_shutdown = 1;
968 list_move(&lgr->list, &lgr_free_list);
969 lgr->freeing = 1;
970 }
971 }
972 spin_unlock_bh(&dev->lgr_lock);
973
974 /* cancel the regular free workers and actually free lgrs */
975 list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
976 list_del_init(&lgr->list);
977 schedule_work(&lgr->terminate_work);
978 }
979 }
980
981 /* Called when an SMCD device is removed or the smc module is unloaded */
smc_smcd_terminate_all(struct smcd_dev * smcd)982 void smc_smcd_terminate_all(struct smcd_dev *smcd)
983 {
984 struct smc_link_group *lgr, *lg;
985 LIST_HEAD(lgr_free_list);
986
987 spin_lock_bh(&smcd->lgr_lock);
988 list_splice_init(&smcd->lgr_list, &lgr_free_list);
989 list_for_each_entry(lgr, &lgr_free_list, list)
990 lgr->freeing = 1;
991 spin_unlock_bh(&smcd->lgr_lock);
992
993 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
994 list_del_init(&lgr->list);
995 __smc_lgr_terminate(lgr, false);
996 }
997
998 if (atomic_read(&smcd->lgr_cnt))
999 wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
1000 }
1001
1002 /* Called when an SMCR device is removed or the smc module is unloaded.
1003 * If smcibdev is given, all SMCR link groups using this device are terminated.
1004 * If smcibdev is NULL, all SMCR link groups are terminated.
1005 */
smc_smcr_terminate_all(struct smc_ib_device * smcibdev)1006 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1007 {
1008 struct smc_link_group *lgr, *lg;
1009 LIST_HEAD(lgr_free_list);
1010 int i;
1011
1012 spin_lock_bh(&smc_lgr_list.lock);
1013 if (!smcibdev) {
1014 list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1015 list_for_each_entry(lgr, &lgr_free_list, list)
1016 lgr->freeing = 1;
1017 } else {
1018 list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1019 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1020 if (lgr->lnk[i].smcibdev == smcibdev)
1021 smcr_link_down_cond_sched(&lgr->lnk[i]);
1022 }
1023 }
1024 }
1025 spin_unlock_bh(&smc_lgr_list.lock);
1026
1027 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1028 list_del_init(&lgr->list);
1029 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1030 __smc_lgr_terminate(lgr, false);
1031 }
1032
1033 if (smcibdev) {
1034 if (atomic_read(&smcibdev->lnk_cnt))
1035 wait_event(smcibdev->lnks_deleted,
1036 !atomic_read(&smcibdev->lnk_cnt));
1037 } else {
1038 if (atomic_read(&lgr_cnt))
1039 wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1040 }
1041 }
1042
1043 /* set new lgr type and clear all asymmetric link tagging */
smcr_lgr_set_type(struct smc_link_group * lgr,enum smc_lgr_type new_type)1044 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1045 {
1046 char *lgr_type = "";
1047 int i;
1048
1049 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1050 if (smc_link_usable(&lgr->lnk[i]))
1051 lgr->lnk[i].link_is_asym = false;
1052 if (lgr->type == new_type)
1053 return;
1054 lgr->type = new_type;
1055
1056 switch (lgr->type) {
1057 case SMC_LGR_NONE:
1058 lgr_type = "NONE";
1059 break;
1060 case SMC_LGR_SINGLE:
1061 lgr_type = "SINGLE";
1062 break;
1063 case SMC_LGR_SYMMETRIC:
1064 lgr_type = "SYMMETRIC";
1065 break;
1066 case SMC_LGR_ASYMMETRIC_PEER:
1067 lgr_type = "ASYMMETRIC_PEER";
1068 break;
1069 case SMC_LGR_ASYMMETRIC_LOCAL:
1070 lgr_type = "ASYMMETRIC_LOCAL";
1071 break;
1072 }
1073 pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: "
1074 "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1075 lgr_type, lgr->pnet_id);
1076 }
1077
1078 /* set new lgr type and tag a link as asymmetric */
smcr_lgr_set_type_asym(struct smc_link_group * lgr,enum smc_lgr_type new_type,int asym_lnk_idx)1079 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1080 enum smc_lgr_type new_type, int asym_lnk_idx)
1081 {
1082 smcr_lgr_set_type(lgr, new_type);
1083 lgr->lnk[asym_lnk_idx].link_is_asym = true;
1084 }
1085
1086 /* abort connection, abort_work scheduled from tasklet context */
smc_conn_abort_work(struct work_struct * work)1087 static void smc_conn_abort_work(struct work_struct *work)
1088 {
1089 struct smc_connection *conn = container_of(work,
1090 struct smc_connection,
1091 abort_work);
1092 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1093
1094 lock_sock(&smc->sk);
1095 smc_conn_kill(conn, true);
1096 release_sock(&smc->sk);
1097 sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1098 }
1099
smcr_port_add(struct smc_ib_device * smcibdev,u8 ibport)1100 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1101 {
1102 struct smc_link_group *lgr, *n;
1103
1104 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1105 struct smc_link *link;
1106
1107 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1108 SMC_MAX_PNETID_LEN) ||
1109 lgr->type == SMC_LGR_SYMMETRIC ||
1110 lgr->type == SMC_LGR_ASYMMETRIC_PEER)
1111 continue;
1112
1113 /* trigger local add link processing */
1114 link = smc_llc_usable_link(lgr);
1115 if (link)
1116 smc_llc_add_link_local(link);
1117 }
1118 }
1119
1120 /* link is down - switch connections to alternate link,
1121 * must be called under lgr->llc_conf_mutex lock
1122 */
smcr_link_down(struct smc_link * lnk)1123 static void smcr_link_down(struct smc_link *lnk)
1124 {
1125 struct smc_link_group *lgr = lnk->lgr;
1126 struct smc_link *to_lnk;
1127 int del_link_id;
1128
1129 if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1130 return;
1131
1132 to_lnk = smc_switch_conns(lgr, lnk, true);
1133 if (!to_lnk) { /* no backup link available */
1134 smcr_link_clear(lnk, true);
1135 return;
1136 }
1137 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1138 del_link_id = lnk->link_id;
1139
1140 if (lgr->role == SMC_SERV) {
1141 /* trigger local delete link processing */
1142 smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1143 } else {
1144 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1145 /* another llc task is ongoing */
1146 mutex_unlock(&lgr->llc_conf_mutex);
1147 wait_event_timeout(lgr->llc_flow_waiter,
1148 (list_empty(&lgr->list) ||
1149 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1150 SMC_LLC_WAIT_TIME);
1151 mutex_lock(&lgr->llc_conf_mutex);
1152 }
1153 if (!list_empty(&lgr->list)) {
1154 smc_llc_send_delete_link(to_lnk, del_link_id,
1155 SMC_LLC_REQ, true,
1156 SMC_LLC_DEL_LOST_PATH);
1157 smcr_link_clear(lnk, true);
1158 }
1159 wake_up(&lgr->llc_flow_waiter); /* wake up next waiter */
1160 }
1161 }
1162
1163 /* must be called under lgr->llc_conf_mutex lock */
smcr_link_down_cond(struct smc_link * lnk)1164 void smcr_link_down_cond(struct smc_link *lnk)
1165 {
1166 if (smc_link_downing(&lnk->state))
1167 smcr_link_down(lnk);
1168 }
1169
1170 /* will get the lgr->llc_conf_mutex lock */
smcr_link_down_cond_sched(struct smc_link * lnk)1171 void smcr_link_down_cond_sched(struct smc_link *lnk)
1172 {
1173 if (smc_link_downing(&lnk->state))
1174 schedule_work(&lnk->link_down_wrk);
1175 }
1176
smcr_port_err(struct smc_ib_device * smcibdev,u8 ibport)1177 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1178 {
1179 struct smc_link_group *lgr, *n;
1180 int i;
1181
1182 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1183 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1184 SMC_MAX_PNETID_LEN))
1185 continue; /* lgr is not affected */
1186 if (list_empty(&lgr->list))
1187 continue;
1188 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1189 struct smc_link *lnk = &lgr->lnk[i];
1190
1191 if (smc_link_usable(lnk) &&
1192 lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1193 smcr_link_down_cond_sched(lnk);
1194 }
1195 }
1196 }
1197
smc_link_down_work(struct work_struct * work)1198 static void smc_link_down_work(struct work_struct *work)
1199 {
1200 struct smc_link *link = container_of(work, struct smc_link,
1201 link_down_wrk);
1202 struct smc_link_group *lgr = link->lgr;
1203
1204 if (list_empty(&lgr->list))
1205 return;
1206 wake_up_all(&lgr->llc_msg_waiter);
1207 mutex_lock(&lgr->llc_conf_mutex);
1208 smcr_link_down(link);
1209 mutex_unlock(&lgr->llc_conf_mutex);
1210 }
1211
smc_vlan_by_tcpsk_walk(struct net_device * lower_dev,struct netdev_nested_priv * priv)1212 static int smc_vlan_by_tcpsk_walk(struct net_device *lower_dev,
1213 struct netdev_nested_priv *priv)
1214 {
1215 unsigned short *vlan_id = (unsigned short *)priv->data;
1216
1217 if (is_vlan_dev(lower_dev)) {
1218 *vlan_id = vlan_dev_vlan_id(lower_dev);
1219 return 1;
1220 }
1221
1222 return 0;
1223 }
1224
1225 /* Determine vlan of internal TCP socket. */
smc_vlan_by_tcpsk(struct socket * clcsock,struct smc_init_info * ini)1226 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1227 {
1228 struct dst_entry *dst = sk_dst_get(clcsock->sk);
1229 struct netdev_nested_priv priv;
1230 struct net_device *ndev;
1231 int rc = 0;
1232
1233 ini->vlan_id = 0;
1234 if (!dst) {
1235 rc = -ENOTCONN;
1236 goto out;
1237 }
1238 if (!dst->dev) {
1239 rc = -ENODEV;
1240 goto out_rel;
1241 }
1242
1243 ndev = dst->dev;
1244 if (is_vlan_dev(ndev)) {
1245 ini->vlan_id = vlan_dev_vlan_id(ndev);
1246 goto out_rel;
1247 }
1248
1249 priv.data = (void *)&ini->vlan_id;
1250 rtnl_lock();
1251 netdev_walk_all_lower_dev(ndev, smc_vlan_by_tcpsk_walk, &priv);
1252 rtnl_unlock();
1253
1254 out_rel:
1255 dst_release(dst);
1256 out:
1257 return rc;
1258 }
1259
smcr_lgr_match(struct smc_link_group * lgr,struct smc_clc_msg_local * lcl,enum smc_lgr_role role,u32 clcqpn)1260 static bool smcr_lgr_match(struct smc_link_group *lgr,
1261 struct smc_clc_msg_local *lcl,
1262 enum smc_lgr_role role, u32 clcqpn)
1263 {
1264 int i;
1265
1266 if (memcmp(lgr->peer_systemid, lcl->id_for_peer, SMC_SYSTEMID_LEN) ||
1267 lgr->role != role)
1268 return false;
1269
1270 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1271 if (!smc_link_active(&lgr->lnk[i]))
1272 continue;
1273 if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
1274 !memcmp(lgr->lnk[i].peer_gid, &lcl->gid, SMC_GID_SIZE) &&
1275 !memcmp(lgr->lnk[i].peer_mac, lcl->mac, sizeof(lcl->mac)))
1276 return true;
1277 }
1278 return false;
1279 }
1280
smcd_lgr_match(struct smc_link_group * lgr,struct smcd_dev * smcismdev,u64 peer_gid)1281 static bool smcd_lgr_match(struct smc_link_group *lgr,
1282 struct smcd_dev *smcismdev, u64 peer_gid)
1283 {
1284 return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1285 }
1286
1287 /* create a new SMC connection (and a new link group if necessary) */
smc_conn_create(struct smc_sock * smc,struct smc_init_info * ini)1288 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1289 {
1290 struct smc_connection *conn = &smc->conn;
1291 struct list_head *lgr_list;
1292 struct smc_link_group *lgr;
1293 enum smc_lgr_role role;
1294 spinlock_t *lgr_lock;
1295 int rc = 0;
1296
1297 lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
1298 &smc_lgr_list.list;
1299 lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
1300 &smc_lgr_list.lock;
1301 ini->first_contact_local = 1;
1302 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1303 if (role == SMC_CLNT && ini->first_contact_peer)
1304 /* create new link group as well */
1305 goto create;
1306
1307 /* determine if an existing link group can be reused */
1308 spin_lock_bh(lgr_lock);
1309 list_for_each_entry(lgr, lgr_list, list) {
1310 write_lock_bh(&lgr->conns_lock);
1311 if ((ini->is_smcd ?
1312 smcd_lgr_match(lgr, ini->ism_dev[ini->ism_selected],
1313 ini->ism_peer_gid[ini->ism_selected]) :
1314 smcr_lgr_match(lgr, ini->ib_lcl, role, ini->ib_clcqpn)) &&
1315 !lgr->sync_err &&
1316 (ini->smcd_version == SMC_V2 ||
1317 lgr->vlan_id == ini->vlan_id) &&
1318 (role == SMC_CLNT || ini->is_smcd ||
1319 lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
1320 /* link group found */
1321 ini->first_contact_local = 0;
1322 conn->lgr = lgr;
1323 rc = smc_lgr_register_conn(conn, false);
1324 write_unlock_bh(&lgr->conns_lock);
1325 if (!rc && delayed_work_pending(&lgr->free_work))
1326 cancel_delayed_work(&lgr->free_work);
1327 break;
1328 }
1329 write_unlock_bh(&lgr->conns_lock);
1330 }
1331 spin_unlock_bh(lgr_lock);
1332 if (rc)
1333 return rc;
1334
1335 if (role == SMC_CLNT && !ini->first_contact_peer &&
1336 ini->first_contact_local) {
1337 /* Server reuses a link group, but Client wants to start
1338 * a new one
1339 * send out_of_sync decline, reason synchr. error
1340 */
1341 return SMC_CLC_DECL_SYNCERR;
1342 }
1343
1344 create:
1345 if (ini->first_contact_local) {
1346 rc = smc_lgr_create(smc, ini);
1347 if (rc)
1348 goto out;
1349 lgr = conn->lgr;
1350 write_lock_bh(&lgr->conns_lock);
1351 rc = smc_lgr_register_conn(conn, true);
1352 write_unlock_bh(&lgr->conns_lock);
1353 if (rc)
1354 goto out;
1355 }
1356 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1357 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1358 conn->urg_state = SMC_URG_READ;
1359 init_waitqueue_head(&conn->cdc_pend_tx_wq);
1360 INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1361 if (ini->is_smcd) {
1362 conn->rx_off = sizeof(struct smcd_cdc_msg);
1363 smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1364 } else {
1365 conn->rx_off = 0;
1366 }
1367 #ifndef KERNEL_HAS_ATOMIC64
1368 spin_lock_init(&conn->acurs_lock);
1369 #endif
1370
1371 out:
1372 return rc;
1373 }
1374
1375 /* convert the RMB size into the compressed notation - minimum 16K.
1376 * In contrast to plain ilog2, this rounds towards the next power of 2,
1377 * so the socket application gets at least its desired sndbuf / rcvbuf size.
1378 */
smc_compress_bufsize(int size)1379 static u8 smc_compress_bufsize(int size)
1380 {
1381 u8 compressed;
1382
1383 if (size <= SMC_BUF_MIN_SIZE)
1384 return 0;
1385
1386 size = (size - 1) >> 14;
1387 compressed = ilog2(size) + 1;
1388 if (compressed >= SMC_RMBE_SIZES)
1389 compressed = SMC_RMBE_SIZES - 1;
1390 return compressed;
1391 }
1392
1393 /* convert the RMB size from compressed notation into integer */
smc_uncompress_bufsize(u8 compressed)1394 int smc_uncompress_bufsize(u8 compressed)
1395 {
1396 u32 size;
1397
1398 size = 0x00000001 << (((int)compressed) + 14);
1399 return (int)size;
1400 }
1401
1402 /* try to reuse a sndbuf or rmb description slot for a certain
1403 * buffer size; if not available, return NULL
1404 */
smc_buf_get_slot(int compressed_bufsize,struct mutex * lock,struct list_head * buf_list)1405 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
1406 struct mutex *lock,
1407 struct list_head *buf_list)
1408 {
1409 struct smc_buf_desc *buf_slot;
1410
1411 mutex_lock(lock);
1412 list_for_each_entry(buf_slot, buf_list, list) {
1413 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
1414 mutex_unlock(lock);
1415 return buf_slot;
1416 }
1417 }
1418 mutex_unlock(lock);
1419 return NULL;
1420 }
1421
1422 /* one of the conditions for announcing a receiver's current window size is
1423 * that it "results in a minimum increase in the window size of 10% of the
1424 * receive buffer space" [RFC7609]
1425 */
smc_rmb_wnd_update_limit(int rmbe_size)1426 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
1427 {
1428 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
1429 }
1430
1431 /* map an rmb buf to a link */
smcr_buf_map_link(struct smc_buf_desc * buf_desc,bool is_rmb,struct smc_link * lnk)1432 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1433 struct smc_link *lnk)
1434 {
1435 int rc;
1436
1437 if (buf_desc->is_map_ib[lnk->link_idx])
1438 return 0;
1439
1440 rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
1441 if (rc)
1442 return rc;
1443 sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
1444 buf_desc->cpu_addr, buf_desc->len);
1445
1446 /* map sg table to DMA address */
1447 rc = smc_ib_buf_map_sg(lnk, buf_desc,
1448 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1449 /* SMC protocol depends on mapping to one DMA address only */
1450 if (rc != 1) {
1451 rc = -EAGAIN;
1452 goto free_table;
1453 }
1454
1455 /* create a new memory region for the RMB */
1456 if (is_rmb) {
1457 rc = smc_ib_get_memory_region(lnk->roce_pd,
1458 IB_ACCESS_REMOTE_WRITE |
1459 IB_ACCESS_LOCAL_WRITE,
1460 buf_desc, lnk->link_idx);
1461 if (rc)
1462 goto buf_unmap;
1463 smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
1464 }
1465 buf_desc->is_map_ib[lnk->link_idx] = true;
1466 return 0;
1467
1468 buf_unmap:
1469 smc_ib_buf_unmap_sg(lnk, buf_desc,
1470 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1471 free_table:
1472 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1473 return rc;
1474 }
1475
1476 /* register a new rmb on IB device,
1477 * must be called under lgr->llc_conf_mutex lock
1478 */
smcr_link_reg_rmb(struct smc_link * link,struct smc_buf_desc * rmb_desc)1479 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
1480 {
1481 if (list_empty(&link->lgr->list))
1482 return -ENOLINK;
1483 if (!rmb_desc->is_reg_mr[link->link_idx]) {
1484 /* register memory region for new rmb */
1485 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
1486 rmb_desc->is_reg_err = true;
1487 return -EFAULT;
1488 }
1489 rmb_desc->is_reg_mr[link->link_idx] = true;
1490 }
1491 return 0;
1492 }
1493
_smcr_buf_map_lgr(struct smc_link * lnk,struct mutex * lock,struct list_head * lst,bool is_rmb)1494 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock,
1495 struct list_head *lst, bool is_rmb)
1496 {
1497 struct smc_buf_desc *buf_desc, *bf;
1498 int rc = 0;
1499
1500 mutex_lock(lock);
1501 list_for_each_entry_safe(buf_desc, bf, lst, list) {
1502 if (!buf_desc->used)
1503 continue;
1504 rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
1505 if (rc)
1506 goto out;
1507 }
1508 out:
1509 mutex_unlock(lock);
1510 return rc;
1511 }
1512
1513 /* map all used buffers of lgr for a new link */
smcr_buf_map_lgr(struct smc_link * lnk)1514 int smcr_buf_map_lgr(struct smc_link *lnk)
1515 {
1516 struct smc_link_group *lgr = lnk->lgr;
1517 int i, rc = 0;
1518
1519 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1520 rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
1521 &lgr->rmbs[i], true);
1522 if (rc)
1523 return rc;
1524 rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
1525 &lgr->sndbufs[i], false);
1526 if (rc)
1527 return rc;
1528 }
1529 return 0;
1530 }
1531
1532 /* register all used buffers of lgr for a new link,
1533 * must be called under lgr->llc_conf_mutex lock
1534 */
smcr_buf_reg_lgr(struct smc_link * lnk)1535 int smcr_buf_reg_lgr(struct smc_link *lnk)
1536 {
1537 struct smc_link_group *lgr = lnk->lgr;
1538 struct smc_buf_desc *buf_desc, *bf;
1539 int i, rc = 0;
1540
1541 mutex_lock(&lgr->rmbs_lock);
1542 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1543 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
1544 if (!buf_desc->used)
1545 continue;
1546 rc = smcr_link_reg_rmb(lnk, buf_desc);
1547 if (rc)
1548 goto out;
1549 }
1550 }
1551 out:
1552 mutex_unlock(&lgr->rmbs_lock);
1553 return rc;
1554 }
1555
smcr_new_buf_create(struct smc_link_group * lgr,bool is_rmb,int bufsize)1556 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
1557 bool is_rmb, int bufsize)
1558 {
1559 struct smc_buf_desc *buf_desc;
1560
1561 /* try to alloc a new buffer */
1562 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1563 if (!buf_desc)
1564 return ERR_PTR(-ENOMEM);
1565
1566 buf_desc->order = get_order(bufsize);
1567 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
1568 __GFP_NOMEMALLOC | __GFP_COMP |
1569 __GFP_NORETRY | __GFP_ZERO,
1570 buf_desc->order);
1571 if (!buf_desc->pages) {
1572 kfree(buf_desc);
1573 return ERR_PTR(-EAGAIN);
1574 }
1575 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
1576 buf_desc->len = bufsize;
1577 return buf_desc;
1578 }
1579
1580 /* map buf_desc on all usable links,
1581 * unused buffers stay mapped as long as the link is up
1582 */
smcr_buf_map_usable_links(struct smc_link_group * lgr,struct smc_buf_desc * buf_desc,bool is_rmb)1583 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
1584 struct smc_buf_desc *buf_desc, bool is_rmb)
1585 {
1586 int i, rc = 0;
1587
1588 /* protect against parallel link reconfiguration */
1589 mutex_lock(&lgr->llc_conf_mutex);
1590 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1591 struct smc_link *lnk = &lgr->lnk[i];
1592
1593 if (!smc_link_usable(lnk))
1594 continue;
1595 if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
1596 rc = -ENOMEM;
1597 goto out;
1598 }
1599 }
1600 out:
1601 mutex_unlock(&lgr->llc_conf_mutex);
1602 return rc;
1603 }
1604
1605 #define SMCD_DMBE_SIZES 6 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1606
smcd_new_buf_create(struct smc_link_group * lgr,bool is_dmb,int bufsize)1607 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
1608 bool is_dmb, int bufsize)
1609 {
1610 struct smc_buf_desc *buf_desc;
1611 int rc;
1612
1613 if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES)
1614 return ERR_PTR(-EAGAIN);
1615
1616 /* try to alloc a new DMB */
1617 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1618 if (!buf_desc)
1619 return ERR_PTR(-ENOMEM);
1620 if (is_dmb) {
1621 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
1622 if (rc) {
1623 kfree(buf_desc);
1624 if (rc == -ENOMEM)
1625 return ERR_PTR(-EAGAIN);
1626 if (rc == -ENOSPC)
1627 return ERR_PTR(-ENOSPC);
1628 return ERR_PTR(-EIO);
1629 }
1630 buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
1631 /* CDC header stored in buf. So, pretend it was smaller */
1632 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
1633 } else {
1634 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
1635 __GFP_NOWARN | __GFP_NORETRY |
1636 __GFP_NOMEMALLOC);
1637 if (!buf_desc->cpu_addr) {
1638 kfree(buf_desc);
1639 return ERR_PTR(-EAGAIN);
1640 }
1641 buf_desc->len = bufsize;
1642 }
1643 return buf_desc;
1644 }
1645
__smc_buf_create(struct smc_sock * smc,bool is_smcd,bool is_rmb)1646 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
1647 {
1648 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
1649 struct smc_connection *conn = &smc->conn;
1650 struct smc_link_group *lgr = conn->lgr;
1651 struct list_head *buf_list;
1652 int bufsize, bufsize_short;
1653 struct mutex *lock; /* lock buffer list */
1654 int sk_buf_size;
1655
1656 if (is_rmb)
1657 /* use socket recv buffer size (w/o overhead) as start value */
1658 sk_buf_size = smc->sk.sk_rcvbuf / 2;
1659 else
1660 /* use socket send buffer size (w/o overhead) as start value */
1661 sk_buf_size = smc->sk.sk_sndbuf / 2;
1662
1663 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
1664 bufsize_short >= 0; bufsize_short--) {
1665
1666 if (is_rmb) {
1667 lock = &lgr->rmbs_lock;
1668 buf_list = &lgr->rmbs[bufsize_short];
1669 } else {
1670 lock = &lgr->sndbufs_lock;
1671 buf_list = &lgr->sndbufs[bufsize_short];
1672 }
1673 bufsize = smc_uncompress_bufsize(bufsize_short);
1674 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
1675 continue;
1676
1677 /* check for reusable slot in the link group */
1678 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
1679 if (buf_desc) {
1680 memset(buf_desc->cpu_addr, 0, bufsize);
1681 break; /* found reusable slot */
1682 }
1683
1684 if (is_smcd)
1685 buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
1686 else
1687 buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
1688
1689 if (PTR_ERR(buf_desc) == -ENOMEM)
1690 break;
1691 if (IS_ERR(buf_desc))
1692 continue;
1693
1694 buf_desc->used = 1;
1695 mutex_lock(lock);
1696 list_add(&buf_desc->list, buf_list);
1697 mutex_unlock(lock);
1698 break; /* found */
1699 }
1700
1701 if (IS_ERR(buf_desc))
1702 return PTR_ERR(buf_desc);
1703
1704 if (!is_smcd) {
1705 if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
1706 smcr_buf_unuse(buf_desc, lgr);
1707 return -ENOMEM;
1708 }
1709 }
1710
1711 if (is_rmb) {
1712 conn->rmb_desc = buf_desc;
1713 conn->rmbe_size_short = bufsize_short;
1714 smc->sk.sk_rcvbuf = bufsize * 2;
1715 atomic_set(&conn->bytes_to_rcv, 0);
1716 conn->rmbe_update_limit =
1717 smc_rmb_wnd_update_limit(buf_desc->len);
1718 if (is_smcd)
1719 smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
1720 } else {
1721 conn->sndbuf_desc = buf_desc;
1722 smc->sk.sk_sndbuf = bufsize * 2;
1723 atomic_set(&conn->sndbuf_space, bufsize);
1724 }
1725 return 0;
1726 }
1727
smc_sndbuf_sync_sg_for_cpu(struct smc_connection * conn)1728 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
1729 {
1730 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1731 return;
1732 smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1733 }
1734
smc_sndbuf_sync_sg_for_device(struct smc_connection * conn)1735 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
1736 {
1737 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1738 return;
1739 smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1740 }
1741
smc_rmb_sync_sg_for_cpu(struct smc_connection * conn)1742 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
1743 {
1744 int i;
1745
1746 if (!conn->lgr || conn->lgr->is_smcd)
1747 return;
1748 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1749 if (!smc_link_active(&conn->lgr->lnk[i]))
1750 continue;
1751 smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
1752 DMA_FROM_DEVICE);
1753 }
1754 }
1755
smc_rmb_sync_sg_for_device(struct smc_connection * conn)1756 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
1757 {
1758 int i;
1759
1760 if (!conn->lgr || conn->lgr->is_smcd)
1761 return;
1762 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1763 if (!smc_link_active(&conn->lgr->lnk[i]))
1764 continue;
1765 smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
1766 DMA_FROM_DEVICE);
1767 }
1768 }
1769
1770 /* create the send and receive buffer for an SMC socket;
1771 * receive buffers are called RMBs;
1772 * (even though the SMC protocol allows more than one RMB-element per RMB,
1773 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
1774 * extra RMB for every connection in a link group
1775 */
smc_buf_create(struct smc_sock * smc,bool is_smcd)1776 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
1777 {
1778 int rc;
1779
1780 /* create send buffer */
1781 rc = __smc_buf_create(smc, is_smcd, false);
1782 if (rc)
1783 return rc;
1784 /* create rmb */
1785 rc = __smc_buf_create(smc, is_smcd, true);
1786 if (rc) {
1787 mutex_lock(&smc->conn.lgr->sndbufs_lock);
1788 list_del(&smc->conn.sndbuf_desc->list);
1789 mutex_unlock(&smc->conn.lgr->sndbufs_lock);
1790 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
1791 smc->conn.sndbuf_desc = NULL;
1792 }
1793 return rc;
1794 }
1795
smc_rmb_reserve_rtoken_idx(struct smc_link_group * lgr)1796 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
1797 {
1798 int i;
1799
1800 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
1801 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
1802 return i;
1803 }
1804 return -ENOSPC;
1805 }
1806
smc_rtoken_find_by_link(struct smc_link_group * lgr,int lnk_idx,u32 rkey)1807 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
1808 u32 rkey)
1809 {
1810 int i;
1811
1812 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1813 if (test_bit(i, lgr->rtokens_used_mask) &&
1814 lgr->rtokens[i][lnk_idx].rkey == rkey)
1815 return i;
1816 }
1817 return -ENOENT;
1818 }
1819
1820 /* set rtoken for a new link to an existing rmb */
smc_rtoken_set(struct smc_link_group * lgr,int link_idx,int link_idx_new,__be32 nw_rkey_known,__be64 nw_vaddr,__be32 nw_rkey)1821 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
1822 __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
1823 {
1824 int rtok_idx;
1825
1826 rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
1827 if (rtok_idx == -ENOENT)
1828 return;
1829 lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
1830 lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
1831 }
1832
1833 /* set rtoken for a new link whose link_id is given */
smc_rtoken_set2(struct smc_link_group * lgr,int rtok_idx,int link_id,__be64 nw_vaddr,__be32 nw_rkey)1834 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
1835 __be64 nw_vaddr, __be32 nw_rkey)
1836 {
1837 u64 dma_addr = be64_to_cpu(nw_vaddr);
1838 u32 rkey = ntohl(nw_rkey);
1839 bool found = false;
1840 int link_idx;
1841
1842 for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
1843 if (lgr->lnk[link_idx].link_id == link_id) {
1844 found = true;
1845 break;
1846 }
1847 }
1848 if (!found)
1849 return;
1850 lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
1851 lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
1852 }
1853
1854 /* add a new rtoken from peer */
smc_rtoken_add(struct smc_link * lnk,__be64 nw_vaddr,__be32 nw_rkey)1855 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
1856 {
1857 struct smc_link_group *lgr = smc_get_lgr(lnk);
1858 u64 dma_addr = be64_to_cpu(nw_vaddr);
1859 u32 rkey = ntohl(nw_rkey);
1860 int i;
1861
1862 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1863 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1864 lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
1865 test_bit(i, lgr->rtokens_used_mask)) {
1866 /* already in list */
1867 return i;
1868 }
1869 }
1870 i = smc_rmb_reserve_rtoken_idx(lgr);
1871 if (i < 0)
1872 return i;
1873 lgr->rtokens[i][lnk->link_idx].rkey = rkey;
1874 lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
1875 return i;
1876 }
1877
1878 /* delete an rtoken from all links */
smc_rtoken_delete(struct smc_link * lnk,__be32 nw_rkey)1879 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
1880 {
1881 struct smc_link_group *lgr = smc_get_lgr(lnk);
1882 u32 rkey = ntohl(nw_rkey);
1883 int i, j;
1884
1885 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1886 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1887 test_bit(i, lgr->rtokens_used_mask)) {
1888 for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
1889 lgr->rtokens[i][j].rkey = 0;
1890 lgr->rtokens[i][j].dma_addr = 0;
1891 }
1892 clear_bit(i, lgr->rtokens_used_mask);
1893 return 0;
1894 }
1895 }
1896 return -ENOENT;
1897 }
1898
1899 /* save rkey and dma_addr received from peer during clc handshake */
smc_rmb_rtoken_handling(struct smc_connection * conn,struct smc_link * lnk,struct smc_clc_msg_accept_confirm * clc)1900 int smc_rmb_rtoken_handling(struct smc_connection *conn,
1901 struct smc_link *lnk,
1902 struct smc_clc_msg_accept_confirm *clc)
1903 {
1904 conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
1905 clc->r0.rmb_rkey);
1906 if (conn->rtoken_idx < 0)
1907 return conn->rtoken_idx;
1908 return 0;
1909 }
1910
smc_core_going_away(void)1911 static void smc_core_going_away(void)
1912 {
1913 struct smc_ib_device *smcibdev;
1914 struct smcd_dev *smcd;
1915
1916 mutex_lock(&smc_ib_devices.mutex);
1917 list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
1918 int i;
1919
1920 for (i = 0; i < SMC_MAX_PORTS; i++)
1921 set_bit(i, smcibdev->ports_going_away);
1922 }
1923 mutex_unlock(&smc_ib_devices.mutex);
1924
1925 mutex_lock(&smcd_dev_list.mutex);
1926 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1927 smcd->going_away = 1;
1928 }
1929 mutex_unlock(&smcd_dev_list.mutex);
1930 }
1931
1932 /* Clean up all SMC link groups */
smc_lgrs_shutdown(void)1933 static void smc_lgrs_shutdown(void)
1934 {
1935 struct smcd_dev *smcd;
1936
1937 smc_core_going_away();
1938
1939 smc_smcr_terminate_all(NULL);
1940
1941 mutex_lock(&smcd_dev_list.mutex);
1942 list_for_each_entry(smcd, &smcd_dev_list.list, list)
1943 smc_smcd_terminate_all(smcd);
1944 mutex_unlock(&smcd_dev_list.mutex);
1945 }
1946
smc_core_reboot_event(struct notifier_block * this,unsigned long event,void * ptr)1947 static int smc_core_reboot_event(struct notifier_block *this,
1948 unsigned long event, void *ptr)
1949 {
1950 smc_lgrs_shutdown();
1951 smc_ib_unregister_client();
1952 return 0;
1953 }
1954
1955 static struct notifier_block smc_reboot_notifier = {
1956 .notifier_call = smc_core_reboot_event,
1957 };
1958
smc_core_init(void)1959 int __init smc_core_init(void)
1960 {
1961 return register_reboot_notifier(&smc_reboot_notifier);
1962 }
1963
1964 /* Called (from smc_exit) when module is removed */
smc_core_exit(void)1965 void smc_core_exit(void)
1966 {
1967 unregister_reboot_notifier(&smc_reboot_notifier);
1968 smc_lgrs_shutdown();
1969 }
1970