4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd.c
34 * Author: Eric Barton <eric@bartonsoftware.com>
38 #include <linux/inetdevice.h>
42 static const struct lnet_lnd the_o2iblnd;
44 struct kib_data kiblnd_data;
47 kiblnd_cksum (void *ptr, int nob)
53 sum = ((sum << 1) | (sum >> 31)) + *c++;
55 /* ensure I don't return 0 (== no checksum) */
56 return (sum == 0) ? 1 : sum;
60 kiblnd_msgtype2str(int type)
63 case IBLND_MSG_CONNREQ:
66 case IBLND_MSG_CONNACK:
72 case IBLND_MSG_IMMEDIATE:
75 case IBLND_MSG_PUT_REQ:
78 case IBLND_MSG_PUT_NAK:
81 case IBLND_MSG_PUT_ACK:
84 case IBLND_MSG_PUT_DONE:
87 case IBLND_MSG_GET_REQ:
90 case IBLND_MSG_GET_DONE:
99 kiblnd_msgtype2size(int type)
101 const int hdr_size = offsetof(struct kib_msg, ibm_u);
104 case IBLND_MSG_CONNREQ:
105 case IBLND_MSG_CONNACK:
106 return hdr_size + sizeof(struct kib_connparams);
111 case IBLND_MSG_IMMEDIATE:
112 return offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[0]);
114 case IBLND_MSG_PUT_REQ:
115 return hdr_size + sizeof(struct kib_putreq_msg);
117 case IBLND_MSG_PUT_ACK:
118 return hdr_size + sizeof(struct kib_putack_msg);
120 case IBLND_MSG_GET_REQ:
121 return hdr_size + sizeof(struct kib_get_msg);
123 case IBLND_MSG_PUT_NAK:
124 case IBLND_MSG_PUT_DONE:
125 case IBLND_MSG_GET_DONE:
126 return hdr_size + sizeof(struct kib_completion_msg);
132 static int kiblnd_unpack_rd(struct kib_msg *msg, int flip)
134 struct kib_rdma_desc *rd;
139 LASSERT (msg->ibm_type == IBLND_MSG_GET_REQ ||
140 msg->ibm_type == IBLND_MSG_PUT_ACK);
142 rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
143 &msg->ibm_u.get.ibgm_rd :
144 &msg->ibm_u.putack.ibpam_rd;
147 __swab32s(&rd->rd_key);
148 __swab32s(&rd->rd_nfrags);
153 if (n <= 0 || n > IBLND_MAX_RDMA_FRAGS) {
154 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
155 n, IBLND_MAX_RDMA_FRAGS);
159 nob = offsetof(struct kib_msg, ibm_u) +
160 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
162 if (msg->ibm_nob < nob) {
163 CERROR("Short %s: %d(%d)\n",
164 kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
171 for (i = 0; i < n; i++) {
172 __swab32s(&rd->rd_frags[i].rf_nob);
173 __swab64s(&rd->rd_frags[i].rf_addr);
179 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
180 int credits, lnet_nid_t dstnid, __u64 dststamp)
182 struct kib_net *net = ni->ni_data;
184 /* CAVEAT EMPTOR! all message fields not set here should have been
185 * initialised previously. */
186 msg->ibm_magic = IBLND_MSG_MAGIC;
187 msg->ibm_version = version;
189 msg->ibm_credits = credits;
192 msg->ibm_srcnid = ni->ni_nid;
193 msg->ibm_srcstamp = net->ibn_incarnation;
194 msg->ibm_dstnid = dstnid;
195 msg->ibm_dststamp = dststamp;
197 if (*kiblnd_tunables.kib_cksum) {
198 /* NB ibm_cksum zero while computing cksum */
199 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
203 int kiblnd_unpack_msg(struct kib_msg *msg, int nob)
205 const int hdr_size = offsetof(struct kib_msg, ibm_u);
211 /* 6 bytes are enough to have received magic + version */
213 CERROR("Short message: %d\n", nob);
217 if (msg->ibm_magic == IBLND_MSG_MAGIC) {
219 } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
222 CERROR("Bad magic: %08x\n", msg->ibm_magic);
226 version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
227 if (version != IBLND_MSG_VERSION &&
228 version != IBLND_MSG_VERSION_1) {
229 CERROR("Bad version: %x\n", version);
233 if (nob < hdr_size) {
234 CERROR("Short message: %d\n", nob);
238 msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
240 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
244 /* checksum must be computed with ibm_cksum zero and BEFORE anything
246 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
248 if (msg_cksum != 0 &&
249 msg_cksum != kiblnd_cksum(msg, msg_nob)) {
250 CERROR("Bad checksum\n");
254 msg->ibm_cksum = msg_cksum;
257 /* leave magic unflipped as a clue to peer_ni endianness */
258 msg->ibm_version = version;
259 BUILD_BUG_ON(sizeof(msg->ibm_type) != 1);
260 BUILD_BUG_ON(sizeof(msg->ibm_credits) != 1);
261 msg->ibm_nob = msg_nob;
262 __swab64s(&msg->ibm_srcnid);
263 __swab64s(&msg->ibm_srcstamp);
264 __swab64s(&msg->ibm_dstnid);
265 __swab64s(&msg->ibm_dststamp);
268 if (msg->ibm_srcnid == LNET_NID_ANY) {
269 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg->ibm_srcnid));
273 if (msg_nob < kiblnd_msgtype2size(msg->ibm_type)) {
274 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg->ibm_type),
275 msg_nob, kiblnd_msgtype2size(msg->ibm_type));
279 switch (msg->ibm_type) {
281 CERROR("Unknown message type %x\n", msg->ibm_type);
285 case IBLND_MSG_IMMEDIATE:
286 case IBLND_MSG_PUT_REQ:
289 case IBLND_MSG_PUT_ACK:
290 case IBLND_MSG_GET_REQ:
291 if (kiblnd_unpack_rd(msg, flip))
295 case IBLND_MSG_PUT_NAK:
296 case IBLND_MSG_PUT_DONE:
297 case IBLND_MSG_GET_DONE:
299 __swab32s(&msg->ibm_u.completion.ibcm_status);
302 case IBLND_MSG_CONNREQ:
303 case IBLND_MSG_CONNACK:
305 __swab16s(&msg->ibm_u.connparams.ibcp_queue_depth);
306 __swab16s(&msg->ibm_u.connparams.ibcp_max_frags);
307 __swab32s(&msg->ibm_u.connparams.ibcp_max_msg_size);
315 kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
318 struct kib_peer_ni *peer_ni;
319 struct kib_net *net = ni->ni_data;
320 int cpt = lnet_cpt_of_nid(nid, ni);
323 LASSERT(net != NULL);
324 LASSERT(nid != LNET_NID_ANY);
326 LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
327 if (peer_ni == NULL) {
328 CERROR("Cannot allocate peer_ni\n");
332 peer_ni->ibp_ni = ni;
333 peer_ni->ibp_nid = nid;
334 peer_ni->ibp_error = 0;
335 peer_ni->ibp_last_alive = 0;
336 peer_ni->ibp_max_frags = IBLND_MAX_RDMA_FRAGS;
337 peer_ni->ibp_queue_depth = ni->ni_net->net_tunables.lct_peer_tx_credits;
338 atomic_set(&peer_ni->ibp_refcount, 1); /* 1 ref for caller */
340 INIT_LIST_HEAD(&peer_ni->ibp_list); /* not in the peer_ni table yet */
341 INIT_LIST_HEAD(&peer_ni->ibp_conns);
342 INIT_LIST_HEAD(&peer_ni->ibp_tx_queue);
344 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
346 /* always called with a ref on ni, which prevents ni being shutdown */
347 LASSERT(net->ibn_shutdown == 0);
349 /* npeers only grows with the global lock held */
350 atomic_inc(&net->ibn_npeers);
352 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
359 kiblnd_destroy_peer(struct kib_peer_ni *peer_ni)
361 struct kib_net *net = peer_ni->ibp_ni->ni_data;
363 LASSERT(net != NULL);
364 LASSERT (atomic_read(&peer_ni->ibp_refcount) == 0);
365 LASSERT(!kiblnd_peer_active(peer_ni));
366 LASSERT(kiblnd_peer_idle(peer_ni));
367 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
369 LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
371 /* NB a peer_ni's connections keep a reference on their peer_ni until
372 * they are destroyed, so we can be assured that _all_ state to do
373 * with this peer_ni has been cleaned up when its refcount drops to
375 atomic_dec(&net->ibn_npeers);
379 kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid)
381 /* the caller is responsible for accounting the additional reference
382 * that this creates */
383 struct list_head *peer_list = kiblnd_nid2peerlist(nid);
384 struct list_head *tmp;
385 struct kib_peer_ni *peer_ni;
387 list_for_each(tmp, peer_list) {
389 peer_ni = list_entry(tmp, struct kib_peer_ni, ibp_list);
390 LASSERT(!kiblnd_peer_idle(peer_ni));
393 * Match a peer if its NID and the NID of the local NI it
394 * communicates over are the same. Otherwise don't match
395 * the peer, which will result in a new lnd peer being
398 if (peer_ni->ibp_nid != nid ||
399 peer_ni->ibp_ni->ni_nid != ni->ni_nid)
402 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d) version: %x\n",
403 peer_ni, libcfs_nid2str(nid),
404 atomic_read(&peer_ni->ibp_refcount),
405 peer_ni->ibp_version);
412 kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni)
414 LASSERT(list_empty(&peer_ni->ibp_conns));
416 LASSERT (kiblnd_peer_active(peer_ni));
417 list_del_init(&peer_ni->ibp_list);
418 /* lose peerlist's ref */
419 kiblnd_peer_decref(peer_ni);
423 kiblnd_get_peer_info(struct lnet_ni *ni, int index,
424 lnet_nid_t *nidp, int *count)
426 struct kib_peer_ni *peer_ni;
427 struct list_head *ptmp;
431 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
433 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
435 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
437 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
438 LASSERT(!kiblnd_peer_idle(peer_ni));
440 if (peer_ni->ibp_ni != ni)
446 *nidp = peer_ni->ibp_nid;
447 *count = atomic_read(&peer_ni->ibp_refcount);
449 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
455 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
460 kiblnd_del_peer_locked(struct kib_peer_ni *peer_ni)
462 struct list_head *ctmp;
463 struct list_head *cnxt;
464 struct kib_conn *conn;
466 if (list_empty(&peer_ni->ibp_conns)) {
467 kiblnd_unlink_peer_locked(peer_ni);
469 list_for_each_safe(ctmp, cnxt, &peer_ni->ibp_conns) {
470 conn = list_entry(ctmp, struct kib_conn, ibc_list);
472 kiblnd_close_conn_locked(conn, 0);
474 /* NB closing peer_ni's last conn unlinked it. */
476 /* NB peer_ni now unlinked; might even be freed if the peer_ni table had the
481 kiblnd_del_peer(struct lnet_ni *ni, lnet_nid_t nid)
484 struct list_head *ptmp;
485 struct list_head *pnxt;
486 struct kib_peer_ni *peer_ni;
493 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
495 if (nid != LNET_NID_ANY) {
496 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
499 hi = kiblnd_data.kib_peer_hash_size - 1;
502 for (i = lo; i <= hi; i++) {
503 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
504 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
505 LASSERT(!kiblnd_peer_idle(peer_ni));
507 if (peer_ni->ibp_ni != ni)
510 if (!(nid == LNET_NID_ANY || peer_ni->ibp_nid == nid))
513 if (!list_empty(&peer_ni->ibp_tx_queue)) {
514 LASSERT(list_empty(&peer_ni->ibp_conns));
516 list_splice_init(&peer_ni->ibp_tx_queue,
520 kiblnd_del_peer_locked(peer_ni);
521 rc = 0; /* matched something */
525 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
527 kiblnd_txlist_done(&zombies, -EIO, LNET_MSG_STATUS_LOCAL_ERROR);
532 static struct kib_conn *
533 kiblnd_get_conn_by_idx(struct lnet_ni *ni, int index)
535 struct kib_peer_ni *peer_ni;
536 struct list_head *ptmp;
537 struct kib_conn *conn;
538 struct list_head *ctmp;
542 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
544 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
545 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
547 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
548 LASSERT(!kiblnd_peer_idle(peer_ni));
550 if (peer_ni->ibp_ni != ni)
553 list_for_each(ctmp, &peer_ni->ibp_conns) {
557 conn = list_entry(ctmp, struct kib_conn, ibc_list);
558 kiblnd_conn_addref(conn);
559 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
566 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
571 kiblnd_debug_rx(struct kib_rx *rx)
573 CDEBUG(D_CONSOLE, " %p msg_type %x cred %d\n",
574 rx, rx->rx_msg->ibm_type,
575 rx->rx_msg->ibm_credits);
579 kiblnd_debug_tx(struct kib_tx *tx)
581 CDEBUG(D_CONSOLE, " %p snd %d q %d w %d rc %d dl %lld "
582 "cookie %#llx msg %s%s type %x cred %d\n",
583 tx, tx->tx_sending, tx->tx_queued, tx->tx_waiting,
584 tx->tx_status, ktime_to_ns(tx->tx_deadline), tx->tx_cookie,
585 tx->tx_lntmsg[0] == NULL ? "-" : "!",
586 tx->tx_lntmsg[1] == NULL ? "-" : "!",
587 tx->tx_msg->ibm_type, tx->tx_msg->ibm_credits);
591 kiblnd_debug_conn(struct kib_conn *conn)
593 struct list_head *tmp;
596 spin_lock(&conn->ibc_lock);
598 CDEBUG(D_CONSOLE, "conn[%d] %p [version %x] -> %s:\n",
599 atomic_read(&conn->ibc_refcount), conn,
600 conn->ibc_version, libcfs_nid2str(conn->ibc_peer->ibp_nid));
601 CDEBUG(D_CONSOLE, " state %d nposted %d/%d cred %d o_cred %d "
602 " r_cred %d\n", conn->ibc_state, conn->ibc_noops_posted,
603 conn->ibc_nsends_posted, conn->ibc_credits,
604 conn->ibc_outstanding_credits, conn->ibc_reserved_credits);
605 CDEBUG(D_CONSOLE, " comms_err %d\n", conn->ibc_comms_error);
607 CDEBUG(D_CONSOLE, " early_rxs:\n");
608 list_for_each(tmp, &conn->ibc_early_rxs)
609 kiblnd_debug_rx(list_entry(tmp, struct kib_rx, rx_list));
611 CDEBUG(D_CONSOLE, " tx_noops:\n");
612 list_for_each(tmp, &conn->ibc_tx_noops)
613 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
615 CDEBUG(D_CONSOLE, " tx_queue_nocred:\n");
616 list_for_each(tmp, &conn->ibc_tx_queue_nocred)
617 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
619 CDEBUG(D_CONSOLE, " tx_queue_rsrvd:\n");
620 list_for_each(tmp, &conn->ibc_tx_queue_rsrvd)
621 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
623 CDEBUG(D_CONSOLE, " tx_queue:\n");
624 list_for_each(tmp, &conn->ibc_tx_queue)
625 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
627 CDEBUG(D_CONSOLE, " active_txs:\n");
628 list_for_each(tmp, &conn->ibc_active_txs)
629 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
631 CDEBUG(D_CONSOLE, " rxs:\n");
632 for (i = 0; i < IBLND_RX_MSGS(conn); i++)
633 kiblnd_debug_rx(&conn->ibc_rxs[i]);
635 spin_unlock(&conn->ibc_lock);
639 kiblnd_translate_mtu(int value)
660 kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
664 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
665 if (cmid->route.path_rec == NULL)
668 mtu = kiblnd_translate_mtu(*kiblnd_tunables.kib_ib_mtu);
671 cmid->route.path_rec->mtu = mtu;
675 kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
683 vectors = conn->ibc_cmid->device->num_comp_vectors;
687 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
689 /* hash NID to CPU id in this partition... */
690 ibp_nid = conn->ibc_peer->ibp_nid;
691 off = do_div(ibp_nid, cpumask_weight(*mask));
692 for_each_cpu(i, *mask) {
702 * Get the scheduler bound to this CPT. If the scheduler has no
703 * threads, which means that the CPT has no CPUs, then grab the
704 * next scheduler that we can use.
706 * This case would be triggered if a NUMA node is configured with
707 * no associated CPUs.
709 static struct kib_sched_info *
710 kiblnd_get_scheduler(int cpt)
712 struct kib_sched_info *sched;
715 sched = kiblnd_data.kib_scheds[cpt];
717 if (sched->ibs_nthreads > 0)
720 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
721 if (sched->ibs_nthreads > 0) {
722 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
723 cpt, sched->ibs_cpt);
731 static unsigned int kiblnd_send_wrs(struct kib_conn *conn)
734 * One WR for the LNet message
735 * And ibc_max_frags for the transfer WRs
738 int multiplier = 1 + conn->ibc_max_frags;
739 enum kib_dev_caps dev_caps = conn->ibc_hdev->ibh_dev->ibd_dev_caps;
741 /* FastReg needs two extra WRs for map and invalidate */
742 if (dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)
745 /* account for a maximum of ibc_queue_depth in-flight transfers */
746 ret = multiplier * conn->ibc_queue_depth;
748 if (ret > conn->ibc_hdev->ibh_max_qp_wr) {
749 CDEBUG(D_NET, "peer_credits %u will result in send work "
750 "request size %d larger than maximum %d device "
751 "can handle\n", conn->ibc_queue_depth, ret,
752 conn->ibc_hdev->ibh_max_qp_wr);
753 conn->ibc_queue_depth =
754 conn->ibc_hdev->ibh_max_qp_wr / multiplier;
757 /* don't go beyond the maximum the device can handle */
758 return min(ret, conn->ibc_hdev->ibh_max_qp_wr);
762 kiblnd_create_conn(struct kib_peer_ni *peer_ni, struct rdma_cm_id *cmid,
763 int state, int version)
766 * If the new conn is created successfully it takes over the caller's
767 * ref on 'peer_ni'. It also "owns" 'cmid' and destroys it when it itself
768 * is destroyed. On failure, the caller's ref on 'peer_ni' remains and
769 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
770 * to destroy 'cmid' here since I'm called from the CM which still has
771 * its ref on 'cmid'). */
772 rwlock_t *glock = &kiblnd_data.kib_global_lock;
773 struct kib_net *net = peer_ni->ibp_ni->ni_data;
775 struct ib_qp_init_attr *init_qp_attr;
776 struct kib_sched_info *sched;
777 #ifdef HAVE_IB_CQ_INIT_ATTR
778 struct ib_cq_init_attr cq_attr = {};
780 struct kib_conn *conn;
787 LASSERT(net != NULL);
788 LASSERT(!in_interrupt());
792 cpt = lnet_cpt_of_nid(peer_ni->ibp_nid, peer_ni->ibp_ni);
793 sched = kiblnd_get_scheduler(cpt);
796 CERROR("no schedulers available. node is unhealthy\n");
801 * The cpt might have changed if we ended up selecting a non cpt
802 * native scheduler. So use the scheduler's cpt instead.
804 cpt = sched->ibs_cpt;
806 LIBCFS_CPT_ALLOC(init_qp_attr, lnet_cpt_table(), cpt,
807 sizeof(*init_qp_attr));
808 if (init_qp_attr == NULL) {
809 CERROR("Can't allocate qp_attr for %s\n",
810 libcfs_nid2str(peer_ni->ibp_nid));
814 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
816 CERROR("Can't allocate connection for %s\n",
817 libcfs_nid2str(peer_ni->ibp_nid));
821 conn->ibc_state = IBLND_CONN_INIT;
822 conn->ibc_version = version;
823 conn->ibc_peer = peer_ni; /* I take the caller's ref */
824 cmid->context = conn; /* for future CM callbacks */
825 conn->ibc_cmid = cmid;
826 conn->ibc_max_frags = peer_ni->ibp_max_frags;
827 conn->ibc_queue_depth = peer_ni->ibp_queue_depth;
828 conn->ibc_rxs = NULL;
829 conn->ibc_rx_pages = NULL;
831 INIT_LIST_HEAD(&conn->ibc_early_rxs);
832 INIT_LIST_HEAD(&conn->ibc_tx_noops);
833 INIT_LIST_HEAD(&conn->ibc_tx_queue);
834 INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
835 INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
836 INIT_LIST_HEAD(&conn->ibc_active_txs);
837 INIT_LIST_HEAD(&conn->ibc_zombie_txs);
838 spin_lock_init(&conn->ibc_lock);
840 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
841 sizeof(*conn->ibc_connvars));
842 if (conn->ibc_connvars == NULL) {
843 CERROR("Can't allocate in-progress connection state\n");
847 write_lock_irqsave(glock, flags);
848 if (dev->ibd_failover) {
849 write_unlock_irqrestore(glock, flags);
850 CERROR("%s: failover in progress\n", dev->ibd_ifname);
854 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
855 /* wakeup failover thread and teardown connection */
856 if (kiblnd_dev_can_failover(dev)) {
857 list_add_tail(&dev->ibd_fail_list,
858 &kiblnd_data.kib_failed_devs);
859 wake_up(&kiblnd_data.kib_failover_waitq);
862 write_unlock_irqrestore(glock, flags);
863 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
864 cmid->device->name, dev->ibd_ifname);
868 kiblnd_hdev_addref_locked(dev->ibd_hdev);
869 conn->ibc_hdev = dev->ibd_hdev;
871 kiblnd_setup_mtu_locked(cmid);
873 write_unlock_irqrestore(glock, flags);
875 #ifdef HAVE_IB_CQ_INIT_ATTR
876 cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
877 cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
878 cq = ib_create_cq(cmid->device,
879 kiblnd_cq_completion, kiblnd_cq_event, conn,
882 cq = ib_create_cq(cmid->device,
883 kiblnd_cq_completion, kiblnd_cq_event, conn,
884 IBLND_CQ_ENTRIES(conn),
885 kiblnd_get_completion_vector(conn, cpt));
889 * on MLX-5 (possibly MLX-4 as well) this error could be
890 * hit if the concurrent_sends and/or peer_tx_credits is set
891 * too high. Or due to an MLX-5 bug which tries to
892 * allocate 256kb via kmalloc for WR cookie array
894 CERROR("Failed to create CQ with %d CQEs: %ld\n",
895 IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
901 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
903 CERROR("Can't request completion notification: %d\n", rc);
907 init_qp_attr->event_handler = kiblnd_qp_event;
908 init_qp_attr->qp_context = conn;
909 init_qp_attr->cap.max_send_sge = *kiblnd_tunables.kib_wrq_sge;
910 init_qp_attr->cap.max_recv_sge = 1;
911 init_qp_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
912 init_qp_attr->qp_type = IB_QPT_RC;
913 init_qp_attr->send_cq = cq;
914 init_qp_attr->recv_cq = cq;
916 * kiblnd_send_wrs() can change the connection's queue depth if
917 * the maximum work requests for the device is maxed out
919 init_qp_attr->cap.max_send_wr = kiblnd_send_wrs(conn);
920 init_qp_attr->cap.max_recv_wr = IBLND_RECV_WRS(conn);
922 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
924 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d, "
925 "send_sge: %d, recv_sge: %d\n",
926 rc, init_qp_attr->cap.max_send_wr,
927 init_qp_attr->cap.max_recv_wr,
928 init_qp_attr->cap.max_send_sge,
929 init_qp_attr->cap.max_recv_sge);
933 conn->ibc_sched = sched;
935 if (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);
942 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
943 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
944 if (conn->ibc_rxs == NULL) {
945 CERROR("Cannot allocate RX buffers\n");
949 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
950 IBLND_RX_MSG_PAGES(conn));
954 kiblnd_map_rx_descs(conn);
956 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
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(&net->ibn_nconns);
1001 kiblnd_destroy_conn(conn);
1002 LIBCFS_FREE(conn, sizeof(*conn));
1004 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
1010 kiblnd_destroy_conn(struct kib_conn *conn)
1012 struct rdma_cm_id *cmid = conn->ibc_cmid;
1013 struct kib_peer_ni *peer_ni = conn->ibc_peer;
1015 LASSERT (!in_interrupt());
1016 LASSERT (atomic_read(&conn->ibc_refcount) == 0);
1017 LASSERT(list_empty(&conn->ibc_early_rxs));
1018 LASSERT(list_empty(&conn->ibc_tx_noops));
1019 LASSERT(list_empty(&conn->ibc_tx_queue));
1020 LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
1021 LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
1022 LASSERT(list_empty(&conn->ibc_active_txs));
1023 LASSERT (conn->ibc_noops_posted == 0);
1024 LASSERT (conn->ibc_nsends_posted == 0);
1026 switch (conn->ibc_state) {
1028 /* conn must be completely disengaged from the network */
1031 case IBLND_CONN_DISCONNECTED:
1032 /* connvars should have been freed already */
1033 LASSERT (conn->ibc_connvars == NULL);
1036 case IBLND_CONN_INIT:
1040 /* conn->ibc_cmid might be destroyed by CM already */
1041 if (cmid != NULL && cmid->qp != NULL)
1042 rdma_destroy_qp(cmid);
1045 ib_destroy_cq(conn->ibc_cq);
1047 kiblnd_txlist_done(&conn->ibc_zombie_txs, -ECONNABORTED,
1048 LNET_MSG_STATUS_OK);
1050 if (conn->ibc_rx_pages != NULL)
1051 kiblnd_unmap_rx_descs(conn);
1053 if (conn->ibc_rxs != NULL) {
1054 LIBCFS_FREE(conn->ibc_rxs,
1055 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
1058 if (conn->ibc_connvars != NULL)
1059 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1061 if (conn->ibc_hdev != NULL)
1062 kiblnd_hdev_decref(conn->ibc_hdev);
1064 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
1065 if (conn->ibc_state != IBLND_CONN_INIT) {
1066 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1068 kiblnd_peer_decref(peer_ni);
1069 rdma_destroy_id(cmid);
1070 atomic_dec(&net->ibn_nconns);
1075 kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why)
1077 struct kib_conn *conn;
1078 struct list_head *ctmp;
1079 struct list_head *cnxt;
1082 list_for_each_safe(ctmp, cnxt, &peer_ni->ibp_conns) {
1083 conn = list_entry(ctmp, struct kib_conn, ibc_list);
1085 CDEBUG(D_NET, "Closing conn -> %s, "
1086 "version: %x, reason: %d\n",
1087 libcfs_nid2str(peer_ni->ibp_nid),
1088 conn->ibc_version, why);
1090 kiblnd_close_conn_locked(conn, why);
1098 kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1099 int version, __u64 incarnation)
1101 struct kib_conn *conn;
1102 struct list_head *ctmp;
1103 struct list_head *cnxt;
1106 list_for_each_safe(ctmp, cnxt, &peer_ni->ibp_conns) {
1107 conn = list_entry(ctmp, struct kib_conn, ibc_list);
1109 if (conn->ibc_version == version &&
1110 conn->ibc_incarnation == incarnation)
1113 CDEBUG(D_NET, "Closing stale conn -> %s version: %x, "
1114 "incarnation:%#llx(%x, %#llx)\n",
1115 libcfs_nid2str(peer_ni->ibp_nid),
1116 conn->ibc_version, conn->ibc_incarnation,
1117 version, incarnation);
1119 kiblnd_close_conn_locked(conn, -ESTALE);
1127 kiblnd_close_matching_conns(struct lnet_ni *ni, lnet_nid_t nid)
1129 struct kib_peer_ni *peer_ni;
1130 struct list_head *ptmp;
1131 struct list_head *pnxt;
1135 unsigned long flags;
1138 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1140 if (nid != LNET_NID_ANY)
1141 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
1144 hi = kiblnd_data.kib_peer_hash_size - 1;
1147 for (i = lo; i <= hi; i++) {
1148 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
1150 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
1151 LASSERT(!kiblnd_peer_idle(peer_ni));
1153 if (peer_ni->ibp_ni != ni)
1156 if (!(nid == LNET_NID_ANY || nid == peer_ni->ibp_nid))
1159 count += kiblnd_close_peer_conns_locked(peer_ni, 0);
1163 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1165 /* wildcards always succeed */
1166 if (nid == LNET_NID_ANY)
1169 return (count == 0) ? -ENOENT : 0;
1173 kiblnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1175 struct libcfs_ioctl_data *data = arg;
1179 case IOC_LIBCFS_GET_PEER: {
1183 rc = kiblnd_get_peer_info(ni, data->ioc_count,
1185 data->ioc_nid = nid;
1186 data->ioc_count = count;
1190 case IOC_LIBCFS_DEL_PEER: {
1191 rc = kiblnd_del_peer(ni, data->ioc_nid);
1194 case IOC_LIBCFS_GET_CONN: {
1195 struct kib_conn *conn;
1198 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1204 LASSERT(conn->ibc_cmid != NULL);
1205 data->ioc_nid = conn->ibc_peer->ibp_nid;
1206 if (conn->ibc_cmid->route.path_rec == NULL)
1207 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1210 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1211 kiblnd_conn_decref(conn);
1214 case IOC_LIBCFS_CLOSE_CONNECTION: {
1215 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1227 kiblnd_query(struct lnet_ni *ni, lnet_nid_t nid, time64_t *when)
1229 time64_t last_alive = 0;
1230 time64_t now = ktime_get_seconds();
1231 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1232 struct kib_peer_ni *peer_ni;
1233 unsigned long flags;
1235 read_lock_irqsave(glock, flags);
1237 peer_ni = kiblnd_find_peer_locked(ni, nid);
1238 if (peer_ni != NULL)
1239 last_alive = peer_ni->ibp_last_alive;
1241 read_unlock_irqrestore(glock, flags);
1243 if (last_alive != 0)
1246 /* peer_ni is not persistent in hash, trigger peer_ni creation
1247 * and connection establishment with a NULL tx */
1248 if (peer_ni == NULL)
1249 kiblnd_launch_tx(ni, NULL, nid);
1251 CDEBUG(D_NET, "peer_ni %s %p, alive %lld secs ago\n",
1252 libcfs_nid2str(nid), peer_ni,
1253 last_alive ? now - last_alive : -1);
1257 kiblnd_free_pages(struct kib_pages *p)
1259 int npages = p->ibp_npages;
1262 for (i = 0; i < npages; i++) {
1263 if (p->ibp_pages[i] != NULL)
1264 __free_page(p->ibp_pages[i]);
1267 LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1271 kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1273 struct kib_pages *p;
1276 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1277 offsetof(struct kib_pages, ibp_pages[npages]));
1279 CERROR("Can't allocate descriptor for %d pages\n", npages);
1283 memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1284 p->ibp_npages = npages;
1286 for (i = 0; i < npages; i++) {
1287 p->ibp_pages[i] = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1289 if (p->ibp_pages[i] == NULL) {
1290 CERROR("Can't allocate page %d of %d\n", i, npages);
1291 kiblnd_free_pages(p);
1301 kiblnd_unmap_rx_descs(struct kib_conn *conn)
1306 LASSERT (conn->ibc_rxs != NULL);
1307 LASSERT (conn->ibc_hdev != NULL);
1309 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1310 rx = &conn->ibc_rxs[i];
1312 LASSERT(rx->rx_nob >= 0); /* not posted */
1314 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1315 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1317 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1320 kiblnd_free_pages(conn->ibc_rx_pages);
1322 conn->ibc_rx_pages = NULL;
1326 kiblnd_map_rx_descs(struct kib_conn *conn)
1334 for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1335 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1336 rx = &conn->ibc_rxs[i];
1339 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1342 kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1343 rx->rx_msg, IBLND_MSG_SIZE,
1345 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1347 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1349 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1350 i, rx->rx_msg, rx->rx_msgaddr,
1351 (__u64)(page_to_phys(pg) + pg_off));
1353 pg_off += IBLND_MSG_SIZE;
1354 LASSERT(pg_off <= PAGE_SIZE);
1356 if (pg_off == PAGE_SIZE) {
1359 LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1365 kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1367 struct kib_hca_dev *hdev = tpo->tpo_hdev;
1371 LASSERT (tpo->tpo_pool.po_allocated == 0);
1376 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1377 tx = &tpo->tpo_tx_descs[i];
1378 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1379 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1381 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1384 kiblnd_hdev_decref(hdev);
1385 tpo->tpo_hdev = NULL;
1388 static struct kib_hca_dev *
1389 kiblnd_current_hdev(struct kib_dev *dev)
1391 struct kib_hca_dev *hdev;
1392 unsigned long flags;
1395 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1396 while (dev->ibd_failover) {
1397 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1399 CDEBUG(D_NET, "%s: Wait for failover\n",
1401 set_current_state(TASK_INTERRUPTIBLE);
1402 schedule_timeout(cfs_time_seconds(1) / 100);
1404 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1407 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1408 hdev = dev->ibd_hdev;
1410 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1416 kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1418 struct kib_pages *txpgs = tpo->tpo_tx_pages;
1419 struct kib_pool *pool = &tpo->tpo_pool;
1420 struct kib_net *net = pool->po_owner->ps_net;
1421 struct kib_dev *dev;
1428 LASSERT (net != NULL);
1432 /* pre-mapped messages are not bigger than 1 page */
1433 BUILD_BUG_ON(IBLND_MSG_SIZE > PAGE_SIZE);
1435 /* No fancy arithmetic when we do the buffer calculations */
1436 BUILD_BUG_ON(PAGE_SIZE % IBLND_MSG_SIZE != 0);
1438 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1440 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1441 page = txpgs->ibp_pages[ipage];
1442 tx = &tpo->tpo_tx_descs[i];
1444 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1447 tx->tx_msgaddr = kiblnd_dma_map_single(tpo->tpo_hdev->ibh_ibdev,
1451 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1453 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1455 list_add(&tx->tx_list, &pool->po_free_list);
1457 page_offset += IBLND_MSG_SIZE;
1458 LASSERT(page_offset <= PAGE_SIZE);
1460 if (page_offset == PAGE_SIZE) {
1463 LASSERT(ipage <= txpgs->ibp_npages);
1469 kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1471 LASSERT(fpo->fpo_map_count == 0);
1473 if (fpo->fpo_is_fmr && fpo->fmr.fpo_fmr_pool) {
1474 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1476 struct kib_fast_reg_descriptor *frd, *tmp;
1479 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1481 list_del(&frd->frd_list);
1482 #ifndef HAVE_IB_MAP_MR_SG
1483 ib_free_fast_reg_page_list(frd->frd_frpl);
1485 ib_dereg_mr(frd->frd_mr);
1486 LIBCFS_FREE(frd, sizeof(*frd));
1489 if (i < fpo->fast_reg.fpo_pool_size)
1490 CERROR("FastReg pool still has %d regions registered\n",
1491 fpo->fast_reg.fpo_pool_size - i);
1495 kiblnd_hdev_decref(fpo->fpo_hdev);
1497 LIBCFS_FREE(fpo, sizeof(*fpo));
1501 kiblnd_destroy_fmr_pool_list(struct list_head *head)
1503 struct kib_fmr_pool *fpo, *tmp;
1505 list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1506 list_del(&fpo->fpo_list);
1507 kiblnd_destroy_fmr_pool(fpo);
1512 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1515 int size = tunables->lnd_fmr_pool_size / ncpts;
1517 return max(IBLND_FMR_POOL, size);
1521 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1524 int size = tunables->lnd_fmr_flush_trigger / ncpts;
1526 return max(IBLND_FMR_POOL_FLUSH, size);
1529 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps,
1530 struct kib_fmr_pool *fpo)
1532 struct ib_fmr_pool_param param = {
1533 .max_pages_per_fmr = LNET_MAX_IOV,
1534 .page_shift = PAGE_SHIFT,
1535 .access = (IB_ACCESS_LOCAL_WRITE |
1536 IB_ACCESS_REMOTE_WRITE),
1537 .pool_size = fps->fps_pool_size,
1538 .dirty_watermark = fps->fps_flush_trigger,
1539 .flush_function = NULL,
1541 .cache = !!fps->fps_cache };
1544 fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1546 if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1547 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1549 CERROR("Failed to create FMR pool: %d\n", rc);
1551 CERROR("FMRs are not supported\n");
1553 fpo->fpo_is_fmr = true;
1558 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps,
1559 struct kib_fmr_pool *fpo,
1560 enum kib_dev_caps dev_caps)
1562 struct kib_fast_reg_descriptor *frd, *tmp;
1565 fpo->fpo_is_fmr = false;
1567 INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1568 fpo->fast_reg.fpo_pool_size = 0;
1569 for (i = 0; i < fps->fps_pool_size; i++) {
1570 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1573 CERROR("Failed to allocate a new fast_reg descriptor\n");
1579 #ifndef HAVE_IB_MAP_MR_SG
1580 frd->frd_frpl = ib_alloc_fast_reg_page_list(fpo->fpo_hdev->ibh_ibdev,
1582 if (IS_ERR(frd->frd_frpl)) {
1583 rc = PTR_ERR(frd->frd_frpl);
1584 CERROR("Failed to allocate ib_fast_reg_page_list: %d\n",
1586 frd->frd_frpl = NULL;
1591 #ifdef HAVE_IB_ALLOC_FAST_REG_MR
1592 frd->frd_mr = ib_alloc_fast_reg_mr(fpo->fpo_hdev->ibh_pd,
1596 * it is expected to get here if this is an MLX-5 card.
1597 * MLX-4 cards will always use FMR and MLX-5 cards will
1598 * always use fast_reg. It turns out that some MLX-5 cards
1599 * (possibly due to older FW versions) do not natively support
1600 * gaps. So we will need to track them here.
1602 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1603 #ifdef IB_MR_TYPE_SG_GAPS
1604 ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1605 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT)) ?
1606 IB_MR_TYPE_SG_GAPS :
1612 if ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1613 (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT))
1614 CWARN("using IB_MR_TYPE_SG_GAPS, expect a performance drop\n");
1616 if (IS_ERR(frd->frd_mr)) {
1617 rc = PTR_ERR(frd->frd_mr);
1618 CERROR("Failed to allocate ib_fast_reg_mr: %d\n", rc);
1623 /* There appears to be a bug in MLX5 code where you must
1624 * invalidate the rkey of a new FastReg pool before first
1625 * using it. Thus, I am marking the FRD invalid here. */
1626 frd->frd_valid = false;
1628 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1629 fpo->fast_reg.fpo_pool_size++;
1636 ib_dereg_mr(frd->frd_mr);
1637 #ifndef HAVE_IB_MAP_MR_SG
1639 ib_free_fast_reg_page_list(frd->frd_frpl);
1641 LIBCFS_FREE(frd, sizeof(*frd));
1644 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1646 list_del(&frd->frd_list);
1647 #ifndef HAVE_IB_MAP_MR_SG
1648 ib_free_fast_reg_page_list(frd->frd_frpl);
1650 ib_dereg_mr(frd->frd_mr);
1651 LIBCFS_FREE(frd, sizeof(*frd));
1657 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1658 struct kib_fmr_pool **pp_fpo)
1660 struct kib_dev *dev = fps->fps_net->ibn_dev;
1661 struct kib_fmr_pool *fpo;
1664 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1668 memset(fpo, 0, sizeof(*fpo));
1670 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1672 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
1673 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1675 rc = kiblnd_alloc_freg_pool(fps, fpo, dev->ibd_dev_caps);
1679 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1680 fpo->fpo_owner = fps;
1686 kiblnd_hdev_decref(fpo->fpo_hdev);
1687 LIBCFS_FREE(fpo, sizeof(*fpo));
1692 kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps, struct list_head *zombies)
1694 if (fps->fps_net == NULL) /* intialized? */
1697 spin_lock(&fps->fps_lock);
1699 while (!list_empty(&fps->fps_pool_list)) {
1700 struct kib_fmr_pool *fpo = list_entry(fps->fps_pool_list.next,
1701 struct kib_fmr_pool,
1704 fpo->fpo_failed = 1;
1705 if (fpo->fpo_map_count == 0)
1706 list_move(&fpo->fpo_list, zombies);
1708 list_move(&fpo->fpo_list, &fps->fps_failed_pool_list);
1711 spin_unlock(&fps->fps_lock);
1715 kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1717 if (fps->fps_net != NULL) { /* initialized? */
1718 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1719 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1724 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1725 struct kib_net *net,
1726 struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1728 struct kib_fmr_pool *fpo;
1731 memset(fps, 0, sizeof(struct kib_fmr_poolset));
1736 fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1737 fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1738 fps->fps_cache = tunables->lnd_fmr_cache;
1740 spin_lock_init(&fps->fps_lock);
1741 INIT_LIST_HEAD(&fps->fps_pool_list);
1742 INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1744 rc = kiblnd_create_fmr_pool(fps, &fpo);
1746 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1752 kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, time64_t now)
1754 if (fpo->fpo_map_count != 0) /* still in use */
1756 if (fpo->fpo_failed)
1758 return now >= fpo->fpo_deadline;
1762 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1764 struct kib_hca_dev *hdev;
1765 __u64 *pages = tx->tx_pages;
1770 hdev = tx->tx_pool->tpo_hdev;
1772 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1773 for (size = 0; size < rd->rd_frags[i].rf_nob;
1774 size += hdev->ibh_page_size) {
1775 pages[npages++] = (rd->rd_frags[i].rf_addr &
1776 hdev->ibh_page_mask) + size;
1784 kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1787 struct kib_fmr_pool *fpo = fmr->fmr_pool;
1788 struct kib_fmr_poolset *fps;
1789 time64_t now = ktime_get_seconds();
1790 struct kib_fmr_pool *tmp;
1796 fps = fpo->fpo_owner;
1797 if (fpo->fpo_is_fmr) {
1798 if (fmr->fmr_pfmr) {
1799 ib_fmr_pool_unmap(fmr->fmr_pfmr);
1800 fmr->fmr_pfmr = NULL;
1804 rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1808 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1811 frd->frd_valid = false;
1812 spin_lock(&fps->fps_lock);
1813 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1814 spin_unlock(&fps->fps_lock);
1815 fmr->fmr_frd = NULL;
1818 fmr->fmr_pool = NULL;
1820 spin_lock(&fps->fps_lock);
1821 fpo->fpo_map_count--; /* decref the pool */
1823 list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1824 /* the first pool is persistent */
1825 if (fps->fps_pool_list.next == &fpo->fpo_list)
1828 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1829 list_move(&fpo->fpo_list, &zombies);
1833 spin_unlock(&fps->fps_lock);
1835 if (!list_empty(&zombies))
1836 kiblnd_destroy_fmr_pool_list(&zombies);
1839 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1840 struct kib_rdma_desc *rd, u32 nob, u64 iov,
1841 struct kib_fmr *fmr)
1843 struct kib_fmr_pool *fpo;
1844 __u64 *pages = tx->tx_pages;
1846 bool is_rx = (rd != tx->tx_rd);
1847 bool tx_pages_mapped = 0;
1852 spin_lock(&fps->fps_lock);
1853 version = fps->fps_version;
1854 list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1855 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1856 fpo->fpo_map_count++;
1858 if (fpo->fpo_is_fmr) {
1859 struct ib_pool_fmr *pfmr;
1861 spin_unlock(&fps->fps_lock);
1863 if (!tx_pages_mapped) {
1864 npages = kiblnd_map_tx_pages(tx, rd);
1865 tx_pages_mapped = 1;
1868 pfmr = kib_fmr_pool_map(fpo->fmr.fpo_fmr_pool,
1869 pages, npages, iov);
1870 if (likely(!IS_ERR(pfmr))) {
1871 fmr->fmr_key = is_rx ? pfmr->fmr->rkey
1873 fmr->fmr_frd = NULL;
1874 fmr->fmr_pfmr = pfmr;
1875 fmr->fmr_pool = fpo;
1880 if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
1881 struct kib_fast_reg_descriptor *frd;
1882 #ifdef HAVE_IB_MAP_MR_SG
1883 struct ib_reg_wr *wr;
1886 struct ib_rdma_wr *wr;
1887 struct ib_fast_reg_page_list *frpl;
1891 frd = list_first_entry(&fpo->fast_reg.fpo_pool_list,
1892 struct kib_fast_reg_descriptor,
1894 list_del(&frd->frd_list);
1895 spin_unlock(&fps->fps_lock);
1897 #ifndef HAVE_IB_MAP_MR_SG
1898 frpl = frd->frd_frpl;
1902 if (!frd->frd_valid) {
1903 struct ib_rdma_wr *inv_wr;
1904 __u32 key = is_rx ? mr->rkey : mr->lkey;
1906 inv_wr = &frd->frd_inv_wr;
1907 memset(inv_wr, 0, sizeof(*inv_wr));
1909 inv_wr->wr.opcode = IB_WR_LOCAL_INV;
1910 inv_wr->wr.wr_id = IBLND_WID_MR;
1911 inv_wr->wr.ex.invalidate_rkey = key;
1914 key = ib_inc_rkey(key);
1915 ib_update_fast_reg_key(mr, key);
1918 #ifdef HAVE_IB_MAP_MR_SG
1919 #ifdef HAVE_IB_MAP_MR_SG_5ARGS
1920 n = ib_map_mr_sg(mr, tx->tx_frags,
1921 tx->tx_nfrags, NULL, PAGE_SIZE);
1923 n = ib_map_mr_sg(mr, tx->tx_frags,
1924 tx->tx_nfrags, PAGE_SIZE);
1926 if (unlikely(n != tx->tx_nfrags)) {
1927 CERROR("Failed to map mr %d/%d "
1928 "elements\n", n, tx->tx_nfrags);
1929 return n < 0 ? n : -EINVAL;
1932 wr = &frd->frd_fastreg_wr;
1933 memset(wr, 0, sizeof(*wr));
1935 wr->wr.opcode = IB_WR_REG_MR;
1936 wr->wr.wr_id = IBLND_WID_MR;
1938 wr->wr.send_flags = 0;
1940 wr->key = is_rx ? mr->rkey : mr->lkey;
1941 wr->access = (IB_ACCESS_LOCAL_WRITE |
1942 IB_ACCESS_REMOTE_WRITE);
1944 if (!tx_pages_mapped) {
1945 npages = kiblnd_map_tx_pages(tx, rd);
1946 tx_pages_mapped = 1;
1949 LASSERT(npages <= frpl->max_page_list_len);
1950 memcpy(frpl->page_list, pages,
1951 sizeof(*pages) * npages);
1953 /* Prepare FastReg WR */
1954 wr = &frd->frd_fastreg_wr;
1955 memset(wr, 0, sizeof(*wr));
1957 wr->wr.opcode = IB_WR_FAST_REG_MR;
1958 wr->wr.wr_id = IBLND_WID_MR;
1960 wr->wr.wr.fast_reg.iova_start = iov;
1961 wr->wr.wr.fast_reg.page_list = frpl;
1962 wr->wr.wr.fast_reg.page_list_len = npages;
1963 wr->wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1964 wr->wr.wr.fast_reg.length = nob;
1965 wr->wr.wr.fast_reg.rkey =
1966 is_rx ? mr->rkey : mr->lkey;
1967 wr->wr.wr.fast_reg.access_flags =
1968 (IB_ACCESS_LOCAL_WRITE |
1969 IB_ACCESS_REMOTE_WRITE);
1972 fmr->fmr_key = is_rx ? mr->rkey : mr->lkey;
1974 fmr->fmr_pfmr = NULL;
1975 fmr->fmr_pool = fpo;
1978 spin_unlock(&fps->fps_lock);
1982 spin_lock(&fps->fps_lock);
1983 fpo->fpo_map_count--;
1984 if (rc != -EAGAIN) {
1985 spin_unlock(&fps->fps_lock);
1989 /* EAGAIN and ... */
1990 if (version != fps->fps_version) {
1991 spin_unlock(&fps->fps_lock);
1996 if (fps->fps_increasing) {
1997 spin_unlock(&fps->fps_lock);
1998 CDEBUG(D_NET, "Another thread is allocating new "
1999 "FMR pool, waiting for her to complete\n");
2005 if (ktime_get_seconds() < fps->fps_next_retry) {
2006 /* someone failed recently */
2007 spin_unlock(&fps->fps_lock);
2011 fps->fps_increasing = 1;
2012 spin_unlock(&fps->fps_lock);
2014 CDEBUG(D_NET, "Allocate new FMR pool\n");
2015 rc = kiblnd_create_fmr_pool(fps, &fpo);
2016 spin_lock(&fps->fps_lock);
2017 fps->fps_increasing = 0;
2020 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
2022 fps->fps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2024 spin_unlock(&fps->fps_lock);
2030 kiblnd_fini_pool(struct kib_pool *pool)
2032 LASSERT(list_empty(&pool->po_free_list));
2033 LASSERT(pool->po_allocated == 0);
2035 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
2039 kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
2041 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
2043 memset(pool, 0, sizeof(struct kib_pool));
2044 INIT_LIST_HEAD(&pool->po_free_list);
2045 pool->po_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
2046 pool->po_owner = ps;
2047 pool->po_size = size;
2051 kiblnd_destroy_pool_list(struct list_head *head)
2053 struct kib_pool *pool;
2055 while (!list_empty(head)) {
2056 pool = list_entry(head->next, struct kib_pool, po_list);
2057 list_del(&pool->po_list);
2059 LASSERT(pool->po_owner != NULL);
2060 pool->po_owner->ps_pool_destroy(pool);
2065 kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
2067 if (ps->ps_net == NULL) /* intialized? */
2070 spin_lock(&ps->ps_lock);
2071 while (!list_empty(&ps->ps_pool_list)) {
2072 struct kib_pool *po = list_entry(ps->ps_pool_list.next,
2073 struct kib_pool, po_list);
2076 if (po->po_allocated == 0)
2077 list_move(&po->po_list, zombies);
2079 list_move(&po->po_list, &ps->ps_failed_pool_list);
2081 spin_unlock(&ps->ps_lock);
2085 kiblnd_fini_poolset(struct kib_poolset *ps)
2087 if (ps->ps_net != NULL) { /* initialized? */
2088 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
2089 kiblnd_destroy_pool_list(&ps->ps_pool_list);
2094 kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
2095 struct kib_net *net, char *name, int size,
2096 kib_ps_pool_create_t po_create,
2097 kib_ps_pool_destroy_t po_destroy,
2098 kib_ps_node_init_t nd_init,
2099 kib_ps_node_fini_t nd_fini)
2101 struct kib_pool *pool;
2104 memset(ps, 0, sizeof(struct kib_poolset));
2108 ps->ps_pool_create = po_create;
2109 ps->ps_pool_destroy = po_destroy;
2110 ps->ps_node_init = nd_init;
2111 ps->ps_node_fini = nd_fini;
2112 ps->ps_pool_size = size;
2113 if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name))
2114 >= sizeof(ps->ps_name))
2116 spin_lock_init(&ps->ps_lock);
2117 INIT_LIST_HEAD(&ps->ps_pool_list);
2118 INIT_LIST_HEAD(&ps->ps_failed_pool_list);
2120 rc = ps->ps_pool_create(ps, size, &pool);
2122 list_add(&pool->po_list, &ps->ps_pool_list);
2124 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
2130 kiblnd_pool_is_idle(struct kib_pool *pool, time64_t now)
2132 if (pool->po_allocated != 0) /* still in use */
2134 if (pool->po_failed)
2136 return now >= pool->po_deadline;
2140 kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
2143 struct kib_poolset *ps = pool->po_owner;
2144 struct kib_pool *tmp;
2145 time64_t now = ktime_get_seconds();
2147 spin_lock(&ps->ps_lock);
2149 if (ps->ps_node_fini != NULL)
2150 ps->ps_node_fini(pool, node);
2152 LASSERT(pool->po_allocated > 0);
2153 list_add(node, &pool->po_free_list);
2154 pool->po_allocated--;
2156 list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
2157 /* the first pool is persistent */
2158 if (ps->ps_pool_list.next == &pool->po_list)
2161 if (kiblnd_pool_is_idle(pool, now))
2162 list_move(&pool->po_list, &zombies);
2164 spin_unlock(&ps->ps_lock);
2166 if (!list_empty(&zombies))
2167 kiblnd_destroy_pool_list(&zombies);
2171 kiblnd_pool_alloc_node(struct kib_poolset *ps)
2173 struct list_head *node;
2174 struct kib_pool *pool;
2176 unsigned int interval = 1;
2177 ktime_t time_before;
2178 unsigned int trips = 0;
2181 spin_lock(&ps->ps_lock);
2182 list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
2183 if (list_empty(&pool->po_free_list))
2186 pool->po_allocated++;
2187 pool->po_deadline = ktime_get_seconds() +
2188 IBLND_POOL_DEADLINE;
2189 node = pool->po_free_list.next;
2192 if (ps->ps_node_init != NULL) {
2193 /* still hold the lock */
2194 ps->ps_node_init(pool, node);
2196 spin_unlock(&ps->ps_lock);
2200 /* no available tx pool and ... */
2201 if (ps->ps_increasing) {
2202 /* another thread is allocating a new pool */
2203 spin_unlock(&ps->ps_lock);
2205 CDEBUG(D_NET, "Another thread is allocating new "
2206 "%s pool, waiting %d HZs for her to complete."
2208 ps->ps_name, interval, trips);
2210 set_current_state(TASK_INTERRUPTIBLE);
2211 schedule_timeout(interval);
2212 if (interval < cfs_time_seconds(1))
2218 if (ktime_get_seconds() < ps->ps_next_retry) {
2219 /* someone failed recently */
2220 spin_unlock(&ps->ps_lock);
2224 ps->ps_increasing = 1;
2225 spin_unlock(&ps->ps_lock);
2227 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
2228 time_before = ktime_get();
2229 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
2230 CDEBUG(D_NET, "ps_pool_create took %lld ms to complete",
2231 ktime_ms_delta(ktime_get(), time_before));
2233 spin_lock(&ps->ps_lock);
2234 ps->ps_increasing = 0;
2236 list_add_tail(&pool->po_list, &ps->ps_pool_list);
2238 ps->ps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2239 CERROR("Can't allocate new %s pool because out of memory\n",
2242 spin_unlock(&ps->ps_lock);
2248 kiblnd_destroy_tx_pool(struct kib_pool *pool)
2250 struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool,
2254 LASSERT (pool->po_allocated == 0);
2256 if (tpo->tpo_tx_pages != NULL) {
2257 kiblnd_unmap_tx_pool(tpo);
2258 kiblnd_free_pages(tpo->tpo_tx_pages);
2261 if (tpo->tpo_tx_descs == NULL)
2264 for (i = 0; i < pool->po_size; i++) {
2265 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2266 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2268 list_del(&tx->tx_list);
2269 if (tx->tx_pages != NULL)
2270 LIBCFS_FREE(tx->tx_pages,
2272 sizeof(*tx->tx_pages));
2273 if (tx->tx_frags != NULL)
2274 LIBCFS_FREE(tx->tx_frags,
2275 (1 + IBLND_MAX_RDMA_FRAGS) *
2276 sizeof(*tx->tx_frags));
2277 if (tx->tx_wrq != NULL)
2278 LIBCFS_FREE(tx->tx_wrq,
2279 (1 + IBLND_MAX_RDMA_FRAGS) *
2280 sizeof(*tx->tx_wrq));
2281 if (tx->tx_sge != NULL)
2282 LIBCFS_FREE(tx->tx_sge,
2283 (1 + IBLND_MAX_RDMA_FRAGS) * wrq_sge *
2284 sizeof(*tx->tx_sge));
2285 if (tx->tx_rd != NULL)
2286 LIBCFS_FREE(tx->tx_rd,
2287 offsetof(struct kib_rdma_desc,
2288 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2291 LIBCFS_FREE(tpo->tpo_tx_descs,
2292 pool->po_size * sizeof(struct kib_tx));
2294 kiblnd_fini_pool(pool);
2298 static int kiblnd_tx_pool_size(struct lnet_ni *ni, int ncpts)
2300 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2303 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2304 ntx = tunables->lnd_ntx / ncpts;
2306 return max(IBLND_TX_POOL, ntx);
2310 kiblnd_create_tx_pool(struct kib_poolset *ps, int size, struct kib_pool **pp_po)
2314 struct kib_pool *pool;
2315 struct kib_tx_pool *tpo;
2317 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2319 CERROR("Failed to allocate TX pool\n");
2323 pool = &tpo->tpo_pool;
2324 kiblnd_init_pool(ps, pool, size);
2325 tpo->tpo_tx_descs = NULL;
2326 tpo->tpo_tx_pages = NULL;
2328 npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2329 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg) != 0) {
2330 CERROR("Can't allocate tx pages: %d\n", npg);
2335 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2336 size * sizeof(struct kib_tx));
2337 if (tpo->tpo_tx_descs == NULL) {
2338 CERROR("Can't allocate %d tx descriptors\n", size);
2339 ps->ps_pool_destroy(pool);
2343 memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2345 for (i = 0; i < size; i++) {
2346 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2347 int wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2350 if (ps->ps_net->ibn_fmr_ps != NULL) {
2351 LIBCFS_CPT_ALLOC(tx->tx_pages,
2352 lnet_cpt_table(), ps->ps_cpt,
2353 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2354 if (tx->tx_pages == NULL)
2358 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2359 (1 + IBLND_MAX_RDMA_FRAGS) *
2360 sizeof(*tx->tx_frags));
2361 if (tx->tx_frags == NULL)
2364 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS + 1);
2366 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2367 (1 + IBLND_MAX_RDMA_FRAGS) *
2368 sizeof(*tx->tx_wrq));
2369 if (tx->tx_wrq == NULL)
2372 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2373 (1 + IBLND_MAX_RDMA_FRAGS) * wrq_sge *
2374 sizeof(*tx->tx_sge));
2375 if (tx->tx_sge == NULL)
2378 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2379 offsetof(struct kib_rdma_desc,
2380 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2381 if (tx->tx_rd == NULL)
2386 kiblnd_map_tx_pool(tpo);
2391 ps->ps_pool_destroy(pool);
2396 kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2398 struct kib_tx_poolset *tps = container_of(pool->po_owner,
2399 struct kib_tx_poolset,
2401 struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list);
2403 tx->tx_cookie = tps->tps_next_tx_cookie++;
2407 kiblnd_net_fini_pools(struct kib_net *net)
2411 cfs_cpt_for_each(i, lnet_cpt_table()) {
2412 struct kib_tx_poolset *tps;
2413 struct kib_fmr_poolset *fps;
2415 if (net->ibn_tx_ps != NULL) {
2416 tps = net->ibn_tx_ps[i];
2417 kiblnd_fini_poolset(&tps->tps_poolset);
2420 if (net->ibn_fmr_ps != NULL) {
2421 fps = net->ibn_fmr_ps[i];
2422 kiblnd_fini_fmr_poolset(fps);
2426 if (net->ibn_tx_ps != NULL) {
2427 cfs_percpt_free(net->ibn_tx_ps);
2428 net->ibn_tx_ps = NULL;
2431 if (net->ibn_fmr_ps != NULL) {
2432 cfs_percpt_free(net->ibn_fmr_ps);
2433 net->ibn_fmr_ps = NULL;
2438 kiblnd_net_init_pools(struct kib_net *net, struct lnet_ni *ni, __u32 *cpts,
2441 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2442 #ifdef HAVE_IB_GET_DMA_MR
2443 unsigned long flags;
2449 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2451 #ifdef HAVE_IB_GET_DMA_MR
2452 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2454 * if lnd_map_on_demand is zero then we have effectively disabled
2455 * FMR or FastReg and we're using global memory regions
2458 if (!tunables->lnd_map_on_demand) {
2459 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2461 goto create_tx_pool;
2464 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2467 if (tunables->lnd_fmr_pool_size < tunables->lnd_ntx / 4) {
2468 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2469 tunables->lnd_fmr_pool_size,
2470 tunables->lnd_ntx / 4);
2475 /* TX pool must be created later than FMR, see LU-2268
2477 LASSERT(net->ibn_tx_ps == NULL);
2479 /* premapping can fail if ibd_nmr > 1, so we always create
2480 * FMR pool and map-on-demand if premapping failed */
2482 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2483 sizeof(struct kib_fmr_poolset));
2484 if (net->ibn_fmr_ps == NULL) {
2485 CERROR("Failed to allocate FMR pool array\n");
2490 for (i = 0; i < ncpts; i++) {
2491 cpt = (cpts == NULL) ? i : cpts[i];
2492 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2495 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2502 LASSERT(i == ncpts);
2504 #ifdef HAVE_IB_GET_DMA_MR
2507 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2508 sizeof(struct kib_tx_poolset));
2509 if (net->ibn_tx_ps == NULL) {
2510 CERROR("Failed to allocate tx pool array\n");
2515 for (i = 0; i < ncpts; i++) {
2516 cpt = (cpts == NULL) ? i : cpts[i];
2517 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2519 kiblnd_tx_pool_size(ni, ncpts),
2520 kiblnd_create_tx_pool,
2521 kiblnd_destroy_tx_pool,
2522 kiblnd_tx_init, NULL);
2524 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2532 kiblnd_net_fini_pools(net);
2538 kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2540 struct ib_device_attr *dev_attr;
2543 /* It's safe to assume a HCA can handle a page size
2544 * matching that of the native system */
2545 hdev->ibh_page_shift = PAGE_SHIFT;
2546 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2547 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2549 #ifndef HAVE_IB_DEVICE_ATTRS
2550 LIBCFS_ALLOC(dev_attr, sizeof(*dev_attr));
2551 if (dev_attr == NULL) {
2552 CERROR("Out of memory\n");
2556 rc = ib_query_device(hdev->ibh_ibdev, dev_attr);
2558 CERROR("Failed to query IB device: %d\n", rc);
2559 goto out_clean_attr;
2562 dev_attr = &hdev->ibh_ibdev->attrs;
2565 hdev->ibh_mr_size = dev_attr->max_mr_size;
2566 hdev->ibh_max_qp_wr = dev_attr->max_qp_wr;
2568 /* Setup device Memory Registration capabilities */
2569 #ifdef HAVE_IB_DEVICE_OPS
2570 if (hdev->ibh_ibdev->ops.alloc_fmr &&
2571 hdev->ibh_ibdev->ops.dealloc_fmr &&
2572 hdev->ibh_ibdev->ops.map_phys_fmr &&
2573 hdev->ibh_ibdev->ops.unmap_fmr) {
2575 if (hdev->ibh_ibdev->alloc_fmr &&
2576 hdev->ibh_ibdev->dealloc_fmr &&
2577 hdev->ibh_ibdev->map_phys_fmr &&
2578 hdev->ibh_ibdev->unmap_fmr) {
2580 LCONSOLE_INFO("Using FMR for registration\n");
2581 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FMR_ENABLED;
2582 } else if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
2583 LCONSOLE_INFO("Using FastReg for registration\n");
2584 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_ENABLED;
2585 #ifndef HAVE_IB_ALLOC_FAST_REG_MR
2586 #ifdef IB_DEVICE_SG_GAPS_REG
2587 if (dev_attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
2588 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT;
2598 #ifndef HAVE_IB_DEVICE_ATTRS
2600 LIBCFS_FREE(dev_attr, sizeof(*dev_attr));
2604 CERROR("IB device does not support FMRs nor FastRegs, can't "
2605 "register memory: %d\n", rc);
2606 else if (rc == -EINVAL)
2607 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2611 #ifdef HAVE_IB_GET_DMA_MR
2613 kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev)
2615 if (hdev->ibh_mrs == NULL)
2618 ib_dereg_mr(hdev->ibh_mrs);
2620 hdev->ibh_mrs = NULL;
2625 kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2627 #ifdef HAVE_IB_GET_DMA_MR
2628 kiblnd_hdev_cleanup_mrs(hdev);
2631 if (hdev->ibh_pd != NULL)
2632 ib_dealloc_pd(hdev->ibh_pd);
2634 if (hdev->ibh_cmid != NULL)
2635 rdma_destroy_id(hdev->ibh_cmid);
2637 LIBCFS_FREE(hdev, sizeof(*hdev));
2640 #ifdef HAVE_IB_GET_DMA_MR
2642 kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev)
2645 int acflags = IB_ACCESS_LOCAL_WRITE |
2646 IB_ACCESS_REMOTE_WRITE;
2648 mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2650 CERROR("Failed ib_get_dma_mr: %ld\n", PTR_ERR(mr));
2651 kiblnd_hdev_cleanup_mrs(hdev);
2662 kiblnd_dummy_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2668 kiblnd_dev_need_failover(struct kib_dev *dev, struct net *ns)
2670 struct rdma_cm_id *cmid;
2671 struct sockaddr_in srcaddr;
2672 struct sockaddr_in dstaddr;
2675 if (dev->ibd_hdev == NULL || /* initializing */
2676 dev->ibd_hdev->ibh_cmid == NULL || /* listener is dead */
2677 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2680 /* XXX: it's UGLY, but I don't have better way to find
2681 * ib-bonding HCA failover because:
2683 * a. no reliable CM event for HCA failover...
2684 * b. no OFED API to get ib_device for current net_device...
2686 * We have only two choices at this point:
2688 * a. rdma_bind_addr(), it will conflict with listener cmid
2689 * b. rdma_resolve_addr() to zero addr */
2690 cmid = kiblnd_rdma_create_id(ns, kiblnd_dummy_callback, dev,
2691 RDMA_PS_TCP, IB_QPT_RC);
2694 CERROR("Failed to create cmid for failover: %d\n", rc);
2698 memset(&srcaddr, 0, sizeof(srcaddr));
2699 srcaddr.sin_family = AF_INET;
2700 srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2702 memset(&dstaddr, 0, sizeof(dstaddr));
2703 dstaddr.sin_family = AF_INET;
2704 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2705 (struct sockaddr *)&dstaddr, 1);
2706 if (rc != 0 || cmid->device == NULL) {
2707 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2708 dev->ibd_ifname, &dev->ibd_ifip,
2710 rdma_destroy_id(cmid);
2714 rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2715 rdma_destroy_id(cmid);
2720 kiblnd_dev_failover(struct kib_dev *dev, struct net *ns)
2722 LIST_HEAD(zombie_tpo);
2723 LIST_HEAD(zombie_ppo);
2724 LIST_HEAD(zombie_fpo);
2725 struct rdma_cm_id *cmid = NULL;
2726 struct kib_hca_dev *hdev = NULL;
2727 struct kib_hca_dev *old;
2729 struct kib_net *net;
2730 struct sockaddr_in addr;
2731 unsigned long flags;
2735 LASSERT (*kiblnd_tunables.kib_dev_failover > 1 ||
2736 dev->ibd_can_failover ||
2737 dev->ibd_hdev == NULL);
2739 rc = kiblnd_dev_need_failover(dev, ns);
2743 if (dev->ibd_hdev != NULL &&
2744 dev->ibd_hdev->ibh_cmid != NULL) {
2745 /* XXX it's not good to close old listener at here,
2746 * because we can fail to create new listener.
2747 * But we have to close it now, otherwise rdma_bind_addr
2748 * will return EADDRINUSE... How crap! */
2749 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2751 cmid = dev->ibd_hdev->ibh_cmid;
2752 /* make next schedule of kiblnd_dev_need_failover()
2753 * return 1 for me */
2754 dev->ibd_hdev->ibh_cmid = NULL;
2755 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2757 rdma_destroy_id(cmid);
2760 cmid = kiblnd_rdma_create_id(ns, kiblnd_cm_callback, dev, RDMA_PS_TCP,
2764 CERROR("Failed to create cmid for failover: %d\n", rc);
2768 memset(&addr, 0, sizeof(addr));
2769 addr.sin_family = AF_INET;
2770 addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2771 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2773 /* Bind to failover device or port */
2774 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2775 if (rc != 0 || cmid->device == NULL) {
2776 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2777 dev->ibd_ifname, &dev->ibd_ifip,
2779 rdma_destroy_id(cmid);
2783 LIBCFS_ALLOC(hdev, sizeof(*hdev));
2785 CERROR("Failed to allocate kib_hca_dev\n");
2786 rdma_destroy_id(cmid);
2791 atomic_set(&hdev->ibh_ref, 1);
2792 hdev->ibh_dev = dev;
2793 hdev->ibh_cmid = cmid;
2794 hdev->ibh_ibdev = cmid->device;
2796 #ifdef HAVE_IB_ALLOC_PD_2ARGS
2797 pd = ib_alloc_pd(cmid->device, 0);
2799 pd = ib_alloc_pd(cmid->device);
2803 CERROR("Can't allocate PD: %d\n", rc);
2809 rc = rdma_listen(cmid, 0);
2811 CERROR("Can't start new listener: %d\n", rc);
2815 rc = kiblnd_hdev_get_attr(hdev);
2817 CERROR("Can't get device attributes: %d\n", rc);
2821 #ifdef HAVE_IB_GET_DMA_MR
2822 rc = kiblnd_hdev_setup_mrs(hdev);
2824 CERROR("Can't setup device: %d\n", rc);
2829 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2831 old = dev->ibd_hdev;
2832 dev->ibd_hdev = hdev; /* take over the refcount */
2835 list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
2836 cfs_cpt_for_each(i, lnet_cpt_table()) {
2837 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
2840 if (net->ibn_fmr_ps != NULL)
2841 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
2846 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2848 if (!list_empty(&zombie_tpo))
2849 kiblnd_destroy_pool_list(&zombie_tpo);
2850 if (!list_empty(&zombie_ppo))
2851 kiblnd_destroy_pool_list(&zombie_ppo);
2852 if (!list_empty(&zombie_fpo))
2853 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
2855 kiblnd_hdev_decref(hdev);
2858 dev->ibd_failed_failover++;
2860 dev->ibd_failed_failover = 0;
2866 kiblnd_destroy_dev(struct kib_dev *dev)
2868 LASSERT(dev->ibd_nnets == 0);
2869 LASSERT(list_empty(&dev->ibd_nets));
2871 list_del(&dev->ibd_fail_list);
2872 list_del(&dev->ibd_list);
2874 if (dev->ibd_hdev != NULL)
2875 kiblnd_hdev_decref(dev->ibd_hdev);
2877 LIBCFS_FREE(dev, sizeof(*dev));
2881 kiblnd_base_shutdown(void)
2883 struct kib_sched_info *sched;
2886 LASSERT(list_empty(&kiblnd_data.kib_devs));
2888 CDEBUG(D_MALLOC, "before LND base cleanup: kmem %d\n",
2889 atomic_read(&libcfs_kmemory));
2891 switch (kiblnd_data.kib_init) {
2895 case IBLND_INIT_ALL:
2896 case IBLND_INIT_DATA:
2897 LASSERT (kiblnd_data.kib_peers != NULL);
2898 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
2899 LASSERT(list_empty(&kiblnd_data.kib_peers[i]));
2901 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
2902 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
2903 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
2904 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
2906 /* flag threads to terminate; wake and wait for them to die */
2907 kiblnd_data.kib_shutdown = 1;
2909 /* NB: we really want to stop scheduler threads net by net
2910 * instead of the whole module, this should be improved
2911 * with dynamic configuration LNet */
2912 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
2913 wake_up_all(&sched->ibs_waitq);
2915 wake_up_all(&kiblnd_data.kib_connd_waitq);
2916 wake_up_all(&kiblnd_data.kib_failover_waitq);
2919 while (atomic_read(&kiblnd_data.kib_nthreads) != 0) {
2922 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2923 "Waiting for %d threads to terminate\n",
2924 atomic_read(&kiblnd_data.kib_nthreads));
2925 set_current_state(TASK_UNINTERRUPTIBLE);
2926 schedule_timeout(cfs_time_seconds(1));
2931 case IBLND_INIT_NOTHING:
2935 if (kiblnd_data.kib_peers != NULL) {
2936 LIBCFS_FREE(kiblnd_data.kib_peers,
2937 sizeof(struct list_head) *
2938 kiblnd_data.kib_peer_hash_size);
2941 if (kiblnd_data.kib_scheds != NULL)
2942 cfs_percpt_free(kiblnd_data.kib_scheds);
2944 CDEBUG(D_MALLOC, "after LND base cleanup: kmem %d\n",
2945 atomic_read(&libcfs_kmemory));
2947 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
2948 module_put(THIS_MODULE);
2952 kiblnd_shutdown(struct lnet_ni *ni)
2954 struct kib_net *net = ni->ni_data;
2955 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2957 unsigned long flags;
2959 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
2964 CDEBUG(D_MALLOC, "before LND net cleanup: kmem %d\n",
2965 atomic_read(&libcfs_kmemory));
2967 write_lock_irqsave(g_lock, flags);
2968 net->ibn_shutdown = 1;
2969 write_unlock_irqrestore(g_lock, flags);
2971 switch (net->ibn_init) {
2975 case IBLND_INIT_ALL:
2976 /* nuke all existing peers within this net */
2977 kiblnd_del_peer(ni, LNET_NID_ANY);
2979 /* Wait for all peer_ni state to clean up */
2981 while (atomic_read(&net->ibn_npeers) != 0) {
2984 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2985 "%s: waiting for %d peers to disconnect\n",
2986 libcfs_nid2str(ni->ni_nid),
2987 atomic_read(&net->ibn_npeers));
2988 set_current_state(TASK_UNINTERRUPTIBLE);
2989 schedule_timeout(cfs_time_seconds(1));
2992 kiblnd_net_fini_pools(net);
2994 write_lock_irqsave(g_lock, flags);
2995 LASSERT(net->ibn_dev->ibd_nnets > 0);
2996 net->ibn_dev->ibd_nnets--;
2997 list_del(&net->ibn_list);
2998 write_unlock_irqrestore(g_lock, flags);
3002 case IBLND_INIT_NOTHING:
3003 LASSERT (atomic_read(&net->ibn_nconns) == 0);
3005 if (net->ibn_dev != NULL &&
3006 net->ibn_dev->ibd_nnets == 0)
3007 kiblnd_destroy_dev(net->ibn_dev);
3012 CDEBUG(D_MALLOC, "after LND net cleanup: kmem %d\n",
3013 atomic_read(&libcfs_kmemory));
3015 net->ibn_init = IBLND_INIT_NOTHING;
3018 LIBCFS_FREE(net, sizeof(*net));
3021 if (list_empty(&kiblnd_data.kib_devs))
3022 kiblnd_base_shutdown();
3026 kiblnd_base_startup(struct net *ns)
3028 struct kib_sched_info *sched;
3032 LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
3034 if (!try_module_get(THIS_MODULE))
3037 memset(&kiblnd_data, 0, sizeof(kiblnd_data)); /* zero pointers, flags etc */
3039 rwlock_init(&kiblnd_data.kib_global_lock);
3041 INIT_LIST_HEAD(&kiblnd_data.kib_devs);
3042 INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
3044 kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
3045 LIBCFS_ALLOC(kiblnd_data.kib_peers,
3046 sizeof(struct list_head) *
3047 kiblnd_data.kib_peer_hash_size);
3048 if (kiblnd_data.kib_peers == NULL)
3051 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
3052 INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]);
3054 spin_lock_init(&kiblnd_data.kib_connd_lock);
3055 INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
3056 INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
3057 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
3058 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
3060 init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
3061 init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
3063 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
3065 if (kiblnd_data.kib_scheds == NULL)
3068 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
3071 spin_lock_init(&sched->ibs_lock);
3072 INIT_LIST_HEAD(&sched->ibs_conns);
3073 init_waitqueue_head(&sched->ibs_waitq);
3075 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
3076 if (*kiblnd_tunables.kib_nscheds > 0) {
3077 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
3079 /* max to half of CPUs, another half is reserved for
3080 * upper layer modules */
3081 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3084 sched->ibs_nthreads_max = nthrs;
3088 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
3090 /* lists/ptrs/locks initialised */
3091 kiblnd_data.kib_init = IBLND_INIT_DATA;
3092 /*****************************************************/
3094 rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
3096 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
3100 if (*kiblnd_tunables.kib_dev_failover != 0)
3101 rc = kiblnd_thread_start(kiblnd_failover_thread, ns,
3105 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
3109 /* flag everything initialised */
3110 kiblnd_data.kib_init = IBLND_INIT_ALL;
3111 /*****************************************************/
3116 kiblnd_base_shutdown();
3121 kiblnd_start_schedulers(struct kib_sched_info *sched)
3127 if (sched->ibs_nthreads == 0) {
3128 if (*kiblnd_tunables.kib_nscheds > 0) {
3129 nthrs = sched->ibs_nthreads_max;
3131 nthrs = cfs_cpt_weight(lnet_cpt_table(),
3133 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3134 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
3137 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
3138 /* increase one thread if there is new interface */
3139 nthrs = (sched->ibs_nthreads < sched->ibs_nthreads_max);
3142 for (i = 0; i < nthrs; i++) {
3145 id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
3146 snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld",
3147 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
3148 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id, name);
3152 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
3153 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
3157 sched->ibs_nthreads += i;
3161 static int kiblnd_dev_start_threads(struct kib_dev *dev, bool newdev, u32 *cpts,
3168 for (i = 0; i < ncpts; i++) {
3169 struct kib_sched_info *sched;
3171 cpt = (cpts == NULL) ? i : cpts[i];
3172 sched = kiblnd_data.kib_scheds[cpt];
3174 if (!newdev && sched->ibs_nthreads > 0)
3177 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
3179 CERROR("Failed to start scheduler threads for %s\n",
3187 static struct kib_dev *
3188 kiblnd_dev_search(char *ifname)
3190 struct kib_dev *alias = NULL;
3191 struct kib_dev *dev;
3195 colon = strchr(ifname, ':');
3196 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3197 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3203 colon2 = strchr(dev->ibd_ifname, ':');
3209 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3221 kiblnd_startup(struct lnet_ni *ni)
3223 char *ifname = NULL;
3224 struct lnet_inetdev *ifaces = NULL;
3225 struct kib_dev *ibdev = NULL;
3226 struct kib_net *net = NULL;
3227 unsigned long flags;
3232 LASSERT(ni->ni_net->net_lnd == &the_o2iblnd);
3234 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
3235 rc = kiblnd_base_startup(ni->ni_net_ns);
3240 LIBCFS_ALLOC(net, sizeof(*net));
3247 net->ibn_incarnation = ktime_get_real_ns() / NSEC_PER_USEC;
3249 kiblnd_tunables_setup(ni);
3252 * ni_interfaces is only to support legacy pre Multi-Rail
3253 * tcp bonding for ksocklnd. Multi-Rail wants each secondary
3254 * IP to be treated as an unique 'struct ni' interfaces instead.
3256 if (ni->ni_interfaces[0] != NULL) {
3257 /* Use the IPoIB interface specified in 'networks=' */
3258 if (ni->ni_interfaces[1] != NULL) {
3259 CERROR("ko2iblnd: Multiple interfaces not supported\n");
3264 ifname = ni->ni_interfaces[0];
3266 ifname = *kiblnd_tunables.kib_default_ipif;
3269 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
3270 CERROR("IPoIB interface name too long: %s\n", ifname);
3275 rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns);
3279 for (i = 0; i < rc; i++) {
3280 if (strcmp(ifname, ifaces[i].li_name) == 0)
3285 CERROR("ko2iblnd: No matching interfaces\n");
3290 ibdev = kiblnd_dev_search(ifname);
3291 newdev = ibdev == NULL;
3292 /* hmm...create kib_dev even for alias */
3293 if (ibdev == NULL || strcmp(&ibdev->ibd_ifname[0], ifname) != 0) {
3294 LIBCFS_ALLOC(ibdev, sizeof(*ibdev));
3300 ibdev->ibd_ifip = ifaces[i].li_ipaddr;
3301 strlcpy(ibdev->ibd_ifname, ifaces[i].li_name,
3302 sizeof(ibdev->ibd_ifname));
3303 ibdev->ibd_can_failover = !!(ifaces[i].li_flags & IFF_MASTER);
3305 INIT_LIST_HEAD(&ibdev->ibd_nets);
3306 INIT_LIST_HEAD(&ibdev->ibd_list); /* not yet in kib_devs */
3307 INIT_LIST_HEAD(&ibdev->ibd_fail_list);
3309 /* initialize the device */
3310 rc = kiblnd_dev_failover(ibdev, ni->ni_net_ns);
3312 CERROR("ko2iblnd: Can't initialize device: rc = %d\n",
3317 list_add_tail(&ibdev->ibd_list, &kiblnd_data.kib_devs);
3320 net->ibn_dev = ibdev;
3321 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip);
3323 ni->ni_dev_cpt = ifaces[i].li_cpt;
3325 rc = kiblnd_dev_start_threads(ibdev, newdev, ni->ni_cpts, ni->ni_ncpts);
3329 rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
3331 CERROR("Failed to initialize NI pools: %d\n", rc);
3335 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3337 list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
3338 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3340 net->ibn_init = IBLND_INIT_ALL;
3345 if (net != NULL && net->ibn_dev == NULL && ibdev != NULL)
3346 kiblnd_destroy_dev(ibdev);
3349 kiblnd_shutdown(ni);
3351 CDEBUG(D_NET, "Configuration of device %s failed: rc = %d\n",
3352 ifname ? ifname : "", rc);
3357 static const struct lnet_lnd the_o2iblnd = {
3358 .lnd_type = O2IBLND,
3359 .lnd_startup = kiblnd_startup,
3360 .lnd_shutdown = kiblnd_shutdown,
3361 .lnd_ctl = kiblnd_ctl,
3362 .lnd_query = kiblnd_query,
3363 .lnd_send = kiblnd_send,
3364 .lnd_recv = kiblnd_recv,
3367 static void __exit ko2iblnd_exit(void)
3369 lnet_unregister_lnd(&the_o2iblnd);
3372 static int __init ko2iblnd_init(void)
3376 BUILD_BUG_ON(sizeof(struct kib_msg) > IBLND_MSG_SIZE);
3377 BUILD_BUG_ON(offsetof(struct kib_msg,
3378 ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3380 BUILD_BUG_ON(offsetof(struct kib_msg,
3381 ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3384 rc = kiblnd_tunables_init();
3388 lnet_register_lnd(&the_o2iblnd);
3393 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3394 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
3395 MODULE_VERSION("2.8.0");
3396 MODULE_LICENSE("GPL");
3398 module_init(ko2iblnd_init);
3399 module_exit(ko2iblnd_exit);