ipmi_si_intf.c 89 KB

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