4 * MontaVista IPMI Poweroff extension to sys_reboot
6 * Author: MontaVista Software, Inc.
7 * Steven Dake <sdake@mvista.com>
8 * Corey Minyard <cminyard@mvista.com>
11 * Copyright 2002,2004 MontaVista Software Inc.
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.
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.
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.
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>
40 #include <linux/kdev_t.h>
41 #include <linux/ipmi.h>
42 #include <linux/ipmi_smi.h>
44 #define PFX "IPMI poweroff: "
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 */
51 /* the IPMI data command */
52 static int poweroff_powercycle;
54 /* parameter definition to allow user to flag power cycle */
55 module_param(poweroff_powercycle, int, 0644);
56 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.");
58 /* Stuff from the get device id command. */
59 static unsigned int mfg_id;
60 static unsigned int prod_id;
61 static unsigned char capabilities;
62 static unsigned char ipmi_version;
64 /* We use our own messages for this operation, we don't let the system
65 allocate them, since we may be in a panic situation. The whole
66 thing is single-threaded, anyway, so multiple messages are not
68 static void dummy_smi_free(struct ipmi_smi_msg *msg)
71 static void dummy_recv_free(struct ipmi_recv_msg *msg)
74 static struct ipmi_smi_msg halt_smi_msg =
76 .done = dummy_smi_free
78 static struct ipmi_recv_msg halt_recv_msg =
80 .done = dummy_recv_free
85 * Code to send a message and wait for the reponse.
88 static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
90 struct completion *comp = recv_msg->user_msg_data;
96 static struct ipmi_user_hndl ipmi_poweroff_handler =
98 .ipmi_recv_hndl = receive_handler
102 static int ipmi_request_wait_for_response(ipmi_user_t user,
103 struct ipmi_addr *addr,
104 struct kernel_ipmi_msg *send_msg)
107 struct completion comp;
109 init_completion(&comp);
111 rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp,
112 &halt_smi_msg, &halt_recv_msg, 0);
116 wait_for_completion(&comp);
118 return halt_recv_msg.msg.data[0];
121 /* We are in run-to-completion mode, no completion is desired. */
122 static int ipmi_request_in_rc_mode(ipmi_user_t user,
123 struct ipmi_addr *addr,
124 struct kernel_ipmi_msg *send_msg)
128 rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL,
129 &halt_smi_msg, &halt_recv_msg, 0);
133 return halt_recv_msg.msg.data[0];
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
145 static int ipmi_atca_detect (ipmi_user_t user)
147 struct ipmi_system_interface_addr smi_addr;
148 struct kernel_ipmi_msg send_msg;
150 unsigned char data[1];
153 * Configure IPMI address for local access
155 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
156 smi_addr.channel = IPMI_BMC_CHANNEL;
160 * Use get address info to check and see if we are ATCA
162 send_msg.netfn = IPMI_NETFN_ATCA;
163 send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
164 data[0] = IPMI_PICMG_ID;
165 send_msg.data = data;
166 send_msg.data_len = sizeof(data);
167 rv = ipmi_request_wait_for_response(user,
168 (struct ipmi_addr *) &smi_addr,
173 static void ipmi_poweroff_atca (ipmi_user_t user)
175 struct ipmi_system_interface_addr smi_addr;
176 struct kernel_ipmi_msg send_msg;
178 unsigned char data[4];
181 * Configure IPMI address for local access
183 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
184 smi_addr.channel = IPMI_BMC_CHANNEL;
187 printk(KERN_INFO PFX "Powering down via ATCA power command\n");
192 send_msg.netfn = IPMI_NETFN_ATCA;
193 send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
194 data[0] = IPMI_PICMG_ID;
195 data[1] = 0; /* FRU id */
196 data[2] = 0; /* Power Level */
197 data[3] = 0; /* Don't change saved presets */
198 send_msg.data = data;
199 send_msg.data_len = sizeof (data);
200 rv = ipmi_request_in_rc_mode(user,
201 (struct ipmi_addr *) &smi_addr,
204 printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
205 " IPMI error 0x%x\n", rv);
217 #define IPMI_NETFN_OEM_1 0xf8
218 #define OEM_GRP_CMD_SET_RESET_STATE 0x84
219 #define OEM_GRP_CMD_SET_POWER_STATE 0x82
220 #define IPMI_NETFN_OEM_8 0xf8
221 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
222 #define OEM_GRP_CMD_GET_SLOT_GA 0xa3
223 #define IPMI_NETFN_SENSOR_EVT 0x10
224 #define IPMI_CMD_GET_EVENT_RECEIVER 0x01
226 #define IPMI_CPI1_PRODUCT_ID 0x000157
227 #define IPMI_CPI1_MANUFACTURER_ID 0x0108
229 static int ipmi_cpi1_detect (ipmi_user_t user)
231 return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
232 && (prod_id == IPMI_CPI1_PRODUCT_ID));
235 static void ipmi_poweroff_cpi1 (ipmi_user_t user)
237 struct ipmi_system_interface_addr smi_addr;
238 struct ipmi_ipmb_addr ipmb_addr;
239 struct kernel_ipmi_msg send_msg;
241 unsigned char data[1];
243 unsigned char hotswap_ipmb;
244 unsigned char aer_addr;
245 unsigned char aer_lun;
248 * Configure IPMI address for local access
250 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
251 smi_addr.channel = IPMI_BMC_CHANNEL;
254 printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
257 * Get IPMI ipmb address
259 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
260 send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
261 send_msg.data = NULL;
262 send_msg.data_len = 0;
263 rv = ipmi_request_in_rc_mode(user,
264 (struct ipmi_addr *) &smi_addr,
268 slot = halt_recv_msg.msg.data[1];
269 hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
272 * Get active event receiver
274 send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
275 send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
276 send_msg.data = NULL;
277 send_msg.data_len = 0;
278 rv = ipmi_request_in_rc_mode(user,
279 (struct ipmi_addr *) &smi_addr,
283 aer_addr = halt_recv_msg.msg.data[1];
284 aer_lun = halt_recv_msg.msg.data[2];
287 * Setup IPMB address target instead of local target
289 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
290 ipmb_addr.channel = 0;
291 ipmb_addr.slave_addr = aer_addr;
292 ipmb_addr.lun = aer_lun;
295 * Send request hotswap control to remove blade from dpv
297 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
298 send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
299 send_msg.data = &hotswap_ipmb;
300 send_msg.data_len = 1;
301 ipmi_request_in_rc_mode(user,
302 (struct ipmi_addr *) &ipmb_addr,
308 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
309 send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
310 send_msg.data = data;
311 data[0] = 1; /* Reset asserted state */
312 send_msg.data_len = 1;
313 rv = ipmi_request_in_rc_mode(user,
314 (struct ipmi_addr *) &smi_addr,
322 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
323 send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
324 send_msg.data = data;
325 data[0] = 1; /* Power down state */
326 send_msg.data_len = 1;
327 rv = ipmi_request_in_rc_mode(user,
328 (struct ipmi_addr *) &smi_addr,
338 * ipmi_dell_chassis_detect()
339 * Dell systems with IPMI < 1.5 don't set the chassis capability bit
340 * but they can handle a chassis poweroff or powercycle command.
343 #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
344 static int ipmi_dell_chassis_detect (ipmi_user_t user)
346 const char ipmi_version_major = ipmi_version & 0xF;
347 const char ipmi_version_minor = (ipmi_version >> 4) & 0xF;
348 const char mfr[3] = DELL_IANA_MFR_ID;
349 if (!memcmp(mfr, &mfg_id, sizeof(mfr)) &&
350 ipmi_version_major <= 1 &&
351 ipmi_version_minor < 5)
357 * Standard chassis support
360 #define IPMI_NETFN_CHASSIS_REQUEST 0
361 #define IPMI_CHASSIS_CONTROL_CMD 0x02
363 static int ipmi_chassis_detect (ipmi_user_t user)
365 /* Chassis support, use it. */
366 return (capabilities & 0x80);
369 static void ipmi_poweroff_chassis (ipmi_user_t user)
371 struct ipmi_system_interface_addr smi_addr;
372 struct kernel_ipmi_msg send_msg;
374 unsigned char data[1];
377 * Configure IPMI address for local access
379 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
380 smi_addr.channel = IPMI_BMC_CHANNEL;
384 printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
385 (poweroff_powercycle ? "cycle" : "down"));
390 send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
391 send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
392 if (poweroff_powercycle)
393 data[0] = IPMI_CHASSIS_POWER_CYCLE;
395 data[0] = IPMI_CHASSIS_POWER_DOWN;
396 send_msg.data = data;
397 send_msg.data_len = sizeof(data);
398 rv = ipmi_request_in_rc_mode(user,
399 (struct ipmi_addr *) &smi_addr,
402 if (poweroff_powercycle) {
403 /* power cycle failed, default to power down */
404 printk(KERN_ERR PFX "Unable to send chassis power " \
405 "cycle message, IPMI error 0x%x\n", rv);
406 poweroff_powercycle = 0;
407 goto powercyclefailed;
410 printk(KERN_ERR PFX "Unable to send chassis power " \
411 "down message, IPMI error 0x%x\n", rv);
416 /* Table of possible power off functions. */
417 struct poweroff_function {
419 int (*detect)(ipmi_user_t user);
420 void (*poweroff_func)(ipmi_user_t user);
423 static struct poweroff_function poweroff_functions[] = {
424 { .platform_type = "ATCA",
425 .detect = ipmi_atca_detect,
426 .poweroff_func = ipmi_poweroff_atca },
427 { .platform_type = "CPI1",
428 .detect = ipmi_cpi1_detect,
429 .poweroff_func = ipmi_poweroff_cpi1 },
430 { .platform_type = "chassis",
431 .detect = ipmi_dell_chassis_detect,
432 .poweroff_func = ipmi_poweroff_chassis },
433 /* Chassis should generally be last, other things should override
435 { .platform_type = "chassis",
436 .detect = ipmi_chassis_detect,
437 .poweroff_func = ipmi_poweroff_chassis },
439 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
440 / sizeof(struct poweroff_function))
443 /* Our local state. */
444 static int ready = 0;
445 static ipmi_user_t ipmi_user;
446 static void (*specific_poweroff_func)(ipmi_user_t user) = NULL;
448 /* Holds the old poweroff function so we can restore it on removal. */
449 static void (*old_poweroff_func)(void);
452 /* Called on a powerdown request. */
453 static void ipmi_poweroff_function (void)
458 /* Use run-to-completion mode, since interrupts may be off. */
459 ipmi_user_set_run_to_completion(ipmi_user, 1);
460 specific_poweroff_func(ipmi_user);
461 ipmi_user_set_run_to_completion(ipmi_user, 0);
464 /* Wait for an IPMI interface to be installed, the first one installed
465 will be grabbed by this code and used to perform the powerdown. */
466 static void ipmi_po_new_smi(int if_num, struct device *device)
468 struct ipmi_system_interface_addr smi_addr;
469 struct kernel_ipmi_msg send_msg;
476 rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
479 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
485 * Do a get device ide and store some results, since this is
486 * used by several functions.
488 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
489 smi_addr.channel = IPMI_BMC_CHANNEL;
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,
500 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
501 " IPMI error 0x%x\n", rv);
505 if (halt_recv_msg.msg.data_len < 12) {
506 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
507 " short, was %d bytes, needed %d bytes\n",
508 halt_recv_msg.msg.data_len, 12);
512 mfg_id = (halt_recv_msg.msg.data[7]
513 | (halt_recv_msg.msg.data[8] << 8)
514 | (halt_recv_msg.msg.data[9] << 16));
515 prod_id = (halt_recv_msg.msg.data[10]
516 | (halt_recv_msg.msg.data[11] << 8));
517 capabilities = halt_recv_msg.msg.data[6];
518 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))
528 printk(KERN_ERR PFX "Unable to find a poweroff function that"
529 " will work, giving up\n");
530 ipmi_destroy_user(ipmi_user);
534 printk(KERN_INFO PFX "Found a %s style poweroff function\n",
535 poweroff_functions[i].platform_type);
536 specific_poweroff_func = poweroff_functions[i].poweroff_func;
537 old_poweroff_func = pm_power_off;
538 pm_power_off = ipmi_poweroff_function;
542 static void ipmi_po_smi_gone(int if_num)
544 /* This can never be called, because once poweroff driver is
545 registered, the interface can't go away until the power
546 driver is unregistered. */
549 static struct ipmi_smi_watcher smi_watcher =
551 .owner = THIS_MODULE,
552 .new_smi = ipmi_po_new_smi,
553 .smi_gone = ipmi_po_smi_gone
557 #ifdef CONFIG_PROC_FS
558 #include <linux/sysctl.h>
560 static ctl_table ipmi_table[] = {
561 { .ctl_name = DEV_IPMI_POWEROFF_POWERCYCLE,
562 .procname = "poweroff_powercycle",
563 .data = &poweroff_powercycle,
564 .maxlen = sizeof(poweroff_powercycle),
566 .proc_handler = &proc_dointvec },
570 static ctl_table ipmi_dir_table[] = {
571 { .ctl_name = DEV_IPMI,
574 .child = ipmi_table },
578 static ctl_table ipmi_root_table[] = {
579 { .ctl_name = CTL_DEV,
582 .child = ipmi_dir_table },
586 static struct ctl_table_header *ipmi_table_header;
587 #endif /* CONFIG_PROC_FS */
590 * Startup and shutdown functions.
592 static int ipmi_poweroff_init (void)
596 printk ("Copyright (C) 2004 MontaVista Software -"
597 " IPMI Powerdown via sys_reboot.\n");
599 if (poweroff_powercycle)
600 printk(KERN_INFO PFX "Power cycle is enabled.\n");
602 #ifdef CONFIG_PROC_FS
603 ipmi_table_header = register_sysctl_table(ipmi_root_table, 1);
604 if (!ipmi_table_header) {
605 printk(KERN_ERR PFX "Unable to register powercycle sysctl\n");
611 rv = ipmi_smi_watcher_register(&smi_watcher);
613 #ifdef CONFIG_PROC_FS
615 unregister_sysctl_table(ipmi_table_header);
616 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
626 static __exit void ipmi_poweroff_cleanup(void)
630 #ifdef CONFIG_PROC_FS
631 unregister_sysctl_table(ipmi_table_header);
634 ipmi_smi_watcher_unregister(&smi_watcher);
637 rv = ipmi_destroy_user(ipmi_user);
639 printk(KERN_ERR PFX "could not cleanup the IPMI"
640 " user: 0x%x\n", rv);
641 pm_power_off = old_poweroff_func;
644 module_exit(ipmi_poweroff_cleanup);
647 module_init(ipmi_poweroff_init);
648 MODULE_LICENSE("GPL");
649 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
650 MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");