ipmi_msghandler.c 104 KB

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