1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
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.
21 * Basic library routines.
28 # define EXPORT_SYMTAB
31 #include <qsnet/kernel.h>
32 #undef printf /* nasty QSW #define */
34 #include <linux/config.h>
35 #include <linux/module.h>
38 # include <elan/epcomms.h>
40 # include <elan3/elanregs.h>
41 # include <elan3/elandev.h>
42 # include <elan3/elanvp.h>
43 # include <elan3/elan3mmu.h>
44 # include <elan3/elanctxt.h>
45 # include <elan3/elandebug.h>
46 # include <elan3/urom_addrs.h>
47 # include <elan3/busops.h>
48 # include <elan3/kcomm.h>
51 #include <linux/kernel.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/locks.h>
57 #include <linux/unistd.h>
59 #include <linux/uio.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
65 #include <linux/file.h>
66 #include <linux/stat.h>
67 #include <linux/list.h>
68 #include <linux/sysctl.h>
69 #include <asm/segment.h>
71 #define DEBUG_SUBSYSTEM S_QSWNAL
73 #include <linux/kp30.h>
74 #include <portals/p30.h>
75 #include <portals/lib-p30.h>
77 #define KQSW_CHECKSUM 0
79 typedef unsigned long kqsw_csum_t;
80 #define KQSW_CSUM_SIZE (2 * sizeof (kqsw_csum_t))
82 #define KQSW_CSUM_SIZE 0
84 #define KQSW_HDR_SIZE (sizeof (ptl_hdr_t) + KQSW_CSUM_SIZE)
87 * Performance Tuning defines
88 * NB no mention of PAGE_SIZE for interoperability
90 #define KQSW_MAXPAYLOAD PTL_MTU
91 #define KQSW_SMALLPAYLOAD ((4<<10) - KQSW_HDR_SIZE) /* small/large ep receiver breakpoint */
93 #define KQSW_TX_MAXCONTIG (1<<10) /* largest payload that gets made contiguous on transmit */
95 #define KQSW_NTXMSGS 8 /* # normal transmit messages */
96 #define KQSW_NNBLK_TXMSGS 256 /* # reserved transmit messages if can't block */
98 #define KQSW_NRXMSGS_LARGE 64 /* # large receive buffers */
99 #define KQSW_EP_ENVELOPES_LARGE 128 /* # large ep envelopes */
101 #define KQSW_NRXMSGS_SMALL 256 /* # small receive buffers */
102 #define KQSW_EP_ENVELOPES_SMALL 2048 /* # small ep envelopes */
104 #define KQSW_RESCHED 100 /* # busy loops that forces scheduler to yield */
106 #define KQSW_OPTIMIZED_GETS 1 /* optimized gets? */
107 #define KQSW_COPY_SMALL_FWD 0 /* copy small fwd messages to pre-mapped buffer? */
113 #define KQSW_TX_BUFFER_SIZE (KQSW_HDR_SIZE + KQSW_TX_MAXCONTIG)
114 /* The pre-allocated tx buffer (hdr + small payload) */
116 #define KQSW_NTXMSGPAGES (btopr(KQSW_TX_BUFFER_SIZE) + 1 + btopr(KQSW_MAXPAYLOAD) + 1)
117 /* Reserve elan address space for pre-allocated and pre-mapped transmit
118 * buffer and a full payload too. Extra pages allow for page alignment */
120 #define KQSW_NRXMSGPAGES_SMALL (btopr(KQSW_HDR_SIZE + KQSW_SMALLPAYLOAD))
121 /* receive hdr/payload always contiguous and page aligned */
122 #define KQSW_NRXMSGBYTES_SMALL (KQSW_NRXMSGPAGES_SMALL * PAGE_SIZE)
124 #define KQSW_NRXMSGPAGES_LARGE (btopr(KQSW_HDR_SIZE + KQSW_MAXPAYLOAD))
125 /* receive hdr/payload always contiguous and page aligned */
126 #define KQSW_NRXMSGBYTES_LARGE (KQSW_NRXMSGPAGES_LARGE * PAGE_SIZE)
127 /* biggest complete packet we can receive (or transmit) */
129 /* Remote memory descriptor */
132 __u32 kqrmd_nfrag; /* # frags */
134 EP_NMD kqrmd_frag[0]; /* actual frags */
136 EP_IOVEC kqrmd_frag[0]; /* actual frags */
138 } kqswnal_remotemd_t;
142 struct list_head krx_list; /* enqueue -> thread */
143 EP_RCVR *krx_eprx; /* port to post receives to */
144 EP_RXD *krx_rxd; /* receive descriptor (for repost) */
146 EP_NMD krx_elanbuffer; /* contiguous Elan buffer */
148 E3_Addr krx_elanbuffer; /* contiguous Elan buffer */
150 int krx_npages; /* # pages in receive buffer */
151 int krx_nob; /* Number Of Bytes received into buffer */
152 int krx_rpc_reply_needed; /* peer waiting for EKC RPC reply */
153 int krx_rpc_reply_sent; /* rpc reply sent */
154 atomic_t krx_refcount; /* how to tell when rpc is done */
155 kpr_fwd_desc_t krx_fwd; /* embedded forwarding descriptor */
156 ptl_kiov_t krx_kiov[KQSW_NRXMSGPAGES_LARGE]; /* buffer frags */
161 struct list_head ktx_list; /* enqueue idle/active */
162 struct list_head ktx_delayed_list; /* enqueue delayedtxds */
163 unsigned int ktx_isnblk:1; /* reserved descriptor? */
164 unsigned int ktx_state:7; /* What I'm doing */
165 unsigned int ktx_firsttmpfrag:1; /* ktx_frags[0] is in my ebuffer ? 0 : 1 */
166 uint32_t ktx_basepage; /* page offset in reserved elan tx vaddrs for mapping pages */
167 int ktx_npages; /* pages reserved for mapping messages */
168 int ktx_nmappedpages; /* # pages mapped for current message */
169 int ktx_port; /* destination ep port */
170 ptl_nid_t ktx_nid; /* destination node */
171 void *ktx_args[2]; /* completion passthru */
172 char *ktx_buffer; /* pre-allocated contiguous buffer for hdr + small payloads */
173 unsigned long ktx_launchtime; /* when (in jiffies) the transmit was launched */
175 /* debug/info fields */
176 pid_t ktx_launcher; /* pid of launching process */
177 ptl_hdr_t *ktx_wire_hdr; /* portals header (wire endian) */
179 int ktx_nfrag; /* # message frags */
181 EP_NMD ktx_ebuffer; /* elan mapping of ktx_buffer */
182 EP_NMD ktx_frags[EP_MAXFRAG];/* elan mapping of msg frags */
184 E3_Addr ktx_ebuffer; /* elan address of ktx_buffer */
185 EP_IOVEC ktx_frags[EP_MAXFRAG];/* msg frags (elan vaddrs) */
189 #define KTX_IDLE 0 /* on kqn_(nblk_)idletxds */
190 #define KTX_SENDING 1 /* local send */
191 #define KTX_FORWARDING 2 /* routing a packet */
192 #define KTX_GETTING 3 /* local optimised get */
196 char kqn_init; /* what's been initialised */
197 char kqn_shuttingdown; /* I'm trying to shut down */
198 atomic_t kqn_nthreads; /* # threads not terminated */
199 atomic_t kqn_nthreads_running;/* # threads still running */
201 int kqn_optimized_gets; /* optimized GETs? */
202 int kqn_copy_small_fwd; /* fwd small msgs from pre-allocated buffer? */
205 struct ctl_table_header *kqn_sysctl; /* sysctl interface */
207 kqswnal_rx_t *kqn_rxds; /* all the receive descriptors */
208 kqswnal_tx_t *kqn_txds; /* all the transmit descriptors */
210 struct list_head kqn_idletxds; /* transmit descriptors free to use */
211 struct list_head kqn_nblk_idletxds; /* reserved free transmit descriptors */
212 struct list_head kqn_activetxds; /* transmit descriptors being used */
213 spinlock_t kqn_idletxd_lock; /* serialise idle txd access */
214 wait_queue_head_t kqn_idletxd_waitq; /* sender blocks here waiting for idle txd */
215 struct list_head kqn_idletxd_fwdq; /* forwarded packets block here waiting for idle txd */
217 spinlock_t kqn_sched_lock; /* serialise packet schedulers */
218 wait_queue_head_t kqn_sched_waitq; /* scheduler blocks here */
220 struct list_head kqn_readyrxds; /* rxds full of data */
221 struct list_head kqn_delayedfwds; /* delayed forwards */
222 struct list_head kqn_delayedtxds; /* delayed transmits */
224 spinlock_t kqn_statelock; /* cb_cli/cb_sti */
225 nal_cb_t *kqn_cb; /* -> kqswnal_lib */
227 EP_SYS *kqn_ep; /* elan system */
228 EP_NMH *kqn_ep_tx_nmh; /* elan reserved tx vaddrs */
229 EP_NMH *kqn_ep_rx_nmh; /* elan reserved rx vaddrs */
231 EP_DEV *kqn_ep; /* elan device */
232 ELAN3_DMA_HANDLE *kqn_eptxdmahandle; /* elan reserved tx vaddrs */
233 ELAN3_DMA_HANDLE *kqn_eprxdmahandle; /* elan reserved rx vaddrs */
235 EP_XMTR *kqn_eptx; /* elan transmitter */
236 EP_RCVR *kqn_eprx_small; /* elan receiver (small messages) */
237 EP_RCVR *kqn_eprx_large; /* elan receiver (large messages) */
238 kpr_router_t kqn_router; /* connection to Kernel Portals Router module */
240 ptl_nid_t kqn_nid_offset; /* this cluster's NID offset */
241 int kqn_nnodes; /* this cluster's size */
242 int kqn_elanid; /* this nodes's elan ID */
246 #define KQN_INIT_NOTHING 0 /* MUST BE ZERO so zeroed state is initialised OK */
247 #define KQN_INIT_DATA 1
248 #define KQN_INIT_PTL 2
249 #define KQN_INIT_ALL 3
251 extern nal_cb_t kqswnal_lib;
252 extern nal_t kqswnal_api;
253 extern kqswnal_data_t kqswnal_data;
255 /* global pre-prepared replies to keep off the stack */
256 extern EP_STATUSBLK kqswnal_rpc_success;
257 extern EP_STATUSBLK kqswnal_rpc_failed;
259 extern int kqswnal_thread_start (int (*fn)(void *arg), void *arg);
260 extern void kqswnal_rxhandler(EP_RXD *rxd);
261 extern int kqswnal_scheduler (void *);
262 extern void kqswnal_fwd_packet (void *arg, kpr_fwd_desc_t *fwd);
263 extern void kqswnal_dma_reply_complete (EP_RXD *rxd);
264 extern void kqswnal_requeue_rx (kqswnal_rx_t *krx);
266 static inline ptl_nid_t
267 kqswnal_elanid2nid (int elanid)
269 return (kqswnal_data.kqn_nid_offset + elanid);
273 kqswnal_nid2elanid (ptl_nid_t nid)
275 /* not in this cluster? */
276 if (nid < kqswnal_data.kqn_nid_offset ||
277 nid >= kqswnal_data.kqn_nid_offset + kqswnal_data.kqn_nnodes)
280 return (nid - kqswnal_data.kqn_nid_offset);
284 kqswnal_pages_spanned (void *base, int nob)
286 unsigned long first_page = ((unsigned long)base) >> PAGE_SHIFT;
287 unsigned long last_page = (((unsigned long)base) + (nob - 1)) >> PAGE_SHIFT;
289 LASSERT (last_page >= first_page); /* can't wrap address space */
290 return (last_page - first_page + 1);
294 static inline kqsw_csum_t kqsw_csum (kqsw_csum_t sum, void *base, int nob)
296 unsigned char *ptr = (unsigned char *)base;
305 static inline void kqswnal_rx_done (kqswnal_rx_t *krx)
307 LASSERT (atomic_read (&krx->krx_refcount) > 0);
308 if (atomic_dec_and_test (&krx->krx_refcount))
309 kqswnal_requeue_rx(krx);
313 # ifndef EP_RAILMASK_ALL
314 # error "old (unsupported) version of EKC headers"
317 /* multirail defines these in <elan/epcomms.h> */
318 #define EP_MSG_SVC_PORTALS_SMALL (0x10) /* Portals over elan port number (large payloads) */
319 #define EP_MSG_SVC_PORTALS_LARGE (0x11) /* Portals over elan port number (small payloads) */
320 /* NB small/large message sizes are GLOBAL constants */
322 /* A minimal attempt to minimise inline #ifdeffing */
324 #define EP_SUCCESS ESUCCESS
325 #define EP_ENOMEM ENOMEM
327 static inline EP_XMTR *
328 ep_alloc_xmtr(EP_DEV *e)
330 return (ep_alloc_large_xmtr(e));
333 static inline EP_RCVR *
334 ep_alloc_rcvr(EP_DEV *e, int svc, int nenv)
336 return (ep_install_large_rcvr(e, svc, nenv));
340 ep_free_xmtr(EP_XMTR *x)
342 ep_free_large_xmtr(x);
346 ep_free_rcvr(EP_RCVR *r)
348 ep_remove_large_rcvr(r);
352 #endif /* _QSWNAL_H */