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