ipmi_sdr.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760
  1. /*
  2. * Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
  3. *
  4. * Based on code from
  5. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * Redistribution of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * Redistribution in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * Neither the name of Sun Microsystems, Inc. or the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * This software is provided "AS IS," without a warranty of any kind.
  23. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  24. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  25. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  26. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  27. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  28. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  29. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  30. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  33. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34. */
  35. #define _BSD_SOURCE
  36. #include <string.h>
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <time.h>
  42. #include <ipmitool/ipmi.h>
  43. //#include <ipmitool/log.h>
  44. #include <ipmitool/ipmi_mc.h>
  45. #include <ipmitool/ipmi_sdr.h>
  46. #include <ipmitool/ipmi_sdradd.h>
  47. #include <ipmitool/ipmi_sensor.h>
  48. #include <ipmitool/ipmi_intf.h>
  49. #include <ipmitool/ipmi_sel.h>
  50. #include <ipmitool/ipmi_entity.h>
  51. #include <ipmitool/ipmi_constants.h>
  52. #include <ipmitool/ipmi_strings.h>
  53. #if HAVE_CONFIG_H
  54. # include <config.h>
  55. #endif
  56. extern int verbose;
  57. static int use_built_in; /* Uses DeviceSDRs instead of SDRR */
  58. static int sdr_max_read_len = 0;
  59. static int sdr_extended = 0;
  60. static long sdriana = 0;
  61. static struct sdr_record_list *sdr_list_head = NULL;
  62. static struct sdr_record_list *sdr_list_tail = NULL;
  63. static struct ipmi_sdr_iterator *sdr_list_itr = NULL;
  64. void printf_sdr_usage();
  65. /* From src/plugins/ipmi_intf.c: */
  66. uint16_t
  67. ipmi_intf_get_max_response_data_size(struct ipmi_intf * intf);
  68. /* ipmi_sdr_get_unit_string - return units for base/modifier
  69. *
  70. * @pct: units are a percentage
  71. * @type: unit type
  72. * @base: base
  73. * @modifier: modifier
  74. *
  75. * returns pointer to static string
  76. */
  77. const char *
  78. ipmi_sdr_get_unit_string(uint8_t pct, uint8_t type, uint8_t base, uint8_t modifier)
  79. {
  80. static char unitstr[16];
  81. /*
  82. * By default, if units are supposed to be percent, we will pre-pend
  83. * the percent string to the textual representation of the units.
  84. */
  85. char *pctstr = pct ? "% " : "";
  86. memset(unitstr, 0, sizeof (unitstr));
  87. switch (type) {
  88. case 2:
  89. snprintf(unitstr, sizeof (unitstr), "%s%s * %s",
  90. pctstr, unit_desc[base], unit_desc[modifier]);
  91. break;
  92. case 1:
  93. snprintf(unitstr, sizeof (unitstr), "%s%s/%s",
  94. pctstr, unit_desc[base], unit_desc[modifier]);
  95. break;
  96. case 0:
  97. default:
  98. /*
  99. * Display the text "percent" only when the Base unit is
  100. * "unspecified" and the caller specified to print percent.
  101. */
  102. if (base == 0 && pct) {
  103. snprintf(unitstr, sizeof(unitstr), "percent");
  104. } else {
  105. snprintf(unitstr, sizeof (unitstr), "%s%s",
  106. pctstr, unit_desc[base]);
  107. }
  108. break;
  109. }
  110. return unitstr;
  111. }
  112. /* sdr_sensor_has_analog_reading - Determine if sensor has an analog reading
  113. *
  114. */
  115. static int
  116. sdr_sensor_has_analog_reading(struct ipmi_intf *intf,
  117. struct sensor_reading *sr)
  118. {
  119. /* Compact sensors can't return analog values so we false */
  120. if (!sr->full) {
  121. return 0;
  122. }
  123. /*
  124. * Per the IPMI Specification:
  125. * Only Full Threshold sensors are identified as providing
  126. * analog readings.
  127. *
  128. * But... HP didn't interpret this as meaning that "Only Threshold
  129. * Sensors" can provide analog readings. So, HP packed analog
  130. * readings into some of their non-Threshold Sensor. There is
  131. * nothing that explictly prohibits this in the spec, so if
  132. * an Analog reading is available in a Non-Threshod sensor and
  133. * there are units specified for identifying the reading then
  134. * we do an analog conversion even though the sensor is
  135. * non-Threshold. To be safe, we provide this extension for
  136. * HP.
  137. *
  138. */
  139. if ( UNITS_ARE_DISCRETE(&sr->full->cmn) ) {
  140. return 0;/* Sensor specified as not having Analog Units */
  141. }
  142. if ( !IS_THRESHOLD_SENSOR(&sr->full->cmn) ) {
  143. /* Non-Threshold Sensors are not defined as having analog */
  144. /* But.. We have one with defined with Analog Units */
  145. if ( (sr->full->cmn.unit.pct | sr->full->cmn.unit.modifier |
  146. sr->full->cmn.unit.type.base |
  147. sr->full->cmn.unit.type.modifier)) {
  148. /* And it does have the necessary units specs */
  149. if ( !(intf->manufacturer_id == IPMI_OEM_HP) ) {
  150. /* But to be safe we only do this for HP */
  151. return 0;
  152. }
  153. } else {
  154. return 0;
  155. }
  156. }
  157. /*
  158. * If sensor has linearization, then we should be able to update the
  159. * reading factors and if we cannot fail the conversion.
  160. */
  161. if (sr->full->linearization >= SDR_SENSOR_L_NONLINEAR &&
  162. sr->full->linearization <= 0x7F) {
  163. if (ipmi_sensor_get_sensor_reading_factors(intf, sr->full, sr->s_reading) < 0){
  164. sr->s_reading_valid = 0;
  165. return 0;
  166. }
  167. }
  168. return 1;
  169. }
  170. /* sdr_convert_sensor_reading - convert raw sensor reading
  171. *
  172. * @sensor: sensor record
  173. * @val: raw sensor reading
  174. *
  175. * returns floating-point sensor reading
  176. */
  177. double
  178. sdr_convert_sensor_reading(struct sdr_record_full_sensor *sensor, uint8_t val)
  179. {
  180. int m, b, k1, k2;
  181. double result;
  182. m = __TO_M(sensor->mtol);
  183. b = __TO_B(sensor->bacc);
  184. k1 = __TO_B_EXP(sensor->bacc);
  185. k2 = __TO_R_EXP(sensor->bacc);
  186. switch (sensor->cmn.unit.analog) {
  187. case 0:
  188. result = (double) (((m * val) +
  189. (b * pow(10, k1))) * pow(10, k2));
  190. break;
  191. case 1:
  192. if (val & 0x80)
  193. val++;
  194. /* Deliberately fall through to case 2. */
  195. case 2:
  196. result = (double) (((m * (int8_t) val) +
  197. (b * pow(10, k1))) * pow(10, k2));
  198. break;
  199. default:
  200. /* Oops! This isn't an analog sensor. */
  201. return 0.0;
  202. }
  203. switch (sensor->linearization & 0x7f) {
  204. case SDR_SENSOR_L_LN:
  205. result = log(result);
  206. break;
  207. case SDR_SENSOR_L_LOG10:
  208. result = log10(result);
  209. break;
  210. case SDR_SENSOR_L_LOG2:
  211. result = (double) (log(result) / log(2.0));
  212. break;
  213. case SDR_SENSOR_L_E:
  214. result = exp(result);
  215. break;
  216. case SDR_SENSOR_L_EXP10:
  217. result = pow(10.0, result);
  218. break;
  219. case SDR_SENSOR_L_EXP2:
  220. result = pow(2.0, result);
  221. break;
  222. case SDR_SENSOR_L_1_X:
  223. result = pow(result, -1.0); /*1/x w/o exception */
  224. break;
  225. case SDR_SENSOR_L_SQR:
  226. result = pow(result, 2.0);
  227. break;
  228. case SDR_SENSOR_L_CUBE:
  229. result = pow(result, 3.0);
  230. break;
  231. case SDR_SENSOR_L_SQRT:
  232. result = sqrt(result);
  233. break;
  234. case SDR_SENSOR_L_CUBERT:
  235. result = cbrt(result);
  236. break;
  237. case SDR_SENSOR_L_LINEAR:
  238. default:
  239. break;
  240. }
  241. return result;
  242. }
  243. /* sdr_convert_sensor_hysterisis - convert raw sensor hysterisis
  244. *
  245. * Even though spec says histerisis should be computed using Mx+B
  246. * formula, B is irrelevant when doing raw comparison
  247. *
  248. * threshold rearm point is computed using threshold +/- hysterisis
  249. * with the full formula however B can't be applied in raw comparisons
  250. *
  251. * @sensor: sensor record
  252. * @val: raw sensor reading
  253. *
  254. * returns floating-point sensor reading
  255. */
  256. double
  257. sdr_convert_sensor_hysterisis(struct sdr_record_full_sensor *sensor, uint8_t val)
  258. {
  259. int m, k2;
  260. double result;
  261. m = __TO_M(sensor->mtol);
  262. k2 = __TO_R_EXP(sensor->bacc);
  263. switch (sensor->cmn.unit.analog) {
  264. case 0:
  265. result = (double) (((m * val)) * pow(10, k2));
  266. break;
  267. case 1:
  268. if (val & 0x80)
  269. val++;
  270. /* Deliberately fall through to case 2. */
  271. case 2:
  272. result = (double) (((m * (int8_t) val) ) * pow(10, k2));
  273. break;
  274. default:
  275. /* Oops! This isn't an analog sensor. */
  276. return 0.0;
  277. }
  278. switch (sensor->linearization & 0x7f) {
  279. case SDR_SENSOR_L_LN:
  280. result = log(result);
  281. break;
  282. case SDR_SENSOR_L_LOG10:
  283. result = log10(result);
  284. break;
  285. case SDR_SENSOR_L_LOG2:
  286. result = (double) (log(result) / log(2.0));
  287. break;
  288. case SDR_SENSOR_L_E:
  289. result = exp(result);
  290. break;
  291. case SDR_SENSOR_L_EXP10:
  292. result = pow(10.0, result);
  293. break;
  294. case SDR_SENSOR_L_EXP2:
  295. result = pow(2.0, result);
  296. break;
  297. case SDR_SENSOR_L_1_X:
  298. result = pow(result, -1.0); /*1/x w/o exception */
  299. break;
  300. case SDR_SENSOR_L_SQR:
  301. result = pow(result, 2.0);
  302. break;
  303. case SDR_SENSOR_L_CUBE:
  304. result = pow(result, 3.0);
  305. break;
  306. case SDR_SENSOR_L_SQRT:
  307. result = sqrt(result);
  308. break;
  309. case SDR_SENSOR_L_CUBERT:
  310. result = cbrt(result);
  311. break;
  312. case SDR_SENSOR_L_LINEAR:
  313. default:
  314. break;
  315. }
  316. return result;
  317. }
  318. /* sdr_convert_sensor_tolerance - convert raw sensor reading
  319. *
  320. * @sensor: sensor record
  321. * @val: raw sensor reading
  322. *
  323. * returns floating-point sensor tolerance(interpreted)
  324. */
  325. double
  326. sdr_convert_sensor_tolerance(struct sdr_record_full_sensor *sensor, uint8_t val)
  327. {
  328. int m, k2;
  329. double result;
  330. m = __TO_M(sensor->mtol);
  331. k2 = __TO_R_EXP(sensor->bacc);
  332. switch (sensor->cmn.unit.analog) {
  333. case 0:
  334. /* as suggested in section 30.4.1 of IPMI 1.5 spec */
  335. result = (double) ((((m * (double)val/2)) ) * pow(10, k2));
  336. break;
  337. case 1:
  338. if (val & 0x80)
  339. val++;
  340. /* Deliberately fall through to case 2. */
  341. case 2:
  342. result = (double) (((m * ((double)((int8_t) val)/2))) * pow(10, k2));
  343. break;
  344. default:
  345. /* Oops! This isn't an analog sensor. */
  346. return 0.0;
  347. }
  348. switch (sensor->linearization & 0x7f) {
  349. // case SDR_SENSOR_L_LN:
  350. // result = log(result);
  351. // break;
  352. // case SDR_SENSOR_L_LOG10:
  353. // result = log10(result);
  354. // break;
  355. // case SDR_SENSOR_L_LOG2:
  356. // result = (double) (log(result) / log(2.0));
  357. // break;
  358. // case SDR_SENSOR_L_E:
  359. // result = exp(result);
  360. // break;
  361. // case SDR_SENSOR_L_EXP10:
  362. // result = pow(10.0, result);
  363. // break;
  364. // case SDR_SENSOR_L_EXP2:
  365. // result = pow(2.0, result);
  366. // break;
  367. // case SDR_SENSOR_L_1_X:
  368. // result = pow(result, -1.0); /*1/x w/o exception */
  369. // break;
  370. // case SDR_SENSOR_L_SQR:
  371. // result = pow(result, 2.0);
  372. // break;
  373. // case SDR_SENSOR_L_CUBE:
  374. // result = pow(result, 3.0);
  375. // break;
  376. // case SDR_SENSOR_L_SQRT:
  377. // result = sqrt(result);
  378. // break;
  379. // case SDR_SENSOR_L_CUBERT:
  380. // result = cbrt(result);
  381. // break;
  382. case SDR_SENSOR_L_LINEAR:
  383. default:
  384. break;
  385. }
  386. return result;
  387. }
  388. /* sdr_convert_sensor_value_to_raw - convert sensor reading back to raw
  389. *
  390. * @sensor: sensor record
  391. * @val: converted sensor reading
  392. *
  393. * returns raw sensor reading
  394. */
  395. uint8_t
  396. sdr_convert_sensor_value_to_raw(struct sdr_record_full_sensor * sensor,
  397. double val)
  398. {
  399. int m, b, k1, k2;
  400. double result;
  401. /* only works for analog sensors */
  402. if (UNITS_ARE_DISCRETE((&sensor->cmn)))
  403. return 0;
  404. m = __TO_M(sensor->mtol);
  405. b = __TO_B(sensor->bacc);
  406. k1 = __TO_B_EXP(sensor->bacc);
  407. k2 = __TO_R_EXP(sensor->bacc);
  408. /* don't divide by zero */
  409. if (m == 0)
  410. return 0;
  411. result = (((val / pow(10, k2)) - (b * pow(10, k1))) / m);
  412. if ((result - (int) result) >= .5)
  413. return (uint8_t) (result+0.5);
  414. else
  415. return (uint8_t) result;
  416. }
  417. /* ipmi_sdr_get_sensor_thresholds - return thresholds for sensor
  418. *
  419. * @intf: ipmi interface
  420. * @sensor: sensor number
  421. * @target: sensor owner ID
  422. * @lun: sensor lun
  423. * @channel: channel number
  424. *
  425. * returns pointer to ipmi response
  426. */
  427. struct ipmi_rs *
  428. ipmi_sdr_get_sensor_thresholds(struct ipmi_intf *intf, uint8_t sensor,
  429. uint8_t target, uint8_t lun, uint8_t channel)
  430. {
  431. struct ipmi_rq req;
  432. struct ipmi_rs *rsp;
  433. uint8_t bridged_request = 0;
  434. uint32_t save_addr;
  435. uint32_t save_channel;
  436. if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
  437. bridged_request = 1;
  438. save_addr = intf->target_addr;
  439. intf->target_addr = target;
  440. save_channel = intf->target_channel;
  441. intf->target_channel = channel;
  442. }
  443. memset(&req, 0, sizeof (req));
  444. req.msg.netfn = IPMI_NETFN_SE;
  445. req.msg.lun = lun;
  446. req.msg.cmd = GET_SENSOR_THRESHOLDS;
  447. req.msg.data = &sensor;
  448. req.msg.data_len = sizeof (sensor);
  449. rsp = intf->sendrecv(intf, &req);
  450. if (bridged_request) {
  451. intf->target_addr = save_addr;
  452. intf->target_channel = save_channel;
  453. }
  454. return rsp;
  455. }
  456. /* ipmi_sdr_get_sensor_hysteresis - return hysteresis for sensor
  457. *
  458. * @intf: ipmi interface
  459. * @sensor: sensor number
  460. * @target: sensor owner ID
  461. * @lun: sensor lun
  462. * @channel: channel number
  463. *
  464. * returns pointer to ipmi response
  465. */
  466. struct ipmi_rs *
  467. ipmi_sdr_get_sensor_hysteresis(struct ipmi_intf *intf, uint8_t sensor,
  468. uint8_t target, uint8_t lun, uint8_t channel)
  469. {
  470. struct ipmi_rq req;
  471. uint8_t rqdata[2];
  472. struct ipmi_rs *rsp;
  473. uint8_t bridged_request = 0;
  474. uint32_t save_addr;
  475. uint32_t save_channel;
  476. if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
  477. bridged_request = 1;
  478. save_addr = intf->target_addr;
  479. intf->target_addr = target;
  480. save_channel = intf->target_channel;
  481. intf->target_channel = channel;
  482. }
  483. rqdata[0] = sensor;
  484. rqdata[1] = 0xff; /* reserved */
  485. memset(&req, 0, sizeof (req));
  486. req.msg.netfn = IPMI_NETFN_SE;
  487. req.msg.lun = lun;
  488. req.msg.cmd = GET_SENSOR_HYSTERESIS;
  489. req.msg.data = rqdata;
  490. req.msg.data_len = 2;
  491. rsp = intf->sendrecv(intf, &req);
  492. if (bridged_request) {
  493. intf->target_addr = save_addr;
  494. intf->target_channel = save_channel;
  495. }
  496. return rsp;
  497. }
  498. /* ipmi_sdr_get_sensor_reading - retrieve a raw sensor reading
  499. *
  500. * @intf: ipmi interface
  501. * @sensor: sensor id
  502. *
  503. * returns ipmi response structure
  504. */
  505. struct ipmi_rs *
  506. ipmi_sdr_get_sensor_reading(struct ipmi_intf *intf, uint8_t sensor)
  507. {
  508. struct ipmi_rq req;
  509. memset(&req, 0, sizeof (req));
  510. req.msg.netfn = IPMI_NETFN_SE;
  511. req.msg.cmd = GET_SENSOR_READING;
  512. req.msg.data = &sensor;
  513. req.msg.data_len = 1;
  514. return intf->sendrecv(intf, &req);
  515. }
  516. /* ipmi_sdr_get_sensor_reading_ipmb - retrieve a raw sensor reading from ipmb
  517. *
  518. * @intf: ipmi interface
  519. * @sensor: sensor id
  520. * @target: IPMB target address
  521. * @lun: sensor lun
  522. * @channel: channel number
  523. *
  524. * returns ipmi response structure
  525. */
  526. struct ipmi_rs *
  527. ipmi_sdr_get_sensor_reading_ipmb(struct ipmi_intf *intf, uint8_t sensor,
  528. uint8_t target, uint8_t lun, uint8_t channel)
  529. {
  530. struct ipmi_rq req;
  531. struct ipmi_rs *rsp;
  532. uint8_t bridged_request = 0;
  533. uint32_t save_addr;
  534. uint32_t save_channel;
  535. if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
  536. printf("Bridge to Sensor "
  537. "Intf my/%#x tgt/%#x:%#x Sdr tgt/%#x:%#x\n",
  538. intf->my_addr, intf->target_addr, intf->target_channel,
  539. target, channel);
  540. bridged_request = 1;
  541. save_addr = intf->target_addr;
  542. intf->target_addr = target;
  543. save_channel = intf->target_channel;
  544. intf->target_channel = channel;
  545. }
  546. memset(&req, 0, sizeof (req));
  547. req.msg.netfn = IPMI_NETFN_SE;
  548. req.msg.lun = lun;
  549. req.msg.cmd = GET_SENSOR_READING;
  550. req.msg.data = &sensor;
  551. req.msg.data_len = 1;
  552. rsp = intf->sendrecv(intf, &req);
  553. if (bridged_request) {
  554. intf->target_addr = save_addr;
  555. intf->target_channel = save_channel;
  556. }
  557. return rsp;
  558. }
  559. /* ipmi_sdr_get_sensor_event_status - retrieve sensor event status
  560. *
  561. * @intf: ipmi interface
  562. * @sensor: sensor id
  563. * @target: sensor owner ID
  564. * @lun: sensor lun
  565. * @channel: channel number
  566. *
  567. * returns ipmi response structure
  568. */
  569. struct ipmi_rs *
  570. ipmi_sdr_get_sensor_event_status(struct ipmi_intf *intf, uint8_t sensor,
  571. uint8_t target, uint8_t lun, uint8_t channel)
  572. {
  573. struct ipmi_rq req;
  574. struct ipmi_rs *rsp;
  575. uint8_t bridged_request = 0;
  576. uint32_t save_addr;
  577. uint32_t save_channel;
  578. if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
  579. bridged_request = 1;
  580. save_addr = intf->target_addr;
  581. intf->target_addr = target;
  582. save_channel = intf->target_channel;
  583. intf->target_channel = channel;
  584. }
  585. memset(&req, 0, sizeof (req));
  586. req.msg.netfn = IPMI_NETFN_SE;
  587. req.msg.lun = lun;
  588. req.msg.cmd = GET_SENSOR_EVENT_STATUS;
  589. req.msg.data = &sensor;
  590. req.msg.data_len = 1;
  591. rsp = intf->sendrecv(intf, &req);
  592. if (bridged_request) {
  593. intf->target_addr = save_addr;
  594. intf->target_channel = save_channel;
  595. }
  596. return rsp;
  597. }
  598. /* ipmi_sdr_get_sensor_event_enable - retrieve sensor event enables
  599. *
  600. * @intf: ipmi interface
  601. * @sensor: sensor id
  602. * @target: sensor owner ID
  603. * @lun: sensor lun
  604. * @channel: channel number
  605. *
  606. * returns ipmi response structure
  607. */
  608. struct ipmi_rs *
  609. ipmi_sdr_get_sensor_event_enable(struct ipmi_intf *intf, uint8_t sensor,
  610. uint8_t target, uint8_t lun, uint8_t channel)
  611. {
  612. struct ipmi_rq req;
  613. struct ipmi_rs *rsp;
  614. uint8_t bridged_request = 0;
  615. uint32_t save_addr;
  616. uint32_t save_channel;
  617. if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
  618. bridged_request = 1;
  619. save_addr = intf->target_addr;
  620. intf->target_addr = target;
  621. save_channel = intf->target_channel;
  622. intf->target_channel = channel;
  623. }
  624. memset(&req, 0, sizeof (req));
  625. req.msg.netfn = IPMI_NETFN_SE;
  626. req.msg.lun = lun;
  627. req.msg.cmd = GET_SENSOR_EVENT_ENABLE;
  628. req.msg.data = &sensor;
  629. req.msg.data_len = 1;
  630. rsp = intf->sendrecv(intf, &req);
  631. if (bridged_request) {
  632. intf->target_addr = save_addr;
  633. intf->target_channel = save_channel;
  634. }
  635. return rsp;
  636. }
  637. /* ipmi_sdr_get_thresh_status - threshold status indicator
  638. *
  639. * @rsp: response from Get Sensor Reading comand
  640. * @validread: validity of the status field argument
  641. * @invalidstr: string to return if status field is not valid
  642. *
  643. * returns
  644. * cr = critical
  645. * nc = non-critical
  646. * nr = non-recoverable
  647. * ok = ok
  648. * ns = not specified
  649. */
  650. const char *
  651. ipmi_sdr_get_thresh_status(struct sensor_reading *sr, const char *invalidstr)
  652. {
  653. uint8_t stat;
  654. if (!sr->s_reading_valid) {
  655. return invalidstr;
  656. }
  657. stat = sr->s_data2;
  658. if (stat & SDR_SENSOR_STAT_LO_NR) {
  659. if (verbose)
  660. return "Lower Non-Recoverable";
  661. else if (sdr_extended)
  662. return "lnr";
  663. else
  664. return "nr";
  665. } else if (stat & SDR_SENSOR_STAT_HI_NR) {
  666. if (verbose)
  667. return "Upper Non-Recoverable";
  668. else if (sdr_extended)
  669. return "unr";
  670. else
  671. return "nr";
  672. } else if (stat & SDR_SENSOR_STAT_LO_CR) {
  673. if (verbose)
  674. return "Lower Critical";
  675. else if (sdr_extended)
  676. return "lcr";
  677. else
  678. return "cr";
  679. } else if (stat & SDR_SENSOR_STAT_HI_CR) {
  680. if (verbose)
  681. return "Upper Critical";
  682. else if (sdr_extended)
  683. return "ucr";
  684. else
  685. return "cr";
  686. } else if (stat & SDR_SENSOR_STAT_LO_NC) {
  687. if (verbose)
  688. return "Lower Non-Critical";
  689. else if (sdr_extended)
  690. return "lnc";
  691. else
  692. return "nc";
  693. } else if (stat & SDR_SENSOR_STAT_HI_NC) {
  694. if (verbose)
  695. return "Upper Non-Critical";
  696. else if (sdr_extended)
  697. return "unc";
  698. else
  699. return "nc";
  700. }
  701. return "ok";
  702. }
  703. /* ipmi_sdr_get_header - retreive SDR record header
  704. *
  705. * @intf: ipmi interface
  706. * @itr: sdr iterator
  707. *
  708. * returns pointer to static sensor retrieval struct
  709. * returns NULL on error
  710. */
  711. static struct sdr_get_rs *
  712. ipmi_sdr_get_header(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
  713. {
  714. struct ipmi_rq req;
  715. struct ipmi_rs *rsp;
  716. struct sdr_get_rq sdr_rq;
  717. static struct sdr_get_rs sdr_rs;
  718. int try = 0;
  719. memset(&sdr_rq, 0, sizeof (sdr_rq));
  720. sdr_rq.reserve_id = itr->reservation;
  721. sdr_rq.id = itr->next;
  722. sdr_rq.offset = 0;
  723. sdr_rq.length = 5; /* only get the header */
  724. memset(&req, 0, sizeof (req));
  725. if (itr->use_built_in == 0) {
  726. req.msg.netfn = IPMI_NETFN_STORAGE;
  727. req.msg.cmd = GET_SDR;
  728. } else {
  729. req.msg.netfn = IPMI_NETFN_SE;
  730. req.msg.cmd = GET_DEVICE_SDR;
  731. }
  732. req.msg.data = (uint8_t *) & sdr_rq;
  733. req.msg.data_len = sizeof (sdr_rq);
  734. for (try = 0; try < 5; try++) {
  735. sdr_rq.reserve_id = itr->reservation;
  736. rsp = intf->sendrecv(intf, &req);
  737. if (rsp == NULL) {
  738. printf( "Get SDR %04x command failed",
  739. itr->next);
  740. continue;
  741. } else if (rsp->ccode == 0xc5) {
  742. /* lost reservation */
  743. printf( "SDR reservation %04x cancelled. "
  744. "Sleeping a bit and retrying...",
  745. itr->reservation);
  746. sleep(rand() & 3);
  747. if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
  748. &(itr->reservation)) < 0) {
  749. printf(
  750. "Unable to renew SDR reservation");
  751. return NULL;
  752. }
  753. } else if (rsp->ccode > 0) {
  754. printf( "Get SDR %04x command failed: %s",
  755. itr->next, val2str(rsp->ccode,
  756. completion_code_vals));
  757. continue;
  758. } else {
  759. break;
  760. }
  761. }
  762. if (try == 5)
  763. return NULL;
  764. if (!rsp)
  765. return NULL;
  766. printf( "SDR record ID : 0x%04x", itr->next);
  767. memcpy(&sdr_rs, rsp->data, sizeof (sdr_rs));
  768. if (sdr_rs.length == 0) {
  769. printf( "SDR record id 0x%04x: invalid length %d",
  770. itr->next, sdr_rs.length);
  771. return NULL;
  772. }
  773. /* achu (chu11 at llnl dot gov): - Some boards are stupid and
  774. * return a record id from the Get SDR Record command
  775. * different than the record id passed in. If we find this
  776. * situation, we cheat and put the original record id back in.
  777. * Otherwise, a later Get SDR Record command will fail with
  778. * completion code CBh = "Requested Sensor, data, or record
  779. * not present". Exception is if 'Record ID' is specified as 0000h.
  780. * For further information see IPMI v2.0 Spec, Section 33.12
  781. */
  782. if ((itr->next != 0x0000) &&
  783. (sdr_rs.id != itr->next)) {
  784. printf( "SDR record id mismatch: 0x%04x", sdr_rs.id);
  785. sdr_rs.id = itr->next;
  786. }
  787. printf( "SDR record type : 0x%02x", sdr_rs.type);
  788. printf( "SDR record next : 0x%04x", sdr_rs.next);
  789. printf( "SDR record bytes: %d", sdr_rs.length);
  790. return &sdr_rs;
  791. }
  792. /* ipmi_sdr_get_next_header - retreive next SDR header
  793. *
  794. * @intf: ipmi interface
  795. * @itr: sdr iterator
  796. *
  797. * returns pointer to sensor retrieval struct
  798. * returns NULL on error
  799. */
  800. struct sdr_get_rs *
  801. ipmi_sdr_get_next_header(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
  802. {
  803. struct sdr_get_rs *header;
  804. if (itr->next == 0xffff)
  805. return NULL;
  806. header = ipmi_sdr_get_header(intf, itr);
  807. if (header == NULL)
  808. return NULL;
  809. itr->next = header->next;
  810. return header;
  811. }
  812. /*
  813. * This macro is used to print nominal, normal and threshold settings,
  814. * but it is not compatible with PRINT_NORMAL/PRINT_THRESH since it does
  815. * not have the sensor.init.thresholds setting qualifier as is done in
  816. * PRINT_THRESH. This means CSV output can be different than non CSV
  817. * output if sensor.init.thresholds is ever zero
  818. */
  819. /* helper macro for printing CSV output for Full SDR Threshold reading */
  820. #define SENSOR_PRINT_CSV(FULLSENS, FLAG, READ) \
  821. if ((FLAG)) { \
  822. if (UNITS_ARE_DISCRETE((&FULLSENS->cmn))) \
  823. printf("0x%02X,", READ); \
  824. else \
  825. printf("%.3f,", sdr_convert_sensor_reading( \
  826. (FULLSENS), READ)); \
  827. } else { \
  828. printf(","); \
  829. }
  830. /* helper macro for printing analog values for Full SDR Threshold readings */
  831. #define SENSOR_PRINT_NORMAL(FULLSENS, NAME, READ) \
  832. if ((FULLSENS)->analog_flag.READ != 0) { \
  833. printf(" %-21s : ", NAME); \
  834. if (UNITS_ARE_DISCRETE((&FULLSENS->cmn))) \
  835. printf("0x%02X\n", \
  836. (FULLSENS)->READ); \
  837. else \
  838. printf("%.3f\n", sdr_convert_sensor_reading( \
  839. (FULLSENS), (FULLSENS)->READ));\
  840. }
  841. /* helper macro for printing Full SDR sensor Thresholds */
  842. #define SENSOR_PRINT_THRESH(FULLSENS, NAME, READ, FLAG) \
  843. if ((FULLSENS)->cmn.sensor.init.thresholds && \
  844. (FULLSENS)->cmn.mask.type.threshold.read.FLAG != 0) { \
  845. printf(" %-21s : ", NAME); \
  846. if (UNITS_ARE_DISCRETE((&FULLSENS->cmn))) \
  847. printf("0x%02X\n", \
  848. (FULLSENS)->threshold.READ); \
  849. else \
  850. printf("%.3f\n", sdr_convert_sensor_reading( \
  851. (FULLSENS), (FULLSENS)->threshold.READ)); \
  852. }
  853. int
  854. ipmi_sdr_print_sensor_event_status(struct ipmi_intf *intf,
  855. uint8_t sensor_num,
  856. uint8_t sensor_type,
  857. uint8_t event_type, int numeric_fmt,
  858. uint8_t target, uint8_t lun, uint8_t channel)
  859. {
  860. struct ipmi_rs *rsp;
  861. int i;
  862. const struct valstr assert_cond_1[] = {
  863. {0x80, "unc+"},
  864. {0x40, "unc-"},
  865. {0x20, "lnr+"},
  866. {0x10, "lnr-"},
  867. {0x08, "lcr+"},
  868. {0x04, "lcr-"},
  869. {0x02, "lnc+"},
  870. {0x01, "lnc-"},
  871. {0x00, NULL},
  872. };
  873. const struct valstr assert_cond_2[] = {
  874. {0x08, "unr+"},
  875. {0x04, "unr-"},
  876. {0x02, "ucr+"},
  877. {0x01, "ucr-"},
  878. {0x00, NULL},
  879. };
  880. rsp = ipmi_sdr_get_sensor_event_status(intf, sensor_num,
  881. target, lun, channel);
  882. if (rsp == NULL) {
  883. printf("Error reading event status for sensor #%02x",
  884. sensor_num);
  885. return -1;
  886. }
  887. if (rsp->ccode > 0) {
  888. printf("Error reading event status for sensor #%02x: %s",
  889. sensor_num, val2str(rsp->ccode, completion_code_vals));
  890. return -1;
  891. }
  892. /* There is an assumption here that data_len >= 1 */
  893. if (IS_READING_UNAVAILABLE(rsp->data[0])) {
  894. printf(" Event Status : Unavailable\n");
  895. return 0;
  896. }
  897. if (IS_SCANNING_DISABLED(rsp->data[0])) {
  898. //printf(" Event Status : Scanning Disabled\n");
  899. //return 0;
  900. }
  901. if (IS_EVENT_MSG_DISABLED(rsp->data[0])) {
  902. printf(" Event Status : Event Messages Disabled\n");
  903. //return 0;
  904. }
  905. switch (numeric_fmt) {
  906. case DISCRETE_SENSOR:
  907. if (rsp->data_len == 2) {
  908. ipmi_sdr_print_discrete_state(intf, "Assertion Events",
  909. sensor_type, event_type,
  910. rsp->data[1], 0);
  911. } else if (rsp->data_len > 2) {
  912. ipmi_sdr_print_discrete_state(intf, "Assertion Events",
  913. sensor_type, event_type,
  914. rsp->data[1],
  915. rsp->data[2]);
  916. }
  917. if (rsp->data_len == 4) {
  918. ipmi_sdr_print_discrete_state(intf, "Deassertion Events",
  919. sensor_type, event_type,
  920. rsp->data[3], 0);
  921. } else if (rsp->data_len > 4) {
  922. ipmi_sdr_print_discrete_state(intf, "Deassertion Events",
  923. sensor_type, event_type,
  924. rsp->data[3],
  925. rsp->data[4]);
  926. }
  927. break;
  928. case ANALOG_SENSOR:
  929. printf(" Assertion Events : ");
  930. for (i = 0; i < 8; i++) {
  931. if (rsp->data[1] & (1 << i))
  932. printf("%s ", val2str(1 << i, assert_cond_1));
  933. }
  934. if (rsp->data_len > 2) {
  935. for (i = 0; i < 4; i++) {
  936. if (rsp->data[2] & (1 << i))
  937. printf("%s ",
  938. val2str(1 << i, assert_cond_2));
  939. }
  940. printf("\n");
  941. if ((rsp->data_len == 4 && rsp->data[3] != 0) ||
  942. (rsp->data_len > 4
  943. && (rsp->data[3] != 0 && rsp->data[4] != 0))) {
  944. printf(" Deassertion Events : ");
  945. for (i = 0; i < 8; i++) {
  946. if (rsp->data[3] & (1 << i))
  947. printf("%s ",
  948. val2str(1 << i,
  949. assert_cond_1));
  950. }
  951. if (rsp->data_len > 4) {
  952. for (i = 0; i < 4; i++) {
  953. if (rsp->data[4] & (1 << i))
  954. printf("%s ",
  955. val2str(1 << i,
  956. assert_cond_2));
  957. }
  958. }
  959. printf("\n");
  960. }
  961. } else {
  962. printf("\n");
  963. }
  964. break;
  965. default:
  966. break;
  967. }
  968. return 0;
  969. }
  970. static int
  971. ipmi_sdr_print_sensor_mask(struct ipmi_intf *intf,
  972. struct sdr_record_mask *mask,
  973. uint8_t sensor_type,
  974. uint8_t event_type, int numeric_fmt)
  975. {
  976. /* iceblink - don't print some event status fields - CVS rev1.53 */
  977. return 0;
  978. switch (numeric_fmt) {
  979. case DISCRETE_SENSOR:
  980. ipmi_sdr_print_discrete_state(intf, "Assert Event Mask", sensor_type,
  981. event_type,
  982. mask->type.discrete.
  983. assert_event & 0xff,
  984. (mask->type.discrete.
  985. assert_event & 0xff00) >> 8);
  986. ipmi_sdr_print_discrete_state(intf, "Deassert Event Mask",
  987. sensor_type, event_type,
  988. mask->type.discrete.
  989. deassert_event & 0xff,
  990. (mask->type.discrete.
  991. deassert_event & 0xff00) >> 8);
  992. break;
  993. case ANALOG_SENSOR:
  994. printf(" Assert Event Mask : ");
  995. if (mask->type.threshold.assert_lnr_high)
  996. printf("lnr+ ");
  997. if (mask->type.threshold.assert_lnr_low)
  998. printf("lnr- ");
  999. if (mask->type.threshold.assert_lcr_high)
  1000. printf("lcr+ ");
  1001. if (mask->type.threshold.assert_lcr_low)
  1002. printf("lcr- ");
  1003. if (mask->type.threshold.assert_lnc_high)
  1004. printf("lnc+ ");
  1005. if (mask->type.threshold.assert_lnc_low)
  1006. printf("lnc- ");
  1007. if (mask->type.threshold.assert_unc_high)
  1008. printf("unc+ ");
  1009. if (mask->type.threshold.assert_unc_low)
  1010. printf("unc- ");
  1011. if (mask->type.threshold.assert_ucr_high)
  1012. printf("ucr+ ");
  1013. if (mask->type.threshold.assert_ucr_low)
  1014. printf("ucr- ");
  1015. if (mask->type.threshold.assert_unr_high)
  1016. printf("unr+ ");
  1017. if (mask->type.threshold.assert_unr_low)
  1018. printf("unr- ");
  1019. printf("\n");
  1020. printf(" Deassert Event Mask : ");
  1021. if (mask->type.threshold.deassert_lnr_high)
  1022. printf("lnr+ ");
  1023. if (mask->type.threshold.deassert_lnr_low)
  1024. printf("lnr- ");
  1025. if (mask->type.threshold.deassert_lcr_high)
  1026. printf("lcr+ ");
  1027. if (mask->type.threshold.deassert_lcr_low)
  1028. printf("lcr- ");
  1029. if (mask->type.threshold.deassert_lnc_high)
  1030. printf("lnc+ ");
  1031. if (mask->type.threshold.deassert_lnc_low)
  1032. printf("lnc- ");
  1033. if (mask->type.threshold.deassert_unc_high)
  1034. printf("unc+ ");
  1035. if (mask->type.threshold.deassert_unc_low)
  1036. printf("unc- ");
  1037. if (mask->type.threshold.deassert_ucr_high)
  1038. printf("ucr+ ");
  1039. if (mask->type.threshold.deassert_ucr_low)
  1040. printf("ucr- ");
  1041. if (mask->type.threshold.deassert_unr_high)
  1042. printf("unr+ ");
  1043. if (mask->type.threshold.deassert_unr_low)
  1044. printf("unr- ");
  1045. printf("\n");
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. return 0;
  1051. }
  1052. int
  1053. ipmi_sdr_print_sensor_event_enable(struct ipmi_intf *intf,
  1054. uint8_t sensor_num,
  1055. uint8_t sensor_type,
  1056. uint8_t event_type, int numeric_fmt,
  1057. uint8_t target, uint8_t lun, uint8_t channel)
  1058. {
  1059. struct ipmi_rs *rsp;
  1060. int i;
  1061. const struct valstr assert_cond_1[] = {
  1062. {0x80, "unc+"},
  1063. {0x40, "unc-"},
  1064. {0x20, "lnr+"},
  1065. {0x10, "lnr-"},
  1066. {0x08, "lcr+"},
  1067. {0x04, "lcr-"},
  1068. {0x02, "lnc+"},
  1069. {0x01, "lnc-"},
  1070. {0x00, NULL},
  1071. };
  1072. const struct valstr assert_cond_2[] = {
  1073. {0x08, "unr+"},
  1074. {0x04, "unr-"},
  1075. {0x02, "ucr+"},
  1076. {0x01, "ucr-"},
  1077. {0x00, NULL},
  1078. };
  1079. rsp = ipmi_sdr_get_sensor_event_enable(intf, sensor_num,
  1080. target, lun, channel);
  1081. if (rsp == NULL) {
  1082. printf( "Error reading event enable for sensor #%02x",
  1083. sensor_num);
  1084. return -1;
  1085. }
  1086. if (rsp->ccode > 0) {
  1087. printf( "Error reading event enable for sensor #%02x: %s",
  1088. sensor_num, val2str(rsp->ccode, completion_code_vals));
  1089. return -1;
  1090. }
  1091. if (IS_SCANNING_DISABLED(rsp->data[0])) {
  1092. //printf(" Event Enable : Scanning Disabled\n");
  1093. //return 0;
  1094. }
  1095. if (IS_EVENT_MSG_DISABLED(rsp->data[0])) {
  1096. printf(" Event Enable : Event Messages Disabled\n");
  1097. //return 0;
  1098. }
  1099. switch (numeric_fmt) {
  1100. case DISCRETE_SENSOR:
  1101. /* discrete */
  1102. if (rsp->data_len == 2) {
  1103. ipmi_sdr_print_discrete_state(intf, "Assertions Enabled",
  1104. sensor_type, event_type,
  1105. rsp->data[1], 0);
  1106. } else if (rsp->data_len > 2) {
  1107. ipmi_sdr_print_discrete_state(intf, "Assertions Enabled",
  1108. sensor_type, event_type,
  1109. rsp->data[1],
  1110. rsp->data[2]);
  1111. }
  1112. if (rsp->data_len == 4) {
  1113. ipmi_sdr_print_discrete_state(intf, "Deassertions Enabled",
  1114. sensor_type, event_type,
  1115. rsp->data[3], 0);
  1116. } else if (rsp->data_len > 4) {
  1117. ipmi_sdr_print_discrete_state(intf, "Deassertions Enabled",
  1118. sensor_type, event_type,
  1119. rsp->data[3],
  1120. rsp->data[4]);
  1121. }
  1122. break;
  1123. case ANALOG_SENSOR:
  1124. /* analog */
  1125. printf(" Assertions Enabled : ");
  1126. for (i = 0; i < 8; i++) {
  1127. if (rsp->data[1] & (1 << i))
  1128. printf("%s ", val2str(1 << i, assert_cond_1));
  1129. }
  1130. if (rsp->data_len > 2) {
  1131. for (i = 0; i < 4; i++) {
  1132. if (rsp->data[2] & (1 << i))
  1133. printf("%s ",
  1134. val2str(1 << i, assert_cond_2));
  1135. }
  1136. printf("\n");
  1137. if ((rsp->data_len == 4 && rsp->data[3] != 0) ||
  1138. (rsp->data_len > 4
  1139. && (rsp->data[3] != 0 || rsp->data[4] != 0))) {
  1140. printf(" Deassertions Enabled : ");
  1141. for (i = 0; i < 8; i++) {
  1142. if (rsp->data[3] & (1 << i))
  1143. printf("%s ",
  1144. val2str(1 << i,
  1145. assert_cond_1));
  1146. }
  1147. if (rsp->data_len > 4) {
  1148. for (i = 0; i < 4; i++) {
  1149. if (rsp->data[4] & (1 << i))
  1150. printf("%s ",
  1151. val2str(1 << i,
  1152. assert_cond_2));
  1153. }
  1154. }
  1155. printf("\n");
  1156. }
  1157. } else {
  1158. printf("\n");
  1159. }
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. return 0;
  1165. }
  1166. /* ipmi_sdr_print_sensor_hysteresis - print hysteresis for Discrete & Analog
  1167. *
  1168. * @sensor: Common Sensor Record SDR pointer
  1169. * @full: Full Sensor Record SDR pointer (if applicable)
  1170. * @hysteresis_value: Actual hysteresis value
  1171. * @hvstr: hysteresis value Identifier String
  1172. *
  1173. * returns void
  1174. */
  1175. void
  1176. ipmi_sdr_print_sensor_hysteresis(struct sdr_record_common_sensor *sensor,
  1177. struct sdr_record_full_sensor *full,
  1178. uint8_t hysteresis_value,
  1179. const char *hvstr)
  1180. {
  1181. /*
  1182. * compact can have pos/neg hysteresis, but they cannot be analog!
  1183. * We use not full in addition to our discrete units check just in
  1184. * case a compact sensor is incorrectly identified as analog.
  1185. */
  1186. if (!full || UNITS_ARE_DISCRETE(sensor)) {
  1187. if ( hysteresis_value == 0x00 || hysteresis_value == 0xff ) {
  1188. printf(" %s : Unspecified\n", hvstr);
  1189. } else {
  1190. printf(" %s : 0x%02X\n", hvstr, hysteresis_value);
  1191. }
  1192. return;
  1193. }
  1194. /* A Full analog sensor */
  1195. double creading = sdr_convert_sensor_hysterisis(full, hysteresis_value);
  1196. if ( hysteresis_value == 0x00 || hysteresis_value == 0xff ||
  1197. creading == 0.0 ) {
  1198. printf(" %s : Unspecified\n", hvstr);
  1199. } else {
  1200. printf(" %s : %.3f\n", hvstr, creading);
  1201. }
  1202. }
  1203. /* print_sensor_min_max - print Discrete & Analog Minimum/Maximum Sensor Range
  1204. *
  1205. * @full: Full Sensor Record SDR pointer
  1206. *
  1207. * returns void
  1208. */
  1209. static void
  1210. print_sensor_min_max(struct sdr_record_full_sensor *full)
  1211. {
  1212. if (!full) { /* No min/max for compact SDR record */
  1213. return;
  1214. }
  1215. double creading = 0.0;
  1216. uint8_t is_analog = !UNITS_ARE_DISCRETE(&full->cmn);
  1217. if (is_analog)
  1218. creading = sdr_convert_sensor_reading(full, full->sensor_min);
  1219. if ((full->cmn.unit.analog == 0 && full->sensor_min == 0x00) ||
  1220. (full->cmn.unit.analog == 1 && full->sensor_min == 0xff) ||
  1221. (full->cmn.unit.analog == 2 && full->sensor_min == 0x80) ||
  1222. (is_analog && (creading == 0.0)))
  1223. printf(" Minimum sensor range : Unspecified\n");
  1224. else {
  1225. if (is_analog)
  1226. printf(" Minimum sensor range : %.3f\n", creading);
  1227. else
  1228. printf(" Minimum sensor range : 0x%02X\n", full->sensor_min);
  1229. }
  1230. if (is_analog)
  1231. creading = sdr_convert_sensor_reading(full, full->sensor_max);
  1232. if ((full->cmn.unit.analog == 0 && full->sensor_max == 0xff) ||
  1233. (full->cmn.unit.analog == 1 && full->sensor_max == 0x00) ||
  1234. (full->cmn.unit.analog == 2 && full->sensor_max == 0x7f) ||
  1235. (is_analog && (creading == 0.0)))
  1236. printf(" Maximum sensor range : Unspecified\n");
  1237. else {
  1238. if (is_analog)
  1239. printf(" Maximum sensor range : %.3f\n", creading);
  1240. else
  1241. printf(" Maximum sensor range : 0x%02X\n", full->sensor_max);
  1242. }
  1243. }
  1244. /* print_csv_discrete - print csv formatted discrete sensor
  1245. *
  1246. * @sensor: common sensor structure
  1247. * @sr: sensor reading
  1248. *
  1249. * returns void
  1250. */
  1251. static void
  1252. print_csv_discrete(struct ipmi_intf *intf,
  1253. struct sdr_record_common_sensor *sensor,
  1254. const struct sensor_reading *sr)
  1255. {
  1256. if (!sr->s_reading_valid || sr->s_reading_unavailable) {
  1257. printf("%02Xh,ns,%d.%d,No Reading",
  1258. sensor->keys.sensor_num,
  1259. sensor->entity.id,
  1260. sensor->entity.instance);
  1261. return;
  1262. }
  1263. if (sr->s_has_analog_value) { /* Sensor has an analog value */
  1264. printf("%s,%s,", sr->s_a_str, sr->s_a_units);
  1265. } else { /* Sensor has a discrete value */
  1266. printf("%02Xh,", sensor->keys.sensor_num);
  1267. }
  1268. printf("ok,%d.%d,",
  1269. sensor->entity.id,
  1270. sensor->entity.instance);
  1271. ipmi_sdr_print_discrete_state_mini(intf, NULL, ", ",
  1272. sensor->sensor.type,
  1273. sensor->event_type,
  1274. sr->s_data2,
  1275. sr->s_data3);
  1276. }
  1277. /* ipmi_sdr_read_sensor_value - read sensor value
  1278. *
  1279. * @intf Interface pointer
  1280. * @sensor Common sensor component pointer
  1281. * @sdr_record_type Type of sdr sensor record
  1282. * @precision decimal precision for analog format conversion
  1283. *
  1284. * returns a pointer to sensor value reading data structure
  1285. */
  1286. struct sensor_reading *
  1287. ipmi_sdr_read_sensor_value(struct ipmi_intf *intf,
  1288. struct sdr_record_common_sensor *sensor,
  1289. uint8_t sdr_record_type, int precision)
  1290. {
  1291. static struct sensor_reading sr;
  1292. if (sensor == NULL)
  1293. return NULL;
  1294. /* Initialize to reading valid value of zero */
  1295. memset(&sr, 0, sizeof(sr));
  1296. switch (sdr_record_type) {
  1297. unsigned int idlen;
  1298. case (SDR_RECORD_TYPE_FULL_SENSOR):
  1299. sr.full = (struct sdr_record_full_sensor *)sensor;
  1300. idlen = sr.full->id_code & 0x1f;
  1301. idlen = idlen < sizeof(sr.s_id) ?
  1302. idlen : sizeof(sr.s_id) - 1;
  1303. memcpy(sr.s_id, sr.full->id_string, idlen);
  1304. break;
  1305. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  1306. sr.compact = (struct sdr_record_compact_sensor *)sensor;
  1307. idlen = sr.compact->id_code & 0x1f;
  1308. idlen = idlen < sizeof(sr.s_id) ?
  1309. idlen : sizeof(sr.s_id) - 1;
  1310. memcpy(sr.s_id, sr.compact->id_string, idlen);
  1311. break;
  1312. default:
  1313. return NULL;
  1314. }
  1315. /*
  1316. * Get current reading via IPMI interface
  1317. */
  1318. struct ipmi_rs *rsp;
  1319. rsp = ipmi_sdr_get_sensor_reading_ipmb(intf,
  1320. sensor->keys.sensor_num,
  1321. sensor->keys.owner_id,
  1322. sensor->keys.lun,
  1323. sensor->keys.channel);
  1324. sr.s_a_val = 0.0; /* init analog value to a floating point 0 */
  1325. sr.s_a_str[0] = '\0'; /* no converted analog value string */
  1326. sr.s_a_units = ""; /* no converted analog units units */
  1327. if (rsp == NULL) {
  1328. printf( "Error reading sensor %s (#%02x)",
  1329. sr.s_id, sensor->keys.sensor_num);
  1330. return &sr;
  1331. }
  1332. if (rsp->ccode) {
  1333. if ( !((sr.full && rsp->ccode == 0xcb) ||
  1334. (sr.compact && rsp->ccode == 0xcd)) ) {
  1335. printf("Error reading sensor %s (#%02x): %s", sr.s_id,
  1336. sensor->keys.sensor_num,
  1337. val2str(rsp->ccode, completion_code_vals));
  1338. }
  1339. return &sr;
  1340. }
  1341. if (rsp->data_len < 2) {
  1342. /*
  1343. * We must be returned both a value (data[0]), and the validity
  1344. * of the value (data[1]), in order to correctly interpret
  1345. * the reading. If we don't have both of these we can't have
  1346. * a valid sensor reading.
  1347. */
  1348. printf( "Error reading sensor %s invalid len %d",
  1349. sr.s_id, rsp->data_len);
  1350. return &sr;
  1351. }
  1352. if (IS_READING_UNAVAILABLE(rsp->data[1]))
  1353. sr.s_reading_unavailable = 1;
  1354. if (IS_SCANNING_DISABLED(rsp->data[1])) {
  1355. sr.s_scanning_disabled = 1;
  1356. printf( "Sensor %s (#%02x) scanning disabled",
  1357. sr.s_id, sensor->keys.sensor_num);
  1358. return &sr;
  1359. }
  1360. if ( !sr.s_reading_unavailable ) {
  1361. sr.s_reading_valid = 1;
  1362. sr.s_reading = rsp->data[0];
  1363. }
  1364. if (rsp->data_len > 2)
  1365. sr.s_data2 = rsp->data[2];
  1366. if (rsp->data_len > 3)
  1367. sr.s_data3 = rsp->data[3];
  1368. if (sdr_sensor_has_analog_reading(intf, &sr)) {
  1369. sr.s_has_analog_value = 1;
  1370. if (sr.s_reading_valid) {
  1371. sr.s_a_val = sdr_convert_sensor_reading(sr.full, sr.s_reading);
  1372. }
  1373. /* determine units string with possible modifiers */
  1374. sr.s_a_units = ipmi_sdr_get_unit_string(sr.full->cmn.unit.pct,
  1375. sr.full->cmn.unit.modifier,
  1376. sr.full->cmn.unit.type.base,
  1377. sr.full->cmn.unit.type.modifier);
  1378. snprintf(sr.s_a_str, sizeof(sr.s_a_str), "%.*f",
  1379. (sr.s_a_val == (int) sr.s_a_val) ? 0 :
  1380. precision, sr.s_a_val);
  1381. }
  1382. return &sr;
  1383. }
  1384. /* ipmi_sdr_print_sensor_fc - print full & compact SDR records
  1385. *
  1386. * @intf: ipmi interface
  1387. * @sensor: common sensor structure
  1388. * @sdr_record_type: type of sdr record, either full or compact
  1389. *
  1390. * returns 0 on success
  1391. * returns -1 on error
  1392. */
  1393. int
  1394. ipmi_sdr_print_sensor_fc(struct ipmi_intf *intf,
  1395. struct sdr_record_common_sensor *sensor,
  1396. uint8_t sdr_record_type)
  1397. {
  1398. char sval[16];
  1399. unsigned int i = 0;
  1400. uint8_t target, lun, channel;
  1401. struct sensor_reading *sr;
  1402. sr = ipmi_sdr_read_sensor_value(intf, sensor, sdr_record_type, 2);
  1403. if (sr == NULL)
  1404. return -1;
  1405. target = sensor->keys.owner_id;
  1406. lun = sensor->keys.lun;
  1407. channel = sensor->keys.channel;
  1408. /*
  1409. * CSV OUTPUT
  1410. */
  1411. if (csv_output) {
  1412. /*
  1413. * print sensor name, reading, unit, state
  1414. */
  1415. printf("%s,", sr->s_id);
  1416. if (!IS_THRESHOLD_SENSOR(sensor)) {
  1417. /* Discrete/Non-Threshold */
  1418. print_csv_discrete(intf, sensor, sr);
  1419. printf("\n");
  1420. }
  1421. else {
  1422. /* Threshold Analog & Discrete*/
  1423. if (sr->s_reading_valid) {
  1424. if (sr->s_has_analog_value) {
  1425. /* Analog/Threshold */
  1426. printf("%.*f,", (sr->s_a_val ==
  1427. (int) sr->s_a_val) ? 0 : 3,
  1428. sr->s_a_val);
  1429. printf("%s,%s", sr->s_a_units,
  1430. ipmi_sdr_get_thresh_status(sr, "ns"));
  1431. } else { /* Discrete/Threshold */
  1432. print_csv_discrete(intf, sensor, sr);
  1433. }
  1434. } else {
  1435. printf(",,ns");
  1436. }
  1437. if (verbose) {
  1438. printf(",%d.%d,%s,%s,",
  1439. sensor->entity.id, sensor->entity.instance,
  1440. val2str(sensor->entity.id, entity_id_vals),
  1441. ipmi_get_sensor_type(intf, sensor->sensor.type));
  1442. if (sr->full) {
  1443. SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.nominal_read,
  1444. sr->full->nominal_read);
  1445. SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.normal_min,
  1446. sr->full->normal_min);
  1447. SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.normal_max,
  1448. sr->full->normal_max);
  1449. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.unr,
  1450. sr->full->threshold.upper.non_recover);
  1451. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.ucr,
  1452. sr->full->threshold.upper.critical);
  1453. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.unc,
  1454. sr->full->threshold.upper.non_critical);
  1455. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lnr,
  1456. sr->full->threshold.lower.non_recover);
  1457. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lcr,
  1458. sr->full->threshold.lower.critical);
  1459. SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lnc,
  1460. sr->full->threshold.lower.non_critical);
  1461. if (UNITS_ARE_DISCRETE(sensor)) {
  1462. printf("0x%02X,0x%02X", sr->full->sensor_min, sr->full->sensor_max);
  1463. }
  1464. else {
  1465. printf("%.3f,%.3f",
  1466. sdr_convert_sensor_reading(sr->full,
  1467. sr->full->sensor_min),
  1468. sdr_convert_sensor_reading(sr->full,
  1469. sr->full->sensor_max));
  1470. }
  1471. } else {
  1472. printf(",,,,,,,,,,");
  1473. }
  1474. }
  1475. printf("\n");
  1476. }
  1477. return 0; /* done */
  1478. }
  1479. /*
  1480. * NORMAL OUTPUT
  1481. */
  1482. if (verbose == 0 && sdr_extended == 0) {
  1483. /*
  1484. * print sensor name, reading, state
  1485. */
  1486. printf("%-16s | ", sr->s_id);
  1487. memset(sval, 0, sizeof (sval));
  1488. if (sr->s_reading_valid) {
  1489. if( sr->s_has_analog_value ) {
  1490. snprintf(sval, sizeof (sval), "%s %s",
  1491. sr->s_a_str,
  1492. sr->s_a_units);
  1493. } else /* Discrete */
  1494. snprintf(sval, sizeof(sval),
  1495. "0x%02x", sr->s_reading);
  1496. }
  1497. else if (sr->s_scanning_disabled)
  1498. snprintf(sval, sizeof (sval), sr->full ? "disabled" : "Not Readable");
  1499. else
  1500. snprintf(sval, sizeof (sval), sr->full ? "no reading" : "Not Readable");
  1501. printf("%s", sval);
  1502. for (i = strlen(sval); i <= sizeof (sval); i++)
  1503. printf(" ");
  1504. printf(" | ");
  1505. if (IS_THRESHOLD_SENSOR(sensor)) {
  1506. printf("%s", ipmi_sdr_get_thresh_status(sr, "ns"));
  1507. }
  1508. else {
  1509. printf("%s", sr->s_reading_valid ? "ok" : "ns");
  1510. }
  1511. printf("\n");
  1512. return 0; /* done */
  1513. } else if (verbose == 0 && sdr_extended == 1) {
  1514. /*
  1515. * print sensor name, number, state, entity, reading
  1516. */
  1517. printf("%-16s | %02Xh | ",
  1518. sr->s_id, sensor->keys.sensor_num);
  1519. if (IS_THRESHOLD_SENSOR(sensor)) {
  1520. /* Threshold Analog & Discrete */
  1521. printf("%-3s | %2d.%1d | ",
  1522. ipmi_sdr_get_thresh_status(sr, "ns"),
  1523. sensor->entity.id, sensor->entity.instance);
  1524. }
  1525. else {
  1526. /* Non Threshold Analog & Discrete */
  1527. printf("%-3s | %2d.%1d | ",
  1528. (sr->s_reading_valid ? "ok" : "ns"),
  1529. sensor->entity.id, sensor->entity.instance);
  1530. }
  1531. memset(sval, 0, sizeof (sval));
  1532. if (sr->s_reading_valid) {
  1533. if (IS_THRESHOLD_SENSOR(sensor) &&
  1534. sr->s_has_analog_value ) {
  1535. /* Threshold Analog */
  1536. snprintf(sval, sizeof (sval), "%s %s",
  1537. sr->s_a_str,
  1538. sr->s_a_units);
  1539. } else {
  1540. /* Analog & Discrete & Threshold/Discrete */
  1541. char *header = NULL;
  1542. if (sr->s_has_analog_value) { /* Sensor has an analog value */
  1543. printf("%s %s", sr->s_a_str, sr->s_a_units);
  1544. header = ", ";
  1545. }
  1546. ipmi_sdr_print_discrete_state_mini(intf, header, ", ",
  1547. sensor->sensor.type,
  1548. sensor->event_type,
  1549. sr->s_data2,
  1550. sr->s_data3);
  1551. }
  1552. }
  1553. else if (sr->s_scanning_disabled)
  1554. snprintf(sval, sizeof (sval), "Disabled");
  1555. else
  1556. snprintf(sval, sizeof (sval), "No Reading");
  1557. printf("%s\n", sval);
  1558. return 0; /* done */
  1559. }
  1560. /*
  1561. * VERBOSE OUTPUT
  1562. */
  1563. printf("Sensor ID : %s (0x%x)\n",
  1564. sr->s_id, sensor->keys.sensor_num);
  1565. printf(" Entity ID : %d.%d (%s)\n",
  1566. sensor->entity.id, sensor->entity.instance,
  1567. val2str(sensor->entity.id, entity_id_vals));
  1568. if (!IS_THRESHOLD_SENSOR(sensor)) {
  1569. /* Discrete */
  1570. printf(" Sensor Type (Discrete): %s (0x%02x)\n",
  1571. ipmi_get_sensor_type(intf, sensor->sensor.type),
  1572. sensor->sensor.type);
  1573. printf( " Event Type Code : 0x%02x",
  1574. sensor->event_type);
  1575. printf(" Sensor Reading : ");
  1576. if (sr->s_reading_valid) {
  1577. if (sr->s_has_analog_value) { /* Sensor has an analog value */
  1578. printf("%s %s\n", sr->s_a_str, sr->s_a_units);
  1579. } else {
  1580. printf("%xh\n", sr->s_reading);
  1581. }
  1582. }
  1583. else if (sr->s_scanning_disabled)
  1584. printf("Disabled\n");
  1585. else {
  1586. /* Used to be 'Not Reading' */
  1587. printf("No Reading\n");
  1588. }
  1589. printf(" Event Message Control : ");
  1590. switch (sensor->sensor.capabilities.event_msg) {
  1591. case 0:
  1592. printf("Per-threshold\n");
  1593. break;
  1594. case 1:
  1595. printf("Entire Sensor Only\n");
  1596. break;
  1597. case 2:
  1598. printf("Global Disable Only\n");
  1599. break;
  1600. case 3:
  1601. printf("No Events From Sensor\n");
  1602. break;
  1603. }
  1604. ipmi_sdr_print_discrete_state(intf, "States Asserted",
  1605. sensor->sensor.type,
  1606. sensor->event_type,
  1607. sr->s_data2,
  1608. sr->s_data3);
  1609. ipmi_sdr_print_sensor_mask(intf, &sensor->mask, sensor->sensor.type,
  1610. sensor->event_type, DISCRETE_SENSOR);
  1611. ipmi_sdr_print_sensor_event_status(intf,
  1612. sensor->keys.sensor_num,
  1613. sensor->sensor.type,
  1614. sensor->event_type,
  1615. DISCRETE_SENSOR,
  1616. target,
  1617. lun, channel);
  1618. ipmi_sdr_print_sensor_event_enable(intf,
  1619. sensor->keys.sensor_num,
  1620. sensor->sensor.type,
  1621. sensor->event_type,
  1622. DISCRETE_SENSOR,
  1623. target,
  1624. lun, channel);
  1625. printf(" OEM : %X\n",
  1626. sr->full ? sr->full->oem : sr->compact->oem);
  1627. printf("\n");
  1628. return 0; /* done */
  1629. }
  1630. printf(" Sensor Type (Threshold) : %s (0x%02x)\n",
  1631. ipmi_get_sensor_type(intf, sensor->sensor.type),
  1632. sensor->sensor.type);
  1633. printf(" Sensor Reading : ");
  1634. if (sr->s_reading_valid) {
  1635. if (sr->full) {
  1636. uint16_t raw_tol = __TO_TOL(sr->full->mtol);
  1637. if (UNITS_ARE_DISCRETE(sensor)) {
  1638. printf("0x%02X (+/- 0x%02X) %s\n",
  1639. sr->s_reading, raw_tol, sr->s_a_units);
  1640. }
  1641. else {
  1642. double tol = sdr_convert_sensor_tolerance(sr->full, raw_tol);
  1643. printf("%.*f (+/- %.*f) %s\n",
  1644. (sr->s_a_val == (int) sr->s_a_val) ? 0 : 3,
  1645. sr->s_a_val, (tol == (int) tol) ? 0 :
  1646. 3, tol, sr->s_a_units);
  1647. }
  1648. } else {
  1649. printf("0x%02X %s\n", sr->s_reading, sr->s_a_units);
  1650. }
  1651. } else if (sr->s_scanning_disabled)
  1652. printf("Disabled\n");
  1653. else
  1654. printf("No Reading\n");
  1655. printf(" Status : %s\n",
  1656. ipmi_sdr_get_thresh_status(sr, "Not Available"));
  1657. if(sr->full) {
  1658. SENSOR_PRINT_NORMAL(sr->full, "Nominal Reading", nominal_read);
  1659. SENSOR_PRINT_NORMAL(sr->full, "Normal Minimum", normal_min);
  1660. SENSOR_PRINT_NORMAL(sr->full, "Normal Maximum", normal_max);
  1661. SENSOR_PRINT_THRESH(sr->full, "Upper non-recoverable", upper.non_recover, unr);
  1662. SENSOR_PRINT_THRESH(sr->full, "Upper critical", upper.critical, ucr);
  1663. SENSOR_PRINT_THRESH(sr->full, "Upper non-critical", upper.non_critical, unc);
  1664. SENSOR_PRINT_THRESH(sr->full, "Lower non-recoverable", lower.non_recover, lnr);
  1665. SENSOR_PRINT_THRESH(sr->full, "Lower critical", lower.critical, lcr);
  1666. SENSOR_PRINT_THRESH(sr->full, "Lower non-critical", lower.non_critical, lnc);
  1667. }
  1668. ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
  1669. sr->full ? sr->full->threshold.hysteresis.positive :
  1670. sr->compact->threshold.hysteresis.positive, "Positive Hysteresis");
  1671. ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
  1672. sr->full ? sr->full->threshold.hysteresis.negative :
  1673. sr->compact->threshold.hysteresis.negative, "Negative Hysteresis");
  1674. print_sensor_min_max(sr->full);
  1675. printf(" Event Message Control : ");
  1676. switch (sensor->sensor.capabilities.event_msg) {
  1677. case 0:
  1678. printf("Per-threshold\n");
  1679. break;
  1680. case 1:
  1681. printf("Entire Sensor Only\n");
  1682. break;
  1683. case 2:
  1684. printf("Global Disable Only\n");
  1685. break;
  1686. case 3:
  1687. printf("No Events From Sensor\n");
  1688. break;
  1689. }
  1690. printf(" Readable Thresholds : ");
  1691. switch (sensor->sensor.capabilities.threshold) {
  1692. case 0:
  1693. printf("No Thresholds\n");
  1694. break;
  1695. case 1: /* readable according to mask */
  1696. case 2: /* readable and settable according to mask */
  1697. if (sensor->mask.type.threshold.read.lnr)
  1698. printf("lnr ");
  1699. if (sensor->mask.type.threshold.read.lcr)
  1700. printf("lcr ");
  1701. if (sensor->mask.type.threshold.read.lnc)
  1702. printf("lnc ");
  1703. if (sensor->mask.type.threshold.read.unc)
  1704. printf("unc ");
  1705. if (sensor->mask.type.threshold.read.ucr)
  1706. printf("ucr ");
  1707. if (sensor->mask.type.threshold.read.unr)
  1708. printf("unr ");
  1709. printf("\n");
  1710. break;
  1711. case 3:
  1712. printf("Thresholds Fixed\n");
  1713. break;
  1714. }
  1715. printf(" Settable Thresholds : ");
  1716. switch (sensor->sensor.capabilities.threshold) {
  1717. case 0:
  1718. printf("No Thresholds\n");
  1719. break;
  1720. case 1: /* readable according to mask */
  1721. case 2: /* readable and settable according to mask */
  1722. if (sensor->mask.type.threshold.set.lnr)
  1723. printf("lnr ");
  1724. if (sensor->mask.type.threshold.set.lcr)
  1725. printf("lcr ");
  1726. if (sensor->mask.type.threshold.set.lnc)
  1727. printf("lnc ");
  1728. if (sensor->mask.type.threshold.set.unc)
  1729. printf("unc ");
  1730. if (sensor->mask.type.threshold.set.ucr)
  1731. printf("ucr ");
  1732. if (sensor->mask.type.threshold.set.unr)
  1733. printf("unr ");
  1734. printf("\n");
  1735. break;
  1736. case 3:
  1737. printf("Thresholds Fixed\n");
  1738. break;
  1739. }
  1740. if (sensor->mask.type.threshold.status_lnr ||
  1741. sensor->mask.type.threshold.status_lcr ||
  1742. sensor->mask.type.threshold.status_lnc ||
  1743. sensor->mask.type.threshold.status_unc ||
  1744. sensor->mask.type.threshold.status_ucr ||
  1745. sensor->mask.type.threshold.status_unr) {
  1746. printf(" Threshold Read Mask : ");
  1747. if (sensor->mask.type.threshold.status_lnr)
  1748. printf("lnr ");
  1749. if (sensor->mask.type.threshold.status_lcr)
  1750. printf("lcr ");
  1751. if (sensor->mask.type.threshold.status_lnc)
  1752. printf("lnc ");
  1753. if (sensor->mask.type.threshold.status_unc)
  1754. printf("unc ");
  1755. if (sensor->mask.type.threshold.status_ucr)
  1756. printf("ucr ");
  1757. if (sensor->mask.type.threshold.status_unr)
  1758. printf("unr ");
  1759. printf("\n");
  1760. }
  1761. ipmi_sdr_print_sensor_mask(intf, &sensor->mask,
  1762. sensor->sensor.type,
  1763. sensor->event_type, ANALOG_SENSOR);
  1764. ipmi_sdr_print_sensor_event_status(intf,
  1765. sensor->keys.sensor_num,
  1766. sensor->sensor.type,
  1767. sensor->event_type, ANALOG_SENSOR,
  1768. target,
  1769. lun, channel);
  1770. ipmi_sdr_print_sensor_event_enable(intf,
  1771. sensor->keys.sensor_num,
  1772. sensor->sensor.type,
  1773. sensor->event_type, ANALOG_SENSOR,
  1774. target,
  1775. lun, channel);
  1776. printf("\n");
  1777. return 0;
  1778. }
  1779. static inline int
  1780. get_offset(uint8_t x)
  1781. {
  1782. int i;
  1783. for (i = 0; i < 8; i++)
  1784. if (x >> i == 1)
  1785. return i;
  1786. return 0;
  1787. }
  1788. /* ipmi_sdr_print_discrete_state_mini - print list of asserted states
  1789. * for a discrete sensor
  1790. *
  1791. * @header : header string if necessary
  1792. * @separator : field separator string
  1793. * @sensor_type : sensor type code
  1794. * @event_type : event type code
  1795. * @state : mask of asserted states
  1796. *
  1797. * no meaningful return value
  1798. */
  1799. void
  1800. ipmi_sdr_print_discrete_state_mini(struct ipmi_intf *intf,
  1801. const char *header, const char *separator,
  1802. uint8_t sensor_type, uint8_t event_type,
  1803. uint8_t state1, uint8_t state2)
  1804. {
  1805. const struct ipmi_event_sensor_types *evt;
  1806. int pre = 0, c = 0;
  1807. if (state1 == 0 && (state2 & 0x7f) == 0)
  1808. return;
  1809. if (header)
  1810. printf("%s", header);
  1811. for (evt = ipmi_get_first_event_sensor_type(intf, sensor_type, event_type);
  1812. evt != NULL; evt = ipmi_get_next_event_sensor_type(evt)) {
  1813. if (evt->data != 0xFF) {
  1814. continue;
  1815. }
  1816. if (evt->offset > 7) {
  1817. if ((1 << (evt->offset - 8)) & (state2 & 0x7f)) {
  1818. if (pre++ != 0) {
  1819. printf("%s", separator);
  1820. }
  1821. if (evt->desc) {
  1822. printf("%s", evt->desc);
  1823. }
  1824. }
  1825. } else {
  1826. if ((1 << evt->offset) & state1) {
  1827. if (pre++ != 0) {
  1828. printf("%s", separator);
  1829. }
  1830. if (evt->desc) {
  1831. printf("%s", evt->desc);
  1832. }
  1833. }
  1834. }
  1835. c++;
  1836. }
  1837. }
  1838. /* ipmi_sdr_print_discrete_state - print list of asserted states
  1839. * for a discrete sensor
  1840. *
  1841. * @desc : description for this line
  1842. * @sensor_type : sensor type code
  1843. * @event_type : event type code
  1844. * @state : mask of asserted states
  1845. *
  1846. * no meaningful return value
  1847. */
  1848. void
  1849. ipmi_sdr_print_discrete_state(struct ipmi_intf *intf, const char *desc,
  1850. uint8_t sensor_type, uint8_t event_type,
  1851. uint8_t state1, uint8_t state2)
  1852. {
  1853. const struct ipmi_event_sensor_types *evt;
  1854. int pre = 0, c = 0;
  1855. if (state1 == 0 && (state2 & 0x7f) == 0)
  1856. return;
  1857. for (evt = ipmi_get_first_event_sensor_type(intf, sensor_type, event_type);
  1858. evt != NULL; evt = ipmi_get_next_event_sensor_type(evt)) {
  1859. if (evt->data != 0xFF) {
  1860. continue;
  1861. }
  1862. if (pre == 0) {
  1863. printf(" %-21s : %s\n", desc, ipmi_get_sensor_type(intf, sensor_type));
  1864. pre = 1;
  1865. }
  1866. if (evt->offset > 7) {
  1867. if ((1 << (evt->offset - 8)) & (state2 & 0x7f)) {
  1868. if (evt->desc) {
  1869. printf(" "
  1870. "[%s]\n",
  1871. evt->desc);
  1872. } else {
  1873. printf(" "
  1874. "[no description]\n");
  1875. }
  1876. }
  1877. } else {
  1878. if ((1 << evt->offset) & state1) {
  1879. if (evt->desc) {
  1880. printf(" "
  1881. "[%s]\n",
  1882. evt->desc);
  1883. } else {
  1884. printf(" "
  1885. "[no description]\n");
  1886. }
  1887. }
  1888. }
  1889. c++;
  1890. }
  1891. }
  1892. /* ipmi_sdr_print_sensor_eventonly - print SDR event only record
  1893. *
  1894. * @intf: ipmi interface
  1895. * @sensor: event only sdr record
  1896. *
  1897. * returns 0 on success
  1898. * returns -1 on error
  1899. */
  1900. int
  1901. ipmi_sdr_print_sensor_eventonly(struct ipmi_intf *intf,
  1902. struct sdr_record_eventonly_sensor *sensor)
  1903. {
  1904. char desc[17];
  1905. if (sensor == NULL)
  1906. return -1;
  1907. memset(desc, 0, sizeof (desc));
  1908. snprintf(desc, (sensor->id_code & 0x1f) + 1, "%s", sensor->id_string);
  1909. if (verbose) {
  1910. printf("Sensor ID : %s (0x%x)\n",
  1911. sensor->id_code ? desc : "", sensor->keys.sensor_num);
  1912. printf("Entity ID : %d.%d (%s)\n",
  1913. sensor->entity.id, sensor->entity.instance,
  1914. val2str(sensor->entity.id, entity_id_vals));
  1915. printf("Sensor Type : %s (0x%02x)\n",
  1916. ipmi_get_sensor_type(intf, sensor->sensor_type),
  1917. sensor->sensor_type);
  1918. printf("Event Type Code : 0x%02x",
  1919. sensor->event_type);
  1920. printf("\n");
  1921. } else {
  1922. if (csv_output)
  1923. printf("%s,%02Xh,ns,%d.%d,Event-Only\n",
  1924. sensor->id_code ? desc : "",
  1925. sensor->keys.sensor_num,
  1926. sensor->entity.id, sensor->entity.instance);
  1927. else if (sdr_extended)
  1928. printf("%-16s | %02Xh | ns | %2d.%1d | Event-Only\n",
  1929. sensor->id_code ? desc : "",
  1930. sensor->keys.sensor_num,
  1931. sensor->entity.id, sensor->entity.instance);
  1932. else
  1933. printf("%-16s | Event-Only | ns\n",
  1934. sensor->id_code ? desc : "");
  1935. }
  1936. return 0;
  1937. }
  1938. /* ipmi_sdr_print_sensor_mc_locator - print SDR MC locator record
  1939. *
  1940. * @intf: ipmi interface
  1941. * @mc: mc locator sdr record
  1942. *
  1943. * returns 0 on success
  1944. * returns -1 on error
  1945. */
  1946. int
  1947. ipmi_sdr_print_sensor_mc_locator(struct ipmi_intf *intf,
  1948. struct sdr_record_mc_locator *mc)
  1949. {
  1950. char desc[17];
  1951. if (mc == NULL)
  1952. return -1;
  1953. memset(desc, 0, sizeof (desc));
  1954. snprintf(desc, (mc->id_code & 0x1f) + 1, "%s", mc->id_string);
  1955. if (verbose == 0) {
  1956. if (csv_output)
  1957. printf("%s,00h,ok,%d.%d\n",
  1958. mc->id_code ? desc : "",
  1959. mc->entity.id, mc->entity.instance);
  1960. else if (sdr_extended) {
  1961. printf("%-16s | 00h | ok | %2d.%1d | ",
  1962. mc->id_code ? desc : "",
  1963. mc->entity.id, mc->entity.instance);
  1964. printf("%s MC @ %02Xh\n",
  1965. (mc->
  1966. pwr_state_notif & 0x1) ? "Static" : "Dynamic",
  1967. mc->dev_slave_addr);
  1968. } else {
  1969. printf("%-16s | %s MC @ %02Xh %s | ok\n",
  1970. mc->id_code ? desc : "",
  1971. (mc->
  1972. pwr_state_notif & 0x1) ? "Static" : "Dynamic",
  1973. mc->dev_slave_addr,
  1974. (mc->pwr_state_notif & 0x1) ? " " : "");
  1975. }
  1976. return 0; /* done */
  1977. }
  1978. printf("Device ID : %s\n", mc->id_string);
  1979. printf("Entity ID : %d.%d (%s)\n",
  1980. mc->entity.id, mc->entity.instance,
  1981. val2str(mc->entity.id, entity_id_vals));
  1982. printf("Device Slave Address : %02Xh\n", mc->dev_slave_addr);
  1983. printf("Channel Number : %01Xh\n", mc->channel_num);
  1984. printf("ACPI System P/S Notif : %sRequired\n",
  1985. (mc->pwr_state_notif & 0x4) ? "" : "Not ");
  1986. printf("ACPI Device P/S Notif : %sRequired\n",
  1987. (mc->pwr_state_notif & 0x2) ? "" : "Not ");
  1988. printf("Controller Presence : %s\n",
  1989. (mc->pwr_state_notif & 0x1) ? "Static" : "Dynamic");
  1990. printf("Logs Init Agent Errors : %s\n",
  1991. (mc->global_init & 0x8) ? "Yes" : "No");
  1992. printf("Event Message Gen : ");
  1993. if (!(mc->global_init & 0x3))
  1994. printf("Enable\n");
  1995. else if ((mc->global_init & 0x3) == 0x1)
  1996. printf("Disable\n");
  1997. else if ((mc->global_init & 0x3) == 0x2)
  1998. printf("Do Not Init Controller\n");
  1999. else
  2000. printf("Reserved\n");
  2001. printf("Device Capabilities\n");
  2002. printf(" Chassis Device : %s\n",
  2003. (mc->dev_support & 0x80) ? "Yes" : "No");
  2004. printf(" Bridge : %s\n",
  2005. (mc->dev_support & 0x40) ? "Yes" : "No");
  2006. printf(" IPMB Event Generator : %s\n",
  2007. (mc->dev_support & 0x20) ? "Yes" : "No");
  2008. printf(" IPMB Event Receiver : %s\n",
  2009. (mc->dev_support & 0x10) ? "Yes" : "No");
  2010. printf(" FRU Inventory Device : %s\n",
  2011. (mc->dev_support & 0x08) ? "Yes" : "No");
  2012. printf(" SEL Device : %s\n",
  2013. (mc->dev_support & 0x04) ? "Yes" : "No");
  2014. printf(" SDR Repository : %s\n",
  2015. (mc->dev_support & 0x02) ? "Yes" : "No");
  2016. printf(" Sensor Device : %s\n",
  2017. (mc->dev_support & 0x01) ? "Yes" : "No");
  2018. printf("\n");
  2019. return 0;
  2020. }
  2021. /* ipmi_sdr_print_sensor_generic_locator - print generic device locator record
  2022. *
  2023. * @intf: ipmi interface
  2024. * @gen: generic device locator sdr record
  2025. *
  2026. * returns 0 on success
  2027. * returns -1 on error
  2028. */
  2029. int
  2030. ipmi_sdr_print_sensor_generic_locator(struct ipmi_intf *intf,
  2031. struct sdr_record_generic_locator *dev)
  2032. {
  2033. char desc[17];
  2034. memset(desc, 0, sizeof (desc));
  2035. snprintf(desc, (dev->id_code & 0x1f) + 1, "%s", dev->id_string);
  2036. if (!verbose) {
  2037. if (csv_output)
  2038. printf("%s,00h,ns,%d.%d\n",
  2039. dev->id_code ? desc : "",
  2040. dev->entity.id, dev->entity.instance);
  2041. else if (sdr_extended)
  2042. printf
  2043. ("%-16s | 00h | ns | %2d.%1d | Generic Device @%02Xh:%02Xh.%1d\n",
  2044. dev->id_code ? desc : "", dev->entity.id,
  2045. dev->entity.instance, dev->dev_access_addr,
  2046. dev->dev_slave_addr, dev->oem);
  2047. else
  2048. printf("%-16s | Generic @%02X:%02X.%-2d | ok\n",
  2049. dev->id_code ? desc : "",
  2050. dev->dev_access_addr,
  2051. dev->dev_slave_addr, dev->oem);
  2052. return 0;
  2053. }
  2054. printf("Device ID : %s\n", dev->id_string);
  2055. printf("Entity ID : %d.%d (%s)\n",
  2056. dev->entity.id, dev->entity.instance,
  2057. val2str(dev->entity.id, entity_id_vals));
  2058. printf("Device Access Address : %02Xh\n", dev->dev_access_addr);
  2059. printf("Device Slave Address : %02Xh\n", dev->dev_slave_addr);
  2060. printf("Address Span : %02Xh\n", dev->addr_span);
  2061. printf("Channel Number : %01Xh\n", dev->channel_num);
  2062. printf("LUN.Bus : %01Xh.%01Xh\n", dev->lun, dev->bus);
  2063. printf("Device Type.Modifier : %01Xh.%01Xh (%s)\n",
  2064. dev->dev_type, dev->dev_type_modifier,
  2065. val2str(dev->dev_type << 8 | dev->dev_type_modifier,
  2066. entity_device_type_vals));
  2067. printf("OEM : %02Xh\n", dev->oem);
  2068. printf("\n");
  2069. return 0;
  2070. }
  2071. /* ipmi_sdr_print_sensor_fru_locator - print FRU locator record
  2072. *
  2073. * @intf: ipmi interface
  2074. * @fru: fru locator sdr record
  2075. *
  2076. * returns 0 on success
  2077. * returns -1 on error
  2078. */
  2079. int
  2080. ipmi_sdr_print_sensor_fru_locator(struct ipmi_intf *intf,
  2081. struct sdr_record_fru_locator *fru)
  2082. {
  2083. char desc[17];
  2084. memset(desc, 0, sizeof (desc));
  2085. snprintf(desc, (fru->id_code & 0x1f) + 1, "%s", fru->id_string);
  2086. if (!verbose) {
  2087. if (csv_output)
  2088. printf("%s,00h,ns,%d.%d\n",
  2089. fru->id_code ? desc : "",
  2090. fru->entity.id, fru->entity.instance);
  2091. else if (sdr_extended)
  2092. printf("%-16s | 00h | ns | %2d.%1d | %s FRU @%02Xh\n",
  2093. fru->id_code ? desc : "",
  2094. fru->entity.id, fru->entity.instance,
  2095. (fru->logical) ? "Logical" : "Physical",
  2096. fru->device_id);
  2097. else
  2098. printf("%-16s | %s FRU @%02Xh %02x.%x | ok\n",
  2099. fru->id_code ? desc : "",
  2100. (fru->logical) ? "Log" : "Phy",
  2101. fru->device_id,
  2102. fru->entity.id, fru->entity.instance);
  2103. return 0;
  2104. }
  2105. printf("Device ID : %s\n", fru->id_string);
  2106. printf("Entity ID : %d.%d (%s)\n",
  2107. fru->entity.id, fru->entity.instance,
  2108. val2str(fru->entity.id, entity_id_vals));
  2109. printf("Device Access Address : %02Xh\n", fru->dev_slave_addr);
  2110. printf("%s: %02Xh\n",
  2111. fru->logical ? "Logical FRU Device " :
  2112. "Slave Address ", fru->device_id);
  2113. printf("Channel Number : %01Xh\n", fru->channel_num);
  2114. printf("LUN.Bus : %01Xh.%01Xh\n", fru->lun, fru->bus);
  2115. printf("Device Type.Modifier : %01Xh.%01Xh (%s)\n",
  2116. fru->dev_type, fru->dev_type_modifier,
  2117. val2str(fru->dev_type << 8 | fru->dev_type_modifier,
  2118. entity_device_type_vals));
  2119. printf("OEM : %02Xh\n", fru->oem);
  2120. printf("\n");
  2121. return 0;
  2122. }
  2123. /* ipmi_sdr_print_sensor_entity_assoc - print SDR entity association record
  2124. *
  2125. * @intf: ipmi interface
  2126. * @mc: entity association sdr record
  2127. *
  2128. * returns 0 on success
  2129. * returns -1 on error
  2130. */
  2131. int
  2132. ipmi_sdr_print_sensor_entity_assoc(struct ipmi_intf *intf,
  2133. struct sdr_record_entity_assoc *assoc)
  2134. {
  2135. return 0;
  2136. }
  2137. /* ipmi_sdr_print_sensor_oem_intel - print Intel OEM sensors
  2138. *
  2139. * @intf: ipmi interface
  2140. * @oem: oem sdr record
  2141. *
  2142. * returns 0 on success
  2143. * returns -1 on error
  2144. */
  2145. static int
  2146. ipmi_sdr_print_sensor_oem_intel(struct ipmi_intf *intf,
  2147. struct sdr_record_oem *oem)
  2148. {
  2149. switch (oem->data[3]) { /* record sub-type */
  2150. case 0x02: /* Power Unit Map */
  2151. if (verbose) {
  2152. printf
  2153. ("Sensor ID : Power Unit Redundancy (0x%x)\n",
  2154. oem->data[4]);
  2155. printf
  2156. ("Sensor Type : Intel OEM - Power Unit Map\n");
  2157. printf("Redundant Supplies : %d", oem->data[6]);
  2158. if (oem->data[5])
  2159. printf(" (flags %xh)", oem->data[5]);
  2160. printf("\n");
  2161. }
  2162. switch (oem->data_len) {
  2163. case 7: /* SR1300, non-redundant */
  2164. if (verbose)
  2165. printf("Power Redundancy : No\n");
  2166. else if (csv_output)
  2167. printf("Power Redundancy,Not Available,nr\n");
  2168. else
  2169. printf
  2170. ("Power Redundancy | Not Available | nr\n");
  2171. break;
  2172. case 8: /* SR2300, redundant, PS1 & PS2 present */
  2173. if (verbose) {
  2174. printf("Power Redundancy : No\n");
  2175. printf("Power Supply 2 Sensor : %x\n",
  2176. oem->data[8]);
  2177. } else if (csv_output) {
  2178. printf("Power Redundancy,PS@%02xh,nr\n",
  2179. oem->data[8]);
  2180. } else {
  2181. printf
  2182. ("Power Redundancy | PS@%02xh | nr\n",
  2183. oem->data[8]);
  2184. }
  2185. break;
  2186. case 9: /* SR2300, non-redundant, PSx present */
  2187. if (verbose) {
  2188. printf("Power Redundancy : Yes\n");
  2189. printf("Power Supply Sensor : %x\n",
  2190. oem->data[7]);
  2191. printf("Power Supply Sensor : %x\n",
  2192. oem->data[8]);
  2193. } else if (csv_output) {
  2194. printf
  2195. ("Power Redundancy,PS@%02xh + PS@%02xh,ok\n",
  2196. oem->data[7], oem->data[8]);
  2197. } else {
  2198. printf
  2199. ("Power Redundancy | PS@%02xh + PS@%02xh | ok\n",
  2200. oem->data[7], oem->data[8]);
  2201. }
  2202. break;
  2203. }
  2204. if (verbose)
  2205. printf("\n");
  2206. break;
  2207. case 0x03: /* Fan Speed Control */
  2208. break;
  2209. case 0x06: /* System Information */
  2210. break;
  2211. case 0x07: /* Ambient Temperature Fan Speed Control */
  2212. break;
  2213. default:
  2214. printf( "Unknown Intel OEM SDR Record type %02x",
  2215. oem->data[3]);
  2216. }
  2217. return 0;
  2218. }
  2219. /* ipmi_sdr_print_sensor_oem - print OEM sensors
  2220. *
  2221. * This function is generally only filled out by decoding what
  2222. * a particular BMC might stuff into its OEM records. The
  2223. * records are keyed off manufacturer ID and record subtypes.
  2224. *
  2225. * @intf: ipmi interface
  2226. * @oem: oem sdr record
  2227. *
  2228. * returns 0 on success
  2229. * returns -1 on error
  2230. */
  2231. static int
  2232. ipmi_sdr_print_sensor_oem(struct ipmi_intf *intf, struct sdr_record_oem *oem)
  2233. {
  2234. int rc = 0;
  2235. if (oem == NULL)
  2236. return -1;
  2237. if (oem->data_len == 0 || oem->data == NULL)
  2238. return -1;
  2239. if (verbose > 2)
  2240. printbuf(oem->data, oem->data_len, "OEM Record");
  2241. /* intel manufacturer id */
  2242. if (oem->data[0] == 0x57 &&
  2243. oem->data[1] == 0x01 && oem->data[2] == 0x00) {
  2244. rc = ipmi_sdr_print_sensor_oem_intel(intf, oem);
  2245. }
  2246. return rc;
  2247. }
  2248. /* ipmi_sdr_print_name_from_rawentry - Print SDR name from raw data
  2249. *
  2250. * @intf: ipmi interface
  2251. * @type: sensor type
  2252. * @raw: raw sensor data
  2253. *
  2254. * returns 0 on success
  2255. * returns -1 on error
  2256. */
  2257. int
  2258. ipmi_sdr_print_name_from_rawentry(struct ipmi_intf *intf, uint16_t id,
  2259. uint8_t type, uint8_t *raw)
  2260. {
  2261. union {
  2262. struct sdr_record_full_sensor *full;
  2263. struct sdr_record_compact_sensor *compact;
  2264. struct sdr_record_eventonly_sensor *eventonly;
  2265. struct sdr_record_generic_locator *genloc;
  2266. struct sdr_record_fru_locator *fruloc;
  2267. struct sdr_record_mc_locator *mcloc;
  2268. struct sdr_record_entity_assoc *entassoc;
  2269. struct sdr_record_oem *oem;
  2270. } record;
  2271. int rc =0;
  2272. char desc[17];
  2273. memset(desc, ' ', sizeof (desc));
  2274. switch ( type) {
  2275. case SDR_RECORD_TYPE_FULL_SENSOR:
  2276. record.full = (struct sdr_record_full_sensor *) raw;
  2277. snprintf(desc, (record.full->id_code & 0x1f) +1, "%s",
  2278. (const char *)record.full->id_string);
  2279. break;
  2280. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2281. record.compact = (struct sdr_record_compact_sensor *) raw ;
  2282. snprintf(desc, (record.compact->id_code & 0x1f) +1, "%s",
  2283. (const char *)record.compact->id_string);
  2284. break;
  2285. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2286. record.eventonly = (struct sdr_record_eventonly_sensor *) raw ;
  2287. snprintf(desc, (record.eventonly->id_code & 0x1f) +1, "%s",
  2288. (const char *)record.eventonly->id_string);
  2289. break;
  2290. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2291. record.mcloc = (struct sdr_record_mc_locator *) raw ;
  2292. snprintf(desc, (record.mcloc->id_code & 0x1f) +1, "%s",
  2293. (const char *)record.mcloc->id_string);
  2294. break;
  2295. default:
  2296. rc = -1;
  2297. break;
  2298. }
  2299. printf( "ID: 0x%04x , NAME: %-16s", id, desc);
  2300. return rc;
  2301. }
  2302. /* ipmi_sdr_print_rawentry - Print SDR entry from raw data
  2303. *
  2304. * @intf: ipmi interface
  2305. * @type: sensor type
  2306. * @raw: raw sensor data
  2307. * @len: length of raw sensor data
  2308. *
  2309. * returns 0 on success
  2310. * returns -1 on error
  2311. */
  2312. int
  2313. ipmi_sdr_print_rawentry(struct ipmi_intf *intf, uint8_t type,
  2314. uint8_t * raw, int len)
  2315. {
  2316. int rc = 0;
  2317. switch (type) {
  2318. case SDR_RECORD_TYPE_FULL_SENSOR:
  2319. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2320. rc = ipmi_sdr_print_sensor_fc(intf,
  2321. (struct sdr_record_common_sensor *) raw,
  2322. type);
  2323. break;
  2324. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2325. rc = ipmi_sdr_print_sensor_eventonly(intf,
  2326. (struct
  2327. sdr_record_eventonly_sensor
  2328. *) raw);
  2329. break;
  2330. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  2331. rc = ipmi_sdr_print_sensor_generic_locator(intf,
  2332. (struct
  2333. sdr_record_generic_locator
  2334. *) raw);
  2335. break;
  2336. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  2337. rc = ipmi_sdr_print_sensor_fru_locator(intf,
  2338. (struct
  2339. sdr_record_fru_locator
  2340. *) raw);
  2341. break;
  2342. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2343. rc = ipmi_sdr_print_sensor_mc_locator(intf,
  2344. (struct
  2345. sdr_record_mc_locator *)
  2346. raw);
  2347. break;
  2348. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  2349. rc = ipmi_sdr_print_sensor_entity_assoc(intf,
  2350. (struct
  2351. sdr_record_entity_assoc
  2352. *) raw);
  2353. break;
  2354. case SDR_RECORD_TYPE_OEM:{
  2355. struct sdr_record_oem oem;
  2356. oem.data = raw;
  2357. oem.data_len = len;
  2358. rc = ipmi_sdr_print_sensor_oem(intf,
  2359. (struct sdr_record_oem *)
  2360. &oem);
  2361. break;
  2362. }
  2363. case SDR_RECORD_TYPE_DEVICE_ENTITY_ASSOC:
  2364. case SDR_RECORD_TYPE_MC_CONFIRMATION:
  2365. case SDR_RECORD_TYPE_BMC_MSG_CHANNEL_INFO:
  2366. /* not implemented */
  2367. break;
  2368. }
  2369. return rc;
  2370. }
  2371. /* ipmi_sdr_print_listentry - Print SDR entry from list
  2372. *
  2373. * @intf: ipmi interface
  2374. * @entry: sdr record list entry
  2375. *
  2376. * returns 0 on success
  2377. * returns -1 on error
  2378. */
  2379. int
  2380. ipmi_sdr_print_listentry(struct ipmi_intf *intf, struct sdr_record_list *entry)
  2381. {
  2382. int rc = 0;
  2383. switch (entry->type) {
  2384. case SDR_RECORD_TYPE_FULL_SENSOR:
  2385. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2386. rc = ipmi_sdr_print_sensor_fc(intf, entry->record.common, entry->type);
  2387. break;
  2388. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2389. rc = ipmi_sdr_print_sensor_eventonly(intf,
  2390. entry->record.eventonly);
  2391. break;
  2392. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  2393. rc = ipmi_sdr_print_sensor_generic_locator(intf,
  2394. entry->record.
  2395. genloc);
  2396. break;
  2397. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  2398. rc = ipmi_sdr_print_sensor_fru_locator(intf,
  2399. entry->record.fruloc);
  2400. break;
  2401. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2402. rc = ipmi_sdr_print_sensor_mc_locator(intf,
  2403. entry->record.mcloc);
  2404. break;
  2405. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  2406. rc = ipmi_sdr_print_sensor_entity_assoc(intf,
  2407. entry->record.entassoc);
  2408. break;
  2409. case SDR_RECORD_TYPE_OEM:
  2410. rc = ipmi_sdr_print_sensor_oem(intf, entry->record.oem);
  2411. break;
  2412. case SDR_RECORD_TYPE_DEVICE_ENTITY_ASSOC:
  2413. case SDR_RECORD_TYPE_MC_CONFIRMATION:
  2414. case SDR_RECORD_TYPE_BMC_MSG_CHANNEL_INFO:
  2415. /* not implemented yet */
  2416. break;
  2417. }
  2418. return rc;
  2419. }
  2420. /* ipmi_sdr_print_sdr - iterate through SDR printing records
  2421. *
  2422. * intf: ipmi interface
  2423. * type: record type to print
  2424. *
  2425. * returns 0 on success
  2426. * returns -1 on error
  2427. */
  2428. int
  2429. ipmi_sdr_print_sdr(struct ipmi_intf *intf, uint8_t type)
  2430. {
  2431. struct sdr_get_rs *header;
  2432. struct sdr_record_list *e;
  2433. int rc = 0;
  2434. printf( "Querying SDR for sensor list");
  2435. if (sdr_list_itr == NULL) {
  2436. sdr_list_itr = ipmi_sdr_start(intf, 0);
  2437. if (sdr_list_itr == NULL) {
  2438. printf( "Unable to open SDR for reading");
  2439. return -1;
  2440. }
  2441. }
  2442. for (e = sdr_list_head; e != NULL; e = e->next) {
  2443. if (type != e->type && type != 0xff && type != 0xfe)
  2444. continue;
  2445. if (type == 0xfe &&
  2446. e->type != SDR_RECORD_TYPE_FULL_SENSOR &&
  2447. e->type != SDR_RECORD_TYPE_COMPACT_SENSOR)
  2448. continue;
  2449. if (ipmi_sdr_print_listentry(intf, e) < 0)
  2450. rc = -1;
  2451. }
  2452. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  2453. uint8_t *rec;
  2454. struct sdr_record_list *sdrr;
  2455. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  2456. if (rec == NULL) {
  2457. printf( "ipmitool: ipmi_sdr_get_record() failed");
  2458. rc = -1;
  2459. continue;
  2460. }
  2461. sdrr = malloc(sizeof (struct sdr_record_list));
  2462. if (sdrr == NULL) {
  2463. printf( "ipmitool: malloc failure");
  2464. if (rec != NULL) {
  2465. free(rec);
  2466. rec = NULL;
  2467. }
  2468. break;
  2469. }
  2470. memset(sdrr, 0, sizeof (struct sdr_record_list));
  2471. sdrr->id = header->id;
  2472. sdrr->type = header->type;
  2473. switch (header->type) {
  2474. case SDR_RECORD_TYPE_FULL_SENSOR:
  2475. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2476. sdrr->record.common =
  2477. (struct sdr_record_common_sensor *) rec;
  2478. break;
  2479. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2480. sdrr->record.eventonly =
  2481. (struct sdr_record_eventonly_sensor *) rec;
  2482. break;
  2483. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  2484. sdrr->record.genloc =
  2485. (struct sdr_record_generic_locator *) rec;
  2486. break;
  2487. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  2488. sdrr->record.fruloc =
  2489. (struct sdr_record_fru_locator *) rec;
  2490. break;
  2491. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2492. sdrr->record.mcloc =
  2493. (struct sdr_record_mc_locator *) rec;
  2494. break;
  2495. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  2496. sdrr->record.entassoc =
  2497. (struct sdr_record_entity_assoc *) rec;
  2498. break;
  2499. default:
  2500. free(rec);
  2501. rec = NULL;
  2502. if (sdrr != NULL) {
  2503. free(sdrr);
  2504. sdrr = NULL;
  2505. }
  2506. continue;
  2507. }
  2508. printf( "SDR record ID : 0x%04x", sdrr->id);
  2509. if (type == header->type || type == 0xff ||
  2510. (type == 0xfe &&
  2511. (header->type == SDR_RECORD_TYPE_FULL_SENSOR ||
  2512. header->type == SDR_RECORD_TYPE_COMPACT_SENSOR))) {
  2513. if (ipmi_sdr_print_rawentry(intf, header->type,
  2514. rec, header->length) < 0)
  2515. rc = -1;
  2516. }
  2517. /* add to global record liset */
  2518. if (sdr_list_head == NULL)
  2519. sdr_list_head = sdrr;
  2520. else
  2521. sdr_list_tail->next = sdrr;
  2522. sdr_list_tail = sdrr;
  2523. }
  2524. return rc;
  2525. }
  2526. /* ipmi_sdr_get_reservation - Obtain SDR reservation ID
  2527. *
  2528. * @intf: ipmi interface
  2529. * @reserve_id: pointer to short int for storing the id
  2530. *
  2531. * returns 0 on success
  2532. * returns -1 on error
  2533. */
  2534. int
  2535. ipmi_sdr_get_reservation(struct ipmi_intf *intf, int use_builtin,
  2536. uint16_t * reserve_id)
  2537. {
  2538. struct ipmi_rs *rsp;
  2539. struct ipmi_rq req;
  2540. /* obtain reservation ID */
  2541. memset(&req, 0, sizeof (req));
  2542. if (use_builtin == 0) {
  2543. req.msg.netfn = IPMI_NETFN_STORAGE;
  2544. } else {
  2545. req.msg.netfn = IPMI_NETFN_SE;
  2546. }
  2547. req.msg.cmd = GET_SDR_RESERVE_REPO;
  2548. rsp = intf->sendrecv(intf, &req);
  2549. /* be slient for errors, they are handled by calling function */
  2550. if (rsp == NULL)
  2551. return -1;
  2552. if (rsp->ccode > 0)
  2553. return -1;
  2554. *reserve_id = ((struct sdr_reserve_repo_rs *) &(rsp->data))->reserve_id;
  2555. printf( "SDR reservation ID %04x", *reserve_id);
  2556. return 0;
  2557. }
  2558. /* ipmi_sdr_start - setup sdr iterator
  2559. *
  2560. * @intf: ipmi interface
  2561. *
  2562. * returns sdr iterator structure pointer
  2563. * returns NULL on error
  2564. */
  2565. struct ipmi_sdr_iterator *
  2566. ipmi_sdr_start(struct ipmi_intf *intf, int use_builtin)
  2567. {
  2568. struct ipmi_sdr_iterator *itr;
  2569. struct ipmi_rs *rsp;
  2570. struct ipmi_rq req;
  2571. struct ipm_devid_rsp *devid;
  2572. itr = malloc(sizeof (struct ipmi_sdr_iterator));
  2573. if (itr == NULL) {
  2574. printf( "ipmitool: malloc failure");
  2575. return NULL;
  2576. }
  2577. /* check SDRR capability */
  2578. memset(&req, 0, sizeof (req));
  2579. req.msg.netfn = IPMI_NETFN_APP;
  2580. req.msg.cmd = BMC_GET_DEVICE_ID;
  2581. req.msg.data_len = 0;
  2582. rsp = intf->sendrecv(intf, &req);
  2583. if (rsp == NULL) {
  2584. printf( "Get Device ID command failed");
  2585. free(itr);
  2586. itr = NULL;
  2587. return NULL;
  2588. }
  2589. if (rsp->ccode > 0) {
  2590. printf( "Get Device ID command failed: %#x %s",
  2591. rsp->ccode, val2str(rsp->ccode, completion_code_vals));
  2592. free(itr);
  2593. itr = NULL;
  2594. return NULL;
  2595. }
  2596. devid = (struct ipm_devid_rsp *) rsp->data;
  2597. sdriana = (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id);
  2598. if (!use_builtin && (devid->device_revision & IPM_DEV_DEVICE_ID_SDR_MASK)) {
  2599. if ((devid->adtl_device_support & 0x02) == 0) {
  2600. if ((devid->adtl_device_support & 0x01)) {
  2601. printf( "Using Device SDRs\n");
  2602. use_built_in = 1;
  2603. } else {
  2604. printf( "Error obtaining SDR info");
  2605. free(itr);
  2606. itr = NULL;
  2607. return NULL;
  2608. }
  2609. } else {
  2610. printf( "Using SDR from Repository \n");
  2611. }
  2612. }
  2613. itr->use_built_in = use_builtin ? 1 : use_built_in;
  2614. /***********************/
  2615. if (itr->use_built_in == 0) {
  2616. struct sdr_repo_info_rs sdr_info;
  2617. /* get sdr repository info */
  2618. memset(&req, 0, sizeof (req));
  2619. req.msg.netfn = IPMI_NETFN_STORAGE;
  2620. req.msg.cmd = GET_SDR_REPO_INFO;
  2621. rsp = intf->sendrecv(intf, &req);
  2622. if (rsp == NULL) {
  2623. printf( "Error obtaining SDR info");
  2624. free(itr);
  2625. itr = NULL;
  2626. return NULL;
  2627. }
  2628. if (rsp->ccode > 0) {
  2629. printf( "Error obtaining SDR info: %s",
  2630. val2str(rsp->ccode, completion_code_vals));
  2631. free(itr);
  2632. itr = NULL;
  2633. return NULL;
  2634. }
  2635. memcpy(&sdr_info, rsp->data, sizeof (sdr_info));
  2636. /* IPMIv1.0 == 0x01
  2637. * IPMIv1.5 == 0x51
  2638. * IPMIv2.0 == 0x02
  2639. */
  2640. if ((sdr_info.version != 0x51) &&
  2641. (sdr_info.version != 0x01) &&
  2642. (sdr_info.version != 0x02)) {
  2643. printf( "WARNING: Unknown SDR repository "
  2644. "version 0x%02x", sdr_info.version);
  2645. }
  2646. itr->total = sdr_info.count;
  2647. itr->next = 0;
  2648. printf( "SDR free space: %d", sdr_info.free);
  2649. printf( "SDR records : %d", sdr_info.count);
  2650. /* Build SDRR if there is no record in repository */
  2651. if( sdr_info.count == 0 ) {
  2652. printf( "Rebuilding SDRR...");
  2653. if( ipmi_sdr_add_from_sensors( intf, 0 ) != 0 ) {
  2654. printf( "Could not build SDRR!");
  2655. free(itr);
  2656. itr = NULL;
  2657. return NULL;
  2658. }
  2659. }
  2660. } else {
  2661. struct sdr_device_info_rs sdr_info;
  2662. /* get device sdr info */
  2663. memset(&req, 0, sizeof (req));
  2664. req.msg.netfn = IPMI_NETFN_SE;
  2665. req.msg.cmd = GET_DEVICE_SDR_INFO;
  2666. rsp = intf->sendrecv(intf, &req);
  2667. if (!rsp || !rsp->data_len || rsp->ccode) {
  2668. printf("Err in cmd get sensor sdr info\n");
  2669. free(itr);
  2670. itr = NULL;
  2671. return NULL;
  2672. }
  2673. memcpy(&sdr_info, rsp->data, sizeof (sdr_info));
  2674. itr->total = sdr_info.count;
  2675. itr->next = 0;
  2676. printf( "SDR records : %d", sdr_info.count);
  2677. }
  2678. if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
  2679. &(itr->reservation)) < 0) {
  2680. printf( "Unable to obtain SDR reservation");
  2681. free(itr);
  2682. itr = NULL;
  2683. return NULL;
  2684. }
  2685. return itr;
  2686. }
  2687. /* ipmi_sdr_get_record - return RAW SDR record
  2688. *
  2689. * @intf: ipmi interface
  2690. * @header: SDR header
  2691. * @itr: SDR iterator
  2692. *
  2693. * returns raw SDR data
  2694. * returns NULL on error
  2695. */
  2696. uint8_t *
  2697. ipmi_sdr_get_record(struct ipmi_intf * intf, struct sdr_get_rs * header,
  2698. struct ipmi_sdr_iterator * itr)
  2699. {
  2700. struct ipmi_rq req;
  2701. struct ipmi_rs *rsp;
  2702. struct sdr_get_rq sdr_rq;
  2703. uint8_t *data;
  2704. int i = 0, len = header->length;
  2705. if (len < 1)
  2706. return NULL;
  2707. data = malloc(len + 1);
  2708. if (data == NULL) {
  2709. printf( "ipmitool: malloc failure");
  2710. return NULL;
  2711. }
  2712. memset(data, 0, len + 1);
  2713. memset(&sdr_rq, 0, sizeof (sdr_rq));
  2714. sdr_rq.reserve_id = itr->reservation;
  2715. sdr_rq.id = header->id;
  2716. sdr_rq.offset = 0;
  2717. memset(&req, 0, sizeof (req));
  2718. if (itr->use_built_in == 0) {
  2719. req.msg.netfn = IPMI_NETFN_STORAGE;
  2720. req.msg.cmd = GET_SDR;
  2721. } else {
  2722. req.msg.netfn = IPMI_NETFN_SE;
  2723. req.msg.cmd = GET_DEVICE_SDR;
  2724. }
  2725. req.msg.data = (uint8_t *) & sdr_rq;
  2726. req.msg.data_len = sizeof (sdr_rq);
  2727. /* check if max length is null */
  2728. if ( sdr_max_read_len == 0 ) {
  2729. /* get maximum response size */
  2730. sdr_max_read_len = ipmi_intf_get_max_response_data_size(intf) - 2;
  2731. /* cap the number of bytes to read */
  2732. if (sdr_max_read_len > 0xFE) {
  2733. sdr_max_read_len = 0xFE;
  2734. }
  2735. }
  2736. /* read SDR record with partial reads
  2737. * because a full read usually exceeds the maximum
  2738. * transport buffer size. (completion code 0xca)
  2739. */
  2740. while (i < len) {
  2741. sdr_rq.length = (len - i < sdr_max_read_len) ?
  2742. len - i : sdr_max_read_len;
  2743. sdr_rq.offset = i + 5; /* 5 header bytes */
  2744. printf( "Getting %d bytes from SDR at offset %d",
  2745. sdr_rq.length, sdr_rq.offset);
  2746. rsp = intf->sendrecv(intf, &req);
  2747. if (rsp == NULL) {
  2748. sdr_max_read_len = sdr_rq.length - 1;
  2749. if (sdr_max_read_len > 0) {
  2750. /* no response may happen if requests are bridged
  2751. and too many bytes are requested */
  2752. continue;
  2753. } else {
  2754. free(data);
  2755. data = NULL;
  2756. return NULL;
  2757. }
  2758. }
  2759. switch (rsp->ccode) {
  2760. case 0xca:
  2761. /* read too many bytes at once */
  2762. sdr_max_read_len = sdr_rq.length - 1;
  2763. continue;
  2764. case 0xc5:
  2765. /* lost reservation */
  2766. printf( "SDR reservation cancelled. "
  2767. "Sleeping a bit and retrying...");
  2768. sleep(rand() & 3);
  2769. if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
  2770. &(itr->reservation)) < 0) {
  2771. free(data);
  2772. data = NULL;
  2773. return NULL;
  2774. }
  2775. sdr_rq.reserve_id = itr->reservation;
  2776. continue;
  2777. }
  2778. /* special completion codes handled above */
  2779. if (rsp->ccode > 0 || rsp->data_len == 0) {
  2780. free(data);
  2781. data = NULL;
  2782. return NULL;
  2783. }
  2784. memcpy(data + i, rsp->data + 2, sdr_rq.length);
  2785. i += sdr_max_read_len;
  2786. }
  2787. return data;
  2788. }
  2789. /* ipmi_sdr_end - cleanup SDR iterator
  2790. *
  2791. * @intf: ipmi interface
  2792. * @itr: SDR iterator
  2793. *
  2794. * no meaningful return code
  2795. */
  2796. void
  2797. ipmi_sdr_end(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
  2798. {
  2799. if (itr) {
  2800. free(itr);
  2801. itr = NULL;
  2802. }
  2803. }
  2804. /* __sdr_list_add - helper function to add SDR record to list
  2805. *
  2806. * @head: list head
  2807. * @entry: new entry to add to end of list
  2808. *
  2809. * returns 0 on success
  2810. * returns -1 on error
  2811. */
  2812. static int
  2813. __sdr_list_add(struct sdr_record_list *head, struct sdr_record_list *entry)
  2814. {
  2815. struct sdr_record_list *e;
  2816. struct sdr_record_list *new;
  2817. if (head == NULL)
  2818. return -1;
  2819. new = malloc(sizeof (struct sdr_record_list));
  2820. if (new == NULL) {
  2821. printf( "ipmitool: malloc failure");
  2822. return -1;
  2823. }
  2824. memcpy(new, entry, sizeof (struct sdr_record_list));
  2825. e = head;
  2826. while (e->next)
  2827. e = e->next;
  2828. e->next = new;
  2829. new->next = NULL;
  2830. return 0;
  2831. }
  2832. /* __sdr_list_empty - low-level handler to clean up record list
  2833. *
  2834. * @head: list head to clean
  2835. *
  2836. * no meaningful return code
  2837. */
  2838. static void
  2839. __sdr_list_empty(struct sdr_record_list *head)
  2840. {
  2841. struct sdr_record_list *e, *f;
  2842. for (e = head; e != NULL; e = f) {
  2843. f = e->next;
  2844. free(e);
  2845. e = NULL;
  2846. }
  2847. head = NULL;
  2848. }
  2849. /* ipmi_sdr_list_empty - clean global SDR list
  2850. *
  2851. * @intf: ipmi interface
  2852. *
  2853. * no meaningful return code
  2854. */
  2855. void
  2856. ipmi_sdr_list_empty(struct ipmi_intf *intf)
  2857. {
  2858. struct sdr_record_list *list, *next;
  2859. ipmi_sdr_end(intf, sdr_list_itr);
  2860. for (list = sdr_list_head; list != NULL; list = next) {
  2861. switch (list->type) {
  2862. case SDR_RECORD_TYPE_FULL_SENSOR:
  2863. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2864. if (list->record.common) {
  2865. free(list->record.common);
  2866. list->record.common = NULL;
  2867. }
  2868. break;
  2869. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2870. if (list->record.eventonly) {
  2871. free(list->record.eventonly);
  2872. list->record.eventonly = NULL;
  2873. }
  2874. break;
  2875. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  2876. if (list->record.genloc) {
  2877. free(list->record.genloc);
  2878. list->record.genloc = NULL;
  2879. }
  2880. break;
  2881. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  2882. if (list->record.fruloc) {
  2883. free(list->record.fruloc);
  2884. list->record.fruloc = NULL;
  2885. }
  2886. break;
  2887. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2888. if (list->record.mcloc) {
  2889. free(list->record.mcloc);
  2890. list->record.mcloc = NULL;
  2891. }
  2892. break;
  2893. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  2894. if (list->record.entassoc) {
  2895. free(list->record.entassoc);
  2896. list->record.entassoc = NULL;
  2897. }
  2898. break;
  2899. }
  2900. next = list->next;
  2901. free(list);
  2902. list = NULL;
  2903. }
  2904. sdr_list_head = NULL;
  2905. sdr_list_tail = NULL;
  2906. sdr_list_itr = NULL;
  2907. }
  2908. /* ipmi_sdr_find_sdr_bynumtype - lookup SDR entry by number/type
  2909. *
  2910. * @intf: ipmi interface
  2911. * @gen_id: sensor owner ID/LUN - SEL generator ID
  2912. * @num: sensor number to search for
  2913. * @type: sensor type to search for
  2914. *
  2915. * returns pointer to SDR list
  2916. * returns NULL on error
  2917. */
  2918. struct sdr_record_list *
  2919. ipmi_sdr_find_sdr_bynumtype(struct ipmi_intf *intf, uint16_t gen_id, uint8_t num, uint8_t type)
  2920. {
  2921. struct sdr_get_rs *header;
  2922. struct sdr_record_list *e;
  2923. int found = 0;
  2924. if (sdr_list_itr == NULL) {
  2925. sdr_list_itr = ipmi_sdr_start(intf, 0);
  2926. if (sdr_list_itr == NULL) {
  2927. printf( "Unable to open SDR for reading");
  2928. return NULL;
  2929. }
  2930. }
  2931. /* check what we've already read */
  2932. for (e = sdr_list_head; e != NULL; e = e->next) {
  2933. switch (e->type) {
  2934. case SDR_RECORD_TYPE_FULL_SENSOR:
  2935. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2936. if (e->record.common->keys.sensor_num == num &&
  2937. e->record.common->keys.owner_id == (gen_id & 0x00ff) &&
  2938. e->record.common->sensor.type == type)
  2939. return e;
  2940. break;
  2941. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2942. if (e->record.eventonly->keys.sensor_num == num &&
  2943. e->record.eventonly->keys.owner_id == (gen_id & 0x00ff) &&
  2944. e->record.eventonly->sensor_type == type)
  2945. return e;
  2946. break;
  2947. }
  2948. }
  2949. /* now keep looking */
  2950. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  2951. uint8_t *rec;
  2952. struct sdr_record_list *sdrr;
  2953. sdrr = malloc(sizeof (struct sdr_record_list));
  2954. if (sdrr == NULL) {
  2955. printf( "ipmitool: malloc failure");
  2956. break;
  2957. }
  2958. memset(sdrr, 0, sizeof (struct sdr_record_list));
  2959. sdrr->id = header->id;
  2960. sdrr->type = header->type;
  2961. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  2962. if (rec == NULL) {
  2963. if (sdrr != NULL) {
  2964. free(sdrr);
  2965. sdrr = NULL;
  2966. }
  2967. continue;
  2968. }
  2969. switch (header->type) {
  2970. case SDR_RECORD_TYPE_FULL_SENSOR:
  2971. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  2972. sdrr->record.common =
  2973. (struct sdr_record_common_sensor *) rec;
  2974. if (sdrr->record.common->keys.sensor_num == num
  2975. && sdrr->record.common->keys.owner_id == (gen_id & 0x00ff)
  2976. && sdrr->record.common->sensor.type == type)
  2977. found = 1;
  2978. break;
  2979. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  2980. sdrr->record.eventonly =
  2981. (struct sdr_record_eventonly_sensor *) rec;
  2982. if (sdrr->record.eventonly->keys.sensor_num == num
  2983. && sdrr->record.eventonly->keys.owner_id == (gen_id & 0x00ff)
  2984. && sdrr->record.eventonly->sensor_type == type)
  2985. found = 1;
  2986. break;
  2987. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  2988. sdrr->record.genloc =
  2989. (struct sdr_record_generic_locator *) rec;
  2990. break;
  2991. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  2992. sdrr->record.fruloc =
  2993. (struct sdr_record_fru_locator *) rec;
  2994. break;
  2995. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  2996. sdrr->record.mcloc =
  2997. (struct sdr_record_mc_locator *) rec;
  2998. break;
  2999. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3000. sdrr->record.entassoc =
  3001. (struct sdr_record_entity_assoc *) rec;
  3002. break;
  3003. default:
  3004. free(rec);
  3005. rec = NULL;
  3006. if (sdrr != NULL) {
  3007. free(sdrr);
  3008. sdrr = NULL;
  3009. }
  3010. continue;
  3011. }
  3012. /* put in the global record list */
  3013. if (sdr_list_head == NULL)
  3014. sdr_list_head = sdrr;
  3015. else
  3016. sdr_list_tail->next = sdrr;
  3017. sdr_list_tail = sdrr;
  3018. if (found)
  3019. return sdrr;
  3020. }
  3021. return NULL;
  3022. }
  3023. /* ipmi_sdr_find_sdr_bysensortype - lookup SDR entry by sensor type
  3024. *
  3025. * @intf: ipmi interface
  3026. * @type: sensor type to search for
  3027. *
  3028. * returns pointer to SDR list
  3029. * returns NULL on error
  3030. */
  3031. struct sdr_record_list *
  3032. ipmi_sdr_find_sdr_bysensortype(struct ipmi_intf *intf, uint8_t type)
  3033. {
  3034. struct sdr_record_list *head;
  3035. struct sdr_get_rs *header;
  3036. struct sdr_record_list *e;
  3037. if (sdr_list_itr == NULL) {
  3038. sdr_list_itr = ipmi_sdr_start(intf, 0);
  3039. if (sdr_list_itr == NULL) {
  3040. printf( "Unable to open SDR for reading");
  3041. return NULL;
  3042. }
  3043. }
  3044. /* check what we've already read */
  3045. head = malloc(sizeof (struct sdr_record_list));
  3046. if (head == NULL) {
  3047. printf( "ipmitool: malloc failure");
  3048. return NULL;
  3049. }
  3050. memset(head, 0, sizeof (struct sdr_record_list));
  3051. for (e = sdr_list_head; e != NULL; e = e->next) {
  3052. switch (e->type) {
  3053. case SDR_RECORD_TYPE_FULL_SENSOR:
  3054. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3055. if (e->record.common->sensor.type == type)
  3056. __sdr_list_add(head, e);
  3057. break;
  3058. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3059. if (e->record.eventonly->sensor_type == type)
  3060. __sdr_list_add(head, e);
  3061. break;
  3062. }
  3063. }
  3064. /* now keep looking */
  3065. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  3066. uint8_t *rec;
  3067. struct sdr_record_list *sdrr;
  3068. sdrr = malloc(sizeof (struct sdr_record_list));
  3069. if (sdrr == NULL) {
  3070. printf( "ipmitool: malloc failure");
  3071. break;
  3072. }
  3073. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3074. sdrr->id = header->id;
  3075. sdrr->type = header->type;
  3076. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  3077. if (rec == NULL) {
  3078. if (sdrr != NULL) {
  3079. free(sdrr);
  3080. sdrr = NULL;
  3081. }
  3082. continue;
  3083. }
  3084. switch (header->type) {
  3085. case SDR_RECORD_TYPE_FULL_SENSOR:
  3086. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3087. sdrr->record.common =
  3088. (struct sdr_record_common_sensor *) rec;
  3089. if (sdrr->record.common->sensor.type == type)
  3090. __sdr_list_add(head, sdrr);
  3091. break;
  3092. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3093. sdrr->record.eventonly =
  3094. (struct sdr_record_eventonly_sensor *) rec;
  3095. if (sdrr->record.eventonly->sensor_type == type)
  3096. __sdr_list_add(head, sdrr);
  3097. break;
  3098. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3099. sdrr->record.genloc =
  3100. (struct sdr_record_generic_locator *) rec;
  3101. break;
  3102. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3103. sdrr->record.fruloc =
  3104. (struct sdr_record_fru_locator *) rec;
  3105. break;
  3106. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3107. sdrr->record.mcloc =
  3108. (struct sdr_record_mc_locator *) rec;
  3109. break;
  3110. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3111. sdrr->record.entassoc =
  3112. (struct sdr_record_entity_assoc *) rec;
  3113. break;
  3114. default:
  3115. free(rec);
  3116. rec = NULL;
  3117. if (sdrr != NULL) {
  3118. free(sdrr);
  3119. sdrr = NULL;
  3120. }
  3121. continue;
  3122. }
  3123. /* put in the global record list */
  3124. if (sdr_list_head == NULL)
  3125. sdr_list_head = sdrr;
  3126. else
  3127. sdr_list_tail->next = sdrr;
  3128. sdr_list_tail = sdrr;
  3129. }
  3130. return head;
  3131. }
  3132. /* ipmi_sdr_find_sdr_byentity - lookup SDR entry by entity association
  3133. *
  3134. * @intf: ipmi interface
  3135. * @entity: entity id/instance to search for
  3136. *
  3137. * returns pointer to SDR list
  3138. * returns NULL on error
  3139. */
  3140. struct sdr_record_list *
  3141. ipmi_sdr_find_sdr_byentity(struct ipmi_intf *intf, struct entity_id *entity)
  3142. {
  3143. struct sdr_get_rs *header;
  3144. struct sdr_record_list *e;
  3145. struct sdr_record_list *head;
  3146. if (sdr_list_itr == NULL) {
  3147. sdr_list_itr = ipmi_sdr_start(intf, 0);
  3148. if (sdr_list_itr == NULL) {
  3149. printf( "Unable to open SDR for reading");
  3150. return NULL;
  3151. }
  3152. }
  3153. head = malloc(sizeof (struct sdr_record_list));
  3154. if (head == NULL) {
  3155. printf( "ipmitool: malloc failure");
  3156. return NULL;
  3157. }
  3158. memset(head, 0, sizeof (struct sdr_record_list));
  3159. /* check what we've already read */
  3160. for (e = sdr_list_head; e != NULL; e = e->next) {
  3161. switch (e->type) {
  3162. case SDR_RECORD_TYPE_FULL_SENSOR:
  3163. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3164. if (e->record.common->entity.id == entity->id &&
  3165. (entity->instance == 0x7f ||
  3166. e->record.common->entity.instance ==
  3167. entity->instance))
  3168. __sdr_list_add(head, e);
  3169. break;
  3170. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3171. if (e->record.eventonly->entity.id == entity->id &&
  3172. (entity->instance == 0x7f ||
  3173. e->record.eventonly->entity.instance ==
  3174. entity->instance))
  3175. __sdr_list_add(head, e);
  3176. break;
  3177. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3178. if (e->record.genloc->entity.id == entity->id &&
  3179. (entity->instance == 0x7f ||
  3180. e->record.genloc->entity.instance ==
  3181. entity->instance))
  3182. __sdr_list_add(head, e);
  3183. break;
  3184. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3185. if (e->record.fruloc->entity.id == entity->id &&
  3186. (entity->instance == 0x7f ||
  3187. e->record.fruloc->entity.instance ==
  3188. entity->instance))
  3189. __sdr_list_add(head, e);
  3190. break;
  3191. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3192. if (e->record.mcloc->entity.id == entity->id &&
  3193. (entity->instance == 0x7f ||
  3194. e->record.mcloc->entity.instance ==
  3195. entity->instance))
  3196. __sdr_list_add(head, e);
  3197. break;
  3198. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3199. if (e->record.entassoc->entity.id == entity->id &&
  3200. (entity->instance == 0x7f ||
  3201. e->record.entassoc->entity.instance ==
  3202. entity->instance))
  3203. __sdr_list_add(head, e);
  3204. break;
  3205. }
  3206. }
  3207. /* now keep looking */
  3208. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  3209. uint8_t *rec;
  3210. struct sdr_record_list *sdrr;
  3211. sdrr = malloc(sizeof (struct sdr_record_list));
  3212. if (sdrr == NULL) {
  3213. printf( "ipmitool: malloc failure");
  3214. break;
  3215. }
  3216. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3217. sdrr->id = header->id;
  3218. sdrr->type = header->type;
  3219. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  3220. if (rec == NULL) {
  3221. if (sdrr != NULL) {
  3222. free(sdrr);
  3223. sdrr = NULL;
  3224. }
  3225. continue;
  3226. }
  3227. switch (header->type) {
  3228. case SDR_RECORD_TYPE_FULL_SENSOR:
  3229. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3230. sdrr->record.common =
  3231. (struct sdr_record_common_sensor *) rec;
  3232. if (sdrr->record.common->entity.id == entity->id
  3233. && (entity->instance == 0x7f
  3234. || sdrr->record.common->entity.instance ==
  3235. entity->instance))
  3236. __sdr_list_add(head, sdrr);
  3237. break;
  3238. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3239. sdrr->record.eventonly =
  3240. (struct sdr_record_eventonly_sensor *) rec;
  3241. if (sdrr->record.eventonly->entity.id == entity->id
  3242. && (entity->instance == 0x7f
  3243. || sdrr->record.eventonly->entity.instance ==
  3244. entity->instance))
  3245. __sdr_list_add(head, sdrr);
  3246. break;
  3247. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3248. sdrr->record.genloc =
  3249. (struct sdr_record_generic_locator *) rec;
  3250. if (sdrr->record.genloc->entity.id == entity->id
  3251. && (entity->instance == 0x7f
  3252. || sdrr->record.genloc->entity.instance ==
  3253. entity->instance))
  3254. __sdr_list_add(head, sdrr);
  3255. break;
  3256. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3257. sdrr->record.fruloc =
  3258. (struct sdr_record_fru_locator *) rec;
  3259. if (sdrr->record.fruloc->entity.id == entity->id
  3260. && (entity->instance == 0x7f
  3261. || sdrr->record.fruloc->entity.instance ==
  3262. entity->instance))
  3263. __sdr_list_add(head, sdrr);
  3264. break;
  3265. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3266. sdrr->record.mcloc =
  3267. (struct sdr_record_mc_locator *) rec;
  3268. if (sdrr->record.mcloc->entity.id == entity->id
  3269. && (entity->instance == 0x7f
  3270. || sdrr->record.mcloc->entity.instance ==
  3271. entity->instance))
  3272. __sdr_list_add(head, sdrr);
  3273. break;
  3274. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3275. sdrr->record.entassoc =
  3276. (struct sdr_record_entity_assoc *) rec;
  3277. if (sdrr->record.entassoc->entity.id == entity->id
  3278. && (entity->instance == 0x7f
  3279. || sdrr->record.entassoc->entity.instance ==
  3280. entity->instance))
  3281. __sdr_list_add(head, sdrr);
  3282. break;
  3283. default:
  3284. free(rec);
  3285. rec = NULL;
  3286. if (sdrr != NULL) {
  3287. free(sdrr);
  3288. sdrr = NULL;
  3289. }
  3290. continue;
  3291. }
  3292. /* add to global record list */
  3293. if (sdr_list_head == NULL)
  3294. sdr_list_head = sdrr;
  3295. else
  3296. sdr_list_tail->next = sdrr;
  3297. sdr_list_tail = sdrr;
  3298. }
  3299. return head;
  3300. }
  3301. /* ipmi_sdr_find_sdr_bytype - lookup SDR entries by type
  3302. *
  3303. * @intf: ipmi interface
  3304. * @type: type of sensor record to search for
  3305. *
  3306. * returns pointer to SDR list with all matching entities
  3307. * returns NULL on error
  3308. */
  3309. struct sdr_record_list *
  3310. ipmi_sdr_find_sdr_bytype(struct ipmi_intf *intf, uint8_t type)
  3311. {
  3312. struct sdr_get_rs *header;
  3313. struct sdr_record_list *e;
  3314. struct sdr_record_list *head;
  3315. if (sdr_list_itr == NULL) {
  3316. sdr_list_itr = ipmi_sdr_start(intf, 0);
  3317. if (sdr_list_itr == NULL) {
  3318. printf( "Unable to open SDR for reading");
  3319. return NULL;
  3320. }
  3321. }
  3322. head = malloc(sizeof (struct sdr_record_list));
  3323. if (head == NULL) {
  3324. printf( "ipmitool: malloc failure");
  3325. return NULL;
  3326. }
  3327. memset(head, 0, sizeof (struct sdr_record_list));
  3328. /* check what we've already read */
  3329. for (e = sdr_list_head; e != NULL; e = e->next)
  3330. if (e->type == type)
  3331. __sdr_list_add(head, e);
  3332. /* now keep looking */
  3333. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  3334. uint8_t *rec;
  3335. struct sdr_record_list *sdrr;
  3336. sdrr = malloc(sizeof (struct sdr_record_list));
  3337. if (sdrr == NULL) {
  3338. printf( "ipmitool: malloc failure");
  3339. break;
  3340. }
  3341. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3342. sdrr->id = header->id;
  3343. sdrr->type = header->type;
  3344. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  3345. if (rec == NULL) {
  3346. if (sdrr != NULL) {
  3347. free(sdrr);
  3348. sdrr = NULL;
  3349. }
  3350. continue;
  3351. }
  3352. switch (header->type) {
  3353. case SDR_RECORD_TYPE_FULL_SENSOR:
  3354. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3355. sdrr->record.common =
  3356. (struct sdr_record_common_sensor *) rec;
  3357. break;
  3358. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3359. sdrr->record.eventonly =
  3360. (struct sdr_record_eventonly_sensor *) rec;
  3361. break;
  3362. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3363. sdrr->record.genloc =
  3364. (struct sdr_record_generic_locator *) rec;
  3365. break;
  3366. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3367. sdrr->record.fruloc =
  3368. (struct sdr_record_fru_locator *) rec;
  3369. break;
  3370. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3371. sdrr->record.mcloc =
  3372. (struct sdr_record_mc_locator *) rec;
  3373. break;
  3374. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3375. sdrr->record.entassoc =
  3376. (struct sdr_record_entity_assoc *) rec;
  3377. break;
  3378. default:
  3379. free(rec);
  3380. rec = NULL;
  3381. if (sdrr != NULL) {
  3382. free(sdrr);
  3383. sdrr = NULL;
  3384. }
  3385. continue;
  3386. }
  3387. if (header->type == type)
  3388. __sdr_list_add(head, sdrr);
  3389. /* add to global record list */
  3390. if (sdr_list_head == NULL)
  3391. sdr_list_head = sdrr;
  3392. else
  3393. sdr_list_tail->next = sdrr;
  3394. sdr_list_tail = sdrr;
  3395. }
  3396. return head;
  3397. }
  3398. /* ipmi_sdr_find_sdr_byid - lookup SDR entry by ID string
  3399. *
  3400. * @intf: ipmi interface
  3401. * @id: string to match for sensor name
  3402. *
  3403. * returns pointer to SDR list
  3404. * returns NULL on error
  3405. */
  3406. struct sdr_record_list *
  3407. ipmi_sdr_find_sdr_byid(struct ipmi_intf *intf, char *id)
  3408. {
  3409. struct sdr_get_rs *header;
  3410. struct sdr_record_list *e;
  3411. int found = 0;
  3412. int idlen;
  3413. if (id == NULL)
  3414. return NULL;
  3415. idlen = strlen(id);
  3416. if (sdr_list_itr == NULL) {
  3417. sdr_list_itr = ipmi_sdr_start(intf, 0);
  3418. if (sdr_list_itr == NULL) {
  3419. printf( "Unable to open SDR for reading");
  3420. return NULL;
  3421. }
  3422. }
  3423. /* check what we've already read */
  3424. for (e = sdr_list_head; e != NULL; e = e->next) {
  3425. switch (e->type) {
  3426. case SDR_RECORD_TYPE_FULL_SENSOR:
  3427. if (!strncmp((const char *)e->record.full->id_string,
  3428. (const char *)id,
  3429. __max(e->record.full->id_code & 0x1f, idlen)))
  3430. return e;
  3431. break;
  3432. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3433. if (!strncmp((const char *)e->record.compact->id_string,
  3434. (const char *)id,
  3435. __max(e->record.compact->id_code & 0x1f, idlen)))
  3436. return e;
  3437. break;
  3438. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3439. if (!strncmp((const char *)e->record.eventonly->id_string,
  3440. (const char *)id,
  3441. __max(e->record.eventonly->id_code & 0x1f, idlen)))
  3442. return e;
  3443. break;
  3444. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3445. if (!strncmp((const char *)e->record.genloc->id_string,
  3446. (const char *)id,
  3447. __max(e->record.genloc->id_code & 0x1f, idlen)))
  3448. return e;
  3449. break;
  3450. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3451. if (!strncmp((const char *)e->record.fruloc->id_string,
  3452. (const char *)id,
  3453. __max(e->record.fruloc->id_code & 0x1f, idlen)))
  3454. return e;
  3455. break;
  3456. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3457. if (!strncmp((const char *)e->record.mcloc->id_string,
  3458. (const char *)id,
  3459. __max(e->record.mcloc->id_code & 0x1f, idlen)))
  3460. return e;
  3461. break;
  3462. }
  3463. }
  3464. /* now keep looking */
  3465. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  3466. uint8_t *rec;
  3467. struct sdr_record_list *sdrr;
  3468. sdrr = malloc(sizeof (struct sdr_record_list));
  3469. if (sdrr == NULL) {
  3470. printf( "ipmitool: malloc failure");
  3471. break;
  3472. }
  3473. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3474. sdrr->id = header->id;
  3475. sdrr->type = header->type;
  3476. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  3477. if (rec == NULL) {
  3478. if (sdrr != NULL) {
  3479. free(sdrr);
  3480. sdrr = NULL;
  3481. }
  3482. continue;
  3483. }
  3484. switch (header->type) {
  3485. case SDR_RECORD_TYPE_FULL_SENSOR:
  3486. sdrr->record.full =
  3487. (struct sdr_record_full_sensor *) rec;
  3488. if (!strncmp(
  3489. (const char *)sdrr->record.full->id_string,
  3490. (const char *)id,
  3491. __max(sdrr->record.full->id_code & 0x1f, idlen)))
  3492. found = 1;
  3493. break;
  3494. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3495. sdrr->record.compact =
  3496. (struct sdr_record_compact_sensor *) rec;
  3497. if (!strncmp(
  3498. (const char *)sdrr->record.compact->id_string,
  3499. (const char *)id,
  3500. __max(sdrr->record.compact->id_code & 0x1f,
  3501. idlen)))
  3502. found = 1;
  3503. break;
  3504. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3505. sdrr->record.eventonly =
  3506. (struct sdr_record_eventonly_sensor *) rec;
  3507. if (!strncmp(
  3508. (const char *)sdrr->record.eventonly->id_string,
  3509. (const char *)id,
  3510. __max(sdrr->record.eventonly->id_code & 0x1f,
  3511. idlen)))
  3512. found = 1;
  3513. break;
  3514. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3515. sdrr->record.genloc =
  3516. (struct sdr_record_generic_locator *) rec;
  3517. if (!strncmp(
  3518. (const char *)sdrr->record.genloc->id_string,
  3519. (const char *)id,
  3520. __max(sdrr->record.genloc->id_code & 0x1f, idlen)))
  3521. found = 1;
  3522. break;
  3523. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3524. sdrr->record.fruloc =
  3525. (struct sdr_record_fru_locator *) rec;
  3526. if (!strncmp(
  3527. (const char *)sdrr->record.fruloc->id_string,
  3528. (const char *)id,
  3529. __max(sdrr->record.fruloc->id_code & 0x1f, idlen)))
  3530. found = 1;
  3531. break;
  3532. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3533. sdrr->record.mcloc =
  3534. (struct sdr_record_mc_locator *) rec;
  3535. if (!strncmp(
  3536. (const char *)sdrr->record.mcloc->id_string,
  3537. (const char *)id,
  3538. __max(sdrr->record.mcloc->id_code & 0x1f, idlen)))
  3539. found = 1;
  3540. break;
  3541. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3542. sdrr->record.entassoc =
  3543. (struct sdr_record_entity_assoc *) rec;
  3544. break;
  3545. default:
  3546. free(rec);
  3547. rec = NULL;
  3548. if (sdrr != NULL) {
  3549. free(sdrr);
  3550. sdrr = NULL;
  3551. }
  3552. continue;
  3553. }
  3554. /* add to global record liset */
  3555. if (sdr_list_head == NULL)
  3556. sdr_list_head = sdrr;
  3557. else
  3558. sdr_list_tail->next = sdrr;
  3559. sdr_list_tail = sdrr;
  3560. if (found)
  3561. return sdrr;
  3562. }
  3563. return NULL;
  3564. }
  3565. /* ipmi_sdr_list_cache_fromfile - generate SDR cache for fast lookup from local file
  3566. *
  3567. * @intf: ipmi interface
  3568. * @ifile: input filename
  3569. *
  3570. * returns pointer to SDR list
  3571. * returns NULL on error
  3572. */
  3573. int
  3574. ipmi_sdr_list_cache_fromfile(struct ipmi_intf *intf, const char *ifile)
  3575. {
  3576. FILE *fp;
  3577. struct __sdr_header {
  3578. uint16_t id;
  3579. uint8_t version;
  3580. uint8_t type;
  3581. uint8_t length;
  3582. } header;
  3583. struct sdr_record_list *sdrr;
  3584. uint8_t *rec;
  3585. int ret = 0, count = 0, bc = 0;
  3586. if (ifile == NULL) {
  3587. printf( "No SDR cache filename given");
  3588. return -1;
  3589. }
  3590. fp = ipmi_open_file_read(ifile);
  3591. if (fp == NULL) {
  3592. printf( "Unable to open SDR cache %s for reading",
  3593. ifile);
  3594. return -1;
  3595. }
  3596. while (feof(fp) == 0) {
  3597. memset(&header, 0, sizeof(header));
  3598. bc = fread(&header, 1, 5, fp);
  3599. if (bc <= 0)
  3600. break;
  3601. if (bc != 5) {
  3602. printf( "header read %d bytes, expected 5",
  3603. bc);
  3604. ret = -1;
  3605. break;
  3606. }
  3607. if (header.length == 0)
  3608. continue;
  3609. if (header.version != 0x51 &&
  3610. header.version != 0x01 &&
  3611. header.version != 0x02) {
  3612. printf( "invalid sdr header version %02x",
  3613. header.version);
  3614. ret = -1;
  3615. break;
  3616. }
  3617. sdrr = malloc(sizeof (struct sdr_record_list));
  3618. if (sdrr == NULL) {
  3619. printf( "ipmitool: malloc failure");
  3620. ret = -1;
  3621. break;
  3622. }
  3623. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3624. sdrr->id = header.id;
  3625. sdrr->type = header.type;
  3626. rec = malloc(header.length + 1);
  3627. if (rec == NULL) {
  3628. printf( "ipmitool: malloc failure");
  3629. ret = -1;
  3630. if (sdrr != NULL) {
  3631. free(sdrr);
  3632. sdrr = NULL;
  3633. }
  3634. break;
  3635. }
  3636. memset(rec, 0, header.length + 1);
  3637. bc = fread(rec, 1, header.length, fp);
  3638. if (bc != header.length) {
  3639. printf(
  3640. "record %04x read %d bytes, expected %d",
  3641. header.id, bc, header.length);
  3642. ret = -1;
  3643. if (sdrr != NULL) {
  3644. free(sdrr);
  3645. sdrr = NULL;
  3646. }
  3647. if (rec != NULL) {
  3648. free(rec);
  3649. rec = NULL;
  3650. }
  3651. break;
  3652. }
  3653. switch (header.type) {
  3654. case SDR_RECORD_TYPE_FULL_SENSOR:
  3655. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3656. sdrr->record.common =
  3657. (struct sdr_record_common_sensor *) rec;
  3658. break;
  3659. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3660. sdrr->record.eventonly =
  3661. (struct sdr_record_eventonly_sensor *) rec;
  3662. break;
  3663. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3664. sdrr->record.genloc =
  3665. (struct sdr_record_generic_locator *) rec;
  3666. break;
  3667. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3668. sdrr->record.fruloc =
  3669. (struct sdr_record_fru_locator *) rec;
  3670. break;
  3671. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3672. sdrr->record.mcloc =
  3673. (struct sdr_record_mc_locator *) rec;
  3674. break;
  3675. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3676. sdrr->record.entassoc =
  3677. (struct sdr_record_entity_assoc *) rec;
  3678. break;
  3679. default:
  3680. free(rec);
  3681. rec = NULL;
  3682. if (sdrr != NULL) {
  3683. free(sdrr);
  3684. sdrr = NULL;
  3685. }
  3686. continue;
  3687. }
  3688. /* add to global record liset */
  3689. if (sdr_list_head == NULL)
  3690. sdr_list_head = sdrr;
  3691. else
  3692. sdr_list_tail->next = sdrr;
  3693. sdr_list_tail = sdrr;
  3694. count++;
  3695. printf( "Read record %04x from file into cache",
  3696. sdrr->id);
  3697. }
  3698. if (sdr_list_itr == NULL) {
  3699. sdr_list_itr = malloc(sizeof (struct ipmi_sdr_iterator));
  3700. if (sdr_list_itr != NULL) {
  3701. sdr_list_itr->reservation = 0;
  3702. sdr_list_itr->total = count;
  3703. sdr_list_itr->next = 0xffff;
  3704. }
  3705. }
  3706. fclose(fp);
  3707. return ret;
  3708. }
  3709. /* ipmi_sdr_list_cache - generate SDR cache for fast lookup
  3710. *
  3711. * @intf: ipmi interface
  3712. *
  3713. * returns pointer to SDR list
  3714. * returns NULL on error
  3715. */
  3716. int
  3717. ipmi_sdr_list_cache(struct ipmi_intf *intf)
  3718. {
  3719. struct sdr_get_rs *header;
  3720. if (sdr_list_itr == NULL) {
  3721. sdr_list_itr = ipmi_sdr_start(intf, 0);
  3722. if (sdr_list_itr == NULL) {
  3723. printf( "Unable to open SDR for reading");
  3724. return -1;
  3725. }
  3726. }
  3727. while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
  3728. uint8_t *rec;
  3729. struct sdr_record_list *sdrr;
  3730. sdrr = malloc(sizeof (struct sdr_record_list));
  3731. if (sdrr == NULL) {
  3732. printf( "ipmitool: malloc failure");
  3733. break;
  3734. }
  3735. memset(sdrr, 0, sizeof (struct sdr_record_list));
  3736. sdrr->id = header->id;
  3737. sdrr->type = header->type;
  3738. rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
  3739. if (rec == NULL) {
  3740. if (sdrr != NULL) {
  3741. free(sdrr);
  3742. sdrr = NULL;
  3743. }
  3744. continue;
  3745. }
  3746. switch (header->type) {
  3747. case SDR_RECORD_TYPE_FULL_SENSOR:
  3748. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  3749. sdrr->record.common =
  3750. (struct sdr_record_common_sensor *) rec;
  3751. break;
  3752. case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
  3753. sdrr->record.eventonly =
  3754. (struct sdr_record_eventonly_sensor *) rec;
  3755. break;
  3756. case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
  3757. sdrr->record.genloc =
  3758. (struct sdr_record_generic_locator *) rec;
  3759. break;
  3760. case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
  3761. sdrr->record.fruloc =
  3762. (struct sdr_record_fru_locator *) rec;
  3763. break;
  3764. case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
  3765. sdrr->record.mcloc =
  3766. (struct sdr_record_mc_locator *) rec;
  3767. break;
  3768. case SDR_RECORD_TYPE_ENTITY_ASSOC:
  3769. sdrr->record.entassoc =
  3770. (struct sdr_record_entity_assoc *) rec;
  3771. break;
  3772. default:
  3773. free(rec);
  3774. rec = NULL;
  3775. if (sdrr != NULL) {
  3776. free(sdrr);
  3777. sdrr = NULL;
  3778. }
  3779. continue;
  3780. }
  3781. /* add to global record liset */
  3782. if (sdr_list_head == NULL)
  3783. sdr_list_head = sdrr;
  3784. else
  3785. sdr_list_tail->next = sdrr;
  3786. sdr_list_tail = sdrr;
  3787. }
  3788. return 0;
  3789. }
  3790. /*
  3791. * ipmi_sdr_get_info
  3792. *
  3793. * Execute the GET SDR REPOSITORY INFO command, and populate the sdr_info
  3794. * structure.
  3795. * See section 33.9 of the IPMI v2 specification for details
  3796. *
  3797. * returns 0 on success
  3798. * -1 on transport error
  3799. * > 0 for other errors
  3800. */
  3801. int
  3802. ipmi_sdr_get_info(struct ipmi_intf *intf,
  3803. struct get_sdr_repository_info_rsp *sdr_repository_info)
  3804. {
  3805. struct ipmi_rs *rsp;
  3806. struct ipmi_rq req;
  3807. memset(&req, 0, sizeof (req));
  3808. req.msg.netfn = IPMI_NETFN_STORAGE; // 0x0A
  3809. req.msg.cmd = IPMI_GET_SDR_REPOSITORY_INFO; // 0x20
  3810. req.msg.data = 0;
  3811. req.msg.data_len = 0;
  3812. rsp = intf->sendrecv(intf, &req);
  3813. if (rsp == NULL) {
  3814. printf( "Get SDR Repository Info command failed");
  3815. return -1;
  3816. }
  3817. if (rsp->ccode > 0) {
  3818. printf( "Get SDR Repository Info command failed: %s",
  3819. val2str(rsp->ccode, completion_code_vals));
  3820. return -1;
  3821. }
  3822. memcpy(sdr_repository_info,
  3823. rsp->data,
  3824. __min(sizeof (struct get_sdr_repository_info_rsp),
  3825. rsp->data_len));
  3826. return 0;
  3827. }
  3828. /* ipmi_sdr_timestamp - return string from timestamp value
  3829. *
  3830. * @stamp: 32bit timestamp
  3831. *
  3832. * returns pointer to static buffer
  3833. */
  3834. static char *
  3835. ipmi_sdr_timestamp(uint32_t stamp)
  3836. {
  3837. static char tbuf[40];
  3838. time_t s = (time_t) stamp;
  3839. memset(tbuf, 0, 40);
  3840. if (stamp)
  3841. strftime(tbuf, sizeof (tbuf), "%m/%d/%Y %H:%M:%S",
  3842. gmtime(&s));
  3843. return tbuf;
  3844. }
  3845. /*
  3846. * ipmi_sdr_print_info
  3847. *
  3848. * Display the return data of the GET SDR REPOSITORY INFO command
  3849. * See section 33.9 of the IPMI v2 specification for details
  3850. *
  3851. * returns 0 on success
  3852. * -1 on error
  3853. */
  3854. int
  3855. ipmi_sdr_print_info(struct ipmi_intf *intf)
  3856. {
  3857. uint32_t timestamp;
  3858. uint16_t free_space;
  3859. struct get_sdr_repository_info_rsp sdr_repository_info;
  3860. if (ipmi_sdr_get_info(intf, &sdr_repository_info) != 0)
  3861. return -1;
  3862. printf("SDR Version : 0x%x\n",
  3863. sdr_repository_info.sdr_version);
  3864. printf("Record Count : %d\n",
  3865. (sdr_repository_info.record_count_msb << 8) |
  3866. sdr_repository_info.record_count_lsb);
  3867. free_space =
  3868. (sdr_repository_info.free_space[1] << 8) |
  3869. sdr_repository_info.free_space[0];
  3870. printf("Free Space : ");
  3871. switch (free_space) {
  3872. case 0x0000:
  3873. printf("none (full)\n");
  3874. break;
  3875. case 0xFFFF:
  3876. printf("unspecified\n");
  3877. break;
  3878. case 0xFFFE:
  3879. printf("> 64Kb - 2 bytes\n");
  3880. break;
  3881. default:
  3882. printf("%d bytes\n", free_space);
  3883. break;
  3884. }
  3885. timestamp =
  3886. (sdr_repository_info.most_recent_addition_timestamp[3] << 24) |
  3887. (sdr_repository_info.most_recent_addition_timestamp[2] << 16) |
  3888. (sdr_repository_info.most_recent_addition_timestamp[1] << 8) |
  3889. sdr_repository_info.most_recent_addition_timestamp[0];
  3890. printf("Most recent Addition : %s\n",
  3891. ipmi_sdr_timestamp(timestamp));
  3892. timestamp =
  3893. (sdr_repository_info.most_recent_erase_timestamp[3] << 24) |
  3894. (sdr_repository_info.most_recent_erase_timestamp[2] << 16) |
  3895. (sdr_repository_info.most_recent_erase_timestamp[1] << 8) |
  3896. sdr_repository_info.most_recent_erase_timestamp[0];
  3897. printf("Most recent Erase : %s\n",
  3898. ipmi_sdr_timestamp(timestamp));
  3899. printf("SDR overflow : %s\n",
  3900. (sdr_repository_info.overflow_flag ? "yes" : "no"));
  3901. printf("SDR Repository Update Support : ");
  3902. switch (sdr_repository_info.modal_update_support) {
  3903. case 0:
  3904. printf("unspecified\n");
  3905. break;
  3906. case 1:
  3907. printf("non-modal\n");
  3908. break;
  3909. case 2:
  3910. printf("modal\n");
  3911. break;
  3912. case 3:
  3913. printf("modal and non-modal\n");
  3914. break;
  3915. default:
  3916. printf("error in response\n");
  3917. break;
  3918. }
  3919. printf("Delete SDR supported : %s\n",
  3920. sdr_repository_info.delete_sdr_supported ? "yes" : "no");
  3921. printf("Partial Add SDR supported : %s\n",
  3922. sdr_repository_info.partial_add_sdr_supported ? "yes" : "no");
  3923. printf("Reserve SDR repository supported : %s\n",
  3924. sdr_repository_info.
  3925. reserve_sdr_repository_supported ? "yes" : "no");
  3926. printf("SDR Repository Alloc info supported : %s\n",
  3927. sdr_repository_info.
  3928. get_sdr_repository_allo_info_supported ? "yes" : "no");
  3929. return 0;
  3930. }
  3931. /* ipmi_sdr_dump_bin - Write raw SDR to binary file
  3932. *
  3933. * used for post-processing by other utilities
  3934. *
  3935. * @intf: ipmi interface
  3936. * @ofile: output filename
  3937. *
  3938. * returns 0 on success
  3939. * returns -1 on error
  3940. */
  3941. static int
  3942. ipmi_sdr_dump_bin(struct ipmi_intf *intf, const char *ofile)
  3943. {
  3944. struct sdr_get_rs *header;
  3945. struct ipmi_sdr_iterator *itr;
  3946. struct sdr_record_list *sdrr;
  3947. FILE *fp;
  3948. int rc = 0;
  3949. /* open connection to SDR */
  3950. itr = ipmi_sdr_start(intf, 0);
  3951. if (itr == NULL) {
  3952. printf( "Unable to open SDR for reading");
  3953. return -1;
  3954. }
  3955. printf("Dumping Sensor Data Repository to '%s'\n", ofile);
  3956. /* generate list of records */
  3957. while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
  3958. sdrr = malloc(sizeof(struct sdr_record_list));
  3959. if (sdrr == NULL) {
  3960. printf( "ipmitool: malloc failure");
  3961. return -1;
  3962. }
  3963. memset(sdrr, 0, sizeof(struct sdr_record_list));
  3964. printf( "Record ID %04x (%d bytes)",
  3965. header->id, header->length);
  3966. sdrr->id = header->id;
  3967. sdrr->version = header->version;
  3968. sdrr->type = header->type;
  3969. sdrr->length = header->length;
  3970. sdrr->raw = ipmi_sdr_get_record(intf, header, itr);
  3971. if (sdrr->raw == NULL) {
  3972. printf( "ipmitool: cannot obtain SDR record %04x", header->id);
  3973. if (sdrr != NULL) {
  3974. free(sdrr);
  3975. sdrr = NULL;
  3976. }
  3977. return -1;
  3978. }
  3979. if (sdr_list_head == NULL)
  3980. sdr_list_head = sdrr;
  3981. else
  3982. sdr_list_tail->next = sdrr;
  3983. sdr_list_tail = sdrr;
  3984. }
  3985. ipmi_sdr_end(intf, itr);
  3986. /* now write to file */
  3987. fp = ipmi_open_file_write(ofile);
  3988. if (fp == NULL)
  3989. return -1;
  3990. for (sdrr = sdr_list_head; sdrr != NULL; sdrr = sdrr->next) {
  3991. int r;
  3992. uint8_t h[5];
  3993. /* build and write sdr header */
  3994. h[0] = sdrr->id & 0xff; // LS Byte first
  3995. h[1] = (sdrr->id >> 8) & 0xff;
  3996. h[2] = sdrr->version;
  3997. h[3] = sdrr->type;
  3998. h[4] = sdrr->length;
  3999. r = fwrite(h, 1, 5, fp);
  4000. if (r != 5) {
  4001. printf( "Error writing header "
  4002. "to output file %s", ofile);
  4003. rc = -1;
  4004. break;
  4005. }
  4006. /* write sdr entry */
  4007. if (!sdrr->raw) {
  4008. printf( "Error: raw data is null (length=%d)",
  4009. sdrr->length);
  4010. rc = -1;
  4011. break;
  4012. }
  4013. r = fwrite(sdrr->raw, 1, sdrr->length, fp);
  4014. if (r != sdrr->length) {
  4015. printf( "Error writing %d record bytes "
  4016. "to output file %s", sdrr->length, ofile);
  4017. rc = -1;
  4018. break;
  4019. }
  4020. }
  4021. fclose(fp);
  4022. return rc;
  4023. }
  4024. /* ipmi_sdr_print_type - print all sensors of specified type
  4025. *
  4026. * @intf: ipmi interface
  4027. * @type: sensor type
  4028. *
  4029. * returns 0 on success
  4030. * returns -1 on error
  4031. */
  4032. int
  4033. ipmi_sdr_print_type(struct ipmi_intf *intf, char *type)
  4034. {
  4035. struct sdr_record_list *list, *entry;
  4036. int rc = 0;
  4037. int x;
  4038. uint8_t sensor_type = 0;
  4039. if (type == NULL ||
  4040. strncasecmp(type, "help", 4) == 0 ||
  4041. strncasecmp(type, "list", 4) == 0) {
  4042. printf("Sensor Types:\n");
  4043. for (x = 1; x < SENSOR_TYPE_MAX; x += 2) {
  4044. printf("\t%-25s (0x%02x) %-25s (0x%02x)\n",
  4045. sensor_type_desc[x], x,
  4046. sensor_type_desc[x + 1], x + 1);
  4047. }
  4048. return 0;
  4049. }
  4050. if (strncmp(type, "0x", 2) == 0) {
  4051. /* begins with 0x so let it be entered as raw hex value */
  4052. if (str2uchar(type, &sensor_type) != 0) {
  4053. printf(
  4054. "Given type of sensor \"%s\" is either invalid or out of range.",
  4055. type);
  4056. return (-1);
  4057. }
  4058. } else {
  4059. for (x = 1; x < SENSOR_TYPE_MAX; x++) {
  4060. if (strncasecmp(sensor_type_desc[x], type,
  4061. __maxlen(type,
  4062. sensor_type_desc[x])) == 0) {
  4063. sensor_type = x;
  4064. break;
  4065. }
  4066. }
  4067. if (sensor_type != x) {
  4068. printf( "Sensor Type \"%s\" not found.",
  4069. type);
  4070. printf("Sensor Types:\n");
  4071. for (x = 1; x < SENSOR_TYPE_MAX; x += 2) {
  4072. printf("\t%-25s (0x%02x) %-25s (0x%02x)\n",
  4073. sensor_type_desc[x], x,
  4074. sensor_type_desc[x + 1], x + 1);
  4075. }
  4076. return 0;
  4077. }
  4078. }
  4079. list = ipmi_sdr_find_sdr_bysensortype(intf, sensor_type);
  4080. for (entry = list; entry != NULL; entry = entry->next) {
  4081. rc = ipmi_sdr_print_listentry(intf, entry);
  4082. }
  4083. __sdr_list_empty(list);
  4084. return rc;
  4085. }
  4086. /* ipmi_sdr_print_entity - print entity's for an id/instance
  4087. *
  4088. * @intf: ipmi interface
  4089. * @entitystr: entity id/instance string, i.e. "1.1"
  4090. *
  4091. * returns 0 on success
  4092. * returns -1 on error
  4093. */
  4094. int
  4095. ipmi_sdr_print_entity(struct ipmi_intf *intf, char *entitystr)
  4096. {
  4097. struct sdr_record_list *list, *entry;
  4098. struct entity_id entity;
  4099. unsigned id = 0;
  4100. unsigned instance = 0;
  4101. int rc = 0;
  4102. if (entitystr == NULL ||
  4103. strncasecmp(entitystr, "help", 4) == 0 ||
  4104. strncasecmp(entitystr, "list", 4) == 0) {
  4105. print_valstr_2col(entity_id_vals, "Entity IDs", -1);
  4106. return 0;
  4107. }
  4108. if (sscanf(entitystr, "%u.%u", &id, &instance) != 2) {
  4109. /* perhaps no instance was passed
  4110. * in which case we want all instances for this entity
  4111. * so set entity.instance = 0x7f to indicate this
  4112. */
  4113. if (sscanf(entitystr, "%u", &id) != 1) {
  4114. int i, j=0;
  4115. /* now try string input */
  4116. for (i = 0; entity_id_vals[i].str != NULL; i++) {
  4117. if (strncasecmp(entitystr, entity_id_vals[i].str,
  4118. __maxlen(entitystr, entity_id_vals[i].str)) == 0) {
  4119. entity.id = entity_id_vals[i].val;
  4120. entity.instance = 0x7f;
  4121. j=1;
  4122. }
  4123. }
  4124. if (j == 0) {
  4125. printf( "Invalid entity: %s", entitystr);
  4126. return -1;
  4127. }
  4128. } else {
  4129. entity.id = id;
  4130. entity.instance = 0x7f;
  4131. }
  4132. } else {
  4133. entity.id = id;
  4134. entity.instance = instance;
  4135. }
  4136. list = ipmi_sdr_find_sdr_byentity(intf, &entity);
  4137. for (entry = list; entry != NULL; entry = entry->next) {
  4138. rc = ipmi_sdr_print_listentry(intf, entry);
  4139. }
  4140. __sdr_list_empty(list);
  4141. return rc;
  4142. }
  4143. /* ipmi_sdr_print_entry_byid - print sdr entries identified by sensor id
  4144. *
  4145. * @intf: ipmi interface
  4146. * @argc: number of entries to print
  4147. * @argv: list of sensor ids
  4148. *
  4149. * returns 0 on success
  4150. * returns -1 on error
  4151. */
  4152. static int
  4153. ipmi_sdr_print_entry_byid(struct ipmi_intf *intf, int argc, char **argv)
  4154. {
  4155. struct sdr_record_list *sdr;
  4156. int rc = 0;
  4157. int v, i;
  4158. if (argc < 1) {
  4159. printf( "No Sensor ID supplied");
  4160. return -1;
  4161. }
  4162. v = verbose;
  4163. verbose = 1;
  4164. for (i = 0; i < argc; i++) {
  4165. sdr = ipmi_sdr_find_sdr_byid(intf, argv[i]);
  4166. if (sdr == NULL) {
  4167. printf( "Unable to find sensor id '%s'",
  4168. argv[i]);
  4169. } else {
  4170. if (ipmi_sdr_print_listentry(intf, sdr) < 0)
  4171. rc = -1;
  4172. }
  4173. }
  4174. verbose = v;
  4175. return rc;
  4176. }
  4177. /* ipmi_sdr_main - top-level handler for SDR subsystem
  4178. *
  4179. * @intf: ipmi interface
  4180. * @argc: number of arguments
  4181. * @argv: argument list
  4182. *
  4183. * returns 0 on success
  4184. * returns -1 on error
  4185. */
  4186. int
  4187. ipmi_sdr_main(struct ipmi_intf *intf, int argc, char **argv)
  4188. {
  4189. int rc = 0;
  4190. /* initialize random numbers used later */
  4191. srand(time(NULL));
  4192. if (argc == 0)
  4193. return ipmi_sdr_print_sdr(intf, 0xfe);
  4194. else if (strncmp(argv[0], "help", 4) == 0) {
  4195. printf_sdr_usage();
  4196. } else if (strncmp(argv[0], "list", 4) == 0
  4197. || strncmp(argv[0], "elist", 5) == 0) {
  4198. if (strncmp(argv[0], "elist", 5) == 0)
  4199. sdr_extended = 1;
  4200. else
  4201. sdr_extended = 0;
  4202. if (argc <= 1)
  4203. rc = ipmi_sdr_print_sdr(intf, 0xfe);
  4204. else if (strncmp(argv[1], "all", 3) == 0)
  4205. rc = ipmi_sdr_print_sdr(intf, 0xff);
  4206. else if (strncmp(argv[1], "full", 4) == 0)
  4207. rc = ipmi_sdr_print_sdr(intf,
  4208. SDR_RECORD_TYPE_FULL_SENSOR);
  4209. else if (strncmp(argv[1], "compact", 7) == 0)
  4210. rc = ipmi_sdr_print_sdr(intf,
  4211. SDR_RECORD_TYPE_COMPACT_SENSOR);
  4212. else if (strncmp(argv[1], "event", 5) == 0)
  4213. rc = ipmi_sdr_print_sdr(intf,
  4214. SDR_RECORD_TYPE_EVENTONLY_SENSOR);
  4215. else if (strncmp(argv[1], "mcloc", 5) == 0)
  4216. rc = ipmi_sdr_print_sdr(intf,
  4217. SDR_RECORD_TYPE_MC_DEVICE_LOCATOR);
  4218. else if (strncmp(argv[1], "fru", 3) == 0)
  4219. rc = ipmi_sdr_print_sdr(intf,
  4220. SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR);
  4221. else if (strncmp(argv[1], "generic", 7) == 0)
  4222. rc = ipmi_sdr_print_sdr(intf,
  4223. SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR);
  4224. else if (strcmp(argv[1], "help") == 0) {
  4225. printf( "usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
  4226. argv[0]);
  4227. return 0;
  4228. }
  4229. else {
  4230. printf( "Invalid SDR %s command: %s",
  4231. argv[0], argv[1]);
  4232. printf( "usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
  4233. argv[0]);
  4234. return (-1);
  4235. }
  4236. } else if (strncmp(argv[0], "type", 4) == 0) {
  4237. sdr_extended = 1;
  4238. rc = ipmi_sdr_print_type(intf, argv[1]);
  4239. } else if (strncmp(argv[0], "entity", 6) == 0) {
  4240. sdr_extended = 1;
  4241. rc = ipmi_sdr_print_entity(intf, argv[1]);
  4242. } else if (strncmp(argv[0], "info", 4) == 0) {
  4243. rc = ipmi_sdr_print_info(intf);
  4244. } else if (strncmp(argv[0], "get", 3) == 0) {
  4245. rc = ipmi_sdr_print_entry_byid(intf, argc - 1, &argv[1]);
  4246. } else if (strncmp(argv[0], "dump", 4) == 0) {
  4247. if (argc < 2) {
  4248. printf( "Not enough parameters given.");
  4249. printf( "usage: sdr dump <file>");
  4250. return (-1);
  4251. }
  4252. rc = ipmi_sdr_dump_bin(intf, argv[1]);
  4253. } else if (strncmp(argv[0], "fill", 4) == 0) {
  4254. if (argc <= 1) {
  4255. printf( "Not enough parameters given.");
  4256. printf( "usage: sdr fill sensors");
  4257. printf( "usage: sdr fill file <file>");
  4258. printf( "usage: sdr fill range <range>");
  4259. return (-1);
  4260. } else if (strncmp(argv[1], "sensors", 7) == 0) {
  4261. rc = ipmi_sdr_add_from_sensors(intf, 21);
  4262. } else if (strncmp(argv[1], "nosat", 5) == 0) {
  4263. rc = ipmi_sdr_add_from_sensors(intf, 0);
  4264. } else if (strncmp(argv[1], "file", 4) == 0) {
  4265. if (argc < 3) {
  4266. printf( "Not enough parameters given.");
  4267. printf( "usage: sdr fill file <file>");
  4268. return (-1);
  4269. }
  4270. rc = ipmi_sdr_add_from_file(intf, argv[2]);
  4271. } else if (strncmp(argv[1], "range", 4) == 0) {
  4272. if (argc < 3) {
  4273. printf( "Not enough parameters given.");
  4274. printf( "usage: sdr fill range <range>");
  4275. return (-1);
  4276. }
  4277. rc = ipmi_sdr_add_from_list(intf, argv[2]);
  4278. } else {
  4279. printf("Invalid SDR %s command: %s",
  4280. argv[0], argv[1]);
  4281. printf( "usage: sdr %s <sensors|nosat|file|range> [options]",
  4282. argv[0]);
  4283. return (-1);
  4284. }
  4285. } else {
  4286. printf( "Invalid SDR command: %s", argv[0]);
  4287. rc = -1;
  4288. }
  4289. return rc;
  4290. }
  4291. void
  4292. printf_sdr_usage()
  4293. {
  4294. printf( "usage: sdr <command> [options]");
  4295. printf(" list | elist [option]");
  4296. printf(" all All SDR Records");
  4297. printf(" full Full Sensor Record");
  4298. printf(" compact Compact Sensor Record");
  4299. printf(" event Event-Only Sensor Record");
  4300. printf(" mcloc Management Controller Locator Record");
  4301. printf(" fru FRU Locator Record");
  4302. printf(" generic Generic Device Locator Record\n");
  4303. printf(" type [option]");
  4304. printf(" <Sensor_Type> Retrieve the state of specified sensor.");
  4305. printf(" Sensor_Type can be specified either as");
  4306. printf(" a string or a hex value.");
  4307. printf(" list Get a list of available sensor types\n");
  4308. printf(" get <Sensor_ID>");
  4309. printf(" Retrieve state of the first sensor matched by Sensor_ID\n");
  4310. printf(" info");
  4311. printf(" Display information about the repository itself\n");
  4312. printf(" entity <Entity_ID>[.<Instance_ID>]");
  4313. printf(" Display all sensors associated with an entity\n");
  4314. printf(" dump <file>");
  4315. printf(" Dump raw SDR data to a file\n");
  4316. printf(" fill <option>");
  4317. printf(" sensors Creates the SDR repository for the current");
  4318. printf(" configuration");
  4319. printf(" nosat Creates the SDR repository for the current");
  4320. printf(" configuration, without satellite scan");
  4321. printf(" file <file> Load SDR repository from a file");
  4322. printf(" range <range> Load SDR repository from a provided list");
  4323. printf(" or range. Use ',' for list or '-' for");
  4324. printf(" range, eg. 0x28,0x32,0x40-0x44");
  4325. }