ipmi_msghandler.c 117 KB

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