Whamcloud - gitweb
* GMNAL
[fs/lustre-release.git] / lnet / klnds / gmlnd / gmlnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Los Alamos National Laboratory (LANL)
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
22
23 /*
24  *      Portals GM kernel NAL header file
25  *      This file makes all declaration and prototypes 
26  *      for the API side and CB side of the NAL
27  */
28 #ifndef __INCLUDE_GMNAL_H__
29 #define __INCLUDE_GMNAL_H__
30
31 /* XXX Lustre as of V1.2.2 drop defines VERSION, which causes problems
32  * when including <GM>/include/gm_lanai.h which defines a structure field
33  * with the name VERSION XXX */
34 #ifdef VERSION
35 # undef VERSION
36 #endif
37
38 #ifndef EXPORT_SYMTAB
39 # define EXPORT_SYMTAB
40 #endif
41
42 #include "linux/config.h"
43 #include "linux/module.h"
44 #include "linux/tty.h"
45 #include "linux/kernel.h"
46 #include "linux/mm.h"
47 #include "linux/string.h"
48 #include "linux/stat.h"
49 #include "linux/errno.h"
50 #include "linux/version.h"
51 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
52 #include "linux/buffer_head.h"
53 #include "linux/fs.h"
54 #else
55 #include "linux/locks.h"
56 #endif
57 #include "linux/unistd.h"
58 #include "linux/init.h"
59 #include "linux/sem.h"
60 #include "linux/vmalloc.h"
61 #include "linux/sysctl.h"
62
63 #define DEBUG_SUBSYSTEM S_NAL
64
65 #include "portals/nal.h"
66 #include "portals/api.h"
67 #include "portals/errno.h"
68 #include "libcfs/kp30.h"
69 #include "portals/p30.h"
70
71 #include "portals/nal.h"
72 #include "portals/lib-p30.h"
73
74 /* undefine these before including the GM headers which clash */
75 #undef PACKAGE_BUGREPORT
76 #undef PACKAGE_NAME
77 #undef PACKAGE_STRING
78 #undef PACKAGE_TARNAME
79 #undef PACKAGE_VERSION
80
81 #define GM_STRONG_TYPES 1
82 #ifdef VERSION
83 #undef VERSION
84 #endif
85 #include "gm.h"
86 #include "gm_internal.h"
87
88
89
90 /*
91  *      Defines for the API NAL
92  */
93
94 /*
95  *      Small message size is configurable
96  *      insmod can set small_msg_size
97  *      which is used to populate nal_data.small_msg_size
98  */
99 #define GMNAL_MAGIC                     0x1234abcd
100
101 #define GMNAL_SMALL_MESSAGE             1078
102
103 extern  int num_rx_threads;
104 extern  int num_stxds;
105 extern  int gm_port_id;
106
107 /*
108  *      Small Transmit Descriptor
109  *      A structre to keep track of a small transmit operation
110  *      This structure has a one-to-one relationship with a small
111  *      transmit buffer (both create by gmnal_stxd_alloc). 
112  *      There are two free list of stxd. One for use by clients of the NAL
113  *      and the other by the NAL rxthreads when doing sends. 
114  *      This helps prevent deadlock caused by stxd starvation.
115  */
116 typedef struct gmnal_stxd {
117         struct gmnal_stxd       *tx_next;
118         void                    *tx_buffer;
119         int                      tx_buffer_size;
120         gm_size_t                tx_gm_size;
121         int                      tx_msg_size;
122         int                      tx_gmlid;
123         int                      tx_gm_priority;
124         int                      tx_type;
125         ptl_nid_t                tx_nid;
126         struct gmnal_ni         *tx_gmni;
127         lib_msg_t               *tx_cookie;
128         int                      tx_niov;
129         int                      tx_rxt; 
130         int                      tx_kniov;
131         struct iovec            *tx_iovec_dup;
132         struct iovec             tx_iov[PTL_MD_MAX_IOV];
133 } gmnal_stxd_t;
134
135 /*
136  *      as for gmnal_stxd_t 
137  *      a hash table in nal_data find srxds from
138  *      the rx buffer address. hash table populated at init time
139  */
140 typedef struct gmnal_srxd {
141         void                    *rx_buffer;
142         int                      rx_size;
143         gm_size_t                rx_gmsize;
144         unsigned int             rx_sender_gmid;
145         __u64                    rx_source_stxd;
146         int                      rx_type;
147         int                      rx_nsiov;
148         int                      rx_nriov;
149         struct iovec            *rx_riov;
150         int                      rx_ncallbacks;
151         spinlock_t               rx_callback_lock;
152         int                      rx_callback_status;
153         lib_msg_t               *rx_cookie;
154         struct gmnal_srxd       *rx_next;
155         struct gmnal_ni         *rx_gmni;
156 } gmnal_srxd_t;
157
158 /*
159  *      Header which lmgnal puts at the start of each message
160  *      watch alignment for ia32/64 interaction
161  */
162 typedef struct gmnal_msghdr {
163         __s32           gmm_magic;
164         __s32           gmm_type;
165         __s32           gmm_niov;
166         __u32           gmm_sender_gmid;
167         __u64           gmm_stxd_remote_ptr;
168 } WIRE_ATTR gmnal_msghdr_t;
169
170 /*
171  *      the caretaker thread (ct_thread) gets receive events
172  *      (and other events) from the myrinet device via the GM2 API.
173  *      caretaker thread populates one work entry for each receive event,
174  *      puts it on a Q in nal_data and wakes a receive thread to  
175  *      process the receive.  
176  *      Processing a portals receive can involve a transmit operation. 
177  *      Because of this the caretaker thread cannot process receives 
178  *      as it may get deadlocked when supply of transmit descriptors 
179  *      is exhausted (as caretaker thread is responsible for replacing 
180  *      transmit descriptors on the free list)
181  */
182 typedef struct gmnal_rxtwe {
183         void                    *buffer;
184         unsigned                snode;
185         unsigned                sport;
186         unsigned                type;
187         unsigned                length;
188         struct gmnal_rxtwe      *next;
189 } gmnal_rxtwe_t;
190
191 /*
192  *      1 receive thread started on each CPU
193  */
194 #define NRXTHREADS 10 /* max number of receiver threads */
195
196 typedef struct gmnal_ni {
197         spinlock_t       gmni_stxd_lock;
198         struct semaphore gmni_stxd_token;
199         gmnal_stxd_t    *gmni_stxd;
200         spinlock_t       gmni_rxt_stxd_lock;
201         struct semaphore gmni_rxt_stxd_token;
202         gmnal_stxd_t    *gmni_rxt_stxd;
203         gmnal_srxd_t    *gmni_srxd;
204         struct gm_hash  *gmni_srxd_hash;
205         nal_t           *gmni_nal;      
206         lib_nal_t       *gmni_libnal;
207         struct gm_port  *gmni_port;
208         __u32            gmni_local_gmid;
209         __u32            gmni_global_gmid;
210         spinlock_t       gmni_gm_lock;          /* serialise GM calls */
211         long             gmni_rxthread_pid[NRXTHREADS];
212         int              gmni_rxthread_stop_flag;
213         spinlock_t       gmni_rxthread_flag_lock;
214         long             gmni_rxthread_flag;
215         long             gmni_ctthread_pid;
216         int              gmni_ctthread_flag;
217         gm_alarm_t       gmni_ctthread_alarm;
218         int              gmni_small_msg_size;
219         int              gmni_small_msg_gmsize;
220         gmnal_rxtwe_t   *gmni_rxtwe_head;
221         gmnal_rxtwe_t   *gmni_rxtwe_tail;
222         spinlock_t       gmni_rxtwe_lock;
223         struct semaphore gmni_rxtwe_wait;
224 } gmnal_ni_t;
225
226 /*
227  *      Flags to start/stop and check status of threads
228  *      each rxthread sets 1 bit (any bit) of the flag on startup
229  *      and clears 1 bit when exiting
230  */
231 #define GMNAL_THREAD_RESET      0
232 #define GMNAL_THREAD_STOP       666
233 #define GMNAL_CTTHREAD_STARTED  333
234 #define GMNAL_RXTHREADS_STARTED ( (1<<num_rx_threads)-1)
235
236
237 /*
238  * for ioctl get pid
239  */
240 #define GMNAL_IOC_GET_GNID 1    
241
242 /*
243  *      FUNCTION PROTOTYPES
244  */
245
246 /*
247  *      API NAL
248  */
249 int gmnal_api_startup(nal_t *, ptl_pid_t, 
250                       ptl_ni_limits_t *, ptl_ni_limits_t *);
251
252 int gmnal_api_forward(nal_t *, int, void *, size_t, void *, size_t);
253
254 void gmnal_api_shutdown(nal_t *);
255
256 int gmnal_api_validate(nal_t *, void *, size_t);
257
258 void gmnal_api_yield(nal_t *, unsigned long *, int);
259
260 void gmnal_api_lock(nal_t *, unsigned long *);
261
262 void gmnal_api_unlock(nal_t *, unsigned long *);
263
264
265 /*
266  *      CB NAL
267  */
268
269 ptl_err_t gmnal_cb_send(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *,
270         int, ptl_nid_t, ptl_pid_t, unsigned int, struct iovec *, size_t, size_t);
271
272 ptl_err_t gmnal_cb_send_pages(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *,
273         int, ptl_nid_t, ptl_pid_t, unsigned int, ptl_kiov_t *, size_t, size_t);
274
275 ptl_err_t gmnal_cb_recv(lib_nal_t *, void *, lib_msg_t *, 
276         unsigned int, struct iovec *, size_t, size_t, size_t);
277
278 ptl_err_t gmnal_cb_recv_pages(lib_nal_t *, void *, lib_msg_t *, 
279         unsigned int, ptl_kiov_t *, size_t, size_t, size_t);
280
281 int gmnal_cb_dist(lib_nal_t *, ptl_nid_t, unsigned long *);
282
283 int gmnal_init(void);
284
285 void  gmnal_fini(void);
286
287
288 /*
289  *      Small and Large Transmit and Receive Descriptor Functions
290  */
291 int             gmnal_alloc_txd(gmnal_ni_t *);
292 void            gmnal_free_txd(gmnal_ni_t *);
293 gmnal_stxd_t*   gmnal_get_stxd(gmnal_ni_t *, int);
294 void            gmnal_return_stxd(gmnal_ni_t *, gmnal_stxd_t *);
295
296 int             gmnal_alloc_srxd(gmnal_ni_t *);
297 void            gmnal_free_srxd(gmnal_ni_t *);
298
299 /*
300  *      general utility functions
301  */
302 gmnal_srxd_t    *gmnal_rxbuffer_to_srxd(gmnal_ni_t *, void*);
303 void            gmnal_stop_rxthread(gmnal_ni_t *);
304 void            gmnal_stop_ctthread(gmnal_ni_t *);
305 void            gmnal_drop_sends_callback(gm_port_t *, void *, gm_status_t);
306 void            gmnal_resume_sending_callback(gm_port_t *, void *, gm_status_t);
307 char            *gmnal_gm_error(gm_status_t);
308 char            *gmnal_rxevent(gm_recv_event_t*);
309 void            gmnal_yield(int);
310 int             gmnal_start_kernel_threads(gmnal_ni_t *);
311
312
313 /*
314  *      Communication functions
315  */
316
317 /*
318  *      Receive threads
319  */
320 int             gmnal_ct_thread(void *); /* caretaker thread */
321 int             gmnal_rx_thread(void *); /* receive thread */
322 void            gmnal_pre_receive(gmnal_ni_t*, gmnal_rxtwe_t*, int);
323 void            gmnal_rx_bad(gmnal_ni_t *, gmnal_rxtwe_t *);
324 void            gmnal_rx_requeue_buffer(gmnal_ni_t *, gmnal_srxd_t *);
325 int             gmnal_add_rxtwe(gmnal_ni_t *, gm_recv_t *);
326 gmnal_rxtwe_t * gmnal_get_rxtwe(gmnal_ni_t *);
327 void            gmnal_remove_rxtwe(gmnal_ni_t *);
328
329
330 /*
331  *      Small messages
332  */
333 ptl_err_t       gmnal_small_tx(lib_nal_t *libnal, void *private, 
334                                lib_msg_t *cookie, ptl_hdr_t *hdr, 
335                                int type, ptl_nid_t nid, 
336                                gmnal_stxd_t *stxd, int size);
337 void            gmnal_small_tx_callback(gm_port_t *, void *, gm_status_t);
338
339 #endif /*__INCLUDE_GMNAL_H__*/