ipmi_msghandler.c 89 KB

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