ipmi_msghandler.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548
  1. /*
  2. * ipmi_msghandler.c
  3. *
  4. * Incoming and outgoing message routing for an IPMI interface.
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * Corey Minyard <minyard@mvista.com>
  8. * source@mvista.com
  9. *
  10. * Copyright 2002 MontaVista Software Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  27. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/errno.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/mutex.h>
  40. #include <linux/slab.h>
  41. #include <linux/ipmi.h>
  42. #include <linux/ipmi_smi.h>
  43. #include <linux/notifier.h>
  44. #include <linux/init.h>
  45. #include <linux/proc_fs.h>
  46. #include <linux/rcupdate.h>
  47. #define PFX "IPMI message handler: "
  48. #define IPMI_DRIVER_VERSION "39.2"
  49. static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
  50. static int ipmi_init_msghandler(void);
  51. static int initialized;
  52. #ifdef CONFIG_PROC_FS
  53. static struct proc_dir_entry *proc_ipmi_root;
  54. #endif /* CONFIG_PROC_FS */
  55. /* Remain in auto-maintenance mode for this amount of time (in ms). */
  56. #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
  57. #define MAX_EVENTS_IN_QUEUE 25
  58. /*
  59. * Don't let a message sit in a queue forever, always time it with at lest
  60. * the max message timer. This is in milliseconds.
  61. */
  62. #define MAX_MSG_TIMEOUT 60000
  63. /*
  64. * The main "user" data structure.
  65. */
  66. struct ipmi_user {
  67. struct list_head link;
  68. /* Set to "0" when the user is destroyed. */
  69. int valid;
  70. struct kref refcount;
  71. /* The upper layer that handles receive messages. */
  72. struct ipmi_user_hndl *handler;
  73. void *handler_data;
  74. /* The interface this user is bound to. */
  75. ipmi_smi_t intf;
  76. /* Does this interface receive IPMI events? */
  77. int gets_events;
  78. };
  79. struct cmd_rcvr {
  80. struct list_head link;
  81. ipmi_user_t user;
  82. unsigned char netfn;
  83. unsigned char cmd;
  84. unsigned int chans;
  85. /*
  86. * This is used to form a linked lised during mass deletion.
  87. * Since this is in an RCU list, we cannot use the link above
  88. * or change any data until the RCU period completes. So we
  89. * use this next variable during mass deletion so we can have
  90. * a list and don't have to wait and restart the search on
  91. * every individual deletion of a command.
  92. */
  93. struct cmd_rcvr *next;
  94. };
  95. struct seq_table {
  96. unsigned int inuse : 1;
  97. unsigned int broadcast : 1;
  98. unsigned long timeout;
  99. unsigned long orig_timeout;
  100. unsigned int retries_left;
  101. /*
  102. * To verify on an incoming send message response that this is
  103. * the message that the response is for, we keep a sequence id
  104. * and increment it every time we send a message.
  105. */
  106. long seqid;
  107. /*
  108. * This is held so we can properly respond to the message on a
  109. * timeout, and it is used to hold the temporary data for
  110. * retransmission, too.
  111. */
  112. struct ipmi_recv_msg *recv_msg;
  113. };
  114. /*
  115. * Store the information in a msgid (long) to allow us to find a
  116. * sequence table entry from the msgid.
  117. */
  118. #define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
  119. #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
  120. do { \
  121. seq = ((msgid >> 26) & 0x3f); \
  122. seqid = (msgid & 0x3fffff); \
  123. } while (0)
  124. #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
  125. struct ipmi_channel {
  126. unsigned char medium;
  127. unsigned char protocol;
  128. /*
  129. * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
  130. * but may be changed by the user.
  131. */
  132. unsigned char address;
  133. /*
  134. * My LUN. This should generally stay the SMS LUN, but just in
  135. * case...
  136. */
  137. unsigned char lun;
  138. };
  139. #ifdef CONFIG_PROC_FS
  140. struct ipmi_proc_entry {
  141. char *name;
  142. struct ipmi_proc_entry *next;
  143. };
  144. #endif
  145. struct bmc_device {
  146. struct platform_device *dev;
  147. struct ipmi_device_id id;
  148. unsigned char guid[16];
  149. int guid_set;
  150. struct kref refcount;
  151. /* bmc device attributes */
  152. struct device_attribute device_id_attr;
  153. struct device_attribute provides_dev_sdrs_attr;
  154. struct device_attribute revision_attr;
  155. struct device_attribute firmware_rev_attr;
  156. struct device_attribute version_attr;
  157. struct device_attribute add_dev_support_attr;
  158. struct device_attribute manufacturer_id_attr;
  159. struct device_attribute product_id_attr;
  160. struct device_attribute guid_attr;
  161. struct device_attribute aux_firmware_rev_attr;
  162. };
  163. /*
  164. * Various statistics for IPMI, these index stats[] in the ipmi_smi
  165. * structure.
  166. */
  167. enum ipmi_stat_indexes {
  168. /* Commands we got from the user that were invalid. */
  169. IPMI_STAT_sent_invalid_commands = 0,
  170. /* Commands we sent to the MC. */
  171. IPMI_STAT_sent_local_commands,
  172. /* Responses from the MC that were delivered to a user. */
  173. IPMI_STAT_handled_local_responses,
  174. /* Responses from the MC that were not delivered to a user. */
  175. IPMI_STAT_unhandled_local_responses,
  176. /* Commands we sent out to the IPMB bus. */
  177. IPMI_STAT_sent_ipmb_commands,
  178. /* Commands sent on the IPMB that had errors on the SEND CMD */
  179. IPMI_STAT_sent_ipmb_command_errs,
  180. /* Each retransmit increments this count. */
  181. IPMI_STAT_retransmitted_ipmb_commands,
  182. /*
  183. * When a message times out (runs out of retransmits) this is
  184. * incremented.
  185. */
  186. IPMI_STAT_timed_out_ipmb_commands,
  187. /*
  188. * This is like above, but for broadcasts. Broadcasts are
  189. * *not* included in the above count (they are expected to
  190. * time out).
  191. */
  192. IPMI_STAT_timed_out_ipmb_broadcasts,
  193. /* Responses I have sent to the IPMB bus. */
  194. IPMI_STAT_sent_ipmb_responses,
  195. /* The response was delivered to the user. */
  196. IPMI_STAT_handled_ipmb_responses,
  197. /* The response had invalid data in it. */
  198. IPMI_STAT_invalid_ipmb_responses,
  199. /* The response didn't have anyone waiting for it. */
  200. IPMI_STAT_unhandled_ipmb_responses,
  201. /* Commands we sent out to the IPMB bus. */
  202. IPMI_STAT_sent_lan_commands,
  203. /* Commands sent on the IPMB that had errors on the SEND CMD */
  204. IPMI_STAT_sent_lan_command_errs,
  205. /* Each retransmit increments this count. */
  206. IPMI_STAT_retransmitted_lan_commands,
  207. /*
  208. * When a message times out (runs out of retransmits) this is
  209. * incremented.
  210. */
  211. IPMI_STAT_timed_out_lan_commands,
  212. /* Responses I have sent to the IPMB bus. */
  213. IPMI_STAT_sent_lan_responses,
  214. /* The response was delivered to the user. */
  215. IPMI_STAT_handled_lan_responses,
  216. /* The response had invalid data in it. */
  217. IPMI_STAT_invalid_lan_responses,
  218. /* The response didn't have anyone waiting for it. */
  219. IPMI_STAT_unhandled_lan_responses,
  220. /* The command was delivered to the user. */
  221. IPMI_STAT_handled_commands,
  222. /* The command had invalid data in it. */
  223. IPMI_STAT_invalid_commands,
  224. /* The command didn't have anyone waiting for it. */
  225. IPMI_STAT_unhandled_commands,
  226. /* Invalid data in an event. */
  227. IPMI_STAT_invalid_events,
  228. /* Events that were received with the proper format. */
  229. IPMI_STAT_events,
  230. /* Retransmissions on IPMB that failed. */
  231. IPMI_STAT_dropped_rexmit_ipmb_commands,
  232. /* Retransmissions on LAN that failed. */
  233. IPMI_STAT_dropped_rexmit_lan_commands,
  234. /* This *must* remain last, add new values above this. */
  235. IPMI_NUM_STATS
  236. };
  237. #define IPMI_IPMB_NUM_SEQ 64
  238. #define IPMI_MAX_CHANNELS 16
  239. struct ipmi_smi {
  240. /* What interface number are we? */
  241. int intf_num;
  242. struct kref refcount;
  243. /* Used for a list of interfaces. */
  244. struct list_head link;
  245. /*
  246. * The list of upper layers that are using me. seq_lock
  247. * protects this.
  248. */
  249. struct list_head users;
  250. /* Information to supply to users. */
  251. unsigned char ipmi_version_major;
  252. unsigned char ipmi_version_minor;
  253. /* Used for wake ups at startup. */
  254. wait_queue_head_t waitq;
  255. struct bmc_device *bmc;
  256. char *my_dev_name;
  257. char *sysfs_name;
  258. /*
  259. * This is the lower-layer's sender routine. Note that you
  260. * must either be holding the ipmi_interfaces_mutex or be in
  261. * an umpreemptible region to use this. You must fetch the
  262. * value into a local variable and make sure it is not NULL.
  263. */
  264. struct ipmi_smi_handlers *handlers;
  265. void *send_info;
  266. #ifdef CONFIG_PROC_FS
  267. /* A list of proc entries for this interface. */
  268. struct mutex proc_entry_lock;
  269. struct ipmi_proc_entry *proc_entries;
  270. #endif
  271. /* Driver-model device for the system interface. */
  272. struct device *si_dev;
  273. /*
  274. * A table of sequence numbers for this interface. We use the
  275. * sequence numbers for IPMB messages that go out of the
  276. * interface to match them up with their responses. A routine
  277. * is called periodically to time the items in this list.
  278. */
  279. spinlock_t seq_lock;
  280. struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
  281. int curr_seq;
  282. /*
  283. * Messages that were delayed for some reason (out of memory,
  284. * for instance), will go in here to be processed later in a
  285. * periodic timer interrupt.
  286. */
  287. spinlock_t waiting_msgs_lock;
  288. struct list_head waiting_msgs;
  289. /*
  290. * The list of command receivers that are registered for commands
  291. * on this interface.
  292. */
  293. struct mutex cmd_rcvrs_mutex;
  294. struct list_head cmd_rcvrs;
  295. /*
  296. * Events that were queues because no one was there to receive
  297. * them.
  298. */
  299. spinlock_t events_lock; /* For dealing with event stuff. */
  300. struct list_head waiting_events;
  301. unsigned int waiting_events_count; /* How many events in queue? */
  302. char delivering_events;
  303. char event_msg_printed;
  304. /*
  305. * The event receiver for my BMC, only really used at panic
  306. * shutdown as a place to store this.
  307. */
  308. unsigned char event_receiver;
  309. unsigned char event_receiver_lun;
  310. unsigned char local_sel_device;
  311. unsigned char local_event_generator;
  312. /* For handling of maintenance mode. */
  313. int maintenance_mode;
  314. int maintenance_mode_enable;
  315. int auto_maintenance_timeout;
  316. spinlock_t maintenance_mode_lock; /* Used in a timer... */
  317. /*
  318. * A cheap hack, if this is non-null and a message to an
  319. * interface comes in with a NULL user, call this routine with
  320. * it. Note that the message will still be freed by the
  321. * caller. This only works on the system interface.
  322. */
  323. void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
  324. /*
  325. * When we are scanning the channels for an SMI, this will
  326. * tell which channel we are scanning.
  327. */
  328. int curr_channel;
  329. /* Channel information */
  330. struct ipmi_channel channels[IPMI_MAX_CHANNELS];
  331. /* Proc FS stuff. */
  332. struct proc_dir_entry *proc_dir;
  333. char proc_dir_name[10];
  334. atomic_t stats[IPMI_NUM_STATS];
  335. /*
  336. * run_to_completion duplicate of smb_info, smi_info
  337. * and ipmi_serial_info structures. Used to decrease numbers of
  338. * parameters passed by "low" level IPMI code.
  339. */
  340. int run_to_completion;
  341. };
  342. #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
  343. /**
  344. * The driver model view of the IPMI messaging driver.
  345. */
  346. static struct platform_driver ipmidriver = {
  347. .driver = {
  348. .name = "ipmi",
  349. .bus = &platform_bus_type
  350. }
  351. };
  352. static DEFINE_MUTEX(ipmidriver_mutex);
  353. static LIST_HEAD(ipmi_interfaces);
  354. static DEFINE_MUTEX(ipmi_interfaces_mutex);
  355. /*
  356. * List of watchers that want to know when smi's are added and deleted.
  357. */
  358. static LIST_HEAD(smi_watchers);
  359. static DEFINE_MUTEX(smi_watchers_mutex);
  360. #define ipmi_inc_stat(intf, stat) \
  361. atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
  362. #define ipmi_get_stat(intf, stat) \
  363. ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
  364. static int is_lan_addr(struct ipmi_addr *addr)
  365. {
  366. return addr->addr_type == IPMI_LAN_ADDR_TYPE;
  367. }
  368. static int is_ipmb_addr(struct ipmi_addr *addr)
  369. {
  370. return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
  371. }
  372. static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
  373. {
  374. return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
  375. }
  376. static void free_recv_msg_list(struct list_head *q)
  377. {
  378. struct ipmi_recv_msg *msg, *msg2;
  379. list_for_each_entry_safe(msg, msg2, q, link) {
  380. list_del(&msg->link);
  381. ipmi_free_recv_msg(msg);
  382. }
  383. }
  384. static void free_smi_msg_list(struct list_head *q)
  385. {
  386. struct ipmi_smi_msg *msg, *msg2;
  387. list_for_each_entry_safe(msg, msg2, q, link) {
  388. list_del(&msg->link);
  389. ipmi_free_smi_msg(msg);
  390. }
  391. }
  392. static void clean_up_interface_data(ipmi_smi_t intf)
  393. {
  394. int i;
  395. struct cmd_rcvr *rcvr, *rcvr2;
  396. struct list_head list;
  397. free_smi_msg_list(&intf->waiting_msgs);
  398. free_recv_msg_list(&intf->waiting_events);
  399. /*
  400. * Wholesale remove all the entries from the list in the
  401. * interface and wait for RCU to know that none are in use.
  402. */
  403. mutex_lock(&intf->cmd_rcvrs_mutex);
  404. INIT_LIST_HEAD(&list);
  405. list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
  406. mutex_unlock(&intf->cmd_rcvrs_mutex);
  407. list_for_each_entry_safe(rcvr, rcvr2, &list, link)
  408. kfree(rcvr);
  409. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  410. if ((intf->seq_table[i].inuse)
  411. && (intf->seq_table[i].recv_msg))
  412. ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
  413. }
  414. }
  415. static void intf_free(struct kref *ref)
  416. {
  417. ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
  418. clean_up_interface_data(intf);
  419. kfree(intf);
  420. }
  421. struct watcher_entry {
  422. int intf_num;
  423. ipmi_smi_t intf;
  424. struct list_head link;
  425. };
  426. int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
  427. {
  428. ipmi_smi_t intf;
  429. LIST_HEAD(to_deliver);
  430. struct watcher_entry *e, *e2;
  431. mutex_lock(&smi_watchers_mutex);
  432. mutex_lock(&ipmi_interfaces_mutex);
  433. /* Build a list of things to deliver. */
  434. list_for_each_entry(intf, &ipmi_interfaces, link) {
  435. if (intf->intf_num == -1)
  436. continue;
  437. e = kmalloc(sizeof(*e), GFP_KERNEL);
  438. if (!e)
  439. goto out_err;
  440. kref_get(&intf->refcount);
  441. e->intf = intf;
  442. e->intf_num = intf->intf_num;
  443. list_add_tail(&e->link, &to_deliver);
  444. }
  445. /* We will succeed, so add it to the list. */
  446. list_add(&watcher->link, &smi_watchers);
  447. mutex_unlock(&ipmi_interfaces_mutex);
  448. list_for_each_entry_safe(e, e2, &to_deliver, link) {
  449. list_del(&e->link);
  450. watcher->new_smi(e->intf_num, e->intf->si_dev);
  451. kref_put(&e->intf->refcount, intf_free);
  452. kfree(e);
  453. }
  454. mutex_unlock(&smi_watchers_mutex);
  455. return 0;
  456. out_err:
  457. mutex_unlock(&ipmi_interfaces_mutex);
  458. mutex_unlock(&smi_watchers_mutex);
  459. list_for_each_entry_safe(e, e2, &to_deliver, link) {
  460. list_del(&e->link);
  461. kref_put(&e->intf->refcount, intf_free);
  462. kfree(e);
  463. }
  464. return -ENOMEM;
  465. }
  466. EXPORT_SYMBOL(ipmi_smi_watcher_register);
  467. int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
  468. {
  469. mutex_lock(&smi_watchers_mutex);
  470. list_del(&(watcher->link));
  471. mutex_unlock(&smi_watchers_mutex);
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
  475. /*
  476. * Must be called with smi_watchers_mutex held.
  477. */
  478. static void
  479. call_smi_watchers(int i, struct device *dev)
  480. {
  481. struct ipmi_smi_watcher *w;
  482. list_for_each_entry(w, &smi_watchers, link) {
  483. if (try_module_get(w->owner)) {
  484. w->new_smi(i, dev);
  485. module_put(w->owner);
  486. }
  487. }
  488. }
  489. static int
  490. ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
  491. {
  492. if (addr1->addr_type != addr2->addr_type)
  493. return 0;
  494. if (addr1->channel != addr2->channel)
  495. return 0;
  496. if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  497. struct ipmi_system_interface_addr *smi_addr1
  498. = (struct ipmi_system_interface_addr *) addr1;
  499. struct ipmi_system_interface_addr *smi_addr2
  500. = (struct ipmi_system_interface_addr *) addr2;
  501. return (smi_addr1->lun == smi_addr2->lun);
  502. }
  503. if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
  504. struct ipmi_ipmb_addr *ipmb_addr1
  505. = (struct ipmi_ipmb_addr *) addr1;
  506. struct ipmi_ipmb_addr *ipmb_addr2
  507. = (struct ipmi_ipmb_addr *) addr2;
  508. return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
  509. && (ipmb_addr1->lun == ipmb_addr2->lun));
  510. }
  511. if (is_lan_addr(addr1)) {
  512. struct ipmi_lan_addr *lan_addr1
  513. = (struct ipmi_lan_addr *) addr1;
  514. struct ipmi_lan_addr *lan_addr2
  515. = (struct ipmi_lan_addr *) addr2;
  516. return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
  517. && (lan_addr1->local_SWID == lan_addr2->local_SWID)
  518. && (lan_addr1->session_handle
  519. == lan_addr2->session_handle)
  520. && (lan_addr1->lun == lan_addr2->lun));
  521. }
  522. return 1;
  523. }
  524. int ipmi_validate_addr(struct ipmi_addr *addr, int len)
  525. {
  526. if (len < sizeof(struct ipmi_system_interface_addr))
  527. return -EINVAL;
  528. if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  529. if (addr->channel != IPMI_BMC_CHANNEL)
  530. return -EINVAL;
  531. return 0;
  532. }
  533. if ((addr->channel == IPMI_BMC_CHANNEL)
  534. || (addr->channel >= IPMI_MAX_CHANNELS)
  535. || (addr->channel < 0))
  536. return -EINVAL;
  537. if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
  538. if (len < sizeof(struct ipmi_ipmb_addr))
  539. return -EINVAL;
  540. return 0;
  541. }
  542. if (is_lan_addr(addr)) {
  543. if (len < sizeof(struct ipmi_lan_addr))
  544. return -EINVAL;
  545. return 0;
  546. }
  547. return -EINVAL;
  548. }
  549. EXPORT_SYMBOL(ipmi_validate_addr);
  550. unsigned int ipmi_addr_length(int addr_type)
  551. {
  552. if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  553. return sizeof(struct ipmi_system_interface_addr);
  554. if ((addr_type == IPMI_IPMB_ADDR_TYPE)
  555. || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
  556. return sizeof(struct ipmi_ipmb_addr);
  557. if (addr_type == IPMI_LAN_ADDR_TYPE)
  558. return sizeof(struct ipmi_lan_addr);
  559. return 0;
  560. }
  561. EXPORT_SYMBOL(ipmi_addr_length);
  562. static void deliver_response(struct ipmi_recv_msg *msg)
  563. {
  564. if (!msg->user) {
  565. ipmi_smi_t intf = msg->user_msg_data;
  566. /* Special handling for NULL users. */
  567. if (intf->null_user_handler) {
  568. intf->null_user_handler(intf, msg);
  569. ipmi_inc_stat(intf, handled_local_responses);
  570. } else {
  571. /* No handler, so give up. */
  572. ipmi_inc_stat(intf, unhandled_local_responses);
  573. }
  574. ipmi_free_recv_msg(msg);
  575. } else {
  576. ipmi_user_t user = msg->user;
  577. user->handler->ipmi_recv_hndl(msg, user->handler_data);
  578. }
  579. }
  580. static void
  581. deliver_err_response(struct ipmi_recv_msg *msg, int err)
  582. {
  583. msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  584. msg->msg_data[0] = err;
  585. msg->msg.netfn |= 1; /* Convert to a response. */
  586. msg->msg.data_len = 1;
  587. msg->msg.data = msg->msg_data;
  588. deliver_response(msg);
  589. }
  590. /*
  591. * Find the next sequence number not being used and add the given
  592. * message with the given timeout to the sequence table. This must be
  593. * called with the interface's seq_lock held.
  594. */
  595. static int intf_next_seq(ipmi_smi_t intf,
  596. struct ipmi_recv_msg *recv_msg,
  597. unsigned long timeout,
  598. int retries,
  599. int broadcast,
  600. unsigned char *seq,
  601. long *seqid)
  602. {
  603. int rv = 0;
  604. unsigned int i;
  605. for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
  606. i = (i+1)%IPMI_IPMB_NUM_SEQ) {
  607. if (!intf->seq_table[i].inuse)
  608. break;
  609. }
  610. if (!intf->seq_table[i].inuse) {
  611. intf->seq_table[i].recv_msg = recv_msg;
  612. /*
  613. * Start with the maximum timeout, when the send response
  614. * comes in we will start the real timer.
  615. */
  616. intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
  617. intf->seq_table[i].orig_timeout = timeout;
  618. intf->seq_table[i].retries_left = retries;
  619. intf->seq_table[i].broadcast = broadcast;
  620. intf->seq_table[i].inuse = 1;
  621. intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
  622. *seq = i;
  623. *seqid = intf->seq_table[i].seqid;
  624. intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
  625. } else {
  626. rv = -EAGAIN;
  627. }
  628. return rv;
  629. }
  630. /*
  631. * Return the receive message for the given sequence number and
  632. * release the sequence number so it can be reused. Some other data
  633. * is passed in to be sure the message matches up correctly (to help
  634. * guard against message coming in after their timeout and the
  635. * sequence number being reused).
  636. */
  637. static int intf_find_seq(ipmi_smi_t intf,
  638. unsigned char seq,
  639. short channel,
  640. unsigned char cmd,
  641. unsigned char netfn,
  642. struct ipmi_addr *addr,
  643. struct ipmi_recv_msg **recv_msg)
  644. {
  645. int rv = -ENODEV;
  646. unsigned long flags;
  647. if (seq >= IPMI_IPMB_NUM_SEQ)
  648. return -EINVAL;
  649. spin_lock_irqsave(&(intf->seq_lock), flags);
  650. if (intf->seq_table[seq].inuse) {
  651. struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
  652. if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
  653. && (msg->msg.netfn == netfn)
  654. && (ipmi_addr_equal(addr, &(msg->addr)))) {
  655. *recv_msg = msg;
  656. intf->seq_table[seq].inuse = 0;
  657. rv = 0;
  658. }
  659. }
  660. spin_unlock_irqrestore(&(intf->seq_lock), flags);
  661. return rv;
  662. }
  663. /* Start the timer for a specific sequence table entry. */
  664. static int intf_start_seq_timer(ipmi_smi_t intf,
  665. long msgid)
  666. {
  667. int rv = -ENODEV;
  668. unsigned long flags;
  669. unsigned char seq;
  670. unsigned long seqid;
  671. GET_SEQ_FROM_MSGID(msgid, seq, seqid);
  672. spin_lock_irqsave(&(intf->seq_lock), flags);
  673. /*
  674. * We do this verification because the user can be deleted
  675. * while a message is outstanding.
  676. */
  677. if ((intf->seq_table[seq].inuse)
  678. && (intf->seq_table[seq].seqid == seqid)) {
  679. struct seq_table *ent = &(intf->seq_table[seq]);
  680. ent->timeout = ent->orig_timeout;
  681. rv = 0;
  682. }
  683. spin_unlock_irqrestore(&(intf->seq_lock), flags);
  684. return rv;
  685. }
  686. /* Got an error for the send message for a specific sequence number. */
  687. static int intf_err_seq(ipmi_smi_t intf,
  688. long msgid,
  689. unsigned int err)
  690. {
  691. int rv = -ENODEV;
  692. unsigned long flags;
  693. unsigned char seq;
  694. unsigned long seqid;
  695. struct ipmi_recv_msg *msg = NULL;
  696. GET_SEQ_FROM_MSGID(msgid, seq, seqid);
  697. spin_lock_irqsave(&(intf->seq_lock), flags);
  698. /*
  699. * We do this verification because the user can be deleted
  700. * while a message is outstanding.
  701. */
  702. if ((intf->seq_table[seq].inuse)
  703. && (intf->seq_table[seq].seqid == seqid)) {
  704. struct seq_table *ent = &(intf->seq_table[seq]);
  705. ent->inuse = 0;
  706. msg = ent->recv_msg;
  707. rv = 0;
  708. }
  709. spin_unlock_irqrestore(&(intf->seq_lock), flags);
  710. if (msg)
  711. deliver_err_response(msg, err);
  712. return rv;
  713. }
  714. int ipmi_create_user(unsigned int if_num,
  715. struct ipmi_user_hndl *handler,
  716. void *handler_data,
  717. ipmi_user_t *user)
  718. {
  719. unsigned long flags;
  720. ipmi_user_t new_user;
  721. int rv = 0;
  722. ipmi_smi_t intf;
  723. /*
  724. * There is no module usecount here, because it's not
  725. * required. Since this can only be used by and called from
  726. * other modules, they will implicitly use this module, and
  727. * thus this can't be removed unless the other modules are
  728. * removed.
  729. */
  730. if (handler == NULL)
  731. return -EINVAL;
  732. /*
  733. * Make sure the driver is actually initialized, this handles
  734. * problems with initialization order.
  735. */
  736. if (!initialized) {
  737. rv = ipmi_init_msghandler();
  738. if (rv)
  739. return rv;
  740. /*
  741. * The init code doesn't return an error if it was turned
  742. * off, but it won't initialize. Check that.
  743. */
  744. if (!initialized)
  745. return -ENODEV;
  746. }
  747. new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
  748. if (!new_user)
  749. return -ENOMEM;
  750. mutex_lock(&ipmi_interfaces_mutex);
  751. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  752. if (intf->intf_num == if_num)
  753. goto found;
  754. }
  755. /* Not found, return an error */
  756. rv = -EINVAL;
  757. goto out_kfree;
  758. found:
  759. /* Note that each existing user holds a refcount to the interface. */
  760. kref_get(&intf->refcount);
  761. kref_init(&new_user->refcount);
  762. new_user->handler = handler;
  763. new_user->handler_data = handler_data;
  764. new_user->intf = intf;
  765. new_user->gets_events = 0;
  766. if (!try_module_get(intf->handlers->owner)) {
  767. rv = -ENODEV;
  768. goto out_kref;
  769. }
  770. if (intf->handlers->inc_usecount) {
  771. rv = intf->handlers->inc_usecount(intf->send_info);
  772. if (rv) {
  773. module_put(intf->handlers->owner);
  774. goto out_kref;
  775. }
  776. }
  777. /*
  778. * Hold the lock so intf->handlers is guaranteed to be good
  779. * until now
  780. */
  781. mutex_unlock(&ipmi_interfaces_mutex);
  782. new_user->valid = 1;
  783. spin_lock_irqsave(&intf->seq_lock, flags);
  784. list_add_rcu(&new_user->link, &intf->users);
  785. spin_unlock_irqrestore(&intf->seq_lock, flags);
  786. *user = new_user;
  787. return 0;
  788. out_kref:
  789. kref_put(&intf->refcount, intf_free);
  790. out_kfree:
  791. mutex_unlock(&ipmi_interfaces_mutex);
  792. kfree(new_user);
  793. return rv;
  794. }
  795. EXPORT_SYMBOL(ipmi_create_user);
  796. int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
  797. {
  798. int rv = 0;
  799. ipmi_smi_t intf;
  800. struct ipmi_smi_handlers *handlers;
  801. mutex_lock(&ipmi_interfaces_mutex);
  802. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  803. if (intf->intf_num == if_num)
  804. goto found;
  805. }
  806. /* Not found, return an error */
  807. rv = -EINVAL;
  808. mutex_unlock(&ipmi_interfaces_mutex);
  809. return rv;
  810. found:
  811. handlers = intf->handlers;
  812. rv = -ENOSYS;
  813. if (handlers->get_smi_info)
  814. rv = handlers->get_smi_info(intf->send_info, data);
  815. mutex_unlock(&ipmi_interfaces_mutex);
  816. return rv;
  817. }
  818. EXPORT_SYMBOL(ipmi_get_smi_info);
  819. static void free_user(struct kref *ref)
  820. {
  821. ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
  822. kfree(user);
  823. }
  824. int ipmi_destroy_user(ipmi_user_t user)
  825. {
  826. ipmi_smi_t intf = user->intf;
  827. int i;
  828. unsigned long flags;
  829. struct cmd_rcvr *rcvr;
  830. struct cmd_rcvr *rcvrs = NULL;
  831. user->valid = 0;
  832. /* Remove the user from the interface's sequence table. */
  833. spin_lock_irqsave(&intf->seq_lock, flags);
  834. list_del_rcu(&user->link);
  835. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  836. if (intf->seq_table[i].inuse
  837. && (intf->seq_table[i].recv_msg->user == user)) {
  838. intf->seq_table[i].inuse = 0;
  839. ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
  840. }
  841. }
  842. spin_unlock_irqrestore(&intf->seq_lock, flags);
  843. /*
  844. * Remove the user from the command receiver's table. First
  845. * we build a list of everything (not using the standard link,
  846. * since other things may be using it till we do
  847. * synchronize_rcu()) then free everything in that list.
  848. */
  849. mutex_lock(&intf->cmd_rcvrs_mutex);
  850. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
  851. if (rcvr->user == user) {
  852. list_del_rcu(&rcvr->link);
  853. rcvr->next = rcvrs;
  854. rcvrs = rcvr;
  855. }
  856. }
  857. mutex_unlock(&intf->cmd_rcvrs_mutex);
  858. synchronize_rcu();
  859. while (rcvrs) {
  860. rcvr = rcvrs;
  861. rcvrs = rcvr->next;
  862. kfree(rcvr);
  863. }
  864. mutex_lock(&ipmi_interfaces_mutex);
  865. if (intf->handlers) {
  866. module_put(intf->handlers->owner);
  867. if (intf->handlers->dec_usecount)
  868. intf->handlers->dec_usecount(intf->send_info);
  869. }
  870. mutex_unlock(&ipmi_interfaces_mutex);
  871. kref_put(&intf->refcount, intf_free);
  872. kref_put(&user->refcount, free_user);
  873. return 0;
  874. }
  875. EXPORT_SYMBOL(ipmi_destroy_user);
  876. void ipmi_get_version(ipmi_user_t user,
  877. unsigned char *major,
  878. unsigned char *minor)
  879. {
  880. *major = user->intf->ipmi_version_major;
  881. *minor = user->intf->ipmi_version_minor;
  882. }
  883. EXPORT_SYMBOL(ipmi_get_version);
  884. int ipmi_set_my_address(ipmi_user_t user,
  885. unsigned int channel,
  886. unsigned char address)
  887. {
  888. if (channel >= IPMI_MAX_CHANNELS)
  889. return -EINVAL;
  890. user->intf->channels[channel].address = address;
  891. return 0;
  892. }
  893. EXPORT_SYMBOL(ipmi_set_my_address);
  894. int ipmi_get_my_address(ipmi_user_t user,
  895. unsigned int channel,
  896. unsigned char *address)
  897. {
  898. if (channel >= IPMI_MAX_CHANNELS)
  899. return -EINVAL;
  900. *address = user->intf->channels[channel].address;
  901. return 0;
  902. }
  903. EXPORT_SYMBOL(ipmi_get_my_address);
  904. int ipmi_set_my_LUN(ipmi_user_t user,
  905. unsigned int channel,
  906. unsigned char LUN)
  907. {
  908. if (channel >= IPMI_MAX_CHANNELS)
  909. return -EINVAL;
  910. user->intf->channels[channel].lun = LUN & 0x3;
  911. return 0;
  912. }
  913. EXPORT_SYMBOL(ipmi_set_my_LUN);
  914. int ipmi_get_my_LUN(ipmi_user_t user,
  915. unsigned int channel,
  916. unsigned char *address)
  917. {
  918. if (channel >= IPMI_MAX_CHANNELS)
  919. return -EINVAL;
  920. *address = user->intf->channels[channel].lun;
  921. return 0;
  922. }
  923. EXPORT_SYMBOL(ipmi_get_my_LUN);
  924. int ipmi_get_maintenance_mode(ipmi_user_t user)
  925. {
  926. int mode;
  927. unsigned long flags;
  928. spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
  929. mode = user->intf->maintenance_mode;
  930. spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
  931. return mode;
  932. }
  933. EXPORT_SYMBOL(ipmi_get_maintenance_mode);
  934. static void maintenance_mode_update(ipmi_smi_t intf)
  935. {
  936. if (intf->handlers->set_maintenance_mode)
  937. intf->handlers->set_maintenance_mode(
  938. intf->send_info, intf->maintenance_mode_enable);
  939. }
  940. int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
  941. {
  942. int rv = 0;
  943. unsigned long flags;
  944. ipmi_smi_t intf = user->intf;
  945. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  946. if (intf->maintenance_mode != mode) {
  947. switch (mode) {
  948. case IPMI_MAINTENANCE_MODE_AUTO:
  949. intf->maintenance_mode = mode;
  950. intf->maintenance_mode_enable
  951. = (intf->auto_maintenance_timeout > 0);
  952. break;
  953. case IPMI_MAINTENANCE_MODE_OFF:
  954. intf->maintenance_mode = mode;
  955. intf->maintenance_mode_enable = 0;
  956. break;
  957. case IPMI_MAINTENANCE_MODE_ON:
  958. intf->maintenance_mode = mode;
  959. intf->maintenance_mode_enable = 1;
  960. break;
  961. default:
  962. rv = -EINVAL;
  963. goto out_unlock;
  964. }
  965. maintenance_mode_update(intf);
  966. }
  967. out_unlock:
  968. spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
  969. return rv;
  970. }
  971. EXPORT_SYMBOL(ipmi_set_maintenance_mode);
  972. int ipmi_set_gets_events(ipmi_user_t user, int val)
  973. {
  974. unsigned long flags;
  975. ipmi_smi_t intf = user->intf;
  976. struct ipmi_recv_msg *msg, *msg2;
  977. struct list_head msgs;
  978. INIT_LIST_HEAD(&msgs);
  979. spin_lock_irqsave(&intf->events_lock, flags);
  980. user->gets_events = val;
  981. if (intf->delivering_events)
  982. /*
  983. * Another thread is delivering events for this, so
  984. * let it handle any new events.
  985. */
  986. goto out;
  987. /* Deliver any queued events. */
  988. while (user->gets_events && !list_empty(&intf->waiting_events)) {
  989. list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
  990. list_move_tail(&msg->link, &msgs);
  991. intf->waiting_events_count = 0;
  992. if (intf->event_msg_printed) {
  993. printk(KERN_WARNING PFX "Event queue no longer"
  994. " full\n");
  995. intf->event_msg_printed = 0;
  996. }
  997. intf->delivering_events = 1;
  998. spin_unlock_irqrestore(&intf->events_lock, flags);
  999. list_for_each_entry_safe(msg, msg2, &msgs, link) {
  1000. msg->user = user;
  1001. kref_get(&user->refcount);
  1002. deliver_response(msg);
  1003. }
  1004. spin_lock_irqsave(&intf->events_lock, flags);
  1005. intf->delivering_events = 0;
  1006. }
  1007. out:
  1008. spin_unlock_irqrestore(&intf->events_lock, flags);
  1009. return 0;
  1010. }
  1011. EXPORT_SYMBOL(ipmi_set_gets_events);
  1012. static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
  1013. unsigned char netfn,
  1014. unsigned char cmd,
  1015. unsigned char chan)
  1016. {
  1017. struct cmd_rcvr *rcvr;
  1018. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
  1019. if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
  1020. && (rcvr->chans & (1 << chan)))
  1021. return rcvr;
  1022. }
  1023. return NULL;
  1024. }
  1025. static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
  1026. unsigned char netfn,
  1027. unsigned char cmd,
  1028. unsigned int chans)
  1029. {
  1030. struct cmd_rcvr *rcvr;
  1031. list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
  1032. if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
  1033. && (rcvr->chans & chans))
  1034. return 0;
  1035. }
  1036. return 1;
  1037. }
  1038. int ipmi_register_for_cmd(ipmi_user_t user,
  1039. unsigned char netfn,
  1040. unsigned char cmd,
  1041. unsigned int chans)
  1042. {
  1043. ipmi_smi_t intf = user->intf;
  1044. struct cmd_rcvr *rcvr;
  1045. int rv = 0;
  1046. rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
  1047. if (!rcvr)
  1048. return -ENOMEM;
  1049. rcvr->cmd = cmd;
  1050. rcvr->netfn = netfn;
  1051. rcvr->chans = chans;
  1052. rcvr->user = user;
  1053. mutex_lock(&intf->cmd_rcvrs_mutex);
  1054. /* Make sure the command/netfn is not already registered. */
  1055. if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
  1056. rv = -EBUSY;
  1057. goto out_unlock;
  1058. }
  1059. list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
  1060. out_unlock:
  1061. mutex_unlock(&intf->cmd_rcvrs_mutex);
  1062. if (rv)
  1063. kfree(rcvr);
  1064. return rv;
  1065. }
  1066. EXPORT_SYMBOL(ipmi_register_for_cmd);
  1067. int ipmi_unregister_for_cmd(ipmi_user_t user,
  1068. unsigned char netfn,
  1069. unsigned char cmd,
  1070. unsigned int chans)
  1071. {
  1072. ipmi_smi_t intf = user->intf;
  1073. struct cmd_rcvr *rcvr;
  1074. struct cmd_rcvr *rcvrs = NULL;
  1075. int i, rv = -ENOENT;
  1076. mutex_lock(&intf->cmd_rcvrs_mutex);
  1077. for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
  1078. if (((1 << i) & chans) == 0)
  1079. continue;
  1080. rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
  1081. if (rcvr == NULL)
  1082. continue;
  1083. if (rcvr->user == user) {
  1084. rv = 0;
  1085. rcvr->chans &= ~chans;
  1086. if (rcvr->chans == 0) {
  1087. list_del_rcu(&rcvr->link);
  1088. rcvr->next = rcvrs;
  1089. rcvrs = rcvr;
  1090. }
  1091. }
  1092. }
  1093. mutex_unlock(&intf->cmd_rcvrs_mutex);
  1094. synchronize_rcu();
  1095. while (rcvrs) {
  1096. rcvr = rcvrs;
  1097. rcvrs = rcvr->next;
  1098. kfree(rcvr);
  1099. }
  1100. return rv;
  1101. }
  1102. EXPORT_SYMBOL(ipmi_unregister_for_cmd);
  1103. static unsigned char
  1104. ipmb_checksum(unsigned char *data, int size)
  1105. {
  1106. unsigned char csum = 0;
  1107. for (; size > 0; size--, data++)
  1108. csum += *data;
  1109. return -csum;
  1110. }
  1111. static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
  1112. struct kernel_ipmi_msg *msg,
  1113. struct ipmi_ipmb_addr *ipmb_addr,
  1114. long msgid,
  1115. unsigned char ipmb_seq,
  1116. int broadcast,
  1117. unsigned char source_address,
  1118. unsigned char source_lun)
  1119. {
  1120. int i = broadcast;
  1121. /* Format the IPMB header data. */
  1122. smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  1123. smi_msg->data[1] = IPMI_SEND_MSG_CMD;
  1124. smi_msg->data[2] = ipmb_addr->channel;
  1125. if (broadcast)
  1126. smi_msg->data[3] = 0;
  1127. smi_msg->data[i+3] = ipmb_addr->slave_addr;
  1128. smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
  1129. smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
  1130. smi_msg->data[i+6] = source_address;
  1131. smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
  1132. smi_msg->data[i+8] = msg->cmd;
  1133. /* Now tack on the data to the message. */
  1134. if (msg->data_len > 0)
  1135. memcpy(&(smi_msg->data[i+9]), msg->data,
  1136. msg->data_len);
  1137. smi_msg->data_size = msg->data_len + 9;
  1138. /* Now calculate the checksum and tack it on. */
  1139. smi_msg->data[i+smi_msg->data_size]
  1140. = ipmb_checksum(&(smi_msg->data[i+6]),
  1141. smi_msg->data_size-6);
  1142. /*
  1143. * Add on the checksum size and the offset from the
  1144. * broadcast.
  1145. */
  1146. smi_msg->data_size += 1 + i;
  1147. smi_msg->msgid = msgid;
  1148. }
  1149. static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
  1150. struct kernel_ipmi_msg *msg,
  1151. struct ipmi_lan_addr *lan_addr,
  1152. long msgid,
  1153. unsigned char ipmb_seq,
  1154. unsigned char source_lun)
  1155. {
  1156. /* Format the IPMB header data. */
  1157. smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  1158. smi_msg->data[1] = IPMI_SEND_MSG_CMD;
  1159. smi_msg->data[2] = lan_addr->channel;
  1160. smi_msg->data[3] = lan_addr->session_handle;
  1161. smi_msg->data[4] = lan_addr->remote_SWID;
  1162. smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
  1163. smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
  1164. smi_msg->data[7] = lan_addr->local_SWID;
  1165. smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
  1166. smi_msg->data[9] = msg->cmd;
  1167. /* Now tack on the data to the message. */
  1168. if (msg->data_len > 0)
  1169. memcpy(&(smi_msg->data[10]), msg->data,
  1170. msg->data_len);
  1171. smi_msg->data_size = msg->data_len + 10;
  1172. /* Now calculate the checksum and tack it on. */
  1173. smi_msg->data[smi_msg->data_size]
  1174. = ipmb_checksum(&(smi_msg->data[7]),
  1175. smi_msg->data_size-7);
  1176. /*
  1177. * Add on the checksum size and the offset from the
  1178. * broadcast.
  1179. */
  1180. smi_msg->data_size += 1;
  1181. smi_msg->msgid = msgid;
  1182. }
  1183. /*
  1184. * Separate from ipmi_request so that the user does not have to be
  1185. * supplied in certain circumstances (mainly at panic time). If
  1186. * messages are supplied, they will be freed, even if an error
  1187. * occurs.
  1188. */
  1189. static int i_ipmi_request(ipmi_user_t user,
  1190. ipmi_smi_t intf,
  1191. struct ipmi_addr *addr,
  1192. long msgid,
  1193. struct kernel_ipmi_msg *msg,
  1194. void *user_msg_data,
  1195. void *supplied_smi,
  1196. struct ipmi_recv_msg *supplied_recv,
  1197. int priority,
  1198. unsigned char source_address,
  1199. unsigned char source_lun,
  1200. int retries,
  1201. unsigned int retry_time_ms)
  1202. {
  1203. int rv = 0;
  1204. struct ipmi_smi_msg *smi_msg;
  1205. struct ipmi_recv_msg *recv_msg;
  1206. unsigned long flags;
  1207. struct ipmi_smi_handlers *handlers;
  1208. if (supplied_recv)
  1209. recv_msg = supplied_recv;
  1210. else {
  1211. recv_msg = ipmi_alloc_recv_msg();
  1212. if (recv_msg == NULL)
  1213. return -ENOMEM;
  1214. }
  1215. recv_msg->user_msg_data = user_msg_data;
  1216. if (supplied_smi)
  1217. smi_msg = (struct ipmi_smi_msg *) supplied_smi;
  1218. else {
  1219. smi_msg = ipmi_alloc_smi_msg();
  1220. if (smi_msg == NULL) {
  1221. ipmi_free_recv_msg(recv_msg);
  1222. return -ENOMEM;
  1223. }
  1224. }
  1225. rcu_read_lock();
  1226. handlers = intf->handlers;
  1227. if (!handlers) {
  1228. rv = -ENODEV;
  1229. goto out_err;
  1230. }
  1231. recv_msg->user = user;
  1232. if (user)
  1233. kref_get(&user->refcount);
  1234. recv_msg->msgid = msgid;
  1235. /*
  1236. * Store the message to send in the receive message so timeout
  1237. * responses can get the proper response data.
  1238. */
  1239. recv_msg->msg = *msg;
  1240. if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
  1241. struct ipmi_system_interface_addr *smi_addr;
  1242. if (msg->netfn & 1) {
  1243. /* Responses are not allowed to the SMI. */
  1244. rv = -EINVAL;
  1245. goto out_err;
  1246. }
  1247. smi_addr = (struct ipmi_system_interface_addr *) addr;
  1248. if (smi_addr->lun > 3) {
  1249. ipmi_inc_stat(intf, sent_invalid_commands);
  1250. rv = -EINVAL;
  1251. goto out_err;
  1252. }
  1253. memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
  1254. if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
  1255. && ((msg->cmd == IPMI_SEND_MSG_CMD)
  1256. || (msg->cmd == IPMI_GET_MSG_CMD)
  1257. || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
  1258. /*
  1259. * We don't let the user do these, since we manage
  1260. * the sequence numbers.
  1261. */
  1262. ipmi_inc_stat(intf, sent_invalid_commands);
  1263. rv = -EINVAL;
  1264. goto out_err;
  1265. }
  1266. if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
  1267. && ((msg->cmd == IPMI_COLD_RESET_CMD)
  1268. || (msg->cmd == IPMI_WARM_RESET_CMD)))
  1269. || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
  1270. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  1271. intf->auto_maintenance_timeout
  1272. = IPMI_MAINTENANCE_MODE_TIMEOUT;
  1273. if (!intf->maintenance_mode
  1274. && !intf->maintenance_mode_enable) {
  1275. intf->maintenance_mode_enable = 1;
  1276. maintenance_mode_update(intf);
  1277. }
  1278. spin_unlock_irqrestore(&intf->maintenance_mode_lock,
  1279. flags);
  1280. }
  1281. if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
  1282. ipmi_inc_stat(intf, sent_invalid_commands);
  1283. rv = -EMSGSIZE;
  1284. goto out_err;
  1285. }
  1286. smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
  1287. smi_msg->data[1] = msg->cmd;
  1288. smi_msg->msgid = msgid;
  1289. smi_msg->user_data = recv_msg;
  1290. if (msg->data_len > 0)
  1291. memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
  1292. smi_msg->data_size = msg->data_len + 2;
  1293. ipmi_inc_stat(intf, sent_local_commands);
  1294. } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
  1295. struct ipmi_ipmb_addr *ipmb_addr;
  1296. unsigned char ipmb_seq;
  1297. long seqid;
  1298. int broadcast = 0;
  1299. if (addr->channel >= IPMI_MAX_CHANNELS) {
  1300. ipmi_inc_stat(intf, sent_invalid_commands);
  1301. rv = -EINVAL;
  1302. goto out_err;
  1303. }
  1304. if (intf->channels[addr->channel].medium
  1305. != IPMI_CHANNEL_MEDIUM_IPMB) {
  1306. ipmi_inc_stat(intf, sent_invalid_commands);
  1307. rv = -EINVAL;
  1308. goto out_err;
  1309. }
  1310. if (retries < 0) {
  1311. if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
  1312. retries = 0; /* Don't retry broadcasts. */
  1313. else
  1314. retries = 4;
  1315. }
  1316. if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
  1317. /*
  1318. * Broadcasts add a zero at the beginning of the
  1319. * message, but otherwise is the same as an IPMB
  1320. * address.
  1321. */
  1322. addr->addr_type = IPMI_IPMB_ADDR_TYPE;
  1323. broadcast = 1;
  1324. }
  1325. /* Default to 1 second retries. */
  1326. if (retry_time_ms == 0)
  1327. retry_time_ms = 1000;
  1328. /*
  1329. * 9 for the header and 1 for the checksum, plus
  1330. * possibly one for the broadcast.
  1331. */
  1332. if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
  1333. ipmi_inc_stat(intf, sent_invalid_commands);
  1334. rv = -EMSGSIZE;
  1335. goto out_err;
  1336. }
  1337. ipmb_addr = (struct ipmi_ipmb_addr *) addr;
  1338. if (ipmb_addr->lun > 3) {
  1339. ipmi_inc_stat(intf, sent_invalid_commands);
  1340. rv = -EINVAL;
  1341. goto out_err;
  1342. }
  1343. memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
  1344. if (recv_msg->msg.netfn & 0x1) {
  1345. /*
  1346. * It's a response, so use the user's sequence
  1347. * from msgid.
  1348. */
  1349. ipmi_inc_stat(intf, sent_ipmb_responses);
  1350. format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
  1351. msgid, broadcast,
  1352. source_address, source_lun);
  1353. /*
  1354. * Save the receive message so we can use it
  1355. * to deliver the response.
  1356. */
  1357. smi_msg->user_data = recv_msg;
  1358. } else {
  1359. /* It's a command, so get a sequence for it. */
  1360. spin_lock_irqsave(&(intf->seq_lock), flags);
  1361. /*
  1362. * Create a sequence number with a 1 second
  1363. * timeout and 4 retries.
  1364. */
  1365. rv = intf_next_seq(intf,
  1366. recv_msg,
  1367. retry_time_ms,
  1368. retries,
  1369. broadcast,
  1370. &ipmb_seq,
  1371. &seqid);
  1372. if (rv) {
  1373. /*
  1374. * We have used up all the sequence numbers,
  1375. * probably, so abort.
  1376. */
  1377. spin_unlock_irqrestore(&(intf->seq_lock),
  1378. flags);
  1379. goto out_err;
  1380. }
  1381. ipmi_inc_stat(intf, sent_ipmb_commands);
  1382. /*
  1383. * Store the sequence number in the message,
  1384. * so that when the send message response
  1385. * comes back we can start the timer.
  1386. */
  1387. format_ipmb_msg(smi_msg, msg, ipmb_addr,
  1388. STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
  1389. ipmb_seq, broadcast,
  1390. source_address, source_lun);
  1391. /*
  1392. * Copy the message into the recv message data, so we
  1393. * can retransmit it later if necessary.
  1394. */
  1395. memcpy(recv_msg->msg_data, smi_msg->data,
  1396. smi_msg->data_size);
  1397. recv_msg->msg.data = recv_msg->msg_data;
  1398. recv_msg->msg.data_len = smi_msg->data_size;
  1399. /*
  1400. * We don't unlock until here, because we need
  1401. * to copy the completed message into the
  1402. * recv_msg before we release the lock.
  1403. * Otherwise, race conditions may bite us. I
  1404. * know that's pretty paranoid, but I prefer
  1405. * to be correct.
  1406. */
  1407. spin_unlock_irqrestore(&(intf->seq_lock), flags);
  1408. }
  1409. } else if (is_lan_addr(addr)) {
  1410. struct ipmi_lan_addr *lan_addr;
  1411. unsigned char ipmb_seq;
  1412. long seqid;
  1413. if (addr->channel >= IPMI_MAX_CHANNELS) {
  1414. ipmi_inc_stat(intf, sent_invalid_commands);
  1415. rv = -EINVAL;
  1416. goto out_err;
  1417. }
  1418. if ((intf->channels[addr->channel].medium
  1419. != IPMI_CHANNEL_MEDIUM_8023LAN)
  1420. && (intf->channels[addr->channel].medium
  1421. != IPMI_CHANNEL_MEDIUM_ASYNC)) {
  1422. ipmi_inc_stat(intf, sent_invalid_commands);
  1423. rv = -EINVAL;
  1424. goto out_err;
  1425. }
  1426. retries = 4;
  1427. /* Default to 1 second retries. */
  1428. if (retry_time_ms == 0)
  1429. retry_time_ms = 1000;
  1430. /* 11 for the header and 1 for the checksum. */
  1431. if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
  1432. ipmi_inc_stat(intf, sent_invalid_commands);
  1433. rv = -EMSGSIZE;
  1434. goto out_err;
  1435. }
  1436. lan_addr = (struct ipmi_lan_addr *) addr;
  1437. if (lan_addr->lun > 3) {
  1438. ipmi_inc_stat(intf, sent_invalid_commands);
  1439. rv = -EINVAL;
  1440. goto out_err;
  1441. }
  1442. memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
  1443. if (recv_msg->msg.netfn & 0x1) {
  1444. /*
  1445. * It's a response, so use the user's sequence
  1446. * from msgid.
  1447. */
  1448. ipmi_inc_stat(intf, sent_lan_responses);
  1449. format_lan_msg(smi_msg, msg, lan_addr, msgid,
  1450. msgid, source_lun);
  1451. /*
  1452. * Save the receive message so we can use it
  1453. * to deliver the response.
  1454. */
  1455. smi_msg->user_data = recv_msg;
  1456. } else {
  1457. /* It's a command, so get a sequence for it. */
  1458. spin_lock_irqsave(&(intf->seq_lock), flags);
  1459. /*
  1460. * Create a sequence number with a 1 second
  1461. * timeout and 4 retries.
  1462. */
  1463. rv = intf_next_seq(intf,
  1464. recv_msg,
  1465. retry_time_ms,
  1466. retries,
  1467. 0,
  1468. &ipmb_seq,
  1469. &seqid);
  1470. if (rv) {
  1471. /*
  1472. * We have used up all the sequence numbers,
  1473. * probably, so abort.
  1474. */
  1475. spin_unlock_irqrestore(&(intf->seq_lock),
  1476. flags);
  1477. goto out_err;
  1478. }
  1479. ipmi_inc_stat(intf, sent_lan_commands);
  1480. /*
  1481. * Store the sequence number in the message,
  1482. * so that when the send message response
  1483. * comes back we can start the timer.
  1484. */
  1485. format_lan_msg(smi_msg, msg, lan_addr,
  1486. STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
  1487. ipmb_seq, source_lun);
  1488. /*
  1489. * Copy the message into the recv message data, so we
  1490. * can retransmit it later if necessary.
  1491. */
  1492. memcpy(recv_msg->msg_data, smi_msg->data,
  1493. smi_msg->data_size);
  1494. recv_msg->msg.data = recv_msg->msg_data;
  1495. recv_msg->msg.data_len = smi_msg->data_size;
  1496. /*
  1497. * We don't unlock until here, because we need
  1498. * to copy the completed message into the
  1499. * recv_msg before we release the lock.
  1500. * Otherwise, race conditions may bite us. I
  1501. * know that's pretty paranoid, but I prefer
  1502. * to be correct.
  1503. */
  1504. spin_unlock_irqrestore(&(intf->seq_lock), flags);
  1505. }
  1506. } else {
  1507. /* Unknown address type. */
  1508. ipmi_inc_stat(intf, sent_invalid_commands);
  1509. rv = -EINVAL;
  1510. goto out_err;
  1511. }
  1512. #ifdef DEBUG_MSGING
  1513. {
  1514. int m;
  1515. for (m = 0; m < smi_msg->data_size; m++)
  1516. printk(" %2.2x", smi_msg->data[m]);
  1517. printk("\n");
  1518. }
  1519. #endif
  1520. handlers->sender(intf->send_info, smi_msg, priority);
  1521. rcu_read_unlock();
  1522. return 0;
  1523. out_err:
  1524. rcu_read_unlock();
  1525. ipmi_free_smi_msg(smi_msg);
  1526. ipmi_free_recv_msg(recv_msg);
  1527. return rv;
  1528. }
  1529. static int check_addr(ipmi_smi_t intf,
  1530. struct ipmi_addr *addr,
  1531. unsigned char *saddr,
  1532. unsigned char *lun)
  1533. {
  1534. if (addr->channel >= IPMI_MAX_CHANNELS)
  1535. return -EINVAL;
  1536. *lun = intf->channels[addr->channel].lun;
  1537. *saddr = intf->channels[addr->channel].address;
  1538. return 0;
  1539. }
  1540. int ipmi_request_settime(ipmi_user_t user,
  1541. struct ipmi_addr *addr,
  1542. long msgid,
  1543. struct kernel_ipmi_msg *msg,
  1544. void *user_msg_data,
  1545. int priority,
  1546. int retries,
  1547. unsigned int retry_time_ms)
  1548. {
  1549. unsigned char saddr, lun;
  1550. int rv;
  1551. if (!user)
  1552. return -EINVAL;
  1553. rv = check_addr(user->intf, addr, &saddr, &lun);
  1554. if (rv)
  1555. return rv;
  1556. return i_ipmi_request(user,
  1557. user->intf,
  1558. addr,
  1559. msgid,
  1560. msg,
  1561. user_msg_data,
  1562. NULL, NULL,
  1563. priority,
  1564. saddr,
  1565. lun,
  1566. retries,
  1567. retry_time_ms);
  1568. }
  1569. EXPORT_SYMBOL(ipmi_request_settime);
  1570. int ipmi_request_supply_msgs(ipmi_user_t user,
  1571. struct ipmi_addr *addr,
  1572. long msgid,
  1573. struct kernel_ipmi_msg *msg,
  1574. void *user_msg_data,
  1575. void *supplied_smi,
  1576. struct ipmi_recv_msg *supplied_recv,
  1577. int priority)
  1578. {
  1579. unsigned char saddr, lun;
  1580. int rv;
  1581. if (!user)
  1582. return -EINVAL;
  1583. rv = check_addr(user->intf, addr, &saddr, &lun);
  1584. if (rv)
  1585. return rv;
  1586. return i_ipmi_request(user,
  1587. user->intf,
  1588. addr,
  1589. msgid,
  1590. msg,
  1591. user_msg_data,
  1592. supplied_smi,
  1593. supplied_recv,
  1594. priority,
  1595. saddr,
  1596. lun,
  1597. -1, 0);
  1598. }
  1599. EXPORT_SYMBOL(ipmi_request_supply_msgs);
  1600. #ifdef CONFIG_PROC_FS
  1601. static int smi_ipmb_proc_show(struct seq_file *m, void *v)
  1602. {
  1603. ipmi_smi_t intf = m->private;
  1604. int i;
  1605. seq_printf(m, "%x", intf->channels[0].address);
  1606. for (i = 1; i < IPMI_MAX_CHANNELS; i++)
  1607. seq_printf(m, " %x", intf->channels[i].address);
  1608. return seq_putc(m, '\n');
  1609. }
  1610. static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
  1611. {
  1612. return single_open(file, smi_ipmb_proc_show, PDE(inode)->data);
  1613. }
  1614. static const struct file_operations smi_ipmb_proc_ops = {
  1615. .open = smi_ipmb_proc_open,
  1616. .read = seq_read,
  1617. .llseek = seq_lseek,
  1618. .release = single_release,
  1619. };
  1620. static int smi_version_proc_show(struct seq_file *m, void *v)
  1621. {
  1622. ipmi_smi_t intf = m->private;
  1623. return seq_printf(m, "%u.%u\n",
  1624. ipmi_version_major(&intf->bmc->id),
  1625. ipmi_version_minor(&intf->bmc->id));
  1626. }
  1627. static int smi_version_proc_open(struct inode *inode, struct file *file)
  1628. {
  1629. return single_open(file, smi_version_proc_show, PDE(inode)->data);
  1630. }
  1631. static const struct file_operations smi_version_proc_ops = {
  1632. .open = smi_version_proc_open,
  1633. .read = seq_read,
  1634. .llseek = seq_lseek,
  1635. .release = single_release,
  1636. };
  1637. static int smi_stats_proc_show(struct seq_file *m, void *v)
  1638. {
  1639. ipmi_smi_t intf = m->private;
  1640. seq_printf(m, "sent_invalid_commands: %u\n",
  1641. ipmi_get_stat(intf, sent_invalid_commands));
  1642. seq_printf(m, "sent_local_commands: %u\n",
  1643. ipmi_get_stat(intf, sent_local_commands));
  1644. seq_printf(m, "handled_local_responses: %u\n",
  1645. ipmi_get_stat(intf, handled_local_responses));
  1646. seq_printf(m, "unhandled_local_responses: %u\n",
  1647. ipmi_get_stat(intf, unhandled_local_responses));
  1648. seq_printf(m, "sent_ipmb_commands: %u\n",
  1649. ipmi_get_stat(intf, sent_ipmb_commands));
  1650. seq_printf(m, "sent_ipmb_command_errs: %u\n",
  1651. ipmi_get_stat(intf, sent_ipmb_command_errs));
  1652. seq_printf(m, "retransmitted_ipmb_commands: %u\n",
  1653. ipmi_get_stat(intf, retransmitted_ipmb_commands));
  1654. seq_printf(m, "timed_out_ipmb_commands: %u\n",
  1655. ipmi_get_stat(intf, timed_out_ipmb_commands));
  1656. seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
  1657. ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
  1658. seq_printf(m, "sent_ipmb_responses: %u\n",
  1659. ipmi_get_stat(intf, sent_ipmb_responses));
  1660. seq_printf(m, "handled_ipmb_responses: %u\n",
  1661. ipmi_get_stat(intf, handled_ipmb_responses));
  1662. seq_printf(m, "invalid_ipmb_responses: %u\n",
  1663. ipmi_get_stat(intf, invalid_ipmb_responses));
  1664. seq_printf(m, "unhandled_ipmb_responses: %u\n",
  1665. ipmi_get_stat(intf, unhandled_ipmb_responses));
  1666. seq_printf(m, "sent_lan_commands: %u\n",
  1667. ipmi_get_stat(intf, sent_lan_commands));
  1668. seq_printf(m, "sent_lan_command_errs: %u\n",
  1669. ipmi_get_stat(intf, sent_lan_command_errs));
  1670. seq_printf(m, "retransmitted_lan_commands: %u\n",
  1671. ipmi_get_stat(intf, retransmitted_lan_commands));
  1672. seq_printf(m, "timed_out_lan_commands: %u\n",
  1673. ipmi_get_stat(intf, timed_out_lan_commands));
  1674. seq_printf(m, "sent_lan_responses: %u\n",
  1675. ipmi_get_stat(intf, sent_lan_responses));
  1676. seq_printf(m, "handled_lan_responses: %u\n",
  1677. ipmi_get_stat(intf, handled_lan_responses));
  1678. seq_printf(m, "invalid_lan_responses: %u\n",
  1679. ipmi_get_stat(intf, invalid_lan_responses));
  1680. seq_printf(m, "unhandled_lan_responses: %u\n",
  1681. ipmi_get_stat(intf, unhandled_lan_responses));
  1682. seq_printf(m, "handled_commands: %u\n",
  1683. ipmi_get_stat(intf, handled_commands));
  1684. seq_printf(m, "invalid_commands: %u\n",
  1685. ipmi_get_stat(intf, invalid_commands));
  1686. seq_printf(m, "unhandled_commands: %u\n",
  1687. ipmi_get_stat(intf, unhandled_commands));
  1688. seq_printf(m, "invalid_events: %u\n",
  1689. ipmi_get_stat(intf, invalid_events));
  1690. seq_printf(m, "events: %u\n",
  1691. ipmi_get_stat(intf, events));
  1692. seq_printf(m, "failed rexmit LAN msgs: %u\n",
  1693. ipmi_get_stat(intf, dropped_rexmit_lan_commands));
  1694. seq_printf(m, "failed rexmit IPMB msgs: %u\n",
  1695. ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
  1696. return 0;
  1697. }
  1698. static int smi_stats_proc_open(struct inode *inode, struct file *file)
  1699. {
  1700. return single_open(file, smi_stats_proc_show, PDE(inode)->data);
  1701. }
  1702. static const struct file_operations smi_stats_proc_ops = {
  1703. .open = smi_stats_proc_open,
  1704. .read = seq_read,
  1705. .llseek = seq_lseek,
  1706. .release = single_release,
  1707. };
  1708. #endif /* CONFIG_PROC_FS */
  1709. int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
  1710. const struct file_operations *proc_ops,
  1711. void *data)
  1712. {
  1713. int rv = 0;
  1714. #ifdef CONFIG_PROC_FS
  1715. struct proc_dir_entry *file;
  1716. struct ipmi_proc_entry *entry;
  1717. /* Create a list element. */
  1718. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1719. if (!entry)
  1720. return -ENOMEM;
  1721. entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
  1722. if (!entry->name) {
  1723. kfree(entry);
  1724. return -ENOMEM;
  1725. }
  1726. strcpy(entry->name, name);
  1727. file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
  1728. if (!file) {
  1729. kfree(entry->name);
  1730. kfree(entry);
  1731. rv = -ENOMEM;
  1732. } else {
  1733. mutex_lock(&smi->proc_entry_lock);
  1734. /* Stick it on the list. */
  1735. entry->next = smi->proc_entries;
  1736. smi->proc_entries = entry;
  1737. mutex_unlock(&smi->proc_entry_lock);
  1738. }
  1739. #endif /* CONFIG_PROC_FS */
  1740. return rv;
  1741. }
  1742. EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
  1743. static int add_proc_entries(ipmi_smi_t smi, int num)
  1744. {
  1745. int rv = 0;
  1746. #ifdef CONFIG_PROC_FS
  1747. sprintf(smi->proc_dir_name, "%d", num);
  1748. smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
  1749. if (!smi->proc_dir)
  1750. rv = -ENOMEM;
  1751. if (rv == 0)
  1752. rv = ipmi_smi_add_proc_entry(smi, "stats",
  1753. &smi_stats_proc_ops,
  1754. smi);
  1755. if (rv == 0)
  1756. rv = ipmi_smi_add_proc_entry(smi, "ipmb",
  1757. &smi_ipmb_proc_ops,
  1758. smi);
  1759. if (rv == 0)
  1760. rv = ipmi_smi_add_proc_entry(smi, "version",
  1761. &smi_version_proc_ops,
  1762. smi);
  1763. #endif /* CONFIG_PROC_FS */
  1764. return rv;
  1765. }
  1766. static void remove_proc_entries(ipmi_smi_t smi)
  1767. {
  1768. #ifdef CONFIG_PROC_FS
  1769. struct ipmi_proc_entry *entry;
  1770. mutex_lock(&smi->proc_entry_lock);
  1771. while (smi->proc_entries) {
  1772. entry = smi->proc_entries;
  1773. smi->proc_entries = entry->next;
  1774. remove_proc_entry(entry->name, smi->proc_dir);
  1775. kfree(entry->name);
  1776. kfree(entry);
  1777. }
  1778. mutex_unlock(&smi->proc_entry_lock);
  1779. remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
  1780. #endif /* CONFIG_PROC_FS */
  1781. }
  1782. static int __find_bmc_guid(struct device *dev, void *data)
  1783. {
  1784. unsigned char *id = data;
  1785. struct bmc_device *bmc = dev_get_drvdata(dev);
  1786. return memcmp(bmc->guid, id, 16) == 0;
  1787. }
  1788. static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
  1789. unsigned char *guid)
  1790. {
  1791. struct device *dev;
  1792. dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
  1793. if (dev)
  1794. return dev_get_drvdata(dev);
  1795. else
  1796. return NULL;
  1797. }
  1798. struct prod_dev_id {
  1799. unsigned int product_id;
  1800. unsigned char device_id;
  1801. };
  1802. static int __find_bmc_prod_dev_id(struct device *dev, void *data)
  1803. {
  1804. struct prod_dev_id *id = data;
  1805. struct bmc_device *bmc = dev_get_drvdata(dev);
  1806. return (bmc->id.product_id == id->product_id
  1807. && bmc->id.device_id == id->device_id);
  1808. }
  1809. static struct bmc_device *ipmi_find_bmc_prod_dev_id(
  1810. struct device_driver *drv,
  1811. unsigned int product_id, unsigned char device_id)
  1812. {
  1813. struct prod_dev_id id = {
  1814. .product_id = product_id,
  1815. .device_id = device_id,
  1816. };
  1817. struct device *dev;
  1818. dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
  1819. if (dev)
  1820. return dev_get_drvdata(dev);
  1821. else
  1822. return NULL;
  1823. }
  1824. static ssize_t device_id_show(struct device *dev,
  1825. struct device_attribute *attr,
  1826. char *buf)
  1827. {
  1828. struct bmc_device *bmc = dev_get_drvdata(dev);
  1829. return snprintf(buf, 10, "%u\n", bmc->id.device_id);
  1830. }
  1831. static ssize_t provides_dev_sdrs_show(struct device *dev,
  1832. struct device_attribute *attr,
  1833. char *buf)
  1834. {
  1835. struct bmc_device *bmc = dev_get_drvdata(dev);
  1836. return snprintf(buf, 10, "%u\n",
  1837. (bmc->id.device_revision & 0x80) >> 7);
  1838. }
  1839. static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
  1840. char *buf)
  1841. {
  1842. struct bmc_device *bmc = dev_get_drvdata(dev);
  1843. return snprintf(buf, 20, "%u\n",
  1844. bmc->id.device_revision & 0x0F);
  1845. }
  1846. static ssize_t firmware_rev_show(struct device *dev,
  1847. struct device_attribute *attr,
  1848. char *buf)
  1849. {
  1850. struct bmc_device *bmc = dev_get_drvdata(dev);
  1851. return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
  1852. bmc->id.firmware_revision_2);
  1853. }
  1854. static ssize_t ipmi_version_show(struct device *dev,
  1855. struct device_attribute *attr,
  1856. char *buf)
  1857. {
  1858. struct bmc_device *bmc = dev_get_drvdata(dev);
  1859. return snprintf(buf, 20, "%u.%u\n",
  1860. ipmi_version_major(&bmc->id),
  1861. ipmi_version_minor(&bmc->id));
  1862. }
  1863. static ssize_t add_dev_support_show(struct device *dev,
  1864. struct device_attribute *attr,
  1865. char *buf)
  1866. {
  1867. struct bmc_device *bmc = dev_get_drvdata(dev);
  1868. return snprintf(buf, 10, "0x%02x\n",
  1869. bmc->id.additional_device_support);
  1870. }
  1871. static ssize_t manufacturer_id_show(struct device *dev,
  1872. struct device_attribute *attr,
  1873. char *buf)
  1874. {
  1875. struct bmc_device *bmc = dev_get_drvdata(dev);
  1876. return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
  1877. }
  1878. static ssize_t product_id_show(struct device *dev,
  1879. struct device_attribute *attr,
  1880. char *buf)
  1881. {
  1882. struct bmc_device *bmc = dev_get_drvdata(dev);
  1883. return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
  1884. }
  1885. static ssize_t aux_firmware_rev_show(struct device *dev,
  1886. struct device_attribute *attr,
  1887. char *buf)
  1888. {
  1889. struct bmc_device *bmc = dev_get_drvdata(dev);
  1890. return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
  1891. bmc->id.aux_firmware_revision[3],
  1892. bmc->id.aux_firmware_revision[2],
  1893. bmc->id.aux_firmware_revision[1],
  1894. bmc->id.aux_firmware_revision[0]);
  1895. }
  1896. static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
  1897. char *buf)
  1898. {
  1899. struct bmc_device *bmc = dev_get_drvdata(dev);
  1900. return snprintf(buf, 100, "%Lx%Lx\n",
  1901. (long long) bmc->guid[0],
  1902. (long long) bmc->guid[8]);
  1903. }
  1904. static void remove_files(struct bmc_device *bmc)
  1905. {
  1906. if (!bmc->dev)
  1907. return;
  1908. device_remove_file(&bmc->dev->dev,
  1909. &bmc->device_id_attr);
  1910. device_remove_file(&bmc->dev->dev,
  1911. &bmc->provides_dev_sdrs_attr);
  1912. device_remove_file(&bmc->dev->dev,
  1913. &bmc->revision_attr);
  1914. device_remove_file(&bmc->dev->dev,
  1915. &bmc->firmware_rev_attr);
  1916. device_remove_file(&bmc->dev->dev,
  1917. &bmc->version_attr);
  1918. device_remove_file(&bmc->dev->dev,
  1919. &bmc->add_dev_support_attr);
  1920. device_remove_file(&bmc->dev->dev,
  1921. &bmc->manufacturer_id_attr);
  1922. device_remove_file(&bmc->dev->dev,
  1923. &bmc->product_id_attr);
  1924. if (bmc->id.aux_firmware_revision_set)
  1925. device_remove_file(&bmc->dev->dev,
  1926. &bmc->aux_firmware_rev_attr);
  1927. if (bmc->guid_set)
  1928. device_remove_file(&bmc->dev->dev,
  1929. &bmc->guid_attr);
  1930. }
  1931. static void
  1932. cleanup_bmc_device(struct kref *ref)
  1933. {
  1934. struct bmc_device *bmc;
  1935. bmc = container_of(ref, struct bmc_device, refcount);
  1936. remove_files(bmc);
  1937. platform_device_unregister(bmc->dev);
  1938. kfree(bmc);
  1939. }
  1940. static void ipmi_bmc_unregister(ipmi_smi_t intf)
  1941. {
  1942. struct bmc_device *bmc = intf->bmc;
  1943. if (intf->sysfs_name) {
  1944. sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
  1945. kfree(intf->sysfs_name);
  1946. intf->sysfs_name = NULL;
  1947. }
  1948. if (intf->my_dev_name) {
  1949. sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
  1950. kfree(intf->my_dev_name);
  1951. intf->my_dev_name = NULL;
  1952. }
  1953. mutex_lock(&ipmidriver_mutex);
  1954. kref_put(&bmc->refcount, cleanup_bmc_device);
  1955. intf->bmc = NULL;
  1956. mutex_unlock(&ipmidriver_mutex);
  1957. }
  1958. static int create_files(struct bmc_device *bmc)
  1959. {
  1960. int err;
  1961. bmc->device_id_attr.attr.name = "device_id";
  1962. bmc->device_id_attr.attr.mode = S_IRUGO;
  1963. bmc->device_id_attr.show = device_id_show;
  1964. sysfs_attr_init(&bmc->device_id_attr.attr);
  1965. bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
  1966. bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
  1967. bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
  1968. sysfs_attr_init(&bmc->provides_dev_sdrs_attr.attr);
  1969. bmc->revision_attr.attr.name = "revision";
  1970. bmc->revision_attr.attr.mode = S_IRUGO;
  1971. bmc->revision_attr.show = revision_show;
  1972. sysfs_attr_init(&bmc->revision_attr.attr);
  1973. bmc->firmware_rev_attr.attr.name = "firmware_revision";
  1974. bmc->firmware_rev_attr.attr.mode = S_IRUGO;
  1975. bmc->firmware_rev_attr.show = firmware_rev_show;
  1976. sysfs_attr_init(&bmc->firmware_rev_attr.attr);
  1977. bmc->version_attr.attr.name = "ipmi_version";
  1978. bmc->version_attr.attr.mode = S_IRUGO;
  1979. bmc->version_attr.show = ipmi_version_show;
  1980. sysfs_attr_init(&bmc->version_attr.attr);
  1981. bmc->add_dev_support_attr.attr.name = "additional_device_support";
  1982. bmc->add_dev_support_attr.attr.mode = S_IRUGO;
  1983. bmc->add_dev_support_attr.show = add_dev_support_show;
  1984. sysfs_attr_init(&bmc->add_dev_support_attr.attr);
  1985. bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
  1986. bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
  1987. bmc->manufacturer_id_attr.show = manufacturer_id_show;
  1988. sysfs_attr_init(&bmc->manufacturer_id_attr.attr);
  1989. bmc->product_id_attr.attr.name = "product_id";
  1990. bmc->product_id_attr.attr.mode = S_IRUGO;
  1991. bmc->product_id_attr.show = product_id_show;
  1992. sysfs_attr_init(&bmc->product_id_attr.attr);
  1993. bmc->guid_attr.attr.name = "guid";
  1994. bmc->guid_attr.attr.mode = S_IRUGO;
  1995. bmc->guid_attr.show = guid_show;
  1996. sysfs_attr_init(&bmc->guid_attr.attr);
  1997. bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
  1998. bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
  1999. bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
  2000. sysfs_attr_init(&bmc->aux_firmware_rev_attr.attr);
  2001. err = device_create_file(&bmc->dev->dev,
  2002. &bmc->device_id_attr);
  2003. if (err)
  2004. goto out;
  2005. err = device_create_file(&bmc->dev->dev,
  2006. &bmc->provides_dev_sdrs_attr);
  2007. if (err)
  2008. goto out_devid;
  2009. err = device_create_file(&bmc->dev->dev,
  2010. &bmc->revision_attr);
  2011. if (err)
  2012. goto out_sdrs;
  2013. err = device_create_file(&bmc->dev->dev,
  2014. &bmc->firmware_rev_attr);
  2015. if (err)
  2016. goto out_rev;
  2017. err = device_create_file(&bmc->dev->dev,
  2018. &bmc->version_attr);
  2019. if (err)
  2020. goto out_firm;
  2021. err = device_create_file(&bmc->dev->dev,
  2022. &bmc->add_dev_support_attr);
  2023. if (err)
  2024. goto out_version;
  2025. err = device_create_file(&bmc->dev->dev,
  2026. &bmc->manufacturer_id_attr);
  2027. if (err)
  2028. goto out_add_dev;
  2029. err = device_create_file(&bmc->dev->dev,
  2030. &bmc->product_id_attr);
  2031. if (err)
  2032. goto out_manu;
  2033. if (bmc->id.aux_firmware_revision_set) {
  2034. err = device_create_file(&bmc->dev->dev,
  2035. &bmc->aux_firmware_rev_attr);
  2036. if (err)
  2037. goto out_prod_id;
  2038. }
  2039. if (bmc->guid_set) {
  2040. err = device_create_file(&bmc->dev->dev,
  2041. &bmc->guid_attr);
  2042. if (err)
  2043. goto out_aux_firm;
  2044. }
  2045. return 0;
  2046. out_aux_firm:
  2047. if (bmc->id.aux_firmware_revision_set)
  2048. device_remove_file(&bmc->dev->dev,
  2049. &bmc->aux_firmware_rev_attr);
  2050. out_prod_id:
  2051. device_remove_file(&bmc->dev->dev,
  2052. &bmc->product_id_attr);
  2053. out_manu:
  2054. device_remove_file(&bmc->dev->dev,
  2055. &bmc->manufacturer_id_attr);
  2056. out_add_dev:
  2057. device_remove_file(&bmc->dev->dev,
  2058. &bmc->add_dev_support_attr);
  2059. out_version:
  2060. device_remove_file(&bmc->dev->dev,
  2061. &bmc->version_attr);
  2062. out_firm:
  2063. device_remove_file(&bmc->dev->dev,
  2064. &bmc->firmware_rev_attr);
  2065. out_rev:
  2066. device_remove_file(&bmc->dev->dev,
  2067. &bmc->revision_attr);
  2068. out_sdrs:
  2069. device_remove_file(&bmc->dev->dev,
  2070. &bmc->provides_dev_sdrs_attr);
  2071. out_devid:
  2072. device_remove_file(&bmc->dev->dev,
  2073. &bmc->device_id_attr);
  2074. out:
  2075. return err;
  2076. }
  2077. static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
  2078. const char *sysfs_name)
  2079. {
  2080. int rv;
  2081. struct bmc_device *bmc = intf->bmc;
  2082. struct bmc_device *old_bmc;
  2083. int size;
  2084. char dummy[1];
  2085. mutex_lock(&ipmidriver_mutex);
  2086. /*
  2087. * Try to find if there is an bmc_device struct
  2088. * representing the interfaced BMC already
  2089. */
  2090. if (bmc->guid_set)
  2091. old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
  2092. else
  2093. old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
  2094. bmc->id.product_id,
  2095. bmc->id.device_id);
  2096. /*
  2097. * If there is already an bmc_device, free the new one,
  2098. * otherwise register the new BMC device
  2099. */
  2100. if (old_bmc) {
  2101. kfree(bmc);
  2102. intf->bmc = old_bmc;
  2103. bmc = old_bmc;
  2104. kref_get(&bmc->refcount);
  2105. mutex_unlock(&ipmidriver_mutex);
  2106. printk(KERN_INFO
  2107. "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
  2108. " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2109. bmc->id.manufacturer_id,
  2110. bmc->id.product_id,
  2111. bmc->id.device_id);
  2112. } else {
  2113. char name[14];
  2114. unsigned char orig_dev_id = bmc->id.device_id;
  2115. int warn_printed = 0;
  2116. snprintf(name, sizeof(name),
  2117. "ipmi_bmc.%4.4x", bmc->id.product_id);
  2118. while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
  2119. bmc->id.product_id,
  2120. bmc->id.device_id)) {
  2121. if (!warn_printed) {
  2122. printk(KERN_WARNING PFX
  2123. "This machine has two different BMCs"
  2124. " with the same product id and device"
  2125. " id. This is an error in the"
  2126. " firmware, but incrementing the"
  2127. " device id to work around the problem."
  2128. " Prod ID = 0x%x, Dev ID = 0x%x\n",
  2129. bmc->id.product_id, bmc->id.device_id);
  2130. warn_printed = 1;
  2131. }
  2132. bmc->id.device_id++; /* Wraps at 255 */
  2133. if (bmc->id.device_id == orig_dev_id) {
  2134. printk(KERN_ERR PFX
  2135. "Out of device ids!\n");
  2136. break;
  2137. }
  2138. }
  2139. bmc->dev = platform_device_alloc(name, bmc->id.device_id);
  2140. if (!bmc->dev) {
  2141. mutex_unlock(&ipmidriver_mutex);
  2142. printk(KERN_ERR
  2143. "ipmi_msghandler:"
  2144. " Unable to allocate platform device\n");
  2145. return -ENOMEM;
  2146. }
  2147. bmc->dev->dev.driver = &ipmidriver.driver;
  2148. dev_set_drvdata(&bmc->dev->dev, bmc);
  2149. kref_init(&bmc->refcount);
  2150. rv = platform_device_add(bmc->dev);
  2151. mutex_unlock(&ipmidriver_mutex);
  2152. if (rv) {
  2153. platform_device_put(bmc->dev);
  2154. bmc->dev = NULL;
  2155. printk(KERN_ERR
  2156. "ipmi_msghandler:"
  2157. " Unable to register bmc device: %d\n",
  2158. rv);
  2159. /*
  2160. * Don't go to out_err, you can only do that if
  2161. * the device is registered already.
  2162. */
  2163. return rv;
  2164. }
  2165. rv = create_files(bmc);
  2166. if (rv) {
  2167. mutex_lock(&ipmidriver_mutex);
  2168. platform_device_unregister(bmc->dev);
  2169. mutex_unlock(&ipmidriver_mutex);
  2170. return rv;
  2171. }
  2172. dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, "
  2173. "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
  2174. bmc->id.manufacturer_id,
  2175. bmc->id.product_id,
  2176. bmc->id.device_id);
  2177. }
  2178. /*
  2179. * create symlink from system interface device to bmc device
  2180. * and back.
  2181. */
  2182. intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
  2183. if (!intf->sysfs_name) {
  2184. rv = -ENOMEM;
  2185. printk(KERN_ERR
  2186. "ipmi_msghandler: allocate link to BMC: %d\n",
  2187. rv);
  2188. goto out_err;
  2189. }
  2190. rv = sysfs_create_link(&intf->si_dev->kobj,
  2191. &bmc->dev->dev.kobj, intf->sysfs_name);
  2192. if (rv) {
  2193. kfree(intf->sysfs_name);
  2194. intf->sysfs_name = NULL;
  2195. printk(KERN_ERR
  2196. "ipmi_msghandler: Unable to create bmc symlink: %d\n",
  2197. rv);
  2198. goto out_err;
  2199. }
  2200. size = snprintf(dummy, 0, "ipmi%d", ifnum);
  2201. intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
  2202. if (!intf->my_dev_name) {
  2203. kfree(intf->sysfs_name);
  2204. intf->sysfs_name = NULL;
  2205. rv = -ENOMEM;
  2206. printk(KERN_ERR
  2207. "ipmi_msghandler: allocate link from BMC: %d\n",
  2208. rv);
  2209. goto out_err;
  2210. }
  2211. snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
  2212. rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
  2213. intf->my_dev_name);
  2214. if (rv) {
  2215. kfree(intf->sysfs_name);
  2216. intf->sysfs_name = NULL;
  2217. kfree(intf->my_dev_name);
  2218. intf->my_dev_name = NULL;
  2219. printk(KERN_ERR
  2220. "ipmi_msghandler:"
  2221. " Unable to create symlink to bmc: %d\n",
  2222. rv);
  2223. goto out_err;
  2224. }
  2225. return 0;
  2226. out_err:
  2227. ipmi_bmc_unregister(intf);
  2228. return rv;
  2229. }
  2230. static int
  2231. send_guid_cmd(ipmi_smi_t intf, int chan)
  2232. {
  2233. struct kernel_ipmi_msg msg;
  2234. struct ipmi_system_interface_addr si;
  2235. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2236. si.channel = IPMI_BMC_CHANNEL;
  2237. si.lun = 0;
  2238. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2239. msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
  2240. msg.data = NULL;
  2241. msg.data_len = 0;
  2242. return i_ipmi_request(NULL,
  2243. intf,
  2244. (struct ipmi_addr *) &si,
  2245. 0,
  2246. &msg,
  2247. intf,
  2248. NULL,
  2249. NULL,
  2250. 0,
  2251. intf->channels[0].address,
  2252. intf->channels[0].lun,
  2253. -1, 0);
  2254. }
  2255. static void
  2256. guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  2257. {
  2258. if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2259. || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
  2260. || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
  2261. /* Not for me */
  2262. return;
  2263. if (msg->msg.data[0] != 0) {
  2264. /* Error from getting the GUID, the BMC doesn't have one. */
  2265. intf->bmc->guid_set = 0;
  2266. goto out;
  2267. }
  2268. if (msg->msg.data_len < 17) {
  2269. intf->bmc->guid_set = 0;
  2270. printk(KERN_WARNING PFX
  2271. "guid_handler: The GUID response from the BMC was too"
  2272. " short, it was %d but should have been 17. Assuming"
  2273. " GUID is not available.\n",
  2274. msg->msg.data_len);
  2275. goto out;
  2276. }
  2277. memcpy(intf->bmc->guid, msg->msg.data, 16);
  2278. intf->bmc->guid_set = 1;
  2279. out:
  2280. wake_up(&intf->waitq);
  2281. }
  2282. static void
  2283. get_guid(ipmi_smi_t intf)
  2284. {
  2285. int rv;
  2286. intf->bmc->guid_set = 0x2;
  2287. intf->null_user_handler = guid_handler;
  2288. rv = send_guid_cmd(intf, 0);
  2289. if (rv)
  2290. /* Send failed, no GUID available. */
  2291. intf->bmc->guid_set = 0;
  2292. wait_event(intf->waitq, intf->bmc->guid_set != 2);
  2293. intf->null_user_handler = NULL;
  2294. }
  2295. static int
  2296. send_channel_info_cmd(ipmi_smi_t intf, int chan)
  2297. {
  2298. struct kernel_ipmi_msg msg;
  2299. unsigned char data[1];
  2300. struct ipmi_system_interface_addr si;
  2301. si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2302. si.channel = IPMI_BMC_CHANNEL;
  2303. si.lun = 0;
  2304. msg.netfn = IPMI_NETFN_APP_REQUEST;
  2305. msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
  2306. msg.data = data;
  2307. msg.data_len = 1;
  2308. data[0] = chan;
  2309. return i_ipmi_request(NULL,
  2310. intf,
  2311. (struct ipmi_addr *) &si,
  2312. 0,
  2313. &msg,
  2314. intf,
  2315. NULL,
  2316. NULL,
  2317. 0,
  2318. intf->channels[0].address,
  2319. intf->channels[0].lun,
  2320. -1, 0);
  2321. }
  2322. static void
  2323. channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  2324. {
  2325. int rv = 0;
  2326. int chan;
  2327. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  2328. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  2329. && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
  2330. /* It's the one we want */
  2331. if (msg->msg.data[0] != 0) {
  2332. /* Got an error from the channel, just go on. */
  2333. if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
  2334. /*
  2335. * If the MC does not support this
  2336. * command, that is legal. We just
  2337. * assume it has one IPMB at channel
  2338. * zero.
  2339. */
  2340. intf->channels[0].medium
  2341. = IPMI_CHANNEL_MEDIUM_IPMB;
  2342. intf->channels[0].protocol
  2343. = IPMI_CHANNEL_PROTOCOL_IPMB;
  2344. rv = -ENOSYS;
  2345. intf->curr_channel = IPMI_MAX_CHANNELS;
  2346. wake_up(&intf->waitq);
  2347. goto out;
  2348. }
  2349. goto next_channel;
  2350. }
  2351. if (msg->msg.data_len < 4) {
  2352. /* Message not big enough, just go on. */
  2353. goto next_channel;
  2354. }
  2355. chan = intf->curr_channel;
  2356. intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
  2357. intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
  2358. next_channel:
  2359. intf->curr_channel++;
  2360. if (intf->curr_channel >= IPMI_MAX_CHANNELS)
  2361. wake_up(&intf->waitq);
  2362. else
  2363. rv = send_channel_info_cmd(intf, intf->curr_channel);
  2364. if (rv) {
  2365. /* Got an error somehow, just give up. */
  2366. intf->curr_channel = IPMI_MAX_CHANNELS;
  2367. wake_up(&intf->waitq);
  2368. printk(KERN_WARNING PFX
  2369. "Error sending channel information: %d\n",
  2370. rv);
  2371. }
  2372. }
  2373. out:
  2374. return;
  2375. }
  2376. void ipmi_poll_interface(ipmi_user_t user)
  2377. {
  2378. ipmi_smi_t intf = user->intf;
  2379. if (intf->handlers->poll)
  2380. intf->handlers->poll(intf->send_info);
  2381. }
  2382. EXPORT_SYMBOL(ipmi_poll_interface);
  2383. int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
  2384. void *send_info,
  2385. struct ipmi_device_id *device_id,
  2386. struct device *si_dev,
  2387. const char *sysfs_name,
  2388. unsigned char slave_addr)
  2389. {
  2390. int i, j;
  2391. int rv;
  2392. ipmi_smi_t intf;
  2393. ipmi_smi_t tintf;
  2394. struct list_head *link;
  2395. /*
  2396. * Make sure the driver is actually initialized, this handles
  2397. * problems with initialization order.
  2398. */
  2399. if (!initialized) {
  2400. rv = ipmi_init_msghandler();
  2401. if (rv)
  2402. return rv;
  2403. /*
  2404. * The init code doesn't return an error if it was turned
  2405. * off, but it won't initialize. Check that.
  2406. */
  2407. if (!initialized)
  2408. return -ENODEV;
  2409. }
  2410. intf = kzalloc(sizeof(*intf), GFP_KERNEL);
  2411. if (!intf)
  2412. return -ENOMEM;
  2413. intf->ipmi_version_major = ipmi_version_major(device_id);
  2414. intf->ipmi_version_minor = ipmi_version_minor(device_id);
  2415. intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
  2416. if (!intf->bmc) {
  2417. kfree(intf);
  2418. return -ENOMEM;
  2419. }
  2420. intf->intf_num = -1; /* Mark it invalid for now. */
  2421. kref_init(&intf->refcount);
  2422. intf->bmc->id = *device_id;
  2423. intf->si_dev = si_dev;
  2424. for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
  2425. intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
  2426. intf->channels[j].lun = 2;
  2427. }
  2428. if (slave_addr != 0)
  2429. intf->channels[0].address = slave_addr;
  2430. INIT_LIST_HEAD(&intf->users);
  2431. intf->handlers = handlers;
  2432. intf->send_info = send_info;
  2433. spin_lock_init(&intf->seq_lock);
  2434. for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
  2435. intf->seq_table[j].inuse = 0;
  2436. intf->seq_table[j].seqid = 0;
  2437. }
  2438. intf->curr_seq = 0;
  2439. #ifdef CONFIG_PROC_FS
  2440. mutex_init(&intf->proc_entry_lock);
  2441. #endif
  2442. spin_lock_init(&intf->waiting_msgs_lock);
  2443. INIT_LIST_HEAD(&intf->waiting_msgs);
  2444. spin_lock_init(&intf->events_lock);
  2445. INIT_LIST_HEAD(&intf->waiting_events);
  2446. intf->waiting_events_count = 0;
  2447. mutex_init(&intf->cmd_rcvrs_mutex);
  2448. spin_lock_init(&intf->maintenance_mode_lock);
  2449. INIT_LIST_HEAD(&intf->cmd_rcvrs);
  2450. init_waitqueue_head(&intf->waitq);
  2451. for (i = 0; i < IPMI_NUM_STATS; i++)
  2452. atomic_set(&intf->stats[i], 0);
  2453. intf->proc_dir = NULL;
  2454. mutex_lock(&smi_watchers_mutex);
  2455. mutex_lock(&ipmi_interfaces_mutex);
  2456. /* Look for a hole in the numbers. */
  2457. i = 0;
  2458. link = &ipmi_interfaces;
  2459. list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
  2460. if (tintf->intf_num != i) {
  2461. link = &tintf->link;
  2462. break;
  2463. }
  2464. i++;
  2465. }
  2466. /* Add the new interface in numeric order. */
  2467. if (i == 0)
  2468. list_add_rcu(&intf->link, &ipmi_interfaces);
  2469. else
  2470. list_add_tail_rcu(&intf->link, link);
  2471. rv = handlers->start_processing(send_info, intf);
  2472. if (rv)
  2473. goto out;
  2474. get_guid(intf);
  2475. if ((intf->ipmi_version_major > 1)
  2476. || ((intf->ipmi_version_major == 1)
  2477. && (intf->ipmi_version_minor >= 5))) {
  2478. /*
  2479. * Start scanning the channels to see what is
  2480. * available.
  2481. */
  2482. intf->null_user_handler = channel_handler;
  2483. intf->curr_channel = 0;
  2484. rv = send_channel_info_cmd(intf, 0);
  2485. if (rv)
  2486. goto out;
  2487. /* Wait for the channel info to be read. */
  2488. wait_event(intf->waitq,
  2489. intf->curr_channel >= IPMI_MAX_CHANNELS);
  2490. intf->null_user_handler = NULL;
  2491. } else {
  2492. /* Assume a single IPMB channel at zero. */
  2493. intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
  2494. intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
  2495. intf->curr_channel = IPMI_MAX_CHANNELS;
  2496. }
  2497. if (rv == 0)
  2498. rv = add_proc_entries(intf, i);
  2499. rv = ipmi_bmc_register(intf, i, sysfs_name);
  2500. out:
  2501. if (rv) {
  2502. if (intf->proc_dir)
  2503. remove_proc_entries(intf);
  2504. intf->handlers = NULL;
  2505. list_del_rcu(&intf->link);
  2506. mutex_unlock(&ipmi_interfaces_mutex);
  2507. mutex_unlock(&smi_watchers_mutex);
  2508. synchronize_rcu();
  2509. kref_put(&intf->refcount, intf_free);
  2510. } else {
  2511. /*
  2512. * Keep memory order straight for RCU readers. Make
  2513. * sure everything else is committed to memory before
  2514. * setting intf_num to mark the interface valid.
  2515. */
  2516. smp_wmb();
  2517. intf->intf_num = i;
  2518. mutex_unlock(&ipmi_interfaces_mutex);
  2519. /* After this point the interface is legal to use. */
  2520. call_smi_watchers(i, intf->si_dev);
  2521. mutex_unlock(&smi_watchers_mutex);
  2522. }
  2523. return rv;
  2524. }
  2525. EXPORT_SYMBOL(ipmi_register_smi);
  2526. static void cleanup_smi_msgs(ipmi_smi_t intf)
  2527. {
  2528. int i;
  2529. struct seq_table *ent;
  2530. /* No need for locks, the interface is down. */
  2531. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
  2532. ent = &(intf->seq_table[i]);
  2533. if (!ent->inuse)
  2534. continue;
  2535. deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
  2536. }
  2537. }
  2538. int ipmi_unregister_smi(ipmi_smi_t intf)
  2539. {
  2540. struct ipmi_smi_watcher *w;
  2541. int intf_num = intf->intf_num;
  2542. ipmi_bmc_unregister(intf);
  2543. mutex_lock(&smi_watchers_mutex);
  2544. mutex_lock(&ipmi_interfaces_mutex);
  2545. intf->intf_num = -1;
  2546. intf->handlers = NULL;
  2547. list_del_rcu(&intf->link);
  2548. mutex_unlock(&ipmi_interfaces_mutex);
  2549. synchronize_rcu();
  2550. cleanup_smi_msgs(intf);
  2551. remove_proc_entries(intf);
  2552. /*
  2553. * Call all the watcher interfaces to tell them that
  2554. * an interface is gone.
  2555. */
  2556. list_for_each_entry(w, &smi_watchers, link)
  2557. w->smi_gone(intf_num);
  2558. mutex_unlock(&smi_watchers_mutex);
  2559. kref_put(&intf->refcount, intf_free);
  2560. return 0;
  2561. }
  2562. EXPORT_SYMBOL(ipmi_unregister_smi);
  2563. static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
  2564. struct ipmi_smi_msg *msg)
  2565. {
  2566. struct ipmi_ipmb_addr ipmb_addr;
  2567. struct ipmi_recv_msg *recv_msg;
  2568. /*
  2569. * This is 11, not 10, because the response must contain a
  2570. * completion code.
  2571. */
  2572. if (msg->rsp_size < 11) {
  2573. /* Message not big enough, just ignore it. */
  2574. ipmi_inc_stat(intf, invalid_ipmb_responses);
  2575. return 0;
  2576. }
  2577. if (msg->rsp[2] != 0) {
  2578. /* An error getting the response, just ignore it. */
  2579. return 0;
  2580. }
  2581. ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
  2582. ipmb_addr.slave_addr = msg->rsp[6];
  2583. ipmb_addr.channel = msg->rsp[3] & 0x0f;
  2584. ipmb_addr.lun = msg->rsp[7] & 3;
  2585. /*
  2586. * It's a response from a remote entity. Look up the sequence
  2587. * number and handle the response.
  2588. */
  2589. if (intf_find_seq(intf,
  2590. msg->rsp[7] >> 2,
  2591. msg->rsp[3] & 0x0f,
  2592. msg->rsp[8],
  2593. (msg->rsp[4] >> 2) & (~1),
  2594. (struct ipmi_addr *) &(ipmb_addr),
  2595. &recv_msg)) {
  2596. /*
  2597. * We were unable to find the sequence number,
  2598. * so just nuke the message.
  2599. */
  2600. ipmi_inc_stat(intf, unhandled_ipmb_responses);
  2601. return 0;
  2602. }
  2603. memcpy(recv_msg->msg_data,
  2604. &(msg->rsp[9]),
  2605. msg->rsp_size - 9);
  2606. /*
  2607. * The other fields matched, so no need to set them, except
  2608. * for netfn, which needs to be the response that was
  2609. * returned, not the request value.
  2610. */
  2611. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  2612. recv_msg->msg.data = recv_msg->msg_data;
  2613. recv_msg->msg.data_len = msg->rsp_size - 10;
  2614. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  2615. ipmi_inc_stat(intf, handled_ipmb_responses);
  2616. deliver_response(recv_msg);
  2617. return 0;
  2618. }
  2619. static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
  2620. struct ipmi_smi_msg *msg)
  2621. {
  2622. struct cmd_rcvr *rcvr;
  2623. int rv = 0;
  2624. unsigned char netfn;
  2625. unsigned char cmd;
  2626. unsigned char chan;
  2627. ipmi_user_t user = NULL;
  2628. struct ipmi_ipmb_addr *ipmb_addr;
  2629. struct ipmi_recv_msg *recv_msg;
  2630. struct ipmi_smi_handlers *handlers;
  2631. if (msg->rsp_size < 10) {
  2632. /* Message not big enough, just ignore it. */
  2633. ipmi_inc_stat(intf, invalid_commands);
  2634. return 0;
  2635. }
  2636. if (msg->rsp[2] != 0) {
  2637. /* An error getting the response, just ignore it. */
  2638. return 0;
  2639. }
  2640. netfn = msg->rsp[4] >> 2;
  2641. cmd = msg->rsp[8];
  2642. chan = msg->rsp[3] & 0xf;
  2643. rcu_read_lock();
  2644. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2645. if (rcvr) {
  2646. user = rcvr->user;
  2647. kref_get(&user->refcount);
  2648. } else
  2649. user = NULL;
  2650. rcu_read_unlock();
  2651. if (user == NULL) {
  2652. /* We didn't find a user, deliver an error response. */
  2653. ipmi_inc_stat(intf, unhandled_commands);
  2654. msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  2655. msg->data[1] = IPMI_SEND_MSG_CMD;
  2656. msg->data[2] = msg->rsp[3];
  2657. msg->data[3] = msg->rsp[6];
  2658. msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
  2659. msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
  2660. msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
  2661. /* rqseq/lun */
  2662. msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
  2663. msg->data[8] = msg->rsp[8]; /* cmd */
  2664. msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
  2665. msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
  2666. msg->data_size = 11;
  2667. #ifdef DEBUG_MSGING
  2668. {
  2669. int m;
  2670. printk("Invalid command:");
  2671. for (m = 0; m < msg->data_size; m++)
  2672. printk(" %2.2x", msg->data[m]);
  2673. printk("\n");
  2674. }
  2675. #endif
  2676. rcu_read_lock();
  2677. handlers = intf->handlers;
  2678. if (handlers) {
  2679. handlers->sender(intf->send_info, msg, 0);
  2680. /*
  2681. * We used the message, so return the value
  2682. * that causes it to not be freed or
  2683. * queued.
  2684. */
  2685. rv = -1;
  2686. }
  2687. rcu_read_unlock();
  2688. } else {
  2689. /* Deliver the message to the user. */
  2690. ipmi_inc_stat(intf, handled_commands);
  2691. recv_msg = ipmi_alloc_recv_msg();
  2692. if (!recv_msg) {
  2693. /*
  2694. * We couldn't allocate memory for the
  2695. * message, so requeue it for handling
  2696. * later.
  2697. */
  2698. rv = 1;
  2699. kref_put(&user->refcount, free_user);
  2700. } else {
  2701. /* Extract the source address from the data. */
  2702. ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
  2703. ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
  2704. ipmb_addr->slave_addr = msg->rsp[6];
  2705. ipmb_addr->lun = msg->rsp[7] & 3;
  2706. ipmb_addr->channel = msg->rsp[3] & 0xf;
  2707. /*
  2708. * Extract the rest of the message information
  2709. * from the IPMB header.
  2710. */
  2711. recv_msg->user = user;
  2712. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  2713. recv_msg->msgid = msg->rsp[7] >> 2;
  2714. recv_msg->msg.netfn = msg->rsp[4] >> 2;
  2715. recv_msg->msg.cmd = msg->rsp[8];
  2716. recv_msg->msg.data = recv_msg->msg_data;
  2717. /*
  2718. * We chop off 10, not 9 bytes because the checksum
  2719. * at the end also needs to be removed.
  2720. */
  2721. recv_msg->msg.data_len = msg->rsp_size - 10;
  2722. memcpy(recv_msg->msg_data,
  2723. &(msg->rsp[9]),
  2724. msg->rsp_size - 10);
  2725. deliver_response(recv_msg);
  2726. }
  2727. }
  2728. return rv;
  2729. }
  2730. static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
  2731. struct ipmi_smi_msg *msg)
  2732. {
  2733. struct ipmi_lan_addr lan_addr;
  2734. struct ipmi_recv_msg *recv_msg;
  2735. /*
  2736. * This is 13, not 12, because the response must contain a
  2737. * completion code.
  2738. */
  2739. if (msg->rsp_size < 13) {
  2740. /* Message not big enough, just ignore it. */
  2741. ipmi_inc_stat(intf, invalid_lan_responses);
  2742. return 0;
  2743. }
  2744. if (msg->rsp[2] != 0) {
  2745. /* An error getting the response, just ignore it. */
  2746. return 0;
  2747. }
  2748. lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
  2749. lan_addr.session_handle = msg->rsp[4];
  2750. lan_addr.remote_SWID = msg->rsp[8];
  2751. lan_addr.local_SWID = msg->rsp[5];
  2752. lan_addr.channel = msg->rsp[3] & 0x0f;
  2753. lan_addr.privilege = msg->rsp[3] >> 4;
  2754. lan_addr.lun = msg->rsp[9] & 3;
  2755. /*
  2756. * It's a response from a remote entity. Look up the sequence
  2757. * number and handle the response.
  2758. */
  2759. if (intf_find_seq(intf,
  2760. msg->rsp[9] >> 2,
  2761. msg->rsp[3] & 0x0f,
  2762. msg->rsp[10],
  2763. (msg->rsp[6] >> 2) & (~1),
  2764. (struct ipmi_addr *) &(lan_addr),
  2765. &recv_msg)) {
  2766. /*
  2767. * We were unable to find the sequence number,
  2768. * so just nuke the message.
  2769. */
  2770. ipmi_inc_stat(intf, unhandled_lan_responses);
  2771. return 0;
  2772. }
  2773. memcpy(recv_msg->msg_data,
  2774. &(msg->rsp[11]),
  2775. msg->rsp_size - 11);
  2776. /*
  2777. * The other fields matched, so no need to set them, except
  2778. * for netfn, which needs to be the response that was
  2779. * returned, not the request value.
  2780. */
  2781. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  2782. recv_msg->msg.data = recv_msg->msg_data;
  2783. recv_msg->msg.data_len = msg->rsp_size - 12;
  2784. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  2785. ipmi_inc_stat(intf, handled_lan_responses);
  2786. deliver_response(recv_msg);
  2787. return 0;
  2788. }
  2789. static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
  2790. struct ipmi_smi_msg *msg)
  2791. {
  2792. struct cmd_rcvr *rcvr;
  2793. int rv = 0;
  2794. unsigned char netfn;
  2795. unsigned char cmd;
  2796. unsigned char chan;
  2797. ipmi_user_t user = NULL;
  2798. struct ipmi_lan_addr *lan_addr;
  2799. struct ipmi_recv_msg *recv_msg;
  2800. if (msg->rsp_size < 12) {
  2801. /* Message not big enough, just ignore it. */
  2802. ipmi_inc_stat(intf, invalid_commands);
  2803. return 0;
  2804. }
  2805. if (msg->rsp[2] != 0) {
  2806. /* An error getting the response, just ignore it. */
  2807. return 0;
  2808. }
  2809. netfn = msg->rsp[6] >> 2;
  2810. cmd = msg->rsp[10];
  2811. chan = msg->rsp[3] & 0xf;
  2812. rcu_read_lock();
  2813. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2814. if (rcvr) {
  2815. user = rcvr->user;
  2816. kref_get(&user->refcount);
  2817. } else
  2818. user = NULL;
  2819. rcu_read_unlock();
  2820. if (user == NULL) {
  2821. /* We didn't find a user, just give up. */
  2822. ipmi_inc_stat(intf, unhandled_commands);
  2823. /*
  2824. * Don't do anything with these messages, just allow
  2825. * them to be freed.
  2826. */
  2827. rv = 0;
  2828. } else {
  2829. /* Deliver the message to the user. */
  2830. ipmi_inc_stat(intf, handled_commands);
  2831. recv_msg = ipmi_alloc_recv_msg();
  2832. if (!recv_msg) {
  2833. /*
  2834. * We couldn't allocate memory for the
  2835. * message, so requeue it for handling later.
  2836. */
  2837. rv = 1;
  2838. kref_put(&user->refcount, free_user);
  2839. } else {
  2840. /* Extract the source address from the data. */
  2841. lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
  2842. lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
  2843. lan_addr->session_handle = msg->rsp[4];
  2844. lan_addr->remote_SWID = msg->rsp[8];
  2845. lan_addr->local_SWID = msg->rsp[5];
  2846. lan_addr->lun = msg->rsp[9] & 3;
  2847. lan_addr->channel = msg->rsp[3] & 0xf;
  2848. lan_addr->privilege = msg->rsp[3] >> 4;
  2849. /*
  2850. * Extract the rest of the message information
  2851. * from the IPMB header.
  2852. */
  2853. recv_msg->user = user;
  2854. recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
  2855. recv_msg->msgid = msg->rsp[9] >> 2;
  2856. recv_msg->msg.netfn = msg->rsp[6] >> 2;
  2857. recv_msg->msg.cmd = msg->rsp[10];
  2858. recv_msg->msg.data = recv_msg->msg_data;
  2859. /*
  2860. * We chop off 12, not 11 bytes because the checksum
  2861. * at the end also needs to be removed.
  2862. */
  2863. recv_msg->msg.data_len = msg->rsp_size - 12;
  2864. memcpy(recv_msg->msg_data,
  2865. &(msg->rsp[11]),
  2866. msg->rsp_size - 12);
  2867. deliver_response(recv_msg);
  2868. }
  2869. }
  2870. return rv;
  2871. }
  2872. /*
  2873. * This routine will handle "Get Message" command responses with
  2874. * channels that use an OEM Medium. The message format belongs to
  2875. * the OEM. See IPMI 2.0 specification, Chapter 6 and
  2876. * Chapter 22, sections 22.6 and 22.24 for more details.
  2877. */
  2878. static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
  2879. struct ipmi_smi_msg *msg)
  2880. {
  2881. struct cmd_rcvr *rcvr;
  2882. int rv = 0;
  2883. unsigned char netfn;
  2884. unsigned char cmd;
  2885. unsigned char chan;
  2886. ipmi_user_t user = NULL;
  2887. struct ipmi_system_interface_addr *smi_addr;
  2888. struct ipmi_recv_msg *recv_msg;
  2889. /*
  2890. * We expect the OEM SW to perform error checking
  2891. * so we just do some basic sanity checks
  2892. */
  2893. if (msg->rsp_size < 4) {
  2894. /* Message not big enough, just ignore it. */
  2895. ipmi_inc_stat(intf, invalid_commands);
  2896. return 0;
  2897. }
  2898. if (msg->rsp[2] != 0) {
  2899. /* An error getting the response, just ignore it. */
  2900. return 0;
  2901. }
  2902. /*
  2903. * This is an OEM Message so the OEM needs to know how
  2904. * handle the message. We do no interpretation.
  2905. */
  2906. netfn = msg->rsp[0] >> 2;
  2907. cmd = msg->rsp[1];
  2908. chan = msg->rsp[3] & 0xf;
  2909. rcu_read_lock();
  2910. rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
  2911. if (rcvr) {
  2912. user = rcvr->user;
  2913. kref_get(&user->refcount);
  2914. } else
  2915. user = NULL;
  2916. rcu_read_unlock();
  2917. if (user == NULL) {
  2918. /* We didn't find a user, just give up. */
  2919. ipmi_inc_stat(intf, unhandled_commands);
  2920. /*
  2921. * Don't do anything with these messages, just allow
  2922. * them to be freed.
  2923. */
  2924. rv = 0;
  2925. } else {
  2926. /* Deliver the message to the user. */
  2927. ipmi_inc_stat(intf, handled_commands);
  2928. recv_msg = ipmi_alloc_recv_msg();
  2929. if (!recv_msg) {
  2930. /*
  2931. * We couldn't allocate memory for the
  2932. * message, so requeue it for handling
  2933. * later.
  2934. */
  2935. rv = 1;
  2936. kref_put(&user->refcount, free_user);
  2937. } else {
  2938. /*
  2939. * OEM Messages are expected to be delivered via
  2940. * the system interface to SMS software. We might
  2941. * need to visit this again depending on OEM
  2942. * requirements
  2943. */
  2944. smi_addr = ((struct ipmi_system_interface_addr *)
  2945. &(recv_msg->addr));
  2946. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2947. smi_addr->channel = IPMI_BMC_CHANNEL;
  2948. smi_addr->lun = msg->rsp[0] & 3;
  2949. recv_msg->user = user;
  2950. recv_msg->user_msg_data = NULL;
  2951. recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
  2952. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  2953. recv_msg->msg.cmd = msg->rsp[1];
  2954. recv_msg->msg.data = recv_msg->msg_data;
  2955. /*
  2956. * The message starts at byte 4 which follows the
  2957. * the Channel Byte in the "GET MESSAGE" command
  2958. */
  2959. recv_msg->msg.data_len = msg->rsp_size - 4;
  2960. memcpy(recv_msg->msg_data,
  2961. &(msg->rsp[4]),
  2962. msg->rsp_size - 4);
  2963. deliver_response(recv_msg);
  2964. }
  2965. }
  2966. return rv;
  2967. }
  2968. static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
  2969. struct ipmi_smi_msg *msg)
  2970. {
  2971. struct ipmi_system_interface_addr *smi_addr;
  2972. recv_msg->msgid = 0;
  2973. smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
  2974. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  2975. smi_addr->channel = IPMI_BMC_CHANNEL;
  2976. smi_addr->lun = msg->rsp[0] & 3;
  2977. recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
  2978. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  2979. recv_msg->msg.cmd = msg->rsp[1];
  2980. memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
  2981. recv_msg->msg.data = recv_msg->msg_data;
  2982. recv_msg->msg.data_len = msg->rsp_size - 3;
  2983. }
  2984. static int handle_read_event_rsp(ipmi_smi_t intf,
  2985. struct ipmi_smi_msg *msg)
  2986. {
  2987. struct ipmi_recv_msg *recv_msg, *recv_msg2;
  2988. struct list_head msgs;
  2989. ipmi_user_t user;
  2990. int rv = 0;
  2991. int deliver_count = 0;
  2992. unsigned long flags;
  2993. if (msg->rsp_size < 19) {
  2994. /* Message is too small to be an IPMB event. */
  2995. ipmi_inc_stat(intf, invalid_events);
  2996. return 0;
  2997. }
  2998. if (msg->rsp[2] != 0) {
  2999. /* An error getting the event, just ignore it. */
  3000. return 0;
  3001. }
  3002. INIT_LIST_HEAD(&msgs);
  3003. spin_lock_irqsave(&intf->events_lock, flags);
  3004. ipmi_inc_stat(intf, events);
  3005. /*
  3006. * Allocate and fill in one message for every user that is
  3007. * getting events.
  3008. */
  3009. rcu_read_lock();
  3010. list_for_each_entry_rcu(user, &intf->users, link) {
  3011. if (!user->gets_events)
  3012. continue;
  3013. recv_msg = ipmi_alloc_recv_msg();
  3014. if (!recv_msg) {
  3015. rcu_read_unlock();
  3016. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
  3017. link) {
  3018. list_del(&recv_msg->link);
  3019. ipmi_free_recv_msg(recv_msg);
  3020. }
  3021. /*
  3022. * We couldn't allocate memory for the
  3023. * message, so requeue it for handling
  3024. * later.
  3025. */
  3026. rv = 1;
  3027. goto out;
  3028. }
  3029. deliver_count++;
  3030. copy_event_into_recv_msg(recv_msg, msg);
  3031. recv_msg->user = user;
  3032. kref_get(&user->refcount);
  3033. list_add_tail(&(recv_msg->link), &msgs);
  3034. }
  3035. rcu_read_unlock();
  3036. if (deliver_count) {
  3037. /* Now deliver all the messages. */
  3038. list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
  3039. list_del(&recv_msg->link);
  3040. deliver_response(recv_msg);
  3041. }
  3042. } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
  3043. /*
  3044. * No one to receive the message, put it in queue if there's
  3045. * not already too many things in the queue.
  3046. */
  3047. recv_msg = ipmi_alloc_recv_msg();
  3048. if (!recv_msg) {
  3049. /*
  3050. * We couldn't allocate memory for the
  3051. * message, so requeue it for handling
  3052. * later.
  3053. */
  3054. rv = 1;
  3055. goto out;
  3056. }
  3057. copy_event_into_recv_msg(recv_msg, msg);
  3058. list_add_tail(&(recv_msg->link), &(intf->waiting_events));
  3059. intf->waiting_events_count++;
  3060. } else if (!intf->event_msg_printed) {
  3061. /*
  3062. * There's too many things in the queue, discard this
  3063. * message.
  3064. */
  3065. printk(KERN_WARNING PFX "Event queue full, discarding"
  3066. " incoming events\n");
  3067. intf->event_msg_printed = 1;
  3068. }
  3069. out:
  3070. spin_unlock_irqrestore(&(intf->events_lock), flags);
  3071. return rv;
  3072. }
  3073. static int handle_bmc_rsp(ipmi_smi_t intf,
  3074. struct ipmi_smi_msg *msg)
  3075. {
  3076. struct ipmi_recv_msg *recv_msg;
  3077. struct ipmi_user *user;
  3078. recv_msg = (struct ipmi_recv_msg *) msg->user_data;
  3079. if (recv_msg == NULL) {
  3080. printk(KERN_WARNING
  3081. "IPMI message received with no owner. This\n"
  3082. "could be because of a malformed message, or\n"
  3083. "because of a hardware error. Contact your\n"
  3084. "hardware vender for assistance\n");
  3085. return 0;
  3086. }
  3087. user = recv_msg->user;
  3088. /* Make sure the user still exists. */
  3089. if (user && !user->valid) {
  3090. /* The user for the message went away, so give up. */
  3091. ipmi_inc_stat(intf, unhandled_local_responses);
  3092. ipmi_free_recv_msg(recv_msg);
  3093. } else {
  3094. struct ipmi_system_interface_addr *smi_addr;
  3095. ipmi_inc_stat(intf, handled_local_responses);
  3096. recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  3097. recv_msg->msgid = msg->msgid;
  3098. smi_addr = ((struct ipmi_system_interface_addr *)
  3099. &(recv_msg->addr));
  3100. smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3101. smi_addr->channel = IPMI_BMC_CHANNEL;
  3102. smi_addr->lun = msg->rsp[0] & 3;
  3103. recv_msg->msg.netfn = msg->rsp[0] >> 2;
  3104. recv_msg->msg.cmd = msg->rsp[1];
  3105. memcpy(recv_msg->msg_data,
  3106. &(msg->rsp[2]),
  3107. msg->rsp_size - 2);
  3108. recv_msg->msg.data = recv_msg->msg_data;
  3109. recv_msg->msg.data_len = msg->rsp_size - 2;
  3110. deliver_response(recv_msg);
  3111. }
  3112. return 0;
  3113. }
  3114. /*
  3115. * Handle a new message. Return 1 if the message should be requeued,
  3116. * 0 if the message should be freed, or -1 if the message should not
  3117. * be freed or requeued.
  3118. */
  3119. static int handle_new_recv_msg(ipmi_smi_t intf,
  3120. struct ipmi_smi_msg *msg)
  3121. {
  3122. int requeue;
  3123. int chan;
  3124. #ifdef DEBUG_MSGING
  3125. int m;
  3126. printk("Recv:");
  3127. for (m = 0; m < msg->rsp_size; m++)
  3128. printk(" %2.2x", msg->rsp[m]);
  3129. printk("\n");
  3130. #endif
  3131. if (msg->rsp_size < 2) {
  3132. /* Message is too small to be correct. */
  3133. printk(KERN_WARNING PFX "BMC returned to small a message"
  3134. " for netfn %x cmd %x, got %d bytes\n",
  3135. (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
  3136. /* Generate an error response for the message. */
  3137. msg->rsp[0] = msg->data[0] | (1 << 2);
  3138. msg->rsp[1] = msg->data[1];
  3139. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  3140. msg->rsp_size = 3;
  3141. } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
  3142. || (msg->rsp[1] != msg->data[1])) {
  3143. /*
  3144. * The NetFN and Command in the response is not even
  3145. * marginally correct.
  3146. */
  3147. printk(KERN_WARNING PFX "BMC returned incorrect response,"
  3148. " expected netfn %x cmd %x, got netfn %x cmd %x\n",
  3149. (msg->data[0] >> 2) | 1, msg->data[1],
  3150. msg->rsp[0] >> 2, msg->rsp[1]);
  3151. /* Generate an error response for the message. */
  3152. msg->rsp[0] = msg->data[0] | (1 << 2);
  3153. msg->rsp[1] = msg->data[1];
  3154. msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
  3155. msg->rsp_size = 3;
  3156. }
  3157. if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3158. && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
  3159. && (msg->user_data != NULL)) {
  3160. /*
  3161. * It's a response to a response we sent. For this we
  3162. * deliver a send message response to the user.
  3163. */
  3164. struct ipmi_recv_msg *recv_msg = msg->user_data;
  3165. requeue = 0;
  3166. if (msg->rsp_size < 2)
  3167. /* Message is too small to be correct. */
  3168. goto out;
  3169. chan = msg->data[2] & 0x0f;
  3170. if (chan >= IPMI_MAX_CHANNELS)
  3171. /* Invalid channel number */
  3172. goto out;
  3173. if (!recv_msg)
  3174. goto out;
  3175. /* Make sure the user still exists. */
  3176. if (!recv_msg->user || !recv_msg->user->valid)
  3177. goto out;
  3178. recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
  3179. recv_msg->msg.data = recv_msg->msg_data;
  3180. recv_msg->msg.data_len = 1;
  3181. recv_msg->msg_data[0] = msg->rsp[2];
  3182. deliver_response(recv_msg);
  3183. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3184. && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
  3185. /* It's from the receive queue. */
  3186. chan = msg->rsp[3] & 0xf;
  3187. if (chan >= IPMI_MAX_CHANNELS) {
  3188. /* Invalid channel number */
  3189. requeue = 0;
  3190. goto out;
  3191. }
  3192. /*
  3193. * We need to make sure the channels have been initialized.
  3194. * The channel_handler routine will set the "curr_channel"
  3195. * equal to or greater than IPMI_MAX_CHANNELS when all the
  3196. * channels for this interface have been initialized.
  3197. */
  3198. if (intf->curr_channel < IPMI_MAX_CHANNELS) {
  3199. requeue = 0; /* Throw the message away */
  3200. goto out;
  3201. }
  3202. switch (intf->channels[chan].medium) {
  3203. case IPMI_CHANNEL_MEDIUM_IPMB:
  3204. if (msg->rsp[4] & 0x04) {
  3205. /*
  3206. * It's a response, so find the
  3207. * requesting message and send it up.
  3208. */
  3209. requeue = handle_ipmb_get_msg_rsp(intf, msg);
  3210. } else {
  3211. /*
  3212. * It's a command to the SMS from some other
  3213. * entity. Handle that.
  3214. */
  3215. requeue = handle_ipmb_get_msg_cmd(intf, msg);
  3216. }
  3217. break;
  3218. case IPMI_CHANNEL_MEDIUM_8023LAN:
  3219. case IPMI_CHANNEL_MEDIUM_ASYNC:
  3220. if (msg->rsp[6] & 0x04) {
  3221. /*
  3222. * It's a response, so find the
  3223. * requesting message and send it up.
  3224. */
  3225. requeue = handle_lan_get_msg_rsp(intf, msg);
  3226. } else {
  3227. /*
  3228. * It's a command to the SMS from some other
  3229. * entity. Handle that.
  3230. */
  3231. requeue = handle_lan_get_msg_cmd(intf, msg);
  3232. }
  3233. break;
  3234. default:
  3235. /* Check for OEM Channels. Clients had better
  3236. register for these commands. */
  3237. if ((intf->channels[chan].medium
  3238. >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
  3239. && (intf->channels[chan].medium
  3240. <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
  3241. requeue = handle_oem_get_msg_cmd(intf, msg);
  3242. } else {
  3243. /*
  3244. * We don't handle the channel type, so just
  3245. * free the message.
  3246. */
  3247. requeue = 0;
  3248. }
  3249. }
  3250. } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
  3251. && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
  3252. /* It's an asyncronous event. */
  3253. requeue = handle_read_event_rsp(intf, msg);
  3254. } else {
  3255. /* It's a response from the local BMC. */
  3256. requeue = handle_bmc_rsp(intf, msg);
  3257. }
  3258. out:
  3259. return requeue;
  3260. }
  3261. /* Handle a new message from the lower layer. */
  3262. void ipmi_smi_msg_received(ipmi_smi_t intf,
  3263. struct ipmi_smi_msg *msg)
  3264. {
  3265. unsigned long flags = 0; /* keep us warning-free. */
  3266. int rv;
  3267. int run_to_completion;
  3268. if ((msg->data_size >= 2)
  3269. && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
  3270. && (msg->data[1] == IPMI_SEND_MSG_CMD)
  3271. && (msg->user_data == NULL)) {
  3272. /*
  3273. * This is the local response to a command send, start
  3274. * the timer for these. The user_data will not be
  3275. * NULL if this is a response send, and we will let
  3276. * response sends just go through.
  3277. */
  3278. /*
  3279. * Check for errors, if we get certain errors (ones
  3280. * that mean basically we can try again later), we
  3281. * ignore them and start the timer. Otherwise we
  3282. * report the error immediately.
  3283. */
  3284. if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
  3285. && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
  3286. && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
  3287. && (msg->rsp[2] != IPMI_BUS_ERR)
  3288. && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
  3289. int chan = msg->rsp[3] & 0xf;
  3290. /* Got an error sending the message, handle it. */
  3291. if (chan >= IPMI_MAX_CHANNELS)
  3292. ; /* This shouldn't happen */
  3293. else if ((intf->channels[chan].medium
  3294. == IPMI_CHANNEL_MEDIUM_8023LAN)
  3295. || (intf->channels[chan].medium
  3296. == IPMI_CHANNEL_MEDIUM_ASYNC))
  3297. ipmi_inc_stat(intf, sent_lan_command_errs);
  3298. else
  3299. ipmi_inc_stat(intf, sent_ipmb_command_errs);
  3300. intf_err_seq(intf, msg->msgid, msg->rsp[2]);
  3301. } else
  3302. /* The message was sent, start the timer. */
  3303. intf_start_seq_timer(intf, msg->msgid);
  3304. ipmi_free_smi_msg(msg);
  3305. goto out;
  3306. }
  3307. /*
  3308. * To preserve message order, if the list is not empty, we
  3309. * tack this message onto the end of the list.
  3310. */
  3311. run_to_completion = intf->run_to_completion;
  3312. if (!run_to_completion)
  3313. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3314. if (!list_empty(&intf->waiting_msgs)) {
  3315. list_add_tail(&msg->link, &intf->waiting_msgs);
  3316. if (!run_to_completion)
  3317. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3318. goto out;
  3319. }
  3320. if (!run_to_completion)
  3321. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3322. rv = handle_new_recv_msg(intf, msg);
  3323. if (rv > 0) {
  3324. /*
  3325. * Could not handle the message now, just add it to a
  3326. * list to handle later.
  3327. */
  3328. run_to_completion = intf->run_to_completion;
  3329. if (!run_to_completion)
  3330. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3331. list_add_tail(&msg->link, &intf->waiting_msgs);
  3332. if (!run_to_completion)
  3333. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3334. } else if (rv == 0) {
  3335. ipmi_free_smi_msg(msg);
  3336. }
  3337. out:
  3338. return;
  3339. }
  3340. EXPORT_SYMBOL(ipmi_smi_msg_received);
  3341. void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
  3342. {
  3343. ipmi_user_t user;
  3344. rcu_read_lock();
  3345. list_for_each_entry_rcu(user, &intf->users, link) {
  3346. if (!user->handler->ipmi_watchdog_pretimeout)
  3347. continue;
  3348. user->handler->ipmi_watchdog_pretimeout(user->handler_data);
  3349. }
  3350. rcu_read_unlock();
  3351. }
  3352. EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
  3353. static struct ipmi_smi_msg *
  3354. smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
  3355. unsigned char seq, long seqid)
  3356. {
  3357. struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
  3358. if (!smi_msg)
  3359. /*
  3360. * If we can't allocate the message, then just return, we
  3361. * get 4 retries, so this should be ok.
  3362. */
  3363. return NULL;
  3364. memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
  3365. smi_msg->data_size = recv_msg->msg.data_len;
  3366. smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
  3367. #ifdef DEBUG_MSGING
  3368. {
  3369. int m;
  3370. printk("Resend: ");
  3371. for (m = 0; m < smi_msg->data_size; m++)
  3372. printk(" %2.2x", smi_msg->data[m]);
  3373. printk("\n");
  3374. }
  3375. #endif
  3376. return smi_msg;
  3377. }
  3378. static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
  3379. struct list_head *timeouts, long timeout_period,
  3380. int slot, unsigned long *flags)
  3381. {
  3382. struct ipmi_recv_msg *msg;
  3383. struct ipmi_smi_handlers *handlers;
  3384. if (intf->intf_num == -1)
  3385. return;
  3386. if (!ent->inuse)
  3387. return;
  3388. ent->timeout -= timeout_period;
  3389. if (ent->timeout > 0)
  3390. return;
  3391. if (ent->retries_left == 0) {
  3392. /* The message has used all its retries. */
  3393. ent->inuse = 0;
  3394. msg = ent->recv_msg;
  3395. list_add_tail(&msg->link, timeouts);
  3396. if (ent->broadcast)
  3397. ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
  3398. else if (is_lan_addr(&ent->recv_msg->addr))
  3399. ipmi_inc_stat(intf, timed_out_lan_commands);
  3400. else
  3401. ipmi_inc_stat(intf, timed_out_ipmb_commands);
  3402. } else {
  3403. struct ipmi_smi_msg *smi_msg;
  3404. /* More retries, send again. */
  3405. /*
  3406. * Start with the max timer, set to normal timer after
  3407. * the message is sent.
  3408. */
  3409. ent->timeout = MAX_MSG_TIMEOUT;
  3410. ent->retries_left--;
  3411. smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
  3412. ent->seqid);
  3413. if (!smi_msg) {
  3414. if (is_lan_addr(&ent->recv_msg->addr))
  3415. ipmi_inc_stat(intf,
  3416. dropped_rexmit_lan_commands);
  3417. else
  3418. ipmi_inc_stat(intf,
  3419. dropped_rexmit_ipmb_commands);
  3420. return;
  3421. }
  3422. spin_unlock_irqrestore(&intf->seq_lock, *flags);
  3423. /*
  3424. * Send the new message. We send with a zero
  3425. * priority. It timed out, I doubt time is that
  3426. * critical now, and high priority messages are really
  3427. * only for messages to the local MC, which don't get
  3428. * resent.
  3429. */
  3430. handlers = intf->handlers;
  3431. if (handlers) {
  3432. if (is_lan_addr(&ent->recv_msg->addr))
  3433. ipmi_inc_stat(intf,
  3434. retransmitted_lan_commands);
  3435. else
  3436. ipmi_inc_stat(intf,
  3437. retransmitted_ipmb_commands);
  3438. intf->handlers->sender(intf->send_info,
  3439. smi_msg, 0);
  3440. } else
  3441. ipmi_free_smi_msg(smi_msg);
  3442. spin_lock_irqsave(&intf->seq_lock, *flags);
  3443. }
  3444. }
  3445. static void ipmi_timeout_handler(long timeout_period)
  3446. {
  3447. ipmi_smi_t intf;
  3448. struct list_head timeouts;
  3449. struct ipmi_recv_msg *msg, *msg2;
  3450. struct ipmi_smi_msg *smi_msg, *smi_msg2;
  3451. unsigned long flags;
  3452. int i;
  3453. rcu_read_lock();
  3454. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3455. /* See if any waiting messages need to be processed. */
  3456. spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
  3457. list_for_each_entry_safe(smi_msg, smi_msg2,
  3458. &intf->waiting_msgs, link) {
  3459. if (!handle_new_recv_msg(intf, smi_msg)) {
  3460. list_del(&smi_msg->link);
  3461. ipmi_free_smi_msg(smi_msg);
  3462. } else {
  3463. /*
  3464. * To preserve message order, quit if we
  3465. * can't handle a message.
  3466. */
  3467. break;
  3468. }
  3469. }
  3470. spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
  3471. /*
  3472. * Go through the seq table and find any messages that
  3473. * have timed out, putting them in the timeouts
  3474. * list.
  3475. */
  3476. INIT_LIST_HEAD(&timeouts);
  3477. spin_lock_irqsave(&intf->seq_lock, flags);
  3478. for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
  3479. check_msg_timeout(intf, &(intf->seq_table[i]),
  3480. &timeouts, timeout_period, i,
  3481. &flags);
  3482. spin_unlock_irqrestore(&intf->seq_lock, flags);
  3483. list_for_each_entry_safe(msg, msg2, &timeouts, link)
  3484. deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
  3485. /*
  3486. * Maintenance mode handling. Check the timeout
  3487. * optimistically before we claim the lock. It may
  3488. * mean a timeout gets missed occasionally, but that
  3489. * only means the timeout gets extended by one period
  3490. * in that case. No big deal, and it avoids the lock
  3491. * most of the time.
  3492. */
  3493. if (intf->auto_maintenance_timeout > 0) {
  3494. spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
  3495. if (intf->auto_maintenance_timeout > 0) {
  3496. intf->auto_maintenance_timeout
  3497. -= timeout_period;
  3498. if (!intf->maintenance_mode
  3499. && (intf->auto_maintenance_timeout <= 0)) {
  3500. intf->maintenance_mode_enable = 0;
  3501. maintenance_mode_update(intf);
  3502. }
  3503. }
  3504. spin_unlock_irqrestore(&intf->maintenance_mode_lock,
  3505. flags);
  3506. }
  3507. }
  3508. rcu_read_unlock();
  3509. }
  3510. static void ipmi_request_event(void)
  3511. {
  3512. ipmi_smi_t intf;
  3513. struct ipmi_smi_handlers *handlers;
  3514. rcu_read_lock();
  3515. /*
  3516. * Called from the timer, no need to check if handlers is
  3517. * valid.
  3518. */
  3519. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3520. /* No event requests when in maintenance mode. */
  3521. if (intf->maintenance_mode_enable)
  3522. continue;
  3523. handlers = intf->handlers;
  3524. if (handlers)
  3525. handlers->request_events(intf->send_info);
  3526. }
  3527. rcu_read_unlock();
  3528. }
  3529. static struct timer_list ipmi_timer;
  3530. /* Call every ~1000 ms. */
  3531. #define IPMI_TIMEOUT_TIME 1000
  3532. /* How many jiffies does it take to get to the timeout time. */
  3533. #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
  3534. /*
  3535. * Request events from the queue every second (this is the number of
  3536. * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
  3537. * future, IPMI will add a way to know immediately if an event is in
  3538. * the queue and this silliness can go away.
  3539. */
  3540. #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
  3541. static atomic_t stop_operation;
  3542. static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  3543. static void ipmi_timeout(unsigned long data)
  3544. {
  3545. if (atomic_read(&stop_operation))
  3546. return;
  3547. ticks_to_req_ev--;
  3548. if (ticks_to_req_ev == 0) {
  3549. ipmi_request_event();
  3550. ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
  3551. }
  3552. ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
  3553. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  3554. }
  3555. static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
  3556. static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
  3557. /* FIXME - convert these to slabs. */
  3558. static void free_smi_msg(struct ipmi_smi_msg *msg)
  3559. {
  3560. atomic_dec(&smi_msg_inuse_count);
  3561. kfree(msg);
  3562. }
  3563. struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
  3564. {
  3565. struct ipmi_smi_msg *rv;
  3566. rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
  3567. if (rv) {
  3568. rv->done = free_smi_msg;
  3569. rv->user_data = NULL;
  3570. atomic_inc(&smi_msg_inuse_count);
  3571. }
  3572. return rv;
  3573. }
  3574. EXPORT_SYMBOL(ipmi_alloc_smi_msg);
  3575. static void free_recv_msg(struct ipmi_recv_msg *msg)
  3576. {
  3577. atomic_dec(&recv_msg_inuse_count);
  3578. kfree(msg);
  3579. }
  3580. static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
  3581. {
  3582. struct ipmi_recv_msg *rv;
  3583. rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
  3584. if (rv) {
  3585. rv->user = NULL;
  3586. rv->done = free_recv_msg;
  3587. atomic_inc(&recv_msg_inuse_count);
  3588. }
  3589. return rv;
  3590. }
  3591. void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
  3592. {
  3593. if (msg->user)
  3594. kref_put(&msg->user->refcount, free_user);
  3595. msg->done(msg);
  3596. }
  3597. EXPORT_SYMBOL(ipmi_free_recv_msg);
  3598. #ifdef CONFIG_IPMI_PANIC_EVENT
  3599. static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
  3600. {
  3601. }
  3602. static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
  3603. {
  3604. }
  3605. #ifdef CONFIG_IPMI_PANIC_STRING
  3606. static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  3607. {
  3608. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  3609. && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
  3610. && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
  3611. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  3612. /* A get event receiver command, save it. */
  3613. intf->event_receiver = msg->msg.data[1];
  3614. intf->event_receiver_lun = msg->msg.data[2] & 0x3;
  3615. }
  3616. }
  3617. static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
  3618. {
  3619. if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
  3620. && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
  3621. && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
  3622. && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
  3623. /*
  3624. * A get device id command, save if we are an event
  3625. * receiver or generator.
  3626. */
  3627. intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
  3628. intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
  3629. }
  3630. }
  3631. #endif
  3632. static void send_panic_events(char *str)
  3633. {
  3634. struct kernel_ipmi_msg msg;
  3635. ipmi_smi_t intf;
  3636. unsigned char data[16];
  3637. struct ipmi_system_interface_addr *si;
  3638. struct ipmi_addr addr;
  3639. struct ipmi_smi_msg smi_msg;
  3640. struct ipmi_recv_msg recv_msg;
  3641. si = (struct ipmi_system_interface_addr *) &addr;
  3642. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3643. si->channel = IPMI_BMC_CHANNEL;
  3644. si->lun = 0;
  3645. /* Fill in an event telling that we have failed. */
  3646. msg.netfn = 0x04; /* Sensor or Event. */
  3647. msg.cmd = 2; /* Platform event command. */
  3648. msg.data = data;
  3649. msg.data_len = 8;
  3650. data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
  3651. data[1] = 0x03; /* This is for IPMI 1.0. */
  3652. data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
  3653. data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
  3654. data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
  3655. /*
  3656. * Put a few breadcrumbs in. Hopefully later we can add more things
  3657. * to make the panic events more useful.
  3658. */
  3659. if (str) {
  3660. data[3] = str[0];
  3661. data[6] = str[1];
  3662. data[7] = str[2];
  3663. }
  3664. smi_msg.done = dummy_smi_done_handler;
  3665. recv_msg.done = dummy_recv_done_handler;
  3666. /* For every registered interface, send the event. */
  3667. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3668. if (!intf->handlers)
  3669. /* Interface is not ready. */
  3670. continue;
  3671. intf->run_to_completion = 1;
  3672. /* Send the event announcing the panic. */
  3673. intf->handlers->set_run_to_completion(intf->send_info, 1);
  3674. i_ipmi_request(NULL,
  3675. intf,
  3676. &addr,
  3677. 0,
  3678. &msg,
  3679. intf,
  3680. &smi_msg,
  3681. &recv_msg,
  3682. 0,
  3683. intf->channels[0].address,
  3684. intf->channels[0].lun,
  3685. 0, 1); /* Don't retry, and don't wait. */
  3686. }
  3687. #ifdef CONFIG_IPMI_PANIC_STRING
  3688. /*
  3689. * On every interface, dump a bunch of OEM event holding the
  3690. * string.
  3691. */
  3692. if (!str)
  3693. return;
  3694. /* For every registered interface, send the event. */
  3695. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3696. char *p = str;
  3697. struct ipmi_ipmb_addr *ipmb;
  3698. int j;
  3699. if (intf->intf_num == -1)
  3700. /* Interface was not ready yet. */
  3701. continue;
  3702. /*
  3703. * intf_num is used as an marker to tell if the
  3704. * interface is valid. Thus we need a read barrier to
  3705. * make sure data fetched before checking intf_num
  3706. * won't be used.
  3707. */
  3708. smp_rmb();
  3709. /*
  3710. * First job here is to figure out where to send the
  3711. * OEM events. There's no way in IPMI to send OEM
  3712. * events using an event send command, so we have to
  3713. * find the SEL to put them in and stick them in
  3714. * there.
  3715. */
  3716. /* Get capabilities from the get device id. */
  3717. intf->local_sel_device = 0;
  3718. intf->local_event_generator = 0;
  3719. intf->event_receiver = 0;
  3720. /* Request the device info from the local MC. */
  3721. msg.netfn = IPMI_NETFN_APP_REQUEST;
  3722. msg.cmd = IPMI_GET_DEVICE_ID_CMD;
  3723. msg.data = NULL;
  3724. msg.data_len = 0;
  3725. intf->null_user_handler = device_id_fetcher;
  3726. i_ipmi_request(NULL,
  3727. intf,
  3728. &addr,
  3729. 0,
  3730. &msg,
  3731. intf,
  3732. &smi_msg,
  3733. &recv_msg,
  3734. 0,
  3735. intf->channels[0].address,
  3736. intf->channels[0].lun,
  3737. 0, 1); /* Don't retry, and don't wait. */
  3738. if (intf->local_event_generator) {
  3739. /* Request the event receiver from the local MC. */
  3740. msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
  3741. msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
  3742. msg.data = NULL;
  3743. msg.data_len = 0;
  3744. intf->null_user_handler = event_receiver_fetcher;
  3745. i_ipmi_request(NULL,
  3746. intf,
  3747. &addr,
  3748. 0,
  3749. &msg,
  3750. intf,
  3751. &smi_msg,
  3752. &recv_msg,
  3753. 0,
  3754. intf->channels[0].address,
  3755. intf->channels[0].lun,
  3756. 0, 1); /* no retry, and no wait. */
  3757. }
  3758. intf->null_user_handler = NULL;
  3759. /*
  3760. * Validate the event receiver. The low bit must not
  3761. * be 1 (it must be a valid IPMB address), it cannot
  3762. * be zero, and it must not be my address.
  3763. */
  3764. if (((intf->event_receiver & 1) == 0)
  3765. && (intf->event_receiver != 0)
  3766. && (intf->event_receiver != intf->channels[0].address)) {
  3767. /*
  3768. * The event receiver is valid, send an IPMB
  3769. * message.
  3770. */
  3771. ipmb = (struct ipmi_ipmb_addr *) &addr;
  3772. ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
  3773. ipmb->channel = 0; /* FIXME - is this right? */
  3774. ipmb->lun = intf->event_receiver_lun;
  3775. ipmb->slave_addr = intf->event_receiver;
  3776. } else if (intf->local_sel_device) {
  3777. /*
  3778. * The event receiver was not valid (or was
  3779. * me), but I am an SEL device, just dump it
  3780. * in my SEL.
  3781. */
  3782. si = (struct ipmi_system_interface_addr *) &addr;
  3783. si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  3784. si->channel = IPMI_BMC_CHANNEL;
  3785. si->lun = 0;
  3786. } else
  3787. continue; /* No where to send the event. */
  3788. msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
  3789. msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
  3790. msg.data = data;
  3791. msg.data_len = 16;
  3792. j = 0;
  3793. while (*p) {
  3794. int size = strlen(p);
  3795. if (size > 11)
  3796. size = 11;
  3797. data[0] = 0;
  3798. data[1] = 0;
  3799. data[2] = 0xf0; /* OEM event without timestamp. */
  3800. data[3] = intf->channels[0].address;
  3801. data[4] = j++; /* sequence # */
  3802. /*
  3803. * Always give 11 bytes, so strncpy will fill
  3804. * it with zeroes for me.
  3805. */
  3806. strncpy(data+5, p, 11);
  3807. p += size;
  3808. i_ipmi_request(NULL,
  3809. intf,
  3810. &addr,
  3811. 0,
  3812. &msg,
  3813. intf,
  3814. &smi_msg,
  3815. &recv_msg,
  3816. 0,
  3817. intf->channels[0].address,
  3818. intf->channels[0].lun,
  3819. 0, 1); /* no retry, and no wait. */
  3820. }
  3821. }
  3822. #endif /* CONFIG_IPMI_PANIC_STRING */
  3823. }
  3824. #endif /* CONFIG_IPMI_PANIC_EVENT */
  3825. static int has_panicked;
  3826. static int panic_event(struct notifier_block *this,
  3827. unsigned long event,
  3828. void *ptr)
  3829. {
  3830. ipmi_smi_t intf;
  3831. if (has_panicked)
  3832. return NOTIFY_DONE;
  3833. has_panicked = 1;
  3834. /* For every registered interface, set it to run to completion. */
  3835. list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
  3836. if (!intf->handlers)
  3837. /* Interface is not ready. */
  3838. continue;
  3839. intf->run_to_completion = 1;
  3840. intf->handlers->set_run_to_completion(intf->send_info, 1);
  3841. }
  3842. #ifdef CONFIG_IPMI_PANIC_EVENT
  3843. send_panic_events(ptr);
  3844. #endif
  3845. return NOTIFY_DONE;
  3846. }
  3847. static struct notifier_block panic_block = {
  3848. .notifier_call = panic_event,
  3849. .next = NULL,
  3850. .priority = 200 /* priority: INT_MAX >= x >= 0 */
  3851. };
  3852. static int ipmi_init_msghandler(void)
  3853. {
  3854. int rv;
  3855. if (initialized)
  3856. return 0;
  3857. rv = driver_register(&ipmidriver.driver);
  3858. if (rv) {
  3859. printk(KERN_ERR PFX "Could not register IPMI driver\n");
  3860. return rv;
  3861. }
  3862. printk(KERN_INFO "ipmi message handler version "
  3863. IPMI_DRIVER_VERSION "\n");
  3864. #ifdef CONFIG_PROC_FS
  3865. proc_ipmi_root = proc_mkdir("ipmi", NULL);
  3866. if (!proc_ipmi_root) {
  3867. printk(KERN_ERR PFX "Unable to create IPMI proc dir");
  3868. return -ENOMEM;
  3869. }
  3870. #endif /* CONFIG_PROC_FS */
  3871. setup_timer(&ipmi_timer, ipmi_timeout, 0);
  3872. mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
  3873. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  3874. initialized = 1;
  3875. return 0;
  3876. }
  3877. static int __init ipmi_init_msghandler_mod(void)
  3878. {
  3879. ipmi_init_msghandler();
  3880. return 0;
  3881. }
  3882. static void __exit cleanup_ipmi(void)
  3883. {
  3884. int count;
  3885. if (!initialized)
  3886. return;
  3887. atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
  3888. /*
  3889. * This can't be called if any interfaces exist, so no worry
  3890. * about shutting down the interfaces.
  3891. */
  3892. /*
  3893. * Tell the timer to stop, then wait for it to stop. This
  3894. * avoids problems with race conditions removing the timer
  3895. * here.
  3896. */
  3897. atomic_inc(&stop_operation);
  3898. del_timer_sync(&ipmi_timer);
  3899. #ifdef CONFIG_PROC_FS
  3900. remove_proc_entry(proc_ipmi_root->name, NULL);
  3901. #endif /* CONFIG_PROC_FS */
  3902. driver_unregister(&ipmidriver.driver);
  3903. initialized = 0;
  3904. /* Check for buffer leaks. */
  3905. count = atomic_read(&smi_msg_inuse_count);
  3906. if (count != 0)
  3907. printk(KERN_WARNING PFX "SMI message count %d at exit\n",
  3908. count);
  3909. count = atomic_read(&recv_msg_inuse_count);
  3910. if (count != 0)
  3911. printk(KERN_WARNING PFX "recv message count %d at exit\n",
  3912. count);
  3913. }
  3914. module_exit(cleanup_ipmi);
  3915. module_init(ipmi_init_msghandler_mod);
  3916. MODULE_LICENSE("GPL");
  3917. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  3918. MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
  3919. " interface.");
  3920. MODULE_VERSION(IPMI_DRIVER_VERSION);