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/
31 * lnet/klnds/o2iblnd/o2iblnd.c
33 * Author: Eric Barton <eric@bartonsoftware.com>
37 #include <linux/ethtool.h>
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, bool 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.
187 msg->ibm_magic = IBLND_MSG_MAGIC;
188 msg->ibm_version = version;
190 msg->ibm_credits = credits;
193 msg->ibm_srcnid = lnet_nid_to_nid4(&ni->ni_nid);
194 msg->ibm_srcstamp = net->ibn_incarnation;
195 msg->ibm_dstnid = dstnid;
196 msg->ibm_dststamp = dststamp;
198 if (*kiblnd_tunables.kib_cksum) {
199 /* NB ibm_cksum zero while computing cksum */
200 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
204 int kiblnd_unpack_msg(struct kib_msg *msg, int nob)
206 const int hdr_size = offsetof(struct kib_msg, ibm_u);
212 /* 6 bytes are enough to have received magic + version */
214 CERROR("Short message: %d\n", nob);
218 if (msg->ibm_magic == IBLND_MSG_MAGIC) {
220 } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
223 CERROR("Bad magic: %08x\n", msg->ibm_magic);
227 version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
228 if (version != IBLND_MSG_VERSION &&
229 version != IBLND_MSG_VERSION_1) {
230 CERROR("Bad version: %x\n", version);
234 if (nob < hdr_size) {
235 CERROR("Short message: %d\n", nob);
239 msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
241 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
245 /* checksum must be computed with ibm_cksum zero and BEFORE anything
248 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
250 if (msg_cksum != 0 &&
251 msg_cksum != kiblnd_cksum(msg, msg_nob)) {
252 CERROR("Bad checksum\n");
256 msg->ibm_cksum = msg_cksum;
259 /* leave magic unflipped as a clue to peer_ni endianness */
260 msg->ibm_version = version;
261 BUILD_BUG_ON(sizeof(msg->ibm_type) != 1);
262 BUILD_BUG_ON(sizeof(msg->ibm_credits) != 1);
263 msg->ibm_nob = msg_nob;
264 __swab64s(&msg->ibm_srcnid);
265 __swab64s(&msg->ibm_srcstamp);
266 __swab64s(&msg->ibm_dstnid);
267 __swab64s(&msg->ibm_dststamp);
270 if (msg->ibm_srcnid == LNET_NID_ANY) {
271 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg->ibm_srcnid));
275 if (msg_nob < kiblnd_msgtype2size(msg->ibm_type)) {
276 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg->ibm_type),
277 msg_nob, kiblnd_msgtype2size(msg->ibm_type));
281 switch (msg->ibm_type) {
283 CERROR("Unknown message type %x\n", msg->ibm_type);
287 case IBLND_MSG_IMMEDIATE:
288 case IBLND_MSG_PUT_REQ:
291 case IBLND_MSG_PUT_ACK:
292 case IBLND_MSG_GET_REQ:
293 if (kiblnd_unpack_rd(msg, flip))
297 case IBLND_MSG_PUT_NAK:
298 case IBLND_MSG_PUT_DONE:
299 case IBLND_MSG_GET_DONE:
301 __swab32s(&msg->ibm_u.completion.ibcm_status);
304 case IBLND_MSG_CONNREQ:
305 case IBLND_MSG_CONNACK:
307 __swab16s(&msg->ibm_u.connparams.ibcp_queue_depth);
308 __swab16s(&msg->ibm_u.connparams.ibcp_max_frags);
309 __swab32s(&msg->ibm_u.connparams.ibcp_max_msg_size);
317 kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
320 struct kib_peer_ni *peer_ni;
321 struct kib_net *net = ni->ni_data;
322 int cpt = lnet_cpt_of_nid(nid, ni);
325 LASSERT(net != NULL);
326 LASSERT(nid != LNET_NID_ANY);
328 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
330 CERROR("Cannot allocate peer_ni\n");
334 peer_ni->ibp_ni = ni;
335 peer_ni->ibp_nid = nid;
336 peer_ni->ibp_error = 0;
337 peer_ni->ibp_last_alive = 0;
338 peer_ni->ibp_max_frags = IBLND_MAX_RDMA_FRAGS;
339 peer_ni->ibp_queue_depth = ni->ni_net->net_tunables.lct_peer_tx_credits;
340 peer_ni->ibp_queue_depth_mod = 0; /* try to use the default */
341 kref_init(&peer_ni->ibp_kref);
342 atomic_set(&peer_ni->ibp_nconns, 0);
344 INIT_HLIST_NODE(&peer_ni->ibp_list);
345 INIT_LIST_HEAD(&peer_ni->ibp_conns);
346 INIT_LIST_HEAD(&peer_ni->ibp_tx_queue);
348 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
350 /* always called with a ref on ni, which prevents ni being shutdown */
351 LASSERT(net->ibn_shutdown == 0);
353 /* npeers only grows with the global lock held */
354 atomic_inc(&net->ibn_npeers);
356 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
363 kiblnd_destroy_peer(struct kref *kref)
365 struct kib_peer_ni *peer_ni = container_of(kref, struct kib_peer_ni,
367 struct kib_net *net = peer_ni->ibp_ni->ni_data;
369 LASSERT(net != NULL);
370 LASSERT(!kiblnd_peer_active(peer_ni));
371 LASSERT(kiblnd_peer_idle(peer_ni));
372 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
374 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
376 /* NB a peer_ni's connections keep a reference on their peer_ni until
377 * they are destroyed, so we can be assured that _all_ state to do
378 * with this peer_ni has been cleaned up when its refcount drops to
381 if (atomic_dec_and_test(&net->ibn_npeers))
382 wake_up_var(&net->ibn_npeers);
386 kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid)
388 /* the caller is responsible for accounting the additional reference
391 struct kib_peer_ni *peer_ni;
393 hash_for_each_possible(kiblnd_data.kib_peers, peer_ni,
395 LASSERT(!kiblnd_peer_idle(peer_ni));
398 * Match a peer if its NID and the NID of the local NI it
399 * communicates over are the same. Otherwise don't match
400 * the peer, which will result in a new lnd peer being
403 if (peer_ni->ibp_nid != nid ||
404 !nid_same(&peer_ni->ibp_ni->ni_nid, &ni->ni_nid))
407 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d) version: %x\n",
408 peer_ni, libcfs_nid2str(nid),
409 kref_read(&peer_ni->ibp_kref),
410 peer_ni->ibp_version);
417 kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni)
419 LASSERT(list_empty(&peer_ni->ibp_conns));
421 LASSERT(kiblnd_peer_active(peer_ni));
422 hlist_del_init(&peer_ni->ibp_list);
423 /* lose peerlist's ref */
424 kiblnd_peer_decref(peer_ni);
429 kiblnd_debug_rx(struct kib_rx *rx)
431 CDEBUG(D_CONSOLE, " %p msg_type %x cred %d\n",
432 rx, rx->rx_msg->ibm_type,
433 rx->rx_msg->ibm_credits);
437 kiblnd_debug_tx(struct kib_tx *tx)
439 CDEBUG(D_CONSOLE, " %p snd %d q %d w %d rc %d dl %lld "
440 "cookie %#llx msg %s%s type %x cred %d\n",
441 tx, tx->tx_sending, tx->tx_queued, tx->tx_waiting,
442 tx->tx_status, ktime_to_ns(tx->tx_deadline), tx->tx_cookie,
443 tx->tx_lntmsg[0] == NULL ? "-" : "!",
444 tx->tx_lntmsg[1] == NULL ? "-" : "!",
445 tx->tx_msg->ibm_type, tx->tx_msg->ibm_credits);
449 kiblnd_debug_conn(struct kib_conn *conn)
451 struct list_head *tmp;
454 spin_lock(&conn->ibc_lock);
456 CDEBUG(D_CONSOLE, "conn[%d] %p [version %x] -> %s:\n",
457 atomic_read(&conn->ibc_refcount), conn,
458 conn->ibc_version, libcfs_nid2str(conn->ibc_peer->ibp_nid));
459 CDEBUG(D_CONSOLE, " state %d nposted %d/%d cred %d o_cred %d "
460 " r_cred %d\n", conn->ibc_state, conn->ibc_noops_posted,
461 conn->ibc_nsends_posted, conn->ibc_credits,
462 conn->ibc_outstanding_credits, conn->ibc_reserved_credits);
463 CDEBUG(D_CONSOLE, " comms_err %d\n", conn->ibc_comms_error);
465 CDEBUG(D_CONSOLE, " early_rxs:\n");
466 list_for_each(tmp, &conn->ibc_early_rxs)
467 kiblnd_debug_rx(list_entry(tmp, struct kib_rx, rx_list));
469 CDEBUG(D_CONSOLE, " tx_noops:\n");
470 list_for_each(tmp, &conn->ibc_tx_noops)
471 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
473 CDEBUG(D_CONSOLE, " tx_queue_nocred:\n");
474 list_for_each(tmp, &conn->ibc_tx_queue_nocred)
475 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
477 CDEBUG(D_CONSOLE, " tx_queue_rsrvd:\n");
478 list_for_each(tmp, &conn->ibc_tx_queue_rsrvd)
479 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
481 CDEBUG(D_CONSOLE, " tx_queue:\n");
482 list_for_each(tmp, &conn->ibc_tx_queue)
483 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
485 CDEBUG(D_CONSOLE, " active_txs:\n");
486 list_for_each(tmp, &conn->ibc_active_txs)
487 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
489 CDEBUG(D_CONSOLE, " rxs:\n");
490 for (i = 0; i < IBLND_RX_MSGS(conn); i++)
491 kiblnd_debug_rx(&conn->ibc_rxs[i]);
493 spin_unlock(&conn->ibc_lock);
497 kiblnd_dump_peer_debug_info(struct kib_peer_ni *peer_ni)
499 struct kib_conn *conn;
500 struct kib_conn *cnxt;
503 CDEBUG(D_CONSOLE, "[last_alive, races, reconnected, error]: %lld, %d, %d, %d\n",
504 peer_ni->ibp_last_alive,
506 peer_ni->ibp_reconnected,
508 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
510 CDEBUG(D_CONSOLE, "Conn %d:\n", count);
511 kiblnd_debug_conn(conn);
518 kiblnd_get_peer_info(struct lnet_ni *ni, lnet_nid_t nid, int index,
519 lnet_nid_t *nidp, int *count)
521 struct kib_peer_ni *peer_ni;
525 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
527 hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list) {
528 LASSERT(!kiblnd_peer_idle(peer_ni));
530 if (peer_ni->ibp_ni != ni)
533 if (peer_ni->ibp_nid == nid)
534 kiblnd_dump_peer_debug_info(peer_ni);
539 *nidp = peer_ni->ibp_nid;
540 *count = kref_read(&peer_ni->ibp_kref);
542 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
546 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
551 kiblnd_del_peer_locked(struct kib_peer_ni *peer_ni)
553 struct kib_conn *cnxt;
554 struct kib_conn *conn;
556 if (list_empty(&peer_ni->ibp_conns)) {
557 kiblnd_unlink_peer_locked(peer_ni);
559 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
561 kiblnd_close_conn_locked(conn, 0);
562 /* NB closing peer_ni's last conn unlinked it. */
564 /* NB peer_ni now unlinked; might even be freed if the peer_ni table had the
569 kiblnd_del_peer(struct lnet_ni *ni, lnet_nid_t nid)
572 struct hlist_node *pnxt;
573 struct kib_peer_ni *peer_ni;
580 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
582 if (nid != LNET_NID_ANY) {
583 lo = hash_min(nid, HASH_BITS(kiblnd_data.kib_peers));
587 hi = HASH_SIZE(kiblnd_data.kib_peers) - 1;
590 for (i = lo; i <= hi; i++) {
591 hlist_for_each_entry_safe(peer_ni, pnxt,
592 &kiblnd_data.kib_peers[i], ibp_list) {
593 LASSERT(!kiblnd_peer_idle(peer_ni));
595 if (peer_ni->ibp_ni != ni)
598 if (!(nid == LNET_NID_ANY || peer_ni->ibp_nid == nid))
601 if (!list_empty(&peer_ni->ibp_tx_queue)) {
602 LASSERT(list_empty(&peer_ni->ibp_conns));
604 list_splice_init(&peer_ni->ibp_tx_queue,
608 kiblnd_del_peer_locked(peer_ni);
609 rc = 0; /* matched something */
613 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
615 kiblnd_txlist_done(&zombies, -EIO, LNET_MSG_STATUS_LOCAL_ERROR);
620 static struct kib_conn *
621 kiblnd_get_conn_by_idx(struct lnet_ni *ni, int index)
623 struct kib_peer_ni *peer_ni;
624 struct kib_conn *conn;
628 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
630 hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list) {
631 LASSERT(!kiblnd_peer_idle(peer_ni));
633 if (peer_ni->ibp_ni != ni)
636 list_for_each_entry(conn, &peer_ni->ibp_conns,
641 kiblnd_conn_addref(conn);
642 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
648 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
653 kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
655 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
656 if (cmid->route.path_rec == NULL)
659 if (*kiblnd_tunables.kib_ib_mtu)
660 cmid->route.path_rec->mtu =
661 ib_mtu_int_to_enum(*kiblnd_tunables.kib_ib_mtu);
665 kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
673 vectors = conn->ibc_cmid->device->num_comp_vectors;
677 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
679 /* hash NID to CPU id in this partition... when targeting a single peer
680 * with multiple QPs, to engage more cores in CQ processing to a single
681 * peer, use ibp_nconns to salt the value the comp_vector value
683 ibp_nid = conn->ibc_peer->ibp_nid +
684 atomic_read(&conn->ibc_peer->ibp_nconns);
685 off = do_div(ibp_nid, cpumask_weight(*mask));
686 for_each_cpu(i, *mask) {
696 * Get the scheduler bound to this CPT. If the scheduler has no
697 * threads, which means that the CPT has no CPUs, then grab the
698 * next scheduler that we can use.
700 * This case would be triggered if a NUMA node is configured with
701 * no associated CPUs.
703 static struct kib_sched_info *
704 kiblnd_get_scheduler(int cpt)
706 struct kib_sched_info *sched;
709 sched = kiblnd_data.kib_scheds[cpt];
711 if (sched->ibs_nthreads > 0)
714 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
715 if (sched->ibs_nthreads > 0) {
716 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
717 cpt, sched->ibs_cpt);
725 static unsigned int kiblnd_send_wrs(struct kib_conn *conn)
728 * One WR for the LNet message
729 * And ibc_max_frags for the transfer WRs
732 int multiplier = 1 + conn->ibc_max_frags;
734 /* FastReg needs two extra WRs for map and invalidate */
735 if (IS_FAST_REG_DEV(conn->ibc_hdev->ibh_dev))
738 /* account for a maximum of ibc_queue_depth in-flight transfers */
739 ret = multiplier * conn->ibc_queue_depth;
741 if (ret > conn->ibc_hdev->ibh_max_qp_wr) {
742 CDEBUG(D_NET, "peer_credits %u will result in send work "
743 "request size %d larger than maximum %d device "
744 "can handle\n", conn->ibc_queue_depth, ret,
745 conn->ibc_hdev->ibh_max_qp_wr);
746 conn->ibc_queue_depth =
747 conn->ibc_hdev->ibh_max_qp_wr / multiplier;
750 /* don't go beyond the maximum the device can handle */
751 return min(ret, conn->ibc_hdev->ibh_max_qp_wr);
755 kiblnd_create_conn(struct kib_peer_ni *peer_ni, struct rdma_cm_id *cmid,
756 int state, int version)
759 * If the new conn is created successfully it takes over the caller's
760 * ref on 'peer_ni'. It also "owns" 'cmid' and destroys it when it itself
761 * is destroyed. On failure, the caller's ref on 'peer_ni' remains and
762 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
763 * to destroy 'cmid' here since I'm called from the CM which still has
764 * its ref on 'cmid'). */
765 rwlock_t *glock = &kiblnd_data.kib_global_lock;
766 struct kib_net *net = peer_ni->ibp_ni->ni_data;
768 struct ib_qp_init_attr init_qp_attr = {};
769 struct kib_sched_info *sched;
770 #ifdef HAVE_IB_CQ_INIT_ATTR
771 struct ib_cq_init_attr cq_attr = {};
773 struct kib_conn *conn;
780 LASSERT(net != NULL);
781 LASSERT(!in_interrupt());
785 cpt = lnet_cpt_of_nid(peer_ni->ibp_nid, peer_ni->ibp_ni);
786 sched = kiblnd_get_scheduler(cpt);
789 CERROR("no schedulers available. node is unhealthy\n");
794 * The cpt might have changed if we ended up selecting a non cpt
795 * native scheduler. So use the scheduler's cpt instead.
797 cpt = sched->ibs_cpt;
799 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
801 CERROR("Can't allocate connection for %s\n",
802 libcfs_nid2str(peer_ni->ibp_nid));
806 conn->ibc_state = IBLND_CONN_INIT;
807 conn->ibc_version = version;
808 conn->ibc_peer = peer_ni; /* I take the caller's ref */
809 cmid->context = conn; /* for future CM callbacks */
810 conn->ibc_cmid = cmid;
811 conn->ibc_max_frags = peer_ni->ibp_max_frags;
812 conn->ibc_queue_depth = peer_ni->ibp_queue_depth;
813 conn->ibc_rxs = NULL;
814 conn->ibc_rx_pages = NULL;
816 INIT_LIST_HEAD(&conn->ibc_early_rxs);
817 INIT_LIST_HEAD(&conn->ibc_tx_noops);
818 INIT_LIST_HEAD(&conn->ibc_tx_queue);
819 INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
820 INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
821 INIT_LIST_HEAD(&conn->ibc_active_txs);
822 INIT_LIST_HEAD(&conn->ibc_zombie_txs);
823 spin_lock_init(&conn->ibc_lock);
825 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
826 sizeof(*conn->ibc_connvars));
827 if (conn->ibc_connvars == NULL) {
828 CERROR("Can't allocate in-progress connection state\n");
832 write_lock_irqsave(glock, flags);
833 if (dev->ibd_failover) {
834 write_unlock_irqrestore(glock, flags);
835 CERROR("%s: failover in progress\n", dev->ibd_ifname);
839 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
840 /* wakeup failover thread and teardown connection */
841 if (kiblnd_dev_can_failover(dev)) {
842 list_add_tail(&dev->ibd_fail_list,
843 &kiblnd_data.kib_failed_devs);
844 wake_up(&kiblnd_data.kib_failover_waitq);
847 write_unlock_irqrestore(glock, flags);
848 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
849 cmid->device->name, dev->ibd_ifname);
853 kiblnd_hdev_addref_locked(dev->ibd_hdev);
854 conn->ibc_hdev = dev->ibd_hdev;
856 kiblnd_setup_mtu_locked(cmid);
858 write_unlock_irqrestore(glock, flags);
860 #ifdef HAVE_IB_CQ_INIT_ATTR
861 cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
862 cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
863 cq = ib_create_cq(cmid->device,
864 kiblnd_cq_completion, kiblnd_cq_event, conn,
867 cq = ib_create_cq(cmid->device,
868 kiblnd_cq_completion, kiblnd_cq_event, conn,
869 IBLND_CQ_ENTRIES(conn),
870 kiblnd_get_completion_vector(conn, cpt));
874 * on MLX-5 (possibly MLX-4 as well) this error could be
875 * hit if the concurrent_sends and/or peer_tx_credits is set
876 * too high. Or due to an MLX-5 bug which tries to
877 * allocate 256kb via kmalloc for WR cookie array
879 CERROR("Failed to create CQ with %d CQEs: %ld\n",
880 IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
886 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
888 CERROR("Can't request completion notification: %d\n", rc);
892 init_qp_attr.event_handler = kiblnd_qp_event;
893 init_qp_attr.qp_context = conn;
894 init_qp_attr.cap.max_send_sge = *kiblnd_tunables.kib_wrq_sge;
895 init_qp_attr.cap.max_recv_sge = 1;
896 init_qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
897 init_qp_attr.qp_type = IB_QPT_RC;
898 init_qp_attr.send_cq = cq;
899 init_qp_attr.recv_cq = cq;
901 if (peer_ni->ibp_queue_depth_mod &&
902 peer_ni->ibp_queue_depth_mod < peer_ni->ibp_queue_depth) {
903 conn->ibc_queue_depth = peer_ni->ibp_queue_depth_mod;
904 CDEBUG(D_NET, "Use reduced queue depth %u (from %u)\n",
905 peer_ni->ibp_queue_depth_mod,
906 peer_ni->ibp_queue_depth);
910 /* kiblnd_send_wrs() can change the connection's queue depth if
911 * the maximum work requests for the device is maxed out
913 init_qp_attr.cap.max_send_wr = kiblnd_send_wrs(conn);
914 init_qp_attr.cap.max_recv_wr = IBLND_RECV_WRS(conn);
915 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd,
917 if (rc != -ENOMEM || conn->ibc_queue_depth < 2)
919 conn->ibc_queue_depth--;
923 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d, "
924 "send_sge: %d, recv_sge: %d\n",
925 rc, init_qp_attr.cap.max_send_wr,
926 init_qp_attr.cap.max_recv_wr,
927 init_qp_attr.cap.max_send_sge,
928 init_qp_attr.cap.max_recv_sge);
932 conn->ibc_sched = sched;
934 if (!peer_ni->ibp_queue_depth_mod &&
935 conn->ibc_queue_depth != peer_ni->ibp_queue_depth) {
936 CWARN("peer %s - queue depth reduced from %u to %u"
937 " to allow for qp creation\n",
938 libcfs_nid2str(peer_ni->ibp_nid),
939 peer_ni->ibp_queue_depth,
940 conn->ibc_queue_depth);
941 peer_ni->ibp_queue_depth_mod = conn->ibc_queue_depth;
944 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
945 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
946 if (conn->ibc_rxs == NULL) {
947 CERROR("Cannot allocate RX buffers\n");
951 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
952 IBLND_RX_MSG_PAGES(conn));
956 kiblnd_map_rx_descs(conn);
958 /* 1 ref for caller and each rxmsg */
959 atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
960 conn->ibc_nrx = IBLND_RX_MSGS(conn);
963 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
964 rc = kiblnd_post_rx(&conn->ibc_rxs[i], IBLND_POSTRX_NO_CREDIT);
966 CERROR("Can't post rxmsg: %d\n", rc);
968 /* Make posted receives complete */
969 kiblnd_abort_receives(conn);
971 /* correct # of posted buffers
972 * NB locking needed now I'm racing with completion */
973 spin_lock_irqsave(&sched->ibs_lock, flags);
974 conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i;
975 spin_unlock_irqrestore(&sched->ibs_lock, flags);
977 /* cmid will be destroyed by CM(ofed) after cm_callback
978 * returned, so we can't refer it anymore
979 * (by kiblnd_connd()->kiblnd_destroy_conn) */
980 rdma_destroy_qp(conn->ibc_cmid);
981 conn->ibc_cmid = NULL;
983 /* Drop my own and unused rxbuffer refcounts */
984 while (i++ <= IBLND_RX_MSGS(conn))
985 kiblnd_conn_decref(conn);
991 /* Init successful! */
992 LASSERT (state == IBLND_CONN_ACTIVE_CONNECT ||
993 state == IBLND_CONN_PASSIVE_WAIT);
994 conn->ibc_state = state;
997 atomic_inc(&peer_ni->ibp_nconns);
998 atomic_inc(&net->ibn_nconns);
1002 kiblnd_destroy_conn(conn);
1003 LIBCFS_FREE(conn, sizeof(*conn));
1009 kiblnd_destroy_conn(struct kib_conn *conn)
1011 struct rdma_cm_id *cmid = conn->ibc_cmid;
1012 struct kib_peer_ni *peer_ni = conn->ibc_peer;
1014 LASSERT (!in_interrupt());
1015 LASSERT (atomic_read(&conn->ibc_refcount) == 0);
1016 LASSERT(list_empty(&conn->ibc_early_rxs));
1017 LASSERT(list_empty(&conn->ibc_tx_noops));
1018 LASSERT(list_empty(&conn->ibc_tx_queue));
1019 LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
1020 LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
1021 LASSERT(list_empty(&conn->ibc_active_txs));
1022 LASSERT (conn->ibc_noops_posted == 0);
1023 LASSERT (conn->ibc_nsends_posted == 0);
1025 switch (conn->ibc_state) {
1027 /* conn must be completely disengaged from the network */
1030 case IBLND_CONN_DISCONNECTED:
1031 /* connvars should have been freed already */
1032 LASSERT (conn->ibc_connvars == NULL);
1035 case IBLND_CONN_INIT:
1039 /* conn->ibc_cmid might be destroyed by CM already */
1040 if (cmid != NULL && cmid->qp != NULL)
1041 rdma_destroy_qp(cmid);
1044 ib_destroy_cq(conn->ibc_cq);
1046 kiblnd_txlist_done(&conn->ibc_zombie_txs, -ECONNABORTED,
1047 LNET_MSG_STATUS_OK);
1049 if (conn->ibc_rx_pages != NULL)
1050 kiblnd_unmap_rx_descs(conn);
1052 if (conn->ibc_rxs != NULL)
1053 CFS_FREE_PTR_ARRAY(conn->ibc_rxs, IBLND_RX_MSGS(conn));
1055 if (conn->ibc_connvars != NULL)
1056 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1058 if (conn->ibc_hdev != NULL)
1059 kiblnd_hdev_decref(conn->ibc_hdev);
1061 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
1062 if (conn->ibc_state != IBLND_CONN_INIT) {
1063 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1065 kiblnd_peer_decref(peer_ni);
1066 rdma_destroy_id(cmid);
1067 atomic_dec(&peer_ni->ibp_nconns);
1068 atomic_dec(&net->ibn_nconns);
1073 kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why)
1075 struct kib_conn *conn;
1076 struct kib_conn *cnxt;
1079 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1081 CDEBUG(D_NET, "Closing conn -> %s, "
1082 "version: %x, reason: %d\n",
1083 libcfs_nid2str(peer_ni->ibp_nid),
1084 conn->ibc_version, why);
1086 kiblnd_close_conn_locked(conn, why);
1094 kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1095 int version, __u64 incarnation)
1097 struct kib_conn *conn;
1098 struct kib_conn *cnxt;
1101 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1103 if (conn->ibc_version == version &&
1104 conn->ibc_incarnation == incarnation)
1107 CDEBUG(D_NET, "Closing stale conn -> %s version: %x, "
1108 "incarnation:%#llx(%x, %#llx)\n",
1109 libcfs_nid2str(peer_ni->ibp_nid),
1110 conn->ibc_version, conn->ibc_incarnation,
1111 version, incarnation);
1113 kiblnd_close_conn_locked(conn, -ESTALE);
1121 kiblnd_close_matching_conns(struct lnet_ni *ni, lnet_nid_t nid)
1123 struct kib_peer_ni *peer_ni;
1124 struct hlist_node *pnxt;
1128 unsigned long flags;
1131 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1133 if (nid != LNET_NID_ANY) {
1134 lo = hash_min(nid, HASH_BITS(kiblnd_data.kib_peers));
1138 hi = HASH_SIZE(kiblnd_data.kib_peers) - 1;
1141 for (i = lo; i <= hi; i++) {
1142 hlist_for_each_entry_safe(peer_ni, pnxt,
1143 &kiblnd_data.kib_peers[i], ibp_list) {
1144 LASSERT(!kiblnd_peer_idle(peer_ni));
1146 if (peer_ni->ibp_ni != ni)
1149 if (!(nid == LNET_NID_ANY || nid == peer_ni->ibp_nid))
1152 count += kiblnd_close_peer_conns_locked(peer_ni, 0);
1156 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1158 /* wildcards always succeed */
1159 if (nid == LNET_NID_ANY)
1162 return (count == 0) ? -ENOENT : 0;
1166 kiblnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1168 struct libcfs_ioctl_data *data = arg;
1172 case IOC_LIBCFS_GET_PEER: {
1176 rc = kiblnd_get_peer_info(ni, data->ioc_nid, data->ioc_count,
1178 data->ioc_nid = nid;
1179 data->ioc_count = count;
1183 case IOC_LIBCFS_DEL_PEER: {
1184 rc = kiblnd_del_peer(ni, data->ioc_nid);
1187 case IOC_LIBCFS_GET_CONN: {
1188 struct kib_conn *conn;
1191 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1197 LASSERT(conn->ibc_cmid != NULL);
1198 data->ioc_nid = conn->ibc_peer->ibp_nid;
1199 if (conn->ibc_cmid->route.path_rec == NULL)
1200 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1203 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1204 kiblnd_conn_decref(conn);
1207 case IOC_LIBCFS_CLOSE_CONNECTION: {
1208 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1219 static const struct ln_key_list kiblnd_tunables_keys = {
1220 .lkl_maxattr = LNET_NET_O2IBLND_TUNABLES_ATTR_MAX,
1222 [LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS] = {
1223 .lkp_value = "peercredits_hiw",
1224 .lkp_data_type = NLA_U32
1226 [LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND] = {
1227 .lkp_value = "map_on_demand",
1228 .lkp_data_type = NLA_FLAG
1230 [LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS] = {
1231 .lkp_value = "concurrent_sends",
1232 .lkp_data_type = NLA_U32
1234 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE] = {
1235 .lkp_value = "fmr_pool_size",
1236 .lkp_data_type = NLA_U32
1238 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER] = {
1239 .lkp_value = "fmr_flush_trigger",
1240 .lkp_data_type = NLA_U32
1242 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE] = {
1243 .lkp_value = "fmr_cache",
1244 .lkp_data_type = NLA_U32
1246 [LNET_NET_O2IBLND_TUNABLES_ATTR_NTX] = {
1248 .lkp_data_type = NLA_U16
1250 [LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER] = {
1251 .lkp_value = "conns_per_peer",
1252 .lkp_data_type = NLA_U16
1254 [LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT] = {
1255 .lkp_value = "timeout",
1256 .lkp_data_type = NLA_U32,
1262 kiblnd_nl_get(int cmd, struct sk_buff *msg, int type, void *data)
1264 struct lnet_ioctl_config_o2iblnd_tunables *tuns;
1265 struct lnet_ni *ni = data;
1270 if (cmd != LNET_CMD_NETS || type != LNET_NET_LOCAL_NI_ATTR_LND_TUNABLES)
1273 tuns = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1274 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS,
1275 tuns->lnd_peercredits_hiw);
1276 if (tuns->lnd_map_on_demand) {
1278 LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND);
1280 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS,
1281 tuns->lnd_concurrent_sends);
1282 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE,
1283 tuns->lnd_fmr_pool_size);
1284 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER,
1285 tuns->lnd_fmr_flush_trigger);
1286 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE,
1287 tuns->lnd_fmr_cache);
1288 nla_put_u16(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_NTX, tuns->lnd_ntx);
1289 nla_put_u16(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER,
1290 tuns->lnd_conns_per_peer);
1291 nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT,
1298 kiblnd_nl_set(int cmd, struct nlattr *attr, int type, void *data)
1300 struct lnet_lnd_tunables *tunables = data;
1303 if (cmd != LNET_CMD_NETS)
1306 if (nla_type(attr) != LN_SCALAR_ATTR_INT_VALUE)
1310 case LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS:
1311 tunables->lnd_tun_u.lnd_o2ib.lnd_peercredits_hiw = nla_get_s64(attr);
1313 case LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND:
1314 tunables->lnd_tun_u.lnd_o2ib.lnd_map_on_demand = nla_get_s64(attr);
1316 case LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS:
1317 tunables->lnd_tun_u.lnd_o2ib.lnd_concurrent_sends = nla_get_s64(attr);
1319 case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE:
1320 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_pool_size = nla_get_s64(attr);
1322 case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER:
1323 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_flush_trigger = nla_get_s64(attr);
1325 case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE:
1326 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_cache = nla_get_s64(attr);
1328 case LNET_NET_O2IBLND_TUNABLES_ATTR_NTX:
1329 tunables->lnd_tun_u.lnd_o2ib.lnd_ntx = nla_get_s64(attr);
1331 case LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER:
1332 num = nla_get_s64(attr);
1333 clamp_t(s64, num, 1, 127);
1334 tunables->lnd_tun_u.lnd_o2ib.lnd_conns_per_peer = num;
1344 kiblnd_free_pages(struct kib_pages *p)
1346 int npages = p->ibp_npages;
1349 for (i = 0; i < npages; i++) {
1350 if (p->ibp_pages[i] != NULL)
1351 __free_page(p->ibp_pages[i]);
1354 LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1358 kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1360 struct kib_pages *p;
1363 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1364 offsetof(struct kib_pages, ibp_pages[npages]));
1366 CERROR("Can't allocate descriptor for %d pages\n", npages);
1370 memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1371 p->ibp_npages = npages;
1373 for (i = 0; i < npages; i++) {
1374 p->ibp_pages[i] = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1376 if (p->ibp_pages[i] == NULL) {
1377 CERROR("Can't allocate page %d of %d\n", i, npages);
1378 kiblnd_free_pages(p);
1388 kiblnd_unmap_rx_descs(struct kib_conn *conn)
1393 LASSERT (conn->ibc_rxs != NULL);
1394 LASSERT (conn->ibc_hdev != NULL);
1396 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1397 rx = &conn->ibc_rxs[i];
1399 LASSERT(rx->rx_nob >= 0); /* not posted */
1401 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1402 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1404 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1407 kiblnd_free_pages(conn->ibc_rx_pages);
1409 conn->ibc_rx_pages = NULL;
1413 kiblnd_map_rx_descs(struct kib_conn *conn)
1421 for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1422 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1423 rx = &conn->ibc_rxs[i];
1426 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1429 kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1430 rx->rx_msg, IBLND_MSG_SIZE,
1432 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1434 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1436 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1437 i, rx->rx_msg, rx->rx_msgaddr,
1438 (__u64)(page_to_phys(pg) + pg_off));
1440 pg_off += IBLND_MSG_SIZE;
1441 LASSERT(pg_off <= PAGE_SIZE);
1443 if (pg_off == PAGE_SIZE) {
1446 LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1452 kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1454 struct kib_hca_dev *hdev = tpo->tpo_hdev;
1458 LASSERT (tpo->tpo_pool.po_allocated == 0);
1463 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1464 tx = &tpo->tpo_tx_descs[i];
1465 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1466 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1468 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1471 kiblnd_hdev_decref(hdev);
1472 tpo->tpo_hdev = NULL;
1475 static struct kib_hca_dev *
1476 kiblnd_current_hdev(struct kib_dev *dev)
1478 struct kib_hca_dev *hdev;
1479 unsigned long flags;
1482 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1483 while (dev->ibd_failover) {
1484 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1486 CDEBUG(D_NET, "%s: Wait for failover\n",
1488 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
1490 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1493 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1494 hdev = dev->ibd_hdev;
1496 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1502 kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1504 struct kib_pages *txpgs = tpo->tpo_tx_pages;
1505 struct kib_pool *pool = &tpo->tpo_pool;
1506 struct kib_net *net = pool->po_owner->ps_net;
1507 struct kib_dev *dev;
1514 LASSERT (net != NULL);
1518 /* pre-mapped messages are not bigger than 1 page */
1519 BUILD_BUG_ON(IBLND_MSG_SIZE > PAGE_SIZE);
1521 /* No fancy arithmetic when we do the buffer calculations */
1522 BUILD_BUG_ON(PAGE_SIZE % IBLND_MSG_SIZE != 0);
1524 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1526 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1527 page = txpgs->ibp_pages[ipage];
1528 tx = &tpo->tpo_tx_descs[i];
1530 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1533 tx->tx_msgaddr = kiblnd_dma_map_single(tpo->tpo_hdev->ibh_ibdev,
1537 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1539 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1541 list_add(&tx->tx_list, &pool->po_free_list);
1543 page_offset += IBLND_MSG_SIZE;
1544 LASSERT(page_offset <= PAGE_SIZE);
1546 if (page_offset == PAGE_SIZE) {
1549 LASSERT(ipage <= txpgs->ibp_npages);
1555 kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1557 LASSERT(fpo->fpo_map_count == 0);
1559 #ifdef HAVE_FMR_POOL_API
1560 if (fpo->fpo_is_fmr && fpo->fmr.fpo_fmr_pool) {
1561 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1563 #endif /* HAVE_FMR_POOL_API */
1565 struct kib_fast_reg_descriptor *frd, *tmp;
1568 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1570 list_del(&frd->frd_list);
1571 #ifndef HAVE_IB_MAP_MR_SG
1572 ib_free_fast_reg_page_list(frd->frd_frpl);
1574 ib_dereg_mr(frd->frd_mr);
1575 LIBCFS_FREE(frd, sizeof(*frd));
1578 if (i < fpo->fast_reg.fpo_pool_size)
1579 CERROR("FastReg pool still has %d regions registered\n",
1580 fpo->fast_reg.fpo_pool_size - i);
1584 kiblnd_hdev_decref(fpo->fpo_hdev);
1586 LIBCFS_FREE(fpo, sizeof(*fpo));
1590 kiblnd_destroy_fmr_pool_list(struct list_head *head)
1592 struct kib_fmr_pool *fpo, *tmp;
1594 list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1595 list_del(&fpo->fpo_list);
1596 kiblnd_destroy_fmr_pool(fpo);
1601 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1604 int size = tunables->lnd_fmr_pool_size / ncpts;
1606 return max(IBLND_FMR_POOL, size);
1610 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1613 int size = tunables->lnd_fmr_flush_trigger / ncpts;
1615 return max(IBLND_FMR_POOL_FLUSH, size);
1618 #ifdef HAVE_FMR_POOL_API
1619 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps,
1620 struct kib_fmr_pool *fpo)
1622 struct ib_fmr_pool_param param = {
1623 .max_pages_per_fmr = IBLND_MAX_RDMA_FRAGS,
1624 .page_shift = PAGE_SHIFT,
1625 .access = (IB_ACCESS_LOCAL_WRITE |
1626 IB_ACCESS_REMOTE_WRITE),
1627 .pool_size = fps->fps_pool_size,
1628 .dirty_watermark = fps->fps_flush_trigger,
1629 .flush_function = NULL,
1631 .cache = !!fps->fps_cache };
1634 fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1636 if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1637 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1639 CERROR("Failed to create FMR pool: %d\n", rc);
1641 CERROR("FMRs are not supported\n");
1643 fpo->fpo_is_fmr = true;
1647 #endif /* HAVE_FMR_POOL_API */
1649 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps,
1650 struct kib_fmr_pool *fpo,
1651 enum kib_dev_caps dev_caps)
1653 struct kib_fast_reg_descriptor *frd, *tmp;
1656 #ifdef HAVE_FMR_POOL_API
1657 fpo->fpo_is_fmr = false;
1660 INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1661 fpo->fast_reg.fpo_pool_size = 0;
1662 for (i = 0; i < fps->fps_pool_size; i++) {
1663 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1666 CERROR("Failed to allocate a new fast_reg descriptor\n");
1672 #ifndef HAVE_IB_MAP_MR_SG
1673 frd->frd_frpl = ib_alloc_fast_reg_page_list(fpo->fpo_hdev->ibh_ibdev,
1674 IBLND_MAX_RDMA_FRAGS);
1675 if (IS_ERR(frd->frd_frpl)) {
1676 rc = PTR_ERR(frd->frd_frpl);
1677 CERROR("Failed to allocate ib_fast_reg_page_list: %d\n",
1679 frd->frd_frpl = NULL;
1684 #ifdef HAVE_IB_ALLOC_FAST_REG_MR
1685 frd->frd_mr = ib_alloc_fast_reg_mr(fpo->fpo_hdev->ibh_pd,
1686 IBLND_MAX_RDMA_FRAGS);
1689 * it is expected to get here if this is an MLX-5 card.
1690 * MLX-4 cards will always use FMR and MLX-5 cards will
1691 * always use fast_reg. It turns out that some MLX-5 cards
1692 * (possibly due to older FW versions) do not natively support
1693 * gaps. So we will need to track them here.
1695 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1696 #ifdef IB_MR_TYPE_SG_GAPS
1697 ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1698 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT)) ?
1699 IB_MR_TYPE_SG_GAPS :
1704 IBLND_MAX_RDMA_FRAGS);
1705 if ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1706 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT))
1707 CWARN("using IB_MR_TYPE_SG_GAPS, expect a performance drop\n");
1709 if (IS_ERR(frd->frd_mr)) {
1710 rc = PTR_ERR(frd->frd_mr);
1711 CERROR("Failed to allocate ib_fast_reg_mr: %d\n", rc);
1716 /* indicate that the local invalidate needs to be generated */
1717 frd->frd_valid = false;
1719 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1720 fpo->fast_reg.fpo_pool_size++;
1727 ib_dereg_mr(frd->frd_mr);
1728 #ifndef HAVE_IB_MAP_MR_SG
1730 ib_free_fast_reg_page_list(frd->frd_frpl);
1732 LIBCFS_FREE(frd, sizeof(*frd));
1735 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1737 list_del(&frd->frd_list);
1738 #ifndef HAVE_IB_MAP_MR_SG
1739 ib_free_fast_reg_page_list(frd->frd_frpl);
1741 ib_dereg_mr(frd->frd_mr);
1742 LIBCFS_FREE(frd, sizeof(*frd));
1748 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1749 struct kib_fmr_pool **pp_fpo)
1751 struct kib_dev *dev = fps->fps_net->ibn_dev;
1752 struct kib_fmr_pool *fpo;
1755 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1759 memset(fpo, 0, sizeof(*fpo));
1761 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1763 #ifdef HAVE_FMR_POOL_API
1764 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
1765 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1767 #endif /* HAVE_FMR_POOL_API */
1768 rc = kiblnd_alloc_freg_pool(fps, fpo, dev->ibd_dev_caps);
1772 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1773 fpo->fpo_owner = fps;
1779 kiblnd_hdev_decref(fpo->fpo_hdev);
1780 LIBCFS_FREE(fpo, sizeof(*fpo));
1785 kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps, struct list_head *zombies)
1787 struct kib_fmr_pool *fpo;
1789 if (fps->fps_net == NULL) /* intialized? */
1792 spin_lock(&fps->fps_lock);
1794 while ((fpo = list_first_entry_or_null(&fps->fps_pool_list,
1795 struct kib_fmr_pool,
1796 fpo_list)) != NULL) {
1797 fpo->fpo_failed = 1;
1798 if (fpo->fpo_map_count == 0)
1799 list_move(&fpo->fpo_list, zombies);
1801 list_move(&fpo->fpo_list, &fps->fps_failed_pool_list);
1804 spin_unlock(&fps->fps_lock);
1808 kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1810 if (fps->fps_net != NULL) { /* initialized? */
1811 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1812 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1817 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1818 struct kib_net *net,
1819 struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1821 struct kib_fmr_pool *fpo;
1824 memset(fps, 0, sizeof(struct kib_fmr_poolset));
1829 fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1830 fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1831 fps->fps_cache = tunables->lnd_fmr_cache;
1833 spin_lock_init(&fps->fps_lock);
1834 INIT_LIST_HEAD(&fps->fps_pool_list);
1835 INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1837 rc = kiblnd_create_fmr_pool(fps, &fpo);
1839 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1845 kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, time64_t now)
1847 if (fpo->fpo_map_count != 0) /* still in use */
1849 if (fpo->fpo_failed)
1851 return now >= fpo->fpo_deadline;
1854 #if defined(HAVE_FMR_POOL_API) || !defined(HAVE_IB_MAP_MR_SG)
1856 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1858 struct kib_hca_dev *hdev;
1859 __u64 *pages = tx->tx_pages;
1864 hdev = tx->tx_pool->tpo_hdev;
1866 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1867 for (size = 0; size < rd->rd_frags[i].rf_nob;
1868 size += hdev->ibh_page_size) {
1869 pages[npages++] = (rd->rd_frags[i].rf_addr &
1870 hdev->ibh_page_mask) + size;
1879 kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1882 struct kib_fmr_pool *fpo = fmr->fmr_pool;
1883 struct kib_fmr_poolset *fps;
1884 time64_t now = ktime_get_seconds();
1885 struct kib_fmr_pool *tmp;
1890 fps = fpo->fpo_owner;
1892 #ifdef HAVE_FMR_POOL_API
1893 if (fpo->fpo_is_fmr) {
1894 if (fmr->fmr_pfmr) {
1895 ib_fmr_pool_unmap(fmr->fmr_pfmr);
1896 fmr->fmr_pfmr = NULL;
1900 int rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1904 #endif /* HAVE_FMR_POOL_API */
1906 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1908 frd->frd_posted = false;
1909 fmr->fmr_frd = NULL;
1910 spin_lock(&fps->fps_lock);
1911 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1912 spin_unlock(&fps->fps_lock);
1915 fmr->fmr_pool = NULL;
1917 spin_lock(&fps->fps_lock);
1918 fpo->fpo_map_count--; /* decref the pool */
1920 list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1921 /* the first pool is persistent */
1922 if (fps->fps_pool_list.next == &fpo->fpo_list)
1925 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1926 list_move(&fpo->fpo_list, &zombies);
1930 spin_unlock(&fps->fps_lock);
1932 if (!list_empty(&zombies))
1933 kiblnd_destroy_fmr_pool_list(&zombies);
1936 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1937 struct kib_rdma_desc *rd, u32 nob, u64 iov,
1938 struct kib_fmr *fmr)
1940 struct kib_fmr_pool *fpo;
1942 bool is_rx = (rd != tx->tx_rd);
1943 #ifdef HAVE_FMR_POOL_API
1944 __u64 *pages = tx->tx_pages;
1945 bool tx_pages_mapped = false;
1951 spin_lock(&fps->fps_lock);
1952 version = fps->fps_version;
1953 list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1954 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1955 fpo->fpo_map_count++;
1957 #ifdef HAVE_FMR_POOL_API
1958 fmr->fmr_pfmr = NULL;
1959 if (fpo->fpo_is_fmr) {
1960 struct ib_pool_fmr *pfmr;
1962 spin_unlock(&fps->fps_lock);
1964 if (!tx_pages_mapped) {
1965 npages = kiblnd_map_tx_pages(tx, rd);
1966 tx_pages_mapped = true;
1969 pfmr = kib_fmr_pool_map(fpo->fmr.fpo_fmr_pool,
1970 pages, npages, iov);
1971 if (likely(!IS_ERR(pfmr))) {
1972 fmr->fmr_key = is_rx ? pfmr->fmr->rkey
1974 fmr->fmr_frd = NULL;
1975 fmr->fmr_pfmr = pfmr;
1976 fmr->fmr_pool = fpo;
1981 #endif /* HAVE_FMR_POOL_API */
1983 if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
1984 struct kib_fast_reg_descriptor *frd;
1985 #ifdef HAVE_IB_MAP_MR_SG
1986 struct ib_reg_wr *wr;
1989 struct ib_rdma_wr *wr;
1990 struct ib_fast_reg_page_list *frpl;
1994 frd = list_first_entry(
1995 &fpo->fast_reg.fpo_pool_list,
1996 struct kib_fast_reg_descriptor,
1998 list_del(&frd->frd_list);
1999 spin_unlock(&fps->fps_lock);
2001 #ifndef HAVE_IB_MAP_MR_SG
2002 frpl = frd->frd_frpl;
2006 if (!frd->frd_valid) {
2007 struct ib_rdma_wr *inv_wr;
2008 __u32 key = is_rx ? mr->rkey : mr->lkey;
2010 frd->frd_valid = true;
2011 inv_wr = &frd->frd_inv_wr;
2012 memset(inv_wr, 0, sizeof(*inv_wr));
2014 inv_wr->wr.opcode = IB_WR_LOCAL_INV;
2015 inv_wr->wr.wr_id = IBLND_WID_MR;
2016 inv_wr->wr.ex.invalidate_rkey = key;
2019 key = ib_inc_rkey(key);
2020 ib_update_fast_reg_key(mr, key);
2023 #ifdef HAVE_IB_MAP_MR_SG
2024 #ifdef HAVE_IB_MAP_MR_SG_5ARGS
2025 n = ib_map_mr_sg(mr, tx->tx_frags,
2026 rd->rd_nfrags, NULL, PAGE_SIZE);
2028 n = ib_map_mr_sg(mr, tx->tx_frags,
2029 rd->rd_nfrags, PAGE_SIZE);
2030 #endif /* HAVE_IB_MAP_MR_SG_5ARGS */
2031 if (unlikely(n != rd->rd_nfrags)) {
2032 CERROR("Failed to map mr %d/%d elements\n",
2034 return n < 0 ? n : -EINVAL;
2037 wr = &frd->frd_fastreg_wr;
2038 memset(wr, 0, sizeof(*wr));
2040 wr->wr.opcode = IB_WR_REG_MR;
2041 wr->wr.wr_id = IBLND_WID_MR;
2043 wr->wr.send_flags = 0;
2045 wr->key = is_rx ? mr->rkey : mr->lkey;
2046 wr->access = (IB_ACCESS_LOCAL_WRITE |
2047 IB_ACCESS_REMOTE_WRITE);
2048 #else /* HAVE_IB_MAP_MR_SG */
2049 if (!tx_pages_mapped) {
2050 npages = kiblnd_map_tx_pages(tx, rd);
2051 tx_pages_mapped = true;
2054 LASSERT(npages <= frpl->max_page_list_len);
2055 memcpy(frpl->page_list, pages,
2056 sizeof(*pages) * npages);
2058 /* Prepare FastReg WR */
2059 wr = &frd->frd_fastreg_wr;
2060 memset(wr, 0, sizeof(*wr));
2062 wr->wr.opcode = IB_WR_FAST_REG_MR;
2063 wr->wr.wr_id = IBLND_WID_MR;
2065 wr->wr.wr.fast_reg.iova_start = iov;
2066 wr->wr.wr.fast_reg.page_list = frpl;
2067 wr->wr.wr.fast_reg.page_list_len = npages;
2068 wr->wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2069 wr->wr.wr.fast_reg.length = nob;
2070 wr->wr.wr.fast_reg.rkey =
2071 is_rx ? mr->rkey : mr->lkey;
2072 wr->wr.wr.fast_reg.access_flags =
2073 (IB_ACCESS_LOCAL_WRITE |
2074 IB_ACCESS_REMOTE_WRITE);
2075 #endif /* HAVE_IB_MAP_MR_SG */
2077 fmr->fmr_key = is_rx ? mr->rkey : mr->lkey;
2079 fmr->fmr_pool = fpo;
2080 frd->frd_posted = false;
2083 spin_unlock(&fps->fps_lock);
2087 spin_lock(&fps->fps_lock);
2088 fpo->fpo_map_count--;
2089 if (rc != -EAGAIN) {
2090 spin_unlock(&fps->fps_lock);
2094 /* EAGAIN and ... */
2095 if (version != fps->fps_version) {
2096 spin_unlock(&fps->fps_lock);
2101 if (fps->fps_increasing) {
2102 spin_unlock(&fps->fps_lock);
2103 CDEBUG(D_NET, "Another thread is allocating new "
2104 "FMR pool, waiting for her to complete\n");
2105 wait_var_event(fps, !fps->fps_increasing);
2110 if (ktime_get_seconds() < fps->fps_next_retry) {
2111 /* someone failed recently */
2112 spin_unlock(&fps->fps_lock);
2116 fps->fps_increasing = 1;
2117 spin_unlock(&fps->fps_lock);
2119 CDEBUG(D_NET, "Allocate new FMR pool\n");
2120 rc = kiblnd_create_fmr_pool(fps, &fpo);
2121 spin_lock(&fps->fps_lock);
2122 fps->fps_increasing = 0;
2126 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
2128 fps->fps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2130 spin_unlock(&fps->fps_lock);
2136 kiblnd_fini_pool(struct kib_pool *pool)
2138 LASSERT(list_empty(&pool->po_free_list));
2139 LASSERT(pool->po_allocated == 0);
2141 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
2145 kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
2147 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
2149 memset(pool, 0, sizeof(struct kib_pool));
2150 INIT_LIST_HEAD(&pool->po_free_list);
2151 pool->po_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
2152 pool->po_owner = ps;
2153 pool->po_size = size;
2157 kiblnd_destroy_pool_list(struct list_head *head)
2159 struct kib_pool *pool;
2161 while ((pool = list_first_entry_or_null(head,
2163 po_list)) != NULL) {
2164 list_del(&pool->po_list);
2166 LASSERT(pool->po_owner != NULL);
2167 pool->po_owner->ps_pool_destroy(pool);
2172 kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
2174 struct kib_pool *po;
2176 if (ps->ps_net == NULL) /* intialized? */
2179 spin_lock(&ps->ps_lock);
2180 while ((po = list_first_entry_or_null(&ps->ps_pool_list,
2182 po_list)) != NULL) {
2184 if (po->po_allocated == 0)
2185 list_move(&po->po_list, zombies);
2187 list_move(&po->po_list, &ps->ps_failed_pool_list);
2189 spin_unlock(&ps->ps_lock);
2193 kiblnd_fini_poolset(struct kib_poolset *ps)
2195 if (ps->ps_net != NULL) { /* initialized? */
2196 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
2197 kiblnd_destroy_pool_list(&ps->ps_pool_list);
2202 kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
2203 struct kib_net *net, char *name, int size,
2204 kib_ps_pool_create_t po_create,
2205 kib_ps_pool_destroy_t po_destroy,
2206 kib_ps_node_init_t nd_init,
2207 kib_ps_node_fini_t nd_fini)
2209 struct kib_pool *pool;
2212 memset(ps, 0, sizeof(struct kib_poolset));
2216 ps->ps_pool_create = po_create;
2217 ps->ps_pool_destroy = po_destroy;
2218 ps->ps_node_init = nd_init;
2219 ps->ps_node_fini = nd_fini;
2220 ps->ps_pool_size = size;
2221 if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name))
2222 >= sizeof(ps->ps_name))
2224 spin_lock_init(&ps->ps_lock);
2225 INIT_LIST_HEAD(&ps->ps_pool_list);
2226 INIT_LIST_HEAD(&ps->ps_failed_pool_list);
2228 rc = ps->ps_pool_create(ps, size, &pool);
2230 list_add(&pool->po_list, &ps->ps_pool_list);
2232 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
2238 kiblnd_pool_is_idle(struct kib_pool *pool, time64_t now)
2240 if (pool->po_allocated != 0) /* still in use */
2242 if (pool->po_failed)
2244 return now >= pool->po_deadline;
2248 kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
2251 struct kib_poolset *ps = pool->po_owner;
2252 struct kib_pool *tmp;
2253 time64_t now = ktime_get_seconds();
2255 spin_lock(&ps->ps_lock);
2257 if (ps->ps_node_fini != NULL)
2258 ps->ps_node_fini(pool, node);
2260 LASSERT(pool->po_allocated > 0);
2261 list_add(node, &pool->po_free_list);
2262 pool->po_allocated--;
2264 list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
2265 /* the first pool is persistent */
2266 if (ps->ps_pool_list.next == &pool->po_list)
2269 if (kiblnd_pool_is_idle(pool, now))
2270 list_move(&pool->po_list, &zombies);
2272 spin_unlock(&ps->ps_lock);
2274 if (!list_empty(&zombies))
2275 kiblnd_destroy_pool_list(&zombies);
2279 kiblnd_pool_alloc_node(struct kib_poolset *ps)
2281 struct list_head *node;
2282 struct kib_pool *pool;
2284 unsigned int interval = 1;
2285 ktime_t time_before;
2286 unsigned int trips = 0;
2289 spin_lock(&ps->ps_lock);
2290 list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
2291 if (list_empty(&pool->po_free_list))
2294 pool->po_allocated++;
2295 pool->po_deadline = ktime_get_seconds() +
2296 IBLND_POOL_DEADLINE;
2297 node = pool->po_free_list.next;
2300 if (ps->ps_node_init != NULL) {
2301 /* still hold the lock */
2302 ps->ps_node_init(pool, node);
2304 spin_unlock(&ps->ps_lock);
2308 /* no available tx pool and ... */
2309 if (ps->ps_increasing) {
2310 /* another thread is allocating a new pool */
2311 spin_unlock(&ps->ps_lock);
2314 "Another thread is allocating new %s pool, waiting %d jiffies for her to complete. trips = %d\n",
2315 ps->ps_name, interval, trips);
2317 schedule_timeout_interruptible(interval);
2318 if (interval < cfs_time_seconds(1))
2324 if (ktime_get_seconds() < ps->ps_next_retry) {
2325 /* someone failed recently */
2326 spin_unlock(&ps->ps_lock);
2330 ps->ps_increasing = 1;
2331 spin_unlock(&ps->ps_lock);
2333 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
2334 time_before = ktime_get();
2335 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
2336 CDEBUG(D_NET, "ps_pool_create took %lld ms to complete\n",
2337 ktime_ms_delta(ktime_get(), time_before));
2339 spin_lock(&ps->ps_lock);
2340 ps->ps_increasing = 0;
2342 list_add_tail(&pool->po_list, &ps->ps_pool_list);
2344 ps->ps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2345 CERROR("Can't allocate new %s pool because out of memory\n",
2348 spin_unlock(&ps->ps_lock);
2354 kiblnd_destroy_tx_pool(struct kib_pool *pool)
2356 struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool,
2360 LASSERT (pool->po_allocated == 0);
2362 if (tpo->tpo_tx_pages != NULL) {
2363 kiblnd_unmap_tx_pool(tpo);
2364 kiblnd_free_pages(tpo->tpo_tx_pages);
2367 if (tpo->tpo_tx_descs == NULL)
2370 for (i = 0; i < pool->po_size; i++) {
2371 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2372 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2374 list_del(&tx->tx_list);
2375 if (tx->tx_pages != NULL)
2376 CFS_FREE_PTR_ARRAY(tx->tx_pages, LNET_MAX_IOV);
2377 if (tx->tx_frags != NULL)
2378 CFS_FREE_PTR_ARRAY(tx->tx_frags,
2379 IBLND_MAX_RDMA_FRAGS);
2380 if (tx->tx_wrq != NULL)
2381 CFS_FREE_PTR_ARRAY(tx->tx_wrq,
2382 IBLND_MAX_RDMA_FRAGS);
2383 if (tx->tx_sge != NULL) {
2384 /* +1 is for the lnet header/message itself */
2385 CFS_FREE_PTR_ARRAY(tx->tx_sge,
2386 (IBLND_MAX_RDMA_FRAGS *
2389 if (tx->tx_rd != NULL)
2390 LIBCFS_FREE(tx->tx_rd,
2391 offsetof(struct kib_rdma_desc,
2392 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2395 CFS_FREE_PTR_ARRAY(tpo->tpo_tx_descs, pool->po_size);
2397 kiblnd_fini_pool(pool);
2401 static int kiblnd_tx_pool_size(struct lnet_ni *ni, int ncpts)
2403 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2406 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2407 ntx = tunables->lnd_ntx / ncpts;
2409 return max(IBLND_TX_POOL, ntx);
2413 kiblnd_create_tx_pool(struct kib_poolset *ps, int size, struct kib_pool **pp_po)
2417 struct kib_pool *pool;
2418 struct kib_tx_pool *tpo;
2420 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2422 CERROR("Failed to allocate TX pool\n");
2426 pool = &tpo->tpo_pool;
2427 kiblnd_init_pool(ps, pool, size);
2428 tpo->tpo_tx_descs = NULL;
2429 tpo->tpo_tx_pages = NULL;
2431 npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2432 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg) != 0) {
2433 CERROR("Can't allocate tx pages: %d\n", npg);
2438 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2439 size * sizeof(struct kib_tx));
2440 if (tpo->tpo_tx_descs == NULL) {
2441 CERROR("Can't allocate %d tx descriptors\n", size);
2442 ps->ps_pool_destroy(pool);
2446 memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2448 for (i = 0; i < size; i++) {
2449 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2450 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2453 if (ps->ps_net->ibn_fmr_ps != NULL) {
2454 LIBCFS_CPT_ALLOC(tx->tx_pages,
2455 lnet_cpt_table(), ps->ps_cpt,
2456 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2457 if (tx->tx_pages == NULL)
2461 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2462 IBLND_MAX_RDMA_FRAGS *
2463 sizeof(*tx->tx_frags));
2464 if (tx->tx_frags == NULL)
2467 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS);
2469 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2470 IBLND_MAX_RDMA_FRAGS *
2471 sizeof(*tx->tx_wrq));
2472 if (tx->tx_wrq == NULL)
2475 /* +1 is for the lnet header/message itself */
2476 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2477 (IBLND_MAX_RDMA_FRAGS * wrq_sge + 1) *
2478 sizeof(*tx->tx_sge));
2479 if (tx->tx_sge == NULL)
2482 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2483 offsetof(struct kib_rdma_desc,
2484 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2485 if (tx->tx_rd == NULL)
2490 kiblnd_map_tx_pool(tpo);
2495 ps->ps_pool_destroy(pool);
2500 kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2502 struct kib_tx_poolset *tps = container_of(pool->po_owner,
2503 struct kib_tx_poolset,
2505 struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list);
2507 tx->tx_cookie = tps->tps_next_tx_cookie++;
2511 kiblnd_net_fini_pools(struct kib_net *net)
2515 cfs_cpt_for_each(i, lnet_cpt_table()) {
2516 struct kib_tx_poolset *tps;
2517 struct kib_fmr_poolset *fps;
2519 if (net->ibn_tx_ps != NULL) {
2520 tps = net->ibn_tx_ps[i];
2521 kiblnd_fini_poolset(&tps->tps_poolset);
2524 if (net->ibn_fmr_ps != NULL) {
2525 fps = net->ibn_fmr_ps[i];
2526 kiblnd_fini_fmr_poolset(fps);
2530 if (net->ibn_tx_ps != NULL) {
2531 cfs_percpt_free(net->ibn_tx_ps);
2532 net->ibn_tx_ps = NULL;
2535 if (net->ibn_fmr_ps != NULL) {
2536 cfs_percpt_free(net->ibn_fmr_ps);
2537 net->ibn_fmr_ps = NULL;
2542 kiblnd_net_init_pools(struct kib_net *net, struct lnet_ni *ni, __u32 *cpts,
2545 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2546 #ifdef HAVE_IB_GET_DMA_MR
2547 unsigned long flags;
2553 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2555 #ifdef HAVE_IB_GET_DMA_MR
2556 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2558 * if lnd_map_on_demand is zero then we have effectively disabled
2559 * FMR or FastReg and we're using global memory regions
2562 if (!tunables->lnd_map_on_demand) {
2563 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2565 goto create_tx_pool;
2568 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2571 if (tunables->lnd_fmr_pool_size < tunables->lnd_ntx / 4) {
2572 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2573 tunables->lnd_fmr_pool_size,
2574 tunables->lnd_ntx / 4);
2579 /* TX pool must be created later than FMR, see LU-2268
2581 LASSERT(net->ibn_tx_ps == NULL);
2583 /* premapping can fail if ibd_nmr > 1, so we always create
2584 * FMR pool and map-on-demand if premapping failed */
2586 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2587 sizeof(struct kib_fmr_poolset));
2588 if (net->ibn_fmr_ps == NULL) {
2589 CERROR("Failed to allocate FMR pool array\n");
2594 for (i = 0; i < ncpts; i++) {
2595 cpt = (cpts == NULL) ? i : cpts[i];
2596 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2599 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2606 LASSERT(i == ncpts);
2608 #ifdef HAVE_IB_GET_DMA_MR
2611 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2612 sizeof(struct kib_tx_poolset));
2613 if (net->ibn_tx_ps == NULL) {
2614 CERROR("Failed to allocate tx pool array\n");
2619 for (i = 0; i < ncpts; i++) {
2620 cpt = (cpts == NULL) ? i : cpts[i];
2621 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2623 kiblnd_tx_pool_size(ni, ncpts),
2624 kiblnd_create_tx_pool,
2625 kiblnd_destroy_tx_pool,
2626 kiblnd_tx_init, NULL);
2628 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2636 kiblnd_net_fini_pools(net);
2642 kiblnd_port_get_attr(struct kib_hca_dev *hdev)
2644 struct ib_port_attr *port_attr;
2646 unsigned long flags;
2647 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2649 LIBCFS_ALLOC(port_attr, sizeof(*port_attr));
2650 if (port_attr == NULL) {
2651 CDEBUG(D_NETERROR, "Out of memory\n");
2655 rc = ib_query_port(hdev->ibh_ibdev, hdev->ibh_port, port_attr);
2657 write_lock_irqsave(g_lock, flags);
2660 hdev->ibh_state = port_attr->state == IB_PORT_ACTIVE
2661 ? IBLND_DEV_PORT_ACTIVE
2662 : IBLND_DEV_PORT_DOWN;
2664 write_unlock_irqrestore(g_lock, flags);
2665 LIBCFS_FREE(port_attr, sizeof(*port_attr));
2668 CDEBUG(D_NETERROR, "Failed to query IB port: %d\n", rc);
2675 kiblnd_set_ni_fatal_on(struct kib_hca_dev *hdev, int val)
2677 struct kib_net *net;
2678 __u32 ni_state_before;
2679 bool update_ping_buf = false;
2680 struct lnet_ni *ni = NULL;
2682 /* for health check */
2683 list_for_each_entry(net, &hdev->ibh_dev->ibd_nets, ibn_list) {
2686 CDEBUG(D_NETERROR, "Fatal device error for NI %s\n",
2687 libcfs_nidstr(&ni->ni_nid));
2688 ni_state_before = lnet_set_link_fatal_state(ni, val);
2690 if (!update_ping_buf &&
2691 (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
2692 (val != ni_state_before) &&
2693 (net->ibn_init == IBLND_INIT_ALL))
2694 update_ping_buf = true;
2697 if (update_ping_buf)
2698 lnet_mark_ping_buffer_for_update();
2702 kiblnd_event_handler(struct ib_event_handler *handler, struct ib_event *event)
2704 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2705 struct kib_hca_dev *hdev;
2706 unsigned long flags;
2708 hdev = container_of(handler, struct kib_hca_dev, ibh_event_handler);
2710 write_lock_irqsave(g_lock, flags);
2712 switch (event->event) {
2713 case IB_EVENT_DEVICE_FATAL:
2714 CDEBUG(D_NET, "IB device fatal\n");
2715 hdev->ibh_state = IBLND_DEV_FATAL;
2716 kiblnd_set_ni_fatal_on(hdev, 1);
2718 case IB_EVENT_PORT_ACTIVE:
2719 CDEBUG(D_NET, "IB port active\n");
2720 if (event->element.port_num == hdev->ibh_port) {
2721 hdev->ibh_state = IBLND_DEV_PORT_ACTIVE;
2722 kiblnd_set_ni_fatal_on(hdev, 0);
2725 case IB_EVENT_PORT_ERR:
2726 CDEBUG(D_NET, "IB port err\n");
2727 if (event->element.port_num == hdev->ibh_port) {
2728 hdev->ibh_state = IBLND_DEV_PORT_DOWN;
2729 kiblnd_set_ni_fatal_on(hdev, 1);
2735 write_unlock_irqrestore(g_lock, flags);
2739 kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2741 struct ib_device_attr *dev_attr;
2745 /* It's safe to assume a HCA can handle a page size
2746 * matching that of the native system */
2747 hdev->ibh_page_shift = PAGE_SHIFT;
2748 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2749 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2751 #ifndef HAVE_IB_DEVICE_ATTRS
2752 LIBCFS_ALLOC(dev_attr, sizeof(*dev_attr));
2753 if (dev_attr == NULL) {
2754 CERROR("Out of memory\n");
2758 rc = ib_query_device(hdev->ibh_ibdev, dev_attr);
2760 CERROR("Failed to query IB device: %d\n", rc);
2761 goto out_clean_attr;
2764 dev_attr = &hdev->ibh_ibdev->attrs;
2767 hdev->ibh_mr_size = dev_attr->max_mr_size;
2768 hdev->ibh_max_qp_wr = dev_attr->max_qp_wr;
2770 /* Setup device Memory Registration capabilities */
2771 #ifdef HAVE_FMR_POOL_API
2772 #ifdef HAVE_IB_DEVICE_OPS
2773 if (hdev->ibh_ibdev->ops.alloc_fmr &&
2774 hdev->ibh_ibdev->ops.dealloc_fmr &&
2775 hdev->ibh_ibdev->ops.map_phys_fmr &&
2776 hdev->ibh_ibdev->ops.unmap_fmr) {
2778 if (hdev->ibh_ibdev->alloc_fmr &&
2779 hdev->ibh_ibdev->dealloc_fmr &&
2780 hdev->ibh_ibdev->map_phys_fmr &&
2781 hdev->ibh_ibdev->unmap_fmr) {
2783 LCONSOLE_INFO("Using FMR for registration\n");
2784 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FMR_ENABLED;
2786 #endif /* HAVE_FMR_POOL_API */
2787 if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
2788 LCONSOLE_INFO("Using FastReg for registration\n");
2789 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_ENABLED;
2790 #ifndef HAVE_IB_ALLOC_FAST_REG_MR
2791 #ifdef IB_DEVICE_SG_GAPS_REG
2792 if (dev_attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
2793 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT;
2800 rc2 = kiblnd_port_get_attr(hdev);
2807 #ifndef HAVE_IB_DEVICE_ATTRS
2809 LIBCFS_FREE(dev_attr, sizeof(*dev_attr));
2813 CERROR("IB device does not support FMRs nor FastRegs, can't "
2814 "register memory: %d\n", rc);
2815 else if (rc == -EINVAL)
2816 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2820 #ifdef HAVE_IB_GET_DMA_MR
2822 kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev)
2824 if (hdev->ibh_mrs == NULL)
2827 ib_dereg_mr(hdev->ibh_mrs);
2829 hdev->ibh_mrs = NULL;
2834 kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2836 if (hdev->ibh_event_handler.device != NULL)
2837 ib_unregister_event_handler(&hdev->ibh_event_handler);
2839 #ifdef HAVE_IB_GET_DMA_MR
2840 kiblnd_hdev_cleanup_mrs(hdev);
2843 if (hdev->ibh_pd != NULL)
2844 ib_dealloc_pd(hdev->ibh_pd);
2846 if (hdev->ibh_cmid != NULL)
2847 rdma_destroy_id(hdev->ibh_cmid);
2849 LIBCFS_FREE(hdev, sizeof(*hdev));
2852 #ifdef HAVE_IB_GET_DMA_MR
2854 kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev)
2857 int acflags = IB_ACCESS_LOCAL_WRITE |
2858 IB_ACCESS_REMOTE_WRITE;
2860 mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2862 CERROR("Failed ib_get_dma_mr: %ld\n", PTR_ERR(mr));
2863 kiblnd_hdev_cleanup_mrs(hdev);
2874 kiblnd_dummy_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2880 kiblnd_dev_need_failover(struct kib_dev *dev, struct net *ns)
2882 struct rdma_cm_id *cmid;
2883 struct sockaddr_in srcaddr;
2884 struct sockaddr_in dstaddr;
2887 if (dev->ibd_hdev == NULL || /* initializing */
2888 dev->ibd_hdev->ibh_cmid == NULL || /* listener is dead */
2889 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2892 /* XXX: it's UGLY, but I don't have better way to find
2893 * ib-bonding HCA failover because:
2895 * a. no reliable CM event for HCA failover...
2896 * b. no OFED API to get ib_device for current net_device...
2898 * We have only two choices at this point:
2900 * a. rdma_bind_addr(), it will conflict with listener cmid
2901 * b. rdma_resolve_addr() to zero addr */
2902 cmid = kiblnd_rdma_create_id(ns, kiblnd_dummy_callback, dev,
2903 RDMA_PS_TCP, IB_QPT_RC);
2906 CERROR("Failed to create cmid for failover: %d\n", rc);
2910 memset(&srcaddr, 0, sizeof(srcaddr));
2911 srcaddr.sin_family = AF_INET;
2912 srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2914 memset(&dstaddr, 0, sizeof(dstaddr));
2915 dstaddr.sin_family = AF_INET;
2916 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2917 (struct sockaddr *)&dstaddr, 1);
2918 if (rc != 0 || cmid->device == NULL) {
2919 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2920 dev->ibd_ifname, &dev->ibd_ifip,
2922 rdma_destroy_id(cmid);
2926 rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2927 rdma_destroy_id(cmid);
2932 kiblnd_dev_failover(struct kib_dev *dev, struct net *ns)
2934 LIST_HEAD(zombie_tpo);
2935 LIST_HEAD(zombie_ppo);
2936 LIST_HEAD(zombie_fpo);
2937 struct rdma_cm_id *cmid = NULL;
2938 struct kib_hca_dev *hdev = NULL;
2939 struct kib_hca_dev *old;
2941 struct kib_net *net;
2942 struct sockaddr_in addr;
2943 struct net_device *netdev;
2944 unsigned long flags;
2947 bool set_fatal = true;
2949 LASSERT(*kiblnd_tunables.kib_dev_failover > 1 ||
2950 dev->ibd_can_failover ||
2951 dev->ibd_hdev == NULL);
2953 rc = kiblnd_dev_need_failover(dev, ns);
2957 if (dev->ibd_hdev != NULL &&
2958 dev->ibd_hdev->ibh_cmid != NULL) {
2959 /* XXX it's not good to close old listener at here,
2960 * because we can fail to create new listener.
2961 * But we have to close it now, otherwise rdma_bind_addr
2962 * will return EADDRINUSE... How crap! */
2963 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2965 cmid = dev->ibd_hdev->ibh_cmid;
2966 /* make next schedule of kiblnd_dev_need_failover()
2967 * return 1 for me */
2968 dev->ibd_hdev->ibh_cmid = NULL;
2969 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2971 rdma_destroy_id(cmid);
2974 cmid = kiblnd_rdma_create_id(ns, kiblnd_cm_callback, dev, RDMA_PS_TCP,
2978 CERROR("Failed to create cmid for failover: %d\n", rc);
2982 memset(&addr, 0, sizeof(addr));
2983 addr.sin_family = AF_INET;
2984 addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2985 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2987 /* Bind to failover device or port */
2988 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2989 if (rc != 0 || cmid->device == NULL) {
2990 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2991 dev->ibd_ifname, &dev->ibd_ifip,
2993 if (!rc && !cmid->device)
2995 rdma_destroy_id(cmid);
2999 LIBCFS_ALLOC(hdev, sizeof(*hdev));
3001 CERROR("Failed to allocate kib_hca_dev\n");
3002 rdma_destroy_id(cmid);
3007 atomic_set(&hdev->ibh_ref, 1);
3008 hdev->ibh_dev = dev;
3009 hdev->ibh_cmid = cmid;
3010 hdev->ibh_ibdev = cmid->device;
3011 hdev->ibh_port = cmid->port_num;
3013 #ifdef HAVE_IB_ALLOC_PD_2ARGS
3014 pd = ib_alloc_pd(cmid->device, 0);
3016 pd = ib_alloc_pd(cmid->device);
3020 CERROR("Can't allocate PD: %d\n", rc);
3026 rc = rdma_listen(cmid, 0);
3028 CERROR("Can't start new listener: %d\n", rc);
3032 rc = kiblnd_hdev_get_attr(hdev);
3034 CERROR("Can't get device attributes: %d\n", rc);
3038 #ifdef HAVE_IB_GET_DMA_MR
3039 rc = kiblnd_hdev_setup_mrs(hdev);
3041 CERROR("Can't setup device: %d\n", rc);
3046 INIT_IB_EVENT_HANDLER(&hdev->ibh_event_handler,
3047 hdev->ibh_ibdev, kiblnd_event_handler);
3048 ib_register_event_handler(&hdev->ibh_event_handler);
3050 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3052 old = dev->ibd_hdev;
3053 dev->ibd_hdev = hdev; /* take over the refcount */
3056 list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
3057 cfs_cpt_for_each(i, lnet_cpt_table()) {
3058 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
3061 if (net->ibn_fmr_ps != NULL)
3062 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
3067 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3069 if (!list_empty(&zombie_tpo))
3070 kiblnd_destroy_pool_list(&zombie_tpo);
3071 if (!list_empty(&zombie_ppo))
3072 kiblnd_destroy_pool_list(&zombie_ppo);
3073 if (!list_empty(&zombie_fpo))
3074 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
3076 kiblnd_hdev_decref(hdev);
3079 dev->ibd_failed_failover++;
3081 dev->ibd_failed_failover = 0;
3085 netdev = dev_get_by_name_rcu(ns, dev->ibd_ifname);
3086 if (netdev && (lnet_get_link_status(netdev) == 1))
3087 kiblnd_set_ni_fatal_on(dev->ibd_hdev, 0);
3096 kiblnd_destroy_dev(struct kib_dev *dev)
3098 LASSERT(dev->ibd_nnets == 0);
3099 LASSERT(list_empty(&dev->ibd_nets));
3101 list_del(&dev->ibd_fail_list);
3102 list_del(&dev->ibd_list);
3104 if (dev->ibd_hdev != NULL)
3105 kiblnd_hdev_decref(dev->ibd_hdev);
3107 LIBCFS_FREE(dev, sizeof(*dev));
3110 static struct kib_dev *
3111 kiblnd_dev_search(char *ifname)
3113 struct kib_dev *alias = NULL;
3114 struct kib_dev *dev;
3118 colon = strchr(ifname, ':');
3119 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3120 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3126 colon2 = strchr(dev->ibd_ifname, ':');
3132 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3144 kiblnd_handle_link_state_change(struct net_device *dev,
3145 unsigned char operstate)
3147 struct lnet_ni *ni = NULL;
3148 struct kib_dev *event_kibdev;
3149 struct kib_net *net;
3150 struct kib_net *cnxt;
3151 bool link_down = !(operstate == IF_OPER_UP);
3152 struct in_device *in_dev;
3153 bool found_ip = false;
3154 __u32 ni_state_before;
3155 bool update_ping_buf = false;
3157 DECLARE_CONST_IN_IFADDR(ifa);
3159 event_kibdev = kiblnd_dev_search(dev->name);
3164 list_for_each_entry_safe(net, cnxt, &event_kibdev->ibd_nets, ibn_list) {
3168 in_dev = __in_dev_get_rtnl(dev);
3170 CDEBUG(D_NET, "Interface %s has no IPv4 status.\n",
3172 ni_state_before = lnet_set_link_fatal_state(ni, 1);
3175 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3176 if (htonl(event_kibdev->ibd_ifip) == ifa->ifa_local)
3182 CDEBUG(D_NET, "Interface %s has no matching ip\n",
3184 ni_state_before = lnet_set_link_fatal_state(ni, 1);
3189 ni_state_before = lnet_set_link_fatal_state(ni, 1);
3191 state = (lnet_get_link_status(dev) == 0);
3192 ni_state_before = lnet_set_link_fatal_state(ni,
3196 if (!update_ping_buf &&
3197 (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
3198 (atomic_read(&ni->ni_fatal_error_on) != ni_state_before) &&
3199 (net->ibn_init == IBLND_INIT_ALL))
3200 update_ping_buf = true;
3203 if (update_ping_buf)
3204 lnet_mark_ping_buffer_for_update();
3210 kiblnd_handle_inetaddr_change(struct in_ifaddr *ifa, unsigned long event)
3212 struct kib_dev *event_kibdev;
3213 struct kib_net *net;
3214 struct kib_net *cnxt;
3215 struct net_device *event_netdev = ifa->ifa_dev->dev;
3216 __u32 ni_state_before;
3217 bool update_ping_buf = false;
3218 struct lnet_ni *ni = NULL;
3221 event_kibdev = kiblnd_dev_search(event_netdev->name);
3226 if (htonl(event_kibdev->ibd_ifip) != ifa->ifa_local)
3229 list_for_each_entry_safe(net, cnxt, &event_kibdev->ibd_nets,
3232 link_down = (event == NETDEV_DOWN);
3233 ni_state_before = lnet_set_link_fatal_state(ni, link_down);
3234 if (!update_ping_buf &&
3235 (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
3236 ((event == NETDEV_DOWN) != ni_state_before) &&
3237 (net->ibn_init == IBLND_INIT_ALL))
3238 update_ping_buf = true;
3241 if (update_ping_buf)
3242 lnet_mark_ping_buffer_for_update();
3248 /************************************
3249 * Net device notifier event handler
3250 ************************************/
3251 static int kiblnd_device_event(struct notifier_block *unused,
3252 unsigned long event, void *ptr)
3254 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3255 unsigned char operstate;
3257 operstate = dev->operstate;
3259 CDEBUG(D_NET, "devevent: status=%ld, iface=%s ifindex %d state %u\n",
3260 event, dev->name, dev->ifindex, operstate);
3266 kiblnd_handle_link_state_change(dev, operstate);
3273 /************************************
3274 * Inetaddr notifier event handler
3275 ************************************/
3276 static int kiblnd_inetaddr_event(struct notifier_block *unused,
3277 unsigned long event, void *ptr)
3279 struct in_ifaddr *ifa = ptr;
3281 CDEBUG(D_NET, "addrevent: status %ld ip addr %pI4, netmask %pI4.\n",
3282 event, &ifa->ifa_address, &ifa->ifa_mask);
3288 kiblnd_handle_inetaddr_change(ifa, event);
3295 static struct notifier_block kiblnd_dev_notifier_block = {
3296 .notifier_call = kiblnd_device_event,
3299 static struct notifier_block kiblnd_inetaddr_notifier_block = {
3300 .notifier_call = kiblnd_inetaddr_event,
3304 kiblnd_base_shutdown(void)
3306 struct kib_sched_info *sched;
3307 struct kib_peer_ni *peer_ni;
3310 LASSERT(list_empty(&kiblnd_data.kib_devs));
3312 CDEBUG(D_MALLOC, "before LND base cleanup: kmem %lld\n",
3313 libcfs_kmem_read());
3315 if (kiblnd_data.kib_init == IBLND_INIT_ALL) {
3316 unregister_netdevice_notifier(&kiblnd_dev_notifier_block);
3317 unregister_inetaddr_notifier(&kiblnd_inetaddr_notifier_block);
3320 switch (kiblnd_data.kib_init) {
3324 case IBLND_INIT_ALL:
3325 case IBLND_INIT_DATA:
3326 hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list)
3328 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
3329 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
3330 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
3331 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
3333 /* flag threads to terminate; wake and wait for them to die */
3334 kiblnd_data.kib_shutdown = 1;
3336 /* NB: we really want to stop scheduler threads net by net
3337 * instead of the whole module, this should be improved
3338 * with dynamic configuration LNet.
3340 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
3341 wake_up_all(&sched->ibs_waitq);
3343 wake_up(&kiblnd_data.kib_connd_waitq);
3344 wake_up(&kiblnd_data.kib_failover_waitq);
3346 wait_var_event_warning(&kiblnd_data.kib_nthreads,
3347 !atomic_read(&kiblnd_data.kib_nthreads),
3348 "Waiting for %d threads to terminate\n",
3349 atomic_read(&kiblnd_data.kib_nthreads));
3352 case IBLND_INIT_NOTHING:
3356 if (kiblnd_data.kib_scheds != NULL)
3357 cfs_percpt_free(kiblnd_data.kib_scheds);
3359 CDEBUG(D_MALLOC, "after LND base cleanup: kmem %lld\n",
3360 libcfs_kmem_read());
3362 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
3363 module_put(THIS_MODULE);
3367 kiblnd_shutdown(struct lnet_ni *ni)
3369 struct kib_net *net = ni->ni_data;
3370 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
3371 unsigned long flags;
3373 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
3378 CDEBUG(D_MALLOC, "before LND net cleanup: kmem %lld\n",
3379 libcfs_kmem_read());
3381 write_lock_irqsave(g_lock, flags);
3382 net->ibn_shutdown = 1;
3383 write_unlock_irqrestore(g_lock, flags);
3385 switch (net->ibn_init) {
3389 case IBLND_INIT_ALL:
3390 /* nuke all existing peers within this net */
3391 kiblnd_del_peer(ni, LNET_NID_ANY);
3393 /* Wait for all peer_ni state to clean up */
3394 wait_var_event_warning(&net->ibn_npeers,
3395 atomic_read(&net->ibn_npeers) == 0,
3396 "%s: waiting for %d peers to disconnect\n",
3397 libcfs_nidstr(&ni->ni_nid),
3398 atomic_read(&net->ibn_npeers));
3400 kiblnd_net_fini_pools(net);
3402 write_lock_irqsave(g_lock, flags);
3403 LASSERT(net->ibn_dev->ibd_nnets > 0);
3404 net->ibn_dev->ibd_nnets--;
3405 list_del(&net->ibn_list);
3406 write_unlock_irqrestore(g_lock, flags);
3408 wake_up_all(&kiblnd_data.kib_connd_waitq);
3409 wait_var_event_warning(&net->ibn_nconns,
3410 atomic_read(&net->ibn_nconns) == 0,
3411 "%s: waiting for %d conns to clean\n",
3412 libcfs_nidstr(&ni->ni_nid),
3413 atomic_read(&net->ibn_nconns));
3416 case IBLND_INIT_NOTHING:
3417 LASSERT (atomic_read(&net->ibn_nconns) == 0);
3419 if (net->ibn_dev != NULL &&
3420 net->ibn_dev->ibd_nnets == 0)
3421 kiblnd_destroy_dev(net->ibn_dev);
3426 CDEBUG(D_MALLOC, "after LND net cleanup: kmem %lld\n",
3427 libcfs_kmem_read());
3429 net->ibn_init = IBLND_INIT_NOTHING;
3432 LIBCFS_FREE(net, sizeof(*net));
3435 if (list_empty(&kiblnd_data.kib_devs))
3436 kiblnd_base_shutdown();
3440 kiblnd_base_startup(struct net *ns)
3442 struct kib_sched_info *sched;
3446 LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
3448 if (!try_module_get(THIS_MODULE))
3451 memset(&kiblnd_data, 0, sizeof(kiblnd_data)); /* zero pointers, flags etc */
3453 rwlock_init(&kiblnd_data.kib_global_lock);
3455 INIT_LIST_HEAD(&kiblnd_data.kib_devs);
3456 INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
3458 hash_init(kiblnd_data.kib_peers);
3460 spin_lock_init(&kiblnd_data.kib_connd_lock);
3461 INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
3462 INIT_LIST_HEAD(&kiblnd_data.kib_connd_waits);
3463 INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
3464 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
3465 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
3467 init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
3468 init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
3470 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
3472 if (kiblnd_data.kib_scheds == NULL)
3475 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
3478 spin_lock_init(&sched->ibs_lock);
3479 INIT_LIST_HEAD(&sched->ibs_conns);
3480 init_waitqueue_head(&sched->ibs_waitq);
3482 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
3483 if (*kiblnd_tunables.kib_nscheds > 0) {
3484 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
3486 /* max to half of CPUs, another half is reserved for
3487 * upper layer modules */
3488 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3491 sched->ibs_nthreads_max = nthrs;
3495 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
3497 /* lists/ptrs/locks initialised */
3498 kiblnd_data.kib_init = IBLND_INIT_DATA;
3499 /*****************************************************/
3501 rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
3503 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
3507 if (*kiblnd_tunables.kib_dev_failover != 0)
3508 rc = kiblnd_thread_start(kiblnd_failover_thread, ns,
3512 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
3516 register_netdevice_notifier(&kiblnd_dev_notifier_block);
3517 register_inetaddr_notifier(&kiblnd_inetaddr_notifier_block);
3519 /* flag everything initialised */
3520 kiblnd_data.kib_init = IBLND_INIT_ALL;
3521 /*****************************************************/
3526 kiblnd_base_shutdown();
3531 kiblnd_start_schedulers(struct kib_sched_info *sched)
3537 if (sched->ibs_nthreads == 0) {
3538 if (*kiblnd_tunables.kib_nscheds > 0) {
3539 nthrs = sched->ibs_nthreads_max;
3541 nthrs = cfs_cpt_weight(lnet_cpt_table(),
3543 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3544 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
3547 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
3548 /* increase one thread if there is new interface */
3549 nthrs = (sched->ibs_nthreads < sched->ibs_nthreads_max);
3552 for (i = 0; i < nthrs; i++) {
3553 long id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
3555 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id,
3556 "kiblnd_sd_%02ld_%02ld",
3557 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
3561 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
3562 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
3566 sched->ibs_nthreads += i;
3570 static int kiblnd_dev_start_threads(struct kib_dev *dev, bool newdev, u32 *cpts,
3577 for (i = 0; i < ncpts; i++) {
3578 struct kib_sched_info *sched;
3580 cpt = (cpts == NULL) ? i : cpts[i];
3581 sched = kiblnd_data.kib_scheds[cpt];
3583 if (!newdev && sched->ibs_nthreads > 0)
3586 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
3588 CERROR("Failed to start scheduler threads for %s\n",
3597 kiblnd_startup(struct lnet_ni *ni)
3599 char *ifname = NULL;
3600 struct lnet_inetdev *ifaces = NULL;
3601 struct kib_dev *ibdev = NULL;
3602 struct kib_net *net = NULL;
3603 unsigned long flags;
3607 struct net_device *netdev;
3609 LASSERT(ni->ni_net->net_lnd == &the_o2iblnd);
3611 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
3612 rc = kiblnd_base_startup(ni->ni_net_ns);
3617 LIBCFS_ALLOC(net, sizeof(*net));
3625 net->ibn_incarnation = ktime_get_real_ns() / NSEC_PER_USEC;
3627 kiblnd_tunables_setup(ni);
3629 /* Multi-Rail wants each secondary
3630 * IP to be treated as an unique 'struct ni' interface.
3632 if (ni->ni_interface != NULL) {
3633 /* Use the IPoIB interface specified in 'networks=' */
3634 ifname = ni->ni_interface;
3636 ifname = *kiblnd_tunables.kib_default_ipif;
3637 rc = libcfs_strnid(&ni->ni_nid, ifname);
3638 if (rc < 0 || ni->ni_nid.nid_type != O2IBLND)
3639 memset(&ni->ni_nid, 0, sizeof(ni->ni_nid));
3642 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
3643 CERROR("IPoIB interface name too long: %s\n", ifname);
3648 rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns, false);
3652 i = lnet_inet_select(ni, ifaces, rc);
3656 if (nid_addr_is_set(&ni->ni_nid)) {
3657 strscpy(ifname, ifaces[i].li_name, sizeof(ifname));
3658 } else if (strcmp(ifname, ifaces[i].li_name) != 0) {
3659 CERROR("ko2iblnd: No matching interfaces\n");
3664 ibdev = kiblnd_dev_search(ifname);
3665 newdev = ibdev == NULL;
3666 /* hmm...create kib_dev even for alias */
3667 if (ibdev == NULL || strcmp(&ibdev->ibd_ifname[0], ifname) != 0) {
3668 LIBCFS_ALLOC(ibdev, sizeof(*ibdev));
3674 ibdev->ibd_ifip = ntohl(ifaces[i].li_ipaddr);
3675 strlcpy(ibdev->ibd_ifname, ifaces[i].li_name,
3676 sizeof(ibdev->ibd_ifname));
3677 ibdev->ibd_can_failover = ifaces[i].li_iff_master;
3679 INIT_LIST_HEAD(&ibdev->ibd_nets);
3680 INIT_LIST_HEAD(&ibdev->ibd_list); /* not yet in kib_devs */
3681 INIT_LIST_HEAD(&ibdev->ibd_fail_list);
3683 /* initialize the device */
3684 rc = kiblnd_dev_failover(ibdev, ni->ni_net_ns);
3686 CERROR("ko2iblnd: Can't initialize device: rc = %d\n",
3691 list_add_tail(&ibdev->ibd_list, &kiblnd_data.kib_devs);
3694 net->ibn_dev = ibdev;
3695 ni->ni_nid.nid_addr[0] = cpu_to_be32(ibdev->ibd_ifip);
3696 if (!ni->ni_interface) {
3697 rc = lnet_ni_add_interface(ni, ifaces[i].li_name);
3699 CWARN("ko2iblnd failed to allocate ni_interface\n");
3701 ni->ni_dev_cpt = ifaces[i].li_cpt;
3703 rc = kiblnd_dev_start_threads(ibdev, newdev, ni->ni_cpts, ni->ni_ncpts);
3707 rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
3709 CERROR("Failed to initialize NI pools: %d\n", rc);
3713 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3715 list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
3716 /* for health check */
3717 if (ibdev->ibd_hdev->ibh_state == IBLND_DEV_PORT_DOWN)
3718 kiblnd_set_ni_fatal_on(ibdev->ibd_hdev, 1);
3721 netdev = dev_get_by_name_rcu(ni->ni_net_ns, net->ibn_dev->ibd_ifname);
3722 if (((netdev->reg_state == NETREG_UNREGISTERING) ||
3723 (netdev->operstate != IF_OPER_UP)) ||
3724 (lnet_get_link_status(netdev) == 0)) {
3725 kiblnd_set_ni_fatal_on(ibdev->ibd_hdev, 1);
3729 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3731 net->ibn_init = IBLND_INIT_ALL;
3736 if (net != NULL && net->ibn_dev == NULL && ibdev != NULL)
3737 kiblnd_destroy_dev(ibdev);
3740 kiblnd_shutdown(ni);
3742 CDEBUG(D_NET, "Configuration of device %s failed: rc = %d\n",
3743 ifname ? ifname : "", rc);
3748 static const struct lnet_lnd the_o2iblnd = {
3749 .lnd_type = O2IBLND,
3750 .lnd_startup = kiblnd_startup,
3751 .lnd_shutdown = kiblnd_shutdown,
3752 .lnd_ctl = kiblnd_ctl,
3753 .lnd_send = kiblnd_send,
3754 .lnd_recv = kiblnd_recv,
3755 .lnd_get_dev_prio = kiblnd_get_dev_prio,
3756 .lnd_nl_get = kiblnd_nl_get,
3757 .lnd_nl_set = kiblnd_nl_set,
3758 .lnd_keys = &kiblnd_tunables_keys,
3761 static void ko2inlnd_assert_wire_constants(void)
3763 BUILD_BUG_ON(IBLND_MSG_MAGIC != 0x0be91b91);
3764 BUILD_BUG_ON(IBLND_MSG_VERSION_1 != 0x11);
3765 BUILD_BUG_ON(IBLND_MSG_VERSION_2 != 0x12);
3766 BUILD_BUG_ON(IBLND_MSG_VERSION != IBLND_MSG_VERSION_2);
3768 BUILD_BUG_ON(IBLND_MSG_CONNREQ != 0xc0);
3769 BUILD_BUG_ON(IBLND_MSG_CONNACK != 0xc1);
3770 BUILD_BUG_ON(IBLND_MSG_NOOP != 0xd0);
3771 BUILD_BUG_ON(IBLND_MSG_IMMEDIATE != 0xd1);
3772 BUILD_BUG_ON(IBLND_MSG_PUT_REQ != 0xd2);
3773 BUILD_BUG_ON(IBLND_MSG_PUT_NAK != 0xd3);
3774 BUILD_BUG_ON(IBLND_MSG_PUT_ACK != 0xd4);
3775 BUILD_BUG_ON(IBLND_MSG_PUT_DONE != 0xd5);
3776 BUILD_BUG_ON(IBLND_MSG_GET_REQ != 0xd6);
3777 BUILD_BUG_ON(IBLND_MSG_GET_DONE != 0xd7);
3779 BUILD_BUG_ON(IBLND_REJECT_CONN_RACE != 1);
3780 BUILD_BUG_ON(IBLND_REJECT_NO_RESOURCES != 2);
3781 BUILD_BUG_ON(IBLND_REJECT_FATAL != 3);
3782 BUILD_BUG_ON(IBLND_REJECT_CONN_UNCOMPAT != 4);
3783 BUILD_BUG_ON(IBLND_REJECT_CONN_STALE != 5);
3784 BUILD_BUG_ON(IBLND_REJECT_RDMA_FRAGS != 6);
3785 BUILD_BUG_ON(IBLND_REJECT_MSG_QUEUE_SIZE != 7);
3786 BUILD_BUG_ON(IBLND_REJECT_INVALID_SRV_ID != 8);
3788 BUILD_BUG_ON((int)sizeof(struct kib_connparams) != 8);
3789 BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_queue_depth) != 0);
3790 BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_queue_depth) != 2);
3791 BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_max_frags) != 2);
3792 BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_max_frags) != 2);
3793 BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_max_msg_size) != 4);
3794 BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_max_msg_size) != 4);
3796 BUILD_BUG_ON((int)sizeof(struct kib_immediate_msg) != 72);
3797 BUILD_BUG_ON((int)offsetof(struct kib_immediate_msg, ibim_hdr) != 0);
3798 BUILD_BUG_ON((int)sizeof(((struct kib_immediate_msg *)0)->ibim_hdr) != 72);
3799 BUILD_BUG_ON((int)offsetof(struct kib_immediate_msg, ibim_payload) != 72);
3800 BUILD_BUG_ON((int)sizeof(((struct kib_immediate_msg *)0)->ibim_payload) != 0);
3802 BUILD_BUG_ON((int)sizeof(struct kib_rdma_frag) != 12);
3803 BUILD_BUG_ON((int)offsetof(struct kib_rdma_frag, rf_nob) != 0);
3804 BUILD_BUG_ON((int)sizeof(((struct kib_rdma_frag *)0)->rf_nob) != 4);
3805 BUILD_BUG_ON((int)offsetof(struct kib_rdma_frag, rf_addr) != 4);
3806 BUILD_BUG_ON((int)sizeof(((struct kib_rdma_frag *)0)->rf_addr) != 8);
3808 BUILD_BUG_ON((int)sizeof(struct kib_rdma_desc) != 8);
3809 BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_key) != 0);
3810 BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_key) != 4);
3811 BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_nfrags) != 4);
3812 BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_nfrags) != 4);
3813 BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_frags) != 8);
3814 BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_frags) != 0);
3816 BUILD_BUG_ON((int)sizeof(struct kib_putreq_msg) != 80);
3817 BUILD_BUG_ON((int)offsetof(struct kib_putreq_msg, ibprm_hdr) != 0);
3818 BUILD_BUG_ON((int)sizeof(((struct kib_putreq_msg *)0)->ibprm_hdr) != 72);
3819 BUILD_BUG_ON((int)offsetof(struct kib_putreq_msg, ibprm_cookie) != 72);
3820 BUILD_BUG_ON((int)sizeof(((struct kib_putreq_msg *)0)->ibprm_cookie) != 8);
3822 BUILD_BUG_ON((int)sizeof(struct kib_putack_msg) != 24);
3823 BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_src_cookie) != 0);
3824 BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_src_cookie) != 8);
3825 BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_dst_cookie) != 8);
3826 BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_dst_cookie) != 8);
3827 BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_rd) != 16);
3828 BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_rd) != 8);
3830 BUILD_BUG_ON((int)sizeof(struct kib_get_msg) != 88);
3831 BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_hdr) != 0);
3832 BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_hdr) != 72);
3833 BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_cookie) != 72);
3834 BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_cookie) != 8);
3835 BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_rd) != 80);
3836 BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_rd) != 8);
3838 BUILD_BUG_ON((int)sizeof(struct kib_completion_msg) != 12);
3839 BUILD_BUG_ON((int)offsetof(struct kib_completion_msg, ibcm_cookie) != 0);
3840 BUILD_BUG_ON((int)sizeof(((struct kib_completion_msg *)0)->ibcm_cookie) != 8);
3841 BUILD_BUG_ON((int)offsetof(struct kib_completion_msg, ibcm_status) != 8);
3842 BUILD_BUG_ON((int)sizeof(((struct kib_completion_msg *)0)->ibcm_status) != 4);
3844 /* Checks for struct kib_msg */
3845 //BUILD_BUG_ON((int)sizeof(struct kib_msg) != 12);
3846 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_magic) != 0);
3847 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_magic) != 4);
3848 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_version) != 4);
3849 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_version) != 2);
3850 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_type) != 6);
3851 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_type) != 1);
3852 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_credits) != 7);
3853 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_credits) != 1);
3854 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_nob) != 8);
3855 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_nob) != 4);
3856 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_cksum) != 12);
3857 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_cksum) != 4);
3858 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_srcnid) != 16);
3859 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_srcnid) != 8);
3860 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_srcstamp) != 24);
3861 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_srcstamp) != 8);
3862 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_dstnid) != 32);
3863 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_dstnid) != 8);
3864 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_dststamp) != 40);
3865 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_dststamp) != 8);
3868 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_queue_depth) != 48);
3869 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_queue_depth) != 2);
3870 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_max_frags) != 50);
3871 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_max_frags) != 2);
3872 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_max_msg_size) != 52);
3873 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_max_msg_size) != 4);
3875 /* Immediate message */
3876 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.immediate.ibim_hdr) != 48);
3877 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.immediate.ibim_hdr) != 72);
3878 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.immediate.ibim_payload) != 120);
3879 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.immediate.ibim_payload) != 0);
3881 /* PUT req message */
3882 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putreq.ibprm_hdr) != 48);
3883 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putreq.ibprm_hdr) != 72);
3884 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putreq.ibprm_cookie) != 120);
3885 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putreq.ibprm_cookie) != 8);
3888 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_src_cookie) != 48);
3889 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_src_cookie) != 8);
3890 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_dst_cookie) != 56);
3891 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_dst_cookie) != 8);
3892 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_rd) != 64);
3893 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_rd) != 8);
3896 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_hdr) != 48);
3897 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_hdr) != 72);
3898 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_cookie) != 120);
3899 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_cookie) != 8);
3900 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_rd) != 128);
3901 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_rd) != 8);
3903 /* Completion message */
3904 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.completion.ibcm_cookie) != 48);
3905 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.completion.ibcm_cookie) != 8);
3906 BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.completion.ibcm_status) != 56);
3907 BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.completion.ibcm_status) != 4);
3910 BUILD_BUG_ON(sizeof(struct kib_msg) > IBLND_MSG_SIZE);
3911 BUILD_BUG_ON(offsetof(struct kib_msg,
3912 ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3914 BUILD_BUG_ON(offsetof(struct kib_msg,
3915 ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3919 static void __exit ko2iblnd_exit(void)
3921 lnet_unregister_lnd(&the_o2iblnd);
3924 static int __init ko2iblnd_init(void)
3928 ko2inlnd_assert_wire_constants();
3930 rc = kiblnd_tunables_init();
3934 lnet_register_lnd(&the_o2iblnd);
3939 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3940 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
3941 MODULE_VERSION("2.8.0");
3942 MODULE_LICENSE("GPL");
3944 module_init(ko2iblnd_init);
3945 module_exit(ko2iblnd_exit);