summaryrefslogtreecommitdiffstats
path: root/src/usr/mbox/mailboxsp.C
blob: bb7b01513289b07ff3786b6071c15ae6e9112578 (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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/mbox/mailboxsp.C $                                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2012,2014                        */
/* [+] Google Inc.                                                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file mailboxsp.C
 * @brief mailbox service provider definition
 */

#include "mailboxsp.H"
#include "mboxdd.H"
#include "ipcSp.H"
#include <config.h>
#include <sys/task.h>
#include <initservice/taskargs.H>
#include <initservice/initserviceif.H>
#include <sys/vfs.h>
#include <devicefw/userif.H>
#include <mbox/mbox_reasoncodes.H>
#include <mbox/mboxUdParser.H>
#include <targeting/common/commontargeting.H>
#include <kernel/ipc.H>
#include <arch/ppc.H>
#include <errl/errlmanager.H>
#include <sys/misc.h>

// Local functions
namespace MBOX
{
    errlHndl_t makeErrlMsgQSendFail(uint64_t i_errno);
};

using namespace MBOX;

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

trace_desc_t* g_trac_mboxmsg = NULL; // g_trac_mbox;
TRAC_INIT(&g_trac_mboxmsg, MBOXMSG_TRACE_NAME,
          2*KILOBYTE, TRACE::BUFFER_SLOW);



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


MailboxSp::MailboxSp()
    :
        iv_msgQ(),
        iv_sendq(),
        iv_respondq(),
        iv_dmaBuffer(),
        iv_trgt(NULL),
        iv_shutdown_msg(NULL),
        iv_rts(true),
        iv_dma_pend(false),
        iv_disabled(true),
        iv_suspended(false),
        iv_suspend_intr(false),
        iv_allow_blk_resp(false),
        iv_sum_alloc(0),
        iv_pend_alloc(),
        iv_allocAddrs()
{
    // 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();
    return NULL;
}


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);

    if(rc)   // could not register msgQ with kernel
    {
        TRACFCOMP(g_trac_mbox,ERR_MRK "MailboxSP::_init() Could not register"
                  "message qeueue with kernel");

        /*@ errorlog tag
         * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
         * @moduleid        MBOX::MOD_MBOXSRV_INIT
         * @reasoncode      MBOX::RC_KERNEL_REG_FAILED
         * @userdata1       rc from msq_q_register
         * @devdesc         Could not register mailbox message queue
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
             MBOX::MOD_MBOXSRV_INIT,
             MBOX::RC_KERNEL_REG_FAILED,    //  reason Code
             rc,                        // rc from msg_send
             0,
             true //Add HB Software Callout
            );

        return err;
    }

    bool mbxComm = mailbox_enabled();

    // create task before registering the msgQ so any waiting interrupts get
    // handled as soon as the msgQ is registered with the interrupt service
    // provider
    task_create(MailboxSp::msg_handler, NULL);

    if(mbxComm)
    {
        // Initialize the mailbox hardware
        err = mboxInit(iv_trgt);
        if (err)
        {
            return err;
        }

        // Register to get interrupts for mailbox
        err = INTR::registerMsgQ(iv_msgQ,
                                 MSG_INTR,
                                 INTR::FSP_MAILBOX);
        if(err)
        {
            return err;
        }
    }

    // Register for IPC messages
    err = INTR::registerMsgQ(iv_msgQ,
                             MSG_IPC,
                             INTR::ISN_INTERPROC);


    if(mbxComm)
    {
        // Enable the mailbox
        iv_disabled = false;

        // 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] =iv_dmaBuffer.toPhysAddr(iv_dmaBuffer.getDmaBufferHead());
        msg->extra_data = NULL;
        MBOX::send(FSP_MAILBOX_MSGQ,msg);

        // Register for shutdown
        INITSERVICE::registerShutdownEvent(iv_msgQ,
                                           MSG_MBOX_SHUTDOWN,
                                           INITSERVICE::MBOX_PRIORITY);
    }
    // else leave iv_disabled as true;

    // Start the the interprocessor communications message handler
    IPC::IpcSp::init(err);

    // call ErrlManager function - tell him that MBOX is ready!
    ERRORLOG::ErrlManager::errlResourceReady(ERRORLOG::MBOX);

    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:
                {
                    err = handleInterrupt();

                    // Respond to the interrupt handler regardless of err
                    INTR::sendEOI(iv_msgQ,msg);

                    // err will be set if scom failed in mbox DD
                    // or MBOX_DATA_WRITE_ERR  - serious - assert
                    if(err)
                    {
                        errlCommit(err,MBOX_COMP_ID);

                        TRACFCOMP(g_trac_mbox, ERR_MRK
                                  "MBOXSP HALTED on critical error!");
                        crit_assert(0);
                    }

                    if(iv_shutdown_msg && quiesced())
                    {
                        // If all DMA buffers still not owned
                        // try once to get them all back.
                        if(!iv_dmaBuffer.ownsAllBlocks() &&
                           !iv_dmaBuffer.shutdownDmaRequestSent())
                        {
                            iv_dmaBuffer.setShutdownDmaRequestSent(true);
                            mbox_msg_t  dma_request_msg;
                            dma_request_msg.msg_queue_id = FSP_MAILBOX_MSGQ;
                            dma_request_msg.msg_payload.type =
                                MSG_REQUEST_DMA_BUFFERS;
                            dma_request_msg.msg_payload.__reserved__async = 1;

                            send_msg(&dma_request_msg);
                        }
                        else
                        {
                            handleShutdown();
                        }
                    }

                    if(iv_suspended && quiesced())
                    {
                        suspend();
                    }
                }

                break;


            case MSG_SEND:
                handleNewMessage(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;

            case MSG_MBOX_SHUTDOWN:
                {
                    TRACFCOMP(g_trac_mbox,
                              "MBOXSP Shutdown event received. Status 0x%x",
                              msg->data[1]);

                    iv_shutdown_msg = msg;      // Respond to this when done
                    iv_disabled = true;         // stop incomming new messages

                    if(iv_suspended == true)
                    {
                        resume();
                    }

                    // Deal with messages never claimed by any HB component
                    handleUnclaimed();

                    // State:  MBOX is quiesced() and owns all the DMA buffers
                    // Action: handleShutdown() now
                    //
                    // State:  MBOX is quiesced(), but does not own all the DMA
                    //         buffers
                    // Action: Must send message to retrieve the buffers.
                    //
                    // State:  MBOX is still busy
                    // Action: Send message to retrieve the DMA buffers if one
                    //         is not pending, but may not get them all back
                    //         at this time.
                    //
                    if(!iv_dmaBuffer.ownsAllBlocks() && !iv_dma_pend)
                    {
                        mbox_msg_t  dma_request_msg;
                        dma_request_msg.msg_queue_id = FSP_MAILBOX_MSGQ;
                        dma_request_msg.msg_payload.type =
                            MSG_REQUEST_DMA_BUFFERS;
                        dma_request_msg.msg_payload.__reserved__async = 1;

                        send_msg(&dma_request_msg);
                    }

                    if(quiesced()) //already in shutdown state
                    {
                        handleShutdown();       // done - shutdown now.
                    }
                    // else wait for things to quiesce
                }
                break;

            case MSG_MBOX_SUSPEND:

                if(!iv_disabled)
                {
                    TRACFCOMP(g_trac_mbox,
                              INFO_MRK"MBOXSP Suspend event received");

                    iv_suspend_msg = msg;   // Respond to this when done
                    iv_suspended = true;    // Queue any new messages
                    iv_suspend_intr = static_cast<bool>(msg->data[0]);
                    iv_allow_blk_resp = static_cast<bool>(msg->data[1]);

                    if(quiesced())
                    {
                        suspend();    // suspended
                    }
                }
                else
                {
                    TRACFCOMP(g_trac_mbox,
                              INFO_MRK"MBOXSP Ignored request to suspend a"
                              " disabled mailbox");
                    msg_respond(iv_msgQ,msg);
                }
                break;

            case MSG_MBOX_RESUME:

                resume();
                msg_respond(iv_msgQ,msg);
                TRACFCOMP(g_trac_mbox,INFO_MRK"Mailbox function resumed");

                break;

            case MSG_IPC: // Look for Interprocessor Messages
                {

                    uint64_t msg_q_id = KernelIpc::ipc_data_area.msg_queue_id;
                    if(msg_q_id == IPC_DATA_AREA_LOCKED)
                    {
                        // msg is being written, but not yet ready to handle
                        msg_q_id = IPC_DATA_AREA_CLEAR;
                    }
                    // desination message queue id is lower 32 bits
                    msg_q_id &= 0x00000000FFFFFFFFull;

                    if(IPC_DATA_AREA_CLEAR != msg_q_id)
                    {
                        // message is ready to handle
                        msg_t * ipc_msg = msg_allocate();
                        isync();
                        *ipc_msg = KernelIpc::ipc_data_area.msg_payload;
                        lwsync();
                        // EOI has already been sent by intrp. Simply clear
                        // ipc area so another message can be sent
                        KernelIpc::ipc_data_area.msg_queue_id =
                            IPC_DATA_AREA_CLEAR;
                        handleIPC(static_cast<queue_id_t>(msg_q_id), ipc_msg);
                    }

                    INTR::sendEOI(iv_msgQ,msg);
                }

                break;

            case MSG_LOCAL_IPC:
                {
                    queue_id_t q_id = static_cast<queue_id_t>(msg->data[0]);
                    msg_t* ipc_msg = reinterpret_cast<msg_t*>(msg->extra_data);

                    handleIPC(q_id, ipc_msg);

                    msg_free(msg);
                }

                break;

            case MSG_MBOX_ALLOCATE:
                handleAllocate(msg);
                break;

            case MSG_MBOX_DEALLOCATE:
                {
                    void * ptr = reinterpret_cast<void*>(msg->data[0]);
                    deallocate(ptr);
                }
                break;

            default:

                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
                 * @devdesc         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,
                     true //Add HB Software Callout
                    );

                errlCommit(err,MBOX_COMP_ID);
                err = NULL;

                msg_free(msg);

                break;
        }

    } // while(1)
}


void MailboxSp::handleNewMessage(msg_t * i_msg)
{
    // Build mailbox message
    mbox_msg_t mbox_msg;
    mbox_msg.msg_queue_id = static_cast<uint32_t>(i_msg->data[0]);
    msg_t * payload = reinterpret_cast<msg_t*>(i_msg->extra_data);
    mbox_msg.msg_payload = *payload;  //copy in payload
    bool i_msg_is_async = msg_is_async(i_msg);

    if(i_msg_is_async)
    {
        msg_free(payload);
        msg_free(i_msg);
        payload = NULL;
        i_msg = NULL;
    }

    if(iv_disabled)
    {
#ifdef CONFIG_DROPPED_MSG_WARNING_AS_DEBUG
        TRACDCOMP(g_trac_mbox,WARN_MRK
                  "MSGSEND - mailboxsp is disabled. Message dropped!"
                  " msgQ=0x%x type=0x%x",
                  mbox_msg.msg_queue_id,
                  mbox_msg.msg_payload.type);
#else
        TRACFCOMP(g_trac_mbox,WARN_MRK
                  "MSGSEND - mailboxsp is disabled. Message dropped!"
                  " msgQ=0x%x type=0x%x",
                  mbox_msg.msg_queue_id,
                  mbox_msg.msg_payload.type);
#endif

        if(!i_msg_is_async) // synchronous
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_INFORMATIONAL
             * @moduleid        MOD_MBOXSRV_SENDMSG
             * @reasoncode      RC_MAILBOX_DISABLED
             * @userdata1       queue_id
             * @userdata2       message type
             * @devdesc         Mailbox is disabled, message dropped.
             */
            errlHndl_t err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_INFORMATIONAL,
                 MBOX::MOD_MBOXSRV_SENDMSG,
                 MBOX::RC_MAILBOX_DISABLED,        //  reason Code
                 i_msg->data[0],                   // queue id
                 payload->type,                    // message type
                 true //Add HB Software Callout
                );

            i_msg->data[1] = reinterpret_cast<uint64_t>(err);

            payload->extra_data = NULL;

            msg_respond(iv_msgQ,i_msg);
        }

        if( mbox_msg.msg_payload.extra_data != NULL )
        {
            TRACDCOMP( g_trac_mbox, "free extra_data %p",
                        mbox_msg.msg_payload.extra_data );

            deallocate ( mbox_msg.msg_payload.extra_data );

            mbox_msg.msg_payload.extra_data = NULL;
        }

    }
    else
    {

        if(!i_msg_is_async)  //synchronous
        {
            i_msg->data[1] = 0;  // used later for return value

            // need to watch for a response
            msg_respond_t * response = new msg_respond_t(i_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);
        }

        // NOTE:This message could still get sent if iv_suspended == true.
        // This could happen if a suspend action has been requested, but the
        // response to the suspend request has not yet been sent.
        // (that is, the iv_sendq has not had a chance to "drain" yet.)
        if(!iv_suspended)
        {
            send_msg(&mbox_msg);
        }
        else
        {
            iv_sendq.push_back(mbox_msg);
            TRACFCOMP(g_trac_mbox,"I>Mailbox suspending or suspended.");
            trace_msg("QUEUED",mbox_msg);
        }
    }

}

