File: ipmi_sensor_hotswap.cpp

package info (click to toggle)
openhpi 2.14.1-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 20,380 kB
  • sloc: ansic: 187,087; cpp: 32,188; sh: 10,415; makefile: 4,467; perl: 1,529
file content (361 lines) | stat: -rw-r--r-- 9,401 bytes parent folder | download | duplicates (8)
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
/*
 * ipmi_sensor_hotswap.cpp
 *
 * Copyright (c) 2004 by FORCE Computers
 * Copyright (c) 2005 by ESO Technologies.
 *
 * Note that this file is based on parts of OpenIPMI
 * written by Corey Minyard <minyard@mvista.com>
 * of MontaVista Software. Corey's code was helpful
 * and many thanks go to him. He gave the permission
 * to use this code in OpenHPI under BSD license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Authors:
 *     Thomas Kanngieser <thomas.kanngieser@fci.com>
 *     Pierre Sangouard  <psangouard@eso-tech.com>
 */

#include "ipmi_sensor_hotswap.h"
#include "ipmi_domain.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <math.h>
#include <oh_utils.h>

cIpmiSensorHotswap::cIpmiSensorHotswap( cIpmiMc *mc )
  : cIpmiSensorDiscrete( mc )
{
}


cIpmiSensorHotswap::~cIpmiSensorHotswap()
{
}


bool
cIpmiSensorHotswap::GetDataFromSdr( cIpmiMc *mc, cIpmiSdr *sdr )
{
  if ( !cIpmiSensorDiscrete::GetDataFromSdr( mc, sdr ) )
       return false;

  return true;
}


bool
cIpmiSensorHotswap::CreateRdr( SaHpiRptEntryT &resource,
                               SaHpiRdrT &rdr )
{
  if ( cIpmiSensorDiscrete::CreateRdr( resource, rdr ) == false )
       return false;

  cIpmiResource *res = Resource();

  if( !res )
       return false;

  if (EntityPath() == res->EntityPath())
  {
    // update resource capabilities
    resource.ResourceCapabilities |= SAHPI_CAPABILITY_MANAGED_HOTSWAP;
    resource.HotSwapCapabilities  |= SAHPI_HS_CAPABILITY_INDICATOR_SUPPORTED;
  }

  return true;
}


SaHpiHsStateT
cIpmiSensorHotswap::ConvertIpmiToHpiHotswapState( tIpmiFruState h )
{
  switch( h )
     {
       case eIpmiFruStateNotInstalled:
            return SAHPI_HS_STATE_NOT_PRESENT;

       case eIpmiFruStateInactive:
            return SAHPI_HS_STATE_INACTIVE;

       case eIpmiFruStateActivationRequest:
            return SAHPI_HS_STATE_INSERTION_PENDING;

       case eIpmiFruStateActivationInProgress:
            return SAHPI_HS_STATE_INSERTION_PENDING;

       case eIpmiFruStateActive:
            return SAHPI_HS_STATE_ACTIVE;

       case eIpmiFruStateDeactivationRequest:
            return SAHPI_HS_STATE_EXTRACTION_PENDING;

       case eIpmiFruStateDeactivationInProgress:
            return SAHPI_HS_STATE_EXTRACTION_PENDING;

       case eIpmiFruStateCommunicationLost:
       default:
            return SAHPI_HS_STATE_NOT_PRESENT;
     }
}

