ipmi_poweroff.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * ipmi_poweroff.c
  3. *
  4. * MontaVista IPMI Poweroff extension to sys_reboot
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * Steven Dake <sdake@mvista.com>
  8. * Corey Minyard <cminyard@mvista.com>
  9. * source@mvista.com
  10. *
  11. * Copyright 2002,2004 MontaVista Software Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  27. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  28. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You should have received a copy of the GNU General Public License along
  31. * with this program; if not, write to the Free Software Foundation, Inc.,
  32. * 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/string.h>
  38. #include <linux/completion.h>
  39. #include <linux/pm.h>
  40. #include <linux/kdev_t.h>
  41. #include <linux/ipmi.h>
  42. #include <linux/ipmi_smi.h>
  43. #define PFX "IPMI poweroff: "
  44. static void ipmi_po_smi_gone(int if_num);
  45. static void ipmi_po_new_smi(int if_num, struct device *device);
  46. /* Definitions for controlling power off (if the system supports it). It
  47. * conveniently matches the IPMI chassis control values. */
  48. #define IPMI_CHASSIS_POWER_DOWN 0 /* power down, the default. */
  49. #define IPMI_CHASSIS_POWER_CYCLE 0x02 /* power cycle */
  50. /* the IPMI data command */
  51. static int poweroff_powercycle;
  52. /* Which interface to use, -1 means the first we see. */
  53. static int ifnum_to_use = -1;
  54. /* Our local state. */
  55. static int ready;
  56. static ipmi_user_t ipmi_user;
  57. static int ipmi_ifnum;
  58. static void (*specific_poweroff_func)(ipmi_user_t user);
  59. /* Holds the old poweroff function so we can restore it on removal. */
  60. static void (*old_poweroff_func)(void);
  61. static int set_param_ifnum(const char *val, struct kernel_param *kp)
  62. {
  63. int rv = param_set_int(val, kp);
  64. if (rv)
  65. return rv;
  66. if ((ifnum_to_use < 0) || (ifnum_to_use == ipmi_ifnum))
  67. return 0;
  68. ipmi_po_smi_gone(ipmi_ifnum);
  69. ipmi_po_new_smi(ifnum_to_use, NULL);
  70. return 0;
  71. }
  72. module_param_call(ifnum_to_use, set_param_ifnum, param_get_int,
  73. &ifnum_to_use, 0644);
  74. MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
  75. "timer. Setting to -1 defaults to the first registered "
  76. "interface");
  77. /* parameter definition to allow user to flag power cycle */
  78. module_param(poweroff_powercycle, int, 0644);
  79. MODULE_PARM_DESC(poweroff_powercycle,
  80. " Set to non-zero to enable power cycle instead of power"
  81. " down. Power cycle is contingent on hardware support,"
  82. " otherwise it defaults back to power down.");
  83. /* Stuff from the get device id command. */
  84. static unsigned int mfg_id;
  85. static unsigned int prod_id;
  86. static unsigned char capabilities;
  87. static unsigned char ipmi_version;
  88. /*
  89. * We use our own messages for this operation, we don't let the system
  90. * allocate them, since we may be in a panic situation. The whole
  91. * thing is single-threaded, anyway, so multiple messages are not
  92. * required.
  93. */
  94. static atomic_t dummy_count = ATOMIC_INIT(0);
  95. static void dummy_smi_free(struct ipmi_smi_msg *msg)
  96. {
  97. atomic_dec(&dummy_count);
  98. }
  99. static void dummy_recv_free(struct ipmi_recv_msg *msg)
  100. {
  101. atomic_dec(&dummy_count);
  102. }
  103. static struct ipmi_smi_msg halt_smi_msg = {
  104. .done = dummy_smi_free
  105. };
  106. static struct ipmi_recv_msg halt_recv_msg = {
  107. .done = dummy_recv_free
  108. };
  109. /*
  110. * Code to send a message and wait for the response.
  111. */
  112. static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
  113. {
  114. struct completion *comp = recv_msg->user_msg_data;
  115. if (comp)
  116. complete(comp);
  117. }
  118. static struct ipmi_user_hndl ipmi_poweroff_handler = {
  119. .ipmi_recv_hndl = receive_handler
  120. };
  121. static int ipmi_request_wait_for_response(ipmi_user_t user,
  122. struct ipmi_addr *addr,
  123. struct kernel_ipmi_msg *send_msg)
  124. {
  125. int rv;
  126. struct completion comp;
  127. init_completion(&comp);
  128. rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp,
  129. &halt_smi_msg, &halt_recv_msg, 0);
  130. if (rv)
  131. return rv;
  132. wait_for_completion(&comp);
  133. return halt_recv_msg.msg.data[0];
  134. }
  135. /* Wait for message to complete, spinning. */
  136. static int ipmi_request_in_rc_mode(ipmi_user_t user,
  137. struct ipmi_addr *addr,
  138. struct kernel_ipmi_msg *send_msg)
  139. {
  140. int rv;
  141. atomic_set(&dummy_count, 2);
  142. rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL,
  143. &halt_smi_msg, &halt_recv_msg, 0);
  144. if (rv) {
  145. atomic_set(&dummy_count, 0);
  146. return rv;
  147. }
  148. /*
  149. * Spin until our message is done.
  150. */
  151. while (atomic_read(&dummy_count) > 0) {
  152. ipmi_poll_interface(user);
  153. cpu_relax();
  154. }
  155. return halt_recv_msg.msg.data[0];
  156. }
  157. /*
  158. * ATCA Support
  159. */
  160. #define IPMI_NETFN_ATCA 0x2c
  161. #define IPMI_ATCA_SET_POWER_CMD 0x11
  162. #define IPMI_ATCA_GET_ADDR_INFO_CMD 0x01
  163. #define IPMI_PICMG_ID 0
  164. #define IPMI_NETFN_OEM 0x2e
  165. #define IPMI_ATCA_PPS_GRACEFUL_RESTART 0x11
  166. #define IPMI_ATCA_PPS_IANA "\x00\x40\x0A"
  167. #define IPMI_MOTOROLA_MANUFACTURER_ID 0x0000A1
  168. #define IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID 0x0051
  169. static void (*atca_oem_poweroff_hook)(ipmi_user_t user);
  170. static void pps_poweroff_atca(ipmi_user_t user)
  171. {
  172. struct ipmi_system_interface_addr smi_addr;
  173. struct kernel_ipmi_msg send_msg;
  174. int rv;
  175. /*
  176. * Configure IPMI address for local access
  177. */
  178. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  179. smi_addr.channel = IPMI_BMC_CHANNEL;
  180. smi_addr.lun = 0;
  181. printk(KERN_INFO PFX "PPS powerdown hook used");
  182. send_msg.netfn = IPMI_NETFN_OEM;
  183. send_msg.cmd = IPMI_ATCA_PPS_GRACEFUL_RESTART;
  184. send_msg.data = IPMI_ATCA_PPS_IANA;
  185. send_msg.data_len = 3;
  186. rv = ipmi_request_in_rc_mode(user,
  187. (struct ipmi_addr *) &smi_addr,
  188. &send_msg);
  189. if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
  190. printk(KERN_ERR PFX "Unable to send ATCA ,"
  191. " IPMI error 0x%x\n", rv);
  192. }
  193. return;
  194. }
  195. static int ipmi_atca_detect(ipmi_user_t user)
  196. {
  197. struct ipmi_system_interface_addr smi_addr;
  198. struct kernel_ipmi_msg send_msg;
  199. int rv;
  200. unsigned char data[1];
  201. /*
  202. * Configure IPMI address for local access
  203. */
  204. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  205. smi_addr.channel = IPMI_BMC_CHANNEL;
  206. smi_addr.lun = 0;
  207. /*
  208. * Use get address info to check and see if we are ATCA
  209. */
  210. send_msg.netfn = IPMI_NETFN_ATCA;
  211. send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
  212. data[0] = IPMI_PICMG_ID;
  213. send_msg.data = data;
  214. send_msg.data_len = sizeof(data);
  215. rv = ipmi_request_wait_for_response(user,
  216. (struct ipmi_addr *) &smi_addr,
  217. &send_msg);
  218. printk(KERN_INFO PFX "ATCA Detect mfg 0x%X prod 0x%X\n",
  219. mfg_id, prod_id);
  220. if ((mfg_id == IPMI_MOTOROLA_MANUFACTURER_ID)
  221. && (prod_id == IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID)) {
  222. printk(KERN_INFO PFX
  223. "Installing Pigeon Point Systems Poweroff Hook\n");
  224. atca_oem_poweroff_hook = pps_poweroff_atca;
  225. }
  226. return !rv;
  227. }
  228. static void ipmi_poweroff_atca(ipmi_user_t user)
  229. {
  230. struct ipmi_system_interface_addr smi_addr;
  231. struct kernel_ipmi_msg send_msg;
  232. int rv;
  233. unsigned char data[4];
  234. /*
  235. * Configure IPMI address for local access
  236. */
  237. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  238. smi_addr.channel = IPMI_BMC_CHANNEL;
  239. smi_addr.lun = 0;
  240. printk(KERN_INFO PFX "Powering down via ATCA power command\n");
  241. /*
  242. * Power down
  243. */
  244. send_msg.netfn = IPMI_NETFN_ATCA;
  245. send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
  246. data[0] = IPMI_PICMG_ID;
  247. data[1] = 0; /* FRU id */
  248. data[2] = 0; /* Power Level */
  249. data[3] = 0; /* Don't change saved presets */
  250. send_msg.data = data;
  251. send_msg.data_len = sizeof(data);
  252. rv = ipmi_request_in_rc_mode(user,
  253. (struct ipmi_addr *) &smi_addr,
  254. &send_msg);
  255. /*
  256. * At this point, the system may be shutting down, and most
  257. * serial drivers (if used) will have interrupts turned off
  258. * it may be better to ignore IPMI_UNKNOWN_ERR_COMPLETION_CODE
  259. * return code
  260. */
  261. if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
  262. printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
  263. " IPMI error 0x%x\n", rv);
  264. goto out;
  265. }
  266. if (atca_oem_poweroff_hook)
  267. atca_oem_poweroff_hook(user);
  268. out:
  269. return;
  270. }
  271. /*
  272. * CPI1 Support
  273. */
  274. #define IPMI_NETFN_OEM_1 0xf8
  275. #define OEM_GRP_CMD_SET_RESET_STATE 0x84
  276. #define OEM_GRP_CMD_SET_POWER_STATE 0x82
  277. #define IPMI_NETFN_OEM_8 0xf8
  278. #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
  279. #define OEM_GRP_CMD_GET_SLOT_GA 0xa3
  280. #define IPMI_NETFN_SENSOR_EVT 0x10
  281. #define IPMI_CMD_GET_EVENT_RECEIVER 0x01
  282. #define IPMI_CPI1_PRODUCT_ID 0x000157
  283. #define IPMI_CPI1_MANUFACTURER_ID 0x0108
  284. static int ipmi_cpi1_detect(ipmi_user_t user)
  285. {
  286. return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
  287. && (prod_id == IPMI_CPI1_PRODUCT_ID));
  288. }
  289. static void ipmi_poweroff_cpi1(ipmi_user_t user)
  290. {
  291. struct ipmi_system_interface_addr smi_addr;
  292. struct ipmi_ipmb_addr ipmb_addr;
  293. struct kernel_ipmi_msg send_msg;
  294. int rv;
  295. unsigned char data[1];
  296. int slot;
  297. unsigned char hotswap_ipmb;
  298. unsigned char aer_addr;
  299. unsigned char aer_lun;
  300. /*
  301. * Configure IPMI address for local access
  302. */
  303. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  304. smi_addr.channel = IPMI_BMC_CHANNEL;
  305. smi_addr.lun = 0;
  306. printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
  307. /*
  308. * Get IPMI ipmb address
  309. */
  310. send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
  311. send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
  312. send_msg.data = NULL;
  313. send_msg.data_len = 0;
  314. rv = ipmi_request_in_rc_mode(user,
  315. (struct ipmi_addr *) &smi_addr,
  316. &send_msg);
  317. if (rv)
  318. goto out;
  319. slot = halt_recv_msg.msg.data[1];
  320. hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
  321. /*
  322. * Get active event receiver
  323. */
  324. send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
  325. send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
  326. send_msg.data = NULL;
  327. send_msg.data_len = 0;
  328. rv = ipmi_request_in_rc_mode(user,
  329. (struct ipmi_addr *) &smi_addr,
  330. &send_msg);
  331. if (rv)
  332. goto out;
  333. aer_addr = halt_recv_msg.msg.data[1];
  334. aer_lun = halt_recv_msg.msg.data[2];
  335. /*
  336. * Setup IPMB address target instead of local target
  337. */
  338. ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
  339. ipmb_addr.channel = 0;
  340. ipmb_addr.slave_addr = aer_addr;
  341. ipmb_addr.lun = aer_lun;
  342. /*
  343. * Send request hotswap control to remove blade from dpv
  344. */
  345. send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
  346. send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
  347. send_msg.data = &hotswap_ipmb;
  348. send_msg.data_len = 1;
  349. ipmi_request_in_rc_mode(user,
  350. (struct ipmi_addr *) &ipmb_addr,
  351. &send_msg);
  352. /*
  353. * Set reset asserted
  354. */
  355. send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
  356. send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
  357. send_msg.data = data;
  358. data[0] = 1; /* Reset asserted state */
  359. send_msg.data_len = 1;
  360. rv = ipmi_request_in_rc_mode(user,
  361. (struct ipmi_addr *) &smi_addr,
  362. &send_msg);
  363. if (rv)
  364. goto out;
  365. /*
  366. * Power down
  367. */
  368. send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
  369. send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
  370. send_msg.data = data;
  371. data[0] = 1; /* Power down state */
  372. send_msg.data_len = 1;
  373. rv = ipmi_request_in_rc_mode(user,
  374. (struct ipmi_addr *) &smi_addr,
  375. &send_msg);
  376. if (rv)
  377. goto out;
  378. out:
  379. return;
  380. }
  381. /*
  382. * ipmi_dell_chassis_detect()
  383. * Dell systems with IPMI < 1.5 don't set the chassis capability bit
  384. * but they can handle a chassis poweroff or powercycle command.
  385. */
  386. #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
  387. static int ipmi_dell_chassis_detect(ipmi_user_t user)
  388. {
  389. const char ipmi_version_major = ipmi_version & 0xF;
  390. const char ipmi_version_minor = (ipmi_version >> 4) & 0xF;
  391. const char mfr[3] = DELL_IANA_MFR_ID;
  392. if (!memcmp(mfr, &mfg_id, sizeof(mfr)) &&
  393. ipmi_version_major <= 1 &&
  394. ipmi_version_minor < 5)
  395. return 1;
  396. return 0;
  397. }
  398. /*
  399. * Standard chassis support
  400. */
  401. #define IPMI_NETFN_CHASSIS_REQUEST 0
  402. #define IPMI_CHASSIS_CONTROL_CMD 0x02
  403. static int ipmi_chassis_detect(ipmi_user_t user)
  404. {
  405. /* Chassis support, use it. */
  406. return (capabilities & 0x80);
  407. }
  408. static void ipmi_poweroff_chassis(ipmi_user_t user)
  409. {
  410. struct ipmi_system_interface_addr smi_addr;
  411. struct kernel_ipmi_msg send_msg;
  412. int rv;
  413. unsigned char data[1];
  414. /*
  415. * Configure IPMI address for local access
  416. */
  417. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  418. smi_addr.channel = IPMI_BMC_CHANNEL;
  419. smi_addr.lun = 0;
  420. powercyclefailed:
  421. printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
  422. (poweroff_powercycle ? "cycle" : "down"));
  423. /*
  424. * Power down
  425. */
  426. send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
  427. send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
  428. if (poweroff_powercycle)
  429. data[0] = IPMI_CHASSIS_POWER_CYCLE;
  430. else
  431. data[0] = IPMI_CHASSIS_POWER_DOWN;
  432. send_msg.data = data;
  433. send_msg.data_len = sizeof(data);
  434. rv = ipmi_request_in_rc_mode(user,
  435. (struct ipmi_addr *) &smi_addr,
  436. &send_msg);
  437. if (rv) {
  438. if (poweroff_powercycle) {
  439. /* power cycle failed, default to power down */
  440. printk(KERN_ERR PFX "Unable to send chassis power " \
  441. "cycle message, IPMI error 0x%x\n", rv);
  442. poweroff_powercycle = 0;
  443. goto powercyclefailed;
  444. }
  445. printk(KERN_ERR PFX "Unable to send chassis power " \
  446. "down message, IPMI error 0x%x\n", rv);
  447. }
  448. }
  449. /* Table of possible power off functions. */
  450. struct poweroff_function {
  451. char *platform_type;
  452. int (*detect)(ipmi_user_t user);
  453. void (*poweroff_func)(ipmi_user_t user);
  454. };
  455. static struct poweroff_function poweroff_functions[] = {
  456. { .platform_type = "ATCA",
  457. .detect = ipmi_atca_detect,
  458. .poweroff_func = ipmi_poweroff_atca },
  459. { .platform_type = "CPI1",
  460. .detect = ipmi_cpi1_detect,
  461. .poweroff_func = ipmi_poweroff_cpi1 },
  462. { .platform_type = "chassis",
  463. .detect = ipmi_dell_chassis_detect,
  464. .poweroff_func = ipmi_poweroff_chassis },
  465. /* Chassis should generally be last, other things should override
  466. it. */
  467. { .platform_type = "chassis",
  468. .detect = ipmi_chassis_detect,
  469. .poweroff_func = ipmi_poweroff_chassis },
  470. };
  471. #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
  472. / sizeof(struct poweroff_function))
  473. /* Called on a powerdown request. */
  474. static void ipmi_poweroff_function(void)
  475. {
  476. if (!ready)
  477. return;
  478. /* Use run-to-completion mode, since interrupts may be off. */
  479. specific_poweroff_func(ipmi_user);
  480. }
  481. /* Wait for an IPMI interface to be installed, the first one installed
  482. will be grabbed by this code and used to perform the powerdown. */
  483. static void ipmi_po_new_smi(int if_num, struct device *device)
  484. {
  485. struct ipmi_system_interface_addr smi_addr;
  486. struct kernel_ipmi_msg send_msg;
  487. int rv;
  488. int i;
  489. if (ready)
  490. return;
  491. if ((ifnum_to_use >= 0) && (ifnum_to_use != if_num))
  492. return;
  493. rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
  494. &ipmi_user);
  495. if (rv) {
  496. printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
  497. rv);
  498. return;
  499. }
  500. ipmi_ifnum = if_num;
  501. /*
  502. * Do a get device ide and store some results, since this is
  503. * used by several functions.
  504. */
  505. smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  506. smi_addr.channel = IPMI_BMC_CHANNEL;
  507. smi_addr.lun = 0;
  508. send_msg.netfn = IPMI_NETFN_APP_REQUEST;
  509. send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
  510. send_msg.data = NULL;
  511. send_msg.data_len = 0;
  512. rv = ipmi_request_wait_for_response(ipmi_user,
  513. (struct ipmi_addr *) &smi_addr,
  514. &send_msg);
  515. if (rv) {
  516. printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
  517. " IPMI error 0x%x\n", rv);
  518. goto out_err;
  519. }
  520. if (halt_recv_msg.msg.data_len < 12) {
  521. printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
  522. " short, was %d bytes, needed %d bytes\n",
  523. halt_recv_msg.msg.data_len, 12);
  524. goto out_err;
  525. }
  526. mfg_id = (halt_recv_msg.msg.data[7]
  527. | (halt_recv_msg.msg.data[8] << 8)
  528. | (halt_recv_msg.msg.data[9] << 16));
  529. prod_id = (halt_recv_msg.msg.data[10]
  530. | (halt_recv_msg.msg.data[11] << 8));
  531. capabilities = halt_recv_msg.msg.data[6];
  532. ipmi_version = halt_recv_msg.msg.data[5];
  533. /* Scan for a poweroff method */
  534. for (i = 0; i < NUM_PO_FUNCS; i++) {
  535. if (poweroff_functions[i].detect(ipmi_user))
  536. goto found;
  537. }
  538. out_err:
  539. printk(KERN_ERR PFX "Unable to find a poweroff function that"
  540. " will work, giving up\n");
  541. ipmi_destroy_user(ipmi_user);
  542. return;
  543. found:
  544. printk(KERN_INFO PFX "Found a %s style poweroff function\n",
  545. poweroff_functions[i].platform_type);
  546. specific_poweroff_func = poweroff_functions[i].poweroff_func;
  547. old_poweroff_func = pm_power_off;
  548. pm_power_off = ipmi_poweroff_function;
  549. ready = 1;
  550. }
  551. static void ipmi_po_smi_gone(int if_num)
  552. {
  553. if (!ready)
  554. return;
  555. if (ipmi_ifnum != if_num)
  556. return;
  557. ready = 0;
  558. ipmi_destroy_user(ipmi_user);
  559. pm_power_off = old_poweroff_func;
  560. }
  561. static struct ipmi_smi_watcher smi_watcher = {
  562. .owner = THIS_MODULE,
  563. .new_smi = ipmi_po_new_smi,
  564. .smi_gone = ipmi_po_smi_gone
  565. };
  566. #ifdef CONFIG_PROC_FS
  567. #include <linux/sysctl.h>
  568. static struct ctl_table ipmi_table[] = {
  569. { .procname = "poweroff_powercycle",
  570. .data = &poweroff_powercycle,
  571. .maxlen = sizeof(poweroff_powercycle),
  572. .mode = 0644,
  573. .proc_handler = proc_dointvec },
  574. { }
  575. };
  576. static struct ctl_table ipmi_dir_table[] = {
  577. { .procname = "ipmi",
  578. .mode = 0555,
  579. .child = ipmi_table },
  580. { }
  581. };
  582. static struct ctl_table ipmi_root_table[] = {
  583. { .procname = "dev",
  584. .mode = 0555,
  585. .child = ipmi_dir_table },
  586. { }
  587. };
  588. static struct ctl_table_header *ipmi_table_header;
  589. #endif /* CONFIG_PROC_FS */
  590. /*
  591. * Startup and shutdown functions.
  592. */
  593. static int __init ipmi_poweroff_init(void)
  594. {
  595. int rv;
  596. printk(KERN_INFO "Copyright (C) 2004 MontaVista Software -"
  597. " IPMI Powerdown via sys_reboot.\n");
  598. if (poweroff_powercycle)
  599. printk(KERN_INFO PFX "Power cycle is enabled.\n");
  600. #ifdef CONFIG_PROC_FS
  601. ipmi_table_header = register_sysctl_table(ipmi_root_table);
  602. if (!ipmi_table_header) {
  603. printk(KERN_ERR PFX "Unable to register powercycle sysctl\n");
  604. rv = -ENOMEM;
  605. goto out_err;
  606. }
  607. #endif
  608. rv = ipmi_smi_watcher_register(&smi_watcher);
  609. #ifdef CONFIG_PROC_FS
  610. if (rv) {
  611. unregister_sysctl_table(ipmi_table_header);
  612. printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
  613. goto out_err;
  614. }
  615. out_err:
  616. #endif
  617. return rv;
  618. }
  619. #ifdef MODULE
  620. static void __exit ipmi_poweroff_cleanup(void)
  621. {
  622. int rv;
  623. #ifdef CONFIG_PROC_FS
  624. unregister_sysctl_table(ipmi_table_header);
  625. #endif
  626. ipmi_smi_watcher_unregister(&smi_watcher);
  627. if (ready) {
  628. rv = ipmi_destroy_user(ipmi_user);
  629. if (rv)
  630. printk(KERN_ERR PFX "could not cleanup the IPMI"
  631. " user: 0x%x\n", rv);
  632. pm_power_off = old_poweroff_func;
  633. }
  634. }
  635. module_exit(ipmi_poweroff_cleanup);
  636. #endif
  637. module_init(ipmi_poweroff_init);
  638. MODULE_LICENSE("GPL");
  639. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  640. MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");