Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lnet / klnds / openiblnd / openiblnd.h
index f0610f2..2b45f5e 100644 (file)
@@ -1,31 +1,49 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2004 Cluster File Systems, Inc.
- *   Author: Eric Barton <eric@bartonsoftware.com>
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lnet/klnds/openiblnd/openiblnd.h
+ *
+ * Author: Eric Barton <eric@bartonsoftware.com>
  */
 
 #ifndef EXPORT_SYMTAB
 # define EXPORT_SYMTAB
 #endif
-
+#ifndef AUTOCONF_INCLUDED
 #include <linux/config.h>
+#endif
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/kmod.h>
 #include <linux/sysctl.h>
 
-#define DEBUG_SUBSYSTEM S_IBNAL
+#include <net/sock.h>
+#include <linux/in.h>
 
-#include <linux/kp30.h>
-#include <portals/p30.h>
-#include <portals/lib-p30.h>
-#include <portals/nal.h>
+#define DEBUG_SUBSYSTEM S_LND
+
+#include <libcfs/libcfs.h>
+#include <lnet/lnet.h>
+#include <lnet/lib-lnet.h>
+#include <lnet/lnet-sysctl.h>
 
 #include <ts_ib_core.h>
 #include <ts_ib_cm.h>
 #include <ts_ib_sa_client.h>
 
-#define IBNAL_SERVICE_NAME   "openibnal"
+#ifndef USING_TSAPI
 
-#if CONFIG_SMP
-# define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
-#else
-# define IBNAL_N_SCHED      1                   /* # schedulers */
-#endif
+/* OpenIB Gen1 */
+typedef struct ib_qp       ib_qp_t;
+typedef struct ib_mr       ib_mr_t;
+typedef struct ib_fmr      ib_fmr_t;
+typedef struct ib_pd       ib_pd_t;
+typedef struct ib_cq       ib_cq_t;
+typedef struct ib_fmr_pool ib_fmr_pool_t;
 
-#define IBNAL_MIN_RECONNECT_INTERVAL HZ         /* first failed connection retry... */
-#define IBNAL_MAX_RECONNECT_INTERVAL (60*HZ)    /* ...exponentially increasing to this */
+#else
 
-#define IBNAL_MSG_SIZE       (4<<10)            /* max size of queued messages (inc hdr) */
+/* Cisco (topspin) */
+typedef void                 ib_qp_t;
+typedef void                 ib_mr_t;
+typedef void                 ib_fmr_t;
+typedef void                 ib_pd_t;
+typedef void                 ib_cq_t;
+typedef void                 ib_fmr_pool_t;
+
+#define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
+#define IB_WQ_SIGNAL_SELECTABLE            TS_IB_ACCESS_LOCAL_WRITE
+#define IB_TRANSPORT_RC                    TS_IB_TRANSPORT_RC
+#define IB_QP_STATE_INIT                   TS_IB_QP_STATE_INIT
+#define IB_QP_ATTRIBUTE_STATE              TS_IB_QP_ATTRIBUTE_STATE
+#define IB_QP_ATTRIBUTE_PORT               TS_IB_QP_ATTRIBUTE_PORT
+#define IB_QP_ATTRIBUTE_PKEY_INDEX         TS_IB_QP_ATTRIBUTE_PKEY_INDEX
+#define IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE
+#define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
+#define IB_ACCESS_REMOTE_WRITE             TS_IB_ACCESS_REMOTE_WRITE
+#define IB_ACCESS_REMOTE_READ              TS_IB_ACCESS_REMOTE_READ
+#define IB_CQ_CALLBACK_INTERRU             TS_IB_CQ_CALLBACK_INTERRUPTPT
+#define IB_CQ_PROVIDER_REARM               TS_IB_CQ_PROVIDER_REARM
+#define IB_CQ_CALLBACK_INTERRUPT           TS_IB_CQ_CALLBACK_INTERRUPT
+#define IB_COMPLETION_STATUS_SUCCESS       TS_IB_COMPLETION_STATUS_SUCCESS
+#define IB_OP_SEND                         TS_IB_OP_SEND
+#define IB_OP_RDMA_WRITE                   TS_IB_OP_RDMA_WRITE
+#define IB_OP_RDMA_READ                    TS_IB_OP_RDMA_READ
 
