PageRenderTime 84ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/char/ipmi/ipmi_si_intf.c

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