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, 2015, 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>
37 #include <asm/div64.h>
41 static struct lnet_lnd the_o2iblnd;
43 struct kib_data kiblnd_data;
45 static __u32 kiblnd_cksum(void *ptr, int nob)
51 sum = ((sum << 1) | (sum >> 31)) + *c++;
53 /* ensure I don't return 0 (== no checksum) */
54 return !sum ? 1 : sum;
57 static char *kiblnd_msgtype2str(int type)
60 case IBLND_MSG_CONNREQ:
63 case IBLND_MSG_CONNACK:
69 case IBLND_MSG_IMMEDIATE:
72 case IBLND_MSG_PUT_REQ:
75 case IBLND_MSG_PUT_NAK:
78 case IBLND_MSG_PUT_ACK:
81 case IBLND_MSG_PUT_DONE:
84 case IBLND_MSG_GET_REQ:
87 case IBLND_MSG_GET_DONE:
95 static int kiblnd_msgtype2size(int type)
97 const int hdr_size = offsetof(struct kib_msg, ibm_u);
100 case IBLND_MSG_CONNREQ:
101 case IBLND_MSG_CONNACK:
102 return hdr_size + sizeof(struct kib_connparams);
107 case IBLND_MSG_IMMEDIATE:
108 return offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[0]);
110 case IBLND_MSG_PUT_REQ:
111 return hdr_size + sizeof(struct kib_putreq_msg);
113 case IBLND_MSG_PUT_ACK:
114 return hdr_size + sizeof(struct kib_putack_msg);
116 case IBLND_MSG_GET_REQ:
117 return hdr_size + sizeof(struct kib_get_msg);
119 case IBLND_MSG_PUT_NAK:
120 case IBLND_MSG_PUT_DONE:
121 case IBLND_MSG_GET_DONE:
122 return hdr_size + sizeof(struct kib_completion_msg);
128 static int kiblnd_unpack_rd(struct kib_msg *msg, int flip)
130 struct kib_rdma_desc *rd;
136 LASSERT(msg->ibm_type == IBLND_MSG_GET_REQ ||
137 msg->ibm_type == IBLND_MSG_PUT_ACK);
139 rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
140 &msg->ibm_u.get.ibgm_rd :
141 &msg->ibm_u.putack.ibpam_rd;
144 __swab32s(&rd->rd_key);
145 __swab32s(&rd->rd_nfrags);
150 nob = offsetof(struct kib_msg, ibm_u) +
151 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
153 if (msg->ibm_nob < nob) {
154 CERROR("Short %s: %d(%d)\n",
155 kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
159 msg_size = kiblnd_rd_size(rd);
160 if (msg_size <= 0 || msg_size > LNET_MAX_PAYLOAD) {
161 CERROR("Bad msg_size: %d, should be 0 < n <= %d\n",
162 msg_size, LNET_MAX_PAYLOAD);
169 for (i = 0; i < n; i++) {
170 __swab32s(&rd->rd_frags[i].rf_nob);
171 __swab64s(&rd->rd_frags[i].rf_addr);
177 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
178 int credits, lnet_nid_t dstnid, __u64 dststamp)
180 struct kib_net *net = ni->ni_data;
183 * CAVEAT EMPTOR! all message fields not set here should have been
184 * 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);
245 * checksum must be computed with ibm_cksum zero and BEFORE anything
248 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
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 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);
316 int kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer **peerp,
319 struct kib_peer *peer;
320 struct kib_net *net = ni->ni_data;
321 int cpt = lnet_cpt_of_nid(nid);
325 LASSERT(nid != LNET_NID_ANY);
327 LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer));
329 CERROR("Cannot allocate peer\n");
336 peer->ibp_last_alive = 0;
337 peer->ibp_max_frags = kiblnd_cfg_rdma_frags(peer->ibp_ni);
338 peer->ibp_queue_depth = ni->ni_peertxcredits;
339 atomic_set(&peer->ibp_refcount, 1); /* 1 ref for caller */
341 INIT_LIST_HEAD(&peer->ibp_list); /* not in the peer table yet */
342 INIT_LIST_HEAD(&peer->ibp_conns);
343 INIT_LIST_HEAD(&peer->ibp_tx_queue);
345 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
347 /* always called with a ref on ni, which prevents ni being shutdown */
348 LASSERT(!net->ibn_shutdown);
350 /* npeers only grows with the global lock held */
351 atomic_inc(&net->ibn_npeers);
353 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
359 void kiblnd_destroy_peer(struct kib_peer *peer)
361 struct kib_net *net = peer->ibp_ni->ni_data;
364 LASSERT(!atomic_read(&peer->ibp_refcount));
365 LASSERT(!kiblnd_peer_active(peer));
366 LASSERT(kiblnd_peer_idle(peer));
367 LASSERT(list_empty(&peer->ibp_tx_queue));
369 LIBCFS_FREE(peer, sizeof(*peer));
372 * NB a peer's connections keep a reference on their peer until
373 * they are destroyed, so we can be assured that _all_ state to do
374 * with this peer has been cleaned up when its refcount drops to
377 atomic_dec(&net->ibn_npeers);
380 struct kib_peer *kiblnd_find_peer_locked(lnet_nid_t nid)
383 * the caller is responsible for accounting the additional reference
386 struct list_head *peer_list = kiblnd_nid2peerlist(nid);
387 struct list_head *tmp;
388 struct kib_peer *peer;
390 list_for_each(tmp, peer_list) {
391 peer = list_entry(tmp, struct kib_peer, ibp_list);
392 LASSERT(!kiblnd_peer_idle(peer));
394 if (peer->ibp_nid != nid)
397 CDEBUG(D_NET, "got peer [%p] -> %s (%d) version: %x\n",
398 peer, libcfs_nid2str(nid),
399 atomic_read(&peer->ibp_refcount),
406 void kiblnd_unlink_peer_locked(struct kib_peer *peer)
408 LASSERT(list_empty(&peer->ibp_conns));
410 LASSERT(kiblnd_peer_active(peer));
411 list_del_init(&peer->ibp_list);
412 /* lose peerlist's ref */
413 kiblnd_peer_decref(peer);
416 static int kiblnd_get_peer_info(struct lnet_ni *ni, int index,
417 lnet_nid_t *nidp, int *count)
419 struct kib_peer *peer;
420 struct list_head *ptmp;
424 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
426 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
427 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
428 peer = list_entry(ptmp, struct kib_peer, ibp_list);
429 LASSERT(!kiblnd_peer_idle(peer));
431 if (peer->ibp_ni != ni)
437 *nidp = peer->ibp_nid;
438 *count = atomic_read(&peer->ibp_refcount);
440 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
446 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
450 static void kiblnd_del_peer_locked(struct kib_peer *peer)
452 struct list_head *ctmp;
453 struct list_head *cnxt;
454 struct kib_conn *conn;
456 if (list_empty(&peer->ibp_conns)) {
457 kiblnd_unlink_peer_locked(peer);
459 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
460 conn = list_entry(ctmp, struct kib_conn, ibc_list);
462 kiblnd_close_conn_locked(conn, 0);
464 /* NB closing peer's last conn unlinked it. */
467 * NB peer now unlinked; might even be freed if the peer table had the
472 static int kiblnd_del_peer(struct lnet_ni *ni, lnet_nid_t nid)
475 struct list_head *ptmp;
476 struct list_head *pnxt;
477 struct kib_peer *peer;
484 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
486 if (nid != LNET_NID_ANY) {
487 lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
488 hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
491 hi = kiblnd_data.kib_peer_hash_size - 1;
494 for (i = lo; i <= hi; i++) {
495 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
496 peer = list_entry(ptmp, struct kib_peer, ibp_list);
497 LASSERT(!kiblnd_peer_idle(peer));
499 if (peer->ibp_ni != ni)
502 if (!(nid == LNET_NID_ANY || peer->ibp_nid == nid))
505 if (!list_empty(&peer->ibp_tx_queue)) {
506 LASSERT(list_empty(&peer->ibp_conns));
508 list_splice_init(&peer->ibp_tx_queue,
512 kiblnd_del_peer_locked(peer);
513 rc = 0; /* matched something */
517 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
519 kiblnd_txlist_done(ni, &zombies, -EIO);
524 static struct kib_conn *kiblnd_get_conn_by_idx(struct lnet_ni *ni, int index)
526 struct kib_peer *peer;
527 struct list_head *ptmp;
528 struct kib_conn *conn;
529 struct list_head *ctmp;
533 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
535 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
536 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
537 peer = list_entry(ptmp, struct kib_peer, ibp_list);
538 LASSERT(!kiblnd_peer_idle(peer));
540 if (peer->ibp_ni != ni)
543 list_for_each(ctmp, &peer->ibp_conns) {
547 conn = list_entry(ctmp, struct kib_conn,
549 kiblnd_conn_addref(conn);
550 read_unlock_irqrestore(
551 &kiblnd_data.kib_global_lock,
558 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
562 int kiblnd_translate_mtu(int value)
582 static void kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
586 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
587 if (!cmid->route.path_rec)
590 mtu = kiblnd_translate_mtu(*kiblnd_tunables.kib_ib_mtu);
593 cmid->route.path_rec->mtu = mtu;
596 static int kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
602 lnet_nid_t nid = conn->ibc_peer->ibp_nid;
604 vectors = conn->ibc_cmid->device->num_comp_vectors;
608 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
612 /* hash NID to CPU id in this partition... */
613 off = do_div(nid, cpumask_weight(mask));
614 for_each_cpu(i, mask) {
623 struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cmid,
624 int state, int version)
628 * If the new conn is created successfully it takes over the caller's
629 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
630 * is destroyed. On failure, the caller's ref on 'peer' remains and
631 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
632 * to destroy 'cmid' here since I'm called from the CM which still has
633 * its ref on 'cmid').
635 rwlock_t *glock = &kiblnd_data.kib_global_lock;
636 struct kib_net *net = peer->ibp_ni->ni_data;
638 struct ib_qp_init_attr *init_qp_attr;
639 struct kib_sched_info *sched;
640 struct ib_cq_init_attr cq_attr = {};
641 struct kib_conn *conn;
649 LASSERT(!in_interrupt());
653 cpt = lnet_cpt_of_nid(peer->ibp_nid);
654 sched = kiblnd_data.kib_scheds[cpt];
656 LASSERT(sched->ibs_nthreads > 0);
658 LIBCFS_CPT_ALLOC(init_qp_attr, lnet_cpt_table(), cpt,
659 sizeof(*init_qp_attr));
661 CERROR("Can't allocate qp_attr for %s\n",
662 libcfs_nid2str(peer->ibp_nid));
666 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
668 CERROR("Can't allocate connection for %s\n",
669 libcfs_nid2str(peer->ibp_nid));
673 conn->ibc_state = IBLND_CONN_INIT;
674 conn->ibc_version = version;
675 conn->ibc_peer = peer; /* I take the caller's ref */
676 cmid->context = conn; /* for future CM callbacks */
677 conn->ibc_cmid = cmid;
678 conn->ibc_max_frags = peer->ibp_max_frags;
679 conn->ibc_queue_depth = peer->ibp_queue_depth;
681 INIT_LIST_HEAD(&conn->ibc_early_rxs);
682 INIT_LIST_HEAD(&conn->ibc_tx_noops);
683 INIT_LIST_HEAD(&conn->ibc_tx_queue);
684 INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
685 INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
686 INIT_LIST_HEAD(&conn->ibc_active_txs);
687 spin_lock_init(&conn->ibc_lock);
689 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
690 sizeof(*conn->ibc_connvars));
691 if (!conn->ibc_connvars) {
692 CERROR("Can't allocate in-progress connection state\n");
696 write_lock_irqsave(glock, flags);
697 if (dev->ibd_failover) {
698 write_unlock_irqrestore(glock, flags);
699 CERROR("%s: failover in progress\n", dev->ibd_ifname);
703 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
704 /* wakeup failover thread and teardown connection */
705 if (kiblnd_dev_can_failover(dev)) {
706 list_add_tail(&dev->ibd_fail_list,
707 &kiblnd_data.kib_failed_devs);
708 wake_up(&kiblnd_data.kib_failover_waitq);
711 write_unlock_irqrestore(glock, flags);
712 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
713 cmid->device->name, dev->ibd_ifname);
717 kiblnd_hdev_addref_locked(dev->ibd_hdev);
718 conn->ibc_hdev = dev->ibd_hdev;
720 kiblnd_setup_mtu_locked(cmid);
722 write_unlock_irqrestore(glock, flags);
724 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
725 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
726 if (!conn->ibc_rxs) {
727 CERROR("Cannot allocate RX buffers\n");
731 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
732 IBLND_RX_MSG_PAGES(conn));
736 kiblnd_map_rx_descs(conn);
738 cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
739 cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
740 cq = ib_create_cq(cmid->device,
741 kiblnd_cq_completion, kiblnd_cq_event, conn,
744 CERROR("Failed to create CQ with %d CQEs: %ld\n",
745 IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
751 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
753 CERROR("Can't request completion notification: %d\n", rc);
757 init_qp_attr->event_handler = kiblnd_qp_event;
758 init_qp_attr->qp_context = conn;
759 init_qp_attr->cap.max_send_wr = IBLND_SEND_WRS(conn);
760 init_qp_attr->cap.max_recv_wr = IBLND_RECV_WRS(conn);
761 init_qp_attr->cap.max_send_sge = 1;
762 init_qp_attr->cap.max_recv_sge = 1;
763 init_qp_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
764 init_qp_attr->qp_type = IB_QPT_RC;
765 init_qp_attr->send_cq = cq;
766 init_qp_attr->recv_cq = cq;
768 conn->ibc_sched = sched;
770 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
772 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
773 rc, init_qp_attr->cap.max_send_wr,
774 init_qp_attr->cap.max_recv_wr);
778 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
780 /* 1 ref for caller and each rxmsg */
781 atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
782 conn->ibc_nrx = IBLND_RX_MSGS(conn);
785 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
786 rc = kiblnd_post_rx(&conn->ibc_rxs[i],
787 IBLND_POSTRX_NO_CREDIT);
789 CERROR("Can't post rxmsg: %d\n", rc);
791 /* Make posted receives complete */
792 kiblnd_abort_receives(conn);
795 * correct # of posted buffers
796 * NB locking needed now I'm racing with completion
798 spin_lock_irqsave(&sched->ibs_lock, flags);
799 conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i;
800 spin_unlock_irqrestore(&sched->ibs_lock, flags);
803 * cmid will be destroyed by CM(ofed) after cm_callback
804 * returned, so we can't refer it anymore
805 * (by kiblnd_connd()->kiblnd_destroy_conn)
807 rdma_destroy_qp(conn->ibc_cmid);
808 conn->ibc_cmid = NULL;
810 /* Drop my own and unused rxbuffer refcounts */
811 while (i++ <= IBLND_RX_MSGS(conn))
812 kiblnd_conn_decref(conn);
818 /* Init successful! */
819 LASSERT(state == IBLND_CONN_ACTIVE_CONNECT ||
820 state == IBLND_CONN_PASSIVE_WAIT);
821 conn->ibc_state = state;
824 atomic_inc(&net->ibn_nconns);
828 kiblnd_destroy_conn(conn);
829 LIBCFS_FREE(conn, sizeof(*conn));
831 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
836 void kiblnd_destroy_conn(struct kib_conn *conn)
838 struct rdma_cm_id *cmid = conn->ibc_cmid;
839 struct kib_peer *peer = conn->ibc_peer;
842 LASSERT(!in_interrupt());
843 LASSERT(!atomic_read(&conn->ibc_refcount));
844 LASSERT(list_empty(&conn->ibc_early_rxs));
845 LASSERT(list_empty(&conn->ibc_tx_noops));
846 LASSERT(list_empty(&conn->ibc_tx_queue));
847 LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
848 LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
849 LASSERT(list_empty(&conn->ibc_active_txs));
850 LASSERT(!conn->ibc_noops_posted);
851 LASSERT(!conn->ibc_nsends_posted);
853 switch (conn->ibc_state) {
855 /* conn must be completely disengaged from the network */
858 case IBLND_CONN_DISCONNECTED:
859 /* connvars should have been freed already */
860 LASSERT(!conn->ibc_connvars);
863 case IBLND_CONN_INIT:
867 /* conn->ibc_cmid might be destroyed by CM already */
868 if (cmid && cmid->qp)
869 rdma_destroy_qp(cmid);
872 rc = ib_destroy_cq(conn->ibc_cq);
874 CWARN("Error destroying CQ: %d\n", rc);
877 if (conn->ibc_rx_pages)
878 kiblnd_unmap_rx_descs(conn);
881 LIBCFS_FREE(conn->ibc_rxs,
882 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
885 if (conn->ibc_connvars)
886 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
889 kiblnd_hdev_decref(conn->ibc_hdev);
891 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
892 if (conn->ibc_state != IBLND_CONN_INIT) {
893 struct kib_net *net = peer->ibp_ni->ni_data;
895 kiblnd_peer_decref(peer);
896 rdma_destroy_id(cmid);
897 atomic_dec(&net->ibn_nconns);
901 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
903 struct kib_conn *conn;
904 struct list_head *ctmp;
905 struct list_head *cnxt;
908 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
909 conn = list_entry(ctmp, struct kib_conn, ibc_list);
911 CDEBUG(D_NET, "Closing conn -> %s, version: %x, reason: %d\n",
912 libcfs_nid2str(peer->ibp_nid),
913 conn->ibc_version, why);
915 kiblnd_close_conn_locked(conn, why);
922 int kiblnd_close_stale_conns_locked(struct kib_peer *peer,
923 int version, __u64 incarnation)
925 struct kib_conn *conn;
926 struct list_head *ctmp;
927 struct list_head *cnxt;
930 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
931 conn = list_entry(ctmp, struct kib_conn, ibc_list);
933 if (conn->ibc_version == version &&
934 conn->ibc_incarnation == incarnation)
938 "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n",
939 libcfs_nid2str(peer->ibp_nid),
940 conn->ibc_version, conn->ibc_incarnation,
941 version, incarnation);
943 kiblnd_close_conn_locked(conn, -ESTALE);
950 static int kiblnd_close_matching_conns(struct lnet_ni *ni, lnet_nid_t nid)
952 struct kib_peer *peer;
953 struct list_head *ptmp;
954 struct list_head *pnxt;
961 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
963 if (nid != LNET_NID_ANY) {
964 lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
965 hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
968 hi = kiblnd_data.kib_peer_hash_size - 1;
971 for (i = lo; i <= hi; i++) {
972 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
973 peer = list_entry(ptmp, struct kib_peer, ibp_list);
974 LASSERT(!kiblnd_peer_idle(peer));
976 if (peer->ibp_ni != ni)
979 if (!(nid == LNET_NID_ANY || nid == peer->ibp_nid))
982 count += kiblnd_close_peer_conns_locked(peer, 0);
986 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
988 /* wildcards always succeed */
989 if (nid == LNET_NID_ANY)
992 return !count ? -ENOENT : 0;
995 static int kiblnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
997 struct libcfs_ioctl_data *data = arg;
1001 case IOC_LIBCFS_GET_PEER: {
1005 rc = kiblnd_get_peer_info(ni, data->ioc_count,
1007 data->ioc_nid = nid;
1008 data->ioc_count = count;
1012 case IOC_LIBCFS_DEL_PEER: {
1013 rc = kiblnd_del_peer(ni, data->ioc_nid);
1016 case IOC_LIBCFS_GET_CONN: {
1017 struct kib_conn *conn;
1020 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1026 LASSERT(conn->ibc_cmid);
1027 data->ioc_nid = conn->ibc_peer->ibp_nid;
1028 if (!conn->ibc_cmid->route.path_rec)
1029 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1032 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1033 kiblnd_conn_decref(conn);
1036 case IOC_LIBCFS_CLOSE_CONNECTION: {
1037 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1048 static void kiblnd_query(struct lnet_ni *ni, lnet_nid_t nid,
1049 unsigned long *when)
1051 unsigned long last_alive = 0;
1052 unsigned long now = cfs_time_current();
1053 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1054 struct kib_peer *peer;
1055 unsigned long flags;
1057 read_lock_irqsave(glock, flags);
1059 peer = kiblnd_find_peer_locked(nid);
1061 last_alive = peer->ibp_last_alive;
1063 read_unlock_irqrestore(glock, flags);
1069 * peer is not persistent in hash, trigger peer creation
1070 * and connection establishment with a NULL tx
1073 kiblnd_launch_tx(ni, NULL, nid);
1075 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago\n",
1076 libcfs_nid2str(nid), peer,
1077 last_alive ? cfs_duration_sec(now - last_alive) : -1);
1080 static void kiblnd_free_pages(struct kib_pages *p)
1082 int npages = p->ibp_npages;
1085 for (i = 0; i < npages; i++) {
1086 if (p->ibp_pages[i])
1087 __free_page(p->ibp_pages[i]);
1090 LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1093 int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1095 struct kib_pages *p;
1098 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1099 offsetof(struct kib_pages, ibp_pages[npages]));
1101 CERROR("Can't allocate descriptor for %d pages\n", npages);
1105 memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1106 p->ibp_npages = npages;
1108 for (i = 0; i < npages; i++) {
1109 p->ibp_pages[i] = alloc_pages_node(
1110 cfs_cpt_spread_node(lnet_cpt_table(), cpt),
1112 if (!p->ibp_pages[i]) {
1113 CERROR("Can't allocate page %d of %d\n", i, npages);
1114 kiblnd_free_pages(p);
1123 void kiblnd_unmap_rx_descs(struct kib_conn *conn)
1128 LASSERT(conn->ibc_rxs);
1129 LASSERT(conn->ibc_hdev);
1131 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1132 rx = &conn->ibc_rxs[i];
1134 LASSERT(rx->rx_nob >= 0); /* not posted */
1136 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1137 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1139 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1142 kiblnd_free_pages(conn->ibc_rx_pages);
1144 conn->ibc_rx_pages = NULL;
1147 void kiblnd_map_rx_descs(struct kib_conn *conn)
1155 for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1156 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1157 rx = &conn->ibc_rxs[i];
1160 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1162 rx->rx_msgaddr = kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1166 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1168 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1170 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1171 i, rx->rx_msg, rx->rx_msgaddr,
1172 (__u64)(page_to_phys(pg) + pg_off));
1174 pg_off += IBLND_MSG_SIZE;
1175 LASSERT(pg_off <= PAGE_SIZE);
1177 if (pg_off == PAGE_SIZE) {
1180 LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1185 static void kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1187 struct kib_hca_dev *hdev = tpo->tpo_hdev;
1191 LASSERT(!tpo->tpo_pool.po_allocated);
1196 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1197 tx = &tpo->tpo_tx_descs[i];
1198 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1199 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1201 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1204 kiblnd_hdev_decref(hdev);
1205 tpo->tpo_hdev = NULL;
1208 static struct kib_hca_dev *kiblnd_current_hdev(struct kib_dev *dev)
1210 struct kib_hca_dev *hdev;
1211 unsigned long flags;
1214 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1215 while (dev->ibd_failover) {
1216 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1218 CDEBUG(D_NET, "%s: Wait for failover\n",
1220 set_current_state(TASK_INTERRUPTIBLE);
1221 schedule_timeout(cfs_time_seconds(1) / 100);
1223 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1226 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1227 hdev = dev->ibd_hdev;
1229 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1234 static void kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1236 struct kib_pages *txpgs = tpo->tpo_tx_pages;
1237 struct kib_pool *pool = &tpo->tpo_pool;
1238 struct kib_net *net = pool->po_owner->ps_net;
1239 struct kib_dev *dev;
1250 /* pre-mapped messages are not bigger than 1 page */
1251 BUILD_BUG_ON(IBLND_MSG_SIZE > PAGE_SIZE);
1253 /* No fancy arithmetic when we do the buffer calculations */
1254 BUILD_BUG_ON(PAGE_SIZE % IBLND_MSG_SIZE);
1256 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1258 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1259 page = txpgs->ibp_pages[ipage];
1260 tx = &tpo->tpo_tx_descs[i];
1262 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1265 tx->tx_msgaddr = kiblnd_dma_map_single(
1266 tpo->tpo_hdev->ibh_ibdev, tx->tx_msg,
1267 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1268 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1270 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1272 list_add(&tx->tx_list, &pool->po_free_list);
1274 page_offset += IBLND_MSG_SIZE;
1275 LASSERT(page_offset <= PAGE_SIZE);
1277 if (page_offset == PAGE_SIZE) {
1280 LASSERT(ipage <= txpgs->ibp_npages);
1285 static void kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1287 LASSERT(!fpo->fpo_map_count);
1289 if (fpo->fpo_is_fmr) {
1290 if (fpo->fmr.fpo_fmr_pool)
1291 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1293 struct kib_fast_reg_descriptor *frd, *tmp;
1296 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1298 list_del(&frd->frd_list);
1299 ib_dereg_mr(frd->frd_mr);
1300 LIBCFS_FREE(frd, sizeof(*frd));
1303 if (i < fpo->fast_reg.fpo_pool_size)
1304 CERROR("FastReg pool still has %d regions registered\n",
1305 fpo->fast_reg.fpo_pool_size - i);
1309 kiblnd_hdev_decref(fpo->fpo_hdev);
1311 LIBCFS_FREE(fpo, sizeof(*fpo));
1314 static void kiblnd_destroy_fmr_pool_list(struct list_head *head)
1316 struct kib_fmr_pool *fpo, *tmp;
1318 list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1319 list_del(&fpo->fpo_list);
1320 kiblnd_destroy_fmr_pool(fpo);
1325 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1328 int size = tunables->lnd_fmr_pool_size / ncpts;
1330 return max(IBLND_FMR_POOL, size);
1334 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1337 int size = tunables->lnd_fmr_flush_trigger / ncpts;
1339 return max(IBLND_FMR_POOL_FLUSH, size);
1342 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo)
1344 struct ib_fmr_pool_param param = {
1345 .max_pages_per_fmr = LNET_MAX_PAYLOAD / PAGE_SIZE,
1346 .page_shift = PAGE_SHIFT,
1347 .access = (IB_ACCESS_LOCAL_WRITE |
1348 IB_ACCESS_REMOTE_WRITE),
1349 .pool_size = fps->fps_pool_size,
1350 .dirty_watermark = fps->fps_flush_trigger,
1351 .flush_function = NULL,
1353 .cache = !!fps->fps_cache };
1356 fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1358 if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1359 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1361 CERROR("Failed to create FMR pool: %d\n", rc);
1363 CERROR("FMRs are not supported\n");
1369 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo)
1371 struct kib_fast_reg_descriptor *frd, *tmp;
1374 INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1375 fpo->fast_reg.fpo_pool_size = 0;
1376 for (i = 0; i < fps->fps_pool_size; i++) {
1377 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1380 CERROR("Failed to allocate a new fast_reg descriptor\n");
1385 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1387 LNET_MAX_PAYLOAD / PAGE_SIZE);
1388 if (IS_ERR(frd->frd_mr)) {
1389 rc = PTR_ERR(frd->frd_mr);
1390 CERROR("Failed to allocate ib_alloc_mr: %d\n", rc);
1395 frd->frd_valid = true;
1397 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1398 fpo->fast_reg.fpo_pool_size++;
1405 ib_dereg_mr(frd->frd_mr);
1406 LIBCFS_FREE(frd, sizeof(*frd));
1409 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1411 list_del(&frd->frd_list);
1412 ib_dereg_mr(frd->frd_mr);
1413 LIBCFS_FREE(frd, sizeof(*frd));
1419 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1420 struct kib_fmr_pool **pp_fpo)
1422 struct kib_dev *dev = fps->fps_net->ibn_dev;
1423 struct ib_device_attr *dev_attr;
1424 struct kib_fmr_pool *fpo;
1427 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1431 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1432 dev_attr = &fpo->fpo_hdev->ibh_ibdev->attrs;
1434 /* Check for FMR or FastReg support */
1435 fpo->fpo_is_fmr = 0;
1436 if (fpo->fpo_hdev->ibh_ibdev->alloc_fmr &&
1437 fpo->fpo_hdev->ibh_ibdev->dealloc_fmr &&
1438 fpo->fpo_hdev->ibh_ibdev->map_phys_fmr &&
1439 fpo->fpo_hdev->ibh_ibdev->unmap_fmr) {
1440 LCONSOLE_INFO("Using FMR for registration\n");
1441 fpo->fpo_is_fmr = 1;
1442 } else if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
1443 LCONSOLE_INFO("Using FastReg for registration\n");
1446 LCONSOLE_ERROR_MSG(rc, "IB device does not support FMRs nor FastRegs, can't register memory\n");
1450 if (fpo->fpo_is_fmr)
1451 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1453 rc = kiblnd_alloc_freg_pool(fps, fpo);
1457 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1458 fpo->fpo_owner = fps;
1464 kiblnd_hdev_decref(fpo->fpo_hdev);
1465 LIBCFS_FREE(fpo, sizeof(*fpo));
1469 static void kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps,
1470 struct list_head *zombies)
1472 if (!fps->fps_net) /* initialized? */
1475 spin_lock(&fps->fps_lock);
1477 while (!list_empty(&fps->fps_pool_list)) {
1478 struct kib_fmr_pool *fpo = list_entry(fps->fps_pool_list.next,
1479 struct kib_fmr_pool, fpo_list);
1480 fpo->fpo_failed = 1;
1481 list_del(&fpo->fpo_list);
1482 if (!fpo->fpo_map_count)
1483 list_add(&fpo->fpo_list, zombies);
1485 list_add(&fpo->fpo_list, &fps->fps_failed_pool_list);
1488 spin_unlock(&fps->fps_lock);
1491 static void kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1493 if (fps->fps_net) { /* initialized? */
1494 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1495 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1500 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1501 struct kib_net *net,
1502 struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1504 struct kib_fmr_pool *fpo;
1507 memset(fps, 0, sizeof(*fps));
1512 fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1513 fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1514 fps->fps_cache = tunables->lnd_fmr_cache;
1516 spin_lock_init(&fps->fps_lock);
1517 INIT_LIST_HEAD(&fps->fps_pool_list);
1518 INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1520 rc = kiblnd_create_fmr_pool(fps, &fpo);
1522 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1527 static int kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, unsigned long now)
1529 if (fpo->fpo_map_count) /* still in use */
1531 if (fpo->fpo_failed)
1533 return cfs_time_aftereq(now, fpo->fpo_deadline);
1537 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1539 __u64 *pages = tx->tx_pages;
1540 struct kib_hca_dev *hdev;
1545 hdev = tx->tx_pool->tpo_hdev;
1547 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1548 for (size = 0; size < rd->rd_frags[i].rf_nob;
1549 size += hdev->ibh_page_size) {
1550 pages[npages++] = (rd->rd_frags[i].rf_addr &
1551 hdev->ibh_page_mask) + size;
1558 void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1561 struct kib_fmr_pool *fpo = fmr->fmr_pool;
1562 struct kib_fmr_poolset *fps;
1563 unsigned long now = cfs_time_current();
1564 struct kib_fmr_pool *tmp;
1570 fps = fpo->fpo_owner;
1571 if (fpo->fpo_is_fmr) {
1572 if (fmr->fmr_pfmr) {
1573 rc = ib_fmr_pool_unmap(fmr->fmr_pfmr);
1575 fmr->fmr_pfmr = NULL;
1579 rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1583 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1586 frd->frd_valid = false;
1587 spin_lock(&fps->fps_lock);
1588 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1589 spin_unlock(&fps->fps_lock);
1590 fmr->fmr_frd = NULL;
1593 fmr->fmr_pool = NULL;
1595 spin_lock(&fps->fps_lock);
1596 fpo->fpo_map_count--; /* decref the pool */
1598 list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1599 /* the first pool is persistent */
1600 if (fps->fps_pool_list.next == &fpo->fpo_list)
1603 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1604 list_move(&fpo->fpo_list, &zombies);
1608 spin_unlock(&fps->fps_lock);
1610 if (!list_empty(&zombies))
1611 kiblnd_destroy_fmr_pool_list(&zombies);
1614 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1615 struct kib_rdma_desc *rd, __u32 nob, __u64 iov,
1616 struct kib_fmr *fmr)
1618 __u64 *pages = tx->tx_pages;
1619 bool is_rx = (rd != tx->tx_rd);
1620 bool tx_pages_mapped = false;
1621 struct kib_fmr_pool *fpo;
1627 spin_lock(&fps->fps_lock);
1628 version = fps->fps_version;
1629 list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1630 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1631 fpo->fpo_map_count++;
1633 if (fpo->fpo_is_fmr) {
1634 struct ib_pool_fmr *pfmr;
1636 spin_unlock(&fps->fps_lock);
1638 if (!tx_pages_mapped) {
1639 npages = kiblnd_map_tx_pages(tx, rd);
1640 tx_pages_mapped = 1;
1643 pfmr = ib_fmr_pool_map_phys(fpo->fmr.fpo_fmr_pool,
1644 pages, npages, iov);
1645 if (likely(!IS_ERR(pfmr))) {
1646 fmr->fmr_key = is_rx ? pfmr->fmr->rkey :
1648 fmr->fmr_frd = NULL;
1649 fmr->fmr_pfmr = pfmr;
1650 fmr->fmr_pool = fpo;
1655 if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
1656 struct kib_fast_reg_descriptor *frd;
1657 struct ib_reg_wr *wr;
1661 frd = list_first_entry(&fpo->fast_reg.fpo_pool_list,
1662 struct kib_fast_reg_descriptor,
1664 list_del(&frd->frd_list);
1665 spin_unlock(&fps->fps_lock);
1669 if (!frd->frd_valid) {
1670 __u32 key = is_rx ? mr->rkey : mr->lkey;
1671 struct ib_send_wr *inv_wr;
1673 inv_wr = &frd->frd_inv_wr;
1674 memset(inv_wr, 0, sizeof(*inv_wr));
1675 inv_wr->opcode = IB_WR_LOCAL_INV;
1676 inv_wr->wr_id = IBLND_WID_MR;
1677 inv_wr->ex.invalidate_rkey = key;
1680 key = ib_inc_rkey(key);
1681 ib_update_fast_reg_key(mr, key);
1684 n = ib_map_mr_sg(mr, tx->tx_frags,
1685 tx->tx_nfrags, NULL, PAGE_SIZE);
1686 if (unlikely(n != tx->tx_nfrags)) {
1687 CERROR("Failed to map mr %d/%d elements\n",
1689 return n < 0 ? n : -EINVAL;
1694 /* Prepare FastReg WR */
1695 wr = &frd->frd_fastreg_wr;
1696 memset(wr, 0, sizeof(*wr));
1697 wr->wr.opcode = IB_WR_REG_MR;
1698 wr->wr.wr_id = IBLND_WID_MR;
1700 wr->wr.send_flags = 0;
1702 wr->key = is_rx ? mr->rkey : mr->lkey;
1703 wr->access = (IB_ACCESS_LOCAL_WRITE |
1704 IB_ACCESS_REMOTE_WRITE);
1706 fmr->fmr_key = is_rx ? mr->rkey : mr->lkey;
1708 fmr->fmr_pfmr = NULL;
1709 fmr->fmr_pool = fpo;
1712 spin_unlock(&fps->fps_lock);
1716 spin_lock(&fps->fps_lock);
1717 fpo->fpo_map_count--;
1718 if (rc != -EAGAIN) {
1719 spin_unlock(&fps->fps_lock);
1723 /* EAGAIN and ... */
1724 if (version != fps->fps_version) {
1725 spin_unlock(&fps->fps_lock);
1730 if (fps->fps_increasing) {
1731 spin_unlock(&fps->fps_lock);
1732 CDEBUG(D_NET, "Another thread is allocating new FMR pool, waiting for her to complete\n");
1737 if (time_before(cfs_time_current(), fps->fps_next_retry)) {
1738 /* someone failed recently */
1739 spin_unlock(&fps->fps_lock);
1743 fps->fps_increasing = 1;
1744 spin_unlock(&fps->fps_lock);
1746 CDEBUG(D_NET, "Allocate new FMR pool\n");
1747 rc = kiblnd_create_fmr_pool(fps, &fpo);
1748 spin_lock(&fps->fps_lock);
1749 fps->fps_increasing = 0;
1752 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1754 fps->fps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1756 spin_unlock(&fps->fps_lock);
1761 static void kiblnd_fini_pool(struct kib_pool *pool)
1763 LASSERT(list_empty(&pool->po_free_list));
1764 LASSERT(!pool->po_allocated);
1766 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
1769 static void kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
1771 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
1773 memset(pool, 0, sizeof(*pool));
1774 INIT_LIST_HEAD(&pool->po_free_list);
1775 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1776 pool->po_owner = ps;
1777 pool->po_size = size;
1780 static void kiblnd_destroy_pool_list(struct list_head *head)
1782 struct kib_pool *pool;
1784 while (!list_empty(head)) {
1785 pool = list_entry(head->next, struct kib_pool, po_list);
1786 list_del(&pool->po_list);
1788 LASSERT(pool->po_owner);
1789 pool->po_owner->ps_pool_destroy(pool);
1793 static void kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
1795 if (!ps->ps_net) /* initialized? */
1798 spin_lock(&ps->ps_lock);
1799 while (!list_empty(&ps->ps_pool_list)) {
1800 struct kib_pool *po = list_entry(ps->ps_pool_list.next,
1801 struct kib_pool, po_list);
1803 list_del(&po->po_list);
1804 if (!po->po_allocated)
1805 list_add(&po->po_list, zombies);
1807 list_add(&po->po_list, &ps->ps_failed_pool_list);
1809 spin_unlock(&ps->ps_lock);
1812 static void kiblnd_fini_poolset(struct kib_poolset *ps)
1814 if (ps->ps_net) { /* initialized? */
1815 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
1816 kiblnd_destroy_pool_list(&ps->ps_pool_list);
1820 static int kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
1821 struct kib_net *net, char *name, int size,
1822 kib_ps_pool_create_t po_create,
1823 kib_ps_pool_destroy_t po_destroy,
1824 kib_ps_node_init_t nd_init,
1825 kib_ps_node_fini_t nd_fini)
1827 struct kib_pool *pool;
1830 memset(ps, 0, sizeof(*ps));
1834 ps->ps_pool_create = po_create;
1835 ps->ps_pool_destroy = po_destroy;
1836 ps->ps_node_init = nd_init;
1837 ps->ps_node_fini = nd_fini;
1838 ps->ps_pool_size = size;
1839 if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name))
1840 >= sizeof(ps->ps_name))
1842 spin_lock_init(&ps->ps_lock);
1843 INIT_LIST_HEAD(&ps->ps_pool_list);
1844 INIT_LIST_HEAD(&ps->ps_failed_pool_list);
1846 rc = ps->ps_pool_create(ps, size, &pool);
1848 list_add(&pool->po_list, &ps->ps_pool_list);
1850 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
1855 static int kiblnd_pool_is_idle(struct kib_pool *pool, unsigned long now)
1857 if (pool->po_allocated) /* still in use */
1859 if (pool->po_failed)
1861 return cfs_time_aftereq(now, pool->po_deadline);
1864 void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
1867 struct kib_poolset *ps = pool->po_owner;
1868 struct kib_pool *tmp;
1869 unsigned long now = cfs_time_current();
1871 spin_lock(&ps->ps_lock);
1873 if (ps->ps_node_fini)
1874 ps->ps_node_fini(pool, node);
1876 LASSERT(pool->po_allocated > 0);
1877 list_add(node, &pool->po_free_list);
1878 pool->po_allocated--;
1880 list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
1881 /* the first pool is persistent */
1882 if (ps->ps_pool_list.next == &pool->po_list)
1885 if (kiblnd_pool_is_idle(pool, now))
1886 list_move(&pool->po_list, &zombies);
1888 spin_unlock(&ps->ps_lock);
1890 if (!list_empty(&zombies))
1891 kiblnd_destroy_pool_list(&zombies);
1894 struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps)
1896 struct list_head *node;
1897 struct kib_pool *pool;
1898 unsigned int interval = 1;
1899 unsigned long time_before;
1900 unsigned int trips = 0;
1904 spin_lock(&ps->ps_lock);
1905 list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
1906 if (list_empty(&pool->po_free_list))
1909 pool->po_allocated++;
1910 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1911 node = pool->po_free_list.next;
1914 if (ps->ps_node_init) {
1915 /* still hold the lock */
1916 ps->ps_node_init(pool, node);
1918 spin_unlock(&ps->ps_lock);
1922 /* no available tx pool and ... */
1923 if (ps->ps_increasing) {
1924 /* another thread is allocating a new pool */
1925 spin_unlock(&ps->ps_lock);
1927 CDEBUG(D_NET, "Another thread is allocating new %s pool, waiting %d HZs for her to complete. trips = %d\n",
1928 ps->ps_name, interval, trips);
1930 set_current_state(TASK_INTERRUPTIBLE);
1931 schedule_timeout(interval);
1932 if (interval < cfs_time_seconds(1))
1938 if (time_before(cfs_time_current(), ps->ps_next_retry)) {
1939 /* someone failed recently */
1940 spin_unlock(&ps->ps_lock);
1944 ps->ps_increasing = 1;
1945 spin_unlock(&ps->ps_lock);
1947 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
1948 time_before = cfs_time_current();
1949 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
1950 CDEBUG(D_NET, "ps_pool_create took %lu HZ to complete",
1951 cfs_time_current() - time_before);
1953 spin_lock(&ps->ps_lock);
1954 ps->ps_increasing = 0;
1956 list_add_tail(&pool->po_list, &ps->ps_pool_list);
1958 ps->ps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1959 CERROR("Can't allocate new %s pool because out of memory\n",
1962 spin_unlock(&ps->ps_lock);
1967 static void kiblnd_destroy_tx_pool(struct kib_pool *pool)
1969 struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool, tpo_pool);
1972 LASSERT(!pool->po_allocated);
1974 if (tpo->tpo_tx_pages) {
1975 kiblnd_unmap_tx_pool(tpo);
1976 kiblnd_free_pages(tpo->tpo_tx_pages);
1979 if (!tpo->tpo_tx_descs)
1982 for (i = 0; i < pool->po_size; i++) {
1983 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
1985 list_del(&tx->tx_list);
1987 LIBCFS_FREE(tx->tx_pages,
1989 sizeof(*tx->tx_pages));
1991 LIBCFS_FREE(tx->tx_frags,
1992 (1 + IBLND_MAX_RDMA_FRAGS) *
1993 sizeof(*tx->tx_frags));
1995 LIBCFS_FREE(tx->tx_wrq,
1996 (1 + IBLND_MAX_RDMA_FRAGS) *
1997 sizeof(*tx->tx_wrq));
1999 LIBCFS_FREE(tx->tx_sge,
2000 (1 + IBLND_MAX_RDMA_FRAGS) *
2001 sizeof(*tx->tx_sge));
2003 LIBCFS_FREE(tx->tx_rd,
2004 offsetof(struct kib_rdma_desc,
2005 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2008 LIBCFS_FREE(tpo->tpo_tx_descs,
2009 pool->po_size * sizeof(struct kib_tx));
2011 kiblnd_fini_pool(pool);
2012 LIBCFS_FREE(tpo, sizeof(*tpo));
2015 static int kiblnd_tx_pool_size(int ncpts)
2017 int ntx = *kiblnd_tunables.kib_ntx / ncpts;
2019 return max(IBLND_TX_POOL, ntx);
2022 static int kiblnd_create_tx_pool(struct kib_poolset *ps, int size,
2023 struct kib_pool **pp_po)
2027 struct kib_pool *pool;
2028 struct kib_tx_pool *tpo;
2030 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2032 CERROR("Failed to allocate TX pool\n");
2036 pool = &tpo->tpo_pool;
2037 kiblnd_init_pool(ps, pool, size);
2038 tpo->tpo_tx_descs = NULL;
2039 tpo->tpo_tx_pages = NULL;
2041 npg = DIV_ROUND_UP(size * IBLND_MSG_SIZE, PAGE_SIZE);
2042 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg)) {
2043 CERROR("Can't allocate tx pages: %d\n", npg);
2044 LIBCFS_FREE(tpo, sizeof(*tpo));
2048 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2049 size * sizeof(struct kib_tx));
2050 if (!tpo->tpo_tx_descs) {
2051 CERROR("Can't allocate %d tx descriptors\n", size);
2052 ps->ps_pool_destroy(pool);
2056 memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2058 for (i = 0; i < size; i++) {
2059 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2062 if (ps->ps_net->ibn_fmr_ps) {
2063 LIBCFS_CPT_ALLOC(tx->tx_pages,
2064 lnet_cpt_table(), ps->ps_cpt,
2065 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2070 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2071 (1 + IBLND_MAX_RDMA_FRAGS) *
2072 sizeof(*tx->tx_frags));
2076 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS + 1);
2078 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2079 (1 + IBLND_MAX_RDMA_FRAGS) *
2080 sizeof(*tx->tx_wrq));
2084 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2085 (1 + IBLND_MAX_RDMA_FRAGS) *
2086 sizeof(*tx->tx_sge));
2090 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2091 offsetof(struct kib_rdma_desc,
2092 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2098 kiblnd_map_tx_pool(tpo);
2103 ps->ps_pool_destroy(pool);
2107 static void kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2109 struct kib_tx_poolset *tps = container_of(pool->po_owner,
2110 struct kib_tx_poolset,
2112 struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list);
2114 tx->tx_cookie = tps->tps_next_tx_cookie++;
2117 static void kiblnd_net_fini_pools(struct kib_net *net)
2121 cfs_cpt_for_each(i, lnet_cpt_table()) {
2122 struct kib_tx_poolset *tps;
2123 struct kib_fmr_poolset *fps;
2125 if (net->ibn_tx_ps) {
2126 tps = net->ibn_tx_ps[i];
2127 kiblnd_fini_poolset(&tps->tps_poolset);
2130 if (net->ibn_fmr_ps) {
2131 fps = net->ibn_fmr_ps[i];
2132 kiblnd_fini_fmr_poolset(fps);
2136 if (net->ibn_tx_ps) {
2137 cfs_percpt_free(net->ibn_tx_ps);
2138 net->ibn_tx_ps = NULL;
2141 if (net->ibn_fmr_ps) {
2142 cfs_percpt_free(net->ibn_fmr_ps);
2143 net->ibn_fmr_ps = NULL;
2147 static int kiblnd_net_init_pools(struct kib_net *net, struct lnet_ni *ni,
2148 __u32 *cpts, int ncpts)
2150 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2155 tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib;
2157 if (tunables->lnd_fmr_pool_size < *kiblnd_tunables.kib_ntx / 4) {
2158 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2159 tunables->lnd_fmr_pool_size,
2160 *kiblnd_tunables.kib_ntx / 4);
2166 * TX pool must be created later than FMR, see LU-2268
2169 LASSERT(!net->ibn_tx_ps);
2172 * premapping can fail if ibd_nmr > 1, so we always create
2173 * FMR pool and map-on-demand if premapping failed
2175 * cfs_precpt_alloc is creating an array of struct kib_fmr_poolset
2176 * The number of struct kib_fmr_poolsets create is equal to the
2177 * number of CPTs that exist, i.e net->ibn_fmr_ps[cpt].
2179 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2180 sizeof(struct kib_fmr_poolset));
2181 if (!net->ibn_fmr_ps) {
2182 CERROR("Failed to allocate FMR pool array\n");
2187 for (i = 0; i < ncpts; i++) {
2188 cpt = !cpts ? i : cpts[i];
2189 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2192 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2199 LASSERT(i == ncpts);
2202 * cfs_precpt_alloc is creating an array of struct kib_tx_poolset
2203 * The number of struct kib_tx_poolsets create is equal to the
2204 * number of CPTs that exist, i.e net->ibn_tx_ps[cpt].
2206 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2207 sizeof(struct kib_tx_poolset));
2208 if (!net->ibn_tx_ps) {
2209 CERROR("Failed to allocate tx pool array\n");
2214 for (i = 0; i < ncpts; i++) {
2215 cpt = !cpts ? i : cpts[i];
2216 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2218 kiblnd_tx_pool_size(ncpts),
2219 kiblnd_create_tx_pool,
2220 kiblnd_destroy_tx_pool,
2221 kiblnd_tx_init, NULL);
2223 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2231 kiblnd_net_fini_pools(net);
2236 static int kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2239 * It's safe to assume a HCA can handle a page size
2240 * matching that of the native system
2242 hdev->ibh_page_shift = PAGE_SHIFT;
2243 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2244 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2246 hdev->ibh_mr_size = hdev->ibh_ibdev->attrs.max_mr_size;
2247 if (hdev->ibh_mr_size == ~0ULL) {
2248 hdev->ibh_mr_shift = 64;
2252 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2256 void kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2259 ib_dealloc_pd(hdev->ibh_pd);
2262 rdma_destroy_id(hdev->ibh_cmid);
2264 LIBCFS_FREE(hdev, sizeof(*hdev));
2268 static int kiblnd_dummy_callback(struct rdma_cm_id *cmid,
2269 struct rdma_cm_event *event)
2274 static int kiblnd_dev_need_failover(struct kib_dev *dev)
2276 struct rdma_cm_id *cmid;
2277 struct sockaddr_in srcaddr;
2278 struct sockaddr_in dstaddr;
2281 if (!dev->ibd_hdev || /* initializing */
2282 !dev->ibd_hdev->ibh_cmid || /* listener is dead */
2283 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2287 * XXX: it's UGLY, but I don't have better way to find
2288 * ib-bonding HCA failover because:
2290 * a. no reliable CM event for HCA failover...
2291 * b. no OFED API to get ib_device for current net_device...
2293 * We have only two choices at this point:
2295 * a. rdma_bind_addr(), it will conflict with listener cmid
2296 * b. rdma_resolve_addr() to zero addr
2298 cmid = kiblnd_rdma_create_id(kiblnd_dummy_callback, dev, RDMA_PS_TCP,
2302 CERROR("Failed to create cmid for failover: %d\n", rc);
2306 memset(&srcaddr, 0, sizeof(srcaddr));
2307 srcaddr.sin_family = AF_INET;
2308 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
2310 memset(&dstaddr, 0, sizeof(dstaddr));
2311 dstaddr.sin_family = AF_INET;
2312 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2313 (struct sockaddr *)&dstaddr, 1);
2314 if (rc || !cmid->device) {
2315 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2316 dev->ibd_ifname, &dev->ibd_ifip,
2318 rdma_destroy_id(cmid);
2322 rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2323 rdma_destroy_id(cmid);
2328 int kiblnd_dev_failover(struct kib_dev *dev)
2330 LIST_HEAD(zombie_tpo);
2331 LIST_HEAD(zombie_ppo);
2332 LIST_HEAD(zombie_fpo);
2333 struct rdma_cm_id *cmid = NULL;
2334 struct kib_hca_dev *hdev = NULL;
2336 struct kib_net *net;
2337 struct sockaddr_in addr;
2338 unsigned long flags;
2342 LASSERT(*kiblnd_tunables.kib_dev_failover > 1 ||
2343 dev->ibd_can_failover || !dev->ibd_hdev);
2345 rc = kiblnd_dev_need_failover(dev);
2349 if (dev->ibd_hdev &&
2350 dev->ibd_hdev->ibh_cmid) {
2352 * XXX it's not good to close old listener at here,
2353 * because we can fail to create new listener.
2354 * But we have to close it now, otherwise rdma_bind_addr
2355 * will return EADDRINUSE... How crap!
2357 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2359 cmid = dev->ibd_hdev->ibh_cmid;
2361 * make next schedule of kiblnd_dev_need_failover()
2364 dev->ibd_hdev->ibh_cmid = NULL;
2365 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2367 rdma_destroy_id(cmid);
2370 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, dev, RDMA_PS_TCP,
2374 CERROR("Failed to create cmid for failover: %d\n", rc);
2378 memset(&addr, 0, sizeof(addr));
2379 addr.sin_family = AF_INET;
2380 addr.sin_addr.s_addr = htonl(dev->ibd_ifip);
2381 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2383 /* Bind to failover device or port */
2384 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2385 if (rc || !cmid->device) {
2386 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2387 dev->ibd_ifname, &dev->ibd_ifip,
2389 rdma_destroy_id(cmid);
2393 LIBCFS_ALLOC(hdev, sizeof(*hdev));
2395 CERROR("Failed to allocate kib_hca_dev\n");
2396 rdma_destroy_id(cmid);
2401 atomic_set(&hdev->ibh_ref, 1);
2402 hdev->ibh_dev = dev;
2403 hdev->ibh_cmid = cmid;
2404 hdev->ibh_ibdev = cmid->device;
2406 pd = ib_alloc_pd(cmid->device, 0);
2409 CERROR("Can't allocate PD: %d\n", rc);
2415 rc = rdma_listen(cmid, 0);
2417 CERROR("Can't start new listener: %d\n", rc);
2421 rc = kiblnd_hdev_get_attr(hdev);
2423 CERROR("Can't get device attributes: %d\n", rc);
2427 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2429 swap(dev->ibd_hdev, hdev); /* take over the refcount */
2431 list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
2432 cfs_cpt_for_each(i, lnet_cpt_table()) {
2433 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
2436 if (net->ibn_fmr_ps)
2437 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
2442 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2444 if (!list_empty(&zombie_tpo))
2445 kiblnd_destroy_pool_list(&zombie_tpo);
2446 if (!list_empty(&zombie_ppo))
2447 kiblnd_destroy_pool_list(&zombie_ppo);
2448 if (!list_empty(&zombie_fpo))
2449 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
2451 kiblnd_hdev_decref(hdev);
2454 dev->ibd_failed_failover++;
2456 dev->ibd_failed_failover = 0;
2461 void kiblnd_destroy_dev(struct kib_dev *dev)
2463 LASSERT(!dev->ibd_nnets);
2464 LASSERT(list_empty(&dev->ibd_nets));
2466 list_del(&dev->ibd_fail_list);
2467 list_del(&dev->ibd_list);
2470 kiblnd_hdev_decref(dev->ibd_hdev);
2472 LIBCFS_FREE(dev, sizeof(*dev));
2475 static struct kib_dev *kiblnd_create_dev(char *ifname)
2477 struct net_device *netdev;
2478 struct kib_dev *dev;
2484 rc = lnet_ipif_query(ifname, &up, &ip, &netmask);
2486 CERROR("Can't query IPoIB interface %s: %d\n",
2492 CERROR("Can't query IPoIB interface %s: it's down\n", ifname);
2496 LIBCFS_ALLOC(dev, sizeof(*dev));
2500 netdev = dev_get_by_name(&init_net, ifname);
2502 dev->ibd_can_failover = 0;
2504 dev->ibd_can_failover = !!(netdev->flags & IFF_MASTER);
2508 INIT_LIST_HEAD(&dev->ibd_nets);
2509 INIT_LIST_HEAD(&dev->ibd_list); /* not yet in kib_devs */
2510 INIT_LIST_HEAD(&dev->ibd_fail_list);
2512 strcpy(&dev->ibd_ifname[0], ifname);
2514 /* initialize the device */
2515 rc = kiblnd_dev_failover(dev);
2517 CERROR("Can't initialize device: %d\n", rc);
2518 LIBCFS_FREE(dev, sizeof(*dev));
2522 list_add_tail(&dev->ibd_list, &kiblnd_data.kib_devs);
2526 static void kiblnd_base_shutdown(void)
2528 struct kib_sched_info *sched;
2531 LASSERT(list_empty(&kiblnd_data.kib_devs));
2533 switch (kiblnd_data.kib_init) {
2537 case IBLND_INIT_ALL:
2538 case IBLND_INIT_DATA:
2539 LASSERT(kiblnd_data.kib_peers);
2540 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
2541 LASSERT(list_empty(&kiblnd_data.kib_peers[i]));
2542 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
2543 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
2544 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
2545 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
2547 /* flag threads to terminate; wake and wait for them to die */
2548 kiblnd_data.kib_shutdown = 1;
2551 * NB: we really want to stop scheduler threads net by net
2552 * instead of the whole module, this should be improved
2553 * with dynamic configuration LNet
2555 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
2556 wake_up_all(&sched->ibs_waitq);
2558 wake_up_all(&kiblnd_data.kib_connd_waitq);
2559 wake_up_all(&kiblnd_data.kib_failover_waitq);
2562 while (atomic_read(&kiblnd_data.kib_nthreads)) {
2565 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2566 "Waiting for %d threads to terminate\n",
2567 atomic_read(&kiblnd_data.kib_nthreads));
2568 set_current_state(TASK_UNINTERRUPTIBLE);
2569 schedule_timeout(cfs_time_seconds(1));
2574 case IBLND_INIT_NOTHING:
2578 if (kiblnd_data.kib_peers) {
2579 LIBCFS_FREE(kiblnd_data.kib_peers,
2580 sizeof(struct list_head) *
2581 kiblnd_data.kib_peer_hash_size);
2584 if (kiblnd_data.kib_scheds)
2585 cfs_percpt_free(kiblnd_data.kib_scheds);
2587 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
2588 module_put(THIS_MODULE);
2591 static void kiblnd_shutdown(struct lnet_ni *ni)
2593 struct kib_net *net = ni->ni_data;
2594 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2596 unsigned long flags;
2598 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
2603 write_lock_irqsave(g_lock, flags);
2604 net->ibn_shutdown = 1;
2605 write_unlock_irqrestore(g_lock, flags);
2607 switch (net->ibn_init) {
2611 case IBLND_INIT_ALL:
2612 /* nuke all existing peers within this net */
2613 kiblnd_del_peer(ni, LNET_NID_ANY);
2615 /* Wait for all peer state to clean up */
2617 while (atomic_read(&net->ibn_npeers)) {
2619 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* 2**n? */
2620 "%s: waiting for %d peers to disconnect\n",
2621 libcfs_nid2str(ni->ni_nid),
2622 atomic_read(&net->ibn_npeers));
2623 set_current_state(TASK_UNINTERRUPTIBLE);
2624 schedule_timeout(cfs_time_seconds(1));
2627 kiblnd_net_fini_pools(net);
2629 write_lock_irqsave(g_lock, flags);
2630 LASSERT(net->ibn_dev->ibd_nnets > 0);
2631 net->ibn_dev->ibd_nnets--;
2632 list_del(&net->ibn_list);
2633 write_unlock_irqrestore(g_lock, flags);
2637 case IBLND_INIT_NOTHING:
2638 LASSERT(!atomic_read(&net->ibn_nconns));
2640 if (net->ibn_dev && !net->ibn_dev->ibd_nnets)
2641 kiblnd_destroy_dev(net->ibn_dev);
2646 net->ibn_init = IBLND_INIT_NOTHING;
2649 LIBCFS_FREE(net, sizeof(*net));
2652 if (list_empty(&kiblnd_data.kib_devs))
2653 kiblnd_base_shutdown();
2656 static int kiblnd_base_startup(void)
2658 struct kib_sched_info *sched;
2662 LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
2664 try_module_get(THIS_MODULE);
2665 /* zero pointers, flags etc */
2666 memset(&kiblnd_data, 0, sizeof(kiblnd_data));
2668 rwlock_init(&kiblnd_data.kib_global_lock);
2670 INIT_LIST_HEAD(&kiblnd_data.kib_devs);
2671 INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
2673 kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
2674 LIBCFS_ALLOC(kiblnd_data.kib_peers,
2675 sizeof(struct list_head) * kiblnd_data.kib_peer_hash_size);
2676 if (!kiblnd_data.kib_peers)
2678 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
2679 INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]);
2681 spin_lock_init(&kiblnd_data.kib_connd_lock);
2682 INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
2683 INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
2684 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
2685 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
2687 init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
2688 init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
2690 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
2692 if (!kiblnd_data.kib_scheds)
2695 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
2698 spin_lock_init(&sched->ibs_lock);
2699 INIT_LIST_HEAD(&sched->ibs_conns);
2700 init_waitqueue_head(&sched->ibs_waitq);
2702 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2703 if (*kiblnd_tunables.kib_nscheds > 0) {
2704 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
2707 * max to half of CPUs, another half is reserved for
2708 * upper layer modules
2710 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
2713 sched->ibs_nthreads_max = nthrs;
2717 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
2719 /* lists/ptrs/locks initialised */
2720 kiblnd_data.kib_init = IBLND_INIT_DATA;
2721 /*****************************************************/
2723 rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
2725 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
2729 if (*kiblnd_tunables.kib_dev_failover)
2730 rc = kiblnd_thread_start(kiblnd_failover_thread, NULL,
2734 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
2738 /* flag everything initialised */
2739 kiblnd_data.kib_init = IBLND_INIT_ALL;
2740 /*****************************************************/
2745 kiblnd_base_shutdown();
2749 static int kiblnd_start_schedulers(struct kib_sched_info *sched)
2755 if (!sched->ibs_nthreads) {
2756 if (*kiblnd_tunables.kib_nscheds > 0) {
2757 nthrs = sched->ibs_nthreads_max;
2759 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2761 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
2762 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
2765 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
2766 /* increase one thread if there is new interface */
2767 nthrs = sched->ibs_nthreads < sched->ibs_nthreads_max;
2770 for (i = 0; i < nthrs; i++) {
2774 id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
2775 snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld",
2776 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
2777 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id, name);
2781 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2782 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
2786 sched->ibs_nthreads += i;
2790 static int kiblnd_dev_start_threads(struct kib_dev *dev, int newdev, __u32 *cpts,
2797 for (i = 0; i < ncpts; i++) {
2798 struct kib_sched_info *sched;
2800 cpt = !cpts ? i : cpts[i];
2801 sched = kiblnd_data.kib_scheds[cpt];
2803 if (!newdev && sched->ibs_nthreads > 0)
2806 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
2808 CERROR("Failed to start scheduler threads for %s\n",
2816 static struct kib_dev *kiblnd_dev_search(char *ifname)
2818 struct kib_dev *alias = NULL;
2819 struct kib_dev *dev;
2823 colon = strchr(ifname, ':');
2824 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
2825 if (!strcmp(&dev->ibd_ifname[0], ifname))
2831 colon2 = strchr(dev->ibd_ifname, ':');
2837 if (!strcmp(&dev->ibd_ifname[0], ifname))
2848 static int kiblnd_startup(struct lnet_ni *ni)
2851 struct kib_dev *ibdev = NULL;
2852 struct kib_net *net;
2853 struct timespec64 tv;
2854 unsigned long flags;
2858 LASSERT(ni->ni_lnd == &the_o2iblnd);
2860 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
2861 rc = kiblnd_base_startup();
2866 LIBCFS_ALLOC(net, sizeof(*net));
2871 ktime_get_real_ts64(&tv);
2872 net->ibn_incarnation = tv.tv_sec * USEC_PER_SEC +
2873 tv.tv_nsec / NSEC_PER_USEC;
2875 rc = kiblnd_tunables_setup(ni);
2879 if (ni->ni_interfaces[0]) {
2880 /* Use the IPoIB interface specified in 'networks=' */
2882 BUILD_BUG_ON(LNET_MAX_INTERFACES <= 1);
2883 if (ni->ni_interfaces[1]) {
2884 CERROR("Multiple interfaces not supported\n");
2888 ifname = ni->ni_interfaces[0];
2890 ifname = *kiblnd_tunables.kib_default_ipif;
2893 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
2894 CERROR("IPoIB interface name too long: %s\n", ifname);
2898 ibdev = kiblnd_dev_search(ifname);
2901 /* hmm...create kib_dev even for alias */
2902 if (!ibdev || strcmp(&ibdev->ibd_ifname[0], ifname))
2903 ibdev = kiblnd_create_dev(ifname);
2908 net->ibn_dev = ibdev;
2909 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip);
2911 rc = kiblnd_dev_start_threads(ibdev, newdev,
2912 ni->ni_cpts, ni->ni_ncpts);
2916 rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
2918 CERROR("Failed to initialize NI pools: %d\n", rc);
2922 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2924 list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
2925 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2927 net->ibn_init = IBLND_INIT_ALL;
2932 if (!net->ibn_dev && ibdev)
2933 kiblnd_destroy_dev(ibdev);
2936 kiblnd_shutdown(ni);
2938 CDEBUG(D_NET, "%s failed\n", __func__);
2942 static struct lnet_lnd the_o2iblnd = {
2943 .lnd_type = O2IBLND,
2944 .lnd_startup = kiblnd_startup,
2945 .lnd_shutdown = kiblnd_shutdown,
2946 .lnd_ctl = kiblnd_ctl,
2947 .lnd_query = kiblnd_query,
2948 .lnd_send = kiblnd_send,
2949 .lnd_recv = kiblnd_recv,
2952 static void __exit ko2iblnd_exit(void)
2954 lnet_unregister_lnd(&the_o2iblnd);
2957 static int __init ko2iblnd_init(void)
2959 BUILD_BUG_ON(sizeof(struct kib_msg) > IBLND_MSG_SIZE);
2960 BUILD_BUG_ON(offsetof(struct kib_msg,
2961 ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
2963 BUILD_BUG_ON(offsetof(struct kib_msg,
2964 ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
2967 kiblnd_tunables_init();
2969 lnet_register_lnd(&the_o2iblnd);
2974 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2975 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
2976 MODULE_VERSION("2.7.0");
2977 MODULE_LICENSE("GPL");
2979 module_init(ko2iblnd_init);
2980 module_exit(ko2iblnd_exit);