-#define IBNAL_MSG_QUEUE_SIZE   8                /* # messages/RDMAs in-flight */
-#define IBNAL_CREDIT_HIGHWATER 6                /* when to eagerly return credits */
-#define IBNAL_RETRY            7                /* # times to retry */
-#define IBNAL_RNR_RETRY        7                /*  */
-#define IBNAL_CM_RETRY         7                /* # times to retry connection */
-#define IBNAL_FLOW_CONTROL     1
-#define IBNAL_RESPONDER_RESOURCES 8
+#endif
 
-#define IBNAL_NTX             64                /* # tx descs */
-#define IBNAL_NTX_NBLK        256               /* # reserved tx descs */
+#ifdef CONFIG_SMP
+# define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
+#else
+# define IBNAL_N_SCHED      1                   /* # schedulers */
+#endif
 
-#define IBNAL_PEER_HASH_SIZE  101               /* # peer lists */
+#define IBNAL_FMR                    1
+//#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_PROCESS
+#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_INTERRUPT
 
-#define IBNAL_RESCHED         100               /* # scheduler loops before reschedule */
 
-#define IBNAL_CONCURRENT_PEERS 1000             /* # nodes all talking at once to me */
+/* tunables fixed at compile time */
+#define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
+#define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
+#define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
+#define IBNAL_CREDIT_HIGHWATER       6          /* when to eagerly return credits */
+#define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
+#define IBNAL_RDMA_BASE              0x0eeb0000
 
-/* default vals for runtime tunables */
-#define IBNAL_IO_TIMEOUT      50                /* default comms timeout (seconds) */
+/* QP tunables */
+#define IBNAL_RETRY                  7          /* # times to retry */
+#define IBNAL_RNR_RETRY              7          /*  */
+#define IBNAL_CM_RETRY               7          /* # times to retry connection */
+#define IBNAL_FLOW_CONTROL           1
+#define IBNAL_RESPONDER_RESOURCES    8
 
 /************************/
 /* derived constants... */
 
 /* TX messages (shared by all connections) */
-#define IBNAL_TX_MSGS       (IBNAL_NTX + IBNAL_NTX_NBLK)
-#define IBNAL_TX_MSG_BYTES  (IBNAL_TX_MSGS * IBNAL_MSG_SIZE)
-#define IBNAL_TX_MSG_PAGES  ((IBNAL_TX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
+#define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
+#define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
+#define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
 
 /* RX messages (per connection) */
-#define IBNAL_RX_MSGS       IBNAL_MSG_QUEUE_SIZE
-#define IBNAL_RX_MSG_BYTES  (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
-#define IBNAL_RX_MSG_PAGES  ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
+#define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE * 2)
+#define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
+#define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
 
 /* we may have up to 2 completions per transmit +
    1 completion per receive, per connection */
-#define IBNAL_CQ_ENTRIES  ((2*IBNAL_TX_MSGS) +                          \
-                           (IBNAL_RX_MSGS * IBNAL_CONCURRENT_PEERS))
-
-#define IBNAL_RDMA_BASE  0x0eeb0000
-#define IBNAL_FMR        1
-#define IBNAL_CKSUM      0
-//#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_PROCESS
-#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_INTERRUPT
+#define IBNAL_CQ_ENTRIES()  ((2*IBNAL_TX_MSGS()) +                                      \
+                             (IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers))
 
-typedef struct 
+typedef struct
 {
-        int               kib_io_timeout;       /* comms timeout (seconds) */
-        struct ctl_table_header *kib_sysctl;    /* sysctl interface */
+        char    **kib_ipif_basename;            /* IPoIB interface base name */
+        int      *kib_n_connd;                  /* # connection daemons */
+        int      *kib_min_reconnect_interval;   /* min connect retry seconds... */
+        int      *kib_max_reconnect_interval;   /* max connect retry seconds */
+        int      *kib_concurrent_peers;         /* max # peers */
+        int      *kib_cksum;                    /* checksum kib_msg_t? */
+        int      *kib_timeout;                  /* comms timeout (seconds) */
+        int      *kib_keepalive;                /* keepalive (seconds) */
+        int      *kib_ntx;                      /* # tx descs */
+        int      *kib_credits;                  /* # concurrent sends */
+        int      *kib_peercredits;              /* # concurrent sends to 1 peer */
+
+        cfs_sysctl_table_header_t *kib_sysctl;  /* sysctl interface */
 } kib_tunables_t;
 
 typedef struct
