4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/o2iblnd/o2iblnd.c
38 * Author: Eric Barton <eric@bartonsoftware.com>
41 #include <asm/div64.h>
45 static lnd_t the_o2iblnd
= {
47 .lnd_startup
= kiblnd_startup
,
48 .lnd_shutdown
= kiblnd_shutdown
,
49 .lnd_ctl
= kiblnd_ctl
,
50 .lnd_query
= kiblnd_query
,
51 .lnd_send
= kiblnd_send
,
52 .lnd_recv
= kiblnd_recv
,
55 kib_data_t kiblnd_data
;
57 static __u32
kiblnd_cksum(void *ptr
, int nob
)
63 sum
= ((sum
<< 1) | (sum
>> 31)) + *c
++;
65 /* ensure I don't return 0 (== no checksum) */
66 return (sum
== 0) ? 1 : sum
;
69 static char *kiblnd_msgtype2str(int type
)
72 case IBLND_MSG_CONNREQ
:
75 case IBLND_MSG_CONNACK
:
81 case IBLND_MSG_IMMEDIATE
:
84 case IBLND_MSG_PUT_REQ
:
87 case IBLND_MSG_PUT_NAK
:
90 case IBLND_MSG_PUT_ACK
:
93 case IBLND_MSG_PUT_DONE
:
96 case IBLND_MSG_GET_REQ
:
99 case IBLND_MSG_GET_DONE
:
107 static int kiblnd_msgtype2size(int type
)
109 const int hdr_size
= offsetof(kib_msg_t
, ibm_u
);
112 case IBLND_MSG_CONNREQ
:
113 case IBLND_MSG_CONNACK
:
114 return hdr_size
+ sizeof(kib_connparams_t
);
119 case IBLND_MSG_IMMEDIATE
:
120 return offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
[0]);
122 case IBLND_MSG_PUT_REQ
:
123 return hdr_size
+ sizeof(kib_putreq_msg_t
);
125 case IBLND_MSG_PUT_ACK
:
126 return hdr_size
+ sizeof(kib_putack_msg_t
);
128 case IBLND_MSG_GET_REQ
:
129 return hdr_size
+ sizeof(kib_get_msg_t
);
131 case IBLND_MSG_PUT_NAK
:
132 case IBLND_MSG_PUT_DONE
:
133 case IBLND_MSG_GET_DONE
:
134 return hdr_size
+ sizeof(kib_completion_msg_t
);
140 static int kiblnd_unpack_rd(kib_msg_t
*msg
, int flip
)
147 LASSERT(msg
->ibm_type
== IBLND_MSG_GET_REQ
||
148 msg
->ibm_type
== IBLND_MSG_PUT_ACK
);
150 rd
= msg
->ibm_type
== IBLND_MSG_GET_REQ
?
151 &msg
->ibm_u
.get
.ibgm_rd
:
152 &msg
->ibm_u
.putack
.ibpam_rd
;
155 __swab32s(&rd
->rd_key
);
156 __swab32s(&rd
->rd_nfrags
);
161 if (n
<= 0 || n
> IBLND_MAX_RDMA_FRAGS
) {
162 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
163 n
, IBLND_MAX_RDMA_FRAGS
);
167 nob
= offsetof(kib_msg_t
, ibm_u
) +
168 kiblnd_rd_msg_size(rd
, msg
->ibm_type
, n
);
170 if (msg
->ibm_nob
< nob
) {
171 CERROR("Short %s: %d(%d)\n",
172 kiblnd_msgtype2str(msg
->ibm_type
), msg
->ibm_nob
, nob
);
179 for (i
= 0; i
< n
; i
++) {
180 __swab32s(&rd
->rd_frags
[i
].rf_nob
);
181 __swab64s(&rd
->rd_frags
[i
].rf_addr
);
187 void kiblnd_pack_msg(lnet_ni_t
*ni
, kib_msg_t
*msg
, int version
,
188 int credits
, lnet_nid_t dstnid
, __u64 dststamp
)
190 kib_net_t
*net
= ni
->ni_data
;
193 * CAVEAT EMPTOR! all message fields not set here should have been
194 * initialised previously.
196 msg
->ibm_magic
= IBLND_MSG_MAGIC
;
197 msg
->ibm_version
= version
;
199 msg
->ibm_credits
= credits
;
202 msg
->ibm_srcnid
= ni
->ni_nid
;
203 msg
->ibm_srcstamp
= net
->ibn_incarnation
;
204 msg
->ibm_dstnid
= dstnid
;
205 msg
->ibm_dststamp
= dststamp
;
207 if (*kiblnd_tunables
.kib_cksum
) {
208 /* NB ibm_cksum zero while computing cksum */
209 msg
->ibm_cksum
= kiblnd_cksum(msg
, msg
->ibm_nob
);
213 int kiblnd_unpack_msg(kib_msg_t
*msg
, int nob
)
215 const int hdr_size
= offsetof(kib_msg_t
, ibm_u
);
221 /* 6 bytes are enough to have received magic + version */
223 CERROR("Short message: %d\n", nob
);
227 if (msg
->ibm_magic
== IBLND_MSG_MAGIC
) {
229 } else if (msg
->ibm_magic
== __swab32(IBLND_MSG_MAGIC
)) {
232 CERROR("Bad magic: %08x\n", msg
->ibm_magic
);
236 version
= flip
? __swab16(msg
->ibm_version
) : msg
->ibm_version
;
237 if (version
!= IBLND_MSG_VERSION
&&
238 version
!= IBLND_MSG_VERSION_1
) {
239 CERROR("Bad version: %x\n", version
);
243 if (nob
< hdr_size
) {
244 CERROR("Short message: %d\n", nob
);
248 msg_nob
= flip
? __swab32(msg
->ibm_nob
) : msg
->ibm_nob
;
250 CERROR("Short message: got %d, wanted %d\n", nob
, msg_nob
);
255 * checksum must be computed with ibm_cksum zero and BEFORE anything
258 msg_cksum
= flip
? __swab32(msg
->ibm_cksum
) : msg
->ibm_cksum
;
260 if (msg_cksum
!= 0 &&
261 msg_cksum
!= kiblnd_cksum(msg
, msg_nob
)) {
262 CERROR("Bad checksum\n");
266 msg
->ibm_cksum
= msg_cksum
;
269 /* leave magic unflipped as a clue to peer endianness */
270 msg
->ibm_version
= version
;
271 CLASSERT(sizeof(msg
->ibm_type
) == 1);
272 CLASSERT(sizeof(msg
->ibm_credits
) == 1);
273 msg
->ibm_nob
= msg_nob
;
274 __swab64s(&msg
->ibm_srcnid
);
275 __swab64s(&msg
->ibm_srcstamp
);
276 __swab64s(&msg
->ibm_dstnid
);
277 __swab64s(&msg
->ibm_dststamp
);
280 if (msg
->ibm_srcnid
== LNET_NID_ANY
) {
281 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg
->ibm_srcnid
));
285 if (msg_nob
< kiblnd_msgtype2size(msg
->ibm_type
)) {
286 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg
->ibm_type
),
287 msg_nob
, kiblnd_msgtype2size(msg
->ibm_type
));
291 switch (msg
->ibm_type
) {
293 CERROR("Unknown message type %x\n", msg
->ibm_type
);
297 case IBLND_MSG_IMMEDIATE
:
298 case IBLND_MSG_PUT_REQ
:
301 case IBLND_MSG_PUT_ACK
:
302 case IBLND_MSG_GET_REQ
:
303 if (kiblnd_unpack_rd(msg
, flip
))
307 case IBLND_MSG_PUT_NAK
:
308 case IBLND_MSG_PUT_DONE
:
309 case IBLND_MSG_GET_DONE
:
311 __swab32s(&msg
->ibm_u
.completion
.ibcm_status
);
314 case IBLND_MSG_CONNREQ
:
315 case IBLND_MSG_CONNACK
:
317 __swab16s(&msg
->ibm_u
.connparams
.ibcp_queue_depth
);
318 __swab16s(&msg
->ibm_u
.connparams
.ibcp_max_frags
);
319 __swab32s(&msg
->ibm_u
.connparams
.ibcp_max_msg_size
);
326 int kiblnd_create_peer(lnet_ni_t
*ni
, kib_peer_t
**peerp
, lnet_nid_t nid
)
329 kib_net_t
*net
= ni
->ni_data
;
330 int cpt
= lnet_cpt_of_nid(nid
);
333 LASSERT(net
!= NULL
);
334 LASSERT(nid
!= LNET_NID_ANY
);
336 LIBCFS_CPT_ALLOC(peer
, lnet_cpt_table(), cpt
, sizeof(*peer
));
338 CERROR("Cannot allocate peer\n");
342 memset(peer
, 0, sizeof(*peer
)); /* zero flags etc */
347 peer
->ibp_last_alive
= 0;
348 atomic_set(&peer
->ibp_refcount
, 1); /* 1 ref for caller */
350 INIT_LIST_HEAD(&peer
->ibp_list
); /* not in the peer table yet */
351 INIT_LIST_HEAD(&peer
->ibp_conns
);
352 INIT_LIST_HEAD(&peer
->ibp_tx_queue
);
354 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
356 /* always called with a ref on ni, which prevents ni being shutdown */
357 LASSERT(net
->ibn_shutdown
== 0);
359 /* npeers only grows with the global lock held */
360 atomic_inc(&net
->ibn_npeers
);
362 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
368 void kiblnd_destroy_peer(kib_peer_t
*peer
)
370 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
372 LASSERT(net
!= NULL
);
373 LASSERT(atomic_read(&peer
->ibp_refcount
) == 0);
374 LASSERT(!kiblnd_peer_active(peer
));
375 LASSERT(peer
->ibp_connecting
== 0);
376 LASSERT(peer
->ibp_accepting
== 0);
377 LASSERT(list_empty(&peer
->ibp_conns
));
378 LASSERT(list_empty(&peer
->ibp_tx_queue
));
380 LIBCFS_FREE(peer
, sizeof(*peer
));
383 * NB a peer's connections keep a reference on their peer until
384 * they are destroyed, so we can be assured that _all_ state to do
385 * with this peer has been cleaned up when its refcount drops to
388 atomic_dec(&net
->ibn_npeers
);
391 kib_peer_t
*kiblnd_find_peer_locked(lnet_nid_t nid
)
394 * the caller is responsible for accounting the additional reference
397 struct list_head
*peer_list
= kiblnd_nid2peerlist(nid
);
398 struct list_head
*tmp
;
401 list_for_each(tmp
, peer_list
) {
402 peer
= list_entry(tmp
, kib_peer_t
, ibp_list
);
404 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
405 peer
->ibp_accepting
> 0 ||
406 !list_empty(&peer
->ibp_conns
)); /* active conn */
408 if (peer
->ibp_nid
!= nid
)
411 CDEBUG(D_NET
, "got peer [%p] -> %s (%d) version: %x\n",
412 peer
, libcfs_nid2str(nid
),
413 atomic_read(&peer
->ibp_refcount
),
420 void kiblnd_unlink_peer_locked(kib_peer_t
*peer
)
422 LASSERT(list_empty(&peer
->ibp_conns
));
424 LASSERT(kiblnd_peer_active(peer
));
425 list_del_init(&peer
->ibp_list
);
426 /* lose peerlist's ref */
427 kiblnd_peer_decref(peer
);
430 static int kiblnd_get_peer_info(lnet_ni_t
*ni
, int index
,
431 lnet_nid_t
*nidp
, int *count
)
434 struct list_head
*ptmp
;
438 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
440 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
441 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
442 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
443 LASSERT(peer
->ibp_connecting
> 0 ||
444 peer
->ibp_accepting
> 0 ||
445 !list_empty(&peer
->ibp_conns
));
447 if (peer
->ibp_ni
!= ni
)
453 *nidp
= peer
->ibp_nid
;
454 *count
= atomic_read(&peer
->ibp_refcount
);
456 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
,
462 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
466 static void kiblnd_del_peer_locked(kib_peer_t
*peer
)
468 struct list_head
*ctmp
;
469 struct list_head
*cnxt
;
472 if (list_empty(&peer
->ibp_conns
)) {
473 kiblnd_unlink_peer_locked(peer
);
475 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
476 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
478 kiblnd_close_conn_locked(conn
, 0);
480 /* NB closing peer's last conn unlinked it. */
483 * NB peer now unlinked; might even be freed if the peer table had the
488 static int kiblnd_del_peer(lnet_ni_t
*ni
, lnet_nid_t nid
)
491 struct list_head
*ptmp
;
492 struct list_head
*pnxt
;
500 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
502 if (nid
!= LNET_NID_ANY
) {
503 lo
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
504 hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
507 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
510 for (i
= lo
; i
<= hi
; i
++) {
511 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
512 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
513 LASSERT(peer
->ibp_connecting
> 0 ||
514 peer
->ibp_accepting
> 0 ||
515 !list_empty(&peer
->ibp_conns
));
517 if (peer
->ibp_ni
!= ni
)
520 if (!(nid
== LNET_NID_ANY
|| peer
->ibp_nid
== nid
))
523 if (!list_empty(&peer
->ibp_tx_queue
)) {
524 LASSERT(list_empty(&peer
->ibp_conns
));
526 list_splice_init(&peer
->ibp_tx_queue
,
530 kiblnd_del_peer_locked(peer
);
531 rc
= 0; /* matched something */
535 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
537 kiblnd_txlist_done(ni
, &zombies
, -EIO
);
542 static kib_conn_t
*kiblnd_get_conn_by_idx(lnet_ni_t
*ni
, int index
)
545 struct list_head
*ptmp
;
547 struct list_head
*ctmp
;
551 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
553 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
554 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
555 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
556 LASSERT(peer
->ibp_connecting
> 0 ||
557 peer
->ibp_accepting
> 0 ||
558 !list_empty(&peer
->ibp_conns
));
560 if (peer
->ibp_ni
!= ni
)
563 list_for_each(ctmp
, &peer
->ibp_conns
) {
567 conn
= list_entry(ctmp
, kib_conn_t
,
569 kiblnd_conn_addref(conn
);
570 read_unlock_irqrestore(
571 &kiblnd_data
.kib_global_lock
,
578 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
582 int kiblnd_translate_mtu(int value
)
602 static void kiblnd_setup_mtu_locked(struct rdma_cm_id
*cmid
)
606 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
607 if (cmid
->route
.path_rec
== NULL
)
610 mtu
= kiblnd_translate_mtu(*kiblnd_tunables
.kib_ib_mtu
);
613 cmid
->route
.path_rec
->mtu
= mtu
;
616 static int kiblnd_get_completion_vector(kib_conn_t
*conn
, int cpt
)
622 lnet_nid_t nid
= conn
->ibc_peer
->ibp_nid
;
624 vectors
= conn
->ibc_cmid
->device
->num_comp_vectors
;
628 mask
= cfs_cpt_cpumask(lnet_cpt_table(), cpt
);
632 /* hash NID to CPU id in this partition... */
633 off
= do_div(nid
, cpumask_weight(mask
));
634 for_each_cpu(i
, mask
) {
643 kib_conn_t
*kiblnd_create_conn(kib_peer_t
*peer
, struct rdma_cm_id
*cmid
,
644 int state
, int version
)
648 * If the new conn is created successfully it takes over the caller's
649 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
650 * is destroyed. On failure, the caller's ref on 'peer' remains and
651 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
652 * to destroy 'cmid' here since I'm called from the CM which still has
653 * its ref on 'cmid').
655 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
656 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
658 struct ib_qp_init_attr
*init_qp_attr
;
659 struct kib_sched_info
*sched
;
660 struct ib_cq_init_attr cq_attr
= {};
668 LASSERT(net
!= NULL
);
669 LASSERT(!in_interrupt());
673 cpt
= lnet_cpt_of_nid(peer
->ibp_nid
);
674 sched
= kiblnd_data
.kib_scheds
[cpt
];
676 LASSERT(sched
->ibs_nthreads
> 0);
678 LIBCFS_CPT_ALLOC(init_qp_attr
, lnet_cpt_table(), cpt
,
679 sizeof(*init_qp_attr
));
680 if (init_qp_attr
== NULL
) {
681 CERROR("Can't allocate qp_attr for %s\n",
682 libcfs_nid2str(peer
->ibp_nid
));
686 LIBCFS_CPT_ALLOC(conn
, lnet_cpt_table(), cpt
, sizeof(*conn
));
688 CERROR("Can't allocate connection for %s\n",
689 libcfs_nid2str(peer
->ibp_nid
));
693 conn
->ibc_state
= IBLND_CONN_INIT
;
694 conn
->ibc_version
= version
;
695 conn
->ibc_peer
= peer
; /* I take the caller's ref */
696 cmid
->context
= conn
; /* for future CM callbacks */
697 conn
->ibc_cmid
= cmid
;
699 INIT_LIST_HEAD(&conn
->ibc_early_rxs
);
700 INIT_LIST_HEAD(&conn
->ibc_tx_noops
);
701 INIT_LIST_HEAD(&conn
->ibc_tx_queue
);
702 INIT_LIST_HEAD(&conn
->ibc_tx_queue_rsrvd
);
703 INIT_LIST_HEAD(&conn
->ibc_tx_queue_nocred
);
704 INIT_LIST_HEAD(&conn
->ibc_active_txs
);
705 spin_lock_init(&conn
->ibc_lock
);
707 LIBCFS_CPT_ALLOC(conn
->ibc_connvars
, lnet_cpt_table(), cpt
,
708 sizeof(*conn
->ibc_connvars
));
709 if (conn
->ibc_connvars
== NULL
) {
710 CERROR("Can't allocate in-progress connection state\n");
714 write_lock_irqsave(glock
, flags
);
715 if (dev
->ibd_failover
) {
716 write_unlock_irqrestore(glock
, flags
);
717 CERROR("%s: failover in progress\n", dev
->ibd_ifname
);
721 if (dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
) {
722 /* wakeup failover thread and teardown connection */
723 if (kiblnd_dev_can_failover(dev
)) {
724 list_add_tail(&dev
->ibd_fail_list
,
725 &kiblnd_data
.kib_failed_devs
);
726 wake_up(&kiblnd_data
.kib_failover_waitq
);
729 write_unlock_irqrestore(glock
, flags
);
730 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
731 cmid
->device
->name
, dev
->ibd_ifname
);
735 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
736 conn
->ibc_hdev
= dev
->ibd_hdev
;
738 kiblnd_setup_mtu_locked(cmid
);
740 write_unlock_irqrestore(glock
, flags
);
742 LIBCFS_CPT_ALLOC(conn
->ibc_rxs
, lnet_cpt_table(), cpt
,
743 IBLND_RX_MSGS(version
) * sizeof(kib_rx_t
));
744 if (conn
->ibc_rxs
== NULL
) {
745 CERROR("Cannot allocate RX buffers\n");
749 rc
= kiblnd_alloc_pages(&conn
->ibc_rx_pages
, cpt
,
750 IBLND_RX_MSG_PAGES(version
));
754 kiblnd_map_rx_descs(conn
);
756 cq_attr
.cqe
= IBLND_CQ_ENTRIES(version
);
757 cq_attr
.comp_vector
= kiblnd_get_completion_vector(conn
, cpt
);
758 cq
= ib_create_cq(cmid
->device
,
759 kiblnd_cq_completion
, kiblnd_cq_event
, conn
,
762 CERROR("Can't create CQ: %ld, cqe: %d\n",
763 PTR_ERR(cq
), IBLND_CQ_ENTRIES(version
));
769 rc
= ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
771 CERROR("Can't request completion notificiation: %d\n", rc
);
775 init_qp_attr
->event_handler
= kiblnd_qp_event
;
776 init_qp_attr
->qp_context
= conn
;
777 init_qp_attr
->cap
.max_send_wr
= IBLND_SEND_WRS(version
);
778 init_qp_attr
->cap
.max_recv_wr
= IBLND_RECV_WRS(version
);
779 init_qp_attr
->cap
.max_send_sge
= 1;
780 init_qp_attr
->cap
.max_recv_sge
= 1;
781 init_qp_attr
->sq_sig_type
= IB_SIGNAL_REQ_WR
;
782 init_qp_attr
->qp_type
= IB_QPT_RC
;
783 init_qp_attr
->send_cq
= cq
;
784 init_qp_attr
->recv_cq
= cq
;
786 conn
->ibc_sched
= sched
;
788 rc
= rdma_create_qp(cmid
, conn
->ibc_hdev
->ibh_pd
, init_qp_attr
);
790 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
791 rc
, init_qp_attr
->cap
.max_send_wr
,
792 init_qp_attr
->cap
.max_recv_wr
);
796 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
798 /* 1 ref for caller and each rxmsg */
799 atomic_set(&conn
->ibc_refcount
, 1 + IBLND_RX_MSGS(version
));
800 conn
->ibc_nrx
= IBLND_RX_MSGS(version
);
803 for (i
= 0; i
< IBLND_RX_MSGS(version
); i
++) {
804 rc
= kiblnd_post_rx(&conn
->ibc_rxs
[i
],
805 IBLND_POSTRX_NO_CREDIT
);
807 CERROR("Can't post rxmsg: %d\n", rc
);
809 /* Make posted receives complete */
810 kiblnd_abort_receives(conn
);
813 * correct # of posted buffers
814 * NB locking needed now I'm racing with completion
816 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
817 conn
->ibc_nrx
-= IBLND_RX_MSGS(version
) - i
;
818 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
821 * cmid will be destroyed by CM(ofed) after cm_callback
822 * returned, so we can't refer it anymore
823 * (by kiblnd_connd()->kiblnd_destroy_conn)
825 rdma_destroy_qp(conn
->ibc_cmid
);
826 conn
->ibc_cmid
= NULL
;
828 /* Drop my own and unused rxbuffer refcounts */
829 while (i
++ <= IBLND_RX_MSGS(version
))
830 kiblnd_conn_decref(conn
);
836 /* Init successful! */
837 LASSERT(state
== IBLND_CONN_ACTIVE_CONNECT
||
838 state
== IBLND_CONN_PASSIVE_WAIT
);
839 conn
->ibc_state
= state
;
842 atomic_inc(&net
->ibn_nconns
);
846 kiblnd_destroy_conn(conn
);
848 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
853 void kiblnd_destroy_conn(kib_conn_t
*conn
)
855 struct rdma_cm_id
*cmid
= conn
->ibc_cmid
;
856 kib_peer_t
*peer
= conn
->ibc_peer
;
859 LASSERT(!in_interrupt());
860 LASSERT(atomic_read(&conn
->ibc_refcount
) == 0);
861 LASSERT(list_empty(&conn
->ibc_early_rxs
));
862 LASSERT(list_empty(&conn
->ibc_tx_noops
));
863 LASSERT(list_empty(&conn
->ibc_tx_queue
));
864 LASSERT(list_empty(&conn
->ibc_tx_queue_rsrvd
));
865 LASSERT(list_empty(&conn
->ibc_tx_queue_nocred
));
866 LASSERT(list_empty(&conn
->ibc_active_txs
));
867 LASSERT(conn
->ibc_noops_posted
== 0);
868 LASSERT(conn
->ibc_nsends_posted
== 0);
870 switch (conn
->ibc_state
) {
872 /* conn must be completely disengaged from the network */
875 case IBLND_CONN_DISCONNECTED
:
876 /* connvars should have been freed already */
877 LASSERT(conn
->ibc_connvars
== NULL
);
880 case IBLND_CONN_INIT
:
884 /* conn->ibc_cmid might be destroyed by CM already */
885 if (cmid
!= NULL
&& cmid
->qp
!= NULL
)
886 rdma_destroy_qp(cmid
);
888 if (conn
->ibc_cq
!= NULL
) {
889 rc
= ib_destroy_cq(conn
->ibc_cq
);
891 CWARN("Error destroying CQ: %d\n", rc
);
894 if (conn
->ibc_rx_pages
!= NULL
)
895 kiblnd_unmap_rx_descs(conn
);
897 if (conn
->ibc_rxs
!= NULL
) {
898 LIBCFS_FREE(conn
->ibc_rxs
,
899 IBLND_RX_MSGS(conn
->ibc_version
)
903 if (conn
->ibc_connvars
!= NULL
)
904 LIBCFS_FREE(conn
->ibc_connvars
, sizeof(*conn
->ibc_connvars
));
906 if (conn
->ibc_hdev
!= NULL
)
907 kiblnd_hdev_decref(conn
->ibc_hdev
);
909 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
910 if (conn
->ibc_state
!= IBLND_CONN_INIT
) {
911 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
913 kiblnd_peer_decref(peer
);
914 rdma_destroy_id(cmid
);
915 atomic_dec(&net
->ibn_nconns
);
918 LIBCFS_FREE(conn
, sizeof(*conn
));
921 int kiblnd_close_peer_conns_locked(kib_peer_t
*peer
, int why
)
924 struct list_head
*ctmp
;
925 struct list_head
*cnxt
;
928 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
929 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
931 CDEBUG(D_NET
, "Closing conn -> %s, version: %x, reason: %d\n",
932 libcfs_nid2str(peer
->ibp_nid
),
933 conn
->ibc_version
, why
);
935 kiblnd_close_conn_locked(conn
, why
);
942 int kiblnd_close_stale_conns_locked(kib_peer_t
*peer
,
943 int version
, __u64 incarnation
)
946 struct list_head
*ctmp
;
947 struct list_head
*cnxt
;
950 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
951 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
953 if (conn
->ibc_version
== version
&&
954 conn
->ibc_incarnation
== incarnation
)
958 "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n",
959 libcfs_nid2str(peer
->ibp_nid
),
960 conn
->ibc_version
, conn
->ibc_incarnation
,
961 version
, incarnation
);
963 kiblnd_close_conn_locked(conn
, -ESTALE
);
970 static int kiblnd_close_matching_conns(lnet_ni_t
*ni
, lnet_nid_t nid
)
973 struct list_head
*ptmp
;
974 struct list_head
*pnxt
;
981 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
983 if (nid
!= LNET_NID_ANY
) {
984 lo
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
985 hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
988 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
991 for (i
= lo
; i
<= hi
; i
++) {
992 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
993 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
994 LASSERT(peer
->ibp_connecting
> 0 ||
995 peer
->ibp_accepting
> 0 ||
996 !list_empty(&peer
->ibp_conns
));
998 if (peer
->ibp_ni
!= ni
)
1001 if (!(nid
== LNET_NID_ANY
|| nid
== peer
->ibp_nid
))
1004 count
+= kiblnd_close_peer_conns_locked(peer
, 0);
1008 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1010 /* wildcards always succeed */
1011 if (nid
== LNET_NID_ANY
)
1014 return (count
== 0) ? -ENOENT
: 0;
1017 int kiblnd_ctl(lnet_ni_t
*ni
, unsigned int cmd
, void *arg
)
1019 struct libcfs_ioctl_data
*data
= arg
;
1023 case IOC_LIBCFS_GET_PEER
: {
1027 rc
= kiblnd_get_peer_info(ni
, data
->ioc_count
,
1029 data
->ioc_nid
= nid
;
1030 data
->ioc_count
= count
;
1034 case IOC_LIBCFS_DEL_PEER
: {
1035 rc
= kiblnd_del_peer(ni
, data
->ioc_nid
);
1038 case IOC_LIBCFS_GET_CONN
: {
1042 conn
= kiblnd_get_conn_by_idx(ni
, data
->ioc_count
);
1048 LASSERT(conn
->ibc_cmid
!= NULL
);
1049 data
->ioc_nid
= conn
->ibc_peer
->ibp_nid
;
1050 if (conn
->ibc_cmid
->route
.path_rec
== NULL
)
1051 data
->ioc_u32
[0] = 0; /* iWarp has no path MTU */
1054 ib_mtu_enum_to_int(conn
->ibc_cmid
->route
.path_rec
->mtu
);
1055 kiblnd_conn_decref(conn
);
1058 case IOC_LIBCFS_CLOSE_CONNECTION
: {
1059 rc
= kiblnd_close_matching_conns(ni
, data
->ioc_nid
);
1070 void kiblnd_query(lnet_ni_t
*ni
, lnet_nid_t nid
, unsigned long *when
)
1072 unsigned long last_alive
= 0;
1073 unsigned long now
= cfs_time_current();
1074 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
1076 unsigned long flags
;
1078 read_lock_irqsave(glock
, flags
);
1080 peer
= kiblnd_find_peer_locked(nid
);
1082 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
1083 peer
->ibp_accepting
> 0 ||
1084 !list_empty(&peer
->ibp_conns
)); /* active conn */
1085 last_alive
= peer
->ibp_last_alive
;
1088 read_unlock_irqrestore(glock
, flags
);
1090 if (last_alive
!= 0)
1094 * peer is not persistent in hash, trigger peer creation
1095 * and connection establishment with a NULL tx
1098 kiblnd_launch_tx(ni
, NULL
, nid
);
1100 CDEBUG(D_NET
, "Peer %s %p, alive %ld secs ago\n",
1101 libcfs_nid2str(nid
), peer
,
1102 last_alive
? cfs_duration_sec(now
- last_alive
) : -1);
1105 void kiblnd_free_pages(kib_pages_t
*p
)
1107 int npages
= p
->ibp_npages
;
1110 for (i
= 0; i
< npages
; i
++) {
1111 if (p
->ibp_pages
[i
] != NULL
)
1112 __free_page(p
->ibp_pages
[i
]);
1115 LIBCFS_FREE(p
, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1118 int kiblnd_alloc_pages(kib_pages_t
**pp
, int cpt
, int npages
)
1123 LIBCFS_CPT_ALLOC(p
, lnet_cpt_table(), cpt
,
1124 offsetof(kib_pages_t
, ibp_pages
[npages
]));
1126 CERROR("Can't allocate descriptor for %d pages\n", npages
);
1130 memset(p
, 0, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1131 p
->ibp_npages
= npages
;
1133 for (i
= 0; i
< npages
; i
++) {
1134 p
->ibp_pages
[i
] = alloc_pages_node(
1135 cfs_cpt_spread_node(lnet_cpt_table(), cpt
),
1137 if (p
->ibp_pages
[i
] == NULL
) {
1138 CERROR("Can't allocate page %d of %d\n", i
, npages
);
1139 kiblnd_free_pages(p
);
1148 void kiblnd_unmap_rx_descs(kib_conn_t
*conn
)
1153 LASSERT(conn
->ibc_rxs
!= NULL
);
1154 LASSERT(conn
->ibc_hdev
!= NULL
);
1156 for (i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1157 rx
= &conn
->ibc_rxs
[i
];
1159 LASSERT(rx
->rx_nob
>= 0); /* not posted */
1161 kiblnd_dma_unmap_single(conn
->ibc_hdev
->ibh_ibdev
,
1162 KIBLND_UNMAP_ADDR(rx
, rx_msgunmap
,
1164 IBLND_MSG_SIZE
, DMA_FROM_DEVICE
);
1167 kiblnd_free_pages(conn
->ibc_rx_pages
);
1169 conn
->ibc_rx_pages
= NULL
;
1172 void kiblnd_map_rx_descs(kib_conn_t
*conn
)
1180 for (pg_off
= ipg
= i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1181 pg
= conn
->ibc_rx_pages
->ibp_pages
[ipg
];
1182 rx
= &conn
->ibc_rxs
[i
];
1185 rx
->rx_msg
= (kib_msg_t
*)(((char *)page_address(pg
)) + pg_off
);
1187 rx
->rx_msgaddr
= kiblnd_dma_map_single(conn
->ibc_hdev
->ibh_ibdev
,
1191 LASSERT(!kiblnd_dma_mapping_error(conn
->ibc_hdev
->ibh_ibdev
,
1193 KIBLND_UNMAP_ADDR_SET(rx
, rx_msgunmap
, rx
->rx_msgaddr
);
1195 CDEBUG(D_NET
, "rx %d: %p %#llx(%#llx)\n",
1196 i
, rx
->rx_msg
, rx
->rx_msgaddr
,
1197 (__u64
)(page_to_phys(pg
) + pg_off
));
1199 pg_off
+= IBLND_MSG_SIZE
;
1200 LASSERT(pg_off
<= PAGE_SIZE
);
1202 if (pg_off
== PAGE_SIZE
) {
1205 LASSERT(ipg
<= IBLND_RX_MSG_PAGES(conn
->ibc_version
));
1210 static void kiblnd_unmap_tx_pool(kib_tx_pool_t
*tpo
)
1212 kib_hca_dev_t
*hdev
= tpo
->tpo_hdev
;
1216 LASSERT(tpo
->tpo_pool
.po_allocated
== 0);
1221 for (i
= 0; i
< tpo
->tpo_pool
.po_size
; i
++) {
1222 tx
= &tpo
->tpo_tx_descs
[i
];
1223 kiblnd_dma_unmap_single(hdev
->ibh_ibdev
,
1224 KIBLND_UNMAP_ADDR(tx
, tx_msgunmap
,
1226 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1229 kiblnd_hdev_decref(hdev
);
1230 tpo
->tpo_hdev
= NULL
;
1233 static kib_hca_dev_t
*kiblnd_current_hdev(kib_dev_t
*dev
)
1235 kib_hca_dev_t
*hdev
;
1236 unsigned long flags
;
1239 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1240 while (dev
->ibd_failover
) {
1241 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1243 CDEBUG(D_NET
, "%s: Wait for failover\n",
1245 schedule_timeout(cfs_time_seconds(1) / 100);
1247 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1250 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
1251 hdev
= dev
->ibd_hdev
;
1253 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1258 static void kiblnd_map_tx_pool(kib_tx_pool_t
*tpo
)
1260 kib_pages_t
*txpgs
= tpo
->tpo_tx_pages
;
1261 kib_pool_t
*pool
= &tpo
->tpo_pool
;
1262 kib_net_t
*net
= pool
->po_owner
->ps_net
;
1270 LASSERT(net
!= NULL
);
1274 /* pre-mapped messages are not bigger than 1 page */
1275 CLASSERT(IBLND_MSG_SIZE
<= PAGE_SIZE
);
1277 /* No fancy arithmetic when we do the buffer calculations */
1278 CLASSERT(PAGE_SIZE
% IBLND_MSG_SIZE
== 0);
1280 tpo
->tpo_hdev
= kiblnd_current_hdev(dev
);
1282 for (ipage
= page_offset
= i
= 0; i
< pool
->po_size
; i
++) {
1283 page
= txpgs
->ibp_pages
[ipage
];
1284 tx
= &tpo
->tpo_tx_descs
[i
];
1286 tx
->tx_msg
= (kib_msg_t
*)(((char *)page_address(page
)) +
1289 tx
->tx_msgaddr
= kiblnd_dma_map_single(
1290 tpo
->tpo_hdev
->ibh_ibdev
, tx
->tx_msg
,
1291 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1292 LASSERT(!kiblnd_dma_mapping_error(tpo
->tpo_hdev
->ibh_ibdev
,
1294 KIBLND_UNMAP_ADDR_SET(tx
, tx_msgunmap
, tx
->tx_msgaddr
);
1296 list_add(&tx
->tx_list
, &pool
->po_free_list
);
1298 page_offset
+= IBLND_MSG_SIZE
;
1299 LASSERT(page_offset
<= PAGE_SIZE
);
1301 if (page_offset
== PAGE_SIZE
) {
1304 LASSERT(ipage
<= txpgs
->ibp_npages
);
1309 struct ib_mr
*kiblnd_find_dma_mr(kib_hca_dev_t
*hdev
, __u64 addr
, __u64 size
)
1313 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1315 if (hdev
->ibh_nmrs
== 1)
1316 return hdev
->ibh_mrs
[0];
1318 index
= addr
>> hdev
->ibh_mr_shift
;
1320 if (index
< hdev
->ibh_nmrs
&&
1321 index
== ((addr
+ size
- 1) >> hdev
->ibh_mr_shift
))
1322 return hdev
->ibh_mrs
[index
];
1327 struct ib_mr
*kiblnd_find_rd_dma_mr(kib_hca_dev_t
*hdev
, kib_rdma_desc_t
*rd
)
1329 struct ib_mr
*prev_mr
;
1333 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1335 if (*kiblnd_tunables
.kib_map_on_demand
> 0 &&
1336 *kiblnd_tunables
.kib_map_on_demand
<= rd
->rd_nfrags
)
1339 if (hdev
->ibh_nmrs
== 1)
1340 return hdev
->ibh_mrs
[0];
1342 for (i
= 0, mr
= prev_mr
= NULL
;
1343 i
< rd
->rd_nfrags
; i
++) {
1344 mr
= kiblnd_find_dma_mr(hdev
,
1345 rd
->rd_frags
[i
].rf_addr
,
1346 rd
->rd_frags
[i
].rf_nob
);
1347 if (prev_mr
== NULL
)
1350 if (mr
== NULL
|| prev_mr
!= mr
) {
1351 /* Can't covered by one single MR */
1360 static void kiblnd_destroy_fmr_pool(kib_fmr_pool_t
*pool
)
1362 LASSERT(pool
->fpo_map_count
== 0);
1364 if (pool
->fpo_fmr_pool
!= NULL
)
1365 ib_destroy_fmr_pool(pool
->fpo_fmr_pool
);
1367 if (pool
->fpo_hdev
!= NULL
)
1368 kiblnd_hdev_decref(pool
->fpo_hdev
);
1370 LIBCFS_FREE(pool
, sizeof(*pool
));
1373 static void kiblnd_destroy_fmr_pool_list(struct list_head
*head
)
1375 kib_fmr_pool_t
*pool
;
1377 while (!list_empty(head
)) {
1378 pool
= list_entry(head
->next
, kib_fmr_pool_t
, fpo_list
);
1379 list_del(&pool
->fpo_list
);
1380 kiblnd_destroy_fmr_pool(pool
);
1384 static int kiblnd_fmr_pool_size(int ncpts
)
1386 int size
= *kiblnd_tunables
.kib_fmr_pool_size
/ ncpts
;
1388 return max(IBLND_FMR_POOL
, size
);
1391 static int kiblnd_fmr_flush_trigger(int ncpts
)
1393 int size
= *kiblnd_tunables
.kib_fmr_flush_trigger
/ ncpts
;
1395 return max(IBLND_FMR_POOL_FLUSH
, size
);
1398 static int kiblnd_create_fmr_pool(kib_fmr_poolset_t
*fps
,
1399 kib_fmr_pool_t
**pp_fpo
)
1401 /* FMR pool for RDMA */
1402 kib_dev_t
*dev
= fps
->fps_net
->ibn_dev
;
1403 kib_fmr_pool_t
*fpo
;
1404 struct ib_fmr_pool_param param
= {
1405 .max_pages_per_fmr
= LNET_MAX_PAYLOAD
/ PAGE_SIZE
,
1406 .page_shift
= PAGE_SHIFT
,
1407 .access
= (IB_ACCESS_LOCAL_WRITE
|
1408 IB_ACCESS_REMOTE_WRITE
),
1409 .pool_size
= fps
->fps_pool_size
,
1410 .dirty_watermark
= fps
->fps_flush_trigger
,
1411 .flush_function
= NULL
,
1413 .cache
= !!*kiblnd_tunables
.kib_fmr_cache
};
1416 LIBCFS_CPT_ALLOC(fpo
, lnet_cpt_table(), fps
->fps_cpt
, sizeof(*fpo
));
1420 fpo
->fpo_hdev
= kiblnd_current_hdev(dev
);
1422 fpo
->fpo_fmr_pool
= ib_create_fmr_pool(fpo
->fpo_hdev
->ibh_pd
, ¶m
);
1423 if (IS_ERR(fpo
->fpo_fmr_pool
)) {
1424 rc
= PTR_ERR(fpo
->fpo_fmr_pool
);
1425 CERROR("Failed to create FMR pool: %d\n", rc
);
1427 kiblnd_hdev_decref(fpo
->fpo_hdev
);
1428 LIBCFS_FREE(fpo
, sizeof(*fpo
));
1432 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1433 fpo
->fpo_owner
= fps
;
1439 static void kiblnd_fail_fmr_poolset(kib_fmr_poolset_t
*fps
,
1440 struct list_head
*zombies
)
1442 if (fps
->fps_net
== NULL
) /* intialized? */
1445 spin_lock(&fps
->fps_lock
);
1447 while (!list_empty(&fps
->fps_pool_list
)) {
1448 kib_fmr_pool_t
*fpo
= list_entry(fps
->fps_pool_list
.next
,
1449 kib_fmr_pool_t
, fpo_list
);
1450 fpo
->fpo_failed
= 1;
1451 list_del(&fpo
->fpo_list
);
1452 if (fpo
->fpo_map_count
== 0)
1453 list_add(&fpo
->fpo_list
, zombies
);
1455 list_add(&fpo
->fpo_list
, &fps
->fps_failed_pool_list
);
1458 spin_unlock(&fps
->fps_lock
);
1461 static void kiblnd_fini_fmr_poolset(kib_fmr_poolset_t
*fps
)
1463 if (fps
->fps_net
!= NULL
) { /* initialized? */
1464 kiblnd_destroy_fmr_pool_list(&fps
->fps_failed_pool_list
);
1465 kiblnd_destroy_fmr_pool_list(&fps
->fps_pool_list
);
1469 static int kiblnd_init_fmr_poolset(kib_fmr_poolset_t
*fps
, int cpt
,
1470 kib_net_t
*net
, int pool_size
,
1473 kib_fmr_pool_t
*fpo
;
1476 memset(fps
, 0, sizeof(*fps
));
1480 fps
->fps_pool_size
= pool_size
;
1481 fps
->fps_flush_trigger
= flush_trigger
;
1482 spin_lock_init(&fps
->fps_lock
);
1483 INIT_LIST_HEAD(&fps
->fps_pool_list
);
1484 INIT_LIST_HEAD(&fps
->fps_failed_pool_list
);
1486 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1488 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1493 static int kiblnd_fmr_pool_is_idle(kib_fmr_pool_t
*fpo
, unsigned long now
)
1495 if (fpo
->fpo_map_count
!= 0) /* still in use */
1497 if (fpo
->fpo_failed
)
1499 return cfs_time_aftereq(now
, fpo
->fpo_deadline
);
1502 void kiblnd_fmr_pool_unmap(kib_fmr_t
*fmr
, int status
)
1505 kib_fmr_pool_t
*fpo
= fmr
->fmr_pool
;
1506 kib_fmr_poolset_t
*fps
= fpo
->fpo_owner
;
1507 unsigned long now
= cfs_time_current();
1508 kib_fmr_pool_t
*tmp
;
1511 rc
= ib_fmr_pool_unmap(fmr
->fmr_pfmr
);
1515 rc
= ib_flush_fmr_pool(fpo
->fpo_fmr_pool
);
1519 fmr
->fmr_pool
= NULL
;
1520 fmr
->fmr_pfmr
= NULL
;
1522 spin_lock(&fps
->fps_lock
);
1523 fpo
->fpo_map_count
--; /* decref the pool */
1525 list_for_each_entry_safe(fpo
, tmp
, &fps
->fps_pool_list
, fpo_list
) {
1526 /* the first pool is persistent */
1527 if (fps
->fps_pool_list
.next
== &fpo
->fpo_list
)
1530 if (kiblnd_fmr_pool_is_idle(fpo
, now
)) {
1531 list_move(&fpo
->fpo_list
, &zombies
);
1535 spin_unlock(&fps
->fps_lock
);
1537 if (!list_empty(&zombies
))
1538 kiblnd_destroy_fmr_pool_list(&zombies
);
1541 int kiblnd_fmr_pool_map(kib_fmr_poolset_t
*fps
, __u64
*pages
, int npages
,
1542 __u64 iov
, kib_fmr_t
*fmr
)
1544 struct ib_pool_fmr
*pfmr
;
1545 kib_fmr_pool_t
*fpo
;
1550 spin_lock(&fps
->fps_lock
);
1551 version
= fps
->fps_version
;
1552 list_for_each_entry(fpo
, &fps
->fps_pool_list
, fpo_list
) {
1553 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1554 fpo
->fpo_map_count
++;
1555 spin_unlock(&fps
->fps_lock
);
1557 pfmr
= ib_fmr_pool_map_phys(fpo
->fpo_fmr_pool
,
1558 pages
, npages
, iov
);
1559 if (likely(!IS_ERR(pfmr
))) {
1560 fmr
->fmr_pool
= fpo
;
1561 fmr
->fmr_pfmr
= pfmr
;
1565 spin_lock(&fps
->fps_lock
);
1566 fpo
->fpo_map_count
--;
1567 if (PTR_ERR(pfmr
) != -EAGAIN
) {
1568 spin_unlock(&fps
->fps_lock
);
1569 return PTR_ERR(pfmr
);
1572 /* EAGAIN and ... */
1573 if (version
!= fps
->fps_version
) {
1574 spin_unlock(&fps
->fps_lock
);
1579 if (fps
->fps_increasing
) {
1580 spin_unlock(&fps
->fps_lock
);
1581 CDEBUG(D_NET
, "Another thread is allocating new FMR pool, waiting for her to complete\n");
1586 if (time_before(cfs_time_current(), fps
->fps_next_retry
)) {
1587 /* someone failed recently */
1588 spin_unlock(&fps
->fps_lock
);
1592 fps
->fps_increasing
= 1;
1593 spin_unlock(&fps
->fps_lock
);
1595 CDEBUG(D_NET
, "Allocate new FMR pool\n");
1596 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1597 spin_lock(&fps
->fps_lock
);
1598 fps
->fps_increasing
= 0;
1601 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1603 fps
->fps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1605 spin_unlock(&fps
->fps_lock
);
1610 static void kiblnd_fini_pool(kib_pool_t
*pool
)
1612 LASSERT(list_empty(&pool
->po_free_list
));
1613 LASSERT(pool
->po_allocated
== 0);
1615 CDEBUG(D_NET
, "Finalize %s pool\n", pool
->po_owner
->ps_name
);
1618 static void kiblnd_init_pool(kib_poolset_t
*ps
, kib_pool_t
*pool
, int size
)
1620 CDEBUG(D_NET
, "Initialize %s pool\n", ps
->ps_name
);
1622 memset(pool
, 0, sizeof(*pool
));
1623 INIT_LIST_HEAD(&pool
->po_free_list
);
1624 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1625 pool
->po_owner
= ps
;
1626 pool
->po_size
= size
;
1629 static void kiblnd_destroy_pool_list(struct list_head
*head
)
1633 while (!list_empty(head
)) {
1634 pool
= list_entry(head
->next
, kib_pool_t
, po_list
);
1635 list_del(&pool
->po_list
);
1637 LASSERT(pool
->po_owner
!= NULL
);
1638 pool
->po_owner
->ps_pool_destroy(pool
);
1642 static void kiblnd_fail_poolset(kib_poolset_t
*ps
, struct list_head
*zombies
)
1644 if (ps
->ps_net
== NULL
) /* intialized? */
1647 spin_lock(&ps
->ps_lock
);
1648 while (!list_empty(&ps
->ps_pool_list
)) {
1649 kib_pool_t
*po
= list_entry(ps
->ps_pool_list
.next
,
1650 kib_pool_t
, po_list
);
1652 list_del(&po
->po_list
);
1653 if (po
->po_allocated
== 0)
1654 list_add(&po
->po_list
, zombies
);
1656 list_add(&po
->po_list
, &ps
->ps_failed_pool_list
);
1658 spin_unlock(&ps
->ps_lock
);
1661 static void kiblnd_fini_poolset(kib_poolset_t
*ps
)
1663 if (ps
->ps_net
!= NULL
) { /* initialized? */
1664 kiblnd_destroy_pool_list(&ps
->ps_failed_pool_list
);
1665 kiblnd_destroy_pool_list(&ps
->ps_pool_list
);
1669 static int kiblnd_init_poolset(kib_poolset_t
*ps
, int cpt
,
1670 kib_net_t
*net
, char *name
, int size
,
1671 kib_ps_pool_create_t po_create
,
1672 kib_ps_pool_destroy_t po_destroy
,
1673 kib_ps_node_init_t nd_init
,
1674 kib_ps_node_fini_t nd_fini
)
1679 memset(ps
, 0, sizeof(*ps
));
1683 ps
->ps_pool_create
= po_create
;
1684 ps
->ps_pool_destroy
= po_destroy
;
1685 ps
->ps_node_init
= nd_init
;
1686 ps
->ps_node_fini
= nd_fini
;
1687 ps
->ps_pool_size
= size
;
1688 if (strlcpy(ps
->ps_name
, name
, sizeof(ps
->ps_name
))
1689 >= sizeof(ps
->ps_name
))
1691 spin_lock_init(&ps
->ps_lock
);
1692 INIT_LIST_HEAD(&ps
->ps_pool_list
);
1693 INIT_LIST_HEAD(&ps
->ps_failed_pool_list
);
1695 rc
= ps
->ps_pool_create(ps
, size
, &pool
);
1697 list_add(&pool
->po_list
, &ps
->ps_pool_list
);
1699 CERROR("Failed to create the first pool for %s\n", ps
->ps_name
);
1704 static int kiblnd_pool_is_idle(kib_pool_t
*pool
, unsigned long now
)
1706 if (pool
->po_allocated
!= 0) /* still in use */
1708 if (pool
->po_failed
)
1710 return cfs_time_aftereq(now
, pool
->po_deadline
);
1713 void kiblnd_pool_free_node(kib_pool_t
*pool
, struct list_head
*node
)
1716 kib_poolset_t
*ps
= pool
->po_owner
;
1718 unsigned long now
= cfs_time_current();
1720 spin_lock(&ps
->ps_lock
);
1722 if (ps
->ps_node_fini
!= NULL
)
1723 ps
->ps_node_fini(pool
, node
);
1725 LASSERT(pool
->po_allocated
> 0);
1726 list_add(node
, &pool
->po_free_list
);
1727 pool
->po_allocated
--;
1729 list_for_each_entry_safe(pool
, tmp
, &ps
->ps_pool_list
, po_list
) {
1730 /* the first pool is persistent */
1731 if (ps
->ps_pool_list
.next
== &pool
->po_list
)
1734 if (kiblnd_pool_is_idle(pool
, now
))
1735 list_move(&pool
->po_list
, &zombies
);
1737 spin_unlock(&ps
->ps_lock
);
1739 if (!list_empty(&zombies
))
1740 kiblnd_destroy_pool_list(&zombies
);
1743 struct list_head
*kiblnd_pool_alloc_node(kib_poolset_t
*ps
)
1745 struct list_head
*node
;
1750 spin_lock(&ps
->ps_lock
);
1751 list_for_each_entry(pool
, &ps
->ps_pool_list
, po_list
) {
1752 if (list_empty(&pool
->po_free_list
))
1755 pool
->po_allocated
++;
1756 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1757 node
= pool
->po_free_list
.next
;
1760 if (ps
->ps_node_init
!= NULL
) {
1761 /* still hold the lock */
1762 ps
->ps_node_init(pool
, node
);
1764 spin_unlock(&ps
->ps_lock
);
1768 /* no available tx pool and ... */
1769 if (ps
->ps_increasing
) {
1770 /* another thread is allocating a new pool */
1771 spin_unlock(&ps
->ps_lock
);
1772 CDEBUG(D_NET
, "Another thread is allocating new %s pool, waiting for her to complete\n",
1778 if (time_before(cfs_time_current(), ps
->ps_next_retry
)) {
1779 /* someone failed recently */
1780 spin_unlock(&ps
->ps_lock
);
1784 ps
->ps_increasing
= 1;
1785 spin_unlock(&ps
->ps_lock
);
1787 CDEBUG(D_NET
, "%s pool exhausted, allocate new pool\n", ps
->ps_name
);
1789 rc
= ps
->ps_pool_create(ps
, ps
->ps_pool_size
, &pool
);
1791 spin_lock(&ps
->ps_lock
);
1792 ps
->ps_increasing
= 0;
1794 list_add_tail(&pool
->po_list
, &ps
->ps_pool_list
);
1796 ps
->ps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1797 CERROR("Can't allocate new %s pool because out of memory\n",
1800 spin_unlock(&ps
->ps_lock
);
1805 static void kiblnd_destroy_tx_pool(kib_pool_t
*pool
)
1807 kib_tx_pool_t
*tpo
= container_of(pool
, kib_tx_pool_t
, tpo_pool
);
1810 LASSERT(pool
->po_allocated
== 0);
1812 if (tpo
->tpo_tx_pages
!= NULL
) {
1813 kiblnd_unmap_tx_pool(tpo
);
1814 kiblnd_free_pages(tpo
->tpo_tx_pages
);
1817 if (tpo
->tpo_tx_descs
== NULL
)
1820 for (i
= 0; i
< pool
->po_size
; i
++) {
1821 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1823 list_del(&tx
->tx_list
);
1824 if (tx
->tx_pages
!= NULL
)
1825 LIBCFS_FREE(tx
->tx_pages
,
1827 sizeof(*tx
->tx_pages
));
1828 if (tx
->tx_frags
!= NULL
)
1829 LIBCFS_FREE(tx
->tx_frags
,
1830 IBLND_MAX_RDMA_FRAGS
*
1831 sizeof(*tx
->tx_frags
));
1832 if (tx
->tx_wrq
!= NULL
)
1833 LIBCFS_FREE(tx
->tx_wrq
,
1834 (1 + IBLND_MAX_RDMA_FRAGS
) *
1835 sizeof(*tx
->tx_wrq
));
1836 if (tx
->tx_sge
!= NULL
)
1837 LIBCFS_FREE(tx
->tx_sge
,
1838 (1 + IBLND_MAX_RDMA_FRAGS
) *
1839 sizeof(*tx
->tx_sge
));
1840 if (tx
->tx_rd
!= NULL
)
1841 LIBCFS_FREE(tx
->tx_rd
,
1842 offsetof(kib_rdma_desc_t
,
1843 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1846 LIBCFS_FREE(tpo
->tpo_tx_descs
,
1847 pool
->po_size
* sizeof(kib_tx_t
));
1849 kiblnd_fini_pool(pool
);
1850 LIBCFS_FREE(tpo
, sizeof(*tpo
));
1853 static int kiblnd_tx_pool_size(int ncpts
)
1855 int ntx
= *kiblnd_tunables
.kib_ntx
/ ncpts
;
1857 return max(IBLND_TX_POOL
, ntx
);
1860 static int kiblnd_create_tx_pool(kib_poolset_t
*ps
, int size
,
1868 LIBCFS_CPT_ALLOC(tpo
, lnet_cpt_table(), ps
->ps_cpt
, sizeof(*tpo
));
1870 CERROR("Failed to allocate TX pool\n");
1874 pool
= &tpo
->tpo_pool
;
1875 kiblnd_init_pool(ps
, pool
, size
);
1876 tpo
->tpo_tx_descs
= NULL
;
1877 tpo
->tpo_tx_pages
= NULL
;
1879 npg
= (size
* IBLND_MSG_SIZE
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1880 if (kiblnd_alloc_pages(&tpo
->tpo_tx_pages
, ps
->ps_cpt
, npg
) != 0) {
1881 CERROR("Can't allocate tx pages: %d\n", npg
);
1882 LIBCFS_FREE(tpo
, sizeof(*tpo
));
1886 LIBCFS_CPT_ALLOC(tpo
->tpo_tx_descs
, lnet_cpt_table(), ps
->ps_cpt
,
1887 size
* sizeof(kib_tx_t
));
1888 if (tpo
->tpo_tx_descs
== NULL
) {
1889 CERROR("Can't allocate %d tx descriptors\n", size
);
1890 ps
->ps_pool_destroy(pool
);
1894 memset(tpo
->tpo_tx_descs
, 0, size
* sizeof(kib_tx_t
));
1896 for (i
= 0; i
< size
; i
++) {
1897 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1900 if (ps
->ps_net
->ibn_fmr_ps
!= NULL
) {
1901 LIBCFS_CPT_ALLOC(tx
->tx_pages
,
1902 lnet_cpt_table(), ps
->ps_cpt
,
1903 LNET_MAX_IOV
* sizeof(*tx
->tx_pages
));
1904 if (tx
->tx_pages
== NULL
)
1908 LIBCFS_CPT_ALLOC(tx
->tx_frags
, lnet_cpt_table(), ps
->ps_cpt
,
1909 IBLND_MAX_RDMA_FRAGS
* sizeof(*tx
->tx_frags
));
1910 if (tx
->tx_frags
== NULL
)
1913 sg_init_table(tx
->tx_frags
, IBLND_MAX_RDMA_FRAGS
);
1915 LIBCFS_CPT_ALLOC(tx
->tx_wrq
, lnet_cpt_table(), ps
->ps_cpt
,
1916 (1 + IBLND_MAX_RDMA_FRAGS
) *
1917 sizeof(*tx
->tx_wrq
));
1918 if (tx
->tx_wrq
== NULL
)
1921 LIBCFS_CPT_ALLOC(tx
->tx_sge
, lnet_cpt_table(), ps
->ps_cpt
,
1922 (1 + IBLND_MAX_RDMA_FRAGS
) *
1923 sizeof(*tx
->tx_sge
));
1924 if (tx
->tx_sge
== NULL
)
1927 LIBCFS_CPT_ALLOC(tx
->tx_rd
, lnet_cpt_table(), ps
->ps_cpt
,
1928 offsetof(kib_rdma_desc_t
,
1929 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1930 if (tx
->tx_rd
== NULL
)
1935 kiblnd_map_tx_pool(tpo
);
1940 ps
->ps_pool_destroy(pool
);
1944 static void kiblnd_tx_init(kib_pool_t
*pool
, struct list_head
*node
)
1946 kib_tx_poolset_t
*tps
= container_of(pool
->po_owner
, kib_tx_poolset_t
,
1948 kib_tx_t
*tx
= list_entry(node
, kib_tx_t
, tx_list
);
1950 tx
->tx_cookie
= tps
->tps_next_tx_cookie
++;
1953 static void kiblnd_net_fini_pools(kib_net_t
*net
)
1957 cfs_cpt_for_each(i
, lnet_cpt_table()) {
1958 kib_tx_poolset_t
*tps
;
1959 kib_fmr_poolset_t
*fps
;
1961 if (net
->ibn_tx_ps
!= NULL
) {
1962 tps
= net
->ibn_tx_ps
[i
];
1963 kiblnd_fini_poolset(&tps
->tps_poolset
);
1966 if (net
->ibn_fmr_ps
!= NULL
) {
1967 fps
= net
->ibn_fmr_ps
[i
];
1968 kiblnd_fini_fmr_poolset(fps
);
1972 if (net
->ibn_tx_ps
!= NULL
) {
1973 cfs_percpt_free(net
->ibn_tx_ps
);
1974 net
->ibn_tx_ps
= NULL
;
1977 if (net
->ibn_fmr_ps
!= NULL
) {
1978 cfs_percpt_free(net
->ibn_fmr_ps
);
1979 net
->ibn_fmr_ps
= NULL
;
1983 static int kiblnd_net_init_pools(kib_net_t
*net
, __u32
*cpts
, int ncpts
)
1985 unsigned long flags
;
1990 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1991 if (*kiblnd_tunables
.kib_map_on_demand
== 0 &&
1992 net
->ibn_dev
->ibd_hdev
->ibh_nmrs
== 1) {
1993 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1994 goto create_tx_pool
;
1997 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1999 if (*kiblnd_tunables
.kib_fmr_pool_size
<
2000 *kiblnd_tunables
.kib_ntx
/ 4) {
2001 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2002 *kiblnd_tunables
.kib_fmr_pool_size
,
2003 *kiblnd_tunables
.kib_ntx
/ 4);
2009 * TX pool must be created later than FMR, see LU-2268
2012 LASSERT(net
->ibn_tx_ps
== NULL
);
2015 * premapping can fail if ibd_nmr > 1, so we always create
2016 * FMR pool and map-on-demand if premapping failed
2019 net
->ibn_fmr_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2020 sizeof(kib_fmr_poolset_t
));
2021 if (net
->ibn_fmr_ps
== NULL
) {
2022 CERROR("Failed to allocate FMR pool array\n");
2027 for (i
= 0; i
< ncpts
; i
++) {
2028 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2029 rc
= kiblnd_init_fmr_poolset(net
->ibn_fmr_ps
[cpt
], cpt
, net
,
2030 kiblnd_fmr_pool_size(ncpts
),
2031 kiblnd_fmr_flush_trigger(ncpts
));
2032 if (rc
== -ENOSYS
&& i
== 0) /* no FMR */
2035 if (rc
!= 0) { /* a real error */
2036 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2043 LASSERT(i
== ncpts
);
2044 goto create_tx_pool
;
2047 cfs_percpt_free(net
->ibn_fmr_ps
);
2048 net
->ibn_fmr_ps
= NULL
;
2050 CWARN("Device does not support FMR\n");
2054 net
->ibn_tx_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2055 sizeof(kib_tx_poolset_t
));
2056 if (net
->ibn_tx_ps
== NULL
) {
2057 CERROR("Failed to allocate tx pool array\n");
2062 for (i
= 0; i
< ncpts
; i
++) {
2063 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2064 rc
= kiblnd_init_poolset(&net
->ibn_tx_ps
[cpt
]->tps_poolset
,
2066 kiblnd_tx_pool_size(ncpts
),
2067 kiblnd_create_tx_pool
,
2068 kiblnd_destroy_tx_pool
,
2069 kiblnd_tx_init
, NULL
);
2071 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2079 kiblnd_net_fini_pools(net
);
2084 static int kiblnd_hdev_get_attr(kib_hca_dev_t
*hdev
)
2087 * It's safe to assume a HCA can handle a page size
2088 * matching that of the native system
2090 hdev
->ibh_page_shift
= PAGE_SHIFT
;
2091 hdev
->ibh_page_size
= 1 << PAGE_SHIFT
;
2092 hdev
->ibh_page_mask
= ~((__u64
)hdev
->ibh_page_size
- 1);
2094 hdev
->ibh_mr_size
= hdev
->ibh_ibdev
->attrs
.max_mr_size
;
2095 if (hdev
->ibh_mr_size
== ~0ULL) {
2096 hdev
->ibh_mr_shift
= 64;
2100 for (hdev
->ibh_mr_shift
= 0;
2101 hdev
->ibh_mr_shift
< 64; hdev
->ibh_mr_shift
++) {
2102 if (hdev
->ibh_mr_size
== (1ULL << hdev
->ibh_mr_shift
) ||
2103 hdev
->ibh_mr_size
== (1ULL << hdev
->ibh_mr_shift
) - 1)
2107 CERROR("Invalid mr size: %#llx\n", hdev
->ibh_mr_size
);
2111 static void kiblnd_hdev_cleanup_mrs(kib_hca_dev_t
*hdev
)
2115 if (hdev
->ibh_nmrs
== 0 || hdev
->ibh_mrs
== NULL
)
2118 for (i
= 0; i
< hdev
->ibh_nmrs
; i
++) {
2119 if (hdev
->ibh_mrs
[i
] == NULL
)
2122 ib_dereg_mr(hdev
->ibh_mrs
[i
]);
2125 LIBCFS_FREE(hdev
->ibh_mrs
, sizeof(*hdev
->ibh_mrs
) * hdev
->ibh_nmrs
);
2126 hdev
->ibh_mrs
= NULL
;
2130 void kiblnd_hdev_destroy(kib_hca_dev_t
*hdev
)
2132 kiblnd_hdev_cleanup_mrs(hdev
);
2134 if (hdev
->ibh_pd
!= NULL
)
2135 ib_dealloc_pd(hdev
->ibh_pd
);
2137 if (hdev
->ibh_cmid
!= NULL
)
2138 rdma_destroy_id(hdev
->ibh_cmid
);
2140 LIBCFS_FREE(hdev
, sizeof(*hdev
));
2143 static int kiblnd_hdev_setup_mrs(kib_hca_dev_t
*hdev
)
2147 int acflags
= IB_ACCESS_LOCAL_WRITE
| IB_ACCESS_REMOTE_WRITE
;
2149 rc
= kiblnd_hdev_get_attr(hdev
);
2153 LIBCFS_ALLOC(hdev
->ibh_mrs
, 1 * sizeof(*hdev
->ibh_mrs
));
2154 if (hdev
->ibh_mrs
== NULL
) {
2155 CERROR("Failed to allocate MRs table\n");
2159 hdev
->ibh_mrs
[0] = NULL
;
2162 mr
= ib_get_dma_mr(hdev
->ibh_pd
, acflags
);
2164 CERROR("Failed ib_get_dma_mr : %ld\n", PTR_ERR(mr
));
2165 kiblnd_hdev_cleanup_mrs(hdev
);
2169 hdev
->ibh_mrs
[0] = mr
;
2175 static int kiblnd_dummy_callback(struct rdma_cm_id
*cmid
,
2176 struct rdma_cm_event
*event
)
2181 static int kiblnd_dev_need_failover(kib_dev_t
*dev
)
2183 struct rdma_cm_id
*cmid
;
2184 struct sockaddr_in srcaddr
;
2185 struct sockaddr_in dstaddr
;
2188 if (dev
->ibd_hdev
== NULL
|| /* initializing */
2189 dev
->ibd_hdev
->ibh_cmid
== NULL
|| /* listener is dead */
2190 *kiblnd_tunables
.kib_dev_failover
> 1) /* debugging */
2194 * XXX: it's UGLY, but I don't have better way to find
2195 * ib-bonding HCA failover because:
2197 * a. no reliable CM event for HCA failover...
2198 * b. no OFED API to get ib_device for current net_device...
2200 * We have only two choices at this point:
2202 * a. rdma_bind_addr(), it will conflict with listener cmid
2203 * b. rdma_resolve_addr() to zero addr
2205 cmid
= kiblnd_rdma_create_id(kiblnd_dummy_callback
, dev
, RDMA_PS_TCP
,
2209 CERROR("Failed to create cmid for failover: %d\n", rc
);
2213 memset(&srcaddr
, 0, sizeof(srcaddr
));
2214 srcaddr
.sin_family
= AF_INET
;
2215 srcaddr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2217 memset(&dstaddr
, 0, sizeof(dstaddr
));
2218 dstaddr
.sin_family
= AF_INET
;
2219 rc
= rdma_resolve_addr(cmid
, (struct sockaddr
*)&srcaddr
,
2220 (struct sockaddr
*)&dstaddr
, 1);
2221 if (rc
!= 0 || cmid
->device
== NULL
) {
2222 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2223 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2225 rdma_destroy_id(cmid
);
2229 rc
= dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
; /* true for failover */
2230 rdma_destroy_id(cmid
);
2235 int kiblnd_dev_failover(kib_dev_t
*dev
)
2237 LIST_HEAD(zombie_tpo
);
2238 LIST_HEAD(zombie_ppo
);
2239 LIST_HEAD(zombie_fpo
);
2240 struct rdma_cm_id
*cmid
= NULL
;
2241 kib_hca_dev_t
*hdev
= NULL
;
2244 struct sockaddr_in addr
;
2245 unsigned long flags
;
2249 LASSERT(*kiblnd_tunables
.kib_dev_failover
> 1 ||
2250 dev
->ibd_can_failover
|| dev
->ibd_hdev
== NULL
);
2252 rc
= kiblnd_dev_need_failover(dev
);
2256 if (dev
->ibd_hdev
!= NULL
&&
2257 dev
->ibd_hdev
->ibh_cmid
!= NULL
) {
2259 * XXX it's not good to close old listener at here,
2260 * because we can fail to create new listener.
2261 * But we have to close it now, otherwise rdma_bind_addr
2262 * will return EADDRINUSE... How crap!
2264 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2266 cmid
= dev
->ibd_hdev
->ibh_cmid
;
2268 * make next schedule of kiblnd_dev_need_failover()
2271 dev
->ibd_hdev
->ibh_cmid
= NULL
;
2272 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2274 rdma_destroy_id(cmid
);
2277 cmid
= kiblnd_rdma_create_id(kiblnd_cm_callback
, dev
, RDMA_PS_TCP
,
2281 CERROR("Failed to create cmid for failover: %d\n", rc
);
2285 memset(&addr
, 0, sizeof(addr
));
2286 addr
.sin_family
= AF_INET
;
2287 addr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2288 addr
.sin_port
= htons(*kiblnd_tunables
.kib_service
);
2290 /* Bind to failover device or port */
2291 rc
= rdma_bind_addr(cmid
, (struct sockaddr
*)&addr
);
2292 if (rc
!= 0 || cmid
->device
== NULL
) {
2293 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2294 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2296 rdma_destroy_id(cmid
);
2300 LIBCFS_ALLOC(hdev
, sizeof(*hdev
));
2302 CERROR("Failed to allocate kib_hca_dev\n");
2303 rdma_destroy_id(cmid
);
2308 atomic_set(&hdev
->ibh_ref
, 1);
2309 hdev
->ibh_dev
= dev
;
2310 hdev
->ibh_cmid
= cmid
;
2311 hdev
->ibh_ibdev
= cmid
->device
;
2313 pd
= ib_alloc_pd(cmid
->device
);
2316 CERROR("Can't allocate PD: %d\n", rc
);
2322 rc
= rdma_listen(cmid
, 0);
2324 CERROR("Can't start new listener: %d\n", rc
);
2328 rc
= kiblnd_hdev_setup_mrs(hdev
);
2330 CERROR("Can't setup device: %d\n", rc
);
2334 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2336 swap(dev
->ibd_hdev
, hdev
); /* take over the refcount */
2338 list_for_each_entry(net
, &dev
->ibd_nets
, ibn_list
) {
2339 cfs_cpt_for_each(i
, lnet_cpt_table()) {
2340 kiblnd_fail_poolset(&net
->ibn_tx_ps
[i
]->tps_poolset
,
2343 if (net
->ibn_fmr_ps
)
2344 kiblnd_fail_fmr_poolset(net
->ibn_fmr_ps
[i
],
2349 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2351 if (!list_empty(&zombie_tpo
))
2352 kiblnd_destroy_pool_list(&zombie_tpo
);
2353 if (!list_empty(&zombie_ppo
))
2354 kiblnd_destroy_pool_list(&zombie_ppo
);
2355 if (!list_empty(&zombie_fpo
))
2356 kiblnd_destroy_fmr_pool_list(&zombie_fpo
);
2358 kiblnd_hdev_decref(hdev
);
2361 dev
->ibd_failed_failover
++;
2363 dev
->ibd_failed_failover
= 0;
2368 void kiblnd_destroy_dev(kib_dev_t
*dev
)
2370 LASSERT(dev
->ibd_nnets
== 0);
2371 LASSERT(list_empty(&dev
->ibd_nets
));
2373 list_del(&dev
->ibd_fail_list
);
2374 list_del(&dev
->ibd_list
);
2376 if (dev
->ibd_hdev
!= NULL
)
2377 kiblnd_hdev_decref(dev
->ibd_hdev
);
2379 LIBCFS_FREE(dev
, sizeof(*dev
));
2382 static kib_dev_t
*kiblnd_create_dev(char *ifname
)
2384 struct net_device
*netdev
;
2391 rc
= lnet_ipif_query(ifname
, &up
, &ip
, &netmask
);
2393 CERROR("Can't query IPoIB interface %s: %d\n",
2399 CERROR("Can't query IPoIB interface %s: it's down\n", ifname
);
2403 LIBCFS_ALLOC(dev
, sizeof(*dev
));
2407 netdev
= dev_get_by_name(&init_net
, ifname
);
2408 if (netdev
== NULL
) {
2409 dev
->ibd_can_failover
= 0;
2411 dev
->ibd_can_failover
= !!(netdev
->flags
& IFF_MASTER
);
2415 INIT_LIST_HEAD(&dev
->ibd_nets
);
2416 INIT_LIST_HEAD(&dev
->ibd_list
); /* not yet in kib_devs */
2417 INIT_LIST_HEAD(&dev
->ibd_fail_list
);
2419 strcpy(&dev
->ibd_ifname
[0], ifname
);
2421 /* initialize the device */
2422 rc
= kiblnd_dev_failover(dev
);
2424 CERROR("Can't initialize device: %d\n", rc
);
2425 LIBCFS_FREE(dev
, sizeof(*dev
));
2429 list_add_tail(&dev
->ibd_list
, &kiblnd_data
.kib_devs
);
2433 static void kiblnd_base_shutdown(void)
2435 struct kib_sched_info
*sched
;
2438 LASSERT(list_empty(&kiblnd_data
.kib_devs
));
2440 switch (kiblnd_data
.kib_init
) {
2444 case IBLND_INIT_ALL
:
2445 case IBLND_INIT_DATA
:
2446 LASSERT(kiblnd_data
.kib_peers
!= NULL
);
2447 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++)
2448 LASSERT(list_empty(&kiblnd_data
.kib_peers
[i
]));
2449 LASSERT(list_empty(&kiblnd_data
.kib_connd_zombies
));
2450 LASSERT(list_empty(&kiblnd_data
.kib_connd_conns
));
2452 /* flag threads to terminate; wake and wait for them to die */
2453 kiblnd_data
.kib_shutdown
= 1;
2456 * NB: we really want to stop scheduler threads net by net
2457 * instead of the whole module, this should be improved
2458 * with dynamic configuration LNet
2460 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
)
2461 wake_up_all(&sched
->ibs_waitq
);
2463 wake_up_all(&kiblnd_data
.kib_connd_waitq
);
2464 wake_up_all(&kiblnd_data
.kib_failover_waitq
);
2467 while (atomic_read(&kiblnd_data
.kib_nthreads
) != 0) {
2470 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
,
2471 "Waiting for %d threads to terminate\n",
2472 atomic_read(&kiblnd_data
.kib_nthreads
));
2473 set_current_state(TASK_UNINTERRUPTIBLE
);
2474 schedule_timeout(cfs_time_seconds(1));
2479 case IBLND_INIT_NOTHING
:
2483 if (kiblnd_data
.kib_peers
!= NULL
) {
2484 LIBCFS_FREE(kiblnd_data
.kib_peers
,
2485 sizeof(struct list_head
) *
2486 kiblnd_data
.kib_peer_hash_size
);
2489 if (kiblnd_data
.kib_scheds
!= NULL
)
2490 cfs_percpt_free(kiblnd_data
.kib_scheds
);
2492 kiblnd_data
.kib_init
= IBLND_INIT_NOTHING
;
2493 module_put(THIS_MODULE
);
2496 void kiblnd_shutdown(lnet_ni_t
*ni
)
2498 kib_net_t
*net
= ni
->ni_data
;
2499 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
2501 unsigned long flags
;
2503 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_ALL
);
2508 write_lock_irqsave(g_lock
, flags
);
2509 net
->ibn_shutdown
= 1;
2510 write_unlock_irqrestore(g_lock
, flags
);
2512 switch (net
->ibn_init
) {
2516 case IBLND_INIT_ALL
:
2517 /* nuke all existing peers within this net */
2518 kiblnd_del_peer(ni
, LNET_NID_ANY
);
2520 /* Wait for all peer state to clean up */
2522 while (atomic_read(&net
->ibn_npeers
) != 0) {
2524 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
, /* 2**n? */
2525 "%s: waiting for %d peers to disconnect\n",
2526 libcfs_nid2str(ni
->ni_nid
),
2527 atomic_read(&net
->ibn_npeers
));
2528 set_current_state(TASK_UNINTERRUPTIBLE
);
2529 schedule_timeout(cfs_time_seconds(1));
2532 kiblnd_net_fini_pools(net
);
2534 write_lock_irqsave(g_lock
, flags
);
2535 LASSERT(net
->ibn_dev
->ibd_nnets
> 0);
2536 net
->ibn_dev
->ibd_nnets
--;
2537 list_del(&net
->ibn_list
);
2538 write_unlock_irqrestore(g_lock
, flags
);
2542 case IBLND_INIT_NOTHING
:
2543 LASSERT(atomic_read(&net
->ibn_nconns
) == 0);
2545 if (net
->ibn_dev
!= NULL
&&
2546 net
->ibn_dev
->ibd_nnets
== 0)
2547 kiblnd_destroy_dev(net
->ibn_dev
);
2552 net
->ibn_init
= IBLND_INIT_NOTHING
;
2555 LIBCFS_FREE(net
, sizeof(*net
));
2558 if (list_empty(&kiblnd_data
.kib_devs
))
2559 kiblnd_base_shutdown();
2562 static int kiblnd_base_startup(void)
2564 struct kib_sched_info
*sched
;
2568 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
);
2570 try_module_get(THIS_MODULE
);
2571 /* zero pointers, flags etc */
2572 memset(&kiblnd_data
, 0, sizeof(kiblnd_data
));
2574 rwlock_init(&kiblnd_data
.kib_global_lock
);
2576 INIT_LIST_HEAD(&kiblnd_data
.kib_devs
);
2577 INIT_LIST_HEAD(&kiblnd_data
.kib_failed_devs
);
2579 kiblnd_data
.kib_peer_hash_size
= IBLND_PEER_HASH_SIZE
;
2580 LIBCFS_ALLOC(kiblnd_data
.kib_peers
,
2581 sizeof(struct list_head
) * kiblnd_data
.kib_peer_hash_size
);
2582 if (kiblnd_data
.kib_peers
== NULL
)
2584 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++)
2585 INIT_LIST_HEAD(&kiblnd_data
.kib_peers
[i
]);
2587 spin_lock_init(&kiblnd_data
.kib_connd_lock
);
2588 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_conns
);
2589 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_zombies
);
2590 init_waitqueue_head(&kiblnd_data
.kib_connd_waitq
);
2591 init_waitqueue_head(&kiblnd_data
.kib_failover_waitq
);
2593 kiblnd_data
.kib_scheds
= cfs_percpt_alloc(lnet_cpt_table(),
2595 if (kiblnd_data
.kib_scheds
== NULL
)
2598 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
) {
2601 spin_lock_init(&sched
->ibs_lock
);
2602 INIT_LIST_HEAD(&sched
->ibs_conns
);
2603 init_waitqueue_head(&sched
->ibs_waitq
);
2605 nthrs
= cfs_cpt_weight(lnet_cpt_table(), i
);
2606 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2607 nthrs
= min(nthrs
, *kiblnd_tunables
.kib_nscheds
);
2610 * max to half of CPUs, another half is reserved for
2611 * upper layer modules
2613 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2616 sched
->ibs_nthreads_max
= nthrs
;
2620 kiblnd_data
.kib_error_qpa
.qp_state
= IB_QPS_ERR
;
2622 /* lists/ptrs/locks initialised */
2623 kiblnd_data
.kib_init
= IBLND_INIT_DATA
;
2624 /*****************************************************/
2626 rc
= kiblnd_thread_start(kiblnd_connd
, NULL
, "kiblnd_connd");
2628 CERROR("Can't spawn o2iblnd connd: %d\n", rc
);
2632 if (*kiblnd_tunables
.kib_dev_failover
!= 0)
2633 rc
= kiblnd_thread_start(kiblnd_failover_thread
, NULL
,
2637 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc
);
2641 /* flag everything initialised */
2642 kiblnd_data
.kib_init
= IBLND_INIT_ALL
;
2643 /*****************************************************/
2648 kiblnd_base_shutdown();
2652 static int kiblnd_start_schedulers(struct kib_sched_info
*sched
)
2658 if (sched
->ibs_nthreads
== 0) {
2659 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2660 nthrs
= sched
->ibs_nthreads_max
;
2662 nthrs
= cfs_cpt_weight(lnet_cpt_table(),
2664 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2665 nthrs
= min(IBLND_N_SCHED_HIGH
, nthrs
);
2668 LASSERT(sched
->ibs_nthreads
<= sched
->ibs_nthreads_max
);
2669 /* increase one thread if there is new interface */
2670 nthrs
= sched
->ibs_nthreads
< sched
->ibs_nthreads_max
;
2673 for (i
= 0; i
< nthrs
; i
++) {
2677 id
= KIB_THREAD_ID(sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
);
2678 snprintf(name
, sizeof(name
), "kiblnd_sd_%02ld_%02ld",
2679 KIB_THREAD_CPT(id
), KIB_THREAD_TID(id
));
2680 rc
= kiblnd_thread_start(kiblnd_scheduler
, (void *)id
, name
);
2684 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2685 sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
, rc
);
2689 sched
->ibs_nthreads
+= i
;
2693 static int kiblnd_dev_start_threads(kib_dev_t
*dev
, int newdev
, __u32
*cpts
,
2700 for (i
= 0; i
< ncpts
; i
++) {
2701 struct kib_sched_info
*sched
;
2703 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2704 sched
= kiblnd_data
.kib_scheds
[cpt
];
2706 if (!newdev
&& sched
->ibs_nthreads
> 0)
2709 rc
= kiblnd_start_schedulers(kiblnd_data
.kib_scheds
[cpt
]);
2711 CERROR("Failed to start scheduler threads for %s\n",
2719 static kib_dev_t
*kiblnd_dev_search(char *ifname
)
2721 kib_dev_t
*alias
= NULL
;
2726 colon
= strchr(ifname
, ':');
2727 list_for_each_entry(dev
, &kiblnd_data
.kib_devs
, ibd_list
) {
2728 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2734 colon2
= strchr(dev
->ibd_ifname
, ':');
2740 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2751 int kiblnd_startup(lnet_ni_t
*ni
)
2754 kib_dev_t
*ibdev
= NULL
;
2756 struct timespec64 tv
;
2757 unsigned long flags
;
2761 LASSERT(ni
->ni_lnd
== &the_o2iblnd
);
2763 if (kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
) {
2764 rc
= kiblnd_base_startup();
2769 LIBCFS_ALLOC(net
, sizeof(*net
));
2774 ktime_get_real_ts64(&tv
);
2775 net
->ibn_incarnation
= tv
.tv_sec
* USEC_PER_SEC
+
2776 tv
.tv_nsec
/ NSEC_PER_USEC
;
2778 ni
->ni_peertimeout
= *kiblnd_tunables
.kib_peertimeout
;
2779 ni
->ni_maxtxcredits
= *kiblnd_tunables
.kib_credits
;
2780 ni
->ni_peertxcredits
= *kiblnd_tunables
.kib_peertxcredits
;
2781 ni
->ni_peerrtrcredits
= *kiblnd_tunables
.kib_peerrtrcredits
;
2783 if (ni
->ni_interfaces
[0] != NULL
) {
2784 /* Use the IPoIB interface specified in 'networks=' */
2786 CLASSERT(LNET_MAX_INTERFACES
> 1);
2787 if (ni
->ni_interfaces
[1] != NULL
) {
2788 CERROR("Multiple interfaces not supported\n");
2792 ifname
= ni
->ni_interfaces
[0];
2794 ifname
= *kiblnd_tunables
.kib_default_ipif
;
2797 if (strlen(ifname
) >= sizeof(ibdev
->ibd_ifname
)) {
2798 CERROR("IPoIB interface name too long: %s\n", ifname
);
2802 ibdev
= kiblnd_dev_search(ifname
);
2804 newdev
= ibdev
== NULL
;
2805 /* hmm...create kib_dev even for alias */
2806 if (ibdev
== NULL
|| strcmp(&ibdev
->ibd_ifname
[0], ifname
) != 0)
2807 ibdev
= kiblnd_create_dev(ifname
);
2812 net
->ibn_dev
= ibdev
;
2813 ni
->ni_nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
), ibdev
->ibd_ifip
);
2815 rc
= kiblnd_dev_start_threads(ibdev
, newdev
,
2816 ni
->ni_cpts
, ni
->ni_ncpts
);
2820 rc
= kiblnd_net_init_pools(net
, ni
->ni_cpts
, ni
->ni_ncpts
);
2822 CERROR("Failed to initialize NI pools: %d\n", rc
);
2826 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2828 list_add_tail(&net
->ibn_list
, &ibdev
->ibd_nets
);
2829 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2831 net
->ibn_init
= IBLND_INIT_ALL
;
2836 if (net
->ibn_dev
== NULL
&& ibdev
!= NULL
)
2837 kiblnd_destroy_dev(ibdev
);
2840 kiblnd_shutdown(ni
);
2842 CDEBUG(D_NET
, "kiblnd_startup failed\n");
2846 static void __exit
kiblnd_module_fini(void)
2848 lnet_unregister_lnd(&the_o2iblnd
);
2851 static int __init
kiblnd_module_init(void)
2855 CLASSERT(sizeof(kib_msg_t
) <= IBLND_MSG_SIZE
);
2856 CLASSERT(offsetof(kib_msg_t
,
2857 ibm_u
.get
.ibgm_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2859 CLASSERT(offsetof(kib_msg_t
,
2860 ibm_u
.putack
.ibpam_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2863 rc
= kiblnd_tunables_init();
2867 lnet_register_lnd(&the_o2iblnd
);
2872 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2873 MODULE_DESCRIPTION("Kernel OpenIB gen2 LND v2.00");
2874 MODULE_LICENSE("GPL");
2876 module_init(kiblnd_module_init
);
2877 module_exit(kiblnd_module_fini
);