Debian Codegraph @

ptl_am/am_reqrep_shmem.c

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 #include <sys/types.h> /* shm_open and signal handling */
36 #include <sys/mman.h>
37 #include <fcntl.h>
38 #include <signal.h>
39
40 #include "psm_user.h"
41 #include "psm_mq_internal.h"
42 #include "psm_am_internal.h"
43 #include "kcopyrw.h"
44 #include "knemrw.h"
45 #include "scifrw.h"
46
47 struct psm_am_max_sizes {
48 uint32_t nargs;
49 uint32_t request_short;
50 uint32_t reply_short;
51 uint32_t request_long;
52 uint32_t reply_long;
53 };
54
55 int psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_NO_KASSIST;
56
57 #ifdef PSM_HAVE_SCIF
58 #define PSM_SCIF_CONNECT_RETRIES_DEFAULT 40
59 int psmi_scif_connect_retries = PSM_SCIF_CONNECT_RETRIES_DEFAULT;
60 #endif
61
62 /* If we push bulk packets, we place them in the target's bulk packet region,
63 * if we don't push bulk packets, we place them in *our* bulk packet region and
64 * have the target pull the data from our region when it needs it. */
65 #define AMSH_BULK_PUSH 1
66
67 /* When do we start using the "huge" buffers -- at 1MB */
68 #define AMSH_HUGE_BYTES 1024*1024
69
70 #define AMMED_SZ 2048
71 #define AMLONG_SZ 8192
72 #define AMHUGE_SZ (524288+sizeof(am_pkt_bulk_t)) /* 512k + E */
73
74 /* short med long huge */
75 static const amsh_qinfo_t amsh_qcounts =
76 { 1024, 256, 16, 1, 1024, 256, 16, 8 };
77
78 /* short med long huge */
79 static const amsh_qinfo_t amsh_qelemsz =
80 { sizeof(am_pkt_short_t), AMMED_SZ+64, AMLONG_SZ, AMHUGE_SZ,
81 sizeof(am_pkt_short_t), AMMED_SZ+64, AMLONG_SZ, AMHUGE_SZ };
82
83 /* we use this internally to break up packets into MTUs */
84 static const amsh_qinfo_t amsh_qpkt_max =
85 { NSHORT_ARGS*8, AMMED_SZ, AMLONG_SZ-sizeof(am_pkt_bulk_t),
86 AMHUGE_SZ-sizeof(am_pkt_bulk_t),
87 NSHORT_ARGS*8, AMMED_SZ, AMLONG_SZ-sizeof(am_pkt_bulk_t),
88 AMHUGE_SZ-sizeof(am_pkt_bulk_t),
89 };
90
91 /* We expose max sizes for the AM ptl. */
92 static const struct psm_am_max_sizes psmi_am_max_sizes =
93 { 6, AMMED_SZ, (uint32_t) -1,
94 AMMED_SZ, (uint32_t) -1 };
95
96 /*
97 * Macro expansion trickery to handle 6 different fifo types:
98 *
99 * _fifo is one of 'reqFifoShort', 'reqFifoMed', 'reqFifoLong',
100 * 'repFifoShort', 'repFifoMed', 'repFifoLong'
101 *
102 * _fifotyp is one of 'short' or 'bulk'
103 */
104 #define QGETPTR(ptl, _shmidx_, _fifo, _fifotyp, _idx) \
105 (am_pkt_ ## _fifotyp ## _t *) \
106 (((uintptr_t)ptl->ep->amsh_qdir[(_shmidx_)].q ## _fifo) + \
107 (_idx) *amsh_qelemsz.q ## _fifo)
108
109 #define QGETPTR_SCIF(ptl, _shmidx_, _node_, _fifo, _fifotyp, _idx) \
110 (am_pkt_ ## _fifotyp ## _t *) \
111 (((uintptr_t)ptl->ep->amsh_qdir[(_shmidx_)].qptrs[_node_].q ## _fifo) +\
112 (_idx) *amsh_qelemsz.q ## _fifo)
113
114 #ifdef PSM_HAVE_SCIF
115 static void *am_ctl_accept_thread(void *arg);
116 static psm_error_t amsh_scif_detach(psm_ep_t ep);
117 #endif
118 static psm_error_t amsh_poll(ptl_t *ptl, int replyonly);
119 static psm_error_t amsh_poll_internal_inner(ptl_t *ptl, int replyonly, int is_internal);
120 static void process_packet(ptl_t *ptl, am_pkt_short_t *pkt, int isreq);
121 static void amsh_conn_handler(void *toki, psm_amarg_t *args, int narg,
122 void *buf, size_t len);
123 static void am_update_directory(ptl_t *ptl, int shmidx);
124
125 /* Kassist helper functions */
126 static const char * psmi_kassist_getmode(int mode);
127 static int psmi_get_kassist_mode();
128
129 /* SCIF DMA helper functions */
130 #ifdef PSM_HAVE_SCIF
131 static const char * psmi_scif_dma_getmode(int mode);
132 static int psmi_get_scif_dma_mode();
133 static int psmi_get_scif_dma_threshold();
134 #endif
135
136 /* Kcopy functionality */
137 int psmi_epaddr_kcopy_pid(psm_epaddr_t epaddr);
138 static int psmi_kcopy_find_minor(int *minor);
139 static int psmi_kcopy_open_minor(int minor);
140
141 static inline void
142 am_ctl_qhdr_init(volatile am_ctl_qhdr_t *q, int elem_cnt, int elem_sz)
143 {
144 q->head = 0;
145 q->elem_cnt = elem_cnt;
146 q->elem_sz = elem_sz;
147 }
148
149 static void
150 am_ctl_bulkpkt_init(am_pkt_bulk_t *base_ptr, size_t elemsz, int nelems)
151 {
152 int i;
153 am_pkt_bulk_t *bulkpkt;
154 uintptr_t bulkptr = (uintptr_t) base_ptr;
155
156 for (i = 0; i < nelems; i++, bulkptr += elemsz) {
157 bulkpkt = (am_pkt_bulk_t *) bulkptr;
158 bulkpkt->idx = i;
159 }
160 }
161
162 #define _PA(type) PSMI_ALIGNUP(amsh_qcounts.q ## type * amsh_qelemsz.q ## type, \
163 PSMI_PAGESIZE)
164 static inline uintptr_t
165 am_ctl_sizeof_block()
166 {
167 return
168 PSMI_ALIGNUP(
169 PSMI_ALIGNUP(AMSH_BLOCK_HEADER_SIZE, PSMI_PAGESIZE) +
170 PSMI_ALIGNUP(sizeof(am_ctl_blockhdr_t), PSMI_PAGESIZE) + /* reqctrl block */
171 _PA(reqFifoShort) + _PA(reqFifoMed) + _PA(reqFifoLong) +
172 _PA(reqFifoHuge) +
173 PSMI_ALIGNUP(sizeof(am_ctl_blockhdr_t), PSMI_PAGESIZE) + /*reqctrl block*/
174 _PA(repFifoShort) + _PA(repFifoMed) + _PA(repFifoLong) +
175 _PA(repFifoHuge),
176 PSMI_PAGESIZE); /* align to page size */
177 }
178 #undef _PA
179
180 /**
181 * Given a number of PEs, determine the amount of memory required.
182 */
183 static
184 size_t
185 psmi_amsh_segsize(int num_pe, int num_nodes)
186 {
187 size_t segsz;
188 segsz = PSMI_ALIGNUP(sizeof(struct am_ctl_dirpage), PSMI_PAGESIZE);
189 segsz += am_ctl_sizeof_block() * num_pe * num_nodes;
190 return segsz;
191 }
192
193 static
194 void
195 amsh_atexit()
196 {
197 static pthread_mutex_t mutex_once = PTHREAD_MUTEX_INITIALIZER;
198 static int atexit_once = 0;
199 psm_ep_t ep;
200 extern psm_ep_t psmi_opened_endpoint;
201
202 pthread_mutex_lock(&mutex_once);
203 if (atexit_once) {
204 pthread_mutex_unlock(&mutex_once);
205 return;
206 }
207 else
208 atexit_once = 1;
209 pthread_mutex_unlock(&mutex_once);
210
211 ep = psmi_opened_endpoint;
212 while (ep) {
213 if (ep->amsh_keyname != NULL) {
214 _IPATH_VDBG("unlinking shm file %s\n", ep->amsh_keyname);
215 shm_unlink(ep->amsh_keyname);
216 }
217
218 if (ep->psmi_kassist_fd != -1) {
219 close(ep->psmi_kassist_fd);
220 ep->psmi_kassist_fd = -1;
221 }
222 ep = ep->user_ep_next;
223 }
224
225 return;
226 }
227
228 static
229 void
230 amsh_mmap_fault(int sig)
231 {
232 static char shm_errmsg[256];
233
234 snprintf(shm_errmsg, sizeof shm_errmsg,
235 "%s: Unable to allocate shared memory for intra-node messaging.\n"
236 "%s: Delete stale shared memory files in /dev/shm.\n",
237 psmi_gethostname(), psmi_gethostname());
238 amsh_atexit();
239 if (write(2, shm_errmsg, strlen(shm_errmsg)+1) == -1)
240 exit(2);
241 else
242 exit(1); /* XXX revisit this... there's probably a better way to exit */
243 }
244
245 /*
246 * Scif init to modify the epid of current process.
247 */
248 #ifdef PSM_HAVE_SCIF
249 static
250 psm_error_t
251 amsh_scif_init(psm_ep_t ep)
252 {
253 scif_epd_t epd;
254 int port, nnodes;
255 uint16_t self;
256 psm_error_t err;
257 union psmi_envvar_val env_retries;
258
259 if(!psmi_getenv("PSM_SCIF_CONNECT_RETRIES",
260 "PSM SCIF connection retry count",
261 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_UINT,
262 (union psmi_envvar_val) psmi_scif_connect_retries,
263 &env_retries)) {
264 psmi_scif_connect_retries = env_retries.e_uint;
265 }
266
267 /* open end pt */
268 if ((epd = scif_open()) < 0) {
269 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
270 "scif_open() failed with err %d", errno);
271 return err;
272 }
273
274 /* bind end pt to specified port */
275 if ((port = scif_bind(epd, 0)) < 0) {
276 scif_close(epd);
277 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
278 "scif_bind() failed with err %d", errno);
279 return err;
280 }
281
282 /* marks an end pt as listening end pt and queues up a maximum of 32
283 * incoming connection requests */
284 if (scif_listen(epd, 40) != 0) {
285 scif_close(epd);
286 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
287 "scif_listen() failed with err %d", errno);
288 return err;
289 }
290
291 if ((nnodes = scif_get_nodeIDs(NULL, 0, &self)) < 0) {
292 scif_close(epd);
293 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
294 "scif_get_nodeIDs() failed with err %d", errno);
295 return err;
296 }
297
298 _IPATH_VDBG("listening on SCIF %d:%d\n", self, port);
299
300 /* Save total scif node #, modify epid to include port and self node ID.*/
301 ep->scif_epd = epd;
302 ep->scif_mynodeid = (int)self;
303 ep->scif_nnodes = nnodes;
304
305 /* Modify epid with acquired info as below */
306 ep->epid |= (((uint64_t)self)&0xFF)<<48;
307 ep->epid |= (((uint64_t)port)&0xFFFF)<<32;
308
309 return PSM_OK;
310 }
311 #endif
312
313 /**
314 * Attach endpoint shared-memory.
315 *
316 * We only try to obtain an shmidx at this point.
317 */
318 psm_error_t
319 psmi_shm_attach(psm_ep_t ep, int *shmidx_o)
320 {
321 int ismaster = 1;
322 int i;
323 int use_kcopy, use_kassist;
324 int shmidx;
325 int kcopy_minor = -1;
326 char shmbuf[256];
327 void *mapptr;
328 size_t segsz;
329 psm_error_t err = PSM_OK;
330
331 if (ep->amsh_shmidx != -1) {
332 *shmidx_o = ep->amsh_shmidx;
333 return PSM_OK;
334 }
335
336 *shmidx_o = -1;
337 if (ep->amsh_keyname != NULL) {
338 if (psmi_uuid_compare(ep->amsh_keyno, ep->key) != 0) {
339 psmi_uuid_unparse(ep->amsh_keyno, shmbuf);
340 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
341 "Shared memory segment already initialized with key=%s",
342 shmbuf);
343 goto fail;
344 }
345 }
346 else {
347 char *p;
348 memcpy(&ep->amsh_keyno, ep->key, sizeof(psm_uuid_t));
349 strncpy(shmbuf, "/psm_shm.", sizeof shmbuf);
350 p = shmbuf + strlen(shmbuf);
351 psmi_uuid_unparse(ep->amsh_keyno, p);
352 ep->amsh_keyname = psmi_strdup(NULL, shmbuf);
353 if (ep->amsh_keyname == NULL) {
354 err = PSM_NO_MEMORY;
355 goto fail;
356 }
357 }
358
359 #ifdef PSM_HAVE_SCIF
360 ep->amsh_qdir = psmi_calloc(NULL, PER_PEER_ENDPOINT,
361 PTL_AMSH_MAX_LOCAL_PROCS*ep->scif_nnodes,
362 sizeof(struct amsh_qdirectory));
363 #else
364 ep->amsh_qdir = psmi_calloc(NULL, PER_PEER_ENDPOINT,
365 PTL_AMSH_MAX_LOCAL_PROCS,
366 sizeof(struct amsh_qdirectory));
367 #endif
368
369 if (ep->amsh_qdir == NULL) {
370 err = PSM_NO_MEMORY;
371 goto fail;
372 }
373
374 /* Get which kassist mode to use. */
375 ep->psmi_kassist_mode = psmi_get_kassist_mode();
376 use_kassist = (ep->psmi_kassist_mode != PSMI_KASSIST_OFF);
377 use_kcopy = (ep->psmi_kassist_mode & PSMI_KASSIST_KCOPY);
378
379 #ifdef PSM_HAVE_SCIF
380 ep->scif_dma_mode = psmi_get_scif_dma_mode();
381 ep->scif_dma_threshold = psmi_get_scif_dma_threshold();
382 #endif
383
384 /* Reserve enough space in the shared memory region for up to
385 PTL_AMSH_MAX_LOCAL_PROCS. Although that much space is reserved in
386 virtual memory, physical pages are not allocated until the
387 corresponding memory location is touched. Memory in this region is
388 only touched as processes initialize their shared queue area in
389 amsh_init_segment(), and physical memory is only allocated by the OS
390 accordingly. So, it looks like this is consumes a lot of memory,
391 but really it consumes as much as necessary for each active process. */
392 #ifdef PSM_HAVE_SCIF
393 segsz = psmi_amsh_segsize(PTL_AMSH_MAX_LOCAL_PROCS,
394 PTL_AMSH_MAX_LOCAL_NODES);
395 #else
396 /* In the non-SCIF case we should be able to get away with just allocating
397 * enough shm for the number of mpi ranks, if the number of ranks is
398 * unavailable, then we will fallback to the number of online cpu cores.
399 * This will help cut back on virtual memory usage.
400 */
401 int nranks, rankid, nprocs;
402 psmi_sharedcontext_params(&nranks, &rankid);
403 nprocs = (nranks <= 0) ? sysconf(_SC_NPROCESSORS_ONLN) : nranks;
404 segsz = psmi_amsh_segsize(nprocs, PTL_AMSH_MAX_LOCAL_NODES);
405 #endif
406
407 ep->amsh_shmfd = shm_open(ep->amsh_keyname,
408 O_RDWR | O_CREAT | O_EXCL | O_TRUNC, S_IRWXU);
409 if (ep->amsh_shmfd < 0) {
410 ismaster = 0;
411 if (errno != EEXIST) {
412 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
413 "Error creating shared memory object in shm_open%s%s",
414 errno != EACCES ? ": " :
415 "(/dev/shm may have stale shm files that need to be removed): ",
416 strerror(errno));
417 goto fail;
418 }
419
420 /* Try to open again, knowing we won't be the shared memory master */
421 ep->amsh_shmfd = shm_open(ep->amsh_keyname, O_RDWR, S_IRWXU);
422 if (ep->amsh_shmfd < 0) {
423 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
424 "Error attaching to shared memory object in shm_open: %s",
425 strerror(errno));
426 goto fail;
427 }
428 }
429
430 /* Now register the atexit handler for cleanup, whether master or slave */
431 atexit(amsh_atexit);
432
433 _IPATH_PRDBG("Registered as %s to key %s\n", ismaster ? "master" : "slave",
434 ep->amsh_keyname);
435
436 if (ismaster) {
437 if (ftruncate(ep->amsh_shmfd, segsz) != 0) {
438 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
439 "Error setting size of shared memory object to %u bytes in "
440 "ftruncate: %s\n", (uint32_t) segsz, strerror(errno));
441 goto fail;
442 }
443 }
444 else {
445 /* Before we do the mmap, make sure that the master has had time to
446 * apply the ftruncate, or else we will get a successful mmap on a
447 * 0-sized object */
448 struct stat fdstat;
449 off_t cursize = 0;
450 while (cursize == 0) {
451 if (fstat(ep->amsh_shmfd, &fdstat)) {
452 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
453 "Error querying size of shared memory object: %s",
454 strerror(errno));
455 goto fail;
456 }
457 cursize = fdstat.st_size;
458 if (cursize == 0)
459 usleep(1); /* be gentle in tight fstat loop */
460 }
461 }
462
463 /* We map the entire shared memory area, consisting of a control structure
464 * followed by per-process shared queue structures. The "master" creates
465 * the control structure and initializes it but every process must lock
466 * appropriate data structures before it reads or writes it.
467 */
468 mapptr = mmap(NULL, segsz, PROT_READ|PROT_WRITE, MAP_SHARED,
469 ep->amsh_shmfd, 0);
470 if (mapptr == MAP_FAILED) {
471 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
472 "Error mmapping shared memory: %s", strerror(errno));
473 goto fail;
474 }
475
476 ep->amsh_shmbase = (uintptr_t) mapptr;
477 ep->amsh_dirpage = (struct am_ctl_dirpage *) ep->amsh_shmbase;
478 ep->amsh_blockbase = ep->amsh_shmbase + psmi_amsh_segsize(0, 0);
479
480 /* We core dump right after here if we don't check the mmap */
481 void (*old_handler_segv)(int) = signal (SIGSEGV, amsh_mmap_fault);
482 void (*old_handler_bus)(int) = signal (SIGBUS, amsh_mmap_fault);
483
484 _IPATH_PRDBG("Mapped shm control object at %p\n", mapptr);
485 if (ismaster) {
486 pthread_mutexattr_t attr;
487 pthread_mutexattr_init(&attr);
488 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
489 pthread_mutex_init(&(ep->amsh_dirpage->lock), &attr);
490 pthread_mutexattr_destroy(&attr);
491
492 ep->amsh_dirpage->num_attached = 0;
493 ep->amsh_dirpage->max_idx = -1;
494
495 for (i = 0; i < PTL_AMSH_MAX_LOCAL_PROCS; i++) {
496 ep->amsh_dirpage->shmidx_map_epid[i] = 0;
497 ep->amsh_dirpage->kassist_pids[i] = 0;
498 }
499
500 for(i = 0; i < PTL_AMSH_MAX_LOCAL_PROCS*PTL_AMSH_MAX_LOCAL_NODES; i++) {
501 struct amsh_qtail* qtail = &ep->amsh_dirpage->qtails[i];
502
503 qtail->reqFifoShort.tail = 0;
504 qtail->reqFifoMed.tail = 0;
505 qtail->reqFifoLong.tail = 0;
506 qtail->reqFifoHuge.tail = 0;
507
508 qtail->repFifoShort.tail = 0;
509 qtail->repFifoMed.tail = 0;
510 qtail->repFifoLong.tail = 0;
511 qtail->repFifoHuge.tail = 0;
512
513 pthread_spin_init(&qtail->reqFifoShort.lock, PTHREAD_PROCESS_SHARED);
514 pthread_spin_init(&qtail->reqFifoMed.lock, PTHREAD_PROCESS_SHARED);
515 pthread_spin_init(&qtail->reqFifoLong.lock, PTHREAD_PROCESS_SHARED);
516 pthread_spin_init(&qtail->reqFifoHuge.lock, PTHREAD_PROCESS_SHARED);
517
518 pthread_spin_init(&qtail->repFifoShort.lock, PTHREAD_PROCESS_SHARED);
519 pthread_spin_init(&qtail->repFifoMed.lock, PTHREAD_PROCESS_SHARED);
520 pthread_spin_init(&qtail->repFifoLong.lock, PTHREAD_PROCESS_SHARED);
521 pthread_spin_init(&qtail->repFifoHuge.lock, PTHREAD_PROCESS_SHARED);
522 }
523
524 if (use_kassist) {
525 if (use_kcopy) {
526 ep->psmi_kassist_fd = psmi_kcopy_find_minor(&kcopy_minor);
527 if (ep->psmi_kassist_fd >= 0)
528 ep->amsh_dirpage->kcopy_minor = kcopy_minor;
529 else
530 ep->amsh_dirpage->kcopy_minor = -1;
531 }
532 else { /* Setup knem */
533 psmi_assert_always(ep->psmi_kassist_mode & PSMI_KASSIST_KNEM);
534 ep->psmi_kassist_fd = knem_open_device();
535 }
536
537 }
538 else
539 ep->psmi_kassist_fd = -1;
540
541 ips_mb();
542
543 ep->amsh_dirpage->is_init = 1;
544 _IPATH_PRDBG("Mapped and initialized shm object control page at %p,"
545 "size=%zu, kcopy minor is %d (mode=%s)\n", mapptr,
546 segsz, kcopy_minor,
547 psmi_kassist_getmode(ep->psmi_kassist_mode));
548 }
549 else {
550 volatile int *is_init = &ep->amsh_dirpage->is_init;
551 while (*is_init == 0)
552 usleep(1);
553 _IPATH_PRDBG("Slave synchronized object control page at "
554 "%p, size=%d, kcopy minor is %d (mode=%s)\n",
555 mapptr, (int) segsz, kcopy_minor,
556 psmi_kassist_getmode(ep->psmi_kassist_mode));
557 }
558
559 /*
560 * First safe point where we can try to attach to the segment.
561 *
562 * Here we reserve the shmidx slot by marking the epid to '1'. We only
563 * update our epid in the init phase once we actually know what our epid
564 * is.
565 */
566 pthread_mutex_lock((pthread_mutex_t *) &(ep->amsh_dirpage->lock));
567 shmidx = -1;
568 for (i = 0; i < PTL_AMSH_MAX_LOCAL_PROCS; i++) {
569 if (ep->amsh_dirpage->shmidx_map_epid[i] == 0) {
570 ep->amsh_dirpage->shmidx_map_epid[i] = 1;
571 ep->amsh_dirpage->psm_verno[i] = PSMI_VERNO;
572 ep->amsh_dirpage->kassist_pids[i] = (int) getpid();
573
574 if (use_kassist) {
575 if (!use_kcopy) {
576 if (!ismaster)
577 ep->psmi_kassist_fd = knem_open_device();
578
579 /* If we are able to use KNEM assume everyone else on the
580 * node can also use it. Advertise that KNEM is active via
581 * the feature flag.
582 */
583 if (ep->psmi_kassist_fd >= 0) {
584 ep->amsh_dirpage->amsh_features[i] |= AMSH_HAVE_KNEM;
585 psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_KNEM;
586 }
587 else {
588 ep->psmi_kassist_mode = PSMI_KASSIST_OFF;
589 use_kassist = 0;
590 psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_NO_KASSIST;
591 }
592 }
593 else if(use_kcopy) {
594 psmi_assert_always(use_kcopy);
595 kcopy_minor = ep->amsh_dirpage->kcopy_minor;
596 if (!ismaster && kcopy_minor >= 0)
597 ep->psmi_kassist_fd = psmi_kcopy_open_minor(kcopy_minor);
598
599 /* If we are able to use KCOPY assume everyone else on the
600 * node can also use it. Advertise that KCOPY is active via
601 * the feature flag.
602 */
603 if (ep->psmi_kassist_fd >= 0) {
604 ep->amsh_dirpage->amsh_features[i] |= AMSH_HAVE_KCOPY;
605 psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_KCOPY;
606 }
607 else {
608 ep->psmi_kassist_mode = PSMI_KASSIST_OFF;
609 use_kassist = 0; use_kcopy = 0;
610 psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_NO_KASSIST;
611 }
612 }
613 }
614 else
615 psmi_shm_mq_rv_thresh = PSMI_MQ_RV_THRESH_NO_KASSIST;
616 _IPATH_PRDBG("KASSIST MODE: %s\n", psmi_kassist_getmode(ep->psmi_kassist_mode));
617 #ifdef PSM_HAVE_SCIF
618 _IPATH_PRDBG("SCIF DMA MODE: %s\n", psmi_scif_dma_getmode(ep->scif_dma_mode));
619 _IPATH_PRDBG("SCIF DMA THRESHOLD: %d\n", ep->scif_dma_threshold);
620 #endif
621
622 ep->amsh_shmidx = shmidx = *shmidx_o = i;
623 _IPATH_PRDBG("Grabbed shmidx %d\n", shmidx);
624 ep->amsh_dirpage->num_attached++;
625 break;
626 }
627 }
628 pthread_mutex_unlock((pthread_mutex_t *) &(ep->amsh_dirpage->lock));
629
630 /* install the old sighandler back */
631 signal(SIGSEGV, old_handler_segv);
632 signal(SIGBUS, old_handler_bus);
633
634 if (shmidx == -1)
635 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
636 "Exceeded maximum of %d support local endpoints: %s",
637 PTL_AMSH_MAX_LOCAL_PROCS, strerror(errno));
638
639 fail:
640 return err;
641 }
642
643 /**
644 * Initialize endpoint shared-memory AM.
645 *
646 * This function ensures that the given endpoint initializes enough shared
647 * memory storage to communicate with up to PSMI_AMSH_MAX_LOCAL_PROCS local
648 * peers. In reality, the implementation need not grow any shared structures
649 * if a single endpoint needs to communicate to 2 or 20 local peers (A local
650 * peer is a peer having a context on any locally-attached LID).
651 *
652 * [pre] Endpoint address epaddr has already been allocated.
653 */
654
655 #define AMSH_QSIZE(type) \
656 PSMI_ALIGNUP(amsh_qelemsz.q ## type * amsh_qcounts.q ## type, \
657 PSMI_PAGESIZE)
658
659 static
660 psm_error_t
661 amsh_init_segment(ptl_t *ptl)
662 {
663 struct amsh_qptrs* qptrs;
664 int shmidx;
665 int i;
666 psm_error_t err = PSM_OK;
667 int scif_nnodes;
668
669 /* Preconditions */
670 psmi_assert_always(ptl != NULL);
671 psmi_assert_always(ptl->ep != NULL);
672 psmi_assert_always(ptl->epaddr != NULL);
673 psmi_assert_always(ptl->ep->epid != 0);
674 psmi_assert_always(ptl->ep->amsh_shmidx != -1);
675
676 shmidx = ptl->ep->amsh_shmidx;
677
678 ptl->amsh_qsizes.qreqFifoShort = AMSH_QSIZE(reqFifoShort);
679 ptl->amsh_qsizes.qreqFifoMed = AMSH_QSIZE(reqFifoMed);
680 ptl->amsh_qsizes.qreqFifoLong = AMSH_QSIZE(reqFifoLong);
681 ptl->amsh_qsizes.qreqFifoHuge = AMSH_QSIZE(reqFifoHuge);
682 ptl->amsh_qsizes.qrepFifoShort = AMSH_QSIZE(repFifoShort);
683 ptl->amsh_qsizes.qrepFifoMed = AMSH_QSIZE(repFifoMed);
684 ptl->amsh_qsizes.qrepFifoLong = AMSH_QSIZE(repFifoLong);
685 ptl->amsh_qsizes.qrepFifoHuge = AMSH_QSIZE(repFifoHuge);
686
687 /* We core dump right after here if we don't check the mmap */
688 void (*old_handler_segv)(int) = signal (SIGSEGV, amsh_mmap_fault);
689 void (*old_handler_bus)(int) = signal (SIGBUS, amsh_mmap_fault);
690
691 pthread_mutex_lock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
692
693 /*
694 * Now that we know our epid, update it in the shmidx array
695 */
696 ptl->ep->amsh_dirpage->shmidx_map_epid[shmidx] = ptl->ep->epid;
697
698 if (shmidx > ptl->ep->amsh_dirpage->max_idx) {
699 ptl->ep->amsh_dirpage->max_idx = shmidx;
700 }
701
702 ptl->shmidx = shmidx;
703 ptl->ep->amsh_qdir[shmidx].amsh_epaddr = ptl->ep->epaddr;
704 for(i = 0; i < PTL_AMSH_MAX_LOCAL_NODES; i++) {
705 ptl->reqH[i].base = ptl->reqH[i].head = ptl->reqH[i].end = NULL;
706 ptl->repH[i].base = ptl->repH[i].head = ptl->repH[i].end = NULL;
707 }
708
709 /* Update all of the local directory entries once here. */
710 for(i = 0; i < PTL_AMSH_MAX_LOCAL_PROCS; i++) {
711 ptl->ep->amsh_qdir[i].amsh_base =
712 (void *)(ptl->ep->amsh_blockbase +
713 am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES * i);
714
715 ptl->ep->amsh_qdir[i].amsh_shmidx = ptl->shmidx;
716
717 /* Encode our SCIF nodeid here. The full epid for local peers isn't
718 known yet, but we do know their nodeid, which is the same as ours.
719 Marking the nodeid here enables process_packet() to work correctly
720 when packets arrive before this epid value has been set with the
721 proper epid, without extra branches in the communication path. */
722 #ifdef PSM_HAVE_SCIF
723 ptl->ep->amsh_qdir[i].amsh_epid =
724 ((psm_epid_t)ptl->ep->scif_mynodeid & 0xff) << 48;
725 #endif
726
727 /* Clear the SCIF socket to -1. This indicates that the socket is not
728 going to be used, ever -- which is true since this is a local peer.
729 This prevents later code from trying to connect to self. */
730 //ptl->ep->amsh_qdir[i].amsh_epd[0] = -1;
731
732 am_update_directory(ptl, i);
733 }
734
735 #ifdef PSM_HAVE_SCIF
736 scif_nnodes = ptl->ep->scif_nnodes;
737 #else
738 /* No SCIF: assume one node. */
739 scif_nnodes = 1;
740 #endif
741
742 /* touch all of my pages */
743 memset(ptl->ep->amsh_qdir[shmidx].amsh_base,
744 0, am_ctl_sizeof_block() * scif_nnodes);
745
746 for(i = 0; i < scif_nnodes; i++) {
747 qptrs = &ptl->ep->amsh_qdir[shmidx].qptrs[i];
748
749 am_ctl_qhdr_init(&qptrs->qreqH->shortq,
750 amsh_qcounts.qreqFifoShort, amsh_qelemsz.qreqFifoShort);
751 am_ctl_qhdr_init(&qptrs->qreqH->medbulkq,
752 amsh_qcounts.qreqFifoMed, amsh_qelemsz.qreqFifoMed);
753 am_ctl_qhdr_init(&qptrs->qreqH->longbulkq,
754 amsh_qcounts.qreqFifoLong, amsh_qelemsz.qreqFifoLong);
755 am_ctl_qhdr_init(&qptrs->qreqH->hugebulkq,
756 amsh_qcounts.qreqFifoHuge, amsh_qelemsz.qreqFifoHuge);
757
758 am_ctl_qhdr_init(&qptrs->qrepH->shortq,
759 amsh_qcounts.qrepFifoShort, amsh_qelemsz.qrepFifoShort);
760 am_ctl_qhdr_init(&qptrs->qrepH->medbulkq,
761 amsh_qcounts.qrepFifoMed, amsh_qelemsz.qrepFifoMed);
762 am_ctl_qhdr_init(&qptrs->qrepH->longbulkq,
763 amsh_qcounts.qrepFifoLong, amsh_qelemsz.qrepFifoLong);
764 am_ctl_qhdr_init(&qptrs->qrepH->hugebulkq,
765 amsh_qcounts.qrepFifoHuge, amsh_qelemsz.qrepFifoHuge);
766
767 /* Set bulkidx in every bulk packet */
768 am_ctl_bulkpkt_init(qptrs->qreqFifoMed,
769 amsh_qelemsz.qreqFifoMed,
770 amsh_qcounts.qreqFifoMed);
771 am_ctl_bulkpkt_init(qptrs->qreqFifoLong,
772 amsh_qelemsz.qreqFifoLong,
773 amsh_qcounts.qreqFifoLong);
774 am_ctl_bulkpkt_init(qptrs->qreqFifoHuge,
775 amsh_qelemsz.qreqFifoHuge,
776 amsh_qcounts.qreqFifoHuge);
777
778 am_ctl_bulkpkt_init(qptrs->qrepFifoMed,
779 amsh_qelemsz.qrepFifoMed,
780 amsh_qcounts.qrepFifoMed);
781 am_ctl_bulkpkt_init(qptrs->qrepFifoLong,
782 amsh_qelemsz.qrepFifoLong,
783 amsh_qcounts.qrepFifoLong);
784 am_ctl_bulkpkt_init(qptrs->qrepFifoHuge,
785 amsh_qelemsz.qrepFifoHuge,
786 amsh_qcounts.qrepFifoHuge);
787 }
788
789 /* install the old sighandler back */
790 signal(SIGSEGV, old_handler_segv);
791 signal(SIGBUS, old_handler_bus);
792
793 pthread_mutex_unlock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
794 return err;
795 }
796
797 psm_error_t
798 psmi_shm_detach(psm_ep_t ep)
799 {
800 psm_error_t err = PSM_OK;
801
802 if (ep->amsh_shmidx == -1 || ep->amsh_keyname == NULL)
803 return err;
804
805 #ifdef PSM_HAVE_SCIF
806 if (amsh_scif_detach(ep)) {
807 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
808 "Error with amsh_scif_detach() of shared segment: %s",
809 strerror(errno));
810 goto fail;
811 }
812 #endif
813
814 _IPATH_VDBG("unlinking shm file %s\n", ep->amsh_keyname+1);
815 shm_unlink(ep->amsh_keyname);
816 psmi_free(ep->amsh_keyname);
817 ep->amsh_keyname = NULL;
818
819 if (ep->psmi_kassist_fd != -1) {
820 close(ep->psmi_kassist_fd);
821 ep->psmi_kassist_fd = -1;
822 }
823
824 /* go mark my shmidx as free */
825 pthread_mutex_lock((pthread_mutex_t *) &(ep->amsh_dirpage->lock));
826
827 ep->amsh_dirpage->num_attached--;
828 ep->amsh_dirpage->shmidx_map_epid[ep->amsh_shmidx] = 0;
829 ep->amsh_shmidx = -1;
830
831 if (ep->amsh_dirpage->num_attached == 0) { /* truncate to nothing */
832 pthread_mutex_unlock((pthread_mutex_t *) &(ep->amsh_dirpage->lock));
833
834 /* Instead of dynamically shrinking the shared memory region, we always
835 leave it allocated for up to PTL_AMSH_MAX_LOCAL_PROCS or number
836 of processors online.
837 Thus mremap() is never necessary, nor is ftruncate() here.
838 However when the attached process count does go to 0, we should
839 fully munmap() the entire region.
840 */
841 #ifdef PSM_HAVE_SCIF
842 if (munmap((void *) ep->amsh_shmbase,
843 psmi_amsh_segsize(PTL_AMSH_MAX_LOCAL_PROCS,
844 PTL_AMSH_MAX_LOCAL_NODES))) {
845 #else
846 int nranks, rankid, nprocs;
847 psmi_sharedcontext_params(&nranks, &rankid);
848 nprocs = (nranks <= 0) ? sysconf(_SC_NPROCESSORS_ONLN) : nranks;
849 if (munmap((void *) ep->amsh_shmbase,
850 psmi_amsh_segsize(nprocs, PTL_AMSH_MAX_LOCAL_NODES))) {
851 #endif
852 err = psmi_handle_error(NULL, PSM_SHMEM_SEGMENT_ERR,
853 "Error with munamp of shared segment: %s", strerror(errno));
854 goto fail;
855 }
856 }
857 else {
858 int i, new_max_idx = ep->amsh_dirpage->max_idx;
859 for (i = ep->amsh_dirpage->max_idx; i >= 0; i--) {
860 if (ep->amsh_dirpage->shmidx_map_epid[i] == 0)
861 new_max_idx = i;
862 else
863 break;
864 }
865
866 ep->amsh_dirpage->max_idx = new_max_idx;
867
868 pthread_mutex_unlock((pthread_mutex_t *) &(ep->amsh_dirpage->lock));
869 }
870
871 ep->amsh_max_idx = -1;
872 ep->amsh_shmfd = -1;
873
874 ep->amsh_shmbase = ep->amsh_blockbase = 0;
875 ep->amsh_dirpage = NULL;
876 memset(ep->amsh_keyno, 0, sizeof(ep->amsh_keyno));
877
878 return PSM_OK;
879
880 fail:
881 return err;
882 }
883
884 /**
885 * Update pointers to our req/rep receive queues
886 *
887 * Only called from am_update_directory()
888 */
889 static
890 void
891 am_hdrcache_update_short(ptl_t *ptl, int shmidx,
892 am_ctl_qshort_cache_t *reqH,
893 am_ctl_qshort_cache_t *repH)
894 {
895 int node;
896
897 for(node = 0; node < PTL_AMSH_MAX_LOCAL_NODES; node++) {
898 reqH[node].base = QGETPTR_SCIF(ptl, shmidx, node,
899 reqFifoShort, short, 0);
900 reqH[node].head = QGETPTR_SCIF(ptl, shmidx, node,
901 reqFifoShort, short, 0);
902 reqH[node].end = QGETPTR_SCIF(ptl, shmidx, node,
903 reqFifoShort, short, amsh_qcounts.qreqFifoShort);
904
905 repH[node].base = QGETPTR_SCIF(ptl, shmidx, node,
906 repFifoShort, short, 0);
907 repH[node].head = QGETPTR_SCIF(ptl, shmidx, node,
908 repFifoShort, short, 0);
909 repH[node].end = QGETPTR_SCIF(ptl, shmidx, node,
910 repFifoShort, short, amsh_qcounts.qrepFifoShort);
911 }
912 }
913
914 /**
915 * Update locally cached shared-pointer directory.
916 *
917 * @param shmidx Endpoint index for which to update local directory.
918 */
919
920 static
921 void
922 am_update_directory(ptl_t *ptl, int shmidx)
923 {
924 psm_ep_t ep = ptl->ep;
925 uintptr_t base_this;
926 uintptr_t base_node;
927 struct amsh_qptrs* qptrs;
928 int i;
929
930 psmi_assert_always(shmidx != -1);
931 base_this =
932 (uintptr_t)ep->amsh_qdir[shmidx].amsh_base + AMSH_BLOCK_HEADER_SIZE;
933
934 if (shmidx < PTL_AMSH_MAX_LOCAL_PROCS) {
935 if(ep->amsh_dirpage->amsh_features[shmidx] & AMSH_HAVE_KASSIST) {
936 ep->amsh_qdir[shmidx].kassist_pid =
937 ep->amsh_dirpage->kassist_pids[shmidx];
938 }
939 } else {
940 ep->amsh_qdir[shmidx].kassist_pid = 0;
941 }
942
943 for(i = 0; i < PTL_AMSH_MAX_LOCAL_NODES; i++) {
944 qptrs = &ep->amsh_qdir[shmidx].qptrs[i];
945
946 base_node = base_this + (i * am_ctl_sizeof_block());
947
948 /* Request queues */
949 qptrs->qreqH = (am_ctl_blockhdr_t *) base_node;
950
951 qptrs->qreqFifoShort = (am_pkt_short_t *)
952 ((uintptr_t) qptrs->qreqH +
953 PSMI_ALIGNUP(sizeof(am_ctl_blockhdr_t), PSMI_PAGESIZE));
954 qptrs->qreqFifoMed = (am_pkt_bulk_t *)
955 ((uintptr_t) qptrs->qreqFifoShort +
956 ptl->amsh_qsizes.qreqFifoShort);
957 qptrs->qreqFifoLong = (am_pkt_bulk_t *)
958 ((uintptr_t) qptrs->qreqFifoMed +
959 ptl->amsh_qsizes.qreqFifoMed);
960 qptrs->qreqFifoHuge = (am_pkt_bulk_t *)
961 ((uintptr_t) qptrs->qreqFifoLong +
962 ptl->amsh_qsizes.qreqFifoLong);
963
964 /* Reply queues */
965 qptrs->qrepH = (am_ctl_blockhdr_t *)
966 ((uintptr_t) qptrs->qreqFifoHuge +
967 ptl->amsh_qsizes.qreqFifoHuge);
968
969 qptrs->qrepFifoShort = (am_pkt_short_t *)
970 ((uintptr_t) qptrs->qrepH +
971 PSMI_ALIGNUP(sizeof(am_ctl_blockhdr_t), PSMI_PAGESIZE));
972 qptrs->qrepFifoMed = (am_pkt_bulk_t *)
973 ((uintptr_t) qptrs->qrepFifoShort +
974 ptl->amsh_qsizes.qrepFifoShort);
975 qptrs->qrepFifoLong = (am_pkt_bulk_t *)
976 ((uintptr_t) qptrs->qrepFifoMed +
977 ptl->amsh_qsizes.qrepFifoMed);
978 qptrs->qrepFifoHuge = (am_pkt_bulk_t *)
979 ((uintptr_t) qptrs->qrepFifoLong +
980 ptl->amsh_qsizes.qrepFifoLong);
981
982 _IPATH_VDBG("shmidx=%d node=%d Request Hdr=%p,Pkt=%p,Med=%p,Long=%p,Huge=%p\n",
983 shmidx, i,
984 qptrs->qreqH,
985 qptrs->qreqFifoShort,
986 qptrs->qreqFifoMed,
987 qptrs->qreqFifoLong,
988 qptrs->qreqFifoHuge);
989 _IPATH_VDBG("shmidx=%d node=%d Reply Hdr=%p,Pkt=%p,Med=%p,Long=%p,Huge=%p\n",
990 shmidx, i,
991 qptrs->qrepH,
992 qptrs->qrepFifoShort,
993 qptrs->qrepFifoMed,
994 qptrs->qrepFifoLong,
995 qptrs->qrepFifoHuge);
996 }
997
998 /* Update local shorthand pointers */
999 #ifdef PSM_HAVE_SCIF
1000 qptrs = &ep->amsh_qdir[shmidx].qptrs[ptl->ep->scif_mynodeid];
1001 #else
1002 qptrs = &ep->amsh_qdir[shmidx].qptrs[0];
1003 #endif
1004
1005 ep->amsh_qdir[shmidx].qreqH = qptrs->qreqH;
1006 ep->amsh_qdir[shmidx].qreqFifoShort = qptrs->qreqFifoShort;
1007 ep->amsh_qdir[shmidx].qreqFifoMed = qptrs->qreqFifoMed;
1008 ep->amsh_qdir[shmidx].qreqFifoLong = qptrs->qreqFifoLong;
1009 ep->amsh_qdir[shmidx].qreqFifoHuge = qptrs->qreqFifoHuge;
1010
1011 ep->amsh_qdir[shmidx].qrepH = qptrs->qrepH;
1012 ep->amsh_qdir[shmidx].qrepFifoShort = qptrs->qrepFifoShort;
1013 ep->amsh_qdir[shmidx].qrepFifoMed = qptrs->qrepFifoMed;
1014 ep->amsh_qdir[shmidx].qrepFifoLong = qptrs->qrepFifoLong;
1015 ep->amsh_qdir[shmidx].qrepFifoHuge = qptrs->qrepFifoHuge;
1016
1017 /* If we're updating our shmidx, we update our cached pointers */
1018 if (ptl->shmidx == shmidx)
1019 am_hdrcache_update_short(ptl, shmidx,
1020 (am_ctl_qshort_cache_t *) ptl->reqH,
1021 (am_ctl_qshort_cache_t *) ptl->repH);
1022
1023 /* Sanity check */
1024 uintptr_t base_next =
1025 (uintptr_t) ep->amsh_qdir[shmidx].qptrs[PTL_AMSH_MAX_LOCAL_NODES - 1].qrepFifoHuge + ptl->amsh_qsizes.qrepFifoHuge;
1026
1027 psmi_assert_always(base_next - base_this <=
1028 am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES);
1029 }
1030
1031 /* ep_epid_share_memory wrapper */
1032 static
1033 int
1034 amsh_epid_reachable(ptl_t *ptl, psm_epid_t epid)
1035 {
1036 int result;
1037 psm_error_t err;
1038 err = psm_ep_epid_share_memory(ptl->ep, epid, &result);
1039 psmi_assert_always(err == PSM_OK);
1040 return result;
1041 }
1042
1043 static
1044 psm_error_t
1045 amsh_epaddr_add(ptl_t *ptl, psm_epid_t epid, int shmidx, psm_epaddr_t *epaddr_o)
1046 {
1047 psm_epaddr_t epaddr;
1048 psm_error_t err = PSM_OK;
1049
1050 psmi_assert(psmi_epid_lookup(ptl->ep, epid) == NULL);
1051
1052 if (epid == ptl->epid) {
1053 epaddr = ptl->epaddr;
1054 } else {
1055 epaddr = (psm_epaddr_t) psmi_calloc(ptl->ep, PER_PEER_ENDPOINT,
1056 1, sizeof(struct psm_epaddr));
1057 if (epaddr == NULL) {
1058 return PSM_NO_MEMORY;
1059 }
1060 psmi_assert_always(ptl->ep->amsh_qdir[shmidx].amsh_epaddr == NULL);
1061 }
1062
1063 epaddr->ptl = ptl;
1064 epaddr->ptlctl = ptl->ctl;
1065 STAILQ_INIT(&epaddr->egrlong);
1066 epaddr->mctxt_prev = epaddr;
1067 epaddr->mctxt_next = epaddr;
1068 epaddr->mctxt_master = epaddr;
1069 epaddr->epid = epid;
1070 epaddr->ep = ptl->ep;
1071 epaddr->_shmidx = shmidx;
1072 AMSH_CSTATE_TO_SET(epaddr, NONE);
1073 AMSH_CSTATE_FROM_SET(epaddr, NONE);
1074 if ((err = psmi_epid_set_hostname(psm_epid_nid(epid),
1075 psmi_gethostname(), 0)))
1076 goto fail;
1077
1078 ptl->ep->amsh_qdir[shmidx].amsh_epaddr = epaddr;
1079
1080 /* Finally, add to table */
1081 if ((err = psmi_epid_add(ptl->ep, epid, epaddr)))
1082 goto fail;
1083
1084 _IPATH_VDBG("epaddr=%s added to ptl=%p\n",
1085 psmi_epaddr_get_name(epid), ptl);
1086
1087 *epaddr_o = epaddr;
1088 return PSM_OK;
1089 fail:
1090 if (epaddr != ptl->epaddr) psmi_free(epaddr);
1091 return err;
1092 }
1093
1094 struct ptl_connection_req
1095 {
1096 int isdone;
1097 int op; /* connect or disconnect */
1098 int numep;
1099 int numep_left;
1100 int phase;
1101
1102 int *epid_mask;
1103 const psm_epid_t *epids; /* input epid list */
1104 psm_epaddr_t *epaddr;
1105 psm_error_t *errors; /* inout errors */
1106
1107 /* Used for connect/disconnect */
1108 psm_amarg_t args[4];
1109 };
1110
1111 /*
1112 * function to make scif connection between nodes and exchange shared memory
1113 */
1114 #ifdef PSM_HAVE_SCIF
1115 static int
1116 amsh_scif_send(scif_epd_t epd, void *buf, size_t len)
1117 {
1118 int ret;
1119 while (len) {
1120 ret = scif_send(epd, buf, (uint32_t)len, SCIF_SEND_BLOCK);
1121 if (ret < 0) {
1122 if (errno == EINTR) continue;
1123 return ret;
1124 }
1125 buf += ret;
1126 len -= ret;
1127 }
1128 return 0;
1129 }
1130
1131 static int
1132 amsh_scif_recv(scif_epd_t epd, void *buf, size_t len)
1133 {
1134 int ret;
1135 while (len) {
1136 ret = scif_recv(epd, buf, (uint32_t)len, SCIF_RECV_BLOCK);
1137 if (ret < 0) {
1138 if (errno == EINTR) continue;
1139 return ret;
1140 }
1141 buf += ret;
1142 len -= ret;
1143 }
1144 return 0;
1145 }
1146
1147 static
1148 psm_error_t
1149 amsh_scif_connect(uint16_t nodeid, uint16_t port, scif_epd_t *epd_o)
1150 {
1151 int tries;
1152 struct scif_portID portID;
1153 scif_epd_t epd;
1154 psm_error_t err;
1155
1156 epd = scif_open();
1157 if (epd < 0) {
1158 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1159 "scif_open failed with error %d\n", errno);
1160 return err;
1161 }
1162
1163 portID.port = port;
1164 portID.node = nodeid;
1165
1166 _IPATH_VDBG("scif connecting to %d:%d\n", nodeid, port);
1167
1168 for(tries = 0; tries < psmi_scif_connect_retries; tries++) {
1169 if (scif_connect(epd, &portID) >= 0) {
1170 break;
1171 } else if(errno != ECONNREFUSED) {
1172 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1173 "scif_connect failed with error %d (%s)\n",
1174 errno, strerror(errno));
1175 scif_close(epd);
1176 return err;
1177 }
1178
1179 /* Wait a bit before trying again. */
1180 if(tries < 20) {
1181 usleep(100000);
1182 } else {
1183 usleep(250000);
1184 }
1185 }
1186
1187 if(tries == psmi_scif_connect_retries) {
1188 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1189 "scif_connect retry limit exceeded\n");
1190 return err;
1191 }
1192
1193 *epd_o = epd;
1194 return PSM_OK;
1195 }
1196
1197 /* Establish a connection to a single epid. */
1198 static psm_error_t amsh_scif_setup(ptl_t* ptl, psm_epid_t epid)
1199 {
1200 psm_ep_t ep = ptl->ep;
1201 psm_error_t err = PSM_OK;
1202 scif_epd_t epd = -1;
1203 void* addr;
1204 int peeridx;
1205
1206 /* Send this struct to identify ourselves to the peer (offset unused) */
1207 /* Receive this struct to get memory mapping information. */
1208 struct { off_t offset; int verno; psm_epid_t epid; } buf;
1209
1210 int port = (int)((epid>>32)&0xffff);
1211 int nodeid = (int)((epid>>48)&0xff);
1212 int shmidx = (int)((epid>>56)&0xff);
1213
1214 /* Skip peers on the same node */
1215 if (nodeid == ep->scif_mynodeid) {
1216 return PSM_OK;
1217 }
1218
1219 /* Figure out the peer's index. */
1220 /* 0 1 mynodeid 3 4 */
1221 /* nodeid 0 1 3 4 */
1222 if(nodeid > ep->scif_mynodeid) {
1223 peeridx = (PTL_AMSH_MAX_LOCAL_PROCS * nodeid) + shmidx;
1224 } else /*nodeid < ep->scif_mynodeid) */ {
1225 peeridx = (PTL_AMSH_MAX_LOCAL_PROCS * (nodeid + 1)) + shmidx;
1226 }
1227
1228 _IPATH_VDBG("%lx scif_connect to %d:%d %d %lx\n",
1229 ep->epid, nodeid, port, peeridx, epid);
1230
1231 if(ep->amsh_qdir[peeridx].amsh_epd[0] != 0) {
1232 /* Already established this side of the connection; all done. */
1233 return err;
1234 }
1235
1236 buf.offset = 0;
1237 buf.verno = PSMI_VERNO;
1238 buf.epid = ep->epid;
1239
1240 err = amsh_scif_connect(nodeid, port, &epd);
1241 if(err) {
1242 return err;
1243 }
1244
1245 /* Send our identification information. */
1246 if (amsh_scif_send(epd, &buf, sizeof(buf))) {
1247 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1248 "scif_send failed: %d %s\n", errno, strerror(errno));
1249 scif_close(epd);
1250 return err;
1251 }
1252
1253 /* Receive memory registration information. */
1254 if(amsh_scif_recv(epd, &buf, sizeof(buf))) {
1255 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1256 "scif_recv failed: %d %s\n", errno, strerror(errno));
1257 scif_close(epd);
1258 return err;
1259 }
1260
1261 addr = scif_mmap(NULL, am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES,
1262 SCIF_PROT_READ|SCIF_PROT_WRITE, 0, epd, buf.offset);
1263 if(addr == SCIF_MMAP_FAILED) {
1264 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
1265 "scif_mmap failed: %d %s\n", errno, strerror(errno));
1266 scif_close(epd);
1267 return err;
1268 }
1269
1270 _IPATH_PRDBG("%lx scif_mmap offset %p -> %p to addr %p -> %p length %ld\n",
1271 ep->epid, (void*)buf.offset,
1272 (void*)(buf.offset + am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES),
1273 addr,
1274 (void*)((uintptr_t)addr + am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES),
1275 am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES);
1276
1277 ep->amsh_qdir[peeridx].amsh_offset = buf.offset;
1278 ep->amsh_qdir[peeridx].amsh_base = addr;
1279 ep->amsh_qdir[peeridx].amsh_epid = buf.epid;
1280 ep->amsh_qdir[peeridx].amsh_verno = buf.verno;
1281
1282 /* Calculate my index from the peer's perspective. */
1283 /* 0 1 mynodeid 3 4 */
1284 /* nodeid 0 1 3 4 */
1285 if(ep->scif_mynodeid < nodeid) {
1286 ep->amsh_qdir[peeridx].amsh_shmidx =
1287 (PTL_AMSH_MAX_LOCAL_PROCS * (ep->scif_mynodeid + 1)) +
1288 ep->amsh_shmidx;
1289 } else {
1290 ep->amsh_qdir[peeridx].amsh_shmidx =
1291 (PTL_AMSH_MAX_LOCAL_PROCS * ep->scif_mynodeid) +
1292 ep->amsh_shmidx;
1293 }
1294
1295 /* There are eventually two connections. epd[0] always has the remote
1296 memory mapped region associated with it, and is used to make requests
1297 to that peer. epd[1] exposes our local shared memory, and is used
1298 to respond to remote requests. */
1299 ep->amsh_qdir[peeridx].amsh_epd[0] = epd;
1300
1301 am_update_directory(ptl, peeridx);
1302
1303 _IPATH_VDBG("shmidx %d connected! set peeridx %d amsh_shmidx %d epd %d\n",
1304 ep->amsh_shmidx, peeridx,
1305 ep->amsh_qdir[peeridx].amsh_shmidx,
1306 ep->amsh_qdir[peeridx].amsh_epd[0]);
1307 return err;
1308 }
1309
1310 static
1311 psm_error_t
1312 amsh_scif_detach(psm_ep_t ep)
1313 {
1314 int i;
1315 int size = am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES;
1316
1317 /* do the rest scif cleanup work */
1318 for (i = 0; i < ep->scif_nnodes*PTL_AMSH_MAX_LOCAL_PROCS; i++) {
1319 if (ep->amsh_qdir[i].amsh_epd[0] == 0) continue;
1320
1321 if(i >= PTL_AMSH_MAX_LOCAL_PROCS) {
1322 if(scif_munmap(ep->amsh_qdir[i].amsh_base, size)) {
1323 _IPATH_INFO("SCIF: unmapping addr %p length %d failed: (%d) %s\n",
1324 ep->amsh_qdir[i].amsh_base, size,
1325 errno, strerror(errno));
1326 return PSM_INTERNAL_ERR;
1327 }
1328
1329 ep->amsh_qdir[i].amsh_base = NULL;
1330 }
1331
1332 if(scif_close(ep->amsh_qdir[i].amsh_epd[0])) {
1333 _IPATH_INFO("SCIF: closing epd[0] %d failed: (%d) %s\n",
1334 ep->amsh_qdir[i].amsh_epd[0],
1335 errno, strerror(errno));
1336 return PSM_INTERNAL_ERR;
1337 }
1338
1339 if(scif_close(ep->amsh_qdir[i].amsh_epd[1])) {
1340 _IPATH_INFO("SCIF: closing epd[1] %d failed: (%d) %s\n",
1341 ep->amsh_qdir[i].amsh_epd[1],
1342 errno, strerror(errno));
1343 return PSM_INTERNAL_ERR;
1344 }
1345
1346 ep->amsh_qdir[i].amsh_epd[0] = 0;
1347 ep->amsh_qdir[i].amsh_epd[1] = 0;
1348 }
1349
1350 /* The accept thread will detect that the listen socket has been closed
1351 and will shut down gracefully. */
1352 if(scif_close(ep->scif_epd)) {
1353 _IPATH_INFO("SCIF: closing listen epd %d failed: (%d) %s\n",
1354 ep->scif_epd,
1355 errno, strerror(errno));
1356 return PSM_INTERNAL_ERR;
1357 }
1358
1359 pthread_join(ep->scif_thread, NULL);
1360
1361 return PSM_OK;
1362 }
1363
1364 #endif //PSM_HAVE_SCIF
1365
1366 #define PTL_OP_CONNECT 0
1367 #define PTL_OP_DISCONNECT 1
1368 #define PTL_OP_ABORT 2
1369
1370 static
1371 psm_error_t
1372 amsh_ep_connreq_init(ptl_t *ptl,
1373 int op, /* connect, disconnect or abort */
1374 int numep,
1375 const psm_epid_t *array_of_epid, /* non-NULL on connect */
1376 const int array_of_epid_mask[],
1377 psm_error_t *array_of_errors,
1378 psm_epaddr_t *array_of_epaddr,
1379 struct ptl_connection_req **req_o)
1380 {
1381 int i, cstate;
1382 psm_epaddr_t epaddr;
1383 psm_epid_t epid;
1384 struct ptl_connection_req *req = NULL;
1385
1386 req = (struct ptl_connection_req *)
1387 psmi_calloc(ptl->ep, PER_PEER_ENDPOINT, 1,
1388 sizeof(struct ptl_connection_req));
1389 if (req == NULL)
1390 return PSM_NO_MEMORY;
1391
1392 req->isdone = 0;
1393 req->op = op;
1394 req->numep = numep;
1395 req->numep_left = 0;
1396 req->phase = ptl->connect_phase;
1397 req->epid_mask = (int *)
1398 psmi_calloc(ptl->ep, PER_PEER_ENDPOINT, numep, sizeof(int));
1399
1400 if (req->epid_mask == NULL) {
1401 psmi_free(req);
1402 return PSM_NO_MEMORY;
1403 }
1404
1405 req->epaddr = array_of_epaddr;
1406 req->epids = array_of_epid;
1407 req->errors = array_of_errors;
1408
1409 /* First check if there's really something to connect/disconnect
1410 * for this PTL */
1411 for (i = 0; i < numep; i++) {
1412 req->epid_mask[i] = AMSH_CMASK_NONE; /* no connect by default */
1413 if (!array_of_epid_mask[i])
1414 continue;
1415 if (op == PTL_OP_CONNECT) {
1416 epid = array_of_epid[i];
1417 if (!amsh_epid_reachable(ptl, epid)) {
1418 array_of_errors[i] = PSM_EPID_UNREACHABLE;
1419 array_of_epaddr[i] = NULL;
1420 continue;
1421 }
1422 _IPATH_VDBG("looking at epid %llx\n", (unsigned long long) epid);
1423 epaddr = psmi_epid_lookup(ptl->ep, epid);
1424 if (epaddr != NULL) {
1425 if (epaddr->ptl != ptl) {
1426 array_of_errors[i] = PSM_EPID_UNREACHABLE;
1427 array_of_epaddr[i] = NULL;
1428 continue;
1429 }
1430 cstate = AMSH_CSTATE_TO_GET(epaddr);
1431 if (cstate == AMSH_CSTATE_TO_ESTABLISHED) {
1432 array_of_epaddr[i] = epaddr;
1433 array_of_errors[i] = PSM_OK;
1434 }
1435 else {
1436 psmi_assert(cstate == AMSH_CSTATE_TO_NONE);
1437 array_of_errors[i] = PSM_TIMEOUT;
1438 array_of_epaddr[i] = epaddr;
1439 req->epid_mask[i] = AMSH_CMASK_PREREQ;
1440 }
1441 }
1442 else {
1443 req->epid_mask[i] = AMSH_CMASK_PREREQ;
1444 array_of_epaddr[i] = NULL;
1445
1446 #ifdef PSM_HAVE_SCIF
1447 psm_error_t err = amsh_scif_setup(ptl, req->epids[i]);
1448 if(err != PSM_OK) {
1449 psmi_free(req->epid_mask);
1450 psmi_free(req);
1451 return err;
1452 }
1453 #endif
1454 }
1455 }
1456 else { /* disc or abort */
1457 epaddr = array_of_epaddr[i];
1458 psmi_assert(epaddr != NULL);
1459 cstate = AMSH_CSTATE_TO_GET(epaddr);
1460 if (cstate == AMSH_CSTATE_TO_ESTABLISHED) {
1461 req->epid_mask[i] = AMSH_CMASK_PREREQ;
1462 _IPATH_VDBG("Just set index %d to AMSH_CMASK_PREREQ\n", i);
1463 }
1464 /* XXX undef ? */
1465 }
1466 if (req->epid_mask[i] != AMSH_CMASK_NONE)
1467 req->numep_left++;
1468 }
1469
1470 if (req->numep_left == 0) { /* nothing to do */
1471 psmi_free(req->epid_mask);
1472 psmi_free(req);
1473 _IPATH_VDBG("Nothing to connect, bump up phase\n");
1474 ptl->connect_phase++;
1475 *req_o = NULL;
1476 return PSM_OK;
1477 }
1478 else {
1479 *req_o = req;
1480 return PSM_OK_NO_PROGRESS;
1481 }
1482 }
1483
1484 static
1485 psm_error_t
1486 amsh_ep_connreq_poll(ptl_t *ptl, struct ptl_connection_req *req)
1487 {
1488 int i, j, cstate, shmidx;
1489 psm_error_t err = PSM_OK;
1490 psm_epid_t epid;
1491 psm_epaddr_t epaddr;
1492
1493 if (req == NULL || req->isdone)
1494 return PSM_OK;
1495
1496 psmi_assert_always(ptl->ep->amsh_dirpage != NULL);
1497 psmi_assert_always(ptl->connect_phase == req->phase);
1498
1499 if (req->op == PTL_OP_DISCONNECT || req->op == PTL_OP_ABORT) {
1500 for (i = 0; i < req->numep; i++) {
1501 if (req->epid_mask[i] == AMSH_CMASK_NONE ||
1502 req->epid_mask[i] == AMSH_CMASK_DONE)
1503 continue;
1504
1505 epaddr = req->epaddr[i];
1506 psmi_assert(epaddr != NULL);
1507 if (req->epid_mask[i] == AMSH_CMASK_PREREQ) {
1508 int shmidx = epaddr->_shmidx;
1509 #ifdef PSM_HAVE_SCIF
1510 if (shmidx < PTL_AMSH_MAX_LOCAL_PROCS) { /* not remote nodes */
1511 #endif
1512 /* Make sure the target of the disconnect is still there */
1513 pthread_mutex_lock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
1514 if (ptl->ep->amsh_dirpage->shmidx_map_epid[shmidx] != epaddr->epid) {
1515 req->numep_left--;
1516 req->epid_mask[i] = AMSH_CMASK_DONE;
1517 AMSH_CSTATE_TO_SET(epaddr, NONE);
1518 }
1519 pthread_mutex_unlock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
1520 #ifdef PSM_HAVE_SCIF
1521 }
1522 #endif
1523 }
1524
1525 if (req->epid_mask[i] == AMSH_CMASK_PREREQ) {
1526 req->args[0].u32w0 = PSMI_AM_DISC_REQ;
1527 req->args[0].u32w1 = ptl->connect_phase;
1528 req->args[1].u64w0 = (uint64_t) ptl->epid;
1529 req->args[2].u32w0 = PSMI_VERNO;
1530 req->args[2].u32w1 = PSM_OK;
1531 req->args[3].u64w0 = (uint64_t)(uintptr_t)&req->errors[i];
1532 psmi_amsh_short_request(ptl, epaddr,
1533 amsh_conn_handler_hidx,
1534 req->args, 4, NULL, 0, 0);
1535 req->epid_mask[i] = AMSH_CMASK_POSTREQ;
1536 }
1537 else if (req->epid_mask[i] == AMSH_CMASK_POSTREQ) {
1538 cstate = AMSH_CSTATE_TO_GET(epaddr);
1539 if (cstate == AMSH_CSTATE_TO_DISC_REPLIED) {
1540 req->numep_left--;
1541 req->epid_mask[i] = AMSH_CMASK_DONE;
1542 AMSH_CSTATE_TO_SET(epaddr, NONE);
1543 }
1544 }
1545 }
1546 }
1547 else {
1548 /* First see if we've made progress on any postreqs */
1549 int n_prereq = 0;
1550 for (i = 0; i < req->numep; i++) {
1551 int cstate;
1552 if (req->epid_mask[i] != AMSH_CMASK_POSTREQ) {
1553 if (req->epid_mask[i] == AMSH_CMASK_PREREQ)
1554 n_prereq++;
1555 continue;
1556 }
1557 epaddr = req->epaddr[i];
1558 psmi_assert(epaddr != NULL);
1559 cstate = AMSH_CSTATE_TO_GET(epaddr);
1560 if (cstate == AMSH_CSTATE_TO_REPLIED) {
1561 req->numep_left--;
1562 AMSH_CSTATE_TO_SET(epaddr, ESTABLISHED);
1563 req->epid_mask[i] = AMSH_CMASK_DONE;
1564 continue;
1565 }
1566 }
1567 if (n_prereq > 0) {
1568 char buf[32];
1569 uint16_t their_verno;
1570
1571 psmi_assert(req->numep_left > 0);
1572 /* Go through the list of peers we need to connect to and find out
1573 * if they each shared ep is mapped into shm */
1574 pthread_mutex_lock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
1575 for (i = 0; i < req->numep; i++) {
1576 if (req->epid_mask[i] != AMSH_CMASK_PREREQ)
1577 continue;
1578 epid = req->epids[i];
1579 epaddr = req->epaddr[i];
1580
1581 #if PSM_HAVE_SCIF
1582 /* Get the peer node-ID and scif port # from epid */
1583 int nodeid = (int)((epid>>48)&0xff);
1584 if (nodeid != ptl->ep->scif_mynodeid) {
1585 int peeridx = (int)((epid>>56)&0xff);
1586
1587 //Don't use a loop, compute the shmidx directly.
1588 if(nodeid < ptl->ep->scif_mynodeid) {
1589 shmidx =
1590 (nodeid + 1) * PTL_AMSH_MAX_LOCAL_PROCS + peeridx;
1591 } else {
1592 shmidx = nodeid * PTL_AMSH_MAX_LOCAL_PROCS + peeridx;
1593 }
1594
1595 psmi_assert(shmidx >= PTL_AMSH_MAX_LOCAL_PROCS);
1596 their_verno = ptl->ep->amsh_qdir[shmidx].amsh_verno;
1597 } else
1598 #endif
1599 {
1600 /* Go through mapped epids and find the epid we're looking for */
1601 for (shmidx = -1, j = 0; j <=
1602 ptl->ep->amsh_dirpage->max_idx; j++) {
1603 /* epid is connected and ready to go */
1604 if (ptl->ep->amsh_dirpage->shmidx_map_epid[j] == epid) {
1605 shmidx = j;
1606 break;
1607 }
1608 }
1609
1610 if (shmidx == -1) /* couldn't find epid, go to next */
1611 continue;
1612 their_verno = ptl->ep->amsh_dirpage->psm_verno[shmidx];
1613 }
1614
1615 /* Before we even send the request out, check to see if
1616 * versions are interoperable */
1617 if (!psmi_verno_isinteroperable(their_verno)) {
1618 snprintf(buf,sizeof buf, "%d.%d",
1619 PSMI_VERNO_GET_MAJOR(their_verno),
1620 PSMI_VERNO_GET_MINOR(their_verno));
1621
1622 _IPATH_INFO(
1623 "Local endpoint id %" PRIx64 " has version %s "
1624 "which is not supported by library version %d.%d",
1625 epid, buf, PSM_VERNO_MAJOR, PSM_VERNO_MINOR);
1626 req->errors[i] = PSM_EPID_INVALID_VERSION;
1627 req->numep_left--;
1628 req->epid_mask[i] = AMSH_CMASK_DONE;
1629 continue;
1630 }
1631
1632 if (epaddr != NULL) {
1633 psmi_assert(epaddr->_shmidx == shmidx);
1634 }
1635 else if ((epaddr = psmi_epid_lookup(ptl->ep, epid)) == NULL) {
1636 if ((err = amsh_epaddr_add(ptl, epid, shmidx, &epaddr))) {
1637 pthread_mutex_unlock(
1638 (pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
1639 return err;
1640 }
1641 }
1642
1643 req->epaddr[i] = epaddr;
1644 req->args[0].u32w0 = PSMI_AM_CONN_REQ;
1645 req->args[0].u32w1 = ptl->connect_phase;
1646 req->args[1].u64w0 = (uint64_t) ptl->epid;
1647 req->args[2].u32w0 = PSMI_VERNO;
1648 req->args[2].u32w1 = PSM_OK;
1649 req->args[3].u64w0 = (uint64_t)(uintptr_t)&req->errors[i];
1650 req->epid_mask[i] = AMSH_CMASK_POSTREQ;
1651 psmi_amsh_short_request(ptl, epaddr, amsh_conn_handler_hidx,
1652 req->args, 4, NULL, 0, 0);
1653 _IPATH_PRDBG("epaddr=%p, epid=%" PRIx64 " at shmidx=%d\n",
1654 epaddr, epid, shmidx);
1655 }
1656 pthread_mutex_unlock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
1657 }
1658 }
1659
1660 if (req->numep_left == 0) { /* we're all done */
1661 req->isdone = 1;
1662 return PSM_OK;
1663 }
1664 else {
1665 sched_yield();
1666 return PSM_OK_NO_PROGRESS;
1667 }
1668 }
1669
1670 static
1671 psm_error_t
1672 amsh_ep_connreq_fini(ptl_t *ptl, struct ptl_connection_req *req)
1673 {
1674 psm_error_t err = PSM_OK;
1675 int i;
1676
1677 /* Whereever we are at in our connect process, we've been instructed to
1678 * finish the connection process */
1679 if (req == NULL)
1680 return PSM_OK;
1681
1682 /* This prevents future connect replies from referencing data structures
1683 * that disappeared */
1684 ptl->connect_phase++;
1685
1686 /* First process any leftovers in postreq or prereq */
1687 for (i = 0; i < req->numep; i++) {
1688 if (req->epid_mask[i] == AMSH_CMASK_NONE)
1689 continue;
1690 else if (req->epid_mask[i] == AMSH_CMASK_POSTREQ) {
1691 int cstate;
1692 req->epid_mask[i] = AMSH_CMASK_DONE;
1693 cstate = AMSH_CSTATE_TO_GET(req->epaddr[i]);
1694 if (cstate == AMSH_CSTATE_TO_REPLIED) {
1695 req->numep_left--;
1696 AMSH_CSTATE_TO_SET(req->epaddr[i], ESTABLISHED);
1697 }
1698 else { /* never actually got reply */
1699 req->errors[i] = PSM_TIMEOUT;
1700 }
1701 }
1702 /* If we couldn't go from prereq to postreq, that means we couldn't
1703 * find the shmidx for an epid in time. This can only be a case of
1704 * time out */
1705 else if (req->epid_mask[i] == AMSH_CMASK_PREREQ) {
1706 req->errors[i] = PSM_TIMEOUT;
1707 req->numep_left--;
1708 req->epaddr[i] = NULL;
1709 req->epid_mask[i] = AMSH_CMASK_DONE;
1710 }
1711 }
1712
1713 /* Whatever is left can only be in DONE or NONE state */
1714 for (i = 0; i < req->numep; i++) {
1715 if (req->epid_mask[i] == AMSH_CMASK_NONE)
1716 continue;
1717 psmi_assert(req->epid_mask[i] == AMSH_CMASK_DONE);
1718
1719 err = psmi_error_cmp(err, req->errors[i]);
1720 /* Report errors in connection. */
1721 /* XXX de-alloc epaddr */
1722 }
1723
1724 psmi_free(req->epid_mask);
1725 psmi_free(req);
1726
1727 return err;
1728 }
1729
1730 /* Wrapper for 2.0's use of connect/disconect. The plan is to move the
1731 * init/poll/fini interface up to the PTL level for 2.2 */
1732 #define CONNREQ_ZERO_POLLS_BEFORE_YIELD 20
1733 static
1734 psm_error_t
1735 amsh_ep_connreq_wrap(ptl_t *ptl, int op,
1736 int numep,
1737 const psm_epid_t *array_of_epid,
1738 const int array_of_epid_mask[],
1739 psm_error_t *array_of_errors,
1740 psm_epaddr_t *array_of_epaddr,
1741 uint64_t timeout_ns)
1742 {
1743 psm_error_t err;
1744 uint64_t t_start;
1745 struct ptl_connection_req *req = NULL;
1746 int num_polls_noprogress = 0;
1747 static int shm_polite_attach = -1;
1748
1749 if (shm_polite_attach == -1) {
1750 char *p = getenv("PSM_SHM_POLITE_ATTACH");
1751 if (p && *p && atoi(p) != 0) {
1752 fprintf(stderr, "%s: Using Polite SHM segment attach\n",
1753 psmi_gethostname());
1754 shm_polite_attach = 1;
1755 }
1756 shm_polite_attach = 0;
1757 }
1758
1759 /* Initialize */
1760 err = amsh_ep_connreq_init(ptl, op, numep,
1761 array_of_epid, array_of_epid_mask, array_of_errors,
1762 array_of_epaddr, &req);
1763 if (err != PSM_OK_NO_PROGRESS) /* Either we're all done with connect or
1764 * there was an error */
1765 return err;
1766
1767 /* Poll until either
1768 * 1. We time out
1769 * 2. We are done with connecting
1770 */
1771 t_start = get_cycles();
1772 do {
1773 psmi_poll_internal(ptl->ep, 1);
1774 err = amsh_ep_connreq_poll(ptl, req);
1775 if (err == PSM_OK)
1776 break; /* Finished before timeout */
1777 else if (err != PSM_OK_NO_PROGRESS) {
1778 psmi_free(req->epid_mask);
1779 psmi_free(req);
1780 goto fail;
1781 } else if (shm_polite_attach &&
1782 ++num_polls_noprogress == CONNREQ_ZERO_POLLS_BEFORE_YIELD) {
1783 num_polls_noprogress = 0;
1784 PSMI_PYIELD();
1785 }
1786 }
1787 while (psmi_cycles_left(t_start, timeout_ns));
1788
1789 err = amsh_ep_connreq_fini(ptl, req);
1790
1791 /* Ensure that both sides of all connections are established before
1792 returning. This prevents MPI-level deadlocks where one rank returns from
1793 here before responding to another ranks handshake and enters a barrier
1794 (which does not poll PSM). That other rank stays in PSM, never
1795 receiving the handshake, and never entering the barrier: deadlock. */
1796 /* This is fixed by Intel MPI 5.0. */
1797 #if 0
1798 if(op == PTL_OP_CONNECT) {
1799 while(ptl->connect_to > ptl->connect_from) {
1800 psmi_poll_internal(ptl->ep, 1);
1801 }
1802 } else { //ABORT or DISCONNECT
1803 while(ptl->connect_to < ptl->connect_from) {
1804 psmi_poll_internal(ptl->ep, 1);
1805 }
1806 }
1807 #endif
1808
1809 fail:
1810 return err;
1811 }
1812
1813 static
1814 psm_error_t
1815 amsh_ep_connect(ptl_t *ptl,
1816 int numep,
1817 const psm_epid_t *array_of_epid,
1818 const int array_of_epid_mask[],
1819 psm_error_t *array_of_errors,
1820 psm_epaddr_t *array_of_epaddr,
1821 uint64_t timeout_ns)
1822 {
1823 return amsh_ep_connreq_wrap(ptl, PTL_OP_CONNECT, numep, array_of_epid,
1824 array_of_epid_mask, array_of_errors,
1825 array_of_epaddr, timeout_ns);
1826 }
1827
1828 static
1829 psm_error_t
1830 amsh_ep_disconnect(ptl_t *ptl, int force, int numep,
1831 const psm_epaddr_t array_of_epaddr[],
1832 const int array_of_epaddr_mask[],
1833 psm_error_t array_of_errors[],
1834 uint64_t timeout_ns)
1835 {
1836 return amsh_ep_connreq_wrap(ptl, force ? PTL_OP_ABORT : PTL_OP_DISCONNECT,
1837 numep, NULL, array_of_epaddr_mask, array_of_errors,
1838 (psm_epaddr_t *) array_of_epaddr, timeout_ns);
1839 }
1840
1841 /* am_ctl_getslot_remote_inner works just like am_ctl_getslot_pkt_inner, but
1842 instead of using the tail/lock in the shq, use a separate per-domain
1843 tail/lock. The queue is actually located on a remote node, but tailinfo
1844 is located on the local node (and shared by peers on the same node) */
1845 static
1846 am_pkt_short_t*
1847 am_ctl_getslot_pkt_inner(struct amsh_qtail_info* tailinfo,
1848 volatile am_ctl_qhdr_t *shq,
1849 am_pkt_short_t *pkt0)
1850 {
1851 am_pkt_short_t* pkt;
1852 uint32_t idx;
1853
1854 /* Acquire a slot/packet in the remote queue. */
1855 pthread_spin_lock(&tailinfo->lock);
1856 idx = tailinfo->tail;
1857
1858 /* Careful here -- pkt is pointing to memory on a remote node, so any
1859 accesses will be expensive over PCIE. */
1860 pkt = (void*)((uintptr_t)pkt0 + idx * shq->elem_sz);
1861 if(pkt->flag == QFREE) {
1862 ips_sync_reads();
1863 pkt->flag = QUSED;
1864
1865 tailinfo->tail += 1;
1866 if(tailinfo->tail == shq->elem_cnt) {
1867 tailinfo->tail = 0;
1868 }
1869 } else {
1870 pkt = NULL;
1871 }
1872 pthread_spin_unlock(&tailinfo->lock);
1873
1874 return pkt;
1875 }
1876
1877 /* AWF - leaving this code for now. With the addition of SCIF/symmetric
1878 support, all communication uses the 'remote' path. */
1879 #if 0
1880 #undef CSWAP
1881 /* AWF - cswap appears to be broken.. fix? */
1882 PSMI_ALWAYS_INLINE(
1883 int32_t
1884 cswap(volatile uint32_t *p, uint32_t old_value, uint32_t new_value))
1885 {
1886 asm volatile ("lock cmpxchg %2, %0" :
1887 "+m" (*p), "+a" (old_value) :
1888 "r" (new_value) :
1889 "memory");
1890 return old_value;
1891 }
1892
1893 PSMI_ALWAYS_INLINE(
1894 am_pkt_short_t *
1895 am_ctl_getslot_pkt_inner(volatile am_ctl_qhdr_t *shq, am_pkt_short_t *pkt0)
1896 )
1897 {
1898 am_pkt_short_t *pkt;
1899 uint32_t idx;
1900 #ifndef CSWAP
1901 pthread_spin_lock(&shq->lock);
1902 idx = shq->tail;
1903 pkt = (am_pkt_short_t *)((uintptr_t) pkt0 + idx * shq->elem_sz);
1904 if (pkt->flag == QFREE) {
1905 ips_sync_reads();
1906 pkt->flag = QUSED;
1907 shq->tail += 1;
1908 if (shq->tail == shq->elem_cnt)
1909 shq->tail = 0;
1910 } else {
1911 pkt = NULL;
1912 }
1913 pthread_spin_unlock(&shq->lock);
1914 #else
1915 uint32_t idx_next;
1916 do {
1917 idx = shq->tail;
1918 idx_next = (idx+1 == shq->elem_cnt) ? 0 : idx+1;
1919 } while (cswap(&shq->tail, idx, idx_next) != idx);
1920
1921 pkt = (am_pkt_short_t *)((uintptr_t) pkt0 + idx * shq->elem_sz);
1922 //AWF - why is another cswap needed here? we already have the packet..
1923 //We'll wait until the packet goes from QUSED -> QFREE
1924 // And as soon as it does, toggle it back to QUSED.
1925 while (cswap(&pkt->flag, QFREE, QUSED) != QFREE)
1926 ;
1927 #endif
1928 return pkt;
1929 }
1930 #endif
1931
1932 /* This is safe because 'flag' is at the same offset on both pkt and bulkpkt */
1933 #define am_ctl_getslot_bulkpkt_inner(shq,pkt0) ((am_pkt_bulk_t *) \
1934 am_ctl_getslot_pkt_inner(shq,(am_pkt_short_t *)(pkt0)))
1935
1936 PSMI_ALWAYS_INLINE(
1937 am_pkt_short_t *
1938 am_ctl_getslot_pkt(ptl_t *ptl, int shmidx, int is_reply)
1939 )
1940 {
1941 struct amsh_qtail_info* tailinfo;
1942 volatile am_ctl_qhdr_t *shq;
1943 am_pkt_short_t *pkt0;
1944
1945 /* It's not obvious, but the packet acquisition code below is accessing
1946 memory mapped remotely from a peer on another SCIF node. Thus we
1947 have to make sure a SCIF connection to that peer is already
1948 established. */
1949 #ifdef PSM_HAVE_SCIF
1950 if(shmidx >= PTL_AMSH_MAX_LOCAL_PROCS &&
1951 ptl->ep->amsh_qdir[shmidx].amsh_epd[0] == 0) {
1952 if(amsh_scif_setup(ptl, ptl->ep->amsh_qdir[shmidx].amsh_epid)
1953 != PSM_OK) {
1954 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
1955 "am_ctl_getslot_remote(): amsh_scif_setup failed");
1956 }
1957 }
1958 #endif
1959
1960 if(!is_reply) {
1961 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].reqFifoShort;
1962 shq = &(ptl->ep->amsh_qdir[shmidx].qreqH->shortq);
1963 pkt0 = ptl->ep->amsh_qdir[shmidx].qreqFifoShort;
1964 } else {
1965 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].repFifoShort;
1966 shq = &(ptl->ep->amsh_qdir[shmidx].qrepH->shortq);
1967 pkt0 = ptl->ep->amsh_qdir[shmidx].qrepFifoShort;
1968 }
1969
1970 return am_ctl_getslot_pkt_inner(tailinfo, shq, pkt0);
1971 }
1972
1973 PSMI_ALWAYS_INLINE(
1974 am_pkt_bulk_t *
1975 am_ctl_getslot_med(ptl_t *ptl, int shmidx, int is_reply)
1976 )
1977 {
1978 struct amsh_qtail_info* tailinfo;
1979 volatile am_ctl_qhdr_t *shq;
1980 am_pkt_bulk_t *pkt0;
1981
1982 if(!is_reply) {
1983 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].reqFifoMed;
1984 shq = &(ptl->ep->amsh_qdir[shmidx].qreqH->medbulkq);
1985 pkt0 = ptl->ep->amsh_qdir[shmidx].qreqFifoMed;
1986 } else {
1987 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].repFifoMed;
1988 shq = &(ptl->ep->amsh_qdir[shmidx].qrepH->medbulkq);
1989 pkt0 = ptl->ep->amsh_qdir[shmidx].qrepFifoMed;
1990 }
1991
1992 return (am_pkt_bulk_t*)am_ctl_getslot_pkt_inner(tailinfo,
1993 shq, (am_pkt_short_t*)pkt0);
1994 }
1995
1996 PSMI_ALWAYS_INLINE(
1997 am_pkt_bulk_t *
1998 am_ctl_getslot_long(ptl_t *ptl, int shmidx, int is_reply)
1999 )
2000 {
2001 struct amsh_qtail_info* tailinfo;
2002 volatile am_ctl_qhdr_t *shq;
2003 am_pkt_bulk_t *pkt0;
2004
2005 if(!is_reply) {
2006 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].reqFifoLong;
2007 shq = &(ptl->ep->amsh_qdir[shmidx].qreqH->longbulkq);
2008 pkt0 = ptl->ep->amsh_qdir[shmidx].qreqFifoLong;
2009 } else {
2010 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].repFifoLong;
2011 shq = &(ptl->ep->amsh_qdir[shmidx].qrepH->longbulkq);
2012 pkt0 = ptl->ep->amsh_qdir[shmidx].qrepFifoLong;
2013 }
2014
2015 return (am_pkt_bulk_t*)am_ctl_getslot_pkt_inner(tailinfo,
2016 shq, (am_pkt_short_t*)pkt0);
2017 }
2018
2019 PSMI_ALWAYS_INLINE(
2020 am_pkt_bulk_t *
2021 am_ctl_getslot_huge(ptl_t *ptl, int shmidx, int is_reply)
2022 )
2023 {
2024 struct amsh_qtail_info* tailinfo;
2025 volatile am_ctl_qhdr_t *shq;
2026 am_pkt_bulk_t *pkt0;
2027
2028 if(!is_reply) {
2029 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].reqFifoHuge;
2030 shq = &(ptl->ep->amsh_qdir[shmidx].qreqH->hugebulkq);
2031 pkt0 = ptl->ep->amsh_qdir[shmidx].qreqFifoHuge;
2032 } else {
2033 tailinfo = &ptl->ep->amsh_dirpage->qtails[shmidx].repFifoHuge;
2034 shq = &(ptl->ep->amsh_qdir[shmidx].qrepH->hugebulkq);
2035 pkt0 = ptl->ep->amsh_qdir[shmidx].qrepFifoHuge;
2036 }
2037
2038 return (am_pkt_bulk_t*)am_ctl_getslot_pkt_inner(tailinfo,
2039 shq, (am_pkt_short_t*)pkt0);
2040 }
2041
2042 psmi_handlertab_t psmi_allhandlers[] = {
2043 { 0 },
2044 { amsh_conn_handler },
2045 { psmi_am_mq_handler },
2046 { psmi_am_mq_handler_data },
2047 { psmi_am_mq_handler_rtsmatch },
2048 { psmi_am_mq_handler_rtsdone },
2049 { psmi_am_handler }
2050 };
2051
2052 PSMI_ALWAYS_INLINE(
2053 void
2054 advance_head(volatile am_ctl_qshort_cache_t *hdr))
2055 {
2056 QMARKFREE(hdr->head);
2057 hdr->head++;
2058 if (hdr->head == hdr->end)
2059 hdr->head = hdr->base;
2060 }
2061
2062 #define AMSH_ZERO_POLLS_BEFORE_YIELD 64
2063 #define AMSH_POLLS_BEFORE_PSM_POLL 16
2064
2065 /* XXX this can be made faster. Instead of checking the flag of the head, keep
2066 * a cached copy of the integer value of the tail and compare it against the
2067 * previous one we saw.
2068 * AWF this trick won't work across nodes, since the receiver doesn't have
2069 * access to the tail value.
2070 */
2071
2072 PSMI_ALWAYS_INLINE(
2073 psm_error_t
2074 amsh_poll_internal_inner(ptl_t *ptl, int replyonly, int is_internal))
2075 {
2076 psm_error_t err = PSM_OK_NO_PROGRESS;
2077
2078 /* poll replies */
2079 #ifdef PSM_HAVE_SCIF
2080 int node;
2081 int nnodes = ptl->ep->scif_nnodes;
2082
2083 for(node = 0; node < nnodes; node++) {
2084 if (!QISEMPTY(ptl->repH[node].head->flag)) {
2085 do {
2086 ips_sync_reads();
2087 process_packet(ptl, (am_pkt_short_t *) ptl->repH[node].head, 0);
2088 advance_head(&ptl->repH[node]);
2089 err = PSM_OK;
2090 } while (!QISEMPTY(ptl->repH[node].head->flag));
2091 }
2092 }
2093 #else
2094 if (!QISEMPTY(ptl->repH[0].head->flag)) {
2095 do {
2096 ips_sync_reads();
2097 process_packet(ptl, (am_pkt_short_t *) ptl->repH[0].head, 0);
2098 advance_head(&ptl->repH[0]);
2099 err = PSM_OK;
2100 } while (!QISEMPTY(ptl->repH[0].head->flag));
2101 }
2102 #endif
2103
2104 if (!replyonly) {
2105 /* Request queue not enable for 2.0, will be re-enabled to support long
2106 * replies */
2107 if (!is_internal && ptl->psmi_am_reqq_fifo.first != NULL) {
2108 psmi_am_reqq_drain(ptl);
2109 err = PSM_OK;
2110 }
2111
2112 #ifdef PSM_HAVE_SCIF
2113 for(node = 0; node < nnodes; node++) {
2114 if (!QISEMPTY(ptl->reqH[node].head->flag)) {
2115 do {
2116 ips_sync_reads();
2117 process_packet(ptl,
2118 (am_pkt_short_t *) ptl->reqH[node].head, 1);
2119 advance_head(&ptl->reqH[node]);
2120 err = PSM_OK;
2121 } while (!QISEMPTY(ptl->reqH[node].head->flag));
2122 }
2123 }
2124 #else
2125 if (!QISEMPTY(ptl->reqH[0].head->flag)) {
2126 do {
2127 ips_sync_reads();
2128 process_packet(ptl,
2129 (am_pkt_short_t *) ptl->reqH[0].head, 1);
2130 advance_head(&ptl->reqH[0]);
2131 err = PSM_OK;
2132 } while (!QISEMPTY(ptl->reqH[0].head->flag));
2133 }
2134 #endif
2135 }
2136
2137 if (is_internal) {
2138 if (err == PSM_OK) /* some progress, no yields */
2139 ptl->zero_polls = 0;
2140 else if (++ptl->zero_polls == AMSH_ZERO_POLLS_BEFORE_YIELD) {
2141 /* no progress for AMSH_ZERO_POLLS_BEFORE_YIELD */
2142 sched_yield();
2143 ptl->zero_polls = 0;
2144 }
2145
2146 if (++ptl->amsh_only_polls == AMSH_POLLS_BEFORE_PSM_POLL) {
2147 psmi_poll_internal(ptl->ep, 0);
2148 ptl->amsh_only_polls = 0;
2149 }
2150 }
2151 return err; /* if we actually did something */
2152 }
2153
2154 /* non-inlined version */
2155 static
2156 psm_error_t
2157 amsh_poll_internal(ptl_t *ptl, int replyonly)
2158 {
2159 return amsh_poll_internal_inner(ptl, replyonly, 1);
2160 }
2161
2162 #ifdef PSM_PROFILE
2163 #define AMSH_POLL_UNTIL(ptl,isreply,cond) do { \
2164 PSMI_PROFILE_BLOCK(); \
2165 while (!(cond)) { \
2166 PSMI_PROFILE_REBLOCK( \
2167 amsh_poll_internal(ptl,isreply) == \
2168 PSM_OK_NO_PROGRESS); \
2169 } \
2170 PSMI_PROFILE_UNBLOCK(); \
2171 } while (0)
2172 #else
2173 #define AMSH_POLL_UNTIL(ptl,isreply,cond) do { \
2174 while (!(cond)) { \
2175 amsh_poll_internal(ptl,isreply); \
2176 } \
2177 } while (0)
2178 #endif
2179
2180 static
2181 psm_error_t
2182 amsh_poll(ptl_t *ptl, int replyonly)
2183 {
2184 return amsh_poll_internal_inner(ptl, replyonly, 0);
2185 }
2186
2187 PSMI_ALWAYS_INLINE(
2188 void
2189 am_send_pkt_short(ptl_t *ptl, uint32_t destidx, uint32_t bulkidx,
2190 uint16_t fmt, uint16_t nargs, uint16_t handleridx,
2191 psm_amarg_t *args, const void *src, uint32_t len, int isreply))
2192 {
2193 int i;
2194 volatile am_pkt_short_t *pkt;
2195
2196 AMSH_POLL_UNTIL(ptl, isreply,
2197 (pkt = am_ctl_getslot_pkt(ptl, destidx, isreply)) != NULL);
2198
2199 #ifdef __MIC__
2200 /* On MIC, a local copy of the packet struct should be filled in, then
2201 copied using one vector operation. MIC does not have write combining,
2202 and the acquired packet is in remote (via PCIE) memory, so filling in
2203 each struct member will cause a separate PCIE transaction. Using a
2204 single vector write reduces latency. */
2205 am_pkt_short_t lcl_pkt; /* Local version of packet data */
2206
2207 lcl_pkt.bulkidx = bulkidx;
2208 lcl_pkt.shmidx = ptl->ep->amsh_qdir[destidx].amsh_shmidx;
2209 lcl_pkt.type = fmt;
2210 lcl_pkt.nargs = nargs;
2211 lcl_pkt.handleridx = handleridx;
2212
2213 for (i = 0; i < nargs; i++)
2214 lcl_pkt.args[i] = args[i];
2215
2216 if (fmt == AMFMT_SHORT_INLINE)
2217 mq_copy_tiny((uint32_t *) &lcl_pkt.args[nargs], (uint32_t *) src, len);
2218
2219 /* Skip the memory fences in QMARKREADY; not necessary here. */
2220 //QMARKREADY(lcl_pkt);
2221 lcl_pkt.flag = QREADY;
2222
2223 /* Now copy the local packet data to the remote packet. */
2224 memcpy((void*)pkt, &lcl_pkt, sizeof(am_pkt_short_t));
2225
2226 #else
2227 /* got a free pkt... fill it in */
2228 pkt->bulkidx = bulkidx;
2229 pkt->shmidx = ptl->ep->amsh_qdir[destidx].amsh_shmidx;
2230 pkt->type = fmt;
2231 pkt->nargs = nargs;
2232 pkt->handleridx = handleridx;
2233
2234 for (i = 0; i < nargs; i++)
2235 pkt->args[i] = args[i];
2236
2237 if (fmt == AMFMT_SHORT_INLINE)
2238 mq_copy_tiny((uint32_t *) &pkt->args[nargs], (uint32_t *) src, len);
2239
2240 QMARKREADY(pkt);
2241 #endif
2242 }
2243
2244 /* It's probably unlikely that the alloca below is problematic, but
2245 * in case we think it is, define the next to 1
2246 */
2247 #define ALLOCA_AS_SCRATCH 0
2248
2249 #if ALLOCA_AS_SCRATCH
2250 static char amsh_medscratch[AMMED_SZ];
2251 #endif
2252
2253 #ifdef __MIC__
2254 #define amsh_shm_copy_short memcpy
2255 #define amsh_shm_copy_long memcpy
2256 #define amsh_shm_copy_huge psmi_memcpyo
2257 #else
2258 #define amsh_shm_copy_short psmi_mq_mtucpy
2259 #define amsh_shm_copy_long psmi_mq_mtucpy
2260 #define amsh_shm_copy_huge psmi_memcpyo
2261 #endif
2262
2263 PSMI_ALWAYS_INLINE(
2264 int
2265 psmi_amsh_generic_inner(uint32_t amtype, ptl_t *ptl, psm_epaddr_t epaddr,
2266 psm_handler_t handler, psm_amarg_t *args, int nargs,
2267 const void *src, size_t len, void *dst, int flags))
2268 {
2269 uint16_t type;
2270 uint32_t bulkidx;
2271 uint16_t hidx = (uint16_t) handler;
2272 int destidx = epaddr->_shmidx;
2273 int is_reply = AM_IS_REPLY(amtype);
2274 volatile am_pkt_bulk_t *bulkpkt;
2275
2276 _IPATH_VDBG("%s epaddr=%s, shmidx=%d, type=%d LOOPBACK=%s\n",
2277 is_reply ? "reply" : "request",
2278 psmi_epaddr_get_name(epaddr->epid), epaddr->_shmidx, amtype,
2279 ptl->epaddr == epaddr ? "YES" : "NO");
2280 if (ptl->epaddr == epaddr) { /* loopback */
2281 amsh_am_token_t tok;
2282 void *bufa;
2283
2284 tok.tok.epaddr_from = epaddr;
2285 tok.ptl = ptl;
2286 tok.mq = ptl->ep->mq;
2287 tok.shmidx = ptl->shmidx;
2288 if (len > 0) {
2289 if (AM_IS_LONG(amtype))
2290 bufa = dst;
2291 else {
2292 psmi_assert_always(len <= AMMED_SZ);
2293 #if ALLOCA_AS_SCRATCH
2294 bufa = (void *) amsh_medscratch;
2295 #else
2296 bufa = alloca(len);
2297 #endif
2298 }
2299 psmi_assert(bufa != NULL);
2300 amsh_shm_copy_short((void *) bufa, src, len);
2301 }
2302 else
2303 bufa = NULL;
2304 psmi_handler_fn_t fn =
2305 (psmi_handler_fn_t) psmi_allhandlers[hidx].fn;
2306 fn(&tok, args, nargs, bufa, len);
2307
2308 return 1;
2309 }
2310
2311 switch (amtype) {
2312 case AMREQUEST_SHORT:
2313 case AMREPLY_SHORT:
2314 if (len + (nargs<<3) <= (NSHORT_ARGS<<3)) {
2315 /* Payload fits in args packet */
2316 type = AMFMT_SHORT_INLINE;
2317 bulkidx = len;
2318 }
2319 else {
2320 psmi_assert(len < amsh_qelemsz.qreqFifoMed);
2321 psmi_assert(src != NULL);
2322 type = AMFMT_SHORT;
2323 #if 1
2324 AMSH_POLL_UNTIL(ptl, is_reply,
2325 (bulkpkt = am_ctl_getslot_med(ptl, destidx, is_reply)) != NULL);
2326 #else
2327 /* This version exposes a compiler bug */
2328 while (1) {
2329 bulkpkt = am_ctl_getslot_med(ptl, destidx, is_reply);
2330 if (bulkpkt == NULL)
2331 break;
2332 amsh_poll_internal(ptl, is_reply);
2333 }
2334 #endif
2335 bulkidx = bulkpkt->idx;
2336 bulkpkt->len = len;
2337 _IPATH_VDBG("bulkpkt %p flag is %d from idx %d\n",
2338 bulkpkt, bulkpkt->flag, destidx);
2339 amsh_shm_copy_short((void*) bulkpkt->payload, src, (uint32_t) len);
2340 QMARKREADY(bulkpkt);
2341 }
2342 am_send_pkt_short(ptl, destidx, bulkidx, type, nargs, hidx,
2343 args, src, len, is_reply);
2344 break;
2345
2346 case AMREQUEST_LONG:
2347 case AMREPLY_LONG:
2348 {
2349 uint32_t bytes_left = len;
2350 uint8_t *src_this = (uint8_t *) src;
2351 uint8_t *dst_this = (uint8_t *) dst;
2352 uint32_t bytes_this;
2353 uint32_t mtu_this;
2354 type = (bytes_left >= AMSH_HUGE_BYTES ? AMFMT_HUGE : AMFMT_LONG);
2355 /* XXX put in my shm block */
2356 int destidx_l = AMSH_BULK_PUSH ? destidx : ptl->shmidx;
2357
2358 if (type == AMFMT_HUGE)
2359 mtu_this = is_reply ? amsh_qpkt_max.qrepFifoHuge :
2360 amsh_qpkt_max.qreqFifoHuge;
2361 else
2362 mtu_this = is_reply ? amsh_qpkt_max.qrepFifoLong :
2363 amsh_qpkt_max.qreqFifoLong;
2364
2365 _IPATH_VDBG("[long][%s] src=%p,dest=%p,len=%d,hidx=%d\n",
2366 is_reply ? "rep" : "req", src, dst, (uint32_t)len, hidx);
2367
2368 while (bytes_left) {
2369 if (type == AMFMT_HUGE) {
2370 bytes_this = min(bytes_left, mtu_this);
2371
2372 AMSH_POLL_UNTIL(ptl, is_reply,
2373 (bulkpkt = am_ctl_getslot_huge(ptl, destidx_l, is_reply)) != NULL);
2374 bytes_left -= bytes_this;
2375 if (bytes_left == 0)
2376 type = AMFMT_HUGE_END;
2377 bulkidx = bulkpkt->idx;
2378 amsh_shm_copy_huge((void *) bulkpkt->payload,
2379 src_this, bytes_this);
2380 }
2381 else {
2382 bytes_this = min(bytes_left, mtu_this);
2383 AMSH_POLL_UNTIL(ptl, is_reply,
2384 (bulkpkt = am_ctl_getslot_long(ptl, destidx_l, is_reply)) != NULL);
2385 bytes_left -= bytes_this;
2386 if (bytes_left == 0)
2387 type = AMFMT_LONG_END;
2388 bulkidx = bulkpkt->idx;
2389 amsh_shm_copy_long((void *) bulkpkt->payload, src_this,
2390 bytes_this);
2391
2392 }
2393
2394 bulkpkt->dest = (uintptr_t) dst;
2395 bulkpkt->dest_off =
2396 (uint32_t)((uintptr_t)dst_this - (uintptr_t)dst);
2397 bulkpkt->len = bytes_this;
2398 QMARKREADY(bulkpkt);
2399
2400 am_send_pkt_short(ptl, destidx, bulkidx, type, nargs,
2401 hidx, args, NULL, 0, is_reply);
2402 src_this += bytes_this;
2403 dst_this += bytes_this;
2404 }
2405 break;
2406 }
2407 default:
2408 break;
2409 }
2410 return 1;
2411 }
2412
2413 /* A generic version that's not inlined */
2414 int
2415 psmi_amsh_generic(uint32_t amtype, ptl_t *ptl, psm_epaddr_t epaddr,
2416 psm_handler_t handler, psm_amarg_t *args, int nargs,
2417 const void *src, size_t len, void *dst, int flags)
2418 {
2419 return psmi_amsh_generic_inner(amtype,ptl,epaddr,handler,args,nargs,src,len,
2420 dst,flags);
2421 }
2422
2423 int
2424 psmi_amsh_short_request(ptl_t *ptl, psm_epaddr_t epaddr,
2425 psm_handler_t handler, psm_amarg_t *args, int nargs,
2426 const void *src, size_t len, int flags)
2427 {
2428 return psmi_amsh_generic_inner(AMREQUEST_SHORT, ptl, epaddr, handler, args, nargs,
2429 src, len, NULL, flags);
2430 }
2431
2432 int
2433 psmi_amsh_long_request(ptl_t *ptl, psm_epaddr_t epaddr,
2434 psm_handler_t handler, psm_amarg_t *args, int nargs,
2435 const void *src, size_t len, void *dest, int flags)
2436 {
2437 return psmi_amsh_generic_inner(AMREQUEST_LONG, ptl, epaddr, handler, args, nargs,
2438 src, len, dest, flags);
2439 }
2440
2441 void
2442 psmi_amsh_short_reply(amsh_am_token_t *tok,
2443 psm_handler_t handler, psm_amarg_t *args, int nargs,
2444 const void *src, size_t len, int flags)
2445 {
2446 psmi_amsh_generic_inner(AMREPLY_SHORT, tok->ptl, tok->tok.epaddr_from,
2447 handler, args, nargs, src, len, NULL, flags);
2448 return;
2449 }
2450
2451 void
2452 psmi_amsh_long_reply(amsh_am_token_t *tok,
2453 psm_handler_t handler, psm_amarg_t *args, int nargs,
2454 const void *src, size_t len, void *dest, int flags)
2455 {
2456 psmi_amsh_generic_inner(AMREPLY_LONG, tok->ptl, tok->tok.epaddr_from,
2457 handler, args, nargs, src, len, dest, flags);
2458 return;
2459 }
2460
2461 void
2462 psmi_am_reqq_init(ptl_t *ptl)
2463 {
2464 ptl->psmi_am_reqq_fifo.first = NULL;
2465 ptl->psmi_am_reqq_fifo.lastp = &ptl->psmi_am_reqq_fifo.first;
2466 }
2467
2468 psm_error_t
2469 psmi_am_reqq_drain(ptl_t *ptl)
2470 {
2471 am_reqq_t *reqn = ptl->psmi_am_reqq_fifo.first;
2472 am_reqq_t *req;
2473 psm_error_t err = PSM_OK_NO_PROGRESS;
2474
2475 /* We're going to process the entire list, and running the generic handler
2476 * below can cause other requests to be enqueued in the queue that we're
2477 * processing. */
2478 ptl->psmi_am_reqq_fifo.first = NULL;
2479 ptl->psmi_am_reqq_fifo.lastp = &ptl->psmi_am_reqq_fifo.first;
2480
2481 while ((req = reqn) != NULL) {
2482 err = PSM_OK;
2483 reqn = req->next;
2484 _IPATH_VDBG("push of reqq=%p epaddr=%s localreq=%p remotereq=%p\n", req,
2485 psmi_epaddr_get_hostname(req->epaddr->epid),
2486 (void *) (uintptr_t) req->args[1].u64w0,
2487 (void *) (uintptr_t) req->args[0].u64w0);
2488 psmi_amsh_generic(req->amtype, req->ptl, req->epaddr,
2489 req->handler, req->args, req->nargs, req->src,
2490 req->len, req->dest, req->amflags);
2491 if (req->flags & AM_FLAG_SRC_TEMP)
2492 psmi_free(req->src);
2493 psmi_free(req);
2494 }
2495 return err;
2496 }
2497
2498 void
2499 psmi_am_reqq_add(int amtype, ptl_t *ptl, psm_epaddr_t epaddr,
2500 psm_handler_t handler, psm_amarg_t *args, int nargs,
2501 void *src, size_t len, void *dest, int amflags)
2502 {
2503 int i;
2504 int flags = 0;
2505 am_reqq_t *nreq =
2506 (am_reqq_t *) psmi_malloc(ptl->ep, UNDEFINED, sizeof(am_reqq_t));
2507 psmi_assert_always(nreq != NULL);
2508 _IPATH_VDBG("alloc of reqq=%p, to epaddr=%s, ptr=%p, len=%d, "
2509 "localreq=%p, remotereq=%p\n", nreq,
2510 psmi_epaddr_get_hostname(epaddr->epid), dest,
2511 (int)len, (void *) (uintptr_t) args[1].u64w0,
2512 (void *) (uintptr_t) args[0].u64w0);
2513
2514 psmi_assert(nargs <= 8);
2515 nreq->next = NULL;
2516 nreq->amtype = amtype;
2517 nreq->ptl = ptl;
2518 nreq->epaddr = epaddr;
2519 nreq->handler = handler;
2520 for (i = 0; i < nargs; i++)
2521 nreq->args[i] = args[i];
2522 nreq->nargs = nargs;
2523 if (AM_IS_LONG(amtype) && src != NULL &&
2524 len > 0 && !(amflags & AM_FLAG_SRC_ASYNC))
2525 {
2526 abort();
2527 flags |= AM_FLAG_SRC_TEMP;
2528 nreq->src = psmi_malloc(ptl->ep, UNDEFINED, len);
2529 psmi_assert_always(nreq->src != NULL); /* XXX mem */
2530 amsh_shm_copy_short(nreq->src, src, len);
2531 }
2532 else
2533 nreq->src = src;
2534 nreq->len = len;
2535 nreq->dest = dest;
2536 nreq->amflags = amflags;
2537 nreq->flags = flags;
2538
2539 nreq->next = NULL;
2540 *(ptl->psmi_am_reqq_fifo.lastp) = nreq;
2541 ptl->psmi_am_reqq_fifo.lastp = &nreq->next;
2542 }
2543
2544 static
2545 void
2546 process_packet(ptl_t *ptl, am_pkt_short_t *pkt, int isreq)
2547 {
2548 amsh_am_token_t tok;
2549 psmi_handler_fn_t fn;
2550 int shmidx = pkt->shmidx;
2551
2552 tok.tok.epaddr_from = ptl->ep->amsh_qdir[shmidx].amsh_epaddr;
2553 tok.ptl = ptl;
2554 tok.mq = ptl->ep->mq;
2555 tok.shmidx = shmidx;
2556
2557 uint16_t hidx = (uint16_t) pkt->handleridx;
2558 int myshmidx = ptl->shmidx;
2559 int shmidx_l = AMSH_BULK_PUSH ? myshmidx : shmidx;
2560 uint32_t bulkidx = pkt->bulkidx;
2561 uintptr_t bulkptr;
2562 am_pkt_bulk_t *bulkpkt;
2563
2564 /* It is possible for packets to arrive (the initial ones for connection
2565 establishment) before amsh_epid is set correctly. However this can only
2566 happen for peers in the same node -- those connecting inter-node via
2567 SCIF will always have their epid set first. Since our local nodeid is
2568 encoded in the amsh_epid of all local proces at initialization time,
2569 it can always be safely extracted here, even before the amsh_epid is
2570 set to its proper value for a given peer. */
2571 #ifdef PSM_HAVE_SCIF
2572 int nodeid = (int)((ptl->ep->amsh_qdir[shmidx].amsh_epid >> 48) & 0xff);
2573 #else
2574 const int nodeid = 0;
2575 #endif
2576
2577 fn = (psmi_handler_fn_t) psmi_allhandlers[hidx].fn;
2578 psmi_assert(fn != NULL);
2579 psmi_assert((uintptr_t) pkt > ptl->ep->amsh_blockbase);
2580
2581 if (pkt->type == AMFMT_SHORT_INLINE) {
2582 _IPATH_VDBG("%s inline flag=%d nargs=%d from_idx=%d pkt=%p hidx=%d\n",
2583 isreq ? "request" : "reply",
2584 pkt->flag, pkt->nargs, shmidx, pkt, hidx);
2585
2586 fn(&tok, pkt->args, pkt->nargs, pkt->length > 0 ?
2587 (void *) &pkt->args[pkt->nargs] : NULL, pkt->length);
2588 }
2589 else {
2590 int isend = 0;
2591 switch (pkt->type) {
2592 case AMFMT_SHORT:
2593 if (isreq) {
2594 bulkptr = (uintptr_t)
2595 ptl->ep->amsh_qdir[myshmidx].qptrs[nodeid].qreqFifoMed;
2596 bulkptr += bulkidx * amsh_qelemsz.qreqFifoMed;
2597 } else {
2598 bulkptr = (uintptr_t)
2599 ptl->ep->amsh_qdir[myshmidx].qptrs[nodeid].qrepFifoMed;
2600 bulkptr += bulkidx * amsh_qelemsz.qrepFifoMed;
2601 }
2602 break;
2603
2604 case AMFMT_LONG_END:
2605 isend = 1;
2606 case AMFMT_LONG:
2607 if (isreq) {
2608 bulkptr = (uintptr_t)
2609 ptl->ep->amsh_qdir[shmidx_l].qptrs[nodeid].qreqFifoLong;
2610 bulkptr += bulkidx * amsh_qelemsz.qreqFifoLong;
2611 }
2612 else {
2613 bulkptr = (uintptr_t)
2614 ptl->ep->amsh_qdir[shmidx_l].qptrs[nodeid].qrepFifoLong;
2615 bulkptr += bulkidx * amsh_qelemsz.qrepFifoLong;
2616 }
2617 break;
2618
2619 case AMFMT_HUGE_END:
2620 isend = 1;
2621 case AMFMT_HUGE:
2622 if (isreq) {
2623 bulkptr = (uintptr_t) ptl->ep->amsh_qdir[shmidx_l].qptrs[nodeid].qreqFifoHuge;
2624 bulkptr += bulkidx * amsh_qelemsz.qreqFifoHuge;
2625 }
2626 else {
2627 bulkptr = (uintptr_t) ptl->ep->amsh_qdir[shmidx_l].qptrs[nodeid].qrepFifoHuge;
2628 bulkptr += bulkidx * amsh_qelemsz.qrepFifoHuge;
2629 }
2630 break;
2631 default:
2632 bulkptr = 0;
2633 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
2634 "Unknown/unhandled packet type 0x%x", pkt->type);
2635 return;
2636 }
2637
2638 bulkpkt = (am_pkt_bulk_t *) bulkptr;
2639 _IPATH_VDBG("ep=%p mq=%p type=%d bulkidx=%d flag=%d/%d nargs=%d "
2640 "from_idx=%d pkt=%p/%p hidx=%d\n",
2641 ptl->ep, ptl->ep->mq, pkt->type, bulkidx, pkt->flag,
2642 bulkpkt->flag, pkt->nargs, shmidx, pkt, bulkpkt, hidx);
2643 psmi_assert(bulkpkt->flag == QREADY);
2644 if (pkt->type == AMFMT_SHORT) {
2645 fn(&tok, pkt->args, pkt->nargs,
2646 (void *) bulkpkt->payload, bulkpkt->len);
2647 QMARKFREE(bulkpkt);
2648 }
2649 else {
2650 if (pkt->type == AMFMT_HUGE || pkt->type == AMFMT_HUGE_END)
2651 amsh_shm_copy_huge((void *) (bulkpkt->dest + bulkpkt->dest_off),
2652 bulkpkt->payload, bulkpkt->len);
2653 else
2654 amsh_shm_copy_long((void *) (bulkpkt->dest + bulkpkt->dest_off),
2655 bulkpkt->payload, bulkpkt->len);
2656
2657 /* If this is the last packet, copy args before running the
2658 * handler */
2659 if (isend) {
2660 psm_amarg_t args[8];
2661 int nargs = pkt->nargs;
2662 int i;
2663 void *dest = (void *) bulkpkt->dest;
2664 size_t len = (size_t) (bulkpkt->dest_off + bulkpkt->len);
2665 for (i = 0; i < nargs; i++)
2666 args[i] = pkt->args[i];
2667 QMARKFREE(bulkpkt);
2668 fn(&tok, args, nargs, dest, len);
2669 }
2670 else
2671 QMARKFREE(bulkpkt);
2672 }
2673 }
2674 return;
2675 }
2676
2677 static
2678 psm_error_t
2679 amsh_mq_rndv(ptl_t *ptl, psm_mq_t mq, psm_mq_req_t req,
2680 psm_epaddr_t epaddr, uint64_t tag, const void *buf, uint32_t len)
2681 {
2682 psm_amarg_t args[5] = {};
2683 psm_error_t err = PSM_OK;
2684
2685 args[0].u32w0 = MQ_MSG_RTS;
2686 args[0].u32w1 = len;
2687 args[1].u64w0 = tag;
2688 args[2].u64w0 = (uint64_t)(uintptr_t) req;
2689 args[3].u64w0 = (uint64_t)(uintptr_t) buf;
2690
2691 /* OK so we want to use SCIF DMA here if enabled.
2692 First check: same node? Use existing local path.
2693 */
2694
2695 #ifdef PSM_HAVE_SCIF
2696 int shmidx = epaddr->_shmidx;
2697 if(shmidx < PTL_AMSH_MAX_LOCAL_PROCS) {
2698 #endif
2699 /* Intra-node: consider using kassist methods */
2700 if (ptl->ep->psmi_kassist_mode == PSMI_KASSIST_KNEM_GET)
2701 /* If KNEM Get is active register region for peer to get from */
2702 args[4].u64w0 = knem_register_region((void*) buf, len, PSMI_FALSE);
2703 else
2704 args[4].u64w0 = 0;
2705 #ifdef PSM_HAVE_SCIF
2706 } else {
2707 /* Inter-node: use SCIF DMA */
2708 if(ptl->ep->scif_dma_mode == PSMI_SCIF_DMA_GET &&
2709 ptl->ep->scif_dma_threshold <= len) {
2710 /* Register the memory region with SCIF and pass the offset over. */
2711 off_t offset;
2712
2713 scif_epd_t epd = epaddr->ep->amsh_qdir[shmidx].amsh_epd[0];
2714
2715 err = scif_register_region(epd, (void*)buf, len, &offset);
2716 if(err != PSM_OK) {
2717 return err;
2718 }
2719
2720 args[4].u64w0 = offset;
2721 } else {
2722 args[4].u64w0 = 0;
2723 }
2724 }
2725 #endif
2726
2727 psmi_assert(req != NULL);
2728 req->type = MQE_TYPE_SEND;
2729 req->buf = (void *) buf;
2730 req->buf_len = len;
2731 req->send_msglen = len;
2732 req->send_msgoff = 0;
2733
2734 psmi_amsh_short_request(ptl, epaddr, mq_handler_hidx, args, 5, NULL, 0, 0);
2735
2736 return err;
2737 }
2738
2739 /*
2740 * All shared am mq sends, req can be NULL
2741 */
2742 PSMI_ALWAYS_INLINE(
2743 psm_error_t
2744 amsh_mq_send_inner(psm_mq_t mq, psm_mq_req_t req, psm_epaddr_t epaddr,
2745 uint32_t flags, uint64_t tag, const void *ubuf, uint32_t len))
2746 {
2747 psm_amarg_t args[3] = {};
2748 psm_error_t err = PSM_OK;
2749 int is_blocking = (req == NULL);
2750
2751 if (!flags && len <= psmi_am_max_sizes.request_short) {
2752 if (len <= 32)
2753 args[0].u32w0 = MQ_MSG_TINY;
2754 else
2755 args[0].u32w0 = MQ_MSG_SHORT;
2756 args[1].u64 = tag;
2757
2758 psmi_amsh_short_request(epaddr->ptl, epaddr, mq_handler_hidx, args, 2,
2759 ubuf, len, 0);
2760 }
2761 else if (flags & PSM_MQ_FLAG_SENDSYNC)
2762 goto do_rendezvous;
2763 else if (len <= mq->shm_thresh_rv) {
2764 uint32_t bytes_left = len;
2765 uint32_t bytes_this = min(bytes_left, psmi_am_max_sizes.request_short);
2766 uint8_t *buf = (uint8_t *)ubuf;
2767 args[0].u32w0 = MQ_MSG_LONG;
2768 args[0].u32w1 = len;
2769 args[1].u64 = tag;
2770 psmi_amsh_short_request(epaddr->ptl, epaddr, mq_handler_hidx, args, 2,
2771 buf, bytes_this, 0);
2772 bytes_left -= bytes_this;
2773 buf += bytes_this;
2774 args[2].u32w0 = 0;
2775 while (bytes_left) {
2776 args[2].u32w0 += bytes_this;
2777 bytes_this = min(bytes_left, psmi_am_max_sizes.request_short);
2778 /* Here we kind of bend the rules, and assume that shared-memory
2779 * active messages are delivered in order */
2780 psmi_amsh_short_request(epaddr->ptl, epaddr,
2781 mq_handler_data_hidx, args,
2782 3, buf, bytes_this, 0);
2783 buf += bytes_this;
2784 bytes_left -= bytes_this;
2785 }
2786 }
2787 else {
2788 do_rendezvous:
2789 if (is_blocking) {
2790 req = psmi_mq_req_alloc(mq, MQE_TYPE_SEND);
2791 if_pf (req == NULL)
2792 return PSM_NO_MEMORY;
2793 req->send_msglen = len;
2794 req->tag = tag;
2795 }
2796 err = amsh_mq_rndv(epaddr->ptl,mq,req,epaddr,tag,ubuf,len);
2797
2798 if (err == PSM_OK && is_blocking) { /* wait... */
2799 err = psmi_mq_wait_internal(&req);
2800 }
2801 return err; /* skip eager accounting below */
2802 }
2803
2804 /* All eager async sends are always "all done" */
2805 if (req != NULL) {
2806 req->state = MQ_STATE_COMPLETE;
2807 mq_qq_append(&mq->completed_q, req);
2808 }
2809
2810 mq->stats.tx_num++;
2811 mq->stats.tx_shm_num++;
2812 mq->stats.tx_eager_num++;
2813 mq->stats.tx_eager_bytes += len;
2814
2815 return err;
2816 }
2817
2818 static
2819 psm_error_t
2820 amsh_mq_isend(psm_mq_t mq, psm_epaddr_t epaddr, uint32_t flags,
2821 uint64_t tag, const void *ubuf, uint32_t len, void *context,
2822 psm_mq_req_t *req_o)
2823 {
2824 psm_mq_req_t req = psmi_mq_req_alloc(mq, MQE_TYPE_SEND);
2825 if_pf (req == NULL)
2826 return PSM_NO_MEMORY;
2827
2828 req->send_msglen = len;
2829 req->tag = tag;
2830 req->context = context;
2831
2832 _IPATH_VDBG("[ishrt][%s->%s][n=0][b=%p][l=%d][t=%"PRIx64"]\n",
2833 psmi_epaddr_get_name(epaddr->ep->epid),
2834 psmi_epaddr_get_name(epaddr->epid), ubuf, len, tag);
2835
2836 amsh_mq_send_inner(mq, req, epaddr, flags, tag, ubuf, len);
2837
2838 *req_o = req;
2839 return PSM_OK;
2840 }
2841
2842 static
2843 psm_error_t
2844 amsh_mq_send(psm_mq_t mq, psm_epaddr_t epaddr, uint32_t flags,
2845 uint64_t tag, const void *ubuf, uint32_t len)
2846 {
2847 amsh_mq_send_inner(mq, NULL, epaddr, flags, tag, ubuf, len);
2848
2849 _IPATH_VDBG("[shrt][%s->%s][n=0][b=%p][l=%d][t=%"PRIx64"]\n",
2850 psmi_epaddr_get_name(epaddr->ep->epid),
2851 psmi_epaddr_get_name(epaddr->epid), ubuf, len, tag);
2852
2853 return PSM_OK;
2854 }
2855
2856 /* Kcopy-related handling */
2857 int
2858 psmi_epaddr_kcopy_pid(psm_epaddr_t epaddr)
2859 {
2860 int shmidx = epaddr->_shmidx;
2861 return epaddr->ep->amsh_qdir[shmidx].kassist_pid;
2862 }
2863
2864 static
2865 int
2866 psmi_kcopy_find_minor(int *minor)
2867 {
2868 int i;
2869 char path[128];
2870
2871 /* process-wide kcopy filedescriptor */
2872 static int fd = -1;
2873 static int kcopy_minor = -1;
2874
2875 if (fd >= 0) {
2876 *minor = kcopy_minor;
2877 return fd;
2878 }
2879
2880 for (i = 0; i < 256; i++) {
2881 snprintf(path, sizeof(path), "/dev/kcopy/%02d", i);
2882 fd = open(path, O_WRONLY | O_EXCL);
2883 if (fd >= 0) {
2884 *minor = kcopy_minor = i;
2885 break;
2886 }
2887 }
2888
2889 return fd;
2890 }
2891
2892 static
2893 int
2894 psmi_kcopy_open_minor(int minor)
2895 {
2896 char path[128];
2897
2898 /* process-wide kcopy filedescriptor */
2899 static int fd = -1;
2900 if (fd >= 0)
2901 return fd;
2902
2903 if (minor >= 0 && minor < 256) {
2904 snprintf(path, sizeof(path), "/dev/kcopy/%02d", minor);
2905 fd = open(path, O_WRONLY);
2906 }
2907 return fd;
2908 }
2909
2910 static
2911 const char *
2912 psmi_kassist_getmode(int mode)
2913 {
2914 switch (mode) {
2915 case PSMI_KASSIST_OFF:
2916 return "kassist off";
2917 case PSMI_KASSIST_KCOPY_PUT:
2918 return "kcopy put";
2919 case PSMI_KASSIST_KCOPY_GET:
2920 return "kcopy get";
2921 case PSMI_KASSIST_KNEM_GET:
2922 return "knem get";
2923 case PSMI_KASSIST_KNEM_PUT:
2924 return "knem put";
2925 default:
2926 return "unknown";
2927 }
2928 }
2929
2930 static
2931 int
2932 psmi_get_kassist_mode()
2933 {
2934 int mode = PSMI_KASSIST_MODE_DEFAULT;
2935 union psmi_envvar_val env_kassist;
2936
2937 /* Preserve backward compatibility */
2938 if (!psmi_getenv("PSM_SHM_KCOPY",
2939 "PSM Shared Memory use kcopy (put,get,none)",
2940 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_STR,
2941 (union psmi_envvar_val) "put",
2942 &env_kassist))
2943 {
2944 char *s = env_kassist.e_str;
2945 if (strcasecmp(s, "put") == 0)
2946 mode = PSMI_KASSIST_KCOPY_PUT;
2947 else if (strcasecmp(s, "get") == 0)
2948 mode = PSMI_KASSIST_KCOPY_PUT;
2949 else
2950 mode = PSMI_KASSIST_OFF;
2951 }
2952 else if(!psmi_getenv("PSM_KASSIST_MODE",
2953 "PSM Shared memory kernel assist mode "
2954 "(knem-put, knem-get, kcopy-put, kcopy-get, none)",
2955 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_STR,
2956 (union psmi_envvar_val) PSMI_KASSIST_MODE_DEFAULT_STRING,
2957 &env_kassist))
2958 {
2959 char *s = env_kassist.e_str;
2960 if (strcasecmp(s, "kcopy-put") == 0)
2961 mode = PSMI_KASSIST_KCOPY_PUT;
2962 else if (strcasecmp(s, "kcopy-get") == 0)
2963 mode = PSMI_KASSIST_KCOPY_GET;
2964 else if (strcasecmp(s, "knem-put") == 0)
2965 mode = PSMI_KASSIST_KNEM_PUT;
2966 else if (strcasecmp(s, "knem-get") == 0)
2967 mode = PSMI_KASSIST_KNEM_GET;
2968 else
2969 mode = PSMI_KASSIST_OFF;
2970
2971 #if !defined(PSM_USE_KNEM)
2972 if (mode & PSMI_KASSIST_KNEM) {
2973 _IPATH_ERROR("KNEM kassist mode requested which has not been compiled "
2974 "into this version of PSM. Switching kassist mode off.\n");
2975 mode = PSMI_KASSIST_OFF;
2976 }
2977 #endif
2978 }
2979 else {
2980
2981 #if defined(PSM_USE_KNEM)
2982 int res;
2983
2984 /* KNEM is the preferred access mechanism if available. Else default to
2985 * using KCOPY.
2986 */
2987 res = access(KNEM_DEVICE_FILENAME, R_OK | W_OK);
2988 if (res == 0)
2989 mode = PSMI_KASSIST_KNEM_PUT;
2990 else
2991 mode = PSMI_KASSIST_KCOPY_PUT;
2992 #else
2993 mode = PSMI_KASSIST_KCOPY_PUT;
2994 #endif
2995 }
2996
2997 return mode;
2998 }
2999
3000 #ifdef PSM_HAVE_SCIF
3001 static int
3002 psmi_get_scif_dma_mode()
3003 {
3004 int mode = PSMI_SCIF_DMA_MODE_DEFAULT;
3005 union psmi_envvar_val env_scif_dma;
3006
3007 if(!psmi_getenv("PSM_SCIF_DMA_MODE",
3008 "PSM Shared memory SCIF DMA transport mode "
3009 "(scif-put, scif-get, none)",
3010 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_STR,
3011 (union psmi_envvar_val) PSMI_SCIF_DMA_MODE_DEFAULT_STRING,
3012 &env_scif_dma))
3013 {
3014 char *s = env_scif_dma.e_str;
3015 if (strcasecmp(s, "scif-put") == 0)
3016 mode = PSMI_SCIF_DMA_PUT;
3017 else if (strcasecmp(s, "scif-get") == 0)
3018 mode = PSMI_SCIF_DMA_GET;
3019 else
3020 mode = PSMI_SCIF_DMA_OFF;
3021 }
3022
3023 return mode;
3024 }
3025
3026 static int
3027 psmi_get_scif_dma_threshold()
3028 {
3029 int threshold = PSMI_MQ_RV_THRESH_SCIF_DMA;
3030 union psmi_envvar_val env_scif_dma;
3031
3032 if(!psmi_getenv("PSM_SCIF_DMA_THRESH",
3033 "PSM SCIF DMA (rendezvous) switchover",
3034 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_UINT,
3035 (union psmi_envvar_val) threshold,
3036 &env_scif_dma)) {
3037 threshold = env_scif_dma.e_uint;
3038 }
3039
3040 return threshold;
3041 }
3042
3043 static
3044 const char *
3045 psmi_scif_dma_getmode(int mode)
3046 {
3047 switch (mode) {
3048 case PSMI_SCIF_DMA_OFF:
3049 return "SCIF DMA off";
3050 case PSMI_SCIF_DMA_PUT:
3051 return "SCIF put";
3052 case PSMI_SCIF_DMA_GET:
3053 return "SCIF get";
3054 default:
3055 return "unknown";
3056 }
3057 }
3058 #endif // PSM_HAVE_SCIF
3059
3060 /* Connection handling for shared memory AM.
3061 *
3062 * arg0 => conn_op, result (PSM error type)
3063 * arg1 => epid (always)
3064 * arg2 => version.
3065 * arg3 => pointer to error for replies.
3066 */
3067 static
3068 void
3069 amsh_conn_handler(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
3070 {
3071 int op = args[0].u32w0;
3072 int phase = args[0].u32w1;
3073 psm_epid_t epid = args[1].u64w0;
3074 psm_error_t err = (psm_error_t) args[2].u32w1;
3075 psm_error_t *perr = (psm_error_t *) (uintptr_t) args[3].u64w0;
3076
3077 psm_epaddr_t epaddr;
3078 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
3079 int shmidx = tok->shmidx;
3080 int is_valid;
3081 ptl_t *ptl = tok->ptl;
3082
3083 /* We do this because it's an assumption below */
3084 psmi_assert_always(buf == NULL && len == 0);
3085
3086 _IPATH_VDBG("Conn op=%d, phase=%d, epid=%llx, err=%d\n",
3087 op, phase, (unsigned long long) epid, err);
3088 switch (op) {
3089 case PSMI_AM_CONN_REQ:
3090 _IPATH_VDBG("Connect from %d:%d\n",
3091 (int) psm_epid_nid(epid),
3092 (int) psm_epid_context(epid));
3093
3094 epaddr = psmi_epid_lookup(ptl->ep, epid);
3095 if (epaddr == NULL) {
3096 /* This can be nasty. If the segment moves as a result of
3097 * adding a new peer, we have to fix the input pointer 'args'
3098 * since it comes from a shared memory location */
3099 if ((err = amsh_epaddr_add(ptl, epid, shmidx, &epaddr)))
3100 /* Unfortunately, no way out of here yet */
3101 psmi_handle_error(PSMI_EP_NORETURN, err, "Fatal error "
3102 "in connecting to shm segment");
3103 psmi_assert(psmi_epid_lookup(ptl->ep, epid) != NULL);
3104 }
3105
3106 /* Do some version comparison, error checking if required. */
3107 /* Rewrite args */
3108 ptl->connect_from++;
3109 args[0].u32w0 = PSMI_AM_CONN_REP;
3110 args[1].u64w0 = (psm_epid_t) ptl->epid;
3111 args[2].u32w1 = PSM_OK;
3112 AMSH_CSTATE_FROM_SET(epaddr, ESTABLISHED);
3113 tok->tok.epaddr_from = epaddr; /* adjust token */
3114 psmi_amsh_short_reply(tok, amsh_conn_handler_hidx,
3115 args, narg, NULL, 0, 0);
3116
3117 break;
3118
3119 case PSMI_AM_CONN_REP:
3120 if (ptl->connect_phase != phase) {
3121 _IPATH_VDBG("Out of phase connect reply\n");
3122 return;
3123 }
3124 epaddr = ptl->ep->amsh_qdir[shmidx].amsh_epaddr;
3125 *perr = err;
3126 AMSH_CSTATE_TO_SET(epaddr, REPLIED);
3127 ptl->connect_to++;
3128 break;
3129
3130 case PSMI_AM_DISC_REQ:
3131 epaddr = tok->tok.epaddr_from;
3132 args[0].u32w0 = PSMI_AM_DISC_REP;
3133 args[2].u32w1 = PSM_OK;
3134 AMSH_CSTATE_FROM_SET(epaddr, DISC_REQ);
3135 ptl->connect_from--;
3136 /* Before sending the reply, make sure the process
3137 * is still connected */
3138
3139 is_valid = 1;
3140 #ifdef PSM_HAVE_SCIF
3141 if (shmidx < PTL_AMSH_MAX_LOCAL_PROCS) {
3142 #endif
3143 pthread_mutex_lock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
3144 if (ptl->ep->amsh_dirpage->shmidx_map_epid[shmidx] != epaddr->epid)
3145 is_valid = 0;
3146 pthread_mutex_unlock((pthread_mutex_t *) &(ptl->ep->amsh_dirpage->lock));
3147 #ifdef PSM_HAVE_SCIF
3148 }
3149 #endif
3150
3151 if (is_valid) {
3152 psmi_amsh_short_reply(tok, amsh_conn_handler_hidx,
3153 args, narg, NULL, 0, 0);
3154 }
3155 break;
3156
3157 case PSMI_AM_DISC_REP:
3158 if (ptl->connect_phase != phase) {
3159 _IPATH_VDBG("Out of phase disconnect reply\n");
3160 return;
3161 }
3162 *perr = err;
3163 epaddr = tok->tok.epaddr_from;
3164 AMSH_CSTATE_TO_SET(epaddr, DISC_REPLIED);
3165 ptl->connect_to--;
3166 break;
3167
3168 default:
3169 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
3170 "Unknown/unhandled connect handler op=%d", op);
3171 break;
3172 }
3173 return;
3174 }
3175
3176 static
3177 size_t
3178 amsh_sizeof(void)
3179 {
3180 return sizeof(ptl_t);
3181 }
3182
3183 /**
3184 * @param ep PSM Endpoint, guaranteed to have initialized epaddr and epid.
3185 * @param ptl Pointer to caller-allocated space for PTL (fill in)
3186 * @param ctl Pointer to caller-allocated space for PTL-control
3187 * structure (fill in)
3188 */
3189 static
3190 psm_error_t
3191 amsh_init(psm_ep_t ep, ptl_t *ptl, ptl_ctl_t *ctl)
3192 {
3193 int shmidx;
3194 psm_error_t err = PSM_OK;
3195
3196 _IPATH_VDBG("PSM Symmetric Mode!\n");
3197 /* Preconditions */
3198 psmi_assert_always(ep != NULL);
3199 psmi_assert_always(ep->epaddr != NULL);
3200 psmi_assert_always(ep->epid != 0);
3201
3202 /* Setup scif listen port and query node information */
3203 /* This is important to get the node count for initializing queues */
3204 #ifdef PSM_HAVE_SCIF
3205 if ((err = amsh_scif_init(ep)))
3206 goto fail;
3207 #endif
3208
3209 /* If we haven't attached to the segment yet, do it now */
3210 if ((err = psmi_shm_attach(ep, &shmidx)))
3211 goto fail;
3212
3213 /* Modify epid with acquired info as below */
3214 ep->epid |= ((((uint64_t)shmidx)&0xFF)<<56);
3215
3216 ptl->ep = ep; /* back pointer */
3217 ptl->epid = ep->epid; /* cache epid */
3218 ptl->epaddr = ep->epaddr; /* cache a copy */
3219 ptl->ctl = ctl;
3220 ptl->zero_polls = 0;
3221
3222 pthread_mutex_init(&ptl->connect_lock, NULL);
3223 ptl->connect_phase = 0;
3224 ptl->connect_from = 0;
3225 ptl->connect_to = 0;
3226
3227 memset(&ptl->amsh_empty_shortpkt, 0, sizeof ptl->amsh_empty_shortpkt);
3228 memset(&ptl->psmi_am_reqq_fifo, 0, sizeof ptl->psmi_am_reqq_fifo);
3229
3230 if ((err = amsh_init_segment(ptl)))
3231 goto fail;
3232
3233 psmi_am_reqq_init(ptl);
3234 memset(ctl, 0, sizeof(*ctl));
3235
3236 /* Fill in the control structure */
3237 ctl->ptl = ptl;
3238 ctl->ep_poll = amsh_poll;
3239 ctl->ep_connect = amsh_ep_connect;
3240 ctl->ep_disconnect = amsh_ep_disconnect;
3241
3242 ctl->mq_send = amsh_mq_send;
3243 ctl->mq_isend = amsh_mq_isend;
3244
3245 ctl->am_short_request = psmi_amsh_am_short_request;
3246 ctl->am_short_reply = psmi_amsh_am_short_reply;
3247
3248 /* No stats in shm (for now...) */
3249 ctl->epaddr_stats_num = NULL;
3250 ctl->epaddr_stats_init = NULL;
3251 ctl->epaddr_stats_get = NULL;
3252
3253 #ifdef PSM_HAVE_SCIF
3254 /* Start a thread to service incoming SCIF connections. */
3255 if (pthread_create(&ptl->ep->scif_thread, NULL,
3256 am_ctl_accept_thread, (void*)ptl)) {
3257 err = psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
3258 "amsh_init_segment(): pthread_create() failed: %d %s",
3259 errno, strerror(errno));
3260 goto fail;
3261 }
3262 #endif
3263
3264 fail:
3265 return err;
3266 }
3267
3268 static
3269 psm_error_t
3270 amsh_fini(ptl_t *ptl, int force, uint64_t timeout_ns)
3271 {
3272 struct psmi_eptab_iterator itor;
3273 psm_epaddr_t epaddr;
3274 psm_error_t err = PSM_OK;
3275 psm_error_t err_seg;
3276 uint64_t t_start = get_cycles();
3277 int i = 0;
3278
3279 /* Close whatever has been left open -- this will be factored out for 2.1 */
3280 if (ptl->connect_to > 0) {
3281 int num_disc = 0;
3282 int *mask;
3283 psm_error_t *errs;
3284 psm_epaddr_t *epaddr_array;
3285
3286 psmi_epid_itor_init(&itor, ptl->ep);
3287 while ((epaddr = psmi_epid_itor_next(&itor))) {
3288 if (epaddr->ptl != ptl)
3289 continue;
3290 if (AMSH_CSTATE_TO_GET(epaddr) == AMSH_CSTATE_TO_ESTABLISHED)
3291 num_disc++;
3292 }
3293 psmi_epid_itor_fini(&itor);
3294
3295 mask = (int *) psmi_calloc(ptl->ep, UNDEFINED, num_disc, sizeof(int));
3296 errs = (psm_error_t *)
3297 psmi_calloc(ptl->ep, UNDEFINED, num_disc, sizeof(psm_error_t));
3298 epaddr_array = (psm_epaddr_t *)
3299 psmi_calloc(ptl->ep, UNDEFINED, num_disc, sizeof(psm_epaddr_t));
3300
3301 if (errs == NULL || epaddr_array == NULL || mask == NULL) {
3302 if (epaddr_array) psmi_free(epaddr_array);
3303 if (errs) psmi_free(errs);
3304 if (mask) psmi_free(mask);
3305 err = PSM_NO_MEMORY;
3306 goto fail;
3307 }
3308 psmi_epid_itor_init(&itor, ptl->ep);
3309 while ((epaddr = psmi_epid_itor_next(&itor))) {
3310 if (epaddr->ptl == ptl) {
3311 if (AMSH_CSTATE_TO_GET(epaddr) == AMSH_CSTATE_TO_ESTABLISHED) {
3312 mask[i] = 1;
3313 epaddr_array[i] = epaddr;
3314 i++;
3315 }
3316 }
3317 }
3318 psmi_epid_itor_fini(&itor);
3319 psmi_assert(i == num_disc && num_disc > 0);
3320 err = amsh_ep_disconnect(ptl, force, num_disc, epaddr_array,
3321 mask, errs, timeout_ns);
3322 psmi_free(mask);
3323 psmi_free(errs);
3324 psmi_free(epaddr_array);
3325 }
3326
3327 //At this point we are never getting a disconnect request from two peers.
3328 //Those peers are polling.. waiting for a response?
3329 //Are we somehow losing a message that arrives somewhere between where we
3330 //start to disconnect, and here?
3331
3332 if (ptl->connect_from > 0 || ptl->connect_to > 0) {
3333 while (ptl->connect_from > 0 || ptl->connect_to > 0) {
3334 if (!psmi_cycles_left(t_start, timeout_ns)) {
3335 err = PSM_TIMEOUT;
3336 _IPATH_VDBG("CCC timed out with from=%d,to=%d\n",
3337 ptl->connect_from,
3338 ptl->connect_to);
3339 break;
3340 }
3341 psmi_poll_internal(ptl->ep, 1);
3342 }
3343 }
3344 else {
3345 _IPATH_VDBG("CCC complete disconnect from=%d,to=%d\n",
3346 ptl->connect_from,
3347 ptl->connect_to);
3348 }
3349
3350 if ((err_seg = psmi_shm_detach(ptl->ep))) {
3351 err = err_seg;
3352 goto fail;
3353 }
3354
3355 /* This prevents poll calls between now and the point where the endpoint is
3356 * deallocated to reference memory that disappeared */
3357 #ifdef PSM_HAVE_SCIF
3358 for(i = 0; i < ptl->ep->scif_nnodes; i++) {
3359 ptl->repH[i].head = &ptl->amsh_empty_shortpkt;
3360 ptl->reqH[i].head = &ptl->amsh_empty_shortpkt;
3361 }
3362 #else
3363 ptl->repH[0].head = &ptl->amsh_empty_shortpkt;
3364 ptl->reqH[0].head = &ptl->amsh_empty_shortpkt;
3365 #endif
3366
3367 return PSM_OK;
3368 fail:
3369 return err;
3370
3371 }
3372
3373 static
3374 psm_error_t
3375 amsh_setopt(const void *component_obj, int optname,
3376 const void *optval, uint64_t optlen)
3377 {
3378 /* No options for AM PTL at the moment */
3379 return psmi_handle_error(NULL, PSM_PARAM_ERR, "Unknown AM ptl option %u.", optname);
3380 }
3381
3382 static
3383 psm_error_t
3384 amsh_getopt(const void *component_obj, int optname,
3385 void *optval, uint64_t *optlen)
3386 {
3387 /* No options for AM PTL at the moment */
3388 return psmi_handle_error(NULL, PSM_PARAM_ERR, "Unknown AM ptl option %u.", optname);
3389 }
3390
3391 /* Only symbol we expose out of here */
3392 struct ptl_ctl_init
3393 psmi_ptl_amsh = {
3394 amsh_sizeof, amsh_init, amsh_fini, amsh_setopt, amsh_getopt
3395 };
3396
3397 #ifdef PSM_HAVE_SCIF
3398 /* Wait for incoming connections on the SCIF listen socket.
3399 When a connection arrives, store the SCIF socket in the correct place and
3400 respond so that the remote process can map our shared queue area.
3401 */
3402 static void* am_ctl_accept_thread(void* arg)
3403 {
3404 ptl_t* ptl = (ptl_t*)arg;
3405 psm_ep_t ep = ptl->ep;
3406 struct scif_portID peer;
3407 scif_epd_t epd;
3408 void* addr;
3409 int peeridx;
3410 int shmidx;
3411 int nodeid;
3412
3413 /* Receive this struct to ID the peer (offset unused). */
3414 /* Send this struct to share memory mapping information. */
3415 struct { off_t offset; int verno; psm_epid_t epid; } inbuf, outbuf;
3416
3417 while(1) {
3418 /* Block on accepting a new connection on the SCIF listen socket. */
3419 if(scif_accept(ep->scif_epd, &peer, &epd, SCIF_ACCEPT_SYNC)) {
3420 if(errno == EINTR) {
3421 /* Time to quit! */
3422 _IPATH_VDBG("SCIF accept thread quitting\n");
3423 pthread_exit(NULL);
3424 return NULL;
3425 }
3426
3427 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
3428 "scif_accept failed: %d %s\n", errno, strerror(errno));
3429 continue;
3430 }
3431
3432 /* Register the shared memory area this peer should access. */
3433 /* SCIF_MAP_FIXED is use to ensure that offset == addr, so that the
3434 returned offset does not need to be tracked as well. */
3435 addr = ep->amsh_qdir[ep->amsh_shmidx].amsh_base;
3436 outbuf.offset = scif_register(epd, addr,
3437 am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES,
3438 (off_t)addr, SCIF_PROT_READ|SCIF_PROT_WRITE, SCIF_MAP_FIXED);
3439
3440 _IPATH_PRDBG("registered addr %p at offset %p length %ld\n",
3441 addr, (void*)outbuf.offset,
3442 am_ctl_sizeof_block() * PTL_AMSH_MAX_LOCAL_NODES);
3443 if(outbuf.offset == SCIF_REGISTER_FAILED) {
3444 psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
3445 "scif_register failed: %d %s\n", errno, strerror(errno));
3446 scif_close(epd);
3447 continue;
3448 }
3449
3450 outbuf.verno = PSMI_VERNO;
3451 outbuf.epid = ep->epid;
3452
3453 if (amsh_scif_send(epd, &outbuf, sizeof(outbuf))) {
3454 psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
3455 "scif_send epd %d failed: %d %s\n",
3456 epd, errno, strerror(errno));
3457 scif_close(epd);
3458 continue;
3459 }
3460
3461 /* Receive peer identification information */
3462 if(amsh_scif_recv(epd, &inbuf, sizeof(inbuf))) {
3463 psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
3464 "scif_recv failed: %d %s\n", errno, strerror(errno));
3465 scif_close(epd);
3466 continue;
3467 }
3468
3469 /* Extract information from the peer's epid. */
3470 nodeid = (int)((inbuf.epid>>48)&0xff);
3471 shmidx = (int)((inbuf.epid>>56)&0xff);
3472
3473 /* Port isn't supposed to match -- we have the peer's listen port,
3474 which won't be the same as the connect socket's port. */
3475 if(peer.node != nodeid) {
3476 psmi_handle_error(NULL, PSM_EP_NO_RESOURCES,
3477 "SCIF node:port %d:%d does not match encoded epid nodeid %d",
3478 peer.node, peer.port, nodeid);
3479 scif_close(epd);
3480 continue;
3481 }
3482
3483 /* Now that the peer's identity is known, store the new connection. */
3484 /* 0 1 mynodeid 3 4 */
3485 /* mynodeid 0 1 3 4 */
3486 if(nodeid > ep->scif_mynodeid) {
3487 peeridx = (PTL_AMSH_MAX_LOCAL_PROCS * nodeid) + shmidx;
3488 } else if(nodeid < ep->scif_mynodeid) {
3489 peeridx = (PTL_AMSH_MAX_LOCAL_PROCS * (nodeid + 1)) + shmidx;
3490 } else {
3491 peeridx = shmidx;
3492 }
3493
3494 ptl->ep->amsh_qdir[peeridx].amsh_epid = inbuf.epid;
3495 ptl->ep->amsh_qdir[peeridx].amsh_verno = inbuf.verno;
3496
3497 /* There are eventually two connections. epd[0] always has the remote
3498 memory mapped region associated with it, and is used to make requests
3499 to that peer. epd[1] exposes our local shared memory, and is used
3500 to respond to remote requests. */
3501 ptl->ep->amsh_qdir[peeridx].amsh_epd[1] = epd;
3502
3503 _IPATH_VDBG(
3504 "shmidx %d accepted %d:%d peeridx %d epd %d shmidx %d\n",
3505 ep->amsh_shmidx, peer.node, peer.port, peeridx,
3506 ep->amsh_qdir[peeridx].amsh_epd[1],
3507 ep->amsh_qdir[peeridx].amsh_shmidx);
3508 }
3509
3510 return NULL;
3511 }
3512 #endif //PSM_HAVE_SCIF
3513