// 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
    //
    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 =
                  reinterpret_cast<void*>(iv_dmaBuffer.toPhysAddr(dma_buffer));

                deallocate( payload->extra_data );
                payload->extra_data = NULL;
            }
            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] =
                iv_dmaBuffer.toPhysAddr(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;
            }
            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
                     * @devdesc         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
                         dma_size,                         // DMA data len
                         iv_msg_to_send.msg_queue_id,      // MSG queueid
                         true //Add HB Software Callout
                        );

                    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;

        if(iv_msg_to_send.msg_queue_id == FSP_MAILBOX_MSGQ &&
           iv_msg_to_send.msg_payload.type == MSG_REQUEST_DMA_BUFFERS)
        {
            iv_dma_pend = true;
        }

        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- RTC:93751 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- RTC:93751 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,MBOX_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(iv_dmaBuffer.isDmaAddress(msg->extra_data))
    {
        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
            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_MSG_SEND_ERROR
                     * @userdata1       rc from msg_send()
                     * @userdata2       msg queue id
                     * @devdesc         Invalid msg or msg queue
                     *
                     */
                    err = new ERRORLOG::ErrlEntry
                        (
                         ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                         MBOX::MOD_MBOXSRV_RCV,
                         MBOX::RC_MSG_SEND_ERROR,   //  reason Code
                         rc,                        // rc from msg_send
                         i_mbox_msg.msg_queue_id,
                         true //Add HB Software Callout
                        );

                    //@todo: RTC:93750 Create real parseable FFDC class
                    err->addFFDC(MBOX_COMP_ID,
                                 msg,
                                 sizeof(msg_t),
                                 1,//version
                                 MBOX_UDT_MSG_DATA);//subsect
                    err->collectTrace(MBOXMSG_TRACE_NAME);
                    errlCommit(err,MBOX_COMP_ID);

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

                    TRACFCOMP(g_trac_mbox,
                              ERR_MRK"MBOXSP HALTED on critical error!");
                    crit_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_id = i_mbox_msg.msg_id;

                // msg_id and msg_queue_id
                mbox_msg.msg_payload.data[0] =
                    *(reinterpret_cast<uint64_t*>(&i_mbox_msg));
                // type & flags
                mbox_msg.msg_payload.data[1] =
                    *(reinterpret_cast<uint64_t*>(msg));

                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
                 * @devdesc         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
                     msg->type,
                     true //Add HB Software Callout
                    );

                UserDetailsMboxMsg
                    ffdc(reinterpret_cast<uint64_t*>(&i_mbox_msg),
                         sizeof(mbox_msg_t),
                         reinterpret_cast<uint64_t*>(msg->extra_data),
                         msg->data[1]);

                ffdc.addToLog(err);

                err->collectTrace(MBOXMSG_TRACE_NAME);
                errlCommit(err,MBOX_COMP_ID);

                free(msg->extra_data);
                msg_free(msg);
            }
        }
        // Else unregistered 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)
        {
            // copy in non-dma instance of payload msg back to mbox msg
            i_mbox_msg.msg_payload = *msg;

            // Is this msg_queue_id valid ?
            if(i_mbox_msg.msg_queue_id < HB_LAST_VALID_MSGQ  &&
               i_mbox_msg.msg_queue_id > HB_MAILBOX_MSGQ)
            {
                // Queue message to wait until queue is registered.
                iv_pendingq.push_back(i_mbox_msg);

                TRACFCOMP(g_trac_mbox,
                          INFO_MRK
                          "MailboxSp::recv_msg. Unregistered msg queue id 0x%x"
                          " message queued.",
                          i_mbox_msg.msg_queue_id);
            }
            else // un defined HB message queue
            {
                // Tell the FSP mbox about it and log an error
                invalidMsgResponder(i_mbox_msg);
                free(msg->extra_data); // toss this if it exists
                msg->extra_data = NULL;
                i_mbox_msg.msg_payload.extra_data = NULL;
            }
            msg_free(msg);
        }
        else // This is a bounce-back msg from the echo server - Ignore
        {
            free(msg->extra_data);
            msg->extra_data = NULL;
            msg_free(msg);
        }
    }
}

