ipmi_msghandler.c 109 KB

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