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: "
45 #define IPMI_POWEROFF_VERSION "v33"
47 /* Where to we insert our poweroff function? */
48 extern void (*pm_power_off)(void);
50 /* Definitions for controlling power off (if the system supports it). It
51 * conveniently matches the IPMI chassis control values. */
52 #define IPMI_CHASSIS_POWER_DOWN 0 /* power down, the default. */
53 #define IPMI_CHASSIS_POWER_CYCLE 0x02 /* power cycle */
55 /* the IPMI data command */
56 static int poweroff_control = IPMI_CHASSIS_POWER_DOWN;
58 /* parameter definition to allow user to flag power cycle */
59 module_param(poweroff_control, int, IPMI_CHASSIS_POWER_DOWN);
60 MODULE_PARM_DESC(poweroff_control, " Set to 2 to enable power cycle instead of power down. Power cycle is contingent on hardware support, otherwise it defaults back to power down.");
62 /* Stuff from the get device id command. */
63 static unsigned int mfg_id;
64 static unsigned int prod_id;
65 static unsigned char capabilities;
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 * Standard chassis support
344 #define IPMI_NETFN_CHASSIS_REQUEST 0
345 #define IPMI_CHASSIS_CONTROL_CMD 0x02
347 static int ipmi_chassis_detect (ipmi_user_t user)
349 /* Chassis support, use it. */
350 return (capabilities & 0x80);
353 static void ipmi_poweroff_chassis (ipmi_user_t user)
355 struct ipmi_system_interface_addr smi_addr;
356 struct kernel_ipmi_msg send_msg;
358 unsigned char data[1];
361 * Configure IPMI address for local access
363 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
364 smi_addr.channel = IPMI_BMC_CHANNEL;
368 printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
369 ((poweroff_control != IPMI_CHASSIS_POWER_CYCLE) ? "down" : "cycle"));
374 send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
375 send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
376 data[0] = poweroff_control;
377 send_msg.data = data;
378 send_msg.data_len = sizeof(data);
379 rv = ipmi_request_in_rc_mode(user,
380 (struct ipmi_addr *) &smi_addr,
383 switch (poweroff_control) {
384 case IPMI_CHASSIS_POWER_CYCLE:
385 /* power cycle failed, default to power down */
386 printk(KERN_ERR PFX "Unable to send chassis power " \
387 "cycle message, IPMI error 0x%x\n", rv);
388 poweroff_control = IPMI_CHASSIS_POWER_DOWN;
389 goto powercyclefailed;
391 case IPMI_CHASSIS_POWER_DOWN:
393 printk(KERN_ERR PFX "Unable to send chassis power " \
394 "down message, IPMI error 0x%x\n", rv);
403 /* Table of possible power off functions. */
404 struct poweroff_function {
406 int (*detect)(ipmi_user_t user);
407 void (*poweroff_func)(ipmi_user_t user);
410 static struct poweroff_function poweroff_functions[] = {
411 { .platform_type = "ATCA",
412 .detect = ipmi_atca_detect,
413 .poweroff_func = ipmi_poweroff_atca },
414 { .platform_type = "CPI1",
415 .detect = ipmi_cpi1_detect,
416 .poweroff_func = ipmi_poweroff_cpi1 },
417 /* Chassis should generally be last, other things should override
419 { .platform_type = "chassis",
420 .detect = ipmi_chassis_detect,
421 .poweroff_func = ipmi_poweroff_chassis },
423 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
424 / sizeof(struct poweroff_function))
427 /* Our local state. */
428 static int ready = 0;
429 static ipmi_user_t ipmi_user;
430 static void (*specific_poweroff_func)(ipmi_user_t user) = NULL;
432 /* Holds the old poweroff function so we can restore it on removal. */
433 static void (*old_poweroff_func)(void);
436 /* Called on a powerdown request. */
437 static void ipmi_poweroff_function (void)
442 /* Use run-to-completion mode, since interrupts may be off. */
443 ipmi_user_set_run_to_completion(ipmi_user, 1);
444 specific_poweroff_func(ipmi_user);
445 ipmi_user_set_run_to_completion(ipmi_user, 0);
448 /* Wait for an IPMI interface to be installed, the first one installed
449 will be grabbed by this code and used to perform the powerdown. */
450 static void ipmi_po_new_smi(int if_num)
452 struct ipmi_system_interface_addr smi_addr;
453 struct kernel_ipmi_msg send_msg;
460 rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
463 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
469 * Do a get device ide and store some results, since this is
470 * used by several functions.
472 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
473 smi_addr.channel = IPMI_BMC_CHANNEL;
476 send_msg.netfn = IPMI_NETFN_APP_REQUEST;
477 send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
478 send_msg.data = NULL;
479 send_msg.data_len = 0;
480 rv = ipmi_request_wait_for_response(ipmi_user,
481 (struct ipmi_addr *) &smi_addr,
484 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
485 " IPMI error 0x%x\n", rv);
489 if (halt_recv_msg.msg.data_len < 12) {
490 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
491 " short, was %d bytes, needed %d bytes\n",
492 halt_recv_msg.msg.data_len, 12);
496 mfg_id = (halt_recv_msg.msg.data[7]
497 | (halt_recv_msg.msg.data[8] << 8)
498 | (halt_recv_msg.msg.data[9] << 16));
499 prod_id = (halt_recv_msg.msg.data[10]
500 | (halt_recv_msg.msg.data[11] << 8));
501 capabilities = halt_recv_msg.msg.data[6];
504 /* Scan for a poweroff method */
505 for (i=0; i<NUM_PO_FUNCS; i++) {
506 if (poweroff_functions[i].detect(ipmi_user))
511 printk(KERN_ERR PFX "Unable to find a poweroff function that"
512 " will work, giving up\n");
513 ipmi_destroy_user(ipmi_user);
517 printk(KERN_INFO PFX "Found a %s style poweroff function\n",
518 poweroff_functions[i].platform_type);
519 specific_poweroff_func = poweroff_functions[i].poweroff_func;
520 old_poweroff_func = pm_power_off;
521 pm_power_off = ipmi_poweroff_function;
525 static void ipmi_po_smi_gone(int if_num)
527 /* This can never be called, because once poweroff driver is
528 registered, the interface can't go away until the power
529 driver is unregistered. */
532 static struct ipmi_smi_watcher smi_watcher =
534 .owner = THIS_MODULE,
535 .new_smi = ipmi_po_new_smi,
536 .smi_gone = ipmi_po_smi_gone
540 #ifdef CONFIG_PROC_FS
541 /* displays properties to proc */
542 static int proc_read_chassctrl(char *page, char **start, off_t off, int count,
543 int *eof, void *data)
545 return sprintf(page, "%d\t[ 0=powerdown 2=powercycle ]\n",
549 /* process property writes from proc */
550 static int proc_write_chassctrl(struct file *file, const char *buffer,
551 unsigned long count, void *data)
554 unsigned int newval = 0;
556 sscanf(buffer, "%d", &newval);
558 case IPMI_CHASSIS_POWER_CYCLE:
559 printk(KERN_INFO PFX "power cycle is now enabled\n");
560 poweroff_control = newval;
563 case IPMI_CHASSIS_POWER_DOWN:
564 poweroff_control = IPMI_CHASSIS_POWER_DOWN;
574 #endif /* CONFIG_PROC_FS */
577 * Startup and shutdown functions.
579 static int ipmi_poweroff_init (void)
582 struct proc_dir_entry *file;
584 printk ("Copyright (C) 2004 MontaVista Software -"
585 " IPMI Powerdown via sys_reboot version "
586 IPMI_POWEROFF_VERSION ".\n");
588 switch (poweroff_control) {
589 case IPMI_CHASSIS_POWER_CYCLE:
590 printk(KERN_INFO PFX "Power cycle is enabled.\n");
593 case IPMI_CHASSIS_POWER_DOWN:
595 poweroff_control = IPMI_CHASSIS_POWER_DOWN;
599 rv = ipmi_smi_watcher_register(&smi_watcher);
601 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
605 #ifdef CONFIG_PROC_FS
606 file = create_proc_entry("poweroff_control", 0, proc_ipmi_root);
608 printk(KERN_ERR PFX "Unable to create proc power control\n");
611 file->read_proc = proc_read_chassctrl;
612 file->write_proc = proc_write_chassctrl;
613 file->owner = THIS_MODULE;
622 static __exit void ipmi_poweroff_cleanup(void)
626 #ifdef CONFIG_PROC_FS
627 remove_proc_entry("poweroff_control", proc_ipmi_root);
630 ipmi_smi_watcher_unregister(&smi_watcher);
633 rv = ipmi_destroy_user(ipmi_user);
635 printk(KERN_ERR PFX "could not cleanup the IPMI"
636 " user: 0x%x\n", rv);
637 pm_power_off = old_poweroff_func;
640 module_exit(ipmi_poweroff_cleanup);
643 module_init(ipmi_poweroff_init);
644 MODULE_LICENSE("GPL");