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