Whamcloud - gitweb
b=5786
[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/locks.h"
51 #include "linux/unistd.h"
52 #include "linux/init.h"
53 #include "linux/sem.h"
54 #include "linux/vmalloc.h"
55 #include "linux/sysctl.h"
56
57 #define DEBUG_SUBSYSTEM S_NAL
58
59 #include "portals/nal.h"
60 #include "portals/api.h"
61 #include "portals/errno.h"
62 #include "libcfs/kp30.h"
63 #include "portals/p30.h"
64
65 #include "portals/nal.h"
66 #include "portals/lib-p30.h"
67
68 #define GM_STRONG_TYPES 1
69 #ifdef VERSION
70 #undef VERSION
71 #endif
72 #include "gm.h"
73 #include "gm_internal.h"
74
75
76
77 /*
78  *      Defines for the API NAL
79  */
80
81 /*
82  *      Small message size is configurable
83  *      insmod can set small_msg_size
84  *      which is used to populate nal_data.small_msg_size
85  */
86 #define GMNAL_SMALL_MESSAGE             1078
87 #define GMNAL_LARGE_MESSAGE_INIT        1079
88 #define GMNAL_LARGE_MESSAGE_ACK 1080
89 #define GMNAL_LARGE_MESSAGE_FINI        1081
90
91 extern  int gmnal_small_msg_size;
92 extern  int num_rx_threads;
93 extern  int num_stxds;
94 extern  int gm_port_id;
95 #define GMNAL_SMALL_MSG_SIZE(a)         a->small_msg_size
96 #define GMNAL_IS_SMALL_MESSAGE(n,a,b,c) gmnal_is_small_msg(n, a, b, c)
97 #define GMNAL_MAGIC                             0x1234abcd
98 /*
99  *      The gm_port to use for gmnal
100  */
101 #define GMNAL_GM_PORT_ID        gm_port_id
102
103
104 /*
105  *      Small Transmit Descriptor
106  *      A structre to keep track of a small transmit operation
107  *      This structure has a one-to-one relationship with a small
108  *      transmit buffer (both create by gmnal_stxd_alloc). 
109  *      There are two free list of stxd. One for use by clients of the NAL
110  *      and the other by the NAL rxthreads when doing sends. 
111  *      This helps prevent deadlock caused by stxd starvation.
112  */
113 typedef struct _gmnal_stxd_t {
114         void                    *buffer;
115         int                     buffer_size;
116         gm_size_t               gm_size;
117         int                     msg_size;
118         int                     gm_target_node;
119         int                     gm_priority;
120         int                     type;
121         struct _gmnal_data_t    *nal_data;
122         lib_msg_t               *cookie;
123         int                     niov;
124         struct iovec            iov[PTL_MD_MAX_IOV];
125         struct _gmnal_stxd_t    *next;
126         int                     rxt; 
127         int                     kniov;
128         struct iovec            *iovec_dup;
129 } gmnal_stxd_t;
130
131 /*
132  *      keeps a transmit token for large transmit (gm_get)
133  *      and a pointer to rxd that is used as context for large receive
134  */
135 typedef struct _gmnal_ltxd_t {
136         struct _gmnal_ltxd_t    *next;
137         struct  _gmnal_srxd_t  *srxd;
138 } gmnal_ltxd_t;
139
140
141 /*
142  *      as for gmnal_stxd_t 
143  *      a hash table in nal_data find srxds from
144  *      the rx buffer address. hash table populated at init time
145  */
146 typedef struct _gmnal_srxd_t {
147         void                    *buffer;
148         int                     size;
149         gm_size_t               gmsize;
150         unsigned int            gm_source_node;
151         gmnal_stxd_t            *source_stxd;
152         int                     type;
153         int                     nsiov;
154         int                     nriov;
155         struct iovec            *riov;
156         int                     ncallbacks;
157         spinlock_t              callback_lock;
158         int                     callback_status;
159         lib_msg_t               *cookie;
160         struct _gmnal_srxd_t    *next;
161         struct _gmnal_data_t    *nal_data;
162 } gmnal_srxd_t;
163
164 /*
165  *      Header which lmgnal puts at the start of each message
166  *      watch alignment for ia32/64 interaction
167  */
168 typedef struct  _gmnal_msghdr {
169         int             magic;
170         int             type;
171         unsigned int    sender_node_id;
172         int             niov;
173         gm_remote_ptr_t stxd_remote_ptr; /* 64 bits */
174         } gmnal_msghdr_t;
175 #define GMNAL_MSGHDR_SIZE       sizeof(gmnal_msghdr_t)
176
177 /*
178  *      the caretaker thread (ct_thread) gets receive events
179  *      (and other events) from the myrinet device via the GM2 API.
180  *      caretaker thread populates one work entry for each receive event,
181  *      puts it on a Q in nal_data and wakes a receive thread to  
182  *      process the receive.  
183  *      Processing a portals receive can involve a transmit operation. 
184  *      Because of this the caretaker thread cannot process receives 
185  *      as it may get deadlocked when supply of transmit descriptors 
186  *      is exhausted (as caretaker thread is responsible for replacing 
187  *      transmit descriptors on the free list)
188  */
189 typedef struct _gmnal_rxtwe {
190         void                    *buffer;
191         unsigned                snode;
192         unsigned                sport;
193         unsigned                type;
194         unsigned                length;
195         struct _gmnal_rxtwe     *next;
196 } gmnal_rxtwe_t;
197
198 /*
199  *      1 receive thread started on each CPU
200  */
201 #define NRXTHREADS 10 /* max number of receiver threads */
202
203 typedef struct _gmnal_data_t {
204         int             refcnt;
205         spinlock_t      cb_lock;
206         spinlock_t      stxd_lock;
207         struct semaphore stxd_token;
208         gmnal_stxd_t    *stxd;
209         spinlock_t      rxt_stxd_lock;
210         struct semaphore rxt_stxd_token;
211         gmnal_stxd_t    *rxt_stxd;
212         spinlock_t      ltxd_lock;
213         struct semaphore ltxd_token;
214         gmnal_ltxd_t    *ltxd;
215         spinlock_t      srxd_lock;
216         struct semaphore srxd_token;
217         gmnal_srxd_t    *srxd;
218         struct gm_hash  *srxd_hash;
219         nal_t           *nal;   
220         lib_nal_t       *libnal;
221         struct gm_port  *gm_port;
222         unsigned int    gm_local_nid;
223         unsigned int    gm_global_nid;
224         spinlock_t      gm_lock;
225         long            rxthread_pid[NRXTHREADS];
226         int             rxthread_stop_flag;
227         spinlock_t      rxthread_flag_lock;
228         long            rxthread_flag;
229         long            ctthread_pid;
230         int             ctthread_flag;
231         gm_alarm_t      ctthread_alarm;
232         int             small_msg_size;
233         int             small_msg_gmsize;
234         gmnal_rxtwe_t   *rxtwe_head;
235         gmnal_rxtwe_t   *rxtwe_tail;
236         spinlock_t      rxtwe_lock;
237         struct  semaphore rxtwe_wait;
238         struct ctl_table_header *sysctl;
239 } gmnal_data_t;
240
241 /*
242  *      Flags to start/stop and check status of threads
243  *      each rxthread sets 1 bit (any bit) of the flag on startup
244  *      and clears 1 bit when exiting
245  */
246 #define GMNAL_THREAD_RESET      0
247 #define GMNAL_THREAD_STOP       666
248 #define GMNAL_CTTHREAD_STARTED  333
249 #define GMNAL_RXTHREADS_STARTED ( (1<<num_rx_threads)-1)
250
251
252 extern gmnal_data_t     *global_nal_data;
253
254 /*
255  * for ioctl get pid
256  */
257 #define GMNAL_IOC_GET_GNID 1    
258
259 /*
260  *      Return codes
261  */
262 #define GMNAL_STATUS_OK 0
263 #define GMNAL_STATUS_FAIL       1
264 #define GMNAL_STATUS_NOMEM      2
265
266
267 /*
268  *      FUNCTION PROTOTYPES
269  */
270
271 /*
272  *      Locking macros
273  */
274
275 /*
276  *      For the Small tx and rx descriptor lists
277  */
278 #define GMNAL_TXD_LOCK_INIT(a)          spin_lock_init(&a->stxd_lock);
279 #define GMNAL_TXD_LOCK(a)               spin_lock(&a->stxd_lock);
280 #define GMNAL_TXD_UNLOCK(a)             spin_unlock(&a->stxd_lock);
281 #define GMNAL_TXD_TOKEN_INIT(a, n)      sema_init(&a->stxd_token, n);
282 #define GMNAL_TXD_GETTOKEN(a)           down(&a->stxd_token);
283 #define GMNAL_TXD_TRYGETTOKEN(a)        down_trylock(&a->stxd_token)
284 #define GMNAL_TXD_RETURNTOKEN(a)        up(&a->stxd_token);
285
286 #define GMNAL_RXT_TXD_LOCK_INIT(a)      spin_lock_init(&a->rxt_stxd_lock);
287 #define GMNAL_RXT_TXD_LOCK(a)           spin_lock(&a->rxt_stxd_lock);
288 #define GMNAL_RXT_TXD_UNLOCK(a)         spin_unlock(&a->rxt_stxd_lock);
289 #define GMNAL_RXT_TXD_TOKEN_INIT(a, n)  sema_init(&a->rxt_stxd_token, n);
290 #define GMNAL_RXT_TXD_GETTOKEN(a)       down(&a->rxt_stxd_token);
291 #define GMNAL_RXT_TXD_TRYGETTOKEN(a)    down_trylock(&a->rxt_stxd_token)
292 #define GMNAL_RXT_TXD_RETURNTOKEN(a)    up(&a->rxt_stxd_token);
293
294 #define GMNAL_LTXD_LOCK_INIT(a)         spin_lock_init(&a->ltxd_lock);
295 #define GMNAL_LTXD_LOCK(a)              spin_lock(&a->ltxd_lock);
296 #define GMNAL_LTXD_UNLOCK(a)            spin_unlock(&a->ltxd_lock);
297 #define GMNAL_LTXD_TOKEN_INIT(a, n)     sema_init(&a->ltxd_token, n);
298 #define GMNAL_LTXD_GETTOKEN(a)          down(&a->ltxd_token);
299 #define GMNAL_LTXD_TRYGETTOKEN(a)       down_trylock(&a->ltxd_token)
300 #define GMNAL_LTXD_RETURNTOKEN(a)       up(&a->ltxd_token);
301
302 #define GMNAL_RXD_LOCK_INIT(a)          spin_lock_init(&a->srxd_lock);
303 #define GMNAL_RXD_LOCK(a)               spin_lock(&a->srxd_lock);
304 #define GMNAL_RXD_UNLOCK(a)             spin_unlock(&a->srxd_lock);
305 #define GMNAL_RXD_TOKEN_INIT(a, n)      sema_init(&a->srxd_token, n);
306 #define GMNAL_RXD_GETTOKEN(a)           down(&a->srxd_token);
307 #define GMNAL_RXD_TRYGETTOKEN(a)        down_trylock(&a->srxd_token)
308 #define GMNAL_RXD_RETURNTOKEN(a)        up(&a->srxd_token);
309
310 #define GMNAL_GM_LOCK_INIT(a)           spin_lock_init(&a->gm_lock);
311 #define GMNAL_GM_LOCK(a)                spin_lock(&a->gm_lock);
312 #define GMNAL_GM_UNLOCK(a)              spin_unlock(&a->gm_lock);
313 #define GMNAL_CB_LOCK_INIT(a)           spin_lock_init(&a->cb_lock);
314
315
316 /*
317  *      Memory Allocator
318  */
319
320 /*
321  *      API NAL
322  */
323 int gmnal_api_startup(nal_t *, ptl_pid_t, 
324                       ptl_ni_limits_t *, ptl_ni_limits_t *);
325
326 int gmnal_api_forward(nal_t *, int, void *, size_t, void *, size_t);
327
328 void gmnal_api_shutdown(nal_t *);
329
330 int gmnal_api_validate(nal_t *, void *, size_t);
331
332 void gmnal_api_yield(nal_t *, unsigned long *, int);
333
334 void gmnal_api_lock(nal_t *, unsigned long *);
335
336 void gmnal_api_unlock(nal_t *, unsigned long *);
337
338
339 #define GMNAL_INIT_NAL(a)       do {    \
340                                 (a)->nal_ni_init = gmnal_api_startup; \
341                                 (a)->nal_ni_fini = gmnal_api_shutdown; \
342                                 (a)->nal_data = NULL; \
343                                 } while (0)
344
345
346 /*
347  *      CB NAL
348  */
349
350 ptl_err_t gmnal_cb_send(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *,
351         int, ptl_nid_t, ptl_pid_t, unsigned int, struct iovec *, size_t, size_t);
352
353 ptl_err_t gmnal_cb_send_pages(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *,
354         int, ptl_nid_t, ptl_pid_t, unsigned int, ptl_kiov_t *, size_t, size_t);
355
356 ptl_err_t gmnal_cb_recv(lib_nal_t *, void *, lib_msg_t *, 
357         unsigned int, struct iovec *, size_t, size_t, size_t);
358
359 ptl_err_t gmnal_cb_recv_pages(lib_nal_t *, void *, lib_msg_t *, 
360         unsigned int, ptl_kiov_t *, size_t, size_t, size_t);
361
362 int gmnal_cb_dist(lib_nal_t *, ptl_nid_t, unsigned long *);
363
364 int gmnal_init(void);
365
366 void  gmnal_fini(void);
367
368
369
370 #define GMNAL_INIT_NAL_CB(a)    do {    \
371                                 a->libnal_send = gmnal_cb_send; \
372                                 a->libnal_send_pages = gmnal_cb_send_pages; \
373                                 a->libnal_recv = gmnal_cb_recv; \
374                                 a->libnal_recv_pages = gmnal_cb_recv_pages; \
375                                 a->libnal_map = NULL; \
376                                 a->libnal_unmap = NULL; \
377                                 a->libnal_dist = gmnal_cb_dist; \
378                                 a->libnal_data = NULL; \
379                                 } while (0)
380
381
382 /*
383  *      Small and Large Transmit and Receive Descriptor Functions
384  */
385 int             gmnal_alloc_txd(gmnal_data_t *);
386 void            gmnal_free_txd(gmnal_data_t *);
387 gmnal_stxd_t*   gmnal_get_stxd(gmnal_data_t *, int);
388 void            gmnal_return_stxd(gmnal_data_t *, gmnal_stxd_t *);
389 gmnal_ltxd_t*   gmnal_get_ltxd(gmnal_data_t *);
390 void            gmnal_return_ltxd(gmnal_data_t *, gmnal_ltxd_t *);
391
392 int             gmnal_alloc_srxd(gmnal_data_t *);
393 void            gmnal_free_srxd(gmnal_data_t *);
394 gmnal_srxd_t*   gmnal_get_srxd(gmnal_data_t *, int);
395 void            gmnal_return_srxd(gmnal_data_t *, gmnal_srxd_t *);
396
397 /*
398  *      general utility functions
399  */
400 gmnal_srxd_t    *gmnal_rxbuffer_to_srxd(gmnal_data_t *, void*);
401 void            gmnal_stop_rxthread(gmnal_data_t *);
402 void            gmnal_stop_ctthread(gmnal_data_t *);
403 void            gmnal_drop_sends_callback(gm_port_t *, void *, gm_status_t);
404 void            gmnal_resume_sending_callback(gm_port_t *, void *, gm_status_t);
405 char            *gmnal_gm_error(gm_status_t);
406 char            *gmnal_rxevent(gm_recv_event_t*);
407 int             gmnal_is_small_msg(gmnal_data_t*, int, struct iovec*, int);
408 void            gmnal_yield(int);
409 int             gmnal_start_kernel_threads(gmnal_data_t *);
410
411
412 /*
413  *      Communication functions
414  */
415
416 /*
417  *      Receive threads
418  */
419 int             gmnal_ct_thread(void *); /* caretaker thread */
420 int             gmnal_rx_thread(void *); /* receive thread */
421 int             gmnal_pre_receive(gmnal_data_t*, gmnal_rxtwe_t*, int);
422 int             gmnal_rx_bad(gmnal_data_t *, gmnal_rxtwe_t *, gmnal_srxd_t*);
423 int             gmnal_rx_requeue_buffer(gmnal_data_t *, gmnal_srxd_t *);
424 int             gmnal_add_rxtwe(gmnal_data_t *, gm_recv_t *);
425 gmnal_rxtwe_t * gmnal_get_rxtwe(gmnal_data_t *);
426 void            gmnal_remove_rxtwe(gmnal_data_t *);
427
428
429 /*
430  *      Small messages
431  */
432 ptl_err_t       gmnal_small_rx(lib_nal_t *, void *, lib_msg_t *);
433 ptl_err_t       gmnal_small_tx(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *, 
434                                 int, ptl_nid_t, ptl_pid_t, 
435                                 gmnal_stxd_t*, int);
436 void            gmnal_small_tx_callback(gm_port_t *, void *, gm_status_t);
437
438
439
440 /*
441  *      Large messages
442  */
443 int             gmnal_large_rx(lib_nal_t *, void *, lib_msg_t *, unsigned int, 
444                                 struct iovec *, size_t, size_t, size_t);
445
446 int             gmnal_large_tx(lib_nal_t *, void *, lib_msg_t *, ptl_hdr_t *, 
447                                 int, ptl_nid_t, ptl_pid_t, unsigned int, 
448                                 struct iovec*, size_t, int);
449
450 void            gmnal_large_tx_callback(gm_port_t *, void *, gm_status_t);
451
452 int             gmnal_remote_get(gmnal_srxd_t *, int, struct iovec*, int, 
453                                   struct iovec*);
454
455 void            gmnal_remote_get_callback(gm_port_t *, void *, gm_status_t);
456
457 int             gmnal_copyiov(int, gmnal_srxd_t *, int, struct iovec*, int, 
458                                struct iovec*);
459
460 void            gmnal_large_tx_ack(gmnal_data_t *, gmnal_srxd_t *);
461 void            gmnal_large_tx_ack_callback(gm_port_t *, void *, gm_status_t);
462 void            gmnal_large_tx_ack_received(gmnal_data_t *, gmnal_srxd_t *);
463
464 #endif /*__INCLUDE_GMNAL_H__*/