summaryrefslogtreecommitdiffstats
path: root/src/usr/i2c/fapi_i2c_dd.C
blob: d800af376b4c6a914b43db379109c381f2373904 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/i2c/fapi_i2c_dd.C $                                   */
/*                                                                        */
/* 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                                                     */
/**
 *  @file fapi_i2c_dd.C
 *
 *  @brief Provides a thin layer on top of our I2C device driver to
 *         serve as the back-end for the FAPI i2c interfaces.
 *         It will locate the appropriate i2c device information
 *         and will perform retries as needed.
 */

#include <trace/interface.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <errl/errludtarget.H>
#include <errl/errludstring.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/commontargeting.H>
#include <devicefw/driverif.H>
#include <i2c/i2creasoncodes.H>
#include "fapi_i2c_dd.H"

extern trace_desc_t* g_trac_i2c;

using namespace DeviceFW;

namespace FAPI_I2C_DD
{

// Easy macro replace for unit testing
//#define TRACUCOMP(args...)  TRACFCOMP(args)
#define TRACUCOMP(args...)


// ------------------------------------------------------------------
// errorIsRetryable
// ------------------------------------------------------------------
static bool errorIsRetryable(uint16_t reasonCode)
{
    return reasonCode == I2C::I2C_NACK_ONLY_FOUND ||
        reasonCode == I2C::I2C_ARBITRATION_LOST_ONLY_FOUND;
}

// Link to device driver interface
DEVICE_REGISTER_ROUTE( DeviceFW::WILDCARD,
                       DeviceFW::FAPI_I2C,
                       TARGETING::TYPE_OCMB_CHIP,
                       fapiI2cPerformOp );

DEVICE_REGISTER_ROUTE( DeviceFW::WILDCARD,
                       DeviceFW::FAPI_I2C,
                       TARGETING::TYPE_PMIC,
                       fapiI2cPerformOp );

errlHndl_t fapiI2cPerformOp(DeviceFW::OperationType i_opType,
                            TARGETING::Target * i_target,
                            void * io_buffer,
                            size_t & io_buflen,
                            int64_t i_accessType,
                            va_list i_args)
{
    errlHndl_t l_err = nullptr;
    errlHndl_t l_err_retryable = nullptr;
    const uint8_t FAPI_I2C_MAX_RETRIES = 2;

    TARGETING::ATTR_FAPI_I2C_CONTROL_INFO_type l_i2cInfo;
    uint8_t * l_cfgData = nullptr;

    size_t l_cfgSize = va_arg( i_args, size_t );
    if (l_cfgSize > 0)
    {
        l_cfgData = va_arg( i_args, uint8_t* );
    }

    TRACUCOMP(g_trac_i2c, ENTER_MRK"fapiI2cPerformOp(): "
      "%s operation on target %.8X",
      (i_opType==DeviceFW::READ)?"READ":"WRITE",
      TARGETING::get_huid(i_target) );

    do
    {
        // Grab i2c access information
        l_err = readI2cAttributes (i_target, l_i2cInfo);
        if( l_err )
        {
            break;
        }

        // grab target pointer to master
        TARGETING::TargetService& ts = TARGETING::targetService();
        TARGETING::Target * i2cm = ts.toTarget(l_i2cInfo.i2cMasterPath);

        TARGETING::Target * sys = nullptr;
        ts.getTopLevelTarget( sys );

        // master target has to exist and can not be just sys target
        if( (i2cm == nullptr) || (i2cm == sys) )
        {
            char* l_masterPath = l_i2cInfo.i2cMasterPath.toString();
            TRACFCOMP( g_trac_i2c, ERR_MRK"fapiI2cPerformOp() - "
                       "I2C Master path (%s) not valid",
                       l_masterPath);
            free(l_masterPath);
            l_masterPath = nullptr;

            /*@
             * @errortype
             * @reasoncode       I2C::INVALID_MASTER_TARGET
             * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid         I2C::FAPI_I2C_PERFORM_OP
             * @userdata1        HUID of target with FAPI_I2C_CONTROL_INFO
             * @devdesc          Invalid I2C master path
             * @custdesc         A problem occurred during the IPL
             *                   of the system.
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          I2C::FAPI_I2C_PERFORM_OP,
                                          I2C::INVALID_MASTER_TARGET,
                                          TARGETING::get_huid(i_target),
                                          0,
                                          true /*Add HB SW Callout*/ );

            l_err->collectTrace( I2C_COMP_NAME );
            break;
        }

        // Verify valid operation type
        if ((i_opType != DeviceFW::READ) && (i_opType != DeviceFW::WRITE))
        {
            TRACFCOMP( g_trac_i2c,
                       ERR_MRK"fapiI2cPerformOp() - Invalid OP type %d.",
                       i_opType );
            /*@
             * @errortype
             * @reasoncode       I2C::I2C_INVALID_OP_TYPE
             * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid         I2C::FAPI_I2C_PERFORM_OP
             * @userdata1        OP type
             * @userdata2        HUID of target
             * @devdesc          Invalid I2C device type
             * @custdesc         A problem occurred during the IPL
             *                   of the system.
             */
            l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          I2C::FAPI_I2C_PERFORM_OP,
                                          I2C::I2C_INVALID_OP_TYPE,
                                          i_opType,
                                          TARGETING::get_huid(i_target),
                                          true /*Add HB SW Callout*/ );

            l_err->collectTrace( I2C_COMP_NAME );
            break;
        }

