summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/attrsync.C
blob: d6c6fd9527e387f62157439906b0dd291c4cddbe (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/attrsync.C $                                */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2014              */
/*                                                                        */
/* 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                                                     */
#include <targeting/attrsync.H>
#include <targeting/common/targreasoncodes.H>
#include <targeting/common/trace.H>
#include <initservice/initserviceif.H>

using namespace ERRORLOG;

namespace TARGETING
{

    AttributeSync::AttributeSync()
        :iv_section_to_sync(SECTION_TYPE_PNOR_RW),iv_total_pages(0),iv_current_page(0)
    {};

    AttributeSync::~AttributeSync()
    {};

    void AttributeSync::getSectionData()
    {
        // make sure we have a clean slate
        iv_total_pages  = 0;
        iv_current_page = 0;
        iv_pages.clear();

        // call the targeting function here to get context.
        TargetService& l_targetService = targetService();

        // read the section data info into the iv_pages structure
        l_targetService.readSectionData( iv_pages, iv_section_to_sync );

        iv_total_pages = iv_pages.size();

        TRACDCOMP(g_trac_targeting, "total pages %d", iv_total_pages );
    }

    ATTR_SYNC_RC AttributeSync::updateSectionData() const
    {
        TARG_INF( ENTER_MRK "AttributeSync::updateSectionData - "
                  "section type %u total pages %d",
                  iv_section_to_sync, iv_total_pages );

        ATTR_SYNC_RC l_rc = ATTR_SYNC_SUCCESS;

        // call the targeting function here to get context.
        TargetService& l_targetService = targetService();

        // write the section data info into the iv_pages structure
        if ( false == l_targetService.writeSectionData( iv_pages ) )
        {
            l_rc = ATTR_SYNC_FAILURE;
        }

        TARG_INF( EXIT_MRK "AttributeSync::updateSectionData");
        return l_rc;
    }

    errlHndl_t AttributeSync::syncSectionToFsp(
                                    TARGETING::SECTION_TYPE i_section_to_sync )
    {
        errlHndl_t l_errl   = NULL;
        msg_t   *  msg      = NULL;

        iv_section_to_sync = i_section_to_sync;


        do{

            // set up the pointers to the data area
            getSectionData();

            for( iv_current_page = 0;
                    iv_current_page < iv_total_pages; iv_current_page++ )
            {
                msg = msg_allocate();

                msg->type = ATTR_SYNC_SECTION_TO_FSP;

                msg->data[0] = 0;

                ATTR_SYNC_ADD_SECTION_ID( iv_section_to_sync, msg->data[0] );

                ATTR_SYNC_ADD_PAGE_NUMBER( iv_current_page, msg->data[0] );

                // set the 2nd data word to the buffer size
                msg->data[1] = PAGESIZE;

                // allocated storage will always be 4k
                msg->extra_data = malloc( PAGESIZE );

                // copy the attributes from mem to our buffer.
                memcpy( msg->extra_data,
                        iv_pages[iv_current_page].dataPtr, PAGESIZE );

                TRACFCOMP(g_trac_targeting,
                        "syncSectionToFsp()  - copy %d bytes from %p to %p",
                        PAGESIZE, iv_pages[iv_current_page].dataPtr,
                        msg->extra_data);

                // mailbox code will free both the msg and the extra data
                // we allocated above for an async message.
                l_errl = sendMboxMessage( ASYNCHRONOUS, msg );

                if( l_errl )
                {
                    TRACFCOMP(g_trac_targeting, "failed sending sync message");
                    break;
                }

            }

            if(( l_errl == NULL ) && ( iv_total_pages != 0 ))
            {
                // tell fsp to commit the last section of data we sent
                l_errl =  sendSyncCompleteMessage();

                if( l_errl )
                {
                    TRACFCOMP(g_trac_targeting,
                            "failed sending sync complete message");
                }
            }

        }while(0);

        return l_errl;

    }

    errlHndl_t AttributeSync::syncSectionFromFsp(
                                    TARGETING::SECTION_TYPE i_section_to_sync,
                                    msg_q_t i_pMsgQ )
    {
        TARG_INF( ENTER_MRK "AttributeSync::syncSectionFromFsp" );

        errlHndl_t l_errl    = NULL;
        bool l_sync_complete = false;
        ATTR_SYNC_RC l_rc    = ATTR_SYNC_FAILURE;
        TARGETING::sectionRefData l_page;

        iv_section_to_sync = i_section_to_sync;
        memset( &l_page, 0, sizeof(TARGETING::sectionRefData) );

        do{

            // send a request to FSP to sync to Hostboot
            l_errl = sendSyncToHBRequestMessage();
            if (l_errl)
            {
                break;
            }

            do{

                // wait for FSP to send the section's attribute data
                TARG_DBG( "Wait for message from FSP");
                msg_t * l_pMsg = msg_wait(i_pMsgQ);

                // process message just received
                if ( ATTR_SYNC_SECTION_TO_HB == l_pMsg->type )
                {
                    TARG_DBG( "HB Attribute Sync Section message type received "
                        "from the FSP");

                    // get the section id
                    l_page.sectionId = ATTR_SYNC_GET_SECTION_ID(l_pMsg->data[0]);

                    // get the page number
                    l_page.pageNumber = ATTR_SYNC_GET_PAGE_NUMBER(l_pMsg->data[0]);

                    // save a pointer to the page
                    l_page.dataPtr =
                        reinterpret_cast<uint8_t *> (l_pMsg->extra_data);

                    // Validate the data received.  Ignore page if
                    // section id or page size is incorrect or if
                    // there are no page received since we cannot send
                    // an error back to the FSP at this point.  We will
                    // check later whether the correct number of valid
                    // pages for the section was received when FSP send
                    // us the sync complete message.

                    // if no page received
                    if ( NULL == l_page.dataPtr)
                    {
                        TARG_ERR("WARNING: "
                            "no attribute page received from FSP");
                    }
                    // if it's not the requested section
                    else if ( iv_section_to_sync != l_page.sectionId )
                    {
                        TARG_ERR("WARNING: "
                            "section type received from FSP = %u, expecting %u",
                            l_page.sectionId, iv_section_to_sync);

                        //Free the memory
                        free(l_pMsg->extra_data);
                        l_pMsg->extra_data = NULL;
                    }
                    // page size should always be 4K
                    else if ( PAGESIZE != l_pMsg->data[1] )
                    {
                        TARG_ERR("WARNING: "
                            "page size received from FSP = %u, expecting 4K",
                            l_pMsg->data[1]);

                        free(l_pMsg->extra_data);
                        l_pMsg->extra_data = NULL;
                    }
                    else
                    {
                        iv_pages.push_back(l_page);
                    }

                    // Free memory allocated for message
                    msg_free( l_pMsg );
                    l_pMsg = NULL;
                }
                else if ( ATTR_SYNC_COMPLETE_TO_HB == l_pMsg->type )
                {
                    TARG_DBG( "HB Attribute Sync Complete message type "
                        "received from the FSP");

                    l_sync_complete = true;

                    iv_total_pages = ATTR_SYNC_GET_PAGE_COUNT( l_pMsg->data[0] );

                    // check that the total # of valid pages received is correct
                    if ( iv_pages.size() == iv_total_pages )
                    {
                        // write the section to the Attribute virtual address
                        // space
                        l_rc = updateSectionData();

                        if (l_rc)
                        {
                            TARG_ERR(
                                "HB failed in writing the attribute section" );
                        }
                    }
                    else
                    {
                        TARG_ERR( "total # of valid pages received = %u, "
                            "expecting %u", iv_pages.size(), iv_total_pages);

                        l_rc = ATTR_SYNC_FAILURE;
                    }

                    if (l_rc)
                    {
                        /*@
                         *   @errortype
                         *   @moduleid      TARG_MOD_ATTR_SYNC
                         *   @reasoncode    TARG_RC_ATTR_SYNC_TO_HB_FAIL
                         *   @userdata1     return code
                         *   @userdata2     section to sync
                         *   @devdesc       The Attribute synchronization from FSP
                         *                  failed.
                         */
                         l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                                TARG_MOD_ATTR_SYNC,
                                                TARG_RC_ATTR_SYNC_TO_HB_FAIL,
                                                l_rc,
                                                iv_section_to_sync);
                    }

                    // send a msg back to FSP indicating success/failure
                    l_pMsg->data[0] = 0;
                    ATTR_SYNC_ADD_RC( l_rc, l_pMsg->data[0] );
                    int l_respond_rc = msg_respond(i_pMsgQ, l_pMsg);
                    if (l_respond_rc)
                    {
                        // Just output a trace here since FSP should
                        // handle error case where it doesn't receive
                        // a response from HB.
                        TARG_ERR( "WARNING: Bad rc from msg_respond: %d",
                            l_respond_rc);
                        msg_free( l_pMsg );
                        l_pMsg = NULL;
                    }
                }
                else
                {
                    TARG_ERR( "WARNING: Invalid message type [0x%x] received "
                        "from the FSP, ignoring...", l_pMsg->type);
                    msg_free( l_pMsg );
                    l_pMsg = NULL;
                }

            }while (false == l_sync_complete);

            // free memory
            if ( iv_pages.size() )
            {
                for ( size_t i = 0; i < iv_pages.size(); i++ )
                {
                    free( iv_pages[i].dataPtr );
                }

                iv_pages.clear();
            }

        }while (0);

        TARG_INF( EXIT_MRK "AttributeSync::syncSectionFromFsp" );
        return l_errl;
    }

    // send the sync complete message
    errlHndl_t AttributeSync::sendSyncCompleteMessage( )
    {
        TRACFCOMP(g_trac_targeting, "sending sync complete message");

        errlHndl_t l_err = NULL;

        msg_t * msg = msg_allocate();

        // initialize msg buffer
        memset( msg, 0, sizeof(msg_t) );

        msg->type = ATTR_SYNC_COMPLETE_TO_FSP;

        ATTR_SYNC_ADD_PAGE_COUNT( iv_total_pages, msg->data[0] );

        l_err = sendMboxMessage( SYNCHRONOUS, msg );

        if( l_err == NULL )
        {
            // see if there was an error on the other end
            ATTR_SYNC_RC return_code = ATTR_SYNC_GET_RC( msg->data[0] );

            if ( return_code )
            {
                TRACFCOMP(g_trac_targeting, "return code: 0x%x", return_code );

                /*@
                 *   @errortype
                 *   @moduleid      TARG_MOD_ATTR_SYNC
                 *   @reasoncode    TARG_RC_ATTR_SYNC_TO_FSP_FAIL
                 *   @userdata1     return code from FSP attribute sync
                 *   @userdata2     section ID of for section being sync'd
                 *
                 *   @devdesc       The Attribute synchronization code on the
                 *                  FSP side was unable to complete the sync
                 *                  operation successfully.
                 */
                 l_err = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                        TARG_MOD_ATTR_SYNC,
                                        TARG_RC_ATTR_SYNC_TO_FSP_FAIL,
                                        return_code,
                                        (uint64_t)iv_section_to_sync);
            }
        }

        // for a syncronous message we need to free the message
        msg_free( msg );

        return l_err;
    }

    // send a request to FSP to sync to Hostboot
    errlHndl_t AttributeSync::sendSyncToHBRequestMessage()
    {
        TARG_INF( ENTER_MRK "AttributeSync::sendSyncToHBRequestMessage" );

        errlHndl_t l_err = NULL;

        // allocate message buffer
        // buffer will be initialized to zero by msg_allocate()
        msg_t * l_pMsg = msg_allocate();

        l_pMsg->type = ATTR_SYNC_REQUEST_TO_HB;

        ATTR_SYNC_ADD_SECTION_ID( iv_section_to_sync, l_pMsg->data[0] );

        l_err = sendMboxMessage( SYNCHRONOUS, l_pMsg );

        if( l_err == NULL )
        {
            // see if there was an error on the other end
            ATTR_SYNC_RC return_code = ATTR_SYNC_GET_RC( l_pMsg->data[0] );

            if ( return_code )
            {
                TARG_ERR(
                    "rc 0x%x received from FSP for Sync to HB request",
                    return_code );

                /*@
                 *   @errortype
                 *   @moduleid      TARG_MOD_ATTR_SYNC
                 *   @reasoncode    TARG_RC_ATTR_SYNC_REQUEST_TO_HB_FAIL
                 *   @userdata1     return code from FSP
                 *   @userdata2     section to sync
                 *   @devdesc       The Attribute synchronization code on the
                 *                  FSP side was unable to fulfill the sync to
                 *                  HB request.
                 */
                 l_err = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                       TARG_MOD_ATTR_SYNC,
                                       TARG_RC_ATTR_SYNC_REQUEST_TO_HB_FAIL,
                                       return_code,
                                       iv_section_to_sync);
            }
        }
        else
        {
            TARG_ERR(
                "Failed to send request to FSP to sync section type %u "
                "to Hostboot.", iv_section_to_sync );
        }

        // for a syncronous message we need to free the message
        msg_free( l_pMsg );
        l_pMsg = NULL;

        TARG_INF( EXIT_MRK "AttributeSync::sendSyncToHBRequestMessage" );
        return l_err;
    }

    errlHndl_t AttributeSync::sendMboxMessage( MBOX_MSG_TYPE type,
                                                msg_t * i_msg )
    {
        errlHndl_t l_errl = NULL;

        TRACDCOMP(g_trac_targeting, "type:  0x%04x",   i_msg->type );
        TRACDCOMP(g_trac_targeting, "data0: 0x%016llx",i_msg->data[0] );
        TRACDCOMP(g_trac_targeting, "data1: 0x%016llx",i_msg->data[1] );
        TRACDCOMP(g_trac_targeting, "extra_data: %p",i_msg->extra_data );

        // determine if its an async message or if we should wait
        // for a response
        if( type == ASYNCHRONOUS )
        {
            TRACDCOMP(g_trac_targeting,
                    "sendMboxMessage() - sending async mbox msg" );
            l_errl = MBOX::send( MBOX::FSP_ATTR_SYNC_MSGQ, i_msg );
        }
        else
        {
            TRACDCOMP(g_trac_targeting,
                    "sendMboxMessage() - sending sync mbox msg" );
            l_errl = MBOX::sendrecv( MBOX::FSP_ATTR_SYNC_MSGQ, i_msg );

        }

        if( l_errl )
        {
            TRACFCOMP(g_trac_targeting,
                    "sendMboxMessage() - failed sending mbox msg" );

            // if the send failed and the message is still valid, check
            // and free the extra data if it exists.
            if( i_msg != NULL && i_msg->extra_data != NULL )
            {
                free( i_msg->extra_data );
            }
        }

        return l_errl;
    }


    errlHndl_t syncAllAttributesToFsp()
    {
        errlHndl_t l_errl = NULL;

        do{
            // If no SP Base Services then skip attribute sync
            // let the test case force message creation/sending
            if( !INITSERVICE::spBaseServicesEnabled() )
            {
                break;
            }

            SECTION_TYPE section_type[] ={SECTION_TYPE_PNOR_RW,
                                      SECTION_TYPE_HEAP_PNOR_INIT,
                                      SECTION_TYPE_HEAP_ZERO_INIT};

            size_t section_count = sizeof(section_type)/sizeof(section_type[0]);

            TRACFCOMP(g_trac_targeting,"section count = %d", section_count );

            // push down all attributes to FSP
            AttributeSync l_Sync;

            uint8_t i = 0;

            for(; i < section_count; i++)
            {
                l_errl = l_Sync.syncSectionToFsp( section_type[i] );

                if( l_errl )
                {
                    TRACFCOMP(g_trac_targeting,
                            "Error returned when syncing section type %d to FSP",
                            section_type[i]);
                    break;
                }
            }

        } while (0);

        return  l_errl;
    }

    errlHndl_t syncAllAttributesFromFsp()
    {
        TARG_INF( ENTER_MRK "syncAllAttributesFromFsp" );

        errlHndl_t l_errl = NULL;

        do{
            // If no SP Base Services then skip attribute sync
            if( !INITSERVICE::spBaseServicesEnabled() )
            {
                TARG_INF( "SP Base Services not enabled, skipping attribute sync" );
                break;
            }

            // create Hostboot message queue
            msg_q_t l_pHbMsgQ = msg_q_create();

            // register Hostboot message queue with mailbox to receive messages
            l_errl = MBOX::msgq_register(MBOX::HB_ATTR_SYNC_MSGQ, l_pHbMsgQ);
            if (l_errl)
            {
                TARG_ERR( "Error registering the Hostboot message queue with "
                    "mailbox service." );
                break;
            }

            // these are the sections we want to sync
            SECTION_TYPE section_type[] ={SECTION_TYPE_PNOR_RW,
                                          SECTION_TYPE_HEAP_PNOR_INIT,
                                          SECTION_TYPE_HEAP_ZERO_INIT};

            size_t section_count = sizeof(section_type)/sizeof(section_type[0]);

            TARG_DBG( "section count = %d", section_count );

            // pull all attributes from FSP
            AttributeSync l_Sync;

            for(uint8_t i = 0; i < section_count; i++)
            {
                TARG_INF( "syncing section type = %d", section_type[i] );
                l_errl = l_Sync.syncSectionFromFsp( section_type[i], l_pHbMsgQ );

                if (l_errl)
                {
                    break;
                }
            }

            // unregister the Hosboot message queue from the mailbox service.
            MBOX::msgq_unregister(MBOX::HB_ATTR_SYNC_MSGQ);

        } while (0);

        // Zero ATTR_RECONFIGURE_LOOP to avoid TI
        TARGETING::Target* l_pTopLevel = NULL;
        TARGETING::targetService().getTopLevelTarget(l_pTopLevel);
        l_pTopLevel->setAttr<TARGETING::ATTR_RECONFIGURE_LOOP>(0);

        TARG_INF( EXIT_MRK "syncAllAttributesFromFsp" );
        return  l_errl;
    }

};   // end namespace


OpenPOWER on IntegriCloud