ipmi_si_intf.c 89 KB

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