void MailboxSp::handle_hbmbox_msg(mbox_msg_t & i_mbox_msg)
{
    msg_t * msg = &(i_mbox_msg.msg_payload);

    if(msg->type == MSG_REQUEST_DMA_BUFFERS)
    {
        // DMA req. will be resolved by send_msg
        send_msg(&i_mbox_msg);   // response message
    }
    else if(msg->type == MSG_INVALID_MSG_QUEUE_ID ||
            msg->type == MSG_INVALID_MSG_TYPE)
    {
        mbox_msg_t * bad_mbox_msg =
            reinterpret_cast<mbox_msg_t*>(&(msg->data[0]));
        msg_t * bad_msg = &(bad_mbox_msg->msg_payload);

        TRACFCOMP(g_trac_mbox, ERR_MRK"Invalid message was sent to FSP. Queue"
                  " id: 0x%08x Type: %08x",
                  bad_mbox_msg->msg_queue_id,
                  bad_msg->type);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MOD_MBOXSRV_FSP_MSG
         * @reasoncode      RC_INVALID_QUEUE
         * @userdata1       msg queue
         * @userdata2       msg type
         * @devdesc         Message from FSP. An invalid message queue ID
         *                  or mesage type was sent to the FSP.
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             MBOX::MOD_MBOXSRV_FSP_MSG,
             MBOX::RC_INVALID_QUEUE,
             bad_mbox_msg->msg_queue_id,
             bad_msg->type,
             true //Add HB Software Callout
            );

        UserDetailsMboxMsg
            ffdc(reinterpret_cast<uint64_t*>(&i_mbox_msg),
                 sizeof(mbox_msg_t),
                 reinterpret_cast<uint64_t*>(msg->extra_data),
                 msg->data[1]);

        ffdc.addToLog(err);

        err->collectTrace(MBOXMSG_TRACE_NAME);

        // If the msg was sync then we need to respond to the
        // orignal sender and clean up the respondq
        if(!msg_is_async(bad_msg))
        {
            msg_t * key = reinterpret_cast<msg_t*>(bad_mbox_msg->msg_id);
            msg_respond_t * response = iv_respondq.find(key);
            if(response)
            {
                iv_respondq.erase(response); // unlink from the list

                //response->key->extra_data points to the original msg
                // Send back the error log
                response->key->data[1] = reinterpret_cast<uint64_t>(err);
                err = NULL;
                msg_respond(iv_msgQ,response->key);

                delete response;
            }
            else // nothing to respond to - just log the error
            {
                errlCommit(err,MBOX_COMP_ID);
            }
        }
        else // async - nothing to respond to -just log the error
        {
            errlCommit(err,MBOX_COMP_ID);
        }
    }
    else // unknown/un-architected message from fsp MBOX
    {
        TRACFCOMP(g_trac_mbox,
                  ERR_MRK
                  "Unknown message of type 0x%x received from FSP.",
                  msg->type);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MBOX::MOD_MBOXSRV_FSP_MSG
         * @reasoncode      MBOX::RC_INVALID_MESSAGE_TYPE
         * @userdata1       msg type
         * @userdata2       msg queue id
         * @devdesc         Message from FSP to HB MBOX of an unknown type
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             MBOX::MOD_MBOXSRV_FSP_MSG,
             MBOX::RC_INVALID_MESSAGE_TYPE,
             msg->type,
             i_mbox_msg.msg_queue_id
            );

        err->addProcedureCallout(HWAS::EPUB_PRC_SP_CODE,
                                 HWAS::SRCI_PRIORITY_HIGH);
        UserDetailsMboxMsg
            ffdc(reinterpret_cast<uint64_t*>(&i_mbox_msg),
                 sizeof(mbox_msg_t),
                 reinterpret_cast<uint64_t*>(msg->extra_data),
                 msg->data[1]);

        ffdc.addToLog(err);

        err->collectTrace(MBOXMSG_TRACE_NAME);

        errlCommit(err,MBOX_COMP_ID);
    }

}