void
cIpmiSensorHotswap::HandleEvent( cIpmiEvent *event )
{
  cIpmiResource *res = Resource();

  if( !res )
     {
       stdlog << "cIpmiSensorHotswap::HandleEvent: No resource !\n";
       return;
     }

  tIpmiFruState state = (tIpmiFruState)(event->m_data[10] & 0x07);
  tIpmiFruState previous_state = (tIpmiFruState)(event->m_data[11] & 0x07);

  switch (state)
  {
  case eIpmiFruStateActivationInProgress:
    if (previous_state == eIpmiFruStateActivationRequest)
    {
        stdlog << "cIpmiSensorHotswap::HandleEvent: M2->M3 ignore\n";
        res->PreviousPrevFruState() = previous_state;
        return;
    }
    break;
  case eIpmiFruStateDeactivationInProgress:
    if (previous_state == eIpmiFruStateDeactivationRequest)
    {
        stdlog << "cIpmiSensorHotswap::HandleEvent: M5->M6 ignore\n";
        res->PreviousPrevFruState() = previous_state;
        return;
    }
    if (previous_state == eIpmiFruStateActivationInProgress)
    {
        stdlog << "cIpmiSensorHotswap::HandleEvent: M3->M6 ignore\n";
        res->PreviousPrevFruState() = previous_state;
        return;
    }
    break;
  default:
    break;
  }

  oh_event *e = (oh_event *)g_malloc0( sizeof( struct oh_event ) );

  SaHpiRptEntryT *rptentry = oh_get_resource_by_id( res->Domain()->GetHandler()->rptcache, res->m_resource_id );

  if ( rptentry )
      e->resource = *rptentry;
  else
      e->resource.ResourceCapabilities = 0;

  // hpi event
  SaHpiEventT &h = e->event;

  h.Source    = res->m_resource_id;
  h.EventType = SAHPI_ET_HOTSWAP;
  // Default severity
  h.Severity = SAHPI_INFORMATIONAL;

  // Hot swap events must be dated here otherwise
  // hot swap policy won't work properly !
  oh_gettimeofday(&h.Timestamp);

  SaHpiHotSwapEventT &he = h.EventDataUnion.HotSwapEvent;

  he.HotSwapState = ConvertIpmiToHpiHotswapState( state );
  he.PreviousHotSwapState = ConvertIpmiToHpiHotswapState( previous_state );

  switch (state)
  {
  case eIpmiFruStateNotInstalled:
    if (previous_state == eIpmiFruStateCommunicationLost)
    {
        he.PreviousHotSwapState = ConvertIpmiToHpiHotswapState( res->PreviousPrevFruState() );
    }
    else if (previous_state != eIpmiFruStateInactive)
    {
      // get severity from plugin cache
      if ( rptentry )
        h.Severity = rptentry->ResourceSeverity;
    }
    break;

  case eIpmiFruStateCommunicationLost:

    h.EventType = SAHPI_ET_RESOURCE;
    h.EventDataUnion.ResourceEvent.ResourceEventType = SAHPI_RESE_RESOURCE_FAILURE;

    stdlog << "cIpmiSensorHotswap::HandleEvent SAHPI_RESE_RESOURCE_FAILURE Event resource " << res->m_resource_id << "\n";

    if ( rptentry )
    {
      rptentry->ResourceFailed = SAHPI_TRUE;
      oh_add_resource(res->Domain()->GetHandler()->rptcache,
                      rptentry, res, 1);
      h.Severity = rptentry->ResourceSeverity;
    }
    break;

  case eIpmiFruStateInactive:

    // M3->M6->M1 special case
    if ((previous_state == eIpmiFruStateDeactivationInProgress)
        && (res->PreviousPrevFruState() == eIpmiFruStateActivationInProgress))
    {
        he.PreviousHotSwapState = SAHPI_HS_STATE_INSERTION_PENDING;
        if ( rptentry )
        {
            h.Severity = rptentry->ResourceSeverity;
        }
    }

  default:
    if (previous_state == eIpmiFruStateCommunicationLost)
    {
      h.EventType = SAHPI_ET_RESOURCE;
      h.EventDataUnion.ResourceEvent.ResourceEventType = SAHPI_RESE_RESOURCE_RESTORED;

      stdlog << "cIpmiSensorHotswap::HandleEvent SAHPI_RESE_RESOURCE_RESTORED Event resource " << res->m_resource_id << "\n";

      if ( rptentry )
      {
        rptentry->ResourceFailed = SAHPI_FALSE;
        oh_add_resource(res->Domain()->GetHandler()->rptcache,
                        rptentry, res, 1);
        h.Severity = rptentry->ResourceSeverity;
      }
    }
    break;
  }

  res->PreviousPrevFruState() = previous_state;

  if (h.EventType == SAHPI_ET_HOTSWAP)
    stdlog << "cIpmiSensorHotswap::HandleEvent SAHPI_ET_HOTSWAP Event resource " << res->m_resource_id << "\n";
  m_mc->Domain()->AddHpiEvent( e );

  if (h.EventType != SAHPI_ET_HOTSWAP)
    return;

  oh_event *oem_e = (oh_event *)g_malloc0( sizeof( struct oh_event ) );

  if ( rptentry )
      oem_e->resource = *rptentry;
  else
      oem_e->resource.ResourceCapabilities = 0;

  // hpi event
  SaHpiEventT &oem_h = oem_e->event;

  oem_h.Source    = h.Source;
  oem_h.Timestamp = h.Timestamp;
  oem_h.EventType = SAHPI_ET_OEM;
  oem_h.Severity = SAHPI_INFORMATIONAL;

  SaHpiOemEventT &oem_he = oem_h.EventDataUnion.OemEvent;

  oem_he.MId = ATCAHPI_PICMG_MID;

  oem_he.OemEventData.DataType =   SAHPI_TL_TYPE_TEXT;
  oem_he.OemEventData.Language =   SAHPI_LANG_UNDEF;
  oem_he.OemEventData.DataLength = 3;
  oem_he.OemEventData.Data[0] =    he.HotSwapState;
  oem_he.OemEventData.Data[1] =    he.PreviousHotSwapState;

  switch ((event->m_data[11] >> 4) & 0x0F)
  {
  case 0x0:
  case 0x1:
    oem_he.OemEventData.Data[2] = 0;
    break;
  case 0x3:
    oem_he.OemEventData.Data[2] = 1;
    break;
  case 0x2:
    oem_he.OemEventData.Data[2] = 2;
    break;
  case 0x7:
    oem_he.OemEventData.Data[2] = 3;
    break;
  case 0x9:
    oem_he.OemEventData.Data[2] = 4;
    break;
  case 0x6:
    oem_he.OemEventData.Data[2] = 5;
    break;
  case 0x8:
    oem_he.OemEventData.Data[2] = 6;
    break;
  case 0xF:
  default:
    oem_he.OemEventData.Data[2] = 7;
    break;
  }

  stdlog << "cIpmiSensorHotswap::HandleEvent SAHPI_ET_OEM Event resource " << res->m_resource_id << "\n";
  m_mc->Domain()->AddHpiEvent( oem_e );
}

