summaryrefslogtreecommitdiffstats
path: root/src/usr/mbox/mailboxsp.C
blob: e8b017a70dd766ad182cbd4ef46594268fa006c5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/mailbox/mailboxsp.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2012
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END
/**
 * @file mailboxsp.C
 * @brief mailbox service provider definition
 */

#include "mailboxsp.H"
#include "mboxdd.H"
#include <sys/task.h>
#include <initservice/taskargs.H>
#include <sys/vfs.h>
#include <devicefw/userif.H>
#include <mbox/mbox_reasoncodes.H>

#define HBMBOX_TRACE_NAME HBMBOX_COMP_NAME

using namespace MBOX;

// Defined in mboxdd.C
extern trace_desc_t * g_trac_mbox;
extern trace_desc_t * g_trac_mboxmsg;

const char HBMBOXMSG_TRACE_NAME[] = "MBOXMSG";
trace_desc_t* g_trac_mboxmsg = NULL; // g_trac_mbox;
TRAC_INIT(&g_trac_mboxmsg, HBMBOXMSG_TRACE_NAME, 2048);



/**
 * setup _start and handle barrier
 */
TASK_ENTRY_MACRO( MailboxSp::init );


MailboxSp::MailboxSp()
    :
        iv_msgQ(),
        iv_sendq(),
        iv_respondq(),
        iv_dmaBuffer(),
        iv_trgt(NULL),
        iv_rts(true),
        iv_dma_pend(false)
{
    // mailbox target
    TARGETING::targetService().masterProcChipTargetHandle(iv_trgt);
}

MailboxSp::~MailboxSp()
{
    msg_q_destroy(iv_msgQ);
}

void MailboxSp::init(errlHndl_t& o_errl)
{
    o_errl = Singleton<MailboxSp>::instance()._init();
}

// helper function to start mailbox message handler
void MailboxSp::msg_handler(void * unused)
{
    Singleton<MailboxSp>::instance().msgHandler();
}


errlHndl_t MailboxSp::_init()
{
    errlHndl_t err = NULL;
    size_t rc = 0;
    iv_msgQ = msg_q_create();
    rc = msg_q_register(iv_msgQ, VFS_ROOT_MSG_MBOX);

    // Register to get interrupts for mailbox
    err = INTR::registerMsgQ(iv_msgQ, INTR::FSP_MAILBOX);

    if(!err)
    {
        err = mboxInit(iv_trgt);

        task_create(MailboxSp::msg_handler, NULL);
    }

    // Send message to FSP on base DMA buffer zone
    msg_t * msg = msg_allocate();
    msg->type = MSG_INITIAL_DMA;
    msg->data[0] = 0;
    msg->data[1] = reinterpret_cast<uint64_t>(iv_dmaBuffer.getDmaBufferHead());
    msg->extra_data = NULL;
    MBOX::send(FSP_MAILBOX_MSGQ,msg);

    return err;
}


