ipmi_si_intf.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786
  1. /*
  2. * ipmi_si.c
  3. *
  4. * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
  5. * BT).
  6. *
  7. * Author: MontaVista Software, Inc.
  8. * Corey Minyard <minyard@mvista.com>
  9. * source@mvista.com
  10. *
  11. * Copyright 2002 MontaVista Software Inc.
  12. * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  28. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  29. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You should have received a copy of the GNU General Public License along
  32. * with this program; if not, write to the Free Software Foundation, Inc.,
  33. * 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. /*
  36. * This file holds the "policy" for the interface to the SMI state
  37. * machine. It does the configuration, handles timers and interrupts,
  38. * and drives the real SMI state machine.
  39. */
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/sched.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/timer.h>
  45. #include <linux/errno.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/slab.h>
  48. #include <linux/delay.h>
  49. #include <linux/list.h>
  50. #include <linux/pci.h>
  51. #include <linux/ioport.h>
  52. #include <linux/notifier.h>
  53. #include <linux/mutex.h>
  54. #include <linux/kthread.h>
  55. #include <asm/irq.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/rcupdate.h>
  58. #include <linux/ipmi.h>
  59. #include <linux/ipmi_smi.h>
  60. #include <asm/io.h>
  61. #include "ipmi_si_sm.h"
  62. #include <linux/dmi.h>
  63. #include <linux/string.h>
  64. #include <linux/ctype.h>
  65. #include <linux/pnp.h>
  66. #include <linux/of_device.h>
  67. #include <linux/of_platform.h>
  68. #include <linux/of_address.h>
  69. #include <linux/of_irq.h>
  70. #ifdef CONFIG_PARISC
  71. #include <asm/hardware.h> /* for register_parisc_driver() stuff */
  72. #include <asm/parisc-device.h>
  73. #endif
  74. #define PFX "ipmi_si: "
  75. /* Measure times between events in the driver. */
  76. #undef DEBUG_TIMING
  77. /* Call every 10 ms. */
  78. #define SI_TIMEOUT_TIME_USEC 10000
  79. #define SI_USEC_PER_JIFFY (1000000/HZ)
  80. #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
  81. #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
  82. short timeout */
  83. enum si_intf_state {
  84. SI_NORMAL,
  85. SI_GETTING_FLAGS,
  86. SI_GETTING_EVENTS,
  87. SI_CLEARING_FLAGS,
  88. SI_GETTING_MESSAGES,
  89. SI_CHECKING_ENABLES,
  90. SI_SETTING_ENABLES
  91. /* FIXME - add watchdog stuff. */
  92. };
  93. /* Some BT-specific defines we need here. */
  94. #define IPMI_BT_INTMASK_REG 2
  95. #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
  96. #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
  97. enum si_type {
  98. SI_KCS, SI_SMIC, SI_BT
  99. };
  100. static char *si_to_str[] = { "kcs", "smic", "bt" };
  101. #define DEVICE_NAME "ipmi_si"
  102. static struct platform_driver ipmi_driver;
  103. /*
  104. * Indexes into stats[] in smi_info below.
  105. */
  106. enum si_stat_indexes {
  107. /*
  108. * Number of times the driver requested a timer while an operation
  109. * was in progress.
  110. */
  111. SI_STAT_short_timeouts = 0,
  112. /*
  113. * Number of times the driver requested a timer while nothing was in
  114. * progress.
  115. */
  116. SI_STAT_long_timeouts,
  117. /* Number of times the interface was idle while being polled. */
  118. SI_STAT_idles,
  119. /* Number of interrupts the driver handled. */
  120. SI_STAT_interrupts,
  121. /* Number of time the driver got an ATTN from the hardware. */
  122. SI_STAT_attentions,
  123. /* Number of times the driver requested flags from the hardware. */
  124. SI_STAT_flag_fetches,
  125. /* Number of times the hardware didn't follow the state machine. */
  126. SI_STAT_hosed_count,
  127. /* Number of completed messages. */
  128. SI_STAT_complete_transactions,
  129. /* Number of IPMI events received from the hardware. */
  130. SI_STAT_events,
  131. /* Number of watchdog pretimeouts. */
  132. SI_STAT_watchdog_pretimeouts,
  133. /* Number of asynchronous messages received. */
  134. SI_STAT_incoming_messages,
  135. /* This *must* remain last, add new values above this. */
  136. SI_NUM_STATS
  137. };
  138. struct smi_info {
  139. int intf_num;
  140. ipmi_smi_t intf;
  141. struct si_sm_data *si_sm;
  142. struct si_sm_handlers *handlers;
  143. enum si_type si_type;
  144. spinlock_t si_lock;
  145. struct ipmi_smi_msg *waiting_msg;
  146. struct ipmi_smi_msg *curr_msg;
  147. enum si_intf_state si_state;
  148. /*
  149. * Used to handle the various types of I/O that can occur with
  150. * IPMI
  151. */
  152. struct si_sm_io io;
  153. int (*io_setup)(struct smi_info *info);
  154. void (*io_cleanup)(struct smi_info *info);
  155. int (*irq_setup)(struct smi_info *info);
  156. void (*irq_cleanup)(struct smi_info *info);
  157. unsigned int io_size;
  158. enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
  159. void (*addr_source_cleanup)(struct smi_info *info);
  160. void *addr_source_data;
  161. /*
  162. * Per-OEM handler, called from handle_flags(). Returns 1
  163. * when handle_flags() needs to be re-run or 0 indicating it
  164. * set si_state itself.
  165. */
  166. int (*oem_data_avail_handler)(struct smi_info *smi_info);
  167. /*
  168. * Flags from the last GET_MSG_FLAGS command, used when an ATTN
  169. * is set to hold the flags until we are done handling everything
  170. * from the flags.
  171. */
  172. #define RECEIVE_MSG_AVAIL 0x01
  173. #define EVENT_MSG_BUFFER_FULL 0x02
  174. #define WDT_PRE_TIMEOUT_INT 0x08
  175. #define OEM0_DATA_AVAIL 0x20
  176. #define OEM1_DATA_AVAIL 0x40
  177. #define OEM2_DATA_AVAIL 0x80
  178. #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
  179. OEM1_DATA_AVAIL | \
  180. OEM2_DATA_AVAIL)
  181. unsigned char msg_flags;
  182. /* Does the BMC have an event buffer? */
  183. bool has_event_buffer;
  184. /*
  185. * If set to true, this will request events the next time the
  186. * state machine is idle.
  187. */
  188. atomic_t req_events;
  189. /*
  190. * If true, run the state machine to completion on every send
  191. * call. Generally used after a panic to make sure stuff goes
  192. * out.
  193. */
  194. bool run_to_completion;
  195. /* The I/O port of an SI interface. */
  196. int port;
  197. /*
  198. * The space between start addresses of the two ports. For
  199. * instance, if the first port is 0xca2 and the spacing is 4, then
  200. * the second port is 0xca6.
  201. */
  202. unsigned int spacing;
  203. /* zero if no irq; */
  204. int irq;
  205. /* The timer for this si. */
  206. struct timer_list si_timer;
  207. /* This flag is set, if the timer is running (timer_pending() isn't enough) */
  208. bool timer_running;
  209. /* The time (in jiffies) the last timeout occurred at. */
  210. unsigned long last_timeout_jiffies;
  211. /* Are we waiting for the events, pretimeouts, received msgs? */
  212. atomic_t need_watch;
  213. /*
  214. * The driver will disable interrupts when it gets into a
  215. * situation where it cannot handle messages due to lack of
  216. * memory. Once that situation clears up, it will re-enable
  217. * interrupts.
  218. */
  219. bool interrupt_disabled;
  220. /*
  221. * Does the BMC support events?
  222. */
  223. bool supports_event_msg_buff;
  224. /*
  225. * Did we get an attention that we did not handle?
  226. */
  227. bool got_attn;
  228. /* From the get device id response... */
  229. struct ipmi_device_id device_id;
  230. /* Driver model stuff. */
  231. struct device *dev;
  232. struct platform_device *pdev;
  233. /*
  234. * True if we allocated the device, false if it came from
  235. * someplace else (like PCI).
  236. */
  237. bool dev_registered;
  238. /* Slave address, could be reported from DMI. */
  239. unsigned char slave_addr;
  240. /* Counters and things for the proc filesystem. */
  241. atomic_t stats[SI_NUM_STATS];
  242. struct task_struct *thread;
  243. struct list_head link;
  244. union ipmi_smi_info_union addr_info;
  245. };
  246. #define smi_inc_stat(smi, stat) \
  247. atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
  248. #define smi_get_stat(smi, stat) \
  249. ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
  250. #define SI_MAX_PARMS 4
  251. static int force_kipmid[SI_MAX_PARMS];
  252. static int num_force_kipmid;
  253. #ifdef CONFIG_PCI
  254. static bool pci_registered;
  255. #endif
  256. #ifdef CONFIG_ACPI
  257. static bool pnp_registered;
  258. #endif
  259. #ifdef CONFIG_PARISC
  260. static bool parisc_registered;
  261. #endif
  262. static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
  263. static int num_max_busy_us;
  264. static bool unload_when_empty = true;
  265. static int add_smi(struct smi_info *smi);
  266. static int try_smi_init(struct smi_info *smi);
  267. static void cleanup_one_si(struct smi_info *to_clean);
  268. static void cleanup_ipmi_si(void);
  269. static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
  270. static int register_xaction_notifier(struct notifier_block *nb)
  271. {
  272. return atomic_notifier_chain_register(&xaction_notifier_list, nb);
  273. }
  274. static void deliver_recv_msg(struct smi_info *smi_info,
  275. struct ipmi_smi_msg *msg)
  276. {
  277. /* Deliver the message to the upper layer. */
  278. if (smi_info->intf)
  279. ipmi_smi_msg_received(smi_info->intf, msg);
  280. else
  281. ipmi_free_smi_msg(msg);
  282. }
  283. static void return_hosed_msg(struct smi_info *smi_info, int cCode)
  284. {
  285. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  286. if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
  287. cCode = IPMI_ERR_UNSPECIFIED;
  288. /* else use it as is */
  289. /* Make it a response */
  290. msg->rsp[0] = msg->data[0] | 4;
  291. msg->rsp[1] = msg->data[1];
  292. msg->rsp[2] = cCode;
  293. msg->rsp_size = 3;
  294. smi_info->curr_msg = NULL;
  295. deliver_recv_msg(smi_info, msg);
  296. }
  297. static enum si_sm_result start_next_msg(struct smi_info *smi_info)
  298. {
  299. int rv;
  300. #ifdef DEBUG_TIMING
  301. struct timeval t;
  302. #endif
  303. if (!smi_info->waiting_msg) {
  304. smi_info->curr_msg = NULL;
  305. rv = SI_SM_IDLE;
  306. } else {
  307. int err;
  308. smi_info->curr_msg = smi_info->waiting_msg;
  309. smi_info->waiting_msg = NULL;
  310. #ifdef DEBUG_TIMING
  311. do_gettimeofday(&t);
  312. printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  313. #endif
  314. err = atomic_notifier_call_chain(&xaction_notifier_list,
  315. 0, smi_info);
  316. if (err & NOTIFY_STOP_MASK) {
  317. rv = SI_SM_CALL_WITHOUT_DELAY;
  318. goto out;
  319. }
  320. err = smi_info->handlers->start_transaction(
  321. smi_info->si_sm,
  322. smi_info->curr_msg->data,
  323. smi_info->curr_msg->data_size);
  324. if (err)
  325. return_hosed_msg(smi_info, err);
  326. rv = SI_SM_CALL_WITHOUT_DELAY;
  327. }
  328. out:
  329. return rv;
  330. }
  331. static void start_check_enables(struct smi_info *smi_info)
  332. {
  333. unsigned char msg[2];
  334. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  335. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  336. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  337. smi_info->si_state = SI_CHECKING_ENABLES;
  338. }
  339. static void start_clear_flags(struct smi_info *smi_info)
  340. {
  341. unsigned char msg[3];
  342. /* Make sure the watchdog pre-timeout flag is not set at startup. */
  343. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  344. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  345. msg[2] = WDT_PRE_TIMEOUT_INT;
  346. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  347. smi_info->si_state = SI_CLEARING_FLAGS;
  348. }
  349. static void start_getting_msg_queue(struct smi_info *smi_info)
  350. {
  351. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  352. smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
  353. smi_info->curr_msg->data_size = 2;
  354. smi_info->handlers->start_transaction(
  355. smi_info->si_sm,
  356. smi_info->curr_msg->data,
  357. smi_info->curr_msg->data_size);
  358. smi_info->si_state = SI_GETTING_MESSAGES;
  359. }
  360. static void start_getting_events(struct smi_info *smi_info)
  361. {
  362. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  363. smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  364. smi_info->curr_msg->data_size = 2;
  365. smi_info->handlers->start_transaction(
  366. smi_info->si_sm,
  367. smi_info->curr_msg->data,
  368. smi_info->curr_msg->data_size);
  369. smi_info->si_state = SI_GETTING_EVENTS;
  370. }
  371. static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
  372. {
  373. smi_info->last_timeout_jiffies = jiffies;
  374. mod_timer(&smi_info->si_timer, new_val);
  375. smi_info->timer_running = true;
  376. }
  377. /*
  378. * When we have a situtaion where we run out of memory and cannot
  379. * allocate messages, we just leave them in the BMC and run the system
  380. * polled until we can allocate some memory. Once we have some
  381. * memory, we will re-enable the interrupt.
  382. */
  383. static inline bool disable_si_irq(struct smi_info *smi_info)
  384. {
  385. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  386. smi_info->interrupt_disabled = true;
  387. start_check_enables(smi_info);
  388. return true;
  389. }
  390. return false;
  391. }
  392. static inline bool enable_si_irq(struct smi_info *smi_info)
  393. {
  394. if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
  395. smi_info->interrupt_disabled = false;
  396. start_check_enables(smi_info);
  397. return true;
  398. }
  399. return false;
  400. }
  401. /*
  402. * Allocate a message. If unable to allocate, start the interrupt
  403. * disable process and return NULL. If able to allocate but
  404. * interrupts are disabled, free the message and return NULL after
  405. * starting the interrupt enable process.
  406. */
  407. static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
  408. {
  409. struct ipmi_smi_msg *msg;
  410. msg = ipmi_alloc_smi_msg();
  411. if (!msg) {
  412. if (!disable_si_irq(smi_info))
  413. smi_info->si_state = SI_NORMAL;
  414. } else if (enable_si_irq(smi_info)) {
  415. ipmi_free_smi_msg(msg);
  416. msg = NULL;
  417. }
  418. return msg;
  419. }
  420. static void handle_flags(struct smi_info *smi_info)
  421. {
  422. retry:
  423. if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
  424. /* Watchdog pre-timeout */
  425. smi_inc_stat(smi_info, watchdog_pretimeouts);
  426. start_clear_flags(smi_info);
  427. smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
  428. if (smi_info->intf)
  429. ipmi_smi_watchdog_pretimeout(smi_info->intf);
  430. } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
  431. /* Messages available. */
  432. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  433. if (!smi_info->curr_msg)
  434. return;
  435. start_getting_msg_queue(smi_info);
  436. } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
  437. /* Events available. */
  438. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  439. if (!smi_info->curr_msg)
  440. return;
  441. start_getting_events(smi_info);
  442. } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
  443. smi_info->oem_data_avail_handler) {
  444. if (smi_info->oem_data_avail_handler(smi_info))
  445. goto retry;
  446. } else
  447. smi_info->si_state = SI_NORMAL;
  448. }
  449. /*
  450. * Global enables we care about.
  451. */
  452. #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
  453. IPMI_BMC_EVT_MSG_INTR)
  454. static u8 current_global_enables(struct smi_info *smi_info, u8 base,
  455. bool *irq_on)
  456. {
  457. u8 enables = 0;
  458. if (smi_info->supports_event_msg_buff)
  459. enables |= IPMI_BMC_EVT_MSG_BUFF;
  460. else
  461. enables &= ~IPMI_BMC_EVT_MSG_BUFF;
  462. if (smi_info->irq && !smi_info->interrupt_disabled)
  463. enables |= IPMI_BMC_RCV_MSG_INTR;
  464. else
  465. enables &= ~IPMI_BMC_RCV_MSG_INTR;
  466. if (smi_info->supports_event_msg_buff &&
  467. smi_info->irq && !smi_info->interrupt_disabled)
  468. enables |= IPMI_BMC_EVT_MSG_INTR;
  469. else
  470. enables &= ~IPMI_BMC_EVT_MSG_INTR;
  471. *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
  472. return enables;
  473. }
  474. static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
  475. {
  476. u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
  477. irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
  478. if ((bool)irqstate == irq_on)
  479. return;
  480. if (irq_on)
  481. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  482. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  483. else
  484. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
  485. }
  486. static void handle_transaction_done(struct smi_info *smi_info)
  487. {
  488. struct ipmi_smi_msg *msg;
  489. #ifdef DEBUG_TIMING
  490. struct timeval t;
  491. do_gettimeofday(&t);
  492. printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  493. #endif
  494. switch (smi_info->si_state) {
  495. case SI_NORMAL:
  496. if (!smi_info->curr_msg)
  497. break;
  498. smi_info->curr_msg->rsp_size
  499. = smi_info->handlers->get_result(
  500. smi_info->si_sm,
  501. smi_info->curr_msg->rsp,
  502. IPMI_MAX_MSG_LENGTH);
  503. /*
  504. * Do this here becase deliver_recv_msg() releases the
  505. * lock, and a new message can be put in during the
  506. * time the lock is released.
  507. */
  508. msg = smi_info->curr_msg;
  509. smi_info->curr_msg = NULL;
  510. deliver_recv_msg(smi_info, msg);
  511. break;
  512. case SI_GETTING_FLAGS:
  513. {
  514. unsigned char msg[4];
  515. unsigned int len;
  516. /* We got the flags from the SMI, now handle them. */
  517. len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  518. if (msg[2] != 0) {
  519. /* Error fetching flags, just give up for now. */
  520. smi_info->si_state = SI_NORMAL;
  521. } else if (len < 4) {
  522. /*
  523. * Hmm, no flags. That's technically illegal, but
  524. * don't use uninitialized data.
  525. */
  526. smi_info->si_state = SI_NORMAL;
  527. } else {
  528. smi_info->msg_flags = msg[3];
  529. handle_flags(smi_info);
  530. }
  531. break;
  532. }
  533. case SI_CLEARING_FLAGS:
  534. {
  535. unsigned char msg[3];
  536. /* We cleared the flags. */
  537. smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
  538. if (msg[2] != 0) {
  539. /* Error clearing flags */
  540. dev_warn(smi_info->dev,
  541. "Error clearing flags: %2.2x\n", msg[2]);
  542. }
  543. smi_info->si_state = SI_NORMAL;
  544. break;
  545. }
  546. case SI_GETTING_EVENTS:
  547. {
  548. smi_info->curr_msg->rsp_size
  549. = smi_info->handlers->get_result(
  550. smi_info->si_sm,
  551. smi_info->curr_msg->rsp,
  552. IPMI_MAX_MSG_LENGTH);
  553. /*
  554. * Do this here becase deliver_recv_msg() releases the
  555. * lock, and a new message can be put in during the
  556. * time the lock is released.
  557. */
  558. msg = smi_info->curr_msg;
  559. smi_info->curr_msg = NULL;
  560. if (msg->rsp[2] != 0) {
  561. /* Error getting event, probably done. */
  562. msg->done(msg);
  563. /* Take off the event flag. */
  564. smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  565. handle_flags(smi_info);
  566. } else {
  567. smi_inc_stat(smi_info, events);
  568. /*
  569. * Do this before we deliver the message
  570. * because delivering the message releases the
  571. * lock and something else can mess with the
  572. * state.
  573. */
  574. handle_flags(smi_info);
  575. deliver_recv_msg(smi_info, msg);
  576. }
  577. break;
  578. }
  579. case SI_GETTING_MESSAGES:
  580. {
  581. smi_info->curr_msg->rsp_size
  582. = smi_info->handlers->get_result(
  583. smi_info->si_sm,
  584. smi_info->curr_msg->rsp,
  585. IPMI_MAX_MSG_LENGTH);
  586. /*
  587. * Do this here becase deliver_recv_msg() releases the
  588. * lock, and a new message can be put in during the
  589. * time the lock is released.
  590. */
  591. msg = smi_info->curr_msg;
  592. smi_info->curr_msg = NULL;
  593. if (msg->rsp[2] != 0) {
  594. /* Error getting event, probably done. */
  595. msg->done(msg);
  596. /* Take off the msg flag. */
  597. smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  598. handle_flags(smi_info);
  599. } else {
  600. smi_inc_stat(smi_info, incoming_messages);
  601. /*
  602. * Do this before we deliver the message
  603. * because delivering the message releases the
  604. * lock and something else can mess with the
  605. * state.
  606. */
  607. handle_flags(smi_info);
  608. deliver_recv_msg(smi_info, msg);
  609. }
  610. break;
  611. }
  612. case SI_CHECKING_ENABLES:
  613. {
  614. unsigned char msg[4];
  615. u8 enables;
  616. bool irq_on;
  617. /* We got the flags from the SMI, now handle them. */
  618. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  619. if (msg[2] != 0) {
  620. dev_warn(smi_info->dev,
  621. "Couldn't get irq info: %x.\n", msg[2]);
  622. dev_warn(smi_info->dev,
  623. "Maybe ok, but ipmi might run very slowly.\n");
  624. smi_info->si_state = SI_NORMAL;
  625. break;
  626. }
  627. enables = current_global_enables(smi_info, 0, &irq_on);
  628. if (smi_info->si_type == SI_BT)
  629. /* BT has its own interrupt enable bit. */
  630. check_bt_irq(smi_info, irq_on);
  631. if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
  632. /* Enables are not correct, fix them. */
  633. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  634. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  635. msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
  636. smi_info->handlers->start_transaction(
  637. smi_info->si_sm, msg, 3);
  638. smi_info->si_state = SI_SETTING_ENABLES;
  639. } else if (smi_info->supports_event_msg_buff) {
  640. smi_info->curr_msg = ipmi_alloc_smi_msg();
  641. if (!smi_info->curr_msg) {
  642. smi_info->si_state = SI_NORMAL;
  643. break;
  644. }
  645. start_getting_msg_queue(smi_info);
  646. } else {
  647. smi_info->si_state = SI_NORMAL;
  648. }
  649. break;
  650. }
  651. case SI_SETTING_ENABLES:
  652. {
  653. unsigned char msg[4];
  654. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  655. if (msg[2] != 0)
  656. dev_warn(smi_info->dev,
  657. "Could not set the global enables: 0x%x.\n",
  658. msg[2]);
  659. if (smi_info->supports_event_msg_buff) {
  660. smi_info->curr_msg = ipmi_alloc_smi_msg();
  661. if (!smi_info->curr_msg) {
  662. smi_info->si_state = SI_NORMAL;
  663. break;
  664. }
  665. start_getting_msg_queue(smi_info);
  666. } else {
  667. smi_info->si_state = SI_NORMAL;
  668. }
  669. break;
  670. }
  671. }
  672. }
  673. /*
  674. * Called on timeouts and events. Timeouts should pass the elapsed
  675. * time, interrupts should pass in zero. Must be called with
  676. * si_lock held and interrupts disabled.
  677. */
  678. static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
  679. int time)
  680. {
  681. enum si_sm_result si_sm_result;
  682. restart:
  683. /*
  684. * There used to be a loop here that waited a little while
  685. * (around 25us) before giving up. That turned out to be
  686. * pointless, the minimum delays I was seeing were in the 300us
  687. * range, which is far too long to wait in an interrupt. So
  688. * we just run until the state machine tells us something
  689. * happened or it needs a delay.
  690. */
  691. si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
  692. time = 0;
  693. while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
  694. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  695. if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
  696. smi_inc_stat(smi_info, complete_transactions);
  697. handle_transaction_done(smi_info);
  698. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  699. } else if (si_sm_result == SI_SM_HOSED) {
  700. smi_inc_stat(smi_info, hosed_count);
  701. /*
  702. * Do the before return_hosed_msg, because that
  703. * releases the lock.
  704. */
  705. smi_info->si_state = SI_NORMAL;
  706. if (smi_info->curr_msg != NULL) {
  707. /*
  708. * If we were handling a user message, format
  709. * a response to send to the upper layer to
  710. * tell it about the error.
  711. */
  712. return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
  713. }
  714. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  715. }
  716. /*
  717. * We prefer handling attn over new messages. But don't do
  718. * this if there is not yet an upper layer to handle anything.
  719. */
  720. if (likely(smi_info->intf) &&
  721. (si_sm_result == SI_SM_ATTN || smi_info->got_attn)) {
  722. unsigned char msg[2];
  723. if (smi_info->si_state != SI_NORMAL) {
  724. /*
  725. * We got an ATTN, but we are doing something else.
  726. * Handle the ATTN later.
  727. */
  728. smi_info->got_attn = true;
  729. } else {
  730. smi_info->got_attn = false;
  731. smi_inc_stat(smi_info, attentions);
  732. /*
  733. * Got a attn, send down a get message flags to see
  734. * what's causing it. It would be better to handle
  735. * this in the upper layer, but due to the way
  736. * interrupts work with the SMI, that's not really
  737. * possible.
  738. */
  739. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  740. msg[1] = IPMI_GET_MSG_FLAGS_CMD;
  741. smi_info->handlers->start_transaction(
  742. smi_info->si_sm, msg, 2);
  743. smi_info->si_state = SI_GETTING_FLAGS;
  744. goto restart;
  745. }
  746. }
  747. /* If we are currently idle, try to start the next message. */
  748. if (si_sm_result == SI_SM_IDLE) {
  749. smi_inc_stat(smi_info, idles);
  750. si_sm_result = start_next_msg(smi_info);
  751. if (si_sm_result != SI_SM_IDLE)
  752. goto restart;
  753. }
  754. if ((si_sm_result == SI_SM_IDLE)
  755. && (atomic_read(&smi_info->req_events))) {
  756. /*
  757. * We are idle and the upper layer requested that I fetch
  758. * events, so do so.
  759. */
  760. atomic_set(&smi_info->req_events, 0);
  761. /*
  762. * Take this opportunity to check the interrupt and
  763. * message enable state for the BMC. The BMC can be
  764. * asynchronously reset, and may thus get interrupts
  765. * disable and messages disabled.
  766. */
  767. if (smi_info->supports_event_msg_buff || smi_info->irq) {
  768. start_check_enables(smi_info);
  769. } else {
  770. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  771. if (!smi_info->curr_msg)
  772. goto out;
  773. start_getting_events(smi_info);
  774. }
  775. goto restart;
  776. }
  777. out:
  778. return si_sm_result;
  779. }
  780. static void check_start_timer_thread(struct smi_info *smi_info)
  781. {
  782. if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
  783. smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
  784. if (smi_info->thread)
  785. wake_up_process(smi_info->thread);
  786. start_next_msg(smi_info);
  787. smi_event_handler(smi_info, 0);
  788. }
  789. }
  790. static void sender(void *send_info,
  791. struct ipmi_smi_msg *msg)
  792. {
  793. struct smi_info *smi_info = send_info;
  794. enum si_sm_result result;
  795. unsigned long flags;
  796. #ifdef DEBUG_TIMING
  797. struct timeval t;
  798. #endif
  799. BUG_ON(smi_info->waiting_msg);
  800. smi_info->waiting_msg = msg;
  801. #ifdef DEBUG_TIMING
  802. do_gettimeofday(&t);
  803. printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  804. #endif
  805. if (smi_info->run_to_completion) {
  806. /*
  807. * If we are running to completion, start it and run
  808. * transactions until everything is clear.
  809. */
  810. smi_info->curr_msg = smi_info->waiting_msg;
  811. smi_info->waiting_msg = NULL;
  812. /*
  813. * Run to completion means we are single-threaded, no
  814. * need for locks.
  815. */
  816. result = smi_event_handler(smi_info, 0);
  817. while (result != SI_SM_IDLE) {
  818. udelay(SI_SHORT_TIMEOUT_USEC);
  819. result = smi_event_handler(smi_info,
  820. SI_SHORT_TIMEOUT_USEC);
  821. }
  822. return;
  823. }
  824. spin_lock_irqsave(&smi_info->si_lock, flags);
  825. check_start_timer_thread(smi_info);
  826. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  827. }
  828. static void set_run_to_completion(void *send_info, bool i_run_to_completion)
  829. {
  830. struct smi_info *smi_info = send_info;
  831. enum si_sm_result result;
  832. smi_info->run_to_completion = i_run_to_completion;
  833. if (i_run_to_completion) {
  834. result = smi_event_handler(smi_info, 0);
  835. while (result != SI_SM_IDLE) {
  836. udelay(SI_SHORT_TIMEOUT_USEC);
  837. result = smi_event_handler(smi_info,
  838. SI_SHORT_TIMEOUT_USEC);
  839. }
  840. }
  841. }
  842. /*
  843. * Use -1 in the nsec value of the busy waiting timespec to tell that
  844. * we are spinning in kipmid looking for something and not delaying
  845. * between checks
  846. */
  847. static inline void ipmi_si_set_not_busy(struct timespec *ts)
  848. {
  849. ts->tv_nsec = -1;
  850. }
  851. static inline int ipmi_si_is_busy(struct timespec *ts)
  852. {
  853. return ts->tv_nsec != -1;
  854. }
  855. static inline int ipmi_thread_busy_wait(enum si_sm_result smi_result,
  856. const struct smi_info *smi_info,
  857. struct timespec *busy_until)
  858. {
  859. unsigned int max_busy_us = 0;
  860. if (smi_info->intf_num < num_max_busy_us)
  861. max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
  862. if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
  863. ipmi_si_set_not_busy(busy_until);
  864. else if (!ipmi_si_is_busy(busy_until)) {
  865. getnstimeofday(busy_until);
  866. timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
  867. } else {
  868. struct timespec now;
  869. getnstimeofday(&now);
  870. if (unlikely(timespec_compare(&now, busy_until) > 0)) {
  871. ipmi_si_set_not_busy(busy_until);
  872. return 0;
  873. }
  874. }
  875. return 1;
  876. }
  877. /*
  878. * A busy-waiting loop for speeding up IPMI operation.
  879. *
  880. * Lousy hardware makes this hard. This is only enabled for systems
  881. * that are not BT and do not have interrupts. It starts spinning
  882. * when an operation is complete or until max_busy tells it to stop
  883. * (if that is enabled). See the paragraph on kimid_max_busy_us in
  884. * Documentation/IPMI.txt for details.
  885. */
  886. static int ipmi_thread(void *data)
  887. {
  888. struct smi_info *smi_info = data;
  889. unsigned long flags;
  890. enum si_sm_result smi_result;
  891. struct timespec busy_until;
  892. ipmi_si_set_not_busy(&busy_until);
  893. set_user_nice(current, MAX_NICE);
  894. while (!kthread_should_stop()) {
  895. int busy_wait;
  896. spin_lock_irqsave(&(smi_info->si_lock), flags);
  897. smi_result = smi_event_handler(smi_info, 0);
  898. /*
  899. * If the driver is doing something, there is a possible
  900. * race with the timer. If the timer handler see idle,
  901. * and the thread here sees something else, the timer
  902. * handler won't restart the timer even though it is
  903. * required. So start it here if necessary.
  904. */
  905. if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
  906. smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
  907. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  908. busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
  909. &busy_until);
  910. if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
  911. ; /* do nothing */
  912. else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
  913. schedule();
  914. else if (smi_result == SI_SM_IDLE) {
  915. if (atomic_read(&smi_info->need_watch)) {
  916. schedule_timeout_interruptible(100);
  917. } else {
  918. /* Wait to be woken up when we are needed. */
  919. __set_current_state(TASK_INTERRUPTIBLE);
  920. schedule();
  921. }
  922. } else
  923. schedule_timeout_interruptible(1);
  924. }
  925. return 0;
  926. }
  927. static void poll(void *send_info)
  928. {
  929. struct smi_info *smi_info = send_info;
  930. unsigned long flags = 0;
  931. bool run_to_completion = smi_info->run_to_completion;
  932. /*
  933. * Make sure there is some delay in the poll loop so we can
  934. * drive time forward and timeout things.
  935. */
  936. udelay(10);
  937. if (!run_to_completion)
  938. spin_lock_irqsave(&smi_info->si_lock, flags);
  939. smi_event_handler(smi_info, 10);
  940. if (!run_to_completion)
  941. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  942. }
  943. static void request_events(void *send_info)
  944. {
  945. struct smi_info *smi_info = send_info;
  946. if (!smi_info->has_event_buffer)
  947. return;
  948. atomic_set(&smi_info->req_events, 1);
  949. }
  950. static void set_need_watch(void *send_info, bool enable)
  951. {
  952. struct smi_info *smi_info = send_info;
  953. unsigned long flags;
  954. atomic_set(&smi_info->need_watch, enable);
  955. spin_lock_irqsave(&smi_info->si_lock, flags);
  956. check_start_timer_thread(smi_info);
  957. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  958. }
  959. static int initialized;
  960. static void smi_timeout(unsigned long data)
  961. {
  962. struct smi_info *smi_info = (struct smi_info *) data;
  963. enum si_sm_result smi_result;
  964. unsigned long flags;
  965. unsigned long jiffies_now;
  966. long time_diff;
  967. long timeout;
  968. #ifdef DEBUG_TIMING
  969. struct timeval t;
  970. #endif
  971. spin_lock_irqsave(&(smi_info->si_lock), flags);
  972. #ifdef DEBUG_TIMING
  973. do_gettimeofday(&t);
  974. printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  975. #endif
  976. jiffies_now = jiffies;
  977. time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
  978. * SI_USEC_PER_JIFFY);
  979. smi_result = smi_event_handler(smi_info, time_diff);
  980. if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
  981. /* Running with interrupts, only do long timeouts. */
  982. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  983. smi_inc_stat(smi_info, long_timeouts);
  984. goto do_mod_timer;
  985. }
  986. /*
  987. * If the state machine asks for a short delay, then shorten
  988. * the timer timeout.
  989. */
  990. if (smi_result == SI_SM_CALL_WITH_DELAY) {
  991. smi_inc_stat(smi_info, short_timeouts);
  992. timeout = jiffies + 1;
  993. } else {
  994. smi_inc_stat(smi_info, long_timeouts);
  995. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  996. }
  997. do_mod_timer:
  998. if (smi_result != SI_SM_IDLE)
  999. smi_mod_timer(smi_info, timeout);
  1000. else
  1001. smi_info->timer_running = false;
  1002. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  1003. }
  1004. static irqreturn_t si_irq_handler(int irq, void *data)
  1005. {
  1006. struct smi_info *smi_info = data;
  1007. unsigned long flags;
  1008. #ifdef DEBUG_TIMING
  1009. struct timeval t;
  1010. #endif
  1011. spin_lock_irqsave(&(smi_info->si_lock), flags);
  1012. smi_inc_stat(smi_info, interrupts);
  1013. #ifdef DEBUG_TIMING
  1014. do_gettimeofday(&t);
  1015. printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  1016. #endif
  1017. smi_event_handler(smi_info, 0);
  1018. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  1019. return IRQ_HANDLED;
  1020. }
  1021. static irqreturn_t si_bt_irq_handler(int irq, void *data)
  1022. {
  1023. struct smi_info *smi_info = data;
  1024. /* We need to clear the IRQ flag for the BT interface. */
  1025. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  1026. IPMI_BT_INTMASK_CLEAR_IRQ_BIT
  1027. | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  1028. return si_irq_handler(irq, data);
  1029. }
  1030. static int smi_start_processing(void *send_info,
  1031. ipmi_smi_t intf)
  1032. {
  1033. struct smi_info *new_smi = send_info;
  1034. int enable = 0;
  1035. new_smi->intf = intf;
  1036. /* Try to claim any interrupts. */
  1037. if (new_smi->irq_setup)
  1038. new_smi->irq_setup(new_smi);
  1039. /* Set up the timer that drives the interface. */
  1040. setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
  1041. smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
  1042. /*
  1043. * Check if the user forcefully enabled the daemon.
  1044. */
  1045. if (new_smi->intf_num < num_force_kipmid)
  1046. enable = force_kipmid[new_smi->intf_num];
  1047. /*
  1048. * The BT interface is efficient enough to not need a thread,
  1049. * and there is no need for a thread if we have interrupts.
  1050. */
  1051. else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
  1052. enable = 1;
  1053. if (enable) {
  1054. new_smi->thread = kthread_run(ipmi_thread, new_smi,
  1055. "kipmi%d", new_smi->intf_num);
  1056. if (IS_ERR(new_smi->thread)) {
  1057. dev_notice(new_smi->dev, "Could not start"
  1058. " kernel thread due to error %ld, only using"
  1059. " timers to drive the interface\n",
  1060. PTR_ERR(new_smi->thread));
  1061. new_smi->thread = NULL;
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
  1067. {
  1068. struct smi_info *smi = send_info;
  1069. data->addr_src = smi->addr_source;
  1070. data->dev = smi->dev;
  1071. data->addr_info = smi->addr_info;
  1072. get_device(smi->dev);
  1073. return 0;
  1074. }
  1075. static void set_maintenance_mode(void *send_info, bool enable)
  1076. {
  1077. struct smi_info *smi_info = send_info;
  1078. if (!enable)
  1079. atomic_set(&smi_info->req_events, 0);
  1080. }
  1081. static struct ipmi_smi_handlers handlers = {
  1082. .owner = THIS_MODULE,
  1083. .start_processing = smi_start_processing,
  1084. .get_smi_info = get_smi_info,
  1085. .sender = sender,
  1086. .request_events = request_events,
  1087. .set_need_watch = set_need_watch,
  1088. .set_maintenance_mode = set_maintenance_mode,
  1089. .set_run_to_completion = set_run_to_completion,
  1090. .poll = poll,
  1091. };
  1092. /*
  1093. * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
  1094. * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
  1095. */
  1096. static LIST_HEAD(smi_infos);
  1097. static DEFINE_MUTEX(smi_infos_lock);
  1098. static int smi_num; /* Used to sequence the SMIs */
  1099. #define DEFAULT_REGSPACING 1
  1100. #define DEFAULT_REGSIZE 1
  1101. #ifdef CONFIG_ACPI
  1102. static bool si_tryacpi = 1;
  1103. #endif
  1104. #ifdef CONFIG_DMI
  1105. static bool si_trydmi = 1;
  1106. #endif
  1107. static bool si_tryplatform = 1;
  1108. #ifdef CONFIG_PCI
  1109. static bool si_trypci = 1;
  1110. #endif
  1111. static bool si_trydefaults = IS_ENABLED(CONFIG_IPMI_SI_PROBE_DEFAULTS);
  1112. static char *si_type[SI_MAX_PARMS];
  1113. #define MAX_SI_TYPE_STR 30
  1114. static char si_type_str[MAX_SI_TYPE_STR];
  1115. static unsigned long addrs[SI_MAX_PARMS];
  1116. static unsigned int num_addrs;
  1117. static unsigned int ports[SI_MAX_PARMS];
  1118. static unsigned int num_ports;
  1119. static int irqs[SI_MAX_PARMS];
  1120. static unsigned int num_irqs;
  1121. static int regspacings[SI_MAX_PARMS];
  1122. static unsigned int num_regspacings;
  1123. static int regsizes[SI_MAX_PARMS];
  1124. static unsigned int num_regsizes;
  1125. static int regshifts[SI_MAX_PARMS];
  1126. static unsigned int num_regshifts;
  1127. static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
  1128. static unsigned int num_slave_addrs;
  1129. #define IPMI_IO_ADDR_SPACE 0
  1130. #define IPMI_MEM_ADDR_SPACE 1
  1131. static char *addr_space_to_str[] = { "i/o", "mem" };
  1132. static int hotmod_handler(const char *val, struct kernel_param *kp);
  1133. module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
  1134. MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
  1135. " Documentation/IPMI.txt in the kernel sources for the"
  1136. " gory details.");
  1137. #ifdef CONFIG_ACPI
  1138. module_param_named(tryacpi, si_tryacpi, bool, 0);
  1139. MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
  1140. " default scan of the interfaces identified via ACPI");
  1141. #endif
  1142. #ifdef CONFIG_DMI
  1143. module_param_named(trydmi, si_trydmi, bool, 0);
  1144. MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
  1145. " default scan of the interfaces identified via DMI");
  1146. #endif
  1147. module_param_named(tryplatform, si_tryplatform, bool, 0);
  1148. MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
  1149. " default scan of the interfaces identified via platform"
  1150. " interfaces like openfirmware");
  1151. #ifdef CONFIG_PCI
  1152. module_param_named(trypci, si_trypci, bool, 0);
  1153. MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
  1154. " default scan of the interfaces identified via pci");
  1155. #endif
  1156. module_param_named(trydefaults, si_trydefaults, bool, 0);
  1157. MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
  1158. " default scan of the KCS and SMIC interface at the standard"
  1159. " address");
  1160. module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
  1161. MODULE_PARM_DESC(type, "Defines the type of each interface, each"
  1162. " interface separated by commas. The types are 'kcs',"
  1163. " 'smic', and 'bt'. For example si_type=kcs,bt will set"
  1164. " the first interface to kcs and the second to bt");
  1165. module_param_array(addrs, ulong, &num_addrs, 0);
  1166. MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
  1167. " addresses separated by commas. Only use if an interface"
  1168. " is in memory. Otherwise, set it to zero or leave"
  1169. " it blank.");
  1170. module_param_array(ports, uint, &num_ports, 0);
  1171. MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
  1172. " addresses separated by commas. Only use if an interface"
  1173. " is a port. Otherwise, set it to zero or leave"
  1174. " it blank.");
  1175. module_param_array(irqs, int, &num_irqs, 0);
  1176. MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
  1177. " addresses separated by commas. Only use if an interface"
  1178. " has an interrupt. Otherwise, set it to zero or leave"
  1179. " it blank.");
  1180. module_param_array(regspacings, int, &num_regspacings, 0);
  1181. MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
  1182. " and each successive register used by the interface. For"
  1183. " instance, if the start address is 0xca2 and the spacing"
  1184. " is 2, then the second address is at 0xca4. Defaults"
  1185. " to 1.");
  1186. module_param_array(regsizes, int, &num_regsizes, 0);
  1187. MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
  1188. " This should generally be 1, 2, 4, or 8 for an 8-bit,"
  1189. " 16-bit, 32-bit, or 64-bit register. Use this if you"
  1190. " the 8-bit IPMI register has to be read from a larger"
  1191. " register.");
  1192. module_param_array(regshifts, int, &num_regshifts, 0);
  1193. MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
  1194. " IPMI register, in bits. For instance, if the data"
  1195. " is read from a 32-bit word and the IPMI data is in"
  1196. " bit 8-15, then the shift would be 8");
  1197. module_param_array(slave_addrs, int, &num_slave_addrs, 0);
  1198. MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
  1199. " the controller. Normally this is 0x20, but can be"
  1200. " overridden by this parm. This is an array indexed"
  1201. " by interface number.");
  1202. module_param_array(force_kipmid, int, &num_force_kipmid, 0);
  1203. MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
  1204. " disabled(0). Normally the IPMI driver auto-detects"
  1205. " this, but the value may be overridden by this parm.");
  1206. module_param(unload_when_empty, bool, 0);
  1207. MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
  1208. " specified or found, default is 1. Setting to 0"
  1209. " is useful for hot add of devices using hotmod.");
  1210. module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
  1211. MODULE_PARM_DESC(kipmid_max_busy_us,
  1212. "Max time (in microseconds) to busy-wait for IPMI data before"
  1213. " sleeping. 0 (default) means to wait forever. Set to 100-500"
  1214. " if kipmid is using up a lot of CPU time.");
  1215. static void std_irq_cleanup(struct smi_info *info)
  1216. {
  1217. if (info->si_type == SI_BT)
  1218. /* Disable the interrupt in the BT interface. */
  1219. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
  1220. free_irq(info->irq, info);
  1221. }
  1222. static int std_irq_setup(struct smi_info *info)
  1223. {
  1224. int rv;
  1225. if (!info->irq)
  1226. return 0;
  1227. if (info->si_type == SI_BT) {
  1228. rv = request_irq(info->irq,
  1229. si_bt_irq_handler,
  1230. IRQF_SHARED,
  1231. DEVICE_NAME,
  1232. info);
  1233. if (!rv)
  1234. /* Enable the interrupt in the BT interface. */
  1235. info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
  1236. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  1237. } else
  1238. rv = request_irq(info->irq,
  1239. si_irq_handler,
  1240. IRQF_SHARED,
  1241. DEVICE_NAME,
  1242. info);
  1243. if (rv) {
  1244. dev_warn(info->dev, "%s unable to claim interrupt %d,"
  1245. " running polled\n",
  1246. DEVICE_NAME, info->irq);
  1247. info->irq = 0;
  1248. } else {
  1249. info->irq_cleanup = std_irq_cleanup;
  1250. dev_info(info->dev, "Using irq %d\n", info->irq);
  1251. }
  1252. return rv;
  1253. }
  1254. static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
  1255. {
  1256. unsigned int addr = io->addr_data;
  1257. return inb(addr + (offset * io->regspacing));
  1258. }
  1259. static void port_outb(struct si_sm_io *io, unsigned int offset,
  1260. unsigned char b)
  1261. {
  1262. unsigned int addr = io->addr_data;
  1263. outb(b, addr + (offset * io->regspacing));
  1264. }
  1265. static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
  1266. {
  1267. unsigned int addr = io->addr_data;
  1268. return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1269. }
  1270. static void port_outw(struct si_sm_io *io, unsigned int offset,
  1271. unsigned char b)
  1272. {
  1273. unsigned int addr = io->addr_data;
  1274. outw(b << io->regshift, addr + (offset * io->regspacing));
  1275. }
  1276. static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
  1277. {
  1278. unsigned int addr = io->addr_data;
  1279. return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
  1280. }
  1281. static void port_outl(struct si_sm_io *io, unsigned int offset,
  1282. unsigned char b)
  1283. {
  1284. unsigned int addr = io->addr_data;
  1285. outl(b << io->regshift, addr+(offset * io->regspacing));
  1286. }
  1287. static void port_cleanup(struct smi_info *info)
  1288. {
  1289. unsigned int addr = info->io.addr_data;
  1290. int idx;
  1291. if (addr) {
  1292. for (idx = 0; idx < info->io_size; idx++)
  1293. release_region(addr + idx * info->io.regspacing,
  1294. info->io.regsize);
  1295. }
  1296. }
  1297. static int port_setup(struct smi_info *info)
  1298. {
  1299. unsigned int addr = info->io.addr_data;
  1300. int idx;
  1301. if (!addr)
  1302. return -ENODEV;
  1303. info->io_cleanup = port_cleanup;
  1304. /*
  1305. * Figure out the actual inb/inw/inl/etc routine to use based
  1306. * upon the register size.
  1307. */
  1308. switch (info->io.regsize) {
  1309. case 1:
  1310. info->io.inputb = port_inb;
  1311. info->io.outputb = port_outb;
  1312. break;
  1313. case 2:
  1314. info->io.inputb = port_inw;
  1315. info->io.outputb = port_outw;
  1316. break;
  1317. case 4:
  1318. info->io.inputb = port_inl;
  1319. info->io.outputb = port_outl;
  1320. break;
  1321. default:
  1322. dev_warn(info->dev, "Invalid register size: %d\n",
  1323. info->io.regsize);
  1324. return -EINVAL;
  1325. }
  1326. /*
  1327. * Some BIOSes reserve disjoint I/O regions in their ACPI
  1328. * tables. This causes problems when trying to register the
  1329. * entire I/O region. Therefore we must register each I/O
  1330. * port separately.
  1331. */
  1332. for (idx = 0; idx < info->io_size; idx++) {
  1333. if (request_region(addr + idx * info->io.regspacing,
  1334. info->io.regsize, DEVICE_NAME) == NULL) {
  1335. /* Undo allocations */
  1336. while (idx--) {
  1337. release_region(addr + idx * info->io.regspacing,
  1338. info->io.regsize);
  1339. }
  1340. return -EIO;
  1341. }
  1342. }
  1343. return 0;
  1344. }
  1345. static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
  1346. {
  1347. return readb((io->addr)+(offset * io->regspacing));
  1348. }
  1349. static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
  1350. unsigned char b)
  1351. {
  1352. writeb(b, (io->addr)+(offset * io->regspacing));
  1353. }
  1354. static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
  1355. {
  1356. return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1357. & 0xff;
  1358. }
  1359. static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
  1360. unsigned char b)
  1361. {
  1362. writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1363. }
  1364. static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
  1365. {
  1366. return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1367. & 0xff;
  1368. }
  1369. static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
  1370. unsigned char b)
  1371. {
  1372. writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1373. }
  1374. #ifdef readq
  1375. static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
  1376. {
  1377. return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
  1378. & 0xff;
  1379. }
  1380. static void mem_outq(struct si_sm_io *io, unsigned int offset,
  1381. unsigned char b)
  1382. {
  1383. writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
  1384. }
  1385. #endif
  1386. static void mem_cleanup(struct smi_info *info)
  1387. {
  1388. unsigned long addr = info->io.addr_data;
  1389. int mapsize;
  1390. if (info->io.addr) {
  1391. iounmap(info->io.addr);
  1392. mapsize = ((info->io_size * info->io.regspacing)
  1393. - (info->io.regspacing - info->io.regsize));
  1394. release_mem_region(addr, mapsize);
  1395. }
  1396. }
  1397. static int mem_setup(struct smi_info *info)
  1398. {
  1399. unsigned long addr = info->io.addr_data;
  1400. int mapsize;
  1401. if (!addr)
  1402. return -ENODEV;
  1403. info->io_cleanup = mem_cleanup;
  1404. /*
  1405. * Figure out the actual readb/readw/readl/etc routine to use based
  1406. * upon the register size.
  1407. */
  1408. switch (info->io.regsize) {
  1409. case 1:
  1410. info->io.inputb = intf_mem_inb;
  1411. info->io.outputb = intf_mem_outb;
  1412. break;
  1413. case 2:
  1414. info->io.inputb = intf_mem_inw;
  1415. info->io.outputb = intf_mem_outw;
  1416. break;
  1417. case 4:
  1418. info->io.inputb = intf_mem_inl;
  1419. info->io.outputb = intf_mem_outl;
  1420. break;
  1421. #ifdef readq
  1422. case 8:
  1423. info->io.inputb = mem_inq;
  1424. info->io.outputb = mem_outq;
  1425. break;
  1426. #endif
  1427. default:
  1428. dev_warn(info->dev, "Invalid register size: %d\n",
  1429. info->io.regsize);
  1430. return -EINVAL;
  1431. }
  1432. /*
  1433. * Calculate the total amount of memory to claim. This is an
  1434. * unusual looking calculation, but it avoids claiming any
  1435. * more memory than it has to. It will claim everything
  1436. * between the first address to the end of the last full
  1437. * register.
  1438. */
  1439. mapsize = ((info->io_size * info->io.regspacing)
  1440. - (info->io.regspacing - info->io.regsize));
  1441. if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
  1442. return -EIO;
  1443. info->io.addr = ioremap(addr, mapsize);
  1444. if (info->io.addr == NULL) {
  1445. release_mem_region(addr, mapsize);
  1446. return -EIO;
  1447. }
  1448. return 0;
  1449. }
  1450. /*
  1451. * Parms come in as <op1>[:op2[:op3...]]. ops are:
  1452. * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
  1453. * Options are:
  1454. * rsp=<regspacing>
  1455. * rsi=<regsize>
  1456. * rsh=<regshift>
  1457. * irq=<irq>
  1458. * ipmb=<ipmb addr>
  1459. */
  1460. enum hotmod_op { HM_ADD, HM_REMOVE };
  1461. struct hotmod_vals {
  1462. char *name;
  1463. int val;
  1464. };
  1465. static struct hotmod_vals hotmod_ops[] = {
  1466. { "add", HM_ADD },
  1467. { "remove", HM_REMOVE },
  1468. { NULL }
  1469. };
  1470. static struct hotmod_vals hotmod_si[] = {
  1471. { "kcs", SI_KCS },
  1472. { "smic", SI_SMIC },
  1473. { "bt", SI_BT },
  1474. { NULL }
  1475. };
  1476. static struct hotmod_vals hotmod_as[] = {
  1477. { "mem", IPMI_MEM_ADDR_SPACE },
  1478. { "i/o", IPMI_IO_ADDR_SPACE },
  1479. { NULL }
  1480. };
  1481. static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
  1482. {
  1483. char *s;
  1484. int i;
  1485. s = strchr(*curr, ',');
  1486. if (!s) {
  1487. printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
  1488. return -EINVAL;
  1489. }
  1490. *s = '\0';
  1491. s++;
  1492. for (i = 0; v[i].name; i++) {
  1493. if (strcmp(*curr, v[i].name) == 0) {
  1494. *val = v[i].val;
  1495. *curr = s;
  1496. return 0;
  1497. }
  1498. }
  1499. printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
  1500. return -EINVAL;
  1501. }
  1502. static int check_hotmod_int_op(const char *curr, const char *option,
  1503. const char *name, int *val)
  1504. {
  1505. char *n;
  1506. if (strcmp(curr, name) == 0) {
  1507. if (!option) {
  1508. printk(KERN_WARNING PFX
  1509. "No option given for '%s'\n",
  1510. curr);
  1511. return -EINVAL;
  1512. }
  1513. *val = simple_strtoul(option, &n, 0);
  1514. if ((*n != '\0') || (*option == '\0')) {
  1515. printk(KERN_WARNING PFX
  1516. "Bad option given for '%s'\n",
  1517. curr);
  1518. return -EINVAL;
  1519. }
  1520. return 1;
  1521. }
  1522. return 0;
  1523. }
  1524. static struct smi_info *smi_info_alloc(void)
  1525. {
  1526. struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
  1527. if (info)
  1528. spin_lock_init(&info->si_lock);
  1529. return info;
  1530. }
  1531. static int hotmod_handler(const char *val, struct kernel_param *kp)
  1532. {
  1533. char *str = kstrdup(val, GFP_KERNEL);
  1534. int rv;
  1535. char *next, *curr, *s, *n, *o;
  1536. enum hotmod_op op;
  1537. enum si_type si_type;
  1538. int addr_space;
  1539. unsigned long addr;
  1540. int regspacing;
  1541. int regsize;
  1542. int regshift;
  1543. int irq;
  1544. int ipmb;
  1545. int ival;
  1546. int len;
  1547. struct smi_info *info;
  1548. if (!str)
  1549. return -ENOMEM;
  1550. /* Kill any trailing spaces, as we can get a "\n" from echo. */
  1551. len = strlen(str);
  1552. ival = len - 1;
  1553. while ((ival >= 0) && isspace(str[ival])) {
  1554. str[ival] = '\0';
  1555. ival--;
  1556. }
  1557. for (curr = str; curr; curr = next) {
  1558. regspacing = 1;
  1559. regsize = 1;
  1560. regshift = 0;
  1561. irq = 0;
  1562. ipmb = 0; /* Choose the default if not specified */
  1563. next = strchr(curr, ':');
  1564. if (next) {
  1565. *next = '\0';
  1566. next++;
  1567. }
  1568. rv = parse_str(hotmod_ops, &ival, "operation", &curr);
  1569. if (rv)
  1570. break;
  1571. op = ival;
  1572. rv = parse_str(hotmod_si, &ival, "interface type", &curr);
  1573. if (rv)
  1574. break;
  1575. si_type = ival;
  1576. rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
  1577. if (rv)
  1578. break;
  1579. s = strchr(curr, ',');
  1580. if (s) {
  1581. *s = '\0';
  1582. s++;
  1583. }
  1584. addr = simple_strtoul(curr, &n, 0);
  1585. if ((*n != '\0') || (*curr == '\0')) {
  1586. printk(KERN_WARNING PFX "Invalid hotmod address"
  1587. " '%s'\n", curr);
  1588. break;
  1589. }
  1590. while (s) {
  1591. curr = s;
  1592. s = strchr(curr, ',');
  1593. if (s) {
  1594. *s = '\0';
  1595. s++;
  1596. }
  1597. o = strchr(curr, '=');
  1598. if (o) {
  1599. *o = '\0';
  1600. o++;
  1601. }
  1602. rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
  1603. if (rv < 0)
  1604. goto out;
  1605. else if (rv)
  1606. continue;
  1607. rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
  1608. if (rv < 0)
  1609. goto out;
  1610. else if (rv)
  1611. continue;
  1612. rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
  1613. if (rv < 0)
  1614. goto out;
  1615. else if (rv)
  1616. continue;
  1617. rv = check_hotmod_int_op(curr, o, "irq", &irq);
  1618. if (rv < 0)
  1619. goto out;
  1620. else if (rv)
  1621. continue;
  1622. rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
  1623. if (rv < 0)
  1624. goto out;
  1625. else if (rv)
  1626. continue;
  1627. rv = -EINVAL;
  1628. printk(KERN_WARNING PFX
  1629. "Invalid hotmod option '%s'\n",
  1630. curr);
  1631. goto out;
  1632. }
  1633. if (op == HM_ADD) {
  1634. info = smi_info_alloc();
  1635. if (!info) {
  1636. rv = -ENOMEM;
  1637. goto out;
  1638. }
  1639. info->addr_source = SI_HOTMOD;
  1640. info->si_type = si_type;
  1641. info->io.addr_data = addr;
  1642. info->io.addr_type = addr_space;
  1643. if (addr_space == IPMI_MEM_ADDR_SPACE)
  1644. info->io_setup = mem_setup;
  1645. else
  1646. info->io_setup = port_setup;
  1647. info->io.addr = NULL;
  1648. info->io.regspacing = regspacing;
  1649. if (!info->io.regspacing)
  1650. info->io.regspacing = DEFAULT_REGSPACING;
  1651. info->io.regsize = regsize;
  1652. if (!info->io.regsize)
  1653. info->io.regsize = DEFAULT_REGSPACING;
  1654. info->io.regshift = regshift;
  1655. info->irq = irq;
  1656. if (info->irq)
  1657. info->irq_setup = std_irq_setup;
  1658. info->slave_addr = ipmb;
  1659. rv = add_smi(info);
  1660. if (rv) {
  1661. kfree(info);
  1662. goto out;
  1663. }
  1664. rv = try_smi_init(info);
  1665. if (rv) {
  1666. cleanup_one_si(info);
  1667. goto out;
  1668. }
  1669. } else {
  1670. /* remove */
  1671. struct smi_info *e, *tmp_e;
  1672. mutex_lock(&smi_infos_lock);
  1673. list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
  1674. if (e->io.addr_type != addr_space)
  1675. continue;
  1676. if (e->si_type != si_type)
  1677. continue;
  1678. if (e->io.addr_data == addr)
  1679. cleanup_one_si(e);
  1680. }
  1681. mutex_unlock(&smi_infos_lock);
  1682. }
  1683. }
  1684. rv = len;
  1685. out:
  1686. kfree(str);
  1687. return rv;
  1688. }
  1689. static int hardcode_find_bmc(void)
  1690. {
  1691. int ret = -ENODEV;
  1692. int i;
  1693. struct smi_info *info;
  1694. for (i = 0; i < SI_MAX_PARMS; i++) {
  1695. if (!ports[i] && !addrs[i])
  1696. continue;
  1697. info = smi_info_alloc();
  1698. if (!info)
  1699. return -ENOMEM;
  1700. info->addr_source = SI_HARDCODED;
  1701. printk(KERN_INFO PFX "probing via hardcoded address\n");
  1702. if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
  1703. info->si_type = SI_KCS;
  1704. } else if (strcmp(si_type[i], "smic") == 0) {
  1705. info->si_type = SI_SMIC;
  1706. } else if (strcmp(si_type[i], "bt") == 0) {
  1707. info->si_type = SI_BT;
  1708. } else {
  1709. printk(KERN_WARNING PFX "Interface type specified "
  1710. "for interface %d, was invalid: %s\n",
  1711. i, si_type[i]);
  1712. kfree(info);
  1713. continue;
  1714. }
  1715. if (ports[i]) {
  1716. /* An I/O port */
  1717. info->io_setup = port_setup;
  1718. info->io.addr_data = ports[i];
  1719. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1720. } else if (addrs[i]) {
  1721. /* A memory port */
  1722. info->io_setup = mem_setup;
  1723. info->io.addr_data = addrs[i];
  1724. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1725. } else {
  1726. printk(KERN_WARNING PFX "Interface type specified "
  1727. "for interface %d, but port and address were "
  1728. "not set or set to zero.\n", i);
  1729. kfree(info);
  1730. continue;
  1731. }
  1732. info->io.addr = NULL;
  1733. info->io.regspacing = regspacings[i];
  1734. if (!info->io.regspacing)
  1735. info->io.regspacing = DEFAULT_REGSPACING;
  1736. info->io.regsize = regsizes[i];
  1737. if (!info->io.regsize)
  1738. info->io.regsize = DEFAULT_REGSPACING;
  1739. info->io.regshift = regshifts[i];
  1740. info->irq = irqs[i];
  1741. if (info->irq)
  1742. info->irq_setup = std_irq_setup;
  1743. info->slave_addr = slave_addrs[i];
  1744. if (!add_smi(info)) {
  1745. if (try_smi_init(info))
  1746. cleanup_one_si(info);
  1747. ret = 0;
  1748. } else {
  1749. kfree(info);
  1750. }
  1751. }
  1752. return ret;
  1753. }
  1754. #ifdef CONFIG_ACPI
  1755. #include <linux/acpi.h>
  1756. /*
  1757. * Once we get an ACPI failure, we don't try any more, because we go
  1758. * through the tables sequentially. Once we don't find a table, there
  1759. * are no more.
  1760. */
  1761. static int acpi_failure;
  1762. /* For GPE-type interrupts. */
  1763. static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
  1764. u32 gpe_number, void *context)
  1765. {
  1766. struct smi_info *smi_info = context;
  1767. unsigned long flags;
  1768. #ifdef DEBUG_TIMING
  1769. struct timeval t;
  1770. #endif
  1771. spin_lock_irqsave(&(smi_info->si_lock), flags);
  1772. smi_inc_stat(smi_info, interrupts);
  1773. #ifdef DEBUG_TIMING
  1774. do_gettimeofday(&t);
  1775. printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
  1776. #endif
  1777. smi_event_handler(smi_info, 0);
  1778. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  1779. return ACPI_INTERRUPT_HANDLED;
  1780. }
  1781. static void acpi_gpe_irq_cleanup(struct smi_info *info)
  1782. {
  1783. if (!info->irq)
  1784. return;
  1785. acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
  1786. }
  1787. static int acpi_gpe_irq_setup(struct smi_info *info)
  1788. {
  1789. acpi_status status;
  1790. if (!info->irq)
  1791. return 0;
  1792. /* FIXME - is level triggered right? */
  1793. status = acpi_install_gpe_handler(NULL,
  1794. info->irq,
  1795. ACPI_GPE_LEVEL_TRIGGERED,
  1796. &ipmi_acpi_gpe,
  1797. info);
  1798. if (status != AE_OK) {
  1799. dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
  1800. " running polled\n", DEVICE_NAME, info->irq);
  1801. info->irq = 0;
  1802. return -EINVAL;
  1803. } else {
  1804. info->irq_cleanup = acpi_gpe_irq_cleanup;
  1805. dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
  1806. return 0;
  1807. }
  1808. }
  1809. /*
  1810. * Defined at
  1811. * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
  1812. */
  1813. struct SPMITable {
  1814. s8 Signature[4];
  1815. u32 Length;
  1816. u8 Revision;
  1817. u8 Checksum;
  1818. s8 OEMID[6];
  1819. s8 OEMTableID[8];
  1820. s8 OEMRevision[4];
  1821. s8 CreatorID[4];
  1822. s8 CreatorRevision[4];
  1823. u8 InterfaceType;
  1824. u8 IPMIlegacy;
  1825. s16 SpecificationRevision;
  1826. /*
  1827. * Bit 0 - SCI interrupt supported
  1828. * Bit 1 - I/O APIC/SAPIC
  1829. */
  1830. u8 InterruptType;
  1831. /*
  1832. * If bit 0 of InterruptType is set, then this is the SCI
  1833. * interrupt in the GPEx_STS register.
  1834. */
  1835. u8 GPE;
  1836. s16 Reserved;
  1837. /*
  1838. * If bit 1 of InterruptType is set, then this is the I/O
  1839. * APIC/SAPIC interrupt.
  1840. */
  1841. u32 GlobalSystemInterrupt;
  1842. /* The actual register address. */
  1843. struct acpi_generic_address addr;
  1844. u8 UID[4];
  1845. s8 spmi_id[1]; /* A '\0' terminated array starts here. */
  1846. };
  1847. static int try_init_spmi(struct SPMITable *spmi)
  1848. {
  1849. struct smi_info *info;
  1850. int rv;
  1851. if (spmi->IPMIlegacy != 1) {
  1852. printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
  1853. return -ENODEV;
  1854. }
  1855. info = smi_info_alloc();
  1856. if (!info) {
  1857. printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
  1858. return -ENOMEM;
  1859. }
  1860. info->addr_source = SI_SPMI;
  1861. printk(KERN_INFO PFX "probing via SPMI\n");
  1862. /* Figure out the interface type. */
  1863. switch (spmi->InterfaceType) {
  1864. case 1: /* KCS */
  1865. info->si_type = SI_KCS;
  1866. break;
  1867. case 2: /* SMIC */
  1868. info->si_type = SI_SMIC;
  1869. break;
  1870. case 3: /* BT */
  1871. info->si_type = SI_BT;
  1872. break;
  1873. case 4: /* SSIF, just ignore */
  1874. kfree(info);
  1875. return -EIO;
  1876. default:
  1877. printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
  1878. spmi->InterfaceType);
  1879. kfree(info);
  1880. return -EIO;
  1881. }
  1882. if (spmi->InterruptType & 1) {
  1883. /* We've got a GPE interrupt. */
  1884. info->irq = spmi->GPE;
  1885. info->irq_setup = acpi_gpe_irq_setup;
  1886. } else if (spmi->InterruptType & 2) {
  1887. /* We've got an APIC/SAPIC interrupt. */
  1888. info->irq = spmi->GlobalSystemInterrupt;
  1889. info->irq_setup = std_irq_setup;
  1890. } else {
  1891. /* Use the default interrupt setting. */
  1892. info->irq = 0;
  1893. info->irq_setup = NULL;
  1894. }
  1895. if (spmi->addr.bit_width) {
  1896. /* A (hopefully) properly formed register bit width. */
  1897. info->io.regspacing = spmi->addr.bit_width / 8;
  1898. } else {
  1899. info->io.regspacing = DEFAULT_REGSPACING;
  1900. }
  1901. info->io.regsize = info->io.regspacing;
  1902. info->io.regshift = spmi->addr.bit_offset;
  1903. if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  1904. info->io_setup = mem_setup;
  1905. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1906. } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  1907. info->io_setup = port_setup;
  1908. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1909. } else {
  1910. kfree(info);
  1911. printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
  1912. return -EIO;
  1913. }
  1914. info->io.addr_data = spmi->addr.address;
  1915. pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
  1916. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  1917. info->io.addr_data, info->io.regsize, info->io.regspacing,
  1918. info->irq);
  1919. rv = add_smi(info);
  1920. if (rv)
  1921. kfree(info);
  1922. return rv;
  1923. }
  1924. static void spmi_find_bmc(void)
  1925. {
  1926. acpi_status status;
  1927. struct SPMITable *spmi;
  1928. int i;
  1929. if (acpi_disabled)
  1930. return;
  1931. if (acpi_failure)
  1932. return;
  1933. for (i = 0; ; i++) {
  1934. status = acpi_get_table(ACPI_SIG_SPMI, i+1,
  1935. (struct acpi_table_header **)&spmi);
  1936. if (status != AE_OK)
  1937. return;
  1938. try_init_spmi(spmi);
  1939. }
  1940. }
  1941. static int ipmi_pnp_probe(struct pnp_dev *dev,
  1942. const struct pnp_device_id *dev_id)
  1943. {
  1944. struct acpi_device *acpi_dev;
  1945. struct smi_info *info;
  1946. struct resource *res, *res_second;
  1947. acpi_handle handle;
  1948. acpi_status status;
  1949. unsigned long long tmp;
  1950. int rv;
  1951. acpi_dev = pnp_acpi_device(dev);
  1952. if (!acpi_dev)
  1953. return -ENODEV;
  1954. info = smi_info_alloc();
  1955. if (!info)
  1956. return -ENOMEM;
  1957. info->addr_source = SI_ACPI;
  1958. printk(KERN_INFO PFX "probing via ACPI\n");
  1959. handle = acpi_dev->handle;
  1960. info->addr_info.acpi_info.acpi_handle = handle;
  1961. /* _IFT tells us the interface type: KCS, BT, etc */
  1962. status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
  1963. if (ACPI_FAILURE(status))
  1964. goto err_free;
  1965. switch (tmp) {
  1966. case 1:
  1967. info->si_type = SI_KCS;
  1968. break;
  1969. case 2:
  1970. info->si_type = SI_SMIC;
  1971. break;
  1972. case 3:
  1973. info->si_type = SI_BT;
  1974. break;
  1975. case 4: /* SSIF, just ignore */
  1976. goto err_free;
  1977. default:
  1978. dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
  1979. goto err_free;
  1980. }
  1981. res = pnp_get_resource(dev, IORESOURCE_IO, 0);
  1982. if (res) {
  1983. info->io_setup = port_setup;
  1984. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  1985. } else {
  1986. res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
  1987. if (res) {
  1988. info->io_setup = mem_setup;
  1989. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  1990. }
  1991. }
  1992. if (!res) {
  1993. dev_err(&dev->dev, "no I/O or memory address\n");
  1994. goto err_free;
  1995. }
  1996. info->io.addr_data = res->start;
  1997. info->io.regspacing = DEFAULT_REGSPACING;
  1998. res_second = pnp_get_resource(dev,
  1999. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
  2000. IORESOURCE_IO : IORESOURCE_MEM,
  2001. 1);
  2002. if (res_second) {
  2003. if (res_second->start > info->io.addr_data)
  2004. info->io.regspacing = res_second->start - info->io.addr_data;
  2005. }
  2006. info->io.regsize = DEFAULT_REGSPACING;
  2007. info->io.regshift = 0;
  2008. /* If _GPE exists, use it; otherwise use standard interrupts */
  2009. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  2010. if (ACPI_SUCCESS(status)) {
  2011. info->irq = tmp;
  2012. info->irq_setup = acpi_gpe_irq_setup;
  2013. } else if (pnp_irq_valid(dev, 0)) {
  2014. info->irq = pnp_irq(dev, 0);
  2015. info->irq_setup = std_irq_setup;
  2016. }
  2017. info->dev = &dev->dev;
  2018. pnp_set_drvdata(dev, info);
  2019. dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
  2020. res, info->io.regsize, info->io.regspacing,
  2021. info->irq);
  2022. rv = add_smi(info);
  2023. if (rv)
  2024. kfree(info);
  2025. return rv;
  2026. err_free:
  2027. kfree(info);
  2028. return -EINVAL;
  2029. }
  2030. static void ipmi_pnp_remove(struct pnp_dev *dev)
  2031. {
  2032. struct smi_info *info = pnp_get_drvdata(dev);
  2033. cleanup_one_si(info);
  2034. }
  2035. static const struct pnp_device_id pnp_dev_table[] = {
  2036. {"IPI0001", 0},
  2037. {"", 0},
  2038. };
  2039. static struct pnp_driver ipmi_pnp_driver = {
  2040. .name = DEVICE_NAME,
  2041. .probe = ipmi_pnp_probe,
  2042. .remove = ipmi_pnp_remove,
  2043. .id_table = pnp_dev_table,
  2044. };
  2045. MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
  2046. #endif
  2047. #ifdef CONFIG_DMI
  2048. struct dmi_ipmi_data {
  2049. u8 type;
  2050. u8 addr_space;
  2051. unsigned long base_addr;
  2052. u8 irq;
  2053. u8 offset;
  2054. u8 slave_addr;
  2055. };
  2056. static int decode_dmi(const struct dmi_header *dm,
  2057. struct dmi_ipmi_data *dmi)
  2058. {
  2059. const u8 *data = (const u8 *)dm;
  2060. unsigned long base_addr;
  2061. u8 reg_spacing;
  2062. u8 len = dm->length;
  2063. dmi->type = data[4];
  2064. memcpy(&base_addr, data+8, sizeof(unsigned long));
  2065. if (len >= 0x11) {
  2066. if (base_addr & 1) {
  2067. /* I/O */
  2068. base_addr &= 0xFFFE;
  2069. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  2070. } else
  2071. /* Memory */
  2072. dmi->addr_space = IPMI_MEM_ADDR_SPACE;
  2073. /* If bit 4 of byte 0x10 is set, then the lsb for the address
  2074. is odd. */
  2075. dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
  2076. dmi->irq = data[0x11];
  2077. /* The top two bits of byte 0x10 hold the register spacing. */
  2078. reg_spacing = (data[0x10] & 0xC0) >> 6;
  2079. switch (reg_spacing) {
  2080. case 0x00: /* Byte boundaries */
  2081. dmi->offset = 1;
  2082. break;
  2083. case 0x01: /* 32-bit boundaries */
  2084. dmi->offset = 4;
  2085. break;
  2086. case 0x02: /* 16-byte boundaries */
  2087. dmi->offset = 16;
  2088. break;
  2089. default:
  2090. /* Some other interface, just ignore it. */
  2091. return -EIO;
  2092. }
  2093. } else {
  2094. /* Old DMI spec. */
  2095. /*
  2096. * Note that technically, the lower bit of the base
  2097. * address should be 1 if the address is I/O and 0 if
  2098. * the address is in memory. So many systems get that
  2099. * wrong (and all that I have seen are I/O) so we just
  2100. * ignore that bit and assume I/O. Systems that use
  2101. * memory should use the newer spec, anyway.
  2102. */
  2103. dmi->base_addr = base_addr & 0xfffe;
  2104. dmi->addr_space = IPMI_IO_ADDR_SPACE;
  2105. dmi->offset = 1;
  2106. }
  2107. dmi->slave_addr = data[6];
  2108. return 0;
  2109. }
  2110. static void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
  2111. {
  2112. struct smi_info *info;
  2113. info = smi_info_alloc();
  2114. if (!info) {
  2115. printk(KERN_ERR PFX "Could not allocate SI data\n");
  2116. return;
  2117. }
  2118. info->addr_source = SI_SMBIOS;
  2119. printk(KERN_INFO PFX "probing via SMBIOS\n");
  2120. switch (ipmi_data->type) {
  2121. case 0x01: /* KCS */
  2122. info->si_type = SI_KCS;
  2123. break;
  2124. case 0x02: /* SMIC */
  2125. info->si_type = SI_SMIC;
  2126. break;
  2127. case 0x03: /* BT */
  2128. info->si_type = SI_BT;
  2129. break;
  2130. default:
  2131. kfree(info);
  2132. return;
  2133. }
  2134. switch (ipmi_data->addr_space) {
  2135. case IPMI_MEM_ADDR_SPACE:
  2136. info->io_setup = mem_setup;
  2137. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2138. break;
  2139. case IPMI_IO_ADDR_SPACE:
  2140. info->io_setup = port_setup;
  2141. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2142. break;
  2143. default:
  2144. kfree(info);
  2145. printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
  2146. ipmi_data->addr_space);
  2147. return;
  2148. }
  2149. info->io.addr_data = ipmi_data->base_addr;
  2150. info->io.regspacing = ipmi_data->offset;
  2151. if (!info->io.regspacing)
  2152. info->io.regspacing = DEFAULT_REGSPACING;
  2153. info->io.regsize = DEFAULT_REGSPACING;
  2154. info->io.regshift = 0;
  2155. info->slave_addr = ipmi_data->slave_addr;
  2156. info->irq = ipmi_data->irq;
  2157. if (info->irq)
  2158. info->irq_setup = std_irq_setup;
  2159. pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
  2160. (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  2161. info->io.addr_data, info->io.regsize, info->io.regspacing,
  2162. info->irq);
  2163. if (add_smi(info))
  2164. kfree(info);
  2165. }
  2166. static void dmi_find_bmc(void)
  2167. {
  2168. const struct dmi_device *dev = NULL;
  2169. struct dmi_ipmi_data data;
  2170. int rv;
  2171. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
  2172. memset(&data, 0, sizeof(data));
  2173. rv = decode_dmi((const struct dmi_header *) dev->device_data,
  2174. &data);
  2175. if (!rv)
  2176. try_init_dmi(&data);
  2177. }
  2178. }
  2179. #endif /* CONFIG_DMI */
  2180. #ifdef CONFIG_PCI
  2181. #define PCI_ERMC_CLASSCODE 0x0C0700
  2182. #define PCI_ERMC_CLASSCODE_MASK 0xffffff00
  2183. #define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
  2184. #define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
  2185. #define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
  2186. #define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
  2187. #define PCI_HP_VENDOR_ID 0x103C
  2188. #define PCI_MMC_DEVICE_ID 0x121A
  2189. #define PCI_MMC_ADDR_CW 0x10
  2190. static void ipmi_pci_cleanup(struct smi_info *info)
  2191. {
  2192. struct pci_dev *pdev = info->addr_source_data;
  2193. pci_disable_device(pdev);
  2194. }
  2195. static int ipmi_pci_probe_regspacing(struct smi_info *info)
  2196. {
  2197. if (info->si_type == SI_KCS) {
  2198. unsigned char status;
  2199. int regspacing;
  2200. info->io.regsize = DEFAULT_REGSIZE;
  2201. info->io.regshift = 0;
  2202. info->io_size = 2;
  2203. info->handlers = &kcs_smi_handlers;
  2204. /* detect 1, 4, 16byte spacing */
  2205. for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
  2206. info->io.regspacing = regspacing;
  2207. if (info->io_setup(info)) {
  2208. dev_err(info->dev,
  2209. "Could not setup I/O space\n");
  2210. return DEFAULT_REGSPACING;
  2211. }
  2212. /* write invalid cmd */
  2213. info->io.outputb(&info->io, 1, 0x10);
  2214. /* read status back */
  2215. status = info->io.inputb(&info->io, 1);
  2216. info->io_cleanup(info);
  2217. if (status)
  2218. return regspacing;
  2219. regspacing *= 4;
  2220. }
  2221. }
  2222. return DEFAULT_REGSPACING;
  2223. }
  2224. static int ipmi_pci_probe(struct pci_dev *pdev,
  2225. const struct pci_device_id *ent)
  2226. {
  2227. int rv;
  2228. int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
  2229. struct smi_info *info;
  2230. info = smi_info_alloc();
  2231. if (!info)
  2232. return -ENOMEM;
  2233. info->addr_source = SI_PCI;
  2234. dev_info(&pdev->dev, "probing via PCI");
  2235. switch (class_type) {
  2236. case PCI_ERMC_CLASSCODE_TYPE_SMIC:
  2237. info->si_type = SI_SMIC;
  2238. break;
  2239. case PCI_ERMC_CLASSCODE_TYPE_KCS:
  2240. info->si_type = SI_KCS;
  2241. break;
  2242. case PCI_ERMC_CLASSCODE_TYPE_BT:
  2243. info->si_type = SI_BT;
  2244. break;
  2245. default:
  2246. kfree(info);
  2247. dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
  2248. return -ENOMEM;
  2249. }
  2250. rv = pci_enable_device(pdev);
  2251. if (rv) {
  2252. dev_err(&pdev->dev, "couldn't enable PCI device\n");
  2253. kfree(info);
  2254. return rv;
  2255. }
  2256. info->addr_source_cleanup = ipmi_pci_cleanup;
  2257. info->addr_source_data = pdev;
  2258. if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
  2259. info->io_setup = port_setup;
  2260. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2261. } else {
  2262. info->io_setup = mem_setup;
  2263. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2264. }
  2265. info->io.addr_data = pci_resource_start(pdev, 0);
  2266. info->io.regspacing = ipmi_pci_probe_regspacing(info);
  2267. info->io.regsize = DEFAULT_REGSIZE;
  2268. info->io.regshift = 0;
  2269. info->irq = pdev->irq;
  2270. if (info->irq)
  2271. info->irq_setup = std_irq_setup;
  2272. info->dev = &pdev->dev;
  2273. pci_set_drvdata(pdev, info);
  2274. dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
  2275. &pdev->resource[0], info->io.regsize, info->io.regspacing,
  2276. info->irq);
  2277. rv = add_smi(info);
  2278. if (rv) {
  2279. kfree(info);
  2280. pci_disable_device(pdev);
  2281. }
  2282. return rv;
  2283. }
  2284. static void ipmi_pci_remove(struct pci_dev *pdev)
  2285. {
  2286. struct smi_info *info = pci_get_drvdata(pdev);
  2287. cleanup_one_si(info);
  2288. pci_disable_device(pdev);
  2289. }
  2290. static struct pci_device_id ipmi_pci_devices[] = {
  2291. { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
  2292. { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
  2293. { 0, }
  2294. };
  2295. MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
  2296. static struct pci_driver ipmi_pci_driver = {
  2297. .name = DEVICE_NAME,
  2298. .id_table = ipmi_pci_devices,
  2299. .probe = ipmi_pci_probe,
  2300. .remove = ipmi_pci_remove,
  2301. };
  2302. #endif /* CONFIG_PCI */
  2303. static struct of_device_id ipmi_match[];
  2304. static int ipmi_probe(struct platform_device *dev)
  2305. {
  2306. #ifdef CONFIG_OF
  2307. const struct of_device_id *match;
  2308. struct smi_info *info;
  2309. struct resource resource;
  2310. const __be32 *regsize, *regspacing, *regshift;
  2311. struct device_node *np = dev->dev.of_node;
  2312. int ret;
  2313. int proplen;
  2314. dev_info(&dev->dev, "probing via device tree\n");
  2315. match = of_match_device(ipmi_match, &dev->dev);
  2316. if (!match)
  2317. return -EINVAL;
  2318. if (!of_device_is_available(np))
  2319. return -EINVAL;
  2320. ret = of_address_to_resource(np, 0, &resource);
  2321. if (ret) {
  2322. dev_warn(&dev->dev, PFX "invalid address from OF\n");
  2323. return ret;
  2324. }
  2325. regsize = of_get_property(np, "reg-size", &proplen);
  2326. if (regsize && proplen != 4) {
  2327. dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
  2328. return -EINVAL;
  2329. }
  2330. regspacing = of_get_property(np, "reg-spacing", &proplen);
  2331. if (regspacing && proplen != 4) {
  2332. dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
  2333. return -EINVAL;
  2334. }
  2335. regshift = of_get_property(np, "reg-shift", &proplen);
  2336. if (regshift && proplen != 4) {
  2337. dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
  2338. return -EINVAL;
  2339. }
  2340. info = smi_info_alloc();
  2341. if (!info) {
  2342. dev_err(&dev->dev,
  2343. "could not allocate memory for OF probe\n");
  2344. return -ENOMEM;
  2345. }
  2346. info->si_type = (enum si_type) match->data;
  2347. info->addr_source = SI_DEVICETREE;
  2348. info->irq_setup = std_irq_setup;
  2349. if (resource.flags & IORESOURCE_IO) {
  2350. info->io_setup = port_setup;
  2351. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2352. } else {
  2353. info->io_setup = mem_setup;
  2354. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2355. }
  2356. info->io.addr_data = resource.start;
  2357. info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
  2358. info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
  2359. info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
  2360. info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  2361. info->dev = &dev->dev;
  2362. dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
  2363. info->io.addr_data, info->io.regsize, info->io.regspacing,
  2364. info->irq);
  2365. dev_set_drvdata(&dev->dev, info);
  2366. ret = add_smi(info);
  2367. if (ret) {
  2368. kfree(info);
  2369. return ret;
  2370. }
  2371. #endif
  2372. return 0;
  2373. }
  2374. static int ipmi_remove(struct platform_device *dev)
  2375. {
  2376. #ifdef CONFIG_OF
  2377. cleanup_one_si(dev_get_drvdata(&dev->dev));
  2378. #endif
  2379. return 0;
  2380. }
  2381. static struct of_device_id ipmi_match[] =
  2382. {
  2383. { .type = "ipmi", .compatible = "ipmi-kcs",
  2384. .data = (void *)(unsigned long) SI_KCS },
  2385. { .type = "ipmi", .compatible = "ipmi-smic",
  2386. .data = (void *)(unsigned long) SI_SMIC },
  2387. { .type = "ipmi", .compatible = "ipmi-bt",
  2388. .data = (void *)(unsigned long) SI_BT },
  2389. {},
  2390. };
  2391. static struct platform_driver ipmi_driver = {
  2392. .driver = {
  2393. .name = DEVICE_NAME,
  2394. .of_match_table = ipmi_match,
  2395. },
  2396. .probe = ipmi_probe,
  2397. .remove = ipmi_remove,
  2398. };
  2399. #ifdef CONFIG_PARISC
  2400. static int ipmi_parisc_probe(struct parisc_device *dev)
  2401. {
  2402. struct smi_info *info;
  2403. int rv;
  2404. info = smi_info_alloc();
  2405. if (!info) {
  2406. dev_err(&dev->dev,
  2407. "could not allocate memory for PARISC probe\n");
  2408. return -ENOMEM;
  2409. }
  2410. info->si_type = SI_KCS;
  2411. info->addr_source = SI_DEVICETREE;
  2412. info->io_setup = mem_setup;
  2413. info->io.addr_type = IPMI_MEM_ADDR_SPACE;
  2414. info->io.addr_data = dev->hpa.start;
  2415. info->io.regsize = 1;
  2416. info->io.regspacing = 1;
  2417. info->io.regshift = 0;
  2418. info->irq = 0; /* no interrupt */
  2419. info->irq_setup = NULL;
  2420. info->dev = &dev->dev;
  2421. dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
  2422. dev_set_drvdata(&dev->dev, info);
  2423. rv = add_smi(info);
  2424. if (rv) {
  2425. kfree(info);
  2426. return rv;
  2427. }
  2428. return 0;
  2429. }
  2430. static int ipmi_parisc_remove(struct parisc_device *dev)
  2431. {
  2432. cleanup_one_si(dev_get_drvdata(&dev->dev));
  2433. return 0;
  2434. }
  2435. static struct parisc_device_id ipmi_parisc_tbl[] = {
  2436. { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
  2437. { 0, }
  2438. };
  2439. static struct parisc_driver ipmi_parisc_driver = {
  2440. .name = "ipmi",
  2441. .id_table = ipmi_parisc_tbl,
  2442. .probe = ipmi_parisc_probe,
  2443. .remove = ipmi_parisc_remove,
  2444. };
  2445. #endif /* CONFIG_PARISC */
  2446. static int wait_for_msg_done(struct smi_info *smi_info)
  2447. {
  2448. enum si_sm_result smi_result;
  2449. smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
  2450. for (;;) {
  2451. if (smi_result == SI_SM_CALL_WITH_DELAY ||
  2452. smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
  2453. schedule_timeout_uninterruptible(1);
  2454. smi_result = smi_info->handlers->event(
  2455. smi_info->si_sm, jiffies_to_usecs(1));
  2456. } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  2457. smi_result = smi_info->handlers->event(
  2458. smi_info->si_sm, 0);
  2459. } else
  2460. break;
  2461. }
  2462. if (smi_result == SI_SM_HOSED)
  2463. /*
  2464. * We couldn't get the state machine to run, so whatever's at
  2465. * the port is probably not an IPMI SMI interface.
  2466. */
  2467. return -ENODEV;
  2468. return 0;
  2469. }
  2470. static int try_get_dev_id(struct smi_info *smi_info)
  2471. {
  2472. unsigned char msg[2];
  2473. unsigned char *resp;
  2474. unsigned long resp_len;
  2475. int rv = 0;
  2476. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  2477. if (!resp)
  2478. return -ENOMEM;
  2479. /*
  2480. * Do a Get Device ID command, since it comes back with some
  2481. * useful info.
  2482. */
  2483. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2484. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  2485. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  2486. rv = wait_for_msg_done(smi_info);
  2487. if (rv)
  2488. goto out;
  2489. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2490. resp, IPMI_MAX_MSG_LENGTH);
  2491. /* Check and record info from the get device id, in case we need it. */
  2492. rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
  2493. out:
  2494. kfree(resp);
  2495. return rv;
  2496. }
  2497. static int try_enable_event_buffer(struct smi_info *smi_info)
  2498. {
  2499. unsigned char msg[3];
  2500. unsigned char *resp;
  2501. unsigned long resp_len;
  2502. int rv = 0;
  2503. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  2504. if (!resp)
  2505. return -ENOMEM;
  2506. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2507. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  2508. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  2509. rv = wait_for_msg_done(smi_info);
  2510. if (rv) {
  2511. printk(KERN_WARNING PFX "Error getting response from get"
  2512. " global enables command, the event buffer is not"
  2513. " enabled.\n");
  2514. goto out;
  2515. }
  2516. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2517. resp, IPMI_MAX_MSG_LENGTH);
  2518. if (resp_len < 4 ||
  2519. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  2520. resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
  2521. resp[2] != 0) {
  2522. printk(KERN_WARNING PFX "Invalid return from get global"
  2523. " enables command, cannot enable the event buffer.\n");
  2524. rv = -EINVAL;
  2525. goto out;
  2526. }
  2527. if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
  2528. /* buffer is already enabled, nothing to do. */
  2529. smi_info->supports_event_msg_buff = true;
  2530. goto out;
  2531. }
  2532. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  2533. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  2534. msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
  2535. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  2536. rv = wait_for_msg_done(smi_info);
  2537. if (rv) {
  2538. printk(KERN_WARNING PFX "Error getting response from set"
  2539. " global, enables command, the event buffer is not"
  2540. " enabled.\n");
  2541. goto out;
  2542. }
  2543. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  2544. resp, IPMI_MAX_MSG_LENGTH);
  2545. if (resp_len < 3 ||
  2546. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  2547. resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
  2548. printk(KERN_WARNING PFX "Invalid return from get global,"
  2549. "enables command, not enable the event buffer.\n");
  2550. rv = -EINVAL;
  2551. goto out;
  2552. }
  2553. if (resp[2] != 0)
  2554. /*
  2555. * An error when setting the event buffer bit means
  2556. * that the event buffer is not supported.
  2557. */
  2558. rv = -ENOENT;
  2559. else
  2560. smi_info->supports_event_msg_buff = true;
  2561. out:
  2562. kfree(resp);
  2563. return rv;
  2564. }
  2565. static int smi_type_proc_show(struct seq_file *m, void *v)
  2566. {
  2567. struct smi_info *smi = m->private;
  2568. return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
  2569. }
  2570. static int smi_type_proc_open(struct inode *inode, struct file *file)
  2571. {
  2572. return single_open(file, smi_type_proc_show, PDE_DATA(inode));
  2573. }
  2574. static const struct file_operations smi_type_proc_ops = {
  2575. .open = smi_type_proc_open,
  2576. .read = seq_read,
  2577. .llseek = seq_lseek,
  2578. .release = single_release,
  2579. };
  2580. static int smi_si_stats_proc_show(struct seq_file *m, void *v)
  2581. {
  2582. struct smi_info *smi = m->private;
  2583. seq_printf(m, "interrupts_enabled: %d\n",
  2584. smi->irq && !smi->interrupt_disabled);
  2585. seq_printf(m, "short_timeouts: %u\n",
  2586. smi_get_stat(smi, short_timeouts));
  2587. seq_printf(m, "long_timeouts: %u\n",
  2588. smi_get_stat(smi, long_timeouts));
  2589. seq_printf(m, "idles: %u\n",
  2590. smi_get_stat(smi, idles));
  2591. seq_printf(m, "interrupts: %u\n",
  2592. smi_get_stat(smi, interrupts));
  2593. seq_printf(m, "attentions: %u\n",
  2594. smi_get_stat(smi, attentions));
  2595. seq_printf(m, "flag_fetches: %u\n",
  2596. smi_get_stat(smi, flag_fetches));
  2597. seq_printf(m, "hosed_count: %u\n",
  2598. smi_get_stat(smi, hosed_count));
  2599. seq_printf(m, "complete_transactions: %u\n",
  2600. smi_get_stat(smi, complete_transactions));
  2601. seq_printf(m, "events: %u\n",
  2602. smi_get_stat(smi, events));
  2603. seq_printf(m, "watchdog_pretimeouts: %u\n",
  2604. smi_get_stat(smi, watchdog_pretimeouts));
  2605. seq_printf(m, "incoming_messages: %u\n",
  2606. smi_get_stat(smi, incoming_messages));
  2607. return 0;
  2608. }
  2609. static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
  2610. {
  2611. return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
  2612. }
  2613. static const struct file_operations smi_si_stats_proc_ops = {
  2614. .open = smi_si_stats_proc_open,
  2615. .read = seq_read,
  2616. .llseek = seq_lseek,
  2617. .release = single_release,
  2618. };
  2619. static int smi_params_proc_show(struct seq_file *m, void *v)
  2620. {
  2621. struct smi_info *smi = m->private;
  2622. return seq_printf(m,
  2623. "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
  2624. si_to_str[smi->si_type],
  2625. addr_space_to_str[smi->io.addr_type],
  2626. smi->io.addr_data,
  2627. smi->io.regspacing,
  2628. smi->io.regsize,
  2629. smi->io.regshift,
  2630. smi->irq,
  2631. smi->slave_addr);
  2632. }
  2633. static int smi_params_proc_open(struct inode *inode, struct file *file)
  2634. {
  2635. return single_open(file, smi_params_proc_show, PDE_DATA(inode));
  2636. }
  2637. static const struct file_operations smi_params_proc_ops = {
  2638. .open = smi_params_proc_open,
  2639. .read = seq_read,
  2640. .llseek = seq_lseek,
  2641. .release = single_release,
  2642. };
  2643. /*
  2644. * oem_data_avail_to_receive_msg_avail
  2645. * @info - smi_info structure with msg_flags set
  2646. *
  2647. * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
  2648. * Returns 1 indicating need to re-run handle_flags().
  2649. */
  2650. static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
  2651. {
  2652. smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
  2653. RECEIVE_MSG_AVAIL);
  2654. return 1;
  2655. }
  2656. /*
  2657. * setup_dell_poweredge_oem_data_handler
  2658. * @info - smi_info.device_id must be populated
  2659. *
  2660. * Systems that match, but have firmware version < 1.40 may assert
  2661. * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
  2662. * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
  2663. * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
  2664. * as RECEIVE_MSG_AVAIL instead.
  2665. *
  2666. * As Dell has no plans to release IPMI 1.5 firmware that *ever*
  2667. * assert the OEM[012] bits, and if it did, the driver would have to
  2668. * change to handle that properly, we don't actually check for the
  2669. * firmware version.
  2670. * Device ID = 0x20 BMC on PowerEdge 8G servers
  2671. * Device Revision = 0x80
  2672. * Firmware Revision1 = 0x01 BMC version 1.40
  2673. * Firmware Revision2 = 0x40 BCD encoded
  2674. * IPMI Version = 0x51 IPMI 1.5
  2675. * Manufacturer ID = A2 02 00 Dell IANA
  2676. *
  2677. * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
  2678. * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
  2679. *
  2680. */
  2681. #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
  2682. #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
  2683. #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
  2684. #define DELL_IANA_MFR_ID 0x0002a2
  2685. static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
  2686. {
  2687. struct ipmi_device_id *id = &smi_info->device_id;
  2688. if (id->manufacturer_id == DELL_IANA_MFR_ID) {
  2689. if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
  2690. id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
  2691. id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
  2692. smi_info->oem_data_avail_handler =
  2693. oem_data_avail_to_receive_msg_avail;
  2694. } else if (ipmi_version_major(id) < 1 ||
  2695. (ipmi_version_major(id) == 1 &&
  2696. ipmi_version_minor(id) < 5)) {
  2697. smi_info->oem_data_avail_handler =
  2698. oem_data_avail_to_receive_msg_avail;
  2699. }
  2700. }
  2701. }
  2702. #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
  2703. static void return_hosed_msg_badsize(struct smi_info *smi_info)
  2704. {
  2705. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  2706. /* Make it a response */
  2707. msg->rsp[0] = msg->data[0] | 4;
  2708. msg->rsp[1] = msg->data[1];
  2709. msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
  2710. msg->rsp_size = 3;
  2711. smi_info->curr_msg = NULL;
  2712. deliver_recv_msg(smi_info, msg);
  2713. }
  2714. /*
  2715. * dell_poweredge_bt_xaction_handler
  2716. * @info - smi_info.device_id must be populated
  2717. *
  2718. * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
  2719. * not respond to a Get SDR command if the length of the data
  2720. * requested is exactly 0x3A, which leads to command timeouts and no
  2721. * data returned. This intercepts such commands, and causes userspace
  2722. * callers to try again with a different-sized buffer, which succeeds.
  2723. */
  2724. #define STORAGE_NETFN 0x0A
  2725. #define STORAGE_CMD_GET_SDR 0x23
  2726. static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
  2727. unsigned long unused,
  2728. void *in)
  2729. {
  2730. struct smi_info *smi_info = in;
  2731. unsigned char *data = smi_info->curr_msg->data;
  2732. unsigned int size = smi_info->curr_msg->data_size;
  2733. if (size >= 8 &&
  2734. (data[0]>>2) == STORAGE_NETFN &&
  2735. data[1] == STORAGE_CMD_GET_SDR &&
  2736. data[7] == 0x3A) {
  2737. return_hosed_msg_badsize(smi_info);
  2738. return NOTIFY_STOP;
  2739. }
  2740. return NOTIFY_DONE;
  2741. }
  2742. static struct notifier_block dell_poweredge_bt_xaction_notifier = {
  2743. .notifier_call = dell_poweredge_bt_xaction_handler,
  2744. };
  2745. /*
  2746. * setup_dell_poweredge_bt_xaction_handler
  2747. * @info - smi_info.device_id must be filled in already
  2748. *
  2749. * Fills in smi_info.device_id.start_transaction_pre_hook
  2750. * when we know what function to use there.
  2751. */
  2752. static void
  2753. setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
  2754. {
  2755. struct ipmi_device_id *id = &smi_info->device_id;
  2756. if (id->manufacturer_id == DELL_IANA_MFR_ID &&
  2757. smi_info->si_type == SI_BT)
  2758. register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
  2759. }
  2760. /*
  2761. * setup_oem_data_handler
  2762. * @info - smi_info.device_id must be filled in already
  2763. *
  2764. * Fills in smi_info.device_id.oem_data_available_handler
  2765. * when we know what function to use there.
  2766. */
  2767. static void setup_oem_data_handler(struct smi_info *smi_info)
  2768. {
  2769. setup_dell_poweredge_oem_data_handler(smi_info);
  2770. }
  2771. static void setup_xaction_handlers(struct smi_info *smi_info)
  2772. {
  2773. setup_dell_poweredge_bt_xaction_handler(smi_info);
  2774. }
  2775. static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
  2776. {
  2777. if (smi_info->thread != NULL)
  2778. kthread_stop(smi_info->thread);
  2779. if (smi_info->timer_running)
  2780. del_timer_sync(&smi_info->si_timer);
  2781. }
  2782. static struct ipmi_default_vals
  2783. {
  2784. int type;
  2785. int port;
  2786. } ipmi_defaults[] =
  2787. {
  2788. { .type = SI_KCS, .port = 0xca2 },
  2789. { .type = SI_SMIC, .port = 0xca9 },
  2790. { .type = SI_BT, .port = 0xe4 },
  2791. { .port = 0 }
  2792. };
  2793. static void default_find_bmc(void)
  2794. {
  2795. struct smi_info *info;
  2796. int i;
  2797. for (i = 0; ; i++) {
  2798. if (!ipmi_defaults[i].port)
  2799. break;
  2800. #ifdef CONFIG_PPC
  2801. if (check_legacy_ioport(ipmi_defaults[i].port))
  2802. continue;
  2803. #endif
  2804. info = smi_info_alloc();
  2805. if (!info)
  2806. return;
  2807. info->addr_source = SI_DEFAULT;
  2808. info->si_type = ipmi_defaults[i].type;
  2809. info->io_setup = port_setup;
  2810. info->io.addr_data = ipmi_defaults[i].port;
  2811. info->io.addr_type = IPMI_IO_ADDR_SPACE;
  2812. info->io.addr = NULL;
  2813. info->io.regspacing = DEFAULT_REGSPACING;
  2814. info->io.regsize = DEFAULT_REGSPACING;
  2815. info->io.regshift = 0;
  2816. if (add_smi(info) == 0) {
  2817. if ((try_smi_init(info)) == 0) {
  2818. /* Found one... */
  2819. printk(KERN_INFO PFX "Found default %s"
  2820. " state machine at %s address 0x%lx\n",
  2821. si_to_str[info->si_type],
  2822. addr_space_to_str[info->io.addr_type],
  2823. info->io.addr_data);
  2824. } else
  2825. cleanup_one_si(info);
  2826. } else {
  2827. kfree(info);
  2828. }
  2829. }
  2830. }
  2831. static int is_new_interface(struct smi_info *info)
  2832. {
  2833. struct smi_info *e;
  2834. list_for_each_entry(e, &smi_infos, link) {
  2835. if (e->io.addr_type != info->io.addr_type)
  2836. continue;
  2837. if (e->io.addr_data == info->io.addr_data)
  2838. return 0;
  2839. }
  2840. return 1;
  2841. }
  2842. static int add_smi(struct smi_info *new_smi)
  2843. {
  2844. int rv = 0;
  2845. printk(KERN_INFO PFX "Adding %s-specified %s state machine",
  2846. ipmi_addr_src_to_str(new_smi->addr_source),
  2847. si_to_str[new_smi->si_type]);
  2848. mutex_lock(&smi_infos_lock);
  2849. if (!is_new_interface(new_smi)) {
  2850. printk(KERN_CONT " duplicate interface\n");
  2851. rv = -EBUSY;
  2852. goto out_err;
  2853. }
  2854. printk(KERN_CONT "\n");
  2855. /* So we know not to free it unless we have allocated one. */
  2856. new_smi->intf = NULL;
  2857. new_smi->si_sm = NULL;
  2858. new_smi->handlers = NULL;
  2859. list_add_tail(&new_smi->link, &smi_infos);
  2860. out_err:
  2861. mutex_unlock(&smi_infos_lock);
  2862. return rv;
  2863. }
  2864. static int try_smi_init(struct smi_info *new_smi)
  2865. {
  2866. int rv = 0;
  2867. int i;
  2868. printk(KERN_INFO PFX "Trying %s-specified %s state"
  2869. " machine at %s address 0x%lx, slave address 0x%x,"
  2870. " irq %d\n",
  2871. ipmi_addr_src_to_str(new_smi->addr_source),
  2872. si_to_str[new_smi->si_type],
  2873. addr_space_to_str[new_smi->io.addr_type],
  2874. new_smi->io.addr_data,
  2875. new_smi->slave_addr, new_smi->irq);
  2876. switch (new_smi->si_type) {
  2877. case SI_KCS:
  2878. new_smi->handlers = &kcs_smi_handlers;
  2879. break;
  2880. case SI_SMIC:
  2881. new_smi->handlers = &smic_smi_handlers;
  2882. break;
  2883. case SI_BT:
  2884. new_smi->handlers = &bt_smi_handlers;
  2885. break;
  2886. default:
  2887. /* No support for anything else yet. */
  2888. rv = -EIO;
  2889. goto out_err;
  2890. }
  2891. /* Allocate the state machine's data and initialize it. */
  2892. new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
  2893. if (!new_smi->si_sm) {
  2894. printk(KERN_ERR PFX
  2895. "Could not allocate state machine memory\n");
  2896. rv = -ENOMEM;
  2897. goto out_err;
  2898. }
  2899. new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
  2900. &new_smi->io);
  2901. /* Now that we know the I/O size, we can set up the I/O. */
  2902. rv = new_smi->io_setup(new_smi);
  2903. if (rv) {
  2904. printk(KERN_ERR PFX "Could not set up I/O space\n");
  2905. goto out_err;
  2906. }
  2907. /* Do low-level detection first. */
  2908. if (new_smi->handlers->detect(new_smi->si_sm)) {
  2909. if (new_smi->addr_source)
  2910. printk(KERN_INFO PFX "Interface detection failed\n");
  2911. rv = -ENODEV;
  2912. goto out_err;
  2913. }
  2914. /*
  2915. * Attempt a get device id command. If it fails, we probably
  2916. * don't have a BMC here.
  2917. */
  2918. rv = try_get_dev_id(new_smi);
  2919. if (rv) {
  2920. if (new_smi->addr_source)
  2921. printk(KERN_INFO PFX "There appears to be no BMC"
  2922. " at this location\n");
  2923. goto out_err;
  2924. }
  2925. setup_oem_data_handler(new_smi);
  2926. setup_xaction_handlers(new_smi);
  2927. new_smi->waiting_msg = NULL;
  2928. new_smi->curr_msg = NULL;
  2929. atomic_set(&new_smi->req_events, 0);
  2930. new_smi->run_to_completion = false;
  2931. for (i = 0; i < SI_NUM_STATS; i++)
  2932. atomic_set(&new_smi->stats[i], 0);
  2933. new_smi->interrupt_disabled = true;
  2934. atomic_set(&new_smi->need_watch, 0);
  2935. new_smi->intf_num = smi_num;
  2936. smi_num++;
  2937. rv = try_enable_event_buffer(new_smi);
  2938. if (rv == 0)
  2939. new_smi->has_event_buffer = true;
  2940. /*
  2941. * Start clearing the flags before we enable interrupts or the
  2942. * timer to avoid racing with the timer.
  2943. */
  2944. start_clear_flags(new_smi);
  2945. /*
  2946. * IRQ is defined to be set when non-zero. req_events will
  2947. * cause a global flags check that will enable interrupts.
  2948. */
  2949. if (new_smi->irq) {
  2950. new_smi->interrupt_disabled = false;
  2951. atomic_set(&new_smi->req_events, 1);
  2952. }
  2953. if (!new_smi->dev) {
  2954. /*
  2955. * If we don't already have a device from something
  2956. * else (like PCI), then register a new one.
  2957. */
  2958. new_smi->pdev = platform_device_alloc("ipmi_si",
  2959. new_smi->intf_num);
  2960. if (!new_smi->pdev) {
  2961. printk(KERN_ERR PFX
  2962. "Unable to allocate platform device\n");
  2963. goto out_err;
  2964. }
  2965. new_smi->dev = &new_smi->pdev->dev;
  2966. new_smi->dev->driver = &ipmi_driver.driver;
  2967. rv = platform_device_add(new_smi->pdev);
  2968. if (rv) {
  2969. printk(KERN_ERR PFX
  2970. "Unable to register system interface device:"
  2971. " %d\n",
  2972. rv);
  2973. goto out_err;
  2974. }
  2975. new_smi->dev_registered = true;
  2976. }
  2977. rv = ipmi_register_smi(&handlers,
  2978. new_smi,
  2979. &new_smi->device_id,
  2980. new_smi->dev,
  2981. new_smi->slave_addr);
  2982. if (rv) {
  2983. dev_err(new_smi->dev, "Unable to register device: error %d\n",
  2984. rv);
  2985. goto out_err_stop_timer;
  2986. }
  2987. rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
  2988. &smi_type_proc_ops,
  2989. new_smi);
  2990. if (rv) {
  2991. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  2992. goto out_err_stop_timer;
  2993. }
  2994. rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
  2995. &smi_si_stats_proc_ops,
  2996. new_smi);
  2997. if (rv) {
  2998. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  2999. goto out_err_stop_timer;
  3000. }
  3001. rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
  3002. &smi_params_proc_ops,
  3003. new_smi);
  3004. if (rv) {
  3005. dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
  3006. goto out_err_stop_timer;
  3007. }
  3008. dev_info(new_smi->dev, "IPMI %s interface initialized\n",
  3009. si_to_str[new_smi->si_type]);
  3010. return 0;
  3011. out_err_stop_timer:
  3012. wait_for_timer_and_thread(new_smi);
  3013. out_err:
  3014. new_smi->interrupt_disabled = true;
  3015. if (new_smi->intf) {
  3016. ipmi_smi_t intf = new_smi->intf;
  3017. new_smi->intf = NULL;
  3018. ipmi_unregister_smi(intf);
  3019. }
  3020. if (new_smi->irq_cleanup) {
  3021. new_smi->irq_cleanup(new_smi);
  3022. new_smi->irq_cleanup = NULL;
  3023. }
  3024. /*
  3025. * Wait until we know that we are out of any interrupt
  3026. * handlers might have been running before we freed the
  3027. * interrupt.
  3028. */
  3029. synchronize_sched();
  3030. if (new_smi->si_sm) {
  3031. if (new_smi->handlers)
  3032. new_smi->handlers->cleanup(new_smi->si_sm);
  3033. kfree(new_smi->si_sm);
  3034. new_smi->si_sm = NULL;
  3035. }
  3036. if (new_smi->addr_source_cleanup) {
  3037. new_smi->addr_source_cleanup(new_smi);
  3038. new_smi->addr_source_cleanup = NULL;
  3039. }
  3040. if (new_smi->io_cleanup) {
  3041. new_smi->io_cleanup(new_smi);
  3042. new_smi->io_cleanup = NULL;
  3043. }
  3044. if (new_smi->dev_registered) {
  3045. platform_device_unregister(new_smi->pdev);
  3046. new_smi->dev_registered = false;
  3047. }
  3048. return rv;
  3049. }
  3050. static int init_ipmi_si(void)
  3051. {
  3052. int i;
  3053. char *str;
  3054. int rv;
  3055. struct smi_info *e;
  3056. enum ipmi_addr_src type = SI_INVALID;
  3057. if (initialized)
  3058. return 0;
  3059. initialized = 1;
  3060. if (si_tryplatform) {
  3061. rv = platform_driver_register(&ipmi_driver);
  3062. if (rv) {
  3063. printk(KERN_ERR PFX "Unable to register "
  3064. "driver: %d\n", rv);
  3065. return rv;
  3066. }
  3067. }
  3068. /* Parse out the si_type string into its components. */
  3069. str = si_type_str;
  3070. if (*str != '\0') {
  3071. for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
  3072. si_type[i] = str;
  3073. str = strchr(str, ',');
  3074. if (str) {
  3075. *str = '\0';
  3076. str++;
  3077. } else {
  3078. break;
  3079. }
  3080. }
  3081. }
  3082. printk(KERN_INFO "IPMI System Interface driver.\n");
  3083. /* If the user gave us a device, they presumably want us to use it */
  3084. if (!hardcode_find_bmc())
  3085. return 0;
  3086. #ifdef CONFIG_PCI
  3087. if (si_trypci) {
  3088. rv = pci_register_driver(&ipmi_pci_driver);
  3089. if (rv)
  3090. printk(KERN_ERR PFX "Unable to register "
  3091. "PCI driver: %d\n", rv);
  3092. else
  3093. pci_registered = true;
  3094. }
  3095. #endif
  3096. #ifdef CONFIG_ACPI
  3097. if (si_tryacpi) {
  3098. pnp_register_driver(&ipmi_pnp_driver);
  3099. pnp_registered = true;
  3100. }
  3101. #endif
  3102. #ifdef CONFIG_DMI
  3103. if (si_trydmi)
  3104. dmi_find_bmc();
  3105. #endif
  3106. #ifdef CONFIG_ACPI
  3107. if (si_tryacpi)
  3108. spmi_find_bmc();
  3109. #endif
  3110. #ifdef CONFIG_PARISC
  3111. register_parisc_driver(&ipmi_parisc_driver);
  3112. parisc_registered = true;
  3113. /* poking PC IO addresses will crash machine, don't do it */
  3114. si_trydefaults = 0;
  3115. #endif
  3116. /* We prefer devices with interrupts, but in the case of a machine
  3117. with multiple BMCs we assume that there will be several instances
  3118. of a given type so if we succeed in registering a type then also
  3119. try to register everything else of the same type */
  3120. mutex_lock(&smi_infos_lock);
  3121. list_for_each_entry(e, &smi_infos, link) {
  3122. /* Try to register a device if it has an IRQ and we either
  3123. haven't successfully registered a device yet or this
  3124. device has the same type as one we successfully registered */
  3125. if (e->irq && (!type || e->addr_source == type)) {
  3126. if (!try_smi_init(e)) {
  3127. type = e->addr_source;
  3128. }
  3129. }
  3130. }
  3131. /* type will only have been set if we successfully registered an si */
  3132. if (type) {
  3133. mutex_unlock(&smi_infos_lock);
  3134. return 0;
  3135. }
  3136. /* Fall back to the preferred device */
  3137. list_for_each_entry(e, &smi_infos, link) {
  3138. if (!e->irq && (!type || e->addr_source == type)) {
  3139. if (!try_smi_init(e)) {
  3140. type = e->addr_source;
  3141. }
  3142. }
  3143. }
  3144. mutex_unlock(&smi_infos_lock);
  3145. if (type)
  3146. return 0;
  3147. if (si_trydefaults) {
  3148. mutex_lock(&smi_infos_lock);
  3149. if (list_empty(&smi_infos)) {
  3150. /* No BMC was found, try defaults. */
  3151. mutex_unlock(&smi_infos_lock);
  3152. default_find_bmc();
  3153. } else
  3154. mutex_unlock(&smi_infos_lock);
  3155. }
  3156. mutex_lock(&smi_infos_lock);
  3157. if (unload_when_empty && list_empty(&smi_infos)) {
  3158. mutex_unlock(&smi_infos_lock);
  3159. cleanup_ipmi_si();
  3160. printk(KERN_WARNING PFX
  3161. "Unable to find any System Interface(s)\n");
  3162. return -ENODEV;
  3163. } else {
  3164. mutex_unlock(&smi_infos_lock);
  3165. return 0;
  3166. }
  3167. }
  3168. module_init(init_ipmi_si);
  3169. static void cleanup_one_si(struct smi_info *to_clean)
  3170. {
  3171. int rv = 0;
  3172. if (!to_clean)
  3173. return;
  3174. if (to_clean->intf) {
  3175. ipmi_smi_t intf = to_clean->intf;
  3176. to_clean->intf = NULL;
  3177. rv = ipmi_unregister_smi(intf);
  3178. if (rv) {
  3179. pr_err(PFX "Unable to unregister device: errno=%d\n",
  3180. rv);
  3181. }
  3182. }
  3183. if (to_clean->dev)
  3184. dev_set_drvdata(to_clean->dev, NULL);
  3185. list_del(&to_clean->link);
  3186. /*
  3187. * Make sure that interrupts, the timer and the thread are
  3188. * stopped and will not run again.
  3189. */
  3190. if (to_clean->irq_cleanup)
  3191. to_clean->irq_cleanup(to_clean);
  3192. wait_for_timer_and_thread(to_clean);
  3193. /*
  3194. * Timeouts are stopped, now make sure the interrupts are off
  3195. * in the BMC. Note that timers and CPU interrupts are off,
  3196. * so no need for locks.
  3197. */
  3198. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  3199. poll(to_clean);
  3200. schedule_timeout_uninterruptible(1);
  3201. }
  3202. disable_si_irq(to_clean);
  3203. while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
  3204. poll(to_clean);
  3205. schedule_timeout_uninterruptible(1);
  3206. }
  3207. if (to_clean->handlers)
  3208. to_clean->handlers->cleanup(to_clean->si_sm);
  3209. kfree(to_clean->si_sm);
  3210. if (to_clean->addr_source_cleanup)
  3211. to_clean->addr_source_cleanup(to_clean);
  3212. if (to_clean->io_cleanup)
  3213. to_clean->io_cleanup(to_clean);
  3214. if (to_clean->dev_registered)
  3215. platform_device_unregister(to_clean->pdev);
  3216. kfree(to_clean);
  3217. }
  3218. static void cleanup_ipmi_si(void)
  3219. {
  3220. struct smi_info *e, *tmp_e;
  3221. if (!initialized)
  3222. return;
  3223. #ifdef CONFIG_PCI
  3224. if (pci_registered)
  3225. pci_unregister_driver(&ipmi_pci_driver);
  3226. #endif
  3227. #ifdef CONFIG_ACPI
  3228. if (pnp_registered)
  3229. pnp_unregister_driver(&ipmi_pnp_driver);
  3230. #endif
  3231. #ifdef CONFIG_PARISC
  3232. if (parisc_registered)
  3233. unregister_parisc_driver(&ipmi_parisc_driver);
  3234. #endif
  3235. platform_driver_unregister(&ipmi_driver);
  3236. mutex_lock(&smi_infos_lock);
  3237. list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
  3238. cleanup_one_si(e);
  3239. mutex_unlock(&smi_infos_lock);
  3240. }
  3241. module_exit(cleanup_ipmi_si);
  3242. MODULE_LICENSE("GPL");
  3243. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  3244. MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
  3245. " system interfaces.");