@@ -129,60 +189,66 @@ typedef struct
         __u64             ibp_vaddr;            /* mapped region vaddr */
         __u32             ibp_lkey;             /* mapped region lkey */
         __u32             ibp_rkey;             /* mapped region rkey */
-        struct ib_mr     *ibp_handle;           /* mapped region handle */
+        ib_mr_t          *ibp_handle;           /* mapped region handle */
         struct page      *ibp_pages[0];
 } kib_pages_t;
-        
-typedef struct 
+
+typedef struct
 {
         int               kib_init;             /* initialisation state */
         __u64             kib_incarnation;      /* which one am I */
         int               kib_shutdown;         /* shut down? */
         atomic_t          kib_nthreads;         /* # live threads */
+        lnet_ni_t        *kib_ni;               /* _the_ openib interface */
 
-        __u64             kib_service_id;       /* service number I listen on */
-        ptl_nid_t         kib_nid;              /* my NID */
-        struct semaphore  kib_nid_mutex;        /* serialise NID ops */
-        struct semaphore  kib_nid_signal;       /* signal completion */
-
+        __u64             kib_svc_id;           /* service number I listen on */
+        tTS_IB_GID        kib_svc_gid;          /* device/port GID */
+        __u16             kib_svc_pkey;         /* device/port pkey */
+        
+        void             *kib_listen_handle;    /* IB listen handle */
+        
         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
 
         struct list_head *kib_peers;            /* hash table of all my known peers */
         int               kib_peer_hash_size;   /* size of kib_peers */
+        int               kib_nonewpeers;       /* prevent new peers? */
         atomic_t          kib_npeers;           /* # peers extant */
         atomic_t          kib_nconns;           /* # connections extant */
 
-        struct list_head  kib_connd_conns;      /* connections to progress */
+        struct list_head  kib_reaper_conns;     /* connections to reap */
+        wait_queue_head_t kib_reaper_waitq;     /* reaper sleeps here */
+        unsigned long     kib_reaper_waketime;  /* when reaper will wake */
+        spinlock_t        kib_reaper_lock;      /* serialise */
+
         struct list_head  kib_connd_peers;      /* peers waiting for a connection */
+        struct list_head  kib_connd_acceptq;    /* accepted sockets to handle */
         wait_queue_head_t kib_connd_waitq;      /* connection daemons sleep here */
-        unsigned long     kib_connd_waketime;   /* when connd will wake */
+        int               kib_connd_connecting; /* # connds connecting */
         spinlock_t        kib_connd_lock;       /* serialise */
 
         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
         struct list_head  kib_sched_txq;        /* tx requiring attention */
         struct list_head  kib_sched_rxq;        /* rx requiring attention */
         spinlock_t        kib_sched_lock;       /* serialise */
-        
+
         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
 
         struct list_head  kib_idle_txs;         /* idle tx descriptors */
-        struct list_head  kib_idle_nblk_txs;    /* idle reserved tx descriptors */
-        wait_queue_head_t kib_idle_tx_waitq;    /* block here for tx descriptor */
         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
         spinlock_t        kib_tx_lock;          /* serialise */
-        
+
+        int               kib_hca_idx;          /* my HCA number */
         struct ib_device *kib_device;           /* "the" device */
         struct ib_device_properties kib_device_props; /* its properties */
         int               kib_port;             /* port on the device */
         struct ib_port_properties kib_port_props; /* its properties */
-        struct ib_pd     *kib_pd;               /* protection domain */
+        ib_pd_t          *kib_pd;               /* protection domain */
 #if IBNAL_FMR
-        struct ib_fmr_pool *kib_fmr_pool;       /* fast memory region pool */
+        ib_fmr_pool_t    *kib_fmr_pool;         /* fast memory region pool */
 #endif
-        struct ib_cq     *kib_cq;               /* completion queue */
-        void             *kib_listen_handle;    /* where I listen for connections */
-        
+        ib_cq_t          *kib_cq;               /* completion queue */
+
 } kib_data_t;
 
 #define IBNAL_INIT_NOTHING         0
@@ -194,18 +260,36 @@ typedef struct
 #define IBNAL_INIT_CQ              6
 #define IBNAL_INIT_ALL             7
 
