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