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
;
192 /* CAVEAT EMPTOR! all message fields not set here should have been
193 * initialised previously. */
194 msg
->ibm_magic
= IBLND_MSG_MAGIC
;
195 msg
->ibm_version
= version
;
197 msg
->ibm_credits
= credits
;
200 msg
->ibm_srcnid
= ni
->ni_nid
;
201 msg
->ibm_srcstamp
= net
->ibn_incarnation
;
202 msg
->ibm_dstnid
= dstnid
;
203 msg
->ibm_dststamp
= dststamp
;
205 if (*kiblnd_tunables
.kib_cksum
) {
206 /* NB ibm_cksum zero while computing cksum */
207 msg
->ibm_cksum
= kiblnd_cksum(msg
, msg
->ibm_nob
);
211 int kiblnd_unpack_msg(kib_msg_t
*msg
, int nob
)
213 const int hdr_size
= offsetof(kib_msg_t
, ibm_u
);
219 /* 6 bytes are enough to have received magic + version */
221 CERROR("Short message: %d\n", nob
);
225 if (msg
->ibm_magic
== IBLND_MSG_MAGIC
) {
227 } else if (msg
->ibm_magic
== __swab32(IBLND_MSG_MAGIC
)) {
230 CERROR("Bad magic: %08x\n", msg
->ibm_magic
);
234 version
= flip
? __swab16(msg
->ibm_version
) : msg
->ibm_version
;
235 if (version
!= IBLND_MSG_VERSION
&&
236 version
!= IBLND_MSG_VERSION_1
) {
237 CERROR("Bad version: %x\n", version
);
241 if (nob
< hdr_size
) {
242 CERROR("Short message: %d\n", nob
);
246 msg_nob
= flip
? __swab32(msg
->ibm_nob
) : msg
->ibm_nob
;
248 CERROR("Short message: got %d, wanted %d\n", nob
, msg_nob
);
252 /* checksum must be computed with ibm_cksum zero and BEFORE anything
254 msg_cksum
= flip
? __swab32(msg
->ibm_cksum
) : msg
->ibm_cksum
;
256 if (msg_cksum
!= 0 &&
257 msg_cksum
!= kiblnd_cksum(msg
, msg_nob
)) {
258 CERROR("Bad checksum\n");
262 msg
->ibm_cksum
= msg_cksum
;
265 /* leave magic unflipped as a clue to peer endianness */
266 msg
->ibm_version
= version
;
267 CLASSERT(sizeof(msg
->ibm_type
) == 1);
268 CLASSERT(sizeof(msg
->ibm_credits
) == 1);
269 msg
->ibm_nob
= msg_nob
;
270 __swab64s(&msg
->ibm_srcnid
);
271 __swab64s(&msg
->ibm_srcstamp
);
272 __swab64s(&msg
->ibm_dstnid
);
273 __swab64s(&msg
->ibm_dststamp
);
276 if (msg
->ibm_srcnid
== LNET_NID_ANY
) {
277 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg
->ibm_srcnid
));
281 if (msg_nob
< kiblnd_msgtype2size(msg
->ibm_type
)) {
282 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg
->ibm_type
),
283 msg_nob
, kiblnd_msgtype2size(msg
->ibm_type
));
287 switch (msg
->ibm_type
) {
289 CERROR("Unknown message type %x\n", msg
->ibm_type
);
293 case IBLND_MSG_IMMEDIATE
:
294 case IBLND_MSG_PUT_REQ
:
297 case IBLND_MSG_PUT_ACK
:
298 case IBLND_MSG_GET_REQ
:
299 if (kiblnd_unpack_rd(msg
, flip
))
303 case IBLND_MSG_PUT_NAK
:
304 case IBLND_MSG_PUT_DONE
:
305 case IBLND_MSG_GET_DONE
:
307 __swab32s(&msg
->ibm_u
.completion
.ibcm_status
);
310 case IBLND_MSG_CONNREQ
:
311 case IBLND_MSG_CONNACK
:
313 __swab16s(&msg
->ibm_u
.connparams
.ibcp_queue_depth
);
314 __swab16s(&msg
->ibm_u
.connparams
.ibcp_max_frags
);
315 __swab32s(&msg
->ibm_u
.connparams
.ibcp_max_msg_size
);
322 int kiblnd_create_peer(lnet_ni_t
*ni
, kib_peer_t
**peerp
, lnet_nid_t nid
)
325 kib_net_t
*net
= ni
->ni_data
;
326 int cpt
= lnet_cpt_of_nid(nid
);
329 LASSERT(net
!= NULL
);
330 LASSERT(nid
!= LNET_NID_ANY
);
332 LIBCFS_CPT_ALLOC(peer
, lnet_cpt_table(), cpt
, sizeof(*peer
));
334 CERROR("Cannot allocate peer\n");
338 memset(peer
, 0, sizeof(*peer
)); /* zero flags etc */
343 peer
->ibp_last_alive
= 0;
344 atomic_set(&peer
->ibp_refcount
, 1); /* 1 ref for caller */
346 INIT_LIST_HEAD(&peer
->ibp_list
); /* not in the peer table yet */
347 INIT_LIST_HEAD(&peer
->ibp_conns
);
348 INIT_LIST_HEAD(&peer
->ibp_tx_queue
);
350 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
352 /* always called with a ref on ni, which prevents ni being shutdown */
353 LASSERT(net
->ibn_shutdown
== 0);
355 /* npeers only grows with the global lock held */
356 atomic_inc(&net
->ibn_npeers
);
358 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
364 void kiblnd_destroy_peer(kib_peer_t
*peer
)
366 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
368 LASSERT(net
!= NULL
);
369 LASSERT(atomic_read(&peer
->ibp_refcount
) == 0);
370 LASSERT(!kiblnd_peer_active(peer
));
371 LASSERT(peer
->ibp_connecting
== 0);
372 LASSERT(peer
->ibp_accepting
== 0);
373 LASSERT(list_empty(&peer
->ibp_conns
));
374 LASSERT(list_empty(&peer
->ibp_tx_queue
));
376 LIBCFS_FREE(peer
, sizeof(*peer
));
378 /* NB a peer's connections keep a reference on their peer until
379 * they are destroyed, so we can be assured that _all_ state to do
380 * with this peer has been cleaned up when its refcount drops to
382 atomic_dec(&net
->ibn_npeers
);
385 kib_peer_t
*kiblnd_find_peer_locked(lnet_nid_t nid
)
387 /* the caller is responsible for accounting the additional reference
388 * that this creates */
389 struct list_head
*peer_list
= kiblnd_nid2peerlist(nid
);
390 struct list_head
*tmp
;
393 list_for_each(tmp
, peer_list
) {
395 peer
= list_entry(tmp
, kib_peer_t
, ibp_list
);
397 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
398 peer
->ibp_accepting
> 0 ||
399 !list_empty(&peer
->ibp_conns
)); /* active conn */
401 if (peer
->ibp_nid
!= nid
)
404 CDEBUG(D_NET
, "got peer [%p] -> %s (%d) version: %x\n",
405 peer
, libcfs_nid2str(nid
),
406 atomic_read(&peer
->ibp_refcount
),
413 void kiblnd_unlink_peer_locked(kib_peer_t
*peer
)
415 LASSERT(list_empty(&peer
->ibp_conns
));
417 LASSERT(kiblnd_peer_active(peer
));
418 list_del_init(&peer
->ibp_list
);
419 /* lose peerlist's ref */
420 kiblnd_peer_decref(peer
);
423 static int kiblnd_get_peer_info(lnet_ni_t
*ni
, int index
,
424 lnet_nid_t
*nidp
, int *count
)
427 struct list_head
*ptmp
;
431 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
433 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
435 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
437 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
438 LASSERT(peer
->ibp_connecting
> 0 ||
439 peer
->ibp_accepting
> 0 ||
440 !list_empty(&peer
->ibp_conns
));
442 if (peer
->ibp_ni
!= ni
)
448 *nidp
= peer
->ibp_nid
;
449 *count
= atomic_read(&peer
->ibp_refcount
);
451 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
,
457 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
461 static void kiblnd_del_peer_locked(kib_peer_t
*peer
)
463 struct list_head
*ctmp
;
464 struct list_head
*cnxt
;
467 if (list_empty(&peer
->ibp_conns
)) {
468 kiblnd_unlink_peer_locked(peer
);
470 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
471 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
473 kiblnd_close_conn_locked(conn
, 0);
475 /* NB closing peer's last conn unlinked it. */
477 /* NB peer now unlinked; might even be freed if the peer table had the
481 static int kiblnd_del_peer(lnet_ni_t
*ni
, lnet_nid_t nid
)
484 struct list_head
*ptmp
;
485 struct list_head
*pnxt
;
493 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
495 if (nid
!= LNET_NID_ANY
) {
496 lo
= hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
499 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
502 for (i
= lo
; i
<= hi
; i
++) {
503 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
504 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
505 LASSERT(peer
->ibp_connecting
> 0 ||
506 peer
->ibp_accepting
> 0 ||
507 !list_empty(&peer
->ibp_conns
));
509 if (peer
->ibp_ni
!= ni
)
512 if (!(nid
== LNET_NID_ANY
|| peer
->ibp_nid
== nid
))
515 if (!list_empty(&peer
->ibp_tx_queue
)) {
516 LASSERT(list_empty(&peer
->ibp_conns
));
518 list_splice_init(&peer
->ibp_tx_queue
,
522 kiblnd_del_peer_locked(peer
);
523 rc
= 0; /* matched something */
527 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
529 kiblnd_txlist_done(ni
, &zombies
, -EIO
);
534 static kib_conn_t
*kiblnd_get_conn_by_idx(lnet_ni_t
*ni
, int index
)
537 struct list_head
*ptmp
;
539 struct list_head
*ctmp
;
543 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
545 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
546 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
548 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
549 LASSERT(peer
->ibp_connecting
> 0 ||
550 peer
->ibp_accepting
> 0 ||
551 !list_empty(&peer
->ibp_conns
));
553 if (peer
->ibp_ni
!= ni
)
556 list_for_each(ctmp
, &peer
->ibp_conns
) {
560 conn
= list_entry(ctmp
, kib_conn_t
,
562 kiblnd_conn_addref(conn
);
563 read_unlock_irqrestore(
564 &kiblnd_data
.kib_global_lock
,
571 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
575 int kiblnd_translate_mtu(int value
)
595 static void kiblnd_setup_mtu_locked(struct rdma_cm_id
*cmid
)
599 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
600 if (cmid
->route
.path_rec
== NULL
)
603 mtu
= kiblnd_translate_mtu(*kiblnd_tunables
.kib_ib_mtu
);
606 cmid
->route
.path_rec
->mtu
= mtu
;
609 static int kiblnd_get_completion_vector(kib_conn_t
*conn
, int cpt
)
615 lnet_nid_t nid
= conn
->ibc_peer
->ibp_nid
;
617 vectors
= conn
->ibc_cmid
->device
->num_comp_vectors
;
621 mask
= cfs_cpt_cpumask(lnet_cpt_table(), cpt
);
625 /* hash NID to CPU id in this partition... */
626 off
= do_div(nid
, cpumask_weight(mask
));
627 for_each_cpu(i
, mask
) {
636 kib_conn_t
*kiblnd_create_conn(kib_peer_t
*peer
, struct rdma_cm_id
*cmid
,
637 int state
, int version
)
640 * If the new conn is created successfully it takes over the caller's
641 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
642 * is destroyed. On failure, the caller's ref on 'peer' remains and
643 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
644 * to destroy 'cmid' here since I'm called from the CM which still has
645 * its ref on 'cmid'). */
646 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
647 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
649 struct ib_qp_init_attr
*init_qp_attr
;
650 struct kib_sched_info
*sched
;
651 struct ib_cq_init_attr cq_attr
= {};
659 LASSERT(net
!= NULL
);
660 LASSERT(!in_interrupt());
664 cpt
= lnet_cpt_of_nid(peer
->ibp_nid
);
665 sched
= kiblnd_data
.kib_scheds
[cpt
];
667 LASSERT(sched
->ibs_nthreads
> 0);
669 LIBCFS_CPT_ALLOC(init_qp_attr
, lnet_cpt_table(), cpt
,
670 sizeof(*init_qp_attr
));
671 if (init_qp_attr
== NULL
) {
672 CERROR("Can't allocate qp_attr for %s\n",
673 libcfs_nid2str(peer
->ibp_nid
));
677 LIBCFS_CPT_ALLOC(conn
, lnet_cpt_table(), cpt
, sizeof(*conn
));
679 CERROR("Can't allocate connection for %s\n",
680 libcfs_nid2str(peer
->ibp_nid
));
684 conn
->ibc_state
= IBLND_CONN_INIT
;
685 conn
->ibc_version
= version
;
686 conn
->ibc_peer
= peer
; /* I take the caller's ref */
687 cmid
->context
= conn
; /* for future CM callbacks */
688 conn
->ibc_cmid
= cmid
;
690 INIT_LIST_HEAD(&conn
->ibc_early_rxs
);
691 INIT_LIST_HEAD(&conn
->ibc_tx_noops
);
692 INIT_LIST_HEAD(&conn
->ibc_tx_queue
);
693 INIT_LIST_HEAD(&conn
->ibc_tx_queue_rsrvd
);
694 INIT_LIST_HEAD(&conn
->ibc_tx_queue_nocred
);
695 INIT_LIST_HEAD(&conn
->ibc_active_txs
);
696 spin_lock_init(&conn
->ibc_lock
);
698 LIBCFS_CPT_ALLOC(conn
->ibc_connvars
, lnet_cpt_table(), cpt
,
699 sizeof(*conn
->ibc_connvars
));
700 if (conn
->ibc_connvars
== NULL
) {
701 CERROR("Can't allocate in-progress connection state\n");
705 write_lock_irqsave(glock
, flags
);
706 if (dev
->ibd_failover
) {
707 write_unlock_irqrestore(glock
, flags
);
708 CERROR("%s: failover in progress\n", dev
->ibd_ifname
);
712 if (dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
) {
713 /* wakeup failover thread and teardown connection */
714 if (kiblnd_dev_can_failover(dev
)) {
715 list_add_tail(&dev
->ibd_fail_list
,
716 &kiblnd_data
.kib_failed_devs
);
717 wake_up(&kiblnd_data
.kib_failover_waitq
);
720 write_unlock_irqrestore(glock
, flags
);
721 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
722 cmid
->device
->name
, dev
->ibd_ifname
);
726 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
727 conn
->ibc_hdev
= dev
->ibd_hdev
;
729 kiblnd_setup_mtu_locked(cmid
);
731 write_unlock_irqrestore(glock
, flags
);
733 LIBCFS_CPT_ALLOC(conn
->ibc_rxs
, lnet_cpt_table(), cpt
,
734 IBLND_RX_MSGS(version
) * sizeof(kib_rx_t
));
735 if (conn
->ibc_rxs
== NULL
) {
736 CERROR("Cannot allocate RX buffers\n");
740 rc
= kiblnd_alloc_pages(&conn
->ibc_rx_pages
, cpt
,
741 IBLND_RX_MSG_PAGES(version
));
745 kiblnd_map_rx_descs(conn
);
747 cq_attr
.cqe
= IBLND_CQ_ENTRIES(version
);
748 cq_attr
.comp_vector
= kiblnd_get_completion_vector(conn
, cpt
);
749 cq
= ib_create_cq(cmid
->device
,
750 kiblnd_cq_completion
, kiblnd_cq_event
, conn
,
753 CERROR("Can't create CQ: %ld, cqe: %d\n",
754 PTR_ERR(cq
), IBLND_CQ_ENTRIES(version
));
760 rc
= ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
762 CERROR("Can't request completion notificiation: %d\n", rc
);
766 init_qp_attr
->event_handler
= kiblnd_qp_event
;
767 init_qp_attr
->qp_context
= conn
;
768 init_qp_attr
->cap
.max_send_wr
= IBLND_SEND_WRS(version
);
769 init_qp_attr
->cap
.max_recv_wr
= IBLND_RECV_WRS(version
);
770 init_qp_attr
->cap
.max_send_sge
= 1;
771 init_qp_attr
->cap
.max_recv_sge
= 1;
772 init_qp_attr
->sq_sig_type
= IB_SIGNAL_REQ_WR
;
773 init_qp_attr
->qp_type
= IB_QPT_RC
;
774 init_qp_attr
->send_cq
= cq
;
775 init_qp_attr
->recv_cq
= cq
;
777 conn
->ibc_sched
= sched
;
779 rc
= rdma_create_qp(cmid
, conn
->ibc_hdev
->ibh_pd
, init_qp_attr
);
781 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
782 rc
, init_qp_attr
->cap
.max_send_wr
,
783 init_qp_attr
->cap
.max_recv_wr
);
787 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
789 /* 1 ref for caller and each rxmsg */
790 atomic_set(&conn
->ibc_refcount
, 1 + IBLND_RX_MSGS(version
));
791 conn
->ibc_nrx
= IBLND_RX_MSGS(version
);
794 for (i
= 0; i
< IBLND_RX_MSGS(version
); i
++) {
795 rc
= kiblnd_post_rx(&conn
->ibc_rxs
[i
],
796 IBLND_POSTRX_NO_CREDIT
);
798 CERROR("Can't post rxmsg: %d\n", rc
);
800 /* Make posted receives complete */
801 kiblnd_abort_receives(conn
);
803 /* correct # of posted buffers
804 * NB locking needed now I'm racing with completion */
805 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
806 conn
->ibc_nrx
-= IBLND_RX_MSGS(version
) - i
;
807 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
809 /* cmid will be destroyed by CM(ofed) after cm_callback
810 * returned, so we can't refer it anymore
811 * (by kiblnd_connd()->kiblnd_destroy_conn) */
812 rdma_destroy_qp(conn
->ibc_cmid
);
813 conn
->ibc_cmid
= NULL
;
815 /* Drop my own and unused rxbuffer refcounts */
816 while (i
++ <= IBLND_RX_MSGS(version
))
817 kiblnd_conn_decref(conn
);
823 /* Init successful! */
824 LASSERT(state
== IBLND_CONN_ACTIVE_CONNECT
||
825 state
== IBLND_CONN_PASSIVE_WAIT
);
826 conn
->ibc_state
= state
;
829 atomic_inc(&net
->ibn_nconns
);
833 kiblnd_destroy_conn(conn
);
835 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
840 void kiblnd_destroy_conn(kib_conn_t
*conn
)
842 struct rdma_cm_id
*cmid
= conn
->ibc_cmid
;
843 kib_peer_t
*peer
= conn
->ibc_peer
;
846 LASSERT(!in_interrupt());
847 LASSERT(atomic_read(&conn
->ibc_refcount
) == 0);
848 LASSERT(list_empty(&conn
->ibc_early_rxs
));
849 LASSERT(list_empty(&conn
->ibc_tx_noops
));
850 LASSERT(list_empty(&conn
->ibc_tx_queue
));
851 LASSERT(list_empty(&conn
->ibc_tx_queue_rsrvd
));
852 LASSERT(list_empty(&conn
->ibc_tx_queue_nocred
));
853 LASSERT(list_empty(&conn
->ibc_active_txs
));
854 LASSERT(conn
->ibc_noops_posted
== 0);
855 LASSERT(conn
->ibc_nsends_posted
== 0);
857 switch (conn
->ibc_state
) {
859 /* conn must be completely disengaged from the network */
862 case IBLND_CONN_DISCONNECTED
:
863 /* connvars should have been freed already */
864 LASSERT(conn
->ibc_connvars
== NULL
);
867 case IBLND_CONN_INIT
:
871 /* conn->ibc_cmid might be destroyed by CM already */
872 if (cmid
!= NULL
&& cmid
->qp
!= NULL
)
873 rdma_destroy_qp(cmid
);
875 if (conn
->ibc_cq
!= NULL
) {
876 rc
= ib_destroy_cq(conn
->ibc_cq
);
878 CWARN("Error destroying CQ: %d\n", rc
);
881 if (conn
->ibc_rx_pages
!= NULL
)
882 kiblnd_unmap_rx_descs(conn
);
884 if (conn
->ibc_rxs
!= NULL
) {
885 LIBCFS_FREE(conn
->ibc_rxs
,
886 IBLND_RX_MSGS(conn
->ibc_version
)
890 if (conn
->ibc_connvars
!= NULL
)
891 LIBCFS_FREE(conn
->ibc_connvars
, sizeof(*conn
->ibc_connvars
));
893 if (conn
->ibc_hdev
!= NULL
)
894 kiblnd_hdev_decref(conn
->ibc_hdev
);
896 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
897 if (conn
->ibc_state
!= IBLND_CONN_INIT
) {
898 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
900 kiblnd_peer_decref(peer
);
901 rdma_destroy_id(cmid
);
902 atomic_dec(&net
->ibn_nconns
);
905 LIBCFS_FREE(conn
, sizeof(*conn
));
908 int kiblnd_close_peer_conns_locked(kib_peer_t
*peer
, int why
)
911 struct list_head
*ctmp
;
912 struct list_head
*cnxt
;
915 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
916 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
918 CDEBUG(D_NET
, "Closing conn -> %s, version: %x, reason: %d\n",
919 libcfs_nid2str(peer
->ibp_nid
),
920 conn
->ibc_version
, why
);
922 kiblnd_close_conn_locked(conn
, why
);
929 int kiblnd_close_stale_conns_locked(kib_peer_t
*peer
,
930 int version
, __u64 incarnation
)
933 struct list_head
*ctmp
;
934 struct list_head
*cnxt
;
937 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
938 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
940 if (conn
->ibc_version
== version
&&
941 conn
->ibc_incarnation
== incarnation
)
945 "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n",
946 libcfs_nid2str(peer
->ibp_nid
),
947 conn
->ibc_version
, conn
->ibc_incarnation
,
948 version
, incarnation
);
950 kiblnd_close_conn_locked(conn
, -ESTALE
);
957 static int kiblnd_close_matching_conns(lnet_ni_t
*ni
, lnet_nid_t nid
)
960 struct list_head
*ptmp
;
961 struct list_head
*pnxt
;
968 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
970 if (nid
!= LNET_NID_ANY
)
971 lo
= hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
974 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
977 for (i
= lo
; i
<= hi
; i
++) {
978 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
980 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
981 LASSERT(peer
->ibp_connecting
> 0 ||
982 peer
->ibp_accepting
> 0 ||
983 !list_empty(&peer
->ibp_conns
));
985 if (peer
->ibp_ni
!= ni
)
988 if (!(nid
== LNET_NID_ANY
|| nid
== peer
->ibp_nid
))
991 count
+= kiblnd_close_peer_conns_locked(peer
, 0);
995 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
997 /* wildcards always succeed */
998 if (nid
== LNET_NID_ANY
)
1001 return (count
== 0) ? -ENOENT
: 0;
1004 int kiblnd_ctl(lnet_ni_t
*ni
, unsigned int cmd
, void *arg
)
1006 struct libcfs_ioctl_data
*data
= arg
;
1010 case IOC_LIBCFS_GET_PEER
: {
1014 rc
= kiblnd_get_peer_info(ni
, data
->ioc_count
,
1016 data
->ioc_nid
= nid
;
1017 data
->ioc_count
= count
;
1021 case IOC_LIBCFS_DEL_PEER
: {
1022 rc
= kiblnd_del_peer(ni
, data
->ioc_nid
);
1025 case IOC_LIBCFS_GET_CONN
: {
1029 conn
= kiblnd_get_conn_by_idx(ni
, data
->ioc_count
);
1035 LASSERT(conn
->ibc_cmid
!= NULL
);
1036 data
->ioc_nid
= conn
->ibc_peer
->ibp_nid
;
1037 if (conn
->ibc_cmid
->route
.path_rec
== NULL
)
1038 data
->ioc_u32
[0] = 0; /* iWarp has no path MTU */
1041 ib_mtu_enum_to_int(conn
->ibc_cmid
->route
.path_rec
->mtu
);
1042 kiblnd_conn_decref(conn
);
1045 case IOC_LIBCFS_CLOSE_CONNECTION
: {
1046 rc
= kiblnd_close_matching_conns(ni
, data
->ioc_nid
);
1057 void kiblnd_query(lnet_ni_t
*ni
, lnet_nid_t nid
, unsigned long *when
)
1059 unsigned long last_alive
= 0;
1060 unsigned long now
= cfs_time_current();
1061 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
1063 unsigned long flags
;
1065 read_lock_irqsave(glock
, flags
);
1067 peer
= kiblnd_find_peer_locked(nid
);
1069 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
1070 peer
->ibp_accepting
> 0 ||
1071 !list_empty(&peer
->ibp_conns
)); /* active conn */
1072 last_alive
= peer
->ibp_last_alive
;
1075 read_unlock_irqrestore(glock
, flags
);
1077 if (last_alive
!= 0)
1080 /* peer is not persistent in hash, trigger peer creation
1081 * and connection establishment with a NULL tx */
1083 kiblnd_launch_tx(ni
, NULL
, nid
);
1085 CDEBUG(D_NET
, "Peer %s %p, alive %ld secs ago\n",
1086 libcfs_nid2str(nid
), peer
,
1087 last_alive
? cfs_duration_sec(now
- last_alive
) : -1);
1090 void kiblnd_free_pages(kib_pages_t
*p
)
1092 int npages
= p
->ibp_npages
;
1095 for (i
= 0; i
< npages
; i
++) {
1096 if (p
->ibp_pages
[i
] != NULL
)
1097 __free_page(p
->ibp_pages
[i
]);
1100 LIBCFS_FREE(p
, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1103 int kiblnd_alloc_pages(kib_pages_t
**pp
, int cpt
, int npages
)
1108 LIBCFS_CPT_ALLOC(p
, lnet_cpt_table(), cpt
,
1109 offsetof(kib_pages_t
, ibp_pages
[npages
]));
1111 CERROR("Can't allocate descriptor for %d pages\n", npages
);
1115 memset(p
, 0, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1116 p
->ibp_npages
= npages
;
1118 for (i
= 0; i
< npages
; i
++) {
1119 p
->ibp_pages
[i
] = alloc_pages_node(
1120 cfs_cpt_spread_node(lnet_cpt_table(), cpt
),
1122 if (p
->ibp_pages
[i
] == NULL
) {
1123 CERROR("Can't allocate page %d of %d\n", i
, npages
);
1124 kiblnd_free_pages(p
);
1133 void kiblnd_unmap_rx_descs(kib_conn_t
*conn
)
1138 LASSERT(conn
->ibc_rxs
!= NULL
);
1139 LASSERT(conn
->ibc_hdev
!= NULL
);
1141 for (i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1142 rx
= &conn
->ibc_rxs
[i
];
1144 LASSERT(rx
->rx_nob
>= 0); /* not posted */
1146 kiblnd_dma_unmap_single(conn
->ibc_hdev
->ibh_ibdev
,
1147 KIBLND_UNMAP_ADDR(rx
, rx_msgunmap
,
1149 IBLND_MSG_SIZE
, DMA_FROM_DEVICE
);
1152 kiblnd_free_pages(conn
->ibc_rx_pages
);
1154 conn
->ibc_rx_pages
= NULL
;
1157 void kiblnd_map_rx_descs(kib_conn_t
*conn
)
1165 for (pg_off
= ipg
= i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1166 pg
= conn
->ibc_rx_pages
->ibp_pages
[ipg
];
1167 rx
= &conn
->ibc_rxs
[i
];
1170 rx
->rx_msg
= (kib_msg_t
*)(((char *)page_address(pg
)) + pg_off
);
1172 rx
->rx_msgaddr
= kiblnd_dma_map_single(conn
->ibc_hdev
->ibh_ibdev
,
1176 LASSERT(!kiblnd_dma_mapping_error(conn
->ibc_hdev
->ibh_ibdev
,
1178 KIBLND_UNMAP_ADDR_SET(rx
, rx_msgunmap
, rx
->rx_msgaddr
);
1180 CDEBUG(D_NET
, "rx %d: %p %#llx(%#llx)\n",
1181 i
, rx
->rx_msg
, rx
->rx_msgaddr
,
1182 (__u64
)(page_to_phys(pg
) + pg_off
));
1184 pg_off
+= IBLND_MSG_SIZE
;
1185 LASSERT(pg_off
<= PAGE_SIZE
);
1187 if (pg_off
== PAGE_SIZE
) {
1190 LASSERT(ipg
<= IBLND_RX_MSG_PAGES(conn
->ibc_version
));
1195 static void kiblnd_unmap_tx_pool(kib_tx_pool_t
*tpo
)
1197 kib_hca_dev_t
*hdev
= tpo
->tpo_hdev
;
1201 LASSERT(tpo
->tpo_pool
.po_allocated
== 0);
1206 for (i
= 0; i
< tpo
->tpo_pool
.po_size
; i
++) {
1207 tx
= &tpo
->tpo_tx_descs
[i
];
1208 kiblnd_dma_unmap_single(hdev
->ibh_ibdev
,
1209 KIBLND_UNMAP_ADDR(tx
, tx_msgunmap
,
1211 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1214 kiblnd_hdev_decref(hdev
);
1215 tpo
->tpo_hdev
= NULL
;
1218 static kib_hca_dev_t
*kiblnd_current_hdev(kib_dev_t
*dev
)
1220 kib_hca_dev_t
*hdev
;
1221 unsigned long flags
;
1224 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1225 while (dev
->ibd_failover
) {
1226 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1228 CDEBUG(D_NET
, "%s: Wait for failover\n",
1230 schedule_timeout(cfs_time_seconds(1) / 100);
1232 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1235 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
1236 hdev
= dev
->ibd_hdev
;
1238 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1243 static void kiblnd_map_tx_pool(kib_tx_pool_t
*tpo
)
1245 kib_pages_t
*txpgs
= tpo
->tpo_tx_pages
;
1246 kib_pool_t
*pool
= &tpo
->tpo_pool
;
1247 kib_net_t
*net
= pool
->po_owner
->ps_net
;
1255 LASSERT(net
!= NULL
);
1259 /* pre-mapped messages are not bigger than 1 page */
1260 CLASSERT(IBLND_MSG_SIZE
<= PAGE_SIZE
);
1262 /* No fancy arithmetic when we do the buffer calculations */
1263 CLASSERT(PAGE_SIZE
% IBLND_MSG_SIZE
== 0);
1265 tpo
->tpo_hdev
= kiblnd_current_hdev(dev
);
1267 for (ipage
= page_offset
= i
= 0; i
< pool
->po_size
; i
++) {
1268 page
= txpgs
->ibp_pages
[ipage
];
1269 tx
= &tpo
->tpo_tx_descs
[i
];
1271 tx
->tx_msg
= (kib_msg_t
*)(((char *)page_address(page
)) +
1274 tx
->tx_msgaddr
= kiblnd_dma_map_single(
1275 tpo
->tpo_hdev
->ibh_ibdev
, tx
->tx_msg
,
1276 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1277 LASSERT(!kiblnd_dma_mapping_error(tpo
->tpo_hdev
->ibh_ibdev
,
1279 KIBLND_UNMAP_ADDR_SET(tx
, tx_msgunmap
, tx
->tx_msgaddr
);
1281 list_add(&tx
->tx_list
, &pool
->po_free_list
);
1283 page_offset
+= IBLND_MSG_SIZE
;
1284 LASSERT(page_offset
<= PAGE_SIZE
);
1286 if (page_offset
== PAGE_SIZE
) {
1289 LASSERT(ipage
<= txpgs
->ibp_npages
);
1294 struct ib_mr
*kiblnd_find_dma_mr(kib_hca_dev_t
*hdev
, __u64 addr
, __u64 size
)
1298 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1300 if (hdev
->ibh_nmrs
== 1)
1301 return hdev
->ibh_mrs
[0];
1303 index
= addr
>> hdev
->ibh_mr_shift
;
1305 if (index
< hdev
->ibh_nmrs
&&
1306 index
== ((addr
+ size
- 1) >> hdev
->ibh_mr_shift
))
1307 return hdev
->ibh_mrs
[index
];
1312 struct ib_mr
*kiblnd_find_rd_dma_mr(kib_hca_dev_t
*hdev
, kib_rdma_desc_t
*rd
)
1314 struct ib_mr
*prev_mr
;
1318 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1320 if (*kiblnd_tunables
.kib_map_on_demand
> 0 &&
1321 *kiblnd_tunables
.kib_map_on_demand
<= rd
->rd_nfrags
)
1324 if (hdev
->ibh_nmrs
== 1)
1325 return hdev
->ibh_mrs
[0];
1327 for (i
= 0, mr
= prev_mr
= NULL
;
1328 i
< rd
->rd_nfrags
; i
++) {
1329 mr
= kiblnd_find_dma_mr(hdev
,
1330 rd
->rd_frags
[i
].rf_addr
,
1331 rd
->rd_frags
[i
].rf_nob
);
1332 if (prev_mr
== NULL
)
1335 if (mr
== NULL
|| prev_mr
!= mr
) {
1336 /* Can't covered by one single MR */
1345 static void kiblnd_destroy_fmr_pool(kib_fmr_pool_t
*pool
)
1347 LASSERT(pool
->fpo_map_count
== 0);
1349 if (pool
->fpo_fmr_pool
!= NULL
)
1350 ib_destroy_fmr_pool(pool
->fpo_fmr_pool
);
1352 if (pool
->fpo_hdev
!= NULL
)
1353 kiblnd_hdev_decref(pool
->fpo_hdev
);
1355 LIBCFS_FREE(pool
, sizeof(kib_fmr_pool_t
));
1358 static void kiblnd_destroy_fmr_pool_list(struct list_head
*head
)
1360 kib_fmr_pool_t
*pool
;
1362 while (!list_empty(head
)) {
1363 pool
= list_entry(head
->next
, kib_fmr_pool_t
, fpo_list
);
1364 list_del(&pool
->fpo_list
);
1365 kiblnd_destroy_fmr_pool(pool
);
1369 static int kiblnd_fmr_pool_size(int ncpts
)
1371 int size
= *kiblnd_tunables
.kib_fmr_pool_size
/ ncpts
;
1373 return max(IBLND_FMR_POOL
, size
);
1376 static int kiblnd_fmr_flush_trigger(int ncpts
)
1378 int size
= *kiblnd_tunables
.kib_fmr_flush_trigger
/ ncpts
;
1380 return max(IBLND_FMR_POOL_FLUSH
, size
);
1383 static int kiblnd_create_fmr_pool(kib_fmr_poolset_t
*fps
,
1384 kib_fmr_pool_t
**pp_fpo
)
1386 /* FMR pool for RDMA */
1387 kib_dev_t
*dev
= fps
->fps_net
->ibn_dev
;
1388 kib_fmr_pool_t
*fpo
;
1389 struct ib_fmr_pool_param param
= {
1390 .max_pages_per_fmr
= LNET_MAX_PAYLOAD
/PAGE_SIZE
,
1391 .page_shift
= PAGE_SHIFT
,
1392 .access
= (IB_ACCESS_LOCAL_WRITE
|
1393 IB_ACCESS_REMOTE_WRITE
),
1394 .pool_size
= fps
->fps_pool_size
,
1395 .dirty_watermark
= fps
->fps_flush_trigger
,
1396 .flush_function
= NULL
,
1398 .cache
= !!*kiblnd_tunables
.kib_fmr_cache
};
1401 LIBCFS_CPT_ALLOC(fpo
, lnet_cpt_table(), fps
->fps_cpt
, sizeof(*fpo
));
1405 fpo
->fpo_hdev
= kiblnd_current_hdev(dev
);
1407 fpo
->fpo_fmr_pool
= ib_create_fmr_pool(fpo
->fpo_hdev
->ibh_pd
, ¶m
);
1408 if (IS_ERR(fpo
->fpo_fmr_pool
)) {
1409 rc
= PTR_ERR(fpo
->fpo_fmr_pool
);
1410 CERROR("Failed to create FMR pool: %d\n", rc
);
1412 kiblnd_hdev_decref(fpo
->fpo_hdev
);
1413 LIBCFS_FREE(fpo
, sizeof(kib_fmr_pool_t
));
1417 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1418 fpo
->fpo_owner
= fps
;
1424 static void kiblnd_fail_fmr_poolset(kib_fmr_poolset_t
*fps
,
1425 struct list_head
*zombies
)
1427 if (fps
->fps_net
== NULL
) /* intialized? */
1430 spin_lock(&fps
->fps_lock
);
1432 while (!list_empty(&fps
->fps_pool_list
)) {
1433 kib_fmr_pool_t
*fpo
= list_entry(fps
->fps_pool_list
.next
,
1434 kib_fmr_pool_t
, fpo_list
);
1435 fpo
->fpo_failed
= 1;
1436 list_del(&fpo
->fpo_list
);
1437 if (fpo
->fpo_map_count
== 0)
1438 list_add(&fpo
->fpo_list
, zombies
);
1440 list_add(&fpo
->fpo_list
, &fps
->fps_failed_pool_list
);
1443 spin_unlock(&fps
->fps_lock
);
1446 static void kiblnd_fini_fmr_poolset(kib_fmr_poolset_t
*fps
)
1448 if (fps
->fps_net
!= NULL
) { /* initialized? */
1449 kiblnd_destroy_fmr_pool_list(&fps
->fps_failed_pool_list
);
1450 kiblnd_destroy_fmr_pool_list(&fps
->fps_pool_list
);
1454 static int kiblnd_init_fmr_poolset(kib_fmr_poolset_t
*fps
, int cpt
,
1455 kib_net_t
*net
, int pool_size
,
1458 kib_fmr_pool_t
*fpo
;
1461 memset(fps
, 0, sizeof(kib_fmr_poolset_t
));
1465 fps
->fps_pool_size
= pool_size
;
1466 fps
->fps_flush_trigger
= flush_trigger
;
1467 spin_lock_init(&fps
->fps_lock
);
1468 INIT_LIST_HEAD(&fps
->fps_pool_list
);
1469 INIT_LIST_HEAD(&fps
->fps_failed_pool_list
);
1471 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1473 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1478 static int kiblnd_fmr_pool_is_idle(kib_fmr_pool_t
*fpo
, unsigned long now
)
1480 if (fpo
->fpo_map_count
!= 0) /* still in use */
1482 if (fpo
->fpo_failed
)
1484 return cfs_time_aftereq(now
, fpo
->fpo_deadline
);
1487 void kiblnd_fmr_pool_unmap(kib_fmr_t
*fmr
, int status
)
1490 kib_fmr_pool_t
*fpo
= fmr
->fmr_pool
;
1491 kib_fmr_poolset_t
*fps
= fpo
->fpo_owner
;
1492 unsigned long now
= cfs_time_current();
1493 kib_fmr_pool_t
*tmp
;
1496 rc
= ib_fmr_pool_unmap(fmr
->fmr_pfmr
);
1500 rc
= ib_flush_fmr_pool(fpo
->fpo_fmr_pool
);
1504 fmr
->fmr_pool
= NULL
;
1505 fmr
->fmr_pfmr
= NULL
;
1507 spin_lock(&fps
->fps_lock
);
1508 fpo
->fpo_map_count
--; /* decref the pool */
1510 list_for_each_entry_safe(fpo
, tmp
, &fps
->fps_pool_list
, fpo_list
) {
1511 /* the first pool is persistent */
1512 if (fps
->fps_pool_list
.next
== &fpo
->fpo_list
)
1515 if (kiblnd_fmr_pool_is_idle(fpo
, now
)) {
1516 list_move(&fpo
->fpo_list
, &zombies
);
1520 spin_unlock(&fps
->fps_lock
);
1522 if (!list_empty(&zombies
))
1523 kiblnd_destroy_fmr_pool_list(&zombies
);
1526 int kiblnd_fmr_pool_map(kib_fmr_poolset_t
*fps
, __u64
*pages
, int npages
,
1527 __u64 iov
, kib_fmr_t
*fmr
)
1529 struct ib_pool_fmr
*pfmr
;
1530 kib_fmr_pool_t
*fpo
;
1535 spin_lock(&fps
->fps_lock
);
1536 version
= fps
->fps_version
;
1537 list_for_each_entry(fpo
, &fps
->fps_pool_list
, fpo_list
) {
1538 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1539 fpo
->fpo_map_count
++;
1540 spin_unlock(&fps
->fps_lock
);
1542 pfmr
= ib_fmr_pool_map_phys(fpo
->fpo_fmr_pool
,
1543 pages
, npages
, iov
);
1544 if (likely(!IS_ERR(pfmr
))) {
1545 fmr
->fmr_pool
= fpo
;
1546 fmr
->fmr_pfmr
= pfmr
;
1550 spin_lock(&fps
->fps_lock
);
1551 fpo
->fpo_map_count
--;
1552 if (PTR_ERR(pfmr
) != -EAGAIN
) {
1553 spin_unlock(&fps
->fps_lock
);
1554 return PTR_ERR(pfmr
);
1557 /* EAGAIN and ... */
1558 if (version
!= fps
->fps_version
) {
1559 spin_unlock(&fps
->fps_lock
);
1564 if (fps
->fps_increasing
) {
1565 spin_unlock(&fps
->fps_lock
);
1567 "Another thread is allocating new FMR pool, waiting for her to complete\n");
1573 if (time_before(cfs_time_current(), fps
->fps_next_retry
)) {
1574 /* someone failed recently */
1575 spin_unlock(&fps
->fps_lock
);
1579 fps
->fps_increasing
= 1;
1580 spin_unlock(&fps
->fps_lock
);
1582 CDEBUG(D_NET
, "Allocate new FMR pool\n");
1583 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1584 spin_lock(&fps
->fps_lock
);
1585 fps
->fps_increasing
= 0;
1588 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1590 fps
->fps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1592 spin_unlock(&fps
->fps_lock
);
1597 static void kiblnd_fini_pool(kib_pool_t
*pool
)
1599 LASSERT(list_empty(&pool
->po_free_list
));
1600 LASSERT(pool
->po_allocated
== 0);
1602 CDEBUG(D_NET
, "Finalize %s pool\n", pool
->po_owner
->ps_name
);
1605 static void kiblnd_init_pool(kib_poolset_t
*ps
, kib_pool_t
*pool
, int size
)
1607 CDEBUG(D_NET
, "Initialize %s pool\n", ps
->ps_name
);
1609 memset(pool
, 0, sizeof(kib_pool_t
));
1610 INIT_LIST_HEAD(&pool
->po_free_list
);
1611 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1612 pool
->po_owner
= ps
;
1613 pool
->po_size
= size
;
1616 static void kiblnd_destroy_pool_list(struct list_head
*head
)
1620 while (!list_empty(head
)) {
1621 pool
= list_entry(head
->next
, kib_pool_t
, po_list
);
1622 list_del(&pool
->po_list
);
1624 LASSERT(pool
->po_owner
!= NULL
);
1625 pool
->po_owner
->ps_pool_destroy(pool
);
1629 static void kiblnd_fail_poolset(kib_poolset_t
*ps
, struct list_head
*zombies
)
1631 if (ps
->ps_net
== NULL
) /* intialized? */
1634 spin_lock(&ps
->ps_lock
);
1635 while (!list_empty(&ps
->ps_pool_list
)) {
1636 kib_pool_t
*po
= list_entry(ps
->ps_pool_list
.next
,
1637 kib_pool_t
, po_list
);
1639 list_del(&po
->po_list
);
1640 if (po
->po_allocated
== 0)
1641 list_add(&po
->po_list
, zombies
);
1643 list_add(&po
->po_list
, &ps
->ps_failed_pool_list
);
1645 spin_unlock(&ps
->ps_lock
);
1648 static void kiblnd_fini_poolset(kib_poolset_t
*ps
)
1650 if (ps
->ps_net
!= NULL
) { /* initialized? */
1651 kiblnd_destroy_pool_list(&ps
->ps_failed_pool_list
);
1652 kiblnd_destroy_pool_list(&ps
->ps_pool_list
);
1656 static int kiblnd_init_poolset(kib_poolset_t
*ps
, int cpt
,
1657 kib_net_t
*net
, char *name
, int size
,
1658 kib_ps_pool_create_t po_create
,
1659 kib_ps_pool_destroy_t po_destroy
,
1660 kib_ps_node_init_t nd_init
,
1661 kib_ps_node_fini_t nd_fini
)
1666 memset(ps
, 0, sizeof(kib_poolset_t
));
1670 ps
->ps_pool_create
= po_create
;
1671 ps
->ps_pool_destroy
= po_destroy
;
1672 ps
->ps_node_init
= nd_init
;
1673 ps
->ps_node_fini
= nd_fini
;
1674 ps
->ps_pool_size
= size
;
1675 if (strlcpy(ps
->ps_name
, name
, sizeof(ps
->ps_name
))
1676 >= sizeof(ps
->ps_name
))
1678 spin_lock_init(&ps
->ps_lock
);
1679 INIT_LIST_HEAD(&ps
->ps_pool_list
);
1680 INIT_LIST_HEAD(&ps
->ps_failed_pool_list
);
1682 rc
= ps
->ps_pool_create(ps
, size
, &pool
);
1684 list_add(&pool
->po_list
, &ps
->ps_pool_list
);
1686 CERROR("Failed to create the first pool for %s\n", ps
->ps_name
);
1691 static int kiblnd_pool_is_idle(kib_pool_t
*pool
, unsigned long now
)
1693 if (pool
->po_allocated
!= 0) /* still in use */
1695 if (pool
->po_failed
)
1697 return cfs_time_aftereq(now
, pool
->po_deadline
);
1700 void kiblnd_pool_free_node(kib_pool_t
*pool
, struct list_head
*node
)
1703 kib_poolset_t
*ps
= pool
->po_owner
;
1705 unsigned long now
= cfs_time_current();
1707 spin_lock(&ps
->ps_lock
);
1709 if (ps
->ps_node_fini
!= NULL
)
1710 ps
->ps_node_fini(pool
, node
);
1712 LASSERT(pool
->po_allocated
> 0);
1713 list_add(node
, &pool
->po_free_list
);
1714 pool
->po_allocated
--;
1716 list_for_each_entry_safe(pool
, tmp
, &ps
->ps_pool_list
, po_list
) {
1717 /* the first pool is persistent */
1718 if (ps
->ps_pool_list
.next
== &pool
->po_list
)
1721 if (kiblnd_pool_is_idle(pool
, now
))
1722 list_move(&pool
->po_list
, &zombies
);
1724 spin_unlock(&ps
->ps_lock
);
1726 if (!list_empty(&zombies
))
1727 kiblnd_destroy_pool_list(&zombies
);
1730 struct list_head
*kiblnd_pool_alloc_node(kib_poolset_t
*ps
)
1732 struct list_head
*node
;
1737 spin_lock(&ps
->ps_lock
);
1738 list_for_each_entry(pool
, &ps
->ps_pool_list
, po_list
) {
1739 if (list_empty(&pool
->po_free_list
))
1742 pool
->po_allocated
++;
1743 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1744 node
= pool
->po_free_list
.next
;
1747 if (ps
->ps_node_init
!= NULL
) {
1748 /* still hold the lock */
1749 ps
->ps_node_init(pool
, node
);
1751 spin_unlock(&ps
->ps_lock
);
1755 /* no available tx pool and ... */
1756 if (ps
->ps_increasing
) {
1757 /* another thread is allocating a new pool */
1758 spin_unlock(&ps
->ps_lock
);
1759 CDEBUG(D_NET
, "Another thread is allocating new %s pool, waiting for her to complete\n",
1765 if (time_before(cfs_time_current(), ps
->ps_next_retry
)) {
1766 /* someone failed recently */
1767 spin_unlock(&ps
->ps_lock
);
1771 ps
->ps_increasing
= 1;
1772 spin_unlock(&ps
->ps_lock
);
1774 CDEBUG(D_NET
, "%s pool exhausted, allocate new pool\n", ps
->ps_name
);
1776 rc
= ps
->ps_pool_create(ps
, ps
->ps_pool_size
, &pool
);
1778 spin_lock(&ps
->ps_lock
);
1779 ps
->ps_increasing
= 0;
1781 list_add_tail(&pool
->po_list
, &ps
->ps_pool_list
);
1783 ps
->ps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1784 CERROR("Can't allocate new %s pool because out of memory\n",
1787 spin_unlock(&ps
->ps_lock
);
1792 static void kiblnd_destroy_tx_pool(kib_pool_t
*pool
)
1794 kib_tx_pool_t
*tpo
= container_of(pool
, kib_tx_pool_t
, tpo_pool
);
1797 LASSERT(pool
->po_allocated
== 0);
1799 if (tpo
->tpo_tx_pages
!= NULL
) {
1800 kiblnd_unmap_tx_pool(tpo
);
1801 kiblnd_free_pages(tpo
->tpo_tx_pages
);
1804 if (tpo
->tpo_tx_descs
== NULL
)
1807 for (i
= 0; i
< pool
->po_size
; i
++) {
1808 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1810 list_del(&tx
->tx_list
);
1811 if (tx
->tx_pages
!= NULL
)
1812 LIBCFS_FREE(tx
->tx_pages
,
1814 sizeof(*tx
->tx_pages
));
1815 if (tx
->tx_frags
!= NULL
)
1816 LIBCFS_FREE(tx
->tx_frags
,
1817 IBLND_MAX_RDMA_FRAGS
*
1818 sizeof(*tx
->tx_frags
));
1819 if (tx
->tx_wrq
!= NULL
)
1820 LIBCFS_FREE(tx
->tx_wrq
,
1821 (1 + IBLND_MAX_RDMA_FRAGS
) *
1822 sizeof(*tx
->tx_wrq
));
1823 if (tx
->tx_sge
!= NULL
)
1824 LIBCFS_FREE(tx
->tx_sge
,
1825 (1 + IBLND_MAX_RDMA_FRAGS
) *
1826 sizeof(*tx
->tx_sge
));
1827 if (tx
->tx_rd
!= NULL
)
1828 LIBCFS_FREE(tx
->tx_rd
,
1829 offsetof(kib_rdma_desc_t
,
1830 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1833 LIBCFS_FREE(tpo
->tpo_tx_descs
,
1834 pool
->po_size
* sizeof(kib_tx_t
));
1836 kiblnd_fini_pool(pool
);
1837 LIBCFS_FREE(tpo
, sizeof(kib_tx_pool_t
));
1840 static int kiblnd_tx_pool_size(int ncpts
)
1842 int ntx
= *kiblnd_tunables
.kib_ntx
/ ncpts
;
1844 return max(IBLND_TX_POOL
, ntx
);
1847 static int kiblnd_create_tx_pool(kib_poolset_t
*ps
, int size
,
1855 LIBCFS_CPT_ALLOC(tpo
, lnet_cpt_table(), ps
->ps_cpt
, sizeof(*tpo
));
1857 CERROR("Failed to allocate TX pool\n");
1861 pool
= &tpo
->tpo_pool
;
1862 kiblnd_init_pool(ps
, pool
, size
);
1863 tpo
->tpo_tx_descs
= NULL
;
1864 tpo
->tpo_tx_pages
= NULL
;
1866 npg
= (size
* IBLND_MSG_SIZE
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1867 if (kiblnd_alloc_pages(&tpo
->tpo_tx_pages
, ps
->ps_cpt
, npg
) != 0) {
1868 CERROR("Can't allocate tx pages: %d\n", npg
);
1869 LIBCFS_FREE(tpo
, sizeof(kib_tx_pool_t
));
1873 LIBCFS_CPT_ALLOC(tpo
->tpo_tx_descs
, lnet_cpt_table(), ps
->ps_cpt
,
1874 size
* sizeof(kib_tx_t
));
1875 if (tpo
->tpo_tx_descs
== NULL
) {
1876 CERROR("Can't allocate %d tx descriptors\n", size
);
1877 ps
->ps_pool_destroy(pool
);
1881 memset(tpo
->tpo_tx_descs
, 0, size
* sizeof(kib_tx_t
));
1883 for (i
= 0; i
< size
; i
++) {
1884 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1887 if (ps
->ps_net
->ibn_fmr_ps
!= NULL
) {
1888 LIBCFS_CPT_ALLOC(tx
->tx_pages
,
1889 lnet_cpt_table(), ps
->ps_cpt
,
1890 LNET_MAX_IOV
* sizeof(*tx
->tx_pages
));
1891 if (tx
->tx_pages
== NULL
)
1895 LIBCFS_CPT_ALLOC(tx
->tx_frags
, lnet_cpt_table(), ps
->ps_cpt
,
1896 IBLND_MAX_RDMA_FRAGS
* sizeof(*tx
->tx_frags
));
1897 if (tx
->tx_frags
== NULL
)
1900 sg_init_table(tx
->tx_frags
, IBLND_MAX_RDMA_FRAGS
);
1902 LIBCFS_CPT_ALLOC(tx
->tx_wrq
, lnet_cpt_table(), ps
->ps_cpt
,
1903 (1 + IBLND_MAX_RDMA_FRAGS
) *
1904 sizeof(*tx
->tx_wrq
));
1905 if (tx
->tx_wrq
== NULL
)
1908 LIBCFS_CPT_ALLOC(tx
->tx_sge
, lnet_cpt_table(), ps
->ps_cpt
,
1909 (1 + IBLND_MAX_RDMA_FRAGS
) *
1910 sizeof(*tx
->tx_sge
));
1911 if (tx
->tx_sge
== NULL
)
1914 LIBCFS_CPT_ALLOC(tx
->tx_rd
, lnet_cpt_table(), ps
->ps_cpt
,
1915 offsetof(kib_rdma_desc_t
,
1916 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1917 if (tx
->tx_rd
== NULL
)
1922 kiblnd_map_tx_pool(tpo
);
1927 ps
->ps_pool_destroy(pool
);
1931 static void kiblnd_tx_init(kib_pool_t
*pool
, struct list_head
*node
)
1933 kib_tx_poolset_t
*tps
= container_of(pool
->po_owner
, kib_tx_poolset_t
,
1935 kib_tx_t
*tx
= list_entry(node
, kib_tx_t
, tx_list
);
1937 tx
->tx_cookie
= tps
->tps_next_tx_cookie
++;
1940 static void kiblnd_net_fini_pools(kib_net_t
*net
)
1944 cfs_cpt_for_each(i
, lnet_cpt_table()) {
1945 kib_tx_poolset_t
*tps
;
1946 kib_fmr_poolset_t
*fps
;
1948 if (net
->ibn_tx_ps
!= NULL
) {
1949 tps
= net
->ibn_tx_ps
[i
];
1950 kiblnd_fini_poolset(&tps
->tps_poolset
);
1953 if (net
->ibn_fmr_ps
!= NULL
) {
1954 fps
= net
->ibn_fmr_ps
[i
];
1955 kiblnd_fini_fmr_poolset(fps
);
1959 if (net
->ibn_tx_ps
!= NULL
) {
1960 cfs_percpt_free(net
->ibn_tx_ps
);
1961 net
->ibn_tx_ps
= NULL
;
1964 if (net
->ibn_fmr_ps
!= NULL
) {
1965 cfs_percpt_free(net
->ibn_fmr_ps
);
1966 net
->ibn_fmr_ps
= NULL
;
1970 static int kiblnd_net_init_pools(kib_net_t
*net
, __u32
*cpts
, int ncpts
)
1972 unsigned long flags
;
1977 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1978 if (*kiblnd_tunables
.kib_map_on_demand
== 0 &&
1979 net
->ibn_dev
->ibd_hdev
->ibh_nmrs
== 1) {
1980 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1981 goto create_tx_pool
;
1984 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1986 if (*kiblnd_tunables
.kib_fmr_pool_size
<
1987 *kiblnd_tunables
.kib_ntx
/ 4) {
1988 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
1989 *kiblnd_tunables
.kib_fmr_pool_size
,
1990 *kiblnd_tunables
.kib_ntx
/ 4);
1996 * TX pool must be created later than FMR, see LU-2268
1999 LASSERT(net
->ibn_tx_ps
== NULL
);
2002 * premapping can fail if ibd_nmr > 1, so we always create
2003 * FMR pool and map-on-demand if premapping failed
2006 net
->ibn_fmr_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2007 sizeof(kib_fmr_poolset_t
));
2008 if (net
->ibn_fmr_ps
== NULL
) {
2009 CERROR("Failed to allocate FMR pool array\n");
2014 for (i
= 0; i
< ncpts
; i
++) {
2015 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2016 rc
= kiblnd_init_fmr_poolset(net
->ibn_fmr_ps
[cpt
], cpt
, net
,
2017 kiblnd_fmr_pool_size(ncpts
),
2018 kiblnd_fmr_flush_trigger(ncpts
));
2019 if (rc
== -ENOSYS
&& i
== 0) /* no FMR */
2022 if (rc
!= 0) { /* a real error */
2023 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2030 LASSERT(i
== ncpts
);
2031 goto create_tx_pool
;
2034 cfs_percpt_free(net
->ibn_fmr_ps
);
2035 net
->ibn_fmr_ps
= NULL
;
2037 CWARN("Device does not support FMR\n");
2041 net
->ibn_tx_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2042 sizeof(kib_tx_poolset_t
));
2043 if (net
->ibn_tx_ps
== NULL
) {
2044 CERROR("Failed to allocate tx pool array\n");
2049 for (i
= 0; i
< ncpts
; i
++) {
2050 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2051 rc
= kiblnd_init_poolset(&net
->ibn_tx_ps
[cpt
]->tps_poolset
,
2053 kiblnd_tx_pool_size(ncpts
),
2054 kiblnd_create_tx_pool
,
2055 kiblnd_destroy_tx_pool
,
2056 kiblnd_tx_init
, NULL
);
2058 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2066 kiblnd_net_fini_pools(net
);
2071 static int kiblnd_hdev_get_attr(kib_hca_dev_t
*hdev
)
2073 struct ib_device_attr
*attr
;
2076 /* It's safe to assume a HCA can handle a page size
2077 * matching that of the native system */
2078 hdev
->ibh_page_shift
= PAGE_SHIFT
;
2079 hdev
->ibh_page_size
= 1 << PAGE_SHIFT
;
2080 hdev
->ibh_page_mask
= ~((__u64
)hdev
->ibh_page_size
- 1);
2082 LIBCFS_ALLOC(attr
, sizeof(*attr
));
2084 CERROR("Out of memory\n");
2088 rc
= ib_query_device(hdev
->ibh_ibdev
, attr
);
2090 hdev
->ibh_mr_size
= attr
->max_mr_size
;
2092 LIBCFS_FREE(attr
, sizeof(*attr
));
2095 CERROR("Failed to query IB device: %d\n", rc
);
2099 if (hdev
->ibh_mr_size
== ~0ULL) {
2100 hdev
->ibh_mr_shift
= 64;
2104 for (hdev
->ibh_mr_shift
= 0;
2105 hdev
->ibh_mr_shift
< 64; hdev
->ibh_mr_shift
++) {
2106 if (hdev
->ibh_mr_size
== (1ULL << hdev
->ibh_mr_shift
) ||
2107 hdev
->ibh_mr_size
== (1ULL << hdev
->ibh_mr_shift
) - 1)
2111 CERROR("Invalid mr size: %#llx\n", hdev
->ibh_mr_size
);
2115 static void kiblnd_hdev_cleanup_mrs(kib_hca_dev_t
*hdev
)
2119 if (hdev
->ibh_nmrs
== 0 || hdev
->ibh_mrs
== NULL
)
2122 for (i
= 0; i
< hdev
->ibh_nmrs
; i
++) {
2123 if (hdev
->ibh_mrs
[i
] == NULL
)
2126 ib_dereg_mr(hdev
->ibh_mrs
[i
]);
2129 LIBCFS_FREE(hdev
->ibh_mrs
, sizeof(*hdev
->ibh_mrs
) * hdev
->ibh_nmrs
);
2130 hdev
->ibh_mrs
= NULL
;
2134 void kiblnd_hdev_destroy(kib_hca_dev_t
*hdev
)
2136 kiblnd_hdev_cleanup_mrs(hdev
);
2138 if (hdev
->ibh_pd
!= NULL
)
2139 ib_dealloc_pd(hdev
->ibh_pd
);
2141 if (hdev
->ibh_cmid
!= NULL
)
2142 rdma_destroy_id(hdev
->ibh_cmid
);
2144 LIBCFS_FREE(hdev
, sizeof(*hdev
));
2147 static int kiblnd_hdev_setup_mrs(kib_hca_dev_t
*hdev
)
2151 int acflags
= IB_ACCESS_LOCAL_WRITE
| IB_ACCESS_REMOTE_WRITE
;
2153 rc
= kiblnd_hdev_get_attr(hdev
);
2157 LIBCFS_ALLOC(hdev
->ibh_mrs
, 1 * sizeof(*hdev
->ibh_mrs
));
2158 if (hdev
->ibh_mrs
== NULL
) {
2159 CERROR("Failed to allocate MRs table\n");
2163 hdev
->ibh_mrs
[0] = NULL
;
2166 mr
= ib_get_dma_mr(hdev
->ibh_pd
, acflags
);
2168 CERROR("Failed ib_get_dma_mr : %ld\n", PTR_ERR(mr
));
2169 kiblnd_hdev_cleanup_mrs(hdev
);
2173 hdev
->ibh_mrs
[0] = mr
;
2179 static int kiblnd_dummy_callback(struct rdma_cm_id
*cmid
,
2180 struct rdma_cm_event
*event
)
2185 static int kiblnd_dev_need_failover(kib_dev_t
*dev
)
2187 struct rdma_cm_id
*cmid
;
2188 struct sockaddr_in srcaddr
;
2189 struct sockaddr_in dstaddr
;
2192 if (dev
->ibd_hdev
== NULL
|| /* initializing */
2193 dev
->ibd_hdev
->ibh_cmid
== NULL
|| /* listener is dead */
2194 *kiblnd_tunables
.kib_dev_failover
> 1) /* debugging */
2197 /* XXX: it's UGLY, but I don't have better way to find
2198 * ib-bonding HCA failover because:
2200 * a. no reliable CM event for HCA failover...
2201 * b. no OFED API to get ib_device for current net_device...
2203 * We have only two choices at this point:
2205 * a. rdma_bind_addr(), it will conflict with listener cmid
2206 * b. rdma_resolve_addr() to zero addr */
2207 cmid
= kiblnd_rdma_create_id(kiblnd_dummy_callback
, dev
, RDMA_PS_TCP
,
2211 CERROR("Failed to create cmid for failover: %d\n", rc
);
2215 memset(&srcaddr
, 0, sizeof(srcaddr
));
2216 srcaddr
.sin_family
= AF_INET
;
2217 srcaddr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2219 memset(&dstaddr
, 0, sizeof(dstaddr
));
2220 dstaddr
.sin_family
= AF_INET
;
2221 rc
= rdma_resolve_addr(cmid
, (struct sockaddr
*)&srcaddr
,
2222 (struct sockaddr
*)&dstaddr
, 1);
2223 if (rc
!= 0 || cmid
->device
== NULL
) {
2224 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2225 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2227 rdma_destroy_id(cmid
);
2231 rc
= dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
; /* true for failover */
2232 rdma_destroy_id(cmid
);
2237 int kiblnd_dev_failover(kib_dev_t
*dev
)
2239 LIST_HEAD(zombie_tpo
);
2240 LIST_HEAD(zombie_ppo
);
2241 LIST_HEAD(zombie_fpo
);
2242 struct rdma_cm_id
*cmid
= NULL
;
2243 kib_hca_dev_t
*hdev
= NULL
;
2246 struct sockaddr_in addr
;
2247 unsigned long flags
;
2251 LASSERT(*kiblnd_tunables
.kib_dev_failover
> 1 ||
2252 dev
->ibd_can_failover
||
2253 dev
->ibd_hdev
== NULL
);
2255 rc
= kiblnd_dev_need_failover(dev
);
2259 if (dev
->ibd_hdev
!= NULL
&&
2260 dev
->ibd_hdev
->ibh_cmid
!= NULL
) {
2261 /* XXX it's not good to close old listener at here,
2262 * because we can fail to create new listener.
2263 * But we have to close it now, otherwise rdma_bind_addr
2264 * will return EADDRINUSE... How crap! */
2265 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2267 cmid
= dev
->ibd_hdev
->ibh_cmid
;
2268 /* make next schedule of kiblnd_dev_need_failover()
2269 * return 1 for me */
2270 dev
->ibd_hdev
->ibh_cmid
= NULL
;
2271 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2273 rdma_destroy_id(cmid
);
2276 cmid
= kiblnd_rdma_create_id(kiblnd_cm_callback
, dev
, RDMA_PS_TCP
,
2280 CERROR("Failed to create cmid for failover: %d\n", rc
);
2284 memset(&addr
, 0, sizeof(addr
));
2285 addr
.sin_family
= AF_INET
;
2286 addr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2287 addr
.sin_port
= htons(*kiblnd_tunables
.kib_service
);
2289 /* Bind to failover device or port */
2290 rc
= rdma_bind_addr(cmid
, (struct sockaddr
*)&addr
);
2291 if (rc
!= 0 || cmid
->device
== NULL
) {
2292 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2293 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2295 rdma_destroy_id(cmid
);
2299 LIBCFS_ALLOC(hdev
, sizeof(*hdev
));
2301 CERROR("Failed to allocate kib_hca_dev\n");
2302 rdma_destroy_id(cmid
);
2307 atomic_set(&hdev
->ibh_ref
, 1);
2308 hdev
->ibh_dev
= dev
;
2309 hdev
->ibh_cmid
= cmid
;
2310 hdev
->ibh_ibdev
= cmid
->device
;
2312 pd
= ib_alloc_pd(cmid
->device
);
2315 CERROR("Can't allocate PD: %d\n", rc
);
2321 rc
= rdma_listen(cmid
, 0);
2323 CERROR("Can't start new listener: %d\n", rc
);
2327 rc
= kiblnd_hdev_setup_mrs(hdev
);
2329 CERROR("Can't setup device: %d\n", rc
);
2333 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2335 swap(dev
->ibd_hdev
, hdev
); /* take over the refcount */
2337 list_for_each_entry(net
, &dev
->ibd_nets
, ibn_list
) {
2338 cfs_cpt_for_each(i
, lnet_cpt_table()) {
2339 kiblnd_fail_poolset(&net
->ibn_tx_ps
[i
]->tps_poolset
,
2342 if (net
->ibn_fmr_ps
)
2343 kiblnd_fail_fmr_poolset(net
->ibn_fmr_ps
[i
],
2348 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2350 if (!list_empty(&zombie_tpo
))
2351 kiblnd_destroy_pool_list(&zombie_tpo
);
2352 if (!list_empty(&zombie_ppo
))
2353 kiblnd_destroy_pool_list(&zombie_ppo
);
2354 if (!list_empty(&zombie_fpo
))
2355 kiblnd_destroy_fmr_pool_list(&zombie_fpo
);
2357 kiblnd_hdev_decref(hdev
);
2360 dev
->ibd_failed_failover
++;
2362 dev
->ibd_failed_failover
= 0;
2367 void kiblnd_destroy_dev(kib_dev_t
*dev
)
2369 LASSERT(dev
->ibd_nnets
== 0);
2370 LASSERT(list_empty(&dev
->ibd_nets
));
2372 list_del(&dev
->ibd_fail_list
);
2373 list_del(&dev
->ibd_list
);
2375 if (dev
->ibd_hdev
!= NULL
)
2376 kiblnd_hdev_decref(dev
->ibd_hdev
);
2378 LIBCFS_FREE(dev
, sizeof(*dev
));
2381 static kib_dev_t
*kiblnd_create_dev(char *ifname
)
2383 struct net_device
*netdev
;
2390 rc
= lnet_ipif_query(ifname
, &up
, &ip
, &netmask
);
2392 CERROR("Can't query IPoIB interface %s: %d\n",
2398 CERROR("Can't query IPoIB interface %s: it's down\n", ifname
);
2402 LIBCFS_ALLOC(dev
, sizeof(*dev
));
2406 netdev
= dev_get_by_name(&init_net
, ifname
);
2407 if (netdev
== NULL
) {
2408 dev
->ibd_can_failover
= 0;
2410 dev
->ibd_can_failover
= !!(netdev
->flags
& IFF_MASTER
);
2414 INIT_LIST_HEAD(&dev
->ibd_nets
);
2415 INIT_LIST_HEAD(&dev
->ibd_list
); /* not yet in kib_devs */
2416 INIT_LIST_HEAD(&dev
->ibd_fail_list
);
2418 strcpy(&dev
->ibd_ifname
[0], ifname
);
2420 /* initialize the device */
2421 rc
= kiblnd_dev_failover(dev
);
2423 CERROR("Can't initialize device: %d\n", rc
);
2424 LIBCFS_FREE(dev
, sizeof(*dev
));
2428 list_add_tail(&dev
->ibd_list
,
2429 &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;
2455 /* NB: we really want to stop scheduler threads net by net
2456 * instead of the whole module, this should be improved
2457 * with dynamic configuration LNet */
2458 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
)
2459 wake_up_all(&sched
->ibs_waitq
);
2461 wake_up_all(&kiblnd_data
.kib_connd_waitq
);
2462 wake_up_all(&kiblnd_data
.kib_failover_waitq
);
2465 while (atomic_read(&kiblnd_data
.kib_nthreads
) != 0) {
2468 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
,
2469 "Waiting for %d threads to terminate\n",
2470 atomic_read(&kiblnd_data
.kib_nthreads
));
2471 set_current_state(TASK_UNINTERRUPTIBLE
);
2472 schedule_timeout(cfs_time_seconds(1));
2477 case IBLND_INIT_NOTHING
:
2481 if (kiblnd_data
.kib_peers
!= NULL
) {
2482 LIBCFS_FREE(kiblnd_data
.kib_peers
,
2483 sizeof(struct list_head
) *
2484 kiblnd_data
.kib_peer_hash_size
);
2487 if (kiblnd_data
.kib_scheds
!= NULL
)
2488 cfs_percpt_free(kiblnd_data
.kib_scheds
);
2490 kiblnd_data
.kib_init
= IBLND_INIT_NOTHING
;
2491 module_put(THIS_MODULE
);
2494 void kiblnd_shutdown(lnet_ni_t
*ni
)
2496 kib_net_t
*net
= ni
->ni_data
;
2497 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
2499 unsigned long flags
;
2501 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_ALL
);
2506 write_lock_irqsave(g_lock
, flags
);
2507 net
->ibn_shutdown
= 1;
2508 write_unlock_irqrestore(g_lock
, flags
);
2510 switch (net
->ibn_init
) {
2514 case IBLND_INIT_ALL
:
2515 /* nuke all existing peers within this net */
2516 kiblnd_del_peer(ni
, LNET_NID_ANY
);
2518 /* Wait for all peer state to clean up */
2520 while (atomic_read(&net
->ibn_npeers
) != 0) {
2522 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
, /* 2**n? */
2523 "%s: waiting for %d peers to disconnect\n",
2524 libcfs_nid2str(ni
->ni_nid
),
2525 atomic_read(&net
->ibn_npeers
));
2526 set_current_state(TASK_UNINTERRUPTIBLE
);
2527 schedule_timeout(cfs_time_seconds(1));
2530 kiblnd_net_fini_pools(net
);
2532 write_lock_irqsave(g_lock
, flags
);
2533 LASSERT(net
->ibn_dev
->ibd_nnets
> 0);
2534 net
->ibn_dev
->ibd_nnets
--;
2535 list_del(&net
->ibn_list
);
2536 write_unlock_irqrestore(g_lock
, flags
);
2540 case IBLND_INIT_NOTHING
:
2541 LASSERT(atomic_read(&net
->ibn_nconns
) == 0);
2543 if (net
->ibn_dev
!= NULL
&&
2544 net
->ibn_dev
->ibd_nnets
== 0)
2545 kiblnd_destroy_dev(net
->ibn_dev
);
2550 net
->ibn_init
= IBLND_INIT_NOTHING
;
2553 LIBCFS_FREE(net
, sizeof(*net
));
2556 if (list_empty(&kiblnd_data
.kib_devs
))
2557 kiblnd_base_shutdown();
2560 static int kiblnd_base_startup(void)
2562 struct kib_sched_info
*sched
;
2566 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
);
2568 try_module_get(THIS_MODULE
);
2569 /* zero pointers, flags etc */
2570 memset(&kiblnd_data
, 0, sizeof(kiblnd_data
));
2572 rwlock_init(&kiblnd_data
.kib_global_lock
);
2574 INIT_LIST_HEAD(&kiblnd_data
.kib_devs
);
2575 INIT_LIST_HEAD(&kiblnd_data
.kib_failed_devs
);
2577 kiblnd_data
.kib_peer_hash_size
= IBLND_PEER_HASH_SIZE
;
2578 LIBCFS_ALLOC(kiblnd_data
.kib_peers
,
2579 sizeof(struct list_head
) * kiblnd_data
.kib_peer_hash_size
);
2580 if (kiblnd_data
.kib_peers
== NULL
)
2582 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++)
2583 INIT_LIST_HEAD(&kiblnd_data
.kib_peers
[i
]);
2585 spin_lock_init(&kiblnd_data
.kib_connd_lock
);
2586 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_conns
);
2587 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_zombies
);
2588 init_waitqueue_head(&kiblnd_data
.kib_connd_waitq
);
2589 init_waitqueue_head(&kiblnd_data
.kib_failover_waitq
);
2591 kiblnd_data
.kib_scheds
= cfs_percpt_alloc(lnet_cpt_table(),
2593 if (kiblnd_data
.kib_scheds
== NULL
)
2596 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
) {
2599 spin_lock_init(&sched
->ibs_lock
);
2600 INIT_LIST_HEAD(&sched
->ibs_conns
);
2601 init_waitqueue_head(&sched
->ibs_waitq
);
2603 nthrs
= cfs_cpt_weight(lnet_cpt_table(), i
);
2604 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2605 nthrs
= min(nthrs
, *kiblnd_tunables
.kib_nscheds
);
2607 /* max to half of CPUs, another half is reserved for
2608 * upper layer modules */
2609 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2612 sched
->ibs_nthreads_max
= nthrs
;
2616 kiblnd_data
.kib_error_qpa
.qp_state
= IB_QPS_ERR
;
2618 /* lists/ptrs/locks initialised */
2619 kiblnd_data
.kib_init
= IBLND_INIT_DATA
;
2620 /*****************************************************/
2622 rc
= kiblnd_thread_start(kiblnd_connd
, NULL
, "kiblnd_connd");
2624 CERROR("Can't spawn o2iblnd connd: %d\n", rc
);
2628 if (*kiblnd_tunables
.kib_dev_failover
!= 0)
2629 rc
= kiblnd_thread_start(kiblnd_failover_thread
, NULL
,
2633 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc
);
2637 /* flag everything initialised */
2638 kiblnd_data
.kib_init
= IBLND_INIT_ALL
;
2639 /*****************************************************/
2644 kiblnd_base_shutdown();
2648 static int kiblnd_start_schedulers(struct kib_sched_info
*sched
)
2654 if (sched
->ibs_nthreads
== 0) {
2655 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2656 nthrs
= sched
->ibs_nthreads_max
;
2658 nthrs
= cfs_cpt_weight(lnet_cpt_table(),
2660 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2661 nthrs
= min(IBLND_N_SCHED_HIGH
, nthrs
);
2664 LASSERT(sched
->ibs_nthreads
<= sched
->ibs_nthreads_max
);
2665 /* increase one thread if there is new interface */
2666 nthrs
= sched
->ibs_nthreads
< sched
->ibs_nthreads_max
;
2669 for (i
= 0; i
< nthrs
; i
++) {
2673 id
= KIB_THREAD_ID(sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
);
2674 snprintf(name
, sizeof(name
), "kiblnd_sd_%02ld_%02ld",
2675 KIB_THREAD_CPT(id
), KIB_THREAD_TID(id
));
2676 rc
= kiblnd_thread_start(kiblnd_scheduler
, (void *)id
, name
);
2680 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2681 sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
, rc
);
2685 sched
->ibs_nthreads
+= i
;
2689 static int kiblnd_dev_start_threads(kib_dev_t
*dev
, int newdev
, __u32
*cpts
,
2696 for (i
= 0; i
< ncpts
; i
++) {
2697 struct kib_sched_info
*sched
;
2699 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2700 sched
= kiblnd_data
.kib_scheds
[cpt
];
2702 if (!newdev
&& sched
->ibs_nthreads
> 0)
2705 rc
= kiblnd_start_schedulers(kiblnd_data
.kib_scheds
[cpt
]);
2707 CERROR("Failed to start scheduler threads for %s\n",
2715 static kib_dev_t
*kiblnd_dev_search(char *ifname
)
2717 kib_dev_t
*alias
= NULL
;
2722 colon
= strchr(ifname
, ':');
2723 list_for_each_entry(dev
, &kiblnd_data
.kib_devs
, ibd_list
) {
2724 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2730 colon2
= strchr(dev
->ibd_ifname
, ':');
2736 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2747 int kiblnd_startup(lnet_ni_t
*ni
)
2750 kib_dev_t
*ibdev
= NULL
;
2752 struct timespec64 tv
;
2753 unsigned long flags
;
2757 LASSERT(ni
->ni_lnd
== &the_o2iblnd
);
2759 if (kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
) {
2760 rc
= kiblnd_base_startup();
2765 LIBCFS_ALLOC(net
, sizeof(*net
));
2770 ktime_get_real_ts64(&tv
);
2771 net
->ibn_incarnation
= tv
.tv_sec
* USEC_PER_SEC
+
2772 tv
.tv_nsec
/ NSEC_PER_USEC
;
2774 ni
->ni_peertimeout
= *kiblnd_tunables
.kib_peertimeout
;
2775 ni
->ni_maxtxcredits
= *kiblnd_tunables
.kib_credits
;
2776 ni
->ni_peertxcredits
= *kiblnd_tunables
.kib_peertxcredits
;
2777 ni
->ni_peerrtrcredits
= *kiblnd_tunables
.kib_peerrtrcredits
;
2779 if (ni
->ni_interfaces
[0] != NULL
) {
2780 /* Use the IPoIB interface specified in 'networks=' */
2782 CLASSERT(LNET_MAX_INTERFACES
> 1);
2783 if (ni
->ni_interfaces
[1] != NULL
) {
2784 CERROR("Multiple interfaces not supported\n");
2788 ifname
= ni
->ni_interfaces
[0];
2790 ifname
= *kiblnd_tunables
.kib_default_ipif
;
2793 if (strlen(ifname
) >= sizeof(ibdev
->ibd_ifname
)) {
2794 CERROR("IPoIB interface name too long: %s\n", ifname
);
2798 ibdev
= kiblnd_dev_search(ifname
);
2800 newdev
= ibdev
== NULL
;
2801 /* hmm...create kib_dev even for alias */
2802 if (ibdev
== NULL
|| strcmp(&ibdev
->ibd_ifname
[0], ifname
) != 0)
2803 ibdev
= kiblnd_create_dev(ifname
);
2808 net
->ibn_dev
= ibdev
;
2809 ni
->ni_nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
), ibdev
->ibd_ifip
);
2811 rc
= kiblnd_dev_start_threads(ibdev
, newdev
,
2812 ni
->ni_cpts
, ni
->ni_ncpts
);
2816 rc
= kiblnd_net_init_pools(net
, ni
->ni_cpts
, ni
->ni_ncpts
);
2818 CERROR("Failed to initialize NI pools: %d\n", rc
);
2822 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2824 list_add_tail(&net
->ibn_list
, &ibdev
->ibd_nets
);
2825 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2827 net
->ibn_init
= IBLND_INIT_ALL
;
2832 if (net
->ibn_dev
== NULL
&& ibdev
!= NULL
)
2833 kiblnd_destroy_dev(ibdev
);
2836 kiblnd_shutdown(ni
);
2838 CDEBUG(D_NET
, "kiblnd_startup failed\n");
2842 static void __exit
kiblnd_module_fini(void)
2844 lnet_unregister_lnd(&the_o2iblnd
);
2847 static int __init
kiblnd_module_init(void)
2851 CLASSERT(sizeof(kib_msg_t
) <= IBLND_MSG_SIZE
);
2852 CLASSERT(offsetof(kib_msg_t
,
2853 ibm_u
.get
.ibgm_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2855 CLASSERT(offsetof(kib_msg_t
,
2856 ibm_u
.putack
.ibpam_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2859 rc
= kiblnd_tunables_init();
2863 lnet_register_lnd(&the_o2iblnd
);
2868 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2869 MODULE_DESCRIPTION("Kernel OpenIB gen2 LND v2.00");
2870 MODULE_LICENSE("GPL");
2872 module_init(kiblnd_module_init
);
2873 module_exit(kiblnd_module_fini
);