Whamcloud - gitweb
LU-2446 build: Update Whamcloud copyright messages for Intel
[fs/lustre-release.git] / lnet / klnds / mxlnd / mxlnd.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  *
32  * Copyright (C) 2006 Myricom, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lnet/klnds/mxlnd/mxlnd.h
39  *
40  * Author: Eric Barton <eric@bartonsoftware.com>
41  * Author: Scott Atchley <atchley at myri.com>
42  */
43
44 #include <linux/module.h>       /* module */
45 #include <linux/kernel.h>       /* module */
46 #include <linux/mm.h>
47 #include <linux/string.h>
48 #include <linux/stat.h>
49 #include <linux/errno.h>
50 #include <linux/unistd.h>
51 #include <linux/uio.h>
52 #include <linux/fs.h>
53
54 #include <asm/uaccess.h>
55 #include <asm/io.h>
56
57 #include <linux/init.h>         /* module */
58 #include <linux/fs.h>
59 #include <linux/file.h>
60 #include <linux/stat.h>
61 #include <linux/list.h>
62 #include <linux/kmod.h>
63 #include <linux/sysctl.h>
64 #include <linux/random.h>
65 #include <linux/utsname.h>
66 #include <linux/jiffies.h>      /* msecs_to_jiffies */
67 #include <asm/semaphore.h>
68
69 #include <net/sock.h>
70 #include <linux/in.h>
71
72 #include <asm/byteorder.h>      /* __LITTLE_ENDIAN */
73 #include <net/arp.h>            /* arp table */
74 #include <linux/netdevice.h>    /* get_device_by_name */
75 #include <linux/inetdevice.h>   /* neigh_lookup, etc. */
76 #include <linux/net.h>          /* sock_create_kern, kernel_connect, sock_release */
77
78 #define DEBUG_SUBSYSTEM S_LND
79
80 #include "libcfs/libcfs.h"
81 #include "lnet/lnet.h"
82 #include "lnet/lib-lnet.h"
83 //#include <lnet/lnet-sysctl.h>
84
85 #define MX_KERNEL 1
86 #include "mx_extensions.h"
87 #include "myriexpress.h"
88
89 #if LNET_MAX_IOV > MX_MAX_SEGMENTS
90     #error LNET_MAX_IOV is greater then MX_MAX_SEGMENTS
91 #endif
92
93 #define MXLND_MSG_MAGIC         0x4d583130              /* unique magic 'MX10' */
94 #define MXLND_MSG_VERSION       0x03
95
96 /* Using MX's 64 match bits
97  * We are using the match bits to specify message type and the cookie.  The
98  * highest four bits (60-63) are reserved for message type. Below we specify
99  * the types. We reserve the remaining combinations for future use.  The next 8
100  * bits (52-59) are reserved for returning a status code for failed GET_DATA
101  * (payload) messages. The last 52 bits are used for cookies. That should allow
102  * unique cookies for 4 KB messages at 10 Gbps line rate without rollover for
103  * about 8 years. That should be enough. */
104
105 #define MXLND_MSG_OFFSET        60      /* msg type offset */
106 #define MXLND_MSG_BITS          (64 - MXLND_MSG_OFFSET)
107 #define MXLND_MSG_MASK          (((1ULL<<MXLND_MSG_BITS) - 1) << MXLND_MSG_OFFSET)
108 #define MXLND_MSG_TYPE(x)       (((x) & MXLND_MSG_MASK) >> MXLND_MSG_OFFSET)
109
110 #define MXLND_ERROR_OFFSET      52      /* error value offset */
111 #define MXLND_ERROR_BITS        (MXLND_MSG_OFFSET - MXLND_ERROR_OFFSET)
112 #define MXLND_ERROR_MASK        (((1ULL<<MXLND_ERROR_BITS) - 1) << MXLND_ERROR_OFFSET)
113 #define MXLND_ERROR_VAL(x)      (((x) & MXLND_ERROR_MASK) >> MXLND_ERROR_OFFSET)
114
115 /* message types */
116 #define MXLND_MSG_ICON_REQ      0xb     /* mx_iconnect() before CONN_REQ */
117 #define MXLND_MSG_CONN_REQ      0xc     /* connection request */
118 #define MXLND_MSG_ICON_ACK      0x9     /* mx_iconnect() before CONN_ACK */
119 #define MXLND_MSG_CONN_ACK      0xa     /* connection request response */
120 #define MXLND_MSG_BYE           0xd     /* disconnect msg */
121 #define MXLND_MSG_EAGER         0xe     /* eager message */
122 #define MXLND_MSG_NOOP          0x1     /* no msg, return credits */
123 #define MXLND_MSG_PUT_REQ       0x2     /* put request src->sink */
124 #define MXLND_MSG_PUT_ACK       0x3     /* put ack     src<-sink */
125 #define MXLND_MSG_PUT_DATA      0x4     /* put payload src->sink */
126 #define MXLND_MSG_GET_REQ       0x5     /* get request sink->src */
127 #define MXLND_MSG_GET_DATA      0x6     /* get payload sink<-src */
128
129 /* when to roll-over the cookie value */
130 #define MXLND_MAX_COOKIE        ((1ULL << MXLND_ERROR_OFFSET) - 1)
131
132 /* defaults for configurable parameters */
133 #define MXLND_N_SCHED           1               /* # schedulers (mx_wait_any() threads) */
134 #define MXLND_NDAEMONS          3               /* connd, timeoutd, tx_queued */
135 #define MXLND_MX_BOARD          0               /* Use the first MX NIC if more than 1 avail */
136 #define MXLND_MX_EP_ID          0               /* MX endpoint ID */
137 #define MXLND_COMM_TIMEOUT      (20 * CFS_HZ)   /* timeout for send/recv (jiffies) */
138 #define MXLND_WAIT_TIMEOUT      CFS_HZ          /* timeout for wait (jiffies) */
139 #define MXLND_CONNECT_TIMEOUT   (5 * CFS_HZ)    /* timeout for connections (jiffies) */
140 #define MXLND_POLLING           1000            /* poll iterations before blocking */
141 #define MXLND_LOOKUP_COUNT      5               /* how many times to try to resolve MAC */
142 #define MXLND_MAX_PEERS         1024            /* number of nodes talking to me */
143
144 #define MXLND_MSG_SIZE          (4<<10)         /* pre-posted eager message size */
145 #define MXLND_MSG_QUEUE_DEPTH   8               /* default msg queue depth */
146 #define MXLND_NTX               256             /* default # of tx msg descriptors */
147
148 #define MXLND_HASH_BITS         6               /* the number of bits to hash over */
149 #define MXLND_HASH_SIZE         (1<<MXLND_HASH_BITS)
150                                                 /* number of peer lists for lookup.
151                                                    we hash over the last N bits of
152                                                    the IP address converted to an int. */
153 #define MXLND_HASH_MASK         (MXLND_HASH_SIZE - 1)
154                                                 /* ensure we use only the last N bits */
155
156 /* derived constants... */
157 /* TX messages (shared by all connections) */
158 #define MXLND_TX_MSGS()       (*kmxlnd_tunables.kmx_ntx)
159 #define MXLND_TX_MSG_BYTES()  (MXLND_TX_MSGS() * MXLND_MSG_SIZE)
160 #define MXLND_TX_MSG_PAGES()  ((MXLND_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
161
162 /* RX messages (per connection) */
163 #define MXLND_RX_MSGS()       (*kmxlnd_tunables.kmx_peercredits)
164 #define MXLND_RX_MSG_BYTES()  (MXLND_RX_MSGS() * MXLND_MSG_SIZE)
165 #define MXLND_RX_MSG_PAGES()  ((MXLND_RX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
166 #define MXLND_CREDIT_HIGHWATER() (*kmxlnd_tunables.kmx_peercredits - 2)
167                                                 /* when to send a noop to return credits */
168
169 /* debugging features */
170 #define MXLND_CKSUM             0       /* checksum kmx_msg_t */
171 #define MXLND_DEBUG             0       /* additional CDEBUG messages */
172
173 /* provide wrappers around LIBCFS_ALLOC/FREE to keep MXLND specific
174  * memory usage stats that include pages */
175
176 #define MXLND_ALLOC(x, size)                                    \
177         do {                                                    \
178                 spin_lock(&kmxlnd_data.kmx_mem_lock);           \
179                 kmxlnd_data.kmx_mem_used += size;               \
180                 spin_unlock(&kmxlnd_data.kmx_mem_lock);         \
181                 LIBCFS_ALLOC(x, size);                          \
182                 if (unlikely(x == NULL)) {                      \
183                         spin_lock(&kmxlnd_data.kmx_mem_lock);   \
184                         kmxlnd_data.kmx_mem_used -= size;       \
185                         spin_unlock(&kmxlnd_data.kmx_mem_lock); \
186                 }                                               \
187         } while (0)
188
189 #define MXLND_FREE(x, size)                                     \
190         do {                                                    \
191                 spin_lock(&kmxlnd_data.kmx_mem_lock);           \
192                 kmxlnd_data.kmx_mem_used -= size;               \
193                 spin_unlock(&kmxlnd_data.kmx_mem_lock);         \
194                 LIBCFS_FREE(x, size);                           \
195         } while (0)
196
197
198 typedef struct kmx_tunables
199 {
200         int     *kmx_n_waitd;           /* # completion threads */
201         int     *kmx_max_peers;         /* max # of potential peers */
202         int     *kmx_cksum;             /* checksum small msgs? */
203         int     *kmx_ntx;               /* total # of tx */
204         int     *kmx_credits;           /* concurrent sends to all peers */
205         int     *kmx_peercredits;       /* concurrent sends to 1 peer */
206         int     *kmx_board;             /* MX board (NIC) number */
207         int     *kmx_ep_id;             /* MX endpoint number */
208         char    **kmx_default_ipif;     /* IPoMX interface name */
209         int     *kmx_polling;           /* if 0, block. if > 0, poll this many
210                                            iterations before blocking */
211 } kmx_tunables_t;
212
213 typedef struct
214 {
215         int               mxg_npages;   /* # pages */
216         struct page      *mxg_pages[0];
217 } kmx_pages_t;
218
219 /* global interface state */
220 typedef struct kmx_data
221 {
222         int                 kmx_init;           /* initialization state */
223         cfs_atomic_t        kmx_shutdown;       /* shutting down? */
224         cfs_atomic_t        kmx_nthreads;       /* number of threads */
225         struct completion   *kmx_completions;   /* array of completion struct */
226         lnet_ni_t           *kmx_ni;            /* the LND instance */
227         u64                 kmx_incarnation;    /* my incarnation value */
228         long                kmx_mem_used;       /* memory used */
229         mx_endpoint_t       kmx_endpt;          /* the MX endpoint */
230         mx_endpoint_addr_t  kmx_epa;            /* the MX endpoint address */
231
232         rwlock_t            kmx_global_lock;    /* global lock */
233         spinlock_t          kmx_mem_lock;       /* memory accounting lock */
234
235         cfs_list_t          kmx_conn_reqs;      /* list of connection reqs */
236         spinlock_t          kmx_conn_lock;      /* connection list lock */
237         struct semaphore    kmx_conn_sem;       /* connection request list */
238         cfs_list_t          kmx_conn_zombies;   /* list of zombie connections */
239         cfs_list_t          kmx_orphan_msgs;    /* list of txs to cancel */
240
241                                                 /* list of all known peers */
242         cfs_list_t          kmx_peers[MXLND_HASH_SIZE];
243         cfs_atomic_t        kmx_npeers;         /* number of peers */
244
245         kmx_pages_t        *kmx_tx_pages;       /* tx msg pages */
246
247         struct kmx_ctx     *kmx_txs;            /* all tx descriptors */
248         cfs_list_t          kmx_tx_idle;        /* list of idle tx */
249         spinlock_t          kmx_tx_idle_lock;   /* lock for idle tx list */
250         s32                 kmx_tx_used;        /* txs in use */
251         u64                 kmx_tx_next_cookie; /* unique id for tx */
252         cfs_list_t          kmx_tx_queue;       /* generic send queue */
253         spinlock_t          kmx_tx_queue_lock;  /* lock for generic sends */
254         struct semaphore    kmx_tx_queue_sem;   /* semaphore for tx queue */
255 } kmx_data_t;
256
257 #define MXLND_INIT_NOTHING      0       /* in the beginning, there was nothing... */
258 #define MXLND_INIT_DATA         1       /* main data structures created */
259 #define MXLND_INIT_TXS          2       /* tx descriptors created */
260 #define MXLND_INIT_MX           3       /* initiate MX library, open endpoint, get NIC id */
261 #define MXLND_INIT_THREADS      4       /* waitd, timeoutd, tx_queued threads */
262 #define MXLND_INIT_ALL          5       /* startup completed */
263
264 /************************************************************************
265  * MXLND Wire message format.
266  * These are sent in sender's byte order (i.e. receiver flips).
267  */
268
269 typedef struct kmx_connreq_msg
270 {
271         u32             mxcrm_queue_depth;              /* per peer max messages in flight */
272         u32             mxcrm_eager_size;               /* size of preposted eager messages */
273 } WIRE_ATTR kmx_connreq_msg_t;
274
275 typedef struct kmx_eager_msg
276 {
277         lnet_hdr_t      mxem_hdr;                       /* lnet header */
278         char            mxem_payload[0];                /* piggy-backed payload */
279 } WIRE_ATTR kmx_eager_msg_t;
280
281 typedef struct kmx_putreq_msg
282 {
283         lnet_hdr_t      mxprm_hdr;                      /* lnet header */
284         u64             mxprm_cookie;                   /* opaque completion cookie */
285 } WIRE_ATTR kmx_putreq_msg_t;
286
287 typedef struct kmx_putack_msg
288 {
289         u64             mxpam_src_cookie;               /* reflected completion cookie */
290         u64             mxpam_dst_cookie;               /* opaque completion cookie */
291 } WIRE_ATTR kmx_putack_msg_t;
292
293 typedef struct kmx_getreq_msg
294 {
295         lnet_hdr_t      mxgrm_hdr;                      /* lnet header */
296         u64             mxgrm_cookie;                   /* opaque completion cookie */
297 } WIRE_ATTR kmx_getreq_msg_t;
298
299 typedef struct kmx_msg
300 {
301         /* First two fields fixed for all time */
302         u32             mxm_magic;                      /* MXLND message */
303         u16             mxm_version;                    /* version number */
304
305         u8              mxm_type;                       /* message type */
306         u8              mxm_credits;                    /* returned credits */
307         u32             mxm_nob;                        /* # of bytes in whole message */
308         u32             mxm_cksum;                      /* checksum (0 == no checksum) */
309         u64             mxm_srcnid;                     /* sender's NID */
310         u64             mxm_srcstamp;                   /* sender's incarnation */
311         u64             mxm_dstnid;                     /* destination's NID */
312         u64             mxm_dststamp;                   /* destination's incarnation */
313
314         union {
315                 kmx_connreq_msg_t       conn_req;
316                 kmx_eager_msg_t         eager;
317                 kmx_putreq_msg_t        put_req;
318                 kmx_putack_msg_t        put_ack;
319                 kmx_getreq_msg_t        get_req;
320         } WIRE_ATTR mxm_u;
321 } WIRE_ATTR kmx_msg_t;
322
323 /***********************************************************************/
324
325 enum kmx_req_type {
326         MXLND_REQ_TX    = 0,
327         MXLND_REQ_RX    = 1,
328 };
329
330 /* The life cycle of a request */
331 enum kmx_req_state {
332         MXLND_CTX_INIT       = 0,               /* just created */
333         MXLND_CTX_IDLE       = 1,               /* available for use */
334         MXLND_CTX_PREP       = 2,               /* getting ready for send/recv */
335         MXLND_CTX_PENDING    = 3,               /* mx_isend() or mx_irecv() called */
336         MXLND_CTX_COMPLETED  = 4,               /* cleaning up after completion or timeout */
337         MXLND_CTX_CANCELED   = 5,               /* timed out but still in ctx list */
338 };
339
340 /* Context Structure - generic tx/rx descriptor
341  * It represents the context (or state) of each send or receive request.
342  * In other LNDs, they have separate TX and RX descriptors and this replaces both.
343  *
344  * The txs live on the global kmx_txs array for cleanup. The rxs are managed
345  * per struct kmx_conn. We will move them between the rx/tx idle lists and the
346  * pending list which is monitored by mxlnd_timeoutd().
347  */
348 typedef struct kmx_ctx
349 {
350         enum kmx_req_type   mxc_type;           /* TX or RX */
351         u64                 mxc_incarnation;    /* store the peer's incarnation here
352                                                    to verify before changing flow
353                                                    control credits after completion */
354         unsigned long       mxc_deadline;       /* request time out in absolute jiffies */
355         enum kmx_req_state  mxc_state;          /* what is the state of the request? */
356         cfs_list_t          mxc_list;           /* place on rx/tx idle list, tx q, peer tx */
357         cfs_list_t          mxc_rx_list;        /* place on mxp_rx_posted list */
358
359         lnet_nid_t          mxc_nid;            /* dst's NID if peer is not known */
360         struct kmx_peer    *mxc_peer;           /* owning peer */
361         struct kmx_conn    *mxc_conn;           /* owning conn */
362         kmx_msg_t          *mxc_msg;            /* msg hdr mapped to mxc_page */
363         lnet_msg_t         *mxc_lntmsg[2];      /* lnet msgs to finalize */
364
365         u8                  mxc_msg_type;       /* what type of message is this? */
366         u64                 mxc_cookie;         /* completion cookie */
367         u64                 mxc_match;          /* MX match info */
368         mx_ksegment_t       mxc_seg;            /* local MX ksegment for non-DATA */
369         mx_ksegment_t      *mxc_seg_list;       /* MX ksegment array for DATA */
370         int                 mxc_nseg;           /* number of segments */
371         unsigned long       mxc_pin_type;       /* MX_PIN_PHYSICAL [| MX_PIN_FULLPAGES] */
372         u32                 mxc_nob;            /* number of bytes sent/received */
373         mx_request_t        mxc_mxreq;          /* MX request */
374         mx_status_t         mxc_status;         /* MX status */
375         u32                 mxc_errno;          /* errno for LNET */
376         u64                 mxc_get;            /* # of times returned from idle list */
377         u64                 mxc_put;            /* # of times returned from idle list */
378 } kmx_ctx_t;
379
380 #define MXLND_CONN_DISCONNECT  -2       /* conn is being destroyed - do not add txs */
381 #define MXLND_CONN_FAIL        -1       /* connect failed (bad handshake, unavail, etc.) */
382 #define MXLND_CONN_INIT         0       /* in the beginning, there was nothing... */
383 #define MXLND_CONN_REQ          1       /* a connection request message is needed */
384 #define MXLND_CONN_ACK          2       /* a connection ack is needed */
385 #define MXLND_CONN_WAIT         3       /* waiting for req or ack to complete */
386 #define MXLND_CONN_READY        4       /* ready to send */
387
388 /* store all data from an unexpected CONN_[REQ|ACK] receive */
389 typedef struct kmx_connparams
390 {
391         cfs_list_t              mxr_list;       /* list to hang on kmx_conn_reqs */
392         void                   *mxr_context;    /* context - unused - will hold net */
393         mx_endpoint_addr_t      mxr_epa;        /* the peer's epa */
394         u64                     mxr_match;      /* the CONN_REQ's match bits */
395         u32                     mxr_nob;        /* length of CONN_REQ message */
396         struct kmx_peer        *mxr_peer;       /* peer if known */
397         struct kmx_conn        *mxr_conn;       /* conn if known */
398         kmx_msg_t               mxr_msg;        /* the msg header & connreq_msg_t */
399 } kmx_connparams_t;
400
401 /* connection state - queues for queued and pending msgs */
402 typedef struct kmx_conn
403 {
404         struct kmx_peer    *mxk_peer;           /* owning peer */
405         cfs_list_t          mxk_list;           /* for placing on mxp_conns */
406         cfs_list_t          mxk_zombie;         /* for placing on zombies list */
407         u64                 mxk_incarnation;    /* connections's incarnation value */
408         u32                 mxk_sid;            /* peer's MX session id */
409         cfs_atomic_t        mxk_refcount;       /* reference counting */
410         int                 mxk_status;         /* can we send messages? MXLND_CONN_* */
411
412         mx_endpoint_addr_t  mxk_epa;            /* peer's endpoint address */
413
414         spinlock_t          mxk_lock;           /* lock */
415         unsigned long       mxk_timeout;        /* expiration of oldest pending tx/rx */
416         unsigned long       mxk_last_tx;        /* when last tx completed with success */
417         unsigned long       mxk_last_rx;        /* when last rx completed */
418
419         kmx_pages_t        *mxk_rx_pages;       /* rx msg pages */
420         kmx_ctx_t          *mxk_rxs;            /* the rx descriptors */
421         cfs_list_t          mxk_rx_idle;        /* list of idle rx */
422
423         int                 mxk_credits;        /* # of my credits for sending to peer */
424         int                 mxk_outstanding;    /* # of credits to return */
425
426         cfs_list_t          mxk_tx_credit_queue; /* send queue for peer */
427         cfs_list_t          mxk_tx_free_queue;  /* send queue for peer */
428         int                 mxk_ntx_msgs;       /* # of msgs on tx queues */
429         int                 mxk_ntx_data ;      /* # of DATA on tx queues */
430         int                 mxk_ntx_posted;     /* # of tx msgs in flight */
431         int                 mxk_data_posted;    /* # of tx data payloads in flight */
432
433         cfs_list_t          mxk_pending;        /* in flight rxs and txs */
434 } kmx_conn_t;
435
436 /* peer state */
437 typedef struct kmx_peer
438 {
439         cfs_list_t          mxp_list;           /* for placing on kmx_peers */
440         lnet_nid_t          mxp_nid;            /* peer's LNET NID */
441         lnet_ni_t          *mxp_ni;             /* LNET interface */
442         cfs_atomic_t        mxp_refcount;       /* reference counts */
443
444         cfs_list_t          mxp_conns;          /* list of connections */
445         kmx_conn_t         *mxp_conn;           /* current connection */
446         cfs_list_t          mxp_tx_queue;       /* msgs waiting for a conn */
447
448         u32                 mxp_board;          /* peer's board rank */
449         u32                 mxp_ep_id;          /* peer's MX endpoint ID */
450         u64                 mxp_nic_id;         /* remote's MX nic_id for mx_connect() */
451
452         unsigned long       mxp_reconnect_time; /* when to retry connect */
453         int                 mxp_incompatible;   /* incorrect conn_req values */
454 } kmx_peer_t;
455
456 extern kmx_data_t       kmxlnd_data;
457 extern kmx_tunables_t   kmxlnd_tunables;
458
459 /* required for the LNET API */
460 int  mxlnd_startup(lnet_ni_t *ni);
461 void mxlnd_shutdown(lnet_ni_t *ni);
462 int  mxlnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
463 int  mxlnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
464 int  mxlnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
465                 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
466                 unsigned int offset, unsigned int mlen, unsigned int rlen);
467
468 /* in mxlnd.c */
469 extern void mxlnd_thread_stop(long id);
470 extern void mxlnd_ctx_init(kmx_ctx_t *ctx);
471 extern int  mxlnd_peer_alloc(kmx_peer_t **peerp, lnet_nid_t nid,
472                 u32 board, u32 ep_id, u64 nic_id);
473 extern int mxlnd_alloc_pages(kmx_pages_t **pp, int npages);
474 extern void mxlnd_free_pages(kmx_pages_t *p);
475
476 /* in mxlnd_cb.c */
477 void mxlnd_eager_recv(void *context, uint64_t match_value, uint32_t length);
478 extern mx_unexp_handler_action_t mxlnd_unexpected_recv(void *context,
479                 mx_endpoint_addr_t source, uint64_t match_value, uint32_t length,
480                 void *data_if_available);
481 extern void mxlnd_peer_free(kmx_peer_t *peer);
482 extern void mxlnd_conn_free_locked(kmx_conn_t *conn);
483 extern void mxlnd_conn_disconnect(kmx_conn_t *conn, int mx_dis, int send_bye);
484 extern int mxlnd_close_matching_conns(lnet_nid_t nid);
485 extern void mxlnd_sleep(unsigned long timeout);
486 extern int  mxlnd_tx_queued(void *arg);
487 extern void mxlnd_handle_rx_completion(kmx_ctx_t *rx);
488 extern int  mxlnd_check_sends(kmx_peer_t *peer);
489 extern int  mxlnd_tx_peer_queued(void *arg);
490 extern int  mxlnd_request_waitd(void *arg);
491 extern int  mxlnd_unex_recvd(void *arg);
492 extern int  mxlnd_timeoutd(void *arg);
493 extern int mxlnd_free_conn_zombies(void);
494 extern int  mxlnd_connd(void *arg);
495 extern int mxlnd_del_peer(lnet_nid_t nid);
496
497
498 /**
499  * mxlnd_nid_to_hash - hash the nid
500  * @nid - LNET ID
501  *
502  * Takes the u64 nid and XORs the lowest N bits by the next lowest N bits.
503  */
504 static inline int
505 mxlnd_nid_to_hash(lnet_nid_t nid)
506 {
507         return (nid & MXLND_HASH_MASK) ^
508                ((nid & (MXLND_HASH_MASK << MXLND_HASH_BITS)) >> MXLND_HASH_BITS);
509 }
510
511
512 #define mxlnd_peer_addref(peer)                                 \
513 do {                                                            \
514         LASSERT(peer != NULL);                                  \
515         LASSERT(cfs_atomic_read(&(peer)->mxp_refcount) > 0);    \
516         cfs_atomic_inc(&(peer)->mxp_refcount);                  \
517 } while (0)
518
519
520 #define mxlnd_peer_decref(peer)                                 \
521 do {                                                            \
522         LASSERT(cfs_atomic_read(&(peer)->mxp_refcount) > 0);    \
523         if (cfs_atomic_dec_and_test(&(peer)->mxp_refcount))     \
524                 mxlnd_peer_free(peer);                          \
525 } while (0)
526
527 #define mxlnd_conn_addref(conn)                                 \
528 do {                                                            \
529         LASSERT(conn != NULL);                                  \
530         LASSERT(cfs_atomic_read(&(conn)->mxk_refcount) > 0);    \
531         cfs_atomic_inc(&(conn)->mxk_refcount);                  \
532 } while (0)
533
534
535 #define mxlnd_conn_decref(conn)                                         \
536 do {                                                                    \
537         LASSERT(conn != NULL);                                          \
538         LASSERT(cfs_atomic_read(&(conn)->mxk_refcount) > 0);            \
539         if (cfs_atomic_dec_and_test(&(conn)->mxk_refcount)) {           \
540                 spin_lock(&kmxlnd_data.kmx_conn_lock);                  \
541                 LASSERT((conn)->mxk_status == MXLND_CONN_DISCONNECT);   \
542                 CDEBUG(D_NET, "adding conn %p to zombies\n", (conn));   \
543                 cfs_list_add_tail(&(conn)->mxk_zombie,                  \
544                                  &kmxlnd_data.kmx_conn_zombies);        \
545                 spin_unlock(&kmxlnd_data.kmx_conn_lock);                \
546                 up(&kmxlnd_data.kmx_conn_sem);                          \
547         }                                                               \
548 } while (0)
549
550 #define mxlnd_valid_msg_type(type)                              \
551 do {                                                            \
552         LASSERT((type) == MXLND_MSG_EAGER    ||                 \
553                 (type) == MXLND_MSG_ICON_REQ ||                 \
554                 (type) == MXLND_MSG_CONN_REQ ||                 \
555                 (type) == MXLND_MSG_ICON_ACK ||                 \
556                 (type) == MXLND_MSG_CONN_ACK ||                 \
557                 (type) == MXLND_MSG_BYE      ||                 \
558                 (type) == MXLND_MSG_NOOP     ||                 \
559                 (type) == MXLND_MSG_PUT_REQ  ||                 \
560                 (type) == MXLND_MSG_PUT_ACK  ||                 \
561                 (type) == MXLND_MSG_PUT_DATA ||                 \
562                 (type) == MXLND_MSG_GET_REQ  ||                 \
563                 (type) == MXLND_MSG_GET_DATA);                  \
564 } while (0)