void MailboxSp::msgHandler()
{
    errlHndl_t err = NULL;

    while(1)
    {
        msg_t* msg = msg_wait(iv_msgQ);

        // First look for responses to messages sent to HB msg queues
        // Theses are responses to sync messages from FSP to HB.
        msg_respond_t * response = iv_respondq.find(msg);
        if(response)
        {
            iv_respondq.erase(response);

            mbox_msg_t mbox_msg;
            mbox_msg.msg_id = response->msg_id;
            mbox_msg.msg_queue_id = response->msg_queue_id;
            mbox_msg.msg_payload = *msg;

            msg_free(msg);
            delete response;

            send_msg(&mbox_msg);

            continue;
        }

        // Now look for other messages
        switch(msg->type)
        {
            // Interrupt from the mailbox hardware
            case MSG_INTR:
                {
                    mbox_msg_t mbox_msg;
                    size_t mbox_msg_size = sizeof(mbox_msg_t);
                    uint64_t mbox_status = 0;

                    // Read message from DD
                    err = DeviceFW::deviceRead(iv_trgt,
                                               static_cast<void*>(&mbox_msg),
                                               mbox_msg_size,
                                               DEVICE_MBOX_ADDRESS(&mbox_status)
                                              );
                    if(err)
                    {
                        err->collectTrace(HBMBOX_TRACE_NAME);
                        err->collectTrace(HBMBOXMSG_TRACE_NAME);
                        // still need to repond to interrupt handler
                        // will cause assert() below.
                    }
                    else
                    {

                        TRACDCOMP(g_trac_mbox,"MBOXSP status=%lx",mbox_status);

                        if(mbox_status & MBOX_HW_ACK)
                        {
                            // send next message if there is one.
                            iv_rts = true;
                            send_msg();
                        }

                        if(mbox_status & MBOX_DATA_PENDING)
                        {
                            trace_msg("RECV",mbox_msg);
                            if(mbox_msg.msg_queue_id == HB_MAILBOX_MSGQ)
                            {
                                // msg to hb mailbox from fsp mbox
                                handle_hbmbox_msg(mbox_msg);
                            }
                            else if((mbox_msg.msg_queue_id==FSP_MAILBOX_MSGQ)&&
                                    (mbox_msg.msg_payload.type ==
                                     MSG_REQUEST_DMA_BUFFERS))
                            {
                                // This is a response from The FSP
                                handle_hbmbox_resp(mbox_msg);
                            }
                            else
                            {
                                // anything else
                                recv_msg(mbox_msg);
                            }
                        }

                        // Look for error status from MB hardware
                        if(mbox_status & MBOX_DOORBELL_ERROR)
                        {
                            TRACFCOMP(g_trac_mbox,
                                      ERR_MRK"MBOX status 0x%lx",
                                      mbox_status);

                            if(mbox_status & MBOX_DATA_WRITE_ERR)
                            {
                                // Write attempted before ACK - HB code error
                                /*@ errorlog tag
                                 * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
                                 * @moduleid        MOD_MBOXSRV_HNDLR
                                 * @reasoncode      RC_DATA_WRITE_ERR
                                 * @userdata1       Status from MB device driver
                                 * @defdesc         Mailbox Data Write attempted
                                 *                  before ACK.
                                 *
                                 */
                                err = new ERRORLOG::ErrlEntry
                                    (
                                     ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                                     MBOX::MOD_MBOXSRV_HNDLR,
                                     MBOX::RC_DATA_WRITE_ERR,  //  reason Code
                                     mbox_status,              //  Status from DD
                                     0
                                    );

                                err->collectTrace(HBMBOX_TRACE_NAME);
                                err->collectTrace(HBMBOXMSG_TRACE_NAME);
                                // will assert later below
                            }

                            else if(mbox_status & MBOX_PARITY_ERR)
                            {
                                // Hardware detected parity error
                                // - TODO How does BB handle this error ???
                                // Log it and continue

                                /*@ errorlog tag
                                 * @errortype       ERRL_SEV_INFORMATIONAL
                                 * @moduleid        MBOX::MOD_MBOXSRV_HNDLR
                                 * @reasoncode      MBOX::RC_PARITY_ERR
                                 * @userdata1       Status from MB device driver
                                 * @defdesc         Mailbox Hardware detected
                                 *                  parity error.
                                 */
                                err = new ERRORLOG::ErrlEntry
                                    (
                                     ERRORLOG::ERRL_SEV_INFORMATIONAL,
                                     MBOX::MOD_MBOXSRV_HNDLR,
                                     MBOX::RC_PARITY_ERR,    //  reason Code
                                     mbox_status,            //  Status from DD
                                     0
                                    );

                                err->collectTrace(HBMBOX_TRACE_NAME);
                                errlCommit(err,HBMBOX_COMP_ID);
                                // err = NULL
                            }
                            else if(mbox_status & MBOX_ILLEGAL_OP)
                            {
                                // Code problem could be FSP or HB 
                                // - log error and continue.

                                /*@ errorlog tag
                                 * @errortype       ERRL_SEV_INFORMATIONAL
                                 * @moduleid        MBOX::MOD_MBOXSRV_HNDLR
                                 * @reasoncode      MBOX::RC_ILLEGAL_OP
                                 * @userdata1       Status from MB device driver
                                 * @defdesc         Retry failed. Bad status
                                 *                  indicated in PIB status reg.
                                 *
                                 */
                                err = new ERRORLOG::ErrlEntry
                                    (
                                     ERRORLOG::ERRL_SEV_INFORMATIONAL,
                                     MBOX::MOD_MBOXSRV_HNDLR,
                                     MBOX::RC_ILLEGAL_OP,    //  reason Code
                                     mbox_status,            //  Status from DD
                                     0
                                    );
                                err->collectTrace(HBMBOX_TRACE_NAME);
                                errlCommit(err,HBMBOX_COMP_ID);
                                // err = NULL
                            }

                            //else if(mbox_status & MBOX_DATA_READ_ERR)
                            //{
                            //    // Read when no message available 
                            //    - Just ignore this one per Dean.
                            //}


                            // The code has the ability to retry sending a
                            // message, but currently there is no error state
                            // that requires it.
                        }
                    }


                    // Respond to the interrupt handler
                    msg->data[0] = 0;
                    msg->data[1] = 0;
                    msg_respond(iv_msgQ,msg);

                    // err will be set if scom failed in mbox DD
                    // or MBOX_DATA_WRITE_ERR  - serious - assert
                    if(err)
                    {
                        // TODO Mask off MB interrupts??
                        errlCommit(err,HBMBOX_COMP_ID);

                        assert(0);
                    }
                }

                break;


            case MSG_SEND:
                {
                    // Build mailbox message
                    mbox_msg_t mbox_msg;
                    mbox_msg.msg_queue_id = static_cast<uint32_t>(msg->data[0]);
                    msg_t * payload = reinterpret_cast<msg_t*>(msg->extra_data);
                    mbox_msg.msg_payload = *payload;  //copy in payload

                    if(msg_is_async(msg))
                    {
                        msg_free(payload);
                        msg_free(msg);
                    }
                    else  //synchronous
                    {
                        msg->data[1] = 0;  // used later for return value

                        // need to watch for a response
                        response = new msg_respond_t(msg);

                        // Convert a 64 bit pointer to a 32 bit unsigned int.
                        mbox_msg.msg_id =
                            static_cast<uint32_t>
                            (reinterpret_cast<uint64_t>(response->key));

                        iv_respondq.insert(response);
                    }

                    send_msg(&mbox_msg);

                }
                break;

            case MSG_REGISTER_MSGQ:
                {
                    errlHndl_t err = NULL;
                    queue_id_t queue_id = static_cast<queue_id_t>(msg->data[0]);
                    msg_q_t msgQ = reinterpret_cast<msg_q_t>(msg->data[1]);
                    err = msgq_register(queue_id,msgQ);
                    msg->data[1] = reinterpret_cast<uint64_t>(err);
                    msg_respond(iv_msgQ,msg);
                }
                break;

            case MSG_UNREGISTER_MSGQ:
                {
                    queue_id_t queue_id = static_cast<queue_id_t>(msg->data[0]);
                    msg_q_t msgQ = msgq_unregister(queue_id);
                    msg->data[1] = reinterpret_cast<uint64_t>(msgQ);
                    msg_respond(iv_msgQ,msg);
                }
                break;

            default: //Huh? Ignore it.

                TRACFCOMP(g_trac_mbox, ERR_MRK "MailboxSp::msgHandler() "
                          "invalid message received 0x%08x",msg->type);

                /*@ errorlog tag
                 * @errortype       ERRL_SEV_INFORMATIONAL
                 * @moduleid        MBOX::MOD_MBOXSRV_HNDLR
                 * @reasoncode      MBOX::RC_INVALID_MBOX_MSG_TYPE
                 * @userdata1       Message type
                 * @defdesc         Invalid message type sent to mailbox msgQ
                 */
                err = new ERRORLOG::ErrlEntry
                    (
                     ERRORLOG::ERRL_SEV_INFORMATIONAL,
                     MBOX::MOD_MBOXSRV_HNDLR,
                     MBOX::RC_INVALID_MBOX_MSG_TYPE,    //  reason Code
                     msg->type,                         // msg type
                     0
                    );

                errlCommit(err,HBMBOX_COMP_ID);
                err = NULL;

                msg_free(msg);

                break;
        }

    } // while(1)
}