void MailboxSp::handle_hbmbox_resp(mbox_msg_t & i_mbox_msg)
{
    //Response for more DMA buffers
    iv_dmaBuffer.addBuffers
        (i_mbox_msg.msg_payload.data[0]);

    iv_dma_pend = false;

    send_msg(); // send next message, if there is one
}

/**
 * Send message to mailbox service to send a remote message
 */
errlHndl_t MailboxSp::send(queue_id_t i_q_id,
                           msg_t * io_msg,
                           msgq_msg_t i_mbox_msg_type)
{
    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 = i_mbox_msg_type;
    msg->data[0] = static_cast<uint64_t>(i_q_id);
    msg->extra_data = reinterpret_cast<void*>(io_msg); // Payload message

    if(mboxQ != NULL)
    {
        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()
             *
             * @devdesc         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
                 true //Add HB Software Callout
                );

            // This Trace has the msg
            err->collectTrace(MBOXMSG_TRACE_NAME);
        }
    }
    else
    {
        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MBOX::MOD_MBOXSRV_SEND
         * @reasoncode      MBOX::RC_MBOX_SERVICE_NOT_READY
         * @userdata1       The destination message queue id
         *
         * @devdesc         Host boot mailbox service is not available
         *                  at this time.
         *
         */
        err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,       //  severity
             MBOX::MOD_MBOXSRV_SEND,                 //  moduleid
             MBOX::RC_MBOX_SERVICE_NOT_READY,        //  reason Code
             i_q_id,                                 //  queue id
             0,                                      //
             true //Add HB Software Callout
            );

        msg_free(msg);
    }

    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);

        // Look for pending messages and send them.
        // Note: remove_if and remove_copy_if not implemented in HB code.
        size_t size = iv_pendingq.size();
        while(size--)
        {
            send_q_t::iterator mbox_msg = iv_pendingq.begin();
            if(i_queue_id == mbox_msg->msg_queue_id)
            {
                recv_msg(*mbox_msg);
            }
            else
            {
                iv_pendingq.push_back(*mbox_msg);
            }

            iv_pendingq.pop_front();
        }
    }
    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
             *
             * @devdesc         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,
                 true //Add HB Software Callout
                );

        }
    }
    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);
}


