1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Author: Eric Barton <eric@bartonsoftware.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 kiblnd_msgtype2str(int type)
30 case IBLND_MSG_CONNREQ:
33 case IBLND_MSG_CONNACK:
39 case IBLND_MSG_IMMEDIATE:
42 case IBLND_MSG_PUT_REQ:
45 case IBLND_MSG_PUT_NAK:
48 case IBLND_MSG_PUT_ACK:
51 case IBLND_MSG_PUT_DONE:
54 case IBLND_MSG_GET_REQ:
57 case IBLND_MSG_GET_DONE:
66 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
68 lnet_msg_t *lntmsg[2];
69 kib_net_t *net = ni->ni_data;
73 LASSERT (net != NULL);
74 LASSERT (!in_interrupt());
75 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
76 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
77 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
79 #if IBLND_MAP_ON_DEMAND
80 if (tx->tx_fmr != NULL) {
81 rc = ib_fmr_pool_unmap(tx->tx_fmr);
84 if (tx->tx_status != 0) {
85 rc = ib_flush_fmr_pool(net->ibn_fmrpool);
92 if (tx->tx_nfrags != 0) {
93 kiblnd_dma_unmap_sg(net->ibn_dev->ibd_cmid->device,
94 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
98 /* tx may have up to 2 lnet msgs to finalise */
99 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
100 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
103 if (tx->tx_conn != NULL) {
104 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
106 kiblnd_conn_decref(tx->tx_conn);
113 spin_lock(&net->ibn_tx_lock);
115 list_add(&tx->tx_list, &net->ibn_idle_txs);
117 spin_unlock(&net->ibn_tx_lock);
119 /* delay finalize until my descs have been freed */
120 for (i = 0; i < 2; i++) {
121 if (lntmsg[i] == NULL)
124 lnet_finalize(ni, lntmsg[i], rc);
129 kiblnd_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int status)
133 while (!list_empty (txlist)) {
134 tx = list_entry (txlist->next, kib_tx_t, tx_list);
136 list_del (&tx->tx_list);
139 tx->tx_status = status;
140 kiblnd_tx_done(ni, tx);
145 kiblnd_get_idle_tx (lnet_ni_t *ni)
147 kib_net_t *net = ni->ni_data;
150 LASSERT (net != NULL);
152 spin_lock(&net->ibn_tx_lock);
154 if (list_empty(&net->ibn_idle_txs)) {
155 spin_unlock(&net->ibn_tx_lock);
159 tx = list_entry(net->ibn_idle_txs.next, kib_tx_t, tx_list);
160 list_del(&tx->tx_list);
162 /* Allocate a new completion cookie. It might not be needed,
163 * but we've got a lock right now and we're unlikely to
165 tx->tx_cookie = kiblnd_data.kib_next_tx_cookie++;
167 spin_unlock(&net->ibn_tx_lock);
169 LASSERT (tx->tx_nwrq == 0);
170 LASSERT (!tx->tx_queued);
171 LASSERT (tx->tx_sending == 0);
172 LASSERT (!tx->tx_waiting);
173 LASSERT (tx->tx_status == 0);
174 LASSERT (tx->tx_conn == NULL);
175 LASSERT (tx->tx_lntmsg[0] == NULL);
176 LASSERT (tx->tx_lntmsg[1] == NULL);
177 #if IBLND_MAP_ON_DEMAND
178 LASSERT (tx->tx_fmr == NULL);
180 LASSERT (tx->tx_nfrags == 0);
187 kiblnd_drop_rx (kib_rx_t *rx)
189 kib_conn_t *conn = rx->rx_conn;
192 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
193 LASSERT (conn->ibc_nrx > 0);
195 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
197 kiblnd_conn_decref(conn);
201 kiblnd_post_rx (kib_rx_t *rx, int credit)
203 kib_conn_t *conn = rx->rx_conn;
204 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
205 struct ib_recv_wr *bad_wrq;
208 LASSERT (net != NULL);
209 LASSERT (!in_interrupt());
210 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
211 credit == IBLND_POSTRX_PEER_CREDIT ||
212 credit == IBLND_POSTRX_RSRVD_CREDIT);
214 rx->rx_sge.length = IBLND_MSG_SIZE;
215 rx->rx_sge.lkey = net->ibn_dev->ibd_mr->lkey;
216 rx->rx_sge.addr = rx->rx_msgaddr;
218 rx->rx_wrq.next = NULL;
219 rx->rx_wrq.sg_list = &rx->rx_sge;
220 rx->rx_wrq.num_sge = 1;
221 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
223 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
224 LASSERT (rx->rx_nob >= 0); /* not posted */
226 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
227 kiblnd_drop_rx(rx); /* No more posts for this rx */
231 rx->rx_nob = -1; /* flag posted */
233 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
235 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
239 CERROR("Can't post rx for %s: %d\n",
240 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
241 kiblnd_close_conn(conn, rc);
242 kiblnd_drop_rx(rx); /* No more posts for this rx */
246 if (credit == IBLND_POSTRX_NO_CREDIT)
249 spin_lock(&conn->ibc_lock);
250 if (credit == IBLND_POSTRX_PEER_CREDIT)
251 conn->ibc_outstanding_credits++;
253 conn->ibc_reserved_credits++;
254 spin_unlock(&conn->ibc_lock);
256 kiblnd_check_sends(conn);
261 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
263 struct list_head *tmp;
265 list_for_each(tmp, &conn->ibc_active_txs) {
266 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
268 LASSERT (!tx->tx_queued);
269 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
271 if (tx->tx_cookie != cookie)
274 if (tx->tx_waiting &&
275 tx->tx_msg->ibm_type == txtype)
278 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
279 tx->tx_waiting ? "" : "NOT ",
280 tx->tx_msg->ibm_type, txtype);
286 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
289 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
292 spin_lock(&conn->ibc_lock);
294 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
296 spin_unlock(&conn->ibc_lock);
298 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
299 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
300 kiblnd_close_conn(conn, -EPROTO);
304 if (tx->tx_status == 0) { /* success so far */
305 if (status < 0) { /* failed? */
306 tx->tx_status = status;
307 } else if (txtype == IBLND_MSG_GET_REQ) {
308 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
314 idle = !tx->tx_queued && (tx->tx_sending == 0);
316 list_del(&tx->tx_list);
318 spin_unlock(&conn->ibc_lock);
321 kiblnd_tx_done(ni, tx);
325 kiblnd_send_completion (kib_conn_t *conn, int type, int status, __u64 cookie)
327 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
328 kib_tx_t *tx = kiblnd_get_idle_tx(ni);
331 CERROR("Can't get tx for completion %x for %s\n",
332 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
336 tx->tx_msg->ibm_u.completion.ibcm_status = status;
337 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
338 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
340 kiblnd_queue_tx(tx, conn);
344 kiblnd_handle_rx (kib_rx_t *rx)
346 kib_msg_t *msg = rx->rx_msg;
347 kib_conn_t *conn = rx->rx_conn;
348 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
349 int credits = msg->ibm_credits;
355 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
357 CDEBUG (D_NET, "Received %x[%d] from %s\n",
358 msg->ibm_type, credits, libcfs_nid2str(conn->ibc_peer->ibp_nid));
361 /* Have I received credits that will let me send? */
362 spin_lock(&conn->ibc_lock);
364 if (conn->ibc_credits + credits > IBLND_MSG_QUEUE_SIZE) {
365 rc2 = conn->ibc_credits;
366 spin_unlock(&conn->ibc_lock);
368 CERROR("Bad credits from %s: %d + %d > %d\n",
369 libcfs_nid2str(conn->ibc_peer->ibp_nid),
370 rc2, credits, IBLND_MSG_QUEUE_SIZE);
372 kiblnd_close_conn(conn, -EPROTO);
373 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
377 conn->ibc_credits += credits;
379 spin_unlock(&conn->ibc_lock);
380 kiblnd_check_sends(conn);
383 switch (msg->ibm_type) {
385 CERROR("Bad IBLND message type %x from %s\n",
386 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
387 post_credit = IBLND_POSTRX_NO_CREDIT;
392 post_credit = IBLND_POSTRX_PEER_CREDIT;
395 case IBLND_MSG_IMMEDIATE:
396 post_credit = IBLND_POSTRX_DONT_POST;
397 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
398 msg->ibm_srcnid, rx, 0);
399 if (rc < 0) /* repost on error */
400 post_credit = IBLND_POSTRX_PEER_CREDIT;
403 case IBLND_MSG_PUT_REQ:
404 post_credit = IBLND_POSTRX_DONT_POST;
405 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
406 msg->ibm_srcnid, rx, 1);
407 if (rc < 0) /* repost on error */
408 post_credit = IBLND_POSTRX_PEER_CREDIT;
411 case IBLND_MSG_PUT_NAK:
412 CWARN ("PUT_NACK from %s\n", libcfs_nid2str(conn->ibc_peer->ibp_nid));
413 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
414 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
415 msg->ibm_u.completion.ibcm_status,
416 msg->ibm_u.completion.ibcm_cookie);
419 case IBLND_MSG_PUT_ACK:
420 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
422 spin_lock(&conn->ibc_lock);
423 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
424 msg->ibm_u.putack.ibpam_src_cookie);
426 list_del(&tx->tx_list);
427 spin_unlock(&conn->ibc_lock);
430 CERROR("Unmatched PUT_ACK from %s\n",
431 libcfs_nid2str(conn->ibc_peer->ibp_nid));
436 LASSERT (tx->tx_waiting);
437 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
438 * (a) I can overwrite tx_msg since my peer has received it!
439 * (b) tx_waiting set tells tx_complete() it's not done. */
441 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
443 rc2 = kiblnd_init_rdma(ni, tx, IBLND_MSG_PUT_DONE,
444 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
445 &msg->ibm_u.putack.ibpam_rd,
446 msg->ibm_u.putack.ibpam_dst_cookie);
448 CERROR("Can't setup rdma for PUT to %s: %d\n",
449 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
451 spin_lock(&conn->ibc_lock);
452 tx->tx_waiting = 0; /* clear waiting and queue atomically */
453 kiblnd_queue_tx_locked(tx, conn);
454 spin_unlock(&conn->ibc_lock);
457 case IBLND_MSG_PUT_DONE:
458 post_credit = IBLND_POSTRX_PEER_CREDIT;
459 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
460 msg->ibm_u.completion.ibcm_status,
461 msg->ibm_u.completion.ibcm_cookie);
464 case IBLND_MSG_GET_REQ:
465 post_credit = IBLND_POSTRX_DONT_POST;
466 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
467 msg->ibm_srcnid, rx, 1);
468 if (rc < 0) /* repost on error */
469 post_credit = IBLND_POSTRX_PEER_CREDIT;
472 case IBLND_MSG_GET_DONE:
473 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
474 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
475 msg->ibm_u.completion.ibcm_status,
476 msg->ibm_u.completion.ibcm_cookie);
480 if (rc < 0) /* protocol error */
481 kiblnd_close_conn(conn, rc);
483 if (post_credit != IBLND_POSTRX_DONT_POST)
484 kiblnd_post_rx(rx, post_credit);
488 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
490 kib_msg_t *msg = rx->rx_msg;
491 kib_conn_t *conn = rx->rx_conn;
492 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
493 kib_net_t *net = ni->ni_data;
498 LASSERT (net != NULL);
499 LASSERT (rx->rx_nob < 0); /* was posted */
500 rx->rx_nob = 0; /* isn't now */
502 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
505 if (status != IB_WC_SUCCESS) {
506 CDEBUG(D_NETERROR, "Rx from %s failed: %d\n",
507 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
514 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
516 CERROR ("Error %d unpacking rx from %s\n",
517 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
521 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
522 msg->ibm_dstnid != ni->ni_nid ||
523 msg->ibm_srcstamp != conn->ibc_incarnation ||
524 msg->ibm_dststamp != net->ibn_incarnation) {
525 CERROR ("Stale rx from %s\n",
526 libcfs_nid2str(conn->ibc_peer->ibp_nid));
531 /* set time last known alive */
532 kiblnd_peer_alive(conn->ibc_peer);
534 /* racing with connection establishment/teardown! */
536 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
537 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
538 /* must check holding global lock to eliminate race */
539 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
540 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
541 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
545 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
548 kiblnd_handle_rx(rx);
552 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
553 kiblnd_close_conn(conn, err);
555 kiblnd_drop_rx(rx); /* Don't re-post rx. */
559 kiblnd_kvaddr_to_page (unsigned long vaddr)
563 if (vaddr >= VMALLOC_START &&
564 vaddr < VMALLOC_END) {
565 page = vmalloc_to_page ((void *)vaddr);
566 LASSERT (page != NULL);
569 #ifdef CONFIG_HIGHMEM
570 if (vaddr >= PKMAP_BASE &&
571 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
572 /* No highmem pages only used for bulk (kiov) I/O */
573 CERROR("find page for address in highmem\n");
577 page = virt_to_page (vaddr);
578 LASSERT (page != NULL);
582 #if !IBLND_MAP_ON_DEMAND
584 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
585 unsigned int niov, struct iovec *iov, int offset, int nob)
588 struct scatterlist *sg;
594 kib_net_t *net = ni->ni_data;
598 LASSERT (net != NULL);
600 while (offset >= iov->iov_len) {
601 offset -= iov->iov_len;
611 vaddr = ((unsigned long)iov->iov_base) + offset;
612 page_offset = vaddr & (PAGE_SIZE - 1);
613 page = kiblnd_kvaddr_to_page(vaddr);
615 CERROR ("Can't find page\n");
619 fragnob = min((int)(iov->iov_len - offset), nob);
620 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
623 sg->offset = page_offset;
624 sg->length = fragnob;
627 if (offset + fragnob < iov->iov_len) {
637 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
639 tx->tx_nfrags = sg - tx->tx_frags;
640 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
642 rd->rd_nfrags = kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
643 tx->tx_frags, tx->tx_nfrags,
645 rd->rd_key = (rd != tx->tx_rd) ?
646 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
648 for (i = 0; i < rd->rd_nfrags; i++) {
649 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
650 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
651 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
652 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
659 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
660 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
662 struct scatterlist *sg;
665 kib_net_t *net = ni->ni_data;
667 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
671 LASSERT (net != NULL);
673 while (offset >= kiov->kiov_len) {
674 offset -= kiov->kiov_len;
684 fragnob = min((int)(kiov->kiov_len - offset), nob);
686 memset(sg, 0, sizeof(*sg));
687 sg->page = kiov->kiov_page;
688 sg->offset = kiov->kiov_offset + offset;
689 sg->length = fragnob;
698 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
700 tx->tx_nfrags = sg - tx->tx_frags;
701 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
703 rd->rd_nfrags = kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
704 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
705 rd->rd_key = (rd != tx->tx_rd) ?
706 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
708 for (i = 0; i < tx->tx_nfrags; i++) {
709 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
710 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
711 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
712 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
714 CDEBUG(D_WARNING,"frag[%d]: "LPX64" for %d\n",
715 i, rd->rd_frags[i].rf_addr, rd->rd_frags[i].rf_nob);
723 kiblnd_map_tx (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
724 int npages, unsigned long page_offset, int nob)
726 struct ib_pool_fmr *fmr;
727 kib_net_t *net = ni->ni_data;
729 LASSERT (net != NULL);
730 LASSERT (tx->tx_fmr == NULL);
731 LASSERT (page_offset < PAGE_SIZE);
732 LASSERT (npages >= (1 + ((page_offset + nob - 1)>>PAGE_SHIFT)));
733 LASSERT (npages <= LNET_MAX_IOV);
737 fmr = ib_fmr_pool_map_phys(net->ibn_fmrpool, tx->tx_pages,
738 npages, rd->rd_addr);
740 CERROR ("Can't map %d pages: %ld\n", npages, PTR_ERR(fmr));
744 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
747 rd->rd_key = (rd != tx->tx_rd) ? fmr->fmr->rkey : fmr->fmr->lkey;
755 kiblnd_setup_rd_iov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
756 unsigned int niov, struct iovec *iov, int offset, int nob)
763 unsigned long page_offset;
769 while (offset >= iov->iov_len) {
770 offset -= iov->iov_len;
776 if (nob > iov->iov_len - offset) {
777 CERROR ("Can't map multiple vaddr fragments\n");
781 vaddr = ((unsigned long)iov->iov_base) + offset;
783 page_offset = vaddr & (PAGE_SIZE - 1);
788 LASSERT (npages < LNET_MAX_IOV);
790 page = kiblnd_kvaddr_to_page(vaddr);
792 CERROR("Can't find page for %lu\n", vaddr);
796 tx->tx_pages[npages++] = lnet_page2phys(page);
798 fragnob = PAGE_SIZE - (vaddr & (PAGE_SIZE - 1));
804 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
808 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
809 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
813 unsigned long page_offset;
815 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
819 LASSERT (nkiov <= LNET_MAX_IOV);
821 while (offset >= kiov->kiov_len) {
822 offset -= kiov->kiov_len;
828 page_offset = kiov->kiov_offset + offset;
830 resid = offset + nob;
834 LASSERT (npages < LNET_MAX_IOV);
837 if ((npages > 0 && kiov->kiov_offset != 0) ||
838 (resid > kiov->kiov_len &&
839 (kiov->kiov_offset + kiov->kiov_len) != PAGE_SIZE)) {
840 /* Can't have gaps */
841 CERROR ("Can't make payload contiguous in I/O VM:"
842 "page %d, offset %d, len %d \n",
843 npages, kiov->kiov_offset, kiov->kiov_len);
848 tx->tx_pages[npages++] = lnet_page2phys(kiov->kiov_page);
849 resid -= kiov->kiov_len;
854 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
859 kiblnd_check_sends (kib_conn_t *conn)
862 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
864 int consume_cred = 0;
865 struct ib_send_wr *bad_wrq;
868 /* Don't send anything until after the connection is established */
869 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
870 CDEBUG(D_NET, "%s too soon\n",
871 libcfs_nid2str(conn->ibc_peer->ibp_nid));
875 spin_lock(&conn->ibc_lock);
877 LASSERT (conn->ibc_nsends_posted <=
878 *kiblnd_tunables.kib_concurrent_sends);
879 LASSERT (conn->ibc_reserved_credits >= 0);
881 while (conn->ibc_reserved_credits > 0 &&
882 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
883 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
885 list_del(&tx->tx_list);
886 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
887 conn->ibc_reserved_credits--;
890 if (list_empty(&conn->ibc_tx_queue) &&
891 list_empty(&conn->ibc_tx_queue_nocred) &&
892 (conn->ibc_outstanding_credits >= IBLND_CREDIT_HIGHWATER ||
893 kiblnd_send_keepalive(conn))) {
894 spin_unlock(&conn->ibc_lock);
896 tx = kiblnd_get_idle_tx(ni);
898 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
900 spin_lock(&conn->ibc_lock);
903 kiblnd_queue_tx_locked(tx, conn);
907 if (!list_empty (&conn->ibc_tx_queue_nocred)) {
908 tx = list_entry (conn->ibc_tx_queue_nocred.next,
911 } else if (!list_empty (&conn->ibc_tx_queue)) {
912 tx = list_entry (conn->ibc_tx_queue.next,
916 /* nothing to send right now */
920 LASSERT (tx->tx_queued);
921 /* We rely on this for QP sizing */
922 LASSERT (tx->tx_nwrq > 0 &&
923 tx->tx_nwrq <= 1 + IBLND_MAX_RDMA_FRAGS);
925 LASSERT (conn->ibc_outstanding_credits >= 0);
926 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE);
927 LASSERT (conn->ibc_credits >= 0);
928 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE);
930 if (conn->ibc_nsends_posted ==
931 *kiblnd_tunables.kib_concurrent_sends) {
932 /* tx completions outstanding... */
933 CDEBUG(D_NET, "%s: posted enough\n",
934 libcfs_nid2str(conn->ibc_peer->ibp_nid));
939 if (conn->ibc_credits == 0) { /* no credits */
940 CDEBUG(D_NET, "%s: no credits\n",
941 libcfs_nid2str(conn->ibc_peer->ibp_nid));
945 if (conn->ibc_credits == 1 && /* last credit reserved for */
946 conn->ibc_outstanding_credits == 0) { /* giving back credits */
947 CDEBUG(D_NET, "%s: not using last credit\n",
948 libcfs_nid2str(conn->ibc_peer->ibp_nid));
953 list_del (&tx->tx_list);
956 /* NB don't drop ibc_lock before bumping tx_sending */
958 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP &&
959 (!list_empty(&conn->ibc_tx_queue) ||
960 !list_empty(&conn->ibc_tx_queue_nocred) ||
961 (conn->ibc_outstanding_credits < IBLND_CREDIT_HIGHWATER &&
962 !kiblnd_send_keepalive(conn)))) {
964 spin_unlock(&conn->ibc_lock);
965 kiblnd_tx_done(ni, tx);
966 spin_lock(&conn->ibc_lock);
967 CDEBUG(D_NET, "%s: redundant noop\n",
968 libcfs_nid2str(conn->ibc_peer->ibp_nid));
972 kiblnd_pack_msg(ni, tx->tx_msg, conn->ibc_outstanding_credits,
973 conn->ibc_peer->ibp_nid, conn->ibc_incarnation);
975 conn->ibc_outstanding_credits = 0;
976 conn->ibc_nsends_posted++;
980 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
981 * PUT. If so, it was first queued here as a PUT_REQ, sent and
982 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
983 * and then re-queued here. It's (just) possible that
984 * tx_sending is non-zero if we've not done the tx_complete() from
985 * the first send; hence the ++ rather than = below. */
988 list_add (&tx->tx_list, &conn->ibc_active_txs);
993 for (i = 0; i < tx->tx_nwrq - 1; i++) {
994 LASSERT (tx->tx_wrq[i].opcode == IB_WR_RDMA_WRITE);
995 LASSERT (tx->tx_wrq[i].next == &tx->tx_wrq[i+1]);
996 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
998 CDEBUG(D_WARNING, "WORK[%d]: RDMA "LPX64
999 " for %d k %x -> "LPX64" k %x\n", i,
1000 tx->tx_wrq[i].sg_list->addr,
1001 tx->tx_wrq[i].sg_list->length,
1002 tx->tx_wrq[i].sg_list->lkey,
1003 tx->tx_wrq[i].wr.rdma.remote_addr,
1004 tx->tx_wrq[i].wr.rdma.rkey);
1007 LASSERT (tx->tx_wrq[i].opcode == IB_WR_SEND);
1008 LASSERT (tx->tx_wrq[i].next == NULL);
1009 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
1011 CDEBUG(D_WARNING, "WORK[%d]: SEND "LPX64" for %d k %x\n", i,
1012 tx->tx_wrq[i].sg_list->addr,
1013 tx->tx_wrq[i].sg_list->length,
1014 tx->tx_wrq[i].sg_list->lkey);
1017 /* I'm still holding ibc_lock! */
1018 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1021 rc = ib_post_send(conn->ibc_cmid->qp, tx->tx_wrq, &bad_wrq);
1023 conn->ibc_last_send = jiffies;
1026 /* NB credits are transferred in the actual
1027 * message, which can only be the last work item */
1028 conn->ibc_outstanding_credits += tx->tx_msg->ibm_credits;
1030 conn->ibc_credits++;
1031 conn->ibc_nsends_posted--;
1037 done = (tx->tx_sending == 0);
1039 list_del (&tx->tx_list);
1041 spin_unlock(&conn->ibc_lock);
1043 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1044 CERROR("Error %d posting transmit to %s\n",
1045 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1047 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1048 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1050 kiblnd_close_conn(conn, rc);
1053 kiblnd_tx_done(ni, tx);
1058 spin_unlock(&conn->ibc_lock);
1062 kiblnd_tx_complete (kib_tx_t *tx, int status)
1064 int failed = (status != IB_WC_SUCCESS);
1065 kib_conn_t *conn = tx->tx_conn;
1068 LASSERT (tx->tx_sending > 0);
1071 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1072 CDEBUG(D_NETERROR, "Tx -> %s cookie "LPX64
1073 "sending %d waiting %d: failed %d\n",
1074 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1075 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1078 kiblnd_close_conn(conn, -EIO);
1080 kiblnd_peer_alive(conn->ibc_peer);
1083 spin_lock(&conn->ibc_lock);
1085 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1086 * gets to free it, which also drops its ref on 'conn'. */
1089 conn->ibc_nsends_posted--;
1092 tx->tx_waiting = 0; /* don't wait for peer */
1093 tx->tx_status = -EIO;
1096 idle = (tx->tx_sending == 0) && /* This is the final callback */
1097 !tx->tx_waiting && /* Not waiting for peer */
1098 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1100 list_del(&tx->tx_list);
1102 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1104 spin_unlock(&conn->ibc_lock);
1107 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1109 kiblnd_check_sends(conn);
1111 kiblnd_conn_decref(conn); /* ...until here */
1115 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1117 kib_net_t *net = ni->ni_data;
1118 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1119 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1120 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1122 LASSERT (net != NULL);
1123 LASSERT (tx->tx_nwrq >= 0);
1124 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1125 LASSERT (nob <= IBLND_MSG_SIZE);
1127 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1129 sge->addr = tx->tx_msgaddr;
1130 sge->lkey = net->ibn_dev->ibd_mr->lkey;
1133 memset(wrq, 0, sizeof(*wrq));
1136 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1139 wrq->opcode = IB_WR_SEND;
1140 wrq->send_flags = IB_SEND_SIGNALED;
1146 kiblnd_init_rdma (lnet_ni_t *ni, kib_tx_t *tx, int type,
1147 int nob, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1149 kib_msg_t *ibmsg = tx->tx_msg;
1150 kib_rdma_desc_t *srcrd = tx->tx_rd;
1151 struct ib_sge *sge = &tx->tx_sge[0];
1152 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1155 #if IBLND_MAP_ON_DEMAND
1156 LASSERT (!in_interrupt());
1157 LASSERT (tx->tx_nwrq == 0);
1158 LASSERT (type == IBLND_MSG_GET_DONE ||
1159 type == IBLND_MSG_PUT_DONE);
1161 sge->addr = srcrd->rd_addr;
1162 sge->lkey = srcrd->rd_key;
1165 wrq = &tx->tx_wrq[0];
1167 wrq->next = &tx->tx_wrq[1];
1168 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1171 wrq->opcode = IB_WR_RDMA_WRITE;
1172 wrq->send_flags = 0;
1174 wrq->wr.rdma.remote_addr = dstrd->rd_addr;
1175 wrq->wr.rdma.rkey = dstrd->rd_key;
1179 /* CAVEAT EMPTOR: this 'consumes' the frags in 'dstrd' */
1181 kib_rdma_frag_t *srcfrag;
1183 kib_rdma_frag_t *dstfrag;
1187 LASSERT (!in_interrupt());
1188 LASSERT (tx->tx_nwrq == 0);
1189 LASSERT (type == IBLND_MSG_GET_DONE ||
1190 type == IBLND_MSG_PUT_DONE);
1192 srcidx = dstidx = 0;
1193 srcfrag = &srcrd->rd_frags[0];
1194 dstfrag = &dstrd->rd_frags[0];
1197 if (srcidx >= srcrd->rd_nfrags) {
1198 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1203 if (dstidx == dstrd->rd_nfrags) {
1204 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1209 if (tx->tx_nwrq == IBLND_MAX_RDMA_FRAGS) {
1210 CERROR("RDMA too fragmented: %d/%d src %d/%d dst frags\n",
1211 srcidx, srcrd->rd_nfrags,
1212 dstidx, dstrd->rd_nfrags);
1217 wrknob = MIN(MIN(srcfrag->rf_nob, dstfrag->rf_nob), resid);
1219 sge = &tx->tx_sge[tx->tx_nwrq];
1220 sge->addr = srcfrag->rf_addr;
1221 sge->length = wrknob;
1222 sge->lkey = srcrd->rd_key;
1224 wrq = &tx->tx_wrq[tx->tx_nwrq];
1226 wrq->next = wrq + 1;
1227 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1230 wrq->opcode = IB_WR_RDMA_WRITE;
1231 wrq->send_flags = 0;
1233 wrq->wr.rdma.remote_addr = dstfrag->rf_addr;
1234 wrq->wr.rdma.rkey = dstrd->rd_key;
1240 if (wrknob < srcfrag->rf_nob) {
1241 srcfrag->rf_nob -= wrknob;
1242 srcfrag->rf_addr += wrknob;
1248 if (wrknob < dstfrag->rf_nob) {
1249 dstfrag->rf_nob -= wrknob;
1250 dstfrag->rf_addr += wrknob;
1259 if (rc < 0) /* no RDMA if completing with failure */
1262 ibmsg->ibm_u.completion.ibcm_status = rc;
1263 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1264 kiblnd_init_tx_msg(ni, tx, type, sizeof (kib_completion_msg_t));
1270 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1272 struct list_head *q;
1274 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1275 LASSERT (!tx->tx_queued); /* not queued for sending already */
1278 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * HZ);
1280 if (tx->tx_conn == NULL) {
1281 kiblnd_conn_addref(conn);
1283 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1285 /* PUT_DONE first attached to conn as a PUT_REQ */
1286 LASSERT (tx->tx_conn == conn);
1287 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1290 switch (tx->tx_msg->ibm_type) {
1294 case IBLND_MSG_PUT_REQ:
1295 case IBLND_MSG_GET_REQ:
1296 q = &conn->ibc_tx_queue_rsrvd;
1299 case IBLND_MSG_PUT_NAK:
1300 case IBLND_MSG_PUT_ACK:
1301 case IBLND_MSG_PUT_DONE:
1302 case IBLND_MSG_GET_DONE:
1303 q = &conn->ibc_tx_queue_nocred;
1306 case IBLND_MSG_NOOP:
1307 case IBLND_MSG_IMMEDIATE:
1308 q = &conn->ibc_tx_queue;
1312 list_add_tail(&tx->tx_list, q);
1316 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1318 spin_lock(&conn->ibc_lock);
1319 kiblnd_queue_tx_locked(tx, conn);
1320 spin_unlock(&conn->ibc_lock);
1322 kiblnd_check_sends(conn);
1326 kiblnd_connect_peer (kib_peer_t *peer)
1328 struct rdma_cm_id *cmid;
1329 kib_net_t *net = peer->ibp_ni->ni_data;
1330 struct sockaddr_in srcaddr;
1331 struct sockaddr_in dstaddr;
1334 LASSERT (net != NULL);
1335 LASSERT (peer->ibp_connecting > 0);
1337 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1339 CERROR("Can't create CMID for %s: %ld\n",
1340 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1345 memset(&srcaddr, 0, sizeof(srcaddr));
1346 srcaddr.sin_family = AF_INET;
1347 srcaddr.sin_addr.s_addr = htonl(net->ibn_dev->ibd_ifip);
1349 memset(&dstaddr, 0, sizeof(dstaddr));
1350 dstaddr.sin_family = AF_INET;
1351 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1352 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1354 kiblnd_peer_addref(peer); /* cmid's ref */
1356 rc = rdma_resolve_addr(cmid,
1357 (struct sockaddr *)&srcaddr,
1358 (struct sockaddr *)&dstaddr,
1359 *kiblnd_tunables.kib_timeout * 1000);
1363 /* Can't initiate address resolution: */
1364 CERROR("Can't resolve addr for %s: %d\n",
1365 libcfs_nid2str(peer->ibp_nid), rc);
1367 kiblnd_peer_decref(peer); /* cmid's ref */
1368 rdma_destroy_id(cmid);
1370 kiblnd_peer_connect_failed(peer, 1, rc);
1374 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1379 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1380 unsigned long flags;
1383 /* If I get here, I've committed to send, so I complete the tx with
1384 * failure on any problems */
1386 LASSERT (tx->tx_conn == NULL); /* only set when assigned a conn */
1387 LASSERT (tx->tx_nwrq > 0); /* work items have been set up */
1389 /* First time, just use a read lock since I expect to find my peer
1391 read_lock_irqsave(g_lock, flags);
1393 peer = kiblnd_find_peer_locked(nid);
1394 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1395 /* Found a peer with an established connection */
1396 conn = kiblnd_get_conn_locked(peer);
1397 kiblnd_conn_addref(conn); /* 1 ref for me... */
1399 read_unlock_irqrestore(g_lock, flags);
1401 kiblnd_queue_tx(tx, conn);
1402 kiblnd_conn_decref(conn); /* ...to here */
1406 read_unlock(g_lock);
1407 /* Re-try with a write lock */
1410 peer = kiblnd_find_peer_locked(nid);
1412 if (list_empty(&peer->ibp_conns)) {
1413 /* found a peer, but it's still connecting... */
1414 LASSERT (peer->ibp_connecting != 0 ||
1415 peer->ibp_accepting != 0);
1416 list_add_tail (&tx->tx_list, &peer->ibp_tx_queue);
1417 write_unlock_irqrestore(g_lock, flags);
1419 conn = kiblnd_get_conn_locked(peer);
1420 kiblnd_conn_addref(conn); /* 1 ref for me... */
1422 write_unlock_irqrestore(g_lock, flags);
1424 kiblnd_queue_tx(tx, conn);
1425 kiblnd_conn_decref(conn); /* ...to here */
1430 write_unlock_irqrestore(g_lock, flags);
1432 /* Allocate a peer ready to add to the peer table and retry */
1433 rc = kiblnd_create_peer(ni, &peer, nid);
1435 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1436 tx->tx_status = -EHOSTUNREACH;
1438 kiblnd_tx_done(ni, tx);
1442 write_lock_irqsave(g_lock, flags);
1444 peer2 = kiblnd_find_peer_locked(nid);
1445 if (peer2 != NULL) {
1446 if (list_empty(&peer2->ibp_conns)) {
1447 /* found a peer, but it's still connecting... */
1448 LASSERT (peer2->ibp_connecting != 0 ||
1449 peer2->ibp_accepting != 0);
1450 list_add_tail (&tx->tx_list, &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);
1458 kiblnd_queue_tx(tx, conn);
1459 kiblnd_conn_decref(conn); /* ...to here */
1462 kiblnd_peer_decref(peer);
1466 /* Brand new peer */
1467 LASSERT (peer->ibp_connecting == 0);
1468 peer->ibp_connecting = 1;
1470 /* always called with a ref on ni, which prevents ni being shutdown */
1471 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1473 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1475 kiblnd_peer_addref(peer);
1476 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1478 write_unlock_irqrestore(g_lock, flags);
1480 kiblnd_connect_peer(peer);
1481 kiblnd_peer_decref(peer);
1485 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1487 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1488 int type = lntmsg->msg_type;
1489 lnet_process_id_t target = lntmsg->msg_target;
1490 int target_is_router = lntmsg->msg_target_is_router;
1491 int routing = lntmsg->msg_routing;
1492 unsigned int payload_niov = lntmsg->msg_niov;
1493 struct iovec *payload_iov = lntmsg->msg_iov;
1494 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1495 unsigned int payload_offset = lntmsg->msg_offset;
1496 unsigned int payload_nob = lntmsg->msg_len;
1502 /* NB 'private' is different depending on what we're sending.... */
1504 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1505 payload_nob, payload_niov, libcfs_id2str(target));
1507 LASSERT (payload_nob == 0 || payload_niov > 0);
1508 LASSERT (payload_niov <= LNET_MAX_IOV);
1510 /* Thread context */
1511 LASSERT (!in_interrupt());
1512 /* payload is either all vaddrs or all pages */
1513 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1521 LASSERT (payload_nob == 0);
1525 if (routing || target_is_router)
1526 break; /* send IMMEDIATE */
1528 /* is the REPLY message too small for RDMA? */
1529 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1530 if (nob <= IBLND_MSG_SIZE)
1531 break; /* send IMMEDIATE */
1533 tx = kiblnd_get_idle_tx(ni);
1535 CERROR("Can allocate txd for GET to %s: \n",
1536 libcfs_nid2str(target.nid));
1541 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1542 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1544 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1545 rc = kiblnd_setup_rd_iov(ni, tx,
1546 &ibmsg->ibm_u.get.ibgm_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,
1552 &ibmsg->ibm_u.get.ibgm_rd,
1553 lntmsg->msg_md->md_niov,
1554 lntmsg->msg_md->md_iov.kiov,
1555 0, lntmsg->msg_md->md_length);
1557 CERROR("Can't setup GET sink for %s: %d\n",
1558 libcfs_nid2str(target.nid), rc);
1559 kiblnd_tx_done(ni, tx);
1562 #if IBLND_MAP_ON_DEMAND
1563 nob = sizeof(kib_get_msg_t);
1565 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1567 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1569 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1570 if (tx->tx_lntmsg[1] == NULL) {
1571 CERROR("Can't create reply for GET -> %s\n",
1572 libcfs_nid2str(target.nid));
1573 kiblnd_tx_done(ni, tx);
1577 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1578 tx->tx_waiting = 1; /* waiting for GET_DONE */
1579 kiblnd_launch_tx(ni, tx, target.nid);
1582 case LNET_MSG_REPLY:
1584 /* Is the payload small enough not to need RDMA? */
1585 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1586 if (nob <= IBLND_MSG_SIZE)
1587 break; /* send IMMEDIATE */
1589 tx = kiblnd_get_idle_tx(ni);
1591 CERROR("Can't allocate %s txd for %s\n",
1592 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1593 libcfs_nid2str(target.nid));
1597 if (payload_kiov == NULL)
1598 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1599 payload_niov, payload_iov,
1600 payload_offset, payload_nob);
1602 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1603 payload_niov, payload_kiov,
1604 payload_offset, payload_nob);
1606 CERROR("Can't setup PUT src for %s: %d\n",
1607 libcfs_nid2str(target.nid), rc);
1608 kiblnd_tx_done(ni, tx);
1613 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1614 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1615 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1617 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1618 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1619 kiblnd_launch_tx(ni, tx, target.nid);
1623 /* send IMMEDIATE */
1625 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1628 tx = kiblnd_get_idle_tx(ni);
1630 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1631 type, libcfs_nid2str(target.nid));
1636 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1638 if (payload_kiov != NULL)
1639 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1640 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1641 payload_niov, payload_kiov,
1642 payload_offset, payload_nob);
1644 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1645 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1646 payload_niov, payload_iov,
1647 payload_offset, payload_nob);
1649 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1650 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1652 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1653 kiblnd_launch_tx(ni, tx, target.nid);
1658 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1660 lnet_process_id_t target = lntmsg->msg_target;
1661 unsigned int niov = lntmsg->msg_niov;
1662 struct iovec *iov = lntmsg->msg_iov;
1663 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1664 unsigned int offset = lntmsg->msg_offset;
1665 unsigned int nob = lntmsg->msg_len;
1669 tx = kiblnd_get_idle_tx(ni);
1671 CERROR("Can't get tx for REPLY to %s\n",
1672 libcfs_nid2str(target.nid));
1678 else if (kiov == NULL)
1679 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1680 niov, iov, offset, nob);
1682 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1683 niov, kiov, offset, nob);
1686 CERROR("Can't setup GET src for %s: %d\n",
1687 libcfs_nid2str(target.nid), rc);
1691 rc = kiblnd_init_rdma(ni, tx, 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 iovec *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;
1729 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1732 LASSERT (mlen <= rlen);
1733 LASSERT (!in_interrupt());
1734 /* Either all pages or all vaddrs */
1735 LASSERT (!(kiov != NULL && iov != NULL));
1737 switch (rxmsg->ibm_type) {
1741 case IBLND_MSG_IMMEDIATE:
1742 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1743 if (nob > rx->rx_nob) {
1744 CERROR ("Immediate message from %s too big: %d(%d)\n",
1745 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1752 lnet_copy_flat2kiov(niov, kiov, offset,
1753 IBLND_MSG_SIZE, rxmsg,
1754 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1757 lnet_copy_flat2iov(niov, iov, offset,
1758 IBLND_MSG_SIZE, rxmsg,
1759 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1761 lnet_finalize (ni, lntmsg, 0);
1764 case IBLND_MSG_PUT_REQ:
1766 lnet_finalize(ni, lntmsg, 0);
1767 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1768 rxmsg->ibm_u.putreq.ibprm_cookie);
1772 tx = kiblnd_get_idle_tx(ni);
1774 CERROR("Can't allocate tx for %s\n",
1775 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1776 /* Not replying will break the connection */
1783 rc = kiblnd_setup_rd_iov(ni, tx,
1784 &txmsg->ibm_u.putack.ibpam_rd,
1785 niov, iov, offset, mlen);
1787 rc = kiblnd_setup_rd_kiov(ni, tx,
1788 &txmsg->ibm_u.putack.ibpam_rd,
1789 niov, kiov, offset, mlen);
1791 CERROR("Can't setup PUT sink for %s: %d\n",
1792 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1793 kiblnd_tx_done(ni, tx);
1794 /* tell peer it's over */
1795 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1796 rxmsg->ibm_u.putreq.ibprm_cookie);
1800 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1801 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1802 #if IBLND_MAP_ON_DEMAND
1803 nob = sizeof(kib_putack_msg_t);
1805 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1807 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1809 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1810 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1811 kiblnd_queue_tx(tx, conn);
1813 /* reposted buffer reserved for PUT_DONE */
1814 post_credit = IBLND_POSTRX_NO_CREDIT;
1817 case IBLND_MSG_GET_REQ:
1818 if (lntmsg != NULL) {
1819 /* Optimized GET; RDMA lntmsg's payload */
1820 kiblnd_reply(ni, rx, lntmsg);
1822 /* GET didn't match anything */
1823 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1825 rxmsg->ibm_u.get.ibgm_cookie);
1830 kiblnd_post_rx(rx, post_credit);
1835 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1837 long pid = kernel_thread (fn, arg, 0);
1842 atomic_inc (&kiblnd_data.kib_nthreads);
1847 kiblnd_thread_fini (void)
1849 atomic_dec (&kiblnd_data.kib_nthreads);
1853 kiblnd_peer_alive (kib_peer_t *peer)
1855 /* This is racy, but everyone's only writing cfs_time_current() */
1856 peer->ibp_last_alive = cfs_time_current();
1861 kiblnd_peer_notify (kib_peer_t *peer)
1863 time_t last_alive = 0;
1865 unsigned long flags;
1867 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1869 if (list_empty(&peer->ibp_conns) &&
1870 peer->ibp_accepting == 0 &&
1871 peer->ibp_connecting == 0 &&
1872 peer->ibp_error != 0) {
1873 error = peer->ibp_error;
1874 peer->ibp_error = 0;
1876 last_alive = cfs_time_current_sec() -
1877 cfs_duration_sec(cfs_time_current() -
1878 peer->ibp_last_alive);
1881 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1884 lnet_notify(peer->ibp_ni,
1885 peer->ibp_nid, 0, last_alive);
1889 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1891 /* This just does the immediate housekeeping. 'error' is zero for a
1892 * normal shutdown which can happen only after the connection has been
1893 * established. If the connection is established, schedule the
1894 * connection to be finished off by the connd. Otherwise the connd is
1895 * already dealing with it (either to set it up or tear it down).
1896 * Caller holds kib_global_lock exclusively in irq context */
1897 unsigned long flags;
1898 kib_peer_t *peer = conn->ibc_peer;
1900 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1902 if (error != 0 && conn->ibc_comms_error == 0)
1903 conn->ibc_comms_error = error;
1905 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1906 return; /* already being handled */
1909 list_empty(&conn->ibc_tx_queue) &&
1910 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1911 list_empty(&conn->ibc_tx_queue_nocred) &&
1912 list_empty(&conn->ibc_active_txs)) {
1913 CDEBUG(D_NET, "closing conn to %s\n",
1914 libcfs_nid2str(peer->ibp_nid));
1916 CDEBUG(D_NETERROR, "Closing conn to %s: error %d%s%s%s%s\n",
1917 libcfs_nid2str(peer->ibp_nid), error,
1918 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1919 list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1920 list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1921 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
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);
1937 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1939 list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1940 wake_up (&kiblnd_data.kib_connd_waitq);
1942 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1946 kiblnd_close_conn (kib_conn_t *conn, int error)
1948 unsigned long flags;
1950 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1952 kiblnd_close_conn_locked(conn, error);
1954 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1958 kiblnd_handle_early_rxs(kib_conn_t *conn)
1960 unsigned long flags;
1963 LASSERT (!in_interrupt());
1964 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1966 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1967 while (!list_empty(&conn->ibc_early_rxs)) {
1968 rx = list_entry(conn->ibc_early_rxs.next,
1970 list_del(&rx->rx_list);
1971 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1973 kiblnd_handle_rx(rx);
1975 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1977 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1981 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1983 LIST_HEAD (zombies);
1984 struct list_head *tmp;
1985 struct list_head *nxt;
1988 spin_lock(&conn->ibc_lock);
1990 list_for_each_safe (tmp, nxt, txs) {
1991 tx = list_entry (tmp, kib_tx_t, tx_list);
1993 if (txs == &conn->ibc_active_txs) {
1994 LASSERT (!tx->tx_queued);
1995 LASSERT (tx->tx_waiting ||
1996 tx->tx_sending != 0);
1998 LASSERT (tx->tx_queued);
2001 tx->tx_status = -ECONNABORTED;
2005 if (tx->tx_sending == 0) {
2006 list_del (&tx->tx_list);
2007 list_add (&tx->tx_list, &zombies);
2011 spin_unlock(&conn->ibc_lock);
2013 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
2014 &zombies, -ECONNABORTED);
2018 kiblnd_finalise_conn (kib_conn_t *conn)
2020 LASSERT (!in_interrupt());
2021 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2023 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2025 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2026 * for connections that didn't get as far as being connected, because
2027 * rdma_disconnect() does this for free. */
2028 kiblnd_abort_receives(conn);
2030 /* Complete all tx descs not waiting for sends to complete.
2031 * NB we should be safe from RDMA now that the QP has changed state */
2033 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2034 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2035 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2036 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2038 kiblnd_handle_early_rxs(conn);
2042 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2044 LIST_HEAD (zombies);
2045 unsigned long flags;
2047 LASSERT (error != 0);
2048 LASSERT (!in_interrupt());
2050 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2053 LASSERT (peer->ibp_connecting > 0);
2054 peer->ibp_connecting--;
2056 LASSERT (peer->ibp_accepting > 0);
2057 peer->ibp_accepting--;
2060 if (peer->ibp_connecting != 0 ||
2061 peer->ibp_accepting != 0) {
2062 /* another connection attempt under way... */
2063 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2067 if (list_empty(&peer->ibp_conns)) {
2068 /* Take peer's blocked transmits to complete with error */
2069 list_add(&zombies, &peer->ibp_tx_queue);
2070 list_del_init(&peer->ibp_tx_queue);
2072 if (kiblnd_peer_active(peer))
2073 kiblnd_unlink_peer_locked(peer);
2075 peer->ibp_error = error;
2077 /* Can't have blocked transmits if there are connections */
2078 LASSERT (list_empty(&peer->ibp_tx_queue));
2081 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2083 kiblnd_peer_notify(peer);
2085 if (list_empty (&zombies))
2088 CDEBUG (D_NETERROR, "Deleting messages for %s: connection failed\n",
2089 libcfs_nid2str(peer->ibp_nid));
2091 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2095 kiblnd_connreq_done(kib_conn_t *conn, int status)
2097 struct list_head txs;
2099 kib_peer_t *peer = conn->ibc_peer;
2101 unsigned long flags;
2104 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2106 CDEBUG(D_NET,"%s: %d, %d\n", libcfs_nid2str(peer->ibp_nid),
2109 LASSERT (!in_interrupt());
2110 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2111 peer->ibp_connecting > 0) ||
2112 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2113 peer->ibp_accepting > 0));
2115 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2116 conn->ibc_connvars = NULL;
2119 /* failed to establish connection */
2120 kiblnd_peer_connect_failed(conn->ibc_peer, active, status);
2121 kiblnd_finalise_conn(conn);
2125 /* connection established */
2126 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2128 conn->ibc_last_send = jiffies;
2129 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2130 kiblnd_peer_alive(peer);
2132 /* Add conn to peer's list and nuke any dangling conns from a different
2133 * peer instance... */
2134 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2135 list_add(&conn->ibc_list, &peer->ibp_conns);
2137 peer->ibp_connecting--;
2139 peer->ibp_accepting--;
2141 kiblnd_close_stale_conns_locked(conn->ibc_peer,
2142 conn->ibc_incarnation);
2144 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2145 conn->ibc_comms_error != 0) { /* error has happened already */
2147 /* start to shut down connection */
2148 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2149 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2153 /* grab pending txs while I have the lock */
2154 list_add(&txs, &peer->ibp_tx_queue);
2155 list_del_init(&peer->ibp_tx_queue);
2157 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2159 /* Schedule blocked txs */
2160 spin_lock (&conn->ibc_lock);
2161 while (!list_empty (&txs)) {
2162 tx = list_entry (txs.next, kib_tx_t, tx_list);
2163 list_del (&tx->tx_list);
2165 kiblnd_queue_tx_locked(tx, conn);
2167 spin_unlock (&conn->ibc_lock);
2169 kiblnd_check_sends(conn);
2171 /* schedule blocked rxs */
2172 kiblnd_handle_early_rxs(conn);
2176 kiblnd_reject(struct rdma_cm_id *cmid, int why)
2179 kib_rej_t rej = {.ibr_magic = IBLND_MSG_MAGIC,
2180 .ibr_version = IBLND_MSG_VERSION,
2183 rc = rdma_reject(cmid, &rej, sizeof(rej));
2186 CWARN("Error %d sending reject\n", rc);
2190 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2193 kib_msg_t *reqmsg = priv;
2194 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2195 struct rdma_conn_param cp;
2196 unsigned long flags;
2197 lnet_ni_t *ni = NULL;
2204 int rej = IBLND_REJECT_FATAL;
2206 LASSERT (!in_interrupt());
2208 /* cmid inherits 'context' from the corresponding listener id */
2209 ibdev = (kib_dev_t *)cmid->context;
2210 LASSERT (ibdev != NULL);
2212 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2213 CERROR("Short connection request\n");
2217 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2218 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC) ||
2219 (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2220 reqmsg->ibm_version != IBLND_MSG_VERSION) ||
2221 (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2222 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION))) {
2223 /* Future protocol version compatibility support! If the
2224 * o2iblnd-specific protocol changes, or when LNET unifies
2225 * protocols over all LNDs, the initial connection will
2226 * negotiate a protocol version. I trap this here to avoid
2227 * console errors; the reject tells the peer which protocol I
2232 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2234 CERROR("Can't parse connection request: %d\n", rc);
2238 nid = reqmsg->ibm_srcnid;
2240 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2241 CERROR("Unexpected connreq msg type: %x from %s\n",
2242 reqmsg->ibm_type, libcfs_nid2str(nid));
2246 if (reqmsg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2247 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2248 libcfs_nid2str(nid),
2249 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2250 IBLND_MSG_QUEUE_SIZE);
2254 if (reqmsg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2255 CERROR("Can't accept %s: incompatible max_frags %d (%d wanted)\n",
2256 libcfs_nid2str(nid),
2257 reqmsg->ibm_u.connparams.ibcp_max_frags,
2258 IBLND_MAX_RDMA_FRAGS);
2262 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2263 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2264 libcfs_nid2str(nid),
2265 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2270 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2271 if (ni == NULL || /* no matching net */
2272 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2273 ((kib_net_t*)ni->ni_data)->ibn_dev != ibdev) { /* wrong device */
2274 CERROR("Can't accept %s: bad dst nid %s\n",
2275 libcfs_nid2str(nid),
2276 libcfs_nid2str(reqmsg->ibm_dstnid));
2281 /* assume 'nid' is a new peer; create */
2282 rc = kiblnd_create_peer(ni, &peer, nid);
2284 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2285 rej = IBLND_REJECT_NO_RESOURCES;
2289 write_lock_irqsave(g_lock, flags);
2291 peer2 = kiblnd_find_peer_locked(nid);
2292 if (peer2 != NULL) {
2293 /* tie-break connection race in favour of the higher NID */
2294 if (peer2->ibp_connecting != 0 &&
2296 write_unlock_irqrestore(g_lock, flags);
2298 CWARN("Conn race %s\n",
2299 libcfs_nid2str(peer2->ibp_nid));
2301 kiblnd_peer_decref(peer);
2302 rej = IBLND_REJECT_CONN_RACE;
2306 peer2->ibp_accepting++;
2307 kiblnd_peer_addref(peer2);
2309 write_unlock_irqrestore(g_lock, flags);
2310 kiblnd_peer_decref(peer);
2313 /* Brand new peer */
2314 LASSERT (peer->ibp_accepting == 0);
2315 peer->ibp_accepting = 1;
2317 /* I have a ref on ni that prevents it being shutdown */
2318 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
2320 kiblnd_peer_addref(peer);
2321 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2323 write_unlock_irqrestore(g_lock, flags);
2326 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT);
2328 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2329 kiblnd_peer_decref(peer);
2330 rej = IBLND_REJECT_NO_RESOURCES;
2334 /* conn now "owns" cmid, so I return success from here on to ensure the
2335 * CM callback doesn't destroy cmid. */
2337 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2338 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2339 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2340 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2343 ackmsg = &conn->ibc_connvars->cv_msg;
2344 memset(ackmsg, 0, sizeof(*ackmsg));
2346 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2347 sizeof(ackmsg->ibm_u.connparams));
2348 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2349 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2350 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2351 kiblnd_pack_msg(ni, ackmsg, 0, nid, reqmsg->ibm_srcstamp);
2353 memset(&cp, 0, sizeof(cp));
2354 cp.private_data = ackmsg;
2355 cp.private_data_len = ackmsg->ibm_nob;
2356 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2357 cp.initiator_depth = 0;
2358 cp.flow_control = 1;
2359 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2360 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2362 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2364 rc = rdma_accept(cmid, &cp);
2366 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2367 kiblnd_reject(cmid, IBLND_REJECT_FATAL);
2368 kiblnd_connreq_done(conn, rc);
2369 kiblnd_conn_decref(conn);
2379 kiblnd_reject(cmid, rej);
2380 return -ECONNREFUSED;
2384 kiblnd_reconnect (kib_conn_t *conn, char *why)
2386 kib_peer_t *peer = conn->ibc_peer;
2388 unsigned long flags;
2390 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2391 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2393 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2395 /* retry connection if it's still needed and no other connection
2396 * attempts (active or passive) are in progress */
2397 if (!list_empty(&peer->ibp_tx_queue) &&
2398 peer->ibp_connecting == 1 &&
2399 peer->ibp_accepting == 0) {
2401 peer->ibp_connecting++;
2404 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2407 CDEBUG(D_NETERROR, "%s: retrying (%s)\n",
2408 libcfs_nid2str(peer->ibp_nid), why);
2409 kiblnd_connect_peer(peer);
2414 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2416 kib_peer_t *peer = conn->ibc_peer;
2418 LASSERT (!in_interrupt());
2419 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2422 case IB_CM_REJ_STALE_CONN:
2423 kiblnd_reconnect(conn, "stale");
2426 case IB_CM_REJ_CONSUMER_DEFINED:
2427 if (priv_nob >= sizeof(kib_rej_t)) {
2428 kib_rej_t *rej = priv;
2430 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2431 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2432 __swab32s(&rej->ibr_magic);
2433 __swab16s(&rej->ibr_version);
2436 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2437 rej->ibr_magic != LNET_PROTO_MAGIC) {
2438 CERROR("%s rejected: consumer defined fatal error\n",
2439 libcfs_nid2str(peer->ibp_nid));
2443 if (rej->ibr_version != IBLND_MSG_VERSION) {
2444 CERROR("%s rejected: o2iblnd version %d error\n",
2445 libcfs_nid2str(peer->ibp_nid),
2450 switch (rej->ibr_why) {
2451 case IBLND_REJECT_CONN_RACE:
2452 kiblnd_reconnect(conn, "conn race");
2455 case IBLND_REJECT_NO_RESOURCES:
2456 CERROR("%s rejected: o2iblnd no resources\n",
2457 libcfs_nid2str(peer->ibp_nid));
2459 case IBLND_REJECT_FATAL:
2460 CERROR("%s rejected: o2iblnd fatal error\n",
2461 libcfs_nid2str(peer->ibp_nid));
2464 CERROR("%s rejected: o2iblnd reason %d\n",
2465 libcfs_nid2str(peer->ibp_nid),
2473 CDEBUG(D_NETERROR, "%s rejected: reason %d, size %d\n",
2474 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2478 kiblnd_connreq_done(conn, -ECONNREFUSED);
2482 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2484 kib_peer_t *peer = conn->ibc_peer;
2485 lnet_ni_t *ni = peer->ibp_ni;
2486 kib_net_t *net = ni->ni_data;
2487 kib_msg_t *msg = priv;
2488 int rc = kiblnd_unpack_msg(msg, priv_nob);
2489 unsigned long flags;
2491 LASSERT (net != NULL);
2494 CERROR("Can't unpack connack from %s: %d\n",
2495 libcfs_nid2str(peer->ibp_nid), rc);
2499 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2500 CERROR("Unexpected message %d from %s\n",
2501 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2506 if (msg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2507 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2508 libcfs_nid2str(peer->ibp_nid),
2509 msg->ibm_u.connparams.ibcp_queue_depth,
2510 IBLND_MSG_QUEUE_SIZE);
2515 if (msg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2516 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2517 libcfs_nid2str(peer->ibp_nid),
2518 msg->ibm_u.connparams.ibcp_max_frags,
2519 IBLND_MAX_RDMA_FRAGS);
2524 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2525 CERROR("%s max message size %d too big (%d max)\n",
2526 libcfs_nid2str(peer->ibp_nid),
2527 msg->ibm_u.connparams.ibcp_max_msg_size,
2533 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2534 if (msg->ibm_dstnid == ni->ni_nid &&
2535 msg->ibm_dststamp == net->ibn_incarnation)
2539 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2542 CERROR("Stale connection reply from %s\n",
2543 libcfs_nid2str(peer->ibp_nid));
2547 conn->ibc_incarnation = msg->ibm_srcstamp;
2548 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2549 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2550 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2553 kiblnd_connreq_done(conn, 0);
2557 /* NB My QP has already established itself, so I handle anything going
2558 * wrong here by setting ibc_comms_error.
2559 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2560 * immediately tears it down. */
2563 conn->ibc_comms_error = rc;
2564 kiblnd_connreq_done(conn, 0);
2568 kiblnd_active_connect (struct rdma_cm_id *cmid)
2570 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2573 struct rdma_conn_param cp;
2576 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT);
2578 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2579 kiblnd_peer_decref(peer); /* lose cmid's ref */
2583 /* conn "owns" cmid now, so I return success from here on to ensure the
2584 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2587 msg = &conn->ibc_connvars->cv_msg;
2589 memset(msg, 0, sizeof(*msg));
2590 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2591 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2592 msg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2593 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2594 kiblnd_pack_msg(peer->ibp_ni, msg, 0, peer->ibp_nid, 0);
2596 memset(&cp, 0, sizeof(cp));
2597 cp.private_data = msg;
2598 cp.private_data_len = msg->ibm_nob;
2599 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2600 cp.initiator_depth = 0;
2601 cp.flow_control = 1;
2602 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2603 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2605 LASSERT(cmid->context == (void *)conn);
2606 LASSERT(conn->ibc_cmid == cmid);
2608 rc = rdma_connect(cmid, &cp);
2610 CERROR("Can't connect to %s: %d\n",
2611 libcfs_nid2str(peer->ibp_nid), rc);
2612 kiblnd_connreq_done(conn, rc);
2613 kiblnd_conn_decref(conn);
2620 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2626 switch (event->event) {
2630 case RDMA_CM_EVENT_CONNECT_REQUEST:
2631 /* destroy cmid on failure */
2632 rc = kiblnd_passive_connect(cmid,
2633 (void *)KIBLND_CONN_PARAM(event),
2634 KIBLND_CONN_PARAM_LEN(event));
2635 CDEBUG(D_NET, "connreq: %d\n", rc);
2638 case RDMA_CM_EVENT_ADDR_ERROR:
2639 peer = (kib_peer_t *)cmid->context;
2640 CDEBUG(D_NETERROR, "%s: ADDR ERROR %d\n",
2641 libcfs_nid2str(peer->ibp_nid), event->status);
2642 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2643 kiblnd_peer_decref(peer);
2644 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2646 case RDMA_CM_EVENT_ADDR_RESOLVED:
2647 peer = (kib_peer_t *)cmid->context;
2649 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2650 libcfs_nid2str(peer->ibp_nid), event->status);
2652 if (event->status != 0) {
2653 CDEBUG(D_NETERROR, "Can't resolve address for %s: %d\n",
2654 libcfs_nid2str(peer->ibp_nid), event->status);
2657 rc = rdma_resolve_route(
2658 cmid, *kiblnd_tunables.kib_timeout * 1000);
2661 /* Can't initiate route resolution */
2662 CERROR("Can't resolve route for %s: %d\n",
2663 libcfs_nid2str(peer->ibp_nid), rc);
2665 kiblnd_peer_connect_failed(peer, 1, rc);
2666 kiblnd_peer_decref(peer);
2667 return rc; /* rc != 0 destroys cmid */
2669 case RDMA_CM_EVENT_ROUTE_ERROR:
2670 peer = (kib_peer_t *)cmid->context;
2671 CDEBUG(D_NETERROR, "%s: ROUTE ERROR %d\n",
2672 libcfs_nid2str(peer->ibp_nid), event->status);
2673 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2674 kiblnd_peer_decref(peer);
2675 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2677 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2678 peer = (kib_peer_t *)cmid->context;
2679 CDEBUG(D_NET,"%s Route resolved: %d\n",
2680 libcfs_nid2str(peer->ibp_nid), event->status);
2682 if (event->status == 0)
2683 return kiblnd_active_connect(cmid);
2685 CDEBUG(D_NETERROR, "Can't resolve route for %s: %d\n",
2686 libcfs_nid2str(peer->ibp_nid), event->status);
2687 kiblnd_peer_connect_failed(peer, 1, event->status);
2688 kiblnd_peer_decref(peer);
2689 return event->status; /* rc != 0 destroys cmid */
2691 case RDMA_CM_EVENT_UNREACHABLE:
2692 conn = (kib_conn_t *)cmid->context;
2693 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2694 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2695 CDEBUG(D_NETERROR, "%s: UNREACHABLE %d\n",
2696 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2697 kiblnd_connreq_done(conn, -ENETDOWN);
2698 kiblnd_conn_decref(conn);
2701 case RDMA_CM_EVENT_CONNECT_ERROR:
2702 conn = (kib_conn_t *)cmid->context;
2703 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2704 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2705 CDEBUG(D_NETERROR, "%s: CONNECT ERROR %d\n",
2706 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2707 kiblnd_connreq_done(conn, -ENOTCONN);
2708 kiblnd_conn_decref(conn);
2711 case RDMA_CM_EVENT_REJECTED:
2712 conn = (kib_conn_t *)cmid->context;
2713 switch (conn->ibc_state) {
2717 case IBLND_CONN_PASSIVE_WAIT:
2718 CERROR ("%s: REJECTED %d\n",
2719 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2721 kiblnd_connreq_done(conn, -ECONNRESET);
2724 case IBLND_CONN_ACTIVE_CONNECT:
2725 kiblnd_rejected(conn, event->status,
2726 (void *)KIBLND_CONN_PARAM(event),
2727 KIBLND_CONN_PARAM_LEN(event));
2730 kiblnd_conn_decref(conn);
2733 case RDMA_CM_EVENT_ESTABLISHED:
2734 conn = (kib_conn_t *)cmid->context;
2735 switch (conn->ibc_state) {
2739 case IBLND_CONN_PASSIVE_WAIT:
2740 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2741 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2742 kiblnd_connreq_done(conn, 0);
2745 case IBLND_CONN_ACTIVE_CONNECT:
2746 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2747 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2748 kiblnd_check_connreply(conn,
2749 (void *)KIBLND_CONN_PARAM(event),
2750 KIBLND_CONN_PARAM_LEN(event));
2753 /* net keeps its ref on conn! */
2756 case RDMA_CM_EVENT_DISCONNECTED:
2757 conn = (kib_conn_t *)cmid->context;
2758 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2759 CERROR("%s DISCONNECTED\n",
2760 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2761 kiblnd_connreq_done(conn, -ECONNRESET);
2763 kiblnd_close_conn(conn, 0);
2765 kiblnd_conn_decref(conn);
2768 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2769 LCONSOLE_ERROR_MSG(0x131,
2770 "Received notification of device removal\n"
2771 "Please shutdown LNET to allow this to proceed\n");
2772 /* Can't remove network from underneath LNET for now, so I have
2779 kiblnd_check_txs (kib_conn_t *conn, struct list_head *txs)
2782 struct list_head *ttmp;
2785 spin_lock(&conn->ibc_lock);
2787 list_for_each (ttmp, txs) {
2788 tx = list_entry (ttmp, kib_tx_t, tx_list);
2790 if (txs != &conn->ibc_active_txs) {
2791 LASSERT (tx->tx_queued);
2793 LASSERT (!tx->tx_queued);
2794 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2797 if (time_after_eq (jiffies, tx->tx_deadline)) {
2803 spin_unlock(&conn->ibc_lock);
2808 kiblnd_conn_timed_out (kib_conn_t *conn)
2810 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2811 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2812 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2813 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2817 kiblnd_check_conns (int idx)
2819 struct list_head *peers = &kiblnd_data.kib_peers[idx];
2820 struct list_head *ptmp;
2823 struct list_head *ctmp;
2824 unsigned long flags;
2827 /* NB. We expect to have a look at all the peers and not find any
2828 * rdmas to time out, so we just use a shared lock while we
2830 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2832 list_for_each (ptmp, peers) {
2833 peer = list_entry (ptmp, kib_peer_t, ibp_list);
2835 list_for_each (ctmp, &peer->ibp_conns) {
2836 conn = list_entry (ctmp, kib_conn_t, ibc_list);
2838 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2840 /* In case we have enough credits to return via a
2841 * NOOP, but there were no non-blocking tx descs
2842 * free to do it last time... */
2843 kiblnd_check_sends(conn);
2845 if (!kiblnd_conn_timed_out(conn))
2848 /* Handle timeout by closing the whole connection. We
2849 * can only be sure RDMA activity has ceased once the
2850 * QP has been modified. */
2852 kiblnd_conn_addref(conn); /* 1 ref for me... */
2854 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2857 CERROR("Timed out RDMA with %s\n",
2858 libcfs_nid2str(peer->ibp_nid));
2860 kiblnd_close_conn(conn, -ETIMEDOUT);
2861 kiblnd_conn_decref(conn); /* ...until here */
2863 /* start again now I've dropped the lock */
2868 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2872 kiblnd_disconnect_conn (kib_conn_t *conn)
2874 LASSERT (!in_interrupt());
2875 LASSERT (current == kiblnd_data.kib_connd);
2876 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
2878 rdma_disconnect(conn->ibc_cmid);
2879 kiblnd_finalise_conn(conn);
2881 kiblnd_peer_notify(conn->ibc_peer);
2885 kiblnd_connd (void *arg)
2888 unsigned long flags;
2894 unsigned long deadline = jiffies;
2896 cfs_daemonize ("kiblnd_connd");
2897 cfs_block_allsigs ();
2899 init_waitqueue_entry (&wait, current);
2900 kiblnd_data.kib_connd = current;
2902 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2904 while (!kiblnd_data.kib_shutdown) {
2908 if (!list_empty (&kiblnd_data.kib_connd_zombies)) {
2909 conn = list_entry (kiblnd_data.kib_connd_zombies.next,
2910 kib_conn_t, ibc_list);
2911 list_del (&conn->ibc_list);
2913 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2916 kiblnd_destroy_conn(conn);
2918 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2921 if (!list_empty (&kiblnd_data.kib_connd_conns)) {
2922 conn = list_entry (kiblnd_data.kib_connd_conns.next,
2923 kib_conn_t, ibc_list);
2924 list_del (&conn->ibc_list);
2926 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2929 kiblnd_disconnect_conn(conn);
2930 kiblnd_conn_decref(conn);
2932 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2935 /* careful with the jiffy wrap... */
2936 timeout = (int)(deadline - jiffies);
2940 int chunk = kiblnd_data.kib_peer_hash_size;
2942 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2945 /* Time to check for RDMA timeouts on a few more
2946 * peers: I do checks every 'p' seconds on a
2947 * proportion of the peer table and I need to check
2948 * every connection 'n' times within a timeout
2949 * interval, to ensure I detect a timeout on any
2950 * connection within (n+1)/n times the timeout
2953 if (*kiblnd_tunables.kib_timeout > n * p)
2954 chunk = (chunk * n * p) /
2955 *kiblnd_tunables.kib_timeout;
2959 for (i = 0; i < chunk; i++) {
2960 kiblnd_check_conns(peer_index);
2961 peer_index = (peer_index + 1) %
2962 kiblnd_data.kib_peer_hash_size;
2966 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2972 /* Nothing to do for 'timeout' */
2973 set_current_state (TASK_INTERRUPTIBLE);
2974 add_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2975 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2977 schedule_timeout (timeout);
2979 set_current_state (TASK_RUNNING);
2980 remove_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2981 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2984 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2986 kiblnd_thread_fini();
2991 kiblnd_qp_event(struct ib_event *event, void *arg)
2993 kib_conn_t *conn = arg;
2995 switch (event->event) {
2996 case IB_EVENT_COMM_EST:
2997 CDEBUG(D_NET, "%s established\n",
2998 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3002 CERROR("%s: Async QP event type %d\n",
3003 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3009 kiblnd_complete (struct ib_wc *wc)
3011 switch (kiblnd_wreqid2type(wc->wr_id)) {
3015 case IBLND_WID_RDMA:
3016 /* We only get RDMA completion notification if it fails. All
3017 * subsequent work items, including the final SEND will fail
3018 * too. However we can't print out any more info about the
3019 * failing RDMA because 'tx' might be back on the idle list or
3020 * even reused already if we didn't manage to post all our work
3022 CDEBUG(D_NETERROR, "RDMA (tx: %p) failed: %d\n",
3023 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3027 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3031 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3038 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3040 /* NB I'm not allowed to schedule this conn once its refcount has
3041 * reached 0. Since fundamentally I'm racing with scheduler threads
3042 * consuming my CQ I could be called after all completions have
3043 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3044 * and this CQ is about to be destroyed so I NOOP. */
3045 kib_conn_t *conn = (kib_conn_t *)arg;
3046 unsigned long flags;
3048 LASSERT (cq == conn->ibc_cq);
3050 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3052 conn->ibc_ready = 1;
3054 if (!conn->ibc_scheduled &&
3055 (conn->ibc_nrx > 0 ||
3056 conn->ibc_nsends_posted > 0)) {
3057 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3058 conn->ibc_scheduled = 1;
3059 list_add_tail(&conn->ibc_sched_list,
3060 &kiblnd_data.kib_sched_conns);
3061 wake_up(&kiblnd_data.kib_sched_waitq);
3064 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3068 kiblnd_cq_event(struct ib_event *event, void *arg)
3070 kib_conn_t *conn = arg;
3072 CERROR("%s: async CQ event type %d\n",
3073 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3077 kiblnd_scheduler(void *arg)
3079 long id = (long)arg;
3082 unsigned long flags;
3089 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3090 cfs_daemonize(name);
3091 cfs_block_allsigs();
3093 init_waitqueue_entry(&wait, current);
3095 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3097 while (!kiblnd_data.kib_shutdown) {
3098 if (busy_loops++ >= IBLND_RESCHED) {
3099 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3105 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3110 if (!list_empty(&kiblnd_data.kib_sched_conns)) {
3111 conn = list_entry(kiblnd_data.kib_sched_conns.next,
3112 kib_conn_t, ibc_sched_list);
3113 /* take over kib_sched_conns' ref on conn... */
3114 LASSERT(conn->ibc_scheduled);
3115 list_del(&conn->ibc_sched_list);
3116 conn->ibc_ready = 0;
3118 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3121 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3123 rc = ib_req_notify_cq(conn->ibc_cq,
3127 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3132 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3135 if (rc != 0 || conn->ibc_ready) {
3136 /* There may be another completion waiting; get
3137 * another scheduler to check while I handle
3139 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3140 list_add_tail(&conn->ibc_sched_list,
3141 &kiblnd_data.kib_sched_conns);
3142 wake_up(&kiblnd_data.kib_sched_waitq);
3144 conn->ibc_scheduled = 0;
3148 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3151 kiblnd_complete(&wc);
3153 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3157 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3164 set_current_state(TASK_INTERRUPTIBLE);
3165 add_wait_queue_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3166 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3171 remove_wait_queue(&kiblnd_data.kib_sched_waitq, &wait);
3172 set_current_state(TASK_RUNNING);
3173 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3176 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3178 kiblnd_thread_fini();