ipmi_msghandler.c 115 KB

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