Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lnet / klnds / iiblnd / iiblnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/klnds/iiblnd/iiblnd.h
37  *
38  * Author: Eric Barton <eric@bartonsoftware.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #ifndef AUTOCONF_INCLUDED
45 #include <linux/config.h>
46 #endif
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.h>
50 #include <linux/string.h>
51 #include <linux/stat.h>
52 #include <linux/errno.h>
53 #include <linux/smp_lock.h>
54 #include <linux/unistd.h>
55 #include <linux/uio.h>
56
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
60
61 #include <linux/init.h>
62 #include <linux/fs.h>
63 #include <linux/file.h>
64 #include <linux/stat.h>
65 #include <linux/list.h>
66 #include <linux/kmod.h>
67 #include <linux/sysctl.h>
68
69 #define DEBUG_SUBSYSTEM S_LND
70
71 #include <libcfs/libcfs.h>
72 #include <lnet/lnet.h>
73 #include <lnet/lib-lnet.h>
74
75 #include <linux/iba/ibt.h>
76
77 #define GCC_VERSION (__GNUC__ * 10000 \
78                 + __GNUC_MINOR__ * 100 \
79                 + __GNUC_PATCHLEVEL__)
80
81 /* Test for GCC > 3.2.2 */
82 #if GCC_VERSION <= 30202
83 /* GCC 3.2.2, and presumably several versions before it, will
84  * miscompile this driver. See
85  * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9853. */
86 #error Invalid GCC version. Must use GCC >= 3.2.3
87 #endif
88
89 #ifdef CONFIG_SMP
90 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
91 #else
92 # define IBNAL_N_SCHED      1                   /* # schedulers */
93 #endif
94
95 #define IBNAL_USE_FMR                0          /* map on demand v. use whole mem mapping */
96 #define KIBLND_DETAILED_DEBUG        0
97
98 /* tunables fixed at compile time */
99 #define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
100 #define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
101 #define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
102 #define IBNAL_CREDIT_HIGHWATER       7          /* when to eagerly return credits */
103 #define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
104 #define IBNAL_RDMA_BASE              0x0eeb0000
105 #define IBNAL_STARTING_PSN           1
106
107 /* QP tunables */
108 /* 7 indicates infinite retry attempts, Infinicon recommended 5 */
109 #define IBNAL_RETRY                  5          /* # times to retry */
110 #define IBNAL_RNR_RETRY              5          /*  */
111 #define IBNAL_CM_RETRY               5          /* # times to retry connection */
112 #define IBNAL_FLOW_CONTROL           1
113 #define IBNAL_ACK_TIMEOUT            20         /* supposedly 4 secs */
114 #define IBNAL_EE_FLOW                1
115 #define IBNAL_LOCAL_SUB              1
116 #define IBNAL_FAILOVER_ACCEPTED      0
117
118 /************************/
119 /* derived constants... */
120
121 /* TX messages (shared by all connections) */
122 #define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
123 #define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
124 #define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
125
126 #if IBNAL_USE_FMR
127 # define IBNAL_MAX_RDMA_FRAGS 1
128 # define IBNAL_CONCURRENT_SENDS IBNAL_RX_MSGS
129 #else
130 # define IBNAL_MAX_RDMA_FRAGS LNET_MAX_IOV
131 # define IBNAL_CONCURRENT_SENDS IBNAL_MSG_QUEUE_SIZE
132 #endif
133
134 /* RX messages (per connection) */
135 #define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE * 2)
136 #define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
137 #define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
138
139 #define IBNAL_CQ_ENTRIES()  (IBNAL_TX_MSGS() * (1 + IBNAL_MAX_RDMA_FRAGS) +             \
140                              (IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers))
141
142 typedef struct
143 {
144         char            **kib_hca_basename;     /* HCA base name */
145         char            **kib_ipif_basename;    /* IPoIB interface base name */
146         char            **kib_service_name;     /* global service name */
147         unsigned int     *kib_service_number;   /* global service number */
148         int              *kib_min_reconnect_interval; /* min connect retry seconds... */
149         int              *kib_max_reconnect_interval; /* max connect retry seconds */
150         int              *kib_concurrent_peers; /* max # peers */
151         int              *kib_cksum;            /* checksum kib_msg_t? */
152         int              *kib_timeout;          /* comms timeout (seconds) */
153         int              *kib_keepalive;        /* keepalive timeout (seconds) */
154         int              *kib_ntx;              /* # tx descs */
155         int              *kib_credits;          /* # concurrent sends */
156         int              *kib_peercredits;      /* # concurrent sends to 1 peer */
157         int              *kib_sd_retries;       /* # concurrent sends to 1 peer */
158         int              *kib_concurrent_sends; /* send work queue sizing */
159 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
160         cfs_sysctl_table_header_t *kib_sysctl;  /* sysctl interface */
161 #endif
162 } kib_tunables_t;
163
164 /* NB The Infinicon stack has specific typedefs for some things
165  * (e.g. IB_{L,R}_KEY), that just map back to __u32 etc */
166 typedef struct
167 {
168         int               ibp_npages;           /* # pages */
169         struct page      *ibp_pages[0];
170 } kib_pages_t;
171
172 typedef struct
173 {
174         IB_HANDLE         md_handle;
175         __u32             md_lkey;
176         __u32             md_rkey;
177         __u64             md_addr;
178 } kib_md_t;
179
180 typedef struct
181 {
182         int               kib_init;             /* initialisation state */
183         __u64             kib_incarnation;      /* which one am I */
184         int               kib_shutdown;         /* shut down? */
185         atomic_t          kib_nthreads;         /* # live threads */
186         lnet_ni_t        *kib_ni;               /* _the_ iib instance */
187
188         __u64             kib_port_guid;        /* my GUID (lo 64 of GID)*/
189         __u16             kib_port_pkey;        /* my pkey, whatever that is */
190         struct semaphore  kib_listener_signal;  /* signal completion */
191         IB_HANDLE         kib_listener_cep;     /* connection end point */
192
193         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
194         int               kib_ready;            /* CQ callback fired */
195         int               kib_checking_cq;      /* a scheduler is checking the CQ */
196
197         struct list_head *kib_peers;            /* hash table of all my known peers */
198         int               kib_peer_hash_size;   /* size of kib_peers */
199         atomic_t          kib_npeers;           /* # peers extant */
200         atomic_t          kib_nconns;           /* # connections extant */
201
202         struct list_head  kib_connd_zombies;    /* connections to free */
203         struct list_head  kib_connd_conns;      /* connections to progress */
204         struct list_head  kib_connd_peers;      /* peers waiting for a connection */
205         wait_queue_head_t kib_connd_waitq;      /* connection daemon sleep here */
206         spinlock_t        kib_connd_lock;       /* serialise */
207
208         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
209         spinlock_t        kib_sched_lock;       /* serialise */
210
211         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
212         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
213
214         struct list_head  kib_idle_txs;         /* idle tx descriptors */
215         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
216         spinlock_t        kib_tx_lock;          /* serialise */
217
218         IB_HANDLE         kib_hca;              /* The HCA */
219         int               kib_port;             /* port on the device */
220         IB_HANDLE         kib_pd;               /* protection domain */
221         IB_HANDLE         kib_sd;               /* SD handle */
222         IB_HANDLE         kib_cq;               /* completion queue */
223         kib_md_t          kib_whole_mem;        /* whole-mem registration */
224
225         int               kib_hca_idx;          /* my HCA number */
226         uint64            kib_hca_guids[8];     /* all the HCA guids */
227         IB_CA_ATTRIBUTES  kib_hca_attrs;        /* where to get HCA attrs */
228
229         COMMAND_CONTROL_PARAMETERS kib_sdretry; /* control SD query retries */
230 } kib_data_t;
231
232 #define IBNAL_INIT_NOTHING         0
233 #define IBNAL_INIT_DATA            1
234 #define IBNAL_INIT_LIB             2
235 #define IBNAL_INIT_HCA             3
236 #define IBNAL_INIT_PORTATTRS       4
237 #define IBNAL_INIT_SD              5
238 #define IBNAL_INIT_PD              6
239 #define IBNAL_INIT_MD              7
240 #define IBNAL_INIT_TXD             8
241 #define IBNAL_INIT_CQ              9
242 #define IBNAL_INIT_ALL             10
243
244 /************************************************************************
245  * Wire message structs.
246  * These are sent in sender's byte order (i.e. receiver flips).
247  * CAVEAT EMPTOR: other structs communicated between nodes (e.g. MAD
248  * private data and SM service info), is LE on the wire.
249  */
250
251 typedef struct kib_connparams
252 {
253         __u32             ibcp_queue_depth;
254         __u32             ibcp_max_msg_size;
255         __u32             ibcp_max_frags;
256 } WIRE_ATTR kib_connparams_t;
257
258 typedef struct
259 {
260         lnet_hdr_t        ibim_hdr;             /* portals header */
261         char              ibim_payload[0];      /* piggy-backed payload */
262 } WIRE_ATTR kib_immediate_msg_t;
263
264 #if IBNAL_USE_FMR
265 typedef struct
266 {
267         __u64             rd_addr;              /* IO VMA address */
268         __u32             rd_nob;               /* # of bytes */
269         __u32             rd_key;               /* remote key */
270 } WIRE_ATTR kib_rdma_desc_t;
271 #else
272 typedef struct
273 {
274         __u32             rf_nob;               /* # of bytes */
275         __u64             rf_addr;              /* remote io vaddr */
276 } WIRE_ATTR kib_rdma_frag_t;
277
278 typedef struct
279 {
280         __u32             rd_key;               /* local/remote key */
281         __u32             rd_nfrag;             /* # fragments */
282         kib_rdma_frag_t   rd_frags[0];          /* buffer frags */
283 } WIRE_ATTR kib_rdma_desc_t;
284 #endif
285
286 typedef struct
287 {
288         lnet_hdr_t        ibprm_hdr;            /* LNET header */
289         __u64             ibprm_cookie;         /* opaque completion cookie */
290 } WIRE_ATTR kib_putreq_msg_t;
291
292 typedef struct
293 {
294         __u64             ibpam_src_cookie;     /* reflected completion cookie */
295         __u64             ibpam_dst_cookie;     /* opaque completion cookie */
296         kib_rdma_desc_t   ibpam_rd;             /* sender's sink buffer */
297 } WIRE_ATTR kib_putack_msg_t;
298
299 typedef struct
300 {
301         lnet_hdr_t        ibgm_hdr;             /* LNET header */
302         __u64             ibgm_cookie;          /* opaque completion cookie */
303         kib_rdma_desc_t   ibgm_rd;              /* sender's sink buffer */
304 } WIRE_ATTR kib_get_msg_t;
305
306 typedef struct
307 {
308         __u64             ibcm_cookie;          /* opaque completion cookie */
309         __u32             ibcm_status;          /* completion status */
310 } WIRE_ATTR kib_completion_msg_t;
311
312 typedef struct
313 {
314         /* First 2 fields fixed FOR ALL TIME */
315         __u32             ibm_magic;            /* I'm an openibnal message */
316         __u16             ibm_version;          /* this is my version number */
317
318         __u8              ibm_type;             /* msg type */
319         __u8              ibm_credits;          /* returned credits */
320         __u32             ibm_nob;              /* # bytes in whole message */
321         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
322         __u64             ibm_srcnid;           /* sender's NID */
323         __u64             ibm_srcstamp;         /* sender's incarnation */
324         __u64             ibm_dstnid;           /* destination's NID */
325         __u64             ibm_dststamp;         /* destination's incarnation */
326         __u64             ibm_seq;              /* sequence number */
327
328         union {
329                 kib_connparams_t      connparams;
330                 kib_immediate_msg_t   immediate;
331                 kib_putreq_msg_t      putreq;
332                 kib_putack_msg_t      putack;
333                 kib_get_msg_t         get;
334                 kib_completion_msg_t  completion;
335         } WIRE_ATTR ibm_u;
336 } WIRE_ATTR kib_msg_t;
337
338 #define IBNAL_MSG_MAGIC LNET_PROTO_IIB_MAGIC    /* unique magic */
339 #define IBNAL_MSG_VERSION              2        /* current protocol version */
340 #define IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD 1   /* previous version */
341
342 #define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
343 #define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
344 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
345 #define IBNAL_MSG_IMMEDIATE         0xd1        /* immediate */
346 #define IBNAL_MSG_PUT_REQ           0xd2        /* putreq (src->sink) */
347 #define IBNAL_MSG_PUT_NAK           0xd3        /* completion (sink->src) */
348 #define IBNAL_MSG_PUT_ACK           0xd4        /* putack (sink->src) */
349 #define IBNAL_MSG_PUT_DONE          0xd5        /* completion (src->sink) */
350 #define IBNAL_MSG_GET_REQ           0xd6        /* getreq (sink->src) */
351 #define IBNAL_MSG_GET_DONE          0xd7        /* completion (src->sink: all OK) */
352
353 /* connection rejection reasons */
354 #define IBNAL_REJECT_CONN_RACE       0          /* You lost connection race */
355 #define IBNAL_REJECT_NO_RESOURCES    1          /* Out of memory/conns etc */
356 #define IBNAL_REJECT_FATAL           2          /* Anything else */
357
358 /***********************************************************************/
359
360 typedef struct kib_rx                           /* receive message */
361 {
362         struct list_head          rx_list;      /* queue for attention */
363         struct kib_conn          *rx_conn;      /* owning conn */
364         int                       rx_nob;       /* # bytes received (-1 while posted) */
365         __u64                     rx_hca_msg;   /* pre-mapped buffer (hca vaddr) */
366         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
367         IB_WORK_REQ2              rx_wrq;
368         IB_LOCAL_DATASEGMENT      rx_gl;        /* and its memory */
369 } kib_rx_t;
370
371 typedef struct kib_tx                           /* transmit message */
372 {
373         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
374         struct kib_conn          *tx_conn;      /* owning conn */
375         int                       tx_mapped;    /* mapped for RDMA? */
376         int                       tx_sending;   /* # tx callbacks outstanding */
377         int                       tx_queued;    /* queued for sending */
378         int                       tx_waiting;   /* waiting for peer */
379         int                       tx_status;    /* completion status */
380         unsigned long             tx_deadline;  /* completion deadline */
381         __u64                     tx_cookie;    /* completion cookie */
382         lnet_msg_t               *tx_lntmsg[2]; /* lnet msgs to finalize on completion */
383         kib_msg_t                *tx_msg;       /* pre-mapped buffer (host vaddr) */
384         __u64                     tx_hca_msg;   /* pre-mapped buffer (HCA vaddr) */
385         int                       tx_nwrq;      /* # send work items */
386 #if IBNAL_USE_FMR
387         IB_WORK_REQ2              tx_wrq[2];    /* send work items... */
388         IB_LOCAL_DATASEGMENT      tx_gl[2];     /* ...and their memory */
389         kib_rdma_desc_t           tx_rd[1];     /* rdma descriptor */
390         kib_md_t                  tx_md;        /* mapping */
391         __u64                    *tx_pages;     /* page phys addrs */
392 #else
393         IB_WORK_REQ2             *tx_wrq;       /* send work items... */
394         IB_LOCAL_DATASEGMENT     *tx_gl;        /* ...and their memory */
395         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor (src buffers) */
396 #endif
397 } kib_tx_t;
398
399 typedef struct
400 {
401         /* scratchpad during connection establishment */
402         IB_QP_ATTRIBUTES_QUERY cv_qpattrs;
403         QUERY                  cv_query;
404         IB_SERVICE_RECORD      cv_svcrec;
405         IB_PATH_RECORD         cv_path;
406         CM_CONN_INFO           cv_cmci;
407 } kib_connvars_t;
408
409 typedef struct kib_conn
410 {
411         struct kib_peer    *ibc_peer;           /* owning peer */
412         struct list_head    ibc_list;           /* stash on peer's conn list */
413         __u64               ibc_incarnation;    /* which instance of the peer */
414         __u64               ibc_txseq;          /* tx sequence number */
415         __u64               ibc_rxseq;          /* rx sequence number */
416         __u32               ibc_version;        /* peer protocol version */
417         atomic_t            ibc_refcount;       /* # users */
418         int                 ibc_state;          /* what's happening */
419         int                 ibc_nsends_posted;  /* # uncompleted sends */
420         int                 ibc_credits;        /* # credits I have */
421         int                 ibc_outstanding_credits; /* # credits to return */
422         int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
423         unsigned long       ibc_last_send;      /* time of last send */
424         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
425         struct list_head    ibc_tx_queue_nocred; /* sends that don't need a cred */
426         struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
427         struct list_head    ibc_tx_queue;       /* send queue */
428         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
429         spinlock_t          ibc_lock;           /* serialise */
430         kib_rx_t           *ibc_rxs;            /* the rx descs */
431         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
432         IB_HANDLE           ibc_qp;             /* queue pair */
433         IB_HANDLE           ibc_cep;            /* CM endpoint */
434         kib_connvars_t     *ibc_cvars;          /* connection scratchpad */
435 } kib_conn_t;
436
437 #define IBNAL_CONN_INIT_NOTHING      0          /* initial state */
438 #define IBNAL_CONN_INIT_QP           1          /* ibc_qp set up */
439 #define IBNAL_CONN_CONNECTING        2          /* started to connect */
440 #define IBNAL_CONN_ESTABLISHED       3          /* connection established */
441 #define IBNAL_CONN_DISCONNECTING     4          /* to send disconnect req */
442 #define IBNAL_CONN_DISCONNECTED      5          /* no more QP or CM traffic */
443
444 /* types of connection */
445 #define IBNAL_CONN_ACTIVE            0          /* active connect */
446 #define IBNAL_CONN_PASSIVE           1          /* passive connect */
447 #define IBNAL_CONN_WAITING           2          /* waiting for connect */
448
449 typedef struct kib_peer
450 {
451         struct list_head    ibp_list;           /* stash on global peer list */
452         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
453         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
454         atomic_t            ibp_refcount;       /* # users */
455         int                 ibp_persistence;    /* "known" peer refs */
456         int                 ibp_version;        /* protocol version */
457         struct list_head    ibp_conns;          /* all active connections */
458         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
459         int                 ibp_connecting;     /* active connects in progress */
460         int                 ibp_accepting;      /* passive connects in progress */
461         int                 ibp_passivewait;    /* waiting for peer to connect */
462         unsigned long       ibp_passivewait_deadline; /* when passive wait must complete */
463         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
464         unsigned long       ibp_reconnect_interval; /* exponential backoff */
465         int                 ibp_error;          /* errno on closing this peer */
466         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
467 } kib_peer_t;
468
469
470 extern kib_data_t      kibnal_data;
471 extern kib_tunables_t  kibnal_tunables;
472
473 /******************************************************************************/
474
475 /* these are purposely avoiding using local vars so they don't increase
476  * stack consumption. */
477
478 #define kibnal_conn_addref(conn)                                \
479 do {                                                            \
480         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
481                (conn), atomic_read(&(conn)->ibc_refcount));     \
482         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
483         atomic_inc(&(conn)->ibc_refcount);                      \
484 } while (0)
485
486 #define kibnal_conn_decref(conn)                                              \
487 do {                                                                          \
488         unsigned long   flags;                                                \
489                                                                               \
490         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
491                (conn), atomic_read(&(conn)->ibc_refcount));                   \
492         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
493         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
494                 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);        \
495                 list_add_tail(&(conn)->ibc_list,                              \
496                               &kibnal_data.kib_connd_zombies);                \
497                 wake_up(&kibnal_data.kib_connd_waitq);                        \
498                 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);   \
499         }                                                                     \
500 } while (0)
501
502 #define kibnal_peer_addref(peer)                                \
503 do {                                                            \
504         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
505                (peer), libcfs_nid2str((peer)->ibp_nid),         \
506                atomic_read (&(peer)->ibp_refcount));            \
507         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
508         atomic_inc(&(peer)->ibp_refcount);                      \
509 } while (0)
510
511 #define kibnal_peer_decref(peer)                                \
512 do {                                                            \
513         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
514                (peer), libcfs_nid2str((peer)->ibp_nid),         \
515                atomic_read (&(peer)->ibp_refcount));            \
516         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
517         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
518                 kibnal_destroy_peer(peer);                      \
519 } while (0)
520
521 /******************************************************************************/
522
523 static inline struct list_head *
524 kibnal_nid2peerlist (lnet_nid_t nid)
525 {
526         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
527
528         return (&kibnal_data.kib_peers [hash]);
529 }
530
531 static inline int
532 kibnal_peer_active(kib_peer_t *peer)
533 {
534         /* Am I in the peer hash table? */
535         return (!list_empty(&peer->ibp_list));
536 }
537
538 static inline int
539 kibnal_peer_connecting(kib_peer_t *peer)
540 {
541         /* Am I expecting a connection to materialise? */
542         return (peer->ibp_connecting != 0 ||
543                 peer->ibp_accepting != 0 ||
544                 peer->ibp_passivewait);
545 }
546
547 static inline void
548 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
549 {
550         struct list_head  *q;
551         
552         LASSERT (tx->tx_nwrq > 0);              /* work items set up */
553         LASSERT (!tx->tx_queued);               /* not queued for sending already */
554
555         tx->tx_queued = 1;
556         tx->tx_deadline = jiffies + (*kibnal_tunables.kib_timeout * HZ);
557
558         if (tx->tx_conn == NULL) {
559                 kibnal_conn_addref(conn);
560                 tx->tx_conn = conn;
561                 LASSERT (tx->tx_msg->ibm_type != IBNAL_MSG_PUT_DONE);
562         } else {
563                 LASSERT (tx->tx_conn == conn);
564                 LASSERT (tx->tx_msg->ibm_type == IBNAL_MSG_PUT_DONE);
565         }
566
567         if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
568                 /* All messages have simple credit control */
569                 q = &conn->ibc_tx_queue;
570         } else {
571                 LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
572                 
573                 switch (tx->tx_msg->ibm_type) {
574                 case IBNAL_MSG_PUT_REQ:
575                 case IBNAL_MSG_GET_REQ:
576                         /* RDMA request: reserve a buffer for the RDMA reply
577                          * before sending */
578                         q = &conn->ibc_tx_queue_rsrvd;
579                         break;
580
581                 case IBNAL_MSG_PUT_NAK:
582                 case IBNAL_MSG_PUT_ACK:
583                 case IBNAL_MSG_PUT_DONE:
584                 case IBNAL_MSG_GET_DONE:
585                         /* RDMA reply/completion: no credits; peer has reserved
586                          * a reply buffer */
587                         q = &conn->ibc_tx_queue_nocred;
588                         break;
589                 
590                 case IBNAL_MSG_NOOP:
591                 case IBNAL_MSG_IMMEDIATE:
592                         /* Otherwise: consume a credit before sending */
593                         q = &conn->ibc_tx_queue;
594                         break;
595                 
596                 default:
597                         LBUG();
598                         q = NULL;
599                 }
600         }
601         
602         list_add_tail(&tx->tx_list, q);
603 }
604
605 static inline int
606 kibnal_send_keepalive(kib_conn_t *conn) 
607 {
608         return (*kibnal_tunables.kib_keepalive > 0) &&
609                 time_after(jiffies, conn->ibc_last_send +
610                            *kibnal_tunables.kib_keepalive*HZ);
611 }
612
613 #define KIBNAL_SERVICE_KEY_MASK  (IB_SERVICE_RECORD_COMP_SERVICENAME |          \
614                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_1 |       \
615                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_2 |       \
616                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_3 |       \
617                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_4 |       \
618                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_5 |       \
619                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_6 |       \
620                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_7 |       \
621                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_8)
622
623 static inline __u64*
624 kibnal_service_nid_field(IB_SERVICE_RECORD *srv)
625 {
626         /* must be consistent with KIBNAL_SERVICE_KEY_MASK */
627         return (__u64 *)srv->ServiceData8;
628 }
629
630 static inline void
631 kibnal_set_service_keys(IB_SERVICE_RECORD *srv, lnet_nid_t nid)
632 {
633         char *svc_name = *kibnal_tunables.kib_service_name;
634
635         LASSERT (strlen(svc_name) < sizeof(srv->ServiceName));
636         memset (srv->ServiceName, 0, sizeof(srv->ServiceName));
637         strcpy (srv->ServiceName, svc_name);
638
639         *kibnal_service_nid_field(srv) = cpu_to_le64(nid);
640 }
641
642 /* CAVEAT EMPTOR: We rely on tx/rx descriptor alignment to allow us to use the
643  * lowest 2 bits of the work request id to stash the work item type (the op
644  * field is not valid when the wc completes in error). */
645
646 #define IBNAL_WID_TX    0
647 #define IBNAL_WID_RX    1
648 #define IBNAL_WID_RDMA  2
649 #define IBNAL_WID_MASK  3UL
650
651 static inline __u64
652 kibnal_ptr2wreqid (void *ptr, int type)
653 {
654         unsigned long lptr = (unsigned long)ptr;
655
656         LASSERT ((lptr & IBNAL_WID_MASK) == 0);
657         LASSERT ((type & ~IBNAL_WID_MASK) == 0);
658         return (__u64)(lptr | type);
659 }
660
661 static inline void *
662 kibnal_wreqid2ptr (__u64 wreqid)
663 {
664         return (void *)(((unsigned long)wreqid) & ~IBNAL_WID_MASK);
665 }
666
667 static inline int
668 kibnal_wreqid2type (__u64 wreqid)
669 {
670         return (wreqid & IBNAL_WID_MASK);
671 }
672
673 static inline void
674 kibnal_set_conn_state (kib_conn_t *conn, int state)
675 {
676         CDEBUG(D_NET,"%p state %d\n", conn, state);
677         conn->ibc_state = state;
678         mb();
679 }
680
681 #if IBNAL_USE_FMR
682
683 static inline int
684 kibnal_rd_size (kib_rdma_desc_t *rd) 
685 {
686         return rd->rd_nob;
687 }
688
689 #else
690 static inline int
691 kibnal_rd_size (kib_rdma_desc_t *rd)
692 {
693         int   i;
694         int   size;
695         
696         for (i = size = 0; i < rd->rd_nfrag; i++)
697                 size += rd->rd_frags[i].rf_nob;
698         
699         return size;
700 }
701 #endif
702
703 int  kibnal_startup (lnet_ni_t *ni);
704 void kibnal_shutdown (lnet_ni_t *ni);
705 int  kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
706 int  kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
707 int  kibnal_eager_recv (lnet_ni_t *ni, void *private, 
708                         lnet_msg_t *lntmsg, void **new_private);
709 int  kibnal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg,
710                   int delayed, unsigned int niov,
711                   struct iovec *iov, lnet_kiov_t *kiov,
712                   unsigned int offset, unsigned int mlen, unsigned int rlen);
713 void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
714 void kibnal_pack_msg(kib_msg_t *msg, __u32 version, int credits, 
715                      lnet_nid_t dstnid, __u64 dststamp, __u64 seq);
716 void kibnal_pack_connmsg(kib_msg_t *msg, __u32 version, int nob, int type,
717                          lnet_nid_t dstnid, __u64 dststamp);
718 int  kibnal_unpack_msg(kib_msg_t *msg, __u32 expected_version, int nob);
719 IB_HANDLE kibnal_create_cep(lnet_nid_t nid);
720 int  kibnal_create_peer (kib_peer_t **peerp, lnet_nid_t nid);
721 void kibnal_destroy_peer (kib_peer_t *peer);
722 kib_peer_t *kibnal_find_peer_locked (lnet_nid_t nid);
723 int  kibnal_del_peer (lnet_nid_t nid);
724 void kibnal_peer_alive (kib_peer_t *peer);
725 void kibnal_unlink_peer_locked (kib_peer_t *peer);
726 int  kibnal_add_persistent_peer (lnet_nid_t nid);
727 int  kibnal_close_stale_conns_locked (kib_peer_t *peer,
728                                       __u64 incarnation);
729 int  kibnal_conn_rts(kib_conn_t *conn,
730                      __u32 qpn, __u8 resp_res, __u8 init_depth, __u32 psn);
731 kib_conn_t *kibnal_create_conn (lnet_nid_t nid, int proto_version);
732 void kibnal_destroy_conn (kib_conn_t *conn);
733 void kibnal_listen_callback(IB_HANDLE cep, CM_CONN_INFO *info, void *arg);
734 int  kibnal_alloc_pages (kib_pages_t **pp, int npages);
735 void kibnal_free_pages (kib_pages_t *p);
736 void kibnal_queue_tx (kib_tx_t *tx, kib_conn_t *conn);
737 void kibnal_txlist_done (struct list_head *txlist, int status);
738 int  kibnal_post_receives (kib_conn_t *conn);
739 int  kibnal_init_rdma (kib_tx_t *tx, int type, int nob,
740                        kib_rdma_desc_t *dstrd, __u64 dstcookie);
741 void kibnal_check_sends (kib_conn_t *conn);
742 void kibnal_close_conn_locked (kib_conn_t *conn, int error);
743 int  kibnal_thread_start (int (*fn)(void *arg), void *arg);
744 int  kibnal_scheduler(void *arg);
745 int  kibnal_connd (void *arg);
746 void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
747 void kibnal_close_conn (kib_conn_t *conn, int why);
748 void kibnal_start_active_rdma (int type, int status,
749                                kib_rx_t *rx, lnet_msg_t *lntmsg,
750                                unsigned int niov,
751                                struct iovec *iov, lnet_kiov_t *kiov,
752                                unsigned int offset, unsigned int nob);
753 void kibnal_hca_async_callback (void *hca_arg, IB_EVENT_RECORD *ev);
754 void kibnal_hca_callback (void *hca_arg, void *cq_arg);
755 int  kibnal_tunables_init (void);
756 void kibnal_tunables_fini (void);