ipmi_msghandler.c 114 KB

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