ipmi_poweroff.c 19 KB

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