1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Basic Transport Functions exploiting Infiniband API
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
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>
21 #include <rdma/ib_verbs.h>
22 #include <rdma/ib_cache.h>
31 #include "smc_close.h"
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)
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),
44 static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */
45 static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted);
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);
51 static void smc_link_down_work(struct work_struct *work);
53 /* return head of link group list and its lock for a given link group */
54 static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr,
55 spinlock_t **lgr_lock)
58 *lgr_lock = &lgr->smcd->lgr_lock;
59 return &lgr->smcd->lgr_list;
62 *lgr_lock = &smc_lgr_list.lock;
63 return &smc_lgr_list.list;
66 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
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.
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);
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.
86 static void smc_lgr_add_alert_token(struct smc_connection *conn)
88 struct rb_node **link, *parent = NULL;
89 u32 token = conn->alert_token_local;
91 link = &conn->lgr->conns_all.rb_node;
93 struct smc_connection *cur = rb_entry(*link,
94 struct smc_connection, alert_node);
97 if (cur->alert_token_local > token)
98 link = &parent->rb_left;
100 link = &parent->rb_right;
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);
107 /* assign an SMC-R link to the connection */
108 static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
110 enum smc_link_state expected = first ? SMC_LNK_ACTIVATING :
114 /* do link balancing */
115 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
116 struct smc_link *lnk = &conn->lgr->lnk[i];
118 if (lnk->state != expected || lnk->link_is_asym)
120 if (conn->lgr->role == SMC_CLNT) {
121 conn->lnk = lnk; /* temporary, SMC server assigns link*/
124 if (conn->lgr->conns_num % 2) {
125 for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
126 struct smc_link *lnk2;
128 lnk2 = &conn->lgr->lnk[j];
129 if (lnk2->state == expected &&
130 !lnk2->link_is_asym) {
141 return SMC_CLC_DECL_NOACTLINK;
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.
150 static int smc_lgr_register_conn(struct smc_connection *conn, bool first)
152 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
153 static atomic_t nexttoken = ATOMIC_INIT(0);
156 if (!conn->lgr->is_smcd) {
157 rc = smcr_lgr_conn_assign_link(conn, first);
161 /* find a new alert_token_local value not yet used by some connection
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;
170 smc_lgr_add_alert_token(conn);
171 conn->lgr->conns_num++;
175 /* Unregister connection and reset the alert token of the given connection<
177 static void __smc_lgr_unregister_conn(struct smc_connection *conn)
179 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
180 struct smc_link_group *lgr = conn->lgr;
182 rb_erase(&conn->alert_node, &lgr->conns_all);
184 conn->alert_token_local = 0;
185 sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
188 /* Unregister connection from lgr
190 static void smc_lgr_unregister_conn(struct smc_connection *conn)
192 struct smc_link_group *lgr = conn->lgr;
196 write_lock_bh(&lgr->conns_lock);
197 if (conn->alert_token_local) {
198 __smc_lgr_unregister_conn(conn);
200 write_unlock_bh(&lgr->conns_lock);
204 void smc_lgr_cleanup_early(struct smc_connection *conn)
206 struct smc_link_group *lgr = conn->lgr;
207 struct list_head *lgr_list;
208 spinlock_t *lgr_lock;
214 lgr_list = smc_lgr_list_head(lgr, &lgr_lock);
215 spin_lock_bh(lgr_lock);
216 /* do not use this link group for new connections */
217 if (!list_empty(lgr_list))
218 list_del_init(lgr_list);
219 spin_unlock_bh(lgr_lock);
220 __smc_lgr_terminate(lgr, true);
223 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)
227 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
228 struct smc_link *lnk = &lgr->lnk[i];
230 if (smc_link_usable(lnk))
231 lnk->state = SMC_LNK_INACTIVE;
233 wake_up_all(&lgr->llc_msg_waiter);
234 wake_up_all(&lgr->llc_flow_waiter);
237 static void smc_lgr_free(struct smc_link_group *lgr);
239 static void smc_lgr_free_work(struct work_struct *work)
241 struct smc_link_group *lgr = container_of(to_delayed_work(work),
242 struct smc_link_group,
244 spinlock_t *lgr_lock;
247 smc_lgr_list_head(lgr, &lgr_lock);
248 spin_lock_bh(lgr_lock);
250 spin_unlock_bh(lgr_lock);
253 read_lock_bh(&lgr->conns_lock);
254 conns = RB_EMPTY_ROOT(&lgr->conns_all);
255 read_unlock_bh(&lgr->conns_lock);
256 if (!conns) { /* number of lgr connections is no longer zero */
257 spin_unlock_bh(lgr_lock);
260 list_del_init(&lgr->list); /* remove from smc_lgr_list */
261 lgr->freeing = 1; /* this instance does the freeing, no new schedule */
262 spin_unlock_bh(lgr_lock);
263 cancel_delayed_work(&lgr->free_work);
265 if (!lgr->is_smcd && !lgr->terminating)
266 smc_llc_send_link_delete_all(lgr, true,
267 SMC_LLC_DEL_PROG_INIT_TERM);
268 if (lgr->is_smcd && !lgr->terminating)
269 smc_ism_signal_shutdown(lgr);
271 smcr_lgr_link_deactivate_all(lgr);
275 static void smc_lgr_terminate_work(struct work_struct *work)
277 struct smc_link_group *lgr = container_of(work, struct smc_link_group,
280 __smc_lgr_terminate(lgr, true);
283 /* return next unique link id for the lgr */
284 static u8 smcr_next_link_id(struct smc_link_group *lgr)
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)
303 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
304 u8 link_idx, struct smc_init_info *ini)
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);
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);
324 get_random_bytes(rndvec, sizeof(rndvec));
325 lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
327 rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
328 ini->vlan_id, lnk->gid, &lnk->sgid_index);
331 rc = smc_llc_link_init(lnk);
334 rc = smc_wr_alloc_link_mem(lnk);
337 rc = smc_ib_create_protection_domain(lnk);
340 rc = smc_ib_create_queue_pair(lnk);
343 rc = smc_wr_create_link(lnk);
346 lnk->state = SMC_LNK_ACTIVATING;
350 smc_ib_destroy_queue_pair(lnk);
352 smc_ib_dealloc_protection_domain(lnk);
354 smc_wr_free_link_mem(lnk);
356 smc_llc_link_clear(lnk, false);
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);
366 /* create a new SMC link group */
367 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
369 struct smc_link_group *lgr;
370 struct list_head *lgr_list;
371 struct smc_link *lnk;
372 spinlock_t *lgr_lock;
377 if (ini->is_smcd && ini->vlan_id) {
378 if (smc_ism_get_vlan(ini->ism_dev, ini->vlan_id)) {
379 rc = SMC_CLC_DECL_ISMVLANERR;
384 lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
386 rc = SMC_CLC_DECL_MEM;
389 lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0,
390 SMC_LGR_ID_SIZE, &lgr->id);
395 lgr->is_smcd = ini->is_smcd;
397 lgr->terminating = 0;
399 lgr->vlan_id = ini->vlan_id;
400 mutex_init(&lgr->sndbufs_lock);
401 mutex_init(&lgr->rmbs_lock);
402 rwlock_init(&lgr->conns_lock);
403 for (i = 0; i < SMC_RMBE_SIZES; i++) {
404 INIT_LIST_HEAD(&lgr->sndbufs[i]);
405 INIT_LIST_HEAD(&lgr->rmbs[i]);
407 lgr->next_link_id = 0;
408 smc_lgr_list.num += SMC_LGR_NUM_INCR;
409 memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
410 INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
411 INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
412 lgr->conns_all = RB_ROOT;
414 /* SMC-D specific settings */
415 get_device(&ini->ism_dev->dev);
416 lgr->peer_gid = ini->ism_peer_gid;
417 lgr->smcd = ini->ism_dev;
418 lgr_list = &ini->ism_dev->lgr_list;
419 lgr_lock = &lgr->smcd->lgr_lock;
420 lgr->peer_shutdown = 0;
421 atomic_inc(&ini->ism_dev->lgr_cnt);
423 /* SMC-R specific settings */
424 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
425 memcpy(lgr->peer_systemid, ini->ib_lcl->id_for_peer,
427 memcpy(lgr->pnet_id, ini->ib_dev->pnetid[ini->ib_port - 1],
429 smc_llc_lgr_init(lgr, smc);
431 link_idx = SMC_SINGLE_LINK;
432 lnk = &lgr->lnk[link_idx];
433 rc = smcr_link_init(lgr, lnk, link_idx, ini);
436 lgr_list = &smc_lgr_list.list;
437 lgr_lock = &smc_lgr_list.lock;
438 atomic_inc(&lgr_cnt);
441 spin_lock_bh(lgr_lock);
442 list_add_tail(&lgr->list, lgr_list);
443 spin_unlock_bh(lgr_lock);
447 destroy_workqueue(lgr->tx_wq);
451 if (ini->is_smcd && ini->vlan_id)
452 smc_ism_put_vlan(ini->ism_dev, ini->vlan_id);
456 rc = SMC_CLC_DECL_MEM;
458 rc = SMC_CLC_DECL_INTERR;
463 static int smc_write_space(struct smc_connection *conn)
465 int buffer_len = conn->peer_rmbe_size;
466 union smc_host_cursor prod;
467 union smc_host_cursor cons;
470 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
471 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
472 /* determine rx_buf space */
473 space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
477 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
478 struct smc_wr_buf *wr_buf)
480 struct smc_connection *conn = &smc->conn;
481 union smc_host_cursor cons, fin;
485 smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
486 smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
487 /* set prod cursor to old state, enforce tx_rdma_writes() */
488 smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
489 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
491 if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
492 /* cons cursor advanced more than fin, and prod was set
493 * fin above, so now prod is smaller than cons. Fix that.
495 diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
496 smc_curs_add(conn->sndbuf_desc->len,
497 &conn->tx_curs_sent, diff);
498 smc_curs_add(conn->sndbuf_desc->len,
499 &conn->tx_curs_fin, diff);
501 smp_mb__before_atomic();
502 atomic_add(diff, &conn->sndbuf_space);
503 smp_mb__after_atomic();
505 smc_curs_add(conn->peer_rmbe_size,
506 &conn->local_tx_ctrl.prod, diff);
507 smc_curs_add(conn->peer_rmbe_size,
508 &conn->local_tx_ctrl_fin, diff);
510 /* recalculate, value is used by tx_rdma_writes() */
511 atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
513 if (smc->sk.sk_state != SMC_INIT &&
514 smc->sk.sk_state != SMC_CLOSED) {
515 rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf);
517 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0);
518 smc->sk.sk_data_ready(&smc->sk);
521 smc_wr_tx_put_slot(conn->lnk,
522 (struct smc_wr_tx_pend_priv *)pend);
527 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
528 struct smc_link *from_lnk, bool is_dev_err)
530 struct smc_link *to_lnk = NULL;
531 struct smc_cdc_tx_pend *pend;
532 struct smc_connection *conn;
533 struct smc_wr_buf *wr_buf;
534 struct smc_sock *smc;
535 struct rb_node *node;
538 /* link is inactive, wake up tx waiters */
539 smc_wr_wakeup_tx_wait(from_lnk);
541 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
542 if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx)
544 if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev &&
545 from_lnk->ibport == lgr->lnk[i].ibport) {
548 to_lnk = &lgr->lnk[i];
552 smc_lgr_terminate_sched(lgr);
556 read_lock_bh(&lgr->conns_lock);
557 for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
558 conn = rb_entry(node, struct smc_connection, alert_node);
559 if (conn->lnk != from_lnk)
561 smc = container_of(conn, struct smc_sock, conn);
562 /* conn->lnk not yet set in SMC_INIT state */
563 if (smc->sk.sk_state == SMC_INIT)
565 if (smc->sk.sk_state == SMC_CLOSED ||
566 smc->sk.sk_state == SMC_PEERCLOSEWAIT1 ||
567 smc->sk.sk_state == SMC_PEERCLOSEWAIT2 ||
568 smc->sk.sk_state == SMC_APPFINCLOSEWAIT ||
569 smc->sk.sk_state == SMC_APPCLOSEWAIT1 ||
570 smc->sk.sk_state == SMC_APPCLOSEWAIT2 ||
571 smc->sk.sk_state == SMC_PEERFINCLOSEWAIT ||
572 smc->sk.sk_state == SMC_PEERABORTWAIT ||
573 smc->sk.sk_state == SMC_PROCESSABORT) {
574 spin_lock_bh(&conn->send_lock);
576 spin_unlock_bh(&conn->send_lock);
580 read_unlock_bh(&lgr->conns_lock);
581 /* pre-fetch buffer outside of send_lock, might sleep */
582 rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend);
584 smcr_link_down_cond_sched(to_lnk);
587 /* avoid race with smcr_tx_sndbuf_nonempty() */
588 spin_lock_bh(&conn->send_lock);
590 rc = smc_switch_cursor(smc, pend, wr_buf);
591 spin_unlock_bh(&conn->send_lock);
594 smcr_link_down_cond_sched(to_lnk);
599 read_unlock_bh(&lgr->conns_lock);
603 static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc,
604 struct smc_link_group *lgr)
608 if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) {
609 /* unregister rmb with peer */
610 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
612 /* protect against smc_llc_cli_rkey_exchange() */
613 mutex_lock(&lgr->llc_conf_mutex);
614 smc_llc_do_delete_rkey(lgr, rmb_desc);
615 rmb_desc->is_conf_rkey = false;
616 mutex_unlock(&lgr->llc_conf_mutex);
617 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
621 if (rmb_desc->is_reg_err) {
622 /* buf registration failed, reuse not possible */
623 mutex_lock(&lgr->rmbs_lock);
624 list_del(&rmb_desc->list);
625 mutex_unlock(&lgr->rmbs_lock);
627 smc_buf_free(lgr, true, rmb_desc);
633 static void smc_buf_unuse(struct smc_connection *conn,
634 struct smc_link_group *lgr)
636 if (conn->sndbuf_desc)
637 conn->sndbuf_desc->used = 0;
638 if (conn->rmb_desc && lgr->is_smcd)
639 conn->rmb_desc->used = 0;
640 else if (conn->rmb_desc)
641 smcr_buf_unuse(conn->rmb_desc, lgr);
644 /* remove a finished connection from its link group */
645 void smc_conn_free(struct smc_connection *conn)
647 struct smc_link_group *lgr = conn->lgr;
652 if (!list_empty(&lgr->list))
653 smc_ism_unset_conn(conn);
654 tasklet_kill(&conn->rx_tsklet);
656 smc_cdc_tx_dismiss_slots(conn);
657 if (current_work() != &conn->abort_work)
658 cancel_work_sync(&conn->abort_work);
660 if (!list_empty(&lgr->list)) {
661 smc_lgr_unregister_conn(conn);
662 smc_buf_unuse(conn, lgr); /* allow buffer reuse */
666 smc_lgr_schedule_free_work(lgr);
669 /* unregister a link from a buf_desc */
670 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb,
671 struct smc_link *lnk)
674 buf_desc->is_reg_mr[lnk->link_idx] = false;
675 if (!buf_desc->is_map_ib[lnk->link_idx])
678 if (buf_desc->mr_rx[lnk->link_idx]) {
679 smc_ib_put_memory_region(
680 buf_desc->mr_rx[lnk->link_idx]);
681 buf_desc->mr_rx[lnk->link_idx] = NULL;
683 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
685 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
687 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
688 buf_desc->is_map_ib[lnk->link_idx] = false;
691 /* unmap all buffers of lgr for a deleted link */
692 static void smcr_buf_unmap_lgr(struct smc_link *lnk)
694 struct smc_link_group *lgr = lnk->lgr;
695 struct smc_buf_desc *buf_desc, *bf;
698 for (i = 0; i < SMC_RMBE_SIZES; i++) {
699 mutex_lock(&lgr->rmbs_lock);
700 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list)
701 smcr_buf_unmap_link(buf_desc, true, lnk);
702 mutex_unlock(&lgr->rmbs_lock);
703 mutex_lock(&lgr->sndbufs_lock);
704 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i],
706 smcr_buf_unmap_link(buf_desc, false, lnk);
707 mutex_unlock(&lgr->sndbufs_lock);
711 static void smcr_rtoken_clear_link(struct smc_link *lnk)
713 struct smc_link_group *lgr = lnk->lgr;
716 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
717 lgr->rtokens[i][lnk->link_idx].rkey = 0;
718 lgr->rtokens[i][lnk->link_idx].dma_addr = 0;
722 /* must be called under lgr->llc_conf_mutex lock */
723 void smcr_link_clear(struct smc_link *lnk, bool log)
725 struct smc_ib_device *smcibdev;
727 if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
730 smc_llc_link_clear(lnk, log);
731 smcr_buf_unmap_lgr(lnk);
732 smcr_rtoken_clear_link(lnk);
733 smc_ib_modify_qp_reset(lnk);
734 smc_wr_free_link(lnk);
735 smc_ib_destroy_queue_pair(lnk);
736 smc_ib_dealloc_protection_domain(lnk);
737 smc_wr_free_link_mem(lnk);
738 put_device(&lnk->smcibdev->ibdev->dev);
739 smcibdev = lnk->smcibdev;
740 memset(lnk, 0, sizeof(struct smc_link));
741 lnk->state = SMC_LNK_UNUSED;
742 if (!atomic_dec_return(&smcibdev->lnk_cnt))
743 wake_up(&smcibdev->lnks_deleted);
746 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
747 struct smc_buf_desc *buf_desc)
751 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
752 smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]);
755 __free_pages(buf_desc->pages, buf_desc->order);
759 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
760 struct smc_buf_desc *buf_desc)
763 /* restore original buf len */
764 buf_desc->len += sizeof(struct smcd_cdc_msg);
765 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
767 kfree(buf_desc->cpu_addr);
772 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
773 struct smc_buf_desc *buf_desc)
776 smcd_buf_free(lgr, is_rmb, buf_desc);
778 smcr_buf_free(lgr, is_rmb, buf_desc);
781 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
783 struct smc_buf_desc *buf_desc, *bf_desc;
784 struct list_head *buf_list;
787 for (i = 0; i < SMC_RMBE_SIZES; i++) {
789 buf_list = &lgr->rmbs[i];
791 buf_list = &lgr->sndbufs[i];
792 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
794 list_del(&buf_desc->list);
795 smc_buf_free(lgr, is_rmb, buf_desc);
800 static void smc_lgr_free_bufs(struct smc_link_group *lgr)
802 /* free send buffers */
803 __smc_lgr_free_bufs(lgr, false);
805 __smc_lgr_free_bufs(lgr, true);
808 /* remove a link group */
809 static void smc_lgr_free(struct smc_link_group *lgr)
814 mutex_lock(&lgr->llc_conf_mutex);
815 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
816 if (lgr->lnk[i].state != SMC_LNK_UNUSED)
817 smcr_link_clear(&lgr->lnk[i], false);
819 mutex_unlock(&lgr->llc_conf_mutex);
820 smc_llc_lgr_clear(lgr);
823 smc_lgr_free_bufs(lgr);
824 destroy_workqueue(lgr->tx_wq);
826 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
827 put_device(&lgr->smcd->dev);
828 if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
829 wake_up(&lgr->smcd->lgrs_deleted);
831 if (!atomic_dec_return(&lgr_cnt))
832 wake_up(&lgrs_deleted);
837 static void smcd_unregister_all_dmbs(struct smc_link_group *lgr)
841 for (i = 0; i < SMC_RMBE_SIZES; i++) {
842 struct smc_buf_desc *buf_desc;
844 list_for_each_entry(buf_desc, &lgr->rmbs[i], list) {
845 buf_desc->len += sizeof(struct smcd_cdc_msg);
846 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
851 static void smc_sk_wake_ups(struct smc_sock *smc)
853 smc->sk.sk_write_space(&smc->sk);
854 smc->sk.sk_data_ready(&smc->sk);
855 smc->sk.sk_state_change(&smc->sk);
858 /* kill a connection */
859 static void smc_conn_kill(struct smc_connection *conn, bool soft)
861 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
863 if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
864 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
866 smc_close_abort(conn);
868 smc->sk.sk_err = ECONNABORTED;
869 smc_sk_wake_ups(smc);
870 if (conn->lgr->is_smcd) {
871 smc_ism_unset_conn(conn);
873 tasklet_kill(&conn->rx_tsklet);
875 tasklet_unlock_wait(&conn->rx_tsklet);
877 smc_cdc_tx_dismiss_slots(conn);
879 smc_lgr_unregister_conn(conn);
880 smc_close_active_abort(smc);
883 static void smc_lgr_cleanup(struct smc_link_group *lgr)
886 smc_ism_signal_shutdown(lgr);
887 smcd_unregister_all_dmbs(lgr);
889 u32 rsn = lgr->llc_termination_rsn;
892 rsn = SMC_LLC_DEL_PROG_INIT_TERM;
893 smc_llc_send_link_delete_all(lgr, false, rsn);
894 smcr_lgr_link_deactivate_all(lgr);
898 /* terminate link group
899 * @soft: true if link group shutdown can take its time
900 * false if immediate link group shutdown is required
902 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
904 struct smc_connection *conn;
905 struct smc_sock *smc;
906 struct rb_node *node;
908 if (lgr->terminating)
909 return; /* lgr already terminating */
910 /* cancel free_work sync, will terminate when lgr->freeing is set */
911 cancel_delayed_work_sync(&lgr->free_work);
912 lgr->terminating = 1;
914 /* kill remaining link group connections */
915 read_lock_bh(&lgr->conns_lock);
916 node = rb_first(&lgr->conns_all);
918 read_unlock_bh(&lgr->conns_lock);
919 conn = rb_entry(node, struct smc_connection, alert_node);
920 smc = container_of(conn, struct smc_sock, conn);
921 sock_hold(&smc->sk); /* sock_put below */
923 smc_conn_kill(conn, soft);
924 release_sock(&smc->sk);
925 sock_put(&smc->sk); /* sock_hold above */
926 read_lock_bh(&lgr->conns_lock);
927 node = rb_first(&lgr->conns_all);
929 read_unlock_bh(&lgr->conns_lock);
930 smc_lgr_cleanup(lgr);
934 /* unlink link group and schedule termination */
935 void smc_lgr_terminate_sched(struct smc_link_group *lgr)
937 spinlock_t *lgr_lock;
939 smc_lgr_list_head(lgr, &lgr_lock);
940 spin_lock_bh(lgr_lock);
941 if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
942 spin_unlock_bh(lgr_lock);
943 return; /* lgr already terminating */
945 list_del_init(&lgr->list);
947 spin_unlock_bh(lgr_lock);
948 schedule_work(&lgr->terminate_work);
951 /* Called when peer lgr shutdown (regularly or abnormally) is received */
952 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
954 struct smc_link_group *lgr, *l;
955 LIST_HEAD(lgr_free_list);
957 /* run common cleanup function and build free list */
958 spin_lock_bh(&dev->lgr_lock);
959 list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
960 if ((!peer_gid || lgr->peer_gid == peer_gid) &&
961 (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
962 if (peer_gid) /* peer triggered termination */
963 lgr->peer_shutdown = 1;
964 list_move(&lgr->list, &lgr_free_list);
968 spin_unlock_bh(&dev->lgr_lock);
970 /* cancel the regular free workers and actually free lgrs */
971 list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
972 list_del_init(&lgr->list);
973 schedule_work(&lgr->terminate_work);
977 /* Called when an SMCD device is removed or the smc module is unloaded */
978 void smc_smcd_terminate_all(struct smcd_dev *smcd)
980 struct smc_link_group *lgr, *lg;
981 LIST_HEAD(lgr_free_list);
983 spin_lock_bh(&smcd->lgr_lock);
984 list_splice_init(&smcd->lgr_list, &lgr_free_list);
985 list_for_each_entry(lgr, &lgr_free_list, list)
987 spin_unlock_bh(&smcd->lgr_lock);
989 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
990 list_del_init(&lgr->list);
991 __smc_lgr_terminate(lgr, false);
994 if (atomic_read(&smcd->lgr_cnt))
995 wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
998 /* Called when an SMCR device is removed or the smc module is unloaded.
999 * If smcibdev is given, all SMCR link groups using this device are terminated.
1000 * If smcibdev is NULL, all SMCR link groups are terminated.
1002 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1004 struct smc_link_group *lgr, *lg;
1005 LIST_HEAD(lgr_free_list);
1008 spin_lock_bh(&smc_lgr_list.lock);
1010 list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1011 list_for_each_entry(lgr, &lgr_free_list, list)
1014 list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1015 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1016 if (lgr->lnk[i].smcibdev == smcibdev)
1017 smcr_link_down_cond_sched(&lgr->lnk[i]);
1021 spin_unlock_bh(&smc_lgr_list.lock);
1023 list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1024 list_del_init(&lgr->list);
1025 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1026 __smc_lgr_terminate(lgr, false);
1030 if (atomic_read(&smcibdev->lnk_cnt))
1031 wait_event(smcibdev->lnks_deleted,
1032 !atomic_read(&smcibdev->lnk_cnt));
1034 if (atomic_read(&lgr_cnt))
1035 wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1039 /* set new lgr type and clear all asymmetric link tagging */
1040 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1042 char *lgr_type = "";
1045 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1046 if (smc_link_usable(&lgr->lnk[i]))
1047 lgr->lnk[i].link_is_asym = false;
1048 if (lgr->type == new_type)
1050 lgr->type = new_type;
1052 switch (lgr->type) {
1056 case SMC_LGR_SINGLE:
1057 lgr_type = "SINGLE";
1059 case SMC_LGR_SYMMETRIC:
1060 lgr_type = "SYMMETRIC";
1062 case SMC_LGR_ASYMMETRIC_PEER:
1063 lgr_type = "ASYMMETRIC_PEER";
1065 case SMC_LGR_ASYMMETRIC_LOCAL:
1066 lgr_type = "ASYMMETRIC_LOCAL";
1069 pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: "
1070 "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1071 lgr_type, lgr->pnet_id);
1074 /* set new lgr type and tag a link as asymmetric */
1075 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1076 enum smc_lgr_type new_type, int asym_lnk_idx)
1078 smcr_lgr_set_type(lgr, new_type);
1079 lgr->lnk[asym_lnk_idx].link_is_asym = true;
1082 /* abort connection, abort_work scheduled from tasklet context */
1083 static void smc_conn_abort_work(struct work_struct *work)
1085 struct smc_connection *conn = container_of(work,
1086 struct smc_connection,
1088 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1090 smc_conn_kill(conn, true);
1091 sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1094 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1096 struct smc_link_group *lgr, *n;
1098 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1099 struct smc_link *link;
1101 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1102 SMC_MAX_PNETID_LEN) ||
1103 lgr->type == SMC_LGR_SYMMETRIC ||
1104 lgr->type == SMC_LGR_ASYMMETRIC_PEER)
1107 /* trigger local add link processing */
1108 link = smc_llc_usable_link(lgr);
1110 smc_llc_add_link_local(link);
1114 /* link is down - switch connections to alternate link,
1115 * must be called under lgr->llc_conf_mutex lock
1117 static void smcr_link_down(struct smc_link *lnk)
1119 struct smc_link_group *lgr = lnk->lgr;
1120 struct smc_link *to_lnk;
1123 if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1126 smc_ib_modify_qp_reset(lnk);
1127 to_lnk = smc_switch_conns(lgr, lnk, true);
1128 if (!to_lnk) { /* no backup link available */
1129 smcr_link_clear(lnk, true);
1132 smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1133 del_link_id = lnk->link_id;
1135 if (lgr->role == SMC_SERV) {
1136 /* trigger local delete link processing */
1137 smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1139 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1140 /* another llc task is ongoing */
1141 mutex_unlock(&lgr->llc_conf_mutex);
1142 wait_event_timeout(lgr->llc_flow_waiter,
1143 (list_empty(&lgr->list) ||
1144 lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1146 mutex_lock(&lgr->llc_conf_mutex);
1148 if (!list_empty(&lgr->list)) {
1149 smc_llc_send_delete_link(to_lnk, del_link_id,
1151 SMC_LLC_DEL_LOST_PATH);
1152 smcr_link_clear(lnk, true);
1154 wake_up(&lgr->llc_flow_waiter); /* wake up next waiter */
1158 /* must be called under lgr->llc_conf_mutex lock */
1159 void smcr_link_down_cond(struct smc_link *lnk)
1161 if (smc_link_downing(&lnk->state))
1162 smcr_link_down(lnk);
1165 /* will get the lgr->llc_conf_mutex lock */
1166 void smcr_link_down_cond_sched(struct smc_link *lnk)
1168 if (smc_link_downing(&lnk->state))
1169 schedule_work(&lnk->link_down_wrk);
1172 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1174 struct smc_link_group *lgr, *n;
1177 list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1178 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1179 SMC_MAX_PNETID_LEN))
1180 continue; /* lgr is not affected */
1181 if (list_empty(&lgr->list))
1183 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1184 struct smc_link *lnk = &lgr->lnk[i];
1186 if (smc_link_usable(lnk) &&
1187 lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1188 smcr_link_down_cond_sched(lnk);
1193 static void smc_link_down_work(struct work_struct *work)
1195 struct smc_link *link = container_of(work, struct smc_link,
1197 struct smc_link_group *lgr = link->lgr;
1199 if (list_empty(&lgr->list))
1201 wake_up_all(&lgr->llc_msg_waiter);
1202 mutex_lock(&lgr->llc_conf_mutex);
1203 smcr_link_down(link);
1204 mutex_unlock(&lgr->llc_conf_mutex);
1207 /* Determine vlan of internal TCP socket.
1208 * @vlan_id: address to store the determined vlan id into
1210 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1212 struct dst_entry *dst = sk_dst_get(clcsock->sk);
1213 struct net_device *ndev;
1214 int i, nest_lvl, rc = 0;
1227 if (is_vlan_dev(ndev)) {
1228 ini->vlan_id = vlan_dev_vlan_id(ndev);
1233 nest_lvl = ndev->lower_level;
1234 for (i = 0; i < nest_lvl; i++) {
1235 struct list_head *lower = &ndev->adj_list.lower;
1237 if (list_empty(lower))
1239 lower = lower->next;
1240 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
1241 if (is_vlan_dev(ndev)) {
1242 ini->vlan_id = vlan_dev_vlan_id(ndev);
1254 static bool smcr_lgr_match(struct smc_link_group *lgr,
1255 struct smc_clc_msg_local *lcl,
1256 enum smc_lgr_role role, u32 clcqpn)
1260 if (memcmp(lgr->peer_systemid, lcl->id_for_peer, SMC_SYSTEMID_LEN) ||
1264 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1265 if (!smc_link_active(&lgr->lnk[i]))
1267 if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
1268 !memcmp(lgr->lnk[i].peer_gid, &lcl->gid, SMC_GID_SIZE) &&
1269 !memcmp(lgr->lnk[i].peer_mac, lcl->mac, sizeof(lcl->mac)))
1275 static bool smcd_lgr_match(struct smc_link_group *lgr,
1276 struct smcd_dev *smcismdev, u64 peer_gid)
1278 return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1281 /* create a new SMC connection (and a new link group if necessary) */
1282 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1284 struct smc_connection *conn = &smc->conn;
1285 struct list_head *lgr_list;
1286 struct smc_link_group *lgr;
1287 enum smc_lgr_role role;
1288 spinlock_t *lgr_lock;
1291 lgr_list = ini->is_smcd ? &ini->ism_dev->lgr_list : &smc_lgr_list.list;
1292 lgr_lock = ini->is_smcd ? &ini->ism_dev->lgr_lock : &smc_lgr_list.lock;
1293 ini->first_contact_local = 1;
1294 role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1295 if (role == SMC_CLNT && ini->first_contact_peer)
1296 /* create new link group as well */
1299 /* determine if an existing link group can be reused */
1300 spin_lock_bh(lgr_lock);
1301 list_for_each_entry(lgr, lgr_list, list) {
1302 write_lock_bh(&lgr->conns_lock);
1304 smcd_lgr_match(lgr, ini->ism_dev, ini->ism_peer_gid) :
1305 smcr_lgr_match(lgr, ini->ib_lcl, role, ini->ib_clcqpn)) &&
1307 lgr->vlan_id == ini->vlan_id &&
1308 (role == SMC_CLNT || ini->is_smcd ||
1309 lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
1310 /* link group found */
1311 ini->first_contact_local = 0;
1313 rc = smc_lgr_register_conn(conn, false);
1314 write_unlock_bh(&lgr->conns_lock);
1315 if (!rc && delayed_work_pending(&lgr->free_work))
1316 cancel_delayed_work(&lgr->free_work);
1319 write_unlock_bh(&lgr->conns_lock);
1321 spin_unlock_bh(lgr_lock);
1325 if (role == SMC_CLNT && !ini->first_contact_peer &&
1326 ini->first_contact_local) {
1327 /* Server reuses a link group, but Client wants to start
1329 * send out_of_sync decline, reason synchr. error
1331 return SMC_CLC_DECL_SYNCERR;
1335 if (ini->first_contact_local) {
1336 rc = smc_lgr_create(smc, ini);
1340 write_lock_bh(&lgr->conns_lock);
1341 rc = smc_lgr_register_conn(conn, true);
1342 write_unlock_bh(&lgr->conns_lock);
1346 conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1347 conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1348 conn->urg_state = SMC_URG_READ;
1349 INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1351 conn->rx_off = sizeof(struct smcd_cdc_msg);
1352 smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1356 #ifndef KERNEL_HAS_ATOMIC64
1357 spin_lock_init(&conn->acurs_lock);
1364 /* convert the RMB size into the compressed notation - minimum 16K.
1365 * In contrast to plain ilog2, this rounds towards the next power of 2,
1366 * so the socket application gets at least its desired sndbuf / rcvbuf size.
1368 static u8 smc_compress_bufsize(int size)
1372 if (size <= SMC_BUF_MIN_SIZE)
1375 size = (size - 1) >> 14;
1376 compressed = ilog2(size) + 1;
1377 if (compressed >= SMC_RMBE_SIZES)
1378 compressed = SMC_RMBE_SIZES - 1;
1382 /* convert the RMB size from compressed notation into integer */
1383 int smc_uncompress_bufsize(u8 compressed)
1387 size = 0x00000001 << (((int)compressed) + 14);
1391 /* try to reuse a sndbuf or rmb description slot for a certain
1392 * buffer size; if not available, return NULL
1394 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
1396 struct list_head *buf_list)
1398 struct smc_buf_desc *buf_slot;
1401 list_for_each_entry(buf_slot, buf_list, list) {
1402 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
1411 /* one of the conditions for announcing a receiver's current window size is
1412 * that it "results in a minimum increase in the window size of 10% of the
1413 * receive buffer space" [RFC7609]
1415 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
1417 return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
1420 /* map an rmb buf to a link */
1421 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1422 struct smc_link *lnk)
1426 if (buf_desc->is_map_ib[lnk->link_idx])
1429 rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
1432 sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
1433 buf_desc->cpu_addr, buf_desc->len);
1435 /* map sg table to DMA address */
1436 rc = smc_ib_buf_map_sg(lnk, buf_desc,
1437 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1438 /* SMC protocol depends on mapping to one DMA address only */
1444 /* create a new memory region for the RMB */
1446 rc = smc_ib_get_memory_region(lnk->roce_pd,
1447 IB_ACCESS_REMOTE_WRITE |
1448 IB_ACCESS_LOCAL_WRITE,
1449 buf_desc, lnk->link_idx);
1452 smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
1454 buf_desc->is_map_ib[lnk->link_idx] = true;
1458 smc_ib_buf_unmap_sg(lnk, buf_desc,
1459 is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1461 sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1465 /* register a new rmb on IB device,
1466 * must be called under lgr->llc_conf_mutex lock
1468 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
1470 if (list_empty(&link->lgr->list))
1472 if (!rmb_desc->is_reg_mr[link->link_idx]) {
1473 /* register memory region for new rmb */
1474 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
1475 rmb_desc->is_reg_err = true;
1478 rmb_desc->is_reg_mr[link->link_idx] = true;
1483 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock,
1484 struct list_head *lst, bool is_rmb)
1486 struct smc_buf_desc *buf_desc, *bf;
1490 list_for_each_entry_safe(buf_desc, bf, lst, list) {
1491 if (!buf_desc->used)
1493 rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
1502 /* map all used buffers of lgr for a new link */
1503 int smcr_buf_map_lgr(struct smc_link *lnk)
1505 struct smc_link_group *lgr = lnk->lgr;
1508 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1509 rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
1510 &lgr->rmbs[i], true);
1513 rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
1514 &lgr->sndbufs[i], false);
1521 /* register all used buffers of lgr for a new link,
1522 * must be called under lgr->llc_conf_mutex lock
1524 int smcr_buf_reg_lgr(struct smc_link *lnk)
1526 struct smc_link_group *lgr = lnk->lgr;
1527 struct smc_buf_desc *buf_desc, *bf;
1530 mutex_lock(&lgr->rmbs_lock);
1531 for (i = 0; i < SMC_RMBE_SIZES; i++) {
1532 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
1533 if (!buf_desc->used)
1535 rc = smcr_link_reg_rmb(lnk, buf_desc);
1541 mutex_unlock(&lgr->rmbs_lock);
1545 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
1546 bool is_rmb, int bufsize)
1548 struct smc_buf_desc *buf_desc;
1550 /* try to alloc a new buffer */
1551 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1553 return ERR_PTR(-ENOMEM);
1555 buf_desc->order = get_order(bufsize);
1556 buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
1557 __GFP_NOMEMALLOC | __GFP_COMP |
1558 __GFP_NORETRY | __GFP_ZERO,
1560 if (!buf_desc->pages) {
1562 return ERR_PTR(-EAGAIN);
1564 buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
1565 buf_desc->len = bufsize;
1569 /* map buf_desc on all usable links,
1570 * unused buffers stay mapped as long as the link is up
1572 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
1573 struct smc_buf_desc *buf_desc, bool is_rmb)
1577 /* protect against parallel link reconfiguration */
1578 mutex_lock(&lgr->llc_conf_mutex);
1579 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1580 struct smc_link *lnk = &lgr->lnk[i];
1582 if (!smc_link_usable(lnk))
1584 if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
1590 mutex_unlock(&lgr->llc_conf_mutex);
1594 #define SMCD_DMBE_SIZES 7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1596 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
1597 bool is_dmb, int bufsize)
1599 struct smc_buf_desc *buf_desc;
1602 if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES)
1603 return ERR_PTR(-EAGAIN);
1605 /* try to alloc a new DMB */
1606 buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1608 return ERR_PTR(-ENOMEM);
1610 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
1613 return (rc == -ENOMEM) ? ERR_PTR(-EAGAIN) : ERR_PTR(rc);
1615 buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
1616 /* CDC header stored in buf. So, pretend it was smaller */
1617 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
1619 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
1620 __GFP_NOWARN | __GFP_NORETRY |
1622 if (!buf_desc->cpu_addr) {
1624 return ERR_PTR(-EAGAIN);
1626 buf_desc->len = bufsize;
1631 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
1633 struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
1634 struct smc_connection *conn = &smc->conn;
1635 struct smc_link_group *lgr = conn->lgr;
1636 struct list_head *buf_list;
1637 int bufsize, bufsize_short;
1638 struct mutex *lock; /* lock buffer list */
1642 /* use socket recv buffer size (w/o overhead) as start value */
1643 sk_buf_size = smc->sk.sk_rcvbuf / 2;
1645 /* use socket send buffer size (w/o overhead) as start value */
1646 sk_buf_size = smc->sk.sk_sndbuf / 2;
1648 for (bufsize_short = smc_compress_bufsize(sk_buf_size);
1649 bufsize_short >= 0; bufsize_short--) {
1652 lock = &lgr->rmbs_lock;
1653 buf_list = &lgr->rmbs[bufsize_short];
1655 lock = &lgr->sndbufs_lock;
1656 buf_list = &lgr->sndbufs[bufsize_short];
1658 bufsize = smc_uncompress_bufsize(bufsize_short);
1659 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
1662 /* check for reusable slot in the link group */
1663 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
1665 memset(buf_desc->cpu_addr, 0, bufsize);
1666 break; /* found reusable slot */
1670 buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
1672 buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
1674 if (PTR_ERR(buf_desc) == -ENOMEM)
1676 if (IS_ERR(buf_desc))
1681 list_add(&buf_desc->list, buf_list);
1686 if (IS_ERR(buf_desc))
1687 return PTR_ERR(buf_desc);
1690 if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
1691 smcr_buf_unuse(buf_desc, lgr);
1697 conn->rmb_desc = buf_desc;
1698 conn->rmbe_size_short = bufsize_short;
1699 smc->sk.sk_rcvbuf = bufsize * 2;
1700 atomic_set(&conn->bytes_to_rcv, 0);
1701 conn->rmbe_update_limit =
1702 smc_rmb_wnd_update_limit(buf_desc->len);
1704 smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
1706 conn->sndbuf_desc = buf_desc;
1707 smc->sk.sk_sndbuf = bufsize * 2;
1708 atomic_set(&conn->sndbuf_space, bufsize);
1713 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
1715 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1717 smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1720 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
1722 if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1724 smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1727 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
1731 if (!conn->lgr || conn->lgr->is_smcd)
1733 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1734 if (!smc_link_active(&conn->lgr->lnk[i]))
1736 smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
1741 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
1745 if (!conn->lgr || conn->lgr->is_smcd)
1747 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1748 if (!smc_link_active(&conn->lgr->lnk[i]))
1750 smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
1755 /* create the send and receive buffer for an SMC socket;
1756 * receive buffers are called RMBs;
1757 * (even though the SMC protocol allows more than one RMB-element per RMB,
1758 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
1759 * extra RMB for every connection in a link group
1761 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
1765 /* create send buffer */
1766 rc = __smc_buf_create(smc, is_smcd, false);
1770 rc = __smc_buf_create(smc, is_smcd, true);
1772 mutex_lock(&smc->conn.lgr->sndbufs_lock);
1773 list_del(&smc->conn.sndbuf_desc->list);
1774 mutex_unlock(&smc->conn.lgr->sndbufs_lock);
1775 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
1776 smc->conn.sndbuf_desc = NULL;
1781 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
1785 for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
1786 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
1792 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
1797 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1798 if (test_bit(i, lgr->rtokens_used_mask) &&
1799 lgr->rtokens[i][lnk_idx].rkey == rkey)
1805 /* set rtoken for a new link to an existing rmb */
1806 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
1807 __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
1811 rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
1812 if (rtok_idx == -ENOENT)
1814 lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
1815 lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
1818 /* set rtoken for a new link whose link_id is given */
1819 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
1820 __be64 nw_vaddr, __be32 nw_rkey)
1822 u64 dma_addr = be64_to_cpu(nw_vaddr);
1823 u32 rkey = ntohl(nw_rkey);
1827 for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
1828 if (lgr->lnk[link_idx].link_id == link_id) {
1835 lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
1836 lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
1839 /* add a new rtoken from peer */
1840 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
1842 struct smc_link_group *lgr = smc_get_lgr(lnk);
1843 u64 dma_addr = be64_to_cpu(nw_vaddr);
1844 u32 rkey = ntohl(nw_rkey);
1847 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1848 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1849 lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
1850 test_bit(i, lgr->rtokens_used_mask)) {
1851 /* already in list */
1855 i = smc_rmb_reserve_rtoken_idx(lgr);
1858 lgr->rtokens[i][lnk->link_idx].rkey = rkey;
1859 lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
1863 /* delete an rtoken from all links */
1864 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
1866 struct smc_link_group *lgr = smc_get_lgr(lnk);
1867 u32 rkey = ntohl(nw_rkey);
1870 for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1871 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1872 test_bit(i, lgr->rtokens_used_mask)) {
1873 for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
1874 lgr->rtokens[i][j].rkey = 0;
1875 lgr->rtokens[i][j].dma_addr = 0;
1877 clear_bit(i, lgr->rtokens_used_mask);
1884 /* save rkey and dma_addr received from peer during clc handshake */
1885 int smc_rmb_rtoken_handling(struct smc_connection *conn,
1886 struct smc_link *lnk,
1887 struct smc_clc_msg_accept_confirm *clc)
1889 conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
1891 if (conn->rtoken_idx < 0)
1892 return conn->rtoken_idx;
1896 static void smc_core_going_away(void)
1898 struct smc_ib_device *smcibdev;
1899 struct smcd_dev *smcd;
1901 mutex_lock(&smc_ib_devices.mutex);
1902 list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
1905 for (i = 0; i < SMC_MAX_PORTS; i++)
1906 set_bit(i, smcibdev->ports_going_away);
1908 mutex_unlock(&smc_ib_devices.mutex);
1910 mutex_lock(&smcd_dev_list.mutex);
1911 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1912 smcd->going_away = 1;
1914 mutex_unlock(&smcd_dev_list.mutex);
1917 /* Clean up all SMC link groups */
1918 static void smc_lgrs_shutdown(void)
1920 struct smcd_dev *smcd;
1922 smc_core_going_away();
1924 smc_smcr_terminate_all(NULL);
1926 mutex_lock(&smcd_dev_list.mutex);
1927 list_for_each_entry(smcd, &smcd_dev_list.list, list)
1928 smc_smcd_terminate_all(smcd);
1929 mutex_unlock(&smcd_dev_list.mutex);
1932 static int smc_core_reboot_event(struct notifier_block *this,
1933 unsigned long event, void *ptr)
1935 smc_lgrs_shutdown();
1936 smc_ib_unregister_client();
1940 static struct notifier_block smc_reboot_notifier = {
1941 .notifier_call = smc_core_reboot_event,
1944 int __init smc_core_init(void)
1946 return register_reboot_notifier(&smc_reboot_notifier);
1949 /* Called (from smc_exit) when module is removed */
1950 void smc_core_exit(void)
1952 unregister_reboot_notifier(&smc_reboot_notifier);
1953 smc_lgrs_shutdown();