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 dma_unmap_sg(net->ibn_dev->ibd_cmid->device->dma_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);
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 !IBNAL_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 = dma_map_sg(net->ibn_dev->ibd_cmid->device->dma_device,
643 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
644 rd->rd_key = (rd != tx->tx_rd) ?
645 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
647 for (i = 0; i < rd->rd_nfrags; i++) {
648 rd->rd_frags[i].rf_nob = sg_dma_len(&tx->tx_frags[i]);
649 rd->rd_frags[i].rf_addr = sg_dma_address(&tx->tx_frags[i]);
656 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
657 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
659 struct scatterlist *sg;
662 kib_net_t *net = ni->ni_data;
664 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
668 LASSERT (net != NULL);
670 while (offset >= kiov->kiov_len) {
671 offset -= kiov->kiov_len;
681 fragnob = min((int)(kiov->kiov_len - offset), nob);
683 memset(sg, 0, sizeof(*sg));
684 sg->page = kiov->kiov_page;
685 sg->offset = kiov->kiov_offset + offset;
686 sg->length = fragnob;
695 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
697 tx->tx_nfrags = sg - tx->tx_frags;
698 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
700 rd->rd_nfrags = dma_map_sg(net->ibn_dev->ibd_cmid->device->dma_device,
701 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
702 rd->rd_key = (rd != tx->tx_rd) ?
703 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
705 for (i = 0; i < tx->tx_nfrags; i++) {
706 rd->rd_frags[i].rf_nob = sg_dma_len(&tx->tx_frags[i]);
707 rd->rd_frags[i].rf_addr = sg_dma_address(&tx->tx_frags[i]);
709 CDEBUG(D_WARNING,"frag[%d]: "LPX64" for %d\n",
710 i, rd->rd_frags[i].rf_addr, rd->rd_frags[i].rf_nob);
718 kiblnd_map_tx (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
719 int npages, unsigned long page_offset, int nob)
721 struct ib_pool_fmr *fmr;
722 kib_net_t *net = ni->ni_data;
724 LASSERT (net != NULL);
725 LASSERT (tx->tx_fmr == NULL);
726 LASSERT (page_offset < PAGE_SIZE);
727 LASSERT (npages >= (1 + ((page_offset + nob - 1)>>PAGE_SHIFT)));
728 LASSERT (npages <= LNET_MAX_IOV);
732 fmr = ib_fmr_pool_map_phys(net->ibn_fmrpool, tx->tx_pages,
733 npages, rd->rd_addr);
735 CERROR ("Can't map %d pages: %ld\n", npages, PTR_ERR(fmr));
739 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
742 rd->rd_key = (rd != tx->tx_rd) ? fmr->fmr->rkey : fmr->fmr->lkey;
750 kiblnd_setup_rd_iov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
751 unsigned int niov, struct iovec *iov, int offset, int nob)
758 unsigned long page_offset;
764 while (offset >= iov->iov_len) {
765 offset -= iov->iov_len;
771 if (nob > iov->iov_len - offset) {
772 CERROR ("Can't map multiple vaddr fragments\n");
776 vaddr = ((unsigned long)iov->iov_base) + offset;
778 page_offset = vaddr & (PAGE_SIZE - 1);
783 LASSERT (npages < LNET_MAX_IOV);
785 page = kiblnd_kvaddr_to_page(vaddr);
787 CERROR("Can't find page for %lu\n", vaddr);
791 tx->tx_pages[npages++] = lnet_page2phys(page);
793 fragnob = PAGE_SIZE - (vaddr & (PAGE_SIZE - 1));
799 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
803 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
804 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
808 unsigned long page_offset;
810 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
814 LASSERT (nkiov <= LNET_MAX_IOV);
816 while (offset >= kiov->kiov_len) {
817 offset -= kiov->kiov_len;
823 page_offset = kiov->kiov_offset + offset;
825 resid = offset + nob;
829 LASSERT (npages < LNET_MAX_IOV);
832 if ((npages > 0 && kiov->kiov_offset != 0) ||
833 (resid > kiov->kiov_len &&
834 (kiov->kiov_offset + kiov->kiov_len) != PAGE_SIZE)) {
835 /* Can't have gaps */
836 CERROR ("Can't make payload contiguous in I/O VM:"
837 "page %d, offset %d, len %d \n",
838 npages, kiov->kiov_offset, kiov->kiov_len);
843 tx->tx_pages[npages++] = lnet_page2phys(kiov->kiov_page);
844 resid -= kiov->kiov_len;
849 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
854 kiblnd_check_sends (kib_conn_t *conn)
857 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
859 int consume_cred = 0;
860 struct ib_send_wr *bad_wrq;
863 /* Don't send anything until after the connection is established */
864 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
865 CDEBUG(D_NET, "%s too soon\n",
866 libcfs_nid2str(conn->ibc_peer->ibp_nid));
870 spin_lock(&conn->ibc_lock);
872 LASSERT (conn->ibc_nsends_posted <=
873 *kiblnd_tunables.kib_concurrent_sends);
874 LASSERT (conn->ibc_reserved_credits >= 0);
876 while (conn->ibc_reserved_credits > 0 &&
877 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
878 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
880 list_del(&tx->tx_list);
881 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
882 conn->ibc_reserved_credits--;
885 if (list_empty(&conn->ibc_tx_queue) &&
886 list_empty(&conn->ibc_tx_queue_nocred) &&
887 (conn->ibc_outstanding_credits >= IBLND_CREDIT_HIGHWATER ||
888 kiblnd_send_keepalive(conn))) {
889 spin_unlock(&conn->ibc_lock);
891 tx = kiblnd_get_idle_tx(ni);
893 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
895 spin_lock(&conn->ibc_lock);
898 kiblnd_queue_tx_locked(tx, conn);
902 if (!list_empty (&conn->ibc_tx_queue_nocred)) {
903 tx = list_entry (conn->ibc_tx_queue_nocred.next,
906 } else if (!list_empty (&conn->ibc_tx_queue)) {
907 tx = list_entry (conn->ibc_tx_queue.next,
911 /* nothing to send right now */
915 LASSERT (tx->tx_queued);
916 /* We rely on this for QP sizing */
917 LASSERT (tx->tx_nwrq > 0 &&
918 tx->tx_nwrq <= 1 + IBLND_MAX_RDMA_FRAGS);
920 LASSERT (conn->ibc_outstanding_credits >= 0);
921 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE);
922 LASSERT (conn->ibc_credits >= 0);
923 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE);
925 if (conn->ibc_nsends_posted ==
926 *kiblnd_tunables.kib_concurrent_sends) {
927 /* tx completions outstanding... */
928 CDEBUG(D_NET, "%s: posted enough\n",
929 libcfs_nid2str(conn->ibc_peer->ibp_nid));
934 if (conn->ibc_credits == 0) { /* no credits */
935 CDEBUG(D_NET, "%s: no credits\n",
936 libcfs_nid2str(conn->ibc_peer->ibp_nid));
940 if (conn->ibc_credits == 1 && /* last credit reserved for */
941 conn->ibc_outstanding_credits == 0) { /* giving back credits */
942 CDEBUG(D_NET, "%s: not using last credit\n",
943 libcfs_nid2str(conn->ibc_peer->ibp_nid));
948 list_del (&tx->tx_list);
951 /* NB don't drop ibc_lock before bumping tx_sending */
953 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP &&
954 (!list_empty(&conn->ibc_tx_queue) ||
955 !list_empty(&conn->ibc_tx_queue_nocred) ||
956 (conn->ibc_outstanding_credits < IBLND_CREDIT_HIGHWATER &&
957 !kiblnd_send_keepalive(conn)))) {
959 spin_unlock(&conn->ibc_lock);
960 kiblnd_tx_done(ni, tx);
961 spin_lock(&conn->ibc_lock);
962 CDEBUG(D_NET, "%s: redundant noop\n",
963 libcfs_nid2str(conn->ibc_peer->ibp_nid));
967 kiblnd_pack_msg(ni, tx->tx_msg, conn->ibc_outstanding_credits,
968 conn->ibc_peer->ibp_nid, conn->ibc_incarnation);
970 conn->ibc_outstanding_credits = 0;
971 conn->ibc_nsends_posted++;
975 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
976 * PUT. If so, it was first queued here as a PUT_REQ, sent and
977 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
978 * and then re-queued here. It's (just) possible that
979 * tx_sending is non-zero if we've not done the tx_complete() from
980 * the first send; hence the ++ rather than = below. */
983 list_add (&tx->tx_list, &conn->ibc_active_txs);
988 for (i = 0; i < tx->tx_nwrq - 1; i++) {
989 LASSERT (tx->tx_wrq[i].opcode == IB_WR_RDMA_WRITE);
990 LASSERT (tx->tx_wrq[i].next == &tx->tx_wrq[i+1]);
991 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
993 CDEBUG(D_WARNING, "WORK[%d]: RDMA "LPX64
994 " for %d k %x -> "LPX64" k %x\n", i,
995 tx->tx_wrq[i].sg_list->addr,
996 tx->tx_wrq[i].sg_list->length,
997 tx->tx_wrq[i].sg_list->lkey,
998 tx->tx_wrq[i].wr.rdma.remote_addr,
999 tx->tx_wrq[i].wr.rdma.rkey);
1002 LASSERT (tx->tx_wrq[i].opcode == IB_WR_SEND);
1003 LASSERT (tx->tx_wrq[i].next == NULL);
1004 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
1006 CDEBUG(D_WARNING, "WORK[%d]: SEND "LPX64" for %d k %x\n", i,
1007 tx->tx_wrq[i].sg_list->addr,
1008 tx->tx_wrq[i].sg_list->length,
1009 tx->tx_wrq[i].sg_list->lkey);
1012 /* I'm still holding ibc_lock! */
1013 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1016 rc = ib_post_send(conn->ibc_cmid->qp, tx->tx_wrq, &bad_wrq);
1018 conn->ibc_last_send = jiffies;
1021 /* NB credits are transferred in the actual
1022 * message, which can only be the last work item */
1023 conn->ibc_outstanding_credits += tx->tx_msg->ibm_credits;
1025 conn->ibc_credits++;
1026 conn->ibc_nsends_posted--;
1032 done = (tx->tx_sending == 0);
1034 list_del (&tx->tx_list);
1036 spin_unlock(&conn->ibc_lock);
1038 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1039 CERROR("Error %d posting transmit to %s\n",
1040 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1042 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1043 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1045 kiblnd_close_conn(conn, rc);
1048 kiblnd_tx_done(ni, tx);
1053 spin_unlock(&conn->ibc_lock);
1057 kiblnd_tx_complete (kib_tx_t *tx, int status)
1059 int failed = (status != IB_WC_SUCCESS);
1060 kib_conn_t *conn = tx->tx_conn;
1063 LASSERT (tx->tx_sending > 0);
1066 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1067 CDEBUG(D_NETERROR, "Tx -> %s cookie "LPX64
1068 "sending %d waiting %d: failed %d\n",
1069 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1070 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1073 kiblnd_close_conn(conn, -EIO);
1075 kiblnd_peer_alive(conn->ibc_peer);
1078 spin_lock(&conn->ibc_lock);
1080 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1081 * gets to free it, which also drops its ref on 'conn'. */
1084 conn->ibc_nsends_posted--;
1087 tx->tx_waiting = 0; /* don't wait for peer */
1088 tx->tx_status = -EIO;
1091 idle = (tx->tx_sending == 0) && /* This is the final callback */
1092 !tx->tx_waiting && /* Not waiting for peer */
1093 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1095 list_del(&tx->tx_list);
1097 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1099 spin_unlock(&conn->ibc_lock);
1102 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1104 kiblnd_check_sends(conn);
1106 kiblnd_conn_decref(conn); /* ...until here */
1110 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1112 kib_net_t *net = ni->ni_data;
1113 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1114 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1115 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1117 LASSERT (net != NULL);
1118 LASSERT (tx->tx_nwrq >= 0);
1119 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1120 LASSERT (nob <= IBLND_MSG_SIZE);
1122 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1124 sge->addr = tx->tx_msgaddr;
1125 sge->lkey = net->ibn_dev->ibd_mr->lkey;
1128 memset(wrq, 0, sizeof(*wrq));
1131 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1134 wrq->opcode = IB_WR_SEND;
1135 wrq->send_flags = IB_SEND_SIGNALED;
1141 kiblnd_init_rdma (lnet_ni_t *ni, kib_tx_t *tx, int type,
1142 int nob, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1144 kib_msg_t *ibmsg = tx->tx_msg;
1145 kib_rdma_desc_t *srcrd = tx->tx_rd;
1146 struct ib_sge *sge = &tx->tx_sge[0];
1147 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1150 #if IBLND_MAP_ON_DEMAND
1151 LASSERT (!in_interrupt());
1152 LASSERT (tx->tx_nwrq == 0);
1153 LASSERT (type == IBLND_MSG_GET_DONE ||
1154 type == IBLND_MSG_PUT_DONE);
1156 sge->addr = srcrd->rd_addr;
1157 sge->lkey = srcrd->rd_key;
1160 wrq = &tx->tx_wrq[0];
1162 wrq->next = &tx->tx_wrq[1];
1163 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1166 wrq->opcode = IB_WR_RDMA_WRITE;
1167 wrq->send_flags = 0;
1169 wrq->wr.rdma.remote_addr = dstrd->rd_addr;
1170 wrq->wr.rdma.rkey = dstrd->rd_key;
1174 /* CAVEAT EMPTOR: this 'consumes' the frags in 'dstrd' */
1176 kib_rdma_frag_t *srcfrag;
1178 kib_rdma_frag_t *dstfrag;
1182 LASSERT (!in_interrupt());
1183 LASSERT (tx->tx_nwrq == 0);
1184 LASSERT (type == IBLND_MSG_GET_DONE ||
1185 type == IBLND_MSG_PUT_DONE);
1187 srcidx = dstidx = 0;
1188 srcfrag = &srcrd->rd_frags[0];
1189 dstfrag = &dstrd->rd_frags[0];
1192 if (srcidx >= srcrd->rd_nfrags) {
1193 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1198 if (dstidx == dstrd->rd_nfrags) {
1199 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1204 if (tx->tx_nwrq == IBLND_MAX_RDMA_FRAGS) {
1205 CERROR("RDMA too fragmented: %d/%d src %d/%d dst frags\n",
1206 srcidx, srcrd->rd_nfrags,
1207 dstidx, dstrd->rd_nfrags);
1212 wrknob = MIN(MIN(srcfrag->rf_nob, dstfrag->rf_nob), resid);
1214 sge = &tx->tx_sge[tx->tx_nwrq];
1215 sge->addr = srcfrag->rf_addr;
1216 sge->length = wrknob;
1217 sge->lkey = srcrd->rd_key;
1219 wrq = &tx->tx_wrq[tx->tx_nwrq];
1221 wrq->next = wrq + 1;
1222 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1225 wrq->opcode = IB_WR_RDMA_WRITE;
1226 wrq->send_flags = 0;
1228 wrq->wr.rdma.remote_addr = dstfrag->rf_addr;
1229 wrq->wr.rdma.rkey = dstrd->rd_key;
1235 if (wrknob < srcfrag->rf_nob) {
1236 srcfrag->rf_nob -= wrknob;
1237 srcfrag->rf_addr += wrknob;
1243 if (wrknob < dstfrag->rf_nob) {
1244 dstfrag->rf_nob -= wrknob;
1245 dstfrag->rf_addr += wrknob;
1254 if (rc < 0) /* no RDMA if completing with failure */
1257 ibmsg->ibm_u.completion.ibcm_status = rc;
1258 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1259 kiblnd_init_tx_msg(ni, tx, type, sizeof (kib_completion_msg_t));
1265 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1267 struct list_head *q;
1269 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1270 LASSERT (!tx->tx_queued); /* not queued for sending already */
1273 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * HZ);
1275 if (tx->tx_conn == NULL) {
1276 kiblnd_conn_addref(conn);
1278 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1280 /* PUT_DONE first attached to conn as a PUT_REQ */
1281 LASSERT (tx->tx_conn == conn);
1282 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1285 switch (tx->tx_msg->ibm_type) {
1289 case IBLND_MSG_PUT_REQ:
1290 case IBLND_MSG_GET_REQ:
1291 q = &conn->ibc_tx_queue_rsrvd;
1294 case IBLND_MSG_PUT_NAK:
1295 case IBLND_MSG_PUT_ACK:
1296 case IBLND_MSG_PUT_DONE:
1297 case IBLND_MSG_GET_DONE:
1298 q = &conn->ibc_tx_queue_nocred;
1301 case IBLND_MSG_NOOP:
1302 case IBLND_MSG_IMMEDIATE:
1303 q = &conn->ibc_tx_queue;
1307 list_add_tail(&tx->tx_list, q);
1311 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1313 spin_lock(&conn->ibc_lock);
1314 kiblnd_queue_tx_locked(tx, conn);
1315 spin_unlock(&conn->ibc_lock);
1317 kiblnd_check_sends(conn);
1321 kiblnd_connect_peer (kib_peer_t *peer)
1323 struct rdma_cm_id *cmid;
1324 struct sockaddr_in sockaddr;
1327 LASSERT (peer->ibp_connecting > 0);
1329 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1331 CERROR("Can't create CMID for %s: %ld\n",
1332 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1337 memset(&sockaddr, 0, sizeof(sockaddr));
1338 sockaddr.sin_family = AF_INET;
1339 sockaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1340 sockaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1342 kiblnd_peer_addref(peer); /* cmid's ref */
1344 rc = rdma_resolve_addr(cmid, NULL, (struct sockaddr *)&sockaddr,
1345 *kiblnd_tunables.kib_timeout * 1000);
1349 /* Can't initiate address resolution: */
1350 CERROR("Can't resolve addr for %s: %d\n",
1351 libcfs_nid2str(peer->ibp_nid), rc);
1353 kiblnd_peer_decref(peer); /* cmid's ref */
1354 rdma_destroy_id(cmid);
1356 kiblnd_peer_connect_failed(peer, 1, rc);
1360 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1365 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1366 unsigned long flags;
1369 /* If I get here, I've committed to send, so I complete the tx with
1370 * failure on any problems */
1372 LASSERT (tx->tx_conn == NULL); /* only set when assigned a conn */
1373 LASSERT (tx->tx_nwrq > 0); /* work items have been set up */
1375 /* First time, just use a read lock since I expect to find my peer
1377 read_lock_irqsave(g_lock, flags);
1379 peer = kiblnd_find_peer_locked(nid);
1380 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1381 /* Found a peer with an established connection */
1382 conn = kiblnd_get_conn_locked(peer);
1383 kiblnd_conn_addref(conn); /* 1 ref for me... */
1385 read_unlock_irqrestore(g_lock, flags);
1387 kiblnd_queue_tx(tx, conn);
1388 kiblnd_conn_decref(conn); /* ...to here */
1392 read_unlock(g_lock);
1393 /* Re-try with a write lock */
1396 peer = kiblnd_find_peer_locked(nid);
1398 if (list_empty(&peer->ibp_conns)) {
1399 /* found a peer, but it's still connecting... */
1400 LASSERT (peer->ibp_connecting != 0 ||
1401 peer->ibp_accepting != 0);
1402 list_add_tail (&tx->tx_list, &peer->ibp_tx_queue);
1403 write_unlock_irqrestore(g_lock, flags);
1405 conn = kiblnd_get_conn_locked(peer);
1406 kiblnd_conn_addref(conn); /* 1 ref for me... */
1408 write_unlock_irqrestore(g_lock, flags);
1410 kiblnd_queue_tx(tx, conn);
1411 kiblnd_conn_decref(conn); /* ...to here */
1416 write_unlock_irqrestore(g_lock, flags);
1418 /* Allocate a peer ready to add to the peer table and retry */
1419 rc = kiblnd_create_peer(ni, &peer, nid);
1421 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1422 tx->tx_status = -EHOSTUNREACH;
1424 kiblnd_tx_done(ni, tx);
1428 write_lock_irqsave(g_lock, flags);
1430 peer2 = kiblnd_find_peer_locked(nid);
1431 if (peer2 != NULL) {
1432 if (list_empty(&peer2->ibp_conns)) {
1433 /* found a peer, but it's still connecting... */
1434 LASSERT (peer2->ibp_connecting != 0 ||
1435 peer2->ibp_accepting != 0);
1436 list_add_tail (&tx->tx_list, &peer2->ibp_tx_queue);
1437 write_unlock_irqrestore(g_lock, flags);
1439 conn = kiblnd_get_conn_locked(peer2);
1440 kiblnd_conn_addref(conn); /* 1 ref for me... */
1442 write_unlock_irqrestore(g_lock, flags);
1444 kiblnd_queue_tx(tx, conn);
1445 kiblnd_conn_decref(conn); /* ...to here */
1448 kiblnd_peer_decref(peer);
1452 /* Brand new peer */
1453 LASSERT (peer->ibp_connecting == 0);
1454 peer->ibp_connecting = 1;
1456 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1458 kiblnd_peer_addref(peer);
1459 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1461 write_unlock_irqrestore(g_lock, flags);
1463 kiblnd_connect_peer(peer);
1464 kiblnd_peer_decref(peer);
1468 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1470 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1471 int type = lntmsg->msg_type;
1472 lnet_process_id_t target = lntmsg->msg_target;
1473 int target_is_router = lntmsg->msg_target_is_router;
1474 int routing = lntmsg->msg_routing;
1475 unsigned int payload_niov = lntmsg->msg_niov;
1476 struct iovec *payload_iov = lntmsg->msg_iov;
1477 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1478 unsigned int payload_offset = lntmsg->msg_offset;
1479 unsigned int payload_nob = lntmsg->msg_len;
1485 /* NB 'private' is different depending on what we're sending.... */
1487 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1488 payload_nob, payload_niov, libcfs_id2str(target));
1490 LASSERT (payload_nob == 0 || payload_niov > 0);
1491 LASSERT (payload_niov <= LNET_MAX_IOV);
1493 /* Thread context */
1494 LASSERT (!in_interrupt());
1495 /* payload is either all vaddrs or all pages */
1496 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1504 LASSERT (payload_nob == 0);
1508 if (routing || target_is_router)
1509 break; /* send IMMEDIATE */
1511 /* is the REPLY message too small for RDMA? */
1512 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1513 if (nob <= IBLND_MSG_SIZE)
1514 break; /* send IMMEDIATE */
1516 tx = kiblnd_get_idle_tx(ni);
1518 CERROR("Can allocate txd for GET to %s: \n",
1519 libcfs_nid2str(target.nid));
1524 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1525 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1527 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1528 rc = kiblnd_setup_rd_iov(ni, tx,
1529 &ibmsg->ibm_u.get.ibgm_rd,
1530 lntmsg->msg_md->md_niov,
1531 lntmsg->msg_md->md_iov.iov,
1532 0, lntmsg->msg_md->md_length);
1534 rc = kiblnd_setup_rd_kiov(ni, tx,
1535 &ibmsg->ibm_u.get.ibgm_rd,
1536 lntmsg->msg_md->md_niov,
1537 lntmsg->msg_md->md_iov.kiov,
1538 0, lntmsg->msg_md->md_length);
1540 CERROR("Can't setup GET sink for %s: %d\n",
1541 libcfs_nid2str(target.nid), rc);
1542 kiblnd_tx_done(ni, tx);
1545 #if IBLND_MAP_ON_DEMAND
1546 nob = sizeof(kib_get_msg_t);
1548 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1550 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1552 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1553 if (tx->tx_lntmsg[1] == NULL) {
1554 CERROR("Can't create reply for GET -> %s\n",
1555 libcfs_nid2str(target.nid));
1556 kiblnd_tx_done(ni, tx);
1560 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1561 tx->tx_waiting = 1; /* waiting for GET_DONE */
1562 kiblnd_launch_tx(ni, tx, target.nid);
1565 case LNET_MSG_REPLY:
1567 /* Is the payload small enough not to need RDMA? */
1568 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1569 if (nob <= IBLND_MSG_SIZE)
1570 break; /* send IMMEDIATE */
1572 tx = kiblnd_get_idle_tx(ni);
1574 CERROR("Can't allocate %s txd for %s\n",
1575 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1576 libcfs_nid2str(target.nid));
1580 if (payload_kiov == NULL)
1581 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1582 payload_niov, payload_iov,
1583 payload_offset, payload_nob);
1585 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1586 payload_niov, payload_kiov,
1587 payload_offset, payload_nob);
1589 CERROR("Can't setup PUT src for %s: %d\n",
1590 libcfs_nid2str(target.nid), rc);
1591 kiblnd_tx_done(ni, tx);
1596 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1597 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1598 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1600 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1601 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1602 kiblnd_launch_tx(ni, tx, target.nid);
1606 /* send IMMEDIATE */
1608 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1611 tx = kiblnd_get_idle_tx(ni);
1613 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1614 type, libcfs_nid2str(target.nid));
1619 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1621 if (payload_kiov != NULL)
1622 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1623 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1624 payload_niov, payload_kiov,
1625 payload_offset, payload_nob);
1627 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1628 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1629 payload_niov, payload_iov,
1630 payload_offset, payload_nob);
1632 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1633 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1635 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1636 kiblnd_launch_tx(ni, tx, target.nid);
1641 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1643 lnet_process_id_t target = lntmsg->msg_target;
1644 unsigned int niov = lntmsg->msg_niov;
1645 struct iovec *iov = lntmsg->msg_iov;
1646 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1647 unsigned int offset = lntmsg->msg_offset;
1648 unsigned int nob = lntmsg->msg_len;
1652 tx = kiblnd_get_idle_tx(ni);
1654 CERROR("Can't get tx for REPLY to %s\n",
1655 libcfs_nid2str(target.nid));
1661 else if (kiov == NULL)
1662 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1663 niov, iov, offset, nob);
1665 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1666 niov, kiov, offset, nob);
1669 CERROR("Can't setup GET src for %s: %d\n",
1670 libcfs_nid2str(target.nid), rc);
1674 rc = kiblnd_init_rdma(ni, tx, IBLND_MSG_GET_DONE, nob,
1675 &rx->rx_msg->ibm_u.get.ibgm_rd,
1676 rx->rx_msg->ibm_u.get.ibgm_cookie);
1678 CERROR("Can't setup rdma for GET from %s: %d\n",
1679 libcfs_nid2str(target.nid), rc);
1684 /* No RDMA: local completion may happen now! */
1685 lnet_finalize(ni, lntmsg, 0);
1687 /* RDMA: lnet_finalize(lntmsg) when it
1689 tx->tx_lntmsg[0] = lntmsg;
1692 kiblnd_queue_tx(tx, rx->rx_conn);
1696 kiblnd_tx_done(ni, tx);
1698 lnet_finalize(ni, lntmsg, -EIO);
1702 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1703 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1704 unsigned int offset, unsigned int mlen, unsigned int rlen)
1706 kib_rx_t *rx = private;
1707 kib_msg_t *rxmsg = rx->rx_msg;
1708 kib_conn_t *conn = rx->rx_conn;
1712 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1715 LASSERT (mlen <= rlen);
1716 LASSERT (!in_interrupt());
1717 /* Either all pages or all vaddrs */
1718 LASSERT (!(kiov != NULL && iov != NULL));
1720 switch (rxmsg->ibm_type) {
1724 case IBLND_MSG_IMMEDIATE:
1725 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1726 if (nob > rx->rx_nob) {
1727 CERROR ("Immediate message from %s too big: %d(%d)\n",
1728 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1735 lnet_copy_flat2kiov(niov, kiov, offset,
1736 IBLND_MSG_SIZE, rxmsg,
1737 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1740 lnet_copy_flat2iov(niov, iov, offset,
1741 IBLND_MSG_SIZE, rxmsg,
1742 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1744 lnet_finalize (ni, lntmsg, 0);
1747 case IBLND_MSG_PUT_REQ:
1749 lnet_finalize(ni, lntmsg, 0);
1750 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1751 rxmsg->ibm_u.putreq.ibprm_cookie);
1755 tx = kiblnd_get_idle_tx(ni);
1757 CERROR("Can't allocate tx for %s\n",
1758 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1759 /* Not replying will break the connection */
1766 rc = kiblnd_setup_rd_iov(ni, tx,
1767 &txmsg->ibm_u.putack.ibpam_rd,
1768 niov, iov, offset, mlen);
1770 rc = kiblnd_setup_rd_kiov(ni, tx,
1771 &txmsg->ibm_u.putack.ibpam_rd,
1772 niov, kiov, offset, mlen);
1774 CERROR("Can't setup PUT sink for %s: %d\n",
1775 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1776 kiblnd_tx_done(ni, tx);
1777 /* tell peer it's over */
1778 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1779 rxmsg->ibm_u.putreq.ibprm_cookie);
1783 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1784 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1785 #if IBLND_MAP_ON_DEMAND
1786 nob = sizeof(kib_putack_msg_t);
1788 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1790 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1792 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1793 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1794 kiblnd_queue_tx(tx, conn);
1796 /* reposted buffer reserved for PUT_DONE */
1797 post_credit = IBLND_POSTRX_NO_CREDIT;
1800 case IBLND_MSG_GET_REQ:
1801 if (lntmsg != NULL) {
1802 /* Optimized GET; RDMA lntmsg's payload */
1803 kiblnd_reply(ni, rx, lntmsg);
1805 /* GET didn't match anything */
1806 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1808 rxmsg->ibm_u.get.ibgm_cookie);
1813 kiblnd_post_rx(rx, post_credit);
1818 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1820 long pid = kernel_thread (fn, arg, 0);
1825 atomic_inc (&kiblnd_data.kib_nthreads);
1830 kiblnd_thread_fini (void)
1832 atomic_dec (&kiblnd_data.kib_nthreads);
1836 kiblnd_peer_alive (kib_peer_t *peer)
1838 /* This is racy, but everyone's only writing cfs_time_current() */
1839 peer->ibp_last_alive = cfs_time_current();
1844 kiblnd_peer_notify (kib_peer_t *peer)
1846 time_t last_alive = 0;
1848 unsigned long flags;
1850 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1852 if (list_empty(&peer->ibp_conns) &&
1853 peer->ibp_accepting == 0 &&
1854 peer->ibp_connecting == 0 &&
1855 peer->ibp_error != 0) {
1856 error = peer->ibp_error;
1857 peer->ibp_error = 0;
1859 last_alive = cfs_time_current_sec() -
1860 cfs_duration_sec(cfs_time_current() -
1861 peer->ibp_last_alive);
1864 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1867 lnet_notify(peer->ibp_ni,
1868 peer->ibp_nid, 0, last_alive);
1872 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1874 /* This just does the immediate housekeeping. 'error' is zero for a
1875 * normal shutdown which can happen only after the connection has been
1876 * established. If the connection is established, schedule the
1877 * connection to be finished off by the connd. Otherwise the connd is
1878 * already dealing with it (either to set it up or tear it down).
1879 * Caller holds kib_global_lock exclusively in irq context */
1880 unsigned long flags;
1881 kib_peer_t *peer = conn->ibc_peer;
1883 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1885 if (error != 0 && conn->ibc_comms_error == 0)
1886 conn->ibc_comms_error = error;
1888 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1889 return; /* already being handled */
1892 list_empty(&conn->ibc_tx_queue) &&
1893 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1894 list_empty(&conn->ibc_tx_queue_nocred) &&
1895 list_empty(&conn->ibc_active_txs)) {
1896 CDEBUG(D_NET, "closing conn to %s\n",
1897 libcfs_nid2str(peer->ibp_nid));
1899 CDEBUG(D_NETERROR, "Closing conn to %s: error %d%s%s%s%s\n",
1900 libcfs_nid2str(peer->ibp_nid), error,
1901 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1902 list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1903 list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1904 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1907 list_del (&conn->ibc_list);
1908 /* connd (see below) takes over ibc_list's ref */
1910 if (list_empty (&peer->ibp_conns) && /* no more conns */
1911 kiblnd_peer_active(peer)) { /* still in peer table */
1912 kiblnd_unlink_peer_locked(peer);
1914 /* set/clear error on last conn */
1915 peer->ibp_error = conn->ibc_comms_error;
1918 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1920 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1922 list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1923 wake_up (&kiblnd_data.kib_connd_waitq);
1925 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1929 kiblnd_close_conn (kib_conn_t *conn, int error)
1931 unsigned long flags;
1933 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1935 kiblnd_close_conn_locked(conn, error);
1937 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1941 kiblnd_handle_early_rxs(kib_conn_t *conn)
1943 unsigned long flags;
1946 LASSERT (!in_interrupt());
1947 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1949 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1950 while (!list_empty(&conn->ibc_early_rxs)) {
1951 rx = list_entry(conn->ibc_early_rxs.next,
1953 list_del(&rx->rx_list);
1954 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1956 kiblnd_handle_rx(rx);
1958 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1960 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1964 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1966 LIST_HEAD (zombies);
1967 struct list_head *tmp;
1968 struct list_head *nxt;
1971 spin_lock(&conn->ibc_lock);
1973 list_for_each_safe (tmp, nxt, txs) {
1974 tx = list_entry (tmp, kib_tx_t, tx_list);
1976 if (txs == &conn->ibc_active_txs) {
1977 LASSERT (!tx->tx_queued);
1978 LASSERT (tx->tx_waiting ||
1979 tx->tx_sending != 0);
1981 LASSERT (tx->tx_queued);
1984 tx->tx_status = -ECONNABORTED;
1988 if (tx->tx_sending == 0) {
1989 list_del (&tx->tx_list);
1990 list_add (&tx->tx_list, &zombies);
1994 spin_unlock(&conn->ibc_lock);
1996 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1997 &zombies, -ECONNABORTED);
2001 kiblnd_finalise_conn (kib_conn_t *conn)
2003 LASSERT (!in_interrupt());
2004 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2006 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2008 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2009 * for connections that didn't get as far as being connected, because
2010 * rdma_disconnect() does this for free. */
2011 kiblnd_abort_receives(conn);
2013 /* Complete all tx descs not waiting for sends to complete.
2014 * NB we should be safe from RDMA now that the QP has changed state */
2016 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2017 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2018 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2019 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2021 kiblnd_handle_early_rxs(conn);
2025 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2027 LIST_HEAD (zombies);
2028 unsigned long flags;
2030 LASSERT (error != 0);
2031 LASSERT (!in_interrupt());
2033 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2036 LASSERT (peer->ibp_connecting > 0);
2037 peer->ibp_connecting--;
2039 LASSERT (peer->ibp_accepting > 0);
2040 peer->ibp_accepting--;
2043 if (peer->ibp_connecting != 0 ||
2044 peer->ibp_accepting != 0) {
2045 /* another connection attempt under way... */
2046 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2050 if (list_empty(&peer->ibp_conns)) {
2051 /* Take peer's blocked transmits to complete with error */
2052 list_add(&zombies, &peer->ibp_tx_queue);
2053 list_del_init(&peer->ibp_tx_queue);
2055 if (kiblnd_peer_active(peer))
2056 kiblnd_unlink_peer_locked(peer);
2058 peer->ibp_error = error;
2060 /* Can't have blocked transmits if there are connections */
2061 LASSERT (list_empty(&peer->ibp_tx_queue));
2064 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2066 kiblnd_peer_notify(peer);
2068 if (list_empty (&zombies))
2071 CDEBUG (D_NETERROR, "Deleting messages for %s: connection failed\n",
2072 libcfs_nid2str(peer->ibp_nid));
2074 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2078 kiblnd_connreq_done(kib_conn_t *conn, int status)
2080 struct list_head txs;
2082 kib_peer_t *peer = conn->ibc_peer;
2084 unsigned long flags;
2087 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2089 CDEBUG(D_NET,"%s: %d, %d\n", libcfs_nid2str(peer->ibp_nid),
2092 LASSERT (!in_interrupt());
2093 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2094 peer->ibp_connecting > 0) ||
2095 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2096 peer->ibp_accepting > 0));
2098 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2099 conn->ibc_connvars = NULL;
2102 /* failed to establish connection */
2103 kiblnd_peer_connect_failed(conn->ibc_peer, active, status);
2104 kiblnd_finalise_conn(conn);
2108 /* connection established */
2109 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2111 conn->ibc_last_send = jiffies;
2112 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2113 kiblnd_peer_alive(peer);
2115 /* Add conn to peer's list and nuke any dangling conns from a different
2116 * peer instance... */
2117 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2118 list_add(&conn->ibc_list, &peer->ibp_conns);
2120 peer->ibp_connecting--;
2122 peer->ibp_accepting--;
2124 kiblnd_close_stale_conns_locked(conn->ibc_peer,
2125 conn->ibc_incarnation);
2127 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2128 conn->ibc_comms_error != 0) { /* error has happened already */
2130 /* start to shut down connection */
2131 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2132 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2136 /* grab pending txs while I have the lock */
2137 list_add(&txs, &peer->ibp_tx_queue);
2138 list_del_init(&peer->ibp_tx_queue);
2140 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2142 /* Schedule blocked txs */
2143 spin_lock (&conn->ibc_lock);
2144 while (!list_empty (&txs)) {
2145 tx = list_entry (txs.next, kib_tx_t, tx_list);
2146 list_del (&tx->tx_list);
2148 kiblnd_queue_tx_locked(tx, conn);
2150 spin_unlock (&conn->ibc_lock);
2152 kiblnd_check_sends(conn);
2154 /* schedule blocked rxs */
2155 kiblnd_handle_early_rxs(conn);
2159 kiblnd_reject(struct rdma_cm_id *cmid, int why)
2162 kib_rej_t rej = {.ibr_magic = IBLND_MSG_MAGIC,
2163 .ibr_version = IBLND_MSG_VERSION,
2166 rc = rdma_reject(cmid, &rej, sizeof(rej));
2169 CWARN("Error %d sending reject\n", rc);
2173 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2176 kib_msg_t *reqmsg = priv;
2177 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2178 struct rdma_conn_param cp;
2179 unsigned long flags;
2180 lnet_ni_t *ni = NULL;
2187 int rej = IBLND_REJECT_FATAL;
2189 LASSERT (!in_interrupt());
2191 /* cmid inherits 'context' from the corresponding listener id */
2192 ibdev = (kib_dev_t *)cmid->context;
2193 LASSERT (ibdev != NULL);
2195 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2196 CERROR("Short connection request\n");
2200 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2201 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC) ||
2202 (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2203 reqmsg->ibm_version != IBLND_MSG_VERSION) ||
2204 (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2205 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION))) {
2206 /* Future protocol version compatibility support! If the
2207 * o2iblnd-specific protocol changes, or when LNET unifies
2208 * protocols over all LNDs, the initial connection will
2209 * negotiate a protocol version. I trap this here to avoid
2210 * console errors; the reject tells the peer which protocol I
2215 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2217 CERROR("Can't parse connection request: %d\n", rc);
2221 nid = reqmsg->ibm_srcnid;
2223 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2224 CERROR("Unexpected connreq msg type: %x from %s\n",
2225 reqmsg->ibm_type, libcfs_nid2str(nid));
2229 if (reqmsg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2230 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2231 libcfs_nid2str(nid),
2232 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2233 IBLND_MSG_QUEUE_SIZE);
2237 if (reqmsg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2238 CERROR("Can't accept %s: incompatible max_frags %d (%d wanted)\n",
2239 libcfs_nid2str(nid),
2240 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2241 IBLND_MSG_QUEUE_SIZE);
2245 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2246 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2247 libcfs_nid2str(nid),
2248 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2253 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2254 if (ni == NULL || /* no matching net */
2255 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2256 ((kib_net_t*)ni->ni_data)->ibn_dev != ibdev) { /* wrong device */
2257 CERROR("Can't accept %s: bad dst nid %s\n",
2258 libcfs_nid2str(nid),
2259 libcfs_nid2str(reqmsg->ibm_dstnid));
2264 /* assume 'nid' is a new peer; create */
2265 rc = kiblnd_create_peer(ni, &peer, nid);
2267 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2268 rej = IBLND_REJECT_NO_RESOURCES;
2272 write_lock_irqsave(g_lock, flags);
2274 peer2 = kiblnd_find_peer_locked(nid);
2275 if (peer2 != NULL) {
2276 /* tie-break connection race in favour of the higher NID */
2277 if (peer2->ibp_connecting != 0 &&
2279 write_unlock_irqrestore(g_lock, flags);
2281 CWARN("Conn race %s\n",
2282 libcfs_nid2str(peer2->ibp_nid));
2284 kiblnd_peer_decref(peer);
2285 rej = IBLND_REJECT_CONN_RACE;
2289 peer2->ibp_accepting++;
2290 kiblnd_peer_addref(peer2);
2292 write_unlock_irqrestore(g_lock, flags);
2293 kiblnd_peer_decref(peer);
2296 /* Brand new peer */
2297 LASSERT (peer->ibp_accepting == 0);
2298 peer->ibp_accepting = 1;
2300 kiblnd_peer_addref(peer);
2301 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2303 write_unlock_irqrestore(g_lock, flags);
2306 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT);
2308 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2309 kiblnd_peer_decref(peer);
2310 rej = IBLND_REJECT_NO_RESOURCES;
2314 /* conn now "owns" cmid, so I return success from here on to ensure the
2315 * CM callback doesn't destroy cmid. */
2317 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2318 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2319 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2320 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2323 ackmsg = &conn->ibc_connvars->cv_msg;
2324 memset(ackmsg, 0, sizeof(*ackmsg));
2326 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2327 sizeof(ackmsg->ibm_u.connparams));
2328 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2329 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2330 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2331 kiblnd_pack_msg(ni, ackmsg, 0, nid, reqmsg->ibm_srcstamp);
2333 memset(&cp, 0, sizeof(cp));
2334 cp.private_data = ackmsg;
2335 cp.private_data_len = ackmsg->ibm_nob;
2336 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2337 cp.initiator_depth = 0;
2338 cp.flow_control = 1;
2339 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2340 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2342 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2344 rc = rdma_accept(cmid, &cp);
2346 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2347 kiblnd_reject(cmid, IBLND_REJECT_FATAL);
2348 kiblnd_connreq_done(conn, rc);
2349 kiblnd_conn_decref(conn);
2359 kiblnd_reject(cmid, rej);
2360 return -ECONNREFUSED;
2364 kiblnd_reconnect (kib_conn_t *conn, char *why)
2366 kib_peer_t *peer = conn->ibc_peer;
2368 unsigned long flags;
2370 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2371 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2373 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2375 /* retry connection if it's still needed and no other connection
2376 * attempts (active or passive) are in progress */
2377 if (!list_empty(&peer->ibp_tx_queue) &&
2378 peer->ibp_connecting == 1 &&
2379 peer->ibp_accepting == 0) {
2381 peer->ibp_connecting++;
2384 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2387 CDEBUG(D_NETERROR, "%s: retrying (%s)\n",
2388 libcfs_nid2str(peer->ibp_nid), why);
2389 kiblnd_connect_peer(peer);
2394 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2396 kib_peer_t *peer = conn->ibc_peer;
2398 LASSERT (!in_interrupt());
2399 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2402 case IB_CM_REJ_STALE_CONN:
2403 kiblnd_reconnect(conn, "stale");
2406 case IB_CM_REJ_CONSUMER_DEFINED:
2407 if (priv_nob >= sizeof(kib_rej_t)) {
2408 kib_rej_t *rej = priv;
2410 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2411 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2412 __swab32s(&rej->ibr_magic);
2413 __swab16s(&rej->ibr_version);
2416 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2417 rej->ibr_magic != LNET_PROTO_MAGIC) {
2418 CERROR("%s rejected: consumer defined fatal error\n",
2419 libcfs_nid2str(peer->ibp_nid));
2423 if (rej->ibr_version != IBLND_MSG_VERSION) {
2424 CERROR("%s rejected: o2iblnd version %d error\n",
2425 libcfs_nid2str(peer->ibp_nid),
2430 switch (rej->ibr_why) {
2431 case IBLND_REJECT_CONN_RACE:
2432 kiblnd_reconnect(conn, "conn race");
2435 case IBLND_REJECT_NO_RESOURCES:
2436 CERROR("%s rejected: o2iblnd no resources\n",
2437 libcfs_nid2str(peer->ibp_nid));
2439 case IBLND_REJECT_FATAL:
2440 CERROR("%s rejected: o2iblnd fatal error\n",
2441 libcfs_nid2str(peer->ibp_nid));
2444 CERROR("%s rejected: o2iblnd reason %d\n",
2445 libcfs_nid2str(peer->ibp_nid),
2453 CDEBUG(D_NETERROR, "%s rejected: reason %d, size %d\n",
2454 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2458 kiblnd_connreq_done(conn, -ECONNREFUSED);
2462 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2464 kib_peer_t *peer = conn->ibc_peer;
2465 lnet_ni_t *ni = peer->ibp_ni;
2466 kib_net_t *net = ni->ni_data;
2467 kib_msg_t *msg = priv;
2468 int rc = kiblnd_unpack_msg(msg, priv_nob);
2469 unsigned long flags;
2471 LASSERT (net != NULL);
2474 CERROR("Can't unpack connack from %s: %d\n",
2475 libcfs_nid2str(peer->ibp_nid), rc);
2479 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2480 CERROR("Unexpected message %d from %s\n",
2481 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2486 if (msg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2487 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2488 libcfs_nid2str(peer->ibp_nid),
2489 msg->ibm_u.connparams.ibcp_queue_depth,
2490 IBLND_MSG_QUEUE_SIZE);
2495 if (msg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2496 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2497 libcfs_nid2str(peer->ibp_nid),
2498 msg->ibm_u.connparams.ibcp_queue_depth,
2499 IBLND_MSG_QUEUE_SIZE);
2504 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2505 CERROR("%s max message size %d too big (%d max)\n",
2506 libcfs_nid2str(peer->ibp_nid),
2507 msg->ibm_u.connparams.ibcp_max_msg_size,
2513 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2514 if (msg->ibm_dstnid == ni->ni_nid &&
2515 msg->ibm_dststamp == net->ibn_incarnation)
2519 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2522 CERROR("Stale connection reply from %s\n",
2523 libcfs_nid2str(peer->ibp_nid));
2527 conn->ibc_incarnation = msg->ibm_srcstamp;
2528 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2529 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2530 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2533 kiblnd_connreq_done(conn, 0);
2537 /* NB My QP has already established itself, so I handle anything going
2538 * wrong here by setting ibc_comms_error.
2539 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2540 * immediately tears it down. */
2543 conn->ibc_comms_error = rc;
2544 kiblnd_connreq_done(conn, 0);
2548 kiblnd_active_connect (struct rdma_cm_id *cmid)
2550 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2553 struct rdma_conn_param cp;
2556 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT);
2558 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2559 kiblnd_peer_decref(peer); /* lose cmid's ref */
2563 /* conn "owns" cmid now, so I return success from here on to ensure the
2564 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2567 msg = &conn->ibc_connvars->cv_msg;
2569 memset(msg, 0, sizeof(*msg));
2570 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2571 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2572 msg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2573 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2574 kiblnd_pack_msg(peer->ibp_ni, msg, 0, peer->ibp_nid, 0);
2576 memset(&cp, 0, sizeof(cp));
2577 cp.private_data = msg;
2578 cp.private_data_len = msg->ibm_nob;
2579 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2580 cp.initiator_depth = 0;
2581 cp.flow_control = 1;
2582 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2583 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2585 LASSERT(cmid->context == (void *)conn);
2586 LASSERT(conn->ibc_cmid == cmid);
2588 rc = rdma_connect(cmid, &cp);
2590 CERROR("Can't connect to %s: %d\n",
2591 libcfs_nid2str(peer->ibp_nid), rc);
2592 kiblnd_connreq_done(conn, rc);
2593 kiblnd_conn_decref(conn);
2600 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2606 switch (event->event) {
2610 case RDMA_CM_EVENT_CONNECT_REQUEST:
2611 /* destroy cmid on failure */
2612 rc = kiblnd_passive_connect(cmid,
2613 event->private_data,
2614 event->private_data_len);
2615 CDEBUG(D_NET, "connreq: %d\n", rc);
2618 case RDMA_CM_EVENT_ADDR_ERROR:
2619 peer = (kib_peer_t *)cmid->context;
2620 CDEBUG(D_NETERROR, "%s: ADDR ERROR %d\n",
2621 libcfs_nid2str(peer->ibp_nid), event->status);
2622 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2623 kiblnd_peer_decref(peer);
2624 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2626 case RDMA_CM_EVENT_ADDR_RESOLVED:
2627 peer = (kib_peer_t *)cmid->context;
2629 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2630 libcfs_nid2str(peer->ibp_nid), event->status);
2632 if (event->status != 0) {
2633 CDEBUG(D_NETERROR, "Can't resolve address for %s: %d\n",
2634 libcfs_nid2str(peer->ibp_nid), event->status);
2637 rc = rdma_resolve_route(
2638 cmid, *kiblnd_tunables.kib_timeout * 1000);
2641 /* Can't initiate route resolution */
2642 CERROR("Can't resolve route for %s: %d\n",
2643 libcfs_nid2str(peer->ibp_nid), rc);
2645 kiblnd_peer_connect_failed(peer, 1, rc);
2646 kiblnd_peer_decref(peer);
2647 return rc; /* rc != 0 destroys cmid */
2649 case RDMA_CM_EVENT_ROUTE_ERROR:
2650 peer = (kib_peer_t *)cmid->context;
2651 CDEBUG(D_NETERROR, "%s: ROUTE ERROR %d\n",
2652 libcfs_nid2str(peer->ibp_nid), event->status);
2653 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2654 kiblnd_peer_decref(peer);
2655 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2657 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2658 peer = (kib_peer_t *)cmid->context;
2659 CDEBUG(D_NET,"%s Route resolved: %d\n",
2660 libcfs_nid2str(peer->ibp_nid), event->status);
2662 if (event->status == 0)
2663 return kiblnd_active_connect(cmid);
2665 CDEBUG(D_NETERROR, "Can't resolve route for %s: %d\n",
2666 libcfs_nid2str(peer->ibp_nid), event->status);
2667 kiblnd_peer_connect_failed(peer, 1, event->status);
2668 kiblnd_peer_decref(peer);
2669 return event->status; /* rc != 0 destroys cmid */
2671 case RDMA_CM_EVENT_UNREACHABLE:
2672 conn = (kib_conn_t *)cmid->context;
2673 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2674 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2675 CDEBUG(D_NETERROR, "%s: UNREACHABLE %d\n",
2676 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2677 kiblnd_connreq_done(conn, -ENETDOWN);
2678 kiblnd_conn_decref(conn);
2681 case RDMA_CM_EVENT_CONNECT_ERROR:
2682 conn = (kib_conn_t *)cmid->context;
2683 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2684 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2685 CDEBUG(D_NETERROR, "%s: CONNECT ERROR %d\n",
2686 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2687 kiblnd_connreq_done(conn, -ENOTCONN);
2688 kiblnd_conn_decref(conn);
2691 case RDMA_CM_EVENT_REJECTED:
2692 conn = (kib_conn_t *)cmid->context;
2693 switch (conn->ibc_state) {
2697 case IBLND_CONN_PASSIVE_WAIT:
2698 CERROR ("%s: REJECTED %d\n",
2699 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2701 kiblnd_connreq_done(conn, -ECONNRESET);
2704 case IBLND_CONN_ACTIVE_CONNECT:
2705 kiblnd_rejected(conn, event->status,
2706 event->private_data,
2707 event->private_data_len);
2710 kiblnd_conn_decref(conn);
2713 case RDMA_CM_EVENT_ESTABLISHED:
2714 conn = (kib_conn_t *)cmid->context;
2715 switch (conn->ibc_state) {
2719 case IBLND_CONN_PASSIVE_WAIT:
2720 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2721 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2722 kiblnd_connreq_done(conn, 0);
2725 case IBLND_CONN_ACTIVE_CONNECT:
2726 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2727 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2728 kiblnd_check_connreply(conn,
2729 event->private_data,
2730 event->private_data_len);
2733 /* net keeps its ref on conn! */
2736 case RDMA_CM_EVENT_DISCONNECTED:
2737 conn = (kib_conn_t *)cmid->context;
2738 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2739 CERROR("%s DISCONNECTED\n",
2740 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2741 kiblnd_connreq_done(conn, -ECONNRESET);
2743 kiblnd_close_conn(conn, 0);
2745 kiblnd_conn_decref(conn);
2748 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2749 LCONSOLE_ERROR("Received notification of device removal\n");
2750 LCONSOLE_ERROR("Please shutdown LNET to allow this to proceed\n");
2751 /* Can't remove network from underneath LNET for now, so I have
2758 kiblnd_check_txs (kib_conn_t *conn, struct list_head *txs)
2761 struct list_head *ttmp;
2764 spin_lock(&conn->ibc_lock);
2766 list_for_each (ttmp, txs) {
2767 tx = list_entry (ttmp, kib_tx_t, tx_list);
2769 if (txs != &conn->ibc_active_txs) {
2770 LASSERT (tx->tx_queued);
2772 LASSERT (!tx->tx_queued);
2773 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2776 if (time_after_eq (jiffies, tx->tx_deadline)) {
2782 spin_unlock(&conn->ibc_lock);
2787 kiblnd_conn_timed_out (kib_conn_t *conn)
2789 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2790 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2791 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2792 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2796 kiblnd_check_conns (int idx)
2798 struct list_head *peers = &kiblnd_data.kib_peers[idx];
2799 struct list_head *ptmp;
2802 struct list_head *ctmp;
2803 unsigned long flags;
2806 /* NB. We expect to have a look at all the peers and not find any
2807 * rdmas to time out, so we just use a shared lock while we
2809 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2811 list_for_each (ptmp, peers) {
2812 peer = list_entry (ptmp, kib_peer_t, ibp_list);
2814 list_for_each (ctmp, &peer->ibp_conns) {
2815 conn = list_entry (ctmp, kib_conn_t, ibc_list);
2817 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2819 /* In case we have enough credits to return via a
2820 * NOOP, but there were no non-blocking tx descs
2821 * free to do it last time... */
2822 kiblnd_check_sends(conn);
2824 if (!kiblnd_conn_timed_out(conn))
2827 /* Handle timeout by closing the whole connection. We
2828 * can only be sure RDMA activity has ceased once the
2829 * QP has been modified. */
2831 kiblnd_conn_addref(conn); /* 1 ref for me... */
2833 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2836 CERROR("Timed out RDMA with %s\n",
2837 libcfs_nid2str(peer->ibp_nid));
2839 kiblnd_close_conn(conn, -ETIMEDOUT);
2840 kiblnd_conn_decref(conn); /* ...until here */
2842 /* start again now I've dropped the lock */
2847 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2851 kiblnd_disconnect_conn (kib_conn_t *conn)
2853 LASSERT (!in_interrupt());
2854 LASSERT (current == kiblnd_data.kib_connd);
2855 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
2857 rdma_disconnect(conn->ibc_cmid);
2858 kiblnd_finalise_conn(conn);
2860 kiblnd_peer_notify(conn->ibc_peer);
2864 kiblnd_connd (void *arg)
2867 unsigned long flags;
2873 unsigned long deadline = jiffies;
2875 cfs_daemonize ("kiblnd_connd");
2876 cfs_block_allsigs ();
2878 init_waitqueue_entry (&wait, current);
2879 kiblnd_data.kib_connd = current;
2881 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2883 while (!kiblnd_data.kib_shutdown) {
2887 if (!list_empty (&kiblnd_data.kib_connd_zombies)) {
2888 conn = list_entry (kiblnd_data.kib_connd_zombies.next,
2889 kib_conn_t, ibc_list);
2890 list_del (&conn->ibc_list);
2892 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2895 kiblnd_destroy_conn(conn);
2897 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2900 if (!list_empty (&kiblnd_data.kib_connd_conns)) {
2901 conn = list_entry (kiblnd_data.kib_connd_conns.next,
2902 kib_conn_t, ibc_list);
2903 list_del (&conn->ibc_list);
2905 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2908 kiblnd_disconnect_conn(conn);
2909 kiblnd_conn_decref(conn);
2911 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2914 /* careful with the jiffy wrap... */
2915 timeout = (int)(deadline - jiffies);
2919 int chunk = kiblnd_data.kib_peer_hash_size;
2921 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2924 /* Time to check for RDMA timeouts on a few more
2925 * peers: I do checks every 'p' seconds on a
2926 * proportion of the peer table and I need to check
2927 * every connection 'n' times within a timeout
2928 * interval, to ensure I detect a timeout on any
2929 * connection within (n+1)/n times the timeout
2932 if (*kiblnd_tunables.kib_timeout > n * p)
2933 chunk = (chunk * n * p) /
2934 *kiblnd_tunables.kib_timeout;
2938 for (i = 0; i < chunk; i++) {
2939 kiblnd_check_conns(peer_index);
2940 peer_index = (peer_index + 1) %
2941 kiblnd_data.kib_peer_hash_size;
2945 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2951 /* Nothing to do for 'timeout' */
2952 set_current_state (TASK_INTERRUPTIBLE);
2953 add_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2954 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2956 schedule_timeout (timeout);
2958 set_current_state (TASK_RUNNING);
2959 remove_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2960 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2963 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2965 kiblnd_thread_fini();
2970 kiblnd_qp_event(struct ib_event *event, void *arg)
2972 kib_conn_t *conn = arg;
2974 switch (event->event) {
2975 case IB_EVENT_COMM_EST:
2976 CDEBUG(D_NET, "%s established\n",
2977 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2981 CERROR("%s: Async QP event type %d\n",
2982 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
2988 kiblnd_complete (struct ib_wc *wc)
2990 switch (kiblnd_wreqid2type(wc->wr_id)) {
2994 case IBLND_WID_RDMA:
2995 /* We only get RDMA completion notification if it fails. All
2996 * subsequent work items, including the final SEND will fail
2997 * too. However we can't print out any more info about the
2998 * failing RDMA because 'tx' might be back on the idle list or
2999 * even reused already if we didn't manage to post all our work
3001 CDEBUG(D_NETERROR, "RDMA (tx: %p) failed: %d\n",
3002 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3006 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3010 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3017 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3019 /* NB I'm not allowed to schedule this conn once its refcount has
3020 * reached 0. Since fundamentally I'm racing with scheduler threads
3021 * consuming my CQ I could be called after all completions have
3022 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3023 * and this CQ is about to be destroyed so I NOOP. */
3024 kib_conn_t *conn = (kib_conn_t *)arg;
3025 unsigned long flags;
3027 LASSERT (cq == conn->ibc_cq);
3029 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3031 conn->ibc_ready = 1;
3033 if (!conn->ibc_scheduled &&
3034 (conn->ibc_nrx > 0 ||
3035 conn->ibc_nsends_posted > 0)) {
3036 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3037 conn->ibc_scheduled = 1;
3038 list_add_tail(&conn->ibc_sched_list,
3039 &kiblnd_data.kib_sched_conns);
3040 wake_up(&kiblnd_data.kib_sched_waitq);
3043 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3047 kiblnd_cq_event(struct ib_event *event, void *arg)
3049 kib_conn_t *conn = arg;
3051 CERROR("%s: async CQ event type %d\n",
3052 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3056 kiblnd_scheduler(void *arg)
3058 long id = (long)arg;
3061 unsigned long flags;
3068 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3069 cfs_daemonize(name);
3070 cfs_block_allsigs();
3072 init_waitqueue_entry(&wait, current);
3074 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3076 while (!kiblnd_data.kib_shutdown) {
3077 if (busy_loops++ >= IBLND_RESCHED) {
3078 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3084 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3089 if (!list_empty(&kiblnd_data.kib_sched_conns)) {
3090 conn = list_entry(kiblnd_data.kib_sched_conns.next,
3091 kib_conn_t, ibc_sched_list);
3092 /* take over kib_sched_conns' ref on conn... */
3093 LASSERT(conn->ibc_scheduled);
3094 list_del(&conn->ibc_sched_list);
3095 conn->ibc_ready = 0;
3097 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3100 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3102 rc = ib_req_notify_cq(conn->ibc_cq,
3106 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3111 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3114 if (rc != 0 || conn->ibc_ready) {
3115 /* There may be another completion waiting; get
3116 * another scheduler to check while I handle
3118 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3119 list_add_tail(&conn->ibc_sched_list,
3120 &kiblnd_data.kib_sched_conns);
3121 wake_up(&kiblnd_data.kib_sched_waitq);
3123 conn->ibc_scheduled = 0;
3127 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3130 kiblnd_complete(&wc);
3132 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3136 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3143 set_current_state(TASK_INTERRUPTIBLE);
3144 add_wait_queue_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3145 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3150 remove_wait_queue(&kiblnd_data.kib_sched_waitq, &wait);
3151 set_current_state(TASK_RUNNING);
3152 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3155 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3157 kiblnd_thread_fini();