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) 2012, 2015, Intel Corporation.
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>
43 static void kiblnd_peer_alive(kib_peer_t *peer);
44 static void kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error);
45 static void kiblnd_check_sends(kib_conn_t *conn);
46 static void kiblnd_init_tx_msg(lnet_ni_t *ni, kib_tx_t *tx,
47 int type, int body_nob);
48 static int kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
49 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie);
50 static void kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn);
51 static void kiblnd_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
52 static void kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx);
55 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
57 lnet_msg_t *lntmsg[2];
58 kib_net_t *net = ni->ni_data;
62 LASSERT (net != NULL);
63 LASSERT (!in_interrupt());
64 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
65 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
66 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
67 LASSERT (tx->tx_pool != NULL);
69 kiblnd_unmap_tx(ni, tx);
71 /* tx may have up to 2 lnet msgs to finalise */
72 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
73 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
76 if (tx->tx_conn != NULL) {
77 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
79 kiblnd_conn_decref(tx->tx_conn);
86 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
88 /* delay finalize until my descs have been freed */
89 for (i = 0; i < 2; i++) {
90 if (lntmsg[i] == NULL)
93 lnet_finalize(ni, lntmsg[i], rc);
98 kiblnd_txlist_done(lnet_ni_t *ni, struct list_head *txlist, int status)
102 while (!list_empty(txlist)) {
103 tx = list_entry(txlist->next, kib_tx_t, tx_list);
105 list_del(&tx->tx_list);
108 tx->tx_status = status;
109 kiblnd_tx_done(ni, tx);
114 kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
116 kib_net_t *net = (kib_net_t *)ni->ni_data;
117 struct list_head *node;
119 kib_tx_poolset_t *tps;
121 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target)];
122 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
125 tx = container_of(node, kib_tx_t, tx_list);
127 LASSERT (tx->tx_nwrq == 0);
128 LASSERT (!tx->tx_queued);
129 LASSERT (tx->tx_sending == 0);
130 LASSERT (!tx->tx_waiting);
131 LASSERT (tx->tx_status == 0);
132 LASSERT (tx->tx_conn == NULL);
133 LASSERT (tx->tx_lntmsg[0] == NULL);
134 LASSERT (tx->tx_lntmsg[1] == NULL);
135 LASSERT (tx->tx_nfrags == 0);
141 kiblnd_drop_rx(kib_rx_t *rx)
143 kib_conn_t *conn = rx->rx_conn;
144 struct kib_sched_info *sched = conn->ibc_sched;
147 spin_lock_irqsave(&sched->ibs_lock, flags);
148 LASSERT(conn->ibc_nrx > 0);
150 spin_unlock_irqrestore(&sched->ibs_lock, flags);
152 kiblnd_conn_decref(conn);
156 kiblnd_post_rx (kib_rx_t *rx, int credit)
158 kib_conn_t *conn = rx->rx_conn;
159 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
160 struct ib_recv_wr *bad_wrq = NULL;
161 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
164 LASSERT (net != NULL);
165 LASSERT (!in_interrupt());
166 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
167 credit == IBLND_POSTRX_PEER_CREDIT ||
168 credit == IBLND_POSTRX_RSRVD_CREDIT);
171 rx->rx_sge.lkey = mr->lkey;
172 rx->rx_sge.addr = rx->rx_msgaddr;
173 rx->rx_sge.length = IBLND_MSG_SIZE;
175 rx->rx_wrq.next = NULL;
176 rx->rx_wrq.sg_list = &rx->rx_sge;
177 rx->rx_wrq.num_sge = 1;
178 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
180 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
181 LASSERT (rx->rx_nob >= 0); /* not posted */
183 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
184 kiblnd_drop_rx(rx); /* No more posts for this rx */
188 rx->rx_nob = -1; /* flag posted */
190 /* NB: need an extra reference after ib_post_recv because we don't
191 * own this rx (and rx::rx_conn) anymore, LU-5678.
193 kiblnd_conn_addref(conn);
194 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
195 if (unlikely(rc != 0)) {
196 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
197 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
201 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
204 if (unlikely(rc != 0)) {
205 kiblnd_close_conn(conn, rc);
206 kiblnd_drop_rx(rx); /* No more posts for this rx */
210 if (credit == IBLND_POSTRX_NO_CREDIT)
213 spin_lock(&conn->ibc_lock);
214 if (credit == IBLND_POSTRX_PEER_CREDIT)
215 conn->ibc_outstanding_credits++;
217 conn->ibc_reserved_credits++;
218 spin_unlock(&conn->ibc_lock);
220 kiblnd_check_sends(conn);
222 kiblnd_conn_decref(conn);
227 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
229 struct list_head *tmp;
231 list_for_each(tmp, &conn->ibc_active_txs) {
232 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
234 LASSERT(!tx->tx_queued);
235 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
237 if (tx->tx_cookie != cookie)
240 if (tx->tx_waiting &&
241 tx->tx_msg->ibm_type == txtype)
244 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
245 tx->tx_waiting ? "" : "NOT ",
246 tx->tx_msg->ibm_type, txtype);
252 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
255 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
258 spin_lock(&conn->ibc_lock);
260 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
262 spin_unlock(&conn->ibc_lock);
264 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
265 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
266 kiblnd_close_conn(conn, -EPROTO);
270 if (tx->tx_status == 0) { /* success so far */
271 if (status < 0) { /* failed? */
272 tx->tx_status = status;
273 } else if (txtype == IBLND_MSG_GET_REQ) {
274 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
280 idle = !tx->tx_queued && (tx->tx_sending == 0);
282 list_del(&tx->tx_list);
284 spin_unlock(&conn->ibc_lock);
287 kiblnd_tx_done(ni, tx);
291 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
293 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
294 kib_tx_t *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
297 CERROR("Can't get tx for completion %x for %s\n",
298 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
302 tx->tx_msg->ibm_u.completion.ibcm_status = status;
303 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
304 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
306 kiblnd_queue_tx(tx, conn);
310 kiblnd_handle_rx (kib_rx_t *rx)
312 kib_msg_t *msg = rx->rx_msg;
313 kib_conn_t *conn = rx->rx_conn;
314 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
315 int credits = msg->ibm_credits;
321 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
323 CDEBUG (D_NET, "Received %x[%d] from %s\n",
324 msg->ibm_type, credits,
325 libcfs_nid2str(conn->ibc_peer->ibp_nid));
328 /* Have I received credits that will let me send? */
329 spin_lock(&conn->ibc_lock);
331 if (conn->ibc_credits + credits >
332 conn->ibc_queue_depth) {
333 rc2 = conn->ibc_credits;
334 spin_unlock(&conn->ibc_lock);
336 CERROR("Bad credits from %s: %d + %d > %d\n",
337 libcfs_nid2str(conn->ibc_peer->ibp_nid),
339 conn->ibc_queue_depth);
341 kiblnd_close_conn(conn, -EPROTO);
342 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
346 conn->ibc_credits += credits;
348 /* This ensures the credit taken by NOOP can be returned */
349 if (msg->ibm_type == IBLND_MSG_NOOP &&
350 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
351 conn->ibc_outstanding_credits++;
353 spin_unlock(&conn->ibc_lock);
354 kiblnd_check_sends(conn);
357 switch (msg->ibm_type) {
359 CERROR("Bad IBLND message type %x from %s\n",
360 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
361 post_credit = IBLND_POSTRX_NO_CREDIT;
366 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
367 post_credit = IBLND_POSTRX_NO_CREDIT;
371 if (credits != 0) /* credit already posted */
372 post_credit = IBLND_POSTRX_NO_CREDIT;
373 else /* a keepalive NOOP */
374 post_credit = IBLND_POSTRX_PEER_CREDIT;
377 case IBLND_MSG_IMMEDIATE:
378 post_credit = IBLND_POSTRX_DONT_POST;
379 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
380 msg->ibm_srcnid, rx, 0);
381 if (rc < 0) /* repost on error */
382 post_credit = IBLND_POSTRX_PEER_CREDIT;
385 case IBLND_MSG_PUT_REQ:
386 post_credit = IBLND_POSTRX_DONT_POST;
387 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
388 msg->ibm_srcnid, rx, 1);
389 if (rc < 0) /* repost on error */
390 post_credit = IBLND_POSTRX_PEER_CREDIT;
393 case IBLND_MSG_PUT_NAK:
394 CWARN ("PUT_NACK from %s\n",
395 libcfs_nid2str(conn->ibc_peer->ibp_nid));
396 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
397 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
398 msg->ibm_u.completion.ibcm_status,
399 msg->ibm_u.completion.ibcm_cookie);
402 case IBLND_MSG_PUT_ACK:
403 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
405 spin_lock(&conn->ibc_lock);
406 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
407 msg->ibm_u.putack.ibpam_src_cookie);
409 list_del(&tx->tx_list);
410 spin_unlock(&conn->ibc_lock);
413 CERROR("Unmatched PUT_ACK from %s\n",
414 libcfs_nid2str(conn->ibc_peer->ibp_nid));
419 LASSERT (tx->tx_waiting);
420 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
421 * (a) I can overwrite tx_msg since my peer has received it!
422 * (b) tx_waiting set tells tx_complete() it's not done. */
424 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
426 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
427 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
428 &msg->ibm_u.putack.ibpam_rd,
429 msg->ibm_u.putack.ibpam_dst_cookie);
431 CERROR("Can't setup rdma for PUT to %s: %d\n",
432 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
434 spin_lock(&conn->ibc_lock);
435 tx->tx_waiting = 0; /* clear waiting and queue atomically */
436 kiblnd_queue_tx_locked(tx, conn);
437 spin_unlock(&conn->ibc_lock);
440 case IBLND_MSG_PUT_DONE:
441 post_credit = IBLND_POSTRX_PEER_CREDIT;
442 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
443 msg->ibm_u.completion.ibcm_status,
444 msg->ibm_u.completion.ibcm_cookie);
447 case IBLND_MSG_GET_REQ:
448 post_credit = IBLND_POSTRX_DONT_POST;
449 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
450 msg->ibm_srcnid, rx, 1);
451 if (rc < 0) /* repost on error */
452 post_credit = IBLND_POSTRX_PEER_CREDIT;
455 case IBLND_MSG_GET_DONE:
456 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
457 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
458 msg->ibm_u.completion.ibcm_status,
459 msg->ibm_u.completion.ibcm_cookie);
463 if (rc < 0) /* protocol error */
464 kiblnd_close_conn(conn, rc);
466 if (post_credit != IBLND_POSTRX_DONT_POST)
467 kiblnd_post_rx(rx, post_credit);
471 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
473 kib_msg_t *msg = rx->rx_msg;
474 kib_conn_t *conn = rx->rx_conn;
475 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
476 kib_net_t *net = ni->ni_data;
480 LASSERT (net != NULL);
481 LASSERT (rx->rx_nob < 0); /* was posted */
482 rx->rx_nob = 0; /* isn't now */
484 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
487 if (status != IB_WC_SUCCESS) {
488 CNETERR("Rx from %s failed: %d\n",
489 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
496 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
498 CERROR ("Error %d unpacking rx from %s\n",
499 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
503 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
504 msg->ibm_dstnid != ni->ni_nid ||
505 msg->ibm_srcstamp != conn->ibc_incarnation ||
506 msg->ibm_dststamp != net->ibn_incarnation) {
507 CERROR ("Stale rx from %s\n",
508 libcfs_nid2str(conn->ibc_peer->ibp_nid));
513 /* set time last known alive */
514 kiblnd_peer_alive(conn->ibc_peer);
516 /* racing with connection establishment/teardown! */
518 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
519 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
522 write_lock_irqsave(g_lock, flags);
523 /* must check holding global lock to eliminate race */
524 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
525 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
526 write_unlock_irqrestore(g_lock, flags);
529 write_unlock_irqrestore(g_lock, flags);
531 kiblnd_handle_rx(rx);
535 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
536 kiblnd_close_conn(conn, err);
538 kiblnd_drop_rx(rx); /* Don't re-post rx. */
542 kiblnd_kvaddr_to_page (unsigned long vaddr)
546 if (is_vmalloc_addr((void *)vaddr)) {
547 page = vmalloc_to_page ((void *)vaddr);
548 LASSERT (page != NULL);
551 #ifdef CONFIG_HIGHMEM
552 if (vaddr >= PKMAP_BASE &&
553 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
554 /* No highmem pages only used for bulk (kiov) I/O */
555 CERROR("find page for address in highmem\n");
559 page = virt_to_page (vaddr);
560 LASSERT (page != NULL);
565 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, __u32 nob)
568 kib_fmr_poolset_t *fps;
572 LASSERT(tx->tx_pool != NULL);
573 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
575 hdev = tx->tx_pool->tpo_hdev;
576 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
578 fps = net->ibn_fmr_ps[cpt];
579 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->fmr);
581 CERROR("Can't map %u pages: %d\n", nob, rc);
585 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
587 rd->rd_key = tx->fmr.fmr_key;
588 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
589 rd->rd_frags[0].rf_nob = nob;
596 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
598 kib_net_t *net = ni->ni_data;
600 LASSERT(net != NULL);
602 if (net->ibn_fmr_ps != NULL)
603 kiblnd_fmr_pool_unmap(&tx->fmr, tx->tx_status);
605 if (tx->tx_nfrags != 0) {
606 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
607 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
613 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd, int nfrags)
615 kib_net_t *net = ni->ni_data;
616 kib_hca_dev_t *hdev = net->ibn_dev->ibd_hdev;
617 struct ib_mr *mr = NULL;
621 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
623 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
624 tx->tx_nfrags = nfrags;
626 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
627 tx->tx_nfrags, tx->tx_dmadir);
629 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
630 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
631 hdev->ibh_ibdev, &tx->tx_frags[i]);
632 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
633 hdev->ibh_ibdev, &tx->tx_frags[i]);
634 nob += rd->rd_frags[i].rf_nob;
637 mr = kiblnd_find_rd_dma_mr(ni, rd,
638 (tx->tx_conn != NULL) ?
639 tx->tx_conn->ibc_max_frags : -1);
641 /* found pre-mapping MR */
642 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
646 if (net->ibn_fmr_ps != NULL)
647 return kiblnd_fmr_map_tx(net, tx, rd, nob);
654 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
655 unsigned int niov, struct kvec *iov, int offset, int nob)
657 kib_net_t *net = ni->ni_data;
659 struct scatterlist *sg;
666 LASSERT (net != NULL);
668 while (offset >= iov->iov_len) {
669 offset -= iov->iov_len;
679 vaddr = ((unsigned long)iov->iov_base) + offset;
680 page_offset = vaddr & (PAGE_SIZE - 1);
681 page = kiblnd_kvaddr_to_page(vaddr);
683 CERROR ("Can't find page\n");
687 fragnob = min((int)(iov->iov_len - offset), nob);
688 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
690 sg_set_page(sg, page, fragnob, page_offset);
693 if (offset + fragnob < iov->iov_len) {
703 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
707 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
708 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
710 kib_net_t *net = ni->ni_data;
711 struct scatterlist *sg;
714 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
718 LASSERT (net != NULL);
720 while (offset >= kiov->kiov_len) {
721 offset -= kiov->kiov_len;
731 fragnob = min((int)(kiov->kiov_len - offset), nob);
733 sg_set_page(sg, kiov->kiov_page, fragnob,
734 kiov->kiov_offset + offset);
743 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
747 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
748 __must_hold(&conn->ibc_lock)
750 kib_msg_t *msg = tx->tx_msg;
751 kib_peer_t *peer = conn->ibc_peer;
752 struct lnet_ni *ni = peer->ibp_ni;
753 int ver = conn->ibc_version;
757 LASSERT(tx->tx_queued);
758 /* We rely on this for QP sizing */
759 LASSERT(tx->tx_nwrq > 0);
760 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
762 LASSERT(credit == 0 || credit == 1);
763 LASSERT(conn->ibc_outstanding_credits >= 0);
764 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
765 LASSERT(conn->ibc_credits >= 0);
766 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
768 if (conn->ibc_nsends_posted ==
769 kiblnd_concurrent_sends(ver, ni)) {
770 /* tx completions outstanding... */
771 CDEBUG(D_NET, "%s: posted enough\n",
772 libcfs_nid2str(peer->ibp_nid));
776 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
777 CDEBUG(D_NET, "%s: no credits\n",
778 libcfs_nid2str(peer->ibp_nid));
782 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
783 conn->ibc_credits == 1 && /* last credit reserved */
784 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
785 CDEBUG(D_NET, "%s: not using last credit\n",
786 libcfs_nid2str(peer->ibp_nid));
790 /* NB don't drop ibc_lock before bumping tx_sending */
791 list_del(&tx->tx_list);
794 if (msg->ibm_type == IBLND_MSG_NOOP &&
795 (!kiblnd_need_noop(conn) || /* redundant NOOP */
796 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
797 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
798 /* OK to drop when posted enough NOOPs, since
799 * kiblnd_check_sends will queue NOOP again when
800 * posted NOOPs complete */
801 spin_unlock(&conn->ibc_lock);
802 kiblnd_tx_done(peer->ibp_ni, tx);
803 spin_lock(&conn->ibc_lock);
804 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
805 libcfs_nid2str(peer->ibp_nid),
806 conn->ibc_noops_posted);
810 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
811 peer->ibp_nid, conn->ibc_incarnation);
813 conn->ibc_credits -= credit;
814 conn->ibc_outstanding_credits = 0;
815 conn->ibc_nsends_posted++;
816 if (msg->ibm_type == IBLND_MSG_NOOP)
817 conn->ibc_noops_posted++;
819 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
820 * PUT. If so, it was first queued here as a PUT_REQ, sent and
821 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
822 * and then re-queued here. It's (just) possible that
823 * tx_sending is non-zero if we've not done the tx_complete()
824 * from the first send; hence the ++ rather than = below. */
826 list_add(&tx->tx_list, &conn->ibc_active_txs);
828 /* I'm still holding ibc_lock! */
829 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
831 } else if (tx->tx_pool->tpo_pool.po_failed ||
832 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
833 /* close_conn will launch failover */
836 struct kib_fast_reg_descriptor *frd = tx->fmr.fmr_frd;
837 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1];
838 struct ib_send_wr *wrq = tx->tx_wrq;
841 if (!frd->frd_valid) {
842 wrq = &frd->frd_inv_wr;
843 #ifdef HAVE_IB_MAP_MR_SG
844 wrq->next = &frd->frd_fastreg_wr.wr;
846 wrq = &frd->frd_fastreg_wr.wr;
848 frd->frd_fastreg_wr.wr.next = tx->tx_wrq;
850 wrq->next = &frd->frd_fastreg_wr;
852 wrq = &frd->frd_fastreg_wr;
854 frd->frd_fastreg_wr.next = tx->tx_wrq;
858 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
859 "bad wr_id "LPX64", opc %d, flags %d, peer: %s\n",
860 bad->wr_id, bad->opcode, bad->send_flags,
861 libcfs_nid2str(conn->ibc_peer->ibp_nid));
863 rc = ib_post_send(conn->ibc_cmid->qp, wrq, &bad);
866 conn->ibc_last_send = jiffies;
871 /* NB credits are transferred in the actual
872 * message, which can only be the last work item */
873 conn->ibc_credits += credit;
874 conn->ibc_outstanding_credits += msg->ibm_credits;
875 conn->ibc_nsends_posted--;
876 if (msg->ibm_type == IBLND_MSG_NOOP)
877 conn->ibc_noops_posted--;
883 done = (tx->tx_sending == 0);
885 list_del(&tx->tx_list);
887 spin_unlock(&conn->ibc_lock);
889 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
890 CERROR("Error %d posting transmit to %s\n",
891 rc, libcfs_nid2str(peer->ibp_nid));
893 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
894 rc, libcfs_nid2str(peer->ibp_nid));
896 kiblnd_close_conn(conn, rc);
899 kiblnd_tx_done(peer->ibp_ni, tx);
901 spin_lock(&conn->ibc_lock);
907 kiblnd_check_sends (kib_conn_t *conn)
909 int ver = conn->ibc_version;
910 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
913 /* Don't send anything until after the connection is established */
914 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
915 CDEBUG(D_NET, "%s too soon\n",
916 libcfs_nid2str(conn->ibc_peer->ibp_nid));
920 spin_lock(&conn->ibc_lock);
922 LASSERT(conn->ibc_nsends_posted <=
923 kiblnd_concurrent_sends(ver, ni));
924 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
925 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
926 LASSERT (conn->ibc_reserved_credits >= 0);
928 while (conn->ibc_reserved_credits > 0 &&
929 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
930 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
932 list_del(&tx->tx_list);
933 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
934 conn->ibc_reserved_credits--;
937 if (kiblnd_need_noop(conn)) {
938 spin_unlock(&conn->ibc_lock);
940 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
942 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
944 spin_lock(&conn->ibc_lock);
946 kiblnd_queue_tx_locked(tx, conn);
952 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
954 tx = list_entry(conn->ibc_tx_queue_nocred.next,
956 } else if (!list_empty(&conn->ibc_tx_noops)) {
957 LASSERT (!IBLND_OOB_CAPABLE(ver));
959 tx = list_entry(conn->ibc_tx_noops.next,
961 } else if (!list_empty(&conn->ibc_tx_queue)) {
963 tx = list_entry(conn->ibc_tx_queue.next,
968 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
972 spin_unlock(&conn->ibc_lock);
976 kiblnd_tx_complete (kib_tx_t *tx, int status)
978 int failed = (status != IB_WC_SUCCESS);
979 kib_conn_t *conn = tx->tx_conn;
982 LASSERT (tx->tx_sending > 0);
985 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
986 CNETERR("Tx -> %s cookie "LPX64
987 " sending %d waiting %d: failed %d\n",
988 libcfs_nid2str(conn->ibc_peer->ibp_nid),
989 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
992 kiblnd_close_conn(conn, -EIO);
994 kiblnd_peer_alive(conn->ibc_peer);
997 spin_lock(&conn->ibc_lock);
999 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1000 * gets to free it, which also drops its ref on 'conn'. */
1003 conn->ibc_nsends_posted--;
1004 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1005 conn->ibc_noops_posted--;
1008 tx->tx_waiting = 0; /* don't wait for peer */
1009 tx->tx_status = -EIO;
1012 idle = (tx->tx_sending == 0) && /* This is the final callback */
1013 !tx->tx_waiting && /* Not waiting for peer */
1014 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1016 list_del(&tx->tx_list);
1018 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1020 spin_unlock(&conn->ibc_lock);
1023 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1025 kiblnd_check_sends(conn);
1027 kiblnd_conn_decref(conn); /* ...until here */
1031 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1033 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1034 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1035 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1036 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1037 struct ib_mr *mr = hdev->ibh_mrs;
1039 LASSERT(tx->tx_nwrq >= 0);
1040 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1041 LASSERT(nob <= IBLND_MSG_SIZE);
1042 LASSERT(mr != NULL);
1044 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1046 sge->lkey = mr->lkey;
1047 sge->addr = tx->tx_msgaddr;
1050 memset(wrq, 0, sizeof(*wrq));
1053 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1056 wrq->opcode = IB_WR_SEND;
1057 wrq->send_flags = IB_SEND_SIGNALED;
1063 kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
1064 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1066 kib_msg_t *ibmsg = tx->tx_msg;
1067 kib_rdma_desc_t *srcrd = tx->tx_rd;
1068 struct ib_sge *sge = &tx->tx_sge[0];
1069 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1075 LASSERT (!in_interrupt());
1076 LASSERT (tx->tx_nwrq == 0);
1077 LASSERT (type == IBLND_MSG_GET_DONE ||
1078 type == IBLND_MSG_PUT_DONE);
1080 srcidx = dstidx = 0;
1083 if (srcidx >= srcrd->rd_nfrags) {
1084 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1089 if (dstidx == dstrd->rd_nfrags) {
1090 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1095 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1096 CERROR("RDMA has too many fragments for peer %s (%d), "
1097 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1098 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1099 conn->ibc_max_frags,
1100 srcidx, srcrd->rd_nfrags,
1101 dstidx, dstrd->rd_nfrags);
1106 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1107 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1109 sge = &tx->tx_sge[tx->tx_nwrq];
1110 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1111 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1112 sge->length = wrknob;
1114 wrq = &tx->tx_wrq[tx->tx_nwrq];
1116 wrq->next = wrq + 1;
1117 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1120 wrq->opcode = IB_WR_RDMA_WRITE;
1121 wrq->send_flags = 0;
1123 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1124 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1126 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1127 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1136 if (rc < 0) /* no RDMA if completing with failure */
1139 ibmsg->ibm_u.completion.ibcm_status = rc;
1140 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1141 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1142 type, sizeof (kib_completion_msg_t));
1148 kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn)
1150 struct list_head *q;
1152 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1153 LASSERT(!tx->tx_queued); /* not queued for sending already */
1154 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1157 tx->tx_deadline = jiffies +
1158 msecs_to_jiffies(*kiblnd_tunables.kib_timeout *
1161 if (tx->tx_conn == NULL) {
1162 kiblnd_conn_addref(conn);
1164 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1166 /* PUT_DONE first attached to conn as a PUT_REQ */
1167 LASSERT (tx->tx_conn == conn);
1168 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1171 switch (tx->tx_msg->ibm_type) {
1175 case IBLND_MSG_PUT_REQ:
1176 case IBLND_MSG_GET_REQ:
1177 q = &conn->ibc_tx_queue_rsrvd;
1180 case IBLND_MSG_PUT_NAK:
1181 case IBLND_MSG_PUT_ACK:
1182 case IBLND_MSG_PUT_DONE:
1183 case IBLND_MSG_GET_DONE:
1184 q = &conn->ibc_tx_queue_nocred;
1187 case IBLND_MSG_NOOP:
1188 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1189 q = &conn->ibc_tx_queue_nocred;
1191 q = &conn->ibc_tx_noops;
1194 case IBLND_MSG_IMMEDIATE:
1195 q = &conn->ibc_tx_queue;
1199 list_add_tail(&tx->tx_list, q);
1203 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1205 spin_lock(&conn->ibc_lock);
1206 kiblnd_queue_tx_locked(tx, conn);
1207 spin_unlock(&conn->ibc_lock);
1209 kiblnd_check_sends(conn);
1212 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1213 struct sockaddr_in *srcaddr,
1214 struct sockaddr_in *dstaddr,
1217 unsigned short port;
1220 /* allow the port to be reused */
1221 rc = rdma_set_reuseaddr(cmid, 1);
1223 CERROR("Unable to set reuse on cmid: %d\n", rc);
1227 /* look for a free privileged port */
1228 for (port = PROT_SOCK-1; port > 0; port--) {
1229 srcaddr->sin_port = htons(port);
1230 rc = rdma_resolve_addr(cmid,
1231 (struct sockaddr *)srcaddr,
1232 (struct sockaddr *)dstaddr,
1235 CDEBUG(D_NET, "bound to port %hu\n", port);
1237 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1238 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1245 CERROR("Failed to bind to a free privileged port\n");
1250 kiblnd_connect_peer (kib_peer_t *peer)
1252 struct rdma_cm_id *cmid;
1254 kib_net_t *net = peer->ibp_ni->ni_data;
1255 struct sockaddr_in srcaddr;
1256 struct sockaddr_in dstaddr;
1259 LASSERT (net != NULL);
1260 LASSERT (peer->ibp_connecting > 0);
1261 LASSERT(!peer->ibp_reconnecting);
1263 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1267 CERROR("Can't create CMID for %s: %ld\n",
1268 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1274 memset(&srcaddr, 0, sizeof(srcaddr));
1275 srcaddr.sin_family = AF_INET;
1276 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1278 memset(&dstaddr, 0, sizeof(dstaddr));
1279 dstaddr.sin_family = AF_INET;
1280 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1281 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1283 kiblnd_peer_addref(peer); /* cmid's ref */
1285 if (*kiblnd_tunables.kib_use_priv_port) {
1286 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1287 *kiblnd_tunables.kib_timeout * 1000);
1289 rc = rdma_resolve_addr(cmid,
1290 (struct sockaddr *)&srcaddr,
1291 (struct sockaddr *)&dstaddr,
1292 *kiblnd_tunables.kib_timeout * 1000);
1295 /* Can't initiate address resolution: */
1296 CERROR("Can't resolve addr for %s: %d\n",
1297 libcfs_nid2str(peer->ibp_nid), rc);
1301 LASSERT (cmid->device != NULL);
1302 CDEBUG(D_NET, "%s: connection bound to %s:%pI4h:%s\n",
1303 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1304 &dev->ibd_ifip, cmid->device->name);
1309 kiblnd_peer_connect_failed(peer, 1, rc);
1310 kiblnd_peer_decref(peer); /* cmid's ref */
1311 rdma_destroy_id(cmid);
1314 kiblnd_peer_connect_failed(peer, 1, rc);
1318 kiblnd_reconnect_peer(kib_peer_t *peer)
1320 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1321 char *reason = NULL;
1322 struct list_head txs;
1323 unsigned long flags;
1325 INIT_LIST_HEAD(&txs);
1327 write_lock_irqsave(glock, flags);
1328 if (peer->ibp_reconnecting == 0) {
1329 if (peer->ibp_accepting)
1330 reason = "accepting";
1331 else if (peer->ibp_connecting)
1332 reason = "connecting";
1333 else if (!list_empty(&peer->ibp_conns))
1334 reason = "connected";
1335 else /* connected then closed */
1341 LASSERT(!peer->ibp_accepting && !peer->ibp_connecting &&
1342 list_empty(&peer->ibp_conns));
1343 peer->ibp_reconnecting = 0;
1345 if (!kiblnd_peer_active(peer)) {
1346 list_splice_init(&peer->ibp_tx_queue, &txs);
1347 reason = "unlinked";
1351 peer->ibp_connecting++;
1352 peer->ibp_reconnected++;
1353 write_unlock_irqrestore(glock, flags);
1355 kiblnd_connect_peer(peer);
1359 write_unlock_irqrestore(glock, flags);
1361 CWARN("Abort reconnection of %s: %s\n",
1362 libcfs_nid2str(peer->ibp_nid), reason);
1363 kiblnd_txlist_done(peer->ibp_ni, &txs, -ECONNABORTED);
1368 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1373 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1374 unsigned long flags;
1377 /* If I get here, I've committed to send, so I complete the tx with
1378 * failure on any problems */
1380 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1381 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1383 /* First time, just use a read lock since I expect to find my peer
1385 read_lock_irqsave(g_lock, flags);
1387 peer = kiblnd_find_peer_locked(nid);
1388 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1389 /* Found a peer with an established connection */
1390 conn = kiblnd_get_conn_locked(peer);
1391 kiblnd_conn_addref(conn); /* 1 ref for me... */
1393 read_unlock_irqrestore(g_lock, flags);
1396 kiblnd_queue_tx(tx, conn);
1397 kiblnd_conn_decref(conn); /* ...to here */
1401 read_unlock(g_lock);
1402 /* Re-try with a write lock */
1405 peer = kiblnd_find_peer_locked(nid);
1407 if (list_empty(&peer->ibp_conns)) {
1408 /* found a peer, but it's still connecting... */
1409 LASSERT(kiblnd_peer_connecting(peer));
1411 list_add_tail(&tx->tx_list,
1412 &peer->ibp_tx_queue);
1413 write_unlock_irqrestore(g_lock, flags);
1415 conn = kiblnd_get_conn_locked(peer);
1416 kiblnd_conn_addref(conn); /* 1 ref for me... */
1418 write_unlock_irqrestore(g_lock, flags);
1421 kiblnd_queue_tx(tx, conn);
1422 kiblnd_conn_decref(conn); /* ...to here */
1427 write_unlock_irqrestore(g_lock, flags);
1429 /* Allocate a peer ready to add to the peer table and retry */
1430 rc = kiblnd_create_peer(ni, &peer, nid);
1432 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1434 tx->tx_status = -EHOSTUNREACH;
1436 kiblnd_tx_done(ni, tx);
1441 write_lock_irqsave(g_lock, flags);
1443 peer2 = kiblnd_find_peer_locked(nid);
1444 if (peer2 != NULL) {
1445 if (list_empty(&peer2->ibp_conns)) {
1446 /* found a peer, but it's still connecting... */
1447 LASSERT(kiblnd_peer_connecting(peer2));
1449 list_add_tail(&tx->tx_list,
1450 &peer2->ibp_tx_queue);
1451 write_unlock_irqrestore(g_lock, flags);
1453 conn = kiblnd_get_conn_locked(peer2);
1454 kiblnd_conn_addref(conn); /* 1 ref for me... */
1456 write_unlock_irqrestore(g_lock, flags);
1459 kiblnd_queue_tx(tx, conn);
1460 kiblnd_conn_decref(conn); /* ...to here */
1463 kiblnd_peer_decref(peer);
1467 /* Brand new peer */
1468 LASSERT (peer->ibp_connecting == 0);
1469 peer->ibp_connecting = 1;
1471 /* always called with a ref on ni, which prevents ni being shutdown */
1472 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1475 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1477 kiblnd_peer_addref(peer);
1478 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1480 write_unlock_irqrestore(g_lock, flags);
1482 kiblnd_connect_peer(peer);
1483 kiblnd_peer_decref(peer);
1487 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1489 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1490 int type = lntmsg->msg_type;
1491 lnet_process_id_t target = lntmsg->msg_target;
1492 int target_is_router = lntmsg->msg_target_is_router;
1493 int routing = lntmsg->msg_routing;
1494 unsigned int payload_niov = lntmsg->msg_niov;
1495 struct kvec *payload_iov = lntmsg->msg_iov;
1496 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1497 unsigned int payload_offset = lntmsg->msg_offset;
1498 unsigned int payload_nob = lntmsg->msg_len;
1500 kib_rdma_desc_t *rd;
1505 /* NB 'private' is different depending on what we're sending.... */
1507 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1508 payload_nob, payload_niov, libcfs_id2str(target));
1510 LASSERT (payload_nob == 0 || payload_niov > 0);
1511 LASSERT (payload_niov <= LNET_MAX_IOV);
1513 /* Thread context */
1514 LASSERT (!in_interrupt());
1515 /* payload is either all vaddrs or all pages */
1516 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1524 LASSERT (payload_nob == 0);
1528 if (routing || target_is_router)
1529 break; /* send IMMEDIATE */
1531 /* is the REPLY message too small for RDMA? */
1532 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1533 if (nob <= IBLND_MSG_SIZE)
1534 break; /* send IMMEDIATE */
1536 tx = kiblnd_get_idle_tx(ni, target.nid);
1538 CERROR("Can't allocate txd for GET to %s\n",
1539 libcfs_nid2str(target.nid));
1544 rd = &ibmsg->ibm_u.get.ibgm_rd;
1545 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1546 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1547 lntmsg->msg_md->md_niov,
1548 lntmsg->msg_md->md_iov.iov,
1549 0, lntmsg->msg_md->md_length);
1551 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1552 lntmsg->msg_md->md_niov,
1553 lntmsg->msg_md->md_iov.kiov,
1554 0, lntmsg->msg_md->md_length);
1556 CERROR("Can't setup GET sink for %s: %d\n",
1557 libcfs_nid2str(target.nid), rc);
1558 kiblnd_tx_done(ni, tx);
1562 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[rd->rd_nfrags]);
1563 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1564 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1566 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1568 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1569 if (tx->tx_lntmsg[1] == NULL) {
1570 CERROR("Can't create reply for GET -> %s\n",
1571 libcfs_nid2str(target.nid));
1572 kiblnd_tx_done(ni, tx);
1576 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1577 tx->tx_waiting = 1; /* waiting for GET_DONE */
1578 kiblnd_launch_tx(ni, tx, target.nid);
1581 case LNET_MSG_REPLY:
1583 /* Is the payload small enough not to need RDMA? */
1584 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1585 if (nob <= IBLND_MSG_SIZE)
1586 break; /* send IMMEDIATE */
1588 tx = kiblnd_get_idle_tx(ni, target.nid);
1590 CERROR("Can't allocate %s txd for %s\n",
1591 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1592 libcfs_nid2str(target.nid));
1596 if (payload_kiov == NULL)
1597 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1598 payload_niov, payload_iov,
1599 payload_offset, payload_nob);
1601 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1602 payload_niov, payload_kiov,
1603 payload_offset, payload_nob);
1605 CERROR("Can't setup PUT src for %s: %d\n",
1606 libcfs_nid2str(target.nid), rc);
1607 kiblnd_tx_done(ni, tx);
1612 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1613 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1614 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1616 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1617 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1618 kiblnd_launch_tx(ni, tx, target.nid);
1622 /* send IMMEDIATE */
1624 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1627 tx = kiblnd_get_idle_tx(ni, target.nid);
1629 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1630 type, libcfs_nid2str(target.nid));
1635 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1637 if (payload_kiov != NULL)
1638 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1639 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1640 payload_niov, payload_kiov,
1641 payload_offset, payload_nob);
1643 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1644 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1645 payload_niov, payload_iov,
1646 payload_offset, payload_nob);
1648 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1649 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1651 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1652 kiblnd_launch_tx(ni, tx, target.nid);
1657 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1659 lnet_process_id_t target = lntmsg->msg_target;
1660 unsigned int niov = lntmsg->msg_niov;
1661 struct kvec *iov = lntmsg->msg_iov;
1662 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1663 unsigned int offset = lntmsg->msg_offset;
1664 unsigned int nob = lntmsg->msg_len;
1668 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1670 CERROR("Can't get tx for REPLY to %s\n",
1671 libcfs_nid2str(target.nid));
1677 else if (kiov == NULL)
1678 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1679 niov, iov, offset, nob);
1681 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1682 niov, kiov, offset, nob);
1685 CERROR("Can't setup GET src for %s: %d\n",
1686 libcfs_nid2str(target.nid), rc);
1690 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1691 IBLND_MSG_GET_DONE, nob,
1692 &rx->rx_msg->ibm_u.get.ibgm_rd,
1693 rx->rx_msg->ibm_u.get.ibgm_cookie);
1695 CERROR("Can't setup rdma for GET from %s: %d\n",
1696 libcfs_nid2str(target.nid), rc);
1701 /* No RDMA: local completion may happen now! */
1702 lnet_finalize(ni, lntmsg, 0);
1704 /* RDMA: lnet_finalize(lntmsg) when it
1706 tx->tx_lntmsg[0] = lntmsg;
1709 kiblnd_queue_tx(tx, rx->rx_conn);
1713 kiblnd_tx_done(ni, tx);
1715 lnet_finalize(ni, lntmsg, -EIO);
1719 kiblnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1720 unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1721 unsigned int offset, unsigned int mlen, unsigned int rlen)
1723 kib_rx_t *rx = private;
1724 kib_msg_t *rxmsg = rx->rx_msg;
1725 kib_conn_t *conn = rx->rx_conn;
1728 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1731 LASSERT (mlen <= rlen);
1732 LASSERT (!in_interrupt());
1733 /* Either all pages or all vaddrs */
1734 LASSERT (!(kiov != NULL && iov != NULL));
1736 switch (rxmsg->ibm_type) {
1740 case IBLND_MSG_IMMEDIATE:
1741 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1742 if (nob > rx->rx_nob) {
1743 CERROR ("Immediate message from %s too big: %d(%d)\n",
1744 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1751 lnet_copy_flat2kiov(niov, kiov, offset,
1752 IBLND_MSG_SIZE, rxmsg,
1753 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1756 lnet_copy_flat2iov(niov, iov, offset,
1757 IBLND_MSG_SIZE, rxmsg,
1758 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1760 lnet_finalize (ni, lntmsg, 0);
1763 case IBLND_MSG_PUT_REQ: {
1765 kib_rdma_desc_t *rd;
1768 lnet_finalize(ni, lntmsg, 0);
1769 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1770 rxmsg->ibm_u.putreq.ibprm_cookie);
1774 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1776 CERROR("Can't allocate tx for %s\n",
1777 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1778 /* Not replying will break the connection */
1784 rd = &txmsg->ibm_u.putack.ibpam_rd;
1786 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1787 niov, iov, offset, mlen);
1789 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1790 niov, kiov, offset, mlen);
1792 CERROR("Can't setup PUT sink for %s: %d\n",
1793 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1794 kiblnd_tx_done(ni, tx);
1795 /* tell peer it's over */
1796 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1797 rxmsg->ibm_u.putreq.ibprm_cookie);
1801 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[rd->rd_nfrags]);
1802 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1803 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1805 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1807 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1808 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1809 kiblnd_queue_tx(tx, conn);
1811 /* reposted buffer reserved for PUT_DONE */
1812 post_credit = IBLND_POSTRX_NO_CREDIT;
1816 case IBLND_MSG_GET_REQ:
1817 if (lntmsg != NULL) {
1818 /* Optimized GET; RDMA lntmsg's payload */
1819 kiblnd_reply(ni, rx, lntmsg);
1821 /* GET didn't match anything */
1822 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1824 rxmsg->ibm_u.get.ibgm_cookie);
1829 kiblnd_post_rx(rx, post_credit);
1834 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1836 struct task_struct *task = kthread_run(fn, arg, name);
1839 return PTR_ERR(task);
1841 atomic_inc(&kiblnd_data.kib_nthreads);
1846 kiblnd_thread_fini (void)
1848 atomic_dec (&kiblnd_data.kib_nthreads);
1852 kiblnd_peer_alive (kib_peer_t *peer)
1854 /* This is racy, but everyone's only writing cfs_time_current() */
1855 peer->ibp_last_alive = cfs_time_current();
1860 kiblnd_peer_notify (kib_peer_t *peer)
1863 cfs_time_t last_alive = 0;
1864 unsigned long flags;
1866 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1868 if (kiblnd_peer_idle(peer) && peer->ibp_error != 0) {
1869 error = peer->ibp_error;
1870 peer->ibp_error = 0;
1872 last_alive = peer->ibp_last_alive;
1875 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1878 lnet_notify(peer->ibp_ni,
1879 peer->ibp_nid, 0, last_alive);
1883 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1885 /* This just does the immediate housekeeping. 'error' is zero for a
1886 * normal shutdown which can happen only after the connection has been
1887 * established. If the connection is established, schedule the
1888 * connection to be finished off by the connd. Otherwise the connd is
1889 * already dealing with it (either to set it up or tear it down).
1890 * Caller holds kib_global_lock exclusively in irq context */
1891 kib_peer_t *peer = conn->ibc_peer;
1893 unsigned long flags;
1895 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1897 if (error != 0 && conn->ibc_comms_error == 0)
1898 conn->ibc_comms_error = error;
1900 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1901 return; /* already being handled */
1904 list_empty(&conn->ibc_tx_noops) &&
1905 list_empty(&conn->ibc_tx_queue) &&
1906 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1907 list_empty(&conn->ibc_tx_queue_nocred) &&
1908 list_empty(&conn->ibc_active_txs)) {
1909 CDEBUG(D_NET, "closing conn to %s\n",
1910 libcfs_nid2str(peer->ibp_nid));
1912 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1913 libcfs_nid2str(peer->ibp_nid), error,
1914 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1915 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1916 list_empty(&conn->ibc_tx_queue_rsrvd) ?
1917 "" : "(sending_rsrvd)",
1918 list_empty(&conn->ibc_tx_queue_nocred) ?
1919 "" : "(sending_nocred)",
1920 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1923 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1924 list_del(&conn->ibc_list);
1925 /* connd (see below) takes over ibc_list's ref */
1927 if (list_empty(&peer->ibp_conns) && /* no more conns */
1928 kiblnd_peer_active(peer)) { /* still in peer table */
1929 kiblnd_unlink_peer_locked(peer);
1931 /* set/clear error on last conn */
1932 peer->ibp_error = conn->ibc_comms_error;
1935 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1938 kiblnd_dev_can_failover(dev)) {
1939 list_add_tail(&dev->ibd_fail_list,
1940 &kiblnd_data.kib_failed_devs);
1941 wake_up(&kiblnd_data.kib_failover_waitq);
1944 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1946 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1947 wake_up(&kiblnd_data.kib_connd_waitq);
1949 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1953 kiblnd_close_conn(kib_conn_t *conn, int error)
1955 unsigned long flags;
1957 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1959 kiblnd_close_conn_locked(conn, error);
1961 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1965 kiblnd_handle_early_rxs(kib_conn_t *conn)
1967 unsigned long flags;
1970 LASSERT(!in_interrupt());
1971 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1973 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1974 while (!list_empty(&conn->ibc_early_rxs)) {
1975 rx = list_entry(conn->ibc_early_rxs.next,
1977 list_del(&rx->rx_list);
1978 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1980 kiblnd_handle_rx(rx);
1982 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1984 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1988 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1990 struct list_head zombies = LIST_HEAD_INIT(zombies);
1991 struct list_head *tmp;
1992 struct list_head *nxt;
1995 spin_lock(&conn->ibc_lock);
1997 list_for_each_safe(tmp, nxt, txs) {
1998 tx = list_entry(tmp, kib_tx_t, tx_list);
2000 if (txs == &conn->ibc_active_txs) {
2001 LASSERT(!tx->tx_queued);
2002 LASSERT(tx->tx_waiting ||
2003 tx->tx_sending != 0);
2005 LASSERT(tx->tx_queued);
2008 tx->tx_status = -ECONNABORTED;
2011 if (tx->tx_sending == 0) {
2013 list_del(&tx->tx_list);
2014 list_add(&tx->tx_list, &zombies);
2018 spin_unlock(&conn->ibc_lock);
2020 kiblnd_txlist_done(conn->ibc_peer->ibp_ni, &zombies, -ECONNABORTED);
2024 kiblnd_finalise_conn (kib_conn_t *conn)
2026 LASSERT (!in_interrupt());
2027 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2029 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2031 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2032 * for connections that didn't get as far as being connected, because
2033 * rdma_disconnect() does this for free. */
2034 kiblnd_abort_receives(conn);
2036 /* Complete all tx descs not waiting for sends to complete.
2037 * NB we should be safe from RDMA now that the QP has changed state */
2039 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2040 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2041 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2042 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2043 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2045 kiblnd_handle_early_rxs(conn);
2049 kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error)
2051 struct list_head zombies = LIST_HEAD_INIT(zombies);
2052 unsigned long flags;
2054 LASSERT (error != 0);
2055 LASSERT (!in_interrupt());
2057 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2060 LASSERT (peer->ibp_connecting > 0);
2061 peer->ibp_connecting--;
2063 LASSERT (peer->ibp_accepting > 0);
2064 peer->ibp_accepting--;
2067 if (kiblnd_peer_connecting(peer)) {
2068 /* another connection attempt under way... */
2069 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2074 peer->ibp_reconnected = 0;
2075 if (list_empty(&peer->ibp_conns)) {
2076 /* Take peer's blocked transmits to complete with error */
2077 list_add(&zombies, &peer->ibp_tx_queue);
2078 list_del_init(&peer->ibp_tx_queue);
2080 if (kiblnd_peer_active(peer))
2081 kiblnd_unlink_peer_locked(peer);
2083 peer->ibp_error = error;
2085 /* Can't have blocked transmits if there are connections */
2086 LASSERT(list_empty(&peer->ibp_tx_queue));
2089 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2091 kiblnd_peer_notify(peer);
2093 if (list_empty(&zombies))
2096 CNETERR("Deleting messages for %s: connection failed\n",
2097 libcfs_nid2str(peer->ibp_nid));
2099 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2103 kiblnd_connreq_done(kib_conn_t *conn, int status)
2105 kib_peer_t *peer = conn->ibc_peer;
2107 struct list_head txs;
2108 unsigned long flags;
2111 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2113 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2114 libcfs_nid2str(peer->ibp_nid), active,
2115 conn->ibc_version, status);
2117 LASSERT (!in_interrupt());
2118 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2119 peer->ibp_connecting > 0) ||
2120 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2121 peer->ibp_accepting > 0));
2123 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2124 conn->ibc_connvars = NULL;
2127 /* failed to establish connection */
2128 kiblnd_peer_connect_failed(peer, active, status);
2129 kiblnd_finalise_conn(conn);
2133 /* connection established */
2134 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2136 conn->ibc_last_send = jiffies;
2137 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2138 kiblnd_peer_alive(peer);
2140 /* Add conn to peer's list and nuke any dangling conns from a different
2141 * peer instance... */
2142 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2143 list_add(&conn->ibc_list, &peer->ibp_conns);
2144 peer->ibp_reconnected = 0;
2146 peer->ibp_connecting--;
2148 peer->ibp_accepting--;
2150 if (peer->ibp_version == 0) {
2151 peer->ibp_version = conn->ibc_version;
2152 peer->ibp_incarnation = conn->ibc_incarnation;
2155 if (peer->ibp_version != conn->ibc_version ||
2156 peer->ibp_incarnation != conn->ibc_incarnation) {
2157 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2158 conn->ibc_incarnation);
2159 peer->ibp_version = conn->ibc_version;
2160 peer->ibp_incarnation = conn->ibc_incarnation;
2163 /* grab pending txs while I have the lock */
2164 list_add(&txs, &peer->ibp_tx_queue);
2165 list_del_init(&peer->ibp_tx_queue);
2167 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2168 conn->ibc_comms_error != 0) { /* error has happened already */
2169 lnet_ni_t *ni = peer->ibp_ni;
2171 /* start to shut down connection */
2172 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2173 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2175 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2180 /* refcount taken by cmid is not reliable after I released the glock
2181 * because this connection is visible to other threads now, another
2182 * thread can find and close this connection right after I released
2183 * the glock, if kiblnd_cm_callback for RDMA_CM_EVENT_DISCONNECTED is
2184 * called, it can release the connection refcount taken by cmid.
2185 * It means the connection could be destroyed before I finish my
2188 kiblnd_conn_addref(conn);
2189 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2191 /* Schedule blocked txs */
2192 spin_lock(&conn->ibc_lock);
2193 while (!list_empty(&txs)) {
2194 tx = list_entry(txs.next, kib_tx_t, tx_list);
2195 list_del(&tx->tx_list);
2197 kiblnd_queue_tx_locked(tx, conn);
2199 spin_unlock(&conn->ibc_lock);
2201 kiblnd_check_sends(conn);
2203 /* schedule blocked rxs */
2204 kiblnd_handle_early_rxs(conn);
2206 kiblnd_conn_decref(conn);
2210 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2214 rc = rdma_reject(cmid, rej, sizeof(*rej));
2217 CWARN("Error %d sending reject\n", rc);
2221 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2223 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2224 kib_msg_t *reqmsg = priv;
2230 lnet_ni_t *ni = NULL;
2231 kib_net_t *net = NULL;
2233 struct rdma_conn_param cp;
2235 int version = IBLND_MSG_VERSION;
2236 unsigned long flags;
2238 struct sockaddr_in *peer_addr;
2239 LASSERT (!in_interrupt());
2241 /* cmid inherits 'context' from the corresponding listener id */
2242 ibdev = (kib_dev_t *)cmid->context;
2243 LASSERT (ibdev != NULL);
2245 memset(&rej, 0, sizeof(rej));
2246 rej.ibr_magic = IBLND_MSG_MAGIC;
2247 rej.ibr_why = IBLND_REJECT_FATAL;
2248 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2250 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2251 if (*kiblnd_tunables.kib_require_priv_port &&
2252 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2253 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2254 CERROR("Peer's port (%pI4h:%hu) is not privileged\n",
2255 &ip, ntohs(peer_addr->sin_port));
2259 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2260 CERROR("Short connection request\n");
2264 /* Future protocol version compatibility support! If the
2265 * o2iblnd-specific protocol changes, or when LNET unifies
2266 * protocols over all LNDs, the initial connection will
2267 * negotiate a protocol version. I trap this here to avoid
2268 * console errors; the reject tells the peer which protocol I
2270 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2271 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2273 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2274 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2275 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2277 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2278 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2279 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2282 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2284 CERROR("Can't parse connection request: %d\n", rc);
2288 nid = reqmsg->ibm_srcnid;
2289 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2292 net = (kib_net_t *)ni->ni_data;
2293 rej.ibr_incarnation = net->ibn_incarnation;
2296 if (ni == NULL || /* no matching net */
2297 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2298 net->ibn_dev != ibdev) { /* wrong device */
2299 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2300 "bad dst nid %s\n", libcfs_nid2str(nid),
2301 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2302 ibdev->ibd_ifname, ibdev->ibd_nnets,
2304 libcfs_nid2str(reqmsg->ibm_dstnid));
2309 /* check time stamp as soon as possible */
2310 if (reqmsg->ibm_dststamp != 0 &&
2311 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2312 CWARN("Stale connection request\n");
2313 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2317 /* I can accept peer's version */
2318 version = reqmsg->ibm_version;
2320 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2321 CERROR("Unexpected connreq msg type: %x from %s\n",
2322 reqmsg->ibm_type, libcfs_nid2str(nid));
2326 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2327 kiblnd_msg_queue_size(version, ni)) {
2328 CERROR("Can't accept conn from %s, queue depth too large: "
2329 " %d (<=%d wanted)\n",
2330 libcfs_nid2str(nid),
2331 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2332 kiblnd_msg_queue_size(version, ni));
2334 if (version == IBLND_MSG_VERSION)
2335 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2340 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2341 kiblnd_rdma_frags(version, ni)) {
2342 CWARN("Can't accept conn from %s (version %x): "
2343 "max_frags %d too large (%d wanted)\n",
2344 libcfs_nid2str(nid), version,
2345 reqmsg->ibm_u.connparams.ibcp_max_frags,
2346 kiblnd_rdma_frags(version, ni));
2348 if (version >= IBLND_MSG_VERSION)
2349 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2352 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2353 kiblnd_rdma_frags(version, ni) &&
2354 net->ibn_fmr_ps == NULL) {
2355 CWARN("Can't accept conn from %s (version %x): "
2356 "max_frags %d incompatible without FMR pool "
2358 libcfs_nid2str(nid), version,
2359 reqmsg->ibm_u.connparams.ibcp_max_frags,
2360 kiblnd_rdma_frags(version, ni));
2362 if (version == IBLND_MSG_VERSION)
2363 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2368 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2369 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2370 libcfs_nid2str(nid),
2371 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2376 /* assume 'nid' is a new peer; create */
2377 rc = kiblnd_create_peer(ni, &peer, nid);
2379 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2380 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2384 /* We have validated the peer's parameters so use those */
2385 peer->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2386 peer->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2388 write_lock_irqsave(g_lock, flags);
2390 peer2 = kiblnd_find_peer_locked(nid);
2391 if (peer2 != NULL) {
2392 if (peer2->ibp_version == 0) {
2393 peer2->ibp_version = version;
2394 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2397 /* not the guy I've talked with */
2398 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2399 peer2->ibp_version != version) {
2400 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2402 if (kiblnd_peer_active(peer2)) {
2403 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2404 peer2->ibp_version = version;
2406 write_unlock_irqrestore(g_lock, flags);
2408 CWARN("Conn stale %s version %x/%x incarnation "LPU64"/"LPU64"\n",
2409 libcfs_nid2str(nid), peer2->ibp_version, version,
2410 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2412 kiblnd_peer_decref(peer);
2413 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2417 /* tie-break connection race in favour of the higher NID */
2418 if (peer2->ibp_connecting != 0 &&
2420 write_unlock_irqrestore(g_lock, flags);
2422 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2424 kiblnd_peer_decref(peer);
2425 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2429 * passive connection is allowed even this peer is waiting for
2432 peer2->ibp_reconnecting = 0;
2433 peer2->ibp_accepting++;
2434 kiblnd_peer_addref(peer2);
2436 /* Race with kiblnd_launch_tx (active connect) to create peer
2437 * so copy validated parameters since we now know what the
2438 * peer's limits are */
2439 peer2->ibp_max_frags = peer->ibp_max_frags;
2440 peer2->ibp_queue_depth = peer->ibp_queue_depth;
2442 write_unlock_irqrestore(g_lock, flags);
2443 kiblnd_peer_decref(peer);
2446 /* Brand new peer */
2447 LASSERT (peer->ibp_accepting == 0);
2448 LASSERT (peer->ibp_version == 0 &&
2449 peer->ibp_incarnation == 0);
2451 peer->ibp_accepting = 1;
2452 peer->ibp_version = version;
2453 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2455 /* I have a ref on ni that prevents it being shutdown */
2456 LASSERT (net->ibn_shutdown == 0);
2458 kiblnd_peer_addref(peer);
2459 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2461 write_unlock_irqrestore(g_lock, flags);
2464 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2466 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2467 kiblnd_peer_decref(peer);
2468 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2472 /* conn now "owns" cmid, so I return success from here on to ensure the
2473 * CM callback doesn't destroy cmid. */
2474 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2475 conn->ibc_credits = conn->ibc_queue_depth;
2476 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2477 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2478 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2480 ackmsg = &conn->ibc_connvars->cv_msg;
2481 memset(ackmsg, 0, sizeof(*ackmsg));
2483 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2484 sizeof(ackmsg->ibm_u.connparams));
2485 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2486 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2487 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2489 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2491 memset(&cp, 0, sizeof(cp));
2492 cp.private_data = ackmsg;
2493 cp.private_data_len = ackmsg->ibm_nob;
2494 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2495 cp.initiator_depth = 0;
2496 cp.flow_control = 1;
2497 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2498 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2500 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2502 rc = rdma_accept(cmid, &cp);
2504 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2505 rej.ibr_version = version;
2506 rej.ibr_why = IBLND_REJECT_FATAL;
2508 kiblnd_reject(cmid, &rej);
2509 kiblnd_connreq_done(conn, rc);
2510 kiblnd_conn_decref(conn);
2520 rej.ibr_version = version;
2521 rej.ibr_cp.ibcp_queue_depth = kiblnd_msg_queue_size(version, ni);
2522 rej.ibr_cp.ibcp_max_frags = kiblnd_rdma_frags(version, ni);
2523 kiblnd_reject(cmid, &rej);
2525 return -ECONNREFUSED;
2529 kiblnd_check_reconnect(kib_conn_t *conn, int version,
2530 __u64 incarnation, int why, kib_connparams_t *cp)
2532 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2533 kib_peer_t *peer = conn->ibc_peer;
2535 int msg_size = IBLND_MSG_SIZE;
2539 unsigned long flags;
2541 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2542 LASSERT(peer->ibp_connecting > 0); /* 'conn' at least */
2543 LASSERT(!peer->ibp_reconnecting);
2546 msg_size = cp->ibcp_max_msg_size;
2547 frag_num = cp->ibcp_max_frags;
2548 queue_dep = cp->ibcp_queue_depth;
2551 write_lock_irqsave(glock, flags);
2552 /* retry connection if it's still needed and no other connection
2553 * attempts (active or passive) are in progress
2554 * NB: reconnect is still needed even when ibp_tx_queue is
2555 * empty if ibp_version != version because reconnect may be
2556 * initiated by kiblnd_query() */
2557 reconnect = (!list_empty(&peer->ibp_tx_queue) ||
2558 peer->ibp_version != version) &&
2559 peer->ibp_connecting == 1 &&
2560 peer->ibp_accepting == 0;
2571 case IBLND_REJECT_RDMA_FRAGS: {
2572 struct lnet_ioctl_config_lnd_tunables *tunables;
2575 reason = "can't negotiate max frags";
2578 tunables = peer->ibp_ni->ni_lnd_tunables;
2579 if (!tunables->lt_tun_u.lt_o2ib.lnd_map_on_demand) {
2580 reason = "map_on_demand must be enabled";
2583 if (conn->ibc_max_frags <= frag_num) {
2584 reason = "unsupported max frags";
2588 peer->ibp_max_frags = frag_num;
2589 reason = "rdma fragments";
2592 case IBLND_REJECT_MSG_QUEUE_SIZE:
2594 reason = "can't negotiate queue depth";
2597 if (conn->ibc_queue_depth <= queue_dep) {
2598 reason = "unsupported queue depth";
2602 peer->ibp_queue_depth = queue_dep;
2603 reason = "queue depth";
2606 case IBLND_REJECT_CONN_STALE:
2610 case IBLND_REJECT_CONN_RACE:
2611 reason = "conn race";
2614 case IBLND_REJECT_CONN_UNCOMPAT:
2615 reason = "version negotiation";
2619 conn->ibc_reconnect = 1;
2620 peer->ibp_reconnecting = 1;
2621 peer->ibp_version = version;
2622 if (incarnation != 0)
2623 peer->ibp_incarnation = incarnation;
2625 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2627 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2628 libcfs_nid2str(peer->ibp_nid),
2629 reconnect ? "reconnect" : "don't reconnect",
2630 reason, IBLND_MSG_VERSION, version, msg_size,
2631 conn->ibc_queue_depth, queue_dep,
2632 conn->ibc_max_frags, frag_num);
2634 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer
2635 * while destroying the zombie
2640 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2642 kib_peer_t *peer = conn->ibc_peer;
2644 LASSERT (!in_interrupt());
2645 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2648 case IB_CM_REJ_STALE_CONN:
2649 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2650 IBLND_REJECT_CONN_STALE, NULL);
2653 case IB_CM_REJ_INVALID_SERVICE_ID:
2654 CNETERR("%s rejected: no listener at %d\n",
2655 libcfs_nid2str(peer->ibp_nid),
2656 *kiblnd_tunables.kib_service);
2659 case IB_CM_REJ_CONSUMER_DEFINED:
2660 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2661 kib_rej_t *rej = priv;
2662 kib_connparams_t *cp = NULL;
2664 __u64 incarnation = -1;
2666 /* NB. default incarnation is -1 because:
2667 * a) V1 will ignore dst incarnation in connreq.
2668 * b) V2 will provide incarnation while rejecting me,
2669 * -1 will be overwrote.
2671 * if I try to connect to a V1 peer with V2 protocol,
2672 * it rejected me then upgrade to V2, I have no idea
2673 * about the upgrading and try to reconnect with V1,
2674 * in this case upgraded V2 can find out I'm trying to
2675 * talk to the old guy and reject me(incarnation is -1).
2678 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2679 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2680 __swab32s(&rej->ibr_magic);
2681 __swab16s(&rej->ibr_version);
2685 if (priv_nob >= sizeof(kib_rej_t) &&
2686 rej->ibr_version > IBLND_MSG_VERSION_1) {
2687 /* priv_nob is always 148 in current version
2688 * of OFED, so we still need to check version.
2689 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2693 __swab64s(&rej->ibr_incarnation);
2694 __swab16s(&cp->ibcp_queue_depth);
2695 __swab16s(&cp->ibcp_max_frags);
2696 __swab32s(&cp->ibcp_max_msg_size);
2699 incarnation = rej->ibr_incarnation;
2702 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2703 rej->ibr_magic != LNET_PROTO_MAGIC) {
2704 CERROR("%s rejected: consumer defined fatal error\n",
2705 libcfs_nid2str(peer->ibp_nid));
2709 if (rej->ibr_version != IBLND_MSG_VERSION &&
2710 rej->ibr_version != IBLND_MSG_VERSION_1) {
2711 CERROR("%s rejected: o2iblnd version %x error\n",
2712 libcfs_nid2str(peer->ibp_nid),
2717 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2718 rej->ibr_version == IBLND_MSG_VERSION_1) {
2719 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2720 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2722 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2723 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2726 switch (rej->ibr_why) {
2727 case IBLND_REJECT_CONN_RACE:
2728 case IBLND_REJECT_CONN_STALE:
2729 case IBLND_REJECT_CONN_UNCOMPAT:
2730 case IBLND_REJECT_MSG_QUEUE_SIZE:
2731 case IBLND_REJECT_RDMA_FRAGS:
2732 kiblnd_check_reconnect(conn, rej->ibr_version,
2733 incarnation, rej->ibr_why, cp);
2736 case IBLND_REJECT_NO_RESOURCES:
2737 CERROR("%s rejected: o2iblnd no resources\n",
2738 libcfs_nid2str(peer->ibp_nid));
2741 case IBLND_REJECT_FATAL:
2742 CERROR("%s rejected: o2iblnd fatal error\n",
2743 libcfs_nid2str(peer->ibp_nid));
2747 CERROR("%s rejected: o2iblnd reason %d\n",
2748 libcfs_nid2str(peer->ibp_nid),
2756 CNETERR("%s rejected: reason %d, size %d\n",
2757 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2761 kiblnd_connreq_done(conn, -ECONNREFUSED);
2765 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2767 kib_peer_t *peer = conn->ibc_peer;
2768 lnet_ni_t *ni = peer->ibp_ni;
2769 kib_net_t *net = ni->ni_data;
2770 kib_msg_t *msg = priv;
2771 int ver = conn->ibc_version;
2772 int rc = kiblnd_unpack_msg(msg, priv_nob);
2773 unsigned long flags;
2775 LASSERT (net != NULL);
2778 CERROR("Can't unpack connack from %s: %d\n",
2779 libcfs_nid2str(peer->ibp_nid), rc);
2783 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2784 CERROR("Unexpected message %d from %s\n",
2785 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2790 if (ver != msg->ibm_version) {
2791 CERROR("%s replied version %x is different with "
2792 "requested version %x\n",
2793 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2798 if (msg->ibm_u.connparams.ibcp_queue_depth >
2799 conn->ibc_queue_depth) {
2800 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
2801 libcfs_nid2str(peer->ibp_nid),
2802 msg->ibm_u.connparams.ibcp_queue_depth,
2803 conn->ibc_queue_depth);
2808 if (msg->ibm_u.connparams.ibcp_max_frags >
2809 conn->ibc_max_frags) {
2810 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
2811 libcfs_nid2str(peer->ibp_nid),
2812 msg->ibm_u.connparams.ibcp_max_frags,
2813 conn->ibc_max_frags);
2818 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2819 CERROR("%s max message size %d too big (%d max)\n",
2820 libcfs_nid2str(peer->ibp_nid),
2821 msg->ibm_u.connparams.ibcp_max_msg_size,
2827 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2828 if (msg->ibm_dstnid == ni->ni_nid &&
2829 msg->ibm_dststamp == net->ibn_incarnation)
2833 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2836 CERROR("Bad connection reply from %s, rc = %d, "
2837 "version: %x max_frags: %d\n",
2838 libcfs_nid2str(peer->ibp_nid), rc,
2839 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2843 conn->ibc_incarnation = msg->ibm_srcstamp;
2844 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
2845 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
2846 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
2847 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
2848 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2849 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
2851 kiblnd_connreq_done(conn, 0);
2855 /* NB My QP has already established itself, so I handle anything going
2856 * wrong here by setting ibc_comms_error.
2857 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2858 * immediately tears it down. */
2861 conn->ibc_comms_error = rc;
2862 kiblnd_connreq_done(conn, 0);
2866 kiblnd_active_connect (struct rdma_cm_id *cmid)
2868 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2871 struct rdma_conn_param cp;
2874 unsigned long flags;
2877 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2879 incarnation = peer->ibp_incarnation;
2880 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION :
2883 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2885 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT,
2888 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2889 kiblnd_peer_decref(peer); /* lose cmid's ref */
2893 /* conn "owns" cmid now, so I return success from here on to ensure the
2894 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2897 msg = &conn->ibc_connvars->cv_msg;
2899 memset(msg, 0, sizeof(*msg));
2900 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2901 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2902 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2903 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2905 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2906 0, peer->ibp_nid, incarnation);
2908 memset(&cp, 0, sizeof(cp));
2909 cp.private_data = msg;
2910 cp.private_data_len = msg->ibm_nob;
2911 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2912 cp.initiator_depth = 0;
2913 cp.flow_control = 1;
2914 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2915 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2917 LASSERT(cmid->context == (void *)conn);
2918 LASSERT(conn->ibc_cmid == cmid);
2920 rc = rdma_connect(cmid, &cp);
2922 CERROR("Can't connect to %s: %d\n",
2923 libcfs_nid2str(peer->ibp_nid), rc);
2924 kiblnd_connreq_done(conn, rc);
2925 kiblnd_conn_decref(conn);
2932 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2938 switch (event->event) {
2940 CERROR("Unexpected event: %d, status: %d\n",
2941 event->event, event->status);
2944 case RDMA_CM_EVENT_CONNECT_REQUEST:
2945 /* destroy cmid on failure */
2946 rc = kiblnd_passive_connect(cmid,
2947 (void *)KIBLND_CONN_PARAM(event),
2948 KIBLND_CONN_PARAM_LEN(event));
2949 CDEBUG(D_NET, "connreq: %d\n", rc);
2952 case RDMA_CM_EVENT_ADDR_ERROR:
2953 peer = (kib_peer_t *)cmid->context;
2954 CNETERR("%s: ADDR ERROR %d\n",
2955 libcfs_nid2str(peer->ibp_nid), event->status);
2956 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2957 kiblnd_peer_decref(peer);
2958 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2960 case RDMA_CM_EVENT_ADDR_RESOLVED:
2961 peer = (kib_peer_t *)cmid->context;
2963 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2964 libcfs_nid2str(peer->ibp_nid), event->status);
2966 if (event->status != 0) {
2967 CNETERR("Can't resolve address for %s: %d\n",
2968 libcfs_nid2str(peer->ibp_nid), event->status);
2971 rc = rdma_resolve_route(
2972 cmid, *kiblnd_tunables.kib_timeout * 1000);
2975 /* Can't initiate route resolution */
2976 CERROR("Can't resolve route for %s: %d\n",
2977 libcfs_nid2str(peer->ibp_nid), rc);
2979 kiblnd_peer_connect_failed(peer, 1, rc);
2980 kiblnd_peer_decref(peer);
2981 return rc; /* rc != 0 destroys cmid */
2983 case RDMA_CM_EVENT_ROUTE_ERROR:
2984 peer = (kib_peer_t *)cmid->context;
2985 CNETERR("%s: ROUTE ERROR %d\n",
2986 libcfs_nid2str(peer->ibp_nid), event->status);
2987 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2988 kiblnd_peer_decref(peer);
2989 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2991 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2992 peer = (kib_peer_t *)cmid->context;
2993 CDEBUG(D_NET,"%s Route resolved: %d\n",
2994 libcfs_nid2str(peer->ibp_nid), event->status);
2996 if (event->status == 0)
2997 return kiblnd_active_connect(cmid);
2999 CNETERR("Can't resolve route for %s: %d\n",
3000 libcfs_nid2str(peer->ibp_nid), event->status);
3001 kiblnd_peer_connect_failed(peer, 1, event->status);
3002 kiblnd_peer_decref(peer);
3003 return event->status; /* rc != 0 destroys cmid */
3005 case RDMA_CM_EVENT_UNREACHABLE:
3006 conn = (kib_conn_t *)cmid->context;
3007 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3008 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3009 CNETERR("%s: UNREACHABLE %d\n",
3010 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3011 kiblnd_connreq_done(conn, -ENETDOWN);
3012 kiblnd_conn_decref(conn);
3015 case RDMA_CM_EVENT_CONNECT_ERROR:
3016 conn = (kib_conn_t *)cmid->context;
3017 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3018 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3019 CNETERR("%s: CONNECT ERROR %d\n",
3020 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3021 kiblnd_connreq_done(conn, -ENOTCONN);
3022 kiblnd_conn_decref(conn);
3025 case RDMA_CM_EVENT_REJECTED:
3026 conn = (kib_conn_t *)cmid->context;
3027 switch (conn->ibc_state) {
3031 case IBLND_CONN_PASSIVE_WAIT:
3032 CERROR ("%s: REJECTED %d\n",
3033 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3035 kiblnd_connreq_done(conn, -ECONNRESET);
3038 case IBLND_CONN_ACTIVE_CONNECT:
3039 kiblnd_rejected(conn, event->status,
3040 (void *)KIBLND_CONN_PARAM(event),
3041 KIBLND_CONN_PARAM_LEN(event));
3044 kiblnd_conn_decref(conn);
3047 case RDMA_CM_EVENT_ESTABLISHED:
3048 conn = (kib_conn_t *)cmid->context;
3049 switch (conn->ibc_state) {
3053 case IBLND_CONN_PASSIVE_WAIT:
3054 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3055 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3056 kiblnd_connreq_done(conn, 0);
3059 case IBLND_CONN_ACTIVE_CONNECT:
3060 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3061 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3062 kiblnd_check_connreply(conn,
3063 (void *)KIBLND_CONN_PARAM(event),
3064 KIBLND_CONN_PARAM_LEN(event));
3067 /* net keeps its ref on conn! */
3070 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3071 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3074 case RDMA_CM_EVENT_DISCONNECTED:
3075 conn = (kib_conn_t *)cmid->context;
3076 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3077 CERROR("%s DISCONNECTED\n",
3078 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3079 kiblnd_connreq_done(conn, -ECONNRESET);
3081 kiblnd_close_conn(conn, 0);
3083 kiblnd_conn_decref(conn);
3084 cmid->context = NULL;
3087 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3088 LCONSOLE_ERROR_MSG(0x131,
3089 "Received notification of device removal\n"
3090 "Please shutdown LNET to allow this to proceed\n");
3091 /* Can't remove network from underneath LNET for now, so I have
3095 case RDMA_CM_EVENT_ADDR_CHANGE:
3096 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3102 kiblnd_check_txs_locked(kib_conn_t *conn, struct list_head *txs)
3105 struct list_head *ttmp;
3107 list_for_each(ttmp, txs) {
3108 tx = list_entry(ttmp, kib_tx_t, tx_list);
3110 if (txs != &conn->ibc_active_txs) {
3111 LASSERT(tx->tx_queued);
3113 LASSERT(!tx->tx_queued);
3114 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3117 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3118 CERROR("Timed out tx: %s, %lu seconds\n",
3119 kiblnd_queue2str(conn, txs),
3120 cfs_duration_sec(jiffies - tx->tx_deadline));
3129 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3131 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3132 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3133 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3134 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3135 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3139 kiblnd_check_conns (int idx)
3141 struct list_head closes = LIST_HEAD_INIT(closes);
3142 struct list_head checksends = LIST_HEAD_INIT(checksends);
3143 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3144 struct list_head *ptmp;
3147 struct list_head *ctmp;
3148 unsigned long flags;
3150 /* NB. We expect to have a look at all the peers and not find any
3151 * RDMAs to time out, so we just use a shared lock while we
3153 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3155 list_for_each(ptmp, peers) {
3156 peer = list_entry(ptmp, kib_peer_t, ibp_list);
3158 list_for_each(ctmp, &peer->ibp_conns) {
3162 conn = list_entry(ctmp, kib_conn_t, ibc_list);
3164 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3166 spin_lock(&conn->ibc_lock);
3168 sendnoop = kiblnd_need_noop(conn);
3169 timedout = kiblnd_conn_timed_out_locked(conn);
3170 if (!sendnoop && !timedout) {
3171 spin_unlock(&conn->ibc_lock);
3176 CERROR("Timed out RDMA with %s (%lu): "
3177 "c: %u, oc: %u, rc: %u\n",
3178 libcfs_nid2str(peer->ibp_nid),
3179 cfs_duration_sec(cfs_time_current() -
3180 peer->ibp_last_alive),
3182 conn->ibc_outstanding_credits,
3183 conn->ibc_reserved_credits);
3184 list_add(&conn->ibc_connd_list, &closes);
3186 list_add(&conn->ibc_connd_list, &checksends);
3188 /* +ref for 'closes' or 'checksends' */
3189 kiblnd_conn_addref(conn);
3191 spin_unlock(&conn->ibc_lock);
3195 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3197 /* Handle timeout by closing the whole
3198 * connection. We can only be sure RDMA activity
3199 * has ceased once the QP has been modified. */
3200 while (!list_empty(&closes)) {
3201 conn = list_entry(closes.next,
3202 kib_conn_t, ibc_connd_list);
3203 list_del(&conn->ibc_connd_list);
3204 kiblnd_close_conn(conn, -ETIMEDOUT);
3205 kiblnd_conn_decref(conn);
3208 /* In case we have enough credits to return via a
3209 * NOOP, but there were no non-blocking tx descs
3210 * free to do it last time... */
3211 while (!list_empty(&checksends)) {
3212 conn = list_entry(checksends.next,
3213 kib_conn_t, ibc_connd_list);
3214 list_del(&conn->ibc_connd_list);
3215 kiblnd_check_sends(conn);
3216 kiblnd_conn_decref(conn);
3221 kiblnd_disconnect_conn (kib_conn_t *conn)
3223 LASSERT (!in_interrupt());
3224 LASSERT (current == kiblnd_data.kib_connd);
3225 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3227 rdma_disconnect(conn->ibc_cmid);
3228 kiblnd_finalise_conn(conn);
3230 kiblnd_peer_notify(conn->ibc_peer);
3234 * High-water for reconnection to the same peer, reconnection attempt should
3235 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3237 #define KIB_RECONN_HIGH_RACE 10
3239 * Allow connd to take a break and handle other things after consecutive
3240 * reconnection attemps.
3242 #define KIB_RECONN_BREAK 100
3245 kiblnd_connd (void *arg)
3247 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3249 unsigned long flags;
3255 unsigned long deadline = jiffies;
3257 cfs_block_allsigs();
3259 init_waitqueue_entry(&wait, current);
3260 kiblnd_data.kib_connd = current;
3262 spin_lock_irqsave(lock, flags);
3264 while (!kiblnd_data.kib_shutdown) {
3269 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3270 kib_peer_t *peer = NULL;
3272 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3273 kib_conn_t, ibc_list);
3274 list_del(&conn->ibc_list);
3275 if (conn->ibc_reconnect) {
3276 peer = conn->ibc_peer;
3277 kiblnd_peer_addref(peer);
3280 spin_unlock_irqrestore(lock, flags);
3283 kiblnd_destroy_conn(conn, !peer);
3285 spin_lock_irqsave(lock, flags);
3289 conn->ibc_peer = peer;
3290 if (peer->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3291 list_add_tail(&conn->ibc_list,
3292 &kiblnd_data.kib_reconn_list);
3294 list_add_tail(&conn->ibc_list,
3295 &kiblnd_data.kib_reconn_wait);
3298 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3299 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3300 kib_conn_t, ibc_list);
3301 list_del(&conn->ibc_list);
3303 spin_unlock_irqrestore(lock, flags);
3306 kiblnd_disconnect_conn(conn);
3307 kiblnd_conn_decref(conn);
3309 spin_lock_irqsave(lock, flags);
3312 while (reconn < KIB_RECONN_BREAK) {
3313 if (kiblnd_data.kib_reconn_sec != get_seconds()) {
3314 kiblnd_data.kib_reconn_sec = get_seconds();
3315 list_splice_init(&kiblnd_data.kib_reconn_wait,
3316 &kiblnd_data.kib_reconn_list);
3319 if (list_empty(&kiblnd_data.kib_reconn_list))
3322 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3323 kib_conn_t, ibc_list);
3324 list_del(&conn->ibc_list);
3326 spin_unlock_irqrestore(lock, flags);
3329 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3330 kiblnd_peer_decref(conn->ibc_peer);
3331 LIBCFS_FREE(conn, sizeof(*conn));
3333 spin_lock_irqsave(lock, flags);
3336 /* careful with the jiffy wrap... */
3337 timeout = (int)(deadline - jiffies);
3341 int chunk = kiblnd_data.kib_peer_hash_size;
3343 spin_unlock_irqrestore(lock, flags);
3346 /* Time to check for RDMA timeouts on a few more
3347 * peers: I do checks every 'p' seconds on a
3348 * proportion of the peer table and I need to check
3349 * every connection 'n' times within a timeout
3350 * interval, to ensure I detect a timeout on any
3351 * connection within (n+1)/n times the timeout
3354 if (*kiblnd_tunables.kib_timeout > n * p)
3355 chunk = (chunk * n * p) /
3356 *kiblnd_tunables.kib_timeout;
3360 for (i = 0; i < chunk; i++) {
3361 kiblnd_check_conns(peer_index);
3362 peer_index = (peer_index + 1) %
3363 kiblnd_data.kib_peer_hash_size;
3366 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3367 spin_lock_irqsave(lock, flags);
3373 /* Nothing to do for 'timeout' */
3374 set_current_state(TASK_INTERRUPTIBLE);
3375 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3376 spin_unlock_irqrestore(lock, flags);
3378 schedule_timeout(timeout);
3380 set_current_state(TASK_RUNNING);
3381 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3382 spin_lock_irqsave(lock, flags);
3385 spin_unlock_irqrestore(lock, flags);
3387 kiblnd_thread_fini();
3392 kiblnd_qp_event(struct ib_event *event, void *arg)
3394 kib_conn_t *conn = arg;
3396 switch (event->event) {
3397 case IB_EVENT_COMM_EST:
3398 CDEBUG(D_NET, "%s established\n",
3399 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3403 CERROR("%s: Async QP event type %d\n",
3404 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3410 kiblnd_complete (struct ib_wc *wc)
3412 switch (kiblnd_wreqid2type(wc->wr_id)) {
3417 if (wc->status != IB_WC_SUCCESS &&
3418 wc->status != IB_WC_WR_FLUSH_ERR)
3419 CNETERR("FastReg failed: %d\n", wc->status);
3422 case IBLND_WID_RDMA:
3423 /* We only get RDMA completion notification if it fails. All
3424 * subsequent work items, including the final SEND will fail
3425 * too. However we can't print out any more info about the
3426 * failing RDMA because 'tx' might be back on the idle list or
3427 * even reused already if we didn't manage to post all our work
3429 CNETERR("RDMA (tx: %p) failed: %d\n",
3430 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3434 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3438 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3445 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3447 /* NB I'm not allowed to schedule this conn once its refcount has
3448 * reached 0. Since fundamentally I'm racing with scheduler threads
3449 * consuming my CQ I could be called after all completions have
3450 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3451 * and this CQ is about to be destroyed so I NOOP. */
3452 kib_conn_t *conn = (kib_conn_t *)arg;
3453 struct kib_sched_info *sched = conn->ibc_sched;
3454 unsigned long flags;
3456 LASSERT(cq == conn->ibc_cq);
3458 spin_lock_irqsave(&sched->ibs_lock, flags);
3460 conn->ibc_ready = 1;
3462 if (!conn->ibc_scheduled &&
3463 (conn->ibc_nrx > 0 ||
3464 conn->ibc_nsends_posted > 0)) {
3465 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3466 conn->ibc_scheduled = 1;
3467 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3469 if (waitqueue_active(&sched->ibs_waitq))
3470 wake_up(&sched->ibs_waitq);
3473 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3477 kiblnd_cq_event(struct ib_event *event, void *arg)
3479 kib_conn_t *conn = arg;
3481 CERROR("%s: async CQ event type %d\n",
3482 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3486 kiblnd_scheduler(void *arg)
3488 long id = (long)arg;
3489 struct kib_sched_info *sched;
3492 unsigned long flags;
3498 cfs_block_allsigs();
3500 init_waitqueue_entry(&wait, current);
3502 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3504 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3506 CWARN("Failed to bind on CPT %d, please verify whether "
3507 "all CPUs are healthy and reload modules if necessary, "
3508 "otherwise your system might under risk of low "
3509 "performance\n", sched->ibs_cpt);
3512 spin_lock_irqsave(&sched->ibs_lock, flags);
3514 while (!kiblnd_data.kib_shutdown) {
3515 if (busy_loops++ >= IBLND_RESCHED) {
3516 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3521 spin_lock_irqsave(&sched->ibs_lock, flags);
3526 if (!list_empty(&sched->ibs_conns)) {
3527 conn = list_entry(sched->ibs_conns.next,
3528 kib_conn_t, ibc_sched_list);
3529 /* take over kib_sched_conns' ref on conn... */
3530 LASSERT(conn->ibc_scheduled);
3531 list_del(&conn->ibc_sched_list);
3532 conn->ibc_ready = 0;
3534 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3536 wc.wr_id = IBLND_WID_INVAL;
3538 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3540 rc = ib_req_notify_cq(conn->ibc_cq,
3543 CWARN("%s: ib_req_notify_cq failed: %d, "
3544 "closing connection\n",
3545 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3546 kiblnd_close_conn(conn, -EIO);
3547 kiblnd_conn_decref(conn);
3548 spin_lock_irqsave(&sched->ibs_lock,
3553 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3556 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3558 "ib_poll_cq (rc: %d) returned invalid "
3559 "wr_id, opcode %d, status: %d, "
3560 "vendor_err: %d, conn: %s status: %d\n"
3561 "please upgrade firmware and OFED or "
3562 "contact vendor.\n", rc,
3563 wc.opcode, wc.status, wc.vendor_err,
3564 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3570 CWARN("%s: ib_poll_cq failed: %d, "
3571 "closing connection\n",
3572 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3574 kiblnd_close_conn(conn, -EIO);
3575 kiblnd_conn_decref(conn);
3576 spin_lock_irqsave(&sched->ibs_lock, flags);
3580 spin_lock_irqsave(&sched->ibs_lock, flags);
3582 if (rc != 0 || conn->ibc_ready) {
3583 /* There may be another completion waiting; get
3584 * another scheduler to check while I handle
3586 /* +1 ref for sched_conns */
3587 kiblnd_conn_addref(conn);
3588 list_add_tail(&conn->ibc_sched_list,
3590 if (waitqueue_active(&sched->ibs_waitq))
3591 wake_up(&sched->ibs_waitq);
3593 conn->ibc_scheduled = 0;
3597 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3598 kiblnd_complete(&wc);
3600 spin_lock_irqsave(&sched->ibs_lock, flags);
3603 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3610 set_current_state(TASK_INTERRUPTIBLE);
3611 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3612 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3617 remove_wait_queue(&sched->ibs_waitq, &wait);
3618 set_current_state(TASK_RUNNING);
3619 spin_lock_irqsave(&sched->ibs_lock, flags);
3622 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3624 kiblnd_thread_fini();
3629 kiblnd_failover_thread(void *arg)
3631 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3634 unsigned long flags;
3637 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3639 cfs_block_allsigs();
3641 init_waitqueue_entry(&wait, current);
3642 write_lock_irqsave(glock, flags);
3644 while (!kiblnd_data.kib_shutdown) {
3645 int do_failover = 0;
3648 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3650 if (cfs_time_before(cfs_time_current(),
3651 dev->ibd_next_failover))
3658 list_del_init(&dev->ibd_fail_list);
3659 dev->ibd_failover = 1;
3660 write_unlock_irqrestore(glock, flags);
3662 rc = kiblnd_dev_failover(dev);
3664 write_lock_irqsave(glock, flags);
3666 LASSERT (dev->ibd_failover);
3667 dev->ibd_failover = 0;
3668 if (rc >= 0) { /* Device is OK or failover succeed */
3669 dev->ibd_next_failover = cfs_time_shift(3);
3673 /* failed to failover, retry later */
3674 dev->ibd_next_failover =
3675 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3676 if (kiblnd_dev_can_failover(dev)) {
3677 list_add_tail(&dev->ibd_fail_list,
3678 &kiblnd_data.kib_failed_devs);
3684 /* long sleep if no more pending failover */
3685 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3687 set_current_state(TASK_INTERRUPTIBLE);
3688 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3689 write_unlock_irqrestore(glock, flags);
3691 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3692 cfs_time_seconds(1));
3693 set_current_state(TASK_RUNNING);
3694 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3695 write_lock_irqsave(glock, flags);
3697 if (!long_sleep || rc != 0)
3700 /* have a long sleep, routine check all active devices,
3701 * we need checking like this because if there is not active
3702 * connection on the dev and no SEND from local, we may listen
3703 * on wrong HCA for ever while there is a bonding failover */
3704 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3705 if (kiblnd_dev_can_failover(dev)) {
3706 list_add_tail(&dev->ibd_fail_list,
3707 &kiblnd_data.kib_failed_devs);
3712 write_unlock_irqrestore(glock, flags);
3714 kiblnd_thread_fini();