+typedef struct kib_acceptsock                   /* accepted socket queued for connd */
+{
+        struct list_head     ibas_list;         /* queue for attention */
+        struct socket       *ibas_sock;         /* the accepted socket */
+} kib_acceptsock_t;
+
 /************************************************************************
- * Wire message structs.
+ * IB Wire message format.
  * These are sent in sender's byte order (i.e. receiver flips).
- * CAVEAT EMPTOR: other structs communicated between nodes (e.g. MAD
- * private data and SM service info), is LE on the wire.
+ * They may be sent via TCP/IP (service ID,GID,PKEY query/response),
+ * as private data in the connection request/response, or "normally".
  */
 
+typedef struct kib_svcrsp                       /* service response */
+{
+        __u64             ibsr_svc_id;          /* service's id */
+        __u8              ibsr_svc_gid[16];     /* service's gid */
+        __u16             ibsr_svc_pkey;        /* service's pkey */
+} WIRE_ATTR kib_svcrsp_t;
+
+typedef struct kib_connparams
+{
+        __u32             ibcp_queue_depth;
+} WIRE_ATTR kib_connparams_t;
+
 typedef struct
 {
         union {
-                struct ib_mr    *mr;
-                struct ib_fmr   *fmr;
+                ib_mr_t         *mr;
+                ib_fmr_t        *fmr;
         }                 md_handle;
         __u32             md_lkey;
         __u32             md_rkey;
@@ -214,51 +298,61 @@ typedef struct
 
 typedef struct
 {
-        __u32                 rd_key;           /* remote key */
-        __u32                 rd_nob;           /* # of bytes */
-        __u64                 rd_addr;          /* remote io vaddr */
-} kib_rdma_desc_t;
-
+        __u32             rd_key;               /* remote key */
+        __u32             rd_nob;               /* # of bytes */
+        __u64             rd_addr;              /* remote io vaddr */
+} WIRE_ATTR kib_rdma_desc_t;
 
 typedef struct
 {
-        ptl_hdr_t         ibim_hdr;             /* portals header */
+        lnet_hdr_t        ibim_hdr;             /* portals header */
         char              ibim_payload[0];      /* piggy-backed payload */
-} kib_immediate_msg_t;
+} WIRE_ATTR kib_immediate_msg_t;
 
 typedef struct
 {
-        ptl_hdr_t         ibrm_hdr;             /* portals header */
+        lnet_hdr_t        ibrm_hdr;             /* portals header */
         __u64             ibrm_cookie;          /* opaque completion cookie */
         kib_rdma_desc_t   ibrm_desc;            /* where to suck/blow */
-} kib_rdma_msg_t;
+} WIRE_ATTR kib_rdma_msg_t;
 
 typedef struct
 {
         __u64             ibcm_cookie;          /* opaque completion cookie */
         __u32             ibcm_status;          /* completion status */
-} kib_completion_msg_t;
+} WIRE_ATTR kib_completion_msg_t;
 
 typedef struct
 {
-        __u32              ibm_magic;           /* I'm an openibnal message */
-        __u16              ibm_version;         /* this is my version number */
-        __u8               ibm_type;            /* msg type */
-        __u8               ibm_credits;         /* returned credits */
-#if IBNAL_CKSUM
-        __u32              ibm_nob;
-        __u32              ibm_cksum;
-#endif
+        /* First 2 fields fixed FOR ALL TIME */
+        __u32             ibm_magic;            /* I'm an openibnal message */
+        __u16             ibm_version;          /* this is my version number */
+
+        __u8              ibm_type;             /* msg type */
+        __u8              ibm_credits;          /* returned credits */
+        __u32             ibm_nob;              /* # bytes in whole message */
+        __u32             ibm_cksum;            /* checksum (0 == no checksum) */
+        __u64             ibm_srcnid;           /* sender's NID */
+        __u64             ibm_srcstamp;         /* sender's incarnation */
+        __u64             ibm_dstnid;           /* destination's NID */
+        __u64             ibm_dststamp;         /* destination's incarnation */
         union {
+                kib_svcrsp_t          svcrsp;
+                kib_connparams_t      connparams;
                 kib_immediate_msg_t   immediate;
                 kib_rdma_msg_t        rdma;
                 kib_completion_msg_t  completion;
-        }                    ibm_u;
-} kib_msg_t;
+        } WIRE_ATTR       ibm_u;
+} WIRE_ATTR kib_msg_t;
 
-#define IBNAL_MSG_MAGIC       0x0be91b91        /* unique magic */
-#define IBNAL_MSG_VERSION              1        /* current protocol version */
+#define IBNAL_MSG_MAGIC LNET_PROTO_OPENIB_MAGIC /* unique magic */
+#define IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD 2   /* previous protocol version */
+#define IBNAL_MSG_VERSION                   3   /* current protocol version */
 
