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
) {
403 peer
= list_entry(tmp
, kib_peer_t
, ibp_list
);
405 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
406 peer
->ibp_accepting
> 0 ||
407 !list_empty(&peer
->ibp_conns
)); /* active conn */
409 if (peer
->ibp_nid
!= nid
)
412 CDEBUG(D_NET
, "got peer [%p] -> %s (%d) version: %x\n",
413 peer
, libcfs_nid2str(nid
),
414 atomic_read(&peer
->ibp_refcount
),
421 void kiblnd_unlink_peer_locked(kib_peer_t
*peer
)
423 LASSERT(list_empty(&peer
->ibp_conns
));
425 LASSERT(kiblnd_peer_active(peer
));
426 list_del_init(&peer
->ibp_list
);
427 /* lose peerlist's ref */
428 kiblnd_peer_decref(peer
);
431 static int kiblnd_get_peer_info(lnet_ni_t
*ni
, int index
,
432 lnet_nid_t
*nidp
, int *count
)
435 struct list_head
*ptmp
;
439 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
441 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
443 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
445 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
446 LASSERT(peer
->ibp_connecting
> 0 ||
447 peer
->ibp_accepting
> 0 ||
448 !list_empty(&peer
->ibp_conns
));
450 if (peer
->ibp_ni
!= ni
)
456 *nidp
= peer
->ibp_nid
;
457 *count
= atomic_read(&peer
->ibp_refcount
);
459 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
,
465 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
469 static void kiblnd_del_peer_locked(kib_peer_t
*peer
)
471 struct list_head
*ctmp
;
472 struct list_head
*cnxt
;
475 if (list_empty(&peer
->ibp_conns
)) {
476 kiblnd_unlink_peer_locked(peer
);
478 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
479 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
481 kiblnd_close_conn_locked(conn
, 0);
483 /* NB closing peer's last conn unlinked it. */
486 * NB peer now unlinked; might even be freed if the peer table had the
491 static int kiblnd_del_peer(lnet_ni_t
*ni
, lnet_nid_t nid
)
494 struct list_head
*ptmp
;
495 struct list_head
*pnxt
;
503 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
505 if (nid
!= LNET_NID_ANY
) {
506 lo
= hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
509 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
512 for (i
= lo
; i
<= hi
; i
++) {
513 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
514 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
515 LASSERT(peer
->ibp_connecting
> 0 ||
516 peer
->ibp_accepting
> 0 ||
517 !list_empty(&peer
->ibp_conns
));
519 if (peer
->ibp_ni
!= ni
)
522 if (!(nid
== LNET_NID_ANY
|| peer
->ibp_nid
== nid
))
525 if (!list_empty(&peer
->ibp_tx_queue
)) {
526 LASSERT(list_empty(&peer
->ibp_conns
));
528 list_splice_init(&peer
->ibp_tx_queue
,
532 kiblnd_del_peer_locked(peer
);
533 rc
= 0; /* matched something */
537 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
539 kiblnd_txlist_done(ni
, &zombies
, -EIO
);
544 static kib_conn_t
*kiblnd_get_conn_by_idx(lnet_ni_t
*ni
, int index
)
547 struct list_head
*ptmp
;
549 struct list_head
*ctmp
;
553 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
555 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++) {
556 list_for_each(ptmp
, &kiblnd_data
.kib_peers
[i
]) {
558 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
559 LASSERT(peer
->ibp_connecting
> 0 ||
560 peer
->ibp_accepting
> 0 ||
561 !list_empty(&peer
->ibp_conns
));
563 if (peer
->ibp_ni
!= ni
)
566 list_for_each(ctmp
, &peer
->ibp_conns
) {
570 conn
= list_entry(ctmp
, kib_conn_t
,
572 kiblnd_conn_addref(conn
);
573 read_unlock_irqrestore(
574 &kiblnd_data
.kib_global_lock
,
581 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
585 int kiblnd_translate_mtu(int value
)
605 static void kiblnd_setup_mtu_locked(struct rdma_cm_id
*cmid
)
609 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
610 if (cmid
->route
.path_rec
== NULL
)
613 mtu
= kiblnd_translate_mtu(*kiblnd_tunables
.kib_ib_mtu
);
616 cmid
->route
.path_rec
->mtu
= mtu
;
619 static int kiblnd_get_completion_vector(kib_conn_t
*conn
, int cpt
)
625 lnet_nid_t nid
= conn
->ibc_peer
->ibp_nid
;
627 vectors
= conn
->ibc_cmid
->device
->num_comp_vectors
;
631 mask
= cfs_cpt_cpumask(lnet_cpt_table(), cpt
);
635 /* hash NID to CPU id in this partition... */
636 off
= do_div(nid
, cpumask_weight(mask
));
637 for_each_cpu(i
, mask
) {
646 kib_conn_t
*kiblnd_create_conn(kib_peer_t
*peer
, struct rdma_cm_id
*cmid
,
647 int state
, int version
)
651 * If the new conn is created successfully it takes over the caller's
652 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
653 * is destroyed. On failure, the caller's ref on 'peer' remains and
654 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
655 * to destroy 'cmid' here since I'm called from the CM which still has
656 * its ref on 'cmid').
658 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
659 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
661 struct ib_qp_init_attr
*init_qp_attr
;
662 struct kib_sched_info
*sched
;
663 struct ib_cq_init_attr cq_attr
= {};
671 LASSERT(net
!= NULL
);
672 LASSERT(!in_interrupt());
676 cpt
= lnet_cpt_of_nid(peer
->ibp_nid
);
677 sched
= kiblnd_data
.kib_scheds
[cpt
];
679 LASSERT(sched
->ibs_nthreads
> 0);
681 LIBCFS_CPT_ALLOC(init_qp_attr
, lnet_cpt_table(), cpt
,
682 sizeof(*init_qp_attr
));
683 if (init_qp_attr
== NULL
) {
684 CERROR("Can't allocate qp_attr for %s\n",
685 libcfs_nid2str(peer
->ibp_nid
));
689 LIBCFS_CPT_ALLOC(conn
, lnet_cpt_table(), cpt
, sizeof(*conn
));
691 CERROR("Can't allocate connection for %s\n",
692 libcfs_nid2str(peer
->ibp_nid
));
696 conn
->ibc_state
= IBLND_CONN_INIT
;
697 conn
->ibc_version
= version
;
698 conn
->ibc_peer
= peer
; /* I take the caller's ref */
699 cmid
->context
= conn
; /* for future CM callbacks */
700 conn
->ibc_cmid
= cmid
;
702 INIT_LIST_HEAD(&conn
->ibc_early_rxs
);
703 INIT_LIST_HEAD(&conn
->ibc_tx_noops
);
704 INIT_LIST_HEAD(&conn
->ibc_tx_queue
);
705 INIT_LIST_HEAD(&conn
->ibc_tx_queue_rsrvd
);
706 INIT_LIST_HEAD(&conn
->ibc_tx_queue_nocred
);
707 INIT_LIST_HEAD(&conn
->ibc_active_txs
);
708 spin_lock_init(&conn
->ibc_lock
);
710 LIBCFS_CPT_ALLOC(conn
->ibc_connvars
, lnet_cpt_table(), cpt
,
711 sizeof(*conn
->ibc_connvars
));
712 if (conn
->ibc_connvars
== NULL
) {
713 CERROR("Can't allocate in-progress connection state\n");
717 write_lock_irqsave(glock
, flags
);
718 if (dev
->ibd_failover
) {
719 write_unlock_irqrestore(glock
, flags
);
720 CERROR("%s: failover in progress\n", dev
->ibd_ifname
);
724 if (dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
) {
725 /* wakeup failover thread and teardown connection */
726 if (kiblnd_dev_can_failover(dev
)) {
727 list_add_tail(&dev
->ibd_fail_list
,
728 &kiblnd_data
.kib_failed_devs
);
729 wake_up(&kiblnd_data
.kib_failover_waitq
);
732 write_unlock_irqrestore(glock
, flags
);
733 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
734 cmid
->device
->name
, dev
->ibd_ifname
);
738 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
739 conn
->ibc_hdev
= dev
->ibd_hdev
;
741 kiblnd_setup_mtu_locked(cmid
);
743 write_unlock_irqrestore(glock
, flags
);
745 LIBCFS_CPT_ALLOC(conn
->ibc_rxs
, lnet_cpt_table(), cpt
,
746 IBLND_RX_MSGS(version
) * sizeof(kib_rx_t
));
747 if (conn
->ibc_rxs
== NULL
) {
748 CERROR("Cannot allocate RX buffers\n");
752 rc
= kiblnd_alloc_pages(&conn
->ibc_rx_pages
, cpt
,
753 IBLND_RX_MSG_PAGES(version
));
757 kiblnd_map_rx_descs(conn
);
759 cq_attr
.cqe
= IBLND_CQ_ENTRIES(version
);
760 cq_attr
.comp_vector
= kiblnd_get_completion_vector(conn
, cpt
);
761 cq
= ib_create_cq(cmid
->device
,
762 kiblnd_cq_completion
, kiblnd_cq_event
, conn
,
765 CERROR("Can't create CQ: %ld, cqe: %d\n",
766 PTR_ERR(cq
), IBLND_CQ_ENTRIES(version
));
772 rc
= ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
774 CERROR("Can't request completion notificiation: %d\n", rc
);
778 init_qp_attr
->event_handler
= kiblnd_qp_event
;
779 init_qp_attr
->qp_context
= conn
;
780 init_qp_attr
->cap
.max_send_wr
= IBLND_SEND_WRS(version
);
781 init_qp_attr
->cap
.max_recv_wr
= IBLND_RECV_WRS(version
);
782 init_qp_attr
->cap
.max_send_sge
= 1;
783 init_qp_attr
->cap
.max_recv_sge
= 1;
784 init_qp_attr
->sq_sig_type
= IB_SIGNAL_REQ_WR
;
785 init_qp_attr
->qp_type
= IB_QPT_RC
;
786 init_qp_attr
->send_cq
= cq
;
787 init_qp_attr
->recv_cq
= cq
;
789 conn
->ibc_sched
= sched
;
791 rc
= rdma_create_qp(cmid
, conn
->ibc_hdev
->ibh_pd
, init_qp_attr
);
793 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
794 rc
, init_qp_attr
->cap
.max_send_wr
,
795 init_qp_attr
->cap
.max_recv_wr
);
799 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
801 /* 1 ref for caller and each rxmsg */
802 atomic_set(&conn
->ibc_refcount
, 1 + IBLND_RX_MSGS(version
));
803 conn
->ibc_nrx
= IBLND_RX_MSGS(version
);
806 for (i
= 0; i
< IBLND_RX_MSGS(version
); i
++) {
807 rc
= kiblnd_post_rx(&conn
->ibc_rxs
[i
],
808 IBLND_POSTRX_NO_CREDIT
);
810 CERROR("Can't post rxmsg: %d\n", rc
);
812 /* Make posted receives complete */
813 kiblnd_abort_receives(conn
);
816 * correct # of posted buffers
817 * NB locking needed now I'm racing with completion
819 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
820 conn
->ibc_nrx
-= IBLND_RX_MSGS(version
) - i
;
821 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
824 * cmid will be destroyed by CM(ofed) after cm_callback
825 * returned, so we can't refer it anymore
826 * (by kiblnd_connd()->kiblnd_destroy_conn)
828 rdma_destroy_qp(conn
->ibc_cmid
);
829 conn
->ibc_cmid
= NULL
;
831 /* Drop my own and unused rxbuffer refcounts */
832 while (i
++ <= IBLND_RX_MSGS(version
))
833 kiblnd_conn_decref(conn
);
839 /* Init successful! */
840 LASSERT(state
== IBLND_CONN_ACTIVE_CONNECT
||
841 state
== IBLND_CONN_PASSIVE_WAIT
);
842 conn
->ibc_state
= state
;
845 atomic_inc(&net
->ibn_nconns
);
849 kiblnd_destroy_conn(conn
);
851 LIBCFS_FREE(init_qp_attr
, sizeof(*init_qp_attr
));
856 void kiblnd_destroy_conn(kib_conn_t
*conn
)
858 struct rdma_cm_id
*cmid
= conn
->ibc_cmid
;
859 kib_peer_t
*peer
= conn
->ibc_peer
;
862 LASSERT(!in_interrupt());
863 LASSERT(atomic_read(&conn
->ibc_refcount
) == 0);
864 LASSERT(list_empty(&conn
->ibc_early_rxs
));
865 LASSERT(list_empty(&conn
->ibc_tx_noops
));
866 LASSERT(list_empty(&conn
->ibc_tx_queue
));
867 LASSERT(list_empty(&conn
->ibc_tx_queue_rsrvd
));
868 LASSERT(list_empty(&conn
->ibc_tx_queue_nocred
));
869 LASSERT(list_empty(&conn
->ibc_active_txs
));
870 LASSERT(conn
->ibc_noops_posted
== 0);
871 LASSERT(conn
->ibc_nsends_posted
== 0);
873 switch (conn
->ibc_state
) {
875 /* conn must be completely disengaged from the network */
878 case IBLND_CONN_DISCONNECTED
:
879 /* connvars should have been freed already */
880 LASSERT(conn
->ibc_connvars
== NULL
);
883 case IBLND_CONN_INIT
:
887 /* conn->ibc_cmid might be destroyed by CM already */
888 if (cmid
!= NULL
&& cmid
->qp
!= NULL
)
889 rdma_destroy_qp(cmid
);
891 if (conn
->ibc_cq
!= NULL
) {
892 rc
= ib_destroy_cq(conn
->ibc_cq
);
894 CWARN("Error destroying CQ: %d\n", rc
);
897 if (conn
->ibc_rx_pages
!= NULL
)
898 kiblnd_unmap_rx_descs(conn
);
900 if (conn
->ibc_rxs
!= NULL
) {
901 LIBCFS_FREE(conn
->ibc_rxs
,
902 IBLND_RX_MSGS(conn
->ibc_version
)
906 if (conn
->ibc_connvars
!= NULL
)
907 LIBCFS_FREE(conn
->ibc_connvars
, sizeof(*conn
->ibc_connvars
));
909 if (conn
->ibc_hdev
!= NULL
)
910 kiblnd_hdev_decref(conn
->ibc_hdev
);
912 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
913 if (conn
->ibc_state
!= IBLND_CONN_INIT
) {
914 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
916 kiblnd_peer_decref(peer
);
917 rdma_destroy_id(cmid
);
918 atomic_dec(&net
->ibn_nconns
);
921 LIBCFS_FREE(conn
, sizeof(*conn
));
924 int kiblnd_close_peer_conns_locked(kib_peer_t
*peer
, int why
)
927 struct list_head
*ctmp
;
928 struct list_head
*cnxt
;
931 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
932 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
934 CDEBUG(D_NET
, "Closing conn -> %s, version: %x, reason: %d\n",
935 libcfs_nid2str(peer
->ibp_nid
),
936 conn
->ibc_version
, why
);
938 kiblnd_close_conn_locked(conn
, why
);
945 int kiblnd_close_stale_conns_locked(kib_peer_t
*peer
,
946 int version
, __u64 incarnation
)
949 struct list_head
*ctmp
;
950 struct list_head
*cnxt
;
953 list_for_each_safe(ctmp
, cnxt
, &peer
->ibp_conns
) {
954 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
956 if (conn
->ibc_version
== version
&&
957 conn
->ibc_incarnation
== incarnation
)
961 "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n",
962 libcfs_nid2str(peer
->ibp_nid
),
963 conn
->ibc_version
, conn
->ibc_incarnation
,
964 version
, incarnation
);
966 kiblnd_close_conn_locked(conn
, -ESTALE
);
973 static int kiblnd_close_matching_conns(lnet_ni_t
*ni
, lnet_nid_t nid
)
976 struct list_head
*ptmp
;
977 struct list_head
*pnxt
;
984 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
986 if (nid
!= LNET_NID_ANY
)
987 lo
= hi
= kiblnd_nid2peerlist(nid
) - kiblnd_data
.kib_peers
;
990 hi
= kiblnd_data
.kib_peer_hash_size
- 1;
993 for (i
= lo
; i
<= hi
; i
++) {
994 list_for_each_safe(ptmp
, pnxt
, &kiblnd_data
.kib_peers
[i
]) {
996 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
997 LASSERT(peer
->ibp_connecting
> 0 ||
998 peer
->ibp_accepting
> 0 ||
999 !list_empty(&peer
->ibp_conns
));
1001 if (peer
->ibp_ni
!= ni
)
1004 if (!(nid
== LNET_NID_ANY
|| nid
== peer
->ibp_nid
))
1007 count
+= kiblnd_close_peer_conns_locked(peer
, 0);
1011 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1013 /* wildcards always succeed */
1014 if (nid
== LNET_NID_ANY
)
1017 return (count
== 0) ? -ENOENT
: 0;
1020 int kiblnd_ctl(lnet_ni_t
*ni
, unsigned int cmd
, void *arg
)
1022 struct libcfs_ioctl_data
*data
= arg
;
1026 case IOC_LIBCFS_GET_PEER
: {
1030 rc
= kiblnd_get_peer_info(ni
, data
->ioc_count
,
1032 data
->ioc_nid
= nid
;
1033 data
->ioc_count
= count
;
1037 case IOC_LIBCFS_DEL_PEER
: {
1038 rc
= kiblnd_del_peer(ni
, data
->ioc_nid
);
1041 case IOC_LIBCFS_GET_CONN
: {
1045 conn
= kiblnd_get_conn_by_idx(ni
, data
->ioc_count
);
1051 LASSERT(conn
->ibc_cmid
!= NULL
);
1052 data
->ioc_nid
= conn
->ibc_peer
->ibp_nid
;
1053 if (conn
->ibc_cmid
->route
.path_rec
== NULL
)
1054 data
->ioc_u32
[0] = 0; /* iWarp has no path MTU */
1057 ib_mtu_enum_to_int(conn
->ibc_cmid
->route
.path_rec
->mtu
);
1058 kiblnd_conn_decref(conn
);
1061 case IOC_LIBCFS_CLOSE_CONNECTION
: {
1062 rc
= kiblnd_close_matching_conns(ni
, data
->ioc_nid
);
1073 void kiblnd_query(lnet_ni_t
*ni
, lnet_nid_t nid
, unsigned long *when
)
1075 unsigned long last_alive
= 0;
1076 unsigned long now
= cfs_time_current();
1077 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
1079 unsigned long flags
;
1081 read_lock_irqsave(glock
, flags
);
1083 peer
= kiblnd_find_peer_locked(nid
);
1085 LASSERT(peer
->ibp_connecting
> 0 || /* creating conns */
1086 peer
->ibp_accepting
> 0 ||
1087 !list_empty(&peer
->ibp_conns
)); /* active conn */
1088 last_alive
= peer
->ibp_last_alive
;
1091 read_unlock_irqrestore(glock
, flags
);
1093 if (last_alive
!= 0)
1097 * peer is not persistent in hash, trigger peer creation
1098 * and connection establishment with a NULL tx
1101 kiblnd_launch_tx(ni
, NULL
, nid
);
1103 CDEBUG(D_NET
, "Peer %s %p, alive %ld secs ago\n",
1104 libcfs_nid2str(nid
), peer
,
1105 last_alive
? cfs_duration_sec(now
- last_alive
) : -1);
1108 void kiblnd_free_pages(kib_pages_t
*p
)
1110 int npages
= p
->ibp_npages
;
1113 for (i
= 0; i
< npages
; i
++) {
1114 if (p
->ibp_pages
[i
] != NULL
)
1115 __free_page(p
->ibp_pages
[i
]);
1118 LIBCFS_FREE(p
, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1121 int kiblnd_alloc_pages(kib_pages_t
**pp
, int cpt
, int npages
)
1126 LIBCFS_CPT_ALLOC(p
, lnet_cpt_table(), cpt
,
1127 offsetof(kib_pages_t
, ibp_pages
[npages
]));
1129 CERROR("Can't allocate descriptor for %d pages\n", npages
);
1133 memset(p
, 0, offsetof(kib_pages_t
, ibp_pages
[npages
]));
1134 p
->ibp_npages
= npages
;
1136 for (i
= 0; i
< npages
; i
++) {
1137 p
->ibp_pages
[i
] = alloc_pages_node(
1138 cfs_cpt_spread_node(lnet_cpt_table(), cpt
),
1140 if (p
->ibp_pages
[i
] == NULL
) {
1141 CERROR("Can't allocate page %d of %d\n", i
, npages
);
1142 kiblnd_free_pages(p
);
1151 void kiblnd_unmap_rx_descs(kib_conn_t
*conn
)
1156 LASSERT(conn
->ibc_rxs
!= NULL
);
1157 LASSERT(conn
->ibc_hdev
!= NULL
);
1159 for (i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1160 rx
= &conn
->ibc_rxs
[i
];
1162 LASSERT(rx
->rx_nob
>= 0); /* not posted */
1164 kiblnd_dma_unmap_single(conn
->ibc_hdev
->ibh_ibdev
,
1165 KIBLND_UNMAP_ADDR(rx
, rx_msgunmap
,
1167 IBLND_MSG_SIZE
, DMA_FROM_DEVICE
);
1170 kiblnd_free_pages(conn
->ibc_rx_pages
);
1172 conn
->ibc_rx_pages
= NULL
;
1175 void kiblnd_map_rx_descs(kib_conn_t
*conn
)
1183 for (pg_off
= ipg
= i
= 0; i
< IBLND_RX_MSGS(conn
->ibc_version
); i
++) {
1184 pg
= conn
->ibc_rx_pages
->ibp_pages
[ipg
];
1185 rx
= &conn
->ibc_rxs
[i
];
1188 rx
->rx_msg
= (kib_msg_t
*)(((char *)page_address(pg
)) + pg_off
);
1190 rx
->rx_msgaddr
= kiblnd_dma_map_single(conn
->ibc_hdev
->ibh_ibdev
,
1194 LASSERT(!kiblnd_dma_mapping_error(conn
->ibc_hdev
->ibh_ibdev
,
1196 KIBLND_UNMAP_ADDR_SET(rx
, rx_msgunmap
, rx
->rx_msgaddr
);
1198 CDEBUG(D_NET
, "rx %d: %p %#llx(%#llx)\n",
1199 i
, rx
->rx_msg
, rx
->rx_msgaddr
,
1200 (__u64
)(page_to_phys(pg
) + pg_off
));
1202 pg_off
+= IBLND_MSG_SIZE
;
1203 LASSERT(pg_off
<= PAGE_SIZE
);
1205 if (pg_off
== PAGE_SIZE
) {
1208 LASSERT(ipg
<= IBLND_RX_MSG_PAGES(conn
->ibc_version
));
1213 static void kiblnd_unmap_tx_pool(kib_tx_pool_t
*tpo
)
1215 kib_hca_dev_t
*hdev
= tpo
->tpo_hdev
;
1219 LASSERT(tpo
->tpo_pool
.po_allocated
== 0);
1224 for (i
= 0; i
< tpo
->tpo_pool
.po_size
; i
++) {
1225 tx
= &tpo
->tpo_tx_descs
[i
];
1226 kiblnd_dma_unmap_single(hdev
->ibh_ibdev
,
1227 KIBLND_UNMAP_ADDR(tx
, tx_msgunmap
,
1229 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1232 kiblnd_hdev_decref(hdev
);
1233 tpo
->tpo_hdev
= NULL
;
1236 static kib_hca_dev_t
*kiblnd_current_hdev(kib_dev_t
*dev
)
1238 kib_hca_dev_t
*hdev
;
1239 unsigned long flags
;
1242 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1243 while (dev
->ibd_failover
) {
1244 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1246 CDEBUG(D_NET
, "%s: Wait for failover\n",
1248 schedule_timeout(cfs_time_seconds(1) / 100);
1250 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1253 kiblnd_hdev_addref_locked(dev
->ibd_hdev
);
1254 hdev
= dev
->ibd_hdev
;
1256 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1261 static void kiblnd_map_tx_pool(kib_tx_pool_t
*tpo
)
1263 kib_pages_t
*txpgs
= tpo
->tpo_tx_pages
;
1264 kib_pool_t
*pool
= &tpo
->tpo_pool
;
1265 kib_net_t
*net
= pool
->po_owner
->ps_net
;
1273 LASSERT(net
!= NULL
);
1277 /* pre-mapped messages are not bigger than 1 page */
1278 CLASSERT(IBLND_MSG_SIZE
<= PAGE_SIZE
);
1280 /* No fancy arithmetic when we do the buffer calculations */
1281 CLASSERT(PAGE_SIZE
% IBLND_MSG_SIZE
== 0);
1283 tpo
->tpo_hdev
= kiblnd_current_hdev(dev
);
1285 for (ipage
= page_offset
= i
= 0; i
< pool
->po_size
; i
++) {
1286 page
= txpgs
->ibp_pages
[ipage
];
1287 tx
= &tpo
->tpo_tx_descs
[i
];
1289 tx
->tx_msg
= (kib_msg_t
*)(((char *)page_address(page
)) +
1292 tx
->tx_msgaddr
= kiblnd_dma_map_single(
1293 tpo
->tpo_hdev
->ibh_ibdev
, tx
->tx_msg
,
1294 IBLND_MSG_SIZE
, DMA_TO_DEVICE
);
1295 LASSERT(!kiblnd_dma_mapping_error(tpo
->tpo_hdev
->ibh_ibdev
,
1297 KIBLND_UNMAP_ADDR_SET(tx
, tx_msgunmap
, tx
->tx_msgaddr
);
1299 list_add(&tx
->tx_list
, &pool
->po_free_list
);
1301 page_offset
+= IBLND_MSG_SIZE
;
1302 LASSERT(page_offset
<= PAGE_SIZE
);
1304 if (page_offset
== PAGE_SIZE
) {
1307 LASSERT(ipage
<= txpgs
->ibp_npages
);
1312 struct ib_mr
*kiblnd_find_dma_mr(kib_hca_dev_t
*hdev
, __u64 addr
, __u64 size
)
1316 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1318 if (hdev
->ibh_nmrs
== 1)
1319 return hdev
->ibh_mrs
[0];
1321 index
= addr
>> hdev
->ibh_mr_shift
;
1323 if (index
< hdev
->ibh_nmrs
&&
1324 index
== ((addr
+ size
- 1) >> hdev
->ibh_mr_shift
))
1325 return hdev
->ibh_mrs
[index
];
1330 struct ib_mr
*kiblnd_find_rd_dma_mr(kib_hca_dev_t
*hdev
, kib_rdma_desc_t
*rd
)
1332 struct ib_mr
*prev_mr
;
1336 LASSERT(hdev
->ibh_mrs
[0] != NULL
);
1338 if (*kiblnd_tunables
.kib_map_on_demand
> 0 &&
1339 *kiblnd_tunables
.kib_map_on_demand
<= rd
->rd_nfrags
)
1342 if (hdev
->ibh_nmrs
== 1)
1343 return hdev
->ibh_mrs
[0];
1345 for (i
= 0, mr
= prev_mr
= NULL
;
1346 i
< rd
->rd_nfrags
; i
++) {
1347 mr
= kiblnd_find_dma_mr(hdev
,
1348 rd
->rd_frags
[i
].rf_addr
,
1349 rd
->rd_frags
[i
].rf_nob
);
1350 if (prev_mr
== NULL
)
1353 if (mr
== NULL
|| prev_mr
!= mr
) {
1354 /* Can't covered by one single MR */
1363 static void kiblnd_destroy_fmr_pool(kib_fmr_pool_t
*pool
)
1365 LASSERT(pool
->fpo_map_count
== 0);
1367 if (pool
->fpo_fmr_pool
!= NULL
)
1368 ib_destroy_fmr_pool(pool
->fpo_fmr_pool
);
1370 if (pool
->fpo_hdev
!= NULL
)
1371 kiblnd_hdev_decref(pool
->fpo_hdev
);
1373 LIBCFS_FREE(pool
, sizeof(*pool
));
1376 static void kiblnd_destroy_fmr_pool_list(struct list_head
*head
)
1378 kib_fmr_pool_t
*pool
;
1380 while (!list_empty(head
)) {
1381 pool
= list_entry(head
->next
, kib_fmr_pool_t
, fpo_list
);
1382 list_del(&pool
->fpo_list
);
1383 kiblnd_destroy_fmr_pool(pool
);
1387 static int kiblnd_fmr_pool_size(int ncpts
)
1389 int size
= *kiblnd_tunables
.kib_fmr_pool_size
/ ncpts
;
1391 return max(IBLND_FMR_POOL
, size
);
1394 static int kiblnd_fmr_flush_trigger(int ncpts
)
1396 int size
= *kiblnd_tunables
.kib_fmr_flush_trigger
/ ncpts
;
1398 return max(IBLND_FMR_POOL_FLUSH
, size
);
1401 static int kiblnd_create_fmr_pool(kib_fmr_poolset_t
*fps
,
1402 kib_fmr_pool_t
**pp_fpo
)
1404 /* FMR pool for RDMA */
1405 kib_dev_t
*dev
= fps
->fps_net
->ibn_dev
;
1406 kib_fmr_pool_t
*fpo
;
1407 struct ib_fmr_pool_param param
= {
1408 .max_pages_per_fmr
= LNET_MAX_PAYLOAD
/PAGE_SIZE
,
1409 .page_shift
= PAGE_SHIFT
,
1410 .access
= (IB_ACCESS_LOCAL_WRITE
|
1411 IB_ACCESS_REMOTE_WRITE
),
1412 .pool_size
= fps
->fps_pool_size
,
1413 .dirty_watermark
= fps
->fps_flush_trigger
,
1414 .flush_function
= NULL
,
1416 .cache
= !!*kiblnd_tunables
.kib_fmr_cache
};
1419 LIBCFS_CPT_ALLOC(fpo
, lnet_cpt_table(), fps
->fps_cpt
, sizeof(*fpo
));
1423 fpo
->fpo_hdev
= kiblnd_current_hdev(dev
);
1425 fpo
->fpo_fmr_pool
= ib_create_fmr_pool(fpo
->fpo_hdev
->ibh_pd
, ¶m
);
1426 if (IS_ERR(fpo
->fpo_fmr_pool
)) {
1427 rc
= PTR_ERR(fpo
->fpo_fmr_pool
);
1428 CERROR("Failed to create FMR pool: %d\n", rc
);
1430 kiblnd_hdev_decref(fpo
->fpo_hdev
);
1431 LIBCFS_FREE(fpo
, sizeof(*fpo
));
1435 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1436 fpo
->fpo_owner
= fps
;
1442 static void kiblnd_fail_fmr_poolset(kib_fmr_poolset_t
*fps
,
1443 struct list_head
*zombies
)
1445 if (fps
->fps_net
== NULL
) /* intialized? */
1448 spin_lock(&fps
->fps_lock
);
1450 while (!list_empty(&fps
->fps_pool_list
)) {
1451 kib_fmr_pool_t
*fpo
= list_entry(fps
->fps_pool_list
.next
,
1452 kib_fmr_pool_t
, fpo_list
);
1453 fpo
->fpo_failed
= 1;
1454 list_del(&fpo
->fpo_list
);
1455 if (fpo
->fpo_map_count
== 0)
1456 list_add(&fpo
->fpo_list
, zombies
);
1458 list_add(&fpo
->fpo_list
, &fps
->fps_failed_pool_list
);
1461 spin_unlock(&fps
->fps_lock
);
1464 static void kiblnd_fini_fmr_poolset(kib_fmr_poolset_t
*fps
)
1466 if (fps
->fps_net
!= NULL
) { /* initialized? */
1467 kiblnd_destroy_fmr_pool_list(&fps
->fps_failed_pool_list
);
1468 kiblnd_destroy_fmr_pool_list(&fps
->fps_pool_list
);
1472 static int kiblnd_init_fmr_poolset(kib_fmr_poolset_t
*fps
, int cpt
,
1473 kib_net_t
*net
, int pool_size
,
1476 kib_fmr_pool_t
*fpo
;
1479 memset(fps
, 0, sizeof(*fps
));
1483 fps
->fps_pool_size
= pool_size
;
1484 fps
->fps_flush_trigger
= flush_trigger
;
1485 spin_lock_init(&fps
->fps_lock
);
1486 INIT_LIST_HEAD(&fps
->fps_pool_list
);
1487 INIT_LIST_HEAD(&fps
->fps_failed_pool_list
);
1489 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1491 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1496 static int kiblnd_fmr_pool_is_idle(kib_fmr_pool_t
*fpo
, unsigned long now
)
1498 if (fpo
->fpo_map_count
!= 0) /* still in use */
1500 if (fpo
->fpo_failed
)
1502 return cfs_time_aftereq(now
, fpo
->fpo_deadline
);
1505 void kiblnd_fmr_pool_unmap(kib_fmr_t
*fmr
, int status
)
1508 kib_fmr_pool_t
*fpo
= fmr
->fmr_pool
;
1509 kib_fmr_poolset_t
*fps
= fpo
->fpo_owner
;
1510 unsigned long now
= cfs_time_current();
1511 kib_fmr_pool_t
*tmp
;
1514 rc
= ib_fmr_pool_unmap(fmr
->fmr_pfmr
);
1518 rc
= ib_flush_fmr_pool(fpo
->fpo_fmr_pool
);
1522 fmr
->fmr_pool
= NULL
;
1523 fmr
->fmr_pfmr
= NULL
;
1525 spin_lock(&fps
->fps_lock
);
1526 fpo
->fpo_map_count
--; /* decref the pool */
1528 list_for_each_entry_safe(fpo
, tmp
, &fps
->fps_pool_list
, fpo_list
) {
1529 /* the first pool is persistent */
1530 if (fps
->fps_pool_list
.next
== &fpo
->fpo_list
)
1533 if (kiblnd_fmr_pool_is_idle(fpo
, now
)) {
1534 list_move(&fpo
->fpo_list
, &zombies
);
1538 spin_unlock(&fps
->fps_lock
);
1540 if (!list_empty(&zombies
))
1541 kiblnd_destroy_fmr_pool_list(&zombies
);
1544 int kiblnd_fmr_pool_map(kib_fmr_poolset_t
*fps
, __u64
*pages
, int npages
,
1545 __u64 iov
, kib_fmr_t
*fmr
)
1547 struct ib_pool_fmr
*pfmr
;
1548 kib_fmr_pool_t
*fpo
;
1553 spin_lock(&fps
->fps_lock
);
1554 version
= fps
->fps_version
;
1555 list_for_each_entry(fpo
, &fps
->fps_pool_list
, fpo_list
) {
1556 fpo
->fpo_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1557 fpo
->fpo_map_count
++;
1558 spin_unlock(&fps
->fps_lock
);
1560 pfmr
= ib_fmr_pool_map_phys(fpo
->fpo_fmr_pool
,
1561 pages
, npages
, iov
);
1562 if (likely(!IS_ERR(pfmr
))) {
1563 fmr
->fmr_pool
= fpo
;
1564 fmr
->fmr_pfmr
= pfmr
;
1568 spin_lock(&fps
->fps_lock
);
1569 fpo
->fpo_map_count
--;
1570 if (PTR_ERR(pfmr
) != -EAGAIN
) {
1571 spin_unlock(&fps
->fps_lock
);
1572 return PTR_ERR(pfmr
);
1575 /* EAGAIN and ... */
1576 if (version
!= fps
->fps_version
) {
1577 spin_unlock(&fps
->fps_lock
);
1582 if (fps
->fps_increasing
) {
1583 spin_unlock(&fps
->fps_lock
);
1584 CDEBUG(D_NET
, "Another thread is allocating new FMR pool, waiting for her to complete\n");
1590 if (time_before(cfs_time_current(), fps
->fps_next_retry
)) {
1591 /* someone failed recently */
1592 spin_unlock(&fps
->fps_lock
);
1596 fps
->fps_increasing
= 1;
1597 spin_unlock(&fps
->fps_lock
);
1599 CDEBUG(D_NET
, "Allocate new FMR pool\n");
1600 rc
= kiblnd_create_fmr_pool(fps
, &fpo
);
1601 spin_lock(&fps
->fps_lock
);
1602 fps
->fps_increasing
= 0;
1605 list_add_tail(&fpo
->fpo_list
, &fps
->fps_pool_list
);
1607 fps
->fps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1609 spin_unlock(&fps
->fps_lock
);
1614 static void kiblnd_fini_pool(kib_pool_t
*pool
)
1616 LASSERT(list_empty(&pool
->po_free_list
));
1617 LASSERT(pool
->po_allocated
== 0);
1619 CDEBUG(D_NET
, "Finalize %s pool\n", pool
->po_owner
->ps_name
);
1622 static void kiblnd_init_pool(kib_poolset_t
*ps
, kib_pool_t
*pool
, int size
)
1624 CDEBUG(D_NET
, "Initialize %s pool\n", ps
->ps_name
);
1626 memset(pool
, 0, sizeof(*pool
));
1627 INIT_LIST_HEAD(&pool
->po_free_list
);
1628 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1629 pool
->po_owner
= ps
;
1630 pool
->po_size
= size
;
1633 static void kiblnd_destroy_pool_list(struct list_head
*head
)
1637 while (!list_empty(head
)) {
1638 pool
= list_entry(head
->next
, kib_pool_t
, po_list
);
1639 list_del(&pool
->po_list
);
1641 LASSERT(pool
->po_owner
!= NULL
);
1642 pool
->po_owner
->ps_pool_destroy(pool
);
1646 static void kiblnd_fail_poolset(kib_poolset_t
*ps
, struct list_head
*zombies
)
1648 if (ps
->ps_net
== NULL
) /* intialized? */
1651 spin_lock(&ps
->ps_lock
);
1652 while (!list_empty(&ps
->ps_pool_list
)) {
1653 kib_pool_t
*po
= list_entry(ps
->ps_pool_list
.next
,
1654 kib_pool_t
, po_list
);
1656 list_del(&po
->po_list
);
1657 if (po
->po_allocated
== 0)
1658 list_add(&po
->po_list
, zombies
);
1660 list_add(&po
->po_list
, &ps
->ps_failed_pool_list
);
1662 spin_unlock(&ps
->ps_lock
);
1665 static void kiblnd_fini_poolset(kib_poolset_t
*ps
)
1667 if (ps
->ps_net
!= NULL
) { /* initialized? */
1668 kiblnd_destroy_pool_list(&ps
->ps_failed_pool_list
);
1669 kiblnd_destroy_pool_list(&ps
->ps_pool_list
);
1673 static int kiblnd_init_poolset(kib_poolset_t
*ps
, int cpt
,
1674 kib_net_t
*net
, char *name
, int size
,
1675 kib_ps_pool_create_t po_create
,
1676 kib_ps_pool_destroy_t po_destroy
,
1677 kib_ps_node_init_t nd_init
,
1678 kib_ps_node_fini_t nd_fini
)
1683 memset(ps
, 0, sizeof(*ps
));
1687 ps
->ps_pool_create
= po_create
;
1688 ps
->ps_pool_destroy
= po_destroy
;
1689 ps
->ps_node_init
= nd_init
;
1690 ps
->ps_node_fini
= nd_fini
;
1691 ps
->ps_pool_size
= size
;
1692 if (strlcpy(ps
->ps_name
, name
, sizeof(ps
->ps_name
))
1693 >= sizeof(ps
->ps_name
))
1695 spin_lock_init(&ps
->ps_lock
);
1696 INIT_LIST_HEAD(&ps
->ps_pool_list
);
1697 INIT_LIST_HEAD(&ps
->ps_failed_pool_list
);
1699 rc
= ps
->ps_pool_create(ps
, size
, &pool
);
1701 list_add(&pool
->po_list
, &ps
->ps_pool_list
);
1703 CERROR("Failed to create the first pool for %s\n", ps
->ps_name
);
1708 static int kiblnd_pool_is_idle(kib_pool_t
*pool
, unsigned long now
)
1710 if (pool
->po_allocated
!= 0) /* still in use */
1712 if (pool
->po_failed
)
1714 return cfs_time_aftereq(now
, pool
->po_deadline
);
1717 void kiblnd_pool_free_node(kib_pool_t
*pool
, struct list_head
*node
)
1720 kib_poolset_t
*ps
= pool
->po_owner
;
1722 unsigned long now
= cfs_time_current();
1724 spin_lock(&ps
->ps_lock
);
1726 if (ps
->ps_node_fini
!= NULL
)
1727 ps
->ps_node_fini(pool
, node
);
1729 LASSERT(pool
->po_allocated
> 0);
1730 list_add(node
, &pool
->po_free_list
);
1731 pool
->po_allocated
--;
1733 list_for_each_entry_safe(pool
, tmp
, &ps
->ps_pool_list
, po_list
) {
1734 /* the first pool is persistent */
1735 if (ps
->ps_pool_list
.next
== &pool
->po_list
)
1738 if (kiblnd_pool_is_idle(pool
, now
))
1739 list_move(&pool
->po_list
, &zombies
);
1741 spin_unlock(&ps
->ps_lock
);
1743 if (!list_empty(&zombies
))
1744 kiblnd_destroy_pool_list(&zombies
);
1747 struct list_head
*kiblnd_pool_alloc_node(kib_poolset_t
*ps
)
1749 struct list_head
*node
;
1754 spin_lock(&ps
->ps_lock
);
1755 list_for_each_entry(pool
, &ps
->ps_pool_list
, po_list
) {
1756 if (list_empty(&pool
->po_free_list
))
1759 pool
->po_allocated
++;
1760 pool
->po_deadline
= cfs_time_shift(IBLND_POOL_DEADLINE
);
1761 node
= pool
->po_free_list
.next
;
1764 if (ps
->ps_node_init
!= NULL
) {
1765 /* still hold the lock */
1766 ps
->ps_node_init(pool
, node
);
1768 spin_unlock(&ps
->ps_lock
);
1772 /* no available tx pool and ... */
1773 if (ps
->ps_increasing
) {
1774 /* another thread is allocating a new pool */
1775 spin_unlock(&ps
->ps_lock
);
1776 CDEBUG(D_NET
, "Another thread is allocating new %s pool, waiting for her to complete\n",
1782 if (time_before(cfs_time_current(), ps
->ps_next_retry
)) {
1783 /* someone failed recently */
1784 spin_unlock(&ps
->ps_lock
);
1788 ps
->ps_increasing
= 1;
1789 spin_unlock(&ps
->ps_lock
);
1791 CDEBUG(D_NET
, "%s pool exhausted, allocate new pool\n", ps
->ps_name
);
1793 rc
= ps
->ps_pool_create(ps
, ps
->ps_pool_size
, &pool
);
1795 spin_lock(&ps
->ps_lock
);
1796 ps
->ps_increasing
= 0;
1798 list_add_tail(&pool
->po_list
, &ps
->ps_pool_list
);
1800 ps
->ps_next_retry
= cfs_time_shift(IBLND_POOL_RETRY
);
1801 CERROR("Can't allocate new %s pool because out of memory\n",
1804 spin_unlock(&ps
->ps_lock
);
1809 static void kiblnd_destroy_tx_pool(kib_pool_t
*pool
)
1811 kib_tx_pool_t
*tpo
= container_of(pool
, kib_tx_pool_t
, tpo_pool
);
1814 LASSERT(pool
->po_allocated
== 0);
1816 if (tpo
->tpo_tx_pages
!= NULL
) {
1817 kiblnd_unmap_tx_pool(tpo
);
1818 kiblnd_free_pages(tpo
->tpo_tx_pages
);
1821 if (tpo
->tpo_tx_descs
== NULL
)
1824 for (i
= 0; i
< pool
->po_size
; i
++) {
1825 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1827 list_del(&tx
->tx_list
);
1828 if (tx
->tx_pages
!= NULL
)
1829 LIBCFS_FREE(tx
->tx_pages
,
1831 sizeof(*tx
->tx_pages
));
1832 if (tx
->tx_frags
!= NULL
)
1833 LIBCFS_FREE(tx
->tx_frags
,
1834 IBLND_MAX_RDMA_FRAGS
*
1835 sizeof(*tx
->tx_frags
));
1836 if (tx
->tx_wrq
!= NULL
)
1837 LIBCFS_FREE(tx
->tx_wrq
,
1838 (1 + IBLND_MAX_RDMA_FRAGS
) *
1839 sizeof(*tx
->tx_wrq
));
1840 if (tx
->tx_sge
!= NULL
)
1841 LIBCFS_FREE(tx
->tx_sge
,
1842 (1 + IBLND_MAX_RDMA_FRAGS
) *
1843 sizeof(*tx
->tx_sge
));
1844 if (tx
->tx_rd
!= NULL
)
1845 LIBCFS_FREE(tx
->tx_rd
,
1846 offsetof(kib_rdma_desc_t
,
1847 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1850 LIBCFS_FREE(tpo
->tpo_tx_descs
,
1851 pool
->po_size
* sizeof(kib_tx_t
));
1853 kiblnd_fini_pool(pool
);
1854 LIBCFS_FREE(tpo
, sizeof(*tpo
));
1857 static int kiblnd_tx_pool_size(int ncpts
)
1859 int ntx
= *kiblnd_tunables
.kib_ntx
/ ncpts
;
1861 return max(IBLND_TX_POOL
, ntx
);
1864 static int kiblnd_create_tx_pool(kib_poolset_t
*ps
, int size
,
1872 LIBCFS_CPT_ALLOC(tpo
, lnet_cpt_table(), ps
->ps_cpt
, sizeof(*tpo
));
1874 CERROR("Failed to allocate TX pool\n");
1878 pool
= &tpo
->tpo_pool
;
1879 kiblnd_init_pool(ps
, pool
, size
);
1880 tpo
->tpo_tx_descs
= NULL
;
1881 tpo
->tpo_tx_pages
= NULL
;
1883 npg
= (size
* IBLND_MSG_SIZE
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1884 if (kiblnd_alloc_pages(&tpo
->tpo_tx_pages
, ps
->ps_cpt
, npg
) != 0) {
1885 CERROR("Can't allocate tx pages: %d\n", npg
);
1886 LIBCFS_FREE(tpo
, sizeof(*tpo
));
1890 LIBCFS_CPT_ALLOC(tpo
->tpo_tx_descs
, lnet_cpt_table(), ps
->ps_cpt
,
1891 size
* sizeof(kib_tx_t
));
1892 if (tpo
->tpo_tx_descs
== NULL
) {
1893 CERROR("Can't allocate %d tx descriptors\n", size
);
1894 ps
->ps_pool_destroy(pool
);
1898 memset(tpo
->tpo_tx_descs
, 0, size
* sizeof(kib_tx_t
));
1900 for (i
= 0; i
< size
; i
++) {
1901 kib_tx_t
*tx
= &tpo
->tpo_tx_descs
[i
];
1904 if (ps
->ps_net
->ibn_fmr_ps
!= NULL
) {
1905 LIBCFS_CPT_ALLOC(tx
->tx_pages
,
1906 lnet_cpt_table(), ps
->ps_cpt
,
1907 LNET_MAX_IOV
* sizeof(*tx
->tx_pages
));
1908 if (tx
->tx_pages
== NULL
)
1912 LIBCFS_CPT_ALLOC(tx
->tx_frags
, lnet_cpt_table(), ps
->ps_cpt
,
1913 IBLND_MAX_RDMA_FRAGS
* sizeof(*tx
->tx_frags
));
1914 if (tx
->tx_frags
== NULL
)
1917 sg_init_table(tx
->tx_frags
, IBLND_MAX_RDMA_FRAGS
);
1919 LIBCFS_CPT_ALLOC(tx
->tx_wrq
, lnet_cpt_table(), ps
->ps_cpt
,
1920 (1 + IBLND_MAX_RDMA_FRAGS
) *
1921 sizeof(*tx
->tx_wrq
));
1922 if (tx
->tx_wrq
== NULL
)
1925 LIBCFS_CPT_ALLOC(tx
->tx_sge
, lnet_cpt_table(), ps
->ps_cpt
,
1926 (1 + IBLND_MAX_RDMA_FRAGS
) *
1927 sizeof(*tx
->tx_sge
));
1928 if (tx
->tx_sge
== NULL
)
1931 LIBCFS_CPT_ALLOC(tx
->tx_rd
, lnet_cpt_table(), ps
->ps_cpt
,
1932 offsetof(kib_rdma_desc_t
,
1933 rd_frags
[IBLND_MAX_RDMA_FRAGS
]));
1934 if (tx
->tx_rd
== NULL
)
1939 kiblnd_map_tx_pool(tpo
);
1944 ps
->ps_pool_destroy(pool
);
1948 static void kiblnd_tx_init(kib_pool_t
*pool
, struct list_head
*node
)
1950 kib_tx_poolset_t
*tps
= container_of(pool
->po_owner
, kib_tx_poolset_t
,
1952 kib_tx_t
*tx
= list_entry(node
, kib_tx_t
, tx_list
);
1954 tx
->tx_cookie
= tps
->tps_next_tx_cookie
++;
1957 static void kiblnd_net_fini_pools(kib_net_t
*net
)
1961 cfs_cpt_for_each(i
, lnet_cpt_table()) {
1962 kib_tx_poolset_t
*tps
;
1963 kib_fmr_poolset_t
*fps
;
1965 if (net
->ibn_tx_ps
!= NULL
) {
1966 tps
= net
->ibn_tx_ps
[i
];
1967 kiblnd_fini_poolset(&tps
->tps_poolset
);
1970 if (net
->ibn_fmr_ps
!= NULL
) {
1971 fps
= net
->ibn_fmr_ps
[i
];
1972 kiblnd_fini_fmr_poolset(fps
);
1976 if (net
->ibn_tx_ps
!= NULL
) {
1977 cfs_percpt_free(net
->ibn_tx_ps
);
1978 net
->ibn_tx_ps
= NULL
;
1981 if (net
->ibn_fmr_ps
!= NULL
) {
1982 cfs_percpt_free(net
->ibn_fmr_ps
);
1983 net
->ibn_fmr_ps
= NULL
;
1987 static int kiblnd_net_init_pools(kib_net_t
*net
, __u32
*cpts
, int ncpts
)
1989 unsigned long flags
;
1994 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1995 if (*kiblnd_tunables
.kib_map_on_demand
== 0 &&
1996 net
->ibn_dev
->ibd_hdev
->ibh_nmrs
== 1) {
1997 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1998 goto create_tx_pool
;
2001 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2003 if (*kiblnd_tunables
.kib_fmr_pool_size
<
2004 *kiblnd_tunables
.kib_ntx
/ 4) {
2005 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2006 *kiblnd_tunables
.kib_fmr_pool_size
,
2007 *kiblnd_tunables
.kib_ntx
/ 4);
2013 * TX pool must be created later than FMR, see LU-2268
2016 LASSERT(net
->ibn_tx_ps
== NULL
);
2019 * premapping can fail if ibd_nmr > 1, so we always create
2020 * FMR pool and map-on-demand if premapping failed
2023 net
->ibn_fmr_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2024 sizeof(kib_fmr_poolset_t
));
2025 if (net
->ibn_fmr_ps
== NULL
) {
2026 CERROR("Failed to allocate FMR pool array\n");
2031 for (i
= 0; i
< ncpts
; i
++) {
2032 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2033 rc
= kiblnd_init_fmr_poolset(net
->ibn_fmr_ps
[cpt
], cpt
, net
,
2034 kiblnd_fmr_pool_size(ncpts
),
2035 kiblnd_fmr_flush_trigger(ncpts
));
2036 if (rc
== -ENOSYS
&& i
== 0) /* no FMR */
2039 if (rc
!= 0) { /* a real error */
2040 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2047 LASSERT(i
== ncpts
);
2048 goto create_tx_pool
;
2051 cfs_percpt_free(net
->ibn_fmr_ps
);
2052 net
->ibn_fmr_ps
= NULL
;
2054 CWARN("Device does not support FMR\n");
2058 net
->ibn_tx_ps
= cfs_percpt_alloc(lnet_cpt_table(),
2059 sizeof(kib_tx_poolset_t
));
2060 if (net
->ibn_tx_ps
== NULL
) {
2061 CERROR("Failed to allocate tx pool array\n");
2066 for (i
= 0; i
< ncpts
; i
++) {
2067 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2068 rc
= kiblnd_init_poolset(&net
->ibn_tx_ps
[cpt
]->tps_poolset
,
2070 kiblnd_tx_pool_size(ncpts
),
2071 kiblnd_create_tx_pool
,
2072 kiblnd_destroy_tx_pool
,
2073 kiblnd_tx_init
, NULL
);
2075 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2083 kiblnd_net_fini_pools(net
);
2088 static int kiblnd_hdev_get_attr(kib_hca_dev_t
*hdev
)
2091 * It's safe to assume a HCA can handle a page size
2092 * matching that of the native system
2094 hdev
->ibh_page_shift
= PAGE_SHIFT
;
2095 hdev
->ibh_page_size
= 1 << PAGE_SHIFT
;
2096 hdev
->ibh_page_mask
= ~((__u64
)hdev
->ibh_page_size
- 1);
2098 hdev
->ibh_mr_size
= hdev
->ibh_ibdev
->attrs
.max_mr_size
;
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 */
2198 * XXX: it's UGLY, but I don't have better way to find
2199 * ib-bonding HCA failover because:
2201 * a. no reliable CM event for HCA failover...
2202 * b. no OFED API to get ib_device for current net_device...
2204 * We have only two choices at this point:
2206 * a. rdma_bind_addr(), it will conflict with listener cmid
2207 * b. rdma_resolve_addr() to zero addr
2209 cmid
= kiblnd_rdma_create_id(kiblnd_dummy_callback
, dev
, RDMA_PS_TCP
,
2213 CERROR("Failed to create cmid for failover: %d\n", rc
);
2217 memset(&srcaddr
, 0, sizeof(srcaddr
));
2218 srcaddr
.sin_family
= AF_INET
;
2219 srcaddr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2221 memset(&dstaddr
, 0, sizeof(dstaddr
));
2222 dstaddr
.sin_family
= AF_INET
;
2223 rc
= rdma_resolve_addr(cmid
, (struct sockaddr
*)&srcaddr
,
2224 (struct sockaddr
*)&dstaddr
, 1);
2225 if (rc
!= 0 || cmid
->device
== NULL
) {
2226 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2227 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2229 rdma_destroy_id(cmid
);
2233 rc
= dev
->ibd_hdev
->ibh_ibdev
!= cmid
->device
; /* true for failover */
2234 rdma_destroy_id(cmid
);
2239 int kiblnd_dev_failover(kib_dev_t
*dev
)
2241 LIST_HEAD(zombie_tpo
);
2242 LIST_HEAD(zombie_ppo
);
2243 LIST_HEAD(zombie_fpo
);
2244 struct rdma_cm_id
*cmid
= NULL
;
2245 kib_hca_dev_t
*hdev
= NULL
;
2248 struct sockaddr_in addr
;
2249 unsigned long flags
;
2253 LASSERT(*kiblnd_tunables
.kib_dev_failover
> 1 ||
2254 dev
->ibd_can_failover
|| dev
->ibd_hdev
== NULL
);
2256 rc
= kiblnd_dev_need_failover(dev
);
2260 if (dev
->ibd_hdev
!= NULL
&&
2261 dev
->ibd_hdev
->ibh_cmid
!= NULL
) {
2263 * XXX it's not good to close old listener at here,
2264 * because we can fail to create new listener.
2265 * But we have to close it now, otherwise rdma_bind_addr
2266 * will return EADDRINUSE... How crap!
2268 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2270 cmid
= dev
->ibd_hdev
->ibh_cmid
;
2272 * make next schedule of kiblnd_dev_need_failover()
2275 dev
->ibd_hdev
->ibh_cmid
= NULL
;
2276 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2278 rdma_destroy_id(cmid
);
2281 cmid
= kiblnd_rdma_create_id(kiblnd_cm_callback
, dev
, RDMA_PS_TCP
,
2285 CERROR("Failed to create cmid for failover: %d\n", rc
);
2289 memset(&addr
, 0, sizeof(addr
));
2290 addr
.sin_family
= AF_INET
;
2291 addr
.sin_addr
.s_addr
= (__force u32
)htonl(dev
->ibd_ifip
);
2292 addr
.sin_port
= htons(*kiblnd_tunables
.kib_service
);
2294 /* Bind to failover device or port */
2295 rc
= rdma_bind_addr(cmid
, (struct sockaddr
*)&addr
);
2296 if (rc
!= 0 || cmid
->device
== NULL
) {
2297 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2298 dev
->ibd_ifname
, &dev
->ibd_ifip
,
2300 rdma_destroy_id(cmid
);
2304 LIBCFS_ALLOC(hdev
, sizeof(*hdev
));
2306 CERROR("Failed to allocate kib_hca_dev\n");
2307 rdma_destroy_id(cmid
);
2312 atomic_set(&hdev
->ibh_ref
, 1);
2313 hdev
->ibh_dev
= dev
;
2314 hdev
->ibh_cmid
= cmid
;
2315 hdev
->ibh_ibdev
= cmid
->device
;
2317 pd
= ib_alloc_pd(cmid
->device
);
2320 CERROR("Can't allocate PD: %d\n", rc
);
2326 rc
= rdma_listen(cmid
, 0);
2328 CERROR("Can't start new listener: %d\n", rc
);
2332 rc
= kiblnd_hdev_setup_mrs(hdev
);
2334 CERROR("Can't setup device: %d\n", rc
);
2338 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2340 swap(dev
->ibd_hdev
, hdev
); /* take over the refcount */
2342 list_for_each_entry(net
, &dev
->ibd_nets
, ibn_list
) {
2343 cfs_cpt_for_each(i
, lnet_cpt_table()) {
2344 kiblnd_fail_poolset(&net
->ibn_tx_ps
[i
]->tps_poolset
,
2347 if (net
->ibn_fmr_ps
)
2348 kiblnd_fail_fmr_poolset(net
->ibn_fmr_ps
[i
],
2353 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2355 if (!list_empty(&zombie_tpo
))
2356 kiblnd_destroy_pool_list(&zombie_tpo
);
2357 if (!list_empty(&zombie_ppo
))
2358 kiblnd_destroy_pool_list(&zombie_ppo
);
2359 if (!list_empty(&zombie_fpo
))
2360 kiblnd_destroy_fmr_pool_list(&zombie_fpo
);
2362 kiblnd_hdev_decref(hdev
);
2365 dev
->ibd_failed_failover
++;
2367 dev
->ibd_failed_failover
= 0;
2372 void kiblnd_destroy_dev(kib_dev_t
*dev
)
2374 LASSERT(dev
->ibd_nnets
== 0);
2375 LASSERT(list_empty(&dev
->ibd_nets
));
2377 list_del(&dev
->ibd_fail_list
);
2378 list_del(&dev
->ibd_list
);
2380 if (dev
->ibd_hdev
!= NULL
)
2381 kiblnd_hdev_decref(dev
->ibd_hdev
);
2383 LIBCFS_FREE(dev
, sizeof(*dev
));
2386 static kib_dev_t
*kiblnd_create_dev(char *ifname
)
2388 struct net_device
*netdev
;
2395 rc
= lnet_ipif_query(ifname
, &up
, &ip
, &netmask
);
2397 CERROR("Can't query IPoIB interface %s: %d\n",
2403 CERROR("Can't query IPoIB interface %s: it's down\n", ifname
);
2407 LIBCFS_ALLOC(dev
, sizeof(*dev
));
2411 netdev
= dev_get_by_name(&init_net
, ifname
);
2412 if (netdev
== NULL
) {
2413 dev
->ibd_can_failover
= 0;
2415 dev
->ibd_can_failover
= !!(netdev
->flags
& IFF_MASTER
);
2419 INIT_LIST_HEAD(&dev
->ibd_nets
);
2420 INIT_LIST_HEAD(&dev
->ibd_list
); /* not yet in kib_devs */
2421 INIT_LIST_HEAD(&dev
->ibd_fail_list
);
2423 strcpy(&dev
->ibd_ifname
[0], ifname
);
2425 /* initialize the device */
2426 rc
= kiblnd_dev_failover(dev
);
2428 CERROR("Can't initialize device: %d\n", rc
);
2429 LIBCFS_FREE(dev
, sizeof(*dev
));
2433 list_add_tail(&dev
->ibd_list
, &kiblnd_data
.kib_devs
);
2437 static void kiblnd_base_shutdown(void)
2439 struct kib_sched_info
*sched
;
2442 LASSERT(list_empty(&kiblnd_data
.kib_devs
));
2444 switch (kiblnd_data
.kib_init
) {
2448 case IBLND_INIT_ALL
:
2449 case IBLND_INIT_DATA
:
2450 LASSERT(kiblnd_data
.kib_peers
!= NULL
);
2451 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++)
2452 LASSERT(list_empty(&kiblnd_data
.kib_peers
[i
]));
2453 LASSERT(list_empty(&kiblnd_data
.kib_connd_zombies
));
2454 LASSERT(list_empty(&kiblnd_data
.kib_connd_conns
));
2456 /* flag threads to terminate; wake and wait for them to die */
2457 kiblnd_data
.kib_shutdown
= 1;
2460 * NB: we really want to stop scheduler threads net by net
2461 * instead of the whole module, this should be improved
2462 * with dynamic configuration LNet
2464 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
)
2465 wake_up_all(&sched
->ibs_waitq
);
2467 wake_up_all(&kiblnd_data
.kib_connd_waitq
);
2468 wake_up_all(&kiblnd_data
.kib_failover_waitq
);
2471 while (atomic_read(&kiblnd_data
.kib_nthreads
) != 0) {
2474 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
,
2475 "Waiting for %d threads to terminate\n",
2476 atomic_read(&kiblnd_data
.kib_nthreads
));
2477 set_current_state(TASK_UNINTERRUPTIBLE
);
2478 schedule_timeout(cfs_time_seconds(1));
2483 case IBLND_INIT_NOTHING
:
2487 if (kiblnd_data
.kib_peers
!= NULL
) {
2488 LIBCFS_FREE(kiblnd_data
.kib_peers
,
2489 sizeof(struct list_head
) *
2490 kiblnd_data
.kib_peer_hash_size
);
2493 if (kiblnd_data
.kib_scheds
!= NULL
)
2494 cfs_percpt_free(kiblnd_data
.kib_scheds
);
2496 kiblnd_data
.kib_init
= IBLND_INIT_NOTHING
;
2497 module_put(THIS_MODULE
);
2500 void kiblnd_shutdown(lnet_ni_t
*ni
)
2502 kib_net_t
*net
= ni
->ni_data
;
2503 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
2505 unsigned long flags
;
2507 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_ALL
);
2512 write_lock_irqsave(g_lock
, flags
);
2513 net
->ibn_shutdown
= 1;
2514 write_unlock_irqrestore(g_lock
, flags
);
2516 switch (net
->ibn_init
) {
2520 case IBLND_INIT_ALL
:
2521 /* nuke all existing peers within this net */
2522 kiblnd_del_peer(ni
, LNET_NID_ANY
);
2524 /* Wait for all peer state to clean up */
2526 while (atomic_read(&net
->ibn_npeers
) != 0) {
2528 CDEBUG(((i
& (-i
)) == i
) ? D_WARNING
: D_NET
, /* 2**n? */
2529 "%s: waiting for %d peers to disconnect\n",
2530 libcfs_nid2str(ni
->ni_nid
),
2531 atomic_read(&net
->ibn_npeers
));
2532 set_current_state(TASK_UNINTERRUPTIBLE
);
2533 schedule_timeout(cfs_time_seconds(1));
2536 kiblnd_net_fini_pools(net
);
2538 write_lock_irqsave(g_lock
, flags
);
2539 LASSERT(net
->ibn_dev
->ibd_nnets
> 0);
2540 net
->ibn_dev
->ibd_nnets
--;
2541 list_del(&net
->ibn_list
);
2542 write_unlock_irqrestore(g_lock
, flags
);
2546 case IBLND_INIT_NOTHING
:
2547 LASSERT(atomic_read(&net
->ibn_nconns
) == 0);
2549 if (net
->ibn_dev
!= NULL
&&
2550 net
->ibn_dev
->ibd_nnets
== 0)
2551 kiblnd_destroy_dev(net
->ibn_dev
);
2556 net
->ibn_init
= IBLND_INIT_NOTHING
;
2559 LIBCFS_FREE(net
, sizeof(*net
));
2562 if (list_empty(&kiblnd_data
.kib_devs
))
2563 kiblnd_base_shutdown();
2566 static int kiblnd_base_startup(void)
2568 struct kib_sched_info
*sched
;
2572 LASSERT(kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
);
2574 try_module_get(THIS_MODULE
);
2575 /* zero pointers, flags etc */
2576 memset(&kiblnd_data
, 0, sizeof(kiblnd_data
));
2578 rwlock_init(&kiblnd_data
.kib_global_lock
);
2580 INIT_LIST_HEAD(&kiblnd_data
.kib_devs
);
2581 INIT_LIST_HEAD(&kiblnd_data
.kib_failed_devs
);
2583 kiblnd_data
.kib_peer_hash_size
= IBLND_PEER_HASH_SIZE
;
2584 LIBCFS_ALLOC(kiblnd_data
.kib_peers
,
2585 sizeof(struct list_head
) * kiblnd_data
.kib_peer_hash_size
);
2586 if (kiblnd_data
.kib_peers
== NULL
)
2588 for (i
= 0; i
< kiblnd_data
.kib_peer_hash_size
; i
++)
2589 INIT_LIST_HEAD(&kiblnd_data
.kib_peers
[i
]);
2591 spin_lock_init(&kiblnd_data
.kib_connd_lock
);
2592 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_conns
);
2593 INIT_LIST_HEAD(&kiblnd_data
.kib_connd_zombies
);
2594 init_waitqueue_head(&kiblnd_data
.kib_connd_waitq
);
2595 init_waitqueue_head(&kiblnd_data
.kib_failover_waitq
);
2597 kiblnd_data
.kib_scheds
= cfs_percpt_alloc(lnet_cpt_table(),
2599 if (kiblnd_data
.kib_scheds
== NULL
)
2602 cfs_percpt_for_each(sched
, i
, kiblnd_data
.kib_scheds
) {
2605 spin_lock_init(&sched
->ibs_lock
);
2606 INIT_LIST_HEAD(&sched
->ibs_conns
);
2607 init_waitqueue_head(&sched
->ibs_waitq
);
2609 nthrs
= cfs_cpt_weight(lnet_cpt_table(), i
);
2610 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2611 nthrs
= min(nthrs
, *kiblnd_tunables
.kib_nscheds
);
2614 * max to half of CPUs, another half is reserved for
2615 * upper layer modules
2617 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2620 sched
->ibs_nthreads_max
= nthrs
;
2624 kiblnd_data
.kib_error_qpa
.qp_state
= IB_QPS_ERR
;
2626 /* lists/ptrs/locks initialised */
2627 kiblnd_data
.kib_init
= IBLND_INIT_DATA
;
2628 /*****************************************************/
2630 rc
= kiblnd_thread_start(kiblnd_connd
, NULL
, "kiblnd_connd");
2632 CERROR("Can't spawn o2iblnd connd: %d\n", rc
);
2636 if (*kiblnd_tunables
.kib_dev_failover
!= 0)
2637 rc
= kiblnd_thread_start(kiblnd_failover_thread
, NULL
,
2641 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc
);
2645 /* flag everything initialised */
2646 kiblnd_data
.kib_init
= IBLND_INIT_ALL
;
2647 /*****************************************************/
2652 kiblnd_base_shutdown();
2656 static int kiblnd_start_schedulers(struct kib_sched_info
*sched
)
2662 if (sched
->ibs_nthreads
== 0) {
2663 if (*kiblnd_tunables
.kib_nscheds
> 0) {
2664 nthrs
= sched
->ibs_nthreads_max
;
2666 nthrs
= cfs_cpt_weight(lnet_cpt_table(),
2668 nthrs
= min(max(IBLND_N_SCHED
, nthrs
>> 1), nthrs
);
2669 nthrs
= min(IBLND_N_SCHED_HIGH
, nthrs
);
2672 LASSERT(sched
->ibs_nthreads
<= sched
->ibs_nthreads_max
);
2673 /* increase one thread if there is new interface */
2674 nthrs
= sched
->ibs_nthreads
< sched
->ibs_nthreads_max
;
2677 for (i
= 0; i
< nthrs
; i
++) {
2681 id
= KIB_THREAD_ID(sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
);
2682 snprintf(name
, sizeof(name
), "kiblnd_sd_%02ld_%02ld",
2683 KIB_THREAD_CPT(id
), KIB_THREAD_TID(id
));
2684 rc
= kiblnd_thread_start(kiblnd_scheduler
, (void *)id
, name
);
2688 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2689 sched
->ibs_cpt
, sched
->ibs_nthreads
+ i
, rc
);
2693 sched
->ibs_nthreads
+= i
;
2697 static int kiblnd_dev_start_threads(kib_dev_t
*dev
, int newdev
, __u32
*cpts
,
2704 for (i
= 0; i
< ncpts
; i
++) {
2705 struct kib_sched_info
*sched
;
2707 cpt
= (cpts
== NULL
) ? i
: cpts
[i
];
2708 sched
= kiblnd_data
.kib_scheds
[cpt
];
2710 if (!newdev
&& sched
->ibs_nthreads
> 0)
2713 rc
= kiblnd_start_schedulers(kiblnd_data
.kib_scheds
[cpt
]);
2715 CERROR("Failed to start scheduler threads for %s\n",
2723 static kib_dev_t
*kiblnd_dev_search(char *ifname
)
2725 kib_dev_t
*alias
= NULL
;
2730 colon
= strchr(ifname
, ':');
2731 list_for_each_entry(dev
, &kiblnd_data
.kib_devs
, ibd_list
) {
2732 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2738 colon2
= strchr(dev
->ibd_ifname
, ':');
2744 if (strcmp(&dev
->ibd_ifname
[0], ifname
) == 0)
2755 int kiblnd_startup(lnet_ni_t
*ni
)
2758 kib_dev_t
*ibdev
= NULL
;
2760 struct timespec64 tv
;
2761 unsigned long flags
;
2765 LASSERT(ni
->ni_lnd
== &the_o2iblnd
);
2767 if (kiblnd_data
.kib_init
== IBLND_INIT_NOTHING
) {
2768 rc
= kiblnd_base_startup();
2773 LIBCFS_ALLOC(net
, sizeof(*net
));
2778 ktime_get_real_ts64(&tv
);
2779 net
->ibn_incarnation
= tv
.tv_sec
* USEC_PER_SEC
+
2780 tv
.tv_nsec
/ NSEC_PER_USEC
;
2782 ni
->ni_peertimeout
= *kiblnd_tunables
.kib_peertimeout
;
2783 ni
->ni_maxtxcredits
= *kiblnd_tunables
.kib_credits
;
2784 ni
->ni_peertxcredits
= *kiblnd_tunables
.kib_peertxcredits
;
2785 ni
->ni_peerrtrcredits
= *kiblnd_tunables
.kib_peerrtrcredits
;
2787 if (ni
->ni_interfaces
[0] != NULL
) {
2788 /* Use the IPoIB interface specified in 'networks=' */
2790 CLASSERT(LNET_MAX_INTERFACES
> 1);
2791 if (ni
->ni_interfaces
[1] != NULL
) {
2792 CERROR("Multiple interfaces not supported\n");
2796 ifname
= ni
->ni_interfaces
[0];
2798 ifname
= *kiblnd_tunables
.kib_default_ipif
;
2801 if (strlen(ifname
) >= sizeof(ibdev
->ibd_ifname
)) {
2802 CERROR("IPoIB interface name too long: %s\n", ifname
);
2806 ibdev
= kiblnd_dev_search(ifname
);
2808 newdev
= ibdev
== NULL
;
2809 /* hmm...create kib_dev even for alias */
2810 if (ibdev
== NULL
|| strcmp(&ibdev
->ibd_ifname
[0], ifname
) != 0)
2811 ibdev
= kiblnd_create_dev(ifname
);
2816 net
->ibn_dev
= ibdev
;
2817 ni
->ni_nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
), ibdev
->ibd_ifip
);
2819 rc
= kiblnd_dev_start_threads(ibdev
, newdev
,
2820 ni
->ni_cpts
, ni
->ni_ncpts
);
2824 rc
= kiblnd_net_init_pools(net
, ni
->ni_cpts
, ni
->ni_ncpts
);
2826 CERROR("Failed to initialize NI pools: %d\n", rc
);
2830 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2832 list_add_tail(&net
->ibn_list
, &ibdev
->ibd_nets
);
2833 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2835 net
->ibn_init
= IBLND_INIT_ALL
;
2840 if (net
->ibn_dev
== NULL
&& ibdev
!= NULL
)
2841 kiblnd_destroy_dev(ibdev
);
2844 kiblnd_shutdown(ni
);
2846 CDEBUG(D_NET
, "kiblnd_startup failed\n");
2850 static void __exit
kiblnd_module_fini(void)
2852 lnet_unregister_lnd(&the_o2iblnd
);
2855 static int __init
kiblnd_module_init(void)
2859 CLASSERT(sizeof(kib_msg_t
) <= IBLND_MSG_SIZE
);
2860 CLASSERT(offsetof(kib_msg_t
,
2861 ibm_u
.get
.ibgm_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2863 CLASSERT(offsetof(kib_msg_t
,
2864 ibm_u
.putack
.ibpam_rd
.rd_frags
[IBLND_MAX_RDMA_FRAGS
])
2867 rc
= kiblnd_tunables_init();
2871 lnet_register_lnd(&the_o2iblnd
);
2876 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2877 MODULE_DESCRIPTION("Kernel OpenIB gen2 LND v2.00");
2878 MODULE_LICENSE("GPL");
2880 module_init(kiblnd_module_init
);
2881 module_exit(kiblnd_module_fini
);