ipmi_si_intf.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ipmi_si.c
  4. *
  5. * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
  6. * BT).
  7. *
  8. * Author: MontaVista Software, Inc.
  9. * Corey Minyard <[email protected]>
  10. * [email protected]
  11. *
  12. * Copyright 2002 MontaVista Software Inc.
  13. * Copyright 2006 IBM Corp., Christian Krafft <[email protected]>
  14. */
  15. /*
  16. * This file holds the "policy" for the interface to the SMI state
  17. * machine. It does the configuration, handles timers and interrupts,
  18. * and drives the real SMI state machine.
  19. */
  20. #define pr_fmt(fmt) "ipmi_si: " fmt
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/sched.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/timer.h>
  26. #include <linux/errno.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/list.h>
  31. #include <linux/notifier.h>
  32. #include <linux/mutex.h>
  33. #include <linux/kthread.h>
  34. #include <asm/irq.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/ipmi.h>
  38. #include <linux/ipmi_smi.h>
  39. #include "ipmi_si.h"
  40. #include "ipmi_si_sm.h"
  41. #include <linux/string.h>
  42. #include <linux/ctype.h>
  43. /* Measure times between events in the driver. */
  44. #undef DEBUG_TIMING
  45. /* Call every 10 ms. */
  46. #define SI_TIMEOUT_TIME_USEC 10000
  47. #define SI_USEC_PER_JIFFY (1000000/HZ)
  48. #define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
  49. #define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
  50. short timeout */
  51. enum si_intf_state {
  52. SI_NORMAL,
  53. SI_GETTING_FLAGS,
  54. SI_GETTING_EVENTS,
  55. SI_CLEARING_FLAGS,
  56. SI_GETTING_MESSAGES,
  57. SI_CHECKING_ENABLES,
  58. SI_SETTING_ENABLES
  59. /* FIXME - add watchdog stuff. */
  60. };
  61. /* Some BT-specific defines we need here. */
  62. #define IPMI_BT_INTMASK_REG 2
  63. #define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
  64. #define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
  65. /* 'invalid' to allow a firmware-specified interface to be disabled */
  66. const char *const si_to_str[] = { "invalid", "kcs", "smic", "bt", NULL };
  67. static bool initialized;
  68. /*
  69. * Indexes into stats[] in smi_info below.
  70. */
  71. enum si_stat_indexes {
  72. /*
  73. * Number of times the driver requested a timer while an operation
  74. * was in progress.
  75. */
  76. SI_STAT_short_timeouts = 0,
  77. /*
  78. * Number of times the driver requested a timer while nothing was in
  79. * progress.
  80. */
  81. SI_STAT_long_timeouts,
  82. /* Number of times the interface was idle while being polled. */
  83. SI_STAT_idles,
  84. /* Number of interrupts the driver handled. */
  85. SI_STAT_interrupts,
  86. /* Number of time the driver got an ATTN from the hardware. */
  87. SI_STAT_attentions,
  88. /* Number of times the driver requested flags from the hardware. */
  89. SI_STAT_flag_fetches,
  90. /* Number of times the hardware didn't follow the state machine. */
  91. SI_STAT_hosed_count,
  92. /* Number of completed messages. */
  93. SI_STAT_complete_transactions,
  94. /* Number of IPMI events received from the hardware. */
  95. SI_STAT_events,
  96. /* Number of watchdog pretimeouts. */
  97. SI_STAT_watchdog_pretimeouts,
  98. /* Number of asynchronous messages received. */
  99. SI_STAT_incoming_messages,
  100. /* This *must* remain last, add new values above this. */
  101. SI_NUM_STATS
  102. };
  103. struct smi_info {
  104. int si_num;
  105. struct ipmi_smi *intf;
  106. struct si_sm_data *si_sm;
  107. const struct si_sm_handlers *handlers;
  108. spinlock_t si_lock;
  109. struct ipmi_smi_msg *waiting_msg;
  110. struct ipmi_smi_msg *curr_msg;
  111. enum si_intf_state si_state;
  112. /*
  113. * Used to handle the various types of I/O that can occur with
  114. * IPMI
  115. */
  116. struct si_sm_io io;
  117. /*
  118. * Per-OEM handler, called from handle_flags(). Returns 1
  119. * when handle_flags() needs to be re-run or 0 indicating it
  120. * set si_state itself.
  121. */
  122. int (*oem_data_avail_handler)(struct smi_info *smi_info);
  123. /*
  124. * Flags from the last GET_MSG_FLAGS command, used when an ATTN
  125. * is set to hold the flags until we are done handling everything
  126. * from the flags.
  127. */
  128. #define RECEIVE_MSG_AVAIL 0x01
  129. #define EVENT_MSG_BUFFER_FULL 0x02
  130. #define WDT_PRE_TIMEOUT_INT 0x08
  131. #define OEM0_DATA_AVAIL 0x20
  132. #define OEM1_DATA_AVAIL 0x40
  133. #define OEM2_DATA_AVAIL 0x80
  134. #define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
  135. OEM1_DATA_AVAIL | \
  136. OEM2_DATA_AVAIL)
  137. unsigned char msg_flags;
  138. /* Does the BMC have an event buffer? */
  139. bool has_event_buffer;
  140. /*
  141. * If set to true, this will request events the next time the
  142. * state machine is idle.
  143. */
  144. atomic_t req_events;
  145. /*
  146. * If true, run the state machine to completion on every send
  147. * call. Generally used after a panic to make sure stuff goes
  148. * out.
  149. */
  150. bool run_to_completion;
  151. /* The timer for this si. */
  152. struct timer_list si_timer;
  153. /* This flag is set, if the timer can be set */
  154. bool timer_can_start;
  155. /* This flag is set, if the timer is running (timer_pending() isn't enough) */
  156. bool timer_running;
  157. /* The time (in jiffies) the last timeout occurred at. */
  158. unsigned long last_timeout_jiffies;
  159. /* Are we waiting for the events, pretimeouts, received msgs? */
  160. atomic_t need_watch;
  161. /*
  162. * The driver will disable interrupts when it gets into a
  163. * situation where it cannot handle messages due to lack of
  164. * memory. Once that situation clears up, it will re-enable
  165. * interrupts.
  166. */
  167. bool interrupt_disabled;
  168. /*
  169. * Does the BMC support events?
  170. */
  171. bool supports_event_msg_buff;
  172. /*
  173. * Can we disable interrupts the global enables receive irq
  174. * bit? There are currently two forms of brokenness, some
  175. * systems cannot disable the bit (which is technically within
  176. * the spec but a bad idea) and some systems have the bit
  177. * forced to zero even though interrupts work (which is
  178. * clearly outside the spec). The next bool tells which form
  179. * of brokenness is present.
  180. */
  181. bool cannot_disable_irq;
  182. /*
  183. * Some systems are broken and cannot set the irq enable
  184. * bit, even if they support interrupts.
  185. */
  186. bool irq_enable_broken;
  187. /* Is the driver in maintenance mode? */
  188. bool in_maintenance_mode;
  189. /*
  190. * Did we get an attention that we did not handle?
  191. */
  192. bool got_attn;
  193. /* From the get device id response... */
  194. struct ipmi_device_id device_id;
  195. /* Have we added the device group to the device? */
  196. bool dev_group_added;
  197. /* Counters and things for the proc filesystem. */
  198. atomic_t stats[SI_NUM_STATS];
  199. struct task_struct *thread;
  200. struct list_head link;
  201. };
  202. #define smi_inc_stat(smi, stat) \
  203. atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
  204. #define smi_get_stat(smi, stat) \
  205. ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
  206. #define IPMI_MAX_INTFS 4
  207. static int force_kipmid[IPMI_MAX_INTFS];
  208. static int num_force_kipmid;
  209. static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
  210. static int num_max_busy_us;
  211. static bool unload_when_empty = true;
  212. static int try_smi_init(struct smi_info *smi);
  213. static void cleanup_one_si(struct smi_info *smi_info);
  214. static void cleanup_ipmi_si(void);
  215. #ifdef DEBUG_TIMING
  216. void debug_timestamp(struct smi_info *smi_info, char *msg)
  217. {
  218. struct timespec64 t;
  219. ktime_get_ts64(&t);
  220. dev_dbg(smi_info->io.dev, "**%s: %lld.%9.9ld\n",
  221. msg, t.tv_sec, t.tv_nsec);
  222. }
  223. #else
  224. #define debug_timestamp(smi_info, x)
  225. #endif
  226. static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
  227. static int register_xaction_notifier(struct notifier_block *nb)
  228. {
  229. return atomic_notifier_chain_register(&xaction_notifier_list, nb);
  230. }
  231. static void deliver_recv_msg(struct smi_info *smi_info,
  232. struct ipmi_smi_msg *msg)
  233. {
  234. /* Deliver the message to the upper layer. */
  235. ipmi_smi_msg_received(smi_info->intf, msg);
  236. }
  237. static void return_hosed_msg(struct smi_info *smi_info, int cCode)
  238. {
  239. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  240. if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
  241. cCode = IPMI_ERR_UNSPECIFIED;
  242. /* else use it as is */
  243. /* Make it a response */
  244. msg->rsp[0] = msg->data[0] | 4;
  245. msg->rsp[1] = msg->data[1];
  246. msg->rsp[2] = cCode;
  247. msg->rsp_size = 3;
  248. smi_info->curr_msg = NULL;
  249. deliver_recv_msg(smi_info, msg);
  250. }
  251. static enum si_sm_result start_next_msg(struct smi_info *smi_info)
  252. {
  253. int rv;
  254. if (!smi_info->waiting_msg) {
  255. smi_info->curr_msg = NULL;
  256. rv = SI_SM_IDLE;
  257. } else {
  258. int err;
  259. smi_info->curr_msg = smi_info->waiting_msg;
  260. smi_info->waiting_msg = NULL;
  261. debug_timestamp(smi_info, "Start2");
  262. err = atomic_notifier_call_chain(&xaction_notifier_list,
  263. 0, smi_info);
  264. if (err & NOTIFY_STOP_MASK) {
  265. rv = SI_SM_CALL_WITHOUT_DELAY;
  266. goto out;
  267. }
  268. err = smi_info->handlers->start_transaction(
  269. smi_info->si_sm,
  270. smi_info->curr_msg->data,
  271. smi_info->curr_msg->data_size);
  272. if (err)
  273. return_hosed_msg(smi_info, err);
  274. rv = SI_SM_CALL_WITHOUT_DELAY;
  275. }
  276. out:
  277. return rv;
  278. }
  279. static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
  280. {
  281. if (!smi_info->timer_can_start)
  282. return;
  283. smi_info->last_timeout_jiffies = jiffies;
  284. mod_timer(&smi_info->si_timer, new_val);
  285. smi_info->timer_running = true;
  286. }
  287. /*
  288. * Start a new message and (re)start the timer and thread.
  289. */
  290. static void start_new_msg(struct smi_info *smi_info, unsigned char *msg,
  291. unsigned int size)
  292. {
  293. smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
  294. if (smi_info->thread)
  295. wake_up_process(smi_info->thread);
  296. smi_info->handlers->start_transaction(smi_info->si_sm, msg, size);
  297. }
  298. static void start_check_enables(struct smi_info *smi_info)
  299. {
  300. unsigned char msg[2];
  301. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  302. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  303. start_new_msg(smi_info, msg, 2);
  304. smi_info->si_state = SI_CHECKING_ENABLES;
  305. }
  306. static void start_clear_flags(struct smi_info *smi_info)
  307. {
  308. unsigned char msg[3];
  309. /* Make sure the watchdog pre-timeout flag is not set at startup. */
  310. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  311. msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
  312. msg[2] = WDT_PRE_TIMEOUT_INT;
  313. start_new_msg(smi_info, msg, 3);
  314. smi_info->si_state = SI_CLEARING_FLAGS;
  315. }
  316. static void start_getting_msg_queue(struct smi_info *smi_info)
  317. {
  318. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  319. smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
  320. smi_info->curr_msg->data_size = 2;
  321. start_new_msg(smi_info, smi_info->curr_msg->data,
  322. smi_info->curr_msg->data_size);
  323. smi_info->si_state = SI_GETTING_MESSAGES;
  324. }
  325. static void start_getting_events(struct smi_info *smi_info)
  326. {
  327. smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
  328. smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
  329. smi_info->curr_msg->data_size = 2;
  330. start_new_msg(smi_info, smi_info->curr_msg->data,
  331. smi_info->curr_msg->data_size);
  332. smi_info->si_state = SI_GETTING_EVENTS;
  333. }
  334. /*
  335. * When we have a situtaion where we run out of memory and cannot
  336. * allocate messages, we just leave them in the BMC and run the system
  337. * polled until we can allocate some memory. Once we have some
  338. * memory, we will re-enable the interrupt.
  339. *
  340. * Note that we cannot just use disable_irq(), since the interrupt may
  341. * be shared.
  342. */
  343. static inline bool disable_si_irq(struct smi_info *smi_info)
  344. {
  345. if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
  346. smi_info->interrupt_disabled = true;
  347. start_check_enables(smi_info);
  348. return true;
  349. }
  350. return false;
  351. }
  352. static inline bool enable_si_irq(struct smi_info *smi_info)
  353. {
  354. if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
  355. smi_info->interrupt_disabled = false;
  356. start_check_enables(smi_info);
  357. return true;
  358. }
  359. return false;
  360. }
  361. /*
  362. * Allocate a message. If unable to allocate, start the interrupt
  363. * disable process and return NULL. If able to allocate but
  364. * interrupts are disabled, free the message and return NULL after
  365. * starting the interrupt enable process.
  366. */
  367. static struct ipmi_smi_msg *alloc_msg_handle_irq(struct smi_info *smi_info)
  368. {
  369. struct ipmi_smi_msg *msg;
  370. msg = ipmi_alloc_smi_msg();
  371. if (!msg) {
  372. if (!disable_si_irq(smi_info))
  373. smi_info->si_state = SI_NORMAL;
  374. } else if (enable_si_irq(smi_info)) {
  375. ipmi_free_smi_msg(msg);
  376. msg = NULL;
  377. }
  378. return msg;
  379. }
  380. static void handle_flags(struct smi_info *smi_info)
  381. {
  382. retry:
  383. if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
  384. /* Watchdog pre-timeout */
  385. smi_inc_stat(smi_info, watchdog_pretimeouts);
  386. start_clear_flags(smi_info);
  387. smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
  388. ipmi_smi_watchdog_pretimeout(smi_info->intf);
  389. } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
  390. /* Messages available. */
  391. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  392. if (!smi_info->curr_msg)
  393. return;
  394. start_getting_msg_queue(smi_info);
  395. } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
  396. /* Events available. */
  397. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  398. if (!smi_info->curr_msg)
  399. return;
  400. start_getting_events(smi_info);
  401. } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
  402. smi_info->oem_data_avail_handler) {
  403. if (smi_info->oem_data_avail_handler(smi_info))
  404. goto retry;
  405. } else
  406. smi_info->si_state = SI_NORMAL;
  407. }
  408. /*
  409. * Global enables we care about.
  410. */
  411. #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
  412. IPMI_BMC_EVT_MSG_INTR)
  413. static u8 current_global_enables(struct smi_info *smi_info, u8 base,
  414. bool *irq_on)
  415. {
  416. u8 enables = 0;
  417. if (smi_info->supports_event_msg_buff)
  418. enables |= IPMI_BMC_EVT_MSG_BUFF;
  419. if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
  420. smi_info->cannot_disable_irq) &&
  421. !smi_info->irq_enable_broken)
  422. enables |= IPMI_BMC_RCV_MSG_INTR;
  423. if (smi_info->supports_event_msg_buff &&
  424. smi_info->io.irq && !smi_info->interrupt_disabled &&
  425. !smi_info->irq_enable_broken)
  426. enables |= IPMI_BMC_EVT_MSG_INTR;
  427. *irq_on = enables & (IPMI_BMC_EVT_MSG_INTR | IPMI_BMC_RCV_MSG_INTR);
  428. return enables;
  429. }
  430. static void check_bt_irq(struct smi_info *smi_info, bool irq_on)
  431. {
  432. u8 irqstate = smi_info->io.inputb(&smi_info->io, IPMI_BT_INTMASK_REG);
  433. irqstate &= IPMI_BT_INTMASK_ENABLE_IRQ_BIT;
  434. if ((bool)irqstate == irq_on)
  435. return;
  436. if (irq_on)
  437. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  438. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  439. else
  440. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG, 0);
  441. }
  442. static void handle_transaction_done(struct smi_info *smi_info)
  443. {
  444. struct ipmi_smi_msg *msg;
  445. debug_timestamp(smi_info, "Done");
  446. switch (smi_info->si_state) {
  447. case SI_NORMAL:
  448. if (!smi_info->curr_msg)
  449. break;
  450. smi_info->curr_msg->rsp_size
  451. = smi_info->handlers->get_result(
  452. smi_info->si_sm,
  453. smi_info->curr_msg->rsp,
  454. IPMI_MAX_MSG_LENGTH);
  455. /*
  456. * Do this here becase deliver_recv_msg() releases the
  457. * lock, and a new message can be put in during the
  458. * time the lock is released.
  459. */
  460. msg = smi_info->curr_msg;
  461. smi_info->curr_msg = NULL;
  462. deliver_recv_msg(smi_info, msg);
  463. break;
  464. case SI_GETTING_FLAGS:
  465. {
  466. unsigned char msg[4];
  467. unsigned int len;
  468. /* We got the flags from the SMI, now handle them. */
  469. len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  470. if (msg[2] != 0) {
  471. /* Error fetching flags, just give up for now. */
  472. smi_info->si_state = SI_NORMAL;
  473. } else if (len < 4) {
  474. /*
  475. * Hmm, no flags. That's technically illegal, but
  476. * don't use uninitialized data.
  477. */
  478. smi_info->si_state = SI_NORMAL;
  479. } else {
  480. smi_info->msg_flags = msg[3];
  481. handle_flags(smi_info);
  482. }
  483. break;
  484. }
  485. case SI_CLEARING_FLAGS:
  486. {
  487. unsigned char msg[3];
  488. /* We cleared the flags. */
  489. smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
  490. if (msg[2] != 0) {
  491. /* Error clearing flags */
  492. dev_warn_ratelimited(smi_info->io.dev,
  493. "Error clearing flags: %2.2x\n", msg[2]);
  494. }
  495. smi_info->si_state = SI_NORMAL;
  496. break;
  497. }
  498. case SI_GETTING_EVENTS:
  499. {
  500. smi_info->curr_msg->rsp_size
  501. = smi_info->handlers->get_result(
  502. smi_info->si_sm,
  503. smi_info->curr_msg->rsp,
  504. IPMI_MAX_MSG_LENGTH);
  505. /*
  506. * Do this here becase deliver_recv_msg() releases the
  507. * lock, and a new message can be put in during the
  508. * time the lock is released.
  509. */
  510. msg = smi_info->curr_msg;
  511. smi_info->curr_msg = NULL;
  512. if (msg->rsp[2] != 0) {
  513. /* Error getting event, probably done. */
  514. msg->done(msg);
  515. /* Take off the event flag. */
  516. smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
  517. handle_flags(smi_info);
  518. } else {
  519. smi_inc_stat(smi_info, events);
  520. /*
  521. * Do this before we deliver the message
  522. * because delivering the message releases the
  523. * lock and something else can mess with the
  524. * state.
  525. */
  526. handle_flags(smi_info);
  527. deliver_recv_msg(smi_info, msg);
  528. }
  529. break;
  530. }
  531. case SI_GETTING_MESSAGES:
  532. {
  533. smi_info->curr_msg->rsp_size
  534. = smi_info->handlers->get_result(
  535. smi_info->si_sm,
  536. smi_info->curr_msg->rsp,
  537. IPMI_MAX_MSG_LENGTH);
  538. /*
  539. * Do this here becase deliver_recv_msg() releases the
  540. * lock, and a new message can be put in during the
  541. * time the lock is released.
  542. */
  543. msg = smi_info->curr_msg;
  544. smi_info->curr_msg = NULL;
  545. if (msg->rsp[2] != 0) {
  546. /* Error getting event, probably done. */
  547. msg->done(msg);
  548. /* Take off the msg flag. */
  549. smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
  550. handle_flags(smi_info);
  551. } else {
  552. smi_inc_stat(smi_info, incoming_messages);
  553. /*
  554. * Do this before we deliver the message
  555. * because delivering the message releases the
  556. * lock and something else can mess with the
  557. * state.
  558. */
  559. handle_flags(smi_info);
  560. deliver_recv_msg(smi_info, msg);
  561. }
  562. break;
  563. }
  564. case SI_CHECKING_ENABLES:
  565. {
  566. unsigned char msg[4];
  567. u8 enables;
  568. bool irq_on;
  569. /* We got the flags from the SMI, now handle them. */
  570. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  571. if (msg[2] != 0) {
  572. dev_warn_ratelimited(smi_info->io.dev,
  573. "Couldn't get irq info: %x,\n"
  574. "Maybe ok, but ipmi might run very slowly.\n",
  575. msg[2]);
  576. smi_info->si_state = SI_NORMAL;
  577. break;
  578. }
  579. enables = current_global_enables(smi_info, 0, &irq_on);
  580. if (smi_info->io.si_type == SI_BT)
  581. /* BT has its own interrupt enable bit. */
  582. check_bt_irq(smi_info, irq_on);
  583. if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
  584. /* Enables are not correct, fix them. */
  585. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  586. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  587. msg[2] = enables | (msg[3] & ~GLOBAL_ENABLES_MASK);
  588. smi_info->handlers->start_transaction(
  589. smi_info->si_sm, msg, 3);
  590. smi_info->si_state = SI_SETTING_ENABLES;
  591. } else if (smi_info->supports_event_msg_buff) {
  592. smi_info->curr_msg = ipmi_alloc_smi_msg();
  593. if (!smi_info->curr_msg) {
  594. smi_info->si_state = SI_NORMAL;
  595. break;
  596. }
  597. start_getting_events(smi_info);
  598. } else {
  599. smi_info->si_state = SI_NORMAL;
  600. }
  601. break;
  602. }
  603. case SI_SETTING_ENABLES:
  604. {
  605. unsigned char msg[4];
  606. smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
  607. if (msg[2] != 0)
  608. dev_warn_ratelimited(smi_info->io.dev,
  609. "Could not set the global enables: 0x%x.\n",
  610. msg[2]);
  611. if (smi_info->supports_event_msg_buff) {
  612. smi_info->curr_msg = ipmi_alloc_smi_msg();
  613. if (!smi_info->curr_msg) {
  614. smi_info->si_state = SI_NORMAL;
  615. break;
  616. }
  617. start_getting_events(smi_info);
  618. } else {
  619. smi_info->si_state = SI_NORMAL;
  620. }
  621. break;
  622. }
  623. }
  624. }
  625. /*
  626. * Called on timeouts and events. Timeouts should pass the elapsed
  627. * time, interrupts should pass in zero. Must be called with
  628. * si_lock held and interrupts disabled.
  629. */
  630. static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
  631. int time)
  632. {
  633. enum si_sm_result si_sm_result;
  634. restart:
  635. /*
  636. * There used to be a loop here that waited a little while
  637. * (around 25us) before giving up. That turned out to be
  638. * pointless, the minimum delays I was seeing were in the 300us
  639. * range, which is far too long to wait in an interrupt. So
  640. * we just run until the state machine tells us something
  641. * happened or it needs a delay.
  642. */
  643. si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
  644. time = 0;
  645. while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
  646. si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
  647. if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
  648. smi_inc_stat(smi_info, complete_transactions);
  649. handle_transaction_done(smi_info);
  650. goto restart;
  651. } else if (si_sm_result == SI_SM_HOSED) {
  652. smi_inc_stat(smi_info, hosed_count);
  653. /*
  654. * Do the before return_hosed_msg, because that
  655. * releases the lock.
  656. */
  657. smi_info->si_state = SI_NORMAL;
  658. if (smi_info->curr_msg != NULL) {
  659. /*
  660. * If we were handling a user message, format
  661. * a response to send to the upper layer to
  662. * tell it about the error.
  663. */
  664. return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
  665. }
  666. goto restart;
  667. }
  668. /*
  669. * We prefer handling attn over new messages. But don't do
  670. * this if there is not yet an upper layer to handle anything.
  671. */
  672. if (si_sm_result == SI_SM_ATTN || smi_info->got_attn) {
  673. unsigned char msg[2];
  674. if (smi_info->si_state != SI_NORMAL) {
  675. /*
  676. * We got an ATTN, but we are doing something else.
  677. * Handle the ATTN later.
  678. */
  679. smi_info->got_attn = true;
  680. } else {
  681. smi_info->got_attn = false;
  682. smi_inc_stat(smi_info, attentions);
  683. /*
  684. * Got a attn, send down a get message flags to see
  685. * what's causing it. It would be better to handle
  686. * this in the upper layer, but due to the way
  687. * interrupts work with the SMI, that's not really
  688. * possible.
  689. */
  690. msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
  691. msg[1] = IPMI_GET_MSG_FLAGS_CMD;
  692. start_new_msg(smi_info, msg, 2);
  693. smi_info->si_state = SI_GETTING_FLAGS;
  694. goto restart;
  695. }
  696. }
  697. /* If we are currently idle, try to start the next message. */
  698. if (si_sm_result == SI_SM_IDLE) {
  699. smi_inc_stat(smi_info, idles);
  700. si_sm_result = start_next_msg(smi_info);
  701. if (si_sm_result != SI_SM_IDLE)
  702. goto restart;
  703. }
  704. if ((si_sm_result == SI_SM_IDLE)
  705. && (atomic_read(&smi_info->req_events))) {
  706. /*
  707. * We are idle and the upper layer requested that I fetch
  708. * events, so do so.
  709. */
  710. atomic_set(&smi_info->req_events, 0);
  711. /*
  712. * Take this opportunity to check the interrupt and
  713. * message enable state for the BMC. The BMC can be
  714. * asynchronously reset, and may thus get interrupts
  715. * disable and messages disabled.
  716. */
  717. if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
  718. start_check_enables(smi_info);
  719. } else {
  720. smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
  721. if (!smi_info->curr_msg)
  722. goto out;
  723. start_getting_events(smi_info);
  724. }
  725. goto restart;
  726. }
  727. if (si_sm_result == SI_SM_IDLE && smi_info->timer_running) {
  728. /* Ok it if fails, the timer will just go off. */
  729. if (del_timer(&smi_info->si_timer))
  730. smi_info->timer_running = false;
  731. }
  732. out:
  733. return si_sm_result;
  734. }
  735. static void check_start_timer_thread(struct smi_info *smi_info)
  736. {
  737. if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
  738. smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
  739. if (smi_info->thread)
  740. wake_up_process(smi_info->thread);
  741. start_next_msg(smi_info);
  742. smi_event_handler(smi_info, 0);
  743. }
  744. }
  745. static void flush_messages(void *send_info)
  746. {
  747. struct smi_info *smi_info = send_info;
  748. enum si_sm_result result;
  749. /*
  750. * Currently, this function is called only in run-to-completion
  751. * mode. This means we are single-threaded, no need for locks.
  752. */
  753. result = smi_event_handler(smi_info, 0);
  754. while (result != SI_SM_IDLE) {
  755. udelay(SI_SHORT_TIMEOUT_USEC);
  756. result = smi_event_handler(smi_info, SI_SHORT_TIMEOUT_USEC);
  757. }
  758. }
  759. static void sender(void *send_info,
  760. struct ipmi_smi_msg *msg)
  761. {
  762. struct smi_info *smi_info = send_info;
  763. unsigned long flags;
  764. debug_timestamp(smi_info, "Enqueue");
  765. if (smi_info->run_to_completion) {
  766. /*
  767. * If we are running to completion, start it. Upper
  768. * layer will call flush_messages to clear it out.
  769. */
  770. smi_info->waiting_msg = msg;
  771. return;
  772. }
  773. spin_lock_irqsave(&smi_info->si_lock, flags);
  774. /*
  775. * The following two lines don't need to be under the lock for
  776. * the lock's sake, but they do need SMP memory barriers to
  777. * avoid getting things out of order. We are already claiming
  778. * the lock, anyway, so just do it under the lock to avoid the
  779. * ordering problem.
  780. */
  781. BUG_ON(smi_info->waiting_msg);
  782. smi_info->waiting_msg = msg;
  783. check_start_timer_thread(smi_info);
  784. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  785. }
  786. static void set_run_to_completion(void *send_info, bool i_run_to_completion)
  787. {
  788. struct smi_info *smi_info = send_info;
  789. smi_info->run_to_completion = i_run_to_completion;
  790. if (i_run_to_completion)
  791. flush_messages(smi_info);
  792. }
  793. /*
  794. * Use -1 as a special constant to tell that we are spinning in kipmid
  795. * looking for something and not delaying between checks
  796. */
  797. #define IPMI_TIME_NOT_BUSY ns_to_ktime(-1ull)
  798. static inline bool ipmi_thread_busy_wait(enum si_sm_result smi_result,
  799. const struct smi_info *smi_info,
  800. ktime_t *busy_until)
  801. {
  802. unsigned int max_busy_us = 0;
  803. if (smi_info->si_num < num_max_busy_us)
  804. max_busy_us = kipmid_max_busy_us[smi_info->si_num];
  805. if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
  806. *busy_until = IPMI_TIME_NOT_BUSY;
  807. else if (*busy_until == IPMI_TIME_NOT_BUSY) {
  808. *busy_until = ktime_get() + max_busy_us * NSEC_PER_USEC;
  809. } else {
  810. if (unlikely(ktime_get() > *busy_until)) {
  811. *busy_until = IPMI_TIME_NOT_BUSY;
  812. return false;
  813. }
  814. }
  815. return true;
  816. }
  817. /*
  818. * A busy-waiting loop for speeding up IPMI operation.
  819. *
  820. * Lousy hardware makes this hard. This is only enabled for systems
  821. * that are not BT and do not have interrupts. It starts spinning
  822. * when an operation is complete or until max_busy tells it to stop
  823. * (if that is enabled). See the paragraph on kimid_max_busy_us in
  824. * Documentation/driver-api/ipmi.rst for details.
  825. */
  826. static int ipmi_thread(void *data)
  827. {
  828. struct smi_info *smi_info = data;
  829. unsigned long flags;
  830. enum si_sm_result smi_result;
  831. ktime_t busy_until = IPMI_TIME_NOT_BUSY;
  832. set_user_nice(current, MAX_NICE);
  833. while (!kthread_should_stop()) {
  834. int busy_wait;
  835. spin_lock_irqsave(&(smi_info->si_lock), flags);
  836. smi_result = smi_event_handler(smi_info, 0);
  837. /*
  838. * If the driver is doing something, there is a possible
  839. * race with the timer. If the timer handler see idle,
  840. * and the thread here sees something else, the timer
  841. * handler won't restart the timer even though it is
  842. * required. So start it here if necessary.
  843. */
  844. if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
  845. smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
  846. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  847. busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
  848. &busy_until);
  849. if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  850. ; /* do nothing */
  851. } else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait) {
  852. /*
  853. * In maintenance mode we run as fast as
  854. * possible to allow firmware updates to
  855. * complete as fast as possible, but normally
  856. * don't bang on the scheduler.
  857. */
  858. if (smi_info->in_maintenance_mode)
  859. schedule();
  860. else
  861. usleep_range(100, 200);
  862. } else if (smi_result == SI_SM_IDLE) {
  863. if (atomic_read(&smi_info->need_watch)) {
  864. schedule_timeout_interruptible(100);
  865. } else {
  866. /* Wait to be woken up when we are needed. */
  867. __set_current_state(TASK_INTERRUPTIBLE);
  868. schedule();
  869. }
  870. } else {
  871. schedule_timeout_interruptible(1);
  872. }
  873. }
  874. return 0;
  875. }
  876. static void poll(void *send_info)
  877. {
  878. struct smi_info *smi_info = send_info;
  879. unsigned long flags = 0;
  880. bool run_to_completion = smi_info->run_to_completion;
  881. /*
  882. * Make sure there is some delay in the poll loop so we can
  883. * drive time forward and timeout things.
  884. */
  885. udelay(10);
  886. if (!run_to_completion)
  887. spin_lock_irqsave(&smi_info->si_lock, flags);
  888. smi_event_handler(smi_info, 10);
  889. if (!run_to_completion)
  890. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  891. }
  892. static void request_events(void *send_info)
  893. {
  894. struct smi_info *smi_info = send_info;
  895. if (!smi_info->has_event_buffer)
  896. return;
  897. atomic_set(&smi_info->req_events, 1);
  898. }
  899. static void set_need_watch(void *send_info, unsigned int watch_mask)
  900. {
  901. struct smi_info *smi_info = send_info;
  902. unsigned long flags;
  903. int enable;
  904. enable = !!watch_mask;
  905. atomic_set(&smi_info->need_watch, enable);
  906. spin_lock_irqsave(&smi_info->si_lock, flags);
  907. check_start_timer_thread(smi_info);
  908. spin_unlock_irqrestore(&smi_info->si_lock, flags);
  909. }
  910. static void smi_timeout(struct timer_list *t)
  911. {
  912. struct smi_info *smi_info = from_timer(smi_info, t, si_timer);
  913. enum si_sm_result smi_result;
  914. unsigned long flags;
  915. unsigned long jiffies_now;
  916. long time_diff;
  917. long timeout;
  918. spin_lock_irqsave(&(smi_info->si_lock), flags);
  919. debug_timestamp(smi_info, "Timer");
  920. jiffies_now = jiffies;
  921. time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
  922. * SI_USEC_PER_JIFFY);
  923. smi_result = smi_event_handler(smi_info, time_diff);
  924. if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
  925. /* Running with interrupts, only do long timeouts. */
  926. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  927. smi_inc_stat(smi_info, long_timeouts);
  928. goto do_mod_timer;
  929. }
  930. /*
  931. * If the state machine asks for a short delay, then shorten
  932. * the timer timeout.
  933. */
  934. if (smi_result == SI_SM_CALL_WITH_DELAY) {
  935. smi_inc_stat(smi_info, short_timeouts);
  936. timeout = jiffies + 1;
  937. } else {
  938. smi_inc_stat(smi_info, long_timeouts);
  939. timeout = jiffies + SI_TIMEOUT_JIFFIES;
  940. }
  941. do_mod_timer:
  942. if (smi_result != SI_SM_IDLE)
  943. smi_mod_timer(smi_info, timeout);
  944. else
  945. smi_info->timer_running = false;
  946. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  947. }
  948. irqreturn_t ipmi_si_irq_handler(int irq, void *data)
  949. {
  950. struct smi_info *smi_info = data;
  951. unsigned long flags;
  952. if (smi_info->io.si_type == SI_BT)
  953. /* We need to clear the IRQ flag for the BT interface. */
  954. smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
  955. IPMI_BT_INTMASK_CLEAR_IRQ_BIT
  956. | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  957. spin_lock_irqsave(&(smi_info->si_lock), flags);
  958. smi_inc_stat(smi_info, interrupts);
  959. debug_timestamp(smi_info, "Interrupt");
  960. smi_event_handler(smi_info, 0);
  961. spin_unlock_irqrestore(&(smi_info->si_lock), flags);
  962. return IRQ_HANDLED;
  963. }
  964. static int smi_start_processing(void *send_info,
  965. struct ipmi_smi *intf)
  966. {
  967. struct smi_info *new_smi = send_info;
  968. int enable = 0;
  969. new_smi->intf = intf;
  970. /* Set up the timer that drives the interface. */
  971. timer_setup(&new_smi->si_timer, smi_timeout, 0);
  972. new_smi->timer_can_start = true;
  973. smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
  974. /* Try to claim any interrupts. */
  975. if (new_smi->io.irq_setup) {
  976. new_smi->io.irq_handler_data = new_smi;
  977. new_smi->io.irq_setup(&new_smi->io);
  978. }
  979. /*
  980. * Check if the user forcefully enabled the daemon.
  981. */
  982. if (new_smi->si_num < num_force_kipmid)
  983. enable = force_kipmid[new_smi->si_num];
  984. /*
  985. * The BT interface is efficient enough to not need a thread,
  986. * and there is no need for a thread if we have interrupts.
  987. */
  988. else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
  989. enable = 1;
  990. if (enable) {
  991. new_smi->thread = kthread_run(ipmi_thread, new_smi,
  992. "kipmi%d", new_smi->si_num);
  993. if (IS_ERR(new_smi->thread)) {
  994. dev_notice(new_smi->io.dev,
  995. "Could not start kernel thread due to error %ld, only using timers to drive the interface\n",
  996. PTR_ERR(new_smi->thread));
  997. new_smi->thread = NULL;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
  1003. {
  1004. struct smi_info *smi = send_info;
  1005. data->addr_src = smi->io.addr_source;
  1006. data->dev = smi->io.dev;
  1007. data->addr_info = smi->io.addr_info;
  1008. get_device(smi->io.dev);
  1009. return 0;
  1010. }
  1011. static void set_maintenance_mode(void *send_info, bool enable)
  1012. {
  1013. struct smi_info *smi_info = send_info;
  1014. if (!enable)
  1015. atomic_set(&smi_info->req_events, 0);
  1016. smi_info->in_maintenance_mode = enable;
  1017. }
  1018. static void shutdown_smi(void *send_info);
  1019. static const struct ipmi_smi_handlers handlers = {
  1020. .owner = THIS_MODULE,
  1021. .start_processing = smi_start_processing,
  1022. .shutdown = shutdown_smi,
  1023. .get_smi_info = get_smi_info,
  1024. .sender = sender,
  1025. .request_events = request_events,
  1026. .set_need_watch = set_need_watch,
  1027. .set_maintenance_mode = set_maintenance_mode,
  1028. .set_run_to_completion = set_run_to_completion,
  1029. .flush_messages = flush_messages,
  1030. .poll = poll,
  1031. };
  1032. static LIST_HEAD(smi_infos);
  1033. static DEFINE_MUTEX(smi_infos_lock);
  1034. static int smi_num; /* Used to sequence the SMIs */
  1035. static const char * const addr_space_to_str[] = { "i/o", "mem" };
  1036. module_param_array(force_kipmid, int, &num_force_kipmid, 0);
  1037. MODULE_PARM_DESC(force_kipmid,
  1038. "Force the kipmi daemon to be enabled (1) or disabled(0). Normally the IPMI driver auto-detects this, but the value may be overridden by this parm.");
  1039. module_param(unload_when_empty, bool, 0);
  1040. MODULE_PARM_DESC(unload_when_empty,
  1041. "Unload the module if no interfaces are specified or found, default is 1. Setting to 0 is useful for hot add of devices using hotmod.");
  1042. module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
  1043. MODULE_PARM_DESC(kipmid_max_busy_us,
  1044. "Max time (in microseconds) to busy-wait for IPMI data before sleeping. 0 (default) means to wait forever. Set to 100-500 if kipmid is using up a lot of CPU time.");
  1045. void ipmi_irq_finish_setup(struct si_sm_io *io)
  1046. {
  1047. if (io->si_type == SI_BT)
  1048. /* Enable the interrupt in the BT interface. */
  1049. io->outputb(io, IPMI_BT_INTMASK_REG,
  1050. IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
  1051. }
  1052. void ipmi_irq_start_cleanup(struct si_sm_io *io)
  1053. {
  1054. if (io->si_type == SI_BT)
  1055. /* Disable the interrupt in the BT interface. */
  1056. io->outputb(io, IPMI_BT_INTMASK_REG, 0);
  1057. }
  1058. static void std_irq_cleanup(struct si_sm_io *io)
  1059. {
  1060. ipmi_irq_start_cleanup(io);
  1061. free_irq(io->irq, io->irq_handler_data);
  1062. }
  1063. int ipmi_std_irq_setup(struct si_sm_io *io)
  1064. {
  1065. int rv;
  1066. if (!io->irq)
  1067. return 0;
  1068. rv = request_irq(io->irq,
  1069. ipmi_si_irq_handler,
  1070. IRQF_SHARED,
  1071. SI_DEVICE_NAME,
  1072. io->irq_handler_data);
  1073. if (rv) {
  1074. dev_warn(io->dev, "%s unable to claim interrupt %d, running polled\n",
  1075. SI_DEVICE_NAME, io->irq);
  1076. io->irq = 0;
  1077. } else {
  1078. io->irq_cleanup = std_irq_cleanup;
  1079. ipmi_irq_finish_setup(io);
  1080. dev_info(io->dev, "Using irq %d\n", io->irq);
  1081. }
  1082. return rv;
  1083. }
  1084. static int wait_for_msg_done(struct smi_info *smi_info)
  1085. {
  1086. enum si_sm_result smi_result;
  1087. smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
  1088. for (;;) {
  1089. if (smi_result == SI_SM_CALL_WITH_DELAY ||
  1090. smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
  1091. schedule_timeout_uninterruptible(1);
  1092. smi_result = smi_info->handlers->event(
  1093. smi_info->si_sm, jiffies_to_usecs(1));
  1094. } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
  1095. smi_result = smi_info->handlers->event(
  1096. smi_info->si_sm, 0);
  1097. } else
  1098. break;
  1099. }
  1100. if (smi_result == SI_SM_HOSED)
  1101. /*
  1102. * We couldn't get the state machine to run, so whatever's at
  1103. * the port is probably not an IPMI SMI interface.
  1104. */
  1105. return -ENODEV;
  1106. return 0;
  1107. }
  1108. static int try_get_dev_id(struct smi_info *smi_info)
  1109. {
  1110. unsigned char msg[2];
  1111. unsigned char *resp;
  1112. unsigned long resp_len;
  1113. int rv = 0;
  1114. unsigned int retry_count = 0;
  1115. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1116. if (!resp)
  1117. return -ENOMEM;
  1118. /*
  1119. * Do a Get Device ID command, since it comes back with some
  1120. * useful info.
  1121. */
  1122. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1123. msg[1] = IPMI_GET_DEVICE_ID_CMD;
  1124. retry:
  1125. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  1126. rv = wait_for_msg_done(smi_info);
  1127. if (rv)
  1128. goto out;
  1129. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1130. resp, IPMI_MAX_MSG_LENGTH);
  1131. /* Check and record info from the get device id, in case we need it. */
  1132. rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
  1133. resp + 2, resp_len - 2, &smi_info->device_id);
  1134. if (rv) {
  1135. /* record completion code */
  1136. unsigned char cc = *(resp + 2);
  1137. if (cc != IPMI_CC_NO_ERROR &&
  1138. ++retry_count <= GET_DEVICE_ID_MAX_RETRY) {
  1139. dev_warn_ratelimited(smi_info->io.dev,
  1140. "BMC returned 0x%2.2x, retry get bmc device id\n",
  1141. cc);
  1142. goto retry;
  1143. }
  1144. }
  1145. out:
  1146. kfree(resp);
  1147. return rv;
  1148. }
  1149. static int get_global_enables(struct smi_info *smi_info, u8 *enables)
  1150. {
  1151. unsigned char msg[3];
  1152. unsigned char *resp;
  1153. unsigned long resp_len;
  1154. int rv;
  1155. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1156. if (!resp)
  1157. return -ENOMEM;
  1158. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1159. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  1160. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  1161. rv = wait_for_msg_done(smi_info);
  1162. if (rv) {
  1163. dev_warn(smi_info->io.dev,
  1164. "Error getting response from get global enables command: %d\n",
  1165. rv);
  1166. goto out;
  1167. }
  1168. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1169. resp, IPMI_MAX_MSG_LENGTH);
  1170. if (resp_len < 4 ||
  1171. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  1172. resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
  1173. resp[2] != 0) {
  1174. dev_warn(smi_info->io.dev,
  1175. "Invalid return from get global enables command: %ld %x %x %x\n",
  1176. resp_len, resp[0], resp[1], resp[2]);
  1177. rv = -EINVAL;
  1178. goto out;
  1179. } else {
  1180. *enables = resp[3];
  1181. }
  1182. out:
  1183. kfree(resp);
  1184. return rv;
  1185. }
  1186. /*
  1187. * Returns 1 if it gets an error from the command.
  1188. */
  1189. static int set_global_enables(struct smi_info *smi_info, u8 enables)
  1190. {
  1191. unsigned char msg[3];
  1192. unsigned char *resp;
  1193. unsigned long resp_len;
  1194. int rv;
  1195. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1196. if (!resp)
  1197. return -ENOMEM;
  1198. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1199. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  1200. msg[2] = enables;
  1201. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  1202. rv = wait_for_msg_done(smi_info);
  1203. if (rv) {
  1204. dev_warn(smi_info->io.dev,
  1205. "Error getting response from set global enables command: %d\n",
  1206. rv);
  1207. goto out;
  1208. }
  1209. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1210. resp, IPMI_MAX_MSG_LENGTH);
  1211. if (resp_len < 3 ||
  1212. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  1213. resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
  1214. dev_warn(smi_info->io.dev,
  1215. "Invalid return from set global enables command: %ld %x %x\n",
  1216. resp_len, resp[0], resp[1]);
  1217. rv = -EINVAL;
  1218. goto out;
  1219. }
  1220. if (resp[2] != 0)
  1221. rv = 1;
  1222. out:
  1223. kfree(resp);
  1224. return rv;
  1225. }
  1226. /*
  1227. * Some BMCs do not support clearing the receive irq bit in the global
  1228. * enables (even if they don't support interrupts on the BMC). Check
  1229. * for this and handle it properly.
  1230. */
  1231. static void check_clr_rcv_irq(struct smi_info *smi_info)
  1232. {
  1233. u8 enables = 0;
  1234. int rv;
  1235. rv = get_global_enables(smi_info, &enables);
  1236. if (!rv) {
  1237. if ((enables & IPMI_BMC_RCV_MSG_INTR) == 0)
  1238. /* Already clear, should work ok. */
  1239. return;
  1240. enables &= ~IPMI_BMC_RCV_MSG_INTR;
  1241. rv = set_global_enables(smi_info, enables);
  1242. }
  1243. if (rv < 0) {
  1244. dev_err(smi_info->io.dev,
  1245. "Cannot check clearing the rcv irq: %d\n", rv);
  1246. return;
  1247. }
  1248. if (rv) {
  1249. /*
  1250. * An error when setting the event buffer bit means
  1251. * clearing the bit is not supported.
  1252. */
  1253. dev_warn(smi_info->io.dev,
  1254. "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
  1255. smi_info->cannot_disable_irq = true;
  1256. }
  1257. }
  1258. /*
  1259. * Some BMCs do not support setting the interrupt bits in the global
  1260. * enables even if they support interrupts. Clearly bad, but we can
  1261. * compensate.
  1262. */
  1263. static void check_set_rcv_irq(struct smi_info *smi_info)
  1264. {
  1265. u8 enables = 0;
  1266. int rv;
  1267. if (!smi_info->io.irq)
  1268. return;
  1269. rv = get_global_enables(smi_info, &enables);
  1270. if (!rv) {
  1271. enables |= IPMI_BMC_RCV_MSG_INTR;
  1272. rv = set_global_enables(smi_info, enables);
  1273. }
  1274. if (rv < 0) {
  1275. dev_err(smi_info->io.dev,
  1276. "Cannot check setting the rcv irq: %d\n", rv);
  1277. return;
  1278. }
  1279. if (rv) {
  1280. /*
  1281. * An error when setting the event buffer bit means
  1282. * setting the bit is not supported.
  1283. */
  1284. dev_warn(smi_info->io.dev,
  1285. "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
  1286. smi_info->cannot_disable_irq = true;
  1287. smi_info->irq_enable_broken = true;
  1288. }
  1289. }
  1290. static int try_enable_event_buffer(struct smi_info *smi_info)
  1291. {
  1292. unsigned char msg[3];
  1293. unsigned char *resp;
  1294. unsigned long resp_len;
  1295. int rv = 0;
  1296. resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
  1297. if (!resp)
  1298. return -ENOMEM;
  1299. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1300. msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
  1301. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
  1302. rv = wait_for_msg_done(smi_info);
  1303. if (rv) {
  1304. pr_warn("Error getting response from get global enables command, the event buffer is not enabled\n");
  1305. goto out;
  1306. }
  1307. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1308. resp, IPMI_MAX_MSG_LENGTH);
  1309. if (resp_len < 4 ||
  1310. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  1311. resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
  1312. resp[2] != 0) {
  1313. pr_warn("Invalid return from get global enables command, cannot enable the event buffer\n");
  1314. rv = -EINVAL;
  1315. goto out;
  1316. }
  1317. if (resp[3] & IPMI_BMC_EVT_MSG_BUFF) {
  1318. /* buffer is already enabled, nothing to do. */
  1319. smi_info->supports_event_msg_buff = true;
  1320. goto out;
  1321. }
  1322. msg[0] = IPMI_NETFN_APP_REQUEST << 2;
  1323. msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
  1324. msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
  1325. smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
  1326. rv = wait_for_msg_done(smi_info);
  1327. if (rv) {
  1328. pr_warn("Error getting response from set global, enables command, the event buffer is not enabled\n");
  1329. goto out;
  1330. }
  1331. resp_len = smi_info->handlers->get_result(smi_info->si_sm,
  1332. resp, IPMI_MAX_MSG_LENGTH);
  1333. if (resp_len < 3 ||
  1334. resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
  1335. resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
  1336. pr_warn("Invalid return from get global, enables command, not enable the event buffer\n");
  1337. rv = -EINVAL;
  1338. goto out;
  1339. }
  1340. if (resp[2] != 0)
  1341. /*
  1342. * An error when setting the event buffer bit means
  1343. * that the event buffer is not supported.
  1344. */
  1345. rv = -ENOENT;
  1346. else
  1347. smi_info->supports_event_msg_buff = true;
  1348. out:
  1349. kfree(resp);
  1350. return rv;
  1351. }
  1352. #define IPMI_SI_ATTR(name) \
  1353. static ssize_t name##_show(struct device *dev, \
  1354. struct device_attribute *attr, \
  1355. char *buf) \
  1356. { \
  1357. struct smi_info *smi_info = dev_get_drvdata(dev); \
  1358. \
  1359. return sysfs_emit(buf, "%u\n", smi_get_stat(smi_info, name)); \
  1360. } \
  1361. static DEVICE_ATTR_RO(name)
  1362. static ssize_t type_show(struct device *dev,
  1363. struct device_attribute *attr,
  1364. char *buf)
  1365. {
  1366. struct smi_info *smi_info = dev_get_drvdata(dev);
  1367. return sysfs_emit(buf, "%s\n", si_to_str[smi_info->io.si_type]);
  1368. }
  1369. static DEVICE_ATTR_RO(type);
  1370. static ssize_t interrupts_enabled_show(struct device *dev,
  1371. struct device_attribute *attr,
  1372. char *buf)
  1373. {
  1374. struct smi_info *smi_info = dev_get_drvdata(dev);
  1375. int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
  1376. return sysfs_emit(buf, "%d\n", enabled);
  1377. }
  1378. static DEVICE_ATTR_RO(interrupts_enabled);
  1379. IPMI_SI_ATTR(short_timeouts);
  1380. IPMI_SI_ATTR(long_timeouts);
  1381. IPMI_SI_ATTR(idles);
  1382. IPMI_SI_ATTR(interrupts);
  1383. IPMI_SI_ATTR(attentions);
  1384. IPMI_SI_ATTR(flag_fetches);
  1385. IPMI_SI_ATTR(hosed_count);
  1386. IPMI_SI_ATTR(complete_transactions);
  1387. IPMI_SI_ATTR(events);
  1388. IPMI_SI_ATTR(watchdog_pretimeouts);
  1389. IPMI_SI_ATTR(incoming_messages);
  1390. static ssize_t params_show(struct device *dev,
  1391. struct device_attribute *attr,
  1392. char *buf)
  1393. {
  1394. struct smi_info *smi_info = dev_get_drvdata(dev);
  1395. return sysfs_emit(buf,
  1396. "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
  1397. si_to_str[smi_info->io.si_type],
  1398. addr_space_to_str[smi_info->io.addr_space],
  1399. smi_info->io.addr_data,
  1400. smi_info->io.regspacing,
  1401. smi_info->io.regsize,
  1402. smi_info->io.regshift,
  1403. smi_info->io.irq,
  1404. smi_info->io.slave_addr);
  1405. }
  1406. static DEVICE_ATTR_RO(params);
  1407. static struct attribute *ipmi_si_dev_attrs[] = {
  1408. &dev_attr_type.attr,
  1409. &dev_attr_interrupts_enabled.attr,
  1410. &dev_attr_short_timeouts.attr,
  1411. &dev_attr_long_timeouts.attr,
  1412. &dev_attr_idles.attr,
  1413. &dev_attr_interrupts.attr,
  1414. &dev_attr_attentions.attr,
  1415. &dev_attr_flag_fetches.attr,
  1416. &dev_attr_hosed_count.attr,
  1417. &dev_attr_complete_transactions.attr,
  1418. &dev_attr_events.attr,
  1419. &dev_attr_watchdog_pretimeouts.attr,
  1420. &dev_attr_incoming_messages.attr,
  1421. &dev_attr_params.attr,
  1422. NULL
  1423. };
  1424. static const struct attribute_group ipmi_si_dev_attr_group = {
  1425. .attrs = ipmi_si_dev_attrs,
  1426. };
  1427. /*
  1428. * oem_data_avail_to_receive_msg_avail
  1429. * @info - smi_info structure with msg_flags set
  1430. *
  1431. * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
  1432. * Returns 1 indicating need to re-run handle_flags().
  1433. */
  1434. static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
  1435. {
  1436. smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
  1437. RECEIVE_MSG_AVAIL);
  1438. return 1;
  1439. }
  1440. /*
  1441. * setup_dell_poweredge_oem_data_handler
  1442. * @info - smi_info.device_id must be populated
  1443. *
  1444. * Systems that match, but have firmware version < 1.40 may assert
  1445. * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
  1446. * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
  1447. * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
  1448. * as RECEIVE_MSG_AVAIL instead.
  1449. *
  1450. * As Dell has no plans to release IPMI 1.5 firmware that *ever*
  1451. * assert the OEM[012] bits, and if it did, the driver would have to
  1452. * change to handle that properly, we don't actually check for the
  1453. * firmware version.
  1454. * Device ID = 0x20 BMC on PowerEdge 8G servers
  1455. * Device Revision = 0x80
  1456. * Firmware Revision1 = 0x01 BMC version 1.40
  1457. * Firmware Revision2 = 0x40 BCD encoded
  1458. * IPMI Version = 0x51 IPMI 1.5
  1459. * Manufacturer ID = A2 02 00 Dell IANA
  1460. *
  1461. * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
  1462. * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
  1463. *
  1464. */
  1465. #define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
  1466. #define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
  1467. #define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
  1468. #define DELL_IANA_MFR_ID 0x0002a2
  1469. static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
  1470. {
  1471. struct ipmi_device_id *id = &smi_info->device_id;
  1472. if (id->manufacturer_id == DELL_IANA_MFR_ID) {
  1473. if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
  1474. id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
  1475. id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
  1476. smi_info->oem_data_avail_handler =
  1477. oem_data_avail_to_receive_msg_avail;
  1478. } else if (ipmi_version_major(id) < 1 ||
  1479. (ipmi_version_major(id) == 1 &&
  1480. ipmi_version_minor(id) < 5)) {
  1481. smi_info->oem_data_avail_handler =
  1482. oem_data_avail_to_receive_msg_avail;
  1483. }
  1484. }
  1485. }
  1486. #define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
  1487. static void return_hosed_msg_badsize(struct smi_info *smi_info)
  1488. {
  1489. struct ipmi_smi_msg *msg = smi_info->curr_msg;
  1490. /* Make it a response */
  1491. msg->rsp[0] = msg->data[0] | 4;
  1492. msg->rsp[1] = msg->data[1];
  1493. msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
  1494. msg->rsp_size = 3;
  1495. smi_info->curr_msg = NULL;
  1496. deliver_recv_msg(smi_info, msg);
  1497. }
  1498. /*
  1499. * dell_poweredge_bt_xaction_handler
  1500. * @info - smi_info.device_id must be populated
  1501. *
  1502. * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
  1503. * not respond to a Get SDR command if the length of the data
  1504. * requested is exactly 0x3A, which leads to command timeouts and no
  1505. * data returned. This intercepts such commands, and causes userspace
  1506. * callers to try again with a different-sized buffer, which succeeds.
  1507. */
  1508. #define STORAGE_NETFN 0x0A
  1509. #define STORAGE_CMD_GET_SDR 0x23
  1510. static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
  1511. unsigned long unused,
  1512. void *in)
  1513. {
  1514. struct smi_info *smi_info = in;
  1515. unsigned char *data = smi_info->curr_msg->data;
  1516. unsigned int size = smi_info->curr_msg->data_size;
  1517. if (size >= 8 &&
  1518. (data[0]>>2) == STORAGE_NETFN &&
  1519. data[1] == STORAGE_CMD_GET_SDR &&
  1520. data[7] == 0x3A) {
  1521. return_hosed_msg_badsize(smi_info);
  1522. return NOTIFY_STOP;
  1523. }
  1524. return NOTIFY_DONE;
  1525. }
  1526. static struct notifier_block dell_poweredge_bt_xaction_notifier = {
  1527. .notifier_call = dell_poweredge_bt_xaction_handler,
  1528. };
  1529. /*
  1530. * setup_dell_poweredge_bt_xaction_handler
  1531. * @info - smi_info.device_id must be filled in already
  1532. *
  1533. * Fills in smi_info.device_id.start_transaction_pre_hook
  1534. * when we know what function to use there.
  1535. */
  1536. static void
  1537. setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
  1538. {
  1539. struct ipmi_device_id *id = &smi_info->device_id;
  1540. if (id->manufacturer_id == DELL_IANA_MFR_ID &&
  1541. smi_info->io.si_type == SI_BT)
  1542. register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
  1543. }
  1544. /*
  1545. * setup_oem_data_handler
  1546. * @info - smi_info.device_id must be filled in already
  1547. *
  1548. * Fills in smi_info.device_id.oem_data_available_handler
  1549. * when we know what function to use there.
  1550. */
  1551. static void setup_oem_data_handler(struct smi_info *smi_info)
  1552. {
  1553. setup_dell_poweredge_oem_data_handler(smi_info);
  1554. }
  1555. static void setup_xaction_handlers(struct smi_info *smi_info)
  1556. {
  1557. setup_dell_poweredge_bt_xaction_handler(smi_info);
  1558. }
  1559. static void check_for_broken_irqs(struct smi_info *smi_info)
  1560. {
  1561. check_clr_rcv_irq(smi_info);
  1562. check_set_rcv_irq(smi_info);
  1563. }
  1564. static inline void stop_timer_and_thread(struct smi_info *smi_info)
  1565. {
  1566. if (smi_info->thread != NULL) {
  1567. kthread_stop(smi_info->thread);
  1568. smi_info->thread = NULL;
  1569. }
  1570. smi_info->timer_can_start = false;
  1571. del_timer_sync(&smi_info->si_timer);
  1572. }
  1573. static struct smi_info *find_dup_si(struct smi_info *info)
  1574. {
  1575. struct smi_info *e;
  1576. list_for_each_entry(e, &smi_infos, link) {
  1577. if (e->io.addr_space != info->io.addr_space)
  1578. continue;
  1579. if (e->io.addr_data == info->io.addr_data) {
  1580. /*
  1581. * This is a cheap hack, ACPI doesn't have a defined
  1582. * slave address but SMBIOS does. Pick it up from
  1583. * any source that has it available.
  1584. */
  1585. if (info->io.slave_addr && !e->io.slave_addr)
  1586. e->io.slave_addr = info->io.slave_addr;
  1587. return e;
  1588. }
  1589. }
  1590. return NULL;
  1591. }
  1592. int ipmi_si_add_smi(struct si_sm_io *io)
  1593. {
  1594. int rv = 0;
  1595. struct smi_info *new_smi, *dup;
  1596. /*
  1597. * If the user gave us a hard-coded device at the same
  1598. * address, they presumably want us to use it and not what is
  1599. * in the firmware.
  1600. */
  1601. if (io->addr_source != SI_HARDCODED && io->addr_source != SI_HOTMOD &&
  1602. ipmi_si_hardcode_match(io->addr_space, io->addr_data)) {
  1603. dev_info(io->dev,
  1604. "Hard-coded device at this address already exists");
  1605. return -ENODEV;
  1606. }
  1607. if (!io->io_setup) {
  1608. if (io->addr_space == IPMI_IO_ADDR_SPACE) {
  1609. io->io_setup = ipmi_si_port_setup;
  1610. } else if (io->addr_space == IPMI_MEM_ADDR_SPACE) {
  1611. io->io_setup = ipmi_si_mem_setup;
  1612. } else {
  1613. return -EINVAL;
  1614. }
  1615. }
  1616. new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
  1617. if (!new_smi)
  1618. return -ENOMEM;
  1619. spin_lock_init(&new_smi->si_lock);
  1620. new_smi->io = *io;
  1621. mutex_lock(&smi_infos_lock);
  1622. dup = find_dup_si(new_smi);
  1623. if (dup) {
  1624. if (new_smi->io.addr_source == SI_ACPI &&
  1625. dup->io.addr_source == SI_SMBIOS) {
  1626. /* We prefer ACPI over SMBIOS. */
  1627. dev_info(dup->io.dev,
  1628. "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
  1629. si_to_str[new_smi->io.si_type]);
  1630. cleanup_one_si(dup);
  1631. } else {
  1632. dev_info(new_smi->io.dev,
  1633. "%s-specified %s state machine: duplicate\n",
  1634. ipmi_addr_src_to_str(new_smi->io.addr_source),
  1635. si_to_str[new_smi->io.si_type]);
  1636. rv = -EBUSY;
  1637. kfree(new_smi);
  1638. goto out_err;
  1639. }
  1640. }
  1641. pr_info("Adding %s-specified %s state machine\n",
  1642. ipmi_addr_src_to_str(new_smi->io.addr_source),
  1643. si_to_str[new_smi->io.si_type]);
  1644. list_add_tail(&new_smi->link, &smi_infos);
  1645. if (initialized)
  1646. rv = try_smi_init(new_smi);
  1647. out_err:
  1648. mutex_unlock(&smi_infos_lock);
  1649. return rv;
  1650. }
  1651. /*
  1652. * Try to start up an interface. Must be called with smi_infos_lock
  1653. * held, primarily to keep smi_num consistent, we only one to do these
  1654. * one at a time.
  1655. */
  1656. static int try_smi_init(struct smi_info *new_smi)
  1657. {
  1658. int rv = 0;
  1659. int i;
  1660. pr_info("Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
  1661. ipmi_addr_src_to_str(new_smi->io.addr_source),
  1662. si_to_str[new_smi->io.si_type],
  1663. addr_space_to_str[new_smi->io.addr_space],
  1664. new_smi->io.addr_data,
  1665. new_smi->io.slave_addr, new_smi->io.irq);
  1666. switch (new_smi->io.si_type) {
  1667. case SI_KCS:
  1668. new_smi->handlers = &kcs_smi_handlers;
  1669. break;
  1670. case SI_SMIC:
  1671. new_smi->handlers = &smic_smi_handlers;
  1672. break;
  1673. case SI_BT:
  1674. new_smi->handlers = &bt_smi_handlers;
  1675. break;
  1676. default:
  1677. /* No support for anything else yet. */
  1678. rv = -EIO;
  1679. goto out_err;
  1680. }
  1681. new_smi->si_num = smi_num;
  1682. /* Do this early so it's available for logs. */
  1683. if (!new_smi->io.dev) {
  1684. pr_err("IPMI interface added with no device\n");
  1685. rv = -EIO;
  1686. goto out_err;
  1687. }
  1688. /* Allocate the state machine's data and initialize it. */
  1689. new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
  1690. if (!new_smi->si_sm) {
  1691. rv = -ENOMEM;
  1692. goto out_err;
  1693. }
  1694. new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
  1695. &new_smi->io);
  1696. /* Now that we know the I/O size, we can set up the I/O. */
  1697. rv = new_smi->io.io_setup(&new_smi->io);
  1698. if (rv) {
  1699. dev_err(new_smi->io.dev, "Could not set up I/O space\n");
  1700. goto out_err;
  1701. }
  1702. /* Do low-level detection first. */
  1703. if (new_smi->handlers->detect(new_smi->si_sm)) {
  1704. if (new_smi->io.addr_source)
  1705. dev_err(new_smi->io.dev,
  1706. "Interface detection failed\n");
  1707. rv = -ENODEV;
  1708. goto out_err;
  1709. }
  1710. /*
  1711. * Attempt a get device id command. If it fails, we probably
  1712. * don't have a BMC here.
  1713. */
  1714. rv = try_get_dev_id(new_smi);
  1715. if (rv) {
  1716. if (new_smi->io.addr_source)
  1717. dev_err(new_smi->io.dev,
  1718. "There appears to be no BMC at this location\n");
  1719. goto out_err;
  1720. }
  1721. setup_oem_data_handler(new_smi);
  1722. setup_xaction_handlers(new_smi);
  1723. check_for_broken_irqs(new_smi);
  1724. new_smi->waiting_msg = NULL;
  1725. new_smi->curr_msg = NULL;
  1726. atomic_set(&new_smi->req_events, 0);
  1727. new_smi->run_to_completion = false;
  1728. for (i = 0; i < SI_NUM_STATS; i++)
  1729. atomic_set(&new_smi->stats[i], 0);
  1730. new_smi->interrupt_disabled = true;
  1731. atomic_set(&new_smi->need_watch, 0);
  1732. rv = try_enable_event_buffer(new_smi);
  1733. if (rv == 0)
  1734. new_smi->has_event_buffer = true;
  1735. /*
  1736. * Start clearing the flags before we enable interrupts or the
  1737. * timer to avoid racing with the timer.
  1738. */
  1739. start_clear_flags(new_smi);
  1740. /*
  1741. * IRQ is defined to be set when non-zero. req_events will
  1742. * cause a global flags check that will enable interrupts.
  1743. */
  1744. if (new_smi->io.irq) {
  1745. new_smi->interrupt_disabled = false;
  1746. atomic_set(&new_smi->req_events, 1);
  1747. }
  1748. dev_set_drvdata(new_smi->io.dev, new_smi);
  1749. rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
  1750. if (rv) {
  1751. dev_err(new_smi->io.dev,
  1752. "Unable to add device attributes: error %d\n",
  1753. rv);
  1754. goto out_err;
  1755. }
  1756. new_smi->dev_group_added = true;
  1757. rv = ipmi_register_smi(&handlers,
  1758. new_smi,
  1759. new_smi->io.dev,
  1760. new_smi->io.slave_addr);
  1761. if (rv) {
  1762. dev_err(new_smi->io.dev,
  1763. "Unable to register device: error %d\n",
  1764. rv);
  1765. goto out_err;
  1766. }
  1767. /* Don't increment till we know we have succeeded. */
  1768. smi_num++;
  1769. dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
  1770. si_to_str[new_smi->io.si_type]);
  1771. WARN_ON(new_smi->io.dev->init_name != NULL);
  1772. out_err:
  1773. if (rv && new_smi->io.io_cleanup) {
  1774. new_smi->io.io_cleanup(&new_smi->io);
  1775. new_smi->io.io_cleanup = NULL;
  1776. }
  1777. if (rv && new_smi->si_sm) {
  1778. kfree(new_smi->si_sm);
  1779. new_smi->si_sm = NULL;
  1780. }
  1781. return rv;
  1782. }
  1783. static int __init init_ipmi_si(void)
  1784. {
  1785. struct smi_info *e;
  1786. enum ipmi_addr_src type = SI_INVALID;
  1787. if (initialized)
  1788. return 0;
  1789. ipmi_hardcode_init();
  1790. pr_info("IPMI System Interface driver\n");
  1791. ipmi_si_platform_init();
  1792. ipmi_si_pci_init();
  1793. ipmi_si_parisc_init();
  1794. /* We prefer devices with interrupts, but in the case of a machine
  1795. with multiple BMCs we assume that there will be several instances
  1796. of a given type so if we succeed in registering a type then also
  1797. try to register everything else of the same type */
  1798. mutex_lock(&smi_infos_lock);
  1799. list_for_each_entry(e, &smi_infos, link) {
  1800. /* Try to register a device if it has an IRQ and we either
  1801. haven't successfully registered a device yet or this
  1802. device has the same type as one we successfully registered */
  1803. if (e->io.irq && (!type || e->io.addr_source == type)) {
  1804. if (!try_smi_init(e)) {
  1805. type = e->io.addr_source;
  1806. }
  1807. }
  1808. }
  1809. /* type will only have been set if we successfully registered an si */
  1810. if (type)
  1811. goto skip_fallback_noirq;
  1812. /* Fall back to the preferred device */
  1813. list_for_each_entry(e, &smi_infos, link) {
  1814. if (!e->io.irq && (!type || e->io.addr_source == type)) {
  1815. if (!try_smi_init(e)) {
  1816. type = e->io.addr_source;
  1817. }
  1818. }
  1819. }
  1820. skip_fallback_noirq:
  1821. initialized = true;
  1822. mutex_unlock(&smi_infos_lock);
  1823. if (type)
  1824. return 0;
  1825. mutex_lock(&smi_infos_lock);
  1826. if (unload_when_empty && list_empty(&smi_infos)) {
  1827. mutex_unlock(&smi_infos_lock);
  1828. cleanup_ipmi_si();
  1829. pr_warn("Unable to find any System Interface(s)\n");
  1830. return -ENODEV;
  1831. } else {
  1832. mutex_unlock(&smi_infos_lock);
  1833. return 0;
  1834. }
  1835. }
  1836. module_init(init_ipmi_si);
  1837. static void wait_msg_processed(struct smi_info *smi_info)
  1838. {
  1839. unsigned long jiffies_now;
  1840. long time_diff;
  1841. while (smi_info->curr_msg || (smi_info->si_state != SI_NORMAL)) {
  1842. jiffies_now = jiffies;
  1843. time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
  1844. * SI_USEC_PER_JIFFY);
  1845. smi_event_handler(smi_info, time_diff);
  1846. schedule_timeout_uninterruptible(1);
  1847. }
  1848. }
  1849. static void shutdown_smi(void *send_info)
  1850. {
  1851. struct smi_info *smi_info = send_info;
  1852. if (smi_info->dev_group_added) {
  1853. device_remove_group(smi_info->io.dev, &ipmi_si_dev_attr_group);
  1854. smi_info->dev_group_added = false;
  1855. }
  1856. if (smi_info->io.dev)
  1857. dev_set_drvdata(smi_info->io.dev, NULL);
  1858. /*
  1859. * Make sure that interrupts, the timer and the thread are
  1860. * stopped and will not run again.
  1861. */
  1862. smi_info->interrupt_disabled = true;
  1863. if (smi_info->io.irq_cleanup) {
  1864. smi_info->io.irq_cleanup(&smi_info->io);
  1865. smi_info->io.irq_cleanup = NULL;
  1866. }
  1867. stop_timer_and_thread(smi_info);
  1868. /*
  1869. * Wait until we know that we are out of any interrupt
  1870. * handlers might have been running before we freed the
  1871. * interrupt.
  1872. */
  1873. synchronize_rcu();
  1874. /*
  1875. * Timeouts are stopped, now make sure the interrupts are off
  1876. * in the BMC. Note that timers and CPU interrupts are off,
  1877. * so no need for locks.
  1878. */
  1879. wait_msg_processed(smi_info);
  1880. if (smi_info->handlers)
  1881. disable_si_irq(smi_info);
  1882. wait_msg_processed(smi_info);
  1883. if (smi_info->handlers)
  1884. smi_info->handlers->cleanup(smi_info->si_sm);
  1885. if (smi_info->io.io_cleanup) {
  1886. smi_info->io.io_cleanup(&smi_info->io);
  1887. smi_info->io.io_cleanup = NULL;
  1888. }
  1889. kfree(smi_info->si_sm);
  1890. smi_info->si_sm = NULL;
  1891. smi_info->intf = NULL;
  1892. }
  1893. /*
  1894. * Must be called with smi_infos_lock held, to serialize the
  1895. * smi_info->intf check.
  1896. */
  1897. static void cleanup_one_si(struct smi_info *smi_info)
  1898. {
  1899. if (!smi_info)
  1900. return;
  1901. list_del(&smi_info->link);
  1902. ipmi_unregister_smi(smi_info->intf);
  1903. kfree(smi_info);
  1904. }
  1905. void ipmi_si_remove_by_dev(struct device *dev)
  1906. {
  1907. struct smi_info *e;
  1908. mutex_lock(&smi_infos_lock);
  1909. list_for_each_entry(e, &smi_infos, link) {
  1910. if (e->io.dev == dev) {
  1911. cleanup_one_si(e);
  1912. break;
  1913. }
  1914. }
  1915. mutex_unlock(&smi_infos_lock);
  1916. }
  1917. struct device *ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
  1918. unsigned long addr)
  1919. {
  1920. /* remove */
  1921. struct smi_info *e, *tmp_e;
  1922. struct device *dev = NULL;
  1923. mutex_lock(&smi_infos_lock);
  1924. list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
  1925. if (e->io.addr_space != addr_space)
  1926. continue;
  1927. if (e->io.si_type != si_type)
  1928. continue;
  1929. if (e->io.addr_data == addr) {
  1930. dev = get_device(e->io.dev);
  1931. cleanup_one_si(e);
  1932. }
  1933. }
  1934. mutex_unlock(&smi_infos_lock);
  1935. return dev;
  1936. }
  1937. static void cleanup_ipmi_si(void)
  1938. {
  1939. struct smi_info *e, *tmp_e;
  1940. if (!initialized)
  1941. return;
  1942. ipmi_si_pci_shutdown();
  1943. ipmi_si_parisc_shutdown();
  1944. ipmi_si_platform_shutdown();
  1945. mutex_lock(&smi_infos_lock);
  1946. list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
  1947. cleanup_one_si(e);
  1948. mutex_unlock(&smi_infos_lock);
  1949. ipmi_si_hardcode_exit();
  1950. ipmi_si_hotmod_exit();
  1951. }
  1952. module_exit(cleanup_ipmi_si);
  1953. MODULE_ALIAS("platform:dmi-ipmi-si");
  1954. MODULE_LICENSE("GPL");
  1955. MODULE_AUTHOR("Corey Minyard <[email protected]>");
  1956. MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT system interfaces.");