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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd.c
34 * Author: Eric Barton <eric@bartonsoftware.com>
38 #include <linux/inetdevice.h>
42 static const struct lnet_lnd the_o2iblnd;
44 struct kib_data kiblnd_data;
47 kiblnd_cksum (void *ptr, int nob)
53 sum = ((sum << 1) | (sum >> 31)) + *c++;
55 /* ensure I don't return 0 (== no checksum) */
56 return (sum == 0) ? 1 : sum;
60 kiblnd_msgtype2str(int type)
63 case IBLND_MSG_CONNREQ:
66 case IBLND_MSG_CONNACK:
72 case IBLND_MSG_IMMEDIATE:
75 case IBLND_MSG_PUT_REQ:
78 case IBLND_MSG_PUT_NAK:
81 case IBLND_MSG_PUT_ACK:
84 case IBLND_MSG_PUT_DONE:
87 case IBLND_MSG_GET_REQ:
90 case IBLND_MSG_GET_DONE:
99 kiblnd_msgtype2size(int type)
101 const int hdr_size = offsetof(struct kib_msg, ibm_u);
104 case IBLND_MSG_CONNREQ:
105 case IBLND_MSG_CONNACK:
106 return hdr_size + sizeof(struct kib_connparams);
111 case IBLND_MSG_IMMEDIATE:
112 return offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[0]);
114 case IBLND_MSG_PUT_REQ:
115 return hdr_size + sizeof(struct kib_putreq_msg);
117 case IBLND_MSG_PUT_ACK:
118 return hdr_size + sizeof(struct kib_putack_msg);
120 case IBLND_MSG_GET_REQ:
121 return hdr_size + sizeof(struct kib_get_msg);
123 case IBLND_MSG_PUT_NAK:
124 case IBLND_MSG_PUT_DONE:
125 case IBLND_MSG_GET_DONE:
126 return hdr_size + sizeof(struct kib_completion_msg);
132 static int kiblnd_unpack_rd(struct kib_msg *msg, int flip)
134 struct kib_rdma_desc *rd;
139 LASSERT (msg->ibm_type == IBLND_MSG_GET_REQ ||
140 msg->ibm_type == IBLND_MSG_PUT_ACK);
142 rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
143 &msg->ibm_u.get.ibgm_rd :
144 &msg->ibm_u.putack.ibpam_rd;
147 __swab32s(&rd->rd_key);
148 __swab32s(&rd->rd_nfrags);
153 if (n <= 0 || n > IBLND_MAX_RDMA_FRAGS) {
154 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
155 n, IBLND_MAX_RDMA_FRAGS);
159 nob = offsetof(struct kib_msg, ibm_u) +
160 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
162 if (msg->ibm_nob < nob) {
163 CERROR("Short %s: %d(%d)\n",
164 kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
171 for (i = 0; i < n; i++) {
172 __swab32s(&rd->rd_frags[i].rf_nob);
173 __swab64s(&rd->rd_frags[i].rf_addr);
179 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
180 int credits, lnet_nid_t dstnid, __u64 dststamp)
182 struct kib_net *net = ni->ni_data;
184 /* CAVEAT EMPTOR! all message fields not set here should have been
185 * initialised previously. */
186 msg->ibm_magic = IBLND_MSG_MAGIC;
187 msg->ibm_version = version;
189 msg->ibm_credits = credits;
192 msg->ibm_srcnid = ni->ni_nid;
193 msg->ibm_srcstamp = net->ibn_incarnation;
194 msg->ibm_dstnid = dstnid;
195 msg->ibm_dststamp = dststamp;
197 if (*kiblnd_tunables.kib_cksum) {
198 /* NB ibm_cksum zero while computing cksum */
199 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
203 int kiblnd_unpack_msg(struct kib_msg *msg, int nob)
205 const int hdr_size = offsetof(struct kib_msg, ibm_u);
211 /* 6 bytes are enough to have received magic + version */
213 CERROR("Short message: %d\n", nob);
217 if (msg->ibm_magic == IBLND_MSG_MAGIC) {
219 } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
222 CERROR("Bad magic: %08x\n", msg->ibm_magic);
226 version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
227 if (version != IBLND_MSG_VERSION &&
228 version != IBLND_MSG_VERSION_1) {
229 CERROR("Bad version: %x\n", version);
233 if (nob < hdr_size) {
234 CERROR("Short message: %d\n", nob);
238 msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
240 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
244 /* checksum must be computed with ibm_cksum zero and BEFORE anything
246 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
248 if (msg_cksum != 0 &&
249 msg_cksum != kiblnd_cksum(msg, msg_nob)) {
250 CERROR("Bad checksum\n");
254 msg->ibm_cksum = msg_cksum;
257 /* leave magic unflipped as a clue to peer_ni endianness */
258 msg->ibm_version = version;
259 BUILD_BUG_ON(sizeof(msg->ibm_type) != 1);
260 BUILD_BUG_ON(sizeof(msg->ibm_credits) != 1);
261 msg->ibm_nob = msg_nob;
262 __swab64s(&msg->ibm_srcnid);
263 __swab64s(&msg->ibm_srcstamp);
264 __swab64s(&msg->ibm_dstnid);
265 __swab64s(&msg->ibm_dststamp);
268 if (msg->ibm_srcnid == LNET_NID_ANY) {
269 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg->ibm_srcnid));
273 if (msg_nob < kiblnd_msgtype2size(msg->ibm_type)) {
274 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg->ibm_type),
275 msg_nob, kiblnd_msgtype2size(msg->ibm_type));
279 switch (msg->ibm_type) {
281 CERROR("Unknown message type %x\n", msg->ibm_type);
285 case IBLND_MSG_IMMEDIATE:
286 case IBLND_MSG_PUT_REQ:
289 case IBLND_MSG_PUT_ACK:
290 case IBLND_MSG_GET_REQ:
291 if (kiblnd_unpack_rd(msg, flip))
295 case IBLND_MSG_PUT_NAK:
296 case IBLND_MSG_PUT_DONE:
297 case IBLND_MSG_GET_DONE:
299 __swab32s(&msg->ibm_u.completion.ibcm_status);
302 case IBLND_MSG_CONNREQ:
303 case IBLND_MSG_CONNACK:
305 __swab16s(&msg->ibm_u.connparams.ibcp_queue_depth);
306 __swab16s(&msg->ibm_u.connparams.ibcp_max_frags);
307 __swab32s(&msg->ibm_u.connparams.ibcp_max_msg_size);
315 kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
318 struct kib_peer_ni *peer_ni;
319 struct kib_net *net = ni->ni_data;
320 int cpt = lnet_cpt_of_nid(nid, ni);
323 LASSERT(net != NULL);
324 LASSERT(nid != LNET_NID_ANY);
326 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
327 if (peer_ni == NULL) {
328 CERROR("Cannot allocate peer_ni\n");
332 peer_ni->ibp_ni = ni;
333 peer_ni->ibp_nid = nid;
334 peer_ni->ibp_error = 0;
335 peer_ni->ibp_last_alive = 0;
336 peer_ni->ibp_max_frags = IBLND_MAX_RDMA_FRAGS;
337 peer_ni->ibp_queue_depth = ni->ni_net->net_tunables.lct_peer_tx_credits;
338 peer_ni->ibp_queue_depth_mod = 0; /* try to use the default */
339 atomic_set(&peer_ni->ibp_refcount, 1); /* 1 ref for caller */
341 INIT_LIST_HEAD(&peer_ni->ibp_list); /* not in the peer_ni table yet */
342 INIT_LIST_HEAD(&peer_ni->ibp_conns);
343 INIT_LIST_HEAD(&peer_ni->ibp_tx_queue);
345 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
347 /* always called with a ref on ni, which prevents ni being shutdown */
348 LASSERT(net->ibn_shutdown == 0);
350 /* npeers only grows with the global lock held */
351 atomic_inc(&net->ibn_npeers);
353 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
360 kiblnd_destroy_peer(struct kib_peer_ni *peer_ni)
362 struct kib_net *net = peer_ni->ibp_ni->ni_data;
364 LASSERT(net != NULL);
365 LASSERT (atomic_read(&peer_ni->ibp_refcount) == 0);
366 LASSERT(!kiblnd_peer_active(peer_ni));
367 LASSERT(kiblnd_peer_idle(peer_ni));
368 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
370 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
372 /* NB a peer_ni's connections keep a reference on their peer_ni until
373 * they are destroyed, so we can be assured that _all_ state to do
374 * with this peer_ni has been cleaned up when its refcount drops to
376 if (atomic_dec_and_test(&net->ibn_npeers))
377 wake_up_var(&net->ibn_npeers);
381 kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid)
383 /* the caller is responsible for accounting the additional reference
384 * that this creates */
385 struct list_head *peer_list = kiblnd_nid2peerlist(nid);
386 struct list_head *tmp;
387 struct kib_peer_ni *peer_ni;
389 list_for_each(tmp, peer_list) {
391 peer_ni = list_entry(tmp, struct kib_peer_ni, ibp_list);
392 LASSERT(!kiblnd_peer_idle(peer_ni));
395 * Match a peer if its NID and the NID of the local NI it
396 * communicates over are the same. Otherwise don't match
397 * the peer, which will result in a new lnd peer being
400 if (peer_ni->ibp_nid != nid ||
401 peer_ni->ibp_ni->ni_nid != ni->ni_nid)
404 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d) version: %x\n",
405 peer_ni, libcfs_nid2str(nid),
406 atomic_read(&peer_ni->ibp_refcount),
407 peer_ni->ibp_version);
414 kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni)
416 LASSERT(list_empty(&peer_ni->ibp_conns));
418 LASSERT (kiblnd_peer_active(peer_ni));
419 list_del_init(&peer_ni->ibp_list);
420 /* lose peerlist's ref */
421 kiblnd_peer_decref(peer_ni);
425 kiblnd_get_peer_info(struct lnet_ni *ni, int index,
426 lnet_nid_t *nidp, int *count)
428 struct kib_peer_ni *peer_ni;
429 struct list_head *ptmp;
433 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
435 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
437 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
439 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
440 LASSERT(!kiblnd_peer_idle(peer_ni));
442 if (peer_ni->ibp_ni != ni)
448 *nidp = peer_ni->ibp_nid;
449 *count = atomic_read(&peer_ni->ibp_refcount);
451 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
457 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
462 kiblnd_del_peer_locked(struct kib_peer_ni *peer_ni)
464 struct kib_conn *cnxt;
465 struct kib_conn *conn;
467 if (list_empty(&peer_ni->ibp_conns)) {
468 kiblnd_unlink_peer_locked(peer_ni);
470 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
472 kiblnd_close_conn_locked(conn, 0);
473 /* NB closing peer_ni's last conn unlinked it. */
475 /* NB peer_ni now unlinked; might even be freed if the peer_ni table had the
480 kiblnd_del_peer(struct lnet_ni *ni, lnet_nid_t nid)
483 struct list_head *ptmp;
484 struct list_head *pnxt;
485 struct kib_peer_ni *peer_ni;
492 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
494 if (nid != LNET_NID_ANY) {
495 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
498 hi = kiblnd_data.kib_peer_hash_size - 1;
501 for (i = lo; i <= hi; i++) {
502 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
503 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
504 LASSERT(!kiblnd_peer_idle(peer_ni));
506 if (peer_ni->ibp_ni != ni)
509 if (!(nid == LNET_NID_ANY || peer_ni->ibp_nid == nid))
512 if (!list_empty(&peer_ni->ibp_tx_queue)) {
513 LASSERT(list_empty(&peer_ni->ibp_conns));
515 list_splice_init(&peer_ni->ibp_tx_queue,
519 kiblnd_del_peer_locked(peer_ni);
520 rc = 0; /* matched something */
524 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
526 kiblnd_txlist_done(&zombies, -EIO, LNET_MSG_STATUS_LOCAL_ERROR);
531 static struct kib_conn *
532 kiblnd_get_conn_by_idx(struct lnet_ni *ni, int index)
534 struct kib_peer_ni *peer_ni;
535 struct list_head *ptmp;
536 struct kib_conn *conn;
537 struct list_head *ctmp;
541 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
543 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
544 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
546 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
547 LASSERT(!kiblnd_peer_idle(peer_ni));
549 if (peer_ni->ibp_ni != ni)
552 list_for_each(ctmp, &peer_ni->ibp_conns) {
556 conn = list_entry(ctmp, struct kib_conn, ibc_list);
557 kiblnd_conn_addref(conn);
558 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
565 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
570 kiblnd_debug_rx(struct kib_rx *rx)
572 CDEBUG(D_CONSOLE, " %p msg_type %x cred %d\n",
573 rx, rx->rx_msg->ibm_type,
574 rx->rx_msg->ibm_credits);
578 kiblnd_debug_tx(struct kib_tx *tx)
580 CDEBUG(D_CONSOLE, " %p snd %d q %d w %d rc %d dl %lld "
581 "cookie %#llx msg %s%s type %x cred %d\n",
582 tx, tx->tx_sending, tx->tx_queued, tx->tx_waiting,
583 tx->tx_status, ktime_to_ns(tx->tx_deadline), tx->tx_cookie,
584 tx->tx_lntmsg[0] == NULL ? "-" : "!",
585 tx->tx_lntmsg[1] == NULL ? "-" : "!",
586 tx->tx_msg->ibm_type, tx->tx_msg->ibm_credits);
590 kiblnd_debug_conn(struct kib_conn *conn)
592 struct list_head *tmp;
595 spin_lock(&conn->ibc_lock);
597 CDEBUG(D_CONSOLE, "conn[%d] %p [version %x] -> %s:\n",
598 atomic_read(&conn->ibc_refcount), conn,
599 conn->ibc_version, libcfs_nid2str(conn->ibc_peer->ibp_nid));
600 CDEBUG(D_CONSOLE, " state %d nposted %d/%d cred %d o_cred %d "
601 " r_cred %d\n", conn->ibc_state, conn->ibc_noops_posted,
602 conn->ibc_nsends_posted, conn->ibc_credits,
603 conn->ibc_outstanding_credits, conn->ibc_reserved_credits);
604 CDEBUG(D_CONSOLE, " comms_err %d\n", conn->ibc_comms_error);
606 CDEBUG(D_CONSOLE, " early_rxs:\n");
607 list_for_each(tmp, &conn->ibc_early_rxs)
608 kiblnd_debug_rx(list_entry(tmp, struct kib_rx, rx_list));
610 CDEBUG(D_CONSOLE, " tx_noops:\n");
611 list_for_each(tmp, &conn->ibc_tx_noops)
612 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
614 CDEBUG(D_CONSOLE, " tx_queue_nocred:\n");
615 list_for_each(tmp, &conn->ibc_tx_queue_nocred)
616 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
618 CDEBUG(D_CONSOLE, " tx_queue_rsrvd:\n");
619 list_for_each(tmp, &conn->ibc_tx_queue_rsrvd)
620 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
622 CDEBUG(D_CONSOLE, " tx_queue:\n");
623 list_for_each(tmp, &conn->ibc_tx_queue)
624 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
626 CDEBUG(D_CONSOLE, " active_txs:\n");
627 list_for_each(tmp, &conn->ibc_active_txs)
628 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
630 CDEBUG(D_CONSOLE, " rxs:\n");
631 for (i = 0; i < IBLND_RX_MSGS(conn); i++)
632 kiblnd_debug_rx(&conn->ibc_rxs[i]);
634 spin_unlock(&conn->ibc_lock);
638 kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
640 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
641 if (cmid->route.path_rec == NULL)
644 if (*kiblnd_tunables.kib_ib_mtu)
645 cmid->route.path_rec->mtu =
646 ib_mtu_int_to_enum(*kiblnd_tunables.kib_ib_mtu);
650 kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
658 vectors = conn->ibc_cmid->device->num_comp_vectors;
662 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
664 /* hash NID to CPU id in this partition... */
665 ibp_nid = conn->ibc_peer->ibp_nid;
666 off = do_div(ibp_nid, cpumask_weight(*mask));
667 for_each_cpu(i, *mask) {
677 * Get the scheduler bound to this CPT. If the scheduler has no
678 * threads, which means that the CPT has no CPUs, then grab the
679 * next scheduler that we can use.
681 * This case would be triggered if a NUMA node is configured with
682 * no associated CPUs.
684 static struct kib_sched_info *
685 kiblnd_get_scheduler(int cpt)
687 struct kib_sched_info *sched;
690 sched = kiblnd_data.kib_scheds[cpt];
692 if (sched->ibs_nthreads > 0)
695 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
696 if (sched->ibs_nthreads > 0) {
697 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
698 cpt, sched->ibs_cpt);
706 static unsigned int kiblnd_send_wrs(struct kib_conn *conn)
709 * One WR for the LNet message
710 * And ibc_max_frags for the transfer WRs
713 int multiplier = 1 + conn->ibc_max_frags;
714 enum kib_dev_caps dev_caps = conn->ibc_hdev->ibh_dev->ibd_dev_caps;
716 /* FastReg needs two extra WRs for map and invalidate */
717 if (dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)
720 /* account for a maximum of ibc_queue_depth in-flight transfers */
721 ret = multiplier * conn->ibc_queue_depth;
723 if (ret > conn->ibc_hdev->ibh_max_qp_wr) {
724 CDEBUG(D_NET, "peer_credits %u will result in send work "
725 "request size %d larger than maximum %d device "
726 "can handle\n", conn->ibc_queue_depth, ret,
727 conn->ibc_hdev->ibh_max_qp_wr);
728 conn->ibc_queue_depth =
729 conn->ibc_hdev->ibh_max_qp_wr / multiplier;
732 /* don't go beyond the maximum the device can handle */
733 return min(ret, conn->ibc_hdev->ibh_max_qp_wr);
737 kiblnd_create_conn(struct kib_peer_ni *peer_ni, struct rdma_cm_id *cmid,
738 int state, int version)
741 * If the new conn is created successfully it takes over the caller's
742 * ref on 'peer_ni'. It also "owns" 'cmid' and destroys it when it itself
743 * is destroyed. On failure, the caller's ref on 'peer_ni' remains and
744 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
745 * to destroy 'cmid' here since I'm called from the CM which still has
746 * its ref on 'cmid'). */
747 rwlock_t *glock = &kiblnd_data.kib_global_lock;
748 struct kib_net *net = peer_ni->ibp_ni->ni_data;
750 struct ib_qp_init_attr init_qp_attr = {};
751 struct kib_sched_info *sched;
752 #ifdef HAVE_IB_CQ_INIT_ATTR
753 struct ib_cq_init_attr cq_attr = {};
755 struct kib_conn *conn;
762 LASSERT(net != NULL);
763 LASSERT(!in_interrupt());
767 cpt = lnet_cpt_of_nid(peer_ni->ibp_nid, peer_ni->ibp_ni);
768 sched = kiblnd_get_scheduler(cpt);
771 CERROR("no schedulers available. node is unhealthy\n");
776 * The cpt might have changed if we ended up selecting a non cpt
777 * native scheduler. So use the scheduler's cpt instead.
779 cpt = sched->ibs_cpt;
781 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
783 CERROR("Can't allocate connection for %s\n",
784 libcfs_nid2str(peer_ni->ibp_nid));
788 conn->ibc_state = IBLND_CONN_INIT;
789 conn->ibc_version = version;
790 conn->ibc_peer = peer_ni; /* I take the caller's ref */
791 cmid->context = conn; /* for future CM callbacks */
792 conn->ibc_cmid = cmid;
793 conn->ibc_max_frags = peer_ni->ibp_max_frags;
794 conn->ibc_queue_depth = peer_ni->ibp_queue_depth;
795 conn->ibc_rxs = NULL;
796 conn->ibc_rx_pages = NULL;
798 INIT_LIST_HEAD(&conn->ibc_early_rxs);
799 INIT_LIST_HEAD(&conn->ibc_tx_noops);
800 INIT_LIST_HEAD(&conn->ibc_tx_queue);
801 INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
802 INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
803 INIT_LIST_HEAD(&conn->ibc_active_txs);
804 INIT_LIST_HEAD(&conn->ibc_zombie_txs);
805 spin_lock_init(&conn->ibc_lock);
807 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
808 sizeof(*conn->ibc_connvars));
809 if (conn->ibc_connvars == NULL) {
810 CERROR("Can't allocate in-progress connection state\n");
814 write_lock_irqsave(glock, flags);
815 if (dev->ibd_failover) {
816 write_unlock_irqrestore(glock, flags);
817 CERROR("%s: failover in progress\n", dev->ibd_ifname);
821 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
822 /* wakeup failover thread and teardown connection */
823 if (kiblnd_dev_can_failover(dev)) {
824 list_add_tail(&dev->ibd_fail_list,
825 &kiblnd_data.kib_failed_devs);
826 wake_up(&kiblnd_data.kib_failover_waitq);
829 write_unlock_irqrestore(glock, flags);
830 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
831 cmid->device->name, dev->ibd_ifname);
835 kiblnd_hdev_addref_locked(dev->ibd_hdev);
836 conn->ibc_hdev = dev->ibd_hdev;
838 kiblnd_setup_mtu_locked(cmid);
840 write_unlock_irqrestore(glock, flags);
842 #ifdef HAVE_IB_CQ_INIT_ATTR
843 cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
844 cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
845 cq = ib_create_cq(cmid->device,
846 kiblnd_cq_completion, kiblnd_cq_event, conn,
849 cq = ib_create_cq(cmid->device,
850 kiblnd_cq_completion, kiblnd_cq_event, conn,
851 IBLND_CQ_ENTRIES(conn),
852 kiblnd_get_completion_vector(conn, cpt));
856 * on MLX-5 (possibly MLX-4 as well) this error could be
857 * hit if the concurrent_sends and/or peer_tx_credits is set
858 * too high. Or due to an MLX-5 bug which tries to
859 * allocate 256kb via kmalloc for WR cookie array
861 CERROR("Failed to create CQ with %d CQEs: %ld\n",
862 IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
868 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
870 CERROR("Can't request completion notification: %d\n", rc);
874 init_qp_attr.event_handler = kiblnd_qp_event;
875 init_qp_attr.qp_context = conn;
876 init_qp_attr.cap.max_send_sge = *kiblnd_tunables.kib_wrq_sge;
877 init_qp_attr.cap.max_recv_sge = 1;
878 init_qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
879 init_qp_attr.qp_type = IB_QPT_RC;
880 init_qp_attr.send_cq = cq;
881 init_qp_attr.recv_cq = cq;
883 if (peer_ni->ibp_queue_depth_mod &&
884 peer_ni->ibp_queue_depth_mod < peer_ni->ibp_queue_depth) {
885 conn->ibc_queue_depth = peer_ni->ibp_queue_depth_mod;
886 CDEBUG(D_NET, "Use reduced queue depth %u (from %u)\n",
887 peer_ni->ibp_queue_depth_mod,
888 peer_ni->ibp_queue_depth);
892 /* kiblnd_send_wrs() can change the connection's queue depth if
893 * the maximum work requests for the device is maxed out
895 init_qp_attr.cap.max_send_wr = kiblnd_send_wrs(conn);
896 init_qp_attr.cap.max_recv_wr = IBLND_RECV_WRS(conn);
897 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd,
899 if (rc != -ENOMEM || conn->ibc_queue_depth < 2)
901 conn->ibc_queue_depth--;
905 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d, "
906 "send_sge: %d, recv_sge: %d\n",
907 rc, init_qp_attr.cap.max_send_wr,
908 init_qp_attr.cap.max_recv_wr,
909 init_qp_attr.cap.max_send_sge,
910 init_qp_attr.cap.max_recv_sge);
914 conn->ibc_sched = sched;
916 if (!peer_ni->ibp_queue_depth_mod &&
917 conn->ibc_queue_depth != peer_ni->ibp_queue_depth) {
918 CWARN("peer %s - queue depth reduced from %u to %u"
919 " to allow for qp creation\n",
920 libcfs_nid2str(peer_ni->ibp_nid),
921 peer_ni->ibp_queue_depth,
922 conn->ibc_queue_depth);
923 peer_ni->ibp_queue_depth_mod = conn->ibc_queue_depth;
926 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
927 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
928 if (conn->ibc_rxs == NULL) {
929 CERROR("Cannot allocate RX buffers\n");
933 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
934 IBLND_RX_MSG_PAGES(conn));
938 kiblnd_map_rx_descs(conn);
940 /* 1 ref for caller and each rxmsg */
941 atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
942 conn->ibc_nrx = IBLND_RX_MSGS(conn);
945 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
946 rc = kiblnd_post_rx(&conn->ibc_rxs[i], IBLND_POSTRX_NO_CREDIT);
948 CERROR("Can't post rxmsg: %d\n", rc);
950 /* Make posted receives complete */
951 kiblnd_abort_receives(conn);
953 /* correct # of posted buffers
954 * NB locking needed now I'm racing with completion */
955 spin_lock_irqsave(&sched->ibs_lock, flags);
956 conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i;
957 spin_unlock_irqrestore(&sched->ibs_lock, flags);
959 /* cmid will be destroyed by CM(ofed) after cm_callback
960 * returned, so we can't refer it anymore
961 * (by kiblnd_connd()->kiblnd_destroy_conn) */
962 rdma_destroy_qp(conn->ibc_cmid);
963 conn->ibc_cmid = NULL;
965 /* Drop my own and unused rxbuffer refcounts */
966 while (i++ <= IBLND_RX_MSGS(conn))
967 kiblnd_conn_decref(conn);
973 /* Init successful! */
974 LASSERT (state == IBLND_CONN_ACTIVE_CONNECT ||
975 state == IBLND_CONN_PASSIVE_WAIT);
976 conn->ibc_state = state;
979 atomic_inc(&net->ibn_nconns);
983 kiblnd_destroy_conn(conn);
984 LIBCFS_FREE(conn, sizeof(*conn));
990 kiblnd_destroy_conn(struct kib_conn *conn)
992 struct rdma_cm_id *cmid = conn->ibc_cmid;
993 struct kib_peer_ni *peer_ni = conn->ibc_peer;
995 LASSERT (!in_interrupt());
996 LASSERT (atomic_read(&conn->ibc_refcount) == 0);
997 LASSERT(list_empty(&conn->ibc_early_rxs));
998 LASSERT(list_empty(&conn->ibc_tx_noops));
999 LASSERT(list_empty(&conn->ibc_tx_queue));
1000 LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
1001 LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
1002 LASSERT(list_empty(&conn->ibc_active_txs));
1003 LASSERT (conn->ibc_noops_posted == 0);
1004 LASSERT (conn->ibc_nsends_posted == 0);
1006 switch (conn->ibc_state) {
1008 /* conn must be completely disengaged from the network */
1011 case IBLND_CONN_DISCONNECTED:
1012 /* connvars should have been freed already */
1013 LASSERT (conn->ibc_connvars == NULL);
1016 case IBLND_CONN_INIT:
1020 /* conn->ibc_cmid might be destroyed by CM already */
1021 if (cmid != NULL && cmid->qp != NULL)
1022 rdma_destroy_qp(cmid);
1025 ib_destroy_cq(conn->ibc_cq);
1027 kiblnd_txlist_done(&conn->ibc_zombie_txs, -ECONNABORTED,
1028 LNET_MSG_STATUS_OK);
1030 if (conn->ibc_rx_pages != NULL)
1031 kiblnd_unmap_rx_descs(conn);
1033 if (conn->ibc_rxs != NULL)
1034 CFS_FREE_PTR_ARRAY(conn->ibc_rxs, IBLND_RX_MSGS(conn));
1036 if (conn->ibc_connvars != NULL)
1037 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1039 if (conn->ibc_hdev != NULL)
1040 kiblnd_hdev_decref(conn->ibc_hdev);
1042 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
1043 if (conn->ibc_state != IBLND_CONN_INIT) {
1044 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1046 kiblnd_peer_decref(peer_ni);
1047 rdma_destroy_id(cmid);
1048 atomic_dec(&net->ibn_nconns);
1053 kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why)
1055 struct kib_conn *conn;
1056 struct kib_conn *cnxt;
1059 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1061 CDEBUG(D_NET, "Closing conn -> %s, "
1062 "version: %x, reason: %d\n",
1063 libcfs_nid2str(peer_ni->ibp_nid),
1064 conn->ibc_version, why);
1066 kiblnd_close_conn_locked(conn, why);
1074 kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1075 int version, __u64 incarnation)
1077 struct kib_conn *conn;
1078 struct kib_conn *cnxt;
1081 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1083 if (conn->ibc_version == version &&
1084 conn->ibc_incarnation == incarnation)
1087 CDEBUG(D_NET, "Closing stale conn -> %s version: %x, "
1088 "incarnation:%#llx(%x, %#llx)\n",
1089 libcfs_nid2str(peer_ni->ibp_nid),
1090 conn->ibc_version, conn->ibc_incarnation,
1091 version, incarnation);
1093 kiblnd_close_conn_locked(conn, -ESTALE);
1101 kiblnd_close_matching_conns(struct lnet_ni *ni, lnet_nid_t nid)
1103 struct kib_peer_ni *peer_ni;
1104 struct list_head *ptmp;
1105 struct list_head *pnxt;
1109 unsigned long flags;
1112 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1114 if (nid != LNET_NID_ANY)
1115 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
1118 hi = kiblnd_data.kib_peer_hash_size - 1;
1121 for (i = lo; i <= hi; i++) {
1122 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
1124 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
1125 LASSERT(!kiblnd_peer_idle(peer_ni));
1127 if (peer_ni->ibp_ni != ni)
1130 if (!(nid == LNET_NID_ANY || nid == peer_ni->ibp_nid))
1133 count += kiblnd_close_peer_conns_locked(peer_ni, 0);
1137 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1139 /* wildcards always succeed */
1140 if (nid == LNET_NID_ANY)
1143 return (count == 0) ? -ENOENT : 0;
1147 kiblnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1149 struct libcfs_ioctl_data *data = arg;
1153 case IOC_LIBCFS_GET_PEER: {
1157 rc = kiblnd_get_peer_info(ni, data->ioc_count,
1159 data->ioc_nid = nid;
1160 data->ioc_count = count;
1164 case IOC_LIBCFS_DEL_PEER: {
1165 rc = kiblnd_del_peer(ni, data->ioc_nid);
1168 case IOC_LIBCFS_GET_CONN: {
1169 struct kib_conn *conn;
1172 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1178 LASSERT(conn->ibc_cmid != NULL);
1179 data->ioc_nid = conn->ibc_peer->ibp_nid;
1180 if (conn->ibc_cmid->route.path_rec == NULL)
1181 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1184 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1185 kiblnd_conn_decref(conn);
1188 case IOC_LIBCFS_CLOSE_CONNECTION: {
1189 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1201 kiblnd_free_pages(struct kib_pages *p)
1203 int npages = p->ibp_npages;
1206 for (i = 0; i < npages; i++) {
1207 if (p->ibp_pages[i] != NULL)
1208 __free_page(p->ibp_pages[i]);
1211 LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1215 kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1217 struct kib_pages *p;
1220 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1221 offsetof(struct kib_pages, ibp_pages[npages]));
1223 CERROR("Can't allocate descriptor for %d pages\n", npages);
1227 memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1228 p->ibp_npages = npages;
1230 for (i = 0; i < npages; i++) {
1231 p->ibp_pages[i] = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1233 if (p->ibp_pages[i] == NULL) {
1234 CERROR("Can't allocate page %d of %d\n", i, npages);
1235 kiblnd_free_pages(p);
1245 kiblnd_unmap_rx_descs(struct kib_conn *conn)
1250 LASSERT (conn->ibc_rxs != NULL);
1251 LASSERT (conn->ibc_hdev != NULL);
1253 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1254 rx = &conn->ibc_rxs[i];
1256 LASSERT(rx->rx_nob >= 0); /* not posted */
1258 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1259 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1261 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1264 kiblnd_free_pages(conn->ibc_rx_pages);
1266 conn->ibc_rx_pages = NULL;
1270 kiblnd_map_rx_descs(struct kib_conn *conn)
1278 for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1279 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1280 rx = &conn->ibc_rxs[i];
1283 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1286 kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1287 rx->rx_msg, IBLND_MSG_SIZE,
1289 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1291 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1293 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1294 i, rx->rx_msg, rx->rx_msgaddr,
1295 (__u64)(page_to_phys(pg) + pg_off));
1297 pg_off += IBLND_MSG_SIZE;
1298 LASSERT(pg_off <= PAGE_SIZE);
1300 if (pg_off == PAGE_SIZE) {
1303 LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1309 kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1311 struct kib_hca_dev *hdev = tpo->tpo_hdev;
1315 LASSERT (tpo->tpo_pool.po_allocated == 0);
1320 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1321 tx = &tpo->tpo_tx_descs[i];
1322 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1323 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1325 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1328 kiblnd_hdev_decref(hdev);
1329 tpo->tpo_hdev = NULL;
1332 static struct kib_hca_dev *
1333 kiblnd_current_hdev(struct kib_dev *dev)
1335 struct kib_hca_dev *hdev;
1336 unsigned long flags;
1339 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1340 while (dev->ibd_failover) {
1341 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1343 CDEBUG(D_NET, "%s: Wait for failover\n",
1345 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
1347 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1350 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1351 hdev = dev->ibd_hdev;
1353 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1359 kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1361 struct kib_pages *txpgs = tpo->tpo_tx_pages;
1362 struct kib_pool *pool = &tpo->tpo_pool;
1363 struct kib_net *net = pool->po_owner->ps_net;
1364 struct kib_dev *dev;
1371 LASSERT (net != NULL);
1375 /* pre-mapped messages are not bigger than 1 page */
1376 BUILD_BUG_ON(IBLND_MSG_SIZE > PAGE_SIZE);
1378 /* No fancy arithmetic when we do the buffer calculations */
1379 BUILD_BUG_ON(PAGE_SIZE % IBLND_MSG_SIZE != 0);
1381 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1383 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1384 page = txpgs->ibp_pages[ipage];
1385 tx = &tpo->tpo_tx_descs[i];
1387 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1390 tx->tx_msgaddr = kiblnd_dma_map_single(tpo->tpo_hdev->ibh_ibdev,
1394 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1396 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1398 list_add(&tx->tx_list, &pool->po_free_list);
1400 page_offset += IBLND_MSG_SIZE;
1401 LASSERT(page_offset <= PAGE_SIZE);
1403 if (page_offset == PAGE_SIZE) {
1406 LASSERT(ipage <= txpgs->ibp_npages);
1412 kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1414 LASSERT(fpo->fpo_map_count == 0);
1416 #ifdef HAVE_FMR_POOL_API
1417 if (fpo->fpo_is_fmr && fpo->fmr.fpo_fmr_pool) {
1418 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1420 #endif /* HAVE_FMR_POOL_API */
1422 struct kib_fast_reg_descriptor *frd, *tmp;
1425 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1427 list_del(&frd->frd_list);
1428 #ifndef HAVE_IB_MAP_MR_SG
1429 ib_free_fast_reg_page_list(frd->frd_frpl);
1431 ib_dereg_mr(frd->frd_mr);
1432 LIBCFS_FREE(frd, sizeof(*frd));
1435 if (i < fpo->fast_reg.fpo_pool_size)
1436 CERROR("FastReg pool still has %d regions registered\n",
1437 fpo->fast_reg.fpo_pool_size - i);
1441 kiblnd_hdev_decref(fpo->fpo_hdev);
1443 LIBCFS_FREE(fpo, sizeof(*fpo));
1447 kiblnd_destroy_fmr_pool_list(struct list_head *head)
1449 struct kib_fmr_pool *fpo, *tmp;
1451 list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1452 list_del(&fpo->fpo_list);
1453 kiblnd_destroy_fmr_pool(fpo);
1458 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1461 int size = tunables->lnd_fmr_pool_size / ncpts;
1463 return max(IBLND_FMR_POOL, size);
1467 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1470 int size = tunables->lnd_fmr_flush_trigger / ncpts;
1472 return max(IBLND_FMR_POOL_FLUSH, size);
1475 #ifdef HAVE_FMR_POOL_API
1476 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps,
1477 struct kib_fmr_pool *fpo)
1479 struct ib_fmr_pool_param param = {
1480 .max_pages_per_fmr = LNET_MAX_IOV,
1481 .page_shift = PAGE_SHIFT,
1482 .access = (IB_ACCESS_LOCAL_WRITE |
1483 IB_ACCESS_REMOTE_WRITE),
1484 .pool_size = fps->fps_pool_size,
1485 .dirty_watermark = fps->fps_flush_trigger,
1486 .flush_function = NULL,
1488 .cache = !!fps->fps_cache };
1491 fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1493 if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1494 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1496 CERROR("Failed to create FMR pool: %d\n", rc);
1498 CERROR("FMRs are not supported\n");
1500 fpo->fpo_is_fmr = true;
1504 #endif /* HAVE_FMR_POOL_API */
1506 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps,
1507 struct kib_fmr_pool *fpo,
1508 enum kib_dev_caps dev_caps)
1510 struct kib_fast_reg_descriptor *frd, *tmp;
1513 #ifdef HAVE_FMR_POOL_API
1514 fpo->fpo_is_fmr = false;
1517 INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1518 fpo->fast_reg.fpo_pool_size = 0;
1519 for (i = 0; i < fps->fps_pool_size; i++) {
1520 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1523 CERROR("Failed to allocate a new fast_reg descriptor\n");
1529 #ifndef HAVE_IB_MAP_MR_SG
1530 frd->frd_frpl = ib_alloc_fast_reg_page_list(fpo->fpo_hdev->ibh_ibdev,
1532 if (IS_ERR(frd->frd_frpl)) {
1533 rc = PTR_ERR(frd->frd_frpl);
1534 CERROR("Failed to allocate ib_fast_reg_page_list: %d\n",
1536 frd->frd_frpl = NULL;
1541 #ifdef HAVE_IB_ALLOC_FAST_REG_MR
1542 frd->frd_mr = ib_alloc_fast_reg_mr(fpo->fpo_hdev->ibh_pd,
1546 * it is expected to get here if this is an MLX-5 card.
1547 * MLX-4 cards will always use FMR and MLX-5 cards will
1548 * always use fast_reg. It turns out that some MLX-5 cards
1549 * (possibly due to older FW versions) do not natively support
1550 * gaps. So we will need to track them here.
1552 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1553 #ifdef IB_MR_TYPE_SG_GAPS
1554 ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1555 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT)) ?
1556 IB_MR_TYPE_SG_GAPS :
1562 if ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1563 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT))
1564 CWARN("using IB_MR_TYPE_SG_GAPS, expect a performance drop\n");
1566 if (IS_ERR(frd->frd_mr)) {
1567 rc = PTR_ERR(frd->frd_mr);
1568 CERROR("Failed to allocate ib_fast_reg_mr: %d\n", rc);
1573 /* There appears to be a bug in MLX5 code where you must
1574 * invalidate the rkey of a new FastReg pool before first
1575 * using it. Thus, I am marking the FRD invalid here. */
1576 frd->frd_valid = false;
1578 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1579 fpo->fast_reg.fpo_pool_size++;
1586 ib_dereg_mr(frd->frd_mr);
1587 #ifndef HAVE_IB_MAP_MR_SG
1589 ib_free_fast_reg_page_list(frd->frd_frpl);
1591 LIBCFS_FREE(frd, sizeof(*frd));
1594 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1596 list_del(&frd->frd_list);
1597 #ifndef HAVE_IB_MAP_MR_SG
1598 ib_free_fast_reg_page_list(frd->frd_frpl);
1600 ib_dereg_mr(frd->frd_mr);
1601 LIBCFS_FREE(frd, sizeof(*frd));
1607 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1608 struct kib_fmr_pool **pp_fpo)
1610 struct kib_dev *dev = fps->fps_net->ibn_dev;
1611 struct kib_fmr_pool *fpo;
1614 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1618 memset(fpo, 0, sizeof(*fpo));
1620 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1622 #ifdef HAVE_FMR_POOL_API
1623 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
1624 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1626 #endif /* HAVE_FMR_POOL_API */
1627 rc = kiblnd_alloc_freg_pool(fps, fpo, dev->ibd_dev_caps);
1631 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1632 fpo->fpo_owner = fps;
1638 kiblnd_hdev_decref(fpo->fpo_hdev);
1639 LIBCFS_FREE(fpo, sizeof(*fpo));
1644 kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps, struct list_head *zombies)
1646 if (fps->fps_net == NULL) /* intialized? */
1649 spin_lock(&fps->fps_lock);
1651 while (!list_empty(&fps->fps_pool_list)) {
1652 struct kib_fmr_pool *fpo = list_entry(fps->fps_pool_list.next,
1653 struct kib_fmr_pool,
1656 fpo->fpo_failed = 1;
1657 if (fpo->fpo_map_count == 0)
1658 list_move(&fpo->fpo_list, zombies);
1660 list_move(&fpo->fpo_list, &fps->fps_failed_pool_list);
1663 spin_unlock(&fps->fps_lock);
1667 kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1669 if (fps->fps_net != NULL) { /* initialized? */
1670 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1671 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1676 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1677 struct kib_net *net,
1678 struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1680 struct kib_fmr_pool *fpo;
1683 memset(fps, 0, sizeof(struct kib_fmr_poolset));
1688 fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1689 fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1690 fps->fps_cache = tunables->lnd_fmr_cache;
1692 spin_lock_init(&fps->fps_lock);
1693 INIT_LIST_HEAD(&fps->fps_pool_list);
1694 INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1696 rc = kiblnd_create_fmr_pool(fps, &fpo);
1698 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1704 kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, time64_t now)
1706 if (fpo->fpo_map_count != 0) /* still in use */
1708 if (fpo->fpo_failed)
1710 return now >= fpo->fpo_deadline;
1713 #if defined(HAVE_FMR_POOL_API) || !defined(HAVE_IB_MAP_MR_SG)
1715 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1717 struct kib_hca_dev *hdev;
1718 __u64 *pages = tx->tx_pages;
1723 hdev = tx->tx_pool->tpo_hdev;
1725 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1726 for (size = 0; size < rd->rd_frags[i].rf_nob;
1727 size += hdev->ibh_page_size) {
1728 pages[npages++] = (rd->rd_frags[i].rf_addr &
1729 hdev->ibh_page_mask) + size;
1738 kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1741 struct kib_fmr_pool *fpo = fmr->fmr_pool;
1742 struct kib_fmr_poolset *fps;
1743 time64_t now = ktime_get_seconds();
1744 struct kib_fmr_pool *tmp;
1749 fps = fpo->fpo_owner;
1751 #ifdef HAVE_FMR_POOL_API
1752 if (fpo->fpo_is_fmr) {
1753 if (fmr->fmr_pfmr) {
1754 ib_fmr_pool_unmap(fmr->fmr_pfmr);
1755 fmr->fmr_pfmr = NULL;
1759 int rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1763 #endif /* HAVE_FMR_POOL_API */
1765 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1768 frd->frd_valid = false;
1769 spin_lock(&fps->fps_lock);
1770 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1771 spin_unlock(&fps->fps_lock);
1772 fmr->fmr_frd = NULL;
1775 fmr->fmr_pool = NULL;
1777 spin_lock(&fps->fps_lock);
1778 fpo->fpo_map_count--; /* decref the pool */
1780 list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1781 /* the first pool is persistent */
1782 if (fps->fps_pool_list.next == &fpo->fpo_list)
1785 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1786 list_move(&fpo->fpo_list, &zombies);
1790 spin_unlock(&fps->fps_lock);
1792 if (!list_empty(&zombies))
1793 kiblnd_destroy_fmr_pool_list(&zombies);
1796 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1797 struct kib_rdma_desc *rd, u32 nob, u64 iov,
1798 struct kib_fmr *fmr)
1800 struct kib_fmr_pool *fpo;
1802 bool is_rx = (rd != tx->tx_rd);
1803 #ifdef HAVE_FMR_POOL_API
1804 __u64 *pages = tx->tx_pages;
1805 bool tx_pages_mapped = 0;
1811 spin_lock(&fps->fps_lock);
1812 version = fps->fps_version;
1813 list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1814 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1815 fpo->fpo_map_count++;
1817 #ifdef HAVE_FMR_POOL_API
1818 fmr->fmr_pfmr = NULL;
1819 if (fpo->fpo_is_fmr) {
1820 struct ib_pool_fmr *pfmr;
1822 spin_unlock(&fps->fps_lock);
1824 if (!tx_pages_mapped) {
1825 npages = kiblnd_map_tx_pages(tx, rd);
1826 tx_pages_mapped = 1;
1829 pfmr = kib_fmr_pool_map(fpo->fmr.fpo_fmr_pool,
1830 pages, npages, iov);
1831 if (likely(!IS_ERR(pfmr))) {
1832 fmr->fmr_key = is_rx ? pfmr->fmr->rkey
1834 fmr->fmr_frd = NULL;
1835 fmr->fmr_pfmr = pfmr;
1836 fmr->fmr_pool = fpo;
1841 #endif /* HAVE_FMR_POOL_API */
1843 if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
1844 struct kib_fast_reg_descriptor *frd;
1845 #ifdef HAVE_IB_MAP_MR_SG
1846 struct ib_reg_wr *wr;
1849 struct ib_rdma_wr *wr;
1850 struct ib_fast_reg_page_list *frpl;
1854 frd = list_first_entry(&fpo->fast_reg.fpo_pool_list,
1855 struct kib_fast_reg_descriptor,
1857 list_del(&frd->frd_list);
1858 spin_unlock(&fps->fps_lock);
1860 #ifndef HAVE_IB_MAP_MR_SG
1861 frpl = frd->frd_frpl;
1865 if (!frd->frd_valid) {
1866 struct ib_rdma_wr *inv_wr;
1867 __u32 key = is_rx ? mr->rkey : mr->lkey;
1869 inv_wr = &frd->frd_inv_wr;
1870 memset(inv_wr, 0, sizeof(*inv_wr));
1872 inv_wr->wr.opcode = IB_WR_LOCAL_INV;
1873 inv_wr->wr.wr_id = IBLND_WID_MR;
1874 inv_wr->wr.ex.invalidate_rkey = key;
1877 key = ib_inc_rkey(key);
1878 ib_update_fast_reg_key(mr, key);
1881 #ifdef HAVE_IB_MAP_MR_SG
1882 #ifdef HAVE_IB_MAP_MR_SG_5ARGS
1883 n = ib_map_mr_sg(mr, tx->tx_frags,
1884 rd->rd_nfrags, NULL, PAGE_SIZE);
1886 n = ib_map_mr_sg(mr, tx->tx_frags,
1887 rd->rd_nfrags, PAGE_SIZE);
1888 #endif /* HAVE_IB_MAP_MR_SG_5ARGS */
1889 if (unlikely(n != rd->rd_nfrags)) {
1890 CERROR("Failed to map mr %d/%d "
1891 "elements\n", n, rd->rd_nfrags);
1892 return n < 0 ? n : -EINVAL;
1895 wr = &frd->frd_fastreg_wr;
1896 memset(wr, 0, sizeof(*wr));
1898 wr->wr.opcode = IB_WR_REG_MR;
1899 wr->wr.wr_id = IBLND_WID_MR;
1901 wr->wr.send_flags = 0;
1903 wr->key = is_rx ? mr->rkey : mr->lkey;
1904 wr->access = (IB_ACCESS_LOCAL_WRITE |
1905 IB_ACCESS_REMOTE_WRITE);
1906 #else /* HAVE_IB_MAP_MR_SG */
1907 if (!tx_pages_mapped) {
1908 npages = kiblnd_map_tx_pages(tx, rd);
1909 tx_pages_mapped = 1;
1912 LASSERT(npages <= frpl->max_page_list_len);
1913 memcpy(frpl->page_list, pages,
1914 sizeof(*pages) * npages);
1916 /* Prepare FastReg WR */
1917 wr = &frd->frd_fastreg_wr;
1918 memset(wr, 0, sizeof(*wr));
1920 wr->wr.opcode = IB_WR_FAST_REG_MR;
1921 wr->wr.wr_id = IBLND_WID_MR;
1923 wr->wr.wr.fast_reg.iova_start = iov;
1924 wr->wr.wr.fast_reg.page_list = frpl;
1925 wr->wr.wr.fast_reg.page_list_len = npages;
1926 wr->wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1927 wr->wr.wr.fast_reg.length = nob;
1928 wr->wr.wr.fast_reg.rkey =
1929 is_rx ? mr->rkey : mr->lkey;
1930 wr->wr.wr.fast_reg.access_flags =
1931 (IB_ACCESS_LOCAL_WRITE |
1932 IB_ACCESS_REMOTE_WRITE);
1933 #endif /* HAVE_IB_MAP_MR_SG */
1935 fmr->fmr_key = is_rx ? mr->rkey : mr->lkey;
1937 fmr->fmr_pool = fpo;
1940 spin_unlock(&fps->fps_lock);
1944 spin_lock(&fps->fps_lock);
1945 fpo->fpo_map_count--;
1946 if (rc != -EAGAIN) {
1947 spin_unlock(&fps->fps_lock);
1951 /* EAGAIN and ... */
1952 if (version != fps->fps_version) {
1953 spin_unlock(&fps->fps_lock);
1958 if (fps->fps_increasing) {
1959 spin_unlock(&fps->fps_lock);
1960 CDEBUG(D_NET, "Another thread is allocating new "
1961 "FMR pool, waiting for her to complete\n");
1962 wait_var_event(fps, !fps->fps_increasing);
1967 if (ktime_get_seconds() < fps->fps_next_retry) {
1968 /* someone failed recently */
1969 spin_unlock(&fps->fps_lock);
1973 fps->fps_increasing = 1;
1974 spin_unlock(&fps->fps_lock);
1976 CDEBUG(D_NET, "Allocate new FMR pool\n");
1977 rc = kiblnd_create_fmr_pool(fps, &fpo);
1978 spin_lock(&fps->fps_lock);
1979 fps->fps_increasing = 0;
1983 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1985 fps->fps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
1987 spin_unlock(&fps->fps_lock);
1993 kiblnd_fini_pool(struct kib_pool *pool)
1995 LASSERT(list_empty(&pool->po_free_list));
1996 LASSERT(pool->po_allocated == 0);
1998 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
2002 kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
2004 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
2006 memset(pool, 0, sizeof(struct kib_pool));
2007 INIT_LIST_HEAD(&pool->po_free_list);
2008 pool->po_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
2009 pool->po_owner = ps;
2010 pool->po_size = size;
2014 kiblnd_destroy_pool_list(struct list_head *head)
2016 struct kib_pool *pool;
2018 while (!list_empty(head)) {
2019 pool = list_entry(head->next, struct kib_pool, po_list);
2020 list_del(&pool->po_list);
2022 LASSERT(pool->po_owner != NULL);
2023 pool->po_owner->ps_pool_destroy(pool);
2028 kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
2030 if (ps->ps_net == NULL) /* intialized? */
2033 spin_lock(&ps->ps_lock);
2034 while (!list_empty(&ps->ps_pool_list)) {
2035 struct kib_pool *po = list_entry(ps->ps_pool_list.next,
2036 struct kib_pool, po_list);
2039 if (po->po_allocated == 0)
2040 list_move(&po->po_list, zombies);
2042 list_move(&po->po_list, &ps->ps_failed_pool_list);
2044 spin_unlock(&ps->ps_lock);
2048 kiblnd_fini_poolset(struct kib_poolset *ps)
2050 if (ps->ps_net != NULL) { /* initialized? */
2051 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
2052 kiblnd_destroy_pool_list(&ps->ps_pool_list);
2057 kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
2058 struct kib_net *net, char *name, int size,
2059 kib_ps_pool_create_t po_create,
2060 kib_ps_pool_destroy_t po_destroy,
2061 kib_ps_node_init_t nd_init,
2062 kib_ps_node_fini_t nd_fini)
2064 struct kib_pool *pool;
2067 memset(ps, 0, sizeof(struct kib_poolset));
2071 ps->ps_pool_create = po_create;
2072 ps->ps_pool_destroy = po_destroy;
2073 ps->ps_node_init = nd_init;
2074 ps->ps_node_fini = nd_fini;
2075 ps->ps_pool_size = size;
2076 if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name))
2077 >= sizeof(ps->ps_name))
2079 spin_lock_init(&ps->ps_lock);
2080 INIT_LIST_HEAD(&ps->ps_pool_list);
2081 INIT_LIST_HEAD(&ps->ps_failed_pool_list);
2083 rc = ps->ps_pool_create(ps, size, &pool);
2085 list_add(&pool->po_list, &ps->ps_pool_list);
2087 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
2093 kiblnd_pool_is_idle(struct kib_pool *pool, time64_t now)
2095 if (pool->po_allocated != 0) /* still in use */
2097 if (pool->po_failed)
2099 return now >= pool->po_deadline;
2103 kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
2106 struct kib_poolset *ps = pool->po_owner;
2107 struct kib_pool *tmp;
2108 time64_t now = ktime_get_seconds();
2110 spin_lock(&ps->ps_lock);
2112 if (ps->ps_node_fini != NULL)
2113 ps->ps_node_fini(pool, node);
2115 LASSERT(pool->po_allocated > 0);
2116 list_add(node, &pool->po_free_list);
2117 pool->po_allocated--;
2119 list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
2120 /* the first pool is persistent */
2121 if (ps->ps_pool_list.next == &pool->po_list)
2124 if (kiblnd_pool_is_idle(pool, now))
2125 list_move(&pool->po_list, &zombies);
2127 spin_unlock(&ps->ps_lock);
2129 if (!list_empty(&zombies))
2130 kiblnd_destroy_pool_list(&zombies);
2134 kiblnd_pool_alloc_node(struct kib_poolset *ps)
2136 struct list_head *node;
2137 struct kib_pool *pool;
2139 unsigned int interval = 1;
2140 ktime_t time_before;
2141 unsigned int trips = 0;
2144 spin_lock(&ps->ps_lock);
2145 list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
2146 if (list_empty(&pool->po_free_list))
2149 pool->po_allocated++;
2150 pool->po_deadline = ktime_get_seconds() +
2151 IBLND_POOL_DEADLINE;
2152 node = pool->po_free_list.next;
2155 if (ps->ps_node_init != NULL) {
2156 /* still hold the lock */
2157 ps->ps_node_init(pool, node);
2159 spin_unlock(&ps->ps_lock);
2163 /* no available tx pool and ... */
2164 if (ps->ps_increasing) {
2165 /* another thread is allocating a new pool */
2166 spin_unlock(&ps->ps_lock);
2169 "Another thread is allocating new %s pool, waiting %d jiffies for her to complete. trips = %d\n",
2170 ps->ps_name, interval, trips);
2172 schedule_timeout_interruptible(interval);
2173 if (interval < cfs_time_seconds(1))
2179 if (ktime_get_seconds() < ps->ps_next_retry) {
2180 /* someone failed recently */
2181 spin_unlock(&ps->ps_lock);
2185 ps->ps_increasing = 1;
2186 spin_unlock(&ps->ps_lock);
2188 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
2189 time_before = ktime_get();
2190 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
2191 CDEBUG(D_NET, "ps_pool_create took %lld ms to complete",
2192 ktime_ms_delta(ktime_get(), time_before));
2194 spin_lock(&ps->ps_lock);
2195 ps->ps_increasing = 0;
2197 list_add_tail(&pool->po_list, &ps->ps_pool_list);
2199 ps->ps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2200 CERROR("Can't allocate new %s pool because out of memory\n",
2203 spin_unlock(&ps->ps_lock);
2209 kiblnd_destroy_tx_pool(struct kib_pool *pool)
2211 struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool,
2215 LASSERT (pool->po_allocated == 0);
2217 if (tpo->tpo_tx_pages != NULL) {
2218 kiblnd_unmap_tx_pool(tpo);
2219 kiblnd_free_pages(tpo->tpo_tx_pages);
2222 if (tpo->tpo_tx_descs == NULL)
2225 for (i = 0; i < pool->po_size; i++) {
2226 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2227 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2229 list_del(&tx->tx_list);
2230 if (tx->tx_pages != NULL)
2231 CFS_FREE_PTR_ARRAY(tx->tx_pages, LNET_MAX_IOV);
2232 if (tx->tx_frags != NULL)
2233 CFS_FREE_PTR_ARRAY(tx->tx_frags,
2234 (1 + IBLND_MAX_RDMA_FRAGS));
2235 if (tx->tx_wrq != NULL)
2236 CFS_FREE_PTR_ARRAY(tx->tx_wrq,
2237 (1 + IBLND_MAX_RDMA_FRAGS));
2238 if (tx->tx_sge != NULL)
2239 CFS_FREE_PTR_ARRAY(tx->tx_sge,
2240 (1 + IBLND_MAX_RDMA_FRAGS) *
2242 if (tx->tx_rd != NULL)
2243 LIBCFS_FREE(tx->tx_rd,
2244 offsetof(struct kib_rdma_desc,
2245 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2248 CFS_FREE_PTR_ARRAY(tpo->tpo_tx_descs, pool->po_size);
2250 kiblnd_fini_pool(pool);
2254 static int kiblnd_tx_pool_size(struct lnet_ni *ni, int ncpts)
2256 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2259 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2260 ntx = tunables->lnd_ntx / ncpts;
2262 return max(IBLND_TX_POOL, ntx);
2266 kiblnd_create_tx_pool(struct kib_poolset *ps, int size, struct kib_pool **pp_po)
2270 struct kib_pool *pool;
2271 struct kib_tx_pool *tpo;
2273 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2275 CERROR("Failed to allocate TX pool\n");
2279 pool = &tpo->tpo_pool;
2280 kiblnd_init_pool(ps, pool, size);
2281 tpo->tpo_tx_descs = NULL;
2282 tpo->tpo_tx_pages = NULL;
2284 npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2285 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg) != 0) {
2286 CERROR("Can't allocate tx pages: %d\n", npg);
2291 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2292 size * sizeof(struct kib_tx));
2293 if (tpo->tpo_tx_descs == NULL) {
2294 CERROR("Can't allocate %d tx descriptors\n", size);
2295 ps->ps_pool_destroy(pool);
2299 memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2301 for (i = 0; i < size; i++) {
2302 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2303 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2306 if (ps->ps_net->ibn_fmr_ps != NULL) {
2307 LIBCFS_CPT_ALLOC(tx->tx_pages,
2308 lnet_cpt_table(), ps->ps_cpt,
2309 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2310 if (tx->tx_pages == NULL)
2314 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2315 (1 + IBLND_MAX_RDMA_FRAGS) *
2316 sizeof(*tx->tx_frags));
2317 if (tx->tx_frags == NULL)
2320 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS + 1);
2322 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2323 (1 + IBLND_MAX_RDMA_FRAGS) *
2324 sizeof(*tx->tx_wrq));
2325 if (tx->tx_wrq == NULL)
2328 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2329 (1 + IBLND_MAX_RDMA_FRAGS) * wrq_sge *
2330 sizeof(*tx->tx_sge));
2331 if (tx->tx_sge == NULL)
2334 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2335 offsetof(struct kib_rdma_desc,
2336 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2337 if (tx->tx_rd == NULL)
2342 kiblnd_map_tx_pool(tpo);
2347 ps->ps_pool_destroy(pool);
2352 kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2354 struct kib_tx_poolset *tps = container_of(pool->po_owner,
2355 struct kib_tx_poolset,
2357 struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list);
2359 tx->tx_cookie = tps->tps_next_tx_cookie++;
2363 kiblnd_net_fini_pools(struct kib_net *net)
2367 cfs_cpt_for_each(i, lnet_cpt_table()) {
2368 struct kib_tx_poolset *tps;
2369 struct kib_fmr_poolset *fps;
2371 if (net->ibn_tx_ps != NULL) {
2372 tps = net->ibn_tx_ps[i];
2373 kiblnd_fini_poolset(&tps->tps_poolset);
2376 if (net->ibn_fmr_ps != NULL) {
2377 fps = net->ibn_fmr_ps[i];
2378 kiblnd_fini_fmr_poolset(fps);
2382 if (net->ibn_tx_ps != NULL) {
2383 cfs_percpt_free(net->ibn_tx_ps);
2384 net->ibn_tx_ps = NULL;
2387 if (net->ibn_fmr_ps != NULL) {
2388 cfs_percpt_free(net->ibn_fmr_ps);
2389 net->ibn_fmr_ps = NULL;
2394 kiblnd_net_init_pools(struct kib_net *net, struct lnet_ni *ni, __u32 *cpts,
2397 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2398 #ifdef HAVE_IB_GET_DMA_MR
2399 unsigned long flags;
2405 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2407 #ifdef HAVE_IB_GET_DMA_MR
2408 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2410 * if lnd_map_on_demand is zero then we have effectively disabled
2411 * FMR or FastReg and we're using global memory regions
2414 if (!tunables->lnd_map_on_demand) {
2415 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2417 goto create_tx_pool;
2420 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2423 if (tunables->lnd_fmr_pool_size < tunables->lnd_ntx / 4) {
2424 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2425 tunables->lnd_fmr_pool_size,
2426 tunables->lnd_ntx / 4);
2431 /* TX pool must be created later than FMR, see LU-2268
2433 LASSERT(net->ibn_tx_ps == NULL);
2435 /* premapping can fail if ibd_nmr > 1, so we always create
2436 * FMR pool and map-on-demand if premapping failed */
2438 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2439 sizeof(struct kib_fmr_poolset));
2440 if (net->ibn_fmr_ps == NULL) {
2441 CERROR("Failed to allocate FMR pool array\n");
2446 for (i = 0; i < ncpts; i++) {
2447 cpt = (cpts == NULL) ? i : cpts[i];
2448 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2451 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2458 LASSERT(i == ncpts);
2460 #ifdef HAVE_IB_GET_DMA_MR
2463 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2464 sizeof(struct kib_tx_poolset));
2465 if (net->ibn_tx_ps == NULL) {
2466 CERROR("Failed to allocate tx pool array\n");
2471 for (i = 0; i < ncpts; i++) {
2472 cpt = (cpts == NULL) ? i : cpts[i];
2473 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2475 kiblnd_tx_pool_size(ni, ncpts),
2476 kiblnd_create_tx_pool,
2477 kiblnd_destroy_tx_pool,
2478 kiblnd_tx_init, NULL);
2480 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2488 kiblnd_net_fini_pools(net);
2494 kiblnd_port_get_attr(struct kib_hca_dev *hdev)
2496 struct ib_port_attr *port_attr;
2498 unsigned long flags;
2499 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2501 LIBCFS_ALLOC(port_attr, sizeof(*port_attr));
2502 if (port_attr == NULL) {
2503 CDEBUG(D_NETERROR, "Out of memory\n");
2507 rc = ib_query_port(hdev->ibh_ibdev, hdev->ibh_port, port_attr);
2509 write_lock_irqsave(g_lock, flags);
2512 hdev->ibh_state = port_attr->state == IB_PORT_ACTIVE
2513 ? IBLND_DEV_PORT_ACTIVE
2514 : IBLND_DEV_PORT_DOWN;
2516 write_unlock_irqrestore(g_lock, flags);
2517 LIBCFS_FREE(port_attr, sizeof(*port_attr));
2520 CDEBUG(D_NETERROR, "Failed to query IB port: %d\n", rc);
2527 kiblnd_set_ni_fatal_on(struct kib_hca_dev *hdev, int val)
2529 struct kib_net *net;
2531 /* for health check */
2532 list_for_each_entry(net, &hdev->ibh_dev->ibd_nets, ibn_list) {
2534 CDEBUG(D_NETERROR, "Fatal device error for NI %s\n",
2535 libcfs_nid2str(net->ibn_ni->ni_nid));
2536 atomic_set(&net->ibn_ni->ni_fatal_error_on, val);
2541 kiblnd_event_handler(struct ib_event_handler *handler, struct ib_event *event)
2543 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2544 struct kib_hca_dev *hdev;
2545 unsigned long flags;
2547 hdev = container_of(handler, struct kib_hca_dev, ibh_event_handler);
2549 write_lock_irqsave(g_lock, flags);
2551 switch (event->event) {
2552 case IB_EVENT_DEVICE_FATAL:
2553 CDEBUG(D_NET, "IB device fatal\n");
2554 hdev->ibh_state = IBLND_DEV_FATAL;
2555 kiblnd_set_ni_fatal_on(hdev, 1);
2557 case IB_EVENT_PORT_ACTIVE:
2558 CDEBUG(D_NET, "IB port active\n");
2559 if (event->element.port_num == hdev->ibh_port) {
2560 hdev->ibh_state = IBLND_DEV_PORT_ACTIVE;
2561 kiblnd_set_ni_fatal_on(hdev, 0);
2564 case IB_EVENT_PORT_ERR:
2565 CDEBUG(D_NET, "IB port err\n");
2566 if (event->element.port_num == hdev->ibh_port) {
2567 hdev->ibh_state = IBLND_DEV_PORT_DOWN;
2568 kiblnd_set_ni_fatal_on(hdev, 1);
2574 write_unlock_irqrestore(g_lock, flags);
2578 kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2580 struct ib_device_attr *dev_attr;
2584 /* It's safe to assume a HCA can handle a page size
2585 * matching that of the native system */
2586 hdev->ibh_page_shift = PAGE_SHIFT;
2587 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2588 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2590 #ifndef HAVE_IB_DEVICE_ATTRS
2591 LIBCFS_ALLOC(dev_attr, sizeof(*dev_attr));
2592 if (dev_attr == NULL) {
2593 CERROR("Out of memory\n");
2597 rc = ib_query_device(hdev->ibh_ibdev, dev_attr);
2599 CERROR("Failed to query IB device: %d\n", rc);
2600 goto out_clean_attr;
2603 dev_attr = &hdev->ibh_ibdev->attrs;
2606 hdev->ibh_mr_size = dev_attr->max_mr_size;
2607 hdev->ibh_max_qp_wr = dev_attr->max_qp_wr;
2609 /* Setup device Memory Registration capabilities */
2610 #ifdef HAVE_FMR_POOL_API
2611 #ifdef HAVE_IB_DEVICE_OPS
2612 if (hdev->ibh_ibdev->ops.alloc_fmr &&
2613 hdev->ibh_ibdev->ops.dealloc_fmr &&
2614 hdev->ibh_ibdev->ops.map_phys_fmr &&
2615 hdev->ibh_ibdev->ops.unmap_fmr) {
2617 if (hdev->ibh_ibdev->alloc_fmr &&
2618 hdev->ibh_ibdev->dealloc_fmr &&
2619 hdev->ibh_ibdev->map_phys_fmr &&
2620 hdev->ibh_ibdev->unmap_fmr) {
2622 LCONSOLE_INFO("Using FMR for registration\n");
2623 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FMR_ENABLED;
2625 #endif /* HAVE_FMR_POOL_API */
2626 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
2627 LCONSOLE_INFO("Using FastReg for registration\n");
2628 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_ENABLED;
2629 #ifndef HAVE_IB_ALLOC_FAST_REG_MR
2630 #ifdef IB_DEVICE_SG_GAPS_REG
2631 if (dev_attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
2632 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT;
2639 rc2 = kiblnd_port_get_attr(hdev);
2646 #ifndef HAVE_IB_DEVICE_ATTRS
2648 LIBCFS_FREE(dev_attr, sizeof(*dev_attr));
2652 CERROR("IB device does not support FMRs nor FastRegs, can't "
2653 "register memory: %d\n", rc);
2654 else if (rc == -EINVAL)
2655 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2659 #ifdef HAVE_IB_GET_DMA_MR
2661 kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev)
2663 if (hdev->ibh_mrs == NULL)
2666 ib_dereg_mr(hdev->ibh_mrs);
2668 hdev->ibh_mrs = NULL;
2673 kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2675 if (hdev->ibh_event_handler.device != NULL)
2676 ib_unregister_event_handler(&hdev->ibh_event_handler);
2678 #ifdef HAVE_IB_GET_DMA_MR
2679 kiblnd_hdev_cleanup_mrs(hdev);
2682 if (hdev->ibh_pd != NULL)
2683 ib_dealloc_pd(hdev->ibh_pd);
2685 if (hdev->ibh_cmid != NULL)
2686 rdma_destroy_id(hdev->ibh_cmid);
2688 LIBCFS_FREE(hdev, sizeof(*hdev));
2691 #ifdef HAVE_IB_GET_DMA_MR
2693 kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev)
2696 int acflags = IB_ACCESS_LOCAL_WRITE |
2697 IB_ACCESS_REMOTE_WRITE;
2699 mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2701 CERROR("Failed ib_get_dma_mr: %ld\n", PTR_ERR(mr));
2702 kiblnd_hdev_cleanup_mrs(hdev);
2713 kiblnd_dummy_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2719 kiblnd_dev_need_failover(struct kib_dev *dev, struct net *ns)
2721 struct rdma_cm_id *cmid;
2722 struct sockaddr_in srcaddr;
2723 struct sockaddr_in dstaddr;
2726 if (dev->ibd_hdev == NULL || /* initializing */
2727 dev->ibd_hdev->ibh_cmid == NULL || /* listener is dead */
2728 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2731 /* XXX: it's UGLY, but I don't have better way to find
2732 * ib-bonding HCA failover because:
2734 * a. no reliable CM event for HCA failover...
2735 * b. no OFED API to get ib_device for current net_device...
2737 * We have only two choices at this point:
2739 * a. rdma_bind_addr(), it will conflict with listener cmid
2740 * b. rdma_resolve_addr() to zero addr */
2741 cmid = kiblnd_rdma_create_id(ns, kiblnd_dummy_callback, dev,
2742 RDMA_PS_TCP, IB_QPT_RC);
2745 CERROR("Failed to create cmid for failover: %d\n", rc);
2749 memset(&srcaddr, 0, sizeof(srcaddr));
2750 srcaddr.sin_family = AF_INET;
2751 srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2753 memset(&dstaddr, 0, sizeof(dstaddr));
2754 dstaddr.sin_family = AF_INET;
2755 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2756 (struct sockaddr *)&dstaddr, 1);
2757 if (rc != 0 || cmid->device == NULL) {
2758 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2759 dev->ibd_ifname, &dev->ibd_ifip,
2761 rdma_destroy_id(cmid);
2765 rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2766 rdma_destroy_id(cmid);
2771 kiblnd_dev_failover(struct kib_dev *dev, struct net *ns)
2773 LIST_HEAD(zombie_tpo);
2774 LIST_HEAD(zombie_ppo);
2775 LIST_HEAD(zombie_fpo);
2776 struct rdma_cm_id *cmid = NULL;
2777 struct kib_hca_dev *hdev = NULL;
2778 struct kib_hca_dev *old;
2780 struct kib_net *net;
2781 struct sockaddr_in addr;
2782 unsigned long flags;
2786 LASSERT (*kiblnd_tunables.kib_dev_failover > 1 ||
2787 dev->ibd_can_failover ||
2788 dev->ibd_hdev == NULL);
2790 rc = kiblnd_dev_need_failover(dev, ns);
2794 if (dev->ibd_hdev != NULL &&
2795 dev->ibd_hdev->ibh_cmid != NULL) {
2796 /* XXX it's not good to close old listener at here,
2797 * because we can fail to create new listener.
2798 * But we have to close it now, otherwise rdma_bind_addr
2799 * will return EADDRINUSE... How crap! */
2800 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2802 cmid = dev->ibd_hdev->ibh_cmid;
2803 /* make next schedule of kiblnd_dev_need_failover()
2804 * return 1 for me */
2805 dev->ibd_hdev->ibh_cmid = NULL;
2806 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2808 rdma_destroy_id(cmid);
2811 cmid = kiblnd_rdma_create_id(ns, kiblnd_cm_callback, dev, RDMA_PS_TCP,
2815 CERROR("Failed to create cmid for failover: %d\n", rc);
2819 memset(&addr, 0, sizeof(addr));
2820 addr.sin_family = AF_INET;
2821 addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2822 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2824 /* Bind to failover device or port */
2825 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2826 if (rc != 0 || cmid->device == NULL) {
2827 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2828 dev->ibd_ifname, &dev->ibd_ifip,
2830 rdma_destroy_id(cmid);
2834 LIBCFS_ALLOC(hdev, sizeof(*hdev));
2836 CERROR("Failed to allocate kib_hca_dev\n");
2837 rdma_destroy_id(cmid);
2842 atomic_set(&hdev->ibh_ref, 1);
2843 hdev->ibh_dev = dev;
2844 hdev->ibh_cmid = cmid;
2845 hdev->ibh_ibdev = cmid->device;
2846 hdev->ibh_port = cmid->port_num;
2848 #ifdef HAVE_IB_ALLOC_PD_2ARGS
2849 pd = ib_alloc_pd(cmid->device, 0);
2851 pd = ib_alloc_pd(cmid->device);
2855 CERROR("Can't allocate PD: %d\n", rc);
2861 rc = rdma_listen(cmid, 0);
2863 CERROR("Can't start new listener: %d\n", rc);
2867 rc = kiblnd_hdev_get_attr(hdev);
2869 CERROR("Can't get device attributes: %d\n", rc);
2873 #ifdef HAVE_IB_GET_DMA_MR
2874 rc = kiblnd_hdev_setup_mrs(hdev);
2876 CERROR("Can't setup device: %d\n", rc);
2881 INIT_IB_EVENT_HANDLER(&hdev->ibh_event_handler,
2882 hdev->ibh_ibdev, kiblnd_event_handler);
2883 ib_register_event_handler(&hdev->ibh_event_handler);
2885 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2887 old = dev->ibd_hdev;
2888 dev->ibd_hdev = hdev; /* take over the refcount */
2891 list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
2892 cfs_cpt_for_each(i, lnet_cpt_table()) {
2893 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
2896 if (net->ibn_fmr_ps != NULL)
2897 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
2902 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2904 if (!list_empty(&zombie_tpo))
2905 kiblnd_destroy_pool_list(&zombie_tpo);
2906 if (!list_empty(&zombie_ppo))
2907 kiblnd_destroy_pool_list(&zombie_ppo);
2908 if (!list_empty(&zombie_fpo))
2909 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
2911 kiblnd_hdev_decref(hdev);
2914 dev->ibd_failed_failover++;
2916 dev->ibd_failed_failover = 0;
2922 kiblnd_destroy_dev(struct kib_dev *dev)
2924 LASSERT(dev->ibd_nnets == 0);
2925 LASSERT(list_empty(&dev->ibd_nets));
2927 list_del(&dev->ibd_fail_list);
2928 list_del(&dev->ibd_list);
2930 if (dev->ibd_hdev != NULL)
2931 kiblnd_hdev_decref(dev->ibd_hdev);
2933 LIBCFS_FREE(dev, sizeof(*dev));
2937 kiblnd_base_shutdown(void)
2939 struct kib_sched_info *sched;
2942 LASSERT(list_empty(&kiblnd_data.kib_devs));
2944 CDEBUG(D_MALLOC, "before LND base cleanup: kmem %lld\n",
2945 libcfs_kmem_read());
2947 switch (kiblnd_data.kib_init) {
2951 case IBLND_INIT_ALL:
2952 case IBLND_INIT_DATA:
2953 LASSERT (kiblnd_data.kib_peers != NULL);
2954 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
2955 LASSERT(list_empty(&kiblnd_data.kib_peers[i]));
2957 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
2958 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
2959 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
2960 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
2962 /* flag threads to terminate; wake and wait for them to die */
2963 kiblnd_data.kib_shutdown = 1;
2965 /* NB: we really want to stop scheduler threads net by net
2966 * instead of the whole module, this should be improved
2967 * with dynamic configuration LNet */
2968 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
2969 wake_up_all(&sched->ibs_waitq);
2971 wake_up_all(&kiblnd_data.kib_connd_waitq);
2972 wake_up_all(&kiblnd_data.kib_failover_waitq);
2974 wait_var_event_warning(&kiblnd_data.kib_nthreads,
2975 !atomic_read(&kiblnd_data.kib_nthreads),
2976 "Waiting for %d threads to terminate\n",
2977 atomic_read(&kiblnd_data.kib_nthreads));
2980 case IBLND_INIT_NOTHING:
2984 if (kiblnd_data.kib_peers)
2985 CFS_FREE_PTR_ARRAY(kiblnd_data.kib_peers,
2986 kiblnd_data.kib_peer_hash_size);
2988 if (kiblnd_data.kib_scheds != NULL)
2989 cfs_percpt_free(kiblnd_data.kib_scheds);
2991 CDEBUG(D_MALLOC, "after LND base cleanup: kmem %lld\n",
2992 libcfs_kmem_read());
2994 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
2995 module_put(THIS_MODULE);
2999 kiblnd_shutdown(struct lnet_ni *ni)
3001 struct kib_net *net = ni->ni_data;
3002 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
3003 unsigned long flags;
3005 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
3010 CDEBUG(D_MALLOC, "before LND net cleanup: kmem %lld\n",
3011 libcfs_kmem_read());
3013 write_lock_irqsave(g_lock, flags);
3014 net->ibn_shutdown = 1;
3015 write_unlock_irqrestore(g_lock, flags);
3017 switch (net->ibn_init) {
3021 case IBLND_INIT_ALL:
3022 /* nuke all existing peers within this net */
3023 kiblnd_del_peer(ni, LNET_NID_ANY);
3025 /* Wait for all peer_ni state to clean up */
3026 wait_var_event_warning(&net->ibn_npeers,
3027 atomic_read(&net->ibn_npeers) == 0,
3028 "%s: waiting for %d peers to disconnect\n",
3029 libcfs_nid2str(ni->ni_nid),
3030 atomic_read(&net->ibn_npeers));
3032 kiblnd_net_fini_pools(net);
3034 write_lock_irqsave(g_lock, flags);
3035 LASSERT(net->ibn_dev->ibd_nnets > 0);
3036 net->ibn_dev->ibd_nnets--;
3037 list_del(&net->ibn_list);
3038 write_unlock_irqrestore(g_lock, flags);
3042 case IBLND_INIT_NOTHING:
3043 LASSERT (atomic_read(&net->ibn_nconns) == 0);
3045 if (net->ibn_dev != NULL &&
3046 net->ibn_dev->ibd_nnets == 0)
3047 kiblnd_destroy_dev(net->ibn_dev);
3052 CDEBUG(D_MALLOC, "after LND net cleanup: kmem %lld\n",
3053 libcfs_kmem_read());
3055 net->ibn_init = IBLND_INIT_NOTHING;
3058 LIBCFS_FREE(net, sizeof(*net));
3061 if (list_empty(&kiblnd_data.kib_devs))
3062 kiblnd_base_shutdown();
3066 kiblnd_base_startup(struct net *ns)
3068 struct kib_sched_info *sched;
3072 LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
3074 if (!try_module_get(THIS_MODULE))
3077 memset(&kiblnd_data, 0, sizeof(kiblnd_data)); /* zero pointers, flags etc */
3079 rwlock_init(&kiblnd_data.kib_global_lock);
3081 INIT_LIST_HEAD(&kiblnd_data.kib_devs);
3082 INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
3084 kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
3085 CFS_ALLOC_PTR_ARRAY(kiblnd_data.kib_peers,
3086 kiblnd_data.kib_peer_hash_size);
3087 if (kiblnd_data.kib_peers == NULL)
3090 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
3091 INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]);
3093 spin_lock_init(&kiblnd_data.kib_connd_lock);
3094 INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
3095 INIT_LIST_HEAD(&kiblnd_data.kib_connd_waits);
3096 INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
3097 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
3098 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
3100 init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
3101 init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
3103 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
3105 if (kiblnd_data.kib_scheds == NULL)
3108 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
3111 spin_lock_init(&sched->ibs_lock);
3112 INIT_LIST_HEAD(&sched->ibs_conns);
3113 init_waitqueue_head(&sched->ibs_waitq);
3115 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
3116 if (*kiblnd_tunables.kib_nscheds > 0) {
3117 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
3119 /* max to half of CPUs, another half is reserved for
3120 * upper layer modules */
3121 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3124 sched->ibs_nthreads_max = nthrs;
3128 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
3130 /* lists/ptrs/locks initialised */
3131 kiblnd_data.kib_init = IBLND_INIT_DATA;
3132 /*****************************************************/
3134 rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
3136 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
3140 if (*kiblnd_tunables.kib_dev_failover != 0)
3141 rc = kiblnd_thread_start(kiblnd_failover_thread, ns,
3145 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
3149 /* flag everything initialised */
3150 kiblnd_data.kib_init = IBLND_INIT_ALL;
3151 /*****************************************************/
3156 kiblnd_base_shutdown();
3161 kiblnd_start_schedulers(struct kib_sched_info *sched)
3167 if (sched->ibs_nthreads == 0) {
3168 if (*kiblnd_tunables.kib_nscheds > 0) {
3169 nthrs = sched->ibs_nthreads_max;
3171 nthrs = cfs_cpt_weight(lnet_cpt_table(),
3173 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3174 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
3177 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
3178 /* increase one thread if there is new interface */
3179 nthrs = (sched->ibs_nthreads < sched->ibs_nthreads_max);
3182 for (i = 0; i < nthrs; i++) {
3185 id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
3186 snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld",
3187 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
3188 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id, name);
3192 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
3193 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
3197 sched->ibs_nthreads += i;
3201 static int kiblnd_dev_start_threads(struct kib_dev *dev, bool newdev, u32 *cpts,
3208 for (i = 0; i < ncpts; i++) {
3209 struct kib_sched_info *sched;
3211 cpt = (cpts == NULL) ? i : cpts[i];
3212 sched = kiblnd_data.kib_scheds[cpt];
3214 if (!newdev && sched->ibs_nthreads > 0)
3217 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
3219 CERROR("Failed to start scheduler threads for %s\n",
3227 static struct kib_dev *
3228 kiblnd_dev_search(char *ifname)
3230 struct kib_dev *alias = NULL;
3231 struct kib_dev *dev;
3235 colon = strchr(ifname, ':');
3236 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3237 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3243 colon2 = strchr(dev->ibd_ifname, ':');
3249 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3261 kiblnd_startup(struct lnet_ni *ni)
3263 char *ifname = NULL;
3264 struct lnet_inetdev *ifaces = NULL;
3265 struct kib_dev *ibdev = NULL;
3266 struct kib_net *net = NULL;
3267 unsigned long flags;
3272 LASSERT(ni->ni_net->net_lnd == &the_o2iblnd);
3274 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
3275 rc = kiblnd_base_startup(ni->ni_net_ns);
3280 LIBCFS_ALLOC(net, sizeof(*net));
3288 net->ibn_incarnation = ktime_get_real_ns() / NSEC_PER_USEC;
3290 kiblnd_tunables_setup(ni);
3293 * ni_interfaces is only to support legacy pre Multi-Rail
3294 * tcp bonding for ksocklnd. Multi-Rail wants each secondary
3295 * IP to be treated as an unique 'struct ni' interfaces instead.
3297 if (ni->ni_interfaces[0] != NULL) {
3298 /* Use the IPoIB interface specified in 'networks=' */
3299 if (ni->ni_interfaces[1] != NULL) {
3300 CERROR("ko2iblnd: Multiple interfaces not supported\n");
3305 ifname = ni->ni_interfaces[0];
3307 ifname = *kiblnd_tunables.kib_default_ipif;
3310 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
3311 CERROR("IPoIB interface name too long: %s\n", ifname);
3316 rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns);
3320 for (i = 0; i < rc; i++) {
3321 if (strcmp(ifname, ifaces[i].li_name) == 0)
3326 CERROR("ko2iblnd: No matching interfaces\n");
3331 ibdev = kiblnd_dev_search(ifname);
3332 newdev = ibdev == NULL;
3333 /* hmm...create kib_dev even for alias */
3334 if (ibdev == NULL || strcmp(&ibdev->ibd_ifname[0], ifname) != 0) {
3335 LIBCFS_ALLOC(ibdev, sizeof(*ibdev));
3341 ibdev->ibd_ifip = ifaces[i].li_ipaddr;
3342 strlcpy(ibdev->ibd_ifname, ifaces[i].li_name,
3343 sizeof(ibdev->ibd_ifname));
3344 ibdev->ibd_can_failover = !!(ifaces[i].li_flags & IFF_MASTER);
3346 INIT_LIST_HEAD(&ibdev->ibd_nets);
3347 INIT_LIST_HEAD(&ibdev->ibd_list); /* not yet in kib_devs */
3348 INIT_LIST_HEAD(&ibdev->ibd_fail_list);
3350 /* initialize the device */
3351 rc = kiblnd_dev_failover(ibdev, ni->ni_net_ns);
3353 CERROR("ko2iblnd: Can't initialize device: rc = %d\n",
3358 list_add_tail(&ibdev->ibd_list, &kiblnd_data.kib_devs);
3361 net->ibn_dev = ibdev;
3362 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip);
3364 ni->ni_dev_cpt = ifaces[i].li_cpt;
3366 rc = kiblnd_dev_start_threads(ibdev, newdev, ni->ni_cpts, ni->ni_ncpts);
3370 rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
3372 CERROR("Failed to initialize NI pools: %d\n", rc);
3376 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3378 list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
3379 /* for health check */
3380 if (ibdev->ibd_hdev->ibh_state == IBLND_DEV_PORT_DOWN)
3381 kiblnd_set_ni_fatal_on(ibdev->ibd_hdev, 1);
3382 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3384 net->ibn_init = IBLND_INIT_ALL;
3389 if (net != NULL && net->ibn_dev == NULL && ibdev != NULL)
3390 kiblnd_destroy_dev(ibdev);
3393 kiblnd_shutdown(ni);
3395 CDEBUG(D_NET, "Configuration of device %s failed: rc = %d\n",
3396 ifname ? ifname : "", rc);
3401 static const struct lnet_lnd the_o2iblnd = {
3402 .lnd_type = O2IBLND,
3403 .lnd_startup = kiblnd_startup,
3404 .lnd_shutdown = kiblnd_shutdown,
3405 .lnd_ctl = kiblnd_ctl,
3406 .lnd_send = kiblnd_send,
3407 .lnd_recv = kiblnd_recv,
3410 static void __exit ko2iblnd_exit(void)
3412 lnet_unregister_lnd(&the_o2iblnd);
3415 static int __init ko2iblnd_init(void)
3419 BUILD_BUG_ON(sizeof(struct kib_msg) > IBLND_MSG_SIZE);
3420 BUILD_BUG_ON(offsetof(struct kib_msg,
3421 ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3423 BUILD_BUG_ON(offsetof(struct kib_msg,
3424 ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3427 rc = kiblnd_tunables_init();
3431 lnet_register_lnd(&the_o2iblnd);
3436 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3437 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
3438 MODULE_VERSION("2.8.0");
3439 MODULE_LICENSE("GPL");
3441 module_init(ko2iblnd_init);
3442 module_exit(ko2iblnd_exit);