ipmi_msghandler.c 116 KB

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