errlHndl_t MailboxSp::handleInterrupt()
{
    errlHndl_t err = NULL;
    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(MBOX_TRACE_NAME);
        err->collectTrace(MBOXMSG_TRACE_NAME);
    }
    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);
            //Adjust address back to Virt here if present
            uint64_t l_dma = reinterpret_cast<uint64_t>(
                           mbox_msg.msg_payload.extra_data);
            if(l_dma)
            {
                mbox_msg.msg_payload.extra_data =
                  iv_dmaBuffer.toVirtAddr(l_dma);
            }

            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 mbox for more DMA buffers
                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 Hardware reported errors detected 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
                 * @devdesc         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,
                     true //Add HB Software Callout
                    );

                err->collectTrace(MBOX_TRACE_NAME);
                err->collectTrace(MBOXMSG_TRACE_NAME);
                // return err
            }

            else if(mbox_status & MBOX_PARITY_ERR)
            {
                // Hardware detected parity 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
                 * @devdesc         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->addHwCallout(iv_trgt,
                                  HWAS::SRCI_PRIORITY_HIGH,
                                  HWAS::NO_DECONFIG,
                                  HWAS::GARD_NULL);

                err->collectTrace(MBOX_TRACE_NAME);
                errlCommit(err,MBOX_COMP_ID);
            }
            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
                 * @devdesc         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->addProcedureCallout(HWAS::EPUB_PRC_SP_CODE,
                                         HWAS::SRCI_PRIORITY_HIGH);

                err->addProcedureCallout(HWAS::EPUB_PRC_HB_CODE,
                                         HWAS::SRCI_PRIORITY_HIGH);

                err->addHwCallout(iv_trgt,
                                  HWAS::SRCI_PRIORITY_LOW,
                                  HWAS::NO_DECONFIG,
                                  HWAS::GARD_NULL);

                err->collectTrace(MBOX_TRACE_NAME);
                errlCommit(err,MBOX_COMP_ID);
            }

            //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.
        }
    }

    return err;
}

bool MailboxSp::quiesced()
{
    bool result = iv_rts && !iv_dma_pend && iv_sendq.empty();

    TRACFCOMP(g_trac_mbox,INFO_MRK"Checking quiesced.. rts[%d] dma_pend[%d] sendq[%d]",
              iv_rts, iv_dma_pend, iv_sendq.empty());


    if( result == true )
    {
        TRACFCOMP(g_trac_mbox,INFO_MRK"quiesed == true, iv_shutdown_msg[%p]",
                  iv_shutdown_msg);
        if(iv_shutdown_msg == NULL ||
           (iv_shutdown_msg->data[1] == SHUTDOWN_STATUS_GOOD))
        {
            //Check that respond q is empty OR don't care
            result = result && (iv_respondq.empty() || iv_allow_blk_resp);
        }
        else // mbox is shutting down and system status is bad
        {
            // Wait for HB to FSP sync message to complete.
            // Don't wait for FSP to HB sync message to complete
            msg_respond_t * resp = iv_respondq.begin();
            while(resp)
            {
                if( resp->msg_queue_id >= MBOX::FSP_FIRST_MSGQ)
                {
                    // stil have pending HB->FSP sync messages
                    result = false;
                    break;
                }
                resp = resp->next;
            }
            // if result is still true then remaining responds, if any,
            // should be ignored - mbox is quiesced.
            if(result)
            {
                while(!iv_respondq.empty())
                {
                    resp = iv_respondq.begin();
                    iv_respondq.erase(resp);
                    delete resp;
                }
            }
        }
    }

    TRACFCOMP(g_trac_mbox,INFO_MRK"Quiesced [%d]", result);

    return result;
}

