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