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 lnd_t 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;
135 LASSERT(msg->ibm_type == IBLND_MSG_GET_REQ ||
136 msg->ibm_type == IBLND_MSG_PUT_ACK);
138 rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
139 &msg->ibm_u.get.ibgm_rd :
140 &msg->ibm_u.putack.ibpam_rd;
143 __swab32s(&rd->rd_key);
144 __swab32s(&rd->rd_nfrags);
149 if (n <= 0 || n > IBLND_MAX_RDMA_FRAGS) {
150 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
151 n, IBLND_MAX_RDMA_FRAGS);
155 nob = offsetof(struct kib_msg, ibm_u) +
156 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
158 if (msg->ibm_nob < nob) {
159 CERROR("Short %s: %d(%d)\n",
160 kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
167 for (i = 0; i < n; i++) {
168 __swab32s(&rd->rd_frags[i].rf_nob);
169 __swab64s(&rd->rd_frags[i].rf_addr);
175 void kiblnd_pack_msg(lnet_ni_t *ni, struct kib_msg *msg, int version,
176 int credits, lnet_nid_t dstnid, __u64 dststamp)
178 struct kib_net *net = ni->ni_data;
181 * CAVEAT EMPTOR! all message fields not set here should have been
182 * initialised previously.
184 msg->ibm_magic = IBLND_MSG_MAGIC;
185 msg->ibm_version = version;
187 msg->ibm_credits = credits;
190 msg->ibm_srcnid = ni->ni_nid;
191 msg->ibm_srcstamp = net->ibn_incarnation;
192 msg->ibm_dstnid = dstnid;
193 msg->ibm_dststamp = dststamp;
195 if (*kiblnd_tunables.kib_cksum) {
196 /* NB ibm_cksum zero while computing cksum */
197 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
201 int kiblnd_unpack_msg(struct kib_msg *msg, int nob)
203 const int hdr_size = offsetof(struct kib_msg, ibm_u);
209 /* 6 bytes are enough to have received magic + version */
211 CERROR("Short message: %d\n", nob);
215 if (msg->ibm_magic == IBLND_MSG_MAGIC) {
217 } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
220 CERROR("Bad magic: %08x\n", msg->ibm_magic);
224 version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
225 if (version != IBLND_MSG_VERSION &&
226 version != IBLND_MSG_VERSION_1) {
227 CERROR("Bad version: %x\n", version);
231 if (nob < hdr_size) {
232 CERROR("Short message: %d\n", nob);
236 msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
238 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
243 * checksum must be computed with ibm_cksum zero and BEFORE anything
246 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
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 endianness */
258 msg->ibm_version = version;
259 CLASSERT(sizeof(msg->ibm_type) == 1);
260 CLASSERT(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);
314 int kiblnd_create_peer(lnet_ni_t *ni, struct kib_peer **peerp, lnet_nid_t nid)
316 struct kib_peer *peer;
317 struct kib_net *net = ni->ni_data;
318 int cpt = lnet_cpt_of_nid(nid);
322 LASSERT(nid != LNET_NID_ANY);
324 LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer));
326 CERROR("Cannot allocate peer\n");
333 peer->ibp_last_alive = 0;
334 peer->ibp_max_frags = kiblnd_cfg_rdma_frags(peer->ibp_ni);
335 peer->ibp_queue_depth = ni->ni_peertxcredits;
336 atomic_set(&peer->ibp_refcount, 1); /* 1 ref for caller */
338 INIT_LIST_HEAD(&peer->ibp_list); /* not in the peer table yet */
339 INIT_LIST_HEAD(&peer->ibp_conns);
340 INIT_LIST_HEAD(&peer->ibp_tx_queue);
342 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
344 /* always called with a ref on ni, which prevents ni being shutdown */
345 LASSERT(!net->ibn_shutdown);
347 /* npeers only grows with the global lock held */
348 atomic_inc(&net->ibn_npeers);
350 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
356 void kiblnd_destroy_peer(struct kib_peer *peer)
358 struct kib_net *net = peer->ibp_ni->ni_data;
361 LASSERT(!atomic_read(&peer->ibp_refcount));
362 LASSERT(!kiblnd_peer_active(peer));
363 LASSERT(kiblnd_peer_idle(peer));
364 LASSERT(list_empty(&peer->ibp_tx_queue));
366 LIBCFS_FREE(peer, sizeof(*peer));
369 * NB a peer's connections keep a reference on their peer until
370 * they are destroyed, so we can be assured that _all_ state to do
371 * with this peer has been cleaned up when its refcount drops to
374 atomic_dec(&net->ibn_npeers);
377 struct kib_peer *kiblnd_find_peer_locked(lnet_nid_t nid)
380 * the caller is responsible for accounting the additional reference
383 struct list_head *peer_list = kiblnd_nid2peerlist(nid);
384 struct list_head *tmp;
385 struct kib_peer *peer;
387 list_for_each(tmp, peer_list) {
388 peer = list_entry(tmp, struct kib_peer, ibp_list);
389 LASSERT(!kiblnd_peer_idle(peer));
391 if (peer->ibp_nid != nid)
394 CDEBUG(D_NET, "got peer [%p] -> %s (%d) version: %x\n",
395 peer, libcfs_nid2str(nid),
396 atomic_read(&peer->ibp_refcount),
403 void kiblnd_unlink_peer_locked(struct kib_peer *peer)
405 LASSERT(list_empty(&peer->ibp_conns));
407 LASSERT(kiblnd_peer_active(peer));
408 list_del_init(&peer->ibp_list);
409 /* lose peerlist's ref */
410 kiblnd_peer_decref(peer);
413 static int kiblnd_get_peer_info(lnet_ni_t *ni, int index,
414 lnet_nid_t *nidp, int *count)
416 struct kib_peer *peer;
417 struct list_head *ptmp;
421 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
423 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
424 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
425 peer = list_entry(ptmp, struct kib_peer, ibp_list);
426 LASSERT(!kiblnd_peer_idle(peer));
428 if (peer->ibp_ni != ni)
434 *nidp = peer->ibp_nid;
435 *count = atomic_read(&peer->ibp_refcount);
437 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
443 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
447 static void kiblnd_del_peer_locked(struct kib_peer *peer)
449 struct list_head *ctmp;
450 struct list_head *cnxt;
451 struct kib_conn *conn;
453 if (list_empty(&peer->ibp_conns)) {
454 kiblnd_unlink_peer_locked(peer);
456 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
457 conn = list_entry(ctmp, struct kib_conn, ibc_list);
459 kiblnd_close_conn_locked(conn, 0);
461 /* NB closing peer's last conn unlinked it. */
464 * NB peer now unlinked; might even be freed if the peer table had the
469 static int kiblnd_del_peer(lnet_ni_t *ni, lnet_nid_t nid)
472 struct list_head *ptmp;
473 struct list_head *pnxt;
474 struct kib_peer *peer;
481 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
483 if (nid != LNET_NID_ANY) {
484 lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
485 hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
488 hi = kiblnd_data.kib_peer_hash_size - 1;
491 for (i = lo; i <= hi; i++) {
492 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
493 peer = list_entry(ptmp, struct kib_peer, ibp_list);
494 LASSERT(!kiblnd_peer_idle(peer));
496 if (peer->ibp_ni != ni)
499 if (!(nid == LNET_NID_ANY || peer->ibp_nid == nid))
502 if (!list_empty(&peer->ibp_tx_queue)) {
503 LASSERT(list_empty(&peer->ibp_conns));
505 list_splice_init(&peer->ibp_tx_queue,
509 kiblnd_del_peer_locked(peer);
510 rc = 0; /* matched something */
514 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
516 kiblnd_txlist_done(ni, &zombies, -EIO);
521 static struct kib_conn *kiblnd_get_conn_by_idx(lnet_ni_t *ni, int index)
523 struct kib_peer *peer;
524 struct list_head *ptmp;
525 struct kib_conn *conn;
526 struct list_head *ctmp;
530 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
532 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
533 list_for_each(ptmp, &kiblnd_data.kib_peers[i]) {
534 peer = list_entry(ptmp, struct kib_peer, ibp_list);
535 LASSERT(!kiblnd_peer_idle(peer));
537 if (peer->ibp_ni != ni)
540 list_for_each(ctmp, &peer->ibp_conns) {
544 conn = list_entry(ctmp, struct kib_conn,
546 kiblnd_conn_addref(conn);
547 read_unlock_irqrestore(
548 &kiblnd_data.kib_global_lock,
555 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
559 int kiblnd_translate_mtu(int value)
579 static void kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
583 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
584 if (!cmid->route.path_rec)
587 mtu = kiblnd_translate_mtu(*kiblnd_tunables.kib_ib_mtu);
590 cmid->route.path_rec->mtu = mtu;
593 static int kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
599 lnet_nid_t nid = conn->ibc_peer->ibp_nid;
601 vectors = conn->ibc_cmid->device->num_comp_vectors;
605 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
609 /* hash NID to CPU id in this partition... */
610 off = do_div(nid, cpumask_weight(mask));
611 for_each_cpu(i, mask) {
620 struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cmid,
621 int state, int version)
625 * If the new conn is created successfully it takes over the caller's
626 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
627 * is destroyed. On failure, the caller's ref on 'peer' remains and
628 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
629 * to destroy 'cmid' here since I'm called from the CM which still has
630 * its ref on 'cmid').
632 rwlock_t *glock = &kiblnd_data.kib_global_lock;
633 struct kib_net *net = peer->ibp_ni->ni_data;
635 struct ib_qp_init_attr *init_qp_attr;
636 struct kib_sched_info *sched;
637 struct ib_cq_init_attr cq_attr = {};
638 struct kib_conn *conn;
646 LASSERT(!in_interrupt());
650 cpt = lnet_cpt_of_nid(peer->ibp_nid);
651 sched = kiblnd_data.kib_scheds[cpt];
653 LASSERT(sched->ibs_nthreads > 0);
655 LIBCFS_CPT_ALLOC(init_qp_attr, lnet_cpt_table(), cpt,
656 sizeof(*init_qp_attr));
658 CERROR("Can't allocate qp_attr for %s\n",
659 libcfs_nid2str(peer->ibp_nid));
663 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
665 CERROR("Can't allocate connection for %s\n",
666 libcfs_nid2str(peer->ibp_nid));
670 conn->ibc_state = IBLND_CONN_INIT;
671 conn->ibc_version = version;
672 conn->ibc_peer = peer; /* I take the caller's ref */
673 cmid->context = conn; /* for future CM callbacks */
674 conn->ibc_cmid = cmid;
675 conn->ibc_max_frags = peer->ibp_max_frags;
676 conn->ibc_queue_depth = peer->ibp_queue_depth;
678 INIT_LIST_HEAD(&conn->ibc_early_rxs);
679 INIT_LIST_HEAD(&conn->ibc_tx_noops);
680 INIT_LIST_HEAD(&conn->ibc_tx_queue);
681 INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
682 INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
683 INIT_LIST_HEAD(&conn->ibc_active_txs);
684 spin_lock_init(&conn->ibc_lock);
686 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
687 sizeof(*conn->ibc_connvars));
688 if (!conn->ibc_connvars) {
689 CERROR("Can't allocate in-progress connection state\n");
693 write_lock_irqsave(glock, flags);
694 if (dev->ibd_failover) {
695 write_unlock_irqrestore(glock, flags);
696 CERROR("%s: failover in progress\n", dev->ibd_ifname);
700 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
701 /* wakeup failover thread and teardown connection */
702 if (kiblnd_dev_can_failover(dev)) {
703 list_add_tail(&dev->ibd_fail_list,
704 &kiblnd_data.kib_failed_devs);
705 wake_up(&kiblnd_data.kib_failover_waitq);
708 write_unlock_irqrestore(glock, flags);
709 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
710 cmid->device->name, dev->ibd_ifname);
714 kiblnd_hdev_addref_locked(dev->ibd_hdev);
715 conn->ibc_hdev = dev->ibd_hdev;
717 kiblnd_setup_mtu_locked(cmid);
719 write_unlock_irqrestore(glock, flags);
721 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
722 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
723 if (!conn->ibc_rxs) {
724 CERROR("Cannot allocate RX buffers\n");
728 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
729 IBLND_RX_MSG_PAGES(conn));
733 kiblnd_map_rx_descs(conn);
735 cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
736 cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
737 cq = ib_create_cq(cmid->device,
738 kiblnd_cq_completion, kiblnd_cq_event, conn,
741 CERROR("Failed to create CQ with %d CQEs: %ld\n",
742 IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
748 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
750 CERROR("Can't request completion notification: %d\n", rc);
754 init_qp_attr->event_handler = kiblnd_qp_event;
755 init_qp_attr->qp_context = conn;
756 init_qp_attr->cap.max_send_wr = IBLND_SEND_WRS(conn);
757 init_qp_attr->cap.max_recv_wr = IBLND_RECV_WRS(conn);
758 init_qp_attr->cap.max_send_sge = 1;
759 init_qp_attr->cap.max_recv_sge = 1;
760 init_qp_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
761 init_qp_attr->qp_type = IB_QPT_RC;
762 init_qp_attr->send_cq = cq;
763 init_qp_attr->recv_cq = cq;
765 conn->ibc_sched = sched;
767 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
769 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
770 rc, init_qp_attr->cap.max_send_wr,
771 init_qp_attr->cap.max_recv_wr);
775 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
777 /* 1 ref for caller and each rxmsg */
778 atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
779 conn->ibc_nrx = IBLND_RX_MSGS(conn);
782 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
783 rc = kiblnd_post_rx(&conn->ibc_rxs[i],
784 IBLND_POSTRX_NO_CREDIT);
786 CERROR("Can't post rxmsg: %d\n", rc);
788 /* Make posted receives complete */
789 kiblnd_abort_receives(conn);
792 * correct # of posted buffers
793 * NB locking needed now I'm racing with completion
795 spin_lock_irqsave(&sched->ibs_lock, flags);
796 conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i;
797 spin_unlock_irqrestore(&sched->ibs_lock, flags);
800 * cmid will be destroyed by CM(ofed) after cm_callback
801 * returned, so we can't refer it anymore
802 * (by kiblnd_connd()->kiblnd_destroy_conn)
804 rdma_destroy_qp(conn->ibc_cmid);
805 conn->ibc_cmid = NULL;
807 /* Drop my own and unused rxbuffer refcounts */
808 while (i++ <= IBLND_RX_MSGS(conn))
809 kiblnd_conn_decref(conn);
815 /* Init successful! */
816 LASSERT(state == IBLND_CONN_ACTIVE_CONNECT ||
817 state == IBLND_CONN_PASSIVE_WAIT);
818 conn->ibc_state = state;
821 atomic_inc(&net->ibn_nconns);
825 kiblnd_destroy_conn(conn, true);
827 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
832 void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn)
834 struct rdma_cm_id *cmid = conn->ibc_cmid;
835 struct kib_peer *peer = conn->ibc_peer;
838 LASSERT(!in_interrupt());
839 LASSERT(!atomic_read(&conn->ibc_refcount));
840 LASSERT(list_empty(&conn->ibc_early_rxs));
841 LASSERT(list_empty(&conn->ibc_tx_noops));
842 LASSERT(list_empty(&conn->ibc_tx_queue));
843 LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
844 LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
845 LASSERT(list_empty(&conn->ibc_active_txs));
846 LASSERT(!conn->ibc_noops_posted);
847 LASSERT(!conn->ibc_nsends_posted);
849 switch (conn->ibc_state) {
851 /* conn must be completely disengaged from the network */
854 case IBLND_CONN_DISCONNECTED:
855 /* connvars should have been freed already */
856 LASSERT(!conn->ibc_connvars);
859 case IBLND_CONN_INIT:
863 /* conn->ibc_cmid might be destroyed by CM already */
864 if (cmid && cmid->qp)
865 rdma_destroy_qp(cmid);
868 rc = ib_destroy_cq(conn->ibc_cq);
870 CWARN("Error destroying CQ: %d\n", rc);
873 if (conn->ibc_rx_pages)
874 kiblnd_unmap_rx_descs(conn);
877 LIBCFS_FREE(conn->ibc_rxs,
878 IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
881 if (conn->ibc_connvars)
882 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
885 kiblnd_hdev_decref(conn->ibc_hdev);
887 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
888 if (conn->ibc_state != IBLND_CONN_INIT) {
889 struct kib_net *net = peer->ibp_ni->ni_data;
891 kiblnd_peer_decref(peer);
892 rdma_destroy_id(cmid);
893 atomic_dec(&net->ibn_nconns);
896 LIBCFS_FREE(conn, sizeof(*conn));
899 int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why)
901 struct kib_conn *conn;
902 struct list_head *ctmp;
903 struct list_head *cnxt;
906 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
907 conn = list_entry(ctmp, struct kib_conn, ibc_list);
909 CDEBUG(D_NET, "Closing conn -> %s, version: %x, reason: %d\n",
910 libcfs_nid2str(peer->ibp_nid),
911 conn->ibc_version, why);
913 kiblnd_close_conn_locked(conn, why);
920 int kiblnd_close_stale_conns_locked(struct kib_peer *peer,
921 int version, __u64 incarnation)
923 struct kib_conn *conn;
924 struct list_head *ctmp;
925 struct list_head *cnxt;
928 list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) {
929 conn = list_entry(ctmp, struct kib_conn, ibc_list);
931 if (conn->ibc_version == version &&
932 conn->ibc_incarnation == incarnation)
936 "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n",
937 libcfs_nid2str(peer->ibp_nid),
938 conn->ibc_version, conn->ibc_incarnation,
939 version, incarnation);
941 kiblnd_close_conn_locked(conn, -ESTALE);
948 static int kiblnd_close_matching_conns(lnet_ni_t *ni, lnet_nid_t nid)
950 struct kib_peer *peer;
951 struct list_head *ptmp;
952 struct list_head *pnxt;
959 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
961 if (nid != LNET_NID_ANY) {
962 lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
963 hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
966 hi = kiblnd_data.kib_peer_hash_size - 1;
969 for (i = lo; i <= hi; i++) {
970 list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
971 peer = list_entry(ptmp, struct kib_peer, ibp_list);
972 LASSERT(!kiblnd_peer_idle(peer));
974 if (peer->ibp_ni != ni)
977 if (!(nid == LNET_NID_ANY || nid == peer->ibp_nid))
980 count += kiblnd_close_peer_conns_locked(peer, 0);
984 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
986 /* wildcards always succeed */
987 if (nid == LNET_NID_ANY)
990 return !count ? -ENOENT : 0;
993 static int kiblnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
995 struct libcfs_ioctl_data *data = arg;
999 case IOC_LIBCFS_GET_PEER: {
1003 rc = kiblnd_get_peer_info(ni, data->ioc_count,
1005 data->ioc_nid = nid;
1006 data->ioc_count = count;
1010 case IOC_LIBCFS_DEL_PEER: {
1011 rc = kiblnd_del_peer(ni, data->ioc_nid);
1014 case IOC_LIBCFS_GET_CONN: {
1015 struct kib_conn *conn;
1018 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1024 LASSERT(conn->ibc_cmid);
1025 data->ioc_nid = conn->ibc_peer->ibp_nid;
1026 if (!conn->ibc_cmid->route.path_rec)
1027 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1030 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1031 kiblnd_conn_decref(conn);
1034 case IOC_LIBCFS_CLOSE_CONNECTION: {
1035 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1046 static void kiblnd_query(lnet_ni_t *ni, lnet_nid_t nid, unsigned long *when)
1048 unsigned long last_alive = 0;
1049 unsigned long now = cfs_time_current();
1050 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1051 struct kib_peer *peer;
1052 unsigned long flags;
1054 read_lock_irqsave(glock, flags);
1056 peer = kiblnd_find_peer_locked(nid);
1058 last_alive = peer->ibp_last_alive;
1060 read_unlock_irqrestore(glock, flags);
1066 * peer is not persistent in hash, trigger peer creation
1067 * and connection establishment with a NULL tx
1070 kiblnd_launch_tx(ni, NULL, nid);
1072 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago\n",
1073 libcfs_nid2str(nid), peer,
1074 last_alive ? cfs_duration_sec(now - last_alive) : -1);
1077 static void kiblnd_free_pages(struct kib_pages *p)
1079 int npages = p->ibp_npages;
1082 for (i = 0; i < npages; i++) {
1083 if (p->ibp_pages[i])
1084 __free_page(p->ibp_pages[i]);
1087 LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1090 int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1092 struct kib_pages *p;
1095 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1096 offsetof(struct kib_pages, ibp_pages[npages]));
1098 CERROR("Can't allocate descriptor for %d pages\n", npages);
1102 memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1103 p->ibp_npages = npages;
1105 for (i = 0; i < npages; i++) {
1106 p->ibp_pages[i] = alloc_pages_node(
1107 cfs_cpt_spread_node(lnet_cpt_table(), cpt),
1109 if (!p->ibp_pages[i]) {
1110 CERROR("Can't allocate page %d of %d\n", i, npages);
1111 kiblnd_free_pages(p);
1120 void kiblnd_unmap_rx_descs(struct kib_conn *conn)
1125 LASSERT(conn->ibc_rxs);
1126 LASSERT(conn->ibc_hdev);
1128 for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1129 rx = &conn->ibc_rxs[i];
1131 LASSERT(rx->rx_nob >= 0); /* not posted */
1133 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1134 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1136 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1139 kiblnd_free_pages(conn->ibc_rx_pages);
1141 conn->ibc_rx_pages = NULL;
1144 void kiblnd_map_rx_descs(struct kib_conn *conn)
1152 for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1153 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1154 rx = &conn->ibc_rxs[i];
1157 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1159 rx->rx_msgaddr = kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1163 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1165 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1167 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1168 i, rx->rx_msg, rx->rx_msgaddr,
1169 (__u64)(page_to_phys(pg) + pg_off));
1171 pg_off += IBLND_MSG_SIZE;
1172 LASSERT(pg_off <= PAGE_SIZE);
1174 if (pg_off == PAGE_SIZE) {
1177 LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1182 static void kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1184 struct kib_hca_dev *hdev = tpo->tpo_hdev;
1188 LASSERT(!tpo->tpo_pool.po_allocated);
1193 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1194 tx = &tpo->tpo_tx_descs[i];
1195 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1196 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1198 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1201 kiblnd_hdev_decref(hdev);
1202 tpo->tpo_hdev = NULL;
1205 static struct kib_hca_dev *kiblnd_current_hdev(struct kib_dev *dev)
1207 struct kib_hca_dev *hdev;
1208 unsigned long flags;
1211 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1212 while (dev->ibd_failover) {
1213 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1215 CDEBUG(D_NET, "%s: Wait for failover\n",
1217 set_current_state(TASK_INTERRUPTIBLE);
1218 schedule_timeout(cfs_time_seconds(1) / 100);
1220 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1223 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1224 hdev = dev->ibd_hdev;
1226 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1231 static void kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1233 struct kib_pages *txpgs = tpo->tpo_tx_pages;
1234 struct kib_pool *pool = &tpo->tpo_pool;
1235 struct kib_net *net = pool->po_owner->ps_net;
1236 struct kib_dev *dev;
1247 /* pre-mapped messages are not bigger than 1 page */
1248 CLASSERT(IBLND_MSG_SIZE <= PAGE_SIZE);
1250 /* No fancy arithmetic when we do the buffer calculations */
1251 CLASSERT(!(PAGE_SIZE % IBLND_MSG_SIZE));
1253 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1255 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1256 page = txpgs->ibp_pages[ipage];
1257 tx = &tpo->tpo_tx_descs[i];
1259 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1262 tx->tx_msgaddr = kiblnd_dma_map_single(
1263 tpo->tpo_hdev->ibh_ibdev, tx->tx_msg,
1264 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1265 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1267 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1269 list_add(&tx->tx_list, &pool->po_free_list);
1271 page_offset += IBLND_MSG_SIZE;
1272 LASSERT(page_offset <= PAGE_SIZE);
1274 if (page_offset == PAGE_SIZE) {
1277 LASSERT(ipage <= txpgs->ibp_npages);
1282 struct ib_mr *kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd,
1283 int negotiated_nfrags)
1285 struct kib_net *net = ni->ni_data;
1286 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
1287 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1291 tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib;
1292 mod = tunables->lnd_map_on_demand;
1293 nfrags = (negotiated_nfrags != -1) ? negotiated_nfrags : mod;
1295 LASSERT(hdev->ibh_mrs);
1297 if (mod > 0 && nfrags <= rd->rd_nfrags)
1300 return hdev->ibh_mrs;
1303 static void kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1305 LASSERT(!fpo->fpo_map_count);
1307 if (fpo->fpo_is_fmr) {
1308 if (fpo->fmr.fpo_fmr_pool)
1309 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1311 struct kib_fast_reg_descriptor *frd, *tmp;
1314 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1316 list_del(&frd->frd_list);
1317 ib_dereg_mr(frd->frd_mr);
1318 LIBCFS_FREE(frd, sizeof(*frd));
1321 if (i < fpo->fast_reg.fpo_pool_size)
1322 CERROR("FastReg pool still has %d regions registered\n",
1323 fpo->fast_reg.fpo_pool_size - i);
1327 kiblnd_hdev_decref(fpo->fpo_hdev);
1329 LIBCFS_FREE(fpo, sizeof(*fpo));
1332 static void kiblnd_destroy_fmr_pool_list(struct list_head *head)
1334 struct kib_fmr_pool *fpo, *tmp;
1336 list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1337 list_del(&fpo->fpo_list);
1338 kiblnd_destroy_fmr_pool(fpo);
1343 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1346 int size = tunables->lnd_fmr_pool_size / ncpts;
1348 return max(IBLND_FMR_POOL, size);
1352 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1355 int size = tunables->lnd_fmr_flush_trigger / ncpts;
1357 return max(IBLND_FMR_POOL_FLUSH, size);
1360 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo)
1362 struct ib_fmr_pool_param param = {
1363 .max_pages_per_fmr = LNET_MAX_PAYLOAD / PAGE_SIZE,
1364 .page_shift = PAGE_SHIFT,
1365 .access = (IB_ACCESS_LOCAL_WRITE |
1366 IB_ACCESS_REMOTE_WRITE),
1367 .pool_size = fps->fps_pool_size,
1368 .dirty_watermark = fps->fps_flush_trigger,
1369 .flush_function = NULL,
1371 .cache = !!fps->fps_cache };
1374 fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1376 if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1377 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1379 CERROR("Failed to create FMR pool: %d\n", rc);
1381 CERROR("FMRs are not supported\n");
1387 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo)
1389 struct kib_fast_reg_descriptor *frd, *tmp;
1392 INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1393 fpo->fast_reg.fpo_pool_size = 0;
1394 for (i = 0; i < fps->fps_pool_size; i++) {
1395 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1398 CERROR("Failed to allocate a new fast_reg descriptor\n");
1403 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1405 LNET_MAX_PAYLOAD / PAGE_SIZE);
1406 if (IS_ERR(frd->frd_mr)) {
1407 rc = PTR_ERR(frd->frd_mr);
1408 CERROR("Failed to allocate ib_alloc_mr: %d\n", rc);
1413 frd->frd_valid = true;
1415 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1416 fpo->fast_reg.fpo_pool_size++;
1423 ib_dereg_mr(frd->frd_mr);
1424 LIBCFS_FREE(frd, sizeof(*frd));
1427 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1429 list_del(&frd->frd_list);
1430 ib_dereg_mr(frd->frd_mr);
1431 LIBCFS_FREE(frd, sizeof(*frd));
1437 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1438 struct kib_fmr_pool **pp_fpo)
1440 struct kib_dev *dev = fps->fps_net->ibn_dev;
1441 struct ib_device_attr *dev_attr;
1442 struct kib_fmr_pool *fpo;
1445 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1449 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1450 dev_attr = &fpo->fpo_hdev->ibh_ibdev->attrs;
1452 /* Check for FMR or FastReg support */
1453 fpo->fpo_is_fmr = 0;
1454 if (fpo->fpo_hdev->ibh_ibdev->alloc_fmr &&
1455 fpo->fpo_hdev->ibh_ibdev->dealloc_fmr &&
1456 fpo->fpo_hdev->ibh_ibdev->map_phys_fmr &&
1457 fpo->fpo_hdev->ibh_ibdev->unmap_fmr) {
1458 LCONSOLE_INFO("Using FMR for registration\n");
1459 fpo->fpo_is_fmr = 1;
1460 } else if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
1461 LCONSOLE_INFO("Using FastReg for registration\n");
1464 LCONSOLE_ERROR_MSG(rc, "IB device does not support FMRs nor FastRegs, can't register memory\n");
1468 if (fpo->fpo_is_fmr)
1469 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1471 rc = kiblnd_alloc_freg_pool(fps, fpo);
1475 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1476 fpo->fpo_owner = fps;
1482 kiblnd_hdev_decref(fpo->fpo_hdev);
1483 LIBCFS_FREE(fpo, sizeof(*fpo));
1487 static void kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps,
1488 struct list_head *zombies)
1490 if (!fps->fps_net) /* intialized? */
1493 spin_lock(&fps->fps_lock);
1495 while (!list_empty(&fps->fps_pool_list)) {
1496 struct kib_fmr_pool *fpo = list_entry(fps->fps_pool_list.next,
1497 struct kib_fmr_pool, fpo_list);
1498 fpo->fpo_failed = 1;
1499 list_del(&fpo->fpo_list);
1500 if (!fpo->fpo_map_count)
1501 list_add(&fpo->fpo_list, zombies);
1503 list_add(&fpo->fpo_list, &fps->fps_failed_pool_list);
1506 spin_unlock(&fps->fps_lock);
1509 static void kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1511 if (fps->fps_net) { /* initialized? */
1512 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1513 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1518 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1519 struct kib_net *net,
1520 struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1522 struct kib_fmr_pool *fpo;
1525 memset(fps, 0, sizeof(*fps));
1530 fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1531 fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1532 fps->fps_cache = tunables->lnd_fmr_cache;
1534 spin_lock_init(&fps->fps_lock);
1535 INIT_LIST_HEAD(&fps->fps_pool_list);
1536 INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1538 rc = kiblnd_create_fmr_pool(fps, &fpo);
1540 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1545 static int kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, unsigned long now)
1547 if (fpo->fpo_map_count) /* still in use */
1549 if (fpo->fpo_failed)
1551 return cfs_time_aftereq(now, fpo->fpo_deadline);
1555 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1557 __u64 *pages = tx->tx_pages;
1558 struct kib_hca_dev *hdev;
1563 hdev = tx->tx_pool->tpo_hdev;
1565 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1566 for (size = 0; size < rd->rd_frags[i].rf_nob;
1567 size += hdev->ibh_page_size) {
1568 pages[npages++] = (rd->rd_frags[i].rf_addr &
1569 hdev->ibh_page_mask) + size;
1576 void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1579 struct kib_fmr_pool *fpo = fmr->fmr_pool;
1580 struct kib_fmr_poolset *fps;
1581 unsigned long now = cfs_time_current();
1582 struct kib_fmr_pool *tmp;
1588 fps = fpo->fpo_owner;
1589 if (fpo->fpo_is_fmr) {
1590 if (fmr->fmr_pfmr) {
1591 rc = ib_fmr_pool_unmap(fmr->fmr_pfmr);
1593 fmr->fmr_pfmr = NULL;
1597 rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1601 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1604 frd->frd_valid = false;
1605 spin_lock(&fps->fps_lock);
1606 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1607 spin_unlock(&fps->fps_lock);
1608 fmr->fmr_frd = NULL;
1611 fmr->fmr_pool = NULL;
1613 spin_lock(&fps->fps_lock);
1614 fpo->fpo_map_count--; /* decref the pool */
1616 list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1617 /* the first pool is persistent */
1618 if (fps->fps_pool_list.next == &fpo->fpo_list)
1621 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1622 list_move(&fpo->fpo_list, &zombies);
1626 spin_unlock(&fps->fps_lock);
1628 if (!list_empty(&zombies))
1629 kiblnd_destroy_fmr_pool_list(&zombies);
1632 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1633 struct kib_rdma_desc *rd, __u32 nob, __u64 iov,
1634 struct kib_fmr *fmr)
1636 __u64 *pages = tx->tx_pages;
1637 bool is_rx = (rd != tx->tx_rd);
1638 bool tx_pages_mapped = 0;
1639 struct kib_fmr_pool *fpo;
1645 spin_lock(&fps->fps_lock);
1646 version = fps->fps_version;
1647 list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1648 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1649 fpo->fpo_map_count++;
1651 if (fpo->fpo_is_fmr) {
1652 struct ib_pool_fmr *pfmr;
1654 spin_unlock(&fps->fps_lock);
1656 if (!tx_pages_mapped) {
1657 npages = kiblnd_map_tx_pages(tx, rd);
1658 tx_pages_mapped = 1;
1661 pfmr = ib_fmr_pool_map_phys(fpo->fmr.fpo_fmr_pool,
1662 pages, npages, iov);
1663 if (likely(!IS_ERR(pfmr))) {
1664 fmr->fmr_key = is_rx ? pfmr->fmr->rkey :
1666 fmr->fmr_frd = NULL;
1667 fmr->fmr_pfmr = pfmr;
1668 fmr->fmr_pool = fpo;
1673 if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
1674 struct kib_fast_reg_descriptor *frd;
1675 struct ib_reg_wr *wr;
1679 frd = list_first_entry(&fpo->fast_reg.fpo_pool_list,
1680 struct kib_fast_reg_descriptor,
1682 list_del(&frd->frd_list);
1683 spin_unlock(&fps->fps_lock);
1687 if (!frd->frd_valid) {
1688 __u32 key = is_rx ? mr->rkey : mr->lkey;
1689 struct ib_send_wr *inv_wr;
1691 inv_wr = &frd->frd_inv_wr;
1692 memset(inv_wr, 0, sizeof(*inv_wr));
1693 inv_wr->opcode = IB_WR_LOCAL_INV;
1694 inv_wr->wr_id = IBLND_WID_MR;
1695 inv_wr->ex.invalidate_rkey = key;
1698 key = ib_inc_rkey(key);
1699 ib_update_fast_reg_key(mr, key);
1702 n = ib_map_mr_sg(mr, tx->tx_frags,
1703 tx->tx_nfrags, NULL, PAGE_SIZE);
1704 if (unlikely(n != tx->tx_nfrags)) {
1705 CERROR("Failed to map mr %d/%d elements\n",
1707 return n < 0 ? n : -EINVAL;
1712 /* Prepare FastReg WR */
1713 wr = &frd->frd_fastreg_wr;
1714 memset(wr, 0, sizeof(*wr));
1715 wr->wr.opcode = IB_WR_REG_MR;
1716 wr->wr.wr_id = IBLND_WID_MR;
1718 wr->wr.send_flags = 0;
1720 wr->key = is_rx ? mr->rkey : mr->lkey;
1721 wr->access = (IB_ACCESS_LOCAL_WRITE |
1722 IB_ACCESS_REMOTE_WRITE);
1724 fmr->fmr_key = is_rx ? mr->rkey : mr->lkey;
1726 fmr->fmr_pfmr = NULL;
1727 fmr->fmr_pool = fpo;
1730 spin_unlock(&fps->fps_lock);
1734 spin_lock(&fps->fps_lock);
1735 fpo->fpo_map_count--;
1736 if (rc != -EAGAIN) {
1737 spin_unlock(&fps->fps_lock);
1741 /* EAGAIN and ... */
1742 if (version != fps->fps_version) {
1743 spin_unlock(&fps->fps_lock);
1748 if (fps->fps_increasing) {
1749 spin_unlock(&fps->fps_lock);
1750 CDEBUG(D_NET, "Another thread is allocating new FMR pool, waiting for her to complete\n");
1755 if (time_before(cfs_time_current(), fps->fps_next_retry)) {
1756 /* someone failed recently */
1757 spin_unlock(&fps->fps_lock);
1761 fps->fps_increasing = 1;
1762 spin_unlock(&fps->fps_lock);
1764 CDEBUG(D_NET, "Allocate new FMR pool\n");
1765 rc = kiblnd_create_fmr_pool(fps, &fpo);
1766 spin_lock(&fps->fps_lock);
1767 fps->fps_increasing = 0;
1770 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1772 fps->fps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1774 spin_unlock(&fps->fps_lock);
1779 static void kiblnd_fini_pool(struct kib_pool *pool)
1781 LASSERT(list_empty(&pool->po_free_list));
1782 LASSERT(!pool->po_allocated);
1784 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
1787 static void kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
1789 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
1791 memset(pool, 0, sizeof(*pool));
1792 INIT_LIST_HEAD(&pool->po_free_list);
1793 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1794 pool->po_owner = ps;
1795 pool->po_size = size;
1798 static void kiblnd_destroy_pool_list(struct list_head *head)
1800 struct kib_pool *pool;
1802 while (!list_empty(head)) {
1803 pool = list_entry(head->next, struct kib_pool, po_list);
1804 list_del(&pool->po_list);
1806 LASSERT(pool->po_owner);
1807 pool->po_owner->ps_pool_destroy(pool);
1811 static void kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
1813 if (!ps->ps_net) /* intialized? */
1816 spin_lock(&ps->ps_lock);
1817 while (!list_empty(&ps->ps_pool_list)) {
1818 struct kib_pool *po = list_entry(ps->ps_pool_list.next,
1819 struct kib_pool, po_list);
1821 list_del(&po->po_list);
1822 if (!po->po_allocated)
1823 list_add(&po->po_list, zombies);
1825 list_add(&po->po_list, &ps->ps_failed_pool_list);
1827 spin_unlock(&ps->ps_lock);
1830 static void kiblnd_fini_poolset(struct kib_poolset *ps)
1832 if (ps->ps_net) { /* initialized? */
1833 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
1834 kiblnd_destroy_pool_list(&ps->ps_pool_list);
1838 static int kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
1839 struct kib_net *net, char *name, int size,
1840 kib_ps_pool_create_t po_create,
1841 kib_ps_pool_destroy_t po_destroy,
1842 kib_ps_node_init_t nd_init,
1843 kib_ps_node_fini_t nd_fini)
1845 struct kib_pool *pool;
1848 memset(ps, 0, sizeof(*ps));
1852 ps->ps_pool_create = po_create;
1853 ps->ps_pool_destroy = po_destroy;
1854 ps->ps_node_init = nd_init;
1855 ps->ps_node_fini = nd_fini;
1856 ps->ps_pool_size = size;
1857 if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name))
1858 >= sizeof(ps->ps_name))
1860 spin_lock_init(&ps->ps_lock);
1861 INIT_LIST_HEAD(&ps->ps_pool_list);
1862 INIT_LIST_HEAD(&ps->ps_failed_pool_list);
1864 rc = ps->ps_pool_create(ps, size, &pool);
1866 list_add(&pool->po_list, &ps->ps_pool_list);
1868 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
1873 static int kiblnd_pool_is_idle(struct kib_pool *pool, unsigned long now)
1875 if (pool->po_allocated) /* still in use */
1877 if (pool->po_failed)
1879 return cfs_time_aftereq(now, pool->po_deadline);
1882 void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
1885 struct kib_poolset *ps = pool->po_owner;
1886 struct kib_pool *tmp;
1887 unsigned long now = cfs_time_current();
1889 spin_lock(&ps->ps_lock);
1891 if (ps->ps_node_fini)
1892 ps->ps_node_fini(pool, node);
1894 LASSERT(pool->po_allocated > 0);
1895 list_add(node, &pool->po_free_list);
1896 pool->po_allocated--;
1898 list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
1899 /* the first pool is persistent */
1900 if (ps->ps_pool_list.next == &pool->po_list)
1903 if (kiblnd_pool_is_idle(pool, now))
1904 list_move(&pool->po_list, &zombies);
1906 spin_unlock(&ps->ps_lock);
1908 if (!list_empty(&zombies))
1909 kiblnd_destroy_pool_list(&zombies);
1912 struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps)
1914 struct list_head *node;
1915 struct kib_pool *pool;
1916 unsigned int interval = 1;
1917 unsigned long time_before;
1918 unsigned int trips = 0;
1922 spin_lock(&ps->ps_lock);
1923 list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
1924 if (list_empty(&pool->po_free_list))
1927 pool->po_allocated++;
1928 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1929 node = pool->po_free_list.next;
1932 if (ps->ps_node_init) {
1933 /* still hold the lock */
1934 ps->ps_node_init(pool, node);
1936 spin_unlock(&ps->ps_lock);
1940 /* no available tx pool and ... */
1941 if (ps->ps_increasing) {
1942 /* another thread is allocating a new pool */
1943 spin_unlock(&ps->ps_lock);
1945 CDEBUG(D_NET, "Another thread is allocating new %s pool, waiting %d HZs for her to complete. trips = %d\n",
1946 ps->ps_name, interval, trips);
1948 set_current_state(TASK_INTERRUPTIBLE);
1949 schedule_timeout(interval);
1950 if (interval < cfs_time_seconds(1))
1956 if (time_before(cfs_time_current(), ps->ps_next_retry)) {
1957 /* someone failed recently */
1958 spin_unlock(&ps->ps_lock);
1962 ps->ps_increasing = 1;
1963 spin_unlock(&ps->ps_lock);
1965 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
1966 time_before = cfs_time_current();
1967 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
1968 CDEBUG(D_NET, "ps_pool_create took %lu HZ to complete",
1969 cfs_time_current() - time_before);
1971 spin_lock(&ps->ps_lock);
1972 ps->ps_increasing = 0;
1974 list_add_tail(&pool->po_list, &ps->ps_pool_list);
1976 ps->ps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1977 CERROR("Can't allocate new %s pool because out of memory\n",
1980 spin_unlock(&ps->ps_lock);
1985 static void kiblnd_destroy_tx_pool(struct kib_pool *pool)
1987 struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool, tpo_pool);
1990 LASSERT(!pool->po_allocated);
1992 if (tpo->tpo_tx_pages) {
1993 kiblnd_unmap_tx_pool(tpo);
1994 kiblnd_free_pages(tpo->tpo_tx_pages);
1997 if (!tpo->tpo_tx_descs)
2000 for (i = 0; i < pool->po_size; i++) {
2001 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2003 list_del(&tx->tx_list);
2005 LIBCFS_FREE(tx->tx_pages,
2007 sizeof(*tx->tx_pages));
2009 LIBCFS_FREE(tx->tx_frags,
2010 (1 + IBLND_MAX_RDMA_FRAGS) *
2011 sizeof(*tx->tx_frags));
2013 LIBCFS_FREE(tx->tx_wrq,
2014 (1 + IBLND_MAX_RDMA_FRAGS) *
2015 sizeof(*tx->tx_wrq));
2017 LIBCFS_FREE(tx->tx_sge,
2018 (1 + IBLND_MAX_RDMA_FRAGS) *
2019 sizeof(*tx->tx_sge));
2021 LIBCFS_FREE(tx->tx_rd,
2022 offsetof(struct kib_rdma_desc,
2023 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2026 LIBCFS_FREE(tpo->tpo_tx_descs,
2027 pool->po_size * sizeof(struct kib_tx));
2029 kiblnd_fini_pool(pool);
2030 LIBCFS_FREE(tpo, sizeof(*tpo));
2033 static int kiblnd_tx_pool_size(int ncpts)
2035 int ntx = *kiblnd_tunables.kib_ntx / ncpts;
2037 return max(IBLND_TX_POOL, ntx);
2040 static int kiblnd_create_tx_pool(struct kib_poolset *ps, int size,
2041 struct kib_pool **pp_po)
2045 struct kib_pool *pool;
2046 struct kib_tx_pool *tpo;
2048 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2050 CERROR("Failed to allocate TX pool\n");
2054 pool = &tpo->tpo_pool;
2055 kiblnd_init_pool(ps, pool, size);
2056 tpo->tpo_tx_descs = NULL;
2057 tpo->tpo_tx_pages = NULL;
2059 npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2060 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg)) {
2061 CERROR("Can't allocate tx pages: %d\n", npg);
2062 LIBCFS_FREE(tpo, sizeof(*tpo));
2066 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2067 size * sizeof(struct kib_tx));
2068 if (!tpo->tpo_tx_descs) {
2069 CERROR("Can't allocate %d tx descriptors\n", size);
2070 ps->ps_pool_destroy(pool);
2074 memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2076 for (i = 0; i < size; i++) {
2077 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2080 if (ps->ps_net->ibn_fmr_ps) {
2081 LIBCFS_CPT_ALLOC(tx->tx_pages,
2082 lnet_cpt_table(), ps->ps_cpt,
2083 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2088 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2089 (1 + IBLND_MAX_RDMA_FRAGS) *
2090 sizeof(*tx->tx_frags));
2094 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS + 1);
2096 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2097 (1 + IBLND_MAX_RDMA_FRAGS) *
2098 sizeof(*tx->tx_wrq));
2102 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2103 (1 + IBLND_MAX_RDMA_FRAGS) *
2104 sizeof(*tx->tx_sge));
2108 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2109 offsetof(struct kib_rdma_desc,
2110 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2116 kiblnd_map_tx_pool(tpo);
2121 ps->ps_pool_destroy(pool);
2125 static void kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2127 struct kib_tx_poolset *tps = container_of(pool->po_owner,
2128 struct kib_tx_poolset,
2130 struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list);
2132 tx->tx_cookie = tps->tps_next_tx_cookie++;
2135 static void kiblnd_net_fini_pools(struct kib_net *net)
2139 cfs_cpt_for_each(i, lnet_cpt_table()) {
2140 struct kib_tx_poolset *tps;
2141 struct kib_fmr_poolset *fps;
2143 if (net->ibn_tx_ps) {
2144 tps = net->ibn_tx_ps[i];
2145 kiblnd_fini_poolset(&tps->tps_poolset);
2148 if (net->ibn_fmr_ps) {
2149 fps = net->ibn_fmr_ps[i];
2150 kiblnd_fini_fmr_poolset(fps);
2154 if (net->ibn_tx_ps) {
2155 cfs_percpt_free(net->ibn_tx_ps);
2156 net->ibn_tx_ps = NULL;
2159 if (net->ibn_fmr_ps) {
2160 cfs_percpt_free(net->ibn_fmr_ps);
2161 net->ibn_fmr_ps = NULL;
2165 static int kiblnd_net_init_pools(struct kib_net *net, lnet_ni_t *ni, __u32 *cpts,
2168 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2169 unsigned long flags;
2174 tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib;
2176 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2177 if (!tunables->lnd_map_on_demand) {
2178 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2179 goto create_tx_pool;
2182 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2184 if (tunables->lnd_fmr_pool_size < *kiblnd_tunables.kib_ntx / 4) {
2185 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2186 tunables->lnd_fmr_pool_size,
2187 *kiblnd_tunables.kib_ntx / 4);
2193 * TX pool must be created later than FMR, see LU-2268
2196 LASSERT(!net->ibn_tx_ps);
2199 * premapping can fail if ibd_nmr > 1, so we always create
2200 * FMR pool and map-on-demand if premapping failed
2202 * cfs_precpt_alloc is creating an array of struct kib_fmr_poolset
2203 * The number of struct kib_fmr_poolsets create is equal to the
2204 * number of CPTs that exist, i.e net->ibn_fmr_ps[cpt].
2206 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2207 sizeof(struct kib_fmr_poolset));
2208 if (!net->ibn_fmr_ps) {
2209 CERROR("Failed to allocate FMR pool array\n");
2214 for (i = 0; i < ncpts; i++) {
2215 cpt = !cpts ? i : cpts[i];
2216 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2219 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2226 LASSERT(i == ncpts);
2230 * cfs_precpt_alloc is creating an array of struct kib_tx_poolset
2231 * The number of struct kib_tx_poolsets create is equal to the
2232 * number of CPTs that exist, i.e net->ibn_tx_ps[cpt].
2234 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2235 sizeof(struct kib_tx_poolset));
2236 if (!net->ibn_tx_ps) {
2237 CERROR("Failed to allocate tx pool array\n");
2242 for (i = 0; i < ncpts; i++) {
2243 cpt = !cpts ? i : cpts[i];
2244 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2246 kiblnd_tx_pool_size(ncpts),
2247 kiblnd_create_tx_pool,
2248 kiblnd_destroy_tx_pool,
2249 kiblnd_tx_init, NULL);
2251 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2259 kiblnd_net_fini_pools(net);
2264 static int kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2267 * It's safe to assume a HCA can handle a page size
2268 * matching that of the native system
2270 hdev->ibh_page_shift = PAGE_SHIFT;
2271 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2272 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2274 hdev->ibh_mr_size = hdev->ibh_ibdev->attrs.max_mr_size;
2275 if (hdev->ibh_mr_size == ~0ULL) {
2276 hdev->ibh_mr_shift = 64;
2280 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2284 static void kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev)
2289 ib_dereg_mr(hdev->ibh_mrs);
2291 hdev->ibh_mrs = NULL;
2294 void kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2296 kiblnd_hdev_cleanup_mrs(hdev);
2299 ib_dealloc_pd(hdev->ibh_pd);
2302 rdma_destroy_id(hdev->ibh_cmid);
2304 LIBCFS_FREE(hdev, sizeof(*hdev));
2307 static int kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev)
2311 int acflags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE;
2313 rc = kiblnd_hdev_get_attr(hdev);
2317 mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2319 CERROR("Failed ib_get_dma_mr : %ld\n", PTR_ERR(mr));
2320 kiblnd_hdev_cleanup_mrs(hdev);
2330 static int kiblnd_dummy_callback(struct rdma_cm_id *cmid,
2331 struct rdma_cm_event *event)
2336 static int kiblnd_dev_need_failover(struct kib_dev *dev)
2338 struct rdma_cm_id *cmid;
2339 struct sockaddr_in srcaddr;
2340 struct sockaddr_in dstaddr;
2343 if (!dev->ibd_hdev || /* initializing */
2344 !dev->ibd_hdev->ibh_cmid || /* listener is dead */
2345 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2349 * XXX: it's UGLY, but I don't have better way to find
2350 * ib-bonding HCA failover because:
2352 * a. no reliable CM event for HCA failover...
2353 * b. no OFED API to get ib_device for current net_device...
2355 * We have only two choices at this point:
2357 * a. rdma_bind_addr(), it will conflict with listener cmid
2358 * b. rdma_resolve_addr() to zero addr
2360 cmid = kiblnd_rdma_create_id(kiblnd_dummy_callback, dev, RDMA_PS_TCP,
2364 CERROR("Failed to create cmid for failover: %d\n", rc);
2368 memset(&srcaddr, 0, sizeof(srcaddr));
2369 srcaddr.sin_family = AF_INET;
2370 srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2372 memset(&dstaddr, 0, sizeof(dstaddr));
2373 dstaddr.sin_family = AF_INET;
2374 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2375 (struct sockaddr *)&dstaddr, 1);
2376 if (rc || !cmid->device) {
2377 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2378 dev->ibd_ifname, &dev->ibd_ifip,
2380 rdma_destroy_id(cmid);
2384 rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2385 rdma_destroy_id(cmid);
2390 int kiblnd_dev_failover(struct kib_dev *dev)
2392 LIST_HEAD(zombie_tpo);
2393 LIST_HEAD(zombie_ppo);
2394 LIST_HEAD(zombie_fpo);
2395 struct rdma_cm_id *cmid = NULL;
2396 struct kib_hca_dev *hdev = NULL;
2398 struct kib_net *net;
2399 struct sockaddr_in addr;
2400 unsigned long flags;
2404 LASSERT(*kiblnd_tunables.kib_dev_failover > 1 ||
2405 dev->ibd_can_failover || !dev->ibd_hdev);
2407 rc = kiblnd_dev_need_failover(dev);
2411 if (dev->ibd_hdev &&
2412 dev->ibd_hdev->ibh_cmid) {
2414 * XXX it's not good to close old listener at here,
2415 * because we can fail to create new listener.
2416 * But we have to close it now, otherwise rdma_bind_addr
2417 * will return EADDRINUSE... How crap!
2419 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2421 cmid = dev->ibd_hdev->ibh_cmid;
2423 * make next schedule of kiblnd_dev_need_failover()
2426 dev->ibd_hdev->ibh_cmid = NULL;
2427 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2429 rdma_destroy_id(cmid);
2432 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, dev, RDMA_PS_TCP,
2436 CERROR("Failed to create cmid for failover: %d\n", rc);
2440 memset(&addr, 0, sizeof(addr));
2441 addr.sin_family = AF_INET;
2442 addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2443 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2445 /* Bind to failover device or port */
2446 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2447 if (rc || !cmid->device) {
2448 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2449 dev->ibd_ifname, &dev->ibd_ifip,
2451 rdma_destroy_id(cmid);
2455 LIBCFS_ALLOC(hdev, sizeof(*hdev));
2457 CERROR("Failed to allocate kib_hca_dev\n");
2458 rdma_destroy_id(cmid);
2463 atomic_set(&hdev->ibh_ref, 1);
2464 hdev->ibh_dev = dev;
2465 hdev->ibh_cmid = cmid;
2466 hdev->ibh_ibdev = cmid->device;
2468 pd = ib_alloc_pd(cmid->device, 0);
2471 CERROR("Can't allocate PD: %d\n", rc);
2477 rc = rdma_listen(cmid, 0);
2479 CERROR("Can't start new listener: %d\n", rc);
2483 rc = kiblnd_hdev_setup_mrs(hdev);
2485 CERROR("Can't setup device: %d\n", rc);
2489 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2491 swap(dev->ibd_hdev, hdev); /* take over the refcount */
2493 list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
2494 cfs_cpt_for_each(i, lnet_cpt_table()) {
2495 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
2498 if (net->ibn_fmr_ps)
2499 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
2504 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2506 if (!list_empty(&zombie_tpo))
2507 kiblnd_destroy_pool_list(&zombie_tpo);
2508 if (!list_empty(&zombie_ppo))
2509 kiblnd_destroy_pool_list(&zombie_ppo);
2510 if (!list_empty(&zombie_fpo))
2511 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
2513 kiblnd_hdev_decref(hdev);
2516 dev->ibd_failed_failover++;
2518 dev->ibd_failed_failover = 0;
2523 void kiblnd_destroy_dev(struct kib_dev *dev)
2525 LASSERT(!dev->ibd_nnets);
2526 LASSERT(list_empty(&dev->ibd_nets));
2528 list_del(&dev->ibd_fail_list);
2529 list_del(&dev->ibd_list);
2532 kiblnd_hdev_decref(dev->ibd_hdev);
2534 LIBCFS_FREE(dev, sizeof(*dev));
2537 static struct kib_dev *kiblnd_create_dev(char *ifname)
2539 struct net_device *netdev;
2540 struct kib_dev *dev;
2546 rc = lnet_ipif_query(ifname, &up, &ip, &netmask);
2548 CERROR("Can't query IPoIB interface %s: %d\n",
2554 CERROR("Can't query IPoIB interface %s: it's down\n", ifname);
2558 LIBCFS_ALLOC(dev, sizeof(*dev));
2562 netdev = dev_get_by_name(&init_net, ifname);
2564 dev->ibd_can_failover = 0;
2566 dev->ibd_can_failover = !!(netdev->flags & IFF_MASTER);
2570 INIT_LIST_HEAD(&dev->ibd_nets);
2571 INIT_LIST_HEAD(&dev->ibd_list); /* not yet in kib_devs */
2572 INIT_LIST_HEAD(&dev->ibd_fail_list);
2574 strcpy(&dev->ibd_ifname[0], ifname);
2576 /* initialize the device */
2577 rc = kiblnd_dev_failover(dev);
2579 CERROR("Can't initialize device: %d\n", rc);
2580 LIBCFS_FREE(dev, sizeof(*dev));
2584 list_add_tail(&dev->ibd_list, &kiblnd_data.kib_devs);
2588 static void kiblnd_base_shutdown(void)
2590 struct kib_sched_info *sched;
2593 LASSERT(list_empty(&kiblnd_data.kib_devs));
2595 switch (kiblnd_data.kib_init) {
2599 case IBLND_INIT_ALL:
2600 case IBLND_INIT_DATA:
2601 LASSERT(kiblnd_data.kib_peers);
2602 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
2603 LASSERT(list_empty(&kiblnd_data.kib_peers[i]));
2604 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
2605 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
2606 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
2607 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
2609 /* flag threads to terminate; wake and wait for them to die */
2610 kiblnd_data.kib_shutdown = 1;
2613 * NB: we really want to stop scheduler threads net by net
2614 * instead of the whole module, this should be improved
2615 * with dynamic configuration LNet
2617 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
2618 wake_up_all(&sched->ibs_waitq);
2620 wake_up_all(&kiblnd_data.kib_connd_waitq);
2621 wake_up_all(&kiblnd_data.kib_failover_waitq);
2624 while (atomic_read(&kiblnd_data.kib_nthreads)) {
2627 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
2628 "Waiting for %d threads to terminate\n",
2629 atomic_read(&kiblnd_data.kib_nthreads));
2630 set_current_state(TASK_UNINTERRUPTIBLE);
2631 schedule_timeout(cfs_time_seconds(1));
2636 case IBLND_INIT_NOTHING:
2640 if (kiblnd_data.kib_peers) {
2641 LIBCFS_FREE(kiblnd_data.kib_peers,
2642 sizeof(struct list_head) *
2643 kiblnd_data.kib_peer_hash_size);
2646 if (kiblnd_data.kib_scheds)
2647 cfs_percpt_free(kiblnd_data.kib_scheds);
2649 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
2650 module_put(THIS_MODULE);
2653 static void kiblnd_shutdown(lnet_ni_t *ni)
2655 struct kib_net *net = ni->ni_data;
2656 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2658 unsigned long flags;
2660 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
2665 write_lock_irqsave(g_lock, flags);
2666 net->ibn_shutdown = 1;
2667 write_unlock_irqrestore(g_lock, flags);
2669 switch (net->ibn_init) {
2673 case IBLND_INIT_ALL:
2674 /* nuke all existing peers within this net */
2675 kiblnd_del_peer(ni, LNET_NID_ANY);
2677 /* Wait for all peer state to clean up */
2679 while (atomic_read(&net->ibn_npeers)) {
2681 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* 2**n? */
2682 "%s: waiting for %d peers to disconnect\n",
2683 libcfs_nid2str(ni->ni_nid),
2684 atomic_read(&net->ibn_npeers));
2685 set_current_state(TASK_UNINTERRUPTIBLE);
2686 schedule_timeout(cfs_time_seconds(1));
2689 kiblnd_net_fini_pools(net);
2691 write_lock_irqsave(g_lock, flags);
2692 LASSERT(net->ibn_dev->ibd_nnets > 0);
2693 net->ibn_dev->ibd_nnets--;
2694 list_del(&net->ibn_list);
2695 write_unlock_irqrestore(g_lock, flags);
2699 case IBLND_INIT_NOTHING:
2700 LASSERT(!atomic_read(&net->ibn_nconns));
2702 if (net->ibn_dev && !net->ibn_dev->ibd_nnets)
2703 kiblnd_destroy_dev(net->ibn_dev);
2708 net->ibn_init = IBLND_INIT_NOTHING;
2711 LIBCFS_FREE(net, sizeof(*net));
2714 if (list_empty(&kiblnd_data.kib_devs))
2715 kiblnd_base_shutdown();
2718 static int kiblnd_base_startup(void)
2720 struct kib_sched_info *sched;
2724 LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
2726 try_module_get(THIS_MODULE);
2727 /* zero pointers, flags etc */
2728 memset(&kiblnd_data, 0, sizeof(kiblnd_data));
2730 rwlock_init(&kiblnd_data.kib_global_lock);
2732 INIT_LIST_HEAD(&kiblnd_data.kib_devs);
2733 INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
2735 kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
2736 LIBCFS_ALLOC(kiblnd_data.kib_peers,
2737 sizeof(struct list_head) * kiblnd_data.kib_peer_hash_size);
2738 if (!kiblnd_data.kib_peers)
2740 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
2741 INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]);
2743 spin_lock_init(&kiblnd_data.kib_connd_lock);
2744 INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
2745 INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
2746 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
2747 INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
2749 init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
2750 init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
2752 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
2754 if (!kiblnd_data.kib_scheds)
2757 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
2760 spin_lock_init(&sched->ibs_lock);
2761 INIT_LIST_HEAD(&sched->ibs_conns);
2762 init_waitqueue_head(&sched->ibs_waitq);
2764 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2765 if (*kiblnd_tunables.kib_nscheds > 0) {
2766 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
2769 * max to half of CPUs, another half is reserved for
2770 * upper layer modules
2772 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
2775 sched->ibs_nthreads_max = nthrs;
2779 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
2781 /* lists/ptrs/locks initialised */
2782 kiblnd_data.kib_init = IBLND_INIT_DATA;
2783 /*****************************************************/
2785 rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
2787 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
2791 if (*kiblnd_tunables.kib_dev_failover)
2792 rc = kiblnd_thread_start(kiblnd_failover_thread, NULL,
2796 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
2800 /* flag everything initialised */
2801 kiblnd_data.kib_init = IBLND_INIT_ALL;
2802 /*****************************************************/
2807 kiblnd_base_shutdown();
2811 static int kiblnd_start_schedulers(struct kib_sched_info *sched)
2817 if (!sched->ibs_nthreads) {
2818 if (*kiblnd_tunables.kib_nscheds > 0) {
2819 nthrs = sched->ibs_nthreads_max;
2821 nthrs = cfs_cpt_weight(lnet_cpt_table(),
2823 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
2824 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
2827 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
2828 /* increase one thread if there is new interface */
2829 nthrs = sched->ibs_nthreads < sched->ibs_nthreads_max;
2832 for (i = 0; i < nthrs; i++) {
2836 id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
2837 snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld",
2838 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
2839 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id, name);
2843 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2844 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
2848 sched->ibs_nthreads += i;
2852 static int kiblnd_dev_start_threads(struct kib_dev *dev, int newdev, __u32 *cpts,
2859 for (i = 0; i < ncpts; i++) {
2860 struct kib_sched_info *sched;
2862 cpt = !cpts ? i : cpts[i];
2863 sched = kiblnd_data.kib_scheds[cpt];
2865 if (!newdev && sched->ibs_nthreads > 0)
2868 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
2870 CERROR("Failed to start scheduler threads for %s\n",
2878 static struct kib_dev *kiblnd_dev_search(char *ifname)
2880 struct kib_dev *alias = NULL;
2881 struct kib_dev *dev;
2885 colon = strchr(ifname, ':');
2886 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
2887 if (!strcmp(&dev->ibd_ifname[0], ifname))
2893 colon2 = strchr(dev->ibd_ifname, ':');
2899 if (!strcmp(&dev->ibd_ifname[0], ifname))
2910 static int kiblnd_startup(lnet_ni_t *ni)
2913 struct kib_dev *ibdev = NULL;
2914 struct kib_net *net;
2915 struct timespec64 tv;
2916 unsigned long flags;
2920 LASSERT(ni->ni_lnd == &the_o2iblnd);
2922 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
2923 rc = kiblnd_base_startup();
2928 LIBCFS_ALLOC(net, sizeof(*net));
2933 ktime_get_real_ts64(&tv);
2934 net->ibn_incarnation = tv.tv_sec * USEC_PER_SEC +
2935 tv.tv_nsec / NSEC_PER_USEC;
2937 rc = kiblnd_tunables_setup(ni);
2941 if (ni->ni_interfaces[0]) {
2942 /* Use the IPoIB interface specified in 'networks=' */
2944 CLASSERT(LNET_MAX_INTERFACES > 1);
2945 if (ni->ni_interfaces[1]) {
2946 CERROR("Multiple interfaces not supported\n");
2950 ifname = ni->ni_interfaces[0];
2952 ifname = *kiblnd_tunables.kib_default_ipif;
2955 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
2956 CERROR("IPoIB interface name too long: %s\n", ifname);
2960 ibdev = kiblnd_dev_search(ifname);
2963 /* hmm...create kib_dev even for alias */
2964 if (!ibdev || strcmp(&ibdev->ibd_ifname[0], ifname))
2965 ibdev = kiblnd_create_dev(ifname);
2970 net->ibn_dev = ibdev;
2971 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip);
2973 rc = kiblnd_dev_start_threads(ibdev, newdev,
2974 ni->ni_cpts, ni->ni_ncpts);
2978 rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
2980 CERROR("Failed to initialize NI pools: %d\n", rc);
2984 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2986 list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
2987 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2989 net->ibn_init = IBLND_INIT_ALL;
2994 if (!net->ibn_dev && ibdev)
2995 kiblnd_destroy_dev(ibdev);
2998 kiblnd_shutdown(ni);
3000 CDEBUG(D_NET, "kiblnd_startup failed\n");
3004 static lnd_t the_o2iblnd = {
3005 .lnd_type = O2IBLND,
3006 .lnd_startup = kiblnd_startup,
3007 .lnd_shutdown = kiblnd_shutdown,
3008 .lnd_ctl = kiblnd_ctl,
3009 .lnd_query = kiblnd_query,
3010 .lnd_send = kiblnd_send,
3011 .lnd_recv = kiblnd_recv,
3014 static void __exit ko2iblnd_exit(void)
3016 lnet_unregister_lnd(&the_o2iblnd);
3019 static int __init ko2iblnd_init(void)
3021 CLASSERT(sizeof(struct kib_msg) <= IBLND_MSG_SIZE);
3022 CLASSERT(offsetof(struct kib_msg,
3023 ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
3025 CLASSERT(offsetof(struct kib_msg,
3026 ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
3029 kiblnd_tunables_init();
3031 lnet_register_lnd(&the_o2iblnd);
3036 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3037 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
3038 MODULE_VERSION("2.7.0");
3039 MODULE_LICENSE("GPL");
3041 module_init(ko2iblnd_init);
3042 module_exit(ko2iblnd_exit);