void MailboxSp::handleIPC(queue_id_t i_msg_q_id, msg_t * i_msg)
{

    TRACFCOMP(g_trac_mboxmsg,
              "MBOXSP IPC RECV MSG: msg_id:0x%08x",
              (uint32_t)i_msg_q_id);
    TRACFCOMP(g_trac_mboxmsg,
              "MBOXSP IPC RECV MSG: 0x%08x 0x%016lx 0x%016lx %p",
              i_msg->type,
              i_msg->data[0],
              i_msg->data[1],
              i_msg->extra_data);

    registry_t::iterator r =
        iv_registry.find(static_cast<queue_id_t>(i_msg_q_id));
    if(r != iv_registry.end())
    {
        // found queue
        msg_q_t msgq = r->second;

        // Only async message supported right now
        // Interface already inforces this.
        int rc = msg_send(msgq,i_msg);

        if(rc)
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
             * @moduleid        MBOX::MOD_MBOXSRV_IPC_MSG
             * @reasoncode      MBOX::RC_MSG_SEND_ERROR
             * @userdata1       rc from msg_send()
             * @userdata2       msg queue id
             * @devdesc         Invalid msg or msg queue
             *
             */
            errlHndl_t err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                 MBOX::MOD_MBOXSRV_IPC_MSG,
                 MBOX::RC_MSG_SEND_ERROR,   //  reason Code
                 rc,                        // rc from msg_send
                 i_msg_q_id,
                 true //Add HB Software Callout
                );

            UserDetailsMboxMsg
                ffdc(reinterpret_cast<uint64_t*>(i_msg),
                     sizeof(msg_t));

            ffdc.addToLog(err);

            err->collectTrace(MBOXMSG_TRACE_NAME);
            errlCommit(err,MBOX_COMP_ID);

            msg_free(i_msg);
        }
    }
    else // not registered
    {
        // thow it away log error
        TRACFCOMP(g_trac_mbox,
                  ERR_MRK
                  "MailboxSp::handleIPC: Unregistered msg queue id 0x%x"
                  " message dropped.",
                  i_msg_q_id);

        /*@ errorlog tag
         * @errortype       ERRL_SEV_INFORMATIONAL
         * @moduleid        MOD_MBOXSRV_IPC_MSG
         * @reasoncode      RC_INVALID_QUEUE
         * @userdata1       msg queue
         * @userdata2       msg type
         * @devdesc         Invalid message queue ID
         */
        errlHndl_t err = new ERRORLOG::ErrlEntry
            (
             ERRORLOG::ERRL_SEV_INFORMATIONAL,
             MBOX::MOD_MBOXSRV_IPC_MSG,
             MBOX::RC_INVALID_QUEUE,
             i_msg_q_id,
             i_msg->type,
             true //Add HB Software Callout
            );

        UserDetailsMboxMsg
            ffdc(reinterpret_cast<uint64_t*>(i_msg), sizeof(msg_t));

        ffdc.addToLog(err);
        err->collectTrace(MBOXMSG_TRACE_NAME);
        errlCommit(err,MBOX_COMP_ID);

        msg_free(i_msg);
    }
}


// Send a message to the FSP mailbox that a message it sent
// had an invalid or undeliverable message
void MailboxSp::invalidMsgResponder(mbox_msg_t & i_mbox_msg)
{
    mbox_msg_t r_mbox_msg;
    r_mbox_msg.msg_queue_id = FSP_MAILBOX_MSGQ;
    r_mbox_msg.msg_payload.type = MSG_INVALID_MSG_QUEUE_ID;
    r_mbox_msg.msg_id = i_mbox_msg.msg_id;

    // data[0] = msg_id and msg_queue_id
    r_mbox_msg.msg_payload.data[0] =
        *(reinterpret_cast<uint64_t*>(&i_mbox_msg));
    // data[1] = type & flags
    r_mbox_msg.msg_payload.data[1] =
        *(reinterpret_cast<uint64_t*>(&(i_mbox_msg.msg_payload)));

    r_mbox_msg.msg_payload.extra_data = NULL;
    r_mbox_msg.msg_payload.__reserved__async = 0; // async

    send_msg(&r_mbox_msg);


    TRACFCOMP(g_trac_mbox,
              ERR_MRK
              "MailboxSp::invalidMsgResponder> Unclaimed mbox message from"
              " FSP. Queueid 0x%08x",
              i_mbox_msg.msg_queue_id);

    /*@ errorlog tag
     * @errortype       ERRL_SEV_INFORMATIONAL
     * @moduleid        MBOX::MOD_MBOXSRC_UNCLAIMED
     * @reasoncode      MBOX::RC_INVALID_QUEUE
     * @userdata1       msg queue
     * @userdata2       msg type
     * @devdesc         Message from FSP. Message not claimed
     *                  by any Hostboot service.
     */
    errlHndl_t err = new ERRORLOG::ErrlEntry
        (
         ERRORLOG::ERRL_SEV_INFORMATIONAL,
         MBOX::MOD_MBOXSRC_UNCLAIMED,
         MBOX::RC_INVALID_QUEUE,            //  reason Code
         i_mbox_msg.msg_queue_id,           //  message queue id
         i_mbox_msg.msg_payload.type        //  message type
        );

    err->addProcedureCallout(HWAS::EPUB_PRC_SP_CODE,
                             HWAS::SRCI_PRIORITY_HIGH);

    err->addProcedureCallout(HWAS::EPUB_PRC_HB_CODE,
                             HWAS::SRCI_PRIORITY_HIGH);

    UserDetailsMboxMsg
        ffdc(reinterpret_cast<uint64_t*>(&i_mbox_msg),
             sizeof(mbox_msg_t),
             reinterpret_cast<uint64_t*>
             (i_mbox_msg.msg_payload.extra_data),
             i_mbox_msg.msg_payload.data[1]);

    ffdc.addToLog(err);

    errlCommit(err,MBOX_COMP_ID);
}

// Handle unclaimed messages in iv_pendingq
void MailboxSp::handleUnclaimed()
{
    for(send_q_t::iterator mbox_msg = iv_pendingq.begin();
        mbox_msg != iv_pendingq.end();
        ++mbox_msg)
    {
        invalidMsgResponder(*mbox_msg);
    }
    iv_pendingq.clear();
}


void MailboxSp::handleShutdown()
{
    // Shutdown the hardware
    errlHndl_t err = mboxddShutDown(iv_trgt);

    INTR::unRegisterMsgQ(INTR::FSP_MAILBOX);
    INTR::unRegisterMsgQ(INTR::ISN_INTERPROC);

    if(err)  // SCOM failed.
    {
        // If this failed, the whole system is probably buggered up.

        errlCommit(err,MBOX_COMP_ID);

        TRACFCOMP(g_trac_mbox,
                  ERR_MRK"MBOXSP Shutdown. HALTED on critical error!");
        crit_assert(0);
    }

    if(iv_pend_alloc.size() || iv_allocAddrs.size() || iv_sum_alloc)
    {
        TRACFCOMP(g_trac_mbox,WARN_MRK
                  "Pending memory allocations = %d "
                  "Allocated memory entrys = %d "
                  "Allocated memory size = %d",
                  iv_pend_alloc.size(),
                  iv_allocAddrs.size(),
                  iv_sum_alloc);
    }

    msg_respond(iv_msgQ,iv_shutdown_msg);
    TRACFCOMP(g_trac_mbox,INFO_MRK"Mailbox is shutdown");
}

