Whamcloud - gitweb
012e50f464322f9fef49bc4d45e69b6250aef3c5
[fs/lustre-release.git] / lnet / klnds / gnilnd / gnilnd.h
1 /*
2  * Copyright (C) 2004 Cluster File Systems, Inc.
3  *
4  * Copyright (C) 2009-2012 Cray, Inc.
5  *
6  *   Derived from work by: Eric Barton <eric@bartonsoftware.com>
7  *   Author: Nic Henke <nic@cray.com>
8  *   Author: James Shimek <jshimek@cray.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26 #ifndef _GNILND_GNILND_H_
27 #define _GNILND_GNILND_H_
28
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #ifdef HAVE_LINUX_KERNEL_LOCK
36 #include <linux/smp_lock.h>
37 #endif
38 #include <linux/unistd.h>
39 #include <linux/uio.h>
40 #include <linux/time.h>
41 #include <asm/timex.h>
42
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/io.h>
46
47 #include <linux/init.h>
48 #include <linux/fs.h>
49 #include <linux/file.h>
50 #include <linux/stat.h>
51 #include <linux/list.h>
52 #include <linux/kmod.h>
53 #include <linux/sysctl.h>
54 #include <linux/kthread.h>
55 #include <linux/nmi.h>
56
57 #include <net/sock.h>
58 #include <linux/in.h>
59 #include <linux/nmi.h>
60
61 #define DEBUG_SUBSYSTEM S_LND
62
63 #include <libcfs/linux/kp30.h>
64 #include <libcfs/libcfs.h>
65 #include <lnet/lnet.h>
66 #include <lnet/lib-lnet.h>
67 #include <lnet/lnet-sysctl.h>
68
69 #include <gni_pub.h>
70 #include "gnilnd_version.h"
71
72
73 /* tunables determined at compile time */
74 #define GNILND_MIN_TIMEOUT      5               /* minimum timeout interval (seconds) */
75 #define GNILND_TO2KA(t)         (((t)-1)/2)     /* timeout -> keepalive interval */
76 #define GNILND_MIN_RECONNECT_TO (GNILND_BASE_TIMEOUT/4)
77 #define GNILND_MAX_RECONNECT_TO GNILND_BASE_TIMEOUT
78 #define GNILND_HARDWARE_TIMEOUT 15              /* maximum time for data to travel between nodes */
79 #define GNILND_MDD_TIMEOUT      15              /* MDD hold timeout in minutes */
80 #define GNILND_SCHED_TIMEOUT       1
81 #define GNILND_DGRAM_TIMEOUT       2
82 #define GNILND_FAST_MAPPING_TRY   \
83         *kgnilnd_tunables.kgn_max_retransmits   /* maximum number to attempt mapping of a tx */
84 #define GNILND_MAP_RETRY_RATE      1            /* interval between mapping attempts in jiffies */
85
86 /* map failure timeout */
87 #define GNILND_MAP_TIMEOUT         \
88         (cfs_time_seconds(*kgnilnd_tunables.kgn_timeout * \
89          *kgnilnd_tunables.kgn_timeout))
90
91 /* reaper thread wakup interval */
92 #define GNILND_REAPER_THREAD_WAKE  1
93 /* reaper thread checks each conn NCHECKS time every kgnilnd_data.kgn_new_min_timeout */
94 #define GNILND_REAPER_NCHECKS      4
95
96 /* fixed constants */
97 #define GNILND_MAXDEVS          1               /* max # of GNI devices currently supported */
98 #define GNILND_MBOX_CREDITS     256             /* number of credits per mailbox */
99 #define GNILND_COOKIE           0xa3579         /* cookie used by along with ptag by GNI */
100 #define GNILND_CONN_MAGIC         0xa100f       /* magic value for verifying connection validity */
101 /* checksum values */
102 #define GNILND_CHECKSUM_OFF             0       /* checksum turned off */
103 #define GNILND_CHECKSUM_SMSG_HEADER     1       /* Only checksum SMSG header */
104 #define GNILND_CHECKSUM_SMSG            2       /* checksum entire SMSG packet */
105 #define GNILND_CHECKSUM_SMSG_BTE        3       /* Full checksum support */
106
107 /* tune down some COMPUTE options as they won't see the same number of connections and
108  * don't need the throughput of multiple threads by default */
109 #if defined(CONFIG_CRAY_COMPUTE)
110 #define GNILND_SCHED_THREADS      1             /* default # of kgnilnd_scheduler threads */
111 #define GNILND_FMABLK             64            /* default number of mboxes per fmablk */
112 #define GNILND_SCHED_NICE         0             /* default nice value for scheduler threads */
113 #define GNILND_COMPUTE            1             /* compute image */
114 #else
115 #define GNILND_SCHED_THREADS      3             /* default # of kgnilnd_scheduler threads */
116 #define GNILND_FMABLK             1024          /* default number of mboxes per fmablk */
117 #define GNILND_SCHED_NICE         -20           /* default nice value for scheduler threads */
118 #define GNILND_COMPUTE            0             /* service image */
119 #endif
120
121 /* EXTRA_BITS are there to allow us to hide NOOP/CLOSE and anything else out of band */
122 #define GNILND_EXTRA_BITS         1
123 /* maximum number of conns & bits for cqid in the SMSG event data */
124 #define GNILND_CQID_NBITS         (21 - GNILND_EXTRA_BITS)
125 #define GNILND_MSGID_TX_NBITS     (32 - GNILND_CQID_NBITS)
126 #define GNILND_MAX_CQID           (1 << GNILND_CQID_NBITS)
127 #define GNILND_MAX_MSG_ID         (1 << GNILND_MSGID_TX_NBITS)
128 #define GNILND_MAX_MSG_SIZE       (*kgnilnd_tunables.kgn_max_immediate + sizeof(kgn_msg_t))
129
130 /* need sane upper bound to limit copy overhead */
131 #define GNILND_MAX_IMMEDIATE      (64<<10)
132
133 /* payload size to add to the base mailbox size
134  * This is subtracting 2 from the concurrent_sends as 4 messages are included in the size
135  * gni_smsg_buff_size_needed calculates, the MAX_PAYLOAD is added to
136  * the calculation return from that function.*/
137 #define GNILND_MBOX_PAYLOAD     \
138           (GNILND_MAX_MSG_SIZE * \
139           ((*kgnilnd_tunables.kgn_concurrent_sends - 2) * 2));
140
141 /* timeout -> deadman timer for kgni mdd holds */
142 #define GNILND_TIMEOUT2DEADMAN   ((*kgnilnd_tunables.kgn_mdd_timeout) * 1000 * 60)
143
144 /* timeout for failing sends in t is in jiffies*/
145 #define GNILND_TIMEOUTRX(t)     (t + cfs_time_seconds(*kgnilnd_tunables.kgn_hardware_timeout))
146
147 /* time when to release from purgatory in the reaper thread in jiffies */
148 #define GNILND_PURG_RELEASE(t)   (GNILND_TIMEOUTRX(t) * 3)
149
150 /* Macro for finding last_rx 2 datapoints are compared
151  * and the most recent one in jiffies is returned.
152  */
153 #define GNILND_LASTRX(conn) (time_after(conn->gnc_last_rx, conn->gnc_last_rx_cq) \
154                                 ? conn->gnc_last_rx : conn->gnc_last_rx_cq)
155
156 /************************************************************************
157  * Enum, flag and tag data
158  */
159 #define GNILND_INIT_NOTHING         0
160 #define GNILND_INIT_DATA            1
161 #define GNILND_INIT_ALL             2
162
163 /* If you change the ordering away from MAPPED = UNMAPPED + 1, things break */
164 #define GNILND_BUF_NONE           0              /* buffer type not set */
165 #define GNILND_BUF_IMMEDIATE      1              /* immediate data */
166 #define GNILND_BUF_IMMEDIATE_KIOV 2              /* immediate data */
167 #define GNILND_BUF_PHYS_UNMAPPED  3              /* physical: not mapped yet */
168 #define GNILND_BUF_PHYS_MAPPED    4              /* physical: mapped already */
169 #define GNILND_BUF_VIRT_UNMAPPED  5              /* virtual: not mapped yet */
170 #define GNILND_BUF_VIRT_MAPPED    6              /* virtual: mapped already */
171
172 #define GNILND_TX_WAITING_REPLY      (1<<1)     /* expecting to receive reply */
173 #define GNILND_TX_WAITING_COMPLETION (1<<2)     /* waiting for smsg_send to complete */
174 #define GNILND_TX_PENDING_RDMA       (1<<3)     /* RDMA transaction pending until we get prev. completion */
175 #define GNILND_TX_QUIET_ERROR        (1<<4)     /* don't print error on tx_done */
176 #define GNILND_TX_FAIL_SMSG          (1<<5)     /* pass down error injection for SMSG fail */
177
178 /* stash above max CQID to avoid any collision */
179 #define GNILND_MSGID_NOOP           (GNILND_MAX_CQID + 128)
180 #define GNILND_MSGID_CLOSE          (GNILND_MSGID_NOOP + 1)
181
182 /* kgn_msg_t::gnm_type */
183 #define GNILND_MSG_NONE              0x00        /* illegal message */
184 #define GNILND_MSG_NOOP              0x01        /* empty gnm_u (keepalive) */
185 #define GNILND_MSG_IMMEDIATE         0x02        /* gnm_u.immediate */
186 #define GNILND_MSG_PUT_REQ           0x03        /* gnm_u.putreq (src->sink) */
187 #define GNILND_MSG_PUT_NAK           0x04        /* gnm_u.completion (no PUT match: sink->src) */
188 #define GNILND_MSG_PUT_ACK           0x05        /* gnm_u.putack (PUT matched: sink->src) */
189 #define GNILND_MSG_PUT_DONE          0x06        /* gnm_u.completion (src->sink) */
190 #define GNILND_MSG_GET_REQ           0x07        /* gnm_u.get (sink->src) */
191 #define GNILND_MSG_GET_NAK           0x08        /* gnm_u.completion (no GET match: src->sink) */
192 #define GNILND_MSG_GET_DONE          0x09        /* gnm_u.completion (src->sink) */
193 #define GNILND_MSG_CLOSE             0x0a        /* empty gnm_u */
194 #define GNILND_MSG_PUT_REQ_REV       0x0b        /* gnm_u.get (src->sink) */
195 #define GNILND_MSG_PUT_DONE_REV      0x0c        /* gnm_u.completion (sink->src) */
196 #define GNILND_MSG_PUT_NAK_REV       0x0d        /* gnm_u.completion (no PUT match: sink->src) */
197 #define GNILND_MSG_GET_REQ_REV       0x0e        /* gnm_u.get (sink->src ) */
198 #define GNILND_MSG_GET_ACK_REV       0x0f        /* gnm_u.getack (GET matched: src->sink) */
199 #define GNILND_MSG_GET_DONE_REV      0x10        /* gnm_u.completion (sink -> src) */
200 #define GNILND_MSG_GET_NAK_REV       0x11        /* gnm_u.completeion (no GET match: sink -> src) */
201
202 /* defines for gnc_*scheduled states */
203 #define GNILND_CONN_IDLE             0
204 #define GNILND_CONN_SCHED            1
205 #define GNILND_CONN_WANTS_SCHED      2
206 #define GNILND_CONN_PROCESS          3
207
208 #define GNILND_DEV_IDLE              0
209 #define GNILND_DEV_IRQ               1
210 #define GNILND_DEV_LOOP              2
211
212 #define GNILND_DGRAM_IDLE            0
213 #define GNILND_DGRAM_SCHED           1
214 #define GNILND_DGRAM_PROCESS         2
215
216 #define GNILND_PEER_IDLE             0
217 #define GNILND_PEER_CONNECT          1
218 #define GNILND_PEER_POSTING          2
219 #define GNILND_PEER_POSTED           3
220 #define GNILND_PEER_NEEDS_DEATH      4
221 #define GNILND_PEER_KILL             5
222
223 /* for gnc_close_recvd */
224 #define GNILND_CLOSE_RX              1
225 #define GNILND_CLOSE_INJECT1         2
226 #define GNILND_CLOSE_INJECT2         3
227 #define GNILND_CLOSE_EARLY           4
228
229 /* defines for why quiesce trigger set */
230 #define GNILND_QUIESCE_IDLE          0
231 #define GNILND_QUIESCE_ADMIN         1
232 #define GNILND_QUIESCE_RESET         2
233 #define GNILND_QUIESCE_HW_QUIESCE    3
234
235 #define GNILND_PEER_CLEAN            0
236 #define GNILND_PEER_PERSISTING       1
237
238 #define GNILND_DEL_CONN              0
239 #define GNILND_DEL_PEER              1
240 #define GNILND_CLEAR_PURGATORY       2
241
242 #define GNILND_RCA_NODE_UP           0
243 #define GNILND_RCA_NODE_DOWN         1
244 #define GNILND_RCA_NODE_UNKNOWN      2
245
246 /* defines for reverse RDMA states */
247 #define GNILND_REVERSE_NONE             0
248 #define GNILND_REVERSE_GET              1
249 #define GNILND_REVERSE_PUT              2
250 #define GNILND_REVERSE_BOTH             (GNILND_REVERSE_GET | GNILND_REVERSE_PUT)
251
252 typedef enum kgn_fmablk_state {
253         GNILND_FMABLK_IDLE = 0, /* is allocated or ready to be freed */
254         GNILND_FMABLK_PHYS,     /* allocated out of slab of physical memory */
255         GNILND_FMABLK_VIRT,     /* 'standard' vmalloc hunk */
256         GNILND_FMABLK_FREED,    /* after free */
257 } kgn_fmablk_state_t;
258
259 typedef enum kgn_tx_list_state {
260         GNILND_TX_IDLE = 0,     /* TX is on the idle list, kgn_idle_txs */
261         GNILND_TX_ALLOCD,       /* TX has been alloced (off of idle), could be in any state transition */
262         GNILND_TX_PEERQ,        /* TX on peer->gnp_tx_queue (no live conn) */
263         GNILND_TX_MAPQ,         /* TX on dev:gnd_map_tx for buffer mapping */
264         GNILND_TX_FMAQ,         /* TX waiting to be send on conn FMA */
265         GNILND_TX_LIVE_FMAQ,    /* TX live on the FMA wire, waiting for completion or reply */
266         GNILND_TX_RDMAQ,        /* TX waiting to send FMA confirmation to auth RDMA PUT */
267         GNILND_TX_LIVE_RDMAQ,   /* TX live on the RDMA wire, waiting for completion */
268         GNILND_TX_DYING,        /* TX got caught on MAPQ or RDMAQ while conn was closing, needs someone to call tx_done */
269         GNILND_TX_FREED         /* TX is free! */
270 } kgn_tx_list_state_t;
271
272 typedef enum kgn_conn_state {
273         /* don't start @ 0 - prevent memset(0) badness */
274         GNILND_CONN_DUMMY = 0,
275         GNILND_CONN_LISTEN,
276         GNILND_CONN_CONNECTING,
277         GNILND_CONN_ESTABLISHED,
278         GNILND_CONN_CLOSING,
279         GNILND_CONN_CLOSED,
280         GNILND_CONN_DONE,
281         GNILND_CONN_DESTROY_EP
282 } kgn_conn_state_t;
283
284 /* changing these requires a change to GNILND_CONNREQ_VERSION and
285  * will result in dropped packets instead of NAKs. Adding to this is
286  * acceptable without changing the CONNREQ_VERSION, but code should
287  * be ready to handle NAKs on version mismatch  */
288 typedef enum kgn_connreq_type {
289         GNILND_CONNREQ_REQ = 1,         /* how YOU doin' ? */
290         GNILND_CONNREQ_NAK,             /* NO soup for you! */
291         GNILND_CONNREQ_CLOSE,           /* we should see other people */
292 } kgn_connreq_type_t;
293
294 typedef enum kgn_dgram_state {
295         /* don't use 0 to avoid thinking a memset of zero is valid data */
296         GNILND_DGRAM_USED = 1,
297         GNILND_DGRAM_POSTING,
298         GNILND_DGRAM_POSTED,
299         GNILND_DGRAM_PROCESSING,
300         GNILND_DGRAM_CANCELED,
301         GNILND_DGRAM_DONE,
302 } kgn_dgram_state_t;
303
304 typedef enum kgn_dgram_type {
305         GNILND_DGRAM_REQ = 1,         /* how YOU doin' ? */
306         GNILND_DGRAM_WC_REQ,          /* you talkin' to ME? */
307         GNILND_DGRAM_NAK,             /* NO soup for you! */
308         GNILND_DGRAM_CLOSE,           /* we should see other people */
309 } kgn_dgram_type_t;
310
311 /************************************************************************
312  * Wire message structs.  These are sent in sender's byte order
313  * (i.e. receiver checks magic and flips if required).
314  */
315
316 #define GNILND_MSG_MAGIC     LNET_PROTO_GNI_MAGIC /* unique magic */
317 #define GNILND_DGRAM_MAGIC   0x0DDBA11
318
319 /*  kgn_msg_t - FMA/SMSG wire struct
320   v2:
321    * - added checksum to FMA
322    * moved seq before paylod
323    * WIRE_ATTR added for alignment
324   v3:
325    * added gnm_payload_len for FMA payload size
326   v4:
327    * added gncm_retval to completion, allowing return code transmission
328      on RDMA NAKs
329   v5:
330    * changed how CQID and TX ids are assigned
331   v6:
332    * added retval on CLOSE
333   v7:
334    * added payload checksumming
335   v8:
336    * reworked checksumming a bit, changed payload checksums
337 */
338 #define GNILND_MSG_VERSION              8
339 /* kgn_connreq_t connection request datagram wire struct
340   v2:
341    * added NAKs
342 */
343
344 #define GNILND_CONNREQ_VERSION          2
345
346 typedef struct kgn_gniparams {
347         __u32            gnpr_host_id;          /* ph. host ID of the NIC */
348         __u32            gnpr_cqid;             /* cqid I want peer to use when sending events to me */
349         gni_smsg_attr_t  gnpr_smsg_attr;        /* my short msg. attributes */
350 } WIRE_ATTR kgn_gniparams_t;
351
352 typedef struct kgn_nak_data {
353         __s32            gnnd_errno;            /* errno reason for NAK */
354
355 } WIRE_ATTR kgn_nak_data_t;
356
357 /* the first bits of the connreq struct CANNOT CHANGE FORM EVER
358  * without breaking the ability for us to properly NAK someone */
359 typedef struct kgn_connreq {                    /* connection request/response */
360         __u32             gncr_magic;           /* I'm an gnilnd connreq */
361         __u32             gncr_cksum;           /* checksum (0 == disabled) */
362         __u16             gncr_type;            /* REQ, NAK, etc */
363         __u16             gncr_version;         /* this is my version number */
364         __u32             gncr_timeout;         /* sender's timeout */
365         __u64             gncr_srcnid;          /* sender's NID */
366         __u64             gncr_dstnid;          /* who sender expects to listen */
367         __u64             gncr_peerstamp;       /* sender's instance stamp */
368         __u64             gncr_connstamp;       /* sender's connection stamp */
369
370         /* everything before this needs to stay static, adding after should
371          * result in a change to GNILND_CONNREQ_VERSION */
372
373         union {
374                 kgn_gniparams_t   gncr_gnparams;        /* sender's endpoint info */
375                 kgn_nak_data_t    gncr_nakdata;         /* data (rc, etc) for NAK */
376         };
377 } WIRE_ATTR kgn_connreq_t;
378
379 typedef struct {
380         gni_mem_handle_t  gnrd_key;
381         __u64             gnrd_addr;
382         __u32             gnrd_nob;
383 } WIRE_ATTR kgn_rdma_desc_t;
384
385 typedef struct {
386         lnet_hdr_t        gnim_hdr;             /* LNet header */
387         /* LNet payload is in FMA "Message Data" */
388 } WIRE_ATTR kgn_immediate_msg_t;
389
390 typedef struct {
391         lnet_hdr_t        gnprm_hdr;            /* LNet header */
392         __u64             gnprm_cookie;         /* opaque completion cookie */
393 } WIRE_ATTR kgn_putreq_msg_t;
394
395 typedef struct {
396         __u64             gnpam_src_cookie;     /* reflected completion cookie */
397         __u64             gnpam_dst_cookie;     /* opaque completion cookie */
398         __u16             gnpam_payload_cksum;  /* checksum for get msg */
399         kgn_rdma_desc_t   gnpam_desc;           /* sender's sink buffer */
400 } WIRE_ATTR kgn_putack_msg_t;
401
402 typedef struct {
403         lnet_hdr_t        gngm_hdr;             /* LNet header */
404         __u64             gngm_cookie;          /* opaque completion cookie */
405         __u16             gngm_payload_cksum;   /* checksum for put msg */
406         kgn_rdma_desc_t   gngm_desc;            /* sender's sink buffer */
407 } WIRE_ATTR kgn_get_msg_t;
408
409 typedef struct {
410         int               gncm_retval;          /* error on NAK, size on REQ */
411         __u64             gncm_cookie;          /* reflected completion cookie */
412 } WIRE_ATTR kgn_completion_msg_t;
413
414 typedef struct {                                /* NB must fit in FMA "Prefix" */
415         __u32             gnm_magic;            /* I'm an gni message */
416         __u16             gnm_version;          /* this is my version number */
417         __u16             gnm_type;             /* msg type */
418         __u64             gnm_srcnid;           /* sender's NID */
419         __u64             gnm_connstamp;        /* sender's connection stamp */
420         __u32             gnm_seq;              /* incrementing sequence number */
421         __u16             gnm_cksum;            /* checksum (0 == no checksum ) */
422         __u16             gnm_payload_cksum;    /* payload checksum (0 == no checksum ) */
423         __u32             gnm_payload_len;      /* size of the FMA payload sent */
424         union {
425                 kgn_immediate_msg_t   immediate;
426                 kgn_putreq_msg_t      putreq;
427                 kgn_putack_msg_t      putack;
428                 kgn_get_msg_t         get;
429                 kgn_completion_msg_t  completion;
430         } gnm_u;
431 } WIRE_ATTR kgn_msg_t;
432
433 /************************************************************************
434  * runtime tunable data
435  */
436
437 typedef struct kgn_tunables {
438         int              *kgn_min_reconnect_interval; /* connreq starting timeout & retransmit interval */
439         int              *kgn_max_reconnect_interval; /* ...exponentially increasing to this */
440         int              *kgn_credits;          /* # concurrent sends */
441         int              *kgn_fma_cq_size;      /* # entries in receive CQ */
442         int              *kgn_peer_credits;     /* # LNet peer credits */
443         int              *kgn_concurrent_sends; /* max # of max_immediate in mbox */
444         int              *kgn_timeout;          /* comms timeout (seconds) */
445         int              *kgn_max_immediate;    /* immediate payload breakpoint */
446         int              *kgn_checksum;         /* checksum data */
447         int              *kgn_checksum_dump;    /* dump raw data to D_INFO log when checksumming */
448         int              *kgn_bte_dlvr_mode;    /* BTE delivery mode mask */
449         int              *kgn_bte_relaxed_ordering; /* relaxed ordering (PASSPW) on BTE transfers */
450         int              *kgn_ptag;             /* PTAG for cdm_create */
451         int              *kgn_max_retransmits;  /* max number of FMA retransmits */
452         int              *kgn_nwildcard;        /* # wildcard per net to post */
453         int              *kgn_nice;             /* nice value for kgnilnd threads */
454         int              *kgn_rdmaq_intervals;  /* # intervals per second for rdmaq throttle */
455         int              *kgn_loops;            /* # of loops sched does before flush/heartbeat tickle */
456         int              *kgn_peer_hash_size;   /* size of kgn_peers */
457         int              *kgn_peer_health;      /* enable/disable peer health */
458         int              *kgn_peer_timeout;     /* Override of the default peer_timeout used by peer_health */
459         int              *kgn_vmap_cksum;       /* enable/disable vmap of kiov checksums */
460         int              *kgn_mbox_per_block;   /* mailboxes per fmablk */
461         int              *kgn_nphys_mbox;       /* # mailboxes to preallocate with physical memory */
462         int              *kgn_mbox_credits;     /* max credits per fma */
463         int              *kgn_sched_threads;    /* number of kgnilnd_scheduler threads */
464         int              *kgn_net_hash_size;    /* size of kgn_net_ht */
465         int              *kgn_hardware_timeout; /* max time for a message to get across the network */
466         int              *kgn_mdd_timeout;      /* max time for ghal to hold an mdd in minutes */
467         int              *kgn_sched_timeout;    /* max time for scheduler to run before yielding */
468         int              *kgn_dgram_timeout;    /* max time for dgram mover to run before scheduling */
469         int              *kgn_sched_nice;       /* nice value for kgnilnd scheduler threads */
470         int              *kgn_reverse_rdma;     /* Reverse RDMA setting */
471 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
472         cfs_sysctl_table_header_t *kgn_sysctl;  /* sysctl interface */
473 #endif
474 } kgn_tunables_t;
475
476 typedef struct kgn_mbox_info {
477         lnet_nid_t mbx_prev_nid;
478         lnet_nid_t mbx_prev_purg_nid;
479         unsigned long mbx_create_conn_memset;
480         unsigned long mbx_add_purgatory;
481         unsigned long mbx_detach_of_purgatory;
482         unsigned long mbx_release_from_purgatory;
483         unsigned long mbx_release_purg_active_dgram;
484         int           mbx_nallocs;
485         int           mbx_nallocs_total;
486 } kgn_mbox_info_t;
487
488 typedef struct kgn_fma_memblock {
489         struct list_head    gnm_bufflist;                          /* memblock is part of device's  gnd_fma_buffs */
490         kgn_fmablk_state_t  gnm_state;                             /* how this memory allocated & state of it */
491         int                 gnm_hold_timeout;                      /* hold_timeout if used at unmap time */
492         int                 gnm_num_mboxs;                         /* total mboxes allocated */
493         int                 gnm_avail_mboxs;                       /* number of available mailboxes in the block */
494         int                 gnm_held_mboxs;                        /* number of purgatory held  mailboxes */
495         int                 gnm_mbox_size;                         /* size of the single mailbox */
496         int                 gnm_next_avail_mbox;                   /* next available mailbox */
497         long                gnm_max_timeout;                       /* max timeout for possible purgatory hold */
498         unsigned int        gnm_blk_size;                          /* how big is our hunk o memory ? */
499         void               *gnm_block;                             /* pointer to mem. block */
500         gni_mem_handle_t    gnm_hndl;                              /* mem. handle of the block */
501         unsigned long      *gnm_bit_array;                         /* bit array tracking allocation of mailboxes */
502         kgn_mbox_info_t    *gnm_mbox_info;                         /* array of mbox_information about each mbox */
503 } kgn_fma_memblock_t;
504
505 typedef struct kgn_device {
506         gni_nic_handle_t        gnd_handle;       /* device handle */
507         gni_cdm_handle_t        gnd_domain;       /* GNI communication domain */
508         gni_err_handle_t        gnd_err_handle;   /* device error handle */
509         unsigned long           gnd_sched_alive;  /* scheduler thread alive stamp */
510         gni_cq_handle_t         gnd_rcv_fma_cqh;  /* FMA rcv. completion queue handle */
511         gni_cq_handle_t         gnd_snd_rdma_cqh; /* rdma send completion queue handle */
512         gni_cq_handle_t         gnd_snd_fma_cqh;  /* rdma send completion queue handle */
513         struct mutex            gnd_cq_mutex;     /* CQ access serialization */
514         __u32                   gnd_host_id;      /* ph. host ID of the NIC */
515         int                     gnd_id;           /* device id, also index in kgn_devices */
516         __u32                   gnd_nid;          /* ph host ID translated to NID */
517         struct list_head        gnd_fma_buffs;    /* list of FMA memory blocks */
518         struct semaphore        gnd_fmablk_sem;   /* semaphore for FMA block memory alloc/free */
519         spinlock_t              gnd_fmablk_lock;  /* lock for mbox alloc/release */
520         atomic_t                gnd_nfmablk;      /* # of fmablk live */
521         atomic_t                gnd_fmablk_vers;  /* gnd_fma_bufs stamp */
522         atomic_t                gnd_neps;         /* # EP allocated to conns */
523         short                   gnd_ready;        /* stuff to do in scheduler thread */
524         struct list_head        gnd_ready_conns;  /* connections ready to tx/rx */
525         struct list_head        gnd_map_tx;       /* TX: needing buffer mapping */
526         wait_queue_head_t       gnd_waitq;        /* scheduler wakeup */
527         spinlock_t              gnd_lock;         /* serialise gnd_ready_conns */
528         struct list_head        gnd_connd_peers;  /* peers waiting for a connection */
529         spinlock_t              gnd_connd_lock;   /* serialise connd_peers */
530         wait_queue_head_t       gnd_dgram_waitq;  /* dgram_mover thread wakeup */
531         wait_queue_head_t       gnd_dgping_waitq; /* dgram thread ping-pong */
532         int                     gnd_dgram_ready;  /* dgrams need movin' */
533         struct list_head       *gnd_dgrams;       /* nid hash to dgrams */
534         atomic_t                gnd_ndgrams;      /* # dgrams extant */
535         atomic_t                gnd_nwcdgrams;    /* # wildcard dgrams to post on device */
536         spinlock_t              gnd_dgram_lock;   /* serialize gnd_dgrams */
537         struct list_head        gnd_map_list;     /* list of all mapped regions */
538         int                     gnd_map_version;  /* version flag for map list */
539         struct timer_list       gnd_map_timer;    /* wakey-wakey */
540         atomic_t                gnd_n_mdd;        /* number of total MDD - fma, tx, etc */
541         atomic_t                gnd_n_mdd_held;   /* number of total MDD held - fma, tx, etc */
542         atomic_t                gnd_nq_map;       /* # queued waiting for mapping (MDD/GART) */
543         atomic64_t              gnd_nbytes_map;   /* bytes of total GART maps - fma, tx, etc */
544         __u32                   gnd_map_nphys;    /* # TX phys mappings */
545         __u32                   gnd_map_physnop;  /* # TX phys pages mapped */
546         __u32                   gnd_map_nvirt;    /* # TX virt mappings */
547         __u64                   gnd_map_virtnob;  /* # TX virt bytes mapped */
548         spinlock_t              gnd_map_lock;     /* serialize gnd_map_XXX */
549         unsigned long           gnd_next_map;     /* next mapping attempt in jiffies */
550         int                     gnd_map_attempt;  /* last map attempt # */
551         unsigned long           gnd_last_map;     /* map timeout base */
552         struct list_head        gnd_rdmaq;        /* RDMA to be sent */
553         spinlock_t              gnd_rdmaq_lock;   /* play nice with others */
554         atomic64_t              gnd_rdmaq_bytes_out; /* # bytes authorized */
555         atomic64_t              gnd_rdmaq_bytes_ok;  /* # bytes allowed until deadline */
556         atomic_t                gnd_rdmaq_nstalls;   /* # stalls due to throttle */
557         unsigned long           gnd_rdmaq_deadline;  /* when does bucket roll over ? */
558         struct timer_list       gnd_rdmaq_timer;     /* wakey-wakey */
559         atomic_t                gnd_short_ntx;      /* TX stats: short messages */
560         atomic64_t              gnd_short_txbytes;  /* TX stats: short message  payload*/
561         atomic_t                gnd_rdma_ntx;       /* TX stats: rdma messages */
562         atomic64_t              gnd_rdma_txbytes;   /* TX stats: rdma message payload*/
563         atomic_t                gnd_short_nrx;      /* RX stats: short messages */
564         atomic64_t              gnd_short_rxbytes;  /* RX stats: short message  payload*/
565         atomic_t                gnd_rdma_nrx;       /* RX stats: rdma messages */
566         atomic64_t              gnd_rdma_rxbytes;   /* RX stats: rdma message payload*/
567         atomic_t                gnd_fast_try;       /* # of times fast send tried */
568         atomic_t                gnd_fast_ok;        /* # of times fast send ok */
569         atomic_t                gnd_fast_block;     /* # of times fast send blocked */
570         unsigned long           gnd_mutex_delay;
571         atomic_t                gnd_n_yield;
572         atomic_t                gnd_n_schedule;
573         atomic_t                gnd_canceled_dgrams; /* # of outstanding cancels */
574         struct rw_semaphore     gnd_conn_sem;       /* serialize connection changes/data movement */
575 } kgn_device_t;
576
577 typedef struct kgn_net {
578         struct list_head    gnn_list;           /* chain on kgni_data::kgn_nets */
579         kgn_device_t       *gnn_dev;            /* device for this net */
580         lnet_ni_t          *gnn_ni;             /* network interface instance */
581         atomic_t            gnn_refcount;       /* # current references */
582         int                 gnn_shutdown;       /* lnd_shutdown set */
583         __u16               gnn_netnum;         /* stash netnum for quicker lookup */
584 } kgn_net_t;
585
586 static inline lnet_nid_t
587 kgnilnd_lnd2lnetnid(lnet_nid_t ni_nid, lnet_nid_t kgnilnd_nid)
588 {
589         return LNET_MKNID(LNET_NIDNET(ni_nid), LNET_NIDADDR(kgnilnd_nid));
590 }
591
592 static inline lnet_nid_t
593 kgnilnd_lnet2lndnid(lnet_nid_t lnet_nid, lnet_nid_t kgnilnd_nid)
594 {
595         return LNET_MKNID(LNET_NIDNET(kgnilnd_nid), LNET_NIDADDR(lnet_nid));
596 }
597
598 /* The code for this is a bit ugly - but really  this just boils down to a __u64
599  * that can have various parts accessed separately.
600  *
601  * The lower 32 bits is the ID
602  * we give to SMSG for our completion event - it needs to be globally unique across
603  * all TX currently in flight. We separate that out into the CQID so that we can
604  * reference the connection (kgnilnd_cqid2conn_locked) and then the msg_id to pull
605  * the actual TX out of the per-connection gnc_tx_ref_table.
606  *
607  * The upper 32 bits are just extra stuff we put into the cookie to ensure this TX
608  * has a unique value we can send with RDMA setup messages to ensure the completion for
609  * those is unique across the wire. The extra 32 bits are there to ensure that TX id
610  * reuse is separated.
611  */
612
613 typedef struct kgn_tx_ev_id {
614         union {
615                 __u64             txe_cookie;    /* are you my mommy ? */
616                 struct {
617                         __u32     txe_chips;     /* extra bits to ensure ID unique across reuse */
618                         union {
619                                 __u32     txe_smsg_id;      /* ID for SMSG CQ event */
620                                 /* N.B: Never ever ever ever use the bit shifts directly,
621                                  * you are just asking for a world of pain and are at the
622                                  * mercy of the compiler layouts */
623                                 struct {
624                                         __u32     txe_cqid :GNILND_CQID_NBITS;
625                                         __u32     txe_idx :GNILND_MSGID_TX_NBITS;
626                                 };
627                         };
628                 };
629         };
630 } kgn_tx_ev_id_t;
631
632 typedef struct kgn_dgram {
633         struct list_head     gndg_list;          /* on hash dev::gnd_dgrams */
634         kgn_dgram_state_t    gndg_state;         /* state of this dgram */
635         kgn_dgram_type_t     gndg_type;          /* REQ, NAK, etc */
636         __u32                gndg_magic;         /* saftey word */
637         unsigned long        gndg_post_time;     /* time when we posted */
638         struct kgn_conn     *gndg_conn;          /* unbound conn with ep & smsg */
639         kgn_connreq_t        gndg_conn_out;      /* connreq from local node */
640         kgn_connreq_t        gndg_conn_in;       /* connreq from remote node */
641 } kgn_dgram_t;
642
643 typedef struct kgn_tx {                         /* message descriptor */
644         struct list_head          tx_list;      /* TX queues - peer, conn, rdma */
645         kgn_tx_list_state_t       tx_list_state;/* where in state machine is this TX ? */
646         struct list_head         *tx_list_p;    /* pointer to current list */
647         struct kgn_conn          *tx_conn;      /* owning conn */
648         lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
649         unsigned long             tx_qtime;     /* when tx started to wait for something (jiffies) */
650         unsigned long             tx_cred_wait; /* time spend waiting for smsg creds */
651         struct list_head          tx_map_list;  /* list entry on device map list */
652         unsigned int              tx_nob;       /* # bytes of payload */
653         int                       tx_buftype;   /* payload buffer type */
654         int                       tx_phys_npages; /* # physical pages */
655         gni_mem_handle_t          tx_map_key;   /* mapping key */
656         gni_mem_handle_t          tx_buffer_copy_map_key;  /* mapping key for page aligned copy */
657         gni_mem_segment_t        *tx_phys;      /* page descriptors */
658         kgn_msg_t                 tx_msg;       /* FMA message buffer */
659         kgn_tx_ev_id_t            tx_id;        /* who are you, who ? who ? */
660         __u8                      tx_state;     /* state of the descriptor */
661         int                       tx_retrans;   /* retrans count of RDMA */
662         int                       tx_rc;        /* if we need to stash the ret code until we see completion */
663         void                     *tx_buffer;    /* source/sink buffer */
664         void                     *tx_buffer_copy;   /* pointer to page aligned buffer */
665         unsigned int              tx_nob_rdma;  /* nob actually rdma */
666         unsigned int              tx_offset;    /* offset of data into copied buffer */
667         union {
668                 gni_post_descriptor_t     tx_rdma_desc; /* rdma descriptor */
669                 struct page              *tx_imm_pages[GNILND_MAX_IMMEDIATE/PAGE_SIZE];  /* page array to map kiov for immediate send */
670         };
671
672         /* we only use one or the other */
673         union {
674                 kgn_putack_msg_t  tx_putinfo;   /* data for differed rdma & re-try */
675                 kgn_get_msg_t     tx_getinfo;   /* data for rdma re-try*/
676         };
677 } kgn_tx_t;
678
679 typedef struct kgn_conn {
680         kgn_device_t       *gnc_device;         /* which device */
681         struct kgn_peer    *gnc_peer;           /* owning peer */
682         int                 gnc_magic;          /* magic value cleared before free */
683         struct list_head    gnc_list;           /* stash on peer's conn list - or pending purgatory lists as we clear them */
684         struct list_head    gnc_hashlist;       /* stash in connection hash table */
685         struct list_head    gnc_schedlist;      /* schedule (on gnd_?_conns) for attention */
686         struct list_head    gnc_fmaq;           /* txs queued for FMA */
687         struct list_head    gnc_mdd_list;       /* hold list for MDD on hard conn reset */
688         __u64               gnc_peerstamp;      /* peer's unique stamp */
689         __u64               gnc_peer_connstamp; /* peer's unique connection stamp */
690         __u64               gnc_my_connstamp;   /* my unique connection stamp */
691         unsigned long       gnc_first_rx;       /* when I first received an FMA message (jiffies) */
692         unsigned long       gnc_last_tx;        /* when I last sent an FMA message (jiffies) */
693         unsigned long       gnc_last_rx;        /* when I last sent an FMA message (jiffies) */
694         unsigned long       gnc_last_tx_cq;     /* when I last received an FMA CQ (jiffies) */
695         unsigned long       gnc_last_rx_cq;     /* when I last received an FMA CQ (jiffies) */
696         unsigned long       gnc_last_noop_want; /* time I wanted to send NOOP */
697         unsigned long       gnc_last_noop_sent; /* time I did gni_smsg_send on NOOP */
698         unsigned long       gnc_last_noop_cq;   /* time when NOOP completed */
699         unsigned long       gnc_last_sched_ask; /* time when conn added to ready_conns */
700         unsigned long       gnc_last_sched_do;  /* time when conn processed from ready_conns */
701         atomic_t            gnc_reaper_noop;    /* # reaper triggered NOOP */
702         atomic_t            gnc_sched_noop;     /* # sched triggered NOOP */
703         unsigned int        gnc_timeout;        /* infer peer death if no rx for this many seconds */
704         __u32               gnc_cqid;           /* my completion callback id (non-unique) */
705         __u32               gnc_tx_seq;         /* tx msg sequence number */
706         __u32               gnc_rx_seq;         /* rx msg sequence number */
707         __u64               gnc_tx_retrans;     /* # retrans on SMSG */
708         atomic_t            gnc_nlive_fma;      /* # live FMA */
709         atomic_t            gnc_nq_rdma;        /* # queued (on device) RDMA */
710         atomic_t            gnc_nlive_rdma;     /* # live RDMA */
711         short               gnc_close_sent;     /* I've sent CLOSE */
712         short               gnc_close_recvd;    /* I've received CLOSE */
713         short               gnc_in_purgatory;   /* in the sin bin */
714         int                 gnc_error;          /* errno when conn being closed due to error */
715         int                 gnc_peer_error;     /* errno peer sent us on CLOSE */
716         kgn_conn_state_t    gnc_state;          /* connection state */
717         int                 gnc_scheduled;      /* being attented to */
718         char                gnc_sched_caller[30]; /* what function last called schedule */
719         int                 gnc_sched_line;     /* what line # last called schedule */
720         atomic_t            gnc_refcount;       /* # users */
721         spinlock_t          gnc_list_lock;      /* serialise tx lists, max_rx_age */
722         gni_ep_handle_t     gnc_ephandle;       /* GNI endpoint */
723         kgn_fma_memblock_t *gnc_fma_blk;        /* pointer to fma block for our mailbox */
724         gni_smsg_attr_t     gnpr_smsg_attr;     /* my short msg. attributes */
725         spinlock_t          gnc_tx_lock;        /* protect tx alloc/free */
726         __u8                gnc_tx_bits[GNILND_MAX_MSG_ID/8]; /* bit table for tx id */
727         int                 gnc_next_tx;        /* next tx to use in tx_ref_table */
728         kgn_tx_t          **gnc_tx_ref_table;   /* table of TX descriptors for this conn */
729         int                 gnc_mbox_id;        /* id of mbox in fma_blk                 */
730         short               gnc_needs_detach;   /* flag set in detach_purgatory_all_locked so reaper will clear out purgatory */
731         short               gnc_needs_closing;  /* flag set in del_conns when called from kgnilnd_del_peer_or_conn */
732         atomic_t            gnc_tx_in_use;      /* # of tx's currently in use by another thread use kgnilnd_peer_conn_lock */
733         kgn_dgram_type_t    gnc_dgram_type;     /* save dgram type used to establish this conn */
734         void               *remote_mbox_addr;   /* save remote mbox address */
735 } kgn_conn_t;
736
737 typedef struct kgn_mdd_purgatory {
738         gni_mem_handle_t    gmp_map_key;        /* mapping key */
739         struct list_head    gmp_list;           /* entry point for purgatory list */
740 } kgn_mdd_purgatory_t;
741
742 typedef struct kgn_peer {
743         struct list_head    gnp_list;                   /* stash on global peer list */
744         struct list_head    gnp_connd_list;             /* schedule on kgn_connd_peers */
745         struct list_head    gnp_conns;                  /* all active connections and all conns in purgatory for the peer */
746         struct list_head    gnp_tx_queue;               /* msgs waiting for a conn */
747         kgn_net_t          *gnp_net;                    /* net instance for this peer */
748         lnet_nid_t          gnp_nid;                    /* who's on the other end(s) */
749         atomic_t            gnp_refcount;               /* # users */
750         __u32               gnp_host_id;                /* ph. host ID of the peer */
751         short               gnp_connecting;             /* connection forming */
752         short               gnp_pending_unlink;         /* need last conn close to trigger unlink */
753         int                 gnp_last_errno;             /* last error conn saw */
754         unsigned long       gnp_last_alive;             /* last time I had valid comms */
755         int                 gnp_last_dgram_errno;       /* last error dgrams saw */
756         unsigned long       gnp_last_dgram_time;        /* last time I tried to connect */
757         unsigned long       gnp_reconnect_time;         /* CURRENT_SECONDS when reconnect OK */
758         unsigned long       gnp_reconnect_interval;     /* exponential backoff */
759         atomic_t            gnp_dirty_eps;              /* # of old but yet to be destroyed EPs from conns */
760         int                 gnp_down;                   /* rca says peer down */
761         unsigned long       gnp_down_event_time;        /* time peer down */
762         unsigned long       gnp_up_event_time;          /* time peer back up */
763 } kgn_peer_t;
764
765 /* the kgn_rx_t is a struct for handing to LNET as the private pointer for things
766  * like lnet_parse. It allows a single pointer to let us get enough
767  * information in _recv and friends */
768 typedef struct kgn_rx {
769         kgn_conn_t              *grx_conn;      /* connection */
770         kgn_msg_t               *grx_msg;       /* message */
771         lnet_msg_t              *grx_lntmsg;    /* lnet msg for this rx (eager only) */
772         int                      grx_eager;     /* if eager, we copied msg to somewhere */
773         struct timespec          grx_received;  /* time this msg received */
774 } kgn_rx_t;
775
776 typedef struct kgn_data {
777         int                     kgn_init;             /* initialisation state */
778         int                     kgn_shutdown;         /* shut down? */
779         int                     kgn_wc_kill;          /* Should I repost the WC */
780         atomic_t                kgn_nthreads;         /* # live threads */
781         int                     kgn_nresets;          /* number of stack resets */
782         int                     kgn_in_reset;         /* are we in stack reset ? */
783
784         __u64                   kgn_nid_trans_private;/* private data for each of the HW nid2nic arenas */
785
786         kgn_device_t            kgn_devices[GNILND_MAXDEVS]; /* device/ptag/cq etc */
787         int                     kgn_ndevs;            /* # devices */
788
789         int                     kgn_ruhroh_running;   /* ruhroh thread is running */
790         int                     kgn_ruhroh_shutdown;  /* ruhroh thread should or is shut down */
791         wait_queue_head_t       kgn_ruhroh_waitq;     /* ruhroh thread wakeup */
792         int                     kgn_quiesce_trigger;  /* should we quiesce ? */
793         atomic_t                kgn_nquiesce;         /* how many quiesced ? */
794         struct semaphore        kgn_quiesce_sem;      /* serialize ruhroh task, startup and shutdown */
795         int                     kgn_needs_reset;      /* we need stack reset */
796
797         /* These next three members implement communication from gnilnd into
798          * the ruhroh task.  To ensure correct operation of the task, code that
799          * writes into them must use memory barriers to ensure that the changes
800          * are visible to other cores in the order the members appear below.  */
801         __u32                   kgn_quiesce_secs;     /* seconds to bump timeouts */
802         int                     kgn_bump_info_rdy;    /* we have info needed to bump */
803         int                     kgn_needs_pause;      /* we need to pause for network quiesce */
804
805         struct list_head       *kgn_nets;             /* hashtable of kgn_net instances */
806         struct rw_semaphore     kgn_net_rw_sem;       /* serialise gnn_shutdown, kgn_nets */
807
808         rwlock_t                kgn_peer_conn_lock;   /* stabilize peer/conn ops */
809         struct list_head       *kgn_peers;            /* hash table of all my known peers */
810         atomic_t                kgn_npeers;           /* # peers extant */
811         int                     kgn_peer_version;     /* version flag for peer tables */
812
813         struct list_head       *kgn_conns;            /* conns hashed by cqid */
814         atomic_t                kgn_nconns;           /* # connections extant */
815         __u64                   kgn_peerstamp;        /* when I started up */
816         __u64                   kgn_connstamp;        /* conn stamp generator */
817         int                     kgn_conn_version;     /* version flag for conn tables */
818         int                     kgn_next_cqid;        /* cqid generator */
819
820         long                    kgn_new_min_timeout;  /* minimum timeout on any new conn */
821         wait_queue_head_t       kgn_reaper_waitq;     /* reaper sleeps here */
822         spinlock_t              kgn_reaper_lock;      /* serialise */
823
824         cfs_mem_cache_t        *kgn_rx_cache;         /* rx descriptor space */
825         cfs_mem_cache_t        *kgn_tx_cache;         /* tx descriptor memory */
826         cfs_mem_cache_t        *kgn_tx_phys_cache;    /* tx phys descriptor memory */
827         atomic_t                kgn_ntx;              /* # tx in use */
828         cfs_mem_cache_t        *kgn_dgram_cache;      /* outgoing datagrams */
829
830         struct page          ***kgn_cksum_map_pages;  /* page arrays for mapping pages on checksum */
831         __u64                   kgn_cksum_npages;     /* Number of pages allocated for checksumming */
832         atomic_t                kgn_nvmap_cksum;      /* # times we vmapped for checksums */
833         atomic_t                kgn_nvmap_short;      /* # times we vmapped for short kiov */
834
835         atomic_t                kgn_nkmap_short;      /* # time we kmapped for a short kiov */
836         long                    kgn_rdmaq_override;   /* bytes per second override */
837
838         struct kmem_cache      *kgn_mbox_cache;       /* mailboxes from not-GART */
839
840         atomic_t                kgn_npending_unlink;  /* # of peers pending unlink */
841         atomic_t                kgn_npending_conns;   /* # of conns with pending closes */
842         atomic_t                kgn_npending_detach;  /* # of conns with a pending detach */
843         unsigned long           kgn_last_scheduled;   /* last time schedule was called in a sched thread */
844         unsigned long           kgn_last_condresched; /* last time cond_resched was called in a sched thread */
845         atomic_t                kgn_rev_offset;       /* number of time REV rdma have been misaligned offsets */
846         atomic_t                kgn_rev_length;       /* Number of times REV rdma have been misaligned lengths */
847         atomic_t                kgn_rev_copy_buff;    /* Number of times REV rdma have had to make a copy buffer */
848 } kgn_data_t;
849
850 extern kgn_data_t         kgnilnd_data;
851 extern kgn_tunables_t     kgnilnd_tunables;
852
853 extern void kgnilnd_destroy_peer(kgn_peer_t *peer);
854 extern void kgnilnd_destroy_conn(kgn_conn_t *conn);
855 extern int _kgnilnd_schedule_conn(kgn_conn_t *conn, const char *caller, int line, int refheld);
856
857 /* Macro wrapper for _kgnilnd_schedule_conn. This will store the function
858  * and the line of the calling function to allow us to debug problematic
859  * schedule calls in the future without the programmer having to mark
860  * the location manually.
861  */
862 #define kgnilnd_schedule_conn(conn)                                     \
863         _kgnilnd_schedule_conn(conn, __func__, __LINE__, 0);
864
865 #define kgnilnd_schedule_conn_refheld(conn, refheld)                            \
866         _kgnilnd_schedule_conn(conn, __func__, __LINE__, refheld);
867
868 static inline int
869 kgnilnd_thread_start(int(*fn)(void *arg), void *arg, char *name, int id)
870 {
871         struct task_struct *thrd = kthread_run(fn, arg, "%s_%02d", name, id);
872         if (IS_ERR(thrd))
873                 return PTR_ERR(thrd);
874
875         atomic_inc(&kgnilnd_data.kgn_nthreads);
876         return 0;
877 }
878
879 static inline void
880 kgnilnd_thread_fini(void)
881 {
882         atomic_dec(&kgnilnd_data.kgn_nthreads);
883 }
884
885 /* like mutex_trylock but with a jiffies spinner. This is to allow certain
886  * parts of the code to avoid a scheduler trip when the mutex is held
887  *
888  * Try to acquire the mutex atomically for 1 jiffie. Returns 1 if the mutex
889  * has been acquired successfully, and 0 on contention.
890  *
891  * NOTE: this function follows the spin_trylock() convention, so
892  * it is negated to the down_trylock() return values! Be careful
893  * about this when converting semaphore users to mutexes.
894  *
895  * This function must not be used in interrupt context. The
896  * mutex must be released by the same task that acquired it.
897  */
898 static inline int kgnilnd_mutex_trylock(struct mutex *lock)
899 {
900         int             ret;
901         unsigned long   timeout;
902
903         LASSERT(!in_interrupt());
904
905         for (timeout = jiffies + 1; time_before(jiffies, timeout);) {
906
907                 ret = mutex_trylock(lock);
908                 if (ret)
909                         return ret;
910         }
911         return 0;
912 }
913
914 /* Copied from DEBUG_REQ in Lustre - the dance is needed to save stack space */
915
916 extern void
917 _kgnilnd_debug_msg(kgn_msg_t *msg,
918                 struct libcfs_debug_msg_data *data, const char *fmt, ... );
919
920 #define kgnilnd_debug_msg(msgdata, mask, cdls, msg, fmt, a...)                \
921 do {                                                                          \
922         CFS_CHECK_STACK(msgdata, mask, cdls);                                 \
923                                                                               \
924         if (((mask) & D_CANTMASK) != 0 ||                                     \
925             ((libcfs_debug & (mask)) != 0 &&                                  \
926              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))                \
927                 _kgnilnd_debug_msg((msg), msgdata, fmt, ##a);                 \
928 } while(0)
929
930 /* for most callers (level is a constant) this is resolved at compile time */
931 #define GNIDBG_MSG(level, msg, fmt, args...)                                  \
932 do {                                                                          \
933         if ((level) & (D_ERROR | D_WARNING | D_NETERROR)) {                   \
934             static cfs_debug_limit_state_t cdls;                              \
935             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);                \
936             kgnilnd_debug_msg(&msgdata, level, &cdls, msg,                    \
937                               "$$ "fmt" from %s ", ## args,                   \
938                               libcfs_nid2str((msg)->gnm_srcnid));             \
939         } else {                                                              \
940             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);                 \
941             kgnilnd_debug_msg(&msgdata, level, NULL, msg,                     \
942                               "$$ "fmt" from %s ", ## args,                   \
943                               libcfs_nid2str((msg)->gnm_srcnid));             \
944         }                                                                     \
945 } while (0)
946
947 /* user puts 'to nid' in msg for us */
948 #define GNIDBG_TOMSG(level, msg, fmt, args...)                                \
949 do {                                                                          \
950         if ((level) & (D_ERROR | D_WARNING | D_NETERROR)) {                   \
951             static cfs_debug_limit_state_t cdls;                              \
952             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);                \
953             kgnilnd_debug_msg(&msgdata, level, &cdls, msg,                    \
954                               "$$ "fmt" ", ## args);                          \
955         } else {                                                              \
956             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);                 \
957             kgnilnd_debug_msg(&msgdata, level, NULL, msg,                     \
958                               "$$ "fmt" ", ## args);                          \
959         }                                                                     \
960 } while (0)
961
962 extern void
963 _kgnilnd_debug_conn(kgn_conn_t *conn,
964                 struct libcfs_debug_msg_data *data, const char *fmt, ... );
965
966 #define kgnilnd_debug_conn(msgdata, mask, cdls, conn, fmt, a...)               \
967 do {                                                                           \
968         CFS_CHECK_STACK(msgdata, mask, cdls);                                  \
969                                                                                \
970         if (((mask) & D_CANTMASK) != 0 ||                                      \
971             ((libcfs_debug & (mask)) != 0 &&                                   \
972              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))                 \
973                 _kgnilnd_debug_conn((conn), msgdata, fmt, ##a);                \
974 } while(0)
975
976 /* for most callers (level is a constant) this is resolved at compile time */
977 #define GNIDBG_CONN(level, conn, fmt, args...)                                  \
978 do {                                                                            \
979         if ((level) & (D_ERROR | D_WARNING | D_NETERROR)) {                     \
980             static cfs_debug_limit_state_t cdls;                                \
981             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);                  \
982             kgnilnd_debug_conn(&msgdata, level, &cdls, conn,                    \
983                                "$$ "fmt" ", ## args);                           \
984         } else {                                                                \
985             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);                   \
986             kgnilnd_debug_conn(&msgdata, level, NULL, conn,                     \
987                                "$$ "fmt" ", ## args);                           \
988         }                                                                       \
989 } while (0)
990
991 extern void
992 _kgnilnd_debug_tx(kgn_tx_t *tx,
993                 struct libcfs_debug_msg_data *data, const char *fmt, ... );
994
995 #define kgnilnd_debug_tx(msgdata, mask, cdls, tx, fmt, a...)                   \
996 do {                                                                           \
997         CFS_CHECK_STACK(msgdata, mask, cdls);                                  \
998                                                                                \
999         if (((mask) & D_CANTMASK) != 0 ||                                      \
1000             ((libcfs_debug & (mask)) != 0 &&                                   \
1001              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))                 \
1002                 _kgnilnd_debug_tx((tx), msgdata, fmt, ##a);                    \
1003 } while(0)
1004
1005 /* for most callers (level is a constant) this is resolved at compile time */
1006 #define GNIDBG_TX(level, tx, fmt, args...)                                      \
1007 do {                                                                            \
1008         if ((level) & (D_ERROR | D_WARNING | D_NETERROR)) {                     \
1009             static cfs_debug_limit_state_t cdls;                                \
1010             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, &cdls);                  \
1011             kgnilnd_debug_tx(&msgdata, level, &cdls, tx,                        \
1012                               "$$ "fmt" ", ## args);                            \
1013         } else {                                                                \
1014             LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, level, NULL);                   \
1015             kgnilnd_debug_tx(&msgdata, level, NULL, tx,                         \
1016                               "$$ "fmt" ", ## args);                            \
1017         }                                                                       \
1018 } while (0)
1019
1020 #define GNITX_ASSERTF(tx, cond, fmt, a...)                                      \
1021 ({                                                                              \
1022         if (unlikely(!(cond))) {                                                \
1023                 GNIDBG_TX(D_EMERG, tx, "ASSERTION(" #cond ") failed:" fmt, a);  \
1024                 LBUG();                                                         \
1025         }                                                                       \
1026 })
1027
1028 #define GNILND_IS_QUIESCED                                                      \
1029         (atomic_read(&kgnilnd_data.kgn_nquiesce) ==                             \
1030                 atomic_read(&kgnilnd_data.kgn_nthreads))
1031
1032 #define KGNILND_SPIN_QUIESCE                                                 \
1033 do {                                                                         \
1034         /* E.T phone home */                                                 \
1035         atomic_inc(&kgnilnd_data.kgn_nquiesce);                              \
1036         CDEBUG(D_NET, "Waiting for thread pause to be over...\n");           \
1037         while (kgnilnd_data.kgn_quiesce_trigger) {                           \
1038                 set_current_state(TASK_INTERRUPTIBLE);                       \
1039                 cfs_schedule_timeout_and_set_state(TASK_INTERRUPTIBLE,       \
1040                         cfs_time_seconds(1));                                \
1041         }                                                                    \
1042         /* Mom, my homework is done */                                       \
1043         CDEBUG(D_NET, "Waking up from thread pause\n");                      \
1044         atomic_dec(&kgnilnd_data.kgn_nquiesce);                              \
1045 } while(0)
1046
1047 /* use macros for addref/decref to get the calling function name in the CDEBUG */
1048 #ifndef LIBCFS_DEBUG
1049 #error "this code uses actions inside LASSERT for ref counting"
1050 #endif
1051
1052 #define kgnilnd_admin_addref(atomic)                                     \
1053 do {                                                                            \
1054         int     val = atomic_inc_return(&atomic);                               \
1055         LASSERTF(val > 0,  #atomic " refcount %d\n", val);                       \
1056         CDEBUG(D_NETTRACE, #atomic " refcount %d\n", val);                       \
1057 } while (0)
1058
1059 #define kgnilnd_admin_decref(atomic)                                     \
1060 do {                                                                            \
1061         int     val = atomic_dec_return(&atomic);                               \
1062         LASSERTF(val >=0,  #atomic " refcount %d\n", val);                        \
1063         CDEBUG(D_NETTRACE, #atomic " refcount %d\n", val);                       \
1064 }while (0)
1065
1066 #define kgnilnd_net_addref(net)                                                 \
1067 do {                                                                            \
1068         int     val = atomic_inc_return(&net->gnn_refcount);                    \
1069         LASSERTF(val > 1, "net %p refcount %d\n", net, val);                    \
1070         CDEBUG(D_NETTRACE, "net %p->%s++ (%d)\n", net,                          \
1071                 libcfs_nid2str(net->gnn_ni->ni_nid), val);                      \
1072 } while (0)
1073
1074 #define kgnilnd_net_decref(net)                                                 \
1075 do {                                                                            \
1076         int     val = atomic_dec_return(&net->gnn_refcount);                    \
1077         LASSERTF(val >= 0, "net %p refcount %d\n", net, val);                   \
1078         CDEBUG(D_NETTRACE, "net %p->%s-- (%d)\n", net,                          \
1079                libcfs_nid2str(net->gnn_ni->ni_nid), val);                       \
1080 } while (0)
1081
1082 #define kgnilnd_peer_addref(peer)                                               \
1083 do {                                                                            \
1084         int     val = atomic_inc_return(&peer->gnp_refcount);                   \
1085         LASSERTF(val > 1, "peer %p refcount %d\n", peer, val);                  \
1086         CDEBUG(D_NETTRACE, "peer %p->%s++ (%d)\n", peer,                        \
1087                libcfs_nid2str(peer->gnp_nid), val);                             \
1088 } while (0)
1089
1090 #define kgnilnd_peer_decref(peer)                                               \
1091 do {                                                                            \
1092         int     val = atomic_dec_return(&peer->gnp_refcount);                   \
1093         LASSERTF(val >= 0, "peer %p refcount %d\n", peer, val);                 \
1094         CDEBUG(D_NETTRACE, "peer %p->%s--(%d)\n", peer,                         \
1095                libcfs_nid2str(peer->gnp_nid), val);                             \
1096         if (val == 0)                                                           \
1097                 kgnilnd_destroy_peer(peer);                                     \
1098 } while(0)
1099
1100 #define kgnilnd_conn_addref(conn)                                       \
1101 do {                                                                    \
1102         int     val;                                                    \
1103                                                                         \
1104         smp_wmb();                                                      \
1105         val = atomic_inc_return(&conn->gnc_refcount);                   \
1106         LASSERTF(val > 1 && conn->gnc_magic == GNILND_CONN_MAGIC,       \
1107                 "conn %p refc %d to %s\n",                              \
1108                 conn, val,                                              \
1109                 conn->gnc_peer                                          \
1110                         ? libcfs_nid2str(conn->gnc_peer->gnp_nid)       \
1111                         : "<?>");                                       \
1112         CDEBUG(D_NETTRACE, "conn %p->%s++ (%d)\n", conn,                \
1113                 conn->gnc_peer                                          \
1114                         ? libcfs_nid2str(conn->gnc_peer->gnp_nid)       \
1115                         : "<?>",                                        \
1116                 val);                                                   \
1117 } while (0)
1118
1119 /* we hijack conn_decref && gnc_refcount = 1 to allow us to push the conn
1120  * through the scheduler thread to get the EP destroyed. This avoids some
1121  * messy semaphore business and allows us to reuse the connd_list and existing
1122  * linkage and avoid creating extra lists just for destroying EPs */
1123
1124 /* Safety Disclaimer:
1125  * Q: If we decrement the refcount and then check it again, is it possible that
1126  *    another caller could have passed through this macro concurrently? If so,
1127  *    then it is possible that both will attempt to call kgnilnd_destroy_conn().
1128  *
1129  * A: Yes, entirely possible in most cases, but we can't get concurrent users
1130  * once we are refcount <= 2. It hinges around gnc_state and membership of
1131  * gnc_hashlist. There are two ways to find a connection - either ask for
1132  * it from the peer, kgnilnd_find_conn_locked(peer) or from the CQ id,
1133  * kgnilnd_cqid2conn_locked(id). While a conn is live, we'll have at least
1134  * 4 refcounts
1135  *
1136  * - #1 from create (kgnilnd_create_conn)
1137  * - #2 for EP (kgnilnd_create_conn)
1138  * - #3 - living on peer (gnc_list, kgnilnd_finish_connect)
1139  * - #4 living in global hash (gnc_hashlist, kgnilnd_finish_connect).
1140  *
1141  * Actually, only 3 live, as at the end of kgnilnd_finish_connect, we drop:
1142  * - #1 - the ref the dgram inherited from kgnilnd_create_conn.
1143  *
1144  * There could be more from TX descriptors during the lifetime of a live
1145  * conn.
1146  *
1147  * If we nuke the conn before finish_connect, we won't have parallel paths
1148  * because nobody besides the dgram handler for the single outstanding
1149  * dgram can find the connection as it isn't in any searchable tables yet.
1150  *
1151  * This leaves connection close, we'll drop 2 refs (#4 and #3) but only
1152  * after calling kgnilnd_schedule_conn, which would add a new ref (#5). At
1153  * this point gnc_refcount=2 (#2, #5). We have a 'maybe' send of the CLOSE
1154  * now on the next scheduler loop, this could be #6 (schedule_conn again)
1155  * and #7 (TX on gnc_fmaq). Both would be cleared quickly as that TX is
1156  * sent. Now the gnc_state == CLOSED, so we hit
1157  * kgnilnd_complete_closed_conn. At this point, nobody can 'find' this conn
1158  * - we've nuked them from the peer and CQ id tables, so we own them and
1159  * are guaranteed serial access - hence the complete lack of conn list
1160  * locking in kgnilnd_complete_closed_conn. We are free then to mark the
1161  * conn DESTROY_EP (add #6 for schedule_conn), then lose #5 in
1162  * kgnilnd_process_conns. Then the next scheduler loop would call
1163  * kgnilnd_destroy_conn_ep (drop #2 for EP) and lose #6 (refcount=0) in
1164  * kgnilnd_process_conns.
1165  *
1166  * Clearly, we are totally safe. Clearly.
1167  */
1168
1169 #define kgnilnd_conn_decref(conn)                                       \
1170 do {                                                                    \
1171         int     val;                                                    \
1172                                                                         \
1173         smp_wmb();                                                      \
1174         val = atomic_dec_return(&conn->gnc_refcount);                   \
1175         LASSERTF(val >= 0, "conn %p refc %d to %s\n",                   \
1176                 conn, val,                                              \
1177                 conn->gnc_peer                                          \
1178                         ? libcfs_nid2str(conn->gnc_peer->gnp_nid)       \
1179                         : "<?>");                                       \
1180         CDEBUG(D_NETTRACE, "conn %p->%s-- (%d)\n", conn,                \
1181                 conn->gnc_peer                                          \
1182                         ? libcfs_nid2str(conn->gnc_peer->gnp_nid)       \
1183                         : "<?>",                                        \
1184                 val);                                                   \
1185         smp_rmb();                                                      \
1186         if ((val == 1) &&                                               \
1187             (conn->gnc_ephandle != NULL) &&                             \
1188             (conn->gnc_state != GNILND_CONN_DESTROY_EP)) {              \
1189                 set_mb(conn->gnc_state, GNILND_CONN_DESTROY_EP);        \
1190                 kgnilnd_schedule_conn(conn);                            \
1191         } else if (val == 0) {                                          \
1192                 kgnilnd_destroy_conn(conn);                             \
1193         }                                                               \
1194 } while (0)
1195
1196 static inline struct list_head *
1197 kgnilnd_nid2peerlist(lnet_nid_t nid)
1198 {
1199         unsigned int hash = ((unsigned int)LNET_NIDADDR(nid)) % *kgnilnd_tunables.kgn_peer_hash_size;
1200
1201         RETURN(&kgnilnd_data.kgn_peers[hash]);
1202 }
1203
1204 static inline struct list_head *
1205 kgnilnd_netnum2netlist(__u16 netnum)
1206 {
1207         unsigned int hash = ((unsigned int) netnum) % *kgnilnd_tunables.kgn_net_hash_size;
1208
1209         RETURN(&kgnilnd_data.kgn_nets[hash]);
1210 }
1211
1212 static inline int
1213 kgnilnd_peer_active(kgn_peer_t *peer)
1214 {
1215         /* Am I in the peer hash table? */
1216         return (!list_empty(&peer->gnp_list));
1217 }
1218
1219 /* need write_lock on kgn_peer_conn_lock */
1220 static inline int
1221 kgnilnd_can_unlink_peer_locked(kgn_peer_t *peer)
1222 {
1223         CDEBUG(D_NET, "peer 0x%p->%s conns? %d tx? %d\n",
1224                 peer, libcfs_nid2str(peer->gnp_nid),
1225                 !list_empty(&peer->gnp_conns),
1226                 !list_empty(&peer->gnp_tx_queue));
1227
1228         /* kgn_peer_conn_lock protects us from conflict with
1229          * kgnilnd_peer_notify and gnp_persistent */
1230         RETURN ((list_empty(&peer->gnp_conns)) &&
1231                 (list_empty(&peer->gnp_tx_queue)));
1232 }
1233
1234 /* returns positive if error was for a clean shutdown of conn */
1235 static inline int
1236 kgnilnd_conn_clean_errno(int errno)
1237 {
1238         /*  - ESHUTDOWN - LND is unloading
1239          *  - EUCLEAN - admin requested via "lctl del_peer"
1240          *  - ENETRESET - admin requested via "lctl disconnect" or rca event
1241          *  - ENOTRECOVERABLE - stack reset
1242          *  - EISCONN - cleared via "lctl push"
1243          *  not doing ESTALE - that isn't clean */
1244         RETURN ((errno == 0) ||
1245                 (errno == -ESHUTDOWN) ||
1246                 (errno == -EUCLEAN) ||
1247                 (errno == -ENETRESET) ||
1248                 (errno == -EISCONN) ||
1249                 (errno == -ENOTRECOVERABLE));
1250 }
1251
1252 /* returns positive if error results in purgatory hold */
1253 static inline int
1254 kgnilnd_check_purgatory_errno(int errno)
1255 {
1256         /* We don't want to save the purgatory lists these cases:
1257          *  - EUCLEAN - admin requested via "lctl del_peer"
1258          *  - ESHUTDOWN - LND is unloading
1259          */
1260         RETURN ((errno != -ESHUTDOWN) &&
1261                 (errno != -EUCLEAN));
1262
1263 }
1264
1265 /* returns positive if a purgatory hold is needed */
1266 static inline int
1267 kgnilnd_check_purgatory_conn(kgn_conn_t *conn)
1268 {
1269         int loopback = 0;
1270
1271         if (conn->gnc_peer) {
1272                 loopback = conn->gnc_peer->gnp_nid ==
1273                        conn->gnc_peer->gnp_net->gnn_ni->ni_nid;
1274         } else {
1275                 /* short circuit - a conn that didn't complete
1276                  * setup never needs a purgatory hold */
1277                 RETURN(0);
1278         }
1279         CDEBUG(D_NETTRACE, "conn 0x%p->%s loopback %d close_recvd %d\n",
1280                 conn, conn->gnc_peer ?
1281                                 libcfs_nid2str(conn->gnc_peer->gnp_nid) :
1282                                 "<?>",
1283                 loopback, conn->gnc_close_recvd);
1284
1285         /* we only use a purgatory hold if we've not received the CLOSE msg
1286          * from our peer - without that message, we can't know the state of
1287          * the other end of this connection and must put it into purgatory
1288          * to prevent reuse and corruption.
1289          * The theory is that a TX error can be communicated in all other cases
1290          */
1291         RETURN(likely(!loopback) && !conn->gnc_close_recvd &&
1292                 kgnilnd_check_purgatory_errno(conn->gnc_error));
1293 }
1294
1295 static inline const char *
1296 kgnilnd_tx_state2str(kgn_tx_list_state_t state);
1297
1298 static inline struct list_head *
1299 kgnilnd_tx_state2list(kgn_peer_t *peer, kgn_conn_t *conn,
1300                         kgn_tx_list_state_t to_state)
1301 {
1302         switch (to_state) {
1303         case GNILND_TX_PEERQ:
1304                 return &peer->gnp_tx_queue;
1305         case GNILND_TX_FMAQ:
1306                 return &conn->gnc_fmaq;
1307         case GNILND_TX_LIVE_FMAQ:
1308         case GNILND_TX_LIVE_RDMAQ:
1309         case GNILND_TX_DYING:
1310                 return NULL;
1311         case GNILND_TX_MAPQ:
1312                 return &conn->gnc_device->gnd_map_tx;
1313         case GNILND_TX_RDMAQ:
1314                 return &conn->gnc_device->gnd_rdmaq;
1315         default:
1316                 /* IDLE, FREED or ALLOCD is not valid "on list" state */
1317                 CERROR("invalid state requested: %s\n",
1318                         kgnilnd_tx_state2str(to_state));
1319                 LBUG();
1320                 break;
1321         }
1322 }
1323
1324 /* should hold tx, conn or peer lock when calling */
1325 static inline void
1326 kgnilnd_tx_add_state_locked(kgn_tx_t *tx, kgn_peer_t *peer,
1327                         kgn_conn_t *conn, kgn_tx_list_state_t state,
1328                         int add_tail)
1329 {
1330         struct list_head        *list = NULL;
1331
1332         /* make sure we have a sane TX state to start */
1333         GNITX_ASSERTF(tx, (tx->tx_list_p == NULL &&
1334                   tx->tx_list_state == GNILND_TX_ALLOCD) &&
1335                 list_empty(&tx->tx_list),
1336                 "bad state with tx_list %s",
1337                 list_empty(&tx->tx_list) ? "empty" : "not empty");
1338
1339         /* WTF - you are already on that state buttmunch */
1340         GNITX_ASSERTF(tx, state != tx->tx_list_state,
1341                       "already at %s", kgnilnd_tx_state2str(state));
1342
1343         /* get proper list from the state requested */
1344         list = kgnilnd_tx_state2list(peer, conn, state);
1345
1346         /* add refcount */
1347         switch (state) {
1348         case GNILND_TX_PEERQ:
1349                 kgnilnd_peer_addref(peer);
1350                 break;
1351         case GNILND_TX_ALLOCD:
1352                 /* no refs needed */
1353                 break;
1354         case GNILND_TX_FMAQ:
1355                 kgnilnd_conn_addref(conn);
1356                 break;
1357         case GNILND_TX_MAPQ:
1358                 atomic_inc(&conn->gnc_device->gnd_nq_map);
1359                 kgnilnd_conn_addref(conn);
1360                 break;
1361         case GNILND_TX_LIVE_FMAQ:
1362                 atomic_inc(&conn->gnc_nlive_fma);
1363                 kgnilnd_conn_addref(conn);
1364                 break;
1365         case GNILND_TX_LIVE_RDMAQ:
1366                 atomic_inc(&conn->gnc_nlive_rdma);
1367                 kgnilnd_conn_addref(conn);
1368                 break;
1369         case GNILND_TX_RDMAQ:
1370                 atomic_inc(&conn->gnc_nq_rdma);
1371                 kgnilnd_conn_addref(conn);
1372                 break;
1373         case GNILND_TX_DYING:
1374                 kgnilnd_conn_addref(conn);
1375                 break;
1376         default:
1377                 CERROR("invalid state requested: %s\n",
1378                         kgnilnd_tx_state2str(state));
1379                 LBUG();
1380                 break;;
1381         }
1382
1383         /* if this changes, change kgnilnd_alloc_tx */
1384         tx->tx_list_state = state;
1385
1386         /* some states don't have lists - we track them in the per conn
1387          * TX table instead. Waste not, want not! */
1388         if (list != NULL) {
1389                 tx->tx_list_p = list;
1390                 if (add_tail)
1391                         list_add_tail(&tx->tx_list, list);
1392                 else
1393                         list_add(&tx->tx_list, list);
1394         } else {
1395                 /* set dummy list_p to make book keeping happy and let debugging
1396                  * be a hair easier */
1397                 tx->tx_list_p = (void *)state;
1398         }
1399
1400         GNIDBG_TX(D_NET, tx, "onto %s->0x%p",
1401                   kgnilnd_tx_state2str(state), list);
1402 }
1403
1404 static inline void
1405 kgnilnd_tx_del_state_locked(kgn_tx_t *tx, kgn_peer_t *peer,
1406                         kgn_conn_t *conn, kgn_tx_list_state_t new_state)
1407 {
1408         /* These is only 1 "off-list" state */
1409         GNITX_ASSERTF(tx, new_state == GNILND_TX_ALLOCD,
1410                       "invalid new_state %s", kgnilnd_tx_state2str(new_state));
1411
1412         /* new_state == ALLOCD means we are deallocating this tx,
1413          * so make sure it was on a valid list to start with */
1414         GNITX_ASSERTF(tx, (tx->tx_list_p != NULL) &&
1415                       (((tx->tx_list_state == GNILND_TX_LIVE_FMAQ) ||
1416                         (tx->tx_list_state == GNILND_TX_LIVE_RDMAQ) ||
1417                         (tx->tx_list_state == GNILND_TX_DYING)) == list_empty(&tx->tx_list)),
1418                       "bad state", NULL);
1419
1420         GNIDBG_TX(D_NET, tx, "off %p", tx->tx_list_p);
1421
1422         /* drop refcount */
1423         switch (tx->tx_list_state) {
1424         case GNILND_TX_PEERQ:
1425                 kgnilnd_peer_decref(peer);
1426                 break;
1427         case GNILND_TX_FREED:
1428         case GNILND_TX_IDLE:
1429         case GNILND_TX_ALLOCD:
1430                 /* no refs needed */
1431                 break;
1432         case GNILND_TX_DYING:
1433                 kgnilnd_conn_decref(conn);
1434                 break;
1435         case GNILND_TX_FMAQ:
1436                 kgnilnd_conn_decref(conn);
1437                 break;
1438         case GNILND_TX_MAPQ:
1439                 atomic_dec(&conn->gnc_device->gnd_nq_map);
1440                 kgnilnd_conn_decref(conn);
1441                 break;
1442         case GNILND_TX_LIVE_FMAQ:
1443                 atomic_dec(&conn->gnc_nlive_fma);
1444                 kgnilnd_conn_decref(conn);
1445                 break;
1446         case GNILND_TX_LIVE_RDMAQ:
1447                 atomic_dec(&conn->gnc_nlive_rdma);
1448                 kgnilnd_conn_decref(conn);
1449                 break;
1450         case GNILND_TX_RDMAQ:
1451                 atomic_dec(&conn->gnc_nq_rdma);
1452                 kgnilnd_conn_decref(conn);
1453         /* don't need to assert on default, already did in set */
1454         }
1455
1456         /* for ALLOCD, this might already be true, but no harm doing it again */
1457         list_del_init(&tx->tx_list);
1458         tx->tx_list_p = NULL;
1459         tx->tx_list_state = new_state;
1460 }
1461
1462 static inline int
1463 kgnilnd_tx_mapped(kgn_tx_t *tx)
1464 {
1465         return (tx->tx_buftype == GNILND_BUF_VIRT_MAPPED ||
1466                 tx->tx_buftype == GNILND_BUF_PHYS_MAPPED);
1467 }
1468
1469 static inline struct list_head *
1470 kgnilnd_cqid2connlist(__u32 cqid)
1471 {
1472         unsigned int hash = cqid % *kgnilnd_tunables.kgn_peer_hash_size;
1473
1474         return (&kgnilnd_data.kgn_conns [hash]);
1475 }
1476
1477 static inline kgn_conn_t *
1478 kgnilnd_cqid2conn_locked(__u32 cqid)
1479 {
1480         struct list_head *conns = kgnilnd_cqid2connlist(cqid);
1481         struct list_head *tmp;
1482         kgn_conn_t       *conn;
1483
1484         list_for_each(tmp, conns) {
1485                 conn = list_entry(tmp, kgn_conn_t, gnc_hashlist);
1486
1487                 if (conn->gnc_cqid == cqid)
1488                         return conn;
1489         }
1490
1491         return NULL;
1492 }
1493
1494 /* returns 1..GNILND_MAX_CQID on success, 0 on failure */
1495 static inline __u32
1496 kgnilnd_get_cqid_locked(void)
1497 {
1498         int     looped = 0;
1499         __u32   cqid;
1500
1501         do {
1502                 cqid = kgnilnd_data.kgn_next_cqid++;
1503                 if (kgnilnd_data.kgn_next_cqid >= GNILND_MAX_CQID) {
1504                         if (looped) {
1505                                 return 0;
1506                         }
1507                         kgnilnd_data.kgn_next_cqid = 1;
1508                         looped = 1;
1509                 }
1510         } while (kgnilnd_cqid2conn_locked(cqid) != NULL);
1511
1512         return cqid;
1513 }
1514
1515 static inline void
1516 kgnilnd_validate_tx_ev_id(kgn_tx_ev_id_t *ev_id, kgn_tx_t **txp, kgn_conn_t **connp)
1517 {
1518         kgn_tx_t        *tx = NULL;
1519         kgn_conn_t      *conn = NULL;
1520
1521         /* set to NULL so any early return is an error */
1522         *txp = NULL;
1523         *connp = NULL;
1524
1525         LASSERTF((ev_id->txe_idx > 0) &&
1526                  (ev_id->txe_idx < GNILND_MAX_MSG_ID),
1527                 "bogus txe_idx %d >= %d\n",
1528                 ev_id->txe_idx, GNILND_MAX_MSG_ID);
1529
1530         LASSERTF((ev_id->txe_cqid > 0) &&
1531                  (ev_id->txe_cqid < GNILND_MAX_CQID),
1532                 "bogus txe_cqid %d >= %d\n",
1533                 ev_id->txe_cqid, GNILND_MAX_CQID);
1534
1535         read_lock(&kgnilnd_data.kgn_peer_conn_lock);
1536         conn = kgnilnd_cqid2conn_locked(ev_id->txe_cqid);
1537
1538         if (conn == NULL) {
1539                 /* Conn was destroyed? */
1540                 read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1541                 CDEBUG(D_NET, "CQID %d lookup failed\n", ev_id->txe_cqid);
1542                 return;
1543         }
1544         /* just insurance */
1545         kgnilnd_conn_addref(conn);
1546         kgnilnd_admin_addref(conn->gnc_tx_in_use);
1547         read_unlock(&kgnilnd_data.kgn_peer_conn_lock);
1548
1549         /* we know this is safe - as the TX won't be reused until AFTER
1550          * the conn is unlinked from the cqid hash, so we can use the TX
1551          * (serializing to avoid any cache oddness) freely from the conn tx ref table */
1552
1553         spin_lock(&conn->gnc_tx_lock);
1554         tx = conn->gnc_tx_ref_table[ev_id->txe_idx];
1555         spin_unlock(&conn->gnc_tx_lock);
1556
1557         /* We could have a tx that was cleared out by other forces
1558          * lctl disconnect or del_peer. */
1559         if (tx == NULL) {
1560                 CNETERR("txe_idx %d is gone, ignoring event\n", ev_id->txe_idx);
1561                 kgnilnd_admin_decref(conn->gnc_tx_in_use);
1562                 kgnilnd_conn_decref(conn);
1563                 return;
1564         }
1565
1566         /* check tx->tx_msg magic to make sure kgni didn't eat it */
1567         GNITX_ASSERTF(tx, tx->tx_msg.gnm_magic == GNILND_MSG_MAGIC,
1568                       "came back from kgni with bad magic %x", tx->tx_msg.gnm_magic);
1569
1570         GNITX_ASSERTF(tx, tx->tx_id.txe_idx == ev_id->txe_idx,
1571                       "conn 0x%p->%s tx_ref_table hosed: wanted txe_idx %d "
1572                       "found tx %p txe_idx %d",
1573                       conn, libcfs_nid2str(conn->gnc_peer->gnp_nid),
1574                       ev_id->txe_idx, tx, tx->tx_id.txe_idx);
1575
1576         GNITX_ASSERTF(tx, tx->tx_conn != NULL, "tx with NULL connection", NULL);
1577
1578         GNITX_ASSERTF(tx, tx->tx_conn == conn, "tx conn does not equal conn", NULL);
1579
1580         *txp = tx;
1581         *connp = conn;
1582
1583         GNIDBG_TX(D_NET, tx, "validated to 0x%p", conn);
1584 }
1585
1586 /* set_normalized_timepsec isn't exported from the kernel, so
1587  * we need to do the same thing inline */
1588 static inline struct timespec
1589 kgnilnd_ts_sub(struct timespec lhs, struct timespec rhs)
1590 {
1591         time_t                  sec;
1592         long                    nsec;
1593         struct timespec         ts;
1594
1595         sec = lhs.tv_sec - rhs.tv_sec;
1596         nsec = lhs.tv_nsec - rhs.tv_nsec;
1597
1598         while (nsec >= NSEC_PER_SEC) {
1599                 nsec -= NSEC_PER_SEC;
1600                 ++sec;
1601         }
1602         while (nsec < 0) {
1603                 nsec += NSEC_PER_SEC;
1604                 --sec;
1605         }
1606         ts.tv_sec = sec;
1607         ts.tv_nsec = nsec;
1608         return ts;
1609 }
1610
1611 static inline int
1612 kgnilnd_count_list(struct list_head *q)
1613 {
1614         struct list_head *e;
1615         int               n = 0;
1616
1617         list_for_each(e, q) {
1618                 n++;
1619         }
1620
1621         return n;
1622 }
1623
1624 /* kgnilnd_find_net adds a reference to the net it finds
1625  * this is so the net will not be removed before the calling function
1626  * has time to use the data returned. This reference needs to be released
1627  * by the calling function once it has finished using the returned net
1628  */
1629
1630 static inline int
1631 kgnilnd_find_net(lnet_nid_t nid, kgn_net_t **netp)
1632 {
1633         kgn_net_t *net;
1634         int rc;
1635
1636         rc = down_read_trylock(&kgnilnd_data.kgn_net_rw_sem);
1637
1638         if (!rc) {
1639                 return -ESHUTDOWN;
1640         }
1641
1642         list_for_each_entry(net, kgnilnd_netnum2netlist(LNET_NETNUM(LNET_NIDNET(nid))), gnn_list) {
1643                 if (!net->gnn_shutdown && LNET_NIDNET(net->gnn_ni->ni_nid) == LNET_NIDNET(nid)) {
1644                         kgnilnd_net_addref(net);
1645                         up_read(&kgnilnd_data.kgn_net_rw_sem);
1646                         *netp = net;
1647                         return 0;
1648                 }
1649         }
1650
1651         up_read(&kgnilnd_data.kgn_net_rw_sem);
1652
1653         return -ENONET;
1654 }
1655
1656 #ifdef CONFIG_DEBUG_SLAB
1657 #define KGNILND_POISON(ptr, c, s) do {} while(0)
1658 #else
1659 #define KGNILND_POISON(ptr, c, s) memset(ptr, c, s)
1660 #endif
1661
1662 int kgnilnd_dev_init(kgn_device_t *dev);
1663 void kgnilnd_dev_fini(kgn_device_t *dev);
1664 int kgnilnd_startup(lnet_ni_t *ni);
1665 void kgnilnd_shutdown(lnet_ni_t *ni);
1666 int kgnilnd_base_startup(void);
1667 void kgnilnd_base_shutdown(void);
1668
1669 int kgnilnd_allocate_phys_fmablk(kgn_device_t *device);
1670 int kgnilnd_map_phys_fmablk(kgn_device_t *device);
1671 void kgnilnd_unmap_phys_fmablk(kgn_device_t *device);
1672 void kgnilnd_free_phys_fmablk(kgn_device_t *device);
1673
1674 int kgnilnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
1675 void kgnilnd_query(lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when);
1676 int kgnilnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
1677 int kgnilnd_eager_recv(lnet_ni_t *ni, void *private,
1678                         lnet_msg_t *lntmsg, void **new_private);
1679 int kgnilnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
1680                 int delayed, unsigned int niov,
1681                 struct iovec *iov, lnet_kiov_t *kiov,
1682                 unsigned int offset, unsigned int mlen, unsigned int rlen);
1683
1684 __u16 kgnilnd_cksum_kiov(unsigned int nkiov, lnet_kiov_t *kiov, unsigned int offset, unsigned int nob, int dump_blob);
1685
1686 /* purgatory functions */
1687 void kgnilnd_add_purgatory_locked(kgn_conn_t *conn, kgn_peer_t *peer);
1688 void kgnilnd_mark_for_detach_purgatory_all_locked(kgn_peer_t *peer);
1689 void kgnilnd_detach_purgatory_locked(kgn_conn_t *conn, struct list_head *conn_list);
1690 void kgnilnd_release_purgatory_list(struct list_head *conn_list);
1691
1692 void kgnilnd_update_reaper_timeout(long timeout);
1693 void kgnilnd_unmap_buffer(kgn_tx_t *tx, int error);
1694 kgn_tx_t *kgnilnd_new_tx_msg(int type, lnet_nid_t source);
1695 void kgnilnd_tx_done(kgn_tx_t *tx, int completion);
1696 void kgnilnd_txlist_done(struct list_head *txlist, int error);
1697 void kgnilnd_unlink_peer_locked(kgn_peer_t *peer);
1698 int _kgnilnd_schedule_conn(kgn_conn_t *conn, const char *caller, int line, int refheld);
1699 int kgnilnd_schedule_process_conn(kgn_conn_t *conn, int sched_intent);
1700
1701 void kgnilnd_schedule_dgram(kgn_device_t *dev);
1702 int kgnilnd_create_peer_safe(kgn_peer_t **peerp, lnet_nid_t nid, kgn_net_t *net);
1703 void kgnilnd_add_peer_locked(lnet_nid_t nid, kgn_peer_t *new_stub_peer, kgn_peer_t **peerp);
1704 int kgnilnd_add_peer(kgn_net_t *net, lnet_nid_t nid, kgn_peer_t **peerp);
1705
1706 kgn_peer_t *kgnilnd_find_peer_locked(lnet_nid_t nid);
1707 int kgnilnd_del_conn_or_peer(kgn_net_t *net, lnet_nid_t nid, int command, int error);
1708 void kgnilnd_peer_increase_reconnect_locked(kgn_peer_t *peer);
1709 void kgnilnd_queue_reply(kgn_conn_t *conn, kgn_tx_t *tx);
1710 void kgnilnd_queue_tx(kgn_conn_t *conn, kgn_tx_t *tx);
1711 void kgnilnd_launch_tx(kgn_tx_t *tx, kgn_net_t *net, lnet_process_id_t *target);
1712 int kgnilnd_send_mapped_tx(kgn_tx_t *tx, int try_map_if_full);
1713 void kgnilnd_consume_rx(kgn_rx_t *rx);
1714
1715 void kgnilnd_schedule_device(kgn_device_t *dev);
1716 void kgnilnd_device_callback(__u32 devid, __u64 arg);
1717 void kgnilnd_schedule_device_timer(unsigned long arg);
1718
1719 int kgnilnd_reaper(void *arg);
1720 int kgnilnd_scheduler(void *arg);
1721 int kgnilnd_dgram_mover(void *arg);
1722 int kgnilnd_rca(void *arg);
1723
1724 int kgnilnd_create_conn(kgn_conn_t **connp, kgn_device_t *dev);
1725 int kgnilnd_conn_isdup_locked(kgn_peer_t *peer, kgn_conn_t *newconn);
1726 kgn_conn_t *kgnilnd_find_conn_locked(kgn_peer_t *peer);
1727 int kgnilnd_get_conn(kgn_conn_t **connp, kgn_peer_t);
1728 kgn_conn_t *kgnilnd_find_or_create_conn_locked(kgn_peer_t *peer);
1729 void kgnilnd_peer_cancel_tx_queue(kgn_peer_t *peer);
1730 void kgnilnd_cancel_peer_connect_locked(kgn_peer_t *peer, struct list_head *zombies);
1731 int kgnilnd_close_stale_conns_locked(kgn_peer_t *peer, kgn_conn_t *newconn);
1732 void kgnilnd_peer_alive(kgn_peer_t *peer);
1733 void kgnilnd_peer_notify(kgn_peer_t *peer, int error);
1734 void kgnilnd_close_conn_locked(kgn_conn_t *conn, int error);
1735 void kgnilnd_close_conn(kgn_conn_t *conn, int error);
1736 void kgnilnd_complete_closed_conn(kgn_conn_t *conn);
1737 void kgnilnd_destroy_conn_ep(kgn_conn_t *conn);
1738
1739 int kgnilnd_close_peer_conns_locked(kgn_peer_t *peer, int why);
1740 int kgnilnd_report_node_state(lnet_nid_t nid, int down);
1741 void kgnilnd_wakeup_rca_thread(void);
1742 int kgnilnd_start_rca_thread(void);
1743
1744 int kgnilnd_tunables_init(void);
1745 void kgnilnd_tunables_fini(void);
1746 void kgnilnd_init_msg(kgn_msg_t *msg, int type, lnet_nid_t source);
1747
1748 void kgnilnd_bump_timeouts(__u32 nap_time, char *reason);
1749 void kgnilnd_pause_threads(void);
1750 int kgnilnd_hw_in_quiesce(void);
1751 int kgnilnd_check_hw_quiesce(void);
1752 void kgnilnd_quiesce_wait(char *reason);
1753 void kgnilnd_quiesce_end_callback(gni_nic_handle_t nic_handle, uint64_t msecs);
1754 int kgnilnd_ruhroh_thread(void *arg);
1755 void kgnilnd_reset_stack(void);
1756 void kgnilnd_critical_error(gni_err_handle_t err_handle);
1757
1758 void kgnilnd_insert_sysctl(void);
1759 void kgnilnd_remove_sysctl(void);
1760 void kgnilnd_proc_init(void);
1761 void kgnilnd_proc_fini(void);
1762
1763 /* gnilnd_conn.c */
1764 void kgnilnd_release_mbox(kgn_conn_t *conn, int purgatory_hold);
1765
1766 int kgnilnd_find_and_cancel_dgram(kgn_device_t *dev, lnet_nid_t dst_nid);
1767 void kgnilnd_cancel_dgram_locked(kgn_dgram_t *dgram);
1768 void kgnilnd_release_dgram(kgn_device_t *dev, kgn_dgram_t *dgram);
1769
1770 int kgnilnd_setup_wildcard_dgram(kgn_device_t *dev);
1771 int kgnilnd_cancel_net_dgrams(kgn_net_t *net);
1772 int kgnilnd_cancel_wc_dgrams(kgn_device_t *dev);
1773 void kgnilnd_wait_for_canceled_dgrams(kgn_device_t *dev);
1774
1775 int kgnilnd_dgram_waitq(void *arg);
1776
1777 int kgnilnd_set_conn_params(kgn_dgram_t *dgram);
1778
1779 /* struct2str functions - we don't use a default: case to cause the compile
1780  * to fail if there is a missing case. This allows us to hide these down here
1781  * out of the way but ensure we'll catch any updates to the enum/types
1782  * above */
1783
1784 #define DO_TYPE(x) case x: return #x;
1785 static inline const char *
1786 kgnilnd_fmablk_state2str(kgn_fmablk_state_t state)
1787 {
1788         /* Only want single char string for this */
1789         switch (state) {
1790         case GNILND_FMABLK_IDLE:
1791                 return "I";
1792         case GNILND_FMABLK_PHYS:
1793                 return "P";
1794         case GNILND_FMABLK_VIRT:
1795                 return "V";
1796         case GNILND_FMABLK_FREED:
1797                 return "F";
1798         }
1799         return "<unknown state>";
1800 }
1801
1802 static inline const char *
1803 kgnilnd_msgtype2str(int type)
1804 {
1805         switch (type) {
1806                 DO_TYPE(GNILND_MSG_NONE);
1807                 DO_TYPE(GNILND_MSG_NOOP);
1808                 DO_TYPE(GNILND_MSG_IMMEDIATE);
1809                 DO_TYPE(GNILND_MSG_PUT_REQ);
1810                 DO_TYPE(GNILND_MSG_PUT_NAK);
1811                 DO_TYPE(GNILND_MSG_PUT_ACK);
1812                 DO_TYPE(GNILND_MSG_PUT_DONE);
1813                 DO_TYPE(GNILND_MSG_GET_REQ);
1814                 DO_TYPE(GNILND_MSG_GET_NAK);
1815                 DO_TYPE(GNILND_MSG_GET_DONE);
1816                 DO_TYPE(GNILND_MSG_CLOSE);
1817                 DO_TYPE(GNILND_MSG_PUT_REQ_REV);
1818                 DO_TYPE(GNILND_MSG_PUT_DONE_REV);
1819                 DO_TYPE(GNILND_MSG_PUT_NAK_REV);
1820                 DO_TYPE(GNILND_MSG_GET_REQ_REV);
1821                 DO_TYPE(GNILND_MSG_GET_ACK_REV);
1822                 DO_TYPE(GNILND_MSG_GET_DONE_REV);
1823                 DO_TYPE(GNILND_MSG_GET_NAK_REV);
1824         }
1825         return "<unknown msg type>";
1826 }
1827
1828 static inline const char *
1829 kgnilnd_tx_state2str(kgn_tx_list_state_t state)
1830 {
1831         switch (state) {
1832                 DO_TYPE(GNILND_TX_IDLE);
1833                 DO_TYPE(GNILND_TX_ALLOCD);
1834                 DO_TYPE(GNILND_TX_PEERQ);
1835                 DO_TYPE(GNILND_TX_MAPQ);
1836                 DO_TYPE(GNILND_TX_FMAQ);
1837                 DO_TYPE(GNILND_TX_LIVE_FMAQ);
1838                 DO_TYPE(GNILND_TX_RDMAQ);
1839                 DO_TYPE(GNILND_TX_LIVE_RDMAQ);
1840                 DO_TYPE(GNILND_TX_DYING);
1841                 DO_TYPE(GNILND_TX_FREED);
1842         }
1843         return "<unknown state>";
1844 }
1845
1846 static inline const char *
1847 kgnilnd_conn_state2str(kgn_conn_t *conn)
1848 {
1849         kgn_conn_state_t state = conn->gnc_state;
1850         switch (state) {
1851                 DO_TYPE(GNILND_CONN_DUMMY);
1852                 DO_TYPE(GNILND_CONN_LISTEN);
1853                 DO_TYPE(GNILND_CONN_CONNECTING);
1854                 DO_TYPE(GNILND_CONN_ESTABLISHED);
1855                 DO_TYPE(GNILND_CONN_CLOSING);
1856                 DO_TYPE(GNILND_CONN_CLOSED);
1857                 DO_TYPE(GNILND_CONN_DONE);
1858                 DO_TYPE(GNILND_CONN_DESTROY_EP);
1859         }
1860         return "<?state?>";
1861 }
1862
1863 static inline const char *
1864 kgnilnd_connreq_type2str(kgn_connreq_t *connreq)
1865 {
1866         kgn_connreq_type_t type = connreq->gncr_type;
1867
1868         switch (type) {
1869                 DO_TYPE(GNILND_CONNREQ_REQ);
1870                 DO_TYPE(GNILND_CONNREQ_NAK);
1871                 DO_TYPE(GNILND_CONNREQ_CLOSE);
1872         }
1873         return "<?type?>";
1874 }
1875
1876 static inline const char *
1877 kgnilnd_dgram_state2str(kgn_dgram_t *dgram)
1878 {
1879         kgn_dgram_state_t state = dgram->gndg_state;
1880
1881         switch (state) {
1882                 DO_TYPE(GNILND_DGRAM_USED);
1883                 DO_TYPE(GNILND_DGRAM_POSTING);
1884                 DO_TYPE(GNILND_DGRAM_POSTED);
1885                 DO_TYPE(GNILND_DGRAM_PROCESSING);
1886                 DO_TYPE(GNILND_DGRAM_DONE);
1887                 DO_TYPE(GNILND_DGRAM_CANCELED);
1888         }
1889         return "<?state?>";
1890 }
1891
1892 static inline const char *
1893 kgnilnd_dgram_type2str(kgn_dgram_t *dgram)
1894 {
1895         kgn_dgram_type_t type = dgram->gndg_type;
1896
1897         switch (type) {
1898                 DO_TYPE(GNILND_DGRAM_REQ);
1899                 DO_TYPE(GNILND_DGRAM_WC_REQ);
1900                 DO_TYPE(GNILND_DGRAM_NAK);
1901                 DO_TYPE(GNILND_DGRAM_CLOSE);
1902         }
1903         return "<?type?>";
1904 }
1905
1906 static inline const char *
1907 kgnilnd_conn_dgram_type2str(kgn_dgram_type_t type)
1908 {
1909         switch (type) {
1910                 DO_TYPE(GNILND_DGRAM_REQ);
1911                 DO_TYPE(GNILND_DGRAM_WC_REQ);
1912                 DO_TYPE(GNILND_DGRAM_NAK);
1913                 DO_TYPE(GNILND_DGRAM_CLOSE);
1914         }
1915         return "<?type?>";
1916 }
1917
1918 #undef DO_TYPE
1919
1920 /* API wrapper functions - include late to pick up all of the other defines */
1921 #include "gnilnd_api_wrap.h"
1922
1923 /* pulls in tunables per platform and adds in nid/nic conversion
1924  * if RCA wasn't available at build time */
1925 #include "gnilnd_hss_ops.h"
1926
1927 #if defined(CONFIG_CRAY_GEMINI)
1928  #include "gnilnd_gemini.h"
1929 #elif defined(CONFIG_CRAY_ARIES)
1930  #include "gnilnd_aries.h"
1931 #else
1932  #error "Undefined Network Hardware Type"
1933 #endif
1934
1935 #endif /* _GNILND_GNILND_H_ */