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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
/*
* ipmi_discover.cpp
*
* discover MCs
*
* Copyright (c) 2004 by FORCE Computers.
* Copyright (c) 2005 by ESO Technologies.
*
* 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>
*
* This file contains:
* 1. discover of MCs
* 2. periodic polling of MCs
* 3. periodic reading SEL
* 4. handling of events
*/
#include "ipmi_domain.h"
#include <assert.h>
#include <errno.h>
class cIpmiMcTask
{
public:
cIpmiMcTask *m_next;
tIpmiMcTask m_task;
cTime m_timeout;
void *m_userdata;
cIpmiMcTask( tIpmiMcTask task, const cTime &timeout,
void *userdata )
: m_next( 0 ), m_task( task ),
m_timeout( timeout ), m_userdata( userdata )
{}
};
cIpmiMcThread::cIpmiMcThread( cIpmiDomain *domain,
unsigned char addr,
unsigned int properties )
: m_domain( domain ), m_addr( addr ),
m_mc( 0 ),
m_properties( properties ),
m_exit( false ), m_tasks( 0 ),
m_sel( 0 ), m_events( 0 )
{
}
cIpmiMcThread::~cIpmiMcThread()
{
ClearMcTaskList();
}
void
cIpmiMcThread::WriteLock()
{
m_domain->WriteLock();
}
void
cIpmiMcThread::WriteUnlock()
{
m_domain->WriteUnlock();
}
void
cIpmiMcThread::AddMcTask( tIpmiMcTask task, const cTime &timeout,
void *userdata )
{
cIpmiMcTask *dt = new cIpmiMcTask( task, timeout, userdata );
cIpmiMcTask *prev = 0;
cIpmiMcTask *current = m_tasks;
// loop to right position
while( current && current->m_timeout <= dt->m_timeout )
{
prev = current;
current = current->m_next;
}
if ( prev == 0 )
{
// insert dt at first position
m_tasks = dt;
dt->m_next = current;
}
else
{
dt->m_next = current;
prev->m_next = dt;
}
}
void
cIpmiMcThread::AddMcTask( tIpmiMcTask task, unsigned int ms,
void *userdata )
{
cTime timeout = cTime::Now();
timeout += ms;
AddMcTask( task, timeout, userdata );
}
bool
cIpmiMcThread::RemMcTask( void *userdata )
{
bool rv = false;
cIpmiMcTask *prev = 0;
cIpmiMcTask *current = m_tasks;
// loop to right position
while( current && current->m_userdata != userdata )
{
prev = current;
current = current->m_next;
}
if ( current && current->m_userdata )
{
if ( prev == 0 )
m_tasks = current->m_next;
else
prev->m_next = current->m_next;
delete current;
rv = true;
}
else
assert( 0 );
return rv;
}
void
cIpmiMcThread::ClearMcTaskList()
{
while( m_tasks )
{
cIpmiMcTask *dt = m_tasks;
m_tasks = m_tasks->m_next;
delete dt;
}
}
void *
cIpmiMcThread::Run()
{
stdlog << "starting MC thread " << m_addr << ".\n";
m_domain->m_mc_thread_lock.Lock();
m_domain->m_num_mc_threads++;
m_domain->m_mc_thread_lock.Unlock();
if ( m_properties & dIpmiMcThreadInitialDiscover )
{
if ( m_addr != dIpmiBmcSlaveAddr )
{
stdlog << "Waiting for BMC discovery (" << m_addr << ").\n";
while ( m_domain->m_bmc_discovered == false )
{
usleep( 100000 );
}
stdlog << "BMC Discovery done, let's go (" << m_addr << ").\n";
}
else
{
stdlog << "BMC Discovery Start\n";
}
Discover();
m_domain->m_initial_discover_lock.Lock();
m_domain->m_initial_discover--;
m_domain->m_initial_discover_lock.Unlock();
// clear initial discover flag
m_properties &= ~dIpmiMcThreadInitialDiscover;
if ( m_addr == dIpmiBmcSlaveAddr )
{
stdlog << "BMC Discovery done\n";
m_domain->m_bmc_discovered = true;
}
}
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
PollAddr( m_mc );
// this is a hack, because
// of the usleep calls and polling
// of event queue and task list.
while( !m_exit )
{
// handling all events in the event
// in the event queue
HandleEvents();
usleep( 100000 );
// check for tasks to do
while( m_tasks )
{
cTime now = cTime::Now();
if ( now < m_tasks->m_timeout )
break;
// timeout
cIpmiMcTask *dt = m_tasks;
m_tasks = m_tasks->m_next;
(this->*dt->m_task)( dt->m_userdata );
delete dt;
}
}
stdlog << "stop MC thread " << m_addr << ".\n";
m_domain->m_mc_thread_lock.Lock();
assert( m_domain->m_num_mc_threads > 0 );
m_domain->m_num_mc_threads--;
m_domain->m_mc_thread_lock.Unlock();
return 0;
}
void
cIpmiMcThread::Discover( cIpmiMsg *get_device_id_rsp )
{
cIpmiAddr addr( eIpmiAddrTypeIpmb, 0, 0, m_addr );
cIpmiMsg gdi_rsp;
if ( !get_device_id_rsp )
{
// send a get device id
cIpmiMsg msg( eIpmiNetfnApp, eIpmiCmdGetDeviceId );
// try sending the command only one time
SaErrorT rv = m_domain->SendCommand( addr, msg, gdi_rsp, 1 );
if ( rv != SA_OK || gdi_rsp.m_data[0] != 0 )
return;
get_device_id_rsp = &gdi_rsp;
}
stdlog << "MC at " << m_addr << " found:\n";
stdlog << "\tdevice id : " << get_device_id_rsp->m_data[1] << "\n";
stdlog << "\tdevice SDR : " << ((get_device_id_rsp->m_data[2] & 0x80) ? "yes" : "no") << "\n";
stdlog << "\tdevice revision : " << (get_device_id_rsp->m_data[2] & 0x0f ) << "\n";
stdlog << "\tdevice available : " << ((get_device_id_rsp->m_data[3] & 0x80) ? "update" : "normal operation" ) << "\n";
stdlog << "\tmajor FW revision : " << (get_device_id_rsp->m_data[3] & 0x7f) << "\n";
stdlog << "\tminor FW revision : " << (int)((get_device_id_rsp->m_data[4] >>4) & 0xf)
<< (int)(get_device_id_rsp->m_data[4] & 0xf) << "\n";
stdlog << "\tIPMI version : " << (int)(get_device_id_rsp->m_data[5] & 0xf) << "."
<< ((get_device_id_rsp->m_data[5] >> 4) & 0xf) << "\n";
stdlog << "\tchassis device : " << ((get_device_id_rsp->m_data[6] & 0x80) ? "yes" : "no") << "\n";
stdlog << "\tbridge : " << ((get_device_id_rsp->m_data[6] & 0x40) ? "yes" : "no") << "\n";
stdlog << "\tIPMB event generator : " << ((get_device_id_rsp->m_data[6] & 0x20) ? "yes" : "no") << "\n";
stdlog << "\tIPMB event receiver : " << ((get_device_id_rsp->m_data[6] & 0x10) ? "yes" : "no") << "\n";
stdlog << "\tFRU inventory data : " << ((get_device_id_rsp->m_data[6] & 0x08) ? "yes" : "no") << "\n";
stdlog << "\tSEL device : " << ((get_device_id_rsp->m_data[6] & 0x04) ? "yes" : "no") << "\n";
stdlog << "\tSDR repository device : " << ((get_device_id_rsp->m_data[6] & 0x02) ? "yes" : "no") << "\n";
stdlog << "\tsensor device : " << ((get_device_id_rsp->m_data[6] & 0x01) ? "yes" : "no") << "\n";
unsigned int mid = get_device_id_rsp->m_data[7]
| (get_device_id_rsp->m_data[8] << 8)
| (get_device_id_rsp->m_data[9] << 16);
stdlog.Hex();
stdlog << "\tmanufacturer id : " << mid << "\n";
unsigned int pid = IpmiGetUint16( get_device_id_rsp->m_data + 10 );
stdlog << "\tproduct id : " << pid << "\n";
if ( m_mc )
{
// m_mc should be NULL here
// Let's clean up this mess
stdlog << "m_mc not NULL !\n";
m_mc->Cleanup();
delete m_mc;
m_mc = 0;
return;
}
m_mc = new cIpmiMc( m_domain, addr );
int rrv = m_mc->GetDeviceIdDataFromRsp( *get_device_id_rsp );
if ( rrv )
{
// If we couldn't handle the device data, just clean
// it up
stdlog << "couldn't handle the device data !\n";
m_mc->Cleanup();
delete m_mc;
m_mc = 0;
return;
}
m_mc->CheckTca();
if ( m_domain->IsTca() )
{
// If board is not ATCA, just give up
if (!m_mc->IsTcaMc())
{
m_mc->Cleanup();
delete m_mc;
m_mc = 0;
return;
}
}
if (( m_domain->m_enable_sel_on_all == false ) && ( addr.SlaveAddr() != dIpmiBmcSlaveAddr ))
{
stdlog << "Disabling SEL for MC " << addr.SlaveAddr() << "\n";
m_mc->SetSel(false);
}
cIpmiMcVendor *mv = cIpmiMcVendorFactory::GetFactory()->Get( mid, pid );
m_mc->SetVendor( mv );
if ( mv->InitMc( m_mc, *get_device_id_rsp ) == false )
{
stdlog << "cannot initialize MC: " << (unsigned char)m_mc->GetAddress() << " !\n";
m_mc->Cleanup();
delete m_mc;
m_mc = 0;
return;
}
SaErrorT rv = m_mc->HandleNew();
if ( rv != SA_OK )
{
stdlog << "ERROR while discover MC " << m_addr << ", giving up !\n";
m_mc->Cleanup();
delete m_mc;
m_mc = 0;
return;
}
WriteLock();
m_domain->AddMc( m_mc );
m_mc->Populate();
WriteUnlock();
if ( m_mc->SelDeviceSupport() )
{
GList *new_events = m_mc->Sel()->GetEvents();
// We only care for events from the BMC SEL
if (( m_addr == dIpmiBmcSlaveAddr )
&& ( new_events ))
m_domain->HandleEvents( new_events );
}
if ( m_mc->SelDeviceSupport() )
{
assert( m_sel == 0 );
stdlog << "addr " << m_addr << ": add read sel. cIpmiMcThread::Discover\n";
m_sel = m_mc->Sel();
AddMcTask( &cIpmiMcThread::ReadSel,
m_domain->m_sel_rescan_interval,
m_sel );
}
}
void
cIpmiMcThread::HandleEvents()
{
bool loop = true;
while( loop )
{
cIpmiEvent *event = 0;
m_events_lock.Lock();
if ( m_events )
{
event = (cIpmiEvent *)m_events->data;
m_events = g_list_remove( m_events, event );
}
loop = m_events ? true : false;
m_events_lock.Unlock();
if ( event )
{
HandleEvent( event );
delete event;
}
}
}
void
cIpmiMcThread::HandleEvent( cIpmiEvent *event )
{
// can handle only events for that mc thread
assert( event->m_data[4] == m_addr );
stdlog << "event: ";
event->Dump( stdlog, "event" );
if ( event->m_type != 0x02 )
{
stdlog << "remove event: unknown event type " << (unsigned char)event->m_type << " !\n";
return;
}
if ( (event->m_data[4] & 0x01) != 0 )
{
stdlog << "remove event: system software event.\n";
return;
}
// reveive an event from an unknown MC
// => discover
if ( !m_mc )
{
assert( m_sel == 0 );
// remove old task
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
stdlog << "addr " << m_addr << ": rem poll. cIpmiMcThread::HandleEvent\n";
RemMcTask( m_mc );
}
Discover();
// add new poll task
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
stdlog << "addr " << m_addr << ": add poll. cIpmiMcThread::HandleEvent\n";
AddMcTask( &cIpmiMcThread::PollAddr, m_domain->m_mc_poll_interval,
m_mc );
}
}
if ( m_mc == 0 )
{
stdlog << "hotswap event without a MC !\n";
return;
}
cIpmiSensor *sensor = m_mc->FindSensor( (event->m_data[5] & 0x3), event->m_data[8] );
if ( sensor == 0 )
{
stdlog << "sensor of event not found !\n";
return;
}
// hotswap event
if ( event->m_data[7] == eIpmiSensorTypeAtcaHotSwap )
{
cIpmiSensorHotswap *hs = dynamic_cast<cIpmiSensorHotswap *>( sensor );
if ( !hs )
{
stdlog << "Not a hotswap sensor !\n";
return;
}
HandleHotswapEvent( hs, event );
return;
}
sensor->HandleEvent( event );
}
void
cIpmiMcThread::HandleHotswapEvent( cIpmiSensorHotswap *sensor,
cIpmiEvent *event )
{
tIpmiFruState current_state = (tIpmiFruState)(event->m_data[10] & 0x0f);
tIpmiFruState prev_state = (tIpmiFruState)(event->m_data[11] & 0x0f);
unsigned int fru_id = event->m_data[12] & 0xff;
stdlog << "hot swap event at MC " << m_addr << ", sensor " << sensor->Num() << ",FRU " << fru_id << ",M" << (int)prev_state << " -> M"
<< (int)current_state << ".\n";
if (sensor->Resource()->GetHotswapSensor() != sensor)
{
stdlog << "WARNING: sensor NOT resource hot swap sensor, discard event\n";
return;
}
if (sensor->Resource()->FruId() != fru_id)
{
stdlog << "WARNING: FRU id NOT resource FRU id, discard event\n";
return;
}
// remove old task
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
stdlog << "addr " << m_addr << ": rem poll. cIpmiMcThread::HandleHotswapEvent\n";
RemMcTask( m_mc );
}
sensor->Resource()->PicmgFruState() = current_state;
sensor->HandleEvent( event );
switch (current_state)
{
case eIpmiFruStateNotInstalled:
// We care only if it's the MC itself
if ( sensor->Resource()->FruId() == 0 )
{
// remove mc
WriteLock();
if ( m_mc )
m_domain->CleanupMc( m_mc );
WriteUnlock();
m_mc = 0;
}
break;
case eIpmiFruStateActivationRequest:
if (sensor->Resource()->Domain()->InsertTimeout() == SAHPI_TIMEOUT_IMMEDIATE)
{
sensor->Resource()->Activate();
}
else
{
sensor->Resource()->PolicyCanceled() = false;
}
break;
case eIpmiFruStateDeactivationRequest:
if (sensor->Resource()->ExtractTimeout() == SAHPI_TIMEOUT_IMMEDIATE)
{
sensor->Resource()->Deactivate();
}
else
{
sensor->Resource()->PolicyCanceled() = false;
}
break;
default:
break;
}
if ( m_mc == 0 && m_sel )
{
RemMcTask( m_sel );
m_sel = 0;
}
// add new poll task
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
stdlog << "addr " << m_addr << ": add poll. cIpmiMcThread::HandleHotswapEvent\n";
AddMcTask( &cIpmiMcThread::PollAddr, m_domain->m_mc_poll_interval, m_mc );
}
}
void
cIpmiMcThread::AddEvent( cIpmiEvent *event )
{
m_events_lock.Lock();
m_events = g_list_append( m_events, event );
m_events_lock.Unlock();
}
void
cIpmiMcThread::PollAddr( void *userdata )
{
cIpmiMc *mc = (cIpmiMc *)userdata;
if ( m_domain->ConLogLevel( dIpmiConLogCmd ) )
stdlog << "poll MC at " << m_addr << ".\n";
// send a get device id
cIpmiAddr addr( eIpmiAddrTypeIpmb, 0, 0, m_addr );
cIpmiMsg msg( eIpmiNetfnApp, eIpmiCmdGetDeviceId );
cIpmiMsg rsp;
SaErrorT rv = m_domain->SendCommand( addr, msg, rsp, 3 );
if ( rv != SA_OK )
{
if ( m_mc )
{
stdlog << "communication lost: " << m_addr << " !\n";
if ( m_properties & dIpmiMcThreadCreateM0 )
{
cIpmiSensorHotswap *hs = m_mc->FindHotswapSensor();
if ( hs )
{
// generate an event hotswap event M0
cIpmiEvent *event = new cIpmiEvent;
event->m_mc = m_mc;
event->m_data[0] = 0; // timestamp
event->m_data[1] = 0;
event->m_data[2] = 0;
event->m_data[3] = 0;
event->m_data[4] = m_mc->GetAddress();
event->m_data[5] = 0;
event->m_data[6] = 0x04; // message format
event->m_data[7] = hs->SensorType();
event->m_data[8] = hs->Num();
event->m_data[9] = 0; // assertion
event->m_data[10] = 0; // M0
event->m_data[11] = hs->Resource()->PicmgFruState() | (7 << 4); // communication lost
event->m_data[12] = 0;
// this is because HandleHotswapEvent first removes the PollAddr task
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
stdlog << "addr " << m_addr << ": add poll. cIpmiMcThread::PollAddr\n";
AddMcTask( &cIpmiMcThread::PollAddr, m_domain->m_mc_poll_interval, m_mc );
}
HandleHotswapEvent( hs, event );
delete event;
return;
}
}
m_domain->CleanupMc( mc );
m_mc = 0;
}
}
else
{
if ( !mc )
// MC found.
Discover( &rsp );
// else if ( m_mc )
// Periodically check the event receiver for existing MCs.
// m_mc->CheckEventRcvr();
}
if ( m_mc == 0 && m_sel )
{
RemMcTask( m_sel );
m_sel = 0;
}
if ( ( m_mc && (m_properties & dIpmiMcThreadPollAliveMc ) )
|| ( !m_mc && (m_properties & dIpmiMcThreadPollDeadMc ) ) )
{
if ( m_domain->ConLogLevel( dIpmiConLogCmd ) )
stdlog << "addr " << m_addr << ": add poll. cIpmiMcThread::PollAddr\n";
AddMcTask( &cIpmiMcThread::PollAddr, m_domain->m_mc_poll_interval, m_mc );
}
}
void
cIpmiMcThread::ReadSel( void *userdata )
{
cIpmiSel *sel = (cIpmiSel *)userdata;
GList *new_events = sel->GetEvents();
if ( m_domain->ConLogLevel( dIpmiConLogCmd ) )
stdlog << "addr " << m_addr << ": add sel reading. cIpmiMcThread::ReadSel\n";
// add myself to task list
AddMcTask( &cIpmiMcThread::ReadSel, m_domain->m_sel_rescan_interval,
userdata );
// We only care for events from the BMC SEL
if (( m_addr == dIpmiBmcSlaveAddr )
&& ( new_events ))
m_domain->HandleEvents( new_events );
}
|