void MailboxSp::suspend()
{
    TRACFCOMP(g_trac_mbox,INFO_MRK"Entering suspended");
    if(iv_suspend_intr)
    {
        errlHndl_t err = mboxddMaskInterrupts(iv_trgt);
        if(err)  // SCOM failed.
        {
            // If this failed, the whole system is probably buggered up.
            errlCommit(err,MBOX_COMP_ID);
            TRACFCOMP(g_trac_mbox,
                      ERR_MRK"MBOXSP suspend HALTED on critical error!");
            crit_assert(0);
        }
    }
    msg_respond(iv_msgQ,iv_suspend_msg);
    TRACFCOMP(g_trac_mbox,INFO_MRK"Mailbox is suspended");
}

void MailboxSp::resume()
{
    iv_suspended = false;
    iv_allow_blk_resp = false;

    if(!iv_disabled)
    {
        //If interrupts were disabled, re-enable
        if(iv_suspend_intr)
        {
            errlHndl_t err = mboxddEnableInterrupts(iv_trgt);
            if(err)  // SCOM failed.
            {
                // If this failed, the whole system is probably buggered up.
                errlCommit(err,MBOX_COMP_ID);
                TRACFCOMP(g_trac_mbox,
                          ERR_MRK"MBOXSP resume HALTED on critical error!");
                crit_assert(0);
            }
            iv_suspend_intr = false;
        }

        send_msg();   // send next message on queue
    }
}

void MailboxSp::handleAllocate(msg_t * i_msg)
{
    uint64_t size = i_msg->data[0];
    msg_t * msg = i_msg;

    // Try to allocate storage
    // If success then respond to message now else respond to message later
    if((iv_sum_alloc + size) < MAX_ALLOCATION)
    {
        uint64_t address = reinterpret_cast<uint64_t>(malloc(size));
        iv_sum_alloc += size;
        iv_allocAddrs.push_back(addr_size_t(address,size));
        msg->data[1] = address;
        msg_respond(iv_msgQ,msg);
    }
    else // save request on pending queue & block task by not responding
    {
        iv_pend_alloc.push_back(msg);
    }
}

void MailboxSp::deallocate(void * i_ptr)
{
    // if MBOX owns this then adjust iv_sum_alloc and
    // remove addr from iv_allocAddrs
    addr_list_t::iterator itr = iv_allocAddrs.begin();
    for(; itr != iv_allocAddrs.end(); ++itr)
    {
        if(itr->first == reinterpret_cast<uint64_t>(i_ptr))
        {
            break;
        }
    }

    free(i_ptr);

    if(itr != iv_allocAddrs.end())
    {
        iv_sum_alloc -= itr->second;
        iv_allocAddrs.erase(itr);

        // check to see if next allocation(s) can be met
        while(iv_pend_alloc.size() != 0)
        {
            msg_t * msg = iv_pend_alloc.front();
            uint64_t size = msg->data[0];
            if((iv_sum_alloc + size) < MAX_ALLOCATION)
            {
                uint64_t address = reinterpret_cast<uint64_t>(malloc(size));
                iv_sum_alloc += size;
                iv_allocAddrs.push_back(addr_size_t(address,size));
                msg->data[1] = address;
                msg_respond(iv_msgQ,msg);
                iv_pend_alloc.pop_front();
            }
            else
            {
                // stop on first allocation that can't be met
                // Must keep messages in order
                break;
            }
        }
    }
}

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

errlHndl_t MBOX::send(queue_id_t i_q_id, msg_t * i_msg,int i_node)
{
    errlHndl_t err = NULL;

    i_msg->__reserved__async = 0;

    if(i_node == MBOX::MBOX_NODE_FSP)
    {
        err = MailboxSp::send(i_q_id, i_msg,MBOX::MSG_SEND);
    }
    else // IPC msg
    {
        if(i_node < MSGQ_TYPE_IPC)
        {
            uint64_t q_handle = i_q_id;
            q_handle |= (((uint64_t)MSGQ_TYPE_IPC | (uint64_t)i_node) << 32);

            TRACFCOMP(g_trac_mboxmsg,INFO_MRK
                      "MBOXSP IPC SEND MSG: Dest node %d. msg_id: %lx",
                      i_node,
                      (uint32_t)i_q_id);

            TRACFCOMP(g_trac_mboxmsg,INFO_MRK
                      "MBOXSP IPC SEND MSG: 0x%08x 0x%016lx 0x%016lx %p",
                      i_msg->type,
                      i_msg->data[0],
                      i_msg->data[1],
                      i_msg->extra_data);

            // node means Hb instance number in this context
            INTR::PIR_t my_pir (KernelIpc::ipc_data_area.pir);
            if(my_pir.nodeId == i_node)
            {
                // Message is to this node - don't use IPC path
                // MBOX sp can look up msgQ
                err = MailboxSp::send(i_q_id, i_msg,MBOX::MSG_LOCAL_IPC);
            }
            else
            {
                int rc = msg_send(reinterpret_cast<msg_q_t>(q_handle),
                                  i_msg);
                TRACFCOMP(g_trac_mboxmsg,INFO_MRK
                          "MBOXSP IPC SENT. This PIR 0x%x",
                          KernelIpc::ipc_data_area.pir);


                if(rc)
                {
                    /*@ errorlog tag
                     * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
                     * @moduleid        MBOX::MOD_MBOX_SEND
                     * @reasoncode      MBOX::RC_INVALID_QUEUE
                     * @userdata1       returncode from msg_send()
                     *
                     * @devdesc         Invalid message or message queue
                     *
                     */
                    err = new ERRORLOG::ErrlEntry
                        (
                         ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,  //  severity
                         MBOX::MOD_MBOX_SEND,                //  moduleid
                         MBOX::RC_INVALID_QUEUE,             // reason Code
                         rc,                                 // msg_send errno
                         q_handle,                           //  msg queue id
                         true //Add HB Software Callout
                        );

                    // This Trace has the msg
                    err->collectTrace(MBOXMSG_TRACE_NAME);
                }
            }
        }
        else
        {
            /*@ errorlog tag
             * @errortype       ERRL_SEV_INFORMATIONAL
             * @moduleid        MBOX::MOD_MBOX_SEND
             * @reasoncode      MBOX::RC_IPC_INVALID_NODE
             * @userdata1       The destination queue id
             * @userdata2       The node
             *
             * @devdesc         An invalid node was specified
             *
             */
            err = new ERRORLOG::ErrlEntry
                (
                 ERRORLOG::ERRL_SEV_INFORMATIONAL,       //  severity
                 MBOX::MOD_MBOX_SEND,                    //  moduleid
                 MBOX::RC_IPC_INVALID_NODE,              //  reason Code
                 i_q_id,                                 //  queue id
                 i_node,                                 //
                 true //Add HB Software Callout
                );

        }
    }
    return err;
}

