ipmi_poweroff.c 19 KB

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