ipmi_msghandler.c 104 KB

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