ipmi_msghandler.c 113 KB

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