ipmi_msghandler.c 115 KB

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