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