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