Whamcloud - gitweb
6ed306ce4019ecd58afef0e9bed7fff9b9407877
[fs/lustre-release.git] / lnet / klnds / openiblnd / openiblnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *   Author: Eric Barton <eric@bartonsoftware.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
37 #include <linux/uio.h>
38
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <linux/kmod.h>
49 #include <linux/sysctl.h>
50
51 #include <net/sock.h>
52 #include <linux/in.h>
53
54 #define DEBUG_SUBSYSTEM S_LND
55
56 #include <libcfs/kp30.h>
57 #include <lnet/lnet.h>
58 #include <lnet/lib-lnet.h>
59
60 #include <ts_ib_core.h>
61 #include <ts_ib_cm.h>
62 #include <ts_ib_sa_client.h>
63
64 #ifndef USING_TSAPI
65
66 /* OpenIB Gen1 */
67 typedef struct ib_qp       ib_qp_t;
68 typedef struct ib_mr       ib_mr_t;
69 typedef struct ib_fmr      ib_fmr_t;
70 typedef struct ib_pd       ib_pd_t;
71 typedef struct ib_cq       ib_cq_t;
72 typedef struct ib_fmr_pool ib_fmr_pool_t;
73
74 #else
75
76 /* Cisco (topspin) */
77 typedef void                 ib_qp_t;
78 typedef void                 ib_mr_t;
79 typedef void                 ib_fmr_t;
80 typedef void                 ib_pd_t;
81 typedef void                 ib_cq_t;
82 typedef void                 ib_fmr_pool_t;
83
84 #define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
85 #define IB_WQ_SIGNAL_SELECTABLE            TS_IB_ACCESS_LOCAL_WRITE
86 #define IB_TRANSPORT_RC                    TS_IB_TRANSPORT_RC
87 #define IB_QP_STATE_INIT                   TS_IB_QP_STATE_INIT
88 #define IB_QP_ATTRIBUTE_STATE              TS_IB_QP_ATTRIBUTE_STATE
89 #define IB_QP_ATTRIBUTE_PORT               TS_IB_QP_ATTRIBUTE_PORT
90 #define IB_QP_ATTRIBUTE_PKEY_INDEX         TS_IB_QP_ATTRIBUTE_PKEY_INDEX
91 #define IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE
92 #define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
93 #define IB_ACCESS_REMOTE_WRITE             TS_IB_ACCESS_REMOTE_WRITE
94 #define IB_ACCESS_REMOTE_READ              TS_IB_ACCESS_REMOTE_READ
95 #define IB_CQ_CALLBACK_INTERRU             TS_IB_CQ_CALLBACK_INTERRUPTPT
96 #define IB_CQ_PROVIDER_REARM               TS_IB_CQ_PROVIDER_REARM
97 #define IB_CQ_CALLBACK_INTERRUPT           TS_IB_CQ_CALLBACK_INTERRUPT
98 #define IB_COMPLETION_STATUS_SUCCESS       TS_IB_COMPLETION_STATUS_SUCCESS
99 #define IB_OP_SEND                         TS_IB_OP_SEND
100 #define IB_OP_RDMA_WRITE                   TS_IB_OP_RDMA_WRITE
101 #define IB_OP_RDMA_READ                    TS_IB_OP_RDMA_READ
102
103 #endif
104
105 #if CONFIG_SMP
106 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
107 #else
108 # define IBNAL_N_SCHED      1                   /* # schedulers */
109 #endif
110
111 #define IBNAL_FMR                    1
112 //#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_PROCESS
113 #define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_INTERRUPT
114
115
116 /* tunables fixed at compile time */
117 #define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
118 #define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
119 #define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
120 #define IBNAL_CREDIT_HIGHWATER       6          /* when to eagerly return credits */
121 #define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
122 #define IBNAL_RDMA_BASE              0x0eeb0000
123
124 /* QP tunables */
125 #define IBNAL_RETRY                  7          /* # times to retry */
126 #define IBNAL_RNR_RETRY              7          /*  */
127 #define IBNAL_CM_RETRY               7          /* # times to retry connection */
128 #define IBNAL_FLOW_CONTROL           1
129 #define IBNAL_RESPONDER_RESOURCES    8
130
131 /************************/
132 /* derived constants... */
133
134 /* TX messages (shared by all connections) */
135 #define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
136 #define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
137 #define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
138
139 /* RX messages (per connection) */
140 #define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE * 2)
141 #define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
142 #define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
143
144 /* we may have up to 2 completions per transmit +
145    1 completion per receive, per connection */
146 #define IBNAL_CQ_ENTRIES()  ((2*IBNAL_TX_MSGS()) +                                      \
147                              (IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers))
148
149 typedef struct
150 {
151         char    **kib_ipif_basename;            /* IPoIB interface base name */
152         int      *kib_n_connd;                  /* # connection daemons */
153         int      *kib_min_reconnect_interval;   /* min connect retry seconds... */
154         int      *kib_max_reconnect_interval;   /* max connect retry seconds */
155         int      *kib_concurrent_peers;         /* max # peers */
156         int      *kib_cksum;                    /* checksum kib_msg_t? */
157         int      *kib_timeout;                  /* comms timeout (seconds) */
158         int      *kib_keepalive;                /* keepalive (seconds) */
159         int      *kib_ntx;                      /* # tx descs */
160         int      *kib_credits;                  /* # concurrent sends */
161         int      *kib_peercredits;              /* # concurrent sends to 1 peer */
162
163         struct ctl_table_header *kib_sysctl;    /* sysctl interface */
164 } kib_tunables_t;
165
166 typedef struct
167 {
168         int               ibp_npages;           /* # pages */
169         int               ibp_mapped;           /* mapped? */
170         __u64             ibp_vaddr;            /* mapped region vaddr */
171         __u32             ibp_lkey;             /* mapped region lkey */
172         __u32             ibp_rkey;             /* mapped region rkey */
173         ib_mr_t          *ibp_handle;           /* mapped region handle */
174         struct page      *ibp_pages[0];
175 } kib_pages_t;
176
177 typedef struct
178 {
179         int               kib_init;             /* initialisation state */
180         __u64             kib_incarnation;      /* which one am I */
181         int               kib_shutdown;         /* shut down? */
182         atomic_t          kib_nthreads;         /* # live threads */
183         lnet_ni_t        *kib_ni;               /* _the_ openib interface */
184
185         __u64             kib_svc_id;           /* service number I listen on */
186         tTS_IB_GID        kib_svc_gid;          /* device/port GID */
187         __u16             kib_svc_pkey;         /* device/port pkey */
188         
189         void             *kib_listen_handle;    /* IB listen handle */
190         
191         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
192
193         struct list_head *kib_peers;            /* hash table of all my known peers */
194         int               kib_peer_hash_size;   /* size of kib_peers */
195         int               kib_nonewpeers;       /* prevent new peers? */
196         atomic_t          kib_npeers;           /* # peers extant */
197         atomic_t          kib_nconns;           /* # connections extant */
198
199         struct list_head  kib_reaper_conns;     /* connections to reap */
200         wait_queue_head_t kib_reaper_waitq;     /* reaper sleeps here */
201         unsigned long     kib_reaper_waketime;  /* when reaper will wake */
202         spinlock_t        kib_reaper_lock;      /* serialise */
203
204         struct list_head  kib_connd_peers;      /* peers waiting for a connection */
205         struct list_head  kib_connd_acceptq;    /* accepted sockets to handle */
206         wait_queue_head_t kib_connd_waitq;      /* connection daemons sleep here */
207         int               kib_connd_connecting; /* # connds connecting */
208         spinlock_t        kib_connd_lock;       /* serialise */
209
210         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
211         struct list_head  kib_sched_txq;        /* tx requiring attention */
212         struct list_head  kib_sched_rxq;        /* rx requiring attention */
213         spinlock_t        kib_sched_lock;       /* serialise */
214
215         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
216         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
217
218         struct list_head  kib_idle_txs;         /* idle tx descriptors */
219         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
220         spinlock_t        kib_tx_lock;          /* serialise */
221
222         int               kib_hca_idx;          /* my HCA number */
223         struct ib_device *kib_device;           /* "the" device */
224         struct ib_device_properties kib_device_props; /* its properties */
225         int               kib_port;             /* port on the device */
226         struct ib_port_properties kib_port_props; /* its properties */
227         ib_pd_t          *kib_pd;               /* protection domain */
228 #if IBNAL_FMR
229         ib_fmr_pool_t    *kib_fmr_pool;         /* fast memory region pool */
230 #endif
231         ib_cq_t          *kib_cq;               /* completion queue */
232
233 } kib_data_t;
234
235 #define IBNAL_INIT_NOTHING         0
236 #define IBNAL_INIT_DATA            1
237 #define IBNAL_INIT_LIB             2
238 #define IBNAL_INIT_PD              3
239 #define IBNAL_INIT_FMR             4
240 #define IBNAL_INIT_TXD             5
241 #define IBNAL_INIT_CQ              6
242 #define IBNAL_INIT_ALL             7
243
244 typedef struct kib_acceptsock                   /* accepted socket queued for connd */
245 {
246         struct list_head     ibas_list;         /* queue for attention */
247         struct socket       *ibas_sock;         /* the accepted socket */
248 } kib_acceptsock_t;
249
250 /************************************************************************
251  * IB Wire message format.
252  * These are sent in sender's byte order (i.e. receiver flips).
253  * They may be sent via TCP/IP (service ID,GID,PKEY query/response),
254  * as private data in the connection request/response, or "normally".
255  */
256
257 typedef struct kib_svcrsp                       /* service response */
258 {
259         __u64             ibsr_svc_id;          /* service's id */
260         __u8              ibsr_svc_gid[16];     /* service's gid */
261         __u16             ibsr_svc_pkey;        /* service's pkey */
262 } WIRE_ATTR kib_svcrsp_t;
263
264 typedef struct kib_connparams
265 {
266         __u32             ibcp_queue_depth;
267 } WIRE_ATTR kib_connparams_t;
268
269 typedef struct
270 {
271         union {
272                 ib_mr_t         *mr;
273                 ib_fmr_t        *fmr;
274         }                 md_handle;
275         __u32             md_lkey;
276         __u32             md_rkey;
277         __u64             md_addr;
278 } kib_md_t;
279
280 typedef struct
281 {
282         __u32             rd_key;               /* remote key */
283         __u32             rd_nob;               /* # of bytes */
284         __u64             rd_addr;              /* remote io vaddr */
285 } WIRE_ATTR kib_rdma_desc_t;
286
287 typedef struct
288 {
289         lnet_hdr_t        ibim_hdr;             /* portals header */
290         char              ibim_payload[0];      /* piggy-backed payload */
291 } WIRE_ATTR kib_immediate_msg_t;
292
293 typedef struct
294 {
295         lnet_hdr_t        ibrm_hdr;             /* portals header */
296         __u64             ibrm_cookie;          /* opaque completion cookie */
297         kib_rdma_desc_t   ibrm_desc;            /* where to suck/blow */
298 } WIRE_ATTR kib_rdma_msg_t;
299
300 typedef struct
301 {
302         __u64             ibcm_cookie;          /* opaque completion cookie */
303         __u32             ibcm_status;          /* completion status */
304 } WIRE_ATTR kib_completion_msg_t;
305
306 typedef struct
307 {
308         /* First 2 fields fixed FOR ALL TIME */
309         __u32             ibm_magic;            /* I'm an openibnal message */
310         __u16             ibm_version;          /* this is my version number */
311
312         __u8              ibm_type;             /* msg type */
313         __u8              ibm_credits;          /* returned credits */
314         __u32             ibm_nob;              /* # bytes in whole message */
315         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
316         __u64             ibm_srcnid;           /* sender's NID */
317         __u64             ibm_srcstamp;         /* sender's incarnation */
318         __u64             ibm_dstnid;           /* destination's NID */
319         __u64             ibm_dststamp;         /* destination's incarnation */
320         union {
321                 kib_svcrsp_t          svcrsp;
322                 kib_connparams_t      connparams;
323                 kib_immediate_msg_t   immediate;
324                 kib_rdma_msg_t        rdma;
325                 kib_completion_msg_t  completion;
326         } WIRE_ATTR       ibm_u;
327 } WIRE_ATTR kib_msg_t;
328
329 #define IBNAL_MSG_MAGIC LNET_PROTO_OPENIB_MAGIC /* unique magic */
330 #define IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD 2   /* previous protocol version */
331 #define IBNAL_MSG_VERSION              3        /* current protocol version */
332
333 #define IBNAL_MSG_SVCQRY            0xb0        /* service query */
334 #define IBNAL_MSG_SVCRSP            0xb1        /* service response */
335 #define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
336 #define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
337 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
338 #define IBNAL_MSG_IMMEDIATE         0xd1        /* portals hdr + payload */
339 #define IBNAL_MSG_PUT_RDMA          0xd2        /* portals PUT hdr + source rdma desc */
340 #define IBNAL_MSG_PUT_DONE          0xd3        /* signal PUT rdma completion */
341 #define IBNAL_MSG_GET_RDMA          0xd4        /* portals GET hdr + sink rdma desc */
342 #define IBNAL_MSG_GET_DONE          0xd5        /* signal GET rdma completion */
343
344 /***********************************************************************/
345
346 typedef struct kib_rx                           /* receive message */
347 {
348         struct list_head          rx_list;      /* queue for attention */
349         struct kib_conn          *rx_conn;      /* owning conn */
350         int                       rx_nob;       /* # bytes received (-1 while posted) */
351         __u64                     rx_vaddr;     /* pre-mapped buffer (hca vaddr) */
352         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
353         struct ib_receive_param   rx_sp;        /* receive work item */
354         struct ib_gather_scatter  rx_gl;        /* and it's memory */
355 } kib_rx_t;
356
357 typedef struct kib_tx                           /* transmit message */
358 {
359         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
360         struct kib_conn          *tx_conn;      /* owning conn */
361         int                       tx_mapped;    /* mapped for RDMA? */
362         int                       tx_sending;   /* # tx callbacks outstanding */
363         int                       tx_status;    /* completion status */
364         unsigned long             tx_deadline;  /* completion deadline */
365         int                       tx_passive_rdma; /* peer sucks/blows */
366         int                       tx_passive_rdma_wait; /* waiting for peer to complete */
367         __u64                     tx_passive_rdma_cookie; /* completion cookie */
368         lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
369         kib_md_t                  tx_md;        /* RDMA mapping (active/passive) */
370         __u64                     tx_vaddr;     /* pre-mapped buffer (hca vaddr) */
371         kib_msg_t                *tx_msg;       /* pre-mapped buffer (host vaddr) */
372         int                       tx_nsp;       /* # send work items */
373         struct ib_send_param      tx_sp[2];     /* send work items... */
374         struct ib_gather_scatter  tx_gl[2];     /* ...and their memory */
375 } kib_tx_t;
376
377 #define KIB_TX_UNMAPPED       0
378 #define KIB_TX_MAPPED         1
379 #define KIB_TX_MAPPED_FMR     2
380
381 typedef struct kib_connreq
382 {
383         /* active connection-in-progress state */
384         struct kib_conn           *cr_conn;
385         kib_msg_t                  cr_msg;
386         __u64                      cr_tid;
387         tTS_IB_GID                 cr_gid;
388         kib_svcrsp_t               cr_svcrsp;
389         struct ib_path_record      cr_path;
390         struct ib_cm_active_param  cr_connparam;
391 } kib_connreq_t;
392
393 typedef struct kib_conn
394 {
395         struct kib_peer    *ibc_peer;           /* owning peer */
396         struct list_head    ibc_list;           /* stash on peer's conn list */
397         __u64               ibc_incarnation;    /* which instance of the peer */
398         int                 ibc_version;        /* peer protocol version */
399         atomic_t            ibc_refcount;       /* # users */
400         int                 ibc_state;          /* what's happening */
401         int                 ibc_nsends_posted;  /* # uncompleted sends */
402         int                 ibc_credits;        /* # credits I have */
403         int                 ibc_outstanding_credits; /* # credits to return */
404         int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
405         unsigned long       ibc_last_send;      /* time of last send */
406         struct list_head    ibc_tx_queue_nocred; /* sends that don't need a credit */
407         struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
408         struct list_head    ibc_tx_queue;       /* send queue */
409         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
410         spinlock_t          ibc_lock;           /* serialise */
411         kib_rx_t           *ibc_rxs;            /* the rx descs */
412         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
413         ib_qp_t            *ibc_qp;             /* queue pair */
414         __u32               ibc_qpn;            /* queue pair number */
415         tTS_IB_CM_COMM_ID   ibc_comm_id;        /* connection ID? */
416         kib_connreq_t      *ibc_connreq;        /* connection request state */
417 } kib_conn_t;
418
419 #define IBNAL_CONN_INIT_NOTHING      0          /* initial state */
420 #define IBNAL_CONN_INIT_QP           1          /* ibc_qp set up */
421 #define IBNAL_CONN_CONNECTING        2          /* started to connect */
422 #define IBNAL_CONN_ESTABLISHED       3          /* connection established */
423 #define IBNAL_CONN_DEATHROW          4          /* waiting to be closed */
424 #define IBNAL_CONN_ZOMBIE            5          /* waiting to be freed */
425
426 typedef struct kib_peer
427 {
428         struct list_head    ibp_list;           /* stash on global peer list */
429         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
430         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
431         __u32               ibp_ip;             /* IP to query for peer conn params */
432         int                 ibp_port;           /* port to qery for peer conn params */
433         __u64               ibp_incarnation;    /* peer's incarnation */
434         atomic_t            ibp_refcount;       /* # users */
435         int                 ibp_persistence;    /* "known" peer refs */
436         struct list_head    ibp_conns;          /* all active connections */
437         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
438         int                 ibp_connecting;     /* current active connection attempts */
439         int                 ibp_accepting;      /* current passive connection attempts */
440         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
441         unsigned long       ibp_reconnect_interval; /* exponential backoff */
442         int                 ibp_error;          /* errno on closing this peer */
443         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
444 } kib_peer_t;
445
446 extern kib_data_t      kibnal_data;
447 extern kib_tunables_t  kibnal_tunables;
448
449 /******************************************************************************/
450
451 /* these are purposely avoiding using local vars so they don't increase
452  * stack consumption. */
453
454 #define kibnal_conn_addref(conn)                                \
455 do {                                                            \
456         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
457                (conn), atomic_read(&(conn)->ibc_refcount));     \
458         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
459         atomic_inc(&(conn)->ibc_refcount);                      \
460 } while (0)
461
462 #define kibnal_conn_decref(conn)                                              \
463 do {                                                                          \
464         unsigned long   flags;                                                \
465                                                                               \
466         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
467                (conn), atomic_read(&(conn)->ibc_refcount));                   \
468         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
469         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
470                 spin_lock_irqsave(&kibnal_data.kib_reaper_lock, flags);       \
471                 list_add_tail(&(conn)->ibc_list,                              \
472                               &kibnal_data.kib_reaper_conns);                 \
473                 wake_up(&kibnal_data.kib_reaper_waitq);                       \
474                 spin_unlock_irqrestore(&kibnal_data.kib_reaper_lock, flags);  \
475         }                                                                     \
476 } while (0)
477
478 #define kibnal_peer_addref(peer)                                \
479 do {                                                            \
480         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
481                (peer), libcfs_nid2str((peer)->ibp_nid),         \
482                atomic_read (&(peer)->ibp_refcount));            \
483         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
484         atomic_inc(&(peer)->ibp_refcount);                      \
485 } while (0)
486
487 #define kibnal_peer_decref(peer)                                \
488 do {                                                            \
489         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
490                (peer), libcfs_nid2str((peer)->ibp_nid),         \
491                atomic_read (&(peer)->ibp_refcount));            \
492         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
493         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
494                 kibnal_destroy_peer(peer);                      \
495 } while (0)
496
497 /******************************************************************************/
498
499 static inline struct list_head *
500 kibnal_nid2peerlist (lnet_nid_t nid)
501 {
502         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
503
504         return (&kibnal_data.kib_peers [hash]);
505 }
506
507 static inline int
508 kibnal_peer_active(kib_peer_t *peer)
509 {
510         /* Am I in the peer hash table? */
511         return (!list_empty(&peer->ibp_list));
512 }
513
514 static inline void
515 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
516 {
517         struct list_head      *q;
518
519         LASSERT (tx->tx_nsp > 0);               /* work items set up */
520         LASSERT (tx->tx_conn == NULL);          /* only set here */
521
522         kibnal_conn_addref(conn);
523         tx->tx_conn = conn;
524         tx->tx_deadline = jiffies + *kibnal_tunables.kib_timeout * HZ;
525
526         if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
527                 /* All messages have simple credit control */
528                 q = &conn->ibc_tx_queue;
529         } else {
530                 LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
531                 
532                 switch (tx->tx_msg->ibm_type) {
533                 case IBNAL_MSG_PUT_RDMA:
534                 case IBNAL_MSG_GET_RDMA:
535                         /* RDMA request: reserve a buffer for the RDMA reply
536                          * before sending */
537                         q = &conn->ibc_tx_queue_rsrvd;
538                         break;
539
540                 case IBNAL_MSG_PUT_DONE:
541                 case IBNAL_MSG_GET_DONE:
542                         /* RDMA completion: no credits; peer has reserved a
543                          * reply buffer */
544                         q = &conn->ibc_tx_queue_nocred;
545                         break;
546                 
547                 case IBNAL_MSG_NOOP:
548                 case IBNAL_MSG_IMMEDIATE:
549                         /* Otherwise: consume a credit before sending */
550                         q = &conn->ibc_tx_queue;
551                         break;
552                 
553                 default:
554                         LBUG();
555                         q = NULL;
556                 }
557         }
558
559         list_add_tail(&tx->tx_list, q);
560 }
561
562 static inline int
563 kibnal_send_keepalive(kib_conn_t *conn) 
564 {
565         return (*kibnal_tunables.kib_keepalive > 0) &&
566                 time_after(jiffies, conn->ibc_last_send +
567                            *kibnal_tunables.kib_keepalive*HZ);
568 }
569
570 /* CAVEAT EMPTOR:
571  * We rely on tx/rx descriptor alignment to allow us to use the lowest bit
572  * of the work request id as a flag to determine if the completion is for a
573  * transmit or a receive.  It seems that that the CQ entry's 'op' field
574  * isn't always set correctly on completions that occur after QP teardown. */
575
576 static inline __u64
577 kibnal_ptr2wreqid (void *ptr, int isrx)
578 {
579         unsigned long lptr = (unsigned long)ptr;
580
581         LASSERT ((lptr & 1) == 0);
582         return (__u64)(lptr | (isrx ? 1 : 0));
583 }
584
585 static inline void *
586 kibnal_wreqid2ptr (__u64 wreqid)
587 {
588         return (void *)(((unsigned long)wreqid) & ~1UL);
589 }
590
591 static inline int
592 kibnal_wreqid_is_rx (__u64 wreqid)
593 {
594         return (wreqid & 1) != 0;
595 }
596
597 #if (IB_NTXRXPARAMS == 3)
598 static inline int
599 kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
600 {
601         return ib_send(qp, p, 1);
602 }
603
604 static inline int
605 kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
606 {
607         return ib_receive(qp, p, 1);
608 }
609 #elif (IB_NTXRXPARAMS == 4)
610 static inline int
611 kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
612 {
613         return ib_send(qp, p, 1, NULL);
614 }
615
616 static inline int
617 kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
618 {
619         return ib_receive(qp, p, 1, NULL);
620 }
621 #else
622  #error "IB_NTXRXPARAMS not set correctly"
623 #endif
624
625 int kibnal_startup (lnet_ni_t *ni);
626 void kibnal_shutdown (lnet_ni_t *ni);
627 int kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
628 int kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
629 int kibnal_eager_recv (lnet_ni_t *ni, void *private, 
630                        lnet_msg_t *lntmsg, void **new_private);
631 int kibnal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
632                 int delayed, unsigned int niov, 
633                 struct iovec *iov, lnet_kiov_t *kiov,
634                 unsigned int offset, unsigned int mlen, unsigned int rlen);
635 int kibnal_accept(lnet_ni_t *ni, struct socket *sock);
636
637 extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
638 extern void kibnal_pack_msg(kib_msg_t *msg, int version, int credits, 
639                             lnet_nid_t dstnid, __u64 dststamp);
640 extern int kibnal_unpack_msg(kib_msg_t *msg, int expected_version, int nob);
641 extern void kibnal_handle_svcqry (struct socket *sock);
642 extern int kibnal_make_svcqry (kib_conn_t *conn);
643 extern void kibnal_free_acceptsock (kib_acceptsock_t *as);
644 extern int kibnal_create_peer (kib_peer_t **peerp, lnet_nid_t nid);
645 extern void kibnal_destroy_peer (kib_peer_t *peer);
646 extern int kibnal_add_persistent_peer(lnet_nid_t nid, __u32 ip, int port);
647 extern int kibnal_del_peer (lnet_nid_t nid);
648 extern kib_peer_t *kibnal_find_peer_locked (lnet_nid_t nid);
649 extern void kibnal_unlink_peer_locked (kib_peer_t *peer);
650 extern void kibnal_peer_alive(kib_peer_t *peer);
651 extern int  kibnal_close_stale_conns_locked (kib_peer_t *peer,
652                                               __u64 incarnation);
653 extern kib_conn_t *kibnal_create_conn (void);
654 extern void kibnal_destroy_conn (kib_conn_t *conn);
655 extern int kibnal_alloc_pages (kib_pages_t **pp, int npages, int access);
656 extern void kibnal_free_pages (kib_pages_t *p);
657
658 extern void kibnal_check_sends (kib_conn_t *conn);
659
660 extern tTS_IB_CM_CALLBACK_RETURN
661 kibnal_bad_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
662                           void *param, void *arg);
663 extern tTS_IB_CM_CALLBACK_RETURN
664 kibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
665                        void *param, void *arg);
666 extern tTS_IB_CM_CALLBACK_RETURN
667 kibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
668                                void *param, void *arg);
669
670 extern void kibnal_close_conn_locked (kib_conn_t *conn, int error);
671 extern void kibnal_destroy_conn (kib_conn_t *conn);
672 extern int  kibnal_thread_start (int (*fn)(void *arg), void *arg);
673 extern int  kibnal_scheduler(void *arg);
674 extern int  kibnal_connd (void *arg);
675 extern int  kibnal_reaper (void *arg);
676 extern void kibnal_callback (ib_cq_t *cq, struct ib_cq_entry *e, void *arg);
677 extern void kibnal_txlist_done (struct list_head *txlist, int status);
678 extern void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
679 extern int  kibnal_close_conn (kib_conn_t *conn, int why);
680 extern void kibnal_start_active_rdma (int type, int status,
681                                       kib_rx_t *rx, lnet_msg_t *lntmsg,
682                                       unsigned int niov,
683                                       struct iovec *iov, lnet_kiov_t *kiov,
684                                       int offset, int nob);
685
686 extern int  kibnal_tunables_init(void);
687 extern void kibnal_tunables_fini(void);