// Note: When called due to an ACK or retry, iv_rts should be true.
void MailboxSp::send_msg(mbox_msg_t * i_msg)
{
    if(i_msg)
    {
        iv_sendq.push_back(*i_msg);
    }

    // Can't send now if:
    //  - busy (waiting for ACK)
    //  - a DMA buffer request is pending
    //  - there is nothing to send
    //
    //  TODO future optimization: If both iv_rts and iv_dma_pend are true then
    //  could look for a mesaage in the sendq that does not require a DMA
    //  buffer and send it.
    if(!iv_rts || iv_dma_pend || iv_sendq.size() == 0)
    {
        return;
    }

    errlHndl_t err = NULL;

    mbox_msg_t * mbox_msg = &(iv_sendq.front());;
    msg_t * payload = &(mbox_msg->msg_payload);

    iv_msg_to_send = *mbox_msg; //copy

    // Is a DMA buffer needed?
    if((payload->extra_data != NULL) ||
       ((iv_msg_to_send.msg_queue_id == HB_MAILBOX_MSGQ) &&
        (payload->type == MSG_REQUEST_DMA_BUFFERS)))

    {
        uint64_t dma_size = payload->data[1];

        if(payload->extra_data == NULL) // DMA req. from FSP.
        {
            dma_size = payload->data[0];
        }

        // getBuffer() returns bit map in dma_size variable.
        void * dma_buffer = iv_dmaBuffer.getBuffer(dma_size);

        if(dma_buffer)
        {
            if(payload->extra_data != NULL)
            {
                memcpy(dma_buffer,payload->extra_data,payload->data[1]);
                iv_msg_to_send.msg_payload.extra_data = dma_buffer;

                free(payload->extra_data);
            }
            else  // DMA buffer request from FSP
            {
                iv_msg_to_send.msg_payload.data[0] = dma_size; // bitmap
                iv_msg_to_send.msg_payload.data[1] = 
                    reinterpret_cast<uint64_t>(dma_buffer);
            }
            iv_sendq.pop_front();
        }
        else // can't get buffer
        {
            if(!iv_dmaBuffer.ownsAllBlocks())
            {
                //   -- can't send the current message - leave it on the queue
                //   -- Instead send a message to FSP for more buffers
                iv_msg_to_send.msg_id = 0;
                iv_msg_to_send.msg_queue_id = FSP_MAILBOX_MSGQ;
                iv_msg_to_send.msg_payload.type = MSG_REQUEST_DMA_BUFFERS;
                iv_msg_to_send.msg_payload.data[0] = 0;
                iv_msg_to_send.msg_payload.data[1] = 0;
                iv_msg_to_send.msg_payload.extra_data = NULL;
                iv_msg_to_send.msg_payload.__reserved__async = 1;
                iv_dma_pend = true;
            }
            else
            {
                // message is asking from more DMA space than exists
                TRACFCOMP(g_trac_mbox,
                          ERR_MRK
                          "MailboxSp::send_msg - Message dropped. "
                          "Can't get DMA buffer size %d. queueid 0x%x",
                          payload->data[1],
                          iv_msg_to_send.msg_queue_id);

                iv_sendq.pop_front();
                if(payload->extra_data == NULL)  //Request was from FSP
                {
                    // just respond with failure
                    iv_msg_to_send.msg_payload.data[0] = 0;
                    iv_msg_to_send.msg_payload.data[1] = 0;
                }
                else
                {
                    /*@ errorlog tag
                     * @errortype       ERRL_SEV_INFORMATIONAL
                     * @moduleid        MOD_MBOXSRV_SENDMSG
                     * @reasoncode      RC_INVALID_DMA_LENGTH
                     * @userdata1       DMA length requested
                     * @userdata2       queue_id
                     * @defdesc         Failed to allocate a DMA buffer.
                     *                  Message dropped.
                     */
                    err = new ERRORLOG::ErrlEntry
                        (
                         ERRORLOG::ERRL_SEV_INFORMATIONAL,
                         MBOX::MOD_MBOXSRV_SENDMSG,
                         MBOX::RC_INVALID_DMA_LENGTH,      //  reason Code
                         payload->data[1],                 // DMA data len
                         iv_msg_to_send.msg_queue_id       // MSG queueid
                        );

                    send_msg();  // drop this message, but send next message
                }
            }
        }
    }
    else // simple message
    {
        iv_sendq.pop_front();
    }

    if(!err)
    {
        size_t mbox_msg_len = sizeof(mbox_msg_t);
        iv_rts = false;

        trace_msg("SEND",iv_msg_to_send);

        err = DeviceFW::deviceWrite(iv_trgt,
                                    &iv_msg_to_send,
                                    mbox_msg_len,
                                    DeviceFW::MAILBOX);
    }

    if(err)
    {
        TRACFCOMP(g_trac_mbox,
                  ERR_MRK
                  "MBOX send_msg could not send message. queue:%x type:%x",
                  iv_msg_to_send.msg_queue_id,
                  iv_msg_to_send.msg_payload.type);

        // If the message that could not be sent was a sync msg
        // originating from HB then there is a task waiting for a response.
        if(!msg_is_async(&(iv_msg_to_send.msg_payload)))
        {
            if(iv_msg_to_send.msg_queue_id >= FSP_FIRST_MSGQ)
            {
                msg_t * key = reinterpret_cast<msg_t *>(iv_msg_to_send.msg_id);
                msg_respond_t * response = iv_respondq.find(key);
                if(response)
                {
                    iv_respondq.erase(response);
                    msg_t * msg = response->key;
                    msg->data[1] = reinterpret_cast<uint64_t>(err);
                    err = NULL;
                    msg_respond(iv_msgQ,msg);
                    delete response;
                }
                // else could be request for more DMA buffers
                // - for now just commit the error. TODO recovery options?
            }
            // else msg is a HB response to a FSP originating sync msg
            // What do we do here? Can't respond to the FSP.
            // For now just commit the error. TODO recovery options?
        }
        // else msg was HB originating async - Just commit it.

        if(err) // have error log and no where to respond to, so commit it.
        {
            errlCommit(err,HBMBOX_COMP_ID);
            err = NULL;
        }
    }
}


