ipmi_msghandler.c 113 KB

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