1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Eric Barton <eric@bartonsoftware.com>
6 * Author: Frank Zago <fzago@systemfabricworks.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 kibnal_tx_done (kib_tx_t *tx)
30 ptl_err_t ptlrc = (tx->tx_status == 0) ? PTL_OK : PTL_FAIL;
33 LASSERT (!in_interrupt());
34 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
35 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
36 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
39 switch (tx->tx_mapped) {
49 vvrc = vv_mem_region_destroy(kibnal_data.kib_hca,
51 LASSERT (vvrc == vv_return_ok);
52 tx->tx_mapped = KIB_TX_UNMAPPED;
57 for (i = 0; i < 2; i++) {
58 /* tx may have up to 2 libmsgs to finalise */
59 if (tx->tx_libmsg[i] == NULL)
62 lib_finalize (&kibnal_lib, NULL, tx->tx_libmsg[i], ptlrc);
63 tx->tx_libmsg[i] = NULL;
66 if (tx->tx_conn != NULL) {
67 kibnal_conn_decref(tx->tx_conn);
74 spin_lock(&kibnal_data.kib_tx_lock);
77 list_add_tail (&tx->tx_list, &kibnal_data.kib_idle_nblk_txs);
79 list_add_tail (&tx->tx_list, &kibnal_data.kib_idle_txs);
80 wake_up (&kibnal_data.kib_idle_tx_waitq);
83 spin_unlock(&kibnal_data.kib_tx_lock);
87 kibnal_get_idle_tx (int may_block)
93 spin_lock(&kibnal_data.kib_tx_lock);
95 /* "normal" descriptor is free */
96 if (!list_empty (&kibnal_data.kib_idle_txs)) {
97 tx = list_entry (kibnal_data.kib_idle_txs.next,
103 /* may dip into reserve pool */
104 if (list_empty (&kibnal_data.kib_idle_nblk_txs)) {
105 CERROR ("reserved tx desc pool exhausted\n");
109 tx = list_entry (kibnal_data.kib_idle_nblk_txs.next,
114 /* block for idle tx */
115 spin_unlock(&kibnal_data.kib_tx_lock);
117 wait_event (kibnal_data.kib_idle_tx_waitq,
118 !list_empty (&kibnal_data.kib_idle_txs) ||
119 kibnal_data.kib_shutdown);
123 list_del (&tx->tx_list);
125 /* Allocate a new completion cookie. It might not be needed,
126 * but we've got a lock right now and we're unlikely to
128 tx->tx_cookie = kibnal_data.kib_next_tx_cookie++;
130 LASSERT (tx->tx_mapped == KIB_TX_UNMAPPED);
132 LASSERT (tx->tx_nwrq == 0);
133 LASSERT (!tx->tx_queued);
134 LASSERT (tx->tx_sending == 0);
135 LASSERT (!tx->tx_waiting);
136 LASSERT (tx->tx_status == 0);
137 LASSERT (tx->tx_conn == NULL);
138 LASSERT (tx->tx_libmsg[0] == NULL);
139 LASSERT (tx->tx_libmsg[1] == NULL);
142 spin_unlock(&kibnal_data.kib_tx_lock);
148 kibnal_post_rx (kib_rx_t *rx, int credit)
150 kib_conn_t *conn = rx->rx_conn;
154 LASSERT (!in_interrupt());
156 rx->rx_gl = (vv_scatgat_t) {
157 .v_address = (void *)((unsigned long)KIBNAL_RX_VADDR(rx)),
158 .l_key = KIBNAL_RX_LKEY(rx),
159 .length = IBNAL_MSG_SIZE,
162 rx->rx_wrq = (vv_wr_t) {
163 .wr_id = kibnal_ptr2wreqid(rx, IBNAL_WID_RX),
164 .completion_notification = 1,
165 .scatgat_list = &rx->rx_gl,
166 .num_of_data_segments = 1,
167 .wr_type = vv_wr_receive,
170 LASSERT (conn->ibc_state >= IBNAL_CONN_INIT);
171 LASSERT (!rx->rx_posted);
173 CDEBUG(D_NET, "posting rx [%d %x %p]\n",
174 rx->rx_wrq.scatgat_list->length,
175 rx->rx_wrq.scatgat_list->l_key,
176 rx->rx_wrq.scatgat_list->v_address);
178 if (conn->ibc_state > IBNAL_CONN_ESTABLISHED) {
179 /* No more posts for this rx; so lose its ref */
180 kibnal_conn_decref(conn);
186 spin_lock(&conn->ibc_lock);
187 /* Serialise vv_post_receive; it's not re-entrant on the same QP */
188 vvrc = vv_post_receive(kibnal_data.kib_hca,
189 conn->ibc_qp, &rx->rx_wrq);
190 spin_unlock(&conn->ibc_lock);
194 spin_lock(&conn->ibc_lock);
195 conn->ibc_outstanding_credits++;
196 spin_unlock(&conn->ibc_lock);
198 kibnal_check_sends(conn);
203 CERROR ("post rx -> "LPX64" failed %d\n",
204 conn->ibc_peer->ibp_nid, vvrc);
206 kibnal_close_conn(rx->rx_conn, rc);
207 /* No more posts for this rx; so lose its ref */
208 kibnal_conn_decref(conn);
213 kibnal_post_receives (kib_conn_t *conn)
218 LASSERT (conn->ibc_state < IBNAL_CONN_ESTABLISHED);
219 LASSERT (conn->ibc_comms_error == 0);
221 for (i = 0; i < IBNAL_RX_MSGS; i++) {
222 /* +1 ref for rx desc. This ref remains until kibnal_post_rx
223 * fails (i.e. actual failure or we're disconnecting) */
224 kibnal_conn_addref(conn);
225 rc = kibnal_post_rx (&conn->ibc_rxs[i], 0);
234 kibnal_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
236 struct list_head *tmp;
238 list_for_each(tmp, &conn->ibc_active_txs) {
239 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
241 LASSERT (!tx->tx_queued);
242 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
244 if (tx->tx_cookie != cookie)
247 if (tx->tx_waiting &&
248 tx->tx_msg->ibm_type == txtype)
251 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
252 tx->tx_waiting ? "" : "NOT ",
253 tx->tx_msg->ibm_type, txtype);
259 kibnal_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
264 spin_lock(&conn->ibc_lock);
266 tx = kibnal_find_waiting_tx_locked(conn, txtype, cookie);
268 spin_unlock(&conn->ibc_lock);
270 CWARN("Unmatched completion type %x cookie "LPX64
272 txtype, cookie, conn->ibc_peer->ibp_nid);
273 kibnal_close_conn (conn, -EPROTO);
277 if (tx->tx_status == 0) { /* success so far */
278 if (status < 0) { /* failed? */
279 tx->tx_status = status;
280 } else if (txtype == IBNAL_MSG_GET_REQ) {
281 /* XXX layering violation: set REPLY data length */
282 LASSERT (tx->tx_libmsg[1] != NULL);
283 LASSERT (tx->tx_libmsg[1]->ev.type ==
284 PTL_EVENT_REPLY_END);
286 tx->tx_libmsg[1]->ev.mlength = status;
292 idle = !tx->tx_queued && (tx->tx_sending == 0);
294 list_del(&tx->tx_list);
296 spin_unlock(&conn->ibc_lock);
303 kibnal_send_completion (kib_conn_t *conn, int type, int status, __u64 cookie)
305 kib_tx_t *tx = kibnal_get_idle_tx(0);
308 CERROR("Can't get tx for completion %x for "LPX64"\n",
309 type, conn->ibc_peer->ibp_nid);
313 tx->tx_msg->ibm_u.completion.ibcm_status = status;
314 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
315 kibnal_init_tx_msg(tx, type, sizeof(kib_completion_msg_t));
317 kibnal_queue_tx(tx, conn);
321 kibnal_handle_rx (kib_rx_t *rx)
323 kib_msg_t *msg = rx->rx_msg;
324 kib_conn_t *conn = rx->rx_conn;
325 int credits = msg->ibm_credits;
329 LASSERT (conn->ibc_state >= IBNAL_CONN_ESTABLISHED);
331 CDEBUG (D_NET, "Received %x[%d] from "LPX64"\n",
332 msg->ibm_type, credits, conn->ibc_peer->ibp_nid);
335 /* Have I received credits that will let me send? */
336 spin_lock(&conn->ibc_lock);
337 conn->ibc_credits += credits;
338 spin_unlock(&conn->ibc_lock);
340 kibnal_check_sends(conn);
343 switch (msg->ibm_type) {
345 CERROR("Bad IBNAL message type %x from "LPX64"\n",
346 msg->ibm_type, conn->ibc_peer->ibp_nid);
352 case IBNAL_MSG_IMMEDIATE:
353 lib_parse(&kibnal_lib, &msg->ibm_u.immediate.ibim_hdr, rx);
356 case IBNAL_MSG_PUT_REQ:
357 rx->rx_responded = 0;
358 lib_parse(&kibnal_lib, &msg->ibm_u.putreq.ibprm_hdr, rx);
359 if (rx->rx_responded)
362 /* I wasn't asked to transfer any payload data. This happens
363 * if the PUT didn't match, or got truncated. */
364 kibnal_send_completion(rx->rx_conn, IBNAL_MSG_PUT_NAK, 0,
365 msg->ibm_u.putreq.ibprm_cookie);
368 case IBNAL_MSG_PUT_NAK:
369 CWARN ("PUT_NACK from "LPX64"\n", conn->ibc_peer->ibp_nid);
370 kibnal_handle_completion(conn, IBNAL_MSG_PUT_REQ,
371 msg->ibm_u.completion.ibcm_status,
372 msg->ibm_u.completion.ibcm_cookie);
375 case IBNAL_MSG_PUT_ACK:
376 spin_lock(&conn->ibc_lock);
377 tx = kibnal_find_waiting_tx_locked(conn, IBNAL_MSG_PUT_REQ,
378 msg->ibm_u.putack.ibpam_src_cookie);
380 list_del(&tx->tx_list);
381 spin_unlock(&conn->ibc_lock);
384 CERROR("Unmatched PUT_ACK from "LPX64"\n",
385 conn->ibc_peer->ibp_nid);
386 kibnal_close_conn(conn, -EPROTO);
390 LASSERT (tx->tx_waiting);
391 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
392 * (a) I can overwrite tx_msg since my peer has received it!
393 * (b) tx_waiting set tells tx_complete() it's not done. */
395 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
397 rc = kibnal_init_rdma(tx, IBNAL_MSG_PUT_DONE,
398 kibnal_rd_size(&msg->ibm_u.putack.ibpam_rd),
399 &msg->ibm_u.putack.ibpam_rd,
400 msg->ibm_u.putack.ibpam_dst_cookie);
402 CERROR("Can't setup rdma for PUT to "LPX64": %d\n",
403 conn->ibc_peer->ibp_nid, rc);
405 spin_lock(&conn->ibc_lock);
406 if (tx->tx_status == 0 && rc < 0)
408 tx->tx_waiting = 0; /* clear waiting and queue atomically */
409 kibnal_queue_tx_locked(tx, conn);
410 spin_unlock(&conn->ibc_lock);
413 case IBNAL_MSG_PUT_DONE:
414 kibnal_handle_completion(conn, IBNAL_MSG_PUT_ACK,
415 msg->ibm_u.completion.ibcm_status,
416 msg->ibm_u.completion.ibcm_cookie);
419 case IBNAL_MSG_GET_REQ:
420 rx->rx_responded = 0;
421 lib_parse(&kibnal_lib, &msg->ibm_u.get.ibgm_hdr, rx);
422 if (rx->rx_responded) /* I responded to the GET_REQ */
424 /* NB GET didn't match (I'd have responded even with no payload
426 kibnal_send_completion(rx->rx_conn, IBNAL_MSG_GET_DONE, -ENODATA,
427 msg->ibm_u.get.ibgm_cookie);
430 case IBNAL_MSG_GET_DONE:
431 kibnal_handle_completion(conn, IBNAL_MSG_GET_REQ,
432 msg->ibm_u.completion.ibcm_status,
433 msg->ibm_u.completion.ibcm_cookie);
437 kibnal_post_rx(rx, 1);
441 kibnal_rx_complete (kib_rx_t *rx, vv_comp_status_t vvrc, int nob, __u64 rxseq)
443 kib_msg_t *msg = rx->rx_msg;
444 kib_conn_t *conn = rx->rx_conn;
448 CDEBUG (D_NET, "rx %p conn %p\n", rx, conn);
449 LASSERT (rx->rx_posted);
452 if (conn->ibc_state > IBNAL_CONN_ESTABLISHED)
455 if (vvrc != vv_comp_status_success) {
456 CERROR("Rx from "LPX64" failed: %d\n",
457 conn->ibc_peer->ibp_nid, vvrc);
461 rc = kibnal_unpack_msg(msg, nob);
463 CERROR ("Error %d unpacking rx from "LPX64"\n",
464 rc, conn->ibc_peer->ibp_nid);
468 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
469 msg->ibm_srcstamp != conn->ibc_incarnation ||
470 msg->ibm_dstnid != kibnal_lib.libnal_ni.ni_pid.nid ||
471 msg->ibm_dststamp != kibnal_data.kib_incarnation) {
472 CERROR ("Stale rx from "LPX64"\n",
473 conn->ibc_peer->ibp_nid);
477 if (msg->ibm_seq != rxseq) {
478 CERROR ("Out-of-sequence rx from "LPX64
479 ": got "LPD64" but expected "LPD64"\n",
480 conn->ibc_peer->ibp_nid, msg->ibm_seq, rxseq);
484 /* racing with connection establishment/teardown! */
486 if (conn->ibc_state < IBNAL_CONN_ESTABLISHED) {
487 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
488 /* must check holding global lock to eliminate race */
489 if (conn->ibc_state < IBNAL_CONN_ESTABLISHED) {
490 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
491 write_unlock_irqrestore(&kibnal_data.kib_global_lock,
495 write_unlock_irqrestore(&kibnal_data.kib_global_lock,
498 kibnal_handle_rx(rx);
502 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
503 kibnal_close_conn(conn, -EIO);
505 /* Don't re-post rx & drop its ref on conn */
506 kibnal_conn_decref(conn);
511 kibnal_append_rdfrag(kib_rdma_desc_t *rd, int active, struct page *page,
512 unsigned long page_offset, unsigned long len)
514 kib_rdma_frag_t *frag = &rd->rd_frags[rd->rd_nfrag];
520 vv_mem_reg_h_t mem_h;
523 if (rd->rd_nfrag >= IBNAL_MAX_RDMA_FRAGS) {
524 CERROR ("Too many RDMA fragments\n");
529 # error "This probably doesn't work because of over/underflow when casting between __u64 and void *..."
531 /* Try to create an address that adapter-tavor will munge into a valid
532 * network address, given how it maps all phys mem into 1 region */
533 addr = page_to_phys(page) + page_offset + PAGE_OFFSET;
535 vvrc = vv_get_gen_mr_attrib(kibnal_data.kib_hca,
536 (void *)((unsigned long)addr),
537 len, &mem_h, &l_key, &r_key);
538 LASSERT (vvrc == vv_return_ok);
541 if (rd->rd_nfrag == 0) {
543 } else if (l_key != rd->rd_key) {
544 CERROR ("> 1 key for single RDMA desc\n");
549 if (rd->rd_nfrag == 0) {
551 } else if (r_key != rd->rd_key) {
552 CERROR ("> 1 key for single RDMA desc\n");
555 vv_va2advertise_addr(kibnal_data.kib_hca,
556 (void *)((unsigned long)addr), &ptr);
557 frag_addr = (unsigned long)ptr;
560 kibnal_rf_set(frag, frag_addr, len);
562 CDEBUG(D_NET,"map frag [%d][%d %x %08x%08x] "LPX64"\n",
563 rd->rd_nfrag, frag->rf_nob, rd->rd_key,
564 frag->rf_addr_hi, frag->rf_addr_lo, frag_addr);
571 kibnal_kvaddr_to_page (unsigned long vaddr)
575 if (vaddr >= VMALLOC_START &&
577 page = vmalloc_to_page ((void *)vaddr);
579 else if (vaddr >= PKMAP_BASE &&
580 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE))
581 page = vmalloc_to_page ((void *)vaddr);
582 /* in 2.4 ^ just walks the page tables */
585 page = virt_to_page (vaddr);
587 return VALID_PAGE(page) ? page : NULL;
591 kibnal_setup_rd_iov(kib_tx_t *tx, kib_rdma_desc_t *rd,
592 vv_access_con_bit_mask_t access,
593 int niov, struct iovec *iov, int offset, int nob)
596 /* active if I'm sending */
597 int active = ((access & vv_acc_r_mem_write) == 0);
606 LASSERT ((rd != tx->tx_rd) == !active);
608 while (offset >= iov->iov_len) {
609 offset -= iov->iov_len;
619 vaddr = ((unsigned long)iov->iov_base) + offset;
620 page_offset = vaddr & (PAGE_SIZE - 1);
621 page = kibnal_kvaddr_to_page(vaddr);
623 CERROR ("Can't find page\n");
627 fragnob = min((int)(iov->iov_len - offset), nob);
628 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
630 rc = kibnal_append_rdfrag(rd, active, page,
631 page_offset, fragnob);
635 if (offset + fragnob < iov->iov_len) {
649 kibnal_setup_rd_kiov (kib_tx_t *tx, kib_rdma_desc_t *rd,
650 vv_access_con_bit_mask_t access,
651 int nkiov, ptl_kiov_t *kiov, int offset, int nob)
653 /* active if I'm sending */
654 int active = ((access & vv_acc_r_mem_write) == 0);
658 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
662 LASSERT ((rd != tx->tx_rd) == !active);
664 while (offset >= kiov->kiov_len) {
665 offset -= kiov->kiov_len;
674 fragnob = min((int)(kiov->kiov_len - offset), nob);
676 rc = kibnal_append_rdfrag(rd, active, kiov->kiov_page,
677 kiov->kiov_offset + offset,
692 kibnal_setup_rd_iov (kib_tx_t *tx, kib_rdma_desc_t *rd,
693 vv_access_con_bit_mask_t access,
694 int niov, struct iovec *iov, int offset, int nob)
697 /* active if I'm sending */
698 int active = ((access & vv_acc_r_mem_write) == 0);
704 LASSERT (tx->tx_mapped == KIB_TX_UNMAPPED);
705 LASSERT ((rd != tx->tx_rd) == !active);
707 while (offset >= iov->iov_len) {
708 offset -= iov->iov_len;
714 if (nob > iov->iov_len - offset) {
715 CERROR ("Can't map multiple vaddr fragments\n");
719 vaddr = (void *)(((unsigned long)iov->iov_base) + offset);
720 tx->tx_md.md_addr = (__u64)((unsigned long)vaddr);
722 vvrc = vv_mem_region_register(kibnal_data.kib_hca, vaddr, nob,
723 kibnal_data.kib_pd, access,
724 &tx->tx_md.md_handle,
727 if (vvrc != vv_return_ok) {
728 CERROR ("Can't map vaddr %p: %d\n", vaddr, vvrc);
732 tx->tx_mapped = KIB_TX_MAPPED;
734 rd->rd_key = active ? tx->tx_md.md_lkey : tx->tx_md.md_rkey;
736 kibnal_rf_set(&rd->rd_frags[0], tx->tx_md.md_addr, nob);
742 kibnal_setup_rd_kiov (kib_tx_t *tx, kib_rdma_desc_t *rd,
743 vv_access_con_bit_mask_t access,
744 int nkiov, ptl_kiov_t *kiov, int offset, int nob)
746 /* active if I'm sending */
747 int active = ((access & vv_acc_r_mem_write) == 0);
749 vv_phy_list_t phys_pages;
757 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
761 LASSERT (tx->tx_mapped == KIB_TX_UNMAPPED);
762 LASSERT ((rd != tx->tx_rd) == !active);
764 while (offset >= kiov->kiov_len) {
765 offset -= kiov->kiov_len;
771 phys_size = nkiov * sizeof (*phys);
772 PORTAL_ALLOC(phys, phys_size);
774 CERROR ("Can't allocate tmp phys\n");
778 page_offset = kiov->kiov_offset + offset;
780 phys[0].start = kibnal_page2phys(kiov->kiov_page);
781 phys[0].size = PAGE_SIZE;
784 resid = nob - (kiov->kiov_len - offset);
791 if (kiov->kiov_offset != 0 ||
792 ((resid > PAGE_SIZE) &&
793 kiov->kiov_len < PAGE_SIZE)) {
795 /* Can't have gaps */
796 CERROR ("Can't make payload contiguous in I/O VM:"
797 "page %d, offset %d, len %d \n", nphys,
798 kiov->kiov_offset, kiov->kiov_len);
800 for (i = -nphys; i < nkiov; i++)
801 CERROR("kiov[%d] %p +%d for %d\n",
802 i, kiov[i].kiov_page,
810 LASSERT (nphys * sizeof (*phys) < phys_size);
811 phys[nphys].start = kibnal_page2phys(kiov->kiov_page);
812 phys[nphys].size = PAGE_SIZE;
819 CWARN ("nphys %d, nob %d, page_offset %d\n", nphys, nob, page_offset);
820 for (i = 0; i < nphys; i++)
821 CWARN (" [%d] "LPX64"\n", i, phys[i]);
824 vvrc = vv_phy_mem_region_register(kibnal_data.kib_hca,
831 &tx->tx_md.md_handle,
836 if (vvrc != vv_return_ok) {
837 CERROR ("Can't map phys: %d\n", vvrc);
842 CDEBUG(D_NET, "Mapped %d pages %d bytes @ offset %d: "
843 "lkey %x, rkey %x, addr "LPX64"\n",
844 nphys, nob, page_offset, tx->tx_md.md_lkey, tx->tx_md.md_rkey,
847 tx->tx_mapped = KIB_TX_MAPPED;
850 rd->rd_key = active ? tx->tx_md.md_lkey : tx->tx_md.md_rkey;
852 kibnal_rf_set(&rd->rd_frags[0], tx->tx_md.md_addr, nob);
855 PORTAL_FREE(phys, phys_size);
861 kibnal_find_conn_locked (kib_peer_t *peer)
863 struct list_head *tmp;
865 /* just return the first connection */
866 list_for_each (tmp, &peer->ibp_conns) {
867 return (list_entry(tmp, kib_conn_t, ibc_list));
874 kibnal_check_sends (kib_conn_t *conn)
882 /* Don't send anything until after the connection is established */
883 if (conn->ibc_state < IBNAL_CONN_ESTABLISHED) {
884 CDEBUG(D_NET, LPX64"too soon\n", conn->ibc_peer->ibp_nid);
888 spin_lock(&conn->ibc_lock);
890 LASSERT (conn->ibc_nsends_posted <= IBNAL_MSG_QUEUE_SIZE);
892 if (list_empty(&conn->ibc_tx_queue) &&
893 conn->ibc_outstanding_credits >= IBNAL_CREDIT_HIGHWATER) {
894 spin_unlock(&conn->ibc_lock);
896 tx = kibnal_get_idle_tx(0); /* don't block */
898 kibnal_init_tx_msg(tx, IBNAL_MSG_NOOP, 0);
900 spin_lock(&conn->ibc_lock);
903 kibnal_queue_tx_locked(tx, conn);
906 while (!list_empty (&conn->ibc_tx_queue)) {
907 tx = list_entry (conn->ibc_tx_queue.next, kib_tx_t, tx_list);
909 LASSERT (tx->tx_queued);
910 /* We rely on this for QP sizing */
911 LASSERT (tx->tx_nwrq > 0 && tx->tx_nwrq <= 1 + IBNAL_MAX_RDMA_FRAGS);
913 LASSERT (conn->ibc_outstanding_credits >= 0);
914 LASSERT (conn->ibc_outstanding_credits <= IBNAL_MSG_QUEUE_SIZE);
915 LASSERT (conn->ibc_credits >= 0);
916 LASSERT (conn->ibc_credits <= IBNAL_MSG_QUEUE_SIZE);
918 if (conn->ibc_nsends_posted == IBNAL_MSG_QUEUE_SIZE) {
919 CDEBUG(D_NET, LPX64": posted enough\n",
920 conn->ibc_peer->ibp_nid);
924 if (conn->ibc_credits == 0) { /* no credits */
925 CDEBUG(D_NET, LPX64": no credits\n",
926 conn->ibc_peer->ibp_nid);
930 if (conn->ibc_credits == 1 && /* last credit reserved for */
931 conn->ibc_outstanding_credits == 0) { /* giving back credits */
932 CDEBUG(D_NET, LPX64": not using last credit\n",
933 conn->ibc_peer->ibp_nid);
937 list_del (&tx->tx_list);
940 /* NB don't drop ibc_lock before bumping tx_sending */
942 if (tx->tx_msg->ibm_type == IBNAL_MSG_NOOP &&
943 (!list_empty(&conn->ibc_tx_queue) ||
944 conn->ibc_outstanding_credits < IBNAL_CREDIT_HIGHWATER)) {
946 spin_unlock(&conn->ibc_lock);
948 spin_lock(&conn->ibc_lock);
949 CDEBUG(D_NET, LPX64": redundant noop\n",
950 conn->ibc_peer->ibp_nid);
954 kibnal_pack_msg(tx->tx_msg, conn->ibc_outstanding_credits,
955 conn->ibc_peer->ibp_nid, conn->ibc_incarnation,
959 conn->ibc_outstanding_credits = 0;
960 conn->ibc_nsends_posted++;
963 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
964 * PUT. If so, it was first queued here as a PUT_REQ, sent and
965 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
966 * and then re-queued here. It's (just) possible that
967 * tx_sending is non-zero if we've not done the tx_complete() from
968 * the first send; hence the ++ rather than = below. */
971 list_add (&tx->tx_list, &conn->ibc_active_txs);
973 /* Keep holding ibc_lock while posting sends on this
974 * connection; vv_post_send() isn't re-entrant on the same
977 LASSERT (tx->tx_nwrq > 0);
981 if (conn->ibc_state == IBNAL_CONN_ESTABLISHED) {
983 vvrc = vv_post_send_list(kibnal_data.kib_hca,
987 vv_operation_type_send_rc);
988 rc = (vvrc == vv_return_ok) ? 0 : -EIO;
992 /* NB credits are transferred in the actual
993 * message, which can only be the last work item */
994 conn->ibc_outstanding_credits += tx->tx_msg->ibm_credits;
996 conn->ibc_nsends_posted--;
1002 done = (tx->tx_sending == 0);
1004 list_del (&tx->tx_list);
1006 spin_unlock(&conn->ibc_lock);
1008 if (conn->ibc_state == IBNAL_CONN_ESTABLISHED)
1009 CERROR ("Error %d posting transmit to "LPX64"\n",
1010 vvrc, conn->ibc_peer->ibp_nid);
1012 CDEBUG (D_NET, "Error %d posting transmit to "
1013 LPX64"\n", rc, conn->ibc_peer->ibp_nid);
1015 kibnal_close_conn (conn, rc);
1018 kibnal_tx_done (tx);
1023 spin_unlock(&conn->ibc_lock);
1027 kibnal_tx_complete (kib_tx_t *tx, vv_comp_status_t vvrc)
1029 kib_conn_t *conn = tx->tx_conn;
1030 int failed = (vvrc != vv_comp_status_success);
1033 CDEBUG(D_NET, "tx %p conn %p sending %d nwrq %d vvrc %d\n",
1034 tx, conn, tx->tx_sending, tx->tx_nwrq, vvrc);
1036 LASSERT (tx->tx_sending > 0);
1039 tx->tx_status == 0 &&
1040 conn->ibc_state == IBNAL_CONN_ESTABLISHED)
1041 CERROR("tx -> "LPX64" type %x cookie "LPX64
1042 "sending %d waiting %d: failed %d\n",
1043 conn->ibc_peer->ibp_nid, tx->tx_msg->ibm_type,
1044 tx->tx_cookie, tx->tx_sending, tx->tx_waiting, vvrc);
1046 spin_lock(&conn->ibc_lock);
1048 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1049 * gets to free it, which also drops its ref on 'conn'. */
1052 conn->ibc_nsends_posted--;
1056 tx->tx_status = -EIO;
1059 idle = (tx->tx_sending == 0) && /* This is the final callback */
1060 !tx->tx_waiting && /* Not waiting for peer */
1061 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1063 list_del(&tx->tx_list);
1065 kibnal_conn_addref(conn); /* 1 ref for me.... */
1067 spin_unlock(&conn->ibc_lock);
1070 kibnal_tx_done (tx);
1073 kibnal_close_conn (conn, -EIO);
1075 kibnal_check_sends(conn);
1077 kibnal_conn_decref(conn); /* ...until here */
1081 kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob)
1083 vv_scatgat_t *gl = &tx->tx_gl[tx->tx_nwrq];
1084 vv_wr_t *wrq = &tx->tx_wrq[tx->tx_nwrq];
1085 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1087 LASSERT (tx->tx_nwrq >= 0 &&
1088 tx->tx_nwrq < (1 + IBNAL_MAX_RDMA_FRAGS));
1089 LASSERT (nob <= IBNAL_MSG_SIZE);
1091 kibnal_init_msg(tx->tx_msg, type, body_nob);
1093 *gl = (vv_scatgat_t) {
1094 .v_address = (void *)((unsigned long)KIBNAL_TX_VADDR(tx)),
1095 .l_key = KIBNAL_TX_LKEY(tx),
1099 memset(wrq, 0, sizeof(*wrq));
1101 wrq->wr_id = kibnal_ptr2wreqid(tx, IBNAL_WID_TX);
1102 wrq->wr_type = vv_wr_send;
1103 wrq->scatgat_list = gl;
1104 wrq->num_of_data_segments = 1;
1105 wrq->completion_notification = 1;
1106 wrq->type.send.solicited_event = 1;
1107 wrq->type.send.immidiate_data_indicator = 0;
1108 wrq->type.send.send_qp_type.rc_type.fance_indicator = 0;
1114 kibnal_init_rdma (kib_tx_t *tx, int type, int nob,
1115 kib_rdma_desc_t *dstrd, __u64 dstcookie)
1117 /* CAVEAT EMPTOR: this 'consumes' the frags in 'dstrd' */
1119 kib_msg_t *ibmsg = tx->tx_msg;
1120 kib_rdma_desc_t *srcrd = tx->tx_rd;
1121 kib_rdma_frag_t *srcfrag;
1123 kib_rdma_frag_t *dstfrag;
1130 /* Called by scheduler */
1131 LASSERT (!in_interrupt());
1133 LASSERT (type == IBNAL_MSG_GET_DONE ||
1134 type == IBNAL_MSG_PUT_DONE);
1136 srcidx = dstidx = 0;
1137 srcfrag = &srcrd->rd_frags[0];
1138 dstfrag = &dstrd->rd_frags[0];
1142 if (srcidx >= srcrd->rd_nfrag) {
1143 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1148 if (dstidx == dstrd->rd_nfrag) {
1149 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1154 if (tx->tx_nwrq == IBNAL_MAX_RDMA_FRAGS) {
1155 CERROR("RDMA too fragmented: %d/%d src %d/%d dst frags\n",
1156 srcidx, srcrd->rd_nfrag,
1157 dstidx, dstrd->rd_nfrag);
1162 wrknob = MIN(MIN(srcfrag->rf_nob, dstfrag->rf_nob), resid);
1164 gl = &tx->tx_gl[tx->tx_nwrq];
1165 gl->v_address = (void *)((unsigned long)kibnal_rf_addr(srcfrag));
1166 gl->length = wrknob;
1167 gl->l_key = srcrd->rd_key;
1169 wrq = &tx->tx_wrq[tx->tx_nwrq];
1171 wrq->wr_id = kibnal_ptr2wreqid(tx, IBNAL_WID_RDMA);
1172 wrq->completion_notification = 0;
1173 wrq->scatgat_list = gl;
1174 wrq->num_of_data_segments = 1;
1175 wrq->wr_type = vv_wr_rdma_write;
1176 wrq->type.send.solicited_event = 0;
1177 wrq->type.send.send_qp_type.rc_type.fance_indicator = 0;
1178 wrq->type.send.send_qp_type.rc_type.r_addr = kibnal_rf_addr(dstfrag);
1179 wrq->type.send.send_qp_type.rc_type.r_r_key = dstrd->rd_key;
1182 if (wrknob < srcfrag->rf_nob) {
1183 kibnal_rf_set(srcfrag,
1184 kibnal_rf_addr(srcfrag) + resid,
1185 srcfrag->rf_nob - wrknob);
1191 if (wrknob < dstfrag->rf_nob) {
1192 kibnal_rf_set(dstfrag,
1193 kibnal_rf_addr(dstfrag) + resid,
1194 dstfrag->rf_nob - wrknob);
1203 if (rc < 0) /* no RDMA if completing with failure */
1206 ibmsg->ibm_u.completion.ibcm_status = rc;
1207 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1208 kibnal_init_tx_msg(tx, type, sizeof (kib_completion_msg_t));
1214 kibnal_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1216 spin_lock(&conn->ibc_lock);
1217 kibnal_queue_tx_locked (tx, conn);
1218 spin_unlock(&conn->ibc_lock);
1220 kibnal_check_sends(conn);
1224 kibnal_launch_tx (kib_tx_t *tx, ptl_nid_t nid)
1228 unsigned long flags;
1229 rwlock_t *g_lock = &kibnal_data.kib_global_lock;
1231 /* If I get here, I've committed to send, so I complete the tx with
1232 * failure on any problems */
1234 LASSERT (tx->tx_conn == NULL); /* only set when assigned a conn */
1235 LASSERT (tx->tx_nwrq > 0); /* work items have been set up */
1237 read_lock_irqsave(g_lock, flags);
1239 peer = kibnal_find_peer_locked (nid);
1241 read_unlock_irqrestore(g_lock, flags);
1242 tx->tx_status = -EHOSTUNREACH;
1244 kibnal_tx_done (tx);
1248 conn = kibnal_find_conn_locked (peer);
1250 kibnal_conn_addref(conn); /* 1 ref for me... */
1251 read_unlock_irqrestore(g_lock, flags);
1253 kibnal_queue_tx (tx, conn);
1254 kibnal_conn_decref(conn); /* ...to here */
1258 /* Making one or more connections; I'll need a write lock... */
1259 read_unlock(g_lock);
1262 peer = kibnal_find_peer_locked (nid);
1264 write_unlock_irqrestore(g_lock, flags);
1265 tx->tx_status = -EHOSTUNREACH;
1267 kibnal_tx_done (tx);
1271 conn = kibnal_find_conn_locked (peer);
1273 /* Connection exists; queue message on it */
1274 kibnal_conn_addref(conn); /* 1 ref for me... */
1275 write_unlock_irqrestore(g_lock, flags);
1277 kibnal_queue_tx (tx, conn);
1278 kibnal_conn_decref(conn); /* ...until here */
1282 if (peer->ibp_connecting == 0) {
1283 if (!time_after_eq(jiffies, peer->ibp_reconnect_time)) {
1284 write_unlock_irqrestore(g_lock, flags);
1285 tx->tx_status = -EHOSTUNREACH;
1287 kibnal_tx_done (tx);
1291 peer->ibp_connecting = 1;
1292 kibnal_peer_addref(peer); /* extra ref for connd */
1294 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
1296 list_add_tail (&peer->ibp_connd_list,
1297 &kibnal_data.kib_connd_peers);
1298 wake_up (&kibnal_data.kib_connd_waitq);
1300 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
1303 /* A connection is being established; queue the message... */
1304 list_add_tail (&tx->tx_list, &peer->ibp_tx_queue);
1306 write_unlock_irqrestore(g_lock, flags);
1310 kibnal_dist(lib_nal_t *nal, ptl_nid_t nid, unsigned long *dist)
1312 /* I would guess that if kibnal_get_peer (nid) == NULL,
1313 and we're not routing, then 'nid' is very distant :) */
1314 if ( nal->libnal_ni.ni_pid.nid == nid ) {
1324 kibnal_sendmsg(lib_nal_t *nal,
1331 unsigned int payload_niov,
1332 struct iovec *payload_iov,
1333 ptl_kiov_t *payload_kiov,
1343 /* NB 'private' is different depending on what we're sending.... */
1345 CDEBUG(D_NET, "sending %d bytes in %d frags to nid:"LPX64
1346 " pid %d\n", payload_nob, payload_niov, nid , pid);
1348 LASSERT (payload_nob == 0 || payload_niov > 0);
1349 LASSERT (payload_niov <= PTL_MD_MAX_IOV);
1351 /* Thread context */
1352 LASSERT (!in_interrupt());
1353 /* payload is either all vaddrs or all pages */
1354 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1361 case PTL_MSG_REPLY: {
1362 /* reply's 'private' is the incoming receive */
1363 kib_rx_t *rx = private;
1365 LASSERT(rx != NULL);
1367 if (rx->rx_msg->ibm_type == IBNAL_MSG_IMMEDIATE) {
1368 /* RDMA not expected */
1369 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1370 if (nob > IBNAL_MSG_SIZE) {
1371 CERROR("REPLY for "LPX64" too big (RDMA not requested):"
1372 "%d (max for message is %d)\n",
1373 nid, payload_nob, IBNAL_MSG_SIZE);
1374 CERROR("Can't REPLY IMMEDIATE %d to "LPX64"\n",
1381 /* Incoming message consistent with RDMA? */
1382 if (rx->rx_msg->ibm_type != IBNAL_MSG_GET_REQ) {
1383 CERROR("REPLY to "LPX64" bad msg type %x!!!\n",
1384 nid, rx->rx_msg->ibm_type);
1388 /* NB rx_complete() will send GET_NAK when I return to it from
1389 * here, unless I set rx_responded! */
1391 tx = kibnal_get_idle_tx(0);
1393 CERROR("Can't get tx for REPLY to "LPX64"\n", nid);
1397 if (payload_nob == 0)
1399 else if (payload_kiov == NULL)
1400 rc = kibnal_setup_rd_iov(tx, tx->tx_rd, 0,
1401 payload_niov, payload_iov,
1402 payload_offset, payload_nob);
1404 rc = kibnal_setup_rd_kiov(tx, tx->tx_rd, 0,
1405 payload_niov, payload_kiov,
1406 payload_offset, payload_nob);
1408 CERROR("Can't setup GET src for "LPX64": %d\n", nid, rc);
1413 rc = kibnal_init_rdma(tx, IBNAL_MSG_GET_DONE, payload_nob,
1414 &rx->rx_msg->ibm_u.get.ibgm_rd,
1415 rx->rx_msg->ibm_u.get.ibgm_cookie);
1417 CERROR("Can't setup rdma for GET from "LPX64": %d\n",
1419 } else if (rc == 0) {
1420 /* No RDMA: local completion may happen now! */
1421 lib_finalize (&kibnal_lib, NULL, libmsg, PTL_OK);
1423 /* RDMA: lib_finalize(libmsg) when it completes */
1424 tx->tx_libmsg[0] = libmsg;
1427 kibnal_queue_tx(tx, rx->rx_conn);
1428 rx->rx_responded = 1;
1429 return (rc >= 0) ? PTL_OK : PTL_FAIL;
1433 /* will the REPLY message be small enough not to need RDMA? */
1434 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[libmsg->md->length]);
1435 if (nob <= IBNAL_MSG_SIZE)
1438 tx = kibnal_get_idle_tx(1); /* may block; caller is an app thread */
1439 LASSERT (tx != NULL);
1442 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1443 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1445 if ((libmsg->md->options & PTL_MD_KIOV) == 0)
1446 rc = kibnal_setup_rd_iov(tx, &ibmsg->ibm_u.get.ibgm_rd,
1448 libmsg->md->md_niov,
1449 libmsg->md->md_iov.iov,
1450 0, libmsg->md->length);
1452 rc = kibnal_setup_rd_kiov(tx, &ibmsg->ibm_u.get.ibgm_rd,
1454 libmsg->md->md_niov,
1455 libmsg->md->md_iov.kiov,
1456 0, libmsg->md->length);
1458 CERROR("Can't setup GET sink for "LPX64": %d\n", nid, rc);
1463 n = ibmsg->ibm_u.get.ibgm_rd.rd_nfrag;
1464 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[n]);
1465 kibnal_init_tx_msg(tx, IBNAL_MSG_GET_REQ, nob);
1467 tx->tx_libmsg[1] = lib_create_reply_msg(&kibnal_lib, nid, libmsg);
1468 if (tx->tx_libmsg[1] == NULL) {
1469 CERROR("Can't create reply for GET -> "LPX64"\n", nid);
1474 tx->tx_libmsg[0] = libmsg; /* finalise libmsg[0,1] on completion */
1475 tx->tx_waiting = 1; /* waiting for GET_DONE */
1476 kibnal_launch_tx(tx, nid);
1480 LASSERT (payload_nob == 0);
1484 /* Is the payload small enough not to need RDMA? */
1485 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1486 if (nob <= IBNAL_MSG_SIZE)
1489 tx = kibnal_get_idle_tx(1); /* may block: caller is app thread */
1490 LASSERT (tx != NULL);
1492 if (payload_kiov == NULL)
1493 rc = kibnal_setup_rd_iov(tx, tx->tx_rd, 0,
1494 payload_niov, payload_iov,
1495 payload_offset, payload_nob);
1497 rc = kibnal_setup_rd_kiov(tx, tx->tx_rd, 0,
1498 payload_niov, payload_kiov,
1499 payload_offset, payload_nob);
1501 CERROR("Can't setup PUT src for "LPX64": %d\n", nid, rc);
1507 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1508 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1509 kibnal_init_tx_msg(tx, IBNAL_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1511 tx->tx_libmsg[0] = libmsg; /* finalise libmsg on completion */
1512 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1513 kibnal_launch_tx(tx, nid);
1517 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1520 tx = kibnal_get_idle_tx(!(type == PTL_MSG_ACK ||
1521 type == PTL_MSG_REPLY));
1523 CERROR ("Can't send %d to "LPX64": tx descs exhausted\n", type, nid);
1524 return PTL_NO_SPACE;
1528 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1530 if (payload_nob > 0) {
1531 if (payload_kiov != NULL)
1532 lib_copy_kiov2buf(ibmsg->ibm_u.immediate.ibim_payload,
1533 payload_niov, payload_kiov,
1534 payload_offset, payload_nob);
1536 lib_copy_iov2buf(ibmsg->ibm_u.immediate.ibim_payload,
1537 payload_niov, payload_iov,
1538 payload_offset, payload_nob);
1541 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1542 kibnal_init_tx_msg (tx, IBNAL_MSG_IMMEDIATE, nob);
1544 tx->tx_libmsg[0] = libmsg; /* finalise libmsg on completion */
1545 kibnal_launch_tx(tx, nid);
1550 kibnal_send (lib_nal_t *nal, void *private, lib_msg_t *cookie,
1551 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1552 unsigned int payload_niov, struct iovec *payload_iov,
1553 size_t payload_offset, size_t payload_len)
1555 CDEBUG(D_NET, " pid = %d, nid="LPU64"\n",
1557 return (kibnal_sendmsg(nal, private, cookie,
1558 hdr, type, nid, pid,
1559 payload_niov, payload_iov, NULL,
1560 payload_offset, payload_len));
1564 kibnal_send_pages (lib_nal_t *nal, void *private, lib_msg_t *cookie,
1565 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1566 unsigned int payload_niov, ptl_kiov_t *payload_kiov,
1567 size_t payload_offset, size_t payload_len)
1569 return (kibnal_sendmsg(nal, private, cookie,
1570 hdr, type, nid, pid,
1571 payload_niov, NULL, payload_kiov,
1572 payload_offset, payload_len));
1576 kibnal_recvmsg (lib_nal_t *nal, void *private, lib_msg_t *libmsg,
1577 unsigned int niov, struct iovec *iov, ptl_kiov_t *kiov,
1578 size_t offset, int mlen, int rlen)
1580 kib_rx_t *rx = private;
1581 kib_msg_t *rxmsg = rx->rx_msg;
1582 kib_conn_t *conn = rx->rx_conn;
1589 LASSERT (mlen <= rlen);
1590 LASSERT (mlen >= 0);
1591 LASSERT (!in_interrupt());
1592 /* Either all pages or all vaddrs */
1593 LASSERT (!(kiov != NULL && iov != NULL));
1595 switch (rxmsg->ibm_type) {
1599 case IBNAL_MSG_IMMEDIATE:
1600 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1601 if (nob > IBNAL_MSG_SIZE) {
1602 CERROR ("Immediate message from "LPX64" too big: %d\n",
1603 rxmsg->ibm_u.immediate.ibim_hdr.src_nid, rlen);
1608 lib_copy_buf2kiov(niov, kiov, offset,
1609 rxmsg->ibm_u.immediate.ibim_payload,
1612 lib_copy_buf2iov(niov, iov, offset,
1613 rxmsg->ibm_u.immediate.ibim_payload,
1616 lib_finalize (nal, NULL, libmsg, PTL_OK);
1619 case IBNAL_MSG_PUT_REQ:
1620 /* NB rx_complete() will send PUT_NAK when I return to it from
1621 * here, unless I set rx_responded! */
1623 if (mlen == 0) { /* No payload to RDMA */
1624 lib_finalize(nal, NULL, libmsg, PTL_OK);
1628 tx = kibnal_get_idle_tx(0);
1630 CERROR("Can't allocate tx for "LPX64"\n",
1631 conn->ibc_peer->ibp_nid);
1637 rc = kibnal_setup_rd_iov(tx,
1638 &txmsg->ibm_u.putack.ibpam_rd,
1640 niov, iov, offset, mlen);
1642 rc = kibnal_setup_rd_kiov(tx,
1643 &txmsg->ibm_u.putack.ibpam_rd,
1645 niov, kiov, offset, mlen);
1647 CERROR("Can't setup PUT sink for "LPX64": %d\n",
1648 conn->ibc_peer->ibp_nid, rc);
1653 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1654 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1656 n = tx->tx_msg->ibm_u.putack.ibpam_rd.rd_nfrag;
1657 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[n]);
1658 kibnal_init_tx_msg(tx, IBNAL_MSG_PUT_ACK, nob);
1660 tx->tx_libmsg[0] = libmsg; /* finalise libmsg on completion */
1661 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1662 kibnal_queue_tx(tx, conn);
1664 LASSERT (!rx->rx_responded);
1665 rx->rx_responded = 1;
1668 case IBNAL_MSG_GET_REQ:
1669 /* We get called here just to discard any junk after the
1671 LASSERT (libmsg == NULL);
1672 lib_finalize (nal, NULL, libmsg, PTL_OK);
1678 kibnal_recv (lib_nal_t *nal, void *private, lib_msg_t *msg,
1679 unsigned int niov, struct iovec *iov,
1680 size_t offset, size_t mlen, size_t rlen)
1682 return (kibnal_recvmsg (nal, private, msg, niov, iov, NULL,
1683 offset, mlen, rlen));
1687 kibnal_recv_pages (lib_nal_t *nal, void *private, lib_msg_t *msg,
1688 unsigned int niov, ptl_kiov_t *kiov,
1689 size_t offset, size_t mlen, size_t rlen)
1691 return (kibnal_recvmsg (nal, private, msg, niov, NULL, kiov,
1692 offset, mlen, rlen));
1696 kibnal_thread_start (int (*fn)(void *arg), void *arg)
1698 long pid = kernel_thread (fn, arg, 0);
1703 atomic_inc (&kibnal_data.kib_nthreads);
1708 kibnal_thread_fini (void)
1710 atomic_dec (&kibnal_data.kib_nthreads);
1714 kibnal_close_conn_locked (kib_conn_t *conn, int error)
1716 /* This just does the immmediate housekeeping. 'error' is zero for a
1717 * normal shutdown which can happen only after the connection has been
1718 * established. If the connection is established, schedule the
1719 * connection to be finished off by the connd. Otherwise the connd is
1720 * already dealing with it (either to set it up or tear it down).
1721 * Caller holds kib_global_lock exclusively in irq context */
1722 kib_peer_t *peer = conn->ibc_peer;
1723 struct list_head *tmp;
1725 LASSERT (error != 0 || conn->ibc_state >= IBNAL_CONN_ESTABLISHED);
1727 if (error != 0 && conn->ibc_comms_error == 0)
1728 conn->ibc_comms_error = error;
1730 if (conn->ibc_state != IBNAL_CONN_ESTABLISHED)
1731 return; /* already being handled */
1733 spin_lock(&conn->ibc_lock);
1736 list_empty(&conn->ibc_tx_queue) &&
1737 list_empty(&conn->ibc_active_txs)) {
1738 CDEBUG(D_NET, "closing conn to "LPX64
1739 " rx# "LPD64" tx# "LPD64"\n",
1740 peer->ibp_nid, conn->ibc_txseq, conn->ibc_rxseq);
1742 CERROR("Closing conn to "LPX64": error %d%s%s"
1743 " rx# "LPD64" tx# "LPD64"\n",
1744 peer->ibp_nid, error,
1745 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1746 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)",
1747 conn->ibc_txseq, conn->ibc_rxseq);
1749 list_for_each(tmp, &conn->ibc_tx_queue) {
1750 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
1752 CERROR(" queued tx type %x cookie "LPX64
1753 " sending %d waiting %d ticks %ld/%d\n",
1754 tx->tx_msg->ibm_type, tx->tx_cookie,
1755 tx->tx_sending, tx->tx_waiting,
1756 (long)(tx->tx_deadline - jiffies), HZ);
1759 list_for_each(tmp, &conn->ibc_active_txs) {
1760 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
1762 CERROR(" active tx type %x cookie "LPX64
1763 " sending %d waiting %d ticks %ld/%d\n",
1764 tx->tx_msg->ibm_type, tx->tx_cookie,
1765 tx->tx_sending, tx->tx_waiting,
1766 (long)(tx->tx_deadline - jiffies), HZ);
1770 spin_unlock(&conn->ibc_lock);
1772 /* connd takes ibc_list's ref */
1773 list_del (&conn->ibc_list);
1775 if (list_empty (&peer->ibp_conns) &&
1776 peer->ibp_persistence == 0) {
1777 /* Non-persistent peer with no more conns... */
1778 kibnal_unlink_peer_locked (peer);
1781 kibnal_set_conn_state(conn, IBNAL_CONN_DISCONNECT1);
1783 spin_lock(&kibnal_data.kib_connd_lock);
1785 list_add_tail (&conn->ibc_list, &kibnal_data.kib_connd_conns);
1786 wake_up (&kibnal_data.kib_connd_waitq);
1788 spin_unlock(&kibnal_data.kib_connd_lock);
1792 kibnal_close_conn (kib_conn_t *conn, int error)
1794 unsigned long flags;
1796 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
1798 kibnal_close_conn_locked (conn, error);
1800 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
1804 kibnal_handle_early_rxs(kib_conn_t *conn)
1806 unsigned long flags;
1809 LASSERT (!in_interrupt());
1810 LASSERT (conn->ibc_state >= IBNAL_CONN_ESTABLISHED);
1812 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
1813 while (!list_empty(&conn->ibc_early_rxs)) {
1814 rx = list_entry(conn->ibc_early_rxs.next,
1816 list_del(&rx->rx_list);
1817 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
1819 kibnal_handle_rx(rx);
1821 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
1823 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
1827 kibnal_conn_disconnected(kib_conn_t *conn)
1829 LIST_HEAD (zombies);
1830 struct list_head *tmp;
1831 struct list_head *nxt;
1835 LASSERT (!in_interrupt());
1836 LASSERT (current == kibnal_data.kib_connd);
1837 LASSERT (conn->ibc_state >= IBNAL_CONN_INIT);
1839 kibnal_set_conn_state(conn, IBNAL_CONN_DISCONNECTED);
1841 /* move QP to error state to make posted work items complete */
1842 kibnal_set_qp_state(conn, vv_qp_state_error);
1844 spin_lock(&conn->ibc_lock);
1846 /* Complete all tx descs not waiting for sends to complete.
1847 * NB we should be safe from RDMA now that the QP has changed state */
1849 list_for_each_safe (tmp, nxt, &conn->ibc_tx_queue) {
1850 tx = list_entry (tmp, kib_tx_t, tx_list);
1852 LASSERT (tx->tx_queued);
1854 tx->tx_status = -ECONNABORTED;
1858 if (tx->tx_sending != 0)
1861 list_del (&tx->tx_list);
1862 list_add (&tx->tx_list, &zombies);
1865 list_for_each_safe (tmp, nxt, &conn->ibc_active_txs) {
1866 tx = list_entry (tmp, kib_tx_t, tx_list);
1868 LASSERT (!tx->tx_queued);
1869 LASSERT (tx->tx_waiting ||
1870 tx->tx_sending != 0);
1872 tx->tx_status = -ECONNABORTED;
1875 if (tx->tx_sending != 0)
1878 list_del (&tx->tx_list);
1879 list_add (&tx->tx_list, &zombies);
1882 spin_unlock(&conn->ibc_lock);
1884 while (!list_empty(&zombies)) {
1885 tx = list_entry (zombies.next, kib_tx_t, tx_list);
1887 list_del(&tx->tx_list);
1888 kibnal_tx_done (tx);
1891 kibnal_handle_early_rxs(conn);
1895 kibnal_peer_connect_failed (kib_peer_t *peer, int active)
1897 struct list_head zombies;
1899 unsigned long flags;
1901 /* Only the connd creates conns => single threaded */
1902 LASSERT (!in_interrupt());
1903 LASSERT (current == kibnal_data.kib_connd);
1904 LASSERT (peer->ibp_reconnect_interval >= IBNAL_MIN_RECONNECT_INTERVAL);
1906 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
1909 LASSERT (peer->ibp_connecting != 0);
1910 peer->ibp_connecting--;
1912 LASSERT (!kibnal_peer_active(peer));
1915 if (peer->ibp_connecting != 0) {
1916 /* another connection attempt under way (loopback?)... */
1917 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
1921 if (list_empty(&peer->ibp_conns)) {
1922 /* Say when active connection can be re-attempted */
1923 peer->ibp_reconnect_time = jiffies + peer->ibp_reconnect_interval;
1924 /* Increase reconnection interval */
1925 peer->ibp_reconnect_interval = MIN (peer->ibp_reconnect_interval * 2,
1926 IBNAL_MAX_RECONNECT_INTERVAL);
1928 /* Take peer's blocked transmits to complete with error */
1929 list_add(&zombies, &peer->ibp_tx_queue);
1930 list_del_init(&peer->ibp_tx_queue);
1932 if (kibnal_peer_active(peer) &&
1933 (peer->ibp_persistence == 0)) {
1934 /* failed connection attempt on non-persistent peer */
1935 kibnal_unlink_peer_locked (peer);
1938 /* Can't have blocked transmits if there are connections */
1939 LASSERT (list_empty(&peer->ibp_tx_queue));
1942 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
1944 if (list_empty (&zombies))
1947 CERROR ("Deleting messages for "LPX64": connection failed\n", peer->ibp_nid);
1949 tx = list_entry (zombies.next, kib_tx_t, tx_list);
1951 list_del (&tx->tx_list);
1953 tx->tx_status = -EHOSTUNREACH;
1954 kibnal_tx_done (tx);
1955 } while (!list_empty (&zombies));
1959 kibnal_connreq_done(kib_conn_t *conn, int active, int status)
1961 static cm_reject_data_t rej;
1963 struct list_head txs;
1964 kib_peer_t *peer = conn->ibc_peer;
1966 unsigned long flags;
1969 /* Only the connd creates conns => single threaded */
1970 LASSERT (!in_interrupt());
1971 LASSERT (current == kibnal_data.kib_connd);
1972 LASSERT (conn->ibc_state < IBNAL_CONN_ESTABLISHED);
1975 LASSERT (peer->ibp_connecting > 0);
1977 LASSERT (!kibnal_peer_active(peer));
1980 PORTAL_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1981 conn->ibc_connvars = NULL;
1984 /* failed to establish connection */
1985 switch (conn->ibc_state) {
1988 case IBNAL_CONN_ACTIVE_CHECK_REPLY:
1989 /* got a connection reply but failed checks */
1991 memset(&rej, 0, sizeof(rej));
1992 rej.reason = cm_rej_code_usr_rej;
1993 cm_reject(conn->ibc_cep, &rej);
1996 case IBNAL_CONN_ACTIVE_CONNECT:
1998 cm_cancel(conn->ibc_cep);
1999 kibnal_pause(HZ/10);
2000 /* cm_connect() failed immediately or
2001 * callback returned failure */
2004 case IBNAL_CONN_ACTIVE_ARP:
2006 /* ibat_get_ib_data() failed immediately
2007 * or callback returned failure */
2010 case IBNAL_CONN_INIT:
2013 case IBNAL_CONN_PASSIVE_WAIT:
2015 /* cm_accept callback returned failure */
2019 kibnal_peer_connect_failed(conn->ibc_peer, active);
2020 kibnal_conn_disconnected(conn);
2024 /* connection established */
2025 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2028 LASSERT(conn->ibc_state == IBNAL_CONN_ACTIVE_RTU);
2030 LASSERT(conn->ibc_state == IBNAL_CONN_PASSIVE_WAIT);
2033 kibnal_set_conn_state(conn, IBNAL_CONN_ESTABLISHED);
2036 peer2 = kibnal_find_peer_locked(peer->ibp_nid);
2037 if (peer2 != NULL) {
2038 /* already in the peer table; swap */
2039 conn->ibc_peer = peer2;
2040 kibnal_peer_addref(peer2);
2041 kibnal_peer_decref(peer);
2042 peer = conn->ibc_peer;
2044 /* add 'peer' to the peer table */
2045 kibnal_peer_addref(peer);
2046 list_add_tail(&peer->ibp_list,
2047 kibnal_nid2peerlist(peer->ibp_nid));
2051 /* Add conn to peer's list and nuke any dangling conns from a different
2052 * peer instance... */
2053 kibnal_conn_addref(conn); /* +1 ref for ibc_list */
2054 list_add(&conn->ibc_list, &peer->ibp_conns);
2055 kibnal_close_stale_conns_locked (conn->ibc_peer,
2056 conn->ibc_incarnation);
2058 if (!kibnal_peer_active(peer) || /* peer has been deleted */
2059 conn->ibc_comms_error != 0 || /* comms error */
2060 conn->ibc_disconnect) { /* need to disconnect */
2062 /* start to shut down connection */
2063 kibnal_close_conn_locked(conn, -ECONNABORTED);
2065 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2066 kibnal_peer_connect_failed(peer, active);
2071 peer->ibp_connecting--;
2073 /* grab pending txs while I have the lock */
2074 list_add(&txs, &peer->ibp_tx_queue);
2075 list_del_init(&peer->ibp_tx_queue);
2077 /* reset reconnect interval for next attempt */
2078 peer->ibp_reconnect_interval = IBNAL_MIN_RECONNECT_INTERVAL;
2079 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2081 /* Schedule blocked txs */
2082 spin_lock (&conn->ibc_lock);
2083 while (!list_empty (&txs)) {
2084 tx = list_entry (txs.next, kib_tx_t, tx_list);
2085 list_del (&tx->tx_list);
2087 kibnal_queue_tx_locked (tx, conn);
2089 spin_unlock (&conn->ibc_lock);
2090 kibnal_check_sends (conn);
2092 /* schedule blocked rxs */
2093 kibnal_handle_early_rxs(conn);
2097 kibnal_cm_callback(cm_cep_handle_t cep, cm_conn_data_t *cmdata, void *arg)
2099 static cm_dreply_data_t drep; /* just zeroed space */
2101 kib_conn_t *conn = (kib_conn_t *)arg;
2102 unsigned long flags;
2104 /* CAVEAT EMPTOR: tasklet context */
2106 switch (cmdata->status) {
2110 case cm_event_disconn_request:
2111 /* IBNAL_CONN_ACTIVE_RTU: gets closed in kibnal_connreq_done
2112 * IBNAL_CONN_ESTABLISHED: I start it closing
2113 * otherwise: it's closing anyway */
2114 cm_disconnect(conn->ibc_cep, NULL, &drep);
2115 cm_cancel(conn->ibc_cep);
2117 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2118 LASSERT (!conn->ibc_disconnect);
2119 conn->ibc_disconnect = 1;
2121 switch (conn->ibc_state) {
2125 case IBNAL_CONN_ACTIVE_RTU:
2126 /* kibnal_connreq_done is getting there; It'll see
2127 * ibc_disconnect set... */
2128 kibnal_conn_decref(conn); /* lose my ref */
2131 case IBNAL_CONN_ESTABLISHED:
2132 /* kibnal_connreq_done got there already; get
2133 * disconnect going... */
2134 kibnal_close_conn_locked(conn, 0);
2135 kibnal_conn_decref(conn); /* lose my ref */
2138 case IBNAL_CONN_DISCONNECT1:
2139 /* kibnal_terminate_conn is getting there; It'll see
2140 * ibc_disconnect set... */
2141 kibnal_conn_decref(conn); /* lose my ref */
2144 case IBNAL_CONN_DISCONNECT2:
2145 /* kibnal_terminate_conn got there already; complete
2146 * the disconnect. NB kib_connd_conns takes my ref */
2147 spin_lock(&kibnal_data.kib_connd_lock);
2148 list_add_tail(&conn->ibc_list, &kibnal_data.kib_connd_conns);
2149 wake_up(&kibnal_data.kib_connd_waitq);
2150 spin_unlock(&kibnal_data.kib_connd_lock);
2153 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2156 case cm_event_disconn_timeout:
2157 case cm_event_disconn_reply:
2158 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2159 LASSERT (conn->ibc_state == IBNAL_CONN_DISCONNECT2);
2160 LASSERT (!conn->ibc_disconnect);
2161 conn->ibc_disconnect = 1;
2163 /* kibnal_terminate_conn sent the disconnect request.
2164 * NB kib_connd_conns takes my ref */
2165 spin_lock(&kibnal_data.kib_connd_lock);
2166 list_add_tail(&conn->ibc_list, &kibnal_data.kib_connd_conns);
2167 wake_up(&kibnal_data.kib_connd_waitq);
2168 spin_unlock(&kibnal_data.kib_connd_lock);
2170 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2173 case cm_event_connected:
2174 case cm_event_conn_timeout:
2175 case cm_event_conn_reject:
2176 LASSERT (conn->ibc_state == IBNAL_CONN_PASSIVE_WAIT);
2177 conn->ibc_connvars->cv_conndata = *cmdata;
2179 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
2180 list_add_tail(&conn->ibc_list, &kibnal_data.kib_connd_conns);
2181 wake_up(&kibnal_data.kib_connd_waitq);
2182 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
2188 kibnal_check_passive_wait(kib_conn_t *conn)
2192 switch (conn->ibc_connvars->cv_conndata.status) {
2196 case cm_event_connected:
2197 kibnal_conn_addref(conn); /* ++ ref for CM callback */
2198 rc = kibnal_set_qp_state(conn, vv_qp_state_rts);
2200 conn->ibc_comms_error = rc;
2201 /* connection _has_ been established; it's just that we've had
2202 * an error immediately... */
2203 kibnal_connreq_done(conn, 0, 0);
2206 case cm_event_conn_timeout:
2207 kibnal_connreq_done(conn, 0, -ETIMEDOUT);
2210 case cm_event_conn_reject:
2211 kibnal_connreq_done(conn, 0, -ECONNRESET);
2217 kibnal_recv_connreq(cm_cep_handle_t *cep, cm_request_data_t *cmreq)
2219 static kib_msg_t txmsg;
2220 static kib_msg_t rxmsg;
2221 static cm_reply_data_t reply;
2222 static cm_reject_data_t reject;
2224 kib_conn_t *conn = NULL;
2228 kib_peer_t *tmp_peer;
2232 /* I'm the connd executing in thread context
2233 * No concurrency problems with static data! */
2234 LASSERT (!in_interrupt());
2235 LASSERT (current == kibnal_data.kib_connd);
2237 if (cmreq->sid != IBNAL_SERVICE_NUMBER) {
2238 CERROR(LPX64" != IBNAL_SERVICE_NUMBER("LPX64")\n",
2239 cmreq->sid, (__u64)IBNAL_SERVICE_NUMBER);
2243 /* copy into rxmsg to avoid alignment issues */
2244 rxmsgnob = MIN(cm_REQ_priv_data_len, sizeof(rxmsg));
2245 memcpy(&rxmsg, cmreq->priv_data, rxmsgnob);
2247 rc = kibnal_unpack_msg(&rxmsg, rxmsgnob);
2249 CERROR("Can't parse connection request: %d\n", rc);
2253 if (rxmsg.ibm_type != IBNAL_MSG_CONNREQ) {
2254 CERROR("Unexpected connreq msg type: %x from "LPX64"\n",
2255 rxmsg.ibm_type, rxmsg.ibm_srcnid);
2259 if (rxmsg.ibm_dstnid != kibnal_lib.libnal_ni.ni_pid.nid) {
2260 CERROR("Can't accept "LPX64": bad dst nid "LPX64"\n",
2261 rxmsg.ibm_srcnid, rxmsg.ibm_dstnid);
2265 if (rxmsg.ibm_u.connparams.ibcp_queue_depth != IBNAL_MSG_QUEUE_SIZE) {
2266 CERROR("Can't accept "LPX64": incompatible queue depth %d (%d wanted)\n",
2267 rxmsg.ibm_srcnid, rxmsg.ibm_u.connparams.ibcp_queue_depth,
2268 IBNAL_MSG_QUEUE_SIZE);
2272 if (rxmsg.ibm_u.connparams.ibcp_max_msg_size > IBNAL_MSG_SIZE) {
2273 CERROR("Can't accept "LPX64": message size %d too big (%d max)\n",
2274 rxmsg.ibm_srcnid, rxmsg.ibm_u.connparams.ibcp_max_msg_size,
2279 if (rxmsg.ibm_u.connparams.ibcp_max_frags > IBNAL_MAX_RDMA_FRAGS) {
2280 CERROR("Can't accept "LPX64": max frags %d too big (%d max)\n",
2281 rxmsg.ibm_srcnid, rxmsg.ibm_u.connparams.ibcp_max_frags,
2282 IBNAL_MAX_RDMA_FRAGS);
2286 conn = kibnal_create_conn(cep);
2288 CERROR("Can't create conn for "LPX64"\n", rxmsg.ibm_srcnid);
2292 /* assume 'rxmsg.ibm_srcnid' is a new peer */
2293 tmp_peer = kibnal_create_peer (rxmsg.ibm_srcnid);
2294 if (tmp_peer == NULL) {
2295 CERROR("Can't create tmp peer for "LPX64"\n", rxmsg.ibm_srcnid);
2296 kibnal_conn_decref(conn);
2301 conn->ibc_peer = tmp_peer; /* conn takes over my ref */
2302 conn->ibc_incarnation = rxmsg.ibm_srcstamp;
2303 conn->ibc_credits = IBNAL_MSG_QUEUE_SIZE;
2305 cv = conn->ibc_connvars;
2307 cv->cv_txpsn = cmreq->cep_data.start_psn;
2308 cv->cv_remote_qpn = cmreq->cep_data.qpn;
2309 cv->cv_path = cmreq->path_data.path;
2310 cv->cv_rnr_count = cmreq->cep_data.rtr_retry_cnt;
2311 // XXX cmreq->cep_data.retry_cnt;
2312 cv->cv_port = cmreq->cep_data.local_port_num;
2314 vvrc = gid2gid_index(kibnal_data.kib_hca, cv->cv_port,
2315 &cv->cv_path.sgid, &cv->cv_sgid_index);
2316 LASSERT (vvrc == vv_return_ok);
2318 vvrc = pkey2pkey_index(kibnal_data.kib_hca, cv->cv_port,
2319 cv->cv_path.pkey, &cv->cv_pkey_index);
2320 LASSERT (vvrc == vv_return_ok);
2322 rc = kibnal_set_qp_state(conn, vv_qp_state_init);
2326 rc = kibnal_post_receives(conn);
2328 CERROR("Can't post receives for "LPX64"\n", rxmsg.ibm_srcnid);
2332 rc = kibnal_set_qp_state(conn, vv_qp_state_rtr);
2336 memset(&reply, 0, sizeof(reply));
2337 reply.qpn = cv->cv_local_qpn;
2338 reply.qkey = IBNAL_QKEY;
2339 reply.start_psn = cv->cv_rxpsn;
2340 reply.arb_initiator_depth = IBNAL_ARB_INITIATOR_DEPTH;
2341 reply.arb_resp_res = IBNAL_ARB_RESP_RES;
2342 reply.failover_accepted = IBNAL_FAILOVER_ACCEPTED;
2343 reply.rnr_retry_count = cv->cv_rnr_count;
2344 reply.targ_ack_delay = kibnal_data.kib_hca_attrs.ack_delay;
2346 /* setup txmsg... */
2347 memset(&txmsg, 0, sizeof(txmsg));
2348 kibnal_init_msg(&txmsg, IBNAL_MSG_CONNACK,
2349 sizeof(txmsg.ibm_u.connparams));
2350 LASSERT (txmsg.ibm_nob <= cm_REP_priv_data_len);
2351 txmsg.ibm_u.connparams.ibcp_queue_depth = IBNAL_MSG_QUEUE_SIZE;
2352 txmsg.ibm_u.connparams.ibcp_max_msg_size = IBNAL_MSG_SIZE;
2353 txmsg.ibm_u.connparams.ibcp_max_frags = IBNAL_MAX_RDMA_FRAGS;
2354 kibnal_pack_msg(&txmsg, 0, rxmsg.ibm_srcnid, rxmsg.ibm_srcstamp, 0);
2356 /* ...and copy into reply to avoid alignment issues */
2357 memcpy(&reply.priv_data, &txmsg, txmsg.ibm_nob);
2359 kibnal_set_conn_state(conn, IBNAL_CONN_PASSIVE_WAIT);
2361 cmrc = cm_accept(conn->ibc_cep, &reply, NULL,
2362 kibnal_cm_callback, conn);
2364 if (cmrc == cm_stat_success)
2365 return; /* callback has got my ref on conn */
2367 /* back out state change (no callback happening) */
2368 kibnal_set_conn_state(conn, IBNAL_CONN_INIT);
2372 CERROR("Rejected connreq from "LPX64"\n", rxmsg.ibm_srcnid);
2374 memset(&reject, 0, sizeof(reject));
2375 reject.reason = cm_rej_code_usr_rej;
2376 cm_reject(cep, &reject);
2380 kibnal_connreq_done(conn, 0, rc);
2382 cm_destroy_cep(cep);
2387 kibnal_listen_callback(cm_cep_handle_t cep, cm_conn_data_t *data, void *arg)
2389 cm_request_data_t *cmreq = &data->data.request;
2391 unsigned long flags;
2393 LASSERT (arg == NULL);
2395 if (data->status != cm_event_conn_request) {
2396 CERROR("status %d is not cm_event_conn_request\n",
2401 PORTAL_ALLOC_ATOMIC(pcr, sizeof(*pcr));
2403 CERROR("Can't allocate passive connreq\n");
2405 cm_reject(cep, &((cm_reject_data_t) /* NB RO struct */
2406 {.reason = cm_rej_code_no_res,}));
2407 cm_destroy_cep(cep);
2412 pcr->pcr_cmreq = *cmreq;
2414 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
2416 list_add_tail(&pcr->pcr_list, &kibnal_data.kib_connd_pcreqs);
2417 wake_up(&kibnal_data.kib_connd_waitq);
2419 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
2424 kibnal_active_connect_callback (cm_cep_handle_t cep, cm_conn_data_t *cd,
2427 /* CAVEAT EMPTOR: tasklet context */
2428 kib_conn_t *conn = (kib_conn_t *)arg;
2429 kib_connvars_t *cv = conn->ibc_connvars;
2430 unsigned long flags;
2432 LASSERT (conn->ibc_state == IBNAL_CONN_ACTIVE_CONNECT);
2433 cv->cv_conndata = *cd;
2435 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
2436 /* connd takes my ref */
2437 list_add_tail(&conn->ibc_list, &kibnal_data.kib_connd_conns);
2438 wake_up(&kibnal_data.kib_connd_waitq);
2439 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
2443 kibnal_connect_conn (kib_conn_t *conn)
2445 static cm_request_data_t cmreq;
2446 static kib_msg_t msg;
2448 kib_connvars_t *cv = conn->ibc_connvars;
2449 kib_peer_t *peer = conn->ibc_peer;
2452 /* Only called by connd => statics OK */
2453 LASSERT (!in_interrupt());
2454 LASSERT (current == kibnal_data.kib_connd);
2455 LASSERT (conn->ibc_state == IBNAL_CONN_ACTIVE_ARP);
2457 memset(&cmreq, 0, sizeof(cmreq));
2459 cmreq.sid = IBNAL_SERVICE_NUMBER;
2461 cmreq.cep_data.ca_guid = kibnal_data.kib_hca_attrs.guid;
2462 cmreq.cep_data.qpn = cv->cv_local_qpn;
2463 cmreq.cep_data.retry_cnt = IBNAL_RETRY_CNT;
2464 cmreq.cep_data.rtr_retry_cnt = IBNAL_RNR_CNT;
2465 cmreq.cep_data.start_psn = cv->cv_rxpsn;
2466 cmreq.cep_data.end_to_end_flow_ctrl = IBNAL_EE_FLOW_CNT;
2469 // offered_initiator_depth
2471 cmreq.path_data.subn_local = IBNAL_LOCAL_SUB;
2472 cmreq.path_data.path = cv->cv_path;
2475 memset(&msg, 0, sizeof(msg));
2476 kibnal_init_msg(&msg, IBNAL_MSG_CONNREQ, sizeof(msg.ibm_u.connparams));
2477 LASSERT(msg.ibm_nob <= cm_REQ_priv_data_len);
2478 msg.ibm_u.connparams.ibcp_queue_depth = IBNAL_MSG_QUEUE_SIZE;
2479 msg.ibm_u.connparams.ibcp_max_msg_size = IBNAL_MSG_SIZE;
2480 msg.ibm_u.connparams.ibcp_max_frags = IBNAL_MAX_RDMA_FRAGS;
2481 kibnal_pack_msg(&msg, 0, peer->ibp_nid, 0, 0);
2483 /* ...and copy into cmreq to avoid alignment issues */
2484 memcpy(&cmreq.priv_data, &msg, msg.ibm_nob);
2486 CDEBUG(D_NET, "Connecting %p to "LPX64"\n", conn, peer->ibp_nid);
2488 kibnal_conn_addref(conn); /* ++ref for CM callback */
2489 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_CONNECT);
2491 cmrc = cm_connect(conn->ibc_cep, &cmreq,
2492 kibnal_active_connect_callback, conn);
2493 if (cmrc == cm_stat_success) {
2494 CDEBUG(D_NET, "connection REQ sent to "LPX64"\n",
2499 CERROR ("Connect "LPX64" failed: %d\n", peer->ibp_nid, cmrc);
2500 kibnal_conn_decref(conn); /* drop callback's ref */
2501 kibnal_connreq_done(conn, 1, -EHOSTUNREACH);
2505 kibnal_check_connreply (kib_conn_t *conn)
2507 static cm_rtu_data_t rtu;
2508 static kib_msg_t msg;
2510 kib_connvars_t *cv = conn->ibc_connvars;
2511 cm_reply_data_t *reply = &cv->cv_conndata.data.reply;
2512 kib_peer_t *peer = conn->ibc_peer;
2515 cm_cep_handle_t cep;
2516 unsigned long flags;
2519 /* Only called by connd => statics OK */
2520 LASSERT (!in_interrupt());
2521 LASSERT (current == kibnal_data.kib_connd);
2522 LASSERT (conn->ibc_state == IBNAL_CONN_ACTIVE_CONNECT);
2524 if (cv->cv_conndata.status == cm_event_conn_reply) {
2525 cv->cv_remote_qpn = reply->qpn;
2526 cv->cv_txpsn = reply->start_psn;
2527 // XXX reply->targ_ack_delay;
2528 cv->cv_rnr_count = reply->rnr_retry_count;
2530 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_CHECK_REPLY);
2532 /* copy into msg to avoid alignment issues */
2533 msgnob = MIN(cm_REP_priv_data_len, sizeof(msg));
2534 memcpy(&msg, &reply->priv_data, msgnob);
2536 rc = kibnal_unpack_msg(&msg, msgnob);
2538 CERROR("Can't unpack reply from "LPX64"\n",
2540 kibnal_connreq_done(conn, 1, rc);
2544 if (msg.ibm_type != IBNAL_MSG_CONNACK ) {
2545 CERROR("Unexpected message type %d from "LPX64"\n",
2546 msg.ibm_type, peer->ibp_nid);
2547 kibnal_connreq_done(conn, 1, -EPROTO);
2551 if (msg.ibm_u.connparams.ibcp_queue_depth != IBNAL_MSG_QUEUE_SIZE) {
2552 CERROR(LPX64" has incompatible queue depth %d(%d wanted)\n",
2553 peer->ibp_nid, msg.ibm_u.connparams.ibcp_queue_depth,
2554 IBNAL_MSG_QUEUE_SIZE);
2555 kibnal_connreq_done(conn, 1, -EPROTO);
2559 if (msg.ibm_u.connparams.ibcp_max_msg_size > IBNAL_MSG_SIZE) {
2560 CERROR(LPX64" max message size %d too big (%d max)\n",
2561 peer->ibp_nid, msg.ibm_u.connparams.ibcp_max_msg_size,
2563 kibnal_connreq_done(conn, 1, -EPROTO);
2567 if (msg.ibm_u.connparams.ibcp_max_frags > IBNAL_MAX_RDMA_FRAGS) {
2568 CERROR(LPX64" max frags %d too big (%d max)\n",
2569 peer->ibp_nid, msg.ibm_u.connparams.ibcp_max_frags,
2570 IBNAL_MAX_RDMA_FRAGS);
2571 kibnal_connreq_done(conn, 1, -EPROTO);
2575 read_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2576 rc = (msg.ibm_dstnid != kibnal_lib.libnal_ni.ni_pid.nid ||
2577 msg.ibm_dststamp != kibnal_data.kib_incarnation) ?
2579 read_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2581 CERROR("Stale connection reply from "LPX64"\n",
2583 kibnal_connreq_done(conn, 1, rc);
2587 conn->ibc_incarnation = msg.ibm_srcstamp;
2588 conn->ibc_credits = IBNAL_MSG_QUEUE_SIZE;
2590 rc = kibnal_post_receives(conn);
2592 CERROR("Can't post receives for "LPX64"\n",
2594 kibnal_connreq_done(conn, 1, rc);
2598 rc = kibnal_set_qp_state(conn, vv_qp_state_rtr);
2600 kibnal_connreq_done(conn, 1, rc);
2604 rc = kibnal_set_qp_state(conn, vv_qp_state_rts);
2606 kibnal_connreq_done(conn, 1, rc);
2610 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_RTU);
2611 kibnal_conn_addref(conn); /* ++for CM callback */
2613 memset(&rtu, 0, sizeof(rtu));
2614 cmrc = cm_accept(conn->ibc_cep, NULL, &rtu,
2615 kibnal_cm_callback, conn);
2616 if (cmrc == cm_stat_success) {
2617 /* Now I'm racing with disconnect signalled by
2618 * kibnal_cm_callback */
2619 kibnal_connreq_done(conn, 1, 0);
2623 CERROR("cm_accept "LPX64" failed: %d\n", peer->ibp_nid, cmrc);
2624 /* Back out of RTU: no callback coming */
2625 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_CHECK_REPLY);
2626 kibnal_conn_decref(conn);
2627 kibnal_connreq_done(conn, 1, -EIO);
2631 if (cv->cv_conndata.status == cm_event_conn_reject) {
2633 if (cv->cv_conndata.data.reject.reason != cm_rej_code_stale_conn) {
2634 CERROR("conn -> "LPX64" rejected: %d\n", peer->ibp_nid,
2635 cv->cv_conndata.data.reject.reason);
2636 kibnal_connreq_done(conn, 1, -ECONNREFUSED);
2640 CWARN ("conn -> "LPX64" stale: retrying\n", peer->ibp_nid);
2642 cep = cm_create_cep(cm_cep_transp_rc);
2644 CERROR("Can't create new CEP\n");
2645 kibnal_connreq_done(conn, 1, -ENOMEM);
2649 cmrc = cm_cancel(conn->ibc_cep);
2650 LASSERT (cmrc == cm_stat_success);
2651 cmrc = cm_destroy_cep(conn->ibc_cep);
2652 LASSERT (cmrc == cm_stat_success);
2654 conn->ibc_cep = cep;
2657 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_ARP);
2658 kibnal_connect_conn(conn);
2662 CERROR("conn -> "LPX64" failed: %d\n", peer->ibp_nid,
2663 cv->cv_conndata.status);
2664 kibnal_connreq_done(conn, 1, -ECONNABORTED);
2668 kibnal_send_connreq (kib_conn_t *conn)
2670 kib_peer_t *peer = conn->ibc_peer;
2671 kib_connvars_t *cv = conn->ibc_connvars;
2672 ibat_arp_data_t *arp = &cv->cv_arp;
2673 ib_path_record_v2_t *path = &cv->cv_path;
2677 /* Only called by connd => statics OK */
2678 LASSERT (!in_interrupt());
2679 LASSERT (current == kibnal_data.kib_connd);
2680 LASSERT (conn->ibc_state == IBNAL_CONN_ACTIVE_ARP);
2682 if (cv->cv_arprc != ibat_stat_ok) {
2683 CERROR("Can't Arp "LPX64"@%u.%u.%u.%u: %d\n", peer->ibp_nid,
2684 HIPQUAD(peer->ibp_ip), cv->cv_arprc);
2685 kibnal_connreq_done(conn, 1, -ENETUNREACH);
2689 if ((arp->mask & IBAT_PRI_PATH_VALID) != 0) {
2690 CDEBUG(D_NET, "Got valid path for "LPX64"\n", peer->ibp_nid);
2692 *path = *arp->primary_path;
2694 vvrc = base_gid2port_num(kibnal_data.kib_hca, &path->sgid,
2696 LASSERT (vvrc == vv_return_ok);
2698 vvrc = gid2gid_index(kibnal_data.kib_hca, cv->cv_port,
2699 &path->sgid, &cv->cv_sgid_index);
2700 LASSERT (vvrc == vv_return_ok);
2702 vvrc = pkey2pkey_index(kibnal_data.kib_hca, cv->cv_port,
2703 path->pkey, &cv->cv_pkey_index);
2704 LASSERT (vvrc == vv_return_ok);
2706 path->mtu = IBNAL_IB_MTU;
2708 } else if ((arp->mask & IBAT_LID_VALID) != 0) {
2709 CWARN("Creating new path record for "LPX64"@%u.%u.%u.%u\n",
2710 peer->ibp_nid, HIPQUAD(peer->ibp_ip));
2712 cv->cv_pkey_index = IBNAL_PKEY_IDX;
2713 cv->cv_sgid_index = IBNAL_SGID_IDX;
2714 cv->cv_port = arp->local_port_num;
2716 memset(path, 0, sizeof(*path));
2718 vvrc = port_num2base_gid(kibnal_data.kib_hca, cv->cv_port,
2720 LASSERT (vvrc == vv_return_ok);
2722 vvrc = port_num2base_lid(kibnal_data.kib_hca, cv->cv_port,
2724 LASSERT (vvrc == vv_return_ok);
2726 path->dgid = arp->gid;
2727 path->sl = IBNAL_SERVICE_LEVEL;
2728 path->dlid = arp->lid;
2729 path->mtu = IBNAL_IB_MTU;
2730 path->rate = IBNAL_STATIC_RATE;
2731 path->pkt_life_time = IBNAL_PKT_LIFETIME;
2732 path->pkey = IBNAL_PKEY;
2733 path->traffic_class = IBNAL_TRAFFIC_CLASS;
2735 CERROR("Can't Arp "LPX64"@%u.%u.%u.%u: no PATH or LID\n",
2736 peer->ibp_nid, HIPQUAD(peer->ibp_ip));
2737 kibnal_connreq_done(conn, 1, -ENETUNREACH);
2741 rc = kibnal_set_qp_state(conn, vv_qp_state_init);
2743 kibnal_connreq_done(conn, 1, rc);
2746 /* do the actual connection request */
2747 kibnal_connect_conn(conn);
2751 kibnal_arp_callback (ibat_stat_t arprc, ibat_arp_data_t *arp_data, void *arg)
2753 /* CAVEAT EMPTOR: tasklet context */
2754 kib_conn_t *conn = (kib_conn_t *)arg;
2755 kib_peer_t *peer = conn->ibc_peer;
2756 unsigned long flags;
2758 CDEBUG(arprc == ibat_stat_ok ? D_NET : D_ERROR,
2759 "Arp "LPX64"@%u.%u.%u.%u rc %d LID %s PATH %s\n",
2760 peer->ibp_nid, HIPQUAD(peer->ibp_ip), arprc,
2761 (arp_data->mask & IBAT_LID_VALID) == 0 ? "invalid" : "valid",
2762 (arp_data->mask & IBAT_PRI_PATH_VALID) == 0 ? "invalid" : "valid");
2763 LASSERT (conn->ibc_state == IBNAL_CONN_ACTIVE_ARP);
2765 conn->ibc_connvars->cv_arprc = arprc;
2766 if (arprc == ibat_stat_ok)
2767 conn->ibc_connvars->cv_arp = *arp_data;
2769 /* connd takes over my ref on conn */
2770 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
2772 list_add_tail(&conn->ibc_list, &kibnal_data.kib_connd_conns);
2773 wake_up(&kibnal_data.kib_connd_waitq);
2775 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
2779 kibnal_arp_peer (kib_peer_t *peer)
2781 cm_cep_handle_t cep;
2785 /* Only the connd does this (i.e. single threaded) */
2786 LASSERT (current == kibnal_data.kib_connd);
2787 LASSERT (peer->ibp_connecting != 0);
2789 cep = cm_create_cep(cm_cep_transp_rc);
2791 CERROR ("Can't create cep for conn->"LPX64"\n",
2793 kibnal_peer_connect_failed(peer, 1);
2797 conn = kibnal_create_conn(cep);
2799 CERROR ("Can't allocate conn->"LPX64"\n",
2801 cm_destroy_cep(cep);
2802 kibnal_peer_connect_failed(peer, 1);
2806 conn->ibc_peer = peer;
2807 kibnal_peer_addref(peer);
2809 kibnal_set_conn_state(conn, IBNAL_CONN_ACTIVE_ARP);
2811 ibatrc = ibat_get_ib_data(htonl(peer->ibp_ip), INADDR_ANY,
2813 &conn->ibc_connvars->cv_arp,
2814 kibnal_arp_callback, conn, 0);
2815 CDEBUG(D_NET,"ibatrc %d\n", ibatrc);
2820 case ibat_stat_pending:
2821 /* NB callback has my ref on conn */
2825 /* Immediate return (ARP cache hit) == no callback. */
2826 conn->ibc_connvars->cv_arprc = ibat_stat_ok;
2827 kibnal_send_connreq(conn);
2828 kibnal_conn_decref(conn);
2831 case ibat_stat_error:
2832 case ibat_stat_timeout:
2833 case ibat_stat_not_found:
2834 CERROR("Arp "LPX64"@%u.%u.%u.%u failed: %d\n", peer->ibp_nid,
2835 HIPQUAD(peer->ibp_ip), ibatrc);
2836 kibnal_connreq_done(conn, 1, -ENETUNREACH);
2837 kibnal_conn_decref(conn);
2843 kibnal_conn_timed_out (kib_conn_t *conn)
2846 struct list_head *ttmp;
2848 spin_lock(&conn->ibc_lock);
2850 list_for_each (ttmp, &conn->ibc_tx_queue) {
2851 tx = list_entry (ttmp, kib_tx_t, tx_list);
2853 LASSERT (tx->tx_queued);
2855 if (time_after_eq (jiffies, tx->tx_deadline)) {
2856 spin_unlock(&conn->ibc_lock);
2861 list_for_each (ttmp, &conn->ibc_active_txs) {
2862 tx = list_entry (ttmp, kib_tx_t, tx_list);
2864 LASSERT (!tx->tx_queued);
2865 LASSERT (tx->tx_waiting ||
2866 tx->tx_sending != 0);
2868 if (time_after_eq (jiffies, tx->tx_deadline)) {
2869 spin_unlock(&conn->ibc_lock);
2874 spin_unlock(&conn->ibc_lock);
2879 kibnal_check_conns (int idx)
2881 struct list_head *peers = &kibnal_data.kib_peers[idx];
2882 struct list_head *ptmp;
2885 struct list_head *ctmp;
2886 unsigned long flags;
2889 /* NB. We expect to have a look at all the peers and not find any
2890 * rdmas to time out, so we just use a shared lock while we
2892 read_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2894 list_for_each (ptmp, peers) {
2895 peer = list_entry (ptmp, kib_peer_t, ibp_list);
2897 list_for_each (ctmp, &peer->ibp_conns) {
2898 conn = list_entry (ctmp, kib_conn_t, ibc_list);
2900 LASSERT (conn->ibc_state == IBNAL_CONN_ESTABLISHED);
2902 /* In case we have enough credits to return via a
2903 * NOOP, but there were no non-blocking tx descs
2904 * free to do it last time... */
2905 kibnal_check_sends(conn);
2907 if (!kibnal_conn_timed_out(conn))
2910 /* Handle timeout by closing the whole connection. We
2911 * can only be sure RDMA activity has ceased once the
2912 * QP has been modified. */
2914 kibnal_conn_addref(conn); /* 1 ref for me... */
2916 read_unlock_irqrestore(&kibnal_data.kib_global_lock,
2919 CERROR("Timed out RDMA with "LPX64"\n",
2922 kibnal_close_conn (conn, -ETIMEDOUT);
2923 kibnal_conn_decref(conn); /* ...until here */
2925 /* start again now I've dropped the lock */
2930 read_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2934 kibnal_disconnect_conn (kib_conn_t *conn)
2936 static cm_drequest_data_t dreq; /* just for the space */
2939 unsigned long flags;
2941 LASSERT (!in_interrupt());
2942 LASSERT (current == kibnal_data.kib_connd);
2944 write_lock_irqsave(&kibnal_data.kib_global_lock, flags);
2946 if (conn->ibc_disconnect) {
2947 /* Had the CM callback already */
2948 write_unlock_irqrestore(&kibnal_data.kib_global_lock,
2950 kibnal_conn_disconnected(conn);
2954 LASSERT (conn->ibc_state == IBNAL_CONN_DISCONNECT1);
2956 /* active disconnect */
2957 cmrc = cm_disconnect(conn->ibc_cep, &dreq, NULL);
2958 if (cmrc == cm_stat_success) {
2959 /* waiting for CM */
2960 conn->ibc_state = IBNAL_CONN_DISCONNECT2;
2961 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2965 write_unlock_irqrestore(&kibnal_data.kib_global_lock, flags);
2967 cm_cancel(conn->ibc_cep);
2968 kibnal_pause(HZ/10);
2970 if (!conn->ibc_disconnect) /* CM callback will never happen now */
2971 kibnal_conn_decref(conn);
2973 LASSERT (atomic_read(&conn->ibc_refcount) > 0);
2974 LASSERT (conn->ibc_state == IBNAL_CONN_DISCONNECT1);
2976 kibnal_conn_disconnected(conn);
2980 kibnal_connd (void *arg)
2983 unsigned long flags;
2991 unsigned long deadline = jiffies;
2993 kportal_daemonize ("kibnal_connd");
2994 kportal_blockallsigs ();
2996 init_waitqueue_entry (&wait, current);
2997 kibnal_data.kib_connd = current;
2999 spin_lock_irqsave (&kibnal_data.kib_connd_lock, flags);
3001 while (!kibnal_data.kib_shutdown) {
3005 if (!list_empty (&kibnal_data.kib_connd_zombies)) {
3006 conn = list_entry (kibnal_data.kib_connd_zombies.next,
3007 kib_conn_t, ibc_list);
3008 list_del (&conn->ibc_list);
3010 spin_unlock_irqrestore (&kibnal_data.kib_connd_lock, flags);
3013 kibnal_destroy_conn(conn);
3015 spin_lock_irqsave (&kibnal_data.kib_connd_lock, flags);
3018 if (!list_empty (&kibnal_data.kib_connd_pcreqs)) {
3019 pcr = list_entry(kibnal_data.kib_connd_pcreqs.next,
3020 kib_pcreq_t, pcr_list);
3021 list_del(&pcr->pcr_list);
3023 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
3026 kibnal_recv_connreq(pcr->pcr_cep, &pcr->pcr_cmreq);
3027 PORTAL_FREE(pcr, sizeof(*pcr));
3029 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
3032 if (!list_empty (&kibnal_data.kib_connd_peers)) {
3033 peer = list_entry (kibnal_data.kib_connd_peers.next,
3034 kib_peer_t, ibp_connd_list);
3036 list_del_init (&peer->ibp_connd_list);
3037 spin_unlock_irqrestore (&kibnal_data.kib_connd_lock, flags);
3040 kibnal_arp_peer (peer);
3041 kibnal_peer_decref (peer);
3043 spin_lock_irqsave (&kibnal_data.kib_connd_lock, flags);
3046 if (!list_empty (&kibnal_data.kib_connd_conns)) {
3047 conn = list_entry (kibnal_data.kib_connd_conns.next,
3048 kib_conn_t, ibc_list);
3049 list_del (&conn->ibc_list);
3051 spin_unlock_irqrestore (&kibnal_data.kib_connd_lock, flags);
3054 switch (conn->ibc_state) {
3058 case IBNAL_CONN_ACTIVE_ARP:
3059 kibnal_send_connreq(conn);
3062 case IBNAL_CONN_ACTIVE_CONNECT:
3063 kibnal_check_connreply(conn);
3066 case IBNAL_CONN_PASSIVE_WAIT:
3067 kibnal_check_passive_wait(conn);
3070 case IBNAL_CONN_DISCONNECT1:
3071 case IBNAL_CONN_DISCONNECT2:
3072 kibnal_disconnect_conn(conn);
3075 kibnal_conn_decref(conn);
3077 spin_lock_irqsave (&kibnal_data.kib_connd_lock, flags);
3080 /* careful with the jiffy wrap... */
3081 timeout = (int)(deadline - jiffies);
3085 int chunk = kibnal_data.kib_peer_hash_size;
3087 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);
3090 /* Time to check for RDMA timeouts on a few more
3091 * peers: I do checks every 'p' seconds on a
3092 * proportion of the peer table and I need to check
3093 * every connection 'n' times within a timeout
3094 * interval, to ensure I detect a timeout on any
3095 * connection within (n+1)/n times the timeout
3098 if (kibnal_tunables.kib_io_timeout > n * p)
3099 chunk = (chunk * n * p) /
3100 kibnal_tunables.kib_io_timeout;
3104 for (i = 0; i < chunk; i++) {
3105 kibnal_check_conns (peer_index);
3106 peer_index = (peer_index + 1) %
3107 kibnal_data.kib_peer_hash_size;
3111 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);
3117 /* Nothing to do for 'timeout' */
3118 set_current_state (TASK_INTERRUPTIBLE);
3119 add_wait_queue (&kibnal_data.kib_connd_waitq, &wait);
3120 spin_unlock_irqrestore (&kibnal_data.kib_connd_lock, flags);
3122 schedule_timeout (timeout);
3124 set_current_state (TASK_RUNNING);
3125 remove_wait_queue (&kibnal_data.kib_connd_waitq, &wait);
3126 spin_lock_irqsave (&kibnal_data.kib_connd_lock, flags);
3129 spin_unlock_irqrestore (&kibnal_data.kib_connd_lock, flags);
3131 kibnal_thread_fini ();
3136 kibnal_async_callback(vv_event_record_t ev)
3138 CERROR("type: %d, port: %d, data: "LPX64"\n",
3139 ev.event_type, ev.port_num, ev.type.data);
3143 kibnal_cq_callback (unsigned long unused_context)
3145 unsigned long flags;
3147 CDEBUG(D_NET, "!!\n");
3149 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3150 kibnal_data.kib_ready = 1;
3151 wake_up(&kibnal_data.kib_sched_waitq);
3152 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock, flags);
3156 kibnal_scheduler(void *arg)
3158 long id = (long)arg;
3164 unsigned long flags;
3169 snprintf(name, sizeof(name), "kibnal_sd_%02ld", id);
3170 kportal_daemonize(name);
3171 kportal_blockallsigs();
3173 init_waitqueue_entry(&wait, current);
3175 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3177 while (!kibnal_data.kib_shutdown) {
3178 if (busy_loops++ >= IBNAL_RESCHED) {
3179 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock,
3185 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3188 if (kibnal_data.kib_ready &&
3189 !kibnal_data.kib_checking_cq) {
3190 /* take ownership of completion polling */
3191 kibnal_data.kib_checking_cq = 1;
3192 /* Assume I'll exhaust the CQ */
3193 kibnal_data.kib_ready = 0;
3194 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock,
3197 vvrc = vv_poll_for_completion(kibnal_data.kib_hca,
3198 kibnal_data.kib_cq, &wc);
3199 if (vvrc == vv_return_err_cq_empty) {
3200 vvrc2 = vv_request_completion_notification(
3201 kibnal_data.kib_hca,
3203 vv_next_solicit_unsolicit_event);
3204 LASSERT (vvrc2 == vv_return_ok);
3207 if (vvrc == vv_return_ok &&
3208 kibnal_wreqid2type(wc.wr_id) == IBNAL_WID_RX) {
3209 rx = (kib_rx_t *)kibnal_wreqid2ptr(wc.wr_id);
3211 /* Grab the RX sequence number NOW before
3212 * anyone else can get an RX completion */
3213 rxseq = rx->rx_conn->ibc_rxseq++;
3216 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3217 /* give up ownership of completion polling */
3218 kibnal_data.kib_checking_cq = 0;
3220 if (vvrc == vv_return_err_cq_empty)
3223 LASSERT (vvrc == vv_return_ok);
3224 /* Assume there's more: get another scheduler to check
3225 * while I handle this completion... */
3227 kibnal_data.kib_ready = 1;
3228 wake_up(&kibnal_data.kib_sched_waitq);
3230 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock,
3233 switch (kibnal_wreqid2type(wc.wr_id)) {
3236 (kib_rx_t *)kibnal_wreqid2ptr(wc.wr_id),
3237 wc.completion_status,
3238 wc.num_bytes_transfered,
3244 (kib_tx_t *)kibnal_wreqid2ptr(wc.wr_id),
3245 wc.completion_status);
3248 case IBNAL_WID_RDMA:
3249 /* We only get RDMA completion notification if
3250 * it fails. So we just ignore them completely
3253 * 1) If an RDMA fails, all subsequent work
3254 * items, including the final SEND will fail
3255 * too, so I'm still guaranteed to notice that
3256 * this connection is hosed.
3258 * 2) It's positively dangerous to look inside
3259 * the tx descriptor obtained from an RDMA work
3260 * item. As soon as I drop the kib_sched_lock,
3261 * I give a scheduler on another CPU a chance
3262 * to get the final SEND completion, so the tx
3263 * descriptor can get freed as I inspect it. */
3264 CERROR ("RDMA failed: %d\n",
3265 wc.completion_status);
3272 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3276 /* Nothing to do; sleep... */
3278 set_current_state(TASK_INTERRUPTIBLE);
3279 add_wait_queue(&kibnal_data.kib_sched_waitq, &wait);
3280 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock,
3285 remove_wait_queue(&kibnal_data.kib_sched_waitq, &wait);
3286 set_current_state(TASK_RUNNING);
3287 spin_lock_irqsave(&kibnal_data.kib_sched_lock, flags);
3290 spin_unlock_irqrestore(&kibnal_data.kib_sched_lock, flags);
3292 kibnal_thread_fini();
3297 lib_nal_t kibnal_lib = {
3298 .libnal_data = &kibnal_data, /* NAL private data */
3299 .libnal_send = kibnal_send,
3300 .libnal_send_pages = kibnal_send_pages,
3301 .libnal_recv = kibnal_recv,
3302 .libnal_recv_pages = kibnal_recv_pages,
3303 .libnal_dist = kibnal_dist