1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 Los Alamos National Laboratory (LANL)
6 * This file is part of Lustre, http://www.lustre.org/
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.
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.
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.
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
28 #ifndef __INCLUDE_GMNAL_H__
29 #define __INCLUDE_GMNAL_H__
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 */
39 # define EXPORT_SYMTAB
42 #include "linux/config.h"
43 #include "linux/module.h"
44 #include "linux/tty.h"
45 #include "linux/kernel.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"
57 #define DEBUG_SUBSYSTEM S_NAL
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"
65 #include "portals/nal.h"
66 #include "portals/lib-p30.h"
68 #define GM_STRONG_TYPES 1
73 #include "gm_internal.h"
78 * Defines for the API NAL
82 * Small message size is configurable
83 * insmod can set small_msg_size
84 * which is used to populate nal_data.small_msg_size
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
91 extern int gmnal_small_msg_size;
92 extern int num_rx_threads;
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
99 * The gm_port to use for gmnal
101 #define GMNAL_GM_PORT_ID gm_port_id
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.
113 typedef struct _gmnal_stxd_t {
121 struct _gmnal_data_t *nal_data;
124 struct iovec iov[PTL_MD_MAX_IOV];
125 struct _gmnal_stxd_t *next;
128 struct iovec *iovec_dup;
132 * keeps a transmit token for large transmit (gm_get)
133 * and a pointer to rxd that is used as context for large receive
135 typedef struct _gmnal_ltxd_t {
136 struct _gmnal_ltxd_t *next;
137 struct _gmnal_srxd_t *srxd;
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
146 typedef struct _gmnal_srxd_t {
150 unsigned int gm_source_node;
151 gmnal_stxd_t *source_stxd;
157 spinlock_t callback_lock;
160 struct _gmnal_srxd_t *next;
161 struct _gmnal_data_t *nal_data;
165 * Header which lmgnal puts at the start of each message
166 * watch alignment for ia32/64 interaction
168 typedef struct _gmnal_msghdr {
171 unsigned int sender_node_id;
173 gm_remote_ptr_t stxd_remote_ptr; /* 64 bits */
175 #define GMNAL_MSGHDR_SIZE sizeof(gmnal_msghdr_t)
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)
189 typedef struct _gmnal_rxtwe {
195 struct _gmnal_rxtwe *next;
199 * 1 receive thread started on each CPU
201 #define NRXTHREADS 10 /* max number of receiver threads */
203 typedef struct _gmnal_data_t {
206 spinlock_t stxd_lock;
207 struct semaphore stxd_token;
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;
215 spinlock_t srxd_lock;
216 struct semaphore srxd_token;
218 struct gm_hash *srxd_hash;
221 struct gm_port *gm_port;
222 unsigned int gm_local_nid;
223 unsigned int gm_global_nid;
225 long rxthread_pid[NRXTHREADS];
226 int rxthread_stop_flag;
227 spinlock_t rxthread_flag_lock;
231 gm_alarm_t ctthread_alarm;
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;
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
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)
252 extern gmnal_data_t *global_nal_data;
257 #define GMNAL_IOC_GET_GNID 1
262 #define GMNAL_STATUS_OK 0
263 #define GMNAL_STATUS_FAIL 1
264 #define GMNAL_STATUS_NOMEM 2
268 * FUNCTION PROTOTYPES
276 * For the Small tx and rx descriptor lists
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);
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);
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);
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);
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);
323 int gmnal_api_startup(nal_t *, ptl_pid_t,
324 ptl_ni_limits_t *, ptl_ni_limits_t *);
326 int gmnal_api_forward(nal_t *, int, void *, size_t, void *, size_t);
328 void gmnal_api_shutdown(nal_t *);
330 int gmnal_api_validate(nal_t *, void *, size_t);
332 void gmnal_api_yield(nal_t *, unsigned long *, int);
334 void gmnal_api_lock(nal_t *, unsigned long *);
336 void gmnal_api_unlock(nal_t *, unsigned long *);
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; \
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);
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);
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);
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);
362 int gmnal_cb_dist(lib_nal_t *, ptl_nid_t, unsigned long *);
364 int gmnal_init(void);
366 void gmnal_fini(void);
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; \
383 * Small and Large Transmit and Receive Descriptor Functions
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 *);
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 *);
398 * general utility functions
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 *);
413 * Communication functions
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 *);
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,
436 void gmnal_small_tx_callback(gm_port_t *, void *, gm_status_t);
443 int gmnal_large_rx(lib_nal_t *, void *, lib_msg_t *, unsigned int,
444 struct iovec *, size_t, size_t, size_t);
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);
450 void gmnal_large_tx_callback(gm_port_t *, void *, gm_status_t);
452 int gmnal_remote_get(gmnal_srxd_t *, int, struct iovec*, int,
455 void gmnal_remote_get_callback(gm_port_t *, void *, gm_status_t);
457 int gmnal_copyiov(int, gmnal_srxd_t *, int, struct iovec*, int,
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 *);
464 #endif /*__INCLUDE_GMNAL_H__*/