void MailboxSp::recv_msg(mbox_msg_t & i_mbox_msg)
{
    errlHndl_t err = NULL;
    int rc = 0;

    //trace_msg("RECV",i_mbox_msg);

    msg_t * msg = msg_allocate();
    *msg = i_mbox_msg.msg_payload;  // copy

    // Handle moving data from DMA buffer
    if(msg->extra_data != NULL)
    {
        uint64_t msg_sz = msg->data[1];
        void * buf = malloc(msg_sz);
        memcpy(buf,msg->extra_data,msg_sz);

        iv_dmaBuffer.release(msg->extra_data,msg_sz);
        msg->extra_data = buf;
        // receiver of the message frees buffer.
    }


    // Is sync response message from FSP

    msg_t * key = reinterpret_cast<msg_t *>(i_mbox_msg.msg_id);

    msg_respond_t * response = iv_respondq.find(key);

    // msg_id matches and the msg_queue_id is an FSP queue id
    // If it's a hostboot target then it's not a response.
    if(response && i_mbox_msg.msg_queue_id >= FSP_FIRST_MSGQ)
    {
        // remove from the list
        iv_respondq.erase(response);

        // resonse->key points to original carrier msg
        // reponse->key->extra_data points to the orignal msg
        // Overwrite original message with response

        *(reinterpret_cast<msg_t *>((response->key)->extra_data)) = *msg; // copy

        msg_free(msg);

        msg_respond(iv_msgQ,response->key);

        delete response;
    }
    else
    {
        // New incomming message
        msg_q_t msgq = NULL;

        registry_t::iterator r = iv_registry.find
            (static_cast<queue_id_t>(i_mbox_msg.msg_queue_id));

        if(r != iv_registry.end())
        {
            msgq = r->second;

            // Check that the type is within range
            // TODO also check if in secure mode msg->type > LAST_SECURE_MSG 
            if(msg->type < MSG_FIRST_SYS_TYPE) // &&
                // (!is_secure() || (is_secure() && msg->type > LAST_SECURE_MSG))
            {
                if(msg_is_async(msg))
                {
                    // send msg to queue
                    rc = msg_send(msgq,msg);
                }
                else //sync message from FSP
                {
                    // Need to save queue_id and msg_id and msg ptr
                    // for reponse
                    msg_respond_t * response = new msg_respond_t(msg);
                    response->msg_id = i_mbox_msg.msg_id;
                    response->msg_queue_id = i_mbox_msg.msg_queue_id;

                    iv_respondq.insert(response);

                    rc = msg_sendrecv_noblk(msgq,msg,iv_msgQ);
                }

                // Since we've filtered out illegal types this can only mean
                // msgq or msg has a bad memory reference - code problem
                if(rc)
                {
                    /*@ errorlog tag
                     * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
                     * @moduleid        MBOX::MOD_MBOXSRV_RCV
                     * @reasoncode      MBOX::RC_INVALID_QUEUE
                     * @userdata1       rc from msg_send()
                     * @userdata2       msg queue id
                     * @defdesc         Ivalid msg or msg queue
                     *
                     */
                    err = new ERRORLOG::ErrlEntry
                        (
                         ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                         MBOX::MOD_MBOXSRV_RCV,
                         MBOX::RC_INVALID_QUEUE,    //  reason Code
                         rc,                        // rc from msg_send
                         i_mbox_msg.msg_queue_id
                        );

                    err->collectTrace(HBMBOXMSG_TRACE_NAME);
                    errlCommit(err,HBMBOX_COMP_ID);

                    free(msg->extra_data);
                    msg_free(msg);

                    assert(0);

                }
            }
            else // Bad type range
            {
                //  Send a message to the FSP
                mbox_msg_t mbox_msg;
                mbox_msg.msg_queue_id = FSP_MAILBOX_MSGQ;
                mbox_msg.msg_payload.type = MSG_INVALID_MSG_TYPE;
                mbox_msg.msg_payload.data[0] = msg->type;
                mbox_msg.msg_payload.data[1] = i_mbox_msg.msg_queue_id;
                mbox_msg.msg_payload.extra_data = NULL;
                mbox_msg.msg_payload.__reserved__async = 0; // async


                send_msg(&mbox_msg);

                TRACFCOMP(g_trac_mbox,
                          ERR_MRK
                          "MailboxSp::recv_msg-Message type 0x%x range error."
                          " Message dropped.",
                          msg->type);

                /*@ errorlog tag
                 * @errortype       ERRL_SEV_INFORMATIONAL
                 * @moduleid        MBOX::MOD_MBOXSRV_RCV
                 * @reasoncode      MBOX::RC_INVALID_MESSAGE_TYPE
                 * @userdata1       msg queue
                 * @userdata2       msg type
                 * @defdesc         Message from FSP. Message type is not
                 *                  within a valid range. Message dropped.
                 */
                err = new ERRORLOG::ErrlEntry
                    (
                     ERRORLOG::ERRL_SEV_INFORMATIONAL,
                     MBOX::MOD_MBOXSRV_RCV,
                     MBOX::RC_INVALID_MESSAGE_TYPE  ,    //  reason Code
                     i_mbox_msg.msg_queue_id,            // rc from msg_send
                     i_mbox_msg.msg_payload.type
                    );

                err->collectTrace(HBMBOXMSG_TRACE_NAME);
                errlCommit(err,HBMBOX_COMP_ID);

                free(msg->extra_data);
                msg_free(msg);
            }
        }
        // Else unregisteredd msg_queue_id
        //  For NOW, ignore FSP mailbox stuff bounced back by the echo server
        else if(i_mbox_msg.msg_queue_id != FSP_MAILBOX_MSGQ)
        {

            TRACFCOMP(g_trac_mbox,
                      ERR_MRK
                      "MailboxSp::recv_msg - Message dropped. "
                      "Unregistered msg queue id 0x%x",
                      i_mbox_msg.msg_queue_id);

            //  Send a message to the FSP
            mbox_msg_t mbox_msg;
            mbox_msg.msg_queue_id = FSP_MAILBOX_MSGQ;
            mbox_msg.msg_payload.type = MSG_INVALID_MSG_QUEUE_ID;
            mbox_msg.msg_payload.data[0] = i_mbox_msg.msg_queue_id;
            mbox_msg.msg_payload.extra_data = NULL;
            mbox_msg.msg_payload.__reserved__async = 0; // async


            // prevent infinite loop with echo mb server
            if(i_mbox_msg.msg_queue_id != FSP_MAILBOX_MSGQ)
            {
                send_msg(&mbox_msg);
            }


            /*@ errorlog tag
             * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
             * @moduleid        MBOX::MOD_MBOXSRV_RCV
             * @reasoncode      MBOX::RC_UNREGISTERED_MSG_QUEUE
             * @userdata1       msg queueid
             * @defdesc         msg queue type is not registered with the
             *                  mailbox. Message dropped.
             *
             */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                 MBOX::MOD_MBOXSRV_RCV,
                 MBOX::RC_UNREGISTERED_MSG_QUEUE,    //  reason Code
                 i_mbox_msg.msg_queue_id,            // rc from msg_send
                 0
                );

            // This trace will have the message content.
            err->collectTrace(HBMBOXMSG_TRACE_NAME);

            errlCommit(err,HBMBOX_COMP_ID);

            free(msg->extra_data);
            msg_free(msg);
        }
        else // This is a bounce-back msg from the echo server - Ignore
        {
            free(msg->extra_data);
            msg_free(msg);
        }
    }
}

