summaryrefslogtreecommitdiffstats
path: root/src/usr/expaccess/test/ocmbcommtest.H
blob: 3c5486a1b8861b608a3f7e5c344efdbfd483a26e (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/expaccess/test/ocmbcommtest.H $                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2018,2019                        */
/* [+] 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                                                     */
#ifndef __OCMBCOMMTEST_H
#define __OCMBCOMMTEST_H
/**
 *  @file ocmbcommtest.H
 *
 *  @brief Test cases for OCMB communication protocol
 */
#include <cxxtest/TestSuite.H>
#include <errl/errlmanager.H>
#include <errl/errlentry.H>
#include <fapi2.H>

#include <plat_hwp_invoker.H>
#include <lib/inband/exp_inband.H>
#include <exp_data_structs.H>
#include <generic/memory/lib/utils/endian_utils.H>
#include "exptest_utils.H"


// EXP_FW_ADAPTER_PROPERTIES_GET data response format
#define FW_ADAPTER_MAX_FW_IMAGE  4
#define FW_ADAPTER_CHIP_VERSION_SIZE  128
#define FW_ADAPTER_SPI_FLASH_ID_SIZE  32
typedef struct
{
    uint32_t fw_number_of_images;     // number of FW images
    uint32_t boot_partion_id;         // ID of current boot partion
    struct fw_version_string
    {
        uint32_t major;               // FW version - Major release
        uint32_t minor;               // FW version - Minor release
        uint32_t build_num;           // FW build number
        uint32_t build_patch;         // FW build path number
        uint32_t sector_size;         // FW sector size
    } fw_ver_str[FW_ADAPTER_MAX_FW_IMAGE];

    uint32_t ram_size_in_bytes;       // RAM size in bytes
    unsigned char chip_version[FW_ADAPTER_CHIP_VERSION_SIZE];  // Explorer chip revision
    unsigned char spi_flash_id[FW_ADAPTER_SPI_FLASH_ID_SIZE];  // SPI flash ID
    uint32_t spi_flash_size;          // SPI flash size in bytes
    uint32_t error_buffer_offset;     // FW error buffer offset in SPI flash
    uint32_t error_buffer_size;       // FW error buffer size in bytes
} FW_ADAPTER_PROPERTIES_type;


class OCMBCommTest: public CxxTest::TestSuite
{
  public:

    /**
     * @brief Fills in command structure for the EXP_FW_ADAPTER_PROPERTIES_GET cmd
     * @param io_cmd -- command that gets filled in
     */
    void buildPropertiesGetCmd(host_fw_command_struct & io_cmd)
    {
        io_cmd.cmd_id = 0x07;   // EXP_FW_ADAPTER_PROPERTIES_GET
        io_cmd.cmd_flags = 0x00; // no additional data
        io_cmd.request_identifier = 0x0203; // host generated id number
        io_cmd.cmd_length = 0x00000000; // length of addditional data
        io_cmd.cmd_crc = 0xFFFFFFFF; // CRC-32 of no additional data
        io_cmd.host_work_area = 0x00000000;
        io_cmd.cmd_work_area = 0x00000000;
        memset(io_cmd.padding, 0, sizeof(io_cmd.padding));
        memset(io_cmd.command_argument, 0, sizeof(io_cmd.command_argument));
    }

    /**
     *  @brief Convert structure from little endian format into big endian
     *  @param o_data -- big endian output
     *  @param i_data -- vector of little endian data
     *  @return true if successful, else false
     */
    bool fw_adapter_properties_struct_from_little_endian(
                                          FW_ADAPTER_PROPERTIES_type & o_data,
                                          std::vector<uint8_t>& i_data)
    {
        bool l_rc = false;

        // make sure we don't go outside i_data range
        if (i_data.size() >= sizeof(o_data))
        {
          uint32_t l_idx = 0;
          l_rc = mss::readLE(i_data, l_idx, o_data.fw_number_of_images);
          l_rc &= mss::readLE(i_data, l_idx, o_data.boot_partion_id);
          for (int i = 0; i < FW_ADAPTER_MAX_FW_IMAGE; ++i)
          {
            l_rc &= mss::readLE(i_data, l_idx, o_data.fw_ver_str[i].major);
            l_rc &= mss::readLE(i_data, l_idx, o_data.fw_ver_str[i].minor);
            l_rc &= mss::readLE(i_data, l_idx, o_data.fw_ver_str[i].build_num);
            l_rc &= mss::readLE(i_data, l_idx, o_data.fw_ver_str[i].build_patch);
            l_rc &= mss::readLE(i_data, l_idx, o_data.fw_ver_str[i].sector_size);
          }
          l_rc &= mss::readLE(i_data, l_idx, o_data.ram_size_in_bytes);
          l_rc &= mss::readLEArray(i_data, FW_ADAPTER_CHIP_VERSION_SIZE, l_idx, o_data.chip_version);
          l_rc &= mss::readLEArray(i_data, FW_ADAPTER_SPI_FLASH_ID_SIZE, l_idx, o_data.spi_flash_id);
          l_rc &= mss::readLE(i_data, l_idx, o_data.spi_flash_size);
          l_rc &= mss::readLE(i_data, l_idx, o_data.error_buffer_offset);
          l_rc &= mss::readLE(i_data, l_idx, o_data.error_buffer_size);
        }
        else
        {
            TS_FAIL("fw_adapter_properties_struct_from_little_endian(): "
              "not enough data present (%d, expected %d)",
              i_data.size(), sizeof(o_data) );
        }
        return l_rc;
    }

    /**
     *  @brief Send and check get_properties Explorer inband command
     *  @return Number of failures
     */
    int sendOcmbInbandCmdRsp(bool setScomI2c)
    {
        errlHndl_t l_errl = nullptr;
        uint8_t failures = 0;

        // Create a vector of TARGETING::Target pointers
        TARGETING::TargetHandleList l_chipList;

        // Get a list of all of the functioning ocmb chips
        TARGETING::getAllChips(l_chipList, TARGETING::TYPE_OCMB_CHIP, true);

        host_fw_command_struct l_cmd;
        host_fw_response_struct l_rsp;
        std::vector<uint8_t> l_rsp_data;

        // Create a non-destructive get_properties command
        buildPropertiesGetCmd(l_cmd);

        for (auto & l_ocmb: l_chipList)
        {
            do
            {
                if (setScomI2c)
                {
                    FAPI_INF("sendOcmbInbandCmdRsp: testing 0x%.8X OCMB using I2C", TARGETING::get_huid(l_ocmb));
                    // disable inband and use i2c when possible
                    exptest::disableInbandScomsOcmb(l_ocmb);
                }
                else
                {
                    FAPI_INF("sendOcmbInbandCmdRsp: testing 0x%.8X OCMB using MMIO", TARGETING::get_huid(l_ocmb));
                    // just incase some other test disabled inband scoms
                    exptest::enableInbandScomsOcmb(l_ocmb);
                }

                fapi2::Target<fapi2::TARGET_TYPE_OCMB_CHIP>l_fapi2_target( l_ocmb );

                TRACFBIN(g_trac_expscom, "l_cmd: ", &l_cmd, sizeof(host_fw_command_struct));

                // send the command
                FAPI_INVOKE_HWP(l_errl, mss::exp::ib::putCMD, l_fapi2_target,
                                l_cmd);
                if (l_errl)
                {
                    TS_FAIL("Error from putCMD for 0x%.8X target",
                        TARGETING::get_huid(l_ocmb));
                    failures++;
                    break;
                }

                FAPI_INF("sendOcmbInbandCmdRsp: reading response");

                // grab the response
                FAPI_INVOKE_HWP(l_errl, mss::exp::ib::getRSP, l_fapi2_target,
                                l_rsp, l_rsp_data);
                if (l_errl)
                {
                    TS_FAIL("Error from getRSP for 0x%.8X target, plid=0x%X rc=0x%X",
                        TARGETING::get_huid(l_ocmb),
                        ERRL_GETPLID_SAFE(l_errl), ERRL_GETRC_SAFE(l_errl));
                    failures++;
                    break;
                }

                TRACFBIN(g_trac_expscom, "l_rsp: ", &l_rsp, sizeof(host_fw_response_struct));
                TRACFBIN(g_trac_expscom, "l_rsp_data: ", l_rsp_data.data(), l_rsp_data.size());

                // Check for a valid data response length
                if (l_rsp.response_length != sizeof(FW_ADAPTER_PROPERTIES_type))
                {
                    TS_FAIL("Unexpected response length 0x%.8X (expected 0x%.8X)",
                      l_rsp.response_length, sizeof(FW_ADAPTER_PROPERTIES_type));
                    failures++;
                    break;
                }

                // Now convert the little endian response data into big endian
                FW_ADAPTER_PROPERTIES_type l_fw_adapter_data;
                fw_adapter_properties_struct_from_little_endian(l_fw_adapter_data,
                                                                l_rsp_data);

                // Check for some expected response values
                // Simics should return 0x88 as the first byte of chip_version
                if (l_fw_adapter_data.chip_version[0] != 0x88 )
                {
                    TS_FAIL("Expected chip_version to start with 0x88, found 0x%02X",
                        l_fw_adapter_data.chip_version[0]);
                    failures++;
                }
            } while (0);

            if (l_errl)
            {
                // Commit the error as this is NOT expected and
                // needs to be investigated
                errlCommit( l_errl, TARG_COMP_ID );
            }

            if (setScomI2c)
            {
                // Default the ocmb back to inband communication
                exptest::enableInbandScomsOcmb(l_ocmb);
            }
        }

        FAPI_INF("sendOcmbInbandCmdRsp: exiting");
        return failures;
    };

    /**
     *  @brief Test the Explorer inband command/response path over MMIO
     */
    void  testOcmbInbandCmdRspOverMMIO( void )
    {
        if (!iv_serializeTestMutex)
        {
            TS_FAIL("iv_serializedTestMutex is not setup, unable to continue");
        }
        else
        {
            // Inband operations can't be run at the same time
            // atomic section >>
            mutex_lock(iv_serializeTestMutex);

            int failures = sendOcmbInbandCmdRsp(false);
            if (failures)
            {
                TS_FAIL("testOcmbInbandCmdRspOverMMIO() failed: %d", failures);
            }
            mutex_unlock(iv_serializeTestMutex);
        }
    }

    /**
     *  @brief Test the Explorer inband command/response path over I2C
     *         using ATTR_FORCE_SRAM_MMIO_OVER_I2C
     */
    void  testOcmbInbandCmdRspOverI2c_via_force( void )
    {
        FAPI_INF("testOcmbInbandCmdRspOverI2c_via_force: entering");
        if (!iv_serializeTestMutex)
        {
            TS_FAIL("iv_serializedTestMutex is not setup, unable to continue");
        }
        else
        {
            // Inband operations can't be run at the same time
            // atomic section >>
            mutex_lock(iv_serializeTestMutex);

            // Set FORCE_SRAM_MMIO_OVER_I2C to change to use I2C instead of MMIO
            TARGETING::Target * l_sys = nullptr;
            TARGETING::targetService().getTopLevelTarget(l_sys);
            crit_assert(l_sys != nullptr);

            l_sys->setAttr<TARGETING::ATTR_FORCE_SRAM_MMIO_OVER_I2C>(0x01);

            int failures = sendOcmbInbandCmdRsp(false);
            if (failures)
            {
                TS_FAIL("testOcmbInbandCmdRspOverI2c_via_force() failed: %d", failures);
            }

            // Restore using MMIO instead of I2C
            l_sys->setAttr<TARGETING::ATTR_FORCE_SRAM_MMIO_OVER_I2C>(0x00);

            mutex_unlock(iv_serializeTestMutex);
        }
        FAPI_INF("testOcmbInbandCmdRspOverI2c_via_force: exiting");
    }

    /**
     *  @brief Test the Explorer inband command/response path over I2C
     *         using scom setting to i2c
     */
    void  testOcmbInbandCmdRspOverI2c_via_scom_switch( void )
    {
        FAPI_INF("testOcmbInbandCmdRspOverI2c_via_scom_switch: entering");
        if (!iv_serializeTestMutex)
        {
            TS_FAIL("iv_serializedTestMutex is not setup, unable to continue");
        }
        else
        {
            // Inband operations can't be run at the same time
            // atomic section >>
            mutex_lock(iv_serializeTestMutex);

            // Set SCOM_SWITCHES to use i2c instead of MMMIO when
            // running the inband cmd/rsp operations
            int failures = sendOcmbInbandCmdRsp(true);
            if (failures)
            {
                TS_FAIL("testOcmbInbandCmdRspOverI2c_via_scom_switch() failed: %d", failures);
            }

            mutex_unlock(iv_serializeTestMutex);
        }
        FAPI_INF("testOcmbInbandCmdRspOverI2c_via_scom_switch: exiting");
    }

    /**
     * @brief Constructor
     */
    OCMBCommTest() : CxxTest::TestSuite()
    {
        // All modules are loaded by runtime,
        // so testcase loading of modules is not required
#ifndef __HOSTBOOT_RUNTIME
        errlHndl_t err = nullptr;

        err = exptest::loadModule(exptest::MSS_LIBRARY_NAME);
        if(err)
        {
            TS_FAIL("OCMBCommTest() - Constuctor: failed to load MSS module");
            errlCommit( err, TARG_COMP_ID );
        }
#endif
        iv_serializeTestMutex = exptest::getTestMutex();
    };

    /**
     * @brief Destructor
     */
    ~OCMBCommTest()
    {
    };

    private:
      // This is used for tests that need to not run operations at the same time
      TARGETING::HB_MUTEX_SERIALIZE_TEST_LOCK_ATTR iv_serializeTestMutex;

};

#endif
OpenPOWER on IntegriCloud