ipmi_msghandler.c 109 KB

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