1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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_cb.c
38 * Author: Eric Barton <eric@bartonsoftware.com>
44 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
46 lnet_msg_t *lntmsg[2];
47 kib_net_t *net = ni->ni_data;
51 LASSERT (net != NULL);
52 LASSERT (!cfs_in_interrupt());
53 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
54 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
55 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
56 LASSERT (tx->tx_pool != NULL);
58 kiblnd_unmap_tx(ni, tx);
60 /* tx may have up to 2 lnet msgs to finalise */
61 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
62 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
65 if (tx->tx_conn != NULL) {
66 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
68 kiblnd_conn_decref(tx->tx_conn);
75 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
77 /* delay finalize until my descs have been freed */
78 for (i = 0; i < 2; i++) {
79 if (lntmsg[i] == NULL)
82 lnet_finalize(ni, lntmsg[i], rc);
87 kiblnd_txlist_done (lnet_ni_t *ni, cfs_list_t *txlist, int status)
91 while (!cfs_list_empty (txlist)) {
92 tx = cfs_list_entry (txlist->next, kib_tx_t, tx_list);
94 cfs_list_del(&tx->tx_list);
97 tx->tx_status = status;
98 kiblnd_tx_done(ni, tx);
103 kiblnd_get_idle_tx (lnet_ni_t *ni)
105 kib_net_t *net = (kib_net_t *)ni->ni_data;
109 node = kiblnd_pool_alloc_node(&net->ibn_tx_ps.tps_poolset);
112 tx = container_of(node, kib_tx_t, tx_list);
114 LASSERT (tx->tx_nwrq == 0);
115 LASSERT (!tx->tx_queued);
116 LASSERT (tx->tx_sending == 0);
117 LASSERT (!tx->tx_waiting);
118 LASSERT (tx->tx_status == 0);
119 LASSERT (tx->tx_conn == NULL);
120 LASSERT (tx->tx_lntmsg[0] == NULL);
121 LASSERT (tx->tx_lntmsg[1] == NULL);
122 LASSERT (tx->tx_u.pmr == NULL);
123 LASSERT (tx->tx_nfrags == 0);
129 kiblnd_drop_rx (kib_rx_t *rx)
131 kib_conn_t *conn = rx->rx_conn;
134 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
135 LASSERT (conn->ibc_nrx > 0);
137 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
139 kiblnd_conn_decref(conn);
143 kiblnd_post_rx (kib_rx_t *rx, int credit)
145 kib_conn_t *conn = rx->rx_conn;
146 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
147 struct ib_recv_wr *bad_wrq = NULL;
151 LASSERT (net != NULL);
152 LASSERT (!cfs_in_interrupt());
153 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
154 credit == IBLND_POSTRX_PEER_CREDIT ||
155 credit == IBLND_POSTRX_RSRVD_CREDIT);
157 mr = kiblnd_find_dma_mr(net, rx->rx_msgaddr, IBLND_MSG_SIZE);
158 LASSERT (mr != NULL);
160 rx->rx_sge.lkey = mr->lkey;
161 rx->rx_sge.addr = rx->rx_msgaddr;
162 rx->rx_sge.length = IBLND_MSG_SIZE;
164 rx->rx_wrq.next = NULL;
165 rx->rx_wrq.sg_list = &rx->rx_sge;
166 rx->rx_wrq.num_sge = 1;
167 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
169 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
170 LASSERT (rx->rx_nob >= 0); /* not posted */
172 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
173 kiblnd_drop_rx(rx); /* No more posts for this rx */
177 rx->rx_nob = -1; /* flag posted */
179 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
181 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
182 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
186 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
190 kiblnd_close_conn(conn, rc);
191 kiblnd_drop_rx(rx); /* No more posts for this rx */
195 if (credit == IBLND_POSTRX_NO_CREDIT)
198 cfs_spin_lock(&conn->ibc_lock);
199 if (credit == IBLND_POSTRX_PEER_CREDIT)
200 conn->ibc_outstanding_credits++;
202 conn->ibc_reserved_credits++;
203 cfs_spin_unlock(&conn->ibc_lock);
205 kiblnd_check_sends(conn);
210 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
214 cfs_list_for_each(tmp, &conn->ibc_active_txs) {
215 kib_tx_t *tx = cfs_list_entry(tmp, kib_tx_t, tx_list);
217 LASSERT (!tx->tx_queued);
218 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
220 if (tx->tx_cookie != cookie)
223 if (tx->tx_waiting &&
224 tx->tx_msg->ibm_type == txtype)
227 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
228 tx->tx_waiting ? "" : "NOT ",
229 tx->tx_msg->ibm_type, txtype);
235 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
238 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
241 cfs_spin_lock(&conn->ibc_lock);
243 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
245 cfs_spin_unlock(&conn->ibc_lock);
247 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
248 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
249 kiblnd_close_conn(conn, -EPROTO);
253 if (tx->tx_status == 0) { /* success so far */
254 if (status < 0) { /* failed? */
255 tx->tx_status = status;
256 } else if (txtype == IBLND_MSG_GET_REQ) {
257 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
263 idle = !tx->tx_queued && (tx->tx_sending == 0);
265 cfs_list_del(&tx->tx_list);
267 cfs_spin_unlock(&conn->ibc_lock);
270 kiblnd_tx_done(ni, tx);
274 kiblnd_send_completion (kib_conn_t *conn, int type, int status, __u64 cookie)
276 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
277 kib_tx_t *tx = kiblnd_get_idle_tx(ni);
280 CERROR("Can't get tx for completion %x for %s\n",
281 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
285 tx->tx_msg->ibm_u.completion.ibcm_status = status;
286 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
287 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
289 kiblnd_queue_tx(tx, conn);
293 kiblnd_handle_rx (kib_rx_t *rx)
295 kib_msg_t *msg = rx->rx_msg;
296 kib_conn_t *conn = rx->rx_conn;
297 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
298 int credits = msg->ibm_credits;
304 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
306 CDEBUG (D_NET, "Received %x[%d] from %s\n",
307 msg->ibm_type, credits,
308 libcfs_nid2str(conn->ibc_peer->ibp_nid));
311 /* Have I received credits that will let me send? */
312 cfs_spin_lock(&conn->ibc_lock);
314 if (conn->ibc_credits + credits >
315 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
316 rc2 = conn->ibc_credits;
317 cfs_spin_unlock(&conn->ibc_lock);
319 CERROR("Bad credits from %s: %d + %d > %d\n",
320 libcfs_nid2str(conn->ibc_peer->ibp_nid),
322 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
324 kiblnd_close_conn(conn, -EPROTO);
325 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
329 conn->ibc_credits += credits;
331 cfs_spin_unlock(&conn->ibc_lock);
332 kiblnd_check_sends(conn);
335 switch (msg->ibm_type) {
337 CERROR("Bad IBLND message type %x from %s\n",
338 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
339 post_credit = IBLND_POSTRX_NO_CREDIT;
344 if (IBLND_OOB_CAPABLE(conn->ibc_version))
345 post_credit = IBLND_POSTRX_NO_CREDIT;
347 post_credit = IBLND_POSTRX_PEER_CREDIT;
350 case IBLND_MSG_IMMEDIATE:
351 post_credit = IBLND_POSTRX_DONT_POST;
352 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
353 msg->ibm_srcnid, rx, 0);
354 if (rc < 0) /* repost on error */
355 post_credit = IBLND_POSTRX_PEER_CREDIT;
358 case IBLND_MSG_PUT_REQ:
359 post_credit = IBLND_POSTRX_DONT_POST;
360 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
361 msg->ibm_srcnid, rx, 1);
362 if (rc < 0) /* repost on error */
363 post_credit = IBLND_POSTRX_PEER_CREDIT;
366 case IBLND_MSG_PUT_NAK:
367 CWARN ("PUT_NACK from %s\n",
368 libcfs_nid2str(conn->ibc_peer->ibp_nid));
369 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
370 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
371 msg->ibm_u.completion.ibcm_status,
372 msg->ibm_u.completion.ibcm_cookie);
375 case IBLND_MSG_PUT_ACK:
376 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
378 cfs_spin_lock(&conn->ibc_lock);
379 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
380 msg->ibm_u.putack.ibpam_src_cookie);
382 cfs_list_del(&tx->tx_list);
383 cfs_spin_unlock(&conn->ibc_lock);
386 CERROR("Unmatched PUT_ACK from %s\n",
387 libcfs_nid2str(conn->ibc_peer->ibp_nid));
392 LASSERT (tx->tx_waiting);
393 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
394 * (a) I can overwrite tx_msg since my peer has received it!
395 * (b) tx_waiting set tells tx_complete() it's not done. */
397 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
399 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
400 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
401 &msg->ibm_u.putack.ibpam_rd,
402 msg->ibm_u.putack.ibpam_dst_cookie);
404 CERROR("Can't setup rdma for PUT to %s: %d\n",
405 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
407 cfs_spin_lock(&conn->ibc_lock);
408 tx->tx_waiting = 0; /* clear waiting and queue atomically */
409 kiblnd_queue_tx_locked(tx, conn);
410 cfs_spin_unlock(&conn->ibc_lock);
413 case IBLND_MSG_PUT_DONE:
414 post_credit = IBLND_POSTRX_PEER_CREDIT;
415 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
416 msg->ibm_u.completion.ibcm_status,
417 msg->ibm_u.completion.ibcm_cookie);
420 case IBLND_MSG_GET_REQ:
421 post_credit = IBLND_POSTRX_DONT_POST;
422 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
423 msg->ibm_srcnid, rx, 1);
424 if (rc < 0) /* repost on error */
425 post_credit = IBLND_POSTRX_PEER_CREDIT;
428 case IBLND_MSG_GET_DONE:
429 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
430 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
431 msg->ibm_u.completion.ibcm_status,
432 msg->ibm_u.completion.ibcm_cookie);
436 if (rc < 0) /* protocol error */
437 kiblnd_close_conn(conn, rc);
439 if (post_credit != IBLND_POSTRX_DONT_POST)
440 kiblnd_post_rx(rx, post_credit);
444 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
446 kib_msg_t *msg = rx->rx_msg;
447 kib_conn_t *conn = rx->rx_conn;
448 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
449 kib_net_t *net = ni->ni_data;
453 LASSERT (net != NULL);
454 LASSERT (rx->rx_nob < 0); /* was posted */
455 rx->rx_nob = 0; /* isn't now */
457 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
460 if (status != IB_WC_SUCCESS) {
461 CDEBUG(D_NETERROR, "Rx from %s failed: %d\n",
462 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
469 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
471 CERROR ("Error %d unpacking rx from %s\n",
472 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
476 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
477 msg->ibm_dstnid != ni->ni_nid ||
478 msg->ibm_srcstamp != conn->ibc_incarnation ||
479 msg->ibm_dststamp != net->ibn_incarnation) {
480 CERROR ("Stale rx from %s\n",
481 libcfs_nid2str(conn->ibc_peer->ibp_nid));
486 /* set time last known alive */
487 kiblnd_peer_alive(conn->ibc_peer);
489 /* racing with connection establishment/teardown! */
491 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
492 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
495 cfs_write_lock_irqsave(g_lock, flags);
496 /* must check holding global lock to eliminate race */
497 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
498 cfs_list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
499 cfs_write_unlock_irqrestore(g_lock, flags);
502 cfs_write_unlock_irqrestore(g_lock, flags);
504 kiblnd_handle_rx(rx);
508 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
509 kiblnd_close_conn(conn, err);
511 kiblnd_drop_rx(rx); /* Don't re-post rx. */
515 kiblnd_kvaddr_to_page (unsigned long vaddr)
519 if (vaddr >= VMALLOC_START &&
520 vaddr < VMALLOC_END) {
521 page = vmalloc_to_page ((void *)vaddr);
522 LASSERT (page != NULL);
525 #ifdef CONFIG_HIGHMEM
526 if (vaddr >= PKMAP_BASE &&
527 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
528 /* No highmem pages only used for bulk (kiov) I/O */
529 CERROR("find page for address in highmem\n");
533 page = virt_to_page (vaddr);
534 LASSERT (page != NULL);
539 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
541 kib_dev_t *ibdev = net->ibn_dev;
542 __u64 *pages = tx->tx_pages;
548 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
549 for (size = 0; size < rd->rd_frags[i].rf_nob;
550 size += ibdev->ibd_page_size) {
551 pages[npages ++] = (rd->rd_frags[i].rf_addr &
552 ibdev->ibd_page_mask) + size;
556 rc = kiblnd_fmr_pool_map(&net->ibn_fmr_ps, pages, npages, 0, &tx->tx_u.fmr);
558 CERROR ("Can't map %d pages: %d\n", npages, rc);
562 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
564 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
565 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
566 rd->rd_frags[0].rf_addr &= ~ibdev->ibd_page_mask;
567 rd->rd_frags[0].rf_nob = nob;
574 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
579 iova = rd->rd_frags[0].rf_addr & ~net->ibn_dev->ibd_page_mask;
581 rc = kiblnd_pmr_pool_map(&net->ibn_pmr_ps, rd, &iova, &tx->tx_u.pmr);
583 CERROR("Failed to create MR by phybuf: %d\n", rc);
587 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
589 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
590 tx->tx_u.pmr->pmr_mr->lkey;
592 rd->rd_frags[0].rf_addr = iova;
593 rd->rd_frags[0].rf_nob = nob;
599 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
601 kib_net_t *net = ni->ni_data;
603 LASSERT (net != NULL);
605 if (net->ibn_with_fmr && tx->tx_u.fmr.fmr_pfmr != NULL) {
606 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
607 tx->tx_u.fmr.fmr_pfmr = NULL;
608 } else if (net->ibn_with_pmr && tx->tx_u.pmr != NULL) {
609 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
613 if (tx->tx_nfrags != 0) {
614 kiblnd_dma_unmap_sg(net->ibn_dev->ibd_cmid->device,
615 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
621 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx,
622 kib_rdma_desc_t *rd, int nfrags)
624 kib_net_t *net = ni->ni_data;
625 struct ib_mr *mr = NULL;
629 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
631 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
632 tx->tx_nfrags = nfrags;
635 kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
636 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
638 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
639 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
640 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
641 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
642 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
643 nob += rd->rd_frags[i].rf_nob;
646 /* looking for pre-mapping MR */
647 mr = kiblnd_find_rd_dma_mr(net, rd);
649 /* found pre-mapping MR */
650 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
654 if (net->ibn_with_fmr)
655 return kiblnd_fmr_map_tx(net, tx, rd, nob);
656 else if (net->ibn_with_pmr)
657 return kiblnd_pmr_map_tx(net, tx, rd, nob);
664 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
665 unsigned int niov, struct iovec *iov, int offset, int nob)
667 kib_net_t *net = ni->ni_data;
669 struct scatterlist *sg;
676 LASSERT (net != NULL);
678 while (offset >= iov->iov_len) {
679 offset -= iov->iov_len;
689 vaddr = ((unsigned long)iov->iov_base) + offset;
690 page_offset = vaddr & (PAGE_SIZE - 1);
691 page = kiblnd_kvaddr_to_page(vaddr);
693 CERROR ("Can't find page\n");
697 fragnob = min((int)(iov->iov_len - offset), nob);
698 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
700 sg_set_page(sg, page, fragnob, page_offset);
703 if (offset + fragnob < iov->iov_len) {
713 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
717 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
718 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
720 kib_net_t *net = ni->ni_data;
721 struct scatterlist *sg;
724 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
728 LASSERT (net != NULL);
730 while (offset >= kiov->kiov_len) {
731 offset -= kiov->kiov_len;
741 fragnob = min((int)(kiov->kiov_len - offset), nob);
743 memset(sg, 0, sizeof(*sg));
744 sg_set_page(sg, kiov->kiov_page, fragnob,
745 kiov->kiov_offset + offset);
754 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
758 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
760 kib_msg_t *msg = tx->tx_msg;
761 kib_peer_t *peer = conn->ibc_peer;
762 int ver = conn->ibc_version;
765 struct ib_send_wr *bad_wrq;
767 LASSERT (tx->tx_queued);
768 /* We rely on this for QP sizing */
769 LASSERT (tx->tx_nwrq > 0);
770 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
772 LASSERT (credit == 0 || credit == 1);
773 LASSERT (conn->ibc_outstanding_credits >= 0);
774 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
775 LASSERT (conn->ibc_credits >= 0);
776 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
778 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
779 /* tx completions outstanding... */
780 CDEBUG(D_NET, "%s: posted enough\n",
781 libcfs_nid2str(peer->ibp_nid));
785 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
786 CDEBUG(D_NET, "%s: no credits\n",
787 libcfs_nid2str(peer->ibp_nid));
791 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
792 conn->ibc_credits == 1 && /* last credit reserved for */
793 conn->ibc_outstanding_credits == 0) { /* giving back credits */
794 CDEBUG(D_NET, "%s: not using last credit\n",
795 libcfs_nid2str(peer->ibp_nid));
799 /* NB don't drop ibc_lock before bumping tx_sending */
800 cfs_list_del(&tx->tx_list);
803 if (msg->ibm_type == IBLND_MSG_NOOP &&
804 (!kiblnd_send_noop(conn) || /* redundant NOOP */
805 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
806 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
807 /* OK to drop when posted enough NOOPs, since
808 * kiblnd_check_sends will queue NOOP again when
809 * posted NOOPs complete */
810 cfs_spin_unlock(&conn->ibc_lock);
811 kiblnd_tx_done(peer->ibp_ni, tx);
812 cfs_spin_lock(&conn->ibc_lock);
813 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
814 libcfs_nid2str(peer->ibp_nid),
815 conn->ibc_noops_posted);
819 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
820 peer->ibp_nid, conn->ibc_incarnation);
822 conn->ibc_credits -= credit;
823 conn->ibc_outstanding_credits = 0;
824 conn->ibc_nsends_posted++;
825 if (msg->ibm_type == IBLND_MSG_NOOP)
826 conn->ibc_noops_posted++;
828 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
829 * PUT. If so, it was first queued here as a PUT_REQ, sent and
830 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
831 * and then re-queued here. It's (just) possible that
832 * tx_sending is non-zero if we've not done the tx_complete()
833 * from the first send; hence the ++ rather than = below. */
835 cfs_list_add(&tx->tx_list, &conn->ibc_active_txs);
837 /* I'm still holding ibc_lock! */
838 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
841 rc = ib_post_send(conn->ibc_cmid->qp,
842 tx->tx_wrq, &bad_wrq);
843 conn->ibc_last_send = jiffies;
848 /* NB credits are transferred in the actual
849 * message, which can only be the last work item */
850 conn->ibc_credits += credit;
851 conn->ibc_outstanding_credits += msg->ibm_credits;
852 conn->ibc_nsends_posted--;
853 if (msg->ibm_type == IBLND_MSG_NOOP)
854 conn->ibc_noops_posted--;
860 done = (tx->tx_sending == 0);
862 cfs_list_del(&tx->tx_list);
864 cfs_spin_unlock(&conn->ibc_lock);
866 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
867 CERROR("Error %d posting transmit to %s\n",
868 rc, libcfs_nid2str(peer->ibp_nid));
870 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
871 rc, libcfs_nid2str(peer->ibp_nid));
873 kiblnd_close_conn(conn, rc);
876 kiblnd_tx_done(peer->ibp_ni, tx);
881 kiblnd_check_sends (kib_conn_t *conn)
883 int ver = conn->ibc_version;
884 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
887 /* Don't send anything until after the connection is established */
888 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
889 CDEBUG(D_NET, "%s too soon\n",
890 libcfs_nid2str(conn->ibc_peer->ibp_nid));
894 cfs_spin_lock(&conn->ibc_lock);
896 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
897 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
898 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
899 LASSERT (conn->ibc_reserved_credits >= 0);
901 while (conn->ibc_reserved_credits > 0 &&
902 !cfs_list_empty(&conn->ibc_tx_queue_rsrvd)) {
903 tx = cfs_list_entry(conn->ibc_tx_queue_rsrvd.next,
905 cfs_list_del(&tx->tx_list);
906 cfs_list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
907 conn->ibc_reserved_credits--;
910 if (kiblnd_send_noop(conn)) {
911 cfs_spin_unlock(&conn->ibc_lock);
913 tx = kiblnd_get_idle_tx(ni);
915 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
917 cfs_spin_lock(&conn->ibc_lock);
919 kiblnd_queue_tx_locked(tx, conn);
925 if (!cfs_list_empty(&conn->ibc_tx_queue_nocred)) {
927 tx = cfs_list_entry(conn->ibc_tx_queue_nocred.next,
929 } else if (!cfs_list_empty(&conn->ibc_tx_queue)) {
931 tx = cfs_list_entry(conn->ibc_tx_queue.next,
936 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
940 cfs_spin_unlock(&conn->ibc_lock);
944 kiblnd_tx_complete (kib_tx_t *tx, int status)
946 int failed = (status != IB_WC_SUCCESS);
947 kib_conn_t *conn = tx->tx_conn;
950 LASSERT (tx->tx_sending > 0);
953 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
954 CDEBUG(D_NETERROR, "Tx -> %s cookie "LPX64
955 " sending %d waiting %d: failed %d\n",
956 libcfs_nid2str(conn->ibc_peer->ibp_nid),
957 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
960 kiblnd_close_conn(conn, -EIO);
962 kiblnd_peer_alive(conn->ibc_peer);
965 cfs_spin_lock(&conn->ibc_lock);
967 /* I could be racing with rdma completion. Whoever makes 'tx' idle
968 * gets to free it, which also drops its ref on 'conn'. */
971 conn->ibc_nsends_posted--;
972 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
973 conn->ibc_noops_posted--;
976 tx->tx_waiting = 0; /* don't wait for peer */
977 tx->tx_status = -EIO;
980 idle = (tx->tx_sending == 0) && /* This is the final callback */
981 !tx->tx_waiting && /* Not waiting for peer */
982 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
984 cfs_list_del(&tx->tx_list);
986 kiblnd_conn_addref(conn); /* 1 ref for me.... */
988 cfs_spin_unlock(&conn->ibc_lock);
991 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
993 kiblnd_check_sends(conn);
995 kiblnd_conn_decref(conn); /* ...until here */
999 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1001 kib_net_t *net = ni->ni_data;
1002 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1003 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1004 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1007 LASSERT (net != NULL);
1008 LASSERT (tx->tx_nwrq >= 0);
1009 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1010 LASSERT (nob <= IBLND_MSG_SIZE);
1012 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1014 mr = kiblnd_find_dma_mr(net, tx->tx_msgaddr, nob);
1015 LASSERT (mr != NULL);
1017 sge->lkey = mr->lkey;
1018 sge->addr = tx->tx_msgaddr;
1021 memset(wrq, 0, sizeof(*wrq));
1024 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1027 wrq->opcode = IB_WR_SEND;
1028 wrq->send_flags = IB_SEND_SIGNALED;
1034 kiblnd_init_rdma (kib_conn_t *conn, kib_tx_t *tx, int type,
1035 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1037 kib_msg_t *ibmsg = tx->tx_msg;
1038 kib_rdma_desc_t *srcrd = tx->tx_rd;
1039 struct ib_sge *sge = &tx->tx_sge[0];
1040 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1046 LASSERT (!cfs_in_interrupt());
1047 LASSERT (tx->tx_nwrq == 0);
1048 LASSERT (type == IBLND_MSG_GET_DONE ||
1049 type == IBLND_MSG_PUT_DONE);
1051 srcidx = dstidx = 0;
1054 if (srcidx >= srcrd->rd_nfrags) {
1055 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1060 if (dstidx == dstrd->rd_nfrags) {
1061 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1066 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1067 CERROR("RDMA too fragmented for %s (%d): "
1068 "%d/%d src %d/%d dst frags\n",
1069 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1070 IBLND_RDMA_FRAGS(conn->ibc_version),
1071 srcidx, srcrd->rd_nfrags,
1072 dstidx, dstrd->rd_nfrags);
1077 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1078 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1080 sge = &tx->tx_sge[tx->tx_nwrq];
1081 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1082 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1083 sge->length = wrknob;
1085 wrq = &tx->tx_wrq[tx->tx_nwrq];
1087 wrq->next = wrq + 1;
1088 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1091 wrq->opcode = IB_WR_RDMA_WRITE;
1092 wrq->send_flags = 0;
1094 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1095 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1097 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1098 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1107 if (rc < 0) /* no RDMA if completing with failure */
1110 ibmsg->ibm_u.completion.ibcm_status = rc;
1111 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1112 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1113 type, sizeof (kib_completion_msg_t));
1119 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1123 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1124 LASSERT (!tx->tx_queued); /* not queued for sending already */
1125 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1128 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * CFS_HZ);
1130 if (tx->tx_conn == NULL) {
1131 kiblnd_conn_addref(conn);
1133 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1135 /* PUT_DONE first attached to conn as a PUT_REQ */
1136 LASSERT (tx->tx_conn == conn);
1137 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1140 switch (tx->tx_msg->ibm_type) {
1144 case IBLND_MSG_PUT_REQ:
1145 case IBLND_MSG_GET_REQ:
1146 q = &conn->ibc_tx_queue_rsrvd;
1149 case IBLND_MSG_PUT_NAK:
1150 case IBLND_MSG_PUT_ACK:
1151 case IBLND_MSG_PUT_DONE:
1152 case IBLND_MSG_GET_DONE:
1153 q = &conn->ibc_tx_queue_nocred;
1156 case IBLND_MSG_NOOP:
1157 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1158 q = &conn->ibc_tx_queue_nocred;
1160 q = &conn->ibc_tx_queue;
1163 case IBLND_MSG_IMMEDIATE:
1164 q = &conn->ibc_tx_queue;
1168 cfs_list_add_tail(&tx->tx_list, q);
1172 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1174 cfs_spin_lock(&conn->ibc_lock);
1175 kiblnd_queue_tx_locked(tx, conn);
1176 cfs_spin_unlock(&conn->ibc_lock);
1178 kiblnd_check_sends(conn);
1182 kiblnd_connect_peer (kib_peer_t *peer)
1184 struct rdma_cm_id *cmid;
1186 kib_net_t *net = peer->ibp_ni->ni_data;
1187 struct sockaddr_in srcaddr;
1188 struct sockaddr_in dstaddr;
1191 LASSERT (net != NULL);
1192 LASSERT (peer->ibp_connecting > 0);
1194 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1196 CERROR("Can't create CMID for %s: %ld\n",
1197 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1203 memset(&srcaddr, 0, sizeof(srcaddr));
1204 srcaddr.sin_family = AF_INET;
1205 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1207 memset(&dstaddr, 0, sizeof(dstaddr));
1208 dstaddr.sin_family = AF_INET;
1209 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1210 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1212 kiblnd_peer_addref(peer); /* cmid's ref */
1214 rc = rdma_resolve_addr(cmid,
1215 (struct sockaddr *)&srcaddr,
1216 (struct sockaddr *)&dstaddr,
1217 *kiblnd_tunables.kib_timeout * 1000);
1219 LASSERT (cmid->device != NULL);
1220 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1221 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1222 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1226 /* Can't initiate address resolution: */
1227 CERROR("Can't resolve addr for %s: %d\n",
1228 libcfs_nid2str(peer->ibp_nid), rc);
1230 kiblnd_peer_decref(peer); /* cmid's ref */
1231 rdma_destroy_id(cmid);
1233 kiblnd_peer_connect_failed(peer, 1, rc);
1237 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1242 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1243 unsigned long flags;
1246 /* If I get here, I've committed to send, so I complete the tx with
1247 * failure on any problems */
1249 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1250 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1252 /* First time, just use a read lock since I expect to find my peer
1254 cfs_read_lock_irqsave(g_lock, flags);
1256 peer = kiblnd_find_peer_locked(nid);
1257 if (peer != NULL && !cfs_list_empty(&peer->ibp_conns)) {
1258 /* Found a peer with an established connection */
1259 conn = kiblnd_get_conn_locked(peer);
1260 kiblnd_conn_addref(conn); /* 1 ref for me... */
1262 cfs_read_unlock_irqrestore(g_lock, flags);
1265 kiblnd_queue_tx(tx, conn);
1266 kiblnd_conn_decref(conn); /* ...to here */
1270 cfs_read_unlock(g_lock);
1271 /* Re-try with a write lock */
1272 cfs_write_lock(g_lock);
1274 peer = kiblnd_find_peer_locked(nid);
1276 if (cfs_list_empty(&peer->ibp_conns)) {
1277 /* found a peer, but it's still connecting... */
1278 LASSERT (peer->ibp_connecting != 0 ||
1279 peer->ibp_accepting != 0);
1281 cfs_list_add_tail(&tx->tx_list,
1282 &peer->ibp_tx_queue);
1283 cfs_write_unlock_irqrestore(g_lock, flags);
1285 conn = kiblnd_get_conn_locked(peer);
1286 kiblnd_conn_addref(conn); /* 1 ref for me... */
1288 cfs_write_unlock_irqrestore(g_lock, flags);
1291 kiblnd_queue_tx(tx, conn);
1292 kiblnd_conn_decref(conn); /* ...to here */
1297 cfs_write_unlock_irqrestore(g_lock, flags);
1299 /* Allocate a peer ready to add to the peer table and retry */
1300 rc = kiblnd_create_peer(ni, &peer, nid);
1302 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1304 tx->tx_status = -EHOSTUNREACH;
1306 kiblnd_tx_done(ni, tx);
1311 cfs_write_lock_irqsave(g_lock, flags);
1313 peer2 = kiblnd_find_peer_locked(nid);
1314 if (peer2 != NULL) {
1315 if (cfs_list_empty(&peer2->ibp_conns)) {
1316 /* found a peer, but it's still connecting... */
1317 LASSERT (peer2->ibp_connecting != 0 ||
1318 peer2->ibp_accepting != 0);
1320 cfs_list_add_tail(&tx->tx_list,
1321 &peer2->ibp_tx_queue);
1322 cfs_write_unlock_irqrestore(g_lock, flags);
1324 conn = kiblnd_get_conn_locked(peer2);
1325 kiblnd_conn_addref(conn); /* 1 ref for me... */
1327 cfs_write_unlock_irqrestore(g_lock, flags);
1330 kiblnd_queue_tx(tx, conn);
1331 kiblnd_conn_decref(conn); /* ...to here */
1334 kiblnd_peer_decref(peer);
1338 /* Brand new peer */
1339 LASSERT (peer->ibp_connecting == 0);
1340 peer->ibp_connecting = 1;
1342 /* always called with a ref on ni, which prevents ni being shutdown */
1343 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1346 cfs_list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1348 kiblnd_peer_addref(peer);
1349 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1351 cfs_write_unlock_irqrestore(g_lock, flags);
1353 kiblnd_connect_peer(peer);
1354 kiblnd_peer_decref(peer);
1358 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1360 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1361 int type = lntmsg->msg_type;
1362 lnet_process_id_t target = lntmsg->msg_target;
1363 int target_is_router = lntmsg->msg_target_is_router;
1364 int routing = lntmsg->msg_routing;
1365 unsigned int payload_niov = lntmsg->msg_niov;
1366 struct iovec *payload_iov = lntmsg->msg_iov;
1367 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1368 unsigned int payload_offset = lntmsg->msg_offset;
1369 unsigned int payload_nob = lntmsg->msg_len;
1375 /* NB 'private' is different depending on what we're sending.... */
1377 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1378 payload_nob, payload_niov, libcfs_id2str(target));
1380 LASSERT (payload_nob == 0 || payload_niov > 0);
1381 LASSERT (payload_niov <= LNET_MAX_IOV);
1383 /* Thread context */
1384 LASSERT (!cfs_in_interrupt());
1385 /* payload is either all vaddrs or all pages */
1386 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1394 LASSERT (payload_nob == 0);
1398 if (routing || target_is_router)
1399 break; /* send IMMEDIATE */
1401 /* is the REPLY message too small for RDMA? */
1402 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1403 if (nob <= IBLND_MSG_SIZE)
1404 break; /* send IMMEDIATE */
1406 tx = kiblnd_get_idle_tx(ni);
1408 CERROR("Can't allocate txd for GET to %s: \n",
1409 libcfs_nid2str(target.nid));
1415 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1416 rc = kiblnd_setup_rd_iov(ni, tx,
1417 &ibmsg->ibm_u.get.ibgm_rd,
1418 lntmsg->msg_md->md_niov,
1419 lntmsg->msg_md->md_iov.iov,
1420 0, lntmsg->msg_md->md_length);
1422 rc = kiblnd_setup_rd_kiov(ni, tx,
1423 &ibmsg->ibm_u.get.ibgm_rd,
1424 lntmsg->msg_md->md_niov,
1425 lntmsg->msg_md->md_iov.kiov,
1426 0, lntmsg->msg_md->md_length);
1428 CERROR("Can't setup GET sink for %s: %d\n",
1429 libcfs_nid2str(target.nid), rc);
1430 kiblnd_tx_done(ni, tx);
1434 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1435 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1436 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1438 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1440 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1441 if (tx->tx_lntmsg[1] == NULL) {
1442 CERROR("Can't create reply for GET -> %s\n",
1443 libcfs_nid2str(target.nid));
1444 kiblnd_tx_done(ni, tx);
1448 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1449 tx->tx_waiting = 1; /* waiting for GET_DONE */
1450 kiblnd_launch_tx(ni, tx, target.nid);
1453 case LNET_MSG_REPLY:
1455 /* Is the payload small enough not to need RDMA? */
1456 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1457 if (nob <= IBLND_MSG_SIZE)
1458 break; /* send IMMEDIATE */
1460 tx = kiblnd_get_idle_tx(ni);
1462 CERROR("Can't allocate %s txd for %s\n",
1463 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1464 libcfs_nid2str(target.nid));
1468 if (payload_kiov == NULL)
1469 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1470 payload_niov, payload_iov,
1471 payload_offset, payload_nob);
1473 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1474 payload_niov, payload_kiov,
1475 payload_offset, payload_nob);
1477 CERROR("Can't setup PUT src for %s: %d\n",
1478 libcfs_nid2str(target.nid), rc);
1479 kiblnd_tx_done(ni, tx);
1484 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1485 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1486 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1488 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1489 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1490 kiblnd_launch_tx(ni, tx, target.nid);
1494 /* send IMMEDIATE */
1496 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1499 tx = kiblnd_get_idle_tx(ni);
1501 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1502 type, libcfs_nid2str(target.nid));
1507 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1509 if (payload_kiov != NULL)
1510 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1511 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1512 payload_niov, payload_kiov,
1513 payload_offset, payload_nob);
1515 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1516 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1517 payload_niov, payload_iov,
1518 payload_offset, payload_nob);
1520 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1521 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1523 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1524 kiblnd_launch_tx(ni, tx, target.nid);
1529 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1531 lnet_process_id_t target = lntmsg->msg_target;
1532 unsigned int niov = lntmsg->msg_niov;
1533 struct iovec *iov = lntmsg->msg_iov;
1534 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1535 unsigned int offset = lntmsg->msg_offset;
1536 unsigned int nob = lntmsg->msg_len;
1540 tx = kiblnd_get_idle_tx(ni);
1542 CERROR("Can't get tx for REPLY to %s\n",
1543 libcfs_nid2str(target.nid));
1549 else if (kiov == NULL)
1550 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1551 niov, iov, offset, nob);
1553 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1554 niov, kiov, offset, nob);
1557 CERROR("Can't setup GET src for %s: %d\n",
1558 libcfs_nid2str(target.nid), rc);
1562 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1563 IBLND_MSG_GET_DONE, nob,
1564 &rx->rx_msg->ibm_u.get.ibgm_rd,
1565 rx->rx_msg->ibm_u.get.ibgm_cookie);
1567 CERROR("Can't setup rdma for GET from %s: %d\n",
1568 libcfs_nid2str(target.nid), rc);
1573 /* No RDMA: local completion may happen now! */
1574 lnet_finalize(ni, lntmsg, 0);
1576 /* RDMA: lnet_finalize(lntmsg) when it
1578 tx->tx_lntmsg[0] = lntmsg;
1581 kiblnd_queue_tx(tx, rx->rx_conn);
1585 kiblnd_tx_done(ni, tx);
1587 lnet_finalize(ni, lntmsg, -EIO);
1591 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1592 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1593 unsigned int offset, unsigned int mlen, unsigned int rlen)
1595 kib_rx_t *rx = private;
1596 kib_msg_t *rxmsg = rx->rx_msg;
1597 kib_conn_t *conn = rx->rx_conn;
1601 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1604 LASSERT (mlen <= rlen);
1605 LASSERT (!cfs_in_interrupt());
1606 /* Either all pages or all vaddrs */
1607 LASSERT (!(kiov != NULL && iov != NULL));
1609 switch (rxmsg->ibm_type) {
1613 case IBLND_MSG_IMMEDIATE:
1614 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1615 if (nob > rx->rx_nob) {
1616 CERROR ("Immediate message from %s too big: %d(%d)\n",
1617 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1624 lnet_copy_flat2kiov(niov, kiov, offset,
1625 IBLND_MSG_SIZE, rxmsg,
1626 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1629 lnet_copy_flat2iov(niov, iov, offset,
1630 IBLND_MSG_SIZE, rxmsg,
1631 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1633 lnet_finalize (ni, lntmsg, 0);
1636 case IBLND_MSG_PUT_REQ:
1638 lnet_finalize(ni, lntmsg, 0);
1639 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1640 rxmsg->ibm_u.putreq.ibprm_cookie);
1644 tx = kiblnd_get_idle_tx(ni);
1646 CERROR("Can't allocate tx for %s\n",
1647 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1648 /* Not replying will break the connection */
1655 rc = kiblnd_setup_rd_iov(ni, tx,
1656 &txmsg->ibm_u.putack.ibpam_rd,
1657 niov, iov, offset, mlen);
1659 rc = kiblnd_setup_rd_kiov(ni, tx,
1660 &txmsg->ibm_u.putack.ibpam_rd,
1661 niov, kiov, offset, mlen);
1663 CERROR("Can't setup PUT sink for %s: %d\n",
1664 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1665 kiblnd_tx_done(ni, tx);
1666 /* tell peer it's over */
1667 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1668 rxmsg->ibm_u.putreq.ibprm_cookie);
1672 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1673 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1674 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1676 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1678 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1679 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1680 kiblnd_queue_tx(tx, conn);
1682 /* reposted buffer reserved for PUT_DONE */
1683 post_credit = IBLND_POSTRX_NO_CREDIT;
1686 case IBLND_MSG_GET_REQ:
1687 if (lntmsg != NULL) {
1688 /* Optimized GET; RDMA lntmsg's payload */
1689 kiblnd_reply(ni, rx, lntmsg);
1691 /* GET didn't match anything */
1692 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1694 rxmsg->ibm_u.get.ibgm_cookie);
1699 kiblnd_post_rx(rx, post_credit);
1704 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1706 long pid = cfs_kernel_thread (fn, arg, 0);
1711 cfs_atomic_inc (&kiblnd_data.kib_nthreads);
1716 kiblnd_thread_fini (void)
1718 cfs_atomic_dec (&kiblnd_data.kib_nthreads);
1722 kiblnd_peer_alive (kib_peer_t *peer)
1724 /* This is racy, but everyone's only writing cfs_time_current() */
1725 peer->ibp_last_alive = cfs_time_current();
1730 kiblnd_peer_notify (kib_peer_t *peer)
1733 cfs_time_t last_alive = 0;
1734 unsigned long flags;
1736 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1738 if (cfs_list_empty(&peer->ibp_conns) &&
1739 peer->ibp_accepting == 0 &&
1740 peer->ibp_connecting == 0 &&
1741 peer->ibp_error != 0) {
1742 error = peer->ibp_error;
1743 peer->ibp_error = 0;
1745 last_alive = peer->ibp_last_alive;
1748 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1751 lnet_notify(peer->ibp_ni,
1752 peer->ibp_nid, 0, last_alive);
1756 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1758 /* This just does the immediate housekeeping. 'error' is zero for a
1759 * normal shutdown which can happen only after the connection has been
1760 * established. If the connection is established, schedule the
1761 * connection to be finished off by the connd. Otherwise the connd is
1762 * already dealing with it (either to set it up or tear it down).
1763 * Caller holds kib_global_lock exclusively in irq context */
1764 unsigned long flags;
1765 kib_peer_t *peer = conn->ibc_peer;
1767 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1769 if (error != 0 && conn->ibc_comms_error == 0)
1770 conn->ibc_comms_error = error;
1772 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1773 return; /* already being handled */
1776 cfs_list_empty(&conn->ibc_tx_queue) &&
1777 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) &&
1778 cfs_list_empty(&conn->ibc_tx_queue_nocred) &&
1779 cfs_list_empty(&conn->ibc_active_txs)) {
1780 CDEBUG(D_NET, "closing conn to %s\n",
1781 libcfs_nid2str(peer->ibp_nid));
1783 CDEBUG(D_NETERROR, "Closing conn to %s: error %d%s%s%s%s\n",
1784 libcfs_nid2str(peer->ibp_nid), error,
1785 cfs_list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1786 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1787 cfs_list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1788 cfs_list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1791 cfs_list_del(&conn->ibc_list);
1792 /* connd (see below) takes over ibc_list's ref */
1794 if (cfs_list_empty (&peer->ibp_conns) && /* no more conns */
1795 kiblnd_peer_active(peer)) { /* still in peer table */
1796 kiblnd_unlink_peer_locked(peer);
1798 /* set/clear error on last conn */
1799 peer->ibp_error = conn->ibc_comms_error;
1802 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1804 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1806 cfs_list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1807 cfs_waitq_signal (&kiblnd_data.kib_connd_waitq);
1809 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1813 kiblnd_close_conn (kib_conn_t *conn, int error)
1815 unsigned long flags;
1817 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1819 kiblnd_close_conn_locked(conn, error);
1821 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1825 kiblnd_handle_early_rxs(kib_conn_t *conn)
1827 unsigned long flags;
1830 LASSERT (!cfs_in_interrupt());
1831 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1833 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1834 while (!cfs_list_empty(&conn->ibc_early_rxs)) {
1835 rx = cfs_list_entry(conn->ibc_early_rxs.next,
1837 cfs_list_del(&rx->rx_list);
1838 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1841 kiblnd_handle_rx(rx);
1843 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1845 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1849 kiblnd_abort_txs(kib_conn_t *conn, cfs_list_t *txs)
1851 CFS_LIST_HEAD (zombies);
1856 cfs_spin_lock(&conn->ibc_lock);
1858 cfs_list_for_each_safe (tmp, nxt, txs) {
1859 tx = cfs_list_entry (tmp, kib_tx_t, tx_list);
1861 if (txs == &conn->ibc_active_txs) {
1862 LASSERT (!tx->tx_queued);
1863 LASSERT (tx->tx_waiting ||
1864 tx->tx_sending != 0);
1866 LASSERT (tx->tx_queued);
1869 tx->tx_status = -ECONNABORTED;
1872 if (tx->tx_sending == 0) {
1874 cfs_list_del (&tx->tx_list);
1875 cfs_list_add (&tx->tx_list, &zombies);
1879 cfs_spin_unlock(&conn->ibc_lock);
1881 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1882 &zombies, -ECONNABORTED);
1886 kiblnd_finalise_conn (kib_conn_t *conn)
1888 LASSERT (!cfs_in_interrupt());
1889 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1891 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1893 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1894 * for connections that didn't get as far as being connected, because
1895 * rdma_disconnect() does this for free. */
1896 kiblnd_abort_receives(conn);
1898 /* Complete all tx descs not waiting for sends to complete.
1899 * NB we should be safe from RDMA now that the QP has changed state */
1901 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1902 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1903 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
1904 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
1906 kiblnd_handle_early_rxs(conn);
1910 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
1912 CFS_LIST_HEAD (zombies);
1913 unsigned long flags;
1915 LASSERT (error != 0);
1916 LASSERT (!cfs_in_interrupt());
1918 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1921 LASSERT (peer->ibp_connecting > 0);
1922 peer->ibp_connecting--;
1924 LASSERT (peer->ibp_accepting > 0);
1925 peer->ibp_accepting--;
1928 if (peer->ibp_connecting != 0 ||
1929 peer->ibp_accepting != 0) {
1930 /* another connection attempt under way... */
1931 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1936 if (cfs_list_empty(&peer->ibp_conns)) {
1937 /* Take peer's blocked transmits to complete with error */
1938 cfs_list_add(&zombies, &peer->ibp_tx_queue);
1939 cfs_list_del_init(&peer->ibp_tx_queue);
1941 if (kiblnd_peer_active(peer))
1942 kiblnd_unlink_peer_locked(peer);
1944 peer->ibp_error = error;
1946 /* Can't have blocked transmits if there are connections */
1947 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
1950 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1952 kiblnd_peer_notify(peer);
1954 if (cfs_list_empty (&zombies))
1957 CDEBUG (D_NETERROR, "Deleting messages for %s: connection failed\n",
1958 libcfs_nid2str(peer->ibp_nid));
1960 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
1964 kiblnd_connreq_done(kib_conn_t *conn, int status)
1966 kib_peer_t *peer = conn->ibc_peer;
1969 unsigned long flags;
1972 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
1974 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
1975 libcfs_nid2str(peer->ibp_nid), active,
1976 conn->ibc_version, status);
1978 LASSERT (!cfs_in_interrupt());
1979 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
1980 peer->ibp_connecting > 0) ||
1981 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
1982 peer->ibp_accepting > 0));
1984 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1985 conn->ibc_connvars = NULL;
1988 /* failed to establish connection */
1989 kiblnd_peer_connect_failed(peer, active, status);
1990 kiblnd_finalise_conn(conn);
1994 /* connection established */
1995 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1997 conn->ibc_last_send = jiffies;
1998 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
1999 kiblnd_peer_alive(peer);
2001 /* Add conn to peer's list and nuke any dangling conns from a different
2002 * peer instance... */
2003 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2004 cfs_list_add(&conn->ibc_list, &peer->ibp_conns);
2006 peer->ibp_connecting--;
2008 peer->ibp_accepting--;
2010 if (peer->ibp_version == 0) {
2011 peer->ibp_version = conn->ibc_version;
2012 peer->ibp_incarnation = conn->ibc_incarnation;
2015 if (peer->ibp_version != conn->ibc_version ||
2016 peer->ibp_incarnation != conn->ibc_incarnation) {
2017 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2018 conn->ibc_incarnation);
2019 peer->ibp_version = conn->ibc_version;
2020 peer->ibp_incarnation = conn->ibc_incarnation;
2023 /* grab pending txs while I have the lock */
2024 cfs_list_add(&txs, &peer->ibp_tx_queue);
2025 cfs_list_del_init(&peer->ibp_tx_queue);
2027 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2028 conn->ibc_comms_error != 0) { /* error has happened already */
2029 lnet_ni_t *ni = peer->ibp_ni;
2031 /* start to shut down connection */
2032 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2033 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2036 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2041 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2043 /* Schedule blocked txs */
2044 cfs_spin_lock (&conn->ibc_lock);
2045 while (!cfs_list_empty (&txs)) {
2046 tx = cfs_list_entry (txs.next, kib_tx_t, tx_list);
2047 cfs_list_del(&tx->tx_list);
2049 kiblnd_queue_tx_locked(tx, conn);
2051 cfs_spin_unlock (&conn->ibc_lock);
2053 kiblnd_check_sends(conn);
2055 /* schedule blocked rxs */
2056 kiblnd_handle_early_rxs(conn);
2060 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2064 rc = rdma_reject(cmid, rej, sizeof(*rej));
2067 CWARN("Error %d sending reject\n", rc);
2071 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2073 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2074 kib_msg_t *reqmsg = priv;
2080 lnet_ni_t *ni = NULL;
2081 kib_net_t *net = NULL;
2083 struct rdma_conn_param cp;
2085 int version = IBLND_MSG_VERSION;
2086 unsigned long flags;
2089 LASSERT (!cfs_in_interrupt());
2091 /* cmid inherits 'context' from the corresponding listener id */
2092 ibdev = (kib_dev_t *)cmid->context;
2093 LASSERT (ibdev != NULL);
2095 memset(&rej, 0, sizeof(rej));
2096 rej.ibr_magic = IBLND_MSG_MAGIC;
2097 rej.ibr_why = IBLND_REJECT_FATAL;
2098 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2100 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2101 CERROR("Short connection request\n");
2105 /* Future protocol version compatibility support! If the
2106 * o2iblnd-specific protocol changes, or when LNET unifies
2107 * protocols over all LNDs, the initial connection will
2108 * negotiate a protocol version. I trap this here to avoid
2109 * console errors; the reject tells the peer which protocol I
2111 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2112 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2114 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2115 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2116 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2118 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2119 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2120 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2123 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2125 CERROR("Can't parse connection request: %d\n", rc);
2129 nid = reqmsg->ibm_srcnid;
2130 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2133 net = (kib_net_t *)ni->ni_data;
2134 rej.ibr_incarnation = net->ibn_incarnation;
2137 if (ni == NULL || /* no matching net */
2138 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2139 net->ibn_dev != ibdev) { /* wrong device */
2140 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2141 "bad dst nid %s\n", libcfs_nid2str(nid),
2142 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2143 ibdev->ibd_ifname, ibdev->ibd_nnets,
2144 HIPQUAD(ibdev->ibd_ifip),
2145 libcfs_nid2str(reqmsg->ibm_dstnid));
2150 /* check time stamp as soon as possible */
2151 if (reqmsg->ibm_dststamp != 0 &&
2152 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2153 CWARN("Stale connection request\n");
2154 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2158 /* I can accept peer's version */
2159 version = reqmsg->ibm_version;
2161 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2162 CERROR("Unexpected connreq msg type: %x from %s\n",
2163 reqmsg->ibm_type, libcfs_nid2str(nid));
2167 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2168 IBLND_MSG_QUEUE_SIZE(version)) {
2169 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2170 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2171 IBLND_MSG_QUEUE_SIZE(version));
2173 if (version == IBLND_MSG_VERSION)
2174 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2179 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2180 IBLND_RDMA_FRAGS(version)) {
2181 CERROR("Can't accept %s(version %x): "
2182 "incompatible max_frags %d (%d wanted)\n",
2183 libcfs_nid2str(nid), version,
2184 reqmsg->ibm_u.connparams.ibcp_max_frags,
2185 IBLND_RDMA_FRAGS(version));
2187 if (version == IBLND_MSG_VERSION)
2188 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2194 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2195 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2196 libcfs_nid2str(nid),
2197 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2202 /* assume 'nid' is a new peer; create */
2203 rc = kiblnd_create_peer(ni, &peer, nid);
2205 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2206 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2210 cfs_write_lock_irqsave(g_lock, flags);
2212 peer2 = kiblnd_find_peer_locked(nid);
2213 if (peer2 != NULL) {
2214 if (peer2->ibp_version == 0) {
2215 peer2->ibp_version = version;
2216 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2219 /* not the guy I've talked with */
2220 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2221 peer2->ibp_version != version) {
2222 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2223 cfs_write_unlock_irqrestore(g_lock, flags);
2225 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2226 libcfs_nid2str(nid), peer2->ibp_version, version);
2228 kiblnd_peer_decref(peer);
2229 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2233 /* tie-break connection race in favour of the higher NID */
2234 if (peer2->ibp_connecting != 0 &&
2236 cfs_write_unlock_irqrestore(g_lock, flags);
2238 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2240 kiblnd_peer_decref(peer);
2241 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2245 peer2->ibp_accepting++;
2246 kiblnd_peer_addref(peer2);
2248 cfs_write_unlock_irqrestore(g_lock, flags);
2249 kiblnd_peer_decref(peer);
2252 /* Brand new peer */
2253 LASSERT (peer->ibp_accepting == 0);
2254 LASSERT (peer->ibp_version == 0 &&
2255 peer->ibp_incarnation == 0);
2257 peer->ibp_accepting = 1;
2258 peer->ibp_version = version;
2259 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2261 /* I have a ref on ni that prevents it being shutdown */
2262 LASSERT (net->ibn_shutdown == 0);
2264 kiblnd_peer_addref(peer);
2265 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2267 cfs_write_unlock_irqrestore(g_lock, flags);
2270 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2272 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2273 kiblnd_peer_decref(peer);
2274 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2278 /* conn now "owns" cmid, so I return success from here on to ensure the
2279 * CM callback doesn't destroy cmid. */
2281 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2282 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2283 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2284 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2285 <= IBLND_RX_MSGS(version));
2287 ackmsg = &conn->ibc_connvars->cv_msg;
2288 memset(ackmsg, 0, sizeof(*ackmsg));
2290 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2291 sizeof(ackmsg->ibm_u.connparams));
2292 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2293 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2294 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2296 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2298 memset(&cp, 0, sizeof(cp));
2299 cp.private_data = ackmsg;
2300 cp.private_data_len = ackmsg->ibm_nob;
2301 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2302 cp.initiator_depth = 0;
2303 cp.flow_control = 1;
2304 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2305 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2307 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2309 rc = rdma_accept(cmid, &cp);
2311 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2312 rej.ibr_version = version;
2313 rej.ibr_why = IBLND_REJECT_FATAL;
2315 kiblnd_reject(cmid, &rej);
2316 kiblnd_connreq_done(conn, rc);
2317 kiblnd_conn_decref(conn);
2327 rej.ibr_version = version;
2328 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2329 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2330 kiblnd_reject(cmid, &rej);
2332 return -ECONNREFUSED;
2336 kiblnd_reconnect (kib_conn_t *conn, int version,
2337 __u64 incarnation, int why, kib_connparams_t *cp)
2339 kib_peer_t *peer = conn->ibc_peer;
2342 unsigned long flags;
2344 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2345 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2347 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2349 /* retry connection if it's still needed and no other connection
2350 * attempts (active or passive) are in progress */
2351 if (!cfs_list_empty(&peer->ibp_tx_queue) &&
2352 peer->ibp_connecting == 1 &&
2353 peer->ibp_accepting == 0) {
2355 peer->ibp_connecting++;
2357 peer->ibp_version = version;
2358 peer->ibp_incarnation = incarnation;
2361 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2371 case IBLND_REJECT_CONN_STALE:
2375 case IBLND_REJECT_CONN_RACE:
2376 reason = "conn race";
2379 case IBLND_REJECT_CONN_UNCOMPAT:
2380 reason = "version negotiation";
2384 CDEBUG(D_NETERROR, "%s: retrying (%s), %x, %x, "
2385 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2386 libcfs_nid2str(peer->ibp_nid),
2387 reason, IBLND_MSG_VERSION, version,
2388 cp != NULL ? cp->ibcp_queue_depth : IBLND_MSG_QUEUE_SIZE(version),
2389 cp != NULL ? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2390 cp != NULL ? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2392 kiblnd_connect_peer(peer);
2396 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2398 kib_peer_t *peer = conn->ibc_peer;
2400 LASSERT (!cfs_in_interrupt());
2401 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2404 case IB_CM_REJ_STALE_CONN:
2405 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2406 IBLND_REJECT_CONN_STALE, NULL);
2409 case IB_CM_REJ_INVALID_SERVICE_ID:
2410 CDEBUG(D_NETERROR, "%s rejected: no listener at %d\n",
2411 libcfs_nid2str(peer->ibp_nid),
2412 *kiblnd_tunables.kib_service);
2415 case IB_CM_REJ_CONSUMER_DEFINED:
2416 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2417 kib_rej_t *rej = priv;
2418 kib_connparams_t *cp = NULL;
2420 __u64 incarnation = -1;
2422 /* NB. default incarnation is -1 because:
2423 * a) V1 will ignore dst incarnation in connreq.
2424 * b) V2 will provide incarnation while rejecting me,
2425 * -1 will be overwrote.
2427 * if I try to connect to a V1 peer with V2 protocol,
2428 * it rejected me then upgrade to V2, I have no idea
2429 * about the upgrading and try to reconnect with V1,
2430 * in this case upgraded V2 can find out I'm trying to
2431 * talk to the old guy and reject me(incarnation is -1).
2434 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2435 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2436 __swab32s(&rej->ibr_magic);
2437 __swab16s(&rej->ibr_version);
2441 if (priv_nob >= sizeof(kib_rej_t) &&
2442 rej->ibr_version > IBLND_MSG_VERSION_1) {
2443 /* priv_nob is always 148 in current version
2444 * of OFED, so we still need to check version.
2445 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2449 __swab64s(&rej->ibr_incarnation);
2450 __swab16s(&cp->ibcp_queue_depth);
2451 __swab16s(&cp->ibcp_max_frags);
2452 __swab32s(&cp->ibcp_max_msg_size);
2455 incarnation = rej->ibr_incarnation;
2458 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2459 rej->ibr_magic != LNET_PROTO_MAGIC) {
2460 CERROR("%s rejected: consumer defined fatal error\n",
2461 libcfs_nid2str(peer->ibp_nid));
2465 if (rej->ibr_version != IBLND_MSG_VERSION &&
2466 rej->ibr_version != IBLND_MSG_VERSION_1) {
2467 CERROR("%s rejected: o2iblnd version %x error\n",
2468 libcfs_nid2str(peer->ibp_nid),
2473 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2474 rej->ibr_version == IBLND_MSG_VERSION_1) {
2475 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2476 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2478 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2479 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2482 switch (rej->ibr_why) {
2483 case IBLND_REJECT_CONN_RACE:
2484 case IBLND_REJECT_CONN_STALE:
2485 case IBLND_REJECT_CONN_UNCOMPAT:
2486 kiblnd_reconnect(conn, rej->ibr_version,
2487 incarnation, rej->ibr_why, cp);
2490 case IBLND_REJECT_MSG_QUEUE_SIZE:
2491 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2492 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2493 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2496 case IBLND_REJECT_RDMA_FRAGS:
2497 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2498 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2499 IBLND_RDMA_FRAGS(conn->ibc_version));
2502 case IBLND_REJECT_NO_RESOURCES:
2503 CERROR("%s rejected: o2iblnd no resources\n",
2504 libcfs_nid2str(peer->ibp_nid));
2507 case IBLND_REJECT_FATAL:
2508 CERROR("%s rejected: o2iblnd fatal error\n",
2509 libcfs_nid2str(peer->ibp_nid));
2513 CERROR("%s rejected: o2iblnd reason %d\n",
2514 libcfs_nid2str(peer->ibp_nid),
2522 CDEBUG(D_NETERROR, "%s rejected: reason %d, size %d\n",
2523 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2527 kiblnd_connreq_done(conn, -ECONNREFUSED);
2531 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2533 kib_peer_t *peer = conn->ibc_peer;
2534 lnet_ni_t *ni = peer->ibp_ni;
2535 kib_net_t *net = ni->ni_data;
2536 kib_msg_t *msg = priv;
2537 int ver = conn->ibc_version;
2538 int rc = kiblnd_unpack_msg(msg, priv_nob);
2539 unsigned long flags;
2541 LASSERT (net != NULL);
2544 CERROR("Can't unpack connack from %s: %d\n",
2545 libcfs_nid2str(peer->ibp_nid), rc);
2549 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2550 CERROR("Unexpected message %d from %s\n",
2551 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2556 if (ver != msg->ibm_version) {
2557 CERROR("%s replied version %x is different with "
2558 "requested version %x\n",
2559 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2564 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2565 IBLND_MSG_QUEUE_SIZE(ver)) {
2566 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2567 libcfs_nid2str(peer->ibp_nid),
2568 msg->ibm_u.connparams.ibcp_queue_depth,
2569 IBLND_MSG_QUEUE_SIZE(ver));
2574 if (msg->ibm_u.connparams.ibcp_max_frags !=
2575 IBLND_RDMA_FRAGS(ver)) {
2576 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2577 libcfs_nid2str(peer->ibp_nid),
2578 msg->ibm_u.connparams.ibcp_max_frags,
2579 IBLND_RDMA_FRAGS(ver));
2584 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2585 CERROR("%s max message size %d too big (%d max)\n",
2586 libcfs_nid2str(peer->ibp_nid),
2587 msg->ibm_u.connparams.ibcp_max_msg_size,
2593 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2594 if (msg->ibm_dstnid == ni->ni_nid &&
2595 msg->ibm_dststamp == net->ibn_incarnation)
2599 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2602 CERROR("Bad connection reply from %s, rc = %d, "
2603 "version: %x max_frags: %d\n",
2604 libcfs_nid2str(peer->ibp_nid), rc,
2605 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2609 conn->ibc_incarnation = msg->ibm_srcstamp;
2611 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2612 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2613 <= IBLND_RX_MSGS(ver));
2615 kiblnd_connreq_done(conn, 0);
2619 /* NB My QP has already established itself, so I handle anything going
2620 * wrong here by setting ibc_comms_error.
2621 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2622 * immediately tears it down. */
2625 conn->ibc_comms_error = rc;
2626 kiblnd_connreq_done(conn, 0);
2630 kiblnd_active_connect (struct rdma_cm_id *cmid)
2632 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2635 struct rdma_conn_param cp;
2638 unsigned long flags;
2641 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2643 incarnation = peer->ibp_incarnation;
2644 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION : peer->ibp_version;
2646 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2648 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2650 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2651 kiblnd_peer_decref(peer); /* lose cmid's ref */
2655 /* conn "owns" cmid now, so I return success from here on to ensure the
2656 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2659 msg = &conn->ibc_connvars->cv_msg;
2661 memset(msg, 0, sizeof(*msg));
2662 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2663 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2664 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2665 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2667 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2668 0, peer->ibp_nid, incarnation);
2670 memset(&cp, 0, sizeof(cp));
2671 cp.private_data = msg;
2672 cp.private_data_len = msg->ibm_nob;
2673 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2674 cp.initiator_depth = 0;
2675 cp.flow_control = 1;
2676 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2677 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2679 LASSERT(cmid->context == (void *)conn);
2680 LASSERT(conn->ibc_cmid == cmid);
2682 rc = rdma_connect(cmid, &cp);
2684 CERROR("Can't connect to %s: %d\n",
2685 libcfs_nid2str(peer->ibp_nid), rc);
2686 kiblnd_connreq_done(conn, rc);
2687 kiblnd_conn_decref(conn);
2694 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2700 switch (event->event) {
2702 CERROR("Unexpected event: %d, status: %d\n",
2703 event->event, event->status);
2706 case RDMA_CM_EVENT_CONNECT_REQUEST:
2707 /* destroy cmid on failure */
2708 rc = kiblnd_passive_connect(cmid,
2709 (void *)KIBLND_CONN_PARAM(event),
2710 KIBLND_CONN_PARAM_LEN(event));
2711 CDEBUG(D_NET, "connreq: %d\n", rc);
2714 case RDMA_CM_EVENT_ADDR_ERROR:
2715 peer = (kib_peer_t *)cmid->context;
2716 CDEBUG(D_NETERROR, "%s: ADDR ERROR %d\n",
2717 libcfs_nid2str(peer->ibp_nid), event->status);
2718 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2719 kiblnd_peer_decref(peer);
2720 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2722 case RDMA_CM_EVENT_ADDR_RESOLVED:
2723 peer = (kib_peer_t *)cmid->context;
2725 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2726 libcfs_nid2str(peer->ibp_nid), event->status);
2728 if (event->status != 0) {
2729 CDEBUG(D_NETERROR, "Can't resolve address for %s: %d\n",
2730 libcfs_nid2str(peer->ibp_nid), event->status);
2733 rc = rdma_resolve_route(
2734 cmid, *kiblnd_tunables.kib_timeout * 1000);
2737 /* Can't initiate route resolution */
2738 CERROR("Can't resolve route for %s: %d\n",
2739 libcfs_nid2str(peer->ibp_nid), rc);
2741 kiblnd_peer_connect_failed(peer, 1, rc);
2742 kiblnd_peer_decref(peer);
2743 return rc; /* rc != 0 destroys cmid */
2745 case RDMA_CM_EVENT_ROUTE_ERROR:
2746 peer = (kib_peer_t *)cmid->context;
2747 CDEBUG(D_NETERROR, "%s: ROUTE ERROR %d\n",
2748 libcfs_nid2str(peer->ibp_nid), event->status);
2749 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2750 kiblnd_peer_decref(peer);
2751 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2753 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2754 peer = (kib_peer_t *)cmid->context;
2755 CDEBUG(D_NET,"%s Route resolved: %d\n",
2756 libcfs_nid2str(peer->ibp_nid), event->status);
2758 if (event->status == 0)
2759 return kiblnd_active_connect(cmid);
2761 CDEBUG(D_NETERROR, "Can't resolve route for %s: %d\n",
2762 libcfs_nid2str(peer->ibp_nid), event->status);
2763 kiblnd_peer_connect_failed(peer, 1, event->status);
2764 kiblnd_peer_decref(peer);
2765 return event->status; /* rc != 0 destroys cmid */
2767 case RDMA_CM_EVENT_UNREACHABLE:
2768 conn = (kib_conn_t *)cmid->context;
2769 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2770 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2771 CDEBUG(D_NETERROR, "%s: UNREACHABLE %d\n",
2772 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2773 kiblnd_connreq_done(conn, -ENETDOWN);
2774 kiblnd_conn_decref(conn);
2777 case RDMA_CM_EVENT_CONNECT_ERROR:
2778 conn = (kib_conn_t *)cmid->context;
2779 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2780 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2781 CDEBUG(D_NETERROR, "%s: CONNECT ERROR %d\n",
2782 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2783 kiblnd_connreq_done(conn, -ENOTCONN);
2784 kiblnd_conn_decref(conn);
2787 case RDMA_CM_EVENT_REJECTED:
2788 conn = (kib_conn_t *)cmid->context;
2789 switch (conn->ibc_state) {
2793 case IBLND_CONN_PASSIVE_WAIT:
2794 CERROR ("%s: REJECTED %d\n",
2795 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2797 kiblnd_connreq_done(conn, -ECONNRESET);
2800 case IBLND_CONN_ACTIVE_CONNECT:
2801 kiblnd_rejected(conn, event->status,
2802 (void *)KIBLND_CONN_PARAM(event),
2803 KIBLND_CONN_PARAM_LEN(event));
2806 kiblnd_conn_decref(conn);
2809 case RDMA_CM_EVENT_ESTABLISHED:
2810 conn = (kib_conn_t *)cmid->context;
2811 switch (conn->ibc_state) {
2815 case IBLND_CONN_PASSIVE_WAIT:
2816 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2817 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2818 kiblnd_connreq_done(conn, 0);
2821 case IBLND_CONN_ACTIVE_CONNECT:
2822 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2823 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2824 kiblnd_check_connreply(conn,
2825 (void *)KIBLND_CONN_PARAM(event),
2826 KIBLND_CONN_PARAM_LEN(event));
2829 /* net keeps its ref on conn! */
2832 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2833 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2834 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2837 case RDMA_CM_EVENT_DISCONNECTED:
2838 conn = (kib_conn_t *)cmid->context;
2839 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2840 CERROR("%s DISCONNECTED\n",
2841 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2842 kiblnd_connreq_done(conn, -ECONNRESET);
2844 kiblnd_close_conn(conn, 0);
2846 kiblnd_conn_decref(conn);
2847 cmid->context = NULL;
2850 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2851 LCONSOLE_ERROR_MSG(0x131,
2852 "Received notification of device removal\n"
2853 "Please shutdown LNET to allow this to proceed\n");
2854 /* Can't remove network from underneath LNET for now, so I have
2858 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2859 case RDMA_CM_EVENT_ADDR_CHANGE:
2860 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2867 kiblnd_check_txs (kib_conn_t *conn, cfs_list_t *txs)
2873 cfs_spin_lock(&conn->ibc_lock);
2875 cfs_list_for_each (ttmp, txs) {
2876 tx = cfs_list_entry (ttmp, kib_tx_t, tx_list);
2878 if (txs != &conn->ibc_active_txs) {
2879 LASSERT (tx->tx_queued);
2881 LASSERT (!tx->tx_queued);
2882 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2885 if (cfs_time_aftereq (jiffies, tx->tx_deadline)) {
2887 CERROR("Timed out tx: %s, %lu seconds\n",
2888 kiblnd_queue2str(conn, txs),
2889 cfs_duration_sec(jiffies - tx->tx_deadline));
2894 cfs_spin_unlock(&conn->ibc_lock);
2899 kiblnd_conn_timed_out (kib_conn_t *conn)
2901 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2902 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2903 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2904 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2908 kiblnd_check_conns (int idx)
2910 cfs_list_t *peers = &kiblnd_data.kib_peers[idx];
2915 unsigned long flags;
2918 /* NB. We expect to have a look at all the peers and not find any
2919 * rdmas to time out, so we just use a shared lock while we
2921 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2923 cfs_list_for_each (ptmp, peers) {
2924 peer = cfs_list_entry (ptmp, kib_peer_t, ibp_list);
2926 cfs_list_for_each (ctmp, &peer->ibp_conns) {
2927 conn = cfs_list_entry (ctmp, kib_conn_t, ibc_list);
2929 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2931 /* In case we have enough credits to return via a
2932 * NOOP, but there were no non-blocking tx descs
2933 * free to do it last time... */
2934 kiblnd_check_sends(conn);
2936 if (!kiblnd_conn_timed_out(conn))
2939 /* Handle timeout by closing the whole connection. We
2940 * can only be sure RDMA activity has ceased once the
2941 * QP has been modified. */
2943 kiblnd_conn_addref(conn); /* 1 ref for me... */
2945 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2948 CERROR("Timed out RDMA with %s (%lu)\n",
2949 libcfs_nid2str(peer->ibp_nid),
2950 cfs_duration_sec(cfs_time_current() -
2951 peer->ibp_last_alive));
2953 kiblnd_close_conn(conn, -ETIMEDOUT);
2954 kiblnd_conn_decref(conn); /* ...until here */
2956 /* start again now I've dropped the lock */
2961 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2965 kiblnd_disconnect_conn (kib_conn_t *conn)
2967 LASSERT (!cfs_in_interrupt());
2968 LASSERT (current == kiblnd_data.kib_connd);
2969 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
2971 rdma_disconnect(conn->ibc_cmid);
2972 kiblnd_finalise_conn(conn);
2974 kiblnd_peer_notify(conn->ibc_peer);
2978 kiblnd_connd (void *arg)
2980 cfs_waitlink_t wait;
2981 unsigned long flags;
2987 unsigned long deadline = jiffies;
2989 cfs_daemonize ("kiblnd_connd");
2990 cfs_block_allsigs ();
2992 cfs_waitlink_init (&wait);
2993 kiblnd_data.kib_connd = current;
2995 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2997 while (!kiblnd_data.kib_shutdown) {
3001 if (!cfs_list_empty (&kiblnd_data.kib_connd_zombies)) {
3002 conn = cfs_list_entry(kiblnd_data. \
3003 kib_connd_zombies.next,
3004 kib_conn_t, ibc_list);
3005 cfs_list_del(&conn->ibc_list);
3007 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3011 kiblnd_destroy_conn(conn);
3013 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3017 if (!cfs_list_empty (&kiblnd_data.kib_connd_conns)) {
3018 conn = cfs_list_entry (kiblnd_data.kib_connd_conns.next,
3019 kib_conn_t, ibc_list);
3020 cfs_list_del(&conn->ibc_list);
3022 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3026 kiblnd_disconnect_conn(conn);
3027 kiblnd_conn_decref(conn);
3029 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3033 /* careful with the jiffy wrap... */
3034 timeout = (int)(deadline - jiffies);
3038 int chunk = kiblnd_data.kib_peer_hash_size;
3040 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3043 /* Time to check for RDMA timeouts on a few more
3044 * peers: I do checks every 'p' seconds on a
3045 * proportion of the peer table and I need to check
3046 * every connection 'n' times within a timeout
3047 * interval, to ensure I detect a timeout on any
3048 * connection within (n+1)/n times the timeout
3051 if (*kiblnd_tunables.kib_timeout > n * p)
3052 chunk = (chunk * n * p) /
3053 *kiblnd_tunables.kib_timeout;
3057 for (i = 0; i < chunk; i++) {
3058 kiblnd_check_conns(peer_index);
3059 peer_index = (peer_index + 1) %
3060 kiblnd_data.kib_peer_hash_size;
3063 deadline += p * CFS_HZ;
3064 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock,
3071 /* Nothing to do for 'timeout' */
3072 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
3073 cfs_waitq_add (&kiblnd_data.kib_connd_waitq, &wait);
3074 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3076 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, timeout);
3078 cfs_set_current_state (CFS_TASK_RUNNING);
3079 cfs_waitq_del (&kiblnd_data.kib_connd_waitq, &wait);
3080 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3083 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3085 kiblnd_thread_fini();
3090 kiblnd_qp_event(struct ib_event *event, void *arg)
3092 kib_conn_t *conn = arg;
3094 switch (event->event) {
3095 case IB_EVENT_COMM_EST:
3096 CDEBUG(D_NET, "%s established\n",
3097 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3101 CERROR("%s: Async QP event type %d\n",
3102 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3108 kiblnd_complete (struct ib_wc *wc)
3110 switch (kiblnd_wreqid2type(wc->wr_id)) {
3114 case IBLND_WID_RDMA:
3115 /* We only get RDMA completion notification if it fails. All
3116 * subsequent work items, including the final SEND will fail
3117 * too. However we can't print out any more info about the
3118 * failing RDMA because 'tx' might be back on the idle list or
3119 * even reused already if we didn't manage to post all our work
3121 CDEBUG(D_NETERROR, "RDMA (tx: %p) failed: %d\n",
3122 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3126 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3130 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3137 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3139 /* NB I'm not allowed to schedule this conn once its refcount has
3140 * reached 0. Since fundamentally I'm racing with scheduler threads
3141 * consuming my CQ I could be called after all completions have
3142 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3143 * and this CQ is about to be destroyed so I NOOP. */
3144 kib_conn_t *conn = (kib_conn_t *)arg;
3145 unsigned long flags;
3147 LASSERT (cq == conn->ibc_cq);
3149 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3151 conn->ibc_ready = 1;
3153 if (!conn->ibc_scheduled &&
3154 (conn->ibc_nrx > 0 ||
3155 conn->ibc_nsends_posted > 0)) {
3156 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3157 conn->ibc_scheduled = 1;
3158 cfs_list_add_tail(&conn->ibc_sched_list,
3159 &kiblnd_data.kib_sched_conns);
3160 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3163 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3167 kiblnd_cq_event(struct ib_event *event, void *arg)
3169 kib_conn_t *conn = arg;
3171 CERROR("%s: async CQ event type %d\n",
3172 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3176 kiblnd_scheduler(void *arg)
3178 long id = (long)arg;
3179 cfs_waitlink_t wait;
3181 unsigned long flags;
3188 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3189 cfs_daemonize(name);
3190 cfs_block_allsigs();
3192 cfs_waitlink_init(&wait);
3194 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3196 while (!kiblnd_data.kib_shutdown) {
3197 if (busy_loops++ >= IBLND_RESCHED) {
3198 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3204 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3210 if (!cfs_list_empty(&kiblnd_data.kib_sched_conns)) {
3211 conn = cfs_list_entry(kiblnd_data.kib_sched_conns.next,
3212 kib_conn_t, ibc_sched_list);
3213 /* take over kib_sched_conns' ref on conn... */
3214 LASSERT(conn->ibc_scheduled);
3215 cfs_list_del(&conn->ibc_sched_list);
3216 conn->ibc_ready = 0;
3218 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3221 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3223 rc = ib_req_notify_cq(conn->ibc_cq,
3226 CWARN("%s: ib_req_notify_cq failed: %d, "
3227 "closing connection\n",
3228 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3229 kiblnd_close_conn(conn, -EIO);
3230 kiblnd_conn_decref(conn);
3231 cfs_spin_lock_irqsave(&kiblnd_data. \
3237 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3241 CWARN("%s: ib_poll_cq failed: %d, "
3242 "closing connection\n",
3243 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3245 kiblnd_close_conn(conn, -EIO);
3246 kiblnd_conn_decref(conn);
3247 cfs_spin_lock_irqsave(&kiblnd_data. \
3248 kib_sched_lock, flags);
3252 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3255 if (rc != 0 || conn->ibc_ready) {
3256 /* There may be another completion waiting; get
3257 * another scheduler to check while I handle
3259 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3260 cfs_list_add_tail(&conn->ibc_sched_list,
3261 &kiblnd_data.kib_sched_conns);
3262 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3264 conn->ibc_scheduled = 0;
3268 cfs_spin_unlock_irqrestore(&kiblnd_data. \
3272 kiblnd_complete(&wc);
3274 cfs_spin_lock_irqsave(&kiblnd_data. \
3279 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3286 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3287 cfs_waitq_add_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3288 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3290 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
3293 cfs_waitq_del(&kiblnd_data.kib_sched_waitq, &wait);
3294 cfs_set_current_state(CFS_TASK_RUNNING);
3295 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3298 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3300 kiblnd_thread_fini();