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