        ////////////////////////////////////////////////////////////////////////
        // Attempt valid i2c operation multiple times ONLY on retryable fails
        ////////////////////////////////////////////////////////////////////////
        for ( uint8_t retry = 0; retry <= FAPI_I2C_MAX_RETRIES; ++retry )
        {
            if( i_opType == DeviceFW::READ )
            {
                l_err = i2cRead( i2cm,
                                 io_buffer,
                                 io_buflen,
                                 &l_i2cInfo,
                                 l_cfgData,
                                 l_cfgSize );
            }
            else if (i_opType == DeviceFW::WRITE )
            {
                l_err = i2cWrite( i2cm,
                                  io_buffer,
                                  io_buflen,
                                  &l_i2cInfo );
            }

            if ( nullptr == l_err )
            {
                // Operation completed successfully
                // break from retry loop
                break;
            }
            else if ( !errorIsRetryable( l_err->reasonCode() ) )
            {
                // Non-retryable failure
                TRACFCOMP( g_trac_i2c, ERR_MRK"fapiI2cPerformOp(): Non-Nack "
                           "Error: rc=0x%X, tgt=0x%X, No Retry (retry=%d)",
                            l_err->reasonCode(),
                            TARGETING::get_huid(i_target), retry);
                l_err->collectTrace(I2C_COMP_NAME);

                // break from retry loop
                break;
            }
            else // Handle retryable error
            {
                // If op will be attempted again: save log and continue
                if ( retry < FAPI_I2C_MAX_RETRIES )
                {
                    // Only save original retryable error
                    if ( nullptr == l_err_retryable )
                    {
                        // Save original retryable error
                        l_err_retryable = l_err;

                        TRACFCOMP( g_trac_i2c, ERR_MRK"fapiI2cPerformOp(): "
                           "Retryable Error rc=0x%X, eid=0x%X, tgt=0x%X, "
                           "retry/MAX=%d/%d. Save error and retry",
                           l_err_retryable->reasonCode(),
                           l_err_retryable->eid(),
                           TARGETING::get_huid(i_target),
                           retry, FAPI_I2C_MAX_RETRIES);
                    }
                    else
                    {
                        // Add data to original retryable error
                        TRACFCOMP( g_trac_i2c, ERR_MRK"fapiI2cPerformOp(): "
                           "Another Retryable Error rc=0x%X, eid=0x%X "
                           "plid=0x%X, tgt=0x%X, retry/MAX=%d/%d. "
                           "Delete error and retry",
                           l_err->reasonCode(), l_err->eid(),
                           l_err->plid(), TARGETING::get_huid(i_target),
                           retry, FAPI_I2C_MAX_RETRIES );

                        ERRORLOG::ErrlUserDetailsString(
                                  "Another Retryable Error found")
                                  .addToLog(l_err_retryable);

                        // Delete this new retryable error
                        delete l_err;
                        l_err = nullptr;
                    }

                    // continue to retry
                    continue;
                }
                else // no more retries: trace and break
                {
                    TRACFCOMP( g_trac_i2c, ERR_MRK"fapiI2cPerformOp(): "
                       "Error rc=0x%X, eid=%d, tgt=0x%X. No More "
                       "Retries (retry/MAX=%d/%d). Returning Error",
                       l_err->reasonCode(), l_err->eid(),
                       TARGETING::get_huid(i_target),
                       retry, FAPI_I2C_MAX_RETRIES);

                    // break from retry loop
                    break;
                }
            } // end of handle retryable error else leg

        } // end of retryable error loop

