ipmi_si_intf.c 89 KB

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