summaryrefslogtreecommitdiffstats
path: root/src/usr/i2c/eepromCache_common.C
blob: 72c98205655d0f71fac1b0b1bca08db234995c5f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/i2c/eepromCache_common.C $                            */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 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                                                     */
#include "eepromCache.H"
#include <errl/errlmanager.H>
#include <i2c/eepromif.H>
#include <i2c/eepromddreasoncodes.H>
#include <errl/errludtarget.H>

#ifdef __HOSTBOOT_RUNTIME
#include <targeting/attrrp.H>
#else
#include <sys/mm.h>
#endif

extern trace_desc_t* g_trac_eeprom;

//#define TRACSSCOMP(args...)  TRACFCOMP(args)
#define TRACSSCOMP(args...)

namespace EEPROM
{

errlHndl_t buildEepromRecordHeader(TARGETING::Target * i_target,
                                   eeprom_addr_t & io_eepromInfo,
                                   eepromRecordHeader & o_eepromRecordHeader)
{

    TARGETING::Target * l_muxTarget = nullptr;
    TARGETING::Target * l_i2cMasterTarget = nullptr;
    TARGETING::TargetService& l_targetService = TARGETING::targetService();
    errlHndl_t l_errl = nullptr;

    do{

        l_errl = eepromReadAttributes(i_target, io_eepromInfo);
        if(l_errl)
        {
            TRACFCOMP( g_trac_eeprom,
                      "buildEepromRecordHeader() error occurred reading eeprom attributes for eepromType %d, target 0x%.08X, returning!!",
                      io_eepromInfo.eepromRole,
                      TARGETING::get_huid(i_target));
            l_errl->collectTrace(EEPROM_COMP_NAME);
            break;
        }

        // Grab the I2C mux target so we can read the HUID, if the target is NULL we will not be able
        // to lookup attribute to uniquely ID this eeprom so we will not cache it
        l_muxTarget = l_targetService.toTarget( io_eepromInfo.i2cMuxPath);
        if(l_muxTarget == nullptr)
        {
            TRACFCOMP( g_trac_eeprom,
                      "buildEepromRecordHeader() Mux target associated with target 0x%.08X resolved to a nullptr , check attribute for eepromType %d. Skipping Cache",
                      TARGETING::get_huid(i_target),
                      io_eepromInfo.eepromRole);
            /*@
            * @errortype
            * @moduleid     EEPROM_CACHE_EEPROM
            * @reasoncode   EEPROM_I2C_MUX_PATH_ERROR
            * @userdata1    HUID of target we want to cache
            * @userdata2    Type of EEPROM we are caching
            * @devdesc      buildEepromRecordHeader invalid mux target
            */
            l_errl = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            EEPROM_CACHE_EEPROM,
                            EEPROM_I2C_MUX_PATH_ERROR,
                            TARGETING::get_huid(i_target),
                            io_eepromInfo.eepromRole,
                            ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
            l_errl->collectTrace(EEPROM_COMP_NAME);
            break;
        }

        // Grab the I2C master target so we can read the HUID, if the target is NULL we will not be able
        // to lookup attribute to uniquely ID this eeprom so we will not cache it
        l_i2cMasterTarget = l_targetService.toTarget( io_eepromInfo.i2cMasterPath );
        if(l_i2cMasterTarget == nullptr)
        {
            TRACFCOMP( g_trac_eeprom,
                      "buildEepromRecordHeader() I2C Master target associated with target 0x%.08X resolved to a nullptr , check attribute for eepromType %d. Skipping Cache ",
                      TARGETING::get_huid(i_target),
                      io_eepromInfo.eepromRole);
            /*@
            * @errortype
            * @moduleid     EEPROM_CACHE_EEPROM
            * @reasoncode   EEPROM_I2C_MASTER_PATH_ERROR
            * @userdata1    HUID of target we want to cache
            * @userdata2    Type of EEPROM we are caching
            * @devdesc      buildEepromRecordHeader invalid master target
            */
            l_errl = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            EEPROM_CACHE_EEPROM,
                            EEPROM_I2C_MASTER_PATH_ERROR,
                            TARGETING::get_huid(i_target),
                            io_eepromInfo.eepromRole,
                            ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
            l_errl->collectTrace(EEPROM_COMP_NAME);
            break;
        }

        // This is what we will compare w/ when we are going through the existing
        // caches in the eeprom to see if we have already cached something
        // Or if no matches are found we will copy this into the header
        o_eepromRecordHeader.completeRecord.i2c_master_huid = l_i2cMasterTarget->getAttr<TARGETING::ATTR_HUID>();
        o_eepromRecordHeader.completeRecord.port            = static_cast<uint8_t>(io_eepromInfo.port);
        o_eepromRecordHeader.completeRecord.engine          = static_cast<uint8_t>(io_eepromInfo.engine);
        o_eepromRecordHeader.completeRecord.devAddr         = static_cast<uint8_t>(io_eepromInfo.devAddr);
        o_eepromRecordHeader.completeRecord.mux_select      = static_cast<uint8_t>(io_eepromInfo.i2cMuxBusSelector);
        o_eepromRecordHeader.completeRecord.cache_copy_size     = static_cast<uint32_t>(io_eepromInfo.devSize_KB);

        // Do not set valid bit nor internal offset here as we do not have
        // enough information availible to determine

    }while(0);

    return l_errl;
}

errlHndl_t eepromPerformOpCache(DeviceFW::OperationType i_opType,
                                TARGETING::Target * i_target,
                                void *  io_buffer,
                                size_t& io_buflen,
                                eeprom_addr_t &i_eepromInfo)
{
    errlHndl_t l_errl = nullptr;
    eepromRecordHeader l_eepromRecordHeader;

    do{

        TRACSSCOMP( g_trac_eeprom, ENTER_MRK"eepromPerformOpCache() "
                    "Target HUID 0x%.08X Enter", TARGETING::get_huid(i_target));

        l_errl = buildEepromRecordHeader(i_target,
                                         i_eepromInfo,
                                         l_eepromRecordHeader);

        if(l_errl)
        {
            // buildEepromRecordHeader should have traced any relavent information if
            // it was needed, just break out and pass the error along
            break;
        }

#ifndef __HOSTBOOT_RUNTIME
        uint64_t l_eepromCacheVaddr = lookupEepromCacheAddr(l_eepromRecordHeader);
#else
        uint8_t l_instance = TARGETING::AttrRP::getNodeId(i_target);
        uint64_t l_eepromCacheVaddr = lookupEepromCacheAddr(l_eepromRecordHeader, l_instance);
#endif

        // Ensure that a copy of the eeprom exists in our map of cached eeproms
        if(l_eepromCacheVaddr)
        {
            // First check if io_buffer is a nullptr, if so then assume user is
            // requesting size back in io_bufferlen
            if(io_buffer == nullptr)
            {
                io_buflen = l_eepromRecordHeader.completeRecord.cache_copy_size * KILOBYTE;
                TRACSSCOMP( g_trac_eeprom, "eepromPerformOpCache() "
                            "io_buffer == nullptr , returning io_buflen as 0x%lx",
                            io_buflen);
                break;
            }

            TRACSSCOMP( g_trac_eeprom, "eepromPerformOpCache() "
                    "Performing %s on target 0x%.08X offset 0x%lx   length 0x%x     vaddr 0x%lx",
                    (i_opType == DeviceFW::READ) ? "READ" : "WRITE",
                    TARGETING::get_huid(i_target),
                    i_eepromInfo.offset, io_buflen, l_eepromCacheVaddr);

            // Make sure that offset + buflen are less than the total size of the eeprom
            if(i_eepromInfo.offset + io_buflen >
              (l_eepromRecordHeader.completeRecord.cache_copy_size * KILOBYTE))
            {
                TRACFCOMP(g_trac_eeprom,
                          ERR_MRK"eepromPerformOpCache: i_eepromInfo.offset + i_offset is greater than size of eeprom (0x%x KB)",
                          l_eepromRecordHeader.completeRecord.cache_copy_size);
                /*@
                * @errortype
                * @moduleid     EEPROM_CACHE_PERFORM_OP
                * @reasoncode   EEPROM_OVERFLOW_ERROR
                * @userdata1    Length of Operation
                * @userdata2    Offset we are attempting to read/write
                * @custdesc     Soft error in Firmware
                * @devdesc      cacheEeprom invalid op type
                */
                l_errl = new ERRORLOG::ErrlEntry(
                                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                EEPROM_CACHE_PERFORM_OP,
                                EEPROM_OVERFLOW_ERROR,
                                TO_UINT64(io_buflen),
                                TO_UINT64(i_eepromInfo.offset),
                                ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
                ERRORLOG::ErrlUserDetailsTarget(i_target).addToLog(l_errl);
                l_errl->collectTrace( EEPROM_COMP_NAME );

                break;
            }

            if(i_opType == DeviceFW::READ)
            {
                memcpy(io_buffer,
                       reinterpret_cast<void *>(l_eepromCacheVaddr + i_eepromInfo.offset),
                       io_buflen);
            }
            else if(i_opType == DeviceFW::WRITE)
            {
                memcpy(reinterpret_cast<void *>(l_eepromCacheVaddr + i_eepromInfo.offset),
                       io_buffer,
                       io_buflen);

#ifndef __HOSTBOOT_RUNTIME
                // Perform flush to ensure pnor is updated
                int rc = mm_remove_pages( FLUSH,
                                          reinterpret_cast<void *>(l_eepromCacheVaddr + i_eepromInfo.offset),
                                          io_buflen );
                if( rc )
                {
                    TRACFCOMP(g_trac_eeprom,
                              ERR_MRK"eepromPerformOpCache:  Error from mm_remove_pages trying for flush contents write to pnor! rc=%d",
                              rc);
                    /*@
                    * @errortype
                    * @moduleid     EEPROM_CACHE_PERFORM_OP
                    * @reasoncode   EEPROM_FAILED_TO_FLUSH_CONTENTS
                    * @userdata1    Requested Address
                    * @userdata2    rc from mm_remove_pages
                    * @devdesc      cacheEeprom mm_remove_pages FLUSH failed
                    */
                    l_errl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    EEPROM_CACHE_PERFORM_OP,
                                    EEPROM_FAILED_TO_FLUSH_CONTENTS,
                                    (l_eepromCacheVaddr + i_eepromInfo.offset),
                                    TO_UINT64(rc),
                                    ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
                    l_errl->collectTrace( EEPROM_COMP_NAME );
                }
#endif
            }
            else
            {
                TRACFCOMP(g_trac_eeprom,
                          ERR_MRK"eepromPerformOpCache: Invalid OP_TYPE passed to function, i_opType=%d",
                          i_opType);
                /*@
                * @errortype
                * @moduleid     EEPROM_CACHE_PERFORM_OP
                * @reasoncode   EEPROM_INVALID_OPERATION
                * @userdata1[0:31]  Op Type that was invalid
                * @userdata1[32:63] Eeprom Role
                * @userdata2    Offset we are attempting to perfrom op on
                * @custdesc     Soft error in Firmware
                * @devdesc      cacheEeprom invalid op type
                */
                l_errl = new ERRORLOG::ErrlEntry(
                                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                EEPROM_CACHE_PERFORM_OP,
                                EEPROM_INVALID_OPERATION,
                                TWO_UINT32_TO_UINT64(i_opType,
                                                     i_eepromInfo.eepromRole),
                                TO_UINT64(i_eepromInfo.offset),
                                ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
                ERRORLOG::ErrlUserDetailsTarget(i_target).addToLog(l_errl);
                l_errl->collectTrace( EEPROM_COMP_NAME );
            }
        }
        else
        {
            TRACFCOMP( g_trac_eeprom,"eepromPerformOpCache: Failed to find entry in cache for 0x%.08X, %s failed",
                       TARGETING::get_huid(i_target),
                       (i_opType == DeviceFW::READ) ? "READ" : "WRITE");
            /*@
            * @errortype
            * @moduleid     EEPROM_CACHE_PERFORM_OP
            * @reasoncode   EEPROM_NOT_IN_CACHE
            * @userdata1[0:31]  Op Type
            * @userdata1[32:63] Eeprom Role
            * @userdata2    Offset we are attempting to read/write
            * @custdesc     Soft error in Firmware
            * @devdesc      Tried to lookup eeprom not in cache
            */
            l_errl = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            EEPROM_CACHE_PERFORM_OP,
                            EEPROM_NOT_IN_CACHE,
                            TWO_UINT32_TO_UINT64(i_opType,
                                                  i_eepromInfo.eepromRole),
                            TO_UINT64(i_eepromInfo.offset),
                            ERRORLOG::ErrlEntry::ADD_SW_CALLOUT);
            ERRORLOG::ErrlUserDetailsTarget(i_target).addToLog(l_errl);
            l_errl->collectTrace( EEPROM_COMP_NAME );
        }

        TRACSSCOMP( g_trac_eeprom, EXIT_MRK"eepromPerformOpCache() "
                    "Target HUID 0x%.08X Exit", TARGETING::get_huid(i_target));

    }while(0);

    return l_errl;
}
}
OpenPOWER on IntegriCloud