void MailboxSp::handle_hbmbox_msg(mbox_msg_t & i_mbox_msg)
{
    if(i_mbox_msg.msg_payload.type == MSG_REQUEST_DMA_BUFFERS)
    {
        // DMA req. will be resolved by send_msg
        send_msg(&i_mbox_msg);   // response message
    }
    else if(i_mbox_msg.msg_payload.type == MSG_INVALID_MSG_QUEUE_ID)
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MOD_MBOXSRV_FSP_MSG
         * @reasoncode      RC_INVALID_QUEUE
         * @userdata1       msg queue
         * @defdesc         Message from FSP. A message queue sent to FSP
         *                  was not within a valid range
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             MBOX::MOD_MBOXSRV_FSP_MSG,
             MBOX::RC_INVALID_QUEUE,
             i_mbox_msg.msg_payload.data[0],
             0
            );

        err->collectTrace(HBMBOXMSG_TRACE_NAME);

        errlCommit(err,HBMBOX_COMP_ID);
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MOD_MBOXSRV_FSP_MSG
         * @reasoncode      RC_INVALID_MBOX_MSG_TYPE
         * @userdata1       msg type
         * @userdata2       msg queue id
         * @defdesc         Message from FSP. A message type sent to FSP
         *                  was not within a valid range
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             MBOX::MOD_MBOXSRV_FSP_MSG,
             MBOX::RC_INVALID_MBOX_MSG_TYPE,
             i_mbox_msg.msg_payload.data[0],
             i_mbox_msg.msg_payload.data[1]
            );

        err->collectTrace(HBMBOXMSG_TRACE_NAME);

        errlCommit(err,HBMBOX_COMP_ID);
    }
}


