ipmi_poweroff.c 20 KB

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