+#define IBNAL_MSG_SVCQRY            0xb0        /* service query */
+#define IBNAL_MSG_SVCRSP            0xb1        /* service response */
+#define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
+#define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
 #define IBNAL_MSG_IMMEDIATE         0xd1        /* portals hdr + payload */
 #define IBNAL_MSG_PUT_RDMA          0xd2        /* portals PUT hdr + source rdma desc */
@@ -272,8 +366,7 @@ typedef struct kib_rx                           /* receive message */
 {
         struct list_head          rx_list;      /* queue for attention */
         struct kib_conn          *rx_conn;      /* owning conn */
-        int                       rx_rdma;      /* RDMA completion posted? */
-        int                       rx_posted;    /* posted? */
+        int                       rx_nob;       /* # bytes received (-1 while posted) */
         __u64                     rx_vaddr;     /* pre-mapped buffer (hca vaddr) */
         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
         struct ib_receive_param   rx_sp;        /* receive work item */
@@ -283,7 +376,6 @@ typedef struct kib_rx                           /* receive message */
 typedef struct kib_tx                           /* transmit message */
 {
         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
-        int                       tx_isnblk;    /* I'm reserved for non-blocking sends */
         struct kib_conn          *tx_conn;      /* owning conn */
         int                       tx_mapped;    /* mapped for RDMA? */
         int                       tx_sending;   /* # tx callbacks outstanding */
@@ -292,7 +384,7 @@ typedef struct kib_tx                           /* transmit message */
         int                       tx_passive_rdma; /* peer sucks/blows */
         int                       tx_passive_rdma_wait; /* waiting for peer to complete */
         __u64                     tx_passive_rdma_cookie; /* completion cookie */
-        lib_msg_t                *tx_libmsg[2]; /* lib msgs to finalize on completion */
+        lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
         kib_md_t                  tx_md;        /* RDMA mapping (active/passive) */
         __u64                     tx_vaddr;     /* pre-mapped buffer (hca vaddr) */
         kib_msg_t                *tx_msg;       /* pre-mapped buffer (host vaddr) */
@@ -305,44 +397,39 @@ typedef struct kib_tx                           /* transmit message */
 #define KIB_TX_MAPPED         1
 #define KIB_TX_MAPPED_FMR     2
 
-typedef struct kib_wire_connreq
-{
-        __u32        wcr_magic;                 /* I'm an openibnal connreq */
-        __u16        wcr_version;               /* this is my version number */
-        __u16        wcr_queue_depth;           /* this is my receive queue size */
-        __u64        wcr_nid;                   /* peer's NID */
-        __u64        wcr_incarnation;           /* peer's incarnation */
-} kib_wire_connreq_t;
-
 typedef struct kib_connreq
 {
-        /* connection-in-progress */
-        struct kib_conn                    *cr_conn;
-        kib_wire_connreq_t                  cr_wcr;
-        __u64                               cr_tid;
-        struct ib_common_attrib_service     cr_service;
-        tTS_IB_GID                          cr_gid;
-        struct ib_path_record               cr_path;
-        struct ib_cm_active_param           cr_connparam;
+        /* active connection-in-progress state */
+        struct kib_conn           *cr_conn;
+        kib_msg_t                  cr_msg;
+        __u64                      cr_tid;
+        tTS_IB_GID                 cr_gid;
+        kib_svcrsp_t               cr_svcrsp;
+        struct ib_path_record      cr_path;
+        struct ib_cm_active_param  cr_connparam;
 } kib_connreq_t;
 
 typedef struct kib_conn
-{ 
+{
         struct kib_peer    *ibc_peer;           /* owning peer */
         struct list_head    ibc_list;           /* stash on peer's conn list */
         __u64               ibc_incarnation;    /* which instance of the peer */
+        int                 ibc_version;        /* peer protocol version */
         atomic_t            ibc_refcount;       /* # users */
         int                 ibc_state;          /* what's happening */
-        atomic_t            ibc_nob;            /* # bytes buffered */
         int                 ibc_nsends_posted;  /* # uncompleted sends */
         int                 ibc_credits;        /* # credits I have */
         int                 ibc_outstanding_credits; /* # credits to return */
+        int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
+        unsigned long       ibc_last_send;      /* time of last send */
+        struct list_head    ibc_tx_queue_nocred; /* sends that don't need a credit */
+        struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
         struct list_head    ibc_tx_queue;       /* send queue */
         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
         spinlock_t          ibc_lock;           /* serialise */
         kib_rx_t           *ibc_rxs;            /* the rx descs */
         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
-        struct ib_qp       *ibc_qp;             /* queue pair */
+        ib_qp_t            *ibc_qp;             /* queue pair */
         __u32               ibc_qpn;            /* queue pair number */
         tTS_IB_CM_COMM_ID   ibc_comm_id;        /* connection ID? */
         kib_connreq_t      *ibc_connreq;        /* connection request state */
@@ -359,26 +446,80 @@ typedef struct kib_peer
 {
         struct list_head    ibp_list;           /* stash on global peer list */
         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
-        ptl_nid_t           ibp_nid;            /* who's on the other end(s) */
+        lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
+        __u32               ibp_ip;             /* IP to query for peer conn params */
+        int                 ibp_port;           /* port to qery for peer conn params */
+        __u64               ibp_incarnation;    /* peer's incarnation */
         atomic_t            ibp_refcount;       /* # users */
         int                 ibp_persistence;    /* "known" peer refs */
         struct list_head    ibp_conns;          /* all active connections */
         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
-        int                 ibp_connecting;     /* connecting+accepting */
+        int                 ibp_connecting;     /* current active connection attempts */
+        int                 ibp_accepting;      /* current passive connection attempts */
         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
         unsigned long       ibp_reconnect_interval; /* exponential backoff */
+        int                 ibp_error;          /* errno on closing this peer */
+        cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
 } kib_peer_t;
 
-
-extern lib_nal_t       kibnal_lib;
 extern kib_data_t      kibnal_data;
 extern kib_tunables_t  kibnal_tunables;
 
+/******************************************************************************/
+
+/* these are purposely avoiding using local vars so they don't increase
+ * stack consumption. */
+
+#define kibnal_conn_addref(conn)                                \
+do {                                                            \
+        CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
+               (conn), atomic_read(&(conn)->ibc_refcount));     \
+        LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
+        atomic_inc(&(conn)->ibc_refcount);                      \
+} while (0)
+
+#define kibnal_conn_decref(conn)                                              \
+do {                                                                          \
+        unsigned long   flags;                                                \
+                                                                              \
+        CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
+               (conn), atomic_read(&(conn)->ibc_refcount));                   \
+        LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
+        if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
+                spin_lock_irqsave(&kibnal_data.kib_reaper_lock, flags);       \
+                list_add_tail(&(conn)->ibc_list,                              \
+                              &kibnal_data.kib_reaper_conns);                 \
+                wake_up(&kibnal_data.kib_reaper_waitq);                       \
+                spin_unlock_irqrestore(&kibnal_data.kib_reaper_lock, flags);  \
+        }                                                                     \
+} while (0)
+
+#define kibnal_peer_addref(peer)                                \
+do {                                                            \
+        CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
+               (peer), libcfs_nid2str((peer)->ibp_nid),         \
+               atomic_read (&(peer)->ibp_refcount));            \
+        LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
+        atomic_inc(&(peer)->ibp_refcount);                      \
+} while (0)
+
+#define kibnal_peer_decref(peer)                                \
+do {                                                            \
+        CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
+               (peer), libcfs_nid2str((peer)->ibp_nid),         \
+               atomic_read (&(peer)->ibp_refcount));            \
+        LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
+        if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
+                kibnal_destroy_peer(peer);                      \
+} while (0)
+
+/******************************************************************************/
+
 static inline struct list_head *
