ipmi_msghandler.c 118 KB

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