        // Handle saved retryable error, if any
        if (l_err_retryable)
        {
            if (l_err)
            {
                // commit original retryable error with new err PLID
                l_err_retryable->plid(l_err->plid());
                TRACFCOMP(g_trac_i2c, "fapiI2cPerformOp(): Committing saved "
                    "retryable error eid=0x%X with plid of returned err 0x%X",
                    l_err_retryable->eid(), l_err_retryable->plid());

                ERRORLOG::ErrlUserDetailsTarget(i_target)
                                               .addToLog(l_err_retryable);
                l_err_retryable->collectTrace(I2C_COMP_NAME);
                errlCommit(l_err_retryable, I2C_COMP_ID);
            }
            else
            {
                // Since we eventually succeeded,
                // delete original retryable error
                TRACUCOMP(g_trac_i2c, "fapiI2cPerformOp(): Op successful, "
                    "deleting saved retryable err eid=0x%X, plid=0x%X",
                    l_err_retryable->eid(), l_err_retryable->plid());

                delete l_err_retryable;
                l_err_retryable = nullptr;
            }
        }

    } while (0);

    return l_err;
}



errlHndl_t i2cRead( TARGETING::Target * i_target,
                    void * o_buffer,
                    size_t & io_buffer_size,
                    TARGETING::ATTR_FAPI_I2C_CONTROL_INFO_type * i_i2cInfo,
                    uint8_t * i_offset_data,
                    const size_t i_offset_data_size)
{
    errlHndl_t l_err = nullptr;

    TRACUCOMP(g_trac_i2c, ENTER_MRK"i2cRead()");

    // This i2c interface writes the offset data to the device then
    // reads the value of the port w/o a stop bit in between ops
    // Note: if i_offset_data_size == 0, it will skip the write
    l_err = deviceOp( DeviceFW::READ,
                      i_target,
                      o_buffer,
                      io_buffer_size,
                      DEVICE_I2C_ADDRESS_OFFSET(
                                            i_i2cInfo->port,
                                            i_i2cInfo->engine,
                                            i_i2cInfo->devAddr,
                                            i_offset_data_size,
                                            i_offset_data,
                                            i_i2cInfo->i2cMuxBusSelector,
                                            &(i_i2cInfo->i2cMuxPath) ) );

    if( l_err )
    {

        TRACFCOMP(g_trac_i2c,
            ERR_MRK"fapi i2cRead(): read failed on %d/%d/0x%X, offsetSize=%d "
            "with eid 0x%x", i_i2cInfo->port,
            i_i2cInfo->engine, i_i2cInfo->devAddr, i_offset_data_size,
            l_err->eid());

        // Printing mux info separately, if combined, nothing is displayed
        char* l_muxPath = i_i2cInfo->i2cMuxPath.toString();
        TRACFCOMP(g_trac_i2c, ERR_MRK"fapi i2cRead(): "
                  "muxSelector=0x%X, muxPath=%s",
                  i_i2cInfo->i2cMuxBusSelector,
                  l_muxPath);
        free(l_muxPath);
        l_muxPath = nullptr;

        if (i_offset_data_size > 0)
        {
            TRACFBIN(g_trac_i2c, "i_offset_data[]",
                i_offset_data, i_offset_data_size);
        }
    }

    TRACUCOMP(g_trac_i2c, EXIT_MRK"i2cRead");

    return l_err;
}


