ipmi_msghandler.c 117 KB

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