forked from cornelisnetworks/opa-psm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psm_mq.c
1289 lines (1093 loc) · 32.3 KB
/
psm_mq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) 2015 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Contact Information:
Intel Corporation, www.intel.com
BSD LICENSE
Copyright(c) 2015 Intel Corporation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2003-2015 Intel Corporation. All rights reserved. */
#include <sched.h>
#include "psm_user.h"
#include "psm_mq_internal.h"
/*
* Functions to manipulate the expected queue in mq_ep.
*/
/*
* Once the linked lists cross the size limit, this function will enable tag
* hashing and disable the non-hashing fastpath. We need to go back and insert
* reqs into the hash tables where the hashing searches will look for them.
*/
void
psmi_mq_fastpath_disable(psm2_mq_t mq)
{
psm2_mq_req_t *curp, cur;
struct mqq *qp;
unsigned hashvals[NUM_HASH_CONFIGS];
int t = PSM2_ANYTAG_ANYSRC;
mq->nohash_fastpath = 0;
/* Everything in the unexpected_q needs to be duplicated into
each of the (three) unexpected hash tables. */
qp = &mq->unexpected_q;
for (curp = &qp->first; (cur = *curp) != NULL; curp = &cur->next[t]) {
mq->unexpected_hash_len++;
hashvals[PSM2_TAG_SRC] =
hash_64(*(uint64_t *) cur->tag.tag) % NUM_HASH_BUCKETS;
hashvals[PSM2_TAG_ANYSRC] =
hash_32(cur->tag.tag[0]) % NUM_HASH_BUCKETS;
hashvals[PSM2_ANYTAG_SRC] =
hash_32(cur->tag.tag[1]) % NUM_HASH_BUCKETS;
for (t = PSM2_TAG_SRC; t < PSM2_ANYTAG_ANYSRC; t++)
mq_qq_append_which(mq->unexpected_htab,
t, hashvals[t], cur);
}
/* Everything in the expected_q needs to be moved into the
(single) correct expected hash table. */
qp = &mq->expected_q;
for (curp = &qp->first; (cur = *curp) != NULL; /*curp = &cur->next*/) {
/* must read next ptr before remove */
curp = &cur->next[PSM2_ANYTAG_ANYSRC];
if ((cur->tagsel.tag[0] == 0xFFFFFFFF) &&
(cur->tagsel.tag[1] == 0xFFFFFFFF)) {
/* hash tag0 and tag1 */
t = PSM2_TAG_SRC;
hashvals[t] = hash_64(*(uint64_t *) cur->tag.tag) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab,
t, hashvals[t], cur);
} else if (cur->tagsel.tag[0] == 0xFFFFFFFF) {
t = PSM2_TAG_ANYSRC;
hashvals[t] = hash_32(cur->tag.tag[0]) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab,
t, hashvals[t], cur);
} else if (cur->tagsel.tag[1] == 0xFFFFFFFF) {
t = PSM2_ANYTAG_SRC;
hashvals[t] = hash_32(cur->tag.tag[1]) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab,
t, hashvals[t], cur);
} else
continue; /* else, req must stay in ANY ANY */
mq->expected_list_len--;
mq->expected_hash_len++;
mq_qq_remove_which(cur, PSM2_ANYTAG_ANYSRC);
}
}
/* easy threshold to re-enable: if |hash| == 0 && |list| < X
aggressive threshold: if |hash| + |list| < X
even easier: if |hash| + |list| == 0
might be better approach to avoid constant bouncing between modes */
void psmi_mq_fastpath_try_reenable(psm2_mq_t mq)
{
if_pf(mq->nohash_fastpath == 0 &&
mq->unexpected_hash_len == 0 &&
mq->expected_hash_len == 0 &&
mq->unexpected_list_len == 0 &&
mq->expected_list_len == 0){
mq->nohash_fastpath = 1;
}
}
/*
* ! @brief PSM exposed version to allow PTLs to match
*/
/*! @brief Try to match against the MQ using a tag and tagsel
*
* @param[in] mq Message Queue
* @param[in] src Source (sender) epaddr, may be PSM2_MQ_ANY_ADDR.
* @param[in] tag Input Tag
* @param[in] tagsel Input Tag Selector
* @param[in] remove Non-zero to remove the req from the queue
*
* @returns NULL if no match or an mq request if there is a match
*/
static
psm2_mq_req_t
mq_req_match_with_tagsel(psm2_mq_t mq, psm2_epaddr_t src,
psm2_mq_tag_t *tag, psm2_mq_tag_t *tagsel, int remove)
{
psm2_mq_req_t *curp;
psm2_mq_req_t cur;
unsigned hashval;
int i, j = 0;
struct mqq *qp;
if_pt (mq->nohash_fastpath) {
i = j = PSM2_ANYTAG_ANYSRC;
qp = &mq->unexpected_q;
} else if ((tagsel->tag[0] == 0xFFFFFFFF) &&
(tagsel->tag[1] == 0xFFFFFFFF)) {
i = PSM2_TAG_SRC;
hashval = hash_64(*(uint64_t *) tag->tag) % NUM_HASH_BUCKETS;
qp = &mq->unexpected_htab[i][hashval];
} else if (tagsel->tag[0] == 0xFFFFFFFF) {
i = PSM2_TAG_ANYSRC;
hashval = hash_32(tag->tag[0]) % NUM_HASH_BUCKETS;
qp = &mq->unexpected_htab[i][hashval];
} else if (tagsel->tag[1] == 0xFFFFFFFF) {
i = PSM2_ANYTAG_SRC;
hashval = hash_32(tag->tag[1]) % NUM_HASH_BUCKETS;
qp = &mq->unexpected_htab[i][hashval];
} else {
/* unhashable tag */
i = PSM2_ANYTAG_ANYSRC;
qp = &mq->unexpected_q;
}
for (curp = &qp->first; (cur = *curp) != NULL; curp = &cur->next[i]) {
psmi_assert(cur->peer != PSM2_MQ_ANY_ADDR);
if ((src == PSM2_MQ_ANY_ADDR || src == cur->peer) &&
!((tag->tag[0] ^ cur->tag.tag[0]) & tagsel->tag[0]) &&
!((tag->tag[1] ^ cur->tag.tag[1]) & tagsel->tag[1]) &&
!((tag->tag[2] ^ cur->tag.tag[2]) & tagsel->tag[2])) {
/* match! */
if (remove) {
if_pt (i == PSM2_ANYTAG_ANYSRC)
mq->unexpected_list_len--;
else
mq->unexpected_hash_len--;
for (; j < NUM_MQ_SUBLISTS; j++)
mq_qq_remove_which(cur, j);
psmi_mq_fastpath_try_reenable(mq);
}
return cur;
}
}
return NULL;
}
static void mq_add_to_expected_hashes(psm2_mq_t mq, psm2_mq_req_t req)
{
unsigned hashval;
int i;
req->timestamp = mq->timestamp++;
if_pt (mq->nohash_fastpath) {
mq_qq_append(&mq->expected_q, req);
req->q[PSM2_ANYTAG_ANYSRC] = &mq->expected_q;
mq->expected_list_len++;
if_pf (mq->expected_list_len >= HASH_THRESHOLD)
psmi_mq_fastpath_disable(mq);
} else if ((req->tagsel.tag[0] == 0xFFFFFFFF) &&
(req->tagsel.tag[1] == 0xFFFFFFFF)) {
i = PSM2_TAG_SRC;
hashval = hash_64(*(uint64_t *) req->tag.tag) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab, i, hashval, req);
mq->expected_hash_len++;
} else if (req->tagsel.tag[0] == 0xFFFFFFFF) {
i = PSM2_TAG_ANYSRC;
hashval = hash_32(req->tag.tag[0]) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab, i, hashval, req);
mq->expected_hash_len++;
} else if (req->tagsel.tag[1] == 0xFFFFFFFF) {
i = PSM2_ANYTAG_SRC;
hashval = hash_32(req->tag.tag[1]) % NUM_HASH_BUCKETS;
mq_qq_append_which(mq->expected_htab, i, hashval, req);
mq->expected_hash_len++;
} else {
mq_qq_append(&mq->expected_q, req);
req->q[PSM2_ANYTAG_ANYSRC] = &mq->expected_q;
mq->expected_list_len++;
}
}
/*! @brief Try to remove the req in the MQ
*
* @param[in] mq Message Queue
* @param[in] req MQ request
*
* @returns 1 if successfully removed, or 0 if req cannot be found.
*/
static
int mq_req_remove_single(psm2_mq_t mq, psm2_mq_req_t req)
{
int i;
/* item should only exist in one expected queue at a time */
psmi_assert((!!req->q[0] + !!req->q[1] + !!req->q[2] + !!req->q[3]) == 1);
for (i = 0; i < NUM_MQ_SUBLISTS; i++)
if (req->q[i]) /* found */
break;
switch (i) {
case PSM2_ANYTAG_ANYSRC:
mq->expected_list_len--;
break;
case PSM2_TAG_SRC:
case PSM2_TAG_ANYSRC:
case PSM2_ANYTAG_SRC:
mq->expected_hash_len--;
break;
default:
return 0;
}
mq_qq_remove_which(req, i);
psmi_mq_fastpath_try_reenable(mq);
return 1;
}
void psmi_mq_mtucpy(void *vdest, const void *vsrc, uint32_t nchars)
{
unsigned char *dest = (unsigned char *)vdest;
const unsigned char *src = (const unsigned char *)vsrc;
if (nchars >> 2)
hfi_dwordcpy((uint32_t *) dest, (uint32_t *) src, nchars >> 2);
dest += (nchars >> 2) << 2;
src += (nchars >> 2) << 2;
switch (nchars & 0x03) {
case 3:
*dest++ = *src++;
case 2:
*dest++ = *src++;
case 1:
*dest++ = *src++;
}
}
#if 0 /* defined(__x86_64__) No consumers of mtucpy safe */
void psmi_mq_mtucpy_safe(void *vdest, const void *vsrc, uint32_t nchars)
{
unsigned char *dest = (unsigned char *)vdest;
const unsigned char *src = (const unsigned char *)vsrc;
if (nchars >> 2)
hfi_dwordcpy_safe((uint32_t *) dest, (uint32_t *) src,
nchars >> 2);
dest += (nchars >> 2) << 2;
src += (nchars >> 2) << 2;
switch (nchars & 0x03) {
case 3:
*dest++ = *src++;
case 2:
*dest++ = *src++;
case 1:
*dest++ = *src++;
}
}
#endif
PSMI_ALWAYS_INLINE(
psm2_mq_req_t
psmi_mq_iprobe_inner(psm2_mq_t mq, psm2_epaddr_t src,
psm2_mq_tag_t *tag,
psm2_mq_tag_t *tagsel, int remove_req))
{
psm2_mq_req_t req;
PSMI_PLOCK();
req = mq_req_match_with_tagsel(mq, src, tag, tagsel, remove_req);
if (req != NULL) {
PSMI_PUNLOCK();
return req;
}
psmi_poll_internal(mq->ep, 1);
/* try again */
req = mq_req_match_with_tagsel(mq, src, tag, tagsel, remove_req);
PSMI_PUNLOCK();
return req;
}
psm2_error_t
__psm2_mq_iprobe2(psm2_mq_t mq, psm2_epaddr_t src,
psm2_mq_tag_t *tag, psm2_mq_tag_t *tagsel,
psm2_mq_status2_t *status)
{
psm2_mq_req_t req;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
req = psmi_mq_iprobe_inner(mq, src, tag, tagsel, 0);
if (req != NULL) {
if (status != NULL) {
mq_status2_copy(req, status);
}
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
PSM2_LOG_MSG("leaving");
return PSM2_MQ_NO_COMPLETIONS;
}
PSMI_API_DECL(psm2_mq_iprobe2)
psm2_error_t
__psm2_mq_iprobe(psm2_mq_t mq, uint64_t tag, uint64_t tagsel,
psm2_mq_status_t *status)
{
psm2_mq_tag_t rtag;
psm2_mq_tag_t rtagsel;
psm2_mq_req_t req;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
*(uint64_t *) rtag.tag = tag;
#ifdef PSM_DEBUG
rtag.tag[2] = 0;
#endif
*(uint64_t *) rtagsel.tag = tagsel;
rtagsel.tag[2] = 0;
req = psmi_mq_iprobe_inner(mq, PSM2_MQ_ANY_ADDR, &rtag, &rtagsel, 0);
if (req != NULL) {
if (status != NULL) {
mq_status_copy(req, status);
}
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
PSM2_LOG_MSG("leaving");
return PSM2_MQ_NO_COMPLETIONS;
}
PSMI_API_DECL(psm2_mq_iprobe)
psm2_error_t
__psm2_mq_improbe2(psm2_mq_t mq, psm2_epaddr_t src,
psm2_mq_tag_t *tag, psm2_mq_tag_t *tagsel,
psm2_mq_req_t *reqo, psm2_mq_status2_t *status)
{
psm2_mq_req_t req;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
req = psmi_mq_iprobe_inner(mq, src, tag, tagsel, 1);
if (req != NULL) {
if (status != NULL) {
mq_status2_copy(req, status);
}
*reqo = req;
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
*reqo = NULL;
PSM2_LOG_MSG("leaving");
return PSM2_MQ_NO_COMPLETIONS;
}
PSMI_API_DECL(psm2_mq_improbe2)
psm2_error_t
__psm2_mq_improbe(psm2_mq_t mq, uint64_t tag, uint64_t tagsel,
psm2_mq_req_t *reqo, psm2_mq_status_t *status)
{
psm2_mq_tag_t rtag;
psm2_mq_tag_t rtagsel;
psm2_mq_req_t req;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
*(uint64_t *) rtag.tag = tag;
#ifdef PSM_DEBUG
rtag.tag[2] = 0;
#endif
*(uint64_t *) rtagsel.tag = tagsel;
rtagsel.tag[2] = 0;
req = psmi_mq_iprobe_inner(mq, PSM2_MQ_ANY_ADDR, &rtag, &rtagsel, 1);
if (req != NULL) {
if (status != NULL) {
mq_status_copy(req, status);
}
*reqo = req;
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
*reqo = NULL;
PSM2_LOG_MSG("leaving");
return PSM2_MQ_NO_COMPLETIONS;
}
PSMI_API_DECL(psm2_mq_improbe)
psm2_error_t __psm2_mq_cancel(psm2_mq_req_t *ireq)
{
psm2_mq_req_t req = *ireq;
psm2_mq_t mq;
psm2_error_t err = PSM2_OK;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
if (req == NULL) {
PSM2_LOG_MSG("leaving");
return PSM2_MQ_NO_COMPLETIONS;
}
/* Cancelling a send is a blocking operation, and expensive.
* We only allow cancellation of rendezvous sends, consider the eager sends
* as always unsuccessfully cancelled.
*/
PSMI_PLOCK();
mq = req->mq;
if (MQE_TYPE_IS_RECV(req->type)) {
if (req->state == MQ_STATE_POSTED) {
int rc;
rc = mq_req_remove_single(mq, req);
psmi_assert_always(rc);
req->state = MQ_STATE_COMPLETE;
mq_qq_append(&mq->completed_q, req);
err = PSM2_OK;
} else
err = PSM2_MQ_NO_COMPLETIONS;
} else {
err = psmi_handle_error(mq->ep, PSM2_PARAM_ERR,
"Cannot cancel send requests (req=%p)",
req);
}
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_cancel)
/* This is the only PSM function that blocks.
* We handle it in a special manner since we don't know what the user's
* execution environment is (threads, oversubscribing processes, etc).
*
* The status argument can be an instance of either type psm2_mq_status_t or
* psm2_mq_status2_t. Depending on the type, a corresponding status copy
* routine should be passed in.
*/
PSMI_ALWAYS_INLINE(
psm2_error_t
psmi_mq_wait_inner(psm2_mq_req_t *ireq, void *status,
psmi_mq_status_copy_t status_copy,
int do_lock))
{
psm2_error_t err = PSM2_OK;
psm2_mq_req_t req = *ireq;
if (req == PSM2_MQ_REQINVALID) {
return PSM2_OK;
}
if (do_lock)
PSMI_PLOCK();
if (req->state != MQ_STATE_COMPLETE) {
psm2_mq_t mq = req->mq;
/* We'll be waiting on this req, mark it as so */
req->type |= MQE_TYPE_WAITING;
_HFI_VDBG("req=%p, buf=%p, len=%d, waiting\n",
req, req->buf, req->buf_len);
if (req->testwait_callback) {
err = req->testwait_callback(ireq);
if (do_lock)
PSMI_PUNLOCK();
if (status != NULL) {
status_copy(req, status);
}
return err;
}
PSMI_BLOCKUNTIL(mq->ep, err, req->state == MQ_STATE_COMPLETE);
if (err > PSM2_OK_NO_PROGRESS)
goto fail_with_lock;
else
err = PSM2_OK;
}
mq_qq_remove(&req->mq->completed_q, req);
if (status != NULL) {
status_copy(req, status);
}
_HFI_VDBG("req=%p complete, buf=%p, len=%d, err=%d\n",
req, req->buf, req->buf_len, req->error_code);
psmi_mq_req_free(req);
*ireq = PSM2_MQ_REQINVALID;
fail_with_lock:
if (do_lock)
PSMI_PUNLOCK();
return err;
}
psm2_error_t
__psm2_mq_wait2(psm2_mq_req_t *ireq, psm2_mq_status2_t *status)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
rv = psmi_mq_wait_inner(ireq, status,
(psmi_mq_status_copy_t) mq_status2_copy, 1);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_mq_wait2)
psm2_error_t
__psm2_mq_wait(psm2_mq_req_t *ireq, psm2_mq_status_t *status)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
rv = psmi_mq_wait_inner(ireq, status,
(psmi_mq_status_copy_t) mq_status_copy, 1);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_mq_wait)
psm2_error_t psmi_mq_wait_internal(psm2_mq_req_t *ireq)
{
return psmi_mq_wait_inner(ireq, NULL, NULL, 0);
}
/* The status argument can be an instance of either type psm2_mq_status_t or
* psm2_mq_status2_t. Depending on the type, a corresponding status copy
* routine should be passed in.
*/
PSMI_ALWAYS_INLINE(
psm2_error_t
psmi_mq_test_inner(psm2_mq_req_t *ireq, void *status,
psmi_mq_status_copy_t status_copy))
{
psm2_mq_req_t req = *ireq;
psm2_error_t err = PSM2_OK;
PSMI_ASSERT_INITIALIZED();
if (req == PSM2_MQ_REQINVALID) {
return PSM2_OK;
}
if (req->state != MQ_STATE_COMPLETE) {
if (req->testwait_callback) {
PSMI_PLOCK();
err = req->testwait_callback(ireq);
if (status != NULL) {
status_copy(req, status);
}
PSMI_PUNLOCK();
return err;
} else
return PSM2_MQ_NO_COMPLETIONS;
}
if (status != NULL)
status_copy(req, status);
_HFI_VDBG
("req=%p complete, tag=%08x.%08x.%08x buf=%p, len=%d, err=%d\n",
req, req->tag.tag[0], req->tag.tag[1], req->tag.tag[2], req->buf,
req->buf_len, req->error_code);
PSMI_PLOCK();
mq_qq_remove(&req->mq->completed_q, req);
psmi_mq_req_free(req);
PSMI_PUNLOCK();
*ireq = PSM2_MQ_REQINVALID;
return err;
}
psm2_error_t
__psm2_mq_test2(psm2_mq_req_t *ireq, psm2_mq_status2_t *status)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
rv = psmi_mq_test_inner(ireq, status,
(psmi_mq_status_copy_t) mq_status2_copy);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_mq_test2)
psm2_error_t
__psm2_mq_test(psm2_mq_req_t *ireq, psm2_mq_status_t *status)
{
psm2_error_t rv;
PSM2_LOG_MSG("entering");
rv = psmi_mq_test_inner(ireq, status,
(psmi_mq_status_copy_t) mq_status_copy);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_mq_test)
psm2_error_t
__psm2_mq_isend2(psm2_mq_t mq, psm2_epaddr_t dest, uint32_t flags,
psm2_mq_tag_t *stag, const void *buf, uint32_t len,
void *context, psm2_mq_req_t *req)
{
psm2_error_t err;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
psmi_assert(stag != NULL);
PSMI_PLOCK();
err =
dest->ptlctl->mq_isend(mq, dest, flags, stag, buf, len, context,
req);
PSMI_PUNLOCK();
#if 0
#ifdef PSM_VALGRIND
/* If the send isn't completed yet, make sure that we mark the memory as
* unaccessible
*/
if (*req != PSM2_MQ_REQINVALID && (*req)->state != MQ_STATE_COMPLETE)
VALGRIND_MAKE_MEM_NOACCESS(buf, len);
#endif
#endif
psmi_assert(*req != NULL);
(*req)->peer = dest;
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_isend2)
psm2_error_t
__psm2_mq_isend(psm2_mq_t mq, psm2_epaddr_t dest, uint32_t flags, uint64_t stag,
const void *buf, uint32_t len, void *context, psm2_mq_req_t *req)
{
psm2_error_t err;
psm2_mq_tag_t tag;
PSM2_LOG_MSG("entering");
*((uint64_t *) tag.tag) = stag;
tag.tag[2] = 0;
PSMI_ASSERT_INITIALIZED();
PSMI_PLOCK();
err =
dest->ptlctl->mq_isend(mq, dest, flags, &tag, buf, len, context,
req);
PSMI_PUNLOCK();
#if 0
#ifdef PSM_VALGRIND
/* If the send isn't completed yet, make sure that we mark the memory as
* unaccessible
*/
if (*req != PSM2_MQ_REQINVALID && (*req)->state != MQ_STATE_COMPLETE)
VALGRIND_MAKE_MEM_NOACCESS(buf, len);
#endif
#endif
psmi_assert(*req != NULL);
(*req)->peer = dest;
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_isend)
psm2_error_t
__psm2_mq_send2(psm2_mq_t mq, psm2_epaddr_t dest, uint32_t flags,
psm2_mq_tag_t *stag, const void *buf, uint32_t len)
{
psm2_error_t err;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
psmi_assert(stag != NULL);
PSMI_PLOCK();
err = dest->ptlctl->mq_send(mq, dest, flags, stag, buf, len);
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_send2)
psm2_error_t
__psm2_mq_send(psm2_mq_t mq, psm2_epaddr_t dest, uint32_t flags, uint64_t stag,
const void *buf, uint32_t len)
{
psm2_error_t err;
psm2_mq_tag_t tag;
PSM2_LOG_MSG("entering");
*((uint64_t *) tag.tag) = stag;
tag.tag[2] = 0;
PSMI_ASSERT_INITIALIZED();
PSMI_PLOCK();
err = dest->ptlctl->mq_send(mq, dest, flags, &tag, buf, len);
PSMI_PUNLOCK();
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_send)
/*
* Common subroutine to psm2_mq_irecv2 and psm2_mq_imrecv. This code assumes
* that the provided request has been matched, and begins copying message data
* that has already arrived to the user's buffer. Any remaining data is copied
* by PSM polling until the message is complete.
*/
static psm2_error_t
psm2_mq_irecv_inner(psm2_mq_t mq, psm2_mq_req_t req, void *buf, uint32_t len)
{
uint32_t copysz;
PSM2_LOG_MSG("entering");
psmi_assert(MQE_TYPE_IS_RECV(req->type));
switch (req->state) {
case MQ_STATE_COMPLETE:
if (req->buf != NULL) { /* 0-byte messages don't alloc a sysbuf */
copysz = mq_set_msglen(req, len, req->send_msglen);
psmi_mq_mtucpy(buf, (const void *)req->buf, copysz);
psmi_sysbuf_free(req->buf);
}
req->buf = buf;
req->buf_len = len;
mq_qq_append(&mq->completed_q, req);
break;
case MQ_STATE_UNEXP: /* not done yet */
copysz = mq_set_msglen(req, len, req->send_msglen);
/* Copy What's been received so far and make sure we don't receive
* any more than copysz. After that, swap system with user buffer
*/
req->recv_msgoff = min(req->recv_msgoff, copysz);
if (req->recv_msgoff) {
psmi_mq_mtucpy(buf, (const void *)req->buf,
req->recv_msgoff);
}
/* What's "left" is no access */
VALGRIND_MAKE_MEM_NOACCESS((void *)((uintptr_t) buf +
req->recv_msgoff),
len - req->recv_msgoff);
psmi_sysbuf_free(req->buf);
req->state = MQ_STATE_MATCHED;
req->buf = buf;
req->buf_len = len;
break;
case MQ_STATE_UNEXP_RV: /* rendez-vous ... */
copysz = mq_set_msglen(req, len, req->send_msglen);
/* Copy What's been received so far and make sure we don't receive
* any more than copysz. After that, swap system with user buffer
*/
req->recv_msgoff = min(req->recv_msgoff, copysz);
if (req->recv_msgoff) {
psmi_mq_mtucpy(buf, (const void *)req->buf,
req->recv_msgoff);
}
/* What's "left" is no access */
VALGRIND_MAKE_MEM_NOACCESS((void *)((uintptr_t) buf +
req->recv_msgoff),
len - req->recv_msgoff);
if (req->send_msgoff) {
psmi_sysbuf_free(req->buf);
}
req->state = MQ_STATE_MATCHED;
req->buf = buf;
req->buf_len = len;
req->rts_callback(req, 0);
break;
default:
fprintf(stderr, "Unexpected state %d in req %p\n", req->state,
req);
fprintf(stderr, "type=%d, mq=%p, tag=%08x.%08x.%08x\n",
req->type, req->mq, req->tag.tag[0], req->tag.tag[1],
req->tag.tag[2]);
abort();
}
PSM2_LOG_MSG("leaving");
return PSM2_OK;
}
psm2_error_t
__psm2_mq_irecv2(psm2_mq_t mq, psm2_epaddr_t src,
psm2_mq_tag_t *tag, psm2_mq_tag_t *tagsel,
uint32_t flags, void *buf, uint32_t len, void *context,
psm2_mq_req_t *reqo)
{
psm2_error_t err = PSM2_OK;
psm2_mq_req_t req;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
PSMI_PLOCK();
/* First check unexpected Queue and remove req if found */
req = mq_req_match_with_tagsel(mq, src, tag, tagsel, REMOVE_ENTRY);
if (req == NULL) {
/* prepost before arrival, add to expected q */
req = psmi_mq_req_alloc(mq, MQE_TYPE_RECV);
if_pf(req == NULL) {
err = PSM2_NO_MEMORY;
goto ret;
}
req->peer = src;
req->tag = *tag;
req->tagsel = *tagsel;
req->state = MQ_STATE_POSTED;
req->buf = buf;
req->buf_len = len;
req->recv_msglen = len;
req->recv_msgoff = 0;
/* Nobody should touch the buffer after it's posted */
VALGRIND_MAKE_MEM_NOACCESS(buf, len);
mq_add_to_expected_hashes(mq, req);
_HFI_VDBG("buf=%p,len=%d,tag=%08x.%08x.%08x "
" tagsel=%08x.%08x.%08x req=%p\n",
buf, len, tag->tag[0], tag->tag[1], tag->tag[2],
tagsel->tag[0], tagsel->tag[1], tagsel->tag[2], req);
} else {
_HFI_VDBG("unexpected buf=%p,len=%d,tag=%08x.%08x.%08x"
" tagsel=%08x.%08x.%08x req=%p\n", buf, len,
tag->tag[0], tag->tag[1], tag->tag[2],
tagsel->tag[0], tagsel->tag[1], tagsel->tag[2], req);
psm2_mq_irecv_inner(mq, req, buf, len);
}
req->context = context;
ret:
PSMI_PUNLOCK();
*reqo = req;
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_irecv2)
psm2_error_t
__psm2_mq_irecv(psm2_mq_t mq, uint64_t tag, uint64_t tagsel, uint32_t flags,
void *buf, uint32_t len, void *context, psm2_mq_req_t *reqo)
{
psm2_error_t rv;
psm2_mq_tag_t rtag;
psm2_mq_tag_t rtagsel;
PSM2_LOG_MSG("entering");
*(uint64_t *) rtag.tag = tag;
#ifdef PSM_DEBUG
rtag.tag[2] = 0;
#endif
*(uint64_t *) rtagsel.tag = tagsel;
rtagsel.tag[2] = 0;
rv = __psm2_mq_irecv2(mq, PSM2_MQ_ANY_ADDR, &rtag, &rtagsel,
flags, buf, len, context, reqo);
PSM2_LOG_MSG("leaving");
return rv;
}
PSMI_API_DECL(psm2_mq_irecv)
psm2_error_t
__psm2_mq_imrecv(psm2_mq_t mq, uint32_t flags, void *buf, uint32_t len,
void *context, psm2_mq_req_t *reqo)
{
psm2_error_t err = PSM2_OK;
psm2_mq_req_t req = *reqo;
PSM2_LOG_MSG("entering");
PSMI_ASSERT_INITIALIZED();
if (req == PSM2_MQ_REQINVALID) {
err = psmi_handle_error(mq->ep, PSM2_PARAM_ERR,
"Invalid request (req=%p)", req);
} else {
/* Message is already matched -- begin delivering message data to the
user's buffer. */
req->context = context;
PSMI_PLOCK();
psm2_mq_irecv_inner(mq, req, buf, len);
PSMI_PUNLOCK();
}
PSM2_LOG_MSG("leaving");
return err;
}
PSMI_API_DECL(psm2_mq_imrecv)
/* The status argument can be an instance of either type psm2_mq_status_t or
* psm2_mq_status2_t. Depending on the type, a corresponding status copy
* routine should be passed in.
*/
PSMI_ALWAYS_INLINE(
psm2_error_t