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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/o2iblnd/o2iblnd.c
38 * Author: Eric Barton <eric@bartonsoftware.com>
45 .lnd_startup = kiblnd_startup,
46 .lnd_shutdown = kiblnd_shutdown,
47 .lnd_ctl = kiblnd_ctl,
48 .lnd_query = kiblnd_query,
49 .lnd_send = kiblnd_send,
50 .lnd_recv = kiblnd_recv,
53 kib_data_t kiblnd_data;
56 kiblnd_cksum (void *ptr, int nob)
62 sum = ((sum << 1) | (sum >> 31)) + *c++;
64 /* ensure I don't return 0 (== no checksum) */
65 return (sum == 0) ? 1 : sum;
69 kiblnd_msgtype2str(int type)
72 case IBLND_MSG_CONNREQ:
75 case IBLND_MSG_CONNACK:
81 case IBLND_MSG_IMMEDIATE:
84 case IBLND_MSG_PUT_REQ:
87 case IBLND_MSG_PUT_NAK:
90 case IBLND_MSG_PUT_ACK:
93 case IBLND_MSG_PUT_DONE:
96 case IBLND_MSG_GET_REQ:
99 case IBLND_MSG_GET_DONE:
108 kiblnd_msgtype2size(int type)
110 const int hdr_size = offsetof(kib_msg_t, ibm_u);
113 case IBLND_MSG_CONNREQ:
114 case IBLND_MSG_CONNACK:
115 return hdr_size + sizeof(kib_connparams_t);
120 case IBLND_MSG_IMMEDIATE:
121 return offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[0]);
123 case IBLND_MSG_PUT_REQ:
124 return hdr_size + sizeof(kib_putreq_msg_t);
126 case IBLND_MSG_PUT_ACK:
127 return hdr_size + sizeof(kib_putack_msg_t);
129 case IBLND_MSG_GET_REQ:
130 return hdr_size + sizeof(kib_get_msg_t);
132 case IBLND_MSG_PUT_NAK:
133 case IBLND_MSG_PUT_DONE:
134 case IBLND_MSG_GET_DONE:
135 return hdr_size + sizeof(kib_completion_msg_t);
142 kiblnd_unpack_rd(kib_msg_t *msg, int flip)
149 LASSERT (msg->ibm_type == IBLND_MSG_GET_REQ ||
150 msg->ibm_type == IBLND_MSG_PUT_ACK);
152 rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
153 &msg->ibm_u.get.ibgm_rd :
154 &msg->ibm_u.putack.ibpam_rd;
157 __swab32s(&rd->rd_key);
158 __swab32s(&rd->rd_nfrags);
163 if (n <= 0 || n > IBLND_MAX_RDMA_FRAGS) {
164 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
165 n, IBLND_MAX_RDMA_FRAGS);
169 nob = offsetof (kib_msg_t, ibm_u) +
170 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
172 if (msg->ibm_nob < nob) {
173 CERROR("Short %s: %d(%d)\n",
174 kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
181 for (i = 0; i < n; i++) {
182 __swab32s(&rd->rd_frags[i].rf_nob);
183 __swab64s(&rd->rd_frags[i].rf_addr);
190 kiblnd_pack_msg (lnet_ni_t *ni, kib_msg_t *msg, int version,
191 int credits, lnet_nid_t dstnid, __u64 dststamp)
193 kib_net_t *net = ni->ni_data;
195 /* CAVEAT EMPTOR! all message fields not set here should have been
196 * initialised previously. */
197 msg->ibm_magic = IBLND_MSG_MAGIC;
198 msg->ibm_version = version;
200 msg->ibm_credits = credits;
203 msg->ibm_srcnid = ni->ni_nid;
204 msg->ibm_srcstamp = net->ibn_incarnation;
205 msg->ibm_dstnid = dstnid;
206 msg->ibm_dststamp = dststamp;
208 if (*kiblnd_tunables.kib_cksum) {
209 /* NB ibm_cksum zero while computing cksum */
210 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
215 kiblnd_unpack_msg(kib_msg_t *msg, int nob)
217 const int hdr_size = offsetof(kib_msg_t, ibm_u);
223 /* 6 bytes are enough to have received magic + version */
225 CERROR("Short message: %d\n", nob);
229 if (msg->ibm_magic == IBLND_MSG_MAGIC) {
231 } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
234 CERROR("Bad magic: %08x\n", msg->ibm_magic);
238 version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
239 if (version != IBLND_MSG_VERSION &&
240 version != IBLND_MSG_VERSION_1) {
241 CERROR("Bad version: %x\n", version);
245 if (nob < hdr_size) {
246 CERROR("Short message: %d\n", nob);
250 msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
252 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
256 /* checksum must be computed with ibm_cksum zero and BEFORE anything
258 msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
260 if (msg_cksum != 0 &&
261 msg_cksum != kiblnd_cksum(msg, msg_nob)) {
262 CERROR("Bad checksum\n");
266 msg->ibm_cksum = msg_cksum;
269 /* leave magic unflipped as a clue to peer endianness */
270 msg->ibm_version = version;
271 CLASSERT (sizeof(msg->ibm_type) == 1);
272 CLASSERT (sizeof(msg->ibm_credits) == 1);
273 msg->ibm_nob = msg_nob;
274 __swab64s(&msg->ibm_srcnid);
275 __swab64s(&msg->ibm_srcstamp);
276 __swab64s(&msg->ibm_dstnid);
277 __swab64s(&msg->ibm_dststamp);
280 if (msg->ibm_srcnid == LNET_NID_ANY) {
281 CERROR("Bad src nid: %s\n", libcfs_nid2str(msg->ibm_srcnid));
285 if (msg_nob < kiblnd_msgtype2size(msg->ibm_type)) {
286 CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg->ibm_type),
287 msg_nob, kiblnd_msgtype2size(msg->ibm_type));
291 switch (msg->ibm_type) {
293 CERROR("Unknown message type %x\n", msg->ibm_type);
297 case IBLND_MSG_IMMEDIATE:
298 case IBLND_MSG_PUT_REQ:
301 case IBLND_MSG_PUT_ACK:
302 case IBLND_MSG_GET_REQ:
303 if (kiblnd_unpack_rd(msg, flip))
307 case IBLND_MSG_PUT_NAK:
308 case IBLND_MSG_PUT_DONE:
309 case IBLND_MSG_GET_DONE:
311 __swab32s(&msg->ibm_u.completion.ibcm_status);
314 case IBLND_MSG_CONNREQ:
315 case IBLND_MSG_CONNACK:
317 __swab16s(&msg->ibm_u.connparams.ibcp_queue_depth);
318 __swab16s(&msg->ibm_u.connparams.ibcp_max_frags);
319 __swab32s(&msg->ibm_u.connparams.ibcp_max_msg_size);
327 kiblnd_create_peer(lnet_ni_t *ni, kib_peer_t **peerp, lnet_nid_t nid)
330 kib_net_t *net = ni->ni_data;
331 int cpt = lnet_cpt_of_nid(nid);
334 LASSERT(net != NULL);
335 LASSERT(nid != LNET_NID_ANY);
337 LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer));
339 CERROR("Cannot allocate peer\n");
343 memset(peer, 0, sizeof(*peer)); /* zero flags etc */
348 peer->ibp_last_alive = 0;
349 cfs_atomic_set(&peer->ibp_refcount, 1); /* 1 ref for caller */
351 CFS_INIT_LIST_HEAD(&peer->ibp_list); /* not in the peer table yet */
352 CFS_INIT_LIST_HEAD(&peer->ibp_conns);
353 CFS_INIT_LIST_HEAD(&peer->ibp_tx_queue);
355 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
357 /* always called with a ref on ni, which prevents ni being shutdown */
358 LASSERT (net->ibn_shutdown == 0);
360 /* npeers only grows with the global lock held */
361 cfs_atomic_inc(&net->ibn_npeers);
363 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
370 kiblnd_destroy_peer (kib_peer_t *peer)
372 kib_net_t *net = peer->ibp_ni->ni_data;
374 LASSERT (net != NULL);
375 LASSERT (cfs_atomic_read(&peer->ibp_refcount) == 0);
376 LASSERT (!kiblnd_peer_active(peer));
377 LASSERT (peer->ibp_connecting == 0);
378 LASSERT (peer->ibp_accepting == 0);
379 LASSERT (cfs_list_empty(&peer->ibp_conns));
380 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
382 LIBCFS_FREE(peer, sizeof(*peer));
384 /* NB a peer's connections keep a reference on their peer until
385 * they are destroyed, so we can be assured that _all_ state to do
386 * with this peer has been cleaned up when its refcount drops to
388 cfs_atomic_dec(&net->ibn_npeers);
392 kiblnd_find_peer_locked (lnet_nid_t nid)
394 /* the caller is responsible for accounting the additional reference
395 * that this creates */
396 cfs_list_t *peer_list = kiblnd_nid2peerlist(nid);
400 cfs_list_for_each (tmp, peer_list) {
402 peer = cfs_list_entry(tmp, kib_peer_t, ibp_list);
404 LASSERT (peer->ibp_connecting > 0 || /* creating conns */
405 peer->ibp_accepting > 0 ||
406 !cfs_list_empty(&peer->ibp_conns)); /* active conn */
408 if (peer->ibp_nid != nid)
411 CDEBUG(D_NET, "got peer [%p] -> %s (%d) version: %x\n",
412 peer, libcfs_nid2str(nid),
413 cfs_atomic_read(&peer->ibp_refcount),
421 kiblnd_unlink_peer_locked (kib_peer_t *peer)
423 LASSERT (cfs_list_empty(&peer->ibp_conns));
425 LASSERT (kiblnd_peer_active(peer));
426 cfs_list_del_init(&peer->ibp_list);
427 /* lose peerlist's ref */
428 kiblnd_peer_decref(peer);
432 kiblnd_get_peer_info (lnet_ni_t *ni, int index,
433 lnet_nid_t *nidp, int *count)
440 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
442 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
444 cfs_list_for_each (ptmp, &kiblnd_data.kib_peers[i]) {
446 peer = cfs_list_entry(ptmp, kib_peer_t, ibp_list);
447 LASSERT (peer->ibp_connecting > 0 ||
448 peer->ibp_accepting > 0 ||
449 !cfs_list_empty(&peer->ibp_conns));
451 if (peer->ibp_ni != ni)
457 *nidp = peer->ibp_nid;
458 *count = cfs_atomic_read(&peer->ibp_refcount);
460 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
466 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
471 kiblnd_del_peer_locked (kib_peer_t *peer)
477 if (cfs_list_empty(&peer->ibp_conns)) {
478 kiblnd_unlink_peer_locked(peer);
480 cfs_list_for_each_safe (ctmp, cnxt, &peer->ibp_conns) {
481 conn = cfs_list_entry(ctmp, kib_conn_t, ibc_list);
483 kiblnd_close_conn_locked(conn, 0);
485 /* NB closing peer's last conn unlinked it. */
487 /* NB peer now unlinked; might even be freed if the peer table had the
492 kiblnd_del_peer (lnet_ni_t *ni, lnet_nid_t nid)
494 CFS_LIST_HEAD (zombies);
504 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
506 if (nid != LNET_NID_ANY) {
507 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
510 hi = kiblnd_data.kib_peer_hash_size - 1;
513 for (i = lo; i <= hi; i++) {
514 cfs_list_for_each_safe (ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
515 peer = cfs_list_entry(ptmp, kib_peer_t, ibp_list);
516 LASSERT (peer->ibp_connecting > 0 ||
517 peer->ibp_accepting > 0 ||
518 !cfs_list_empty(&peer->ibp_conns));
520 if (peer->ibp_ni != ni)
523 if (!(nid == LNET_NID_ANY || peer->ibp_nid == nid))
526 if (!cfs_list_empty(&peer->ibp_tx_queue)) {
527 LASSERT (cfs_list_empty(&peer->ibp_conns));
529 cfs_list_splice_init(&peer->ibp_tx_queue,
533 kiblnd_del_peer_locked(peer);
534 rc = 0; /* matched something */
538 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
540 kiblnd_txlist_done(ni, &zombies, -EIO);
546 kiblnd_get_conn_by_idx (lnet_ni_t *ni, int index)
555 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
557 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
558 cfs_list_for_each (ptmp, &kiblnd_data.kib_peers[i]) {
560 peer = cfs_list_entry(ptmp, kib_peer_t, ibp_list);
561 LASSERT (peer->ibp_connecting > 0 ||
562 peer->ibp_accepting > 0 ||
563 !cfs_list_empty(&peer->ibp_conns));
565 if (peer->ibp_ni != ni)
568 cfs_list_for_each (ctmp, &peer->ibp_conns) {
572 conn = cfs_list_entry(ctmp, kib_conn_t,
574 kiblnd_conn_addref(conn);
575 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
582 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
587 kiblnd_debug_rx (kib_rx_t *rx)
589 CDEBUG(D_CONSOLE, " %p status %d msg_type %x cred %d\n",
590 rx, rx->rx_status, rx->rx_msg->ibm_type,
591 rx->rx_msg->ibm_credits);
595 kiblnd_debug_tx (kib_tx_t *tx)
597 CDEBUG(D_CONSOLE, " %p snd %d q %d w %d rc %d dl %lx "
598 "cookie "LPX64" msg %s%s type %x cred %d\n",
599 tx, tx->tx_sending, tx->tx_queued, tx->tx_waiting,
600 tx->tx_status, tx->tx_deadline, tx->tx_cookie,
601 tx->tx_lntmsg[0] == NULL ? "-" : "!",
602 tx->tx_lntmsg[1] == NULL ? "-" : "!",
603 tx->tx_msg->ibm_type, tx->tx_msg->ibm_credits);
607 kiblnd_debug_conn (kib_conn_t *conn)
612 spin_lock(&conn->ibc_lock);
614 CDEBUG(D_CONSOLE, "conn[%d] %p [version %x] -> %s: \n",
615 cfs_atomic_read(&conn->ibc_refcount), conn,
616 conn->ibc_version, libcfs_nid2str(conn->ibc_peer->ibp_nid));
617 CDEBUG(D_CONSOLE, " state %d nposted %d/%d cred %d o_cred %d r_cred %d\n",
618 conn->ibc_state, conn->ibc_noops_posted,
619 conn->ibc_nsends_posted, conn->ibc_credits,
620 conn->ibc_outstanding_credits, conn->ibc_reserved_credits);
621 CDEBUG(D_CONSOLE, " comms_err %d\n", conn->ibc_comms_error);
623 CDEBUG(D_CONSOLE, " early_rxs:\n");
624 cfs_list_for_each(tmp, &conn->ibc_early_rxs)
625 kiblnd_debug_rx(cfs_list_entry(tmp, kib_rx_t, rx_list));
627 CDEBUG(D_CONSOLE, " tx_noops:\n");
628 cfs_list_for_each(tmp, &conn->ibc_tx_noops)
629 kiblnd_debug_tx(cfs_list_entry(tmp, kib_tx_t, tx_list));
631 CDEBUG(D_CONSOLE, " tx_queue_nocred:\n");
632 cfs_list_for_each(tmp, &conn->ibc_tx_queue_nocred)
633 kiblnd_debug_tx(cfs_list_entry(tmp, kib_tx_t, tx_list));
635 CDEBUG(D_CONSOLE, " tx_queue_rsrvd:\n");
636 cfs_list_for_each(tmp, &conn->ibc_tx_queue_rsrvd)
637 kiblnd_debug_tx(cfs_list_entry(tmp, kib_tx_t, tx_list));
639 CDEBUG(D_CONSOLE, " tx_queue:\n");
640 cfs_list_for_each(tmp, &conn->ibc_tx_queue)
641 kiblnd_debug_tx(cfs_list_entry(tmp, kib_tx_t, tx_list));
643 CDEBUG(D_CONSOLE, " active_txs:\n");
644 cfs_list_for_each(tmp, &conn->ibc_active_txs)
645 kiblnd_debug_tx(cfs_list_entry(tmp, kib_tx_t, tx_list));
647 CDEBUG(D_CONSOLE, " rxs:\n");
648 for (i = 0; i < IBLND_RX_MSGS(conn->ibc_version); i++)
649 kiblnd_debug_rx(&conn->ibc_rxs[i]);
651 spin_unlock(&conn->ibc_lock);
655 kiblnd_translate_mtu(int value)
676 kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
680 /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
681 if (cmid->route.path_rec == NULL)
684 mtu = kiblnd_translate_mtu(*kiblnd_tunables.kib_ib_mtu);
687 cmid->route.path_rec->mtu = mtu;
690 #ifdef HAVE_OFED_IB_COMP_VECTOR
692 kiblnd_get_completion_vector(kib_conn_t *conn, int cpt)
699 vectors = conn->ibc_cmid->device->num_comp_vectors;
703 mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
705 /* hash NID to CPU id in this partition... */
706 off = conn->ibc_peer->ibp_nid % cpus_weight(*mask);
707 for_each_cpu_mask(i, *mask) {
718 kiblnd_create_conn(kib_peer_t *peer, struct rdma_cm_id *cmid,
719 int state, int version)
722 * If the new conn is created successfully it takes over the caller's
723 * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself
724 * is destroyed. On failure, the caller's ref on 'peer' remains and
725 * she must dispose of 'cmid'. (Actually I'd block forever if I tried
726 * to destroy 'cmid' here since I'm called from the CM which still has
727 * its ref on 'cmid'). */
728 rwlock_t *glock = &kiblnd_data.kib_global_lock;
729 kib_net_t *net = peer->ibp_ni->ni_data;
730 kib_dev_t *dev = net->ibn_dev;
731 struct ib_qp_init_attr *init_qp_attr;
732 struct kib_sched_info *sched;
740 LASSERT(net != NULL);
741 LASSERT(!cfs_in_interrupt());
743 cpt = lnet_cpt_of_nid(peer->ibp_nid);
744 sched = kiblnd_data.kib_scheds[cpt];
746 LASSERT(sched->ibs_nthreads > 0);
748 LIBCFS_CPT_ALLOC(init_qp_attr, lnet_cpt_table(), cpt,
749 sizeof(*init_qp_attr));
750 if (init_qp_attr == NULL) {
751 CERROR("Can't allocate qp_attr for %s\n",
752 libcfs_nid2str(peer->ibp_nid));
756 LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
758 CERROR("Can't allocate connection for %s\n",
759 libcfs_nid2str(peer->ibp_nid));
763 conn->ibc_state = IBLND_CONN_INIT;
764 conn->ibc_version = version;
765 conn->ibc_peer = peer; /* I take the caller's ref */
766 cmid->context = conn; /* for future CM callbacks */
767 conn->ibc_cmid = cmid;
769 CFS_INIT_LIST_HEAD(&conn->ibc_early_rxs);
770 CFS_INIT_LIST_HEAD(&conn->ibc_tx_noops);
771 CFS_INIT_LIST_HEAD(&conn->ibc_tx_queue);
772 CFS_INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
773 CFS_INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
774 CFS_INIT_LIST_HEAD(&conn->ibc_active_txs);
775 spin_lock_init(&conn->ibc_lock);
777 LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
778 sizeof(*conn->ibc_connvars));
779 if (conn->ibc_connvars == NULL) {
780 CERROR("Can't allocate in-progress connection state\n");
784 write_lock_irqsave(glock, flags);
785 if (dev->ibd_failover) {
786 write_unlock_irqrestore(glock, flags);
787 CERROR("%s: failover in progress\n", dev->ibd_ifname);
791 if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
792 /* wakeup failover thread and teardown connection */
793 if (kiblnd_dev_can_failover(dev)) {
794 cfs_list_add_tail(&dev->ibd_fail_list,
795 &kiblnd_data.kib_failed_devs);
796 cfs_waitq_signal(&kiblnd_data.kib_failover_waitq);
799 write_unlock_irqrestore(glock, flags);
800 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
801 cmid->device->name, dev->ibd_ifname);
805 kiblnd_hdev_addref_locked(dev->ibd_hdev);
806 conn->ibc_hdev = dev->ibd_hdev;
808 kiblnd_setup_mtu_locked(cmid);
810 write_unlock_irqrestore(glock, flags);
812 LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
813 IBLND_RX_MSGS(version) * sizeof(kib_rx_t));
814 if (conn->ibc_rxs == NULL) {
815 CERROR("Cannot allocate RX buffers\n");
819 rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
820 IBLND_RX_MSG_PAGES(version));
824 kiblnd_map_rx_descs(conn);
826 #ifdef HAVE_OFED_IB_COMP_VECTOR
827 cq = ib_create_cq(cmid->device,
828 kiblnd_cq_completion, kiblnd_cq_event, conn,
829 IBLND_CQ_ENTRIES(version),
830 kiblnd_get_completion_vector(conn, cpt));
832 cq = ib_create_cq(cmid->device,
833 kiblnd_cq_completion, kiblnd_cq_event, conn,
834 IBLND_CQ_ENTRIES(version));
837 CERROR("Can't create CQ: %ld, cqe: %d\n",
838 PTR_ERR(cq), IBLND_CQ_ENTRIES(version));
844 rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
846 CERROR("Can't request completion notificiation: %d\n", rc);
850 init_qp_attr->event_handler = kiblnd_qp_event;
851 init_qp_attr->qp_context = conn;
852 init_qp_attr->cap.max_send_wr = IBLND_SEND_WRS(version);
853 init_qp_attr->cap.max_recv_wr = IBLND_RECV_WRS(version);
854 init_qp_attr->cap.max_send_sge = 1;
855 init_qp_attr->cap.max_recv_sge = 1;
856 init_qp_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
857 init_qp_attr->qp_type = IB_QPT_RC;
858 init_qp_attr->send_cq = cq;
859 init_qp_attr->recv_cq = cq;
861 conn->ibc_sched = sched;
863 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr);
865 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n",
866 rc, init_qp_attr->cap.max_send_wr,
867 init_qp_attr->cap.max_recv_wr);
871 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
873 /* 1 ref for caller and each rxmsg */
874 cfs_atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(version));
875 conn->ibc_nrx = IBLND_RX_MSGS(version);
878 for (i = 0; i < IBLND_RX_MSGS(version); i++) {
879 rc = kiblnd_post_rx(&conn->ibc_rxs[i],
880 IBLND_POSTRX_NO_CREDIT);
882 CERROR("Can't post rxmsg: %d\n", rc);
884 /* Make posted receives complete */
885 kiblnd_abort_receives(conn);
887 /* correct # of posted buffers
888 * NB locking needed now I'm racing with completion */
889 spin_lock_irqsave(&sched->ibs_lock, flags);
890 conn->ibc_nrx -= IBLND_RX_MSGS(version) - i;
891 spin_unlock_irqrestore(&sched->ibs_lock, flags);
893 /* cmid will be destroyed by CM(ofed) after cm_callback
894 * returned, so we can't refer it anymore
895 * (by kiblnd_connd()->kiblnd_destroy_conn) */
896 rdma_destroy_qp(conn->ibc_cmid);
897 conn->ibc_cmid = NULL;
899 /* Drop my own and unused rxbuffer refcounts */
900 while (i++ <= IBLND_RX_MSGS(version))
901 kiblnd_conn_decref(conn);
907 /* Init successful! */
908 LASSERT (state == IBLND_CONN_ACTIVE_CONNECT ||
909 state == IBLND_CONN_PASSIVE_WAIT);
910 conn->ibc_state = state;
913 cfs_atomic_inc(&net->ibn_nconns);
917 kiblnd_destroy_conn(conn);
919 LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr));
925 kiblnd_destroy_conn (kib_conn_t *conn)
927 struct rdma_cm_id *cmid = conn->ibc_cmid;
928 kib_peer_t *peer = conn->ibc_peer;
931 LASSERT (!cfs_in_interrupt());
932 LASSERT (cfs_atomic_read(&conn->ibc_refcount) == 0);
933 LASSERT (cfs_list_empty(&conn->ibc_early_rxs));
934 LASSERT (cfs_list_empty(&conn->ibc_tx_noops));
935 LASSERT (cfs_list_empty(&conn->ibc_tx_queue));
936 LASSERT (cfs_list_empty(&conn->ibc_tx_queue_rsrvd));
937 LASSERT (cfs_list_empty(&conn->ibc_tx_queue_nocred));
938 LASSERT (cfs_list_empty(&conn->ibc_active_txs));
939 LASSERT (conn->ibc_noops_posted == 0);
940 LASSERT (conn->ibc_nsends_posted == 0);
942 switch (conn->ibc_state) {
944 /* conn must be completely disengaged from the network */
947 case IBLND_CONN_DISCONNECTED:
948 /* connvars should have been freed already */
949 LASSERT (conn->ibc_connvars == NULL);
952 case IBLND_CONN_INIT:
956 /* conn->ibc_cmid might be destroyed by CM already */
957 if (cmid != NULL && cmid->qp != NULL)
958 rdma_destroy_qp(cmid);
960 if (conn->ibc_cq != NULL) {
961 rc = ib_destroy_cq(conn->ibc_cq);
963 CWARN("Error destroying CQ: %d\n", rc);
966 if (conn->ibc_rx_pages != NULL)
967 kiblnd_unmap_rx_descs(conn);
969 if (conn->ibc_rxs != NULL) {
970 LIBCFS_FREE(conn->ibc_rxs,
971 IBLND_RX_MSGS(conn->ibc_version) * sizeof(kib_rx_t));
974 if (conn->ibc_connvars != NULL)
975 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
977 if (conn->ibc_hdev != NULL)
978 kiblnd_hdev_decref(conn->ibc_hdev);
980 /* See CAVEAT EMPTOR above in kiblnd_create_conn */
981 if (conn->ibc_state != IBLND_CONN_INIT) {
982 kib_net_t *net = peer->ibp_ni->ni_data;
984 kiblnd_peer_decref(peer);
985 rdma_destroy_id(cmid);
986 cfs_atomic_dec(&net->ibn_nconns);
989 LIBCFS_FREE(conn, sizeof(*conn));
993 kiblnd_close_peer_conns_locked (kib_peer_t *peer, int why)
1000 cfs_list_for_each_safe (ctmp, cnxt, &peer->ibp_conns) {
1001 conn = cfs_list_entry(ctmp, kib_conn_t, ibc_list);
1003 CDEBUG(D_NET, "Closing conn -> %s, "
1004 "version: %x, reason: %d\n",
1005 libcfs_nid2str(peer->ibp_nid),
1006 conn->ibc_version, why);
1008 kiblnd_close_conn_locked(conn, why);
1016 kiblnd_close_stale_conns_locked (kib_peer_t *peer,
1017 int version, __u64 incarnation)
1024 cfs_list_for_each_safe (ctmp, cnxt, &peer->ibp_conns) {
1025 conn = cfs_list_entry(ctmp, kib_conn_t, ibc_list);
1027 if (conn->ibc_version == version &&
1028 conn->ibc_incarnation == incarnation)
1031 CDEBUG(D_NET, "Closing stale conn -> %s version: %x, "
1032 "incarnation:"LPX64"(%x, "LPX64")\n",
1033 libcfs_nid2str(peer->ibp_nid),
1034 conn->ibc_version, conn->ibc_incarnation,
1035 version, incarnation);
1037 kiblnd_close_conn_locked(conn, -ESTALE);
1045 kiblnd_close_matching_conns (lnet_ni_t *ni, lnet_nid_t nid)
1053 unsigned long flags;
1056 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1058 if (nid != LNET_NID_ANY)
1059 lo = hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers;
1062 hi = kiblnd_data.kib_peer_hash_size - 1;
1065 for (i = lo; i <= hi; i++) {
1066 cfs_list_for_each_safe (ptmp, pnxt, &kiblnd_data.kib_peers[i]) {
1068 peer = cfs_list_entry(ptmp, kib_peer_t, ibp_list);
1069 LASSERT (peer->ibp_connecting > 0 ||
1070 peer->ibp_accepting > 0 ||
1071 !cfs_list_empty(&peer->ibp_conns));
1073 if (peer->ibp_ni != ni)
1076 if (!(nid == LNET_NID_ANY || nid == peer->ibp_nid))
1079 count += kiblnd_close_peer_conns_locked(peer, 0);
1083 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1085 /* wildcards always succeed */
1086 if (nid == LNET_NID_ANY)
1089 return (count == 0) ? -ENOENT : 0;
1093 kiblnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
1095 struct libcfs_ioctl_data *data = arg;
1099 case IOC_LIBCFS_GET_PEER: {
1103 rc = kiblnd_get_peer_info(ni, data->ioc_count,
1105 data->ioc_nid = nid;
1106 data->ioc_count = count;
1110 case IOC_LIBCFS_DEL_PEER: {
1111 rc = kiblnd_del_peer(ni, data->ioc_nid);
1114 case IOC_LIBCFS_GET_CONN: {
1118 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1124 LASSERT (conn->ibc_cmid != NULL);
1125 data->ioc_nid = conn->ibc_peer->ibp_nid;
1126 if (conn->ibc_cmid->route.path_rec == NULL)
1127 data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1130 ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1131 kiblnd_conn_decref(conn);
1134 case IOC_LIBCFS_CLOSE_CONNECTION: {
1135 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1147 kiblnd_query (lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when)
1149 cfs_time_t last_alive = 0;
1150 cfs_time_t now = cfs_time_current();
1151 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1153 unsigned long flags;
1155 read_lock_irqsave(glock, flags);
1157 peer = kiblnd_find_peer_locked(nid);
1159 LASSERT (peer->ibp_connecting > 0 || /* creating conns */
1160 peer->ibp_accepting > 0 ||
1161 !cfs_list_empty(&peer->ibp_conns)); /* active conn */
1162 last_alive = peer->ibp_last_alive;
1165 read_unlock_irqrestore(glock, flags);
1167 if (last_alive != 0)
1170 /* peer is not persistent in hash, trigger peer creation
1171 * and connection establishment with a NULL tx */
1173 kiblnd_launch_tx(ni, NULL, nid);
1175 CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago\n",
1176 libcfs_nid2str(nid), peer,
1177 last_alive ? cfs_duration_sec(now - last_alive) : -1);
1182 kiblnd_free_pages(kib_pages_t *p)
1184 int npages = p->ibp_npages;
1187 for (i = 0; i < npages; i++) {
1188 if (p->ibp_pages[i] != NULL)
1189 cfs_free_page(p->ibp_pages[i]);
1192 LIBCFS_FREE(p, offsetof(kib_pages_t, ibp_pages[npages]));
1196 kiblnd_alloc_pages(kib_pages_t **pp, int cpt, int npages)
1201 LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1202 offsetof(kib_pages_t, ibp_pages[npages]));
1204 CERROR("Can't allocate descriptor for %d pages\n", npages);
1208 memset(p, 0, offsetof(kib_pages_t, ibp_pages[npages]));
1209 p->ibp_npages = npages;
1211 for (i = 0; i < npages; i++) {
1212 p->ibp_pages[i] = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1214 if (p->ibp_pages[i] == NULL) {
1215 CERROR("Can't allocate page %d of %d\n", i, npages);
1216 kiblnd_free_pages(p);
1226 kiblnd_unmap_rx_descs(kib_conn_t *conn)
1231 LASSERT (conn->ibc_rxs != NULL);
1232 LASSERT (conn->ibc_hdev != NULL);
1234 for (i = 0; i < IBLND_RX_MSGS(conn->ibc_version); i++) {
1235 rx = &conn->ibc_rxs[i];
1237 LASSERT (rx->rx_nob >= 0); /* not posted */
1239 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1240 KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1242 IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1245 kiblnd_free_pages(conn->ibc_rx_pages);
1247 conn->ibc_rx_pages = NULL;
1251 kiblnd_map_rx_descs(kib_conn_t *conn)
1259 for (pg_off = ipg = i = 0;
1260 i < IBLND_RX_MSGS(conn->ibc_version); i++) {
1261 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1262 rx = &conn->ibc_rxs[i];
1265 rx->rx_msg = (kib_msg_t *)(((char *)page_address(pg)) + pg_off);
1267 rx->rx_msgaddr = kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1268 rx->rx_msg, IBLND_MSG_SIZE,
1270 LASSERT (!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1272 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1274 CDEBUG(D_NET,"rx %d: %p "LPX64"("LPX64")\n",
1275 i, rx->rx_msg, rx->rx_msgaddr,
1276 lnet_page2phys(pg) + pg_off);
1278 pg_off += IBLND_MSG_SIZE;
1279 LASSERT (pg_off <= PAGE_SIZE);
1281 if (pg_off == PAGE_SIZE) {
1284 LASSERT (ipg <= IBLND_RX_MSG_PAGES(conn->ibc_version));
1290 kiblnd_unmap_tx_pool(kib_tx_pool_t *tpo)
1292 kib_hca_dev_t *hdev = tpo->tpo_hdev;
1296 LASSERT (tpo->tpo_pool.po_allocated == 0);
1301 for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1302 tx = &tpo->tpo_tx_descs[i];
1303 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1304 KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1306 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1309 kiblnd_hdev_decref(hdev);
1310 tpo->tpo_hdev = NULL;
1313 static kib_hca_dev_t *
1314 kiblnd_current_hdev(kib_dev_t *dev)
1316 kib_hca_dev_t *hdev;
1317 unsigned long flags;
1320 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1321 while (dev->ibd_failover) {
1322 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1324 CDEBUG(D_NET, "%s: Wait for failover\n",
1326 cfs_schedule_timeout(cfs_time_seconds(1) / 100);
1328 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1331 kiblnd_hdev_addref_locked(dev->ibd_hdev);
1332 hdev = dev->ibd_hdev;
1334 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1340 kiblnd_map_tx_pool(kib_tx_pool_t *tpo)
1342 kib_pages_t *txpgs = tpo->tpo_tx_pages;
1343 kib_pool_t *pool = &tpo->tpo_pool;
1344 kib_net_t *net = pool->po_owner->ps_net;
1345 kib_dev_t *dev = net->ibn_dev;
1352 LASSERT (net != NULL);
1354 /* pre-mapped messages are not bigger than 1 page */
1355 CLASSERT (IBLND_MSG_SIZE <= PAGE_SIZE);
1357 /* No fancy arithmetic when we do the buffer calculations */
1358 CLASSERT (PAGE_SIZE % IBLND_MSG_SIZE == 0);
1360 tpo->tpo_hdev = kiblnd_current_hdev(dev);
1362 for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1363 page = txpgs->ibp_pages[ipage];
1364 tx = &tpo->tpo_tx_descs[i];
1366 tx->tx_msg = (kib_msg_t *)(((char *)page_address(page)) +
1369 tx->tx_msgaddr = kiblnd_dma_map_single(
1370 tpo->tpo_hdev->ibh_ibdev, tx->tx_msg,
1371 IBLND_MSG_SIZE, DMA_TO_DEVICE);
1372 LASSERT (!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1374 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1376 cfs_list_add(&tx->tx_list, &pool->po_free_list);
1378 page_offset += IBLND_MSG_SIZE;
1379 LASSERT (page_offset <= PAGE_SIZE);
1381 if (page_offset == PAGE_SIZE) {
1384 LASSERT (ipage <= txpgs->ibp_npages);
1390 kiblnd_find_dma_mr(kib_hca_dev_t *hdev, __u64 addr, __u64 size)
1394 LASSERT (hdev->ibh_mrs[0] != NULL);
1396 if (hdev->ibh_nmrs == 1)
1397 return hdev->ibh_mrs[0];
1399 index = addr >> hdev->ibh_mr_shift;
1401 if (index < hdev->ibh_nmrs &&
1402 index == ((addr + size - 1) >> hdev->ibh_mr_shift))
1403 return hdev->ibh_mrs[index];
1409 kiblnd_find_rd_dma_mr(kib_hca_dev_t *hdev, kib_rdma_desc_t *rd)
1411 struct ib_mr *prev_mr;
1415 LASSERT (hdev->ibh_mrs[0] != NULL);
1417 if (*kiblnd_tunables.kib_map_on_demand > 0 &&
1418 *kiblnd_tunables.kib_map_on_demand <= rd->rd_nfrags)
1421 if (hdev->ibh_nmrs == 1)
1422 return hdev->ibh_mrs[0];
1424 for (i = 0, mr = prev_mr = NULL;
1425 i < rd->rd_nfrags; i++) {
1426 mr = kiblnd_find_dma_mr(hdev,
1427 rd->rd_frags[i].rf_addr,
1428 rd->rd_frags[i].rf_nob);
1429 if (prev_mr == NULL)
1432 if (mr == NULL || prev_mr != mr) {
1433 /* Can't covered by one single MR */
1443 kiblnd_destroy_fmr_pool(kib_fmr_pool_t *pool)
1445 LASSERT (pool->fpo_map_count == 0);
1447 if (pool->fpo_fmr_pool != NULL)
1448 ib_destroy_fmr_pool(pool->fpo_fmr_pool);
1450 if (pool->fpo_hdev != NULL)
1451 kiblnd_hdev_decref(pool->fpo_hdev);
1453 LIBCFS_FREE(pool, sizeof(kib_fmr_pool_t));
1457 kiblnd_destroy_fmr_pool_list(cfs_list_t *head)
1459 kib_fmr_pool_t *pool;
1461 while (!cfs_list_empty(head)) {
1462 pool = cfs_list_entry(head->next, kib_fmr_pool_t, fpo_list);
1463 cfs_list_del(&pool->fpo_list);
1464 kiblnd_destroy_fmr_pool(pool);
1468 static int kiblnd_fmr_pool_size(int ncpts)
1470 int size = *kiblnd_tunables.kib_fmr_pool_size / ncpts;
1472 return max(IBLND_FMR_POOL, size);
1475 static int kiblnd_fmr_flush_trigger(int ncpts)
1477 int size = *kiblnd_tunables.kib_fmr_flush_trigger / ncpts;
1479 return max(IBLND_FMR_POOL_FLUSH, size);
1483 kiblnd_create_fmr_pool(kib_fmr_poolset_t *fps, kib_fmr_pool_t **pp_fpo)
1485 /* FMR pool for RDMA */
1486 kib_dev_t *dev = fps->fps_net->ibn_dev;
1487 kib_fmr_pool_t *fpo;
1488 struct ib_fmr_pool_param param = {
1489 .max_pages_per_fmr = LNET_MAX_PAYLOAD/PAGE_SIZE,
1490 .page_shift = PAGE_SHIFT,
1491 .access = (IB_ACCESS_LOCAL_WRITE |
1492 IB_ACCESS_REMOTE_WRITE),
1493 .pool_size = fps->fps_pool_size,
1494 .dirty_watermark = fps->fps_flush_trigger,
1495 .flush_function = NULL,
1497 .cache = !!*kiblnd_tunables.kib_fmr_cache};
1500 LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1504 fpo->fpo_hdev = kiblnd_current_hdev(dev);
1506 fpo->fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd, ¶m);
1507 if (IS_ERR(fpo->fpo_fmr_pool)) {
1508 rc = PTR_ERR(fpo->fpo_fmr_pool);
1509 CERROR("Failed to create FMR pool: %d\n", rc);
1511 kiblnd_hdev_decref(fpo->fpo_hdev);
1512 LIBCFS_FREE(fpo, sizeof(kib_fmr_pool_t));
1516 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1517 fpo->fpo_owner = fps;
1524 kiblnd_fail_fmr_poolset(kib_fmr_poolset_t *fps, cfs_list_t *zombies)
1526 if (fps->fps_net == NULL) /* intialized? */
1529 spin_lock(&fps->fps_lock);
1531 while (!cfs_list_empty(&fps->fps_pool_list)) {
1532 kib_fmr_pool_t *fpo = cfs_list_entry(fps->fps_pool_list.next,
1533 kib_fmr_pool_t, fpo_list);
1534 fpo->fpo_failed = 1;
1535 cfs_list_del(&fpo->fpo_list);
1536 if (fpo->fpo_map_count == 0)
1537 cfs_list_add(&fpo->fpo_list, zombies);
1539 cfs_list_add(&fpo->fpo_list, &fps->fps_failed_pool_list);
1542 spin_unlock(&fps->fps_lock);
1546 kiblnd_fini_fmr_poolset(kib_fmr_poolset_t *fps)
1548 if (fps->fps_net != NULL) { /* initialized? */
1549 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1550 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1555 kiblnd_init_fmr_poolset(kib_fmr_poolset_t *fps, int cpt, kib_net_t *net,
1556 int pool_size, int flush_trigger)
1558 kib_fmr_pool_t *fpo;
1561 memset(fps, 0, sizeof(kib_fmr_poolset_t));
1565 fps->fps_pool_size = pool_size;
1566 fps->fps_flush_trigger = flush_trigger;
1567 spin_lock_init(&fps->fps_lock);
1568 CFS_INIT_LIST_HEAD(&fps->fps_pool_list);
1569 CFS_INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1571 rc = kiblnd_create_fmr_pool(fps, &fpo);
1573 cfs_list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1579 kiblnd_fmr_pool_is_idle(kib_fmr_pool_t *fpo, cfs_time_t now)
1581 if (fpo->fpo_map_count != 0) /* still in use */
1583 if (fpo->fpo_failed)
1585 return cfs_time_aftereq(now, fpo->fpo_deadline);
1589 kiblnd_fmr_pool_unmap(kib_fmr_t *fmr, int status)
1591 CFS_LIST_HEAD (zombies);
1592 kib_fmr_pool_t *fpo = fmr->fmr_pool;
1593 kib_fmr_poolset_t *fps = fpo->fpo_owner;
1594 cfs_time_t now = cfs_time_current();
1595 kib_fmr_pool_t *tmp;
1598 rc = ib_fmr_pool_unmap(fmr->fmr_pfmr);
1602 rc = ib_flush_fmr_pool(fpo->fpo_fmr_pool);
1606 fmr->fmr_pool = NULL;
1607 fmr->fmr_pfmr = NULL;
1609 spin_lock(&fps->fps_lock);
1610 fpo->fpo_map_count --; /* decref the pool */
1612 cfs_list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1613 /* the first pool is persistent */
1614 if (fps->fps_pool_list.next == &fpo->fpo_list)
1617 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1618 cfs_list_move(&fpo->fpo_list, &zombies);
1619 fps->fps_version ++;
1622 spin_unlock(&fps->fps_lock);
1624 if (!cfs_list_empty(&zombies))
1625 kiblnd_destroy_fmr_pool_list(&zombies);
1629 kiblnd_fmr_pool_map(kib_fmr_poolset_t *fps, __u64 *pages, int npages,
1630 __u64 iov, kib_fmr_t *fmr)
1632 struct ib_pool_fmr *pfmr;
1633 kib_fmr_pool_t *fpo;
1638 spin_lock(&fps->fps_lock);
1639 version = fps->fps_version;
1640 cfs_list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
1641 fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1642 fpo->fpo_map_count++;
1643 spin_unlock(&fps->fps_lock);
1645 pfmr = ib_fmr_pool_map_phys(fpo->fpo_fmr_pool,
1646 pages, npages, iov);
1647 if (likely(!IS_ERR(pfmr))) {
1648 fmr->fmr_pool = fpo;
1649 fmr->fmr_pfmr = pfmr;
1653 spin_lock(&fps->fps_lock);
1654 fpo->fpo_map_count--;
1655 if (PTR_ERR(pfmr) != -EAGAIN) {
1656 spin_unlock(&fps->fps_lock);
1657 return PTR_ERR(pfmr);
1660 /* EAGAIN and ... */
1661 if (version != fps->fps_version) {
1662 spin_unlock(&fps->fps_lock);
1667 if (fps->fps_increasing) {
1668 spin_unlock(&fps->fps_lock);
1669 CDEBUG(D_NET, "Another thread is allocating new "
1670 "FMR pool, waiting for her to complete\n");
1676 if (cfs_time_before(cfs_time_current(), fps->fps_next_retry)) {
1677 /* someone failed recently */
1678 spin_unlock(&fps->fps_lock);
1682 fps->fps_increasing = 1;
1683 spin_unlock(&fps->fps_lock);
1685 CDEBUG(D_NET, "Allocate new FMR pool\n");
1686 rc = kiblnd_create_fmr_pool(fps, &fpo);
1687 spin_lock(&fps->fps_lock);
1688 fps->fps_increasing = 0;
1691 cfs_list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1693 fps->fps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1695 spin_unlock(&fps->fps_lock);
1701 kiblnd_fini_pool(kib_pool_t *pool)
1703 LASSERT (cfs_list_empty(&pool->po_free_list));
1704 LASSERT (pool->po_allocated == 0);
1706 CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
1710 kiblnd_init_pool(kib_poolset_t *ps, kib_pool_t *pool, int size)
1712 CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
1714 memset(pool, 0, sizeof(kib_pool_t));
1715 CFS_INIT_LIST_HEAD(&pool->po_free_list);
1716 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1717 pool->po_owner = ps;
1718 pool->po_size = size;
1722 kiblnd_destroy_pool_list(cfs_list_t *head)
1726 while (!cfs_list_empty(head)) {
1727 pool = cfs_list_entry(head->next, kib_pool_t, po_list);
1728 cfs_list_del(&pool->po_list);
1730 LASSERT (pool->po_owner != NULL);
1731 pool->po_owner->ps_pool_destroy(pool);
1736 kiblnd_fail_poolset(kib_poolset_t *ps, cfs_list_t *zombies)
1738 if (ps->ps_net == NULL) /* intialized? */
1741 spin_lock(&ps->ps_lock);
1742 while (!cfs_list_empty(&ps->ps_pool_list)) {
1743 kib_pool_t *po = cfs_list_entry(ps->ps_pool_list.next,
1744 kib_pool_t, po_list);
1746 cfs_list_del(&po->po_list);
1747 if (po->po_allocated == 0)
1748 cfs_list_add(&po->po_list, zombies);
1750 cfs_list_add(&po->po_list, &ps->ps_failed_pool_list);
1752 spin_unlock(&ps->ps_lock);
1756 kiblnd_fini_poolset(kib_poolset_t *ps)
1758 if (ps->ps_net != NULL) { /* initialized? */
1759 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
1760 kiblnd_destroy_pool_list(&ps->ps_pool_list);
1765 kiblnd_init_poolset(kib_poolset_t *ps, int cpt,
1766 kib_net_t *net, char *name, int size,
1767 kib_ps_pool_create_t po_create,
1768 kib_ps_pool_destroy_t po_destroy,
1769 kib_ps_node_init_t nd_init,
1770 kib_ps_node_fini_t nd_fini)
1775 memset(ps, 0, sizeof(kib_poolset_t));
1779 ps->ps_pool_create = po_create;
1780 ps->ps_pool_destroy = po_destroy;
1781 ps->ps_node_init = nd_init;
1782 ps->ps_node_fini = nd_fini;
1783 ps->ps_pool_size = size;
1784 strncpy(ps->ps_name, name, IBLND_POOL_NAME_LEN);
1785 spin_lock_init(&ps->ps_lock);
1786 CFS_INIT_LIST_HEAD(&ps->ps_pool_list);
1787 CFS_INIT_LIST_HEAD(&ps->ps_failed_pool_list);
1789 rc = ps->ps_pool_create(ps, size, &pool);
1791 cfs_list_add(&pool->po_list, &ps->ps_pool_list);
1793 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
1799 kiblnd_pool_is_idle(kib_pool_t *pool, cfs_time_t now)
1801 if (pool->po_allocated != 0) /* still in use */
1803 if (pool->po_failed)
1805 return cfs_time_aftereq(now, pool->po_deadline);
1809 kiblnd_pool_free_node(kib_pool_t *pool, cfs_list_t *node)
1811 CFS_LIST_HEAD (zombies);
1812 kib_poolset_t *ps = pool->po_owner;
1814 cfs_time_t now = cfs_time_current();
1816 spin_lock(&ps->ps_lock);
1818 if (ps->ps_node_fini != NULL)
1819 ps->ps_node_fini(pool, node);
1821 LASSERT (pool->po_allocated > 0);
1822 cfs_list_add(node, &pool->po_free_list);
1823 pool->po_allocated --;
1825 cfs_list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
1826 /* the first pool is persistent */
1827 if (ps->ps_pool_list.next == &pool->po_list)
1830 if (kiblnd_pool_is_idle(pool, now))
1831 cfs_list_move(&pool->po_list, &zombies);
1833 spin_unlock(&ps->ps_lock);
1835 if (!cfs_list_empty(&zombies))
1836 kiblnd_destroy_pool_list(&zombies);
1840 kiblnd_pool_alloc_node(kib_poolset_t *ps)
1847 spin_lock(&ps->ps_lock);
1848 cfs_list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
1849 if (cfs_list_empty(&pool->po_free_list))
1852 pool->po_allocated ++;
1853 pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE);
1854 node = pool->po_free_list.next;
1857 if (ps->ps_node_init != NULL) {
1858 /* still hold the lock */
1859 ps->ps_node_init(pool, node);
1861 spin_unlock(&ps->ps_lock);
1865 /* no available tx pool and ... */
1866 if (ps->ps_increasing) {
1867 /* another thread is allocating a new pool */
1868 spin_unlock(&ps->ps_lock);
1869 CDEBUG(D_NET, "Another thread is allocating new "
1870 "%s pool, waiting for her to complete\n",
1876 if (cfs_time_before(cfs_time_current(), ps->ps_next_retry)) {
1877 /* someone failed recently */
1878 spin_unlock(&ps->ps_lock);
1882 ps->ps_increasing = 1;
1883 spin_unlock(&ps->ps_lock);
1885 CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
1887 rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
1889 spin_lock(&ps->ps_lock);
1890 ps->ps_increasing = 0;
1892 cfs_list_add_tail(&pool->po_list, &ps->ps_pool_list);
1894 ps->ps_next_retry = cfs_time_shift(IBLND_POOL_RETRY);
1895 CERROR("Can't allocate new %s pool because out of memory\n",
1898 spin_unlock(&ps->ps_lock);
1904 kiblnd_pmr_pool_unmap(kib_phys_mr_t *pmr)
1906 kib_pmr_pool_t *ppo = pmr->pmr_pool;
1907 struct ib_mr *mr = pmr->pmr_mr;
1910 kiblnd_pool_free_node(&ppo->ppo_pool, &pmr->pmr_list);
1916 kiblnd_pmr_pool_map(kib_pmr_poolset_t *pps, kib_hca_dev_t *hdev,
1917 kib_rdma_desc_t *rd, __u64 *iova, kib_phys_mr_t **pp_pmr)
1924 node = kiblnd_pool_alloc_node(&pps->pps_poolset);
1926 CERROR("Failed to allocate PMR descriptor\n");
1930 pmr = container_of(node, kib_phys_mr_t, pmr_list);
1931 if (pmr->pmr_pool->ppo_hdev != hdev) {
1932 kiblnd_pool_free_node(&pmr->pmr_pool->ppo_pool, node);
1936 for (i = 0; i < rd->rd_nfrags; i ++) {
1937 pmr->pmr_ipb[i].addr = rd->rd_frags[i].rf_addr;
1938 pmr->pmr_ipb[i].size = rd->rd_frags[i].rf_nob;
1941 pmr->pmr_mr = ib_reg_phys_mr(hdev->ibh_pd,
1942 pmr->pmr_ipb, rd->rd_nfrags,
1943 IB_ACCESS_LOCAL_WRITE |
1944 IB_ACCESS_REMOTE_WRITE,
1946 if (!IS_ERR(pmr->pmr_mr)) {
1947 pmr->pmr_iova = *iova;
1952 rc = PTR_ERR(pmr->pmr_mr);
1953 CERROR("Failed ib_reg_phys_mr: %d\n", rc);
1956 kiblnd_pool_free_node(&pmr->pmr_pool->ppo_pool, node);
1962 kiblnd_destroy_pmr_pool(kib_pool_t *pool)
1964 kib_pmr_pool_t *ppo = container_of(pool, kib_pmr_pool_t, ppo_pool);
1967 LASSERT (pool->po_allocated == 0);
1969 while (!cfs_list_empty(&pool->po_free_list)) {
1970 pmr = cfs_list_entry(pool->po_free_list.next,
1971 kib_phys_mr_t, pmr_list);
1973 LASSERT (pmr->pmr_mr == NULL);
1974 cfs_list_del(&pmr->pmr_list);
1976 if (pmr->pmr_ipb != NULL) {
1977 LIBCFS_FREE(pmr->pmr_ipb,
1978 IBLND_MAX_RDMA_FRAGS *
1979 sizeof(struct ib_phys_buf));
1982 LIBCFS_FREE(pmr, sizeof(kib_phys_mr_t));
1985 kiblnd_fini_pool(pool);
1986 if (ppo->ppo_hdev != NULL)
1987 kiblnd_hdev_decref(ppo->ppo_hdev);
1989 LIBCFS_FREE(ppo, sizeof(kib_pmr_pool_t));
1992 static inline int kiblnd_pmr_pool_size(int ncpts)
1994 int size = *kiblnd_tunables.kib_pmr_pool_size / ncpts;
1996 return max(IBLND_PMR_POOL, size);
2000 kiblnd_create_pmr_pool(kib_poolset_t *ps, int size, kib_pool_t **pp_po)
2002 struct kib_pmr_pool *ppo;
2003 struct kib_pool *pool;
2007 LIBCFS_CPT_ALLOC(ppo, lnet_cpt_table(),
2008 ps->ps_cpt, sizeof(kib_pmr_pool_t));
2010 CERROR("Failed to allocate PMR pool\n");
2014 pool = &ppo->ppo_pool;
2015 kiblnd_init_pool(ps, pool, size);
2017 for (i = 0; i < size; i++) {
2018 LIBCFS_CPT_ALLOC(pmr, lnet_cpt_table(),
2019 ps->ps_cpt, sizeof(kib_phys_mr_t));
2023 pmr->pmr_pool = ppo;
2024 LIBCFS_CPT_ALLOC(pmr->pmr_ipb, lnet_cpt_table(), ps->ps_cpt,
2025 IBLND_MAX_RDMA_FRAGS * sizeof(*pmr->pmr_ipb));
2026 if (pmr->pmr_ipb == NULL)
2029 cfs_list_add(&pmr->pmr_list, &pool->po_free_list);
2033 ps->ps_pool_destroy(pool);
2037 ppo->ppo_hdev = kiblnd_current_hdev(ps->ps_net->ibn_dev);
2043 kiblnd_destroy_tx_pool(kib_pool_t *pool)
2045 kib_tx_pool_t *tpo = container_of(pool, kib_tx_pool_t, tpo_pool);
2048 LASSERT (pool->po_allocated == 0);
2050 if (tpo->tpo_tx_pages != NULL) {
2051 kiblnd_unmap_tx_pool(tpo);
2052 kiblnd_free_pages(tpo->tpo_tx_pages);
2055 if (tpo->tpo_tx_descs == NULL)
2058 for (i = 0; i < pool->po_size; i++) {
2059 kib_tx_t *tx = &tpo->tpo_tx_descs[i];
2061 cfs_list_del(&tx->tx_list);
2062 if (tx->tx_pages != NULL)
2063 LIBCFS_FREE(tx->tx_pages,
2065 sizeof(*tx->tx_pages));
2066 if (tx->tx_frags != NULL)
2067 LIBCFS_FREE(tx->tx_frags,
2068 IBLND_MAX_RDMA_FRAGS *
2069 sizeof(*tx->tx_frags));
2070 if (tx->tx_wrq != NULL)
2071 LIBCFS_FREE(tx->tx_wrq,
2072 (1 + IBLND_MAX_RDMA_FRAGS) *
2073 sizeof(*tx->tx_wrq));
2074 if (tx->tx_sge != NULL)
2075 LIBCFS_FREE(tx->tx_sge,
2076 (1 + IBLND_MAX_RDMA_FRAGS) *
2077 sizeof(*tx->tx_sge));
2078 if (tx->tx_rd != NULL)
2079 LIBCFS_FREE(tx->tx_rd,
2080 offsetof(kib_rdma_desc_t,
2081 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2084 LIBCFS_FREE(tpo->tpo_tx_descs,
2085 pool->po_size * sizeof(kib_tx_t));
2087 kiblnd_fini_pool(pool);
2088 LIBCFS_FREE(tpo, sizeof(kib_tx_pool_t));
2091 static int kiblnd_tx_pool_size(int ncpts)
2093 int ntx = *kiblnd_tunables.kib_ntx / ncpts;
2095 return max(IBLND_TX_POOL, ntx);
2099 kiblnd_create_tx_pool(kib_poolset_t *ps, int size, kib_pool_t **pp_po)
2106 LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2108 CERROR("Failed to allocate TX pool\n");
2112 pool = &tpo->tpo_pool;
2113 kiblnd_init_pool(ps, pool, size);
2114 tpo->tpo_tx_descs = NULL;
2115 tpo->tpo_tx_pages = NULL;
2117 npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2118 if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg) != 0) {
2119 CERROR("Can't allocate tx pages: %d\n", npg);
2120 LIBCFS_FREE(tpo, sizeof(kib_tx_pool_t));
2124 LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2125 size * sizeof(kib_tx_t));
2126 if (tpo->tpo_tx_descs == NULL) {
2127 CERROR("Can't allocate %d tx descriptors\n", size);
2128 ps->ps_pool_destroy(pool);
2132 memset(tpo->tpo_tx_descs, 0, size * sizeof(kib_tx_t));
2134 for (i = 0; i < size; i++) {
2135 kib_tx_t *tx = &tpo->tpo_tx_descs[i];
2138 if (ps->ps_net->ibn_fmr_ps != NULL) {
2139 LIBCFS_CPT_ALLOC(tx->tx_pages,
2140 lnet_cpt_table(), ps->ps_cpt,
2141 LNET_MAX_IOV * sizeof(*tx->tx_pages));
2142 if (tx->tx_pages == NULL)
2146 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2147 IBLND_MAX_RDMA_FRAGS * sizeof(*tx->tx_frags));
2148 if (tx->tx_frags == NULL)
2151 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS);
2153 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2154 (1 + IBLND_MAX_RDMA_FRAGS) *
2155 sizeof(*tx->tx_wrq));
2156 if (tx->tx_wrq == NULL)
2159 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2160 (1 + IBLND_MAX_RDMA_FRAGS) *
2161 sizeof(*tx->tx_sge));
2162 if (tx->tx_sge == NULL)
2165 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2166 offsetof(kib_rdma_desc_t,
2167 rd_frags[IBLND_MAX_RDMA_FRAGS]));
2168 if (tx->tx_rd == NULL)
2173 kiblnd_map_tx_pool(tpo);
2178 ps->ps_pool_destroy(pool);
2183 kiblnd_tx_init(kib_pool_t *pool, cfs_list_t *node)
2185 kib_tx_poolset_t *tps = container_of(pool->po_owner, kib_tx_poolset_t,
2187 kib_tx_t *tx = cfs_list_entry(node, kib_tx_t, tx_list);
2189 tx->tx_cookie = tps->tps_next_tx_cookie ++;
2193 kiblnd_net_fini_pools(kib_net_t *net)
2197 cfs_cpt_for_each(i, lnet_cpt_table()) {
2198 kib_tx_poolset_t *tps;
2199 kib_fmr_poolset_t *fps;
2200 kib_pmr_poolset_t *pps;
2202 if (net->ibn_tx_ps != NULL) {
2203 tps = net->ibn_tx_ps[i];
2204 kiblnd_fini_poolset(&tps->tps_poolset);
2207 if (net->ibn_fmr_ps != NULL) {
2208 fps = net->ibn_fmr_ps[i];
2209 kiblnd_fini_fmr_poolset(fps);
2212 if (net->ibn_pmr_ps != NULL) {
2213 pps = net->ibn_pmr_ps[i];
2214 kiblnd_fini_poolset(&pps->pps_poolset);
2218 if (net->ibn_tx_ps != NULL) {
2219 cfs_percpt_free(net->ibn_tx_ps);
2220 net->ibn_tx_ps = NULL;
2223 if (net->ibn_fmr_ps != NULL) {
2224 cfs_percpt_free(net->ibn_fmr_ps);
2225 net->ibn_fmr_ps = NULL;
2228 if (net->ibn_pmr_ps != NULL) {
2229 cfs_percpt_free(net->ibn_pmr_ps);
2230 net->ibn_pmr_ps = NULL;
2235 kiblnd_net_init_pools(kib_net_t *net, __u32 *cpts, int ncpts)
2237 unsigned long flags;
2242 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2243 if (*kiblnd_tunables.kib_map_on_demand == 0 &&
2244 net->ibn_dev->ibd_hdev->ibh_nmrs == 1) {
2245 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2247 goto create_tx_pool;
2250 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2252 if (*kiblnd_tunables.kib_fmr_pool_size <
2253 *kiblnd_tunables.kib_ntx / 4) {
2254 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2255 *kiblnd_tunables.kib_fmr_pool_size,
2256 *kiblnd_tunables.kib_ntx / 4);
2261 /* TX pool must be created later than FMR/PMR, see LU-2268
2263 LASSERT(net->ibn_tx_ps == NULL);
2265 /* premapping can fail if ibd_nmr > 1, so we always create
2266 * FMR/PMR pool and map-on-demand if premapping failed */
2268 net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2269 sizeof(kib_fmr_poolset_t));
2270 if (net->ibn_fmr_ps == NULL) {
2271 CERROR("Failed to allocate FMR pool array\n");
2276 for (i = 0; i < ncpts; i++) {
2277 cpt = (cpts == NULL) ? i : cpts[i];
2278 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, net,
2279 kiblnd_fmr_pool_size(ncpts),
2280 kiblnd_fmr_flush_trigger(ncpts));
2281 if (rc == -ENOSYS && i == 0) /* no FMR */
2282 break; /* create PMR pool */
2284 if (rc != 0) { /* a real error */
2285 CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2292 LASSERT(i == ncpts);
2293 goto create_tx_pool;
2296 cfs_percpt_free(net->ibn_fmr_ps);
2297 net->ibn_fmr_ps = NULL;
2299 CWARN("Device does not support FMR, failing back to PMR\n");
2301 if (*kiblnd_tunables.kib_pmr_pool_size <
2302 *kiblnd_tunables.kib_ntx / 4) {
2303 CERROR("Can't set pmr pool size (%d) < ntx / 4(%d)\n",
2304 *kiblnd_tunables.kib_pmr_pool_size,
2305 *kiblnd_tunables.kib_ntx / 4);
2310 net->ibn_pmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2311 sizeof(kib_pmr_poolset_t));
2312 if (net->ibn_pmr_ps == NULL) {
2313 CERROR("Failed to allocate PMR pool array\n");
2318 for (i = 0; i < ncpts; i++) {
2319 cpt = (cpts == NULL) ? i : cpts[i];
2320 rc = kiblnd_init_poolset(&net->ibn_pmr_ps[cpt]->pps_poolset,
2322 kiblnd_pmr_pool_size(ncpts),
2323 kiblnd_create_pmr_pool,
2324 kiblnd_destroy_pmr_pool, NULL, NULL);
2326 CERROR("Can't initialize PMR pool for CPT %d: %d\n",
2333 net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2334 sizeof(kib_tx_poolset_t));
2335 if (net->ibn_tx_ps == NULL) {
2336 CERROR("Failed to allocate tx pool array\n");
2341 for (i = 0; i < ncpts; i++) {
2342 cpt = (cpts == NULL) ? i : cpts[i];
2343 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2345 kiblnd_tx_pool_size(ncpts),
2346 kiblnd_create_tx_pool,
2347 kiblnd_destroy_tx_pool,
2348 kiblnd_tx_init, NULL);
2350 CERROR("Can't initialize TX pool for CPT %d: %d\n",
2358 kiblnd_net_fini_pools(net);
2364 kiblnd_hdev_get_attr(kib_hca_dev_t *hdev)
2366 struct ib_device_attr *attr;
2369 /* It's safe to assume a HCA can handle a page size
2370 * matching that of the native system */
2371 hdev->ibh_page_shift = PAGE_SHIFT;
2372 hdev->ibh_page_size = 1 << PAGE_SHIFT;
2373 hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1);
2375 LIBCFS_ALLOC(attr, sizeof(*attr));
2377 CERROR("Out of memory\n");
2381 rc = ib_query_device(hdev->ibh_ibdev, attr);
2383 hdev->ibh_mr_size = attr->max_mr_size;
2385 LIBCFS_FREE(attr, sizeof(*attr));
2388 CERROR("Failed to query IB device: %d\n", rc);
2392 if (hdev->ibh_mr_size == ~0ULL) {
2393 hdev->ibh_mr_shift = 64;
2397 for (hdev->ibh_mr_shift = 0;
2398 hdev->ibh_mr_shift < 64; hdev->ibh_mr_shift ++) {
2399 if (hdev->ibh_mr_size == (1ULL << hdev->ibh_mr_shift) ||
2400 hdev->ibh_mr_size == (1ULL << hdev->ibh_mr_shift) - 1)
2404 CERROR("Invalid mr size: "LPX64"\n", hdev->ibh_mr_size);
2409 kiblnd_hdev_cleanup_mrs(kib_hca_dev_t *hdev)
2413 if (hdev->ibh_nmrs == 0 || hdev->ibh_mrs == NULL)
2416 for (i = 0; i < hdev->ibh_nmrs; i++) {
2417 if (hdev->ibh_mrs[i] == NULL)
2420 ib_dereg_mr(hdev->ibh_mrs[i]);
2423 LIBCFS_FREE(hdev->ibh_mrs, sizeof(*hdev->ibh_mrs) * hdev->ibh_nmrs);
2424 hdev->ibh_mrs = NULL;
2429 kiblnd_hdev_destroy(kib_hca_dev_t *hdev)
2431 kiblnd_hdev_cleanup_mrs(hdev);
2433 if (hdev->ibh_pd != NULL)
2434 ib_dealloc_pd(hdev->ibh_pd);
2436 if (hdev->ibh_cmid != NULL)
2437 rdma_destroy_id(hdev->ibh_cmid);
2439 LIBCFS_FREE(hdev, sizeof(*hdev));
2443 kiblnd_hdev_setup_mrs(kib_hca_dev_t *hdev)
2450 int acflags = IB_ACCESS_LOCAL_WRITE |
2451 IB_ACCESS_REMOTE_WRITE;
2453 rc = kiblnd_hdev_get_attr(hdev);
2457 if (hdev->ibh_mr_shift == 64) {
2458 LIBCFS_ALLOC(hdev->ibh_mrs, 1 * sizeof(*hdev->ibh_mrs));
2459 if (hdev->ibh_mrs == NULL) {
2460 CERROR("Failed to allocate MRs table\n");
2464 hdev->ibh_mrs[0] = NULL;
2467 mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2469 CERROR("Failed ib_get_dma_mr : %ld\n", PTR_ERR(mr));
2470 kiblnd_hdev_cleanup_mrs(hdev);
2474 hdev->ibh_mrs[0] = mr;
2479 mr_size = (1ULL << hdev->ibh_mr_shift);
2480 mm_size = (unsigned long)high_memory - PAGE_OFFSET;
2482 hdev->ibh_nmrs = (int)((mm_size + mr_size - 1) >> hdev->ibh_mr_shift);
2484 if (hdev->ibh_mr_shift < 32 || hdev->ibh_nmrs > 1024) {
2485 /* it's 4T..., assume we will re-code at that time */
2486 CERROR("Can't support memory size: x"LPX64
2487 " with MR size: x"LPX64"\n", mm_size, mr_size);
2491 /* create an array of MRs to cover all memory */
2492 LIBCFS_ALLOC(hdev->ibh_mrs, sizeof(*hdev->ibh_mrs) * hdev->ibh_nmrs);
2493 if (hdev->ibh_mrs == NULL) {
2494 CERROR("Failed to allocate MRs' table\n");
2498 memset(hdev->ibh_mrs, 0, sizeof(*hdev->ibh_mrs) * hdev->ibh_nmrs);
2500 for (i = 0; i < hdev->ibh_nmrs; i++) {
2501 struct ib_phys_buf ipb;
2504 ipb.size = hdev->ibh_mr_size;
2505 ipb.addr = i * mr_size;
2508 mr = ib_reg_phys_mr(hdev->ibh_pd, &ipb, 1, acflags, &iova);
2510 CERROR("Failed ib_reg_phys_mr addr "LPX64
2511 " size "LPX64" : %ld\n",
2512 ipb.addr, ipb.size, PTR_ERR(mr));
2513 kiblnd_hdev_cleanup_mrs(hdev);
2517 LASSERT (iova == ipb.addr);
2519 hdev->ibh_mrs[i] = mr;
2523 if (hdev->ibh_mr_size != ~0ULL || hdev->ibh_nmrs != 1)
2524 LCONSOLE_INFO("Register global MR array, MR size: "
2525 LPX64", array size: %d\n",
2526 hdev->ibh_mr_size, hdev->ibh_nmrs);
2531 kiblnd_dummy_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2537 kiblnd_dev_need_failover(kib_dev_t *dev)
2539 struct rdma_cm_id *cmid;
2540 struct sockaddr_in srcaddr;
2541 struct sockaddr_in dstaddr;
2544 if (dev->ibd_hdev == NULL || /* initializing */
2545 dev->ibd_hdev->ibh_cmid == NULL || /* listener is dead */
2546 *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2549 /* XXX: it's UGLY, but I don't have better way to find
2550 * ib-bonding HCA failover because:
2552 * a. no reliable CM event for HCA failover...
2553 * b. no OFED API to get ib_device for current net_device...
2555 * We have only two choices at this point:
2557 * a. rdma_bind_addr(), it will conflict with listener cmid
2558 * b. rdma_resolve_addr() to zero addr */
2559 cmid = kiblnd_rdma_create_id(kiblnd_dummy_callback, dev, RDMA_PS_TCP,
2563 CERROR("Failed to create cmid for failover: %d\n", rc);
2567 memset(&srcaddr, 0, sizeof(srcaddr));
2568 srcaddr.sin_family = AF_INET;
2569 srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2571 memset(&dstaddr, 0, sizeof(dstaddr));
2572 dstaddr.sin_family = AF_INET;
2573 rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2574 (struct sockaddr *)&dstaddr, 1);
2575 if (rc != 0 || cmid->device == NULL) {
2576 CERROR("Failed to bind %s:%u.%u.%u.%u to device(%p): %d\n",
2577 dev->ibd_ifname, HIPQUAD(dev->ibd_ifip),
2579 rdma_destroy_id(cmid);
2583 if (dev->ibd_hdev->ibh_ibdev == cmid->device) {
2584 /* don't need device failover */
2585 rdma_destroy_id(cmid);
2593 kiblnd_dev_failover(kib_dev_t *dev)
2595 CFS_LIST_HEAD (zombie_tpo);
2596 CFS_LIST_HEAD (zombie_ppo);
2597 CFS_LIST_HEAD (zombie_fpo);
2598 struct rdma_cm_id *cmid = NULL;
2599 kib_hca_dev_t *hdev = NULL;
2603 struct sockaddr_in addr;
2604 unsigned long flags;
2608 LASSERT (*kiblnd_tunables.kib_dev_failover > 1 ||
2609 dev->ibd_can_failover ||
2610 dev->ibd_hdev == NULL);
2612 rc = kiblnd_dev_need_failover(dev);
2616 if (dev->ibd_hdev != NULL &&
2617 dev->ibd_hdev->ibh_cmid != NULL) {
2618 /* XXX it's not good to close old listener at here,
2619 * because we can fail to create new listener.
2620 * But we have to close it now, otherwise rdma_bind_addr
2621 * will return EADDRINUSE... How crap! */
2622 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2624 cmid = dev->ibd_hdev->ibh_cmid;
2625 /* make next schedule of kiblnd_dev_need_failover()
2626 * return 1 for me */
2627 dev->ibd_hdev->ibh_cmid = NULL;
2628 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2630 rdma_destroy_id(cmid);
2633 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, dev, RDMA_PS_TCP,
2637 CERROR("Failed to create cmid for failover: %d\n", rc);
2641 memset(&addr, 0, sizeof(addr));
2642 addr.sin_family = AF_INET;
2643 addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2644 addr.sin_port = htons(*kiblnd_tunables.kib_service);
2646 /* Bind to failover device or port */
2647 rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
2648 if (rc != 0 || cmid->device == NULL) {
2649 CERROR("Failed to bind %s:%u.%u.%u.%u to device(%p): %d\n",
2650 dev->ibd_ifname, HIPQUAD(dev->ibd_ifip),
2652 rdma_destroy_id(cmid);
2656 LIBCFS_ALLOC(hdev, sizeof(*hdev));
2658 CERROR("Failed to allocate kib_hca_dev\n");
2659 rdma_destroy_id(cmid);
2664 atomic_set(&hdev->ibh_ref, 1);
2665 hdev->ibh_dev = dev;
2666 hdev->ibh_cmid = cmid;
2667 hdev->ibh_ibdev = cmid->device;
2669 pd = ib_alloc_pd(cmid->device);
2672 CERROR("Can't allocate PD: %d\n", rc);
2678 rc = rdma_listen(cmid, 0);
2680 CERROR("Can't start new listener: %d\n", rc);
2684 rc = kiblnd_hdev_setup_mrs(hdev);
2686 CERROR("Can't setup device: %d\n", rc);
2690 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2692 old = dev->ibd_hdev;
2693 dev->ibd_hdev = hdev; /* take over the refcount */
2696 cfs_list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
2697 cfs_cpt_for_each(i, lnet_cpt_table()) {
2698 kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
2701 if (net->ibn_fmr_ps != NULL) {
2702 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
2705 } else if (net->ibn_pmr_ps != NULL) {
2706 kiblnd_fail_poolset(&net->ibn_pmr_ps[i]->
2707 pps_poolset, &zombie_ppo);
2712 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2714 if (!cfs_list_empty(&zombie_tpo))
2715 kiblnd_destroy_pool_list(&zombie_tpo);
2716 if (!cfs_list_empty(&zombie_ppo))
2717 kiblnd_destroy_pool_list(&zombie_ppo);
2718 if (!cfs_list_empty(&zombie_fpo))
2719 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
2721 kiblnd_hdev_decref(hdev);
2724 dev->ibd_failed_failover++;
2726 dev->ibd_failed_failover = 0;
2732 kiblnd_destroy_dev (kib_dev_t *dev)
2734 LASSERT (dev->ibd_nnets == 0);
2735 LASSERT (cfs_list_empty(&dev->ibd_nets));
2737 cfs_list_del(&dev->ibd_fail_list);
2738 cfs_list_del(&dev->ibd_list);
2740 if (dev->ibd_hdev != NULL)
2741 kiblnd_hdev_decref(dev->ibd_hdev);
2743 LIBCFS_FREE(dev, sizeof(*dev));
2747 kiblnd_create_dev(char *ifname)
2749 struct net_device *netdev;
2756 rc = libcfs_ipif_query(ifname, &up, &ip, &netmask);
2758 CERROR("Can't query IPoIB interface %s: %d\n",
2764 CERROR("Can't query IPoIB interface %s: it's down\n", ifname);
2768 LIBCFS_ALLOC(dev, sizeof(*dev));
2772 memset(dev, 0, sizeof(*dev));
2773 #ifdef HAVE_DEV_GET_BY_NAME_2ARG
2774 netdev = dev_get_by_name(&init_net, ifname);
2776 netdev = dev_get_by_name(ifname);
2778 if (netdev == NULL) {
2779 dev->ibd_can_failover = 0;
2781 dev->ibd_can_failover = !!(netdev->flags & IFF_MASTER);
2785 CFS_INIT_LIST_HEAD(&dev->ibd_nets);
2786 CFS_INIT_LIST_HEAD(&dev->ibd_list); /* not yet in kib_devs */
2787 CFS_INIT_LIST_HEAD(&dev->ibd_fail_list);
2789 strcpy(&dev->ibd_ifname[0], ifname);
2791 /* initialize the device */
2792 rc = kiblnd_dev_failover(dev);
2794 CERROR("Can't initialize device: %d\n", rc);
2795 LIBCFS_FREE(dev, sizeof(*dev));
2799 cfs_list_add_tail(&dev->ibd_list,
2800 &kiblnd_data.kib_devs);
2805 kiblnd_base_shutdown(void)
2807 struct kib_sched_info *sched;
2810 LASSERT (cfs_list_empty(&kiblnd_data.kib_devs));
2812 CDEBUG(D_MALLOC, "before LND base cleanup: kmem %d\n",
2813 cfs_atomic_read(&libcfs_kmemory));
2815 switch (kiblnd_data.kib_init) {
2819 case IBLND_INIT_ALL:
2820 case IBLND_INIT_DATA:
2821 LASSERT (kiblnd_data.kib_peers != NULL);
2822 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) {
2823 LASSERT (cfs_list_empty(&kiblnd_data.kib_peers[i]));
2825 LASSERT (cfs_list_empty(&kiblnd_data.kib_connd_zombies));
2826 LASSERT (cfs_list_empty(&kiblnd_data.kib_connd_conns));
2828 /* flag threads to terminate; wake and wait for them to die */
2829 kiblnd_data.kib_shutdown = 1;
2831 /* NB: we really want to stop scheduler threads net by net
2832 * instead of the whole module, this should be improved
2833 * with dynamic configuration LNet */
2834 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
2835 cfs_waitq_broadcast(&sched->ibs_waitq);
2837 cfs_waitq_broadcast(&kiblnd_data.kib_connd_waitq);
2838 cfs_waitq_broadcast(&kiblnd_data.kib_failover_waitq);
2841 while (cfs_atomic_read(&kiblnd_data.kib_nthreads) != 0) {
2843 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2844 "Waiting for %d threads to terminate\n",
2845 cfs_atomic_read(&kiblnd_data.kib_nthreads));
2846 cfs_pause(cfs_time_seconds(1));
2851 case IBLND_INIT_NOTHING:
2855 if (kiblnd_data.kib_peers != NULL) {
2856 LIBCFS_FREE(kiblnd_data.kib_peers,
2857 sizeof(cfs_list_t) *
2858 kiblnd_data.kib_peer_hash_size);
2861 if (kiblnd_data.kib_scheds != NULL)
2862 cfs_percpt_free(kiblnd_data.kib_scheds);
2864 CDEBUG(D_MALLOC, "after LND base cleanup: kmem %d\n",
2865 cfs_atomic_read(&libcfs_kmemory));
2867 kiblnd_data.kib_init = IBLND_INIT_NOTHING;
2868 PORTAL_MODULE_UNUSE;
2872 kiblnd_shutdown (lnet_ni_t *ni)
2874 kib_net_t *net = ni->ni_data;
2875 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2877 unsigned long flags;
2879 LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
2884 CDEBUG(D_MALLOC, "before LND net cleanup: kmem %d\n",
2885 cfs_atomic_read(&libcfs_kmemory));
2887 write_lock_irqsave(g_lock, flags);
2888 net->ibn_shutdown = 1;
2889 write_unlock_irqrestore(g_lock, flags);
2891 switch (net->ibn_init) {
2895 case IBLND_INIT_ALL:
2896 /* nuke all existing peers within this net */
2897 kiblnd_del_peer(ni, LNET_NID_ANY);
2899 /* Wait for all peer state to clean up */
2901 while (cfs_atomic_read(&net->ibn_npeers) != 0) {
2903 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* 2**n? */
2904 "%s: waiting for %d peers to disconnect\n",
2905 libcfs_nid2str(ni->ni_nid),
2906 cfs_atomic_read(&net->ibn_npeers));
2907 cfs_pause(cfs_time_seconds(1));
2910 kiblnd_net_fini_pools(net);
2912 write_lock_irqsave(g_lock, flags);
2913 LASSERT(net->ibn_dev->ibd_nnets > 0);
2914 net->ibn_dev->ibd_nnets--;
2915 cfs_list_del(&net->ibn_list);
2916 write_unlock_irqrestore(g_lock, flags);
2920 case IBLND_INIT_NOTHING:
2921 LASSERT (cfs_atomic_read(&net->ibn_nconns) == 0);
2923 if (net->ibn_dev != NULL &&
2924 net->ibn_dev->ibd_nnets == 0)
2925 kiblnd_destroy_dev(net->ibn_dev);
2930 CDEBUG(D_MALLOC, "after LND net cleanup: kmem %d\n",
2931 cfs_atomic_read(&libcfs_kmemory));
2933 net->ibn_init = IBLND_INIT_NOTHING;
2936 LIBCFS_FREE(net, sizeof(*net));
2939 if (cfs_list_empty(&kiblnd_data.kib_devs))
2940 kiblnd_base_shutdown();
2945 kiblnd_base_startup(void)
2947 struct kib_sched_info *sched;
2951 LASSERT (kiblnd_data.kib_init == IBLND_INIT_NOTHING);
2954 memset(&kiblnd_data, 0, sizeof(kiblnd_data)); /* zero pointers, flags etc */
2956 rwlock_init(&kiblnd_data.kib_global_lock);
2958 CFS_INIT_LIST_HEAD(&kiblnd_data.kib_devs);
2959 CFS_INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
2961 kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE;
2962 LIBCFS_ALLOC(kiblnd_data.kib_peers,
2963 sizeof(cfs_list_t) *
2964 kiblnd_data.kib_peer_hash_size);
2965 if (kiblnd_data.kib_peers == NULL) {
2968 for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++)
2969 CFS_INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]);
2971 spin_lock_init(&kiblnd_data.kib_connd_lock);
2972 CFS_INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
2973 CFS_INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
2974 cfs_waitq_init(&kiblnd_data.kib_connd_waitq);
2975 cfs_waitq_init(&kiblnd_data.kib_failover_waitq);
2977 kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
2979 if (kiblnd_data.kib_scheds == NULL)
2982 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
2985 spin_lock_init(&sched->ibs_lock);
2986 CFS_INIT_LIST_HEAD(&sched->ibs_conns);
2987 cfs_waitq_init(&sched->ibs_waitq);
2989 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2990 if (*kiblnd_tunables.kib_nscheds > 0) {
2991 nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
2993 /* max to half of CPUs, another half is reserved for
2994 * upper layer modules */
2995 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
2998 sched->ibs_nthreads_max = nthrs;
3002 kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
3004 /* lists/ptrs/locks initialised */
3005 kiblnd_data.kib_init = IBLND_INIT_DATA;
3006 /*****************************************************/
3008 rc = kiblnd_thread_start(kiblnd_connd, NULL);
3010 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
3014 if (*kiblnd_tunables.kib_dev_failover != 0)
3015 rc = kiblnd_thread_start(kiblnd_failover_thread, NULL);
3018 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
3022 /* flag everything initialised */
3023 kiblnd_data.kib_init = IBLND_INIT_ALL;
3024 /*****************************************************/
3029 kiblnd_base_shutdown();
3034 kiblnd_start_schedulers(struct kib_sched_info *sched)
3040 if (sched->ibs_nthreads == 0) {
3041 if (*kiblnd_tunables.kib_nscheds > 0) {
3042 nthrs = sched->ibs_nthreads_max;
3044 nthrs = cfs_cpt_weight(lnet_cpt_table(),
3046 nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3047 nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
3050 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
3051 /* increase one thread if there is new interface */
3052 nthrs = (sched->ibs_nthreads < sched->ibs_nthreads_max);
3055 for (i = 0; i < nthrs; i++) {
3058 id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
3059 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id);
3063 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
3064 sched->ibs_cpt, sched->ibs_nthreads + i, rc);
3068 sched->ibs_nthreads += i;
3073 kiblnd_dev_start_threads(kib_dev_t *dev, int newdev, __u32 *cpts, int ncpts)
3079 for (i = 0; i < ncpts; i++) {
3080 struct kib_sched_info *sched;
3082 cpt = (cpts == NULL) ? i : cpts[i];
3083 sched = kiblnd_data.kib_scheds[cpt];
3085 if (!newdev && sched->ibs_nthreads > 0)
3088 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
3090 CERROR("Failed to start scheduler threads for %s\n",
3099 kiblnd_dev_search(char *ifname)
3101 kib_dev_t *alias = NULL;
3106 colon = strchr(ifname, ':');
3107 cfs_list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3108 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3114 colon2 = strchr(dev->ibd_ifname, ':');
3120 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3132 kiblnd_startup (lnet_ni_t *ni)
3135 kib_dev_t *ibdev = NULL;
3138 unsigned long flags;
3142 LASSERT (ni->ni_lnd == &the_o2iblnd);
3144 if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
3145 rc = kiblnd_base_startup();
3150 LIBCFS_ALLOC(net, sizeof(*net));
3155 memset(net, 0, sizeof(*net));
3157 cfs_gettimeofday(&tv);
3158 net->ibn_incarnation = (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
3160 ni->ni_peertimeout = *kiblnd_tunables.kib_peertimeout;
3161 ni->ni_maxtxcredits = *kiblnd_tunables.kib_credits;
3162 ni->ni_peertxcredits = *kiblnd_tunables.kib_peertxcredits;
3163 ni->ni_peerrtrcredits = *kiblnd_tunables.kib_peerrtrcredits;
3165 if (ni->ni_interfaces[0] != NULL) {
3166 /* Use the IPoIB interface specified in 'networks=' */
3168 CLASSERT (LNET_MAX_INTERFACES > 1);
3169 if (ni->ni_interfaces[1] != NULL) {
3170 CERROR("Multiple interfaces not supported\n");
3174 ifname = ni->ni_interfaces[0];
3176 ifname = *kiblnd_tunables.kib_default_ipif;
3179 if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
3180 CERROR("IPoIB interface name too long: %s\n", ifname);
3184 ibdev = kiblnd_dev_search(ifname);
3186 newdev = ibdev == NULL;
3187 /* hmm...create kib_dev even for alias */
3188 if (ibdev == NULL || strcmp(&ibdev->ibd_ifname[0], ifname) != 0)
3189 ibdev = kiblnd_create_dev(ifname);
3194 net->ibn_dev = ibdev;
3195 ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip);
3197 rc = kiblnd_dev_start_threads(ibdev, newdev,
3198 ni->ni_cpts, ni->ni_ncpts);
3202 rc = kiblnd_net_init_pools(net, ni->ni_cpts, ni->ni_ncpts);
3204 CERROR("Failed to initialize NI pools: %d\n", rc);
3208 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3210 cfs_list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
3211 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3213 net->ibn_init = IBLND_INIT_ALL;
3218 if (net->ibn_dev == NULL && ibdev != NULL)
3219 kiblnd_destroy_dev(ibdev);
3221 kiblnd_shutdown(ni);
3223 CDEBUG(D_NET, "kiblnd_startup failed\n");
3228 kiblnd_module_fini (void)
3230 lnet_unregister_lnd(&the_o2iblnd);
3231 kiblnd_tunables_fini();
3235 kiblnd_module_init (void)
3239 CLASSERT (sizeof(kib_msg_t) <= IBLND_MSG_SIZE);
3240 CLASSERT (offsetof(kib_msg_t, ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
3242 CLASSERT (offsetof(kib_msg_t, ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS])
3245 rc = kiblnd_tunables_init();
3249 lnet_register_lnd(&the_o2iblnd);
3254 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3255 MODULE_DESCRIPTION("Kernel OpenIB gen2 LND v2.00");
3256 MODULE_LICENSE("GPL");
3258 module_init(kiblnd_module_init);
3259 module_exit(kiblnd_module_fini);