acpi_ipmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * acpi_ipmi.c - ACPI IPMI opregion
  3. *
  4. * Copyright (C) 2010 Intel Corporation
  5. * Copyright (C) 2010 Zhao Yakui <yakui.zhao@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/delay.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/list.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/io.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. #include <linux/ipmi.h>
  39. #include <linux/device.h>
  40. #include <linux/pnp.h>
  41. #include <linux/spinlock.h>
  42. MODULE_AUTHOR("Zhao Yakui");
  43. MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
  44. MODULE_LICENSE("GPL");
  45. #define IPMI_FLAGS_HANDLER_INSTALL 0
  46. #define ACPI_IPMI_OK 0
  47. #define ACPI_IPMI_TIMEOUT 0x10
  48. #define ACPI_IPMI_UNKNOWN 0x07
  49. /* the IPMI timeout is 5s */
  50. #define IPMI_TIMEOUT (5 * HZ)
  51. #define ACPI_IPMI_MAX_MSG_LENGTH 64
  52. struct acpi_ipmi_device {
  53. /* the device list attached to driver_data.ipmi_devices */
  54. struct list_head head;
  55. /* the IPMI request message list */
  56. struct list_head tx_msg_list;
  57. spinlock_t tx_msg_lock;
  58. acpi_handle handle;
  59. struct pnp_dev *pnp_dev;
  60. ipmi_user_t user_interface;
  61. int ipmi_ifnum; /* IPMI interface number */
  62. long curr_msgid;
  63. unsigned long flags;
  64. struct ipmi_smi_info smi_data;
  65. };
  66. struct ipmi_driver_data {
  67. struct list_head ipmi_devices;
  68. struct ipmi_smi_watcher bmc_events;
  69. struct ipmi_user_hndl ipmi_hndlrs;
  70. struct mutex ipmi_lock;
  71. };
  72. struct acpi_ipmi_msg {
  73. struct list_head head;
  74. /*
  75. * General speaking the addr type should be SI_ADDR_TYPE. And
  76. * the addr channel should be BMC.
  77. * In fact it can also be IPMB type. But we will have to
  78. * parse it from the Netfn command buffer. It is so complex
  79. * that it is skipped.
  80. */
  81. struct ipmi_addr addr;
  82. long tx_msgid;
  83. /* it is used to track whether the IPMI message is finished */
  84. struct completion tx_complete;
  85. struct kernel_ipmi_msg tx_message;
  86. int msg_done;
  87. /* tx/rx data . And copy it from/to ACPI object buffer */
  88. u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
  89. u8 rx_len;
  90. struct acpi_ipmi_device *device;
  91. };
  92. /* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
  93. struct acpi_ipmi_buffer {
  94. u8 status;
  95. u8 length;
  96. u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
  97. };
  98. static void ipmi_register_bmc(int iface, struct device *dev);
  99. static void ipmi_bmc_gone(int iface);
  100. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
  101. static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device);
  102. static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device);
  103. static struct ipmi_driver_data driver_data = {
  104. .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
  105. .bmc_events = {
  106. .owner = THIS_MODULE,
  107. .new_smi = ipmi_register_bmc,
  108. .smi_gone = ipmi_bmc_gone,
  109. },
  110. .ipmi_hndlrs = {
  111. .ipmi_recv_hndl = ipmi_msg_handler,
  112. },
  113. };
  114. static struct acpi_ipmi_msg *acpi_alloc_ipmi_msg(struct acpi_ipmi_device *ipmi)
  115. {
  116. struct acpi_ipmi_msg *ipmi_msg;
  117. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  118. ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
  119. if (!ipmi_msg) {
  120. dev_warn(&pnp_dev->dev, "Can't allocate memory for ipmi_msg\n");
  121. return NULL;
  122. }
  123. init_completion(&ipmi_msg->tx_complete);
  124. INIT_LIST_HEAD(&ipmi_msg->head);
  125. ipmi_msg->device = ipmi;
  126. return ipmi_msg;
  127. }
  128. #define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
  129. #define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
  130. static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
  131. acpi_physical_address address,
  132. acpi_integer *value)
  133. {
  134. struct kernel_ipmi_msg *msg;
  135. struct acpi_ipmi_buffer *buffer;
  136. struct acpi_ipmi_device *device;
  137. unsigned long flags;
  138. msg = &tx_msg->tx_message;
  139. /*
  140. * IPMI network function and command are encoded in the address
  141. * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
  142. */
  143. msg->netfn = IPMI_OP_RGN_NETFN(address);
  144. msg->cmd = IPMI_OP_RGN_CMD(address);
  145. msg->data = tx_msg->data;
  146. /*
  147. * value is the parameter passed by the IPMI opregion space handler.
  148. * It points to the IPMI request message buffer
  149. */
  150. buffer = (struct acpi_ipmi_buffer *)value;
  151. /* copy the tx message data */
  152. if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
  153. dev_WARN_ONCE(&tx_msg->device->pnp_dev->dev, true,
  154. "Unexpected request (msg len %d).\n",
  155. buffer->length);
  156. return -EINVAL;
  157. }
  158. msg->data_len = buffer->length;
  159. memcpy(tx_msg->data, buffer->data, msg->data_len);
  160. /*
  161. * now the default type is SYSTEM_INTERFACE and channel type is BMC.
  162. * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
  163. * the addr type should be changed to IPMB. Then we will have to parse
  164. * the IPMI request message buffer to get the IPMB address.
  165. * If so, please fix me.
  166. */
  167. tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  168. tx_msg->addr.channel = IPMI_BMC_CHANNEL;
  169. tx_msg->addr.data[0] = 0;
  170. /* Get the msgid */
  171. device = tx_msg->device;
  172. spin_lock_irqsave(&device->tx_msg_lock, flags);
  173. device->curr_msgid++;
  174. tx_msg->tx_msgid = device->curr_msgid;
  175. spin_unlock_irqrestore(&device->tx_msg_lock, flags);
  176. return 0;
  177. }
  178. static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
  179. acpi_integer *value, int rem_time)
  180. {
  181. struct acpi_ipmi_buffer *buffer;
  182. /*
  183. * value is also used as output parameter. It represents the response
  184. * IPMI message returned by IPMI command.
  185. */
  186. buffer = (struct acpi_ipmi_buffer *)value;
  187. if (!rem_time && !msg->msg_done) {
  188. buffer->status = ACPI_IPMI_TIMEOUT;
  189. return;
  190. }
  191. /*
  192. * If the flag of msg_done is not set or the recv length is zero, it
  193. * means that the IPMI command is not executed correctly.
  194. * The status code will be ACPI_IPMI_UNKNOWN.
  195. */
  196. if (!msg->msg_done || !msg->rx_len) {
  197. buffer->status = ACPI_IPMI_UNKNOWN;
  198. return;
  199. }
  200. /*
  201. * If the IPMI response message is obtained correctly, the status code
  202. * will be ACPI_IPMI_OK
  203. */
  204. buffer->status = ACPI_IPMI_OK;
  205. buffer->length = msg->rx_len;
  206. memcpy(buffer->data, msg->data, msg->rx_len);
  207. }
  208. static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
  209. {
  210. struct acpi_ipmi_msg *tx_msg, *temp;
  211. int count = HZ / 10;
  212. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  213. list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
  214. /* wake up the sleep thread on the Tx msg */
  215. complete(&tx_msg->tx_complete);
  216. }
  217. /* wait for about 100ms to flush the tx message list */
  218. while (count--) {
  219. if (list_empty(&ipmi->tx_msg_list))
  220. break;
  221. schedule_timeout(1);
  222. }
  223. if (!list_empty(&ipmi->tx_msg_list))
  224. dev_warn(&pnp_dev->dev, "tx msg list is not NULL\n");
  225. }
  226. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  227. {
  228. struct acpi_ipmi_device *ipmi_device = user_msg_data;
  229. int msg_found = 0;
  230. struct acpi_ipmi_msg *tx_msg;
  231. struct pnp_dev *pnp_dev = ipmi_device->pnp_dev;
  232. unsigned long flags;
  233. if (msg->user != ipmi_device->user_interface) {
  234. dev_warn(&pnp_dev->dev, "Unexpected response is returned. "
  235. "returned user %p, expected user %p\n",
  236. msg->user, ipmi_device->user_interface);
  237. goto out_msg;
  238. }
  239. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  240. list_for_each_entry(tx_msg, &ipmi_device->tx_msg_list, head) {
  241. if (msg->msgid == tx_msg->tx_msgid) {
  242. msg_found = 1;
  243. break;
  244. }
  245. }
  246. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  247. if (!msg_found) {
  248. dev_warn(&pnp_dev->dev, "Unexpected response (msg id %ld) is "
  249. "returned.\n", msg->msgid);
  250. goto out_msg;
  251. }
  252. /* copy the response data to Rx_data buffer */
  253. if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
  254. dev_WARN_ONCE(&pnp_dev->dev, true,
  255. "Unexpected response (msg len %d).\n",
  256. msg->msg.data_len);
  257. } else {
  258. tx_msg->rx_len = msg->msg.data_len;
  259. memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
  260. tx_msg->msg_done = 1;
  261. }
  262. complete(&tx_msg->tx_complete);
  263. out_msg:
  264. ipmi_free_recv_msg(msg);
  265. };
  266. static void ipmi_register_bmc(int iface, struct device *dev)
  267. {
  268. struct acpi_ipmi_device *ipmi_device, *temp;
  269. struct pnp_dev *pnp_dev;
  270. ipmi_user_t user;
  271. int err;
  272. struct ipmi_smi_info smi_data;
  273. acpi_handle handle;
  274. err = ipmi_get_smi_info(iface, &smi_data);
  275. if (err)
  276. return;
  277. if (smi_data.addr_src != SI_ACPI) {
  278. put_device(smi_data.dev);
  279. return;
  280. }
  281. handle = smi_data.addr_info.acpi_info.acpi_handle;
  282. mutex_lock(&driver_data.ipmi_lock);
  283. list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
  284. /*
  285. * if the corresponding ACPI handle is already added
  286. * to the device list, don't add it again.
  287. */
  288. if (temp->handle == handle)
  289. goto out;
  290. }
  291. ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
  292. if (!ipmi_device)
  293. goto out;
  294. pnp_dev = to_pnp_dev(smi_data.dev);
  295. ipmi_device->handle = handle;
  296. ipmi_device->pnp_dev = pnp_dev;
  297. err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
  298. ipmi_device, &user);
  299. if (err) {
  300. dev_warn(&pnp_dev->dev, "Can't create IPMI user interface\n");
  301. kfree(ipmi_device);
  302. goto out;
  303. }
  304. acpi_add_ipmi_device(ipmi_device);
  305. ipmi_device->user_interface = user;
  306. ipmi_device->ipmi_ifnum = iface;
  307. mutex_unlock(&driver_data.ipmi_lock);
  308. memcpy(&ipmi_device->smi_data, &smi_data, sizeof(struct ipmi_smi_info));
  309. return;
  310. out:
  311. mutex_unlock(&driver_data.ipmi_lock);
  312. put_device(smi_data.dev);
  313. return;
  314. }
  315. static void ipmi_bmc_gone(int iface)
  316. {
  317. struct acpi_ipmi_device *ipmi_device, *temp;
  318. mutex_lock(&driver_data.ipmi_lock);
  319. list_for_each_entry_safe(ipmi_device, temp,
  320. &driver_data.ipmi_devices, head) {
  321. if (ipmi_device->ipmi_ifnum != iface)
  322. continue;
  323. acpi_remove_ipmi_device(ipmi_device);
  324. put_device(ipmi_device->smi_data.dev);
  325. kfree(ipmi_device);
  326. break;
  327. }
  328. mutex_unlock(&driver_data.ipmi_lock);
  329. }
  330. /* --------------------------------------------------------------------------
  331. * Address Space Management
  332. * -------------------------------------------------------------------------- */
  333. /*
  334. * This is the IPMI opregion space handler.
  335. * @function: indicates the read/write. In fact as the IPMI message is driven
  336. * by command, only write is meaningful.
  337. * @address: This contains the netfn/command of IPMI request message.
  338. * @bits : not used.
  339. * @value : it is an in/out parameter. It points to the IPMI message buffer.
  340. * Before the IPMI message is sent, it represents the actual request
  341. * IPMI message. After the IPMI message is finished, it represents
  342. * the response IPMI message returned by IPMI command.
  343. * @handler_context: IPMI device context.
  344. */
  345. static acpi_status
  346. acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
  347. u32 bits, acpi_integer *value,
  348. void *handler_context, void *region_context)
  349. {
  350. struct acpi_ipmi_msg *tx_msg;
  351. struct acpi_ipmi_device *ipmi_device = handler_context;
  352. int err, rem_time;
  353. acpi_status status;
  354. unsigned long flags;
  355. /*
  356. * IPMI opregion message.
  357. * IPMI message is firstly written to the BMC and system software
  358. * can get the respsonse. So it is unmeaningful for the read access
  359. * of IPMI opregion.
  360. */
  361. if ((function & ACPI_IO_MASK) == ACPI_READ)
  362. return AE_TYPE;
  363. if (!ipmi_device->user_interface)
  364. return AE_NOT_EXIST;
  365. tx_msg = acpi_alloc_ipmi_msg(ipmi_device);
  366. if (!tx_msg)
  367. return AE_NO_MEMORY;
  368. if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
  369. status = AE_TYPE;
  370. goto out_msg;
  371. }
  372. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  373. list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
  374. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  375. err = ipmi_request_settime(ipmi_device->user_interface,
  376. &tx_msg->addr,
  377. tx_msg->tx_msgid,
  378. &tx_msg->tx_message,
  379. NULL, 0, 0, 0);
  380. if (err) {
  381. status = AE_ERROR;
  382. goto out_list;
  383. }
  384. rem_time = wait_for_completion_timeout(&tx_msg->tx_complete,
  385. IPMI_TIMEOUT);
  386. acpi_format_ipmi_response(tx_msg, value, rem_time);
  387. status = AE_OK;
  388. out_list:
  389. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  390. list_del(&tx_msg->head);
  391. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  392. out_msg:
  393. kfree(tx_msg);
  394. return status;
  395. }
  396. static void ipmi_remove_space_handler(struct acpi_ipmi_device *ipmi)
  397. {
  398. if (!test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
  399. return;
  400. acpi_remove_address_space_handler(ipmi->handle,
  401. ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
  402. clear_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
  403. }
  404. static int ipmi_install_space_handler(struct acpi_ipmi_device *ipmi)
  405. {
  406. acpi_status status;
  407. if (test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
  408. return 0;
  409. status = acpi_install_address_space_handler(ipmi->handle,
  410. ACPI_ADR_SPACE_IPMI,
  411. &acpi_ipmi_space_handler,
  412. NULL, ipmi);
  413. if (ACPI_FAILURE(status)) {
  414. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  415. dev_warn(&pnp_dev->dev, "Can't register IPMI opregion space "
  416. "handle\n");
  417. return -EINVAL;
  418. }
  419. set_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
  420. return 0;
  421. }
  422. static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device)
  423. {
  424. INIT_LIST_HEAD(&ipmi_device->head);
  425. spin_lock_init(&ipmi_device->tx_msg_lock);
  426. INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
  427. ipmi_install_space_handler(ipmi_device);
  428. list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
  429. }
  430. static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device)
  431. {
  432. /*
  433. * If the IPMI user interface is created, it should be
  434. * destroyed.
  435. */
  436. if (ipmi_device->user_interface) {
  437. ipmi_destroy_user(ipmi_device->user_interface);
  438. ipmi_device->user_interface = NULL;
  439. }
  440. /* flush the Tx_msg list */
  441. if (!list_empty(&ipmi_device->tx_msg_list))
  442. ipmi_flush_tx_msg(ipmi_device);
  443. list_del(&ipmi_device->head);
  444. ipmi_remove_space_handler(ipmi_device);
  445. }
  446. static int __init acpi_ipmi_init(void)
  447. {
  448. int result = 0;
  449. if (acpi_disabled)
  450. return result;
  451. mutex_init(&driver_data.ipmi_lock);
  452. result = ipmi_smi_watcher_register(&driver_data.bmc_events);
  453. return result;
  454. }
  455. static void __exit acpi_ipmi_exit(void)
  456. {
  457. struct acpi_ipmi_device *ipmi_device, *temp;
  458. if (acpi_disabled)
  459. return;
  460. ipmi_smi_watcher_unregister(&driver_data.bmc_events);
  461. /*
  462. * When one smi_watcher is unregistered, it is only deleted
  463. * from the smi_watcher list. But the smi_gone callback function
  464. * is not called. So explicitly uninstall the ACPI IPMI oregion
  465. * handler and free it.
  466. */
  467. mutex_lock(&driver_data.ipmi_lock);
  468. list_for_each_entry_safe(ipmi_device, temp,
  469. &driver_data.ipmi_devices, head) {
  470. acpi_remove_ipmi_device(ipmi_device);
  471. put_device(ipmi_device->smi_data.dev);
  472. kfree(ipmi_device);
  473. }
  474. mutex_unlock(&driver_data.ipmi_lock);
  475. }
  476. module_init(acpi_ipmi_init);
  477. module_exit(acpi_ipmi_exit);