-kibnal_nid2peerlist (ptl_nid_t nid) 
+kibnal_nid2peerlist (lnet_nid_t nid)
 {
         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
-        
+
         return (&kibnal_data.kib_peers [hash]);
 }
 
@@ -392,77 +533,58 @@ kibnal_peer_active(kib_peer_t *peer)
 static inline void
 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
 {
-        /* CAVEAT EMPTOR: tx takes caller's ref on conn */
+        struct list_head      *q;
 
         LASSERT (tx->tx_nsp > 0);               /* work items set up */
         LASSERT (tx->tx_conn == NULL);          /* only set here */
 
+        kibnal_conn_addref(conn);
         tx->tx_conn = conn;
-        tx->tx_deadline = jiffies + kibnal_tunables.kib_io_timeout * HZ;
-        list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
-}
-
-#define KIBNAL_SERVICE_KEY_MASK  (IB_SA_SERVICE_COMP_MASK_NAME |        \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_1 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_2 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_3 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_4 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_5 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_6 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_7 |     \
-                                  IB_SA_SERVICE_COMP_MASK_DATA8_8)
-
-static inline __u64*
-kibnal_service_nid_field(struct ib_common_attrib_service *srv)
-{
-        /* must be consistent with KIBNAL_SERVICE_KEY_MASK */
-        return (__u64 *)srv->service_data8;
-}
-
-
-static inline void
-kibnal_set_service_keys(struct ib_common_attrib_service *srv, ptl_nid_t nid)
-{
-        LASSERT (strlen (IBNAL_SERVICE_NAME) < sizeof(srv->service_name));
-        memset (srv->service_name, 0, sizeof(srv->service_name));
-        strcpy (srv->service_name, IBNAL_SERVICE_NAME);
-
-        *kibnal_service_nid_field(srv) = cpu_to_le64(nid);
-}
-
-#if 0
-static inline void
-kibnal_show_rdma_attr (kib_conn_t *conn)
-{
-        struct ib_qp_attribute qp_attr;
-        int                    rc;
-        
-        memset (&qp_attr, 0, sizeof(qp_attr));
-        rc = ib_qp_query(conn->ibc_qp, &qp_attr);
-        if (rc != 0) {
-                CERROR ("Can't get qp attrs: %d\n", rc);
-                return;
+        tx->tx_deadline = jiffies + *kibnal_tunables.kib_timeout * HZ;
+
+        if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
+                /* All messages have simple credit control */
+                q = &conn->ibc_tx_queue;
+        } else {
+                LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
+                
+                switch (tx->tx_msg->ibm_type) {
+                case IBNAL_MSG_PUT_RDMA:
+                case IBNAL_MSG_GET_RDMA:
+                        /* RDMA request: reserve a buffer for the RDMA reply
+                         * before sending */
+                        q = &conn->ibc_tx_queue_rsrvd;
+                        break;
+
+                case IBNAL_MSG_PUT_DONE:
+                case IBNAL_MSG_GET_DONE:
+                        /* RDMA completion: no credits; peer has reserved a
+                         * reply buffer */
+                        q = &conn->ibc_tx_queue_nocred;
+                        break;
+                
+                case IBNAL_MSG_NOOP:
+                case IBNAL_MSG_IMMEDIATE:
+                        /* Otherwise: consume a credit before sending */
+                        q = &conn->ibc_tx_queue;
+                        break;
+                
+                default:
+                        LBUG();
+                        q = NULL;
+                }
         }
-        
-        CWARN ("RDMA CAPABILITY: write %s read %s\n",
-               (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
-               (qp_attr.enable_rdma_write ? "enabled" : "disabled") : "invalid",
-               (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
-               (qp_attr.enable_rdma_read ? "enabled" : "disabled") : "invalid");
+
+        list_add_tail(&tx->tx_list, q);
 }
-#endif
 
-#if CONFIG_X86
-static inline __u64
-kibnal_page2phys (struct page *p)
+static inline int
+kibnal_send_keepalive(kib_conn_t *conn) 
 {
-        __u64 page_number = p - mem_map;
-        
-        return (page_number << PAGE_SHIFT);
+        return (*kibnal_tunables.kib_keepalive > 0) &&
+                time_after(jiffies, conn->ibc_last_send +
+                           *kibnal_tunables.kib_keepalive*HZ);
 }
-#else
-# error "no page->phys"
-#endif
 
 /* CAVEAT EMPTOR:
  * We rely on tx/rx descriptor alignment to allow us to use the lowest bit
@@ -491,15 +613,63 @@ kibnal_wreqid_is_rx (__u64 wreqid)
         return (wreqid & 1) != 0;
 }
 
-extern kib_peer_t *kibnal_create_peer (ptl_nid_t nid);
-extern void kibnal_put_peer (kib_peer_t *peer);
-extern int kibnal_del_peer (ptl_nid_t nid, int single_share);
-extern kib_peer_t *kibnal_find_peer_locked (ptl_nid_t nid);
+#if (IB_NTXRXPARAMS == 3)
+static inline int
+kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
+{
+        return ib_send(qp, p, 1);
+}
+
+static inline int
+kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
+{
+        return ib_receive(qp, p, 1);
+}
+#elif (IB_NTXRXPARAMS == 4)
+static inline int
+kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
+{
+        return ib_send(qp, p, 1, NULL);
+}
+
+static inline int
+kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
+{
+        return ib_receive(qp, p, 1, NULL);
+}
+#else
+ #error "IB_NTXRXPARAMS not set correctly"
+#endif
+
+int kibnal_startup (lnet_ni_t *ni);
+void kibnal_shutdown (lnet_ni_t *ni);
+int kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
+int kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
+int kibnal_eager_recv (lnet_ni_t *ni, void *private, 
+                       lnet_msg_t *lntmsg, void **new_private);
+int kibnal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
+                int delayed, unsigned int niov, 
+                struct iovec *iov, lnet_kiov_t *kiov,
+                unsigned int offset, unsigned int mlen, unsigned int rlen);
+int kibnal_accept(lnet_ni_t *ni, struct socket *sock);
+
+extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
+extern void kibnal_pack_msg(kib_msg_t *msg, int version, int credits, 
+                            lnet_nid_t dstnid, __u64 dststamp);
+extern int kibnal_unpack_msg(kib_msg_t *msg, int expected_version, int nob);
+extern void kibnal_handle_svcqry (struct socket *sock);
+extern int kibnal_make_svcqry (kib_conn_t *conn);
+extern void kibnal_free_acceptsock (kib_acceptsock_t *as);
+extern int kibnal_create_peer (kib_peer_t **peerp, lnet_nid_t nid);
+extern void kibnal_destroy_peer (kib_peer_t *peer);
+extern int kibnal_add_persistent_peer(lnet_nid_t nid, __u32 ip, int port);
+extern int kibnal_del_peer (lnet_nid_t nid);
+extern kib_peer_t *kibnal_find_peer_locked (lnet_nid_t nid);
 extern void kibnal_unlink_peer_locked (kib_peer_t *peer);
-extern int  kibnal_close_stale_conns_locked (kib_peer_t *peer, 
+extern void kibnal_peer_alive(kib_peer_t *peer);
+extern int  kibnal_close_stale_conns_locked (kib_peer_t *peer,
                                               __u64 incarnation);
 extern kib_conn_t *kibnal_create_conn (void);
-extern void kibnal_put_conn (kib_conn_t *conn);
 extern void kibnal_destroy_conn (kib_conn_t *conn);
 extern int kibnal_alloc_pages (kib_pages_t **pp, int npages, int access);
 extern void kibnal_free_pages (kib_pages_t *p);
@@ -507,9 +677,12 @@ extern void kibnal_free_pages (kib_pages_t *p);
 extern void kibnal_check_sends (kib_conn_t *conn);
 
 extern tTS_IB_CM_CALLBACK_RETURN
+kibnal_bad_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
+                          void *param, void *arg);
+extern tTS_IB_CM_CALLBACK_RETURN
 kibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
                        void *param, void *arg);
-extern tTS_IB_CM_CALLBACK_RETURN 
+extern tTS_IB_CM_CALLBACK_RETURN
 kibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
                                void *param, void *arg);
 
@@ -518,16 +691,16 @@ extern void kibnal_destroy_conn (kib_conn_t *conn);
 extern int  kibnal_thread_start (int (*fn)(void *arg), void *arg);
 extern int  kibnal_scheduler(void *arg);
 extern int  kibnal_connd (void *arg);
-extern void kibnal_callback (struct ib_cq *cq, struct ib_cq_entry *e, void *arg);
+extern int  kibnal_reaper (void *arg);
+extern void kibnal_callback (ib_cq_t *cq, struct ib_cq_entry *e, void *arg);
+extern void kibnal_txlist_done (struct list_head *txlist, int status);
 extern void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
 extern int  kibnal_close_conn (kib_conn_t *conn, int why);
-extern void kibnal_start_active_rdma (int type, int status, 
-                                      kib_rx_t *rx, lib_msg_t *libmsg, 
-                                      unsigned int niov, 
-                                      struct iovec *iov, ptl_kiov_t *kiov,
-                                      size_t offset, size_t nob);
-
-
-
-
-
+extern void kibnal_start_active_rdma (int type, int status,
+                                      kib_rx_t *rx, lnet_msg_t *lntmsg,
+                                      unsigned int niov,
+                                      struct iovec *iov, lnet_kiov_t *kiov,
+                                      int offset, int nob);
+
+extern int  kibnal_tunables_init(void);
+extern void kibnal_tunables_fini(void);