void MailboxSp::handle_hbmbox_resp(mbox_msg_t & i_mbox_msg)
{

    //Response for more DMA buffers
    if(i_mbox_msg.msg_payload.data[0] != 0)
    {
        iv_dmaBuffer.addBuffers
            (i_mbox_msg.msg_payload.data[0]);
        iv_dma_pend = false;
        send_msg(); // send next message on queue
    }
    else // This is not really a response from the FSP
        // This is an echo back from echo server of a req the HB MBOX sent.
    {
        // This message should never come from the real FSP, so it must be
        // from the echo server. Responding to the echo server will
        // make the message echo back again as if it were a response from the
        // FSP. Since it's not possible to know which buffers the FSP owns, for
        // testing purposes play the role of the FSP and assume the FSP owns
        // all DMA buffers and will return them all.
        // TODO This should probably be removed when/if the echo server
        // is no longer used.
        TRACFCOMP(g_trac_mbox,"FAKEFSP returning all DMA buffers");

        i_mbox_msg.msg_payload.data[0] = 0xFFFFFFFFFFFFFFFF;

        // Since the HB is waiting for DMA buffers and holding up all other
        // messages, just sneek this one on the front of the queue, disable
        // the dma_pending flag to just long enough to send this message.
        // All other values in the msg should be left as is.
        iv_dma_pend = false;
        iv_sendq.push_front(i_mbox_msg);
        send_msg(); // respond
        iv_dma_pend = true;
    }
}