SaErrorT
cIpmiSensorHotswap::GetPicmgState( tIpmiFruState &state )
{
  // read hotswap state
  cIpmiMsg rsp;

  // Default value just in case
  state = eIpmiFruStateCommunicationLost;

  SaErrorT rv = GetSensorData( rsp );

  if ( rv != SA_OK )
     {
       stdlog << "cannot get hotswap state !\n";
       return rv;
     }

  // Reading should be 0 according to PICMG 3.0 specification
  // However if it's not this is not a fatal error so just flag it
  if ( rsp.m_data[1] != 0 )
     {
       stdlog << "WARNING: hotswap sensor reading not 0 : " << rsp.m_data[1] << " !\n";
     }

  unsigned int value = rsp.m_data[3];

  for( int i = 0; i < 8; i++ )
       if ( value & ( 1 << i ) )
          {
            state = (tIpmiFruState)i;
            return SA_OK;
          }

  stdlog << "WRONG Hot Swap State " << value << "\n";

  return SA_ERR_HPI_INVALID_DATA;
}


SaErrorT
cIpmiSensorHotswap::GetHpiState( SaHpiHsStateT &state )
{
  tIpmiFruState fs;
  SaErrorT rv = GetPicmgState( fs );

  if ( rv != SA_OK )
       return rv;

  state = ConvertIpmiToHpiHotswapState( fs );

  return SA_OK;
}