Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lnet / klnds / qswlnd / qswlnd.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/klnds/qswlnd/qswlnd.h
35  *
36  * Basic library routines.
37  */
38
39 #ifndef _QSWNAL_H
40 #define _QSWNAL_H
41
42 #include <qsnet/kernel.h>
43 #undef printf                                   /* nasty QSW #define */
44 #ifndef AUTOCONF_INCLUDED
45 #include <linux/config.h>
46 #endif
47 #include <linux/module.h>
48
49 #include <elan/epcomms.h>
50
51 #include <linux/kernel.h>
52 #include <linux/mm.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/buffer_head.h>  /* wait_on_buffer */
57 #include <linux/unistd.h>
58 #include <net/sock.h>
59 #include <linux/uio.h>
60
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63
64 #include <linux/fs.h>
65 #include <linux/file.h>
66 #include <linux/stat.h>
67 #include <linux/list.h>
68 #include <linux/sysctl.h>
69
70 #define DEBUG_SUBSYSTEM S_LND
71
72 #include <libcfs/libcfs.h>
73 #include <lnet/lnet.h>
74 #include <lnet/lib-lnet.h>
75 #include <lnet/lnet-sysctl.h>
76
77 /* fixed constants */
78 #define KQSW_SMALLMSG                  (4<<10)  /* small/large ep receiver breakpoint */
79 #define KQSW_RESCHED                    100     /* # busy loops that forces scheduler to yield */
80
81 #define KQSW_CKSUM                      0       /* enable checksumming (protocol incompatible) */
82
83 /*
84  * derived constants
85  */
86
87 #define KQSW_TX_BUFFER_SIZE     (offsetof(kqswnal_msg_t, \
88                                           kqm_u.immediate.kqim_payload[*kqswnal_tunables.kqn_tx_maxcontig]))
89 /* The pre-allocated tx buffer (hdr + small payload) */
90
91 #define KQSW_NTXMSGPAGES        (btopr(KQSW_TX_BUFFER_SIZE) + 1 + btopr(LNET_MAX_PAYLOAD) + 1)
92 /* Reserve elan address space for pre-allocated and pre-mapped transmit
93  * buffer and a full payload too.  Extra pages allow for page alignment */
94
95 #define KQSW_NRXMSGPAGES_SMALL  (btopr(KQSW_SMALLMSG))
96 /* receive hdr/payload always contiguous and page aligned */
97 #define KQSW_NRXMSGBYTES_SMALL  (KQSW_NRXMSGPAGES_SMALL * PAGE_SIZE)
98
99 #define KQSW_NRXMSGPAGES_LARGE  (btopr(sizeof(lnet_msg_t) + LNET_MAX_PAYLOAD))
100 /* receive hdr/payload always contiguous and page aligned */
101 #define KQSW_NRXMSGBYTES_LARGE  (KQSW_NRXMSGPAGES_LARGE * PAGE_SIZE)
102 /* biggest complete packet we can receive (or transmit) */
103
104 /* Wire messages */
105 /* Remote memory descriptor */
106 typedef struct
107 {
108         __u32            kqrmd_nfrag;           /* # frags */
109         EP_NMD           kqrmd_frag[0];         /* actual frags */
110 } kqswnal_remotemd_t;
111
112 /* Immediate data */
113 typedef struct
114 {
115         lnet_hdr_t       kqim_hdr;              /* LNET header */
116         char             kqim_payload[0];       /* piggy-backed payload */
117 } WIRE_ATTR kqswnal_immediate_msg_t;
118
119 /* RDMA request */
120 typedef struct
121 {
122         lnet_hdr_t          kqrm_hdr;           /* LNET header */
123         kqswnal_remotemd_t  kqrm_rmd;           /* peer's buffer */
124 } WIRE_ATTR kqswnal_rdma_msg_t;
125
126 typedef struct
127 {
128         __u32            kqm_magic;             /* I'm a qswlnd message */
129         __u16            kqm_version;           /* this is my version number */
130         __u16            kqm_type;              /* msg type */
131 #if KQSW_CKSUM
132         __u32            kqm_cksum;             /* crc32 checksum */
133         __u32            kqm_nob;               /* original msg length */
134 #endif
135         union {
136                 kqswnal_immediate_msg_t  immediate;
137                 kqswnal_rdma_msg_t       rdma;
138         } WIRE_ATTR kqm_u;
139 } WIRE_ATTR kqswnal_msg_t;
140
141 #if KQSW_CKSUM                                           /* enable checksums ? */
142 # include <linux/crc32.h>
143 static inline __u32 kqswnal_csum(__u32 crc, unsigned char const *p, size_t len)
144 {
145 #if 1
146         return crc32_le(crc, p, len);
147 #else
148         while (len-- > 0)
149                 crc = ((crc + 0x100) & ~0xff) | ((crc + *p++) & 0xff) ;
150         return crc;
151 #endif
152 }
153 # define QSWLND_PROTO_VERSION         0xbeef
154 #else
155 # define QSWLND_PROTO_VERSION         1
156 #endif
157
158 #define QSWLND_MSG_IMMEDIATE          0
159 #define QSWLND_MSG_RDMA               1
160
161 typedef union {
162         EP_STATUSBLK     ep_statusblk;
163         struct {
164                 __u32       status;
165                 __u32       magic;
166                 __u32       version;
167                 union {
168                         struct {
169                                 __u32    len;
170                                 __u32    cksum;
171                         } WIRE_ATTR get;
172                 } WIRE_ATTR u;
173         } WIRE_ATTR     msg;
174 } kqswnal_rpc_reply_t;
175
176 typedef struct kqswnal_rx
177 {
178         cfs_list_t           krx_list;     /* enqueue -> thread */
179         struct kqswnal_rx   *krx_alloclist;/* stack in kqn_rxds */
180         EP_RCVR             *krx_eprx;     /* port to post receives to */
181         EP_RXD              *krx_rxd;      /* receive descriptor (for repost) */
182         EP_NMD               krx_elanbuffer;/* contiguous Elan buffer */
183         int                  krx_npages;    /* # pages in receive buffer */
184         int                  krx_nob;       /* Number Of Bytes received into buffer */
185         int                  krx_rpc_reply_needed:1; /* peer waiting for EKC RPC reply */
186         int                  krx_state;     /* what this RX is doing */
187         cfs_atomic_t         krx_refcount;  /* how to tell when rpc is done */
188 #if KQSW_CKSUM
189         __u32                krx_cksum;     /* checksum */
190 #endif
191         kqswnal_rpc_reply_t  krx_rpc_reply; /* rpc reply status block */
192         lnet_kiov_t          krx_kiov[KQSW_NRXMSGPAGES_LARGE];/* buffer frags */
193 }  kqswnal_rx_t;
194
195 #define KRX_POSTED       1                      /* receiving */
196 #define KRX_PARSE        2                      /* ready to be parsed */
197 #define KRX_COMPLETING   3                      /* waiting to be completed */
198
199
200 typedef struct kqswnal_tx
201 {
202         cfs_list_t            ktx_list;         /* enqueue idle/active */
203         cfs_list_t            ktx_schedlist;    /* enqueue on scheduler */
204         struct kqswnal_tx    *ktx_alloclist;    /* stack in kqn_txds */
205         unsigned int          ktx_state:7;      /* What I'm doing */
206         unsigned int          ktx_firsttmpfrag:1;  /* ktx_frags[0] is in my ebuffer ? 0 : 1 */
207         __u32                 ktx_basepage;     /* page offset in reserved elan tx vaddrs for mapping pages */
208         int                   ktx_npages;       /* pages reserved for mapping messages */
209         int                   ktx_nmappedpages; /* # pages mapped for current message */
210         int                   ktx_port;         /* destination ep port */
211         lnet_nid_t            ktx_nid;          /* destination node */
212         void                 *ktx_args[3];      /* completion passthru */
213         char                 *ktx_buffer;       /* pre-allocated contiguous buffer for hdr + small payloads */
214         cfs_time_t            ktx_launchtime;   /* when (in jiffies) the
215                                                  * transmit was launched */
216         int                   ktx_status;       /* completion status */
217 #if KQSW_CKSUM
218         __u32                 ktx_cksum;        /* optimized GET payload checksum */
219 #endif
220         /* debug/info fields */
221         pid_t                 ktx_launcher;     /* pid of launching process */
222
223         int                   ktx_nfrag;        /* # message frags */
224         int                   ktx_rail;         /* preferred rail */
225         EP_NMD                ktx_ebuffer;      /* elan mapping of ktx_buffer */
226         EP_NMD                ktx_frags[EP_MAXFRAG];/* elan mapping of msg frags */
227 } kqswnal_tx_t;
228
229 #define KTX_IDLE        0                       /* on kqn_idletxds */
230 #define KTX_SENDING     1                       /* normal send */
231 #define KTX_GETTING     2                       /* sending optimised get */
232 #define KTX_PUTTING     3                       /* sending optimised put */
233 #define KTX_RDMA_FETCH  4                       /* handling optimised put */
234 #define KTX_RDMA_STORE  5                       /* handling optimised get */
235
236 typedef struct
237 {
238         int               *kqn_tx_maxcontig;    /* maximum payload to defrag */
239         int               *kqn_ntxmsgs;         /* # normal tx msgs */
240         int               *kqn_credits;         /* # concurrent sends */
241         int               *kqn_peercredits;     /* # concurrent sends to 1 peer */
242         int               *kqn_nrxmsgs_large;   /* # 'large' rx msgs */
243         int               *kqn_ep_envelopes_large; /* # 'large' rx ep envelopes */
244         int               *kqn_nrxmsgs_small;   /* # 'small' rx msgs */
245         int               *kqn_ep_envelopes_small; /* # 'small' rx ep envelopes */
246         int               *kqn_optimized_puts;  /* optimized PUTs? */
247         int               *kqn_optimized_gets;  /* optimized GETs? */
248 #if KQSW_CKSUM
249         int               *kqn_inject_csum_error; /* # csum errors to inject */
250 #endif
251
252 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
253         cfs_sysctl_table_header_t *kqn_sysctl;  /* sysctl interface */
254 #endif
255 } kqswnal_tunables_t;
256
257 typedef struct
258 {
259         char                 kqn_init;        /* what's been initialised */
260         char                 kqn_shuttingdown;/* I'm trying to shut down */
261         cfs_atomic_t         kqn_nthreads;    /* # threads running */
262         lnet_ni_t           *kqn_ni;          /* _the_ instance of me */
263
264         kqswnal_rx_t        *kqn_rxds;        /* stack of all the receive descriptors */
265         kqswnal_tx_t        *kqn_txds;        /* stack of all the transmit descriptors */
266
267         cfs_list_t           kqn_idletxds;    /* transmit descriptors free to use */
268         cfs_list_t           kqn_activetxds;  /* transmit descriptors being used */
269         cfs_spinlock_t       kqn_idletxd_lock; /* serialise idle txd access */
270         cfs_atomic_t         kqn_pending_txs;/* # transmits being prepped */
271
272         cfs_spinlock_t       kqn_sched_lock; /* serialise packet schedulers */
273         cfs_waitq_t          kqn_sched_waitq;/* scheduler blocks here */
274
275         cfs_list_t           kqn_readyrxds;  /* rxds full of data */
276         cfs_list_t           kqn_donetxds;   /* completed transmits */
277         cfs_list_t           kqn_delayedtxds;/* delayed transmits */
278
279         EP_SYS              *kqn_ep;         /* elan system */
280         EP_NMH              *kqn_ep_tx_nmh;  /* elan reserved tx vaddrs */
281         EP_NMH              *kqn_ep_rx_nmh;  /* elan reserved rx vaddrs */
282         EP_XMTR             *kqn_eptx;       /* elan transmitter */
283         EP_RCVR             *kqn_eprx_small; /* elan receiver (small messages) */
284         EP_RCVR             *kqn_eprx_large; /* elan receiver (large messages) */
285
286         int                  kqn_nnodes;     /* this cluster's size */
287         int                  kqn_elanid;     /* this nodes's elan ID */
288
289         EP_STATUSBLK         kqn_rpc_success;/* preset RPC reply status blocks */
290         EP_STATUSBLK         kqn_rpc_failed;
291         EP_STATUSBLK         kqn_rpc_version;/* reply to future version query */
292         EP_STATUSBLK         kqn_rpc_magic;  /* reply to future version query */
293 }  kqswnal_data_t;
294
295 /* kqn_init state */
296 #define KQN_INIT_NOTHING        0               /* MUST BE ZERO so zeroed state is initialised OK */
297 #define KQN_INIT_DATA           1
298 #define KQN_INIT_ALL            2
299
300 extern kqswnal_tunables_t  kqswnal_tunables;
301 extern kqswnal_data_t      kqswnal_data;
302
303 extern int kqswnal_thread_start (int (*fn)(void *arg), void *arg);
304 extern void kqswnal_rxhandler(EP_RXD *rxd);
305 extern int kqswnal_scheduler (void *);
306 extern void kqswnal_rx_done (kqswnal_rx_t *krx);
307
308 static inline lnet_nid_t
309 kqswnal_elanid2nid (int elanid)
310 {
311         return LNET_MKNID(LNET_NIDNET(kqswnal_data.kqn_ni->ni_nid), elanid);
312 }
313
314 static inline int
315 kqswnal_nid2elanid (lnet_nid_t nid)
316 {
317         __u32 elanid = LNET_NIDADDR(nid);
318
319         /* not in this cluster? */
320         return (elanid >= kqswnal_data.kqn_nnodes) ? -1 : elanid;
321 }
322
323 static inline lnet_nid_t
324 kqswnal_rx_nid(kqswnal_rx_t *krx)
325 {
326         return (kqswnal_elanid2nid(ep_rxd_node(krx->krx_rxd)));
327 }
328
329 static inline int
330 kqswnal_pages_spanned (void *base, int nob)
331 {
332         unsigned long first_page = ((unsigned long)base) >> PAGE_SHIFT;
333         unsigned long last_page  = (((unsigned long)base) + (nob - 1)) >> PAGE_SHIFT;
334
335         LASSERT (last_page >= first_page);      /* can't wrap address space */
336         return (last_page - first_page + 1);
337 }
338
339 static inline void kqswnal_rx_decref (kqswnal_rx_t *krx)
340 {
341         LASSERT (cfs_atomic_read (&krx->krx_refcount) > 0);
342         if (cfs_atomic_dec_and_test (&krx->krx_refcount))
343                 kqswnal_rx_done(krx);
344 }
345
346 int kqswnal_startup (lnet_ni_t *ni);
347 void kqswnal_shutdown (lnet_ni_t *ni);
348 int kqswnal_ctl (lnet_ni_t *ni, unsigned int cmd, void *arg);
349 int kqswnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
350 int kqswnal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
351                  int delayed, unsigned int niov, 
352                  struct iovec *iov, lnet_kiov_t *kiov,
353                  unsigned int offset, unsigned int mlen, unsigned int rlen);
354
355 int kqswnal_tunables_init(void);
356 void kqswnal_tunables_fini(void);
357
358 #endif /* _QSWNAL_H */