/**
 * Send message to mailbox service to send a remote message
 */
errlHndl_t MailboxSp::send(queue_id_t i_q_id, msg_t * io_msg)
{
    errlHndl_t err = NULL;
    int rc = 0;

    msg_q_t mboxQ = msg_q_resolve(VFS_ROOT_MSG_MBOX);

    msg_t* msg = msg_allocate();
    msg->type = MBOX::MSG_SEND;
    msg->data[0] = static_cast<uint64_t>(i_q_id);
    msg->extra_data = reinterpret_cast<void*>(io_msg); // Payload message

    if(msg_is_async(io_msg))
    {
        rc = msg_send(mboxQ, msg);
    }
    else
    {
        rc = msg_sendrecv(mboxQ, msg);

        if(0 == rc)
        {
            err = reinterpret_cast<errlHndl_t>(msg->data[1]);
        }

        msg_free(msg);

        // io_msg now contains response message
    }

    if(rc != 0)
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        MBOX::MOD_MBOXSRV_SEND
         * @reasoncode      MBOX::RC_INVALID_QUEUE
         * @userdata1       returncode from msg_sendrecv()
         *
         * @defdesc         Invalid message or message queue
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,   //  severity
             MBOX::MOD_MBOXSRV_SEND,                 //  moduleid
             MBOX::RC_INVALID_QUEUE,                 //  reason Code
             rc,                                     //  msg_sendrecv errno
             i_q_id                                  //  msg queue id
            );

        // This Trace has the msg
        err->collectTrace(HBMBOXMSG_TRACE_NAME);
    }

    return err;
}


errlHndl_t MailboxSp::msgq_register(queue_id_t i_queue_id, msg_q_t i_msgQ)
{
    errlHndl_t err = NULL;

    registry_t::iterator r = iv_registry.find(i_queue_id);
    if(r == iv_registry.end())
    {
        iv_registry[i_queue_id] = i_msgQ;

        TRACFCOMP(g_trac_mbox,INFO_MRK"MailboxSp::msgq_register queue id 0x%x",
                  i_queue_id);
    }
    else
    {
        // If its the same queue being registered again, just ignore it.
        // If its a different queue then error.
        if(r->second != i_msgQ)
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_INFORMATIONAL
             * @moduleid        MBOX::MOD_MBOXREGISTER
             * @reasoncode      MBOX::RC_ALREADY_REGISTERED
             * @userdata1       queue_id_t queueId
             * @userdata2       0
             *
             * @defdesc         Message queue already registered with mailbox
             *                  using a different queue.
             *
             */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_INFORMATIONAL,    // severity
                 MBOX::MOD_MBOXREGISTER,              // moduleid
                 MBOX::RC_ALREADY_REGISTERED,         // reason code
                 i_queue_id,
                 0
                );
        }
    }
    return err;
}

