Debian Codegraph @

psm_mq_internal.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2013. Intel Corporation. All rights reserved.
3 * Copyright (c) 2006-2012. QLogic Corporation. All rights reserved.
4 * Copyright (c) 2003-2006, PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #ifndef MQ_INT_H
36 #define MQ_INT_H
37
38 #include "psm_user.h"
39
40 #define MM_FLAG_NONE 0
41 #define MM_FLAG_TRANSIENT 0x1
42 #define MM_NUM_OF_POOLS 7
43
44 typedef struct _mem_block_ctrl mem_block_ctrl;
45 typedef struct _mem_ctrl mem_ctrl;
46
47 struct _mem_ctrl {
48 mem_block_ctrl *free_list;
49 uint32_t total_alloc;
50 uint32_t current_available;
51 uint32_t block_size;
52 uint32_t flags;
53 uint32_t replenishing_rate;
54 };
55
56 struct _mem_block_ctrl {
57 union {
58 mem_ctrl *mem_handler;
59 mem_block_ctrl *next;
60 };
61 char _redzone[PSM_VALGRIND_REDZONE_SZ];
62 };
63
64 typedef psm_error_t (*psm_mq_unexpected_callback_fn_t)
65 (psm_mq_t mq, uint16_t mode, psm_epaddr_t epaddr,
66 uint64_t tag, uint32_t send_msglen,
67 const void *payload, uint32_t paylen);
68
69 struct psm_mq {
70 psm_ep_t ep; /**> ep back pointer */
71 mpool_t sreq_pool;
72 mpool_t rreq_pool;
73
74 psm_mq_unexpected_callback_fn_t unexpected_callback;
75 struct mqsq expected_q; /**> Preposted (expected) queue */
76 struct mqsq unexpected_q; /**> Unexpected queue */
77 struct mqq completed_q; /**> Completed queue */
78
79 uint64_t cur_sysbuf_bytes;
80 uint64_t max_sysbuf_bytes;
81 uint32_t ipath_thresh_rv;
82 uint32_t shm_thresh_rv;
83 uint32_t ipath_window_rv;
84 int memmode;
85
86 psm_mq_stats_t stats; /**> MQ stats, accumulated by each PTL */
87
88 mem_ctrl handler_index[MM_NUM_OF_POOLS];
89 int mem_ctrl_is_init;
90 uint64_t mem_ctrl_total_bytes;
91 };
92
93 #define MQ_IPATH_THRESH_TINY 8
94 #define MQ_IPATH_THRESH_EGR_SDMA 34000
95 #define MQ_IPATH_THRESH_EGR_SDMA_SQ 8192
96
97 #define MQE_TYPE_IS_SEND(type) ((type) & MQE_TYPE_SEND)
98 #define MQE_TYPE_IS_RECV(type) ((type) & MQE_TYPE_RECV)
99
100 #define MQE_TYPE_SEND 0x1000
101 #define MQE_TYPE_RECV 0x2000
102 #define MQE_TYPE_FLAGMASK 0x0fff
103 #define MQE_TYPE_WAITING 0x0001
104 #define MQE_TYPE_WAITING_PEER 0x0004
105 #define MQE_TYPE_EGRLONG 0x0008
106
107 #define MQ_STATE_COMPLETE 0
108 #define MQ_STATE_POSTED 1
109 #define MQ_STATE_MATCHED 2
110 #define MQ_STATE_UNEXP 3
111 #define MQ_STATE_UNEXP_RV 4
112 #define MQ_STATE_FREE 5
113
114 #define MQ_MSG_TINY 1
115 #define MQ_MSG_SHORT 2
116 #define MQ_MSG_LONG 3
117 #define MQ_MSG_RTS 4
118 #define MQ_MSG_RTS_EGR 5
119 #define MQ_MSG_RTS_WAIT 6
120 #define MQ_MSG_DATA 9
121 #define MQ_MSG_DATA_BLK 10
122 #define MQ_MSG_DATA_REQ 11
123 #define MQ_MSG_DATA_REQ_BLK 12
124 #define MQ_MSG_CTS_EGR 13
125
126 #define MQ_MSG_USER_FIRST 64
127
128 /*
129 * Descriptor allocation limits.
130 * The 'LIMITS' predefines fill in a psmi_rlimits_mpool structure
131 */
132 #define MQ_SENDREQ_LIMITS { \
133 .env = "PSM_MQ_SENDREQS_MAX", \
134 .descr = "Max num of isend requests in flight", \
135 .env_level = PSMI_ENVVAR_LEVEL_USER, \
136 .minval = 1, \
137 .maxval = ~0, \
138 .mode[PSMI_MEMMODE_NORMAL] = { 1024, 1048576 }, \
139 .mode[PSMI_MEMMODE_MINIMAL] = { 1024, 65536 }, \
140 .mode[PSMI_MEMMODE_LARGE] = { 8192, 16777216 } \
141 }
142
143 #define MQ_RECVREQ_LIMITS { \
144 .env = "PSM_MQ_RECVREQS_MAX", \
145 .descr = "Max num of irecv requests in flight", \
146 .env_level = PSMI_ENVVAR_LEVEL_USER, \
147 .minval = 1, \
148 .maxval = ~0, \
149 .mode[PSMI_MEMMODE_NORMAL] = { 1024, 1048576 }, \
150 .mode[PSMI_MEMMODE_MINIMAL] = { 1024, 65536 }, \
151 .mode[PSMI_MEMMODE_LARGE] = { 8192, 16777216 } \
152 }
153
154 typedef psm_error_t (*mq_rts_callback_fn_t)(psm_mq_req_t req, int was_posted);
155 typedef psm_error_t (*mq_testwait_callback_fn_t)(psm_mq_req_t *req, int istest,
156 psm_mq_status_t *status);
157
158 /* receive mq_req, the default */
159 struct psm_mq_req {
160 struct {
161 psm_mq_req_t next;
162 psm_mq_req_t *pprev; /* used in completion queue */
163 };
164 uint32_t state;
165 uint32_t type;
166 psm_mq_t mq;
167
168 /* Tag matching vars */
169 uint64_t tag;
170 uint64_t tagsel; /* used for receives */
171
172 /* Some PTLs want to get notified when there's a test/wait event */
173 mq_testwait_callback_fn_t testwait_callback;
174
175 /* Buffer attached to request. May be a system buffer for unexpected
176 * messages or a user buffer when an expected message */
177 uint8_t *buf;
178 uint32_t buf_len;
179 uint32_t error_code;
180
181 /* Used only for eager LONGs */
182 STAILQ_ENTRY(psm_mq_req) nextq; /* used for egr-long only */
183 psmi_egrid_t egrid;
184 psm_epaddr_t epaddr;
185 uint16_t msg_seqnum; /* msg seq num for mctxt */
186 uint8_t tid_grant[128]; /* don't change the size unless... */
187
188 uint32_t recv_msglen; /* Message length we are ready to receive */
189 uint32_t send_msglen; /* Message length from sender */
190 uint32_t recv_msgoff; /* Message offset into buf */
191 union {
192 uint32_t send_msgoff; /* Bytes received so far.. can be larger than buf_len */
193 uint32_t recv_msgposted;
194 };
195
196 /* Used for request to send messages */
197 void *context; /* user context associated to sends or receives */
198
199 /* Used to keep track of unexpected rendezvous */
200 mq_rts_callback_fn_t rts_callback;
201 psm_epaddr_t rts_peer;
202 uint32_t rts_reqidx_peer;
203 uintptr_t rts_sbuf;
204
205 /* PTLs get to store their own per-request data. MQ manages the allocation
206 * by allocating psm_mq_req so that ptl_req_data has enough space for all
207 * possible PTLs.
208 */
209 union {
210 void *ptl_req_ptr; /* when used by ptl as pointer */
211 uint8_t ptl_req_data[0]; /* when used by ptl for "inline" data */
212 };
213 };
214
215 void psmi_mq_mtucpy(void *vdest, const void *vsrc, uint32_t nchars);
216
217 #if defined(__x86_64__)
218 void psmi_mq_mtucpy_safe(void *vdest, const void *vsrc, uint32_t nchars);
219 #else
220 #define psmi_mq_mtucpy_safe psmi_mq_mtucpy
221 #endif
222
223 /*
224 * Optimize for 0-8 byte case, but also handle others.
225 */
226 PSMI_ALWAYS_INLINE(
227 void mq_copy_tiny(uint32_t* dest, uint32_t* src, uint8_t len)
228 )
229 {
230 switch (len) {
231 case 8: *dest++ = *src++;
232 case 4: *dest++ = *src++;
233 case 0: return;
234 case 7:
235 case 6:
236 case 5: *dest++ = *src++; len -= 4;
237 case 3:
238 case 2:
239 case 1: break;
240 default: /* greater than 8 */
241 psmi_mq_mtucpy(dest,src,len);
242 return;
243 }
244 uint8_t* dest1 = (uint8_t*) dest;
245 uint8_t* src1 = (uint8_t*) src;
246 switch(len) {
247 case 3: *dest1++ = *src1++;
248 case 2: *dest1++ = *src1++;
249 case 1: *dest1++ = *src1++;
250 }
251 }
252
253 /*
254 * Given an req with buffer ubuf of length ubuf_len,
255 * fill in the req's status and return the amount of bytes the request
256 * can receive.
257 *
258 * The function sets status truncation errors. Basically what MPI_Status.
259 */
260 PSMI_ALWAYS_INLINE(
261 void mq_status_copy(psm_mq_req_t req, psm_mq_status_t *status))
262 {
263 status->msg_tag = req->tag;
264 status->msg_length = req->send_msglen;
265 status->nbytes = req->recv_msglen;
266 status->error_code = req->error_code;
267 status->context = req->context;
268 }
269
270 PSMI_ALWAYS_INLINE(
271 uint32_t mq_set_msglen(psm_mq_req_t req, uint32_t recvlen, uint32_t sendlen))
272 {
273 req->send_msglen = sendlen;
274 if (recvlen < sendlen) {
275 req->recv_msglen = recvlen;
276 req->error_code = PSM_MQ_TRUNCATION;
277 return recvlen;
278 }
279 else {
280 req->recv_msglen = sendlen;
281 req->error_code = PSM_OK;
282 return sendlen;
283 }
284 }
285
286 #ifndef PSM_DEBUG
287
288 PSMI_ALWAYS_INLINE(
289 void
290 mq_qq_append(struct mqq *q, psm_mq_req_t req))
291 {
292 req->next = NULL;
293 req->pprev = q->lastp;
294 *(q->lastp) = req;
295 q->lastp = &req->next;
296 }
297 #else
298 #define mq_qq_append(q,req) do { \
299 (req)->next = NULL;\
300 (req)->pprev = (q)->lastp;\
301 *((q)->lastp) = (req); \
302 (q)->lastp = &(req)->next; \
303 if (q == &(req)->mq->completed_q) \
304 _IPATH_VDBG("Moving (req)=%p to completed queue on %s, %d\n", (req), __FILE__, __LINE__); \
305 } while (0)
306 #endif
307
308 PSMI_ALWAYS_INLINE(
309 void
310 mq_sq_append(struct mqsq *q, psm_mq_req_t req))
311 {
312 req->next = NULL;
313 *(q->lastp) = req;
314 q->lastp = &req->next;
315 }
316
317 PSMI_ALWAYS_INLINE(
318 void
319 mq_qq_remove(struct mqq *q, psm_mq_req_t req))
320 {
321 if (req->next != NULL)
322 req->next->pprev = req->pprev;
323 else
324 q->lastp = req->pprev;
325 *(req->pprev) = req->next;
326 }
327
328 psm_error_t psmi_mq_req_init(psm_mq_t mq);
329 psm_error_t psmi_mq_req_fini(psm_mq_t mq);
330 psm_mq_req_t psmi_mq_req_alloc(psm_mq_t mq, uint32_t type);
331 #define psmi_mq_req_free(req) psmi_mpool_put(req)
332
333 /*
334 * MQ unexpected buffer management
335 */
336 void psmi_mq_sysbuf_init(psm_mq_t mq);
337 void psmi_mq_sysbuf_fini(psm_mq_t mq);
338 void * psmi_mq_sysbuf_alloc(psm_mq_t mq, uint32_t nbytes);
339 void psmi_mq_sysbuf_free(psm_mq_t mq, void *);
340 void psmi_mq_sysbuf_getinfo(psm_mq_t mq, char *buf, size_t len);
341
342 /*
343 * Main receive progress engine, for shmops and ipath, in mq.c
344 */
345 psm_error_t psmi_mq_malloc(psm_mq_t *mqo);
346 psm_error_t psmi_mq_initialize_defaults(psm_mq_t mq);
347 psm_error_t psmi_mq_free(psm_mq_t mq);
348
349 /* Three functions that handle all MQ stuff */
350 #define MQ_RET_MATCH_OK 0
351 #define MQ_RET_UNEXP_OK 1
352 #define MQ_RET_UNEXP_NO_RESOURCES 2
353 #define MQ_RET_DATA_OK 3
354 #define MQ_RET_DATA_OUT_OF_ORDER 4
355
356 int psmi_mq_handle_outoforder_queue(psm_epaddr_t epaddr);
357 int psmi_mq_handle_envelope_outoforder(psm_mq_t mq, uint16_t mode,
358 psm_epaddr_t epaddr, uint16_t msg_seqnum,
359 uint64_t tag, psmi_egrid_t egrid, uint32_t msglen,
360 const void *payload, uint32_t paylen);
361 int psmi_mq_handle_envelope(psm_mq_t mq, uint16_t mode, psm_epaddr_t epaddr,
362 uint64_t tag, psmi_egrid_t egrid, uint32_t msglen,
363 const void *payload, uint32_t paylen);
364 int psmi_mq_handle_data(psm_mq_req_t req, psm_epaddr_t epaddr,
365 uint32_t egrid, uint32_t offset,
366 const void *payload, uint32_t paylen);
367
368 /* If rtsreq is non-NULL, it contains enough information to pull the data from
369 * the initiator and signal completion at a later time */
370 int psmi_mq_handle_rts_outoforder(psm_mq_t mq, uint64_t tag,
371 uintptr_t send_buf, uint32_t send_msglen,
372 psm_epaddr_t peer, uint16_t msg_seqnum,
373 mq_rts_callback_fn_t cb, psm_mq_req_t *req_o);
374 int psmi_mq_handle_rts(psm_mq_t mq, uint64_t tag, uintptr_t send_buf,
375 uint32_t send_msglen, psm_epaddr_t peer,
376 mq_rts_callback_fn_t cb, psm_mq_req_t *req_o);
377 void psmi_mq_handle_rts_complete(psm_mq_req_t req);
378
379 void psmi_mq_stats_register(psm_mq_t mq, mpspawn_stats_add_fn add_fn);
380
381 PSMI_ALWAYS_INLINE(
382 psm_mq_req_t
383 mq_req_match(struct mqsq *q, uint64_t tag, int remove)
384 )
385 {
386 psm_mq_req_t *curp;
387 psm_mq_req_t cur;
388
389 for (curp = &q->first; (cur = *curp) != NULL; curp = &cur->next) {
390 if (!((tag ^ cur->tag) & cur->tagsel)) { /* match! */
391 if (remove) {
392 if ((*curp = cur->next) == NULL) /* fix tail */
393 q->lastp = curp;
394 cur->next = NULL;
395 }
396 return cur;
397 }
398 }
399 return NULL; /* no match */
400 }
401
402 PSMI_ALWAYS_INLINE(
403 psm_mq_req_t
404 mq_ooo_match(struct mqsq *q, uint16_t msg_seqnum)
405 )
406 {
407 psm_mq_req_t *curp;
408 psm_mq_req_t cur;
409
410 for (curp = &q->first; (cur = *curp) != NULL; curp = &cur->next) {
411 if (cur->msg_seqnum == msg_seqnum) { /* match! */
412 if ((*curp = cur->next) == NULL) /* fix tail */
413 q->lastp = curp;
414 cur->next = NULL;
415 return cur;
416 }
417 }
418 return NULL; /* no match */
419 }
420
421 /* Default handler */
422 int __fastpath
423 psmi_mq_handle_envelope_unexpected(
424 psm_mq_t mq, uint16_t mode, psm_epaddr_t epaddr,
425 uint64_t tag, psmi_egrid_t egrid, uint32_t send_msglen,
426 const void *payload, uint32_t paylen);
427
428 /* Not exposed in public psm, but may extend parts of PSM 2.1 to support
429 * this feature before 2.3 */
430 psm_mq_unexpected_callback_fn_t
431 psmi_mq_register_unexpected_callback(psm_mq_t mq,
432 psm_mq_unexpected_callback_fn_t fn);
433
434
435 PSMI_ALWAYS_INLINE(
436 int
437 psmi_mq_handle_tiny_envelope(psm_mq_t mq, psm_epaddr_t epaddr,
438 uint64_t tag, const void *payload, uint32_t tinylen))
439 {
440 psm_mq_req_t req;
441 uint32_t msglen;
442 int rc;
443 psmi_assert(epaddr != NULL);
444
445 req = mq_req_match(&(mq->expected_q), tag, 1);
446 if (req) { /* we have a match */
447 req->tag = tag;
448 msglen = mq_set_msglen(req, req->buf_len, tinylen);
449 PSM_VALGRIND_DEFINE_MQ_RECV(req->buf, req->buf_len, msglen);
450 mq_copy_tiny((uint32_t *)req->buf, (uint32_t *)payload, msglen);
451 req->state = MQ_STATE_COMPLETE;
452 mq_qq_append(&mq->completed_q, req);
453 mq->stats.rx_user_bytes += msglen;
454 mq->stats.rx_user_num++;
455 _IPATH_VDBG("tiny from=%s match=YES (req=%p) mode=1 mqtag=%llu "
456 "msglen=%d paylen=%d\n", psmi_epaddr_get_name(epaddr->epid), req,
457 (unsigned long long) tag, msglen, tinylen);
458 rc = MQ_RET_MATCH_OK;
459 }
460 else {
461 rc = psmi_mq_handle_envelope_unexpected(mq, MQ_MSG_TINY, epaddr, tag,
462 (union psmi_egrid) 0U, tinylen, payload, tinylen);
463 }
464 return rc;
465 }
466
467 PSMI_ALWAYS_INLINE(
468 void
469 psmi_mq_stats_rts_account(psm_mq_req_t req))
470 {
471 psm_mq_t mq = req->mq;
472 if (MQE_TYPE_IS_SEND(req->type)) {
473 mq->stats.tx_num++;
474 mq->stats.tx_rndv_num++;
475 mq->stats.tx_rndv_bytes += req->send_msglen;
476 }
477 else {
478 mq->stats.rx_user_num++;
479 mq->stats.rx_user_bytes += req->recv_msglen;
480 }
481 return;
482 }
483
484 #endif