ipmi_si_intf.c 60 KB

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