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