ipmi_msghandler.c 110 KB

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