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>
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 kranal_dist(lib_nal_t *nal, ptl_nid_t nid, unsigned long *dist)
29 /* I would guess that if kranal_get_peer (nid) == NULL,
30 and we're not routing, then 'nid' is very distant :) */
31 if ( nal->libnal_ni.ni_pid.nid == nid ) {
41 kranal_device_callback(RAP_INT32 devid)
47 for (i = 0; i < kranal_data.kra_ndevs; i++) {
49 dev = &kranal_data.kra_devices[i];
50 if (dev->rad_id != devid)
53 spin_lock_irqsave(&dev->rad_lock, flags);
55 if (!dev->rad_ready) {
57 wake_up(&dev->rad_waitq);
60 spin_unlock_irqrestore(&dev->rad_lock, flags);
64 CWARN("callback for unknown device %d\n", devid);
68 kranal_schedule_conn(kra_conn_t *conn)
70 kra_device_t *dev = conn->rac_device;
73 spin_lock_irqsave(&dev->rad_lock, flags);
75 if (!conn->rac_scheduled) {
76 kranal_conn_addref(conn); /* +1 ref for scheduler */
77 conn->rac_scheduled = 1;
78 list_add_tail(&conn->rac_schedlist, &dev->rad_connq);
79 wake_up(&dev->rad_waitq);
82 spin_unlock_irqrestore(&dev->rad_lock, flags);
86 kranal_get_idle_tx (int may_block)
92 spin_lock_irqsave(&kranal_data.kra_tx_lock, flags);
94 /* "normal" descriptor is free */
95 if (!list_empty(&kranal_data.kra_idle_txs)) {
96 tx = list_entry(kranal_data.kra_idle_txs.next,
102 /* may dip into reserve pool */
103 if (list_empty(&kranal_data.kra_idle_nblk_txs)) {
104 CERROR("reserved tx desc pool exhausted\n");
108 tx = list_entry(kranal_data.kra_idle_nblk_txs.next,
113 /* block for idle tx */
114 spin_unlock_irqrestore(&kranal_data.kra_tx_lock, flags);
116 wait_event(kranal_data.kra_idle_tx_waitq,
117 !list_empty(&kranal_data.kra_idle_txs));
121 list_del(&tx->tx_list);
123 /* Allocate a new completion cookie. It might not be
124 * needed, but we've got a lock right now... */
125 tx->tx_cookie = kranal_data.kra_next_tx_cookie++;
127 LASSERT (tx->tx_buftype == RANAL_BUF_NONE);
128 LASSERT (tx->tx_msg.ram_type == RANAL_MSG_NONE);
129 LASSERT (tx->tx_conn == NULL);
130 LASSERT (tx->tx_libmsg[0] == NULL);
131 LASSERT (tx->tx_libmsg[1] == NULL);
134 spin_unlock_irqrestore(&kranal_data.kra_tx_lock, flags);
140 kranal_init_msg(kra_msg_t *msg, int type)
142 msg->ram_magic = RANAL_MSG_MAGIC;
143 msg->ram_version = RANAL_MSG_VERSION;
144 msg->ram_type = type;
145 msg->ram_srcnid = kranal_lib.libnal_ni.ni_pid.nid;
146 /* ram_connstamp gets set when FMA is sent */
150 kranal_new_tx_msg (int may_block, int type)
152 kra_tx_t *tx = kranal_get_idle_tx(may_block);
157 kranal_init_msg(&tx->tx_msg, type);
162 kranal_setup_immediate_buffer (kra_tx_t *tx, int niov, struct iovec *iov,
166 /* For now this is almost identical to kranal_setup_virt_buffer, but we
167 * could "flatten" the payload into a single contiguous buffer ready
168 * for sending direct over an FMA if we ever needed to. */
172 LASSERT (tx->tx_buftype == RANAL_BUF_NONE);
174 while (offset >= iov->iov_len) {
175 offset -= iov->iov_len;
181 if (nob > iov->iov_len - offset) {
182 CERROR("Can't handle multiple vaddr fragments\n");
186 tx->tx_buftype = RANAL_BUF_IMMEDIATE;
188 tx->tx_buffer = (void *)(((unsigned long)iov->iov_base) + offset);
193 kranal_setup_virt_buffer (kra_tx_t *tx, int niov, struct iovec *iov,
199 LASSERT (tx->tx_buftype == RANAL_BUF_NONE);
201 while (offset >= iov->iov_len) {
202 offset -= iov->iov_len;
208 if (nob > iov->iov_len - offset) {
209 CERROR("Can't handle multiple vaddr fragments\n");
213 tx->tx_buftype = RANAL_BUF_VIRT_UNMAPPED;
215 tx->tx_buffer = (void *)(((unsigned long)iov->iov_base) + offset);
220 kranal_setup_phys_buffer (kra_tx_t *tx, int nkiov, ptl_kiov_t *kiov,
223 RAP_PHYS_REGION *phys = tx->tx_phys;
226 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
230 LASSERT (tx->tx_buftype == RANAL_BUF_NONE);
232 while (offset >= kiov->kiov_len) {
233 offset -= kiov->kiov_len;
239 tx->tx_buftype = RANAL_BUF_PHYS_UNMAPPED;
241 tx->tx_buffer = (void *)((unsigned long)(kiov->kiov_offset + offset));
243 phys->Address = kranal_page2phys(kiov->kiov_page);
246 resid = nob - (kiov->kiov_len - offset);
252 if (kiov->kiov_offset != 0 ||
253 ((resid > PAGE_SIZE) &&
254 kiov->kiov_len < PAGE_SIZE)) {
255 /* Can't have gaps */
256 CERROR("Can't make payload contiguous in I/O VM:"
257 "page %d, offset %d, len %d \n",
259 kiov->kiov_offset, kiov->kiov_len);
263 if ((phys - tx->tx_phys) == PTL_MD_MAX_IOV) {
264 CERROR ("payload too big (%d)\n", phys - tx->tx_phys);
268 phys->Address = kranal_page2phys(kiov->kiov_page);
274 tx->tx_phys_npages = phys - tx->tx_phys;
279 kranal_setup_rdma_buffer (kra_tx_t *tx, int niov,
280 struct iovec *iov, ptl_kiov_t *kiov,
283 LASSERT ((iov == NULL) != (kiov == NULL));
286 return kranal_setup_phys_buffer(tx, niov, kiov, offset, nob);
288 return kranal_setup_virt_buffer(tx, niov, iov, offset, nob);
292 kranal_map_buffer (kra_tx_t *tx)
294 kra_conn_t *conn = tx->tx_conn;
295 kra_device_t *dev = conn->rac_device;
298 LASSERT (current == dev->rad_scheduler);
300 switch (tx->tx_buftype) {
305 case RANAL_BUF_IMMEDIATE:
306 case RANAL_BUF_PHYS_MAPPED:
307 case RANAL_BUF_VIRT_MAPPED:
310 case RANAL_BUF_PHYS_UNMAPPED:
311 rrc = RapkRegisterPhys(dev->rad_handle,
312 tx->tx_phys, tx->tx_phys_npages,
314 LASSERT (rrc == RAP_SUCCESS);
315 tx->tx_buftype = RANAL_BUF_PHYS_MAPPED;
318 case RANAL_BUF_VIRT_UNMAPPED:
319 rrc = RapkRegisterMemory(dev->rad_handle,
320 tx->tx_buffer, tx->tx_nob,
322 LASSERT (rrc == RAP_SUCCESS);
323 tx->tx_buftype = RANAL_BUF_VIRT_MAPPED;
329 kranal_unmap_buffer (kra_tx_t *tx)
334 switch (tx->tx_buftype) {
339 case RANAL_BUF_IMMEDIATE:
340 case RANAL_BUF_PHYS_UNMAPPED:
341 case RANAL_BUF_VIRT_UNMAPPED:
344 case RANAL_BUF_PHYS_MAPPED:
345 LASSERT (tx->tx_conn != NULL);
346 dev = tx->tx_conn->rac_device;
347 LASSERT (current == dev->rad_scheduler);
348 rrc = RapkDeregisterMemory(dev->rad_handle, NULL,
350 LASSERT (rrc == RAP_SUCCESS);
351 tx->tx_buftype = RANAL_BUF_PHYS_UNMAPPED;
354 case RANAL_BUF_VIRT_MAPPED:
355 LASSERT (tx->tx_conn != NULL);
356 dev = tx->tx_conn->rac_device;
357 LASSERT (current == dev->rad_scheduler);
358 rrc = RapkDeregisterMemory(dev->rad_handle, tx->tx_buffer,
360 LASSERT (rrc == RAP_SUCCESS);
361 tx->tx_buftype = RANAL_BUF_VIRT_UNMAPPED;
367 kranal_tx_done (kra_tx_t *tx, int completion)
369 ptl_err_t ptlrc = (completion == 0) ? PTL_OK : PTL_FAIL;
373 LASSERT (!in_interrupt());
375 kranal_unmap_buffer(tx);
377 for (i = 0; i < 2; i++) {
378 /* tx may have up to 2 libmsgs to finalise */
379 if (tx->tx_libmsg[i] == NULL)
382 lib_finalize(&kranal_lib, NULL, tx->tx_libmsg[i], ptlrc);
383 tx->tx_libmsg[i] = NULL;
386 tx->tx_buftype = RANAL_BUF_NONE;
387 tx->tx_msg.ram_type = RANAL_MSG_NONE;
390 spin_lock_irqsave(&kranal_data.kra_tx_lock, flags);
393 list_add_tail(&tx->tx_list, &kranal_data.kra_idle_nblk_txs);
395 list_add_tail(&tx->tx_list, &kranal_data.kra_idle_txs);
396 wake_up(&kranal_data.kra_idle_tx_waitq);
399 spin_unlock_irqrestore(&kranal_data.kra_tx_lock, flags);
403 kranal_find_conn_locked (kra_peer_t *peer)
405 struct list_head *tmp;
407 /* just return the first connection */
408 list_for_each (tmp, &peer->rap_conns) {
409 return list_entry(tmp, kra_conn_t, rac_list);
416 kranal_post_fma (kra_conn_t *conn, kra_tx_t *tx)
422 spin_lock_irqsave(&conn->rac_lock, flags);
423 list_add_tail(&tx->tx_list, &conn->rac_fmaq);
424 tx->tx_qtime = jiffies;
425 spin_unlock_irqrestore(&conn->rac_lock, flags);
427 kranal_schedule_conn(conn);
431 kranal_launch_tx (kra_tx_t *tx, ptl_nid_t nid)
437 rwlock_t *g_lock = &kranal_data.kra_global_lock;
439 /* If I get here, I've committed to send, so I complete the tx with
440 * failure on any problems */
442 LASSERT (tx->tx_conn == NULL); /* only set when assigned a conn */
446 peer = kranal_find_peer_locked(nid);
449 kranal_tx_done(tx, -EHOSTUNREACH);
453 conn = kranal_find_conn_locked(peer);
455 kranal_post_fma(conn, tx);
460 /* Making one or more connections; I'll need a write lock... */
462 write_lock_irqsave(g_lock, flags);
464 peer = kranal_find_peer_locked(nid);
466 write_unlock_irqrestore(g_lock, flags);
467 kranal_tx_done(tx, -EHOSTUNREACH);
471 conn = kranal_find_conn_locked(peer);
473 /* Connection exists; queue message on it */
474 kranal_post_fma(conn, tx);
475 write_unlock_irqrestore(g_lock, flags);
479 LASSERT (peer->rap_persistence > 0);
481 if (!peer->rap_connecting) {
482 LASSERT (list_empty(&peer->rap_tx_queue));
485 if (now < peer->rap_reconnect_time) {
486 write_unlock_irqrestore(g_lock, flags);
487 kranal_tx_done(tx, -EHOSTUNREACH);
491 peer->rap_connecting = 1;
492 kranal_peer_addref(peer); /* extra ref for connd */
494 spin_lock(&kranal_data.kra_connd_lock);
496 list_add_tail(&peer->rap_connd_list,
497 &kranal_data.kra_connd_peers);
498 wake_up(&kranal_data.kra_connd_waitq);
500 spin_unlock(&kranal_data.kra_connd_lock);
503 /* A connection is being established; queue the message... */
504 list_add_tail(&tx->tx_list, &peer->rap_tx_queue);
506 write_unlock_irqrestore(g_lock, flags);
510 kranal_rdma(kra_tx_t *tx, int type,
511 kra_rdma_desc_t *sink, int nob, __u64 cookie)
513 kra_conn_t *conn = tx->tx_conn;
517 LASSERT (kranal_tx_mapped(tx));
518 LASSERT (nob <= sink->rard_nob);
519 LASSERT (nob <= tx->tx_nob);
521 /* No actual race with scheduler sending CLOSE (I'm she!) */
522 LASSERT (current == conn->rac_device->rad_scheduler);
524 memset(&tx->tx_rdma_desc, 0, sizeof(tx->tx_rdma_desc));
525 tx->tx_rdma_desc.SrcPtr.AddressBits = (__u64)((unsigned long)tx->tx_buffer);
526 tx->tx_rdma_desc.SrcKey = tx->tx_map_key;
527 tx->tx_rdma_desc.DstPtr = sink->rard_addr;
528 tx->tx_rdma_desc.DstKey = sink->rard_key;
529 tx->tx_rdma_desc.Length = nob;
530 tx->tx_rdma_desc.AppPtr = tx;
532 /* prep final completion message */
533 kranal_init_msg(&tx->tx_msg, type);
534 tx->tx_msg.ram_u.completion.racm_cookie = cookie;
536 if (nob == 0) { /* Immediate completion */
537 kranal_post_fma(conn, tx);
541 LASSERT (!conn->rac_close_sent); /* Don't lie (CLOSE == RDMA idle) */
543 rrc = RapkPostRdma(conn->rac_rihandle, &tx->tx_rdma_desc);
544 LASSERT (rrc == RAP_SUCCESS);
546 spin_lock_irqsave(&conn->rac_lock, flags);
547 list_add_tail(&tx->tx_list, &conn->rac_rdmaq);
548 tx->tx_qtime = jiffies;
549 spin_unlock_irqrestore(&conn->rac_lock, flags);
553 kranal_consume_rxmsg (kra_conn_t *conn, void *buffer, int nob)
555 __u32 nob_received = nob;
558 LASSERT (conn->rac_rxmsg != NULL);
560 rrc = RapkFmaCopyOut(conn->rac_rihandle, buffer,
561 &nob_received, sizeof(kra_msg_t));
562 LASSERT (rrc == RAP_SUCCESS);
564 conn->rac_rxmsg = NULL;
566 if (nob_received != nob) {
567 CWARN("Expected %d immediate bytes but got %d\n",
576 kranal_do_send (lib_nal_t *nal,
593 /* NB 'private' is different depending on what we're sending.... */
595 CDEBUG(D_NET, "sending "LPSZ" bytes in %d frags to nid:"LPX64
596 " pid %d\n", nob, niov, nid , pid);
598 LASSERT (nob == 0 || niov > 0);
599 LASSERT (niov <= PTL_MD_MAX_IOV);
601 LASSERT (!in_interrupt());
602 /* payload is either all vaddrs or all pages */
603 LASSERT (!(kiov != NULL && iov != NULL));
609 case PTL_MSG_REPLY: {
610 /* reply's 'private' is the conn that received the GET_REQ */
612 LASSERT (conn->rac_rxmsg != NULL);
614 if (conn->rac_rxmsg->ram_type == RANAL_MSG_IMMEDIATE) {
615 if (nob > RANAL_FMA_MAX_DATA) {
616 CERROR("Can't REPLY IMMEDIATE %d to "LPX64"\n",
620 break; /* RDMA not expected */
623 /* Incoming message consistent with immediate reply? */
624 if (conn->rac_rxmsg->ram_type != RANAL_MSG_GET_REQ) {
625 CERROR("REPLY to "LPX64" bad msg type %x!!!\n",
626 nid, conn->rac_rxmsg->ram_type);
630 tx = kranal_get_idle_tx(0);
634 rc = kranal_setup_rdma_buffer(tx, niov, iov, kiov, offset, nob);
636 kranal_tx_done(tx, rc);
641 tx->tx_libmsg[0] = libmsg;
643 kranal_map_buffer(tx);
644 kranal_rdma(tx, RANAL_MSG_GET_DONE,
645 &conn->rac_rxmsg->ram_u.get.ragm_desc, nob,
646 conn->rac_rxmsg->ram_u.get.ragm_cookie);
653 /* We have to consider the eventual sink buffer rather than any
654 * payload passed here (there isn't any, and strictly, looking
655 * inside libmsg is a layering violation). We send a simple
656 * IMMEDIATE GET if the sink buffer is mapped already and small
659 if ((libmsg->md->options & PTL_MD_KIOV) == 0 &&
660 libmsg->md->length <= RANAL_FMA_MAX_DATA &&
661 libmsg->md->length <= kranal_tunables.kra_max_immediate)
664 tx = kranal_new_tx_msg(!in_interrupt(), RANAL_MSG_GET_REQ);
668 if ((libmsg->md->options & PTL_MD_KIOV) == 0)
669 rc = kranal_setup_virt_buffer(tx, libmsg->md->md_niov,
670 libmsg->md->md_iov.iov,
671 0, libmsg->md->length);
673 rc = kranal_setup_phys_buffer(tx, libmsg->md->md_niov,
674 libmsg->md->md_iov.kiov,
675 0, libmsg->md->length);
677 kranal_tx_done(tx, rc);
681 tx->tx_libmsg[1] = lib_create_reply_msg(&kranal_lib, nid, libmsg);
682 if (tx->tx_libmsg[1] == NULL) {
683 CERROR("Can't create reply for GET to "LPX64"\n", nid);
684 kranal_tx_done(tx, rc);
688 tx->tx_libmsg[0] = libmsg;
689 tx->tx_msg.ram_u.get.ragm_hdr = *hdr;
690 /* rest of tx_msg is setup just before it is sent */
691 kranal_launch_tx(tx, nid);
699 if (kiov == NULL && /* not paged */
700 nob <= RANAL_MAX_IMMEDIATE && /* small enough */
701 nob <= kranal_tunables.kra_max_immediate)
702 break; /* send IMMEDIATE */
704 tx = kranal_new_tx_msg(!in_interrupt(), RANAL_MSG_PUT_REQ);
708 rc = kranal_setup_rdma_buffer(tx, niov, iov, kiov, offset, nob);
710 kranal_tx_done(tx, rc);
714 tx->tx_libmsg[0] = libmsg;
715 tx->tx_msg.ram_u.putreq.raprm_hdr = *hdr;
716 /* rest of tx_msg is setup just before it is sent */
717 kranal_launch_tx(tx, nid);
721 LASSERT (kiov == NULL);
722 LASSERT (nob <= RANAL_MAX_IMMEDIATE);
724 tx = kranal_new_tx_msg(!(type == PTL_MSG_ACK ||
725 type == PTL_MSG_REPLY ||
727 RANAL_MSG_IMMEDIATE);
731 rc = kranal_setup_immediate_buffer(tx, niov, iov, offset, nob);
733 kranal_tx_done(tx, rc);
737 tx->tx_msg.ram_u.immediate.raim_hdr = *hdr;
738 tx->tx_libmsg[0] = libmsg;
739 kranal_launch_tx(tx, nid);
744 kranal_send (lib_nal_t *nal, void *private, lib_msg_t *cookie,
745 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
746 unsigned int niov, struct iovec *iov,
747 size_t offset, size_t len)
749 return kranal_do_send(nal, private, cookie,
756 kranal_send_pages (lib_nal_t *nal, void *private, lib_msg_t *cookie,
757 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
758 unsigned int niov, ptl_kiov_t *kiov,
759 size_t offset, size_t len)
761 return kranal_do_send(nal, private, cookie,
768 kranal_recvmsg (lib_nal_t *nal, void *private, lib_msg_t *libmsg,
769 unsigned int niov, struct iovec *iov, ptl_kiov_t *kiov,
770 size_t offset, size_t mlen, size_t rlen)
772 kra_conn_t *conn = private;
773 kra_msg_t *rxmsg = conn->rac_rxmsg;
778 LASSERT (mlen <= rlen);
779 LASSERT (!in_interrupt());
780 /* Either all pages or all vaddrs */
781 LASSERT (!(kiov != NULL && iov != NULL));
783 switch(rxmsg->ram_type) {
788 case RANAL_MSG_IMMEDIATE:
791 } else if (kiov != NULL) {
792 CERROR("Can't recv immediate into paged buffer\n");
796 while (offset >= iov->iov_len) {
797 offset -= iov->iov_len;
802 if (mlen > iov->iov_len - offset) {
803 CERROR("Can't handle immediate frags\n");
806 buffer = ((char *)iov->iov_base) + offset;
808 rc = kranal_consume_rxmsg(conn, buffer, mlen);
809 lib_finalize(nal, NULL, libmsg, (rc == 0) ? PTL_OK : PTL_FAIL);
812 case RANAL_MSG_GET_REQ:
813 /* If the GET matched, we've already handled it in
814 * kranal_do_send which is called to send the REPLY. We're
815 * only called here to complete the GET receive (if we needed
816 * it which we don't, but I digress...) */
817 LASSERT (libmsg == NULL);
818 lib_finalize(nal, NULL, libmsg, PTL_OK);
821 case RANAL_MSG_PUT_REQ:
822 if (libmsg == NULL) { /* PUT didn't match... */
823 lib_finalize(nal, NULL, libmsg, PTL_OK);
827 tx = kranal_new_tx_msg(0, RANAL_MSG_PUT_ACK);
831 rc = kranal_setup_rdma_buffer(tx, niov, iov, kiov, offset, mlen);
833 kranal_tx_done(tx, rc);
838 kranal_map_buffer(tx);
840 tx->tx_msg.ram_u.putack.rapam_src_cookie =
841 conn->rac_rxmsg->ram_u.putreq.raprm_cookie;
842 tx->tx_msg.ram_u.putack.rapam_dst_cookie = tx->tx_cookie;
843 tx->tx_msg.ram_u.putack.rapam_desc.rard_key = tx->tx_map_key;
844 tx->tx_msg.ram_u.putack.rapam_desc.rard_addr.AddressBits =
845 (__u64)((unsigned long)tx->tx_buffer);
846 tx->tx_msg.ram_u.putack.rapam_desc.rard_nob = mlen;
848 tx->tx_libmsg[0] = libmsg; /* finalize this on RDMA_DONE */
850 kranal_post_fma(conn, tx);
852 /* flag matched by consuming rx message */
853 kranal_consume_rxmsg(conn, NULL, 0);
859 kranal_recv (lib_nal_t *nal, void *private, lib_msg_t *msg,
860 unsigned int niov, struct iovec *iov,
861 size_t offset, size_t mlen, size_t rlen)
863 return kranal_recvmsg(nal, private, msg, niov, iov, NULL,
868 kranal_recv_pages (lib_nal_t *nal, void *private, lib_msg_t *msg,
869 unsigned int niov, ptl_kiov_t *kiov,
870 size_t offset, size_t mlen, size_t rlen)
872 return kranal_recvmsg(nal, private, msg, niov, NULL, kiov,
877 kranal_thread_start (int(*fn)(void *arg), void *arg)
879 long pid = kernel_thread(fn, arg, 0);
884 atomic_inc(&kranal_data.kra_nthreads);
889 kranal_thread_fini (void)
891 atomic_dec(&kranal_data.kra_nthreads);
895 kranal_check_conn_timeouts (kra_conn_t *conn)
898 struct list_head *ttmp;
901 unsigned long now = jiffies;
903 LASSERT (conn->rac_state == RANAL_CONN_ESTABLISHED ||
904 conn->rac_state == RANAL_CONN_CLOSING);
906 if (!conn->rac_close_sent &&
907 time_after_eq(now, conn->rac_last_tx + conn->rac_keepalive * HZ)) {
908 /* not sent in a while; schedule conn so scheduler sends a keepalive */
909 kranal_schedule_conn(conn);
912 timeout = conn->rac_timeout * HZ;
914 if (!conn->rac_close_recvd &&
915 time_after_eq(now, conn->rac_last_rx + timeout)) {
916 CERROR("Nothing received from "LPX64" within %lu seconds\n",
917 conn->rac_peer->rap_nid, (now - conn->rac_last_rx)/HZ);
921 if (conn->rac_state != RANAL_CONN_ESTABLISHED)
924 /* Check the conn's queues are moving. These are "belt+braces" checks,
925 * in case of hardware/software errors that make this conn seem
926 * responsive even though it isn't progressing its message queues. */
928 spin_lock_irqsave(&conn->rac_lock, flags);
930 list_for_each (ttmp, &conn->rac_fmaq) {
931 tx = list_entry(ttmp, kra_tx_t, tx_list);
933 if (time_after_eq(now, tx->tx_qtime + timeout)) {
934 spin_unlock_irqrestore(&conn->rac_lock, flags);
935 CERROR("tx on fmaq for "LPX64" blocked %lu seconds\n",
936 conn->rac_peer->rap_nid, (now - tx->tx_qtime)/HZ);
941 list_for_each (ttmp, &conn->rac_rdmaq) {
942 tx = list_entry(ttmp, kra_tx_t, tx_list);
944 if (time_after_eq(now, tx->tx_qtime + timeout)) {
945 spin_unlock_irqrestore(&conn->rac_lock, flags);
946 CERROR("tx on rdmaq for "LPX64" blocked %lu seconds\n",
947 conn->rac_peer->rap_nid, (now - tx->tx_qtime)/HZ);
952 list_for_each (ttmp, &conn->rac_replyq) {
953 tx = list_entry(ttmp, kra_tx_t, tx_list);
955 if (time_after_eq(now, tx->tx_qtime + timeout)) {
956 spin_unlock_irqrestore(&conn->rac_lock, flags);
957 CERROR("tx on replyq for "LPX64" blocked %lu seconds\n",
958 conn->rac_peer->rap_nid, (now - tx->tx_qtime)/HZ);
963 spin_unlock_irqrestore(&conn->rac_lock, flags);
968 kranal_reaper_check (int idx, unsigned long *min_timeoutp)
970 struct list_head *conns = &kranal_data.kra_conns[idx];
971 struct list_head *ctmp;
977 /* NB. We expect to check all the conns and not find any problems, so
978 * we just use a shared lock while we take a look... */
979 read_lock(&kranal_data.kra_global_lock);
981 list_for_each (ctmp, conns) {
982 conn = list_entry(ctmp, kra_conn_t, rac_hashlist);
984 if (conn->rac_timeout < *min_timeoutp )
985 *min_timeoutp = conn->rac_timeout;
986 if (conn->rac_keepalive < *min_timeoutp )
987 *min_timeoutp = conn->rac_keepalive;
989 rc = kranal_check_conn_timeouts(conn);
993 kranal_conn_addref(conn);
994 read_unlock(&kranal_data.kra_global_lock);
996 CERROR("Conn to "LPX64", cqid %d timed out\n",
997 conn->rac_peer->rap_nid, conn->rac_cqid);
999 write_lock_irqsave(&kranal_data.kra_global_lock, flags);
1001 switch (conn->rac_state) {
1005 case RANAL_CONN_ESTABLISHED:
1006 kranal_close_conn_locked(conn, -ETIMEDOUT);
1009 case RANAL_CONN_CLOSING:
1010 kranal_terminate_conn_locked(conn);
1014 write_unlock_irqrestore(&kranal_data.kra_global_lock, flags);
1016 kranal_conn_decref(conn);
1018 /* start again now I've dropped the lock */
1022 read_unlock(&kranal_data.kra_global_lock);
1026 kranal_connd (void *arg)
1030 unsigned long flags;
1032 kra_acceptsock_t *ras;
1035 snprintf(name, sizeof(name), "kranal_connd_%02ld", (long)arg);
1036 kportal_daemonize(name);
1037 kportal_blockallsigs();
1039 init_waitqueue_entry(&wait, current);
1041 spin_lock_irqsave(&kranal_data.kra_connd_lock, flags);
1043 while (!kranal_data.kra_shutdown) {
1046 if (!list_empty(&kranal_data.kra_connd_acceptq)) {
1047 ras = list_entry(kranal_data.kra_connd_acceptq.next,
1048 kra_acceptsock_t, ras_list);
1049 list_del(&ras->ras_list);
1050 spin_unlock_irqrestore(&kranal_data.kra_connd_lock, flags);
1052 kranal_conn_handshake(ras->ras_sock, NULL);
1053 sock_release(ras->ras_sock);
1054 PORTAL_FREE(ras, sizeof(*ras));
1056 spin_lock_irqsave(&kranal_data.kra_connd_lock, flags);
1060 if (!list_empty(&kranal_data.kra_connd_peers)) {
1061 peer = list_entry(kranal_data.kra_connd_peers.next,
1062 kra_peer_t, rap_connd_list);
1064 list_del_init(&peer->rap_connd_list);
1065 spin_unlock_irqrestore(&kranal_data.kra_connd_lock, flags);
1067 kranal_connect(peer);
1068 kranal_peer_decref(peer);
1070 spin_lock_irqsave(&kranal_data.kra_connd_lock, flags);
1077 set_current_state(TASK_INTERRUPTIBLE);
1078 add_wait_queue(&kranal_data.kra_connd_waitq, &wait);
1080 spin_unlock_irqrestore(&kranal_data.kra_connd_lock, flags);
1084 set_current_state(TASK_RUNNING);
1085 remove_wait_queue(&kranal_data.kra_connd_waitq, &wait);
1087 spin_lock_irqsave(&kranal_data.kra_connd_lock, flags);
1090 spin_unlock_irqrestore(&kranal_data.kra_connd_lock, flags);
1092 kranal_thread_fini();
1097 kranal_update_reaper_timeout(long timeout)
1099 unsigned long flags;
1101 LASSERT (timeout > 0);
1103 spin_lock_irqsave(&kranal_data.kra_reaper_lock, flags);
1105 if (timeout < kranal_data.kra_new_min_timeout)
1106 kranal_data.kra_new_min_timeout = timeout;
1108 spin_unlock_irqrestore(&kranal_data.kra_reaper_lock, flags);
1112 kranal_reaper (void *arg)
1115 unsigned long flags;
1118 int conn_entries = kranal_data.kra_conn_hash_size;
1120 int base_index = conn_entries - 1;
1121 unsigned long next_check_time = jiffies;
1122 long next_min_timeout = MAX_SCHEDULE_TIMEOUT;
1123 long current_min_timeout = 1;
1125 kportal_daemonize("kranal_reaper");
1126 kportal_blockallsigs();
1128 init_waitqueue_entry(&wait, current);
1130 spin_lock_irqsave(&kranal_data.kra_reaper_lock, flags);
1132 while (!kranal_data.kra_shutdown) {
1134 /* careful with the jiffy wrap... */
1135 timeout = (long)(next_check_time - jiffies);
1138 /* I wake up every 'p' seconds to check for
1139 * timeouts on some more peers. I try to check
1140 * every connection 'n' times within the global
1141 * minimum of all keepalive and timeout intervals,
1142 * to ensure I attend to every connection within
1143 * (n+1)/n times its timeout intervals. */
1147 unsigned long min_timeout;
1150 if (kranal_data.kra_new_min_timeout != MAX_SCHEDULE_TIMEOUT) {
1151 /* new min timeout set: restart min timeout scan */
1152 next_min_timeout = MAX_SCHEDULE_TIMEOUT;
1153 base_index = conn_index - 1;
1155 base_index = conn_entries - 1;
1157 if (kranal_data.kra_new_min_timeout < current_min_timeout) {
1158 current_min_timeout = kranal_data.kra_new_min_timeout;
1159 CWARN("Set new min timeout %ld\n",
1160 current_min_timeout);
1163 kranal_data.kra_new_min_timeout = MAX_SCHEDULE_TIMEOUT;
1165 min_timeout = current_min_timeout;
1167 spin_unlock_irqrestore(&kranal_data.kra_reaper_lock,
1170 LASSERT (min_timeout > 0);
1172 /* Compute how many table entries to check now so I
1173 * get round the whole table fast enough (NB I do
1174 * this at fixed intervals of 'p' seconds) */
1175 chunk = conn_entries;
1176 if (min_timeout > n * p)
1177 chunk = (chunk * n * p) / min_timeout;
1181 for (i = 0; i < chunk; i++) {
1182 kranal_reaper_check(conn_index,
1184 conn_index = (conn_index + 1) % conn_entries;
1187 next_check_time += p * HZ;
1189 spin_lock_irqsave(&kranal_data.kra_reaper_lock, flags);
1191 if (((conn_index - chunk <= base_index &&
1192 base_index < conn_index) ||
1193 (conn_index - conn_entries - chunk <= base_index &&
1194 base_index < conn_index - conn_entries))) {
1196 /* Scanned all conns: set current_min_timeout... */
1197 if (current_min_timeout != next_min_timeout) {
1198 current_min_timeout = next_min_timeout;
1199 CWARN("Set new min timeout %ld\n",
1200 current_min_timeout);
1203 /* ...and restart min timeout scan */
1204 next_min_timeout = MAX_SCHEDULE_TIMEOUT;
1205 base_index = conn_index - 1;
1207 base_index = conn_entries - 1;
1211 set_current_state(TASK_INTERRUPTIBLE);
1212 add_wait_queue(&kranal_data.kra_reaper_waitq, &wait);
1214 spin_unlock_irqrestore(&kranal_data.kra_reaper_lock, flags);
1216 schedule_timeout(timeout);
1218 spin_lock_irqsave(&kranal_data.kra_reaper_lock, flags);
1220 set_current_state(TASK_RUNNING);
1221 remove_wait_queue(&kranal_data.kra_reaper_waitq, &wait);
1224 kranal_thread_fini();
1229 kranal_check_rdma_cq (kra_device_t *dev)
1234 unsigned long flags;
1235 RAP_RDMA_DESCRIPTOR *desc;
1240 rrc = RapkCQDone(dev->rad_rdma_cqh, &cqid, &event_type);
1241 if (rrc == RAP_NOT_DONE)
1244 LASSERT (rrc == RAP_SUCCESS);
1245 LASSERT ((event_type & RAPK_CQ_EVENT_OVERRUN) == 0);
1247 read_lock(&kranal_data.kra_global_lock);
1249 conn = kranal_cqid2conn_locked(cqid);
1251 /* Conn was destroyed? */
1252 CWARN("RDMA CQID lookup %d failed\n", cqid);
1253 read_unlock(&kranal_data.kra_global_lock);
1257 rrc = RapkRdmaDone(conn->rac_rihandle, &desc);
1258 LASSERT (rrc == RAP_SUCCESS);
1260 spin_lock_irqsave(&conn->rac_lock, flags);
1262 LASSERT (!list_empty(&conn->rac_rdmaq));
1263 tx = list_entry(conn->rac_rdmaq.next, kra_tx_t, tx_list);
1264 list_del(&tx->tx_list);
1266 LASSERT(desc->AppPtr == (void *)tx);
1267 LASSERT(tx->tx_msg.ram_type == RANAL_MSG_PUT_DONE ||
1268 tx->tx_msg.ram_type == RANAL_MSG_GET_DONE);
1270 list_add_tail(&tx->tx_list, &conn->rac_fmaq);
1271 tx->tx_qtime = jiffies;
1273 spin_unlock_irqrestore(&conn->rac_lock, flags);
1275 /* Get conn's fmaq processed, now I've just put something
1277 kranal_schedule_conn(conn);
1279 read_unlock(&kranal_data.kra_global_lock);
1284 kranal_check_fma_cq (kra_device_t *dev)
1290 struct list_head *conns;
1291 struct list_head *tmp;
1295 rrc = RapkCQDone(dev->rad_fma_cqh, &cqid, &event_type);
1296 if (rrc != RAP_NOT_DONE)
1299 LASSERT (rrc == RAP_SUCCESS);
1301 if ((event_type & RAPK_CQ_EVENT_OVERRUN) == 0) {
1303 read_lock(&kranal_data.kra_global_lock);
1305 conn = kranal_cqid2conn_locked(cqid);
1307 CWARN("FMA CQID lookup %d failed\n", cqid);
1309 kranal_schedule_conn(conn);
1311 read_unlock(&kranal_data.kra_global_lock);
1315 /* FMA CQ has overflowed: check ALL conns */
1316 CWARN("Scheduling ALL conns on device %d\n", dev->rad_id);
1318 for (i = 0; i < kranal_data.kra_conn_hash_size; i++) {
1320 read_lock(&kranal_data.kra_global_lock);
1322 conns = &kranal_data.kra_conns[i];
1324 list_for_each (tmp, conns) {
1325 conn = list_entry(tmp, kra_conn_t,
1328 if (conn->rac_device == dev)
1329 kranal_schedule_conn(conn);
1332 /* don't block write lockers for too long... */
1333 read_unlock(&kranal_data.kra_global_lock);
1339 kranal_sendmsg(kra_conn_t *conn, kra_msg_t *msg,
1340 void *immediate, int immediatenob)
1342 int sync = (msg->ram_type & RANAL_MSG_FENCE) != 0;
1345 LASSERT (sizeof(*msg) <= RANAL_FMA_MAX_PREFIX);
1346 LASSERT ((msg->ram_type == RANAL_MSG_IMMEDIATE) ?
1347 immediatenob <= RANAL_FMA_MAX_DATA :
1350 msg->ram_connstamp = conn->rac_my_connstamp;
1351 msg->ram_seq = conn->rac_tx_seq;
1354 rrc = RapkFmaSyncSend(conn->rac_device->rad_handle,
1355 immediate, immediatenob,
1358 rrc = RapkFmaSend(conn->rac_device->rad_handle,
1359 immediate, immediatenob,
1367 conn->rac_last_tx = jiffies;
1377 kranal_process_fmaq (kra_conn_t *conn)
1379 unsigned long flags;
1385 /* NB 1. kranal_sendmsg() may fail if I'm out of credits right now.
1386 * However I will be rescheduled some by an FMA completion event
1387 * when I eventually get some.
1388 * NB 2. Sampling rac_state here, races with setting it elsewhere
1389 * kranal_close_conn_locked. But it doesn't matter if I try to
1390 * send a "real" message just as I start closing because I'll get
1391 * scheduled to send the close anyway. */
1393 if (conn->rac_state != RANAL_CONN_ESTABLISHED) {
1394 if (!list_empty(&conn->rac_rdmaq)) {
1395 /* RDMAs in progress */
1396 LASSERT (!conn->rac_close_sent);
1398 if (time_after_eq(jiffies,
1400 conn->rac_keepalive)) {
1401 kranal_init_msg(&conn->rac_msg, RANAL_MSG_NOOP);
1402 kranal_sendmsg(conn, &conn->rac_msg, NULL, 0);
1407 if (conn->rac_close_sent)
1410 kranal_init_msg(&conn->rac_msg, RANAL_MSG_CLOSE);
1411 rc = kranal_sendmsg(conn, &conn->rac_msg, NULL, 0);
1415 conn->rac_close_sent = 1;
1416 if (!conn->rac_close_recvd)
1419 write_lock_irqsave(&kranal_data.kra_global_lock, flags);
1421 if (conn->rac_state == RANAL_CONN_CLOSING)
1422 kranal_terminate_conn_locked(conn);
1424 write_unlock_irqrestore(&kranal_data.kra_global_lock, flags);
1428 spin_lock_irqsave(&conn->rac_lock, flags);
1430 if (list_empty(&conn->rac_fmaq)) {
1432 spin_unlock_irqrestore(&conn->rac_lock, flags);
1434 if (time_after_eq(jiffies,
1435 conn->rac_last_tx + conn->rac_keepalive)) {
1436 kranal_init_msg(&conn->rac_msg, RANAL_MSG_NOOP);
1437 kranal_sendmsg(conn, &conn->rac_msg, NULL, 0);
1442 tx = list_entry(conn->rac_fmaq.next, kra_tx_t, tx_list);
1443 list_del(&tx->tx_list);
1444 more_to_do = !list_empty(&conn->rac_fmaq);
1446 spin_unlock_irqrestore(&conn->rac_lock, flags);
1449 switch (tx->tx_msg.ram_type) {
1453 case RANAL_MSG_IMMEDIATE:
1454 case RANAL_MSG_PUT_NAK:
1455 case RANAL_MSG_PUT_DONE:
1456 case RANAL_MSG_GET_NAK:
1457 case RANAL_MSG_GET_DONE:
1458 rc = kranal_sendmsg(conn, &tx->tx_msg,
1459 tx->tx_buffer, tx->tx_nob);
1463 case RANAL_MSG_PUT_REQ:
1464 tx->tx_msg.ram_u.putreq.raprm_cookie = tx->tx_cookie;
1465 rc = kranal_sendmsg(conn, &tx->tx_msg, NULL, 0);
1466 kranal_map_buffer(tx);
1470 case RANAL_MSG_PUT_ACK:
1471 rc = kranal_sendmsg(conn, &tx->tx_msg, NULL, 0);
1475 case RANAL_MSG_GET_REQ:
1476 kranal_map_buffer(tx);
1477 tx->tx_msg.ram_u.get.ragm_cookie = tx->tx_cookie;
1478 tx->tx_msg.ram_u.get.ragm_desc.rard_key = tx->tx_map_key;
1479 tx->tx_msg.ram_u.get.ragm_desc.rard_addr.AddressBits =
1480 (__u64)((unsigned long)tx->tx_buffer);
1481 tx->tx_msg.ram_u.get.ragm_desc.rard_nob = tx->tx_nob;
1482 rc = kranal_sendmsg(conn, &tx->tx_msg, NULL, 0);
1487 if (rc == -EAGAIN) {
1488 /* I need credits to send this. Replace tx at the head of the
1489 * fmaq and I'll get rescheduled when credits appear */
1490 spin_lock_irqsave(&conn->rac_lock, flags);
1491 list_add(&tx->tx_list, &conn->rac_fmaq);
1492 spin_unlock_irqrestore(&conn->rac_lock, flags);
1498 if (!expect_reply) {
1499 kranal_tx_done(tx, 0);
1501 spin_lock_irqsave(&conn->rac_lock, flags);
1502 list_add_tail(&tx->tx_list, &conn->rac_replyq);
1503 tx->tx_qtime = jiffies;
1504 spin_unlock_irqrestore(&conn->rac_lock, flags);
1508 kranal_schedule_conn(conn);
1512 kranal_swab_rdma_desc (kra_rdma_desc_t *d)
1514 __swab64s(&d->rard_key.Key);
1515 __swab16s(&d->rard_key.Cookie);
1516 __swab16s(&d->rard_key.MdHandle);
1517 __swab32s(&d->rard_key.Flags);
1518 __swab64s(&d->rard_addr.AddressBits);
1519 __swab32s(&d->rard_nob);
1523 kranal_match_reply(kra_conn_t *conn, int type, __u64 cookie)
1525 struct list_head *ttmp;
1528 list_for_each(ttmp, &conn->rac_replyq) {
1529 tx = list_entry(ttmp, kra_tx_t, tx_list);
1531 if (tx->tx_cookie != cookie)
1534 if (tx->tx_msg.ram_type != type) {
1535 CWARN("Unexpected type %x (%x expected) "
1536 "matched reply from "LPX64"\n",
1537 tx->tx_msg.ram_type, type,
1538 conn->rac_peer->rap_nid);
1543 CWARN("Unmatched reply from "LPX64"\n", conn->rac_peer->rap_nid);
1548 kranal_check_fma_rx (kra_conn_t *conn)
1550 unsigned long flags;
1555 RAP_RETURN rrc = RapkFmaGetPrefix(conn->rac_rihandle, &prefix);
1556 kra_peer_t *peer = conn->rac_peer;
1558 if (rrc == RAP_NOT_DONE)
1561 LASSERT (rrc == RAP_SUCCESS);
1562 conn->rac_last_rx = jiffies;
1563 seq = conn->rac_rx_seq++;
1564 msg = (kra_msg_t *)prefix;
1566 if (msg->ram_magic != RANAL_MSG_MAGIC) {
1567 if (__swab32(msg->ram_magic) != RANAL_MSG_MAGIC) {
1568 CERROR("Unexpected magic %08x from "LPX64"\n",
1569 msg->ram_magic, peer->rap_nid);
1573 __swab32s(&msg->ram_magic);
1574 __swab16s(&msg->ram_version);
1575 __swab16s(&msg->ram_type);
1576 __swab64s(&msg->ram_srcnid);
1577 __swab64s(&msg->ram_connstamp);
1578 __swab32s(&msg->ram_seq);
1580 /* NB message type checked below; NOT here... */
1581 switch (msg->ram_type) {
1582 case RANAL_MSG_PUT_ACK:
1583 kranal_swab_rdma_desc(&msg->ram_u.putack.rapam_desc);
1586 case RANAL_MSG_GET_REQ:
1587 kranal_swab_rdma_desc(&msg->ram_u.get.ragm_desc);
1595 if (msg->ram_version != RANAL_MSG_VERSION) {
1596 CERROR("Unexpected protocol version %d from "LPX64"\n",
1597 msg->ram_version, peer->rap_nid);
1601 if (msg->ram_srcnid != peer->rap_nid) {
1602 CERROR("Unexpected peer "LPX64" from "LPX64"\n",
1603 msg->ram_srcnid, peer->rap_nid);
1607 if (msg->ram_connstamp != conn->rac_peer_connstamp) {
1608 CERROR("Unexpected connstamp "LPX64"("LPX64
1609 " expected) from "LPX64"\n",
1610 msg->ram_connstamp, conn->rac_peer_connstamp,
1615 if (msg->ram_seq != seq) {
1616 CERROR("Unexpected sequence number %d(%d expected) from "
1617 LPX64"\n", msg->ram_seq, seq, peer->rap_nid);
1621 if ((msg->ram_type & RANAL_MSG_FENCE) != 0) {
1622 /* This message signals RDMA completion... */
1623 rrc = RapkFmaSyncWait(conn->rac_rihandle);
1624 LASSERT (rrc == RAP_SUCCESS);
1627 if (conn->rac_close_recvd) {
1628 CERROR("Unexpected message %d after CLOSE from "LPX64"\n",
1629 msg->ram_type, conn->rac_peer->rap_nid);
1633 if (msg->ram_type == RANAL_MSG_CLOSE) {
1634 conn->rac_close_recvd = 1;
1635 write_lock_irqsave(&kranal_data.kra_global_lock, flags);
1637 if (conn->rac_state == RANAL_CONN_ESTABLISHED)
1638 kranal_close_conn_locked(conn, 0);
1639 else if (conn->rac_close_sent)
1640 kranal_terminate_conn_locked(conn);
1642 write_unlock_irqrestore(&kranal_data.kra_global_lock, flags);
1646 if (conn->rac_state != RANAL_CONN_ESTABLISHED)
1649 conn->rac_rxmsg = msg; /* stash message for portals callbacks */
1650 /* they'll NULL rac_rxmsg if they consume it */
1651 switch (msg->ram_type) {
1652 case RANAL_MSG_NOOP:
1653 /* Nothing to do; just a keepalive */
1656 case RANAL_MSG_IMMEDIATE:
1657 lib_parse(&kranal_lib, &msg->ram_u.immediate.raim_hdr, conn);
1660 case RANAL_MSG_PUT_REQ:
1661 lib_parse(&kranal_lib, &msg->ram_u.putreq.raprm_hdr, conn);
1663 if (conn->rac_rxmsg == NULL) /* lib_parse matched something */
1666 tx = kranal_new_tx_msg(0, RANAL_MSG_PUT_NAK);
1670 tx->tx_msg.ram_u.completion.racm_cookie =
1671 msg->ram_u.putreq.raprm_cookie;
1672 kranal_post_fma(conn, tx);
1675 case RANAL_MSG_PUT_NAK:
1676 tx = kranal_match_reply(conn, RANAL_MSG_PUT_REQ,
1677 msg->ram_u.completion.racm_cookie);
1681 LASSERT (tx->tx_buftype == RANAL_BUF_PHYS_MAPPED ||
1682 tx->tx_buftype == RANAL_BUF_VIRT_MAPPED);
1683 kranal_tx_done(tx, -ENOENT); /* no match */
1686 case RANAL_MSG_PUT_ACK:
1687 tx = kranal_match_reply(conn, RANAL_MSG_PUT_REQ,
1688 msg->ram_u.putack.rapam_src_cookie);
1692 kranal_rdma(tx, RANAL_MSG_PUT_DONE,
1693 &msg->ram_u.putack.rapam_desc,
1694 msg->ram_u.putack.rapam_desc.rard_nob,
1695 msg->ram_u.putack.rapam_dst_cookie);
1698 case RANAL_MSG_PUT_DONE:
1699 tx = kranal_match_reply(conn, RANAL_MSG_PUT_ACK,
1700 msg->ram_u.completion.racm_cookie);
1704 LASSERT (tx->tx_buftype == RANAL_BUF_PHYS_MAPPED ||
1705 tx->tx_buftype == RANAL_BUF_VIRT_MAPPED);
1706 kranal_tx_done(tx, 0);
1709 case RANAL_MSG_GET_REQ:
1710 lib_parse(&kranal_lib, &msg->ram_u.get.ragm_hdr, conn);
1712 if (conn->rac_rxmsg == NULL) /* lib_parse matched something */
1715 tx = kranal_new_tx_msg(0, RANAL_MSG_GET_NAK);
1719 tx->tx_msg.ram_u.completion.racm_cookie = msg->ram_u.get.ragm_cookie;
1720 kranal_post_fma(conn, tx);
1723 case RANAL_MSG_GET_NAK:
1724 tx = kranal_match_reply(conn, RANAL_MSG_GET_REQ,
1725 msg->ram_u.completion.racm_cookie);
1729 LASSERT (tx->tx_buftype == RANAL_BUF_PHYS_MAPPED ||
1730 tx->tx_buftype == RANAL_BUF_VIRT_MAPPED);
1731 kranal_tx_done(tx, -ENOENT); /* no match */
1734 case RANAL_MSG_GET_DONE:
1735 tx = kranal_match_reply(conn, RANAL_MSG_GET_REQ,
1736 msg->ram_u.completion.racm_cookie);
1740 LASSERT (tx->tx_buftype == RANAL_BUF_PHYS_MAPPED ||
1741 tx->tx_buftype == RANAL_BUF_VIRT_MAPPED);
1742 kranal_tx_done(tx, 0);
1747 if (conn->rac_rxmsg != NULL)
1748 kranal_consume_rxmsg(conn, NULL, 0);
1750 /* check again later */
1751 kranal_schedule_conn(conn);
1755 kranal_complete_closed_conn (kra_conn_t *conn)
1759 LASSERT (conn->rac_state == RANAL_CONN_CLOSED);
1761 while (!list_empty(&conn->rac_fmaq)) {
1762 tx = list_entry(conn->rac_fmaq.next, kra_tx_t, tx_list);
1764 list_del(&tx->tx_list);
1765 kranal_tx_done(tx, -ECONNABORTED);
1768 LASSERT (list_empty(&conn->rac_rdmaq));
1770 while (!list_empty(&conn->rac_replyq)) {
1771 tx = list_entry(conn->rac_replyq.next, kra_tx_t, tx_list);
1773 list_del(&tx->tx_list);
1774 kranal_tx_done(tx, -ECONNABORTED);
1779 kranal_scheduler (void *arg)
1781 kra_device_t *dev = (kra_device_t *)arg;
1785 unsigned long flags;
1788 snprintf(name, sizeof(name), "kranal_sd_%02d", dev->rad_idx);
1789 kportal_daemonize(name);
1790 kportal_blockallsigs();
1792 dev->rad_scheduler = current;
1793 init_waitqueue_entry(&wait, current);
1795 spin_lock_irqsave(&dev->rad_lock, flags);
1797 while (!kranal_data.kra_shutdown) {
1798 /* Safe: kra_shutdown only set when quiescent */
1800 if (busy_loops++ >= RANAL_RESCHED) {
1801 spin_unlock_irqrestore(&dev->rad_lock, flags);
1806 spin_lock_irqsave(&dev->rad_lock, flags);
1809 if (dev->rad_ready) {
1810 /* Device callback fired since I last checked it */
1812 spin_unlock_irqrestore(&dev->rad_lock, flags);
1814 kranal_check_rdma_cq(dev);
1815 kranal_check_fma_cq(dev);
1817 spin_lock_irqsave(&dev->rad_lock, flags);
1820 if (!list_empty(&dev->rad_connq)) {
1821 /* Connection needs attention */
1822 conn = list_entry(dev->rad_connq.next,
1823 kra_conn_t, rac_schedlist);
1824 list_del_init(&conn->rac_schedlist);
1825 LASSERT (conn->rac_scheduled);
1826 conn->rac_scheduled = 0;
1827 spin_unlock_irqrestore(&dev->rad_lock, flags);
1829 kranal_check_fma_rx(conn);
1830 kranal_process_fmaq(conn);
1832 if (conn->rac_state == RANAL_CONN_CLOSED)
1833 kranal_complete_closed_conn(conn);
1835 kranal_conn_decref(conn);
1837 spin_lock_irqsave(&dev->rad_lock, flags);
1841 add_wait_queue(&dev->rad_waitq, &wait);
1842 set_current_state(TASK_INTERRUPTIBLE);
1844 spin_unlock_irqrestore(&dev->rad_lock, flags);
1849 set_current_state(TASK_RUNNING);
1850 remove_wait_queue(&dev->rad_waitq, &wait);
1852 spin_lock_irqsave(&dev->rad_lock, flags);
1855 spin_unlock_irqrestore(&dev->rad_lock, flags);
1857 dev->rad_scheduler = NULL;
1858 kranal_thread_fini();
1863 lib_nal_t kranal_lib = {
1864 libnal_data: &kranal_data, /* NAL private data */
1865 libnal_send: kranal_send,
1866 libnal_send_pages: kranal_send_pages,
1867 libnal_recv: kranal_recv,
1868 libnal_recv_pages: kranal_recv_pages,
1869 libnal_dist: kranal_dist