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>
20 #include <rdma/ib_verbs.h>
21 #include <rdma/ib_cache.h>
30 #include "smc_close.h"
33 #define SMC_LGR_NUM_INCR 256
34 #define SMC_LGR_FREE_DELAY_SERV (600 * HZ)
35 #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
36 #define SMC_LGR_FREE_DELAY_FAST (8 * 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 /* return head of link group list and its lock for a given link group */
52 static inline struct list_head
*smc_lgr_list_head(struct smc_link_group
*lgr
,
53 spinlock_t
**lgr_lock
)
56 *lgr_lock
= &lgr
->smcd
->lgr_lock
;
57 return &lgr
->smcd
->lgr_list
;
60 *lgr_lock
= &smc_lgr_list
.lock
;
61 return &smc_lgr_list
.list
;
64 static void smc_lgr_schedule_free_work(struct smc_link_group
*lgr
)
66 /* client link group creation always follows the server link group
67 * creation. For client use a somewhat higher removal delay time,
68 * otherwise there is a risk of out-of-sync link groups.
70 if (!lgr
->freeing
&& !lgr
->freefast
) {
71 mod_delayed_work(system_wq
, &lgr
->free_work
,
72 (!lgr
->is_smcd
&& lgr
->role
== SMC_CLNT
) ?
73 SMC_LGR_FREE_DELAY_CLNT
:
74 SMC_LGR_FREE_DELAY_SERV
);
78 void smc_lgr_schedule_free_work_fast(struct smc_link_group
*lgr
)
80 if (!lgr
->freeing
&& !lgr
->freefast
) {
82 mod_delayed_work(system_wq
, &lgr
->free_work
,
83 SMC_LGR_FREE_DELAY_FAST
);
87 /* Register connection's alert token in our lookup structure.
88 * To use rbtrees we have to implement our own insert core.
89 * Requires @conns_lock
90 * @smc connection to register
91 * Returns 0 on success, != otherwise.
93 static void smc_lgr_add_alert_token(struct smc_connection
*conn
)
95 struct rb_node
**link
, *parent
= NULL
;
96 u32 token
= conn
->alert_token_local
;
98 link
= &conn
->lgr
->conns_all
.rb_node
;
100 struct smc_connection
*cur
= rb_entry(*link
,
101 struct smc_connection
, alert_node
);
104 if (cur
->alert_token_local
> token
)
105 link
= &parent
->rb_left
;
107 link
= &parent
->rb_right
;
109 /* Put the new node there */
110 rb_link_node(&conn
->alert_node
, parent
, link
);
111 rb_insert_color(&conn
->alert_node
, &conn
->lgr
->conns_all
);
114 /* Register connection in link group by assigning an alert token
115 * registered in a search tree.
116 * Requires @conns_lock
117 * Note that '0' is a reserved value and not assigned.
119 static void smc_lgr_register_conn(struct smc_connection
*conn
)
121 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
122 static atomic_t nexttoken
= ATOMIC_INIT(0);
124 /* find a new alert_token_local value not yet used by some connection
127 sock_hold(&smc
->sk
); /* sock_put in smc_lgr_unregister_conn() */
128 while (!conn
->alert_token_local
) {
129 conn
->alert_token_local
= atomic_inc_return(&nexttoken
);
130 if (smc_lgr_find_conn(conn
->alert_token_local
, conn
->lgr
))
131 conn
->alert_token_local
= 0;
133 smc_lgr_add_alert_token(conn
);
134 conn
->lgr
->conns_num
++;
137 /* Unregister connection and reset the alert token of the given connection<
139 static void __smc_lgr_unregister_conn(struct smc_connection
*conn
)
141 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
142 struct smc_link_group
*lgr
= conn
->lgr
;
144 rb_erase(&conn
->alert_node
, &lgr
->conns_all
);
146 conn
->alert_token_local
= 0;
147 sock_put(&smc
->sk
); /* sock_hold in smc_lgr_register_conn() */
150 /* Unregister connection from lgr
152 static void smc_lgr_unregister_conn(struct smc_connection
*conn
)
154 struct smc_link_group
*lgr
= conn
->lgr
;
158 write_lock_bh(&lgr
->conns_lock
);
159 if (conn
->alert_token_local
) {
160 __smc_lgr_unregister_conn(conn
);
162 write_unlock_bh(&lgr
->conns_lock
);
166 void smc_lgr_cleanup_early(struct smc_connection
*conn
)
168 struct smc_link_group
*lgr
= conn
->lgr
;
175 smc_lgr_schedule_free_work_fast(lgr
);
178 /* Send delete link, either as client to request the initiation
179 * of the DELETE LINK sequence from server; or as server to
180 * initiate the delete processing. See smc_llc_rx_delete_link().
182 static int smc_link_send_delete(struct smc_link
*lnk
, bool orderly
)
184 if (lnk
->state
== SMC_LNK_ACTIVE
&&
185 !smc_llc_send_delete_link(lnk
, SMC_LLC_REQ
, orderly
)) {
186 smc_llc_link_deleting(lnk
);
192 static void smc_lgr_free(struct smc_link_group
*lgr
);
194 static void smc_lgr_free_work(struct work_struct
*work
)
196 struct smc_link_group
*lgr
= container_of(to_delayed_work(work
),
197 struct smc_link_group
,
199 spinlock_t
*lgr_lock
;
200 struct smc_link
*lnk
;
203 smc_lgr_list_head(lgr
, &lgr_lock
);
204 spin_lock_bh(lgr_lock
);
206 spin_unlock_bh(lgr_lock
);
209 read_lock_bh(&lgr
->conns_lock
);
210 conns
= RB_EMPTY_ROOT(&lgr
->conns_all
);
211 read_unlock_bh(&lgr
->conns_lock
);
212 if (!conns
) { /* number of lgr connections is no longer zero */
213 spin_unlock_bh(lgr_lock
);
216 list_del_init(&lgr
->list
); /* remove from smc_lgr_list */
218 lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
219 if (!lgr
->is_smcd
&& !lgr
->terminating
) {
220 /* try to send del link msg, on error free lgr immediately */
221 if (lnk
->state
== SMC_LNK_ACTIVE
&&
222 !smc_link_send_delete(lnk
, true)) {
223 /* reschedule in case we never receive a response */
224 smc_lgr_schedule_free_work(lgr
);
225 spin_unlock_bh(lgr_lock
);
229 lgr
->freeing
= 1; /* this instance does the freeing, no new schedule */
230 spin_unlock_bh(lgr_lock
);
231 cancel_delayed_work(&lgr
->free_work
);
233 if (!lgr
->is_smcd
&& lnk
->state
!= SMC_LNK_INACTIVE
)
234 smc_llc_link_inactive(lnk
);
235 if (lgr
->is_smcd
&& !lgr
->terminating
)
236 smc_ism_signal_shutdown(lgr
);
240 static void smc_lgr_terminate_work(struct work_struct
*work
)
242 struct smc_link_group
*lgr
= container_of(work
, struct smc_link_group
,
245 __smc_lgr_terminate(lgr
, true);
248 /* create a new SMC link group */
249 static int smc_lgr_create(struct smc_sock
*smc
, struct smc_init_info
*ini
)
251 struct smc_link_group
*lgr
;
252 struct list_head
*lgr_list
;
253 struct smc_link
*lnk
;
254 spinlock_t
*lgr_lock
;
259 if (ini
->is_smcd
&& ini
->vlan_id
) {
260 if (smc_ism_get_vlan(ini
->ism_dev
, ini
->vlan_id
)) {
261 rc
= SMC_CLC_DECL_ISMVLANERR
;
266 lgr
= kzalloc(sizeof(*lgr
), GFP_KERNEL
);
268 rc
= SMC_CLC_DECL_MEM
;
271 lgr
->is_smcd
= ini
->is_smcd
;
273 lgr
->terminating
= 0;
276 lgr
->vlan_id
= ini
->vlan_id
;
277 rwlock_init(&lgr
->sndbufs_lock
);
278 rwlock_init(&lgr
->rmbs_lock
);
279 rwlock_init(&lgr
->conns_lock
);
280 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
281 INIT_LIST_HEAD(&lgr
->sndbufs
[i
]);
282 INIT_LIST_HEAD(&lgr
->rmbs
[i
]);
284 smc_lgr_list
.num
+= SMC_LGR_NUM_INCR
;
285 memcpy(&lgr
->id
, (u8
*)&smc_lgr_list
.num
, SMC_LGR_ID_SIZE
);
286 INIT_DELAYED_WORK(&lgr
->free_work
, smc_lgr_free_work
);
287 INIT_WORK(&lgr
->terminate_work
, smc_lgr_terminate_work
);
288 lgr
->conns_all
= RB_ROOT
;
290 /* SMC-D specific settings */
291 get_device(&ini
->ism_dev
->dev
);
292 lgr
->peer_gid
= ini
->ism_gid
;
293 lgr
->smcd
= ini
->ism_dev
;
294 lgr_list
= &ini
->ism_dev
->lgr_list
;
295 lgr_lock
= &lgr
->smcd
->lgr_lock
;
296 lgr
->peer_shutdown
= 0;
297 atomic_inc(&ini
->ism_dev
->lgr_cnt
);
299 /* SMC-R specific settings */
300 get_device(&ini
->ib_dev
->ibdev
->dev
);
301 lgr
->role
= smc
->listen_smc
? SMC_SERV
: SMC_CLNT
;
302 memcpy(lgr
->peer_systemid
, ini
->ib_lcl
->id_for_peer
,
305 lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
306 /* initialize link */
307 lnk
->state
= SMC_LNK_ACTIVATING
;
308 lnk
->link_id
= SMC_SINGLE_LINK
;
309 lnk
->smcibdev
= ini
->ib_dev
;
310 lnk
->ibport
= ini
->ib_port
;
311 lgr_list
= &smc_lgr_list
.list
;
312 lgr_lock
= &smc_lgr_list
.lock
;
314 ini
->ib_dev
->pattr
[ini
->ib_port
- 1].active_mtu
;
315 if (!ini
->ib_dev
->initialized
)
316 smc_ib_setup_per_ibdev(ini
->ib_dev
);
317 get_random_bytes(rndvec
, sizeof(rndvec
));
318 lnk
->psn_initial
= rndvec
[0] + (rndvec
[1] << 8) +
320 rc
= smc_ib_determine_gid(lnk
->smcibdev
, lnk
->ibport
,
321 ini
->vlan_id
, lnk
->gid
,
325 rc
= smc_llc_link_init(lnk
);
328 rc
= smc_wr_alloc_link_mem(lnk
);
331 rc
= smc_ib_create_protection_domain(lnk
);
334 rc
= smc_ib_create_queue_pair(lnk
);
337 rc
= smc_wr_create_link(lnk
);
340 atomic_inc(&lgr_cnt
);
341 atomic_inc(&ini
->ib_dev
->lnk_cnt
);
344 spin_lock_bh(lgr_lock
);
345 list_add(&lgr
->list
, lgr_list
);
346 spin_unlock_bh(lgr_lock
);
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
);
360 if (ini
->is_smcd
&& ini
->vlan_id
)
361 smc_ism_put_vlan(ini
->ism_dev
, ini
->vlan_id
);
365 rc
= SMC_CLC_DECL_MEM
;
367 rc
= SMC_CLC_DECL_INTERR
;
372 static void smc_buf_unuse(struct smc_connection
*conn
,
373 struct smc_link_group
*lgr
)
375 if (conn
->sndbuf_desc
)
376 conn
->sndbuf_desc
->used
= 0;
377 if (conn
->rmb_desc
) {
378 if (!conn
->rmb_desc
->regerr
) {
379 if (!lgr
->is_smcd
&& !list_empty(&lgr
->list
)) {
380 /* unregister rmb with peer */
381 smc_llc_do_delete_rkey(
382 &lgr
->lnk
[SMC_SINGLE_LINK
],
385 conn
->rmb_desc
->used
= 0;
387 /* buf registration failed, reuse not possible */
388 write_lock_bh(&lgr
->rmbs_lock
);
389 list_del(&conn
->rmb_desc
->list
);
390 write_unlock_bh(&lgr
->rmbs_lock
);
392 smc_buf_free(lgr
, true, conn
->rmb_desc
);
397 /* remove a finished connection from its link group */
398 void smc_conn_free(struct smc_connection
*conn
)
400 struct smc_link_group
*lgr
= conn
->lgr
;
405 if (!list_empty(&lgr
->list
))
406 smc_ism_unset_conn(conn
);
407 tasklet_kill(&conn
->rx_tsklet
);
409 smc_cdc_tx_dismiss_slots(conn
);
411 if (!list_empty(&lgr
->list
)) {
412 smc_lgr_unregister_conn(conn
);
413 smc_buf_unuse(conn
, lgr
); /* allow buffer reuse */
417 smc_lgr_schedule_free_work(lgr
);
420 static void smc_link_clear(struct smc_link
*lnk
)
423 smc_llc_link_clear(lnk
);
424 smc_ib_modify_qp_reset(lnk
);
425 smc_wr_free_link(lnk
);
426 smc_ib_destroy_queue_pair(lnk
);
427 smc_ib_dealloc_protection_domain(lnk
);
428 smc_wr_free_link_mem(lnk
);
429 if (!atomic_dec_return(&lnk
->smcibdev
->lnk_cnt
))
430 wake_up(&lnk
->smcibdev
->lnks_deleted
);
433 static void smcr_buf_free(struct smc_link_group
*lgr
, bool is_rmb
,
434 struct smc_buf_desc
*buf_desc
)
436 struct smc_link
*lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
439 if (buf_desc
->mr_rx
[SMC_SINGLE_LINK
])
440 smc_ib_put_memory_region(
441 buf_desc
->mr_rx
[SMC_SINGLE_LINK
]);
442 smc_ib_buf_unmap_sg(lnk
->smcibdev
, buf_desc
,
445 smc_ib_buf_unmap_sg(lnk
->smcibdev
, buf_desc
,
448 sg_free_table(&buf_desc
->sgt
[SMC_SINGLE_LINK
]);
450 __free_pages(buf_desc
->pages
, buf_desc
->order
);
454 static void smcd_buf_free(struct smc_link_group
*lgr
, bool is_dmb
,
455 struct smc_buf_desc
*buf_desc
)
458 /* restore original buf len */
459 buf_desc
->len
+= sizeof(struct smcd_cdc_msg
);
460 smc_ism_unregister_dmb(lgr
->smcd
, buf_desc
);
462 kfree(buf_desc
->cpu_addr
);
467 static void smc_buf_free(struct smc_link_group
*lgr
, bool is_rmb
,
468 struct smc_buf_desc
*buf_desc
)
471 smcd_buf_free(lgr
, is_rmb
, buf_desc
);
473 smcr_buf_free(lgr
, is_rmb
, buf_desc
);
476 static void __smc_lgr_free_bufs(struct smc_link_group
*lgr
, bool is_rmb
)
478 struct smc_buf_desc
*buf_desc
, *bf_desc
;
479 struct list_head
*buf_list
;
482 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
484 buf_list
= &lgr
->rmbs
[i
];
486 buf_list
= &lgr
->sndbufs
[i
];
487 list_for_each_entry_safe(buf_desc
, bf_desc
, buf_list
,
489 list_del(&buf_desc
->list
);
490 smc_buf_free(lgr
, is_rmb
, buf_desc
);
495 static void smc_lgr_free_bufs(struct smc_link_group
*lgr
)
497 /* free send buffers */
498 __smc_lgr_free_bufs(lgr
, false);
500 __smc_lgr_free_bufs(lgr
, true);
503 /* remove a link group */
504 static void smc_lgr_free(struct smc_link_group
*lgr
)
506 smc_lgr_free_bufs(lgr
);
508 if (!lgr
->terminating
) {
509 smc_ism_put_vlan(lgr
->smcd
, lgr
->vlan_id
);
510 put_device(&lgr
->smcd
->dev
);
512 if (!atomic_dec_return(&lgr
->smcd
->lgr_cnt
))
513 wake_up(&lgr
->smcd
->lgrs_deleted
);
515 smc_link_clear(&lgr
->lnk
[SMC_SINGLE_LINK
]);
516 put_device(&lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
->ibdev
->dev
);
517 if (!atomic_dec_return(&lgr_cnt
))
518 wake_up(&lgrs_deleted
);
523 void smc_lgr_forget(struct smc_link_group
*lgr
)
525 struct list_head
*lgr_list
;
526 spinlock_t
*lgr_lock
;
528 lgr_list
= smc_lgr_list_head(lgr
, &lgr_lock
);
529 spin_lock_bh(lgr_lock
);
530 /* do not use this link group for new connections */
531 if (!list_empty(lgr_list
))
532 list_del_init(lgr_list
);
533 spin_unlock_bh(lgr_lock
);
536 static void smcd_unregister_all_dmbs(struct smc_link_group
*lgr
)
540 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
541 struct smc_buf_desc
*buf_desc
;
543 list_for_each_entry(buf_desc
, &lgr
->rmbs
[i
], list
) {
544 buf_desc
->len
+= sizeof(struct smcd_cdc_msg
);
545 smc_ism_unregister_dmb(lgr
->smcd
, buf_desc
);
550 static void smc_sk_wake_ups(struct smc_sock
*smc
)
552 smc
->sk
.sk_write_space(&smc
->sk
);
553 smc
->sk
.sk_data_ready(&smc
->sk
);
554 smc
->sk
.sk_state_change(&smc
->sk
);
557 /* kill a connection */
558 static void smc_conn_kill(struct smc_connection
*conn
, bool soft
)
560 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
562 if (conn
->lgr
->is_smcd
&& conn
->lgr
->peer_shutdown
)
563 conn
->local_tx_ctrl
.conn_state_flags
.peer_conn_abort
= 1;
565 smc_close_abort(conn
);
567 smc
->sk
.sk_err
= ECONNABORTED
;
568 smc_sk_wake_ups(smc
);
569 if (conn
->lgr
->is_smcd
) {
570 smc_ism_unset_conn(conn
);
572 tasklet_kill(&conn
->rx_tsklet
);
574 tasklet_unlock_wait(&conn
->rx_tsklet
);
576 smc_cdc_tx_dismiss_slots(conn
);
578 smc_lgr_unregister_conn(conn
);
579 smc_close_active_abort(smc
);
582 static void smc_lgr_cleanup(struct smc_link_group
*lgr
)
585 smc_ism_signal_shutdown(lgr
);
586 smcd_unregister_all_dmbs(lgr
);
587 smc_ism_put_vlan(lgr
->smcd
, lgr
->vlan_id
);
588 put_device(&lgr
->smcd
->dev
);
590 struct smc_link
*lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
592 if (lnk
->state
!= SMC_LNK_INACTIVE
)
593 smc_llc_link_inactive(lnk
);
597 /* terminate link group
598 * @soft: true if link group shutdown can take its time
599 * false if immediate link group shutdown is required
601 static void __smc_lgr_terminate(struct smc_link_group
*lgr
, bool soft
)
603 struct smc_connection
*conn
;
604 struct smc_sock
*smc
;
605 struct rb_node
*node
;
607 if (lgr
->terminating
)
608 return; /* lgr already terminating */
610 cancel_delayed_work_sync(&lgr
->free_work
);
611 lgr
->terminating
= 1;
613 smc_llc_link_inactive(&lgr
->lnk
[SMC_SINGLE_LINK
]);
615 /* kill remaining link group connections */
616 read_lock_bh(&lgr
->conns_lock
);
617 node
= rb_first(&lgr
->conns_all
);
619 read_unlock_bh(&lgr
->conns_lock
);
620 conn
= rb_entry(node
, struct smc_connection
, alert_node
);
621 smc
= container_of(conn
, struct smc_sock
, conn
);
622 sock_hold(&smc
->sk
); /* sock_put below */
624 smc_conn_kill(conn
, soft
);
625 release_sock(&smc
->sk
);
626 sock_put(&smc
->sk
); /* sock_hold above */
627 read_lock_bh(&lgr
->conns_lock
);
628 node
= rb_first(&lgr
->conns_all
);
630 read_unlock_bh(&lgr
->conns_lock
);
631 smc_lgr_cleanup(lgr
);
633 smc_lgr_schedule_free_work_fast(lgr
);
638 /* unlink link group and schedule termination */
639 void smc_lgr_terminate_sched(struct smc_link_group
*lgr
)
641 spinlock_t
*lgr_lock
;
643 smc_lgr_list_head(lgr
, &lgr_lock
);
644 spin_lock_bh(lgr_lock
);
645 if (list_empty(&lgr
->list
) || lgr
->terminating
|| lgr
->freeing
) {
646 spin_unlock_bh(lgr_lock
);
647 return; /* lgr already terminating */
649 list_del_init(&lgr
->list
);
650 spin_unlock_bh(lgr_lock
);
651 schedule_work(&lgr
->terminate_work
);
654 /* Called when IB port is terminated */
655 void smc_port_terminate(struct smc_ib_device
*smcibdev
, u8 ibport
)
657 struct smc_link_group
*lgr
, *l
;
658 LIST_HEAD(lgr_free_list
);
660 spin_lock_bh(&smc_lgr_list
.lock
);
661 list_for_each_entry_safe(lgr
, l
, &smc_lgr_list
.list
, list
) {
663 lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
== smcibdev
&&
664 lgr
->lnk
[SMC_SINGLE_LINK
].ibport
== ibport
) {
665 list_move(&lgr
->list
, &lgr_free_list
);
669 spin_unlock_bh(&smc_lgr_list
.lock
);
671 list_for_each_entry_safe(lgr
, l
, &lgr_free_list
, list
) {
672 list_del_init(&lgr
->list
);
673 __smc_lgr_terminate(lgr
, false);
677 /* Called when peer lgr shutdown (regularly or abnormally) is received */
678 void smc_smcd_terminate(struct smcd_dev
*dev
, u64 peer_gid
, unsigned short vlan
)
680 struct smc_link_group
*lgr
, *l
;
681 LIST_HEAD(lgr_free_list
);
683 /* run common cleanup function and build free list */
684 spin_lock_bh(&dev
->lgr_lock
);
685 list_for_each_entry_safe(lgr
, l
, &dev
->lgr_list
, list
) {
686 if ((!peer_gid
|| lgr
->peer_gid
== peer_gid
) &&
687 (vlan
== VLAN_VID_MASK
|| lgr
->vlan_id
== vlan
)) {
688 if (peer_gid
) /* peer triggered termination */
689 lgr
->peer_shutdown
= 1;
690 list_move(&lgr
->list
, &lgr_free_list
);
693 spin_unlock_bh(&dev
->lgr_lock
);
695 /* cancel the regular free workers and actually free lgrs */
696 list_for_each_entry_safe(lgr
, l
, &lgr_free_list
, list
) {
697 list_del_init(&lgr
->list
);
698 schedule_work(&lgr
->terminate_work
);
702 /* Called when an SMCD device is removed or the smc module is unloaded */
703 void smc_smcd_terminate_all(struct smcd_dev
*smcd
)
705 struct smc_link_group
*lgr
, *lg
;
706 LIST_HEAD(lgr_free_list
);
708 spin_lock_bh(&smcd
->lgr_lock
);
709 list_splice_init(&smcd
->lgr_list
, &lgr_free_list
);
710 list_for_each_entry(lgr
, &lgr_free_list
, list
)
712 spin_unlock_bh(&smcd
->lgr_lock
);
714 list_for_each_entry_safe(lgr
, lg
, &lgr_free_list
, list
) {
715 list_del_init(&lgr
->list
);
716 __smc_lgr_terminate(lgr
, false);
719 if (atomic_read(&smcd
->lgr_cnt
))
720 wait_event(smcd
->lgrs_deleted
, !atomic_read(&smcd
->lgr_cnt
));
723 /* Called when an SMCR device is removed or the smc module is unloaded.
724 * If smcibdev is given, all SMCR link groups using this device are terminated.
725 * If smcibdev is NULL, all SMCR link groups are terminated.
727 void smc_smcr_terminate_all(struct smc_ib_device
*smcibdev
)
729 struct smc_link_group
*lgr
, *lg
;
730 LIST_HEAD(lgr_free_list
);
732 spin_lock_bh(&smc_lgr_list
.lock
);
734 list_splice_init(&smc_lgr_list
.list
, &lgr_free_list
);
735 list_for_each_entry(lgr
, &lgr_free_list
, list
)
738 list_for_each_entry_safe(lgr
, lg
, &smc_lgr_list
.list
, list
) {
739 if (lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
== smcibdev
) {
740 list_move(&lgr
->list
, &lgr_free_list
);
745 spin_unlock_bh(&smc_lgr_list
.lock
);
747 list_for_each_entry_safe(lgr
, lg
, &lgr_free_list
, list
) {
748 list_del_init(&lgr
->list
);
749 __smc_lgr_terminate(lgr
, false);
753 if (atomic_read(&smcibdev
->lnk_cnt
))
754 wait_event(smcibdev
->lnks_deleted
,
755 !atomic_read(&smcibdev
->lnk_cnt
));
757 if (atomic_read(&lgr_cnt
))
758 wait_event(lgrs_deleted
, !atomic_read(&lgr_cnt
));
762 /* Determine vlan of internal TCP socket.
763 * @vlan_id: address to store the determined vlan id into
765 int smc_vlan_by_tcpsk(struct socket
*clcsock
, struct smc_init_info
*ini
)
767 struct dst_entry
*dst
= sk_dst_get(clcsock
->sk
);
768 struct net_device
*ndev
;
769 int i
, nest_lvl
, rc
= 0;
782 if (is_vlan_dev(ndev
)) {
783 ini
->vlan_id
= vlan_dev_vlan_id(ndev
);
788 nest_lvl
= ndev
->lower_level
;
789 for (i
= 0; i
< nest_lvl
; i
++) {
790 struct list_head
*lower
= &ndev
->adj_list
.lower
;
792 if (list_empty(lower
))
795 ndev
= (struct net_device
*)netdev_lower_get_next(ndev
, &lower
);
796 if (is_vlan_dev(ndev
)) {
797 ini
->vlan_id
= vlan_dev_vlan_id(ndev
);
809 static bool smcr_lgr_match(struct smc_link_group
*lgr
,
810 struct smc_clc_msg_local
*lcl
,
811 enum smc_lgr_role role
, u32 clcqpn
)
813 return !memcmp(lgr
->peer_systemid
, lcl
->id_for_peer
,
815 !memcmp(lgr
->lnk
[SMC_SINGLE_LINK
].peer_gid
, &lcl
->gid
,
817 !memcmp(lgr
->lnk
[SMC_SINGLE_LINK
].peer_mac
, lcl
->mac
,
820 (lgr
->role
== SMC_SERV
||
821 lgr
->lnk
[SMC_SINGLE_LINK
].peer_qpn
== clcqpn
);
824 static bool smcd_lgr_match(struct smc_link_group
*lgr
,
825 struct smcd_dev
*smcismdev
, u64 peer_gid
)
827 return lgr
->peer_gid
== peer_gid
&& lgr
->smcd
== smcismdev
;
830 /* create a new SMC connection (and a new link group if necessary) */
831 int smc_conn_create(struct smc_sock
*smc
, struct smc_init_info
*ini
)
833 struct smc_connection
*conn
= &smc
->conn
;
834 struct list_head
*lgr_list
;
835 struct smc_link_group
*lgr
;
836 enum smc_lgr_role role
;
837 spinlock_t
*lgr_lock
;
840 lgr_list
= ini
->is_smcd
? &ini
->ism_dev
->lgr_list
: &smc_lgr_list
.list
;
841 lgr_lock
= ini
->is_smcd
? &ini
->ism_dev
->lgr_lock
: &smc_lgr_list
.lock
;
842 ini
->cln_first_contact
= SMC_FIRST_CONTACT
;
843 role
= smc
->listen_smc
? SMC_SERV
: SMC_CLNT
;
844 if (role
== SMC_CLNT
&& ini
->srv_first_contact
)
845 /* create new link group as well */
848 /* determine if an existing link group can be reused */
849 spin_lock_bh(lgr_lock
);
850 list_for_each_entry(lgr
, lgr_list
, list
) {
851 write_lock_bh(&lgr
->conns_lock
);
853 smcd_lgr_match(lgr
, ini
->ism_dev
, ini
->ism_gid
) :
854 smcr_lgr_match(lgr
, ini
->ib_lcl
, role
, ini
->ib_clcqpn
)) &&
856 lgr
->vlan_id
== ini
->vlan_id
&&
858 lgr
->conns_num
< SMC_RMBS_PER_LGR_MAX
)) {
859 /* link group found */
860 ini
->cln_first_contact
= SMC_REUSE_CONTACT
;
862 smc_lgr_register_conn(conn
); /* add smc conn to lgr */
863 if (delayed_work_pending(&lgr
->free_work
))
864 cancel_delayed_work(&lgr
->free_work
);
865 write_unlock_bh(&lgr
->conns_lock
);
868 write_unlock_bh(&lgr
->conns_lock
);
870 spin_unlock_bh(lgr_lock
);
872 if (role
== SMC_CLNT
&& !ini
->srv_first_contact
&&
873 ini
->cln_first_contact
== SMC_FIRST_CONTACT
) {
874 /* Server reuses a link group, but Client wants to start
876 * send out_of_sync decline, reason synchr. error
878 return SMC_CLC_DECL_SYNCERR
;
882 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
) {
883 rc
= smc_lgr_create(smc
, ini
);
887 write_lock_bh(&lgr
->conns_lock
);
888 smc_lgr_register_conn(conn
); /* add smc conn to lgr */
889 write_unlock_bh(&lgr
->conns_lock
);
891 conn
->local_tx_ctrl
.common
.type
= SMC_CDC_MSG_TYPE
;
892 conn
->local_tx_ctrl
.len
= SMC_WR_TX_SIZE
;
893 conn
->urg_state
= SMC_URG_READ
;
895 conn
->rx_off
= sizeof(struct smcd_cdc_msg
);
896 smcd_cdc_rx_init(conn
); /* init tasklet for this conn */
898 #ifndef KERNEL_HAS_ATOMIC64
899 spin_lock_init(&conn
->acurs_lock
);
906 /* convert the RMB size into the compressed notation - minimum 16K.
907 * In contrast to plain ilog2, this rounds towards the next power of 2,
908 * so the socket application gets at least its desired sndbuf / rcvbuf size.
910 static u8
smc_compress_bufsize(int size
)
914 if (size
<= SMC_BUF_MIN_SIZE
)
917 size
= (size
- 1) >> 14;
918 compressed
= ilog2(size
) + 1;
919 if (compressed
>= SMC_RMBE_SIZES
)
920 compressed
= SMC_RMBE_SIZES
- 1;
924 /* convert the RMB size from compressed notation into integer */
925 int smc_uncompress_bufsize(u8 compressed
)
929 size
= 0x00000001 << (((int)compressed
) + 14);
933 /* try to reuse a sndbuf or rmb description slot for a certain
934 * buffer size; if not available, return NULL
936 static struct smc_buf_desc
*smc_buf_get_slot(int compressed_bufsize
,
938 struct list_head
*buf_list
)
940 struct smc_buf_desc
*buf_slot
;
943 list_for_each_entry(buf_slot
, buf_list
, list
) {
944 if (cmpxchg(&buf_slot
->used
, 0, 1) == 0) {
945 read_unlock_bh(lock
);
949 read_unlock_bh(lock
);
953 /* one of the conditions for announcing a receiver's current window size is
954 * that it "results in a minimum increase in the window size of 10% of the
955 * receive buffer space" [RFC7609]
957 static inline int smc_rmb_wnd_update_limit(int rmbe_size
)
959 return min_t(int, rmbe_size
/ 10, SOCK_MIN_SNDBUF
/ 2);
962 static struct smc_buf_desc
*smcr_new_buf_create(struct smc_link_group
*lgr
,
963 bool is_rmb
, int bufsize
)
965 struct smc_buf_desc
*buf_desc
;
966 struct smc_link
*lnk
;
969 /* try to alloc a new buffer */
970 buf_desc
= kzalloc(sizeof(*buf_desc
), GFP_KERNEL
);
972 return ERR_PTR(-ENOMEM
);
974 buf_desc
->order
= get_order(bufsize
);
975 buf_desc
->pages
= alloc_pages(GFP_KERNEL
| __GFP_NOWARN
|
976 __GFP_NOMEMALLOC
| __GFP_COMP
|
977 __GFP_NORETRY
| __GFP_ZERO
,
979 if (!buf_desc
->pages
) {
981 return ERR_PTR(-EAGAIN
);
983 buf_desc
->cpu_addr
= (void *)page_address(buf_desc
->pages
);
985 /* build the sg table from the pages */
986 lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
987 rc
= sg_alloc_table(&buf_desc
->sgt
[SMC_SINGLE_LINK
], 1,
990 smc_buf_free(lgr
, is_rmb
, buf_desc
);
993 sg_set_buf(buf_desc
->sgt
[SMC_SINGLE_LINK
].sgl
,
994 buf_desc
->cpu_addr
, bufsize
);
996 /* map sg table to DMA address */
997 rc
= smc_ib_buf_map_sg(lnk
->smcibdev
, buf_desc
,
998 is_rmb
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
999 /* SMC protocol depends on mapping to one DMA address only */
1001 smc_buf_free(lgr
, is_rmb
, buf_desc
);
1002 return ERR_PTR(-EAGAIN
);
1005 /* create a new memory region for the RMB */
1007 rc
= smc_ib_get_memory_region(lnk
->roce_pd
,
1008 IB_ACCESS_REMOTE_WRITE
|
1009 IB_ACCESS_LOCAL_WRITE
,
1012 smc_buf_free(lgr
, is_rmb
, buf_desc
);
1017 buf_desc
->len
= bufsize
;
1021 #define SMCD_DMBE_SIZES 7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1023 static struct smc_buf_desc
*smcd_new_buf_create(struct smc_link_group
*lgr
,
1024 bool is_dmb
, int bufsize
)
1026 struct smc_buf_desc
*buf_desc
;
1029 if (smc_compress_bufsize(bufsize
) > SMCD_DMBE_SIZES
)
1030 return ERR_PTR(-EAGAIN
);
1032 /* try to alloc a new DMB */
1033 buf_desc
= kzalloc(sizeof(*buf_desc
), GFP_KERNEL
);
1035 return ERR_PTR(-ENOMEM
);
1037 rc
= smc_ism_register_dmb(lgr
, bufsize
, buf_desc
);
1040 return ERR_PTR(-EAGAIN
);
1042 buf_desc
->pages
= virt_to_page(buf_desc
->cpu_addr
);
1043 /* CDC header stored in buf. So, pretend it was smaller */
1044 buf_desc
->len
= bufsize
- sizeof(struct smcd_cdc_msg
);
1046 buf_desc
->cpu_addr
= kzalloc(bufsize
, GFP_KERNEL
|
1047 __GFP_NOWARN
| __GFP_NORETRY
|
1049 if (!buf_desc
->cpu_addr
) {
1051 return ERR_PTR(-EAGAIN
);
1053 buf_desc
->len
= bufsize
;
1058 static int __smc_buf_create(struct smc_sock
*smc
, bool is_smcd
, bool is_rmb
)
1060 struct smc_buf_desc
*buf_desc
= ERR_PTR(-ENOMEM
);
1061 struct smc_connection
*conn
= &smc
->conn
;
1062 struct smc_link_group
*lgr
= conn
->lgr
;
1063 struct list_head
*buf_list
;
1064 int bufsize
, bufsize_short
;
1069 /* use socket recv buffer size (w/o overhead) as start value */
1070 sk_buf_size
= smc
->sk
.sk_rcvbuf
/ 2;
1072 /* use socket send buffer size (w/o overhead) as start value */
1073 sk_buf_size
= smc
->sk
.sk_sndbuf
/ 2;
1075 for (bufsize_short
= smc_compress_bufsize(sk_buf_size
);
1076 bufsize_short
>= 0; bufsize_short
--) {
1079 lock
= &lgr
->rmbs_lock
;
1080 buf_list
= &lgr
->rmbs
[bufsize_short
];
1082 lock
= &lgr
->sndbufs_lock
;
1083 buf_list
= &lgr
->sndbufs
[bufsize_short
];
1085 bufsize
= smc_uncompress_bufsize(bufsize_short
);
1086 if ((1 << get_order(bufsize
)) > SG_MAX_SINGLE_ALLOC
)
1089 /* check for reusable slot in the link group */
1090 buf_desc
= smc_buf_get_slot(bufsize_short
, lock
, buf_list
);
1092 memset(buf_desc
->cpu_addr
, 0, bufsize
);
1093 break; /* found reusable slot */
1097 buf_desc
= smcd_new_buf_create(lgr
, is_rmb
, bufsize
);
1099 buf_desc
= smcr_new_buf_create(lgr
, is_rmb
, bufsize
);
1101 if (PTR_ERR(buf_desc
) == -ENOMEM
)
1103 if (IS_ERR(buf_desc
))
1107 write_lock_bh(lock
);
1108 list_add(&buf_desc
->list
, buf_list
);
1109 write_unlock_bh(lock
);
1113 if (IS_ERR(buf_desc
))
1117 conn
->rmb_desc
= buf_desc
;
1118 conn
->rmbe_size_short
= bufsize_short
;
1119 smc
->sk
.sk_rcvbuf
= bufsize
* 2;
1120 atomic_set(&conn
->bytes_to_rcv
, 0);
1121 conn
->rmbe_update_limit
=
1122 smc_rmb_wnd_update_limit(buf_desc
->len
);
1124 smc_ism_set_conn(conn
); /* map RMB/smcd_dev to conn */
1126 conn
->sndbuf_desc
= buf_desc
;
1127 smc
->sk
.sk_sndbuf
= bufsize
* 2;
1128 atomic_set(&conn
->sndbuf_space
, bufsize
);
1133 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection
*conn
)
1135 struct smc_link_group
*lgr
= conn
->lgr
;
1137 if (!conn
->lgr
|| conn
->lgr
->is_smcd
)
1139 smc_ib_sync_sg_for_cpu(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
1140 conn
->sndbuf_desc
, DMA_TO_DEVICE
);
1143 void smc_sndbuf_sync_sg_for_device(struct smc_connection
*conn
)
1145 struct smc_link_group
*lgr
= conn
->lgr
;
1147 if (!conn
->lgr
|| conn
->lgr
->is_smcd
)
1149 smc_ib_sync_sg_for_device(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
1150 conn
->sndbuf_desc
, DMA_TO_DEVICE
);
1153 void smc_rmb_sync_sg_for_cpu(struct smc_connection
*conn
)
1155 struct smc_link_group
*lgr
= conn
->lgr
;
1157 if (!conn
->lgr
|| conn
->lgr
->is_smcd
)
1159 smc_ib_sync_sg_for_cpu(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
1160 conn
->rmb_desc
, DMA_FROM_DEVICE
);
1163 void smc_rmb_sync_sg_for_device(struct smc_connection
*conn
)
1165 struct smc_link_group
*lgr
= conn
->lgr
;
1167 if (!conn
->lgr
|| conn
->lgr
->is_smcd
)
1169 smc_ib_sync_sg_for_device(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
1170 conn
->rmb_desc
, DMA_FROM_DEVICE
);
1173 /* create the send and receive buffer for an SMC socket;
1174 * receive buffers are called RMBs;
1175 * (even though the SMC protocol allows more than one RMB-element per RMB,
1176 * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
1177 * extra RMB for every connection in a link group
1179 int smc_buf_create(struct smc_sock
*smc
, bool is_smcd
)
1183 /* create send buffer */
1184 rc
= __smc_buf_create(smc
, is_smcd
, false);
1188 rc
= __smc_buf_create(smc
, is_smcd
, true);
1190 smc_buf_free(smc
->conn
.lgr
, false, smc
->conn
.sndbuf_desc
);
1194 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group
*lgr
)
1198 for_each_clear_bit(i
, lgr
->rtokens_used_mask
, SMC_RMBS_PER_LGR_MAX
) {
1199 if (!test_and_set_bit(i
, lgr
->rtokens_used_mask
))
1205 /* add a new rtoken from peer */
1206 int smc_rtoken_add(struct smc_link_group
*lgr
, __be64 nw_vaddr
, __be32 nw_rkey
)
1208 u64 dma_addr
= be64_to_cpu(nw_vaddr
);
1209 u32 rkey
= ntohl(nw_rkey
);
1212 for (i
= 0; i
< SMC_RMBS_PER_LGR_MAX
; i
++) {
1213 if ((lgr
->rtokens
[i
][SMC_SINGLE_LINK
].rkey
== rkey
) &&
1214 (lgr
->rtokens
[i
][SMC_SINGLE_LINK
].dma_addr
== dma_addr
) &&
1215 test_bit(i
, lgr
->rtokens_used_mask
)) {
1216 /* already in list */
1220 i
= smc_rmb_reserve_rtoken_idx(lgr
);
1223 lgr
->rtokens
[i
][SMC_SINGLE_LINK
].rkey
= rkey
;
1224 lgr
->rtokens
[i
][SMC_SINGLE_LINK
].dma_addr
= dma_addr
;
1228 /* delete an rtoken */
1229 int smc_rtoken_delete(struct smc_link_group
*lgr
, __be32 nw_rkey
)
1231 u32 rkey
= ntohl(nw_rkey
);
1234 for (i
= 0; i
< SMC_RMBS_PER_LGR_MAX
; i
++) {
1235 if (lgr
->rtokens
[i
][SMC_SINGLE_LINK
].rkey
== rkey
&&
1236 test_bit(i
, lgr
->rtokens_used_mask
)) {
1237 lgr
->rtokens
[i
][SMC_SINGLE_LINK
].rkey
= 0;
1238 lgr
->rtokens
[i
][SMC_SINGLE_LINK
].dma_addr
= 0;
1240 clear_bit(i
, lgr
->rtokens_used_mask
);
1247 /* save rkey and dma_addr received from peer during clc handshake */
1248 int smc_rmb_rtoken_handling(struct smc_connection
*conn
,
1249 struct smc_clc_msg_accept_confirm
*clc
)
1251 conn
->rtoken_idx
= smc_rtoken_add(conn
->lgr
, clc
->rmb_dma_addr
,
1253 if (conn
->rtoken_idx
< 0)
1254 return conn
->rtoken_idx
;
1258 static void smc_core_going_away(void)
1260 struct smc_ib_device
*smcibdev
;
1261 struct smcd_dev
*smcd
;
1263 spin_lock(&smc_ib_devices
.lock
);
1264 list_for_each_entry(smcibdev
, &smc_ib_devices
.list
, list
) {
1267 for (i
= 0; i
< SMC_MAX_PORTS
; i
++)
1268 set_bit(i
, smcibdev
->ports_going_away
);
1270 spin_unlock(&smc_ib_devices
.lock
);
1272 spin_lock(&smcd_dev_list
.lock
);
1273 list_for_each_entry(smcd
, &smcd_dev_list
.list
, list
) {
1274 smcd
->going_away
= 1;
1276 spin_unlock(&smcd_dev_list
.lock
);
1279 /* Clean up all SMC link groups */
1280 static void smc_lgrs_shutdown(void)
1282 struct smcd_dev
*smcd
;
1284 smc_core_going_away();
1286 smc_smcr_terminate_all(NULL
);
1288 spin_lock(&smcd_dev_list
.lock
);
1289 list_for_each_entry(smcd
, &smcd_dev_list
.list
, list
)
1290 smc_smcd_terminate_all(smcd
);
1291 spin_unlock(&smcd_dev_list
.lock
);
1294 static int smc_core_reboot_event(struct notifier_block
*this,
1295 unsigned long event
, void *ptr
)
1297 smc_lgrs_shutdown();
1298 smc_ib_unregister_client();
1302 static struct notifier_block smc_reboot_notifier
= {
1303 .notifier_call
= smc_core_reboot_event
,
1306 int __init
smc_core_init(void)
1308 return register_reboot_notifier(&smc_reboot_notifier
);
1311 /* Called (from smc_exit) when module is removed */
1312 void smc_core_exit(void)
1314 unregister_reboot_notifier(&smc_reboot_notifier
);
1315 smc_lgrs_shutdown();