ipmi_msghandler.c 118 KB

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