ipmi_msghandler.c 86 KB

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