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/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/proc_fs.h>
38 #include <linux/string.h>
39 #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 /* Where to we insert our poweroff function? */
47 extern void (*pm_power_off)(void);
49 /* Definitions for controlling power off (if the system supports it). It
50 * conveniently matches the IPMI chassis control values. */
51 #define IPMI_CHASSIS_POWER_DOWN 0 /* power down, the default. */
52 #define IPMI_CHASSIS_POWER_CYCLE 0x02 /* power cycle */
54 /* the IPMI data command */
55 static int poweroff_powercycle;
57 /* parameter definition to allow user to flag power cycle */
58 module_param(poweroff_powercycle, int, 0);
59 MODULE_PARM_DESC(poweroff_powercycles, " 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.");
61 /* Stuff from the get device id command. */
62 static unsigned int mfg_id;
63 static unsigned int prod_id;
64 static unsigned char capabilities;
65 static unsigned char ipmi_version;
67 /* We use our own messages for this operation, we don't let the system
68 allocate them, since we may be in a panic situation. The whole
69 thing is single-threaded, anyway, so multiple messages are not
71 static void dummy_smi_free(struct ipmi_smi_msg *msg)
74 static void dummy_recv_free(struct ipmi_recv_msg *msg)
77 static struct ipmi_smi_msg halt_smi_msg =
79 .done = dummy_smi_free
81 static struct ipmi_recv_msg halt_recv_msg =
83 .done = dummy_recv_free
88 * Code to send a message and wait for the reponse.
91 static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
93 struct completion *comp = recv_msg->user_msg_data;
99 static struct ipmi_user_hndl ipmi_poweroff_handler =
101 .ipmi_recv_hndl = receive_handler
105 static int ipmi_request_wait_for_response(ipmi_user_t user,
106 struct ipmi_addr *addr,
107 struct kernel_ipmi_msg *send_msg)
110 struct completion comp;
112 init_completion(&comp);
114 rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp,
115 &halt_smi_msg, &halt_recv_msg, 0);
119 wait_for_completion(&comp);
121 return halt_recv_msg.msg.data[0];
124 /* We are in run-to-completion mode, no completion is desired. */
125 static int ipmi_request_in_rc_mode(ipmi_user_t user,
126 struct ipmi_addr *addr,
127 struct kernel_ipmi_msg *send_msg)
131 rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL,
132 &halt_smi_msg, &halt_recv_msg, 0);
136 return halt_recv_msg.msg.data[0];
143 #define IPMI_NETFN_ATCA 0x2c
144 #define IPMI_ATCA_SET_POWER_CMD 0x11
145 #define IPMI_ATCA_GET_ADDR_INFO_CMD 0x01
146 #define IPMI_PICMG_ID 0
148 static int ipmi_atca_detect (ipmi_user_t user)
150 struct ipmi_system_interface_addr smi_addr;
151 struct kernel_ipmi_msg send_msg;
153 unsigned char data[1];
156 * Configure IPMI address for local access
158 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
159 smi_addr.channel = IPMI_BMC_CHANNEL;
163 * Use get address info to check and see if we are ATCA
165 send_msg.netfn = IPMI_NETFN_ATCA;
166 send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
167 data[0] = IPMI_PICMG_ID;
168 send_msg.data = data;
169 send_msg.data_len = sizeof(data);
170 rv = ipmi_request_wait_for_response(user,
171 (struct ipmi_addr *) &smi_addr,
176 static void ipmi_poweroff_atca (ipmi_user_t user)
178 struct ipmi_system_interface_addr smi_addr;
179 struct kernel_ipmi_msg send_msg;
181 unsigned char data[4];
184 * Configure IPMI address for local access
186 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
187 smi_addr.channel = IPMI_BMC_CHANNEL;
190 printk(KERN_INFO PFX "Powering down via ATCA power command\n");
195 send_msg.netfn = IPMI_NETFN_ATCA;
196 send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
197 data[0] = IPMI_PICMG_ID;
198 data[1] = 0; /* FRU id */
199 data[2] = 0; /* Power Level */
200 data[3] = 0; /* Don't change saved presets */
201 send_msg.data = data;
202 send_msg.data_len = sizeof (data);
203 rv = ipmi_request_in_rc_mode(user,
204 (struct ipmi_addr *) &smi_addr,
207 printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
208 " IPMI error 0x%x\n", rv);
220 #define IPMI_NETFN_OEM_1 0xf8
221 #define OEM_GRP_CMD_SET_RESET_STATE 0x84
222 #define OEM_GRP_CMD_SET_POWER_STATE 0x82
223 #define IPMI_NETFN_OEM_8 0xf8
224 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
225 #define OEM_GRP_CMD_GET_SLOT_GA 0xa3
226 #define IPMI_NETFN_SENSOR_EVT 0x10
227 #define IPMI_CMD_GET_EVENT_RECEIVER 0x01
229 #define IPMI_CPI1_PRODUCT_ID 0x000157
230 #define IPMI_CPI1_MANUFACTURER_ID 0x0108
232 static int ipmi_cpi1_detect (ipmi_user_t user)
234 return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
235 && (prod_id == IPMI_CPI1_PRODUCT_ID));
238 static void ipmi_poweroff_cpi1 (ipmi_user_t user)
240 struct ipmi_system_interface_addr smi_addr;
241 struct ipmi_ipmb_addr ipmb_addr;
242 struct kernel_ipmi_msg send_msg;
244 unsigned char data[1];
246 unsigned char hotswap_ipmb;
247 unsigned char aer_addr;
248 unsigned char aer_lun;
251 * Configure IPMI address for local access
253 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
254 smi_addr.channel = IPMI_BMC_CHANNEL;
257 printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
260 * Get IPMI ipmb address
262 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
263 send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
264 send_msg.data = NULL;
265 send_msg.data_len = 0;
266 rv = ipmi_request_in_rc_mode(user,
267 (struct ipmi_addr *) &smi_addr,
271 slot = halt_recv_msg.msg.data[1];
272 hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
275 * Get active event receiver
277 send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
278 send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
279 send_msg.data = NULL;
280 send_msg.data_len = 0;
281 rv = ipmi_request_in_rc_mode(user,
282 (struct ipmi_addr *) &smi_addr,
286 aer_addr = halt_recv_msg.msg.data[1];
287 aer_lun = halt_recv_msg.msg.data[2];
290 * Setup IPMB address target instead of local target
292 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
293 ipmb_addr.channel = 0;
294 ipmb_addr.slave_addr = aer_addr;
295 ipmb_addr.lun = aer_lun;
298 * Send request hotswap control to remove blade from dpv
300 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
301 send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
302 send_msg.data = &hotswap_ipmb;
303 send_msg.data_len = 1;
304 ipmi_request_in_rc_mode(user,
305 (struct ipmi_addr *) &ipmb_addr,
311 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
312 send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
313 send_msg.data = data;
314 data[0] = 1; /* Reset asserted state */
315 send_msg.data_len = 1;
316 rv = ipmi_request_in_rc_mode(user,
317 (struct ipmi_addr *) &smi_addr,
325 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
326 send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
327 send_msg.data = data;
328 data[0] = 1; /* Power down state */
329 send_msg.data_len = 1;
330 rv = ipmi_request_in_rc_mode(user,
331 (struct ipmi_addr *) &smi_addr,
341 * ipmi_dell_chassis_detect()
342 * Dell systems with IPMI < 1.5 don't set the chassis capability bit
343 * but they can handle a chassis poweroff or powercycle command.
346 #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
347 static int ipmi_dell_chassis_detect (ipmi_user_t user)
349 const char ipmi_version_major = ipmi_version & 0xF;
350 const char ipmi_version_minor = (ipmi_version >> 4) & 0xF;
351 const char mfr[3]=DELL_IANA_MFR_ID;
352 if (!memcmp(mfr, &mfg_id, sizeof(mfr)) &&
353 ipmi_version_major <= 1 &&
354 ipmi_version_minor < 5)
360 * Standard chassis support
363 #define IPMI_NETFN_CHASSIS_REQUEST 0
364 #define IPMI_CHASSIS_CONTROL_CMD 0x02
366 static int ipmi_chassis_detect (ipmi_user_t user)
368 /* Chassis support, use it. */
369 return (capabilities & 0x80);
372 static void ipmi_poweroff_chassis (ipmi_user_t user)
374 struct ipmi_system_interface_addr smi_addr;
375 struct kernel_ipmi_msg send_msg;
377 unsigned char data[1];
380 * Configure IPMI address for local access
382 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
383 smi_addr.channel = IPMI_BMC_CHANNEL;
387 printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
388 (poweroff_powercycle ? "cycle" : "down"));
393 send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
394 send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
395 if (poweroff_powercycle)
396 data[0] = IPMI_CHASSIS_POWER_CYCLE;
398 data[0] = IPMI_CHASSIS_POWER_DOWN;
399 send_msg.data = data;
400 send_msg.data_len = sizeof(data);
401 rv = ipmi_request_in_rc_mode(user,
402 (struct ipmi_addr *) &smi_addr,
405 if (poweroff_powercycle) {
406 /* power cycle failed, default to power down */
407 printk(KERN_ERR PFX "Unable to send chassis power " \
408 "cycle message, IPMI error 0x%x\n", rv);
409 poweroff_powercycle = 0;
410 goto powercyclefailed;
413 printk(KERN_ERR PFX "Unable to send chassis power " \
414 "down message, IPMI error 0x%x\n", rv);
419 /* Table of possible power off functions. */
420 struct poweroff_function {
422 int (*detect)(ipmi_user_t user);
423 void (*poweroff_func)(ipmi_user_t user);
426 static struct poweroff_function poweroff_functions[] = {
427 { .platform_type = "ATCA",
428 .detect = ipmi_atca_detect,
429 .poweroff_func = ipmi_poweroff_atca },
430 { .platform_type = "CPI1",
431 .detect = ipmi_cpi1_detect,
432 .poweroff_func = ipmi_poweroff_cpi1 },
433 { .platform_type = "chassis",
434 .detect = ipmi_dell_chassis_detect,
435 .poweroff_func = ipmi_poweroff_chassis },
436 /* Chassis should generally be last, other things should override
438 { .platform_type = "chassis",
439 .detect = ipmi_chassis_detect,
440 .poweroff_func = ipmi_poweroff_chassis },
442 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
443 / sizeof(struct poweroff_function))
446 /* Our local state. */
447 static int ready = 0;
448 static ipmi_user_t ipmi_user;
449 static void (*specific_poweroff_func)(ipmi_user_t user) = NULL;
451 /* Holds the old poweroff function so we can restore it on removal. */
452 static void (*old_poweroff_func)(void);
455 /* Called on a powerdown request. */
456 static void ipmi_poweroff_function (void)
461 /* Use run-to-completion mode, since interrupts may be off. */
462 ipmi_user_set_run_to_completion(ipmi_user, 1);
463 specific_poweroff_func(ipmi_user);
464 ipmi_user_set_run_to_completion(ipmi_user, 0);
467 /* Wait for an IPMI interface to be installed, the first one installed
468 will be grabbed by this code and used to perform the powerdown. */
469 static void ipmi_po_new_smi(int if_num)
471 struct ipmi_system_interface_addr smi_addr;
472 struct kernel_ipmi_msg send_msg;
479 rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
482 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
488 * Do a get device ide and store some results, since this is
489 * used by several functions.
491 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
492 smi_addr.channel = IPMI_BMC_CHANNEL;
495 send_msg.netfn = IPMI_NETFN_APP_REQUEST;
496 send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
497 send_msg.data = NULL;
498 send_msg.data_len = 0;
499 rv = ipmi_request_wait_for_response(ipmi_user,
500 (struct ipmi_addr *) &smi_addr,
503 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
504 " IPMI error 0x%x\n", rv);
508 if (halt_recv_msg.msg.data_len < 12) {
509 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
510 " short, was %d bytes, needed %d bytes\n",
511 halt_recv_msg.msg.data_len, 12);
515 mfg_id = (halt_recv_msg.msg.data[7]
516 | (halt_recv_msg.msg.data[8] << 8)
517 | (halt_recv_msg.msg.data[9] << 16));
518 prod_id = (halt_recv_msg.msg.data[10]
519 | (halt_recv_msg.msg.data[11] << 8));
520 capabilities = halt_recv_msg.msg.data[6];
521 ipmi_version = halt_recv_msg.msg.data[5];
524 /* Scan for a poweroff method */
525 for (i = 0; i < NUM_PO_FUNCS; i++) {
526 if (poweroff_functions[i].detect(ipmi_user))
531 printk(KERN_ERR PFX "Unable to find a poweroff function that"
532 " will work, giving up\n");
533 ipmi_destroy_user(ipmi_user);
537 printk(KERN_INFO PFX "Found a %s style poweroff function\n",
538 poweroff_functions[i].platform_type);
539 specific_poweroff_func = poweroff_functions[i].poweroff_func;
540 old_poweroff_func = pm_power_off;
541 pm_power_off = ipmi_poweroff_function;
545 static void ipmi_po_smi_gone(int if_num)
547 /* This can never be called, because once poweroff driver is
548 registered, the interface can't go away until the power
549 driver is unregistered. */
552 static struct ipmi_smi_watcher smi_watcher =
554 .owner = THIS_MODULE,
555 .new_smi = ipmi_po_new_smi,
556 .smi_gone = ipmi_po_smi_gone
560 #ifdef CONFIG_PROC_FS
561 #include <linux/sysctl.h>
563 static ctl_table ipmi_table[] = {
564 { .ctl_name = DEV_IPMI_POWEROFF_POWERCYCLE,
565 .procname = "poweroff_powercycle",
566 .data = &poweroff_powercycle,
567 .maxlen = sizeof(poweroff_powercycle),
569 .proc_handler = &proc_dointvec },
573 static ctl_table ipmi_dir_table[] = {
574 { .ctl_name = DEV_IPMI,
577 .child = ipmi_table },
581 static ctl_table ipmi_root_table[] = {
582 { .ctl_name = CTL_DEV,
585 .child = ipmi_dir_table },
589 static struct ctl_table_header *ipmi_table_header;
590 #endif /* CONFIG_PROC_FS */
593 * Startup and shutdown functions.
595 static int ipmi_poweroff_init (void)
599 printk ("Copyright (C) 2004 MontaVista Software -"
600 " IPMI Powerdown via sys_reboot.\n");
602 if (poweroff_powercycle)
603 printk(KERN_INFO PFX "Power cycle is enabled.\n");
605 #ifdef CONFIG_PROC_FS
606 ipmi_table_header = register_sysctl_table(ipmi_root_table, 1);
607 if (!ipmi_table_header) {
608 printk(KERN_ERR PFX "Unable to register powercycle sysctl\n");
614 #ifdef CONFIG_PROC_FS
615 rv = ipmi_smi_watcher_register(&smi_watcher);
618 unregister_sysctl_table(ipmi_table_header);
619 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
628 static __exit void ipmi_poweroff_cleanup(void)
632 #ifdef CONFIG_PROC_FS
633 unregister_sysctl_table(ipmi_table_header);
636 ipmi_smi_watcher_unregister(&smi_watcher);
639 rv = ipmi_destroy_user(ipmi_user);
641 printk(KERN_ERR PFX "could not cleanup the IPMI"
642 " user: 0x%x\n", rv);
643 pm_power_off = old_poweroff_func;
646 module_exit(ipmi_poweroff_cleanup);
649 module_init(ipmi_poweroff_init);
650 MODULE_LICENSE("GPL");
651 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
652 MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");