ipmi_si_intf.c 90 KB

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