ipmi_poweroff.c 20 KB

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