// ---------------------------------------------------------------------------

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, MBOX::MSG_SEND);
}

// ---------------------------------------------------------------------------

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
         *
         * @devdesc         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,
             true //Add HB Software Callout
            );

    }

    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;
}

// ---------------------------------------------------------------------------

bool MBOX::mailbox_enabled()
{
    bool enabled = false;

    TARGETING::Target * sys = NULL;

    TARGETING::targetService().getTopLevelTarget( sys );

    TARGETING::SpFunctions spfuncs;

    if( sys &&
        sys->tryGetAttr<TARGETING::ATTR_SP_FUNCTIONS>(spfuncs) &&
        spfuncs.mailboxEnabled)
    {
        enabled = true;
    }

    return enabled;
}

errlHndl_t MBOX::suspend(bool i_disable_hw_int, bool i_allow_resp)
{
    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_MBOX_SUSPEND;
        msg->data[0] = static_cast<uint64_t>(i_disable_hw_int);
        msg->data[1] = static_cast<uint64_t>(i_allow_resp);

        int rc = msg_sendrecv(mboxQ, msg);

        if (rc)
        {
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::suspend msg_sendrecv() failed. errno = %d",
                      rc);

            err = makeErrlMsgQSendFail(rc);
        }

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

    return err;
}

errlHndl_t MBOX::resume()
{
    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_MBOX_RESUME;

        int rc = msg_sendrecv(mboxQ, msg);

        if (rc)
        {
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::resume msg_sendrecv failed. errno = %d",
                      rc);
            err = makeErrlMsgQSendFail(rc);
        }

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

    return err;
}

errlHndl_t MBOX::makeErrlMsgQSendFail(uint64_t i_errno)
{
    TRACFCOMP(g_trac_mbox, ERR_MRK"Mailbox Service not available");

    /*@ errorlog tag
     * @errortype       ERRL_SEV_INFORMATIONAL
     * @moduleid        MBOX::MOD_MBOX_MSGQ_FAIL
     * @reasoncode      MBOX::RC_MSG_SEND_ERROR
     * @userdata1       kernel errno
     * @userdata2       <unused>
     *
     * @devdesc         Message send to mailbox sp failed
     * @custdesc        A problem occurred during the IPL of the system
     *
     */
    errlHndl_t err = new ERRORLOG::ErrlEntry
        (
         ERRORLOG::ERRL_SEV_INFORMATIONAL,    // severity
             MBOX::MOD_MBOX_MSGQ_FAIL,            // moduleid
             MBOX::RC_MSG_SEND_ERROR,             // reason code
             i_errno,
             0,
             true //Add HB Software Callout
            );

        return err;
}

// ----------------------------------------------------------------------------

void * MBOX::allocate(size_t i_size)
{
    void * response = NULL;

    msg_q_t mboxQ = msg_q_resolve(VFS_ROOT_MSG_MBOX);
    if(mboxQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_MBOX_ALLOCATE;
        msg->data[0] = i_size;

        int rc = msg_sendrecv(mboxQ, msg);

        if (rc)
        {
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::allocate msg_sendrecv() failed. errno = %d",
                      rc);
            // Creating error log would cause recursion on this function.
            // Leave response NULL and get memory from the heap.
        }
        else
        {
            response = reinterpret_cast<void *>(msg->data[1]);
        }

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

    if(NULL == response)
    {
        response = malloc(i_size);
    }
    
    return response;
}

void MBOX::deallocate(void * i_ptr)
{
    msg_q_t mboxQ = msg_q_resolve(VFS_ROOT_MSG_MBOX);
    if(mboxQ)
    {
        msg_t * msg = msg_allocate();
        msg->type = MSG_MBOX_DEALLOCATE;
        msg->data[0] = reinterpret_cast<uint64_t>(i_ptr);

        int rc = msg_sendrecv(mboxQ, msg);
        if(rc)
        {
            // kernel problem, Creating error log could cause recursion on
            // this function - just free memory from heap
            TRACFCOMP(g_trac_mbox, ERR_MRK
                      "MBOX::deallocate msg_sendrecv() failed. errno = %d",
                      rc);
            free(i_ptr);
        }
    }
    else
    {
        TRACFCOMP(g_trac_mbox, ERR_MRK"MBOX::deallocate - "
                  "Mailbox Service not available");
        free(i_ptr);
    }
}

OpenPOWER on IntegriCloud