ipmi_si_intf.c 59 KB

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