msg_q_t MailboxSp::msgq_unregister(queue_id_t i_queue_id)
{
    msg_q_t msgQ = NULL;
    registry_t::iterator r = iv_registry.find(i_queue_id);
    if(r != iv_registry.end())
    {
        msgQ = r->second;
        iv_registry.erase(r);
        TRACFCOMP(g_trac_mbox,INFO_MRK"MailboxSp::msgq_unregister queue id 0x%x",
                  i_queue_id);
    }
    return msgQ;
}

void MailboxSp::trace_msg(const char * i_text,
                          const mbox_msg_t & i_mbox_msg) const
{
    TRACFCOMP(g_trac_mboxmsg,
              "MBOXSP %s MSG HEAD: msg_id:0x%x msg_queue_id:0x%x",
              i_text,
              i_mbox_msg.msg_id,
              i_mbox_msg.msg_queue_id);

    TRACFCOMP(g_trac_mboxmsg,
              "MBOXSP %s MSG: 0x%08x 0x%016lx 0x%016lx %p",
              i_text,
              i_mbox_msg.msg_payload.type,
              i_mbox_msg.msg_payload.data[0],
              i_mbox_msg.msg_payload.data[1],
              i_mbox_msg.msg_payload.extra_data);
}

// ----------------------------------------------------------------------------
// External Interfaces @see mboxif.H
// ----------------------------------------------------------------------------

errlHndl_t MBOX::send(queue_id_t i_q_id, msg_t * i_msg)
{
    i_msg->__reserved__async = 0;
    return MailboxSp::send(i_q_id, i_msg);
}

errlHndl_t MBOX::sendrecv(queue_id_t i_q_id, msg_t * io_msg)
{
    io_msg->__reserved__async = 1;
    return MailboxSp::send(i_q_id, io_msg);
}

errlHndl_t MBOX::msgq_register(queue_id_t i_queue_id, msg_q_t i_msgQ)
{
    // Could use a mutex to protect the queueid to msgQ map, but since
    // registering is a rare thing, just send a message to the mailbox service.
    errlHndl_t err = NULL;
    msg_q_t mboxQ = msg_q_resolve(VFS_ROOT_MSG_MBOX);

    if(mboxQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_REGISTER_MSGQ;
        msg->data[0] = static_cast<uint64_t>(i_queue_id);
        msg->data[1] = reinterpret_cast<uint64_t>(i_msgQ);

        int rc = msg_sendrecv(mboxQ, msg);

        if(!rc)
        {
            err = reinterpret_cast<errlHndl_t>(msg->data[1]);
        }
        else
        {
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::msgq_register - msg_sendrecv failed. errno = %d",
                      rc);
        }
        msg_free(msg);
    }
    else
    {
        TRACFCOMP(g_trac_mbox, ERR_MRK"Mailbox Service not available");

        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MBOX::MOD_MBOXREGISTER
         * @reasoncode      MBOX::RC_MBOX_SERVICE_NOT_READY
         * @userdata1       queue_id_t queueId
         * @userdata2       0
         *
         * @defdesc         Mailbox service is not available now.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,    // severity
             MBOX::MOD_MBOXREGISTER,              // moduleid
             MBOX::RC_MBOX_SERVICE_NOT_READY,     // reason code
             i_queue_id,
             0
            );
    }

    return err;
}

msg_q_t MBOX::msgq_unregister(queue_id_t i_queue_id)
{
    msg_q_t msgQ = NULL;
    msg_q_t mboxQ = msg_q_resolve(VFS_ROOT_MSG_MBOX);
    if(mboxQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_UNREGISTER_MSGQ;
        msg->data[0] = static_cast<uint64_t>(i_queue_id);

        int rc = msg_sendrecv(mboxQ, msg);

        if(!rc)
        {
            msgQ = reinterpret_cast<msg_q_t>(msg->data[1]);
        }
        else
        {
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::msgq_unregister - msg_sendrecv failed. errno = %d",
                      rc);
        }

        msg_free(msg);
    }
    else
    {
        TRACFCOMP(g_trac_mbox, ERR_MRK"Mailbox Service not available");
    }

    return msgQ;
}



OpenPOWER on IntegriCloud