ipmi_msghandler.c 116 KB

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