errlHndl_t i2cWrite( TARGETING::Target * i_target,
                     void * i_buffer,
                     size_t & io_buffer_size,
                     TARGETING::ATTR_FAPI_I2C_CONTROL_INFO_type * i_i2cInfo )
{
    TRACUCOMP(g_trac_i2c, ENTER_MRK"i2cWrite");

    errlHndl_t l_err = nullptr;

    // Do the actual data write
    l_err = deviceOp( DeviceFW::WRITE,
                      i_target,
                      i_buffer,
                      io_buffer_size,
                      DEVICE_I2C_ADDRESS(i_i2cInfo->port,
                                         i_i2cInfo->engine,
                                         i_i2cInfo->devAddr,
                                         i_i2cInfo->i2cMuxBusSelector,
                                         &(i_i2cInfo->i2cMuxPath) ) );

    if( l_err )
    {
        TRACFCOMP(g_trac_i2c,
            ERR_MRK"fapi i2cWrite(): write failed on %d/%d/0x%X, length %d "
            "with eid 0x%x", i_i2cInfo->port,
            i_i2cInfo->engine, i_i2cInfo->devAddr, io_buffer_size,
            l_err->eid());

        // Printing mux info separately, if combined, nothing is displayed
        char* l_muxPath = i_i2cInfo->i2cMuxPath.toString();
        TRACFCOMP(g_trac_i2c, ERR_MRK"fapi i2cWrite(): "
                  "muxSelector=0x%X, muxPath=%s",
                  i_i2cInfo->i2cMuxBusSelector,
                  l_muxPath);
        free(l_muxPath);
        l_muxPath = nullptr;
    }


    TRACUCOMP(g_trac_i2c, EXIT_MRK"i2cWrite");
    return l_err;
}


errlHndl_t readI2cAttributes( TARGETING::Target * i_target,
                       TARGETING::ATTR_FAPI_I2C_CONTROL_INFO_type & io_i2cInfo )
{
    errlHndl_t l_err = nullptr;

    if( !(i_target->tryGetAttr<TARGETING::ATTR_FAPI_I2C_CONTROL_INFO>
                      (io_i2cInfo)) )
    {
        TRACFCOMP( g_trac_i2c,
                   ERR_MRK"readI2cAttributes() - ERROR reading "
                   "attributes for target huid %.8X",
                    TARGETING::get_huid(i_target) );

        /*@
         * @errortype
         * @reasoncode       I2C::I2C_ATTRIBUTE_NOT_FOUND
         * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
         * @moduleid         I2C::READ_I2C_ATTRIBUTES
         * @userdata1        HUID of target
         * @devdesc          FAPI_I2C_CONTROL_INFO attribute was not found
         * @custdesc         A problem occurred during the IPL
         *                   of the system.
         */
        l_err = new ERRORLOG::ErrlEntry( ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                         I2C::READ_I2C_ATTRIBUTES,
                                         I2C::I2C_ATTRIBUTE_NOT_FOUND,
                                         TARGETING::get_huid(i_target),
                                         0,
                                         true /*Add HB SW Callout*/ );

        l_err->collectTrace( I2C_COMP_NAME );
    }
    return l_err;
}

}; // end namespace FAPI_I2C

OpenPOWER on IntegriCloud