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>
41 #include <linux/kdev_t.h>
42 #include <linux/ipmi.h>
43 #include <linux/ipmi_smi.h>
45 #define PFX "IPMI poweroff: "
47 /* Definitions for controlling power off (if the system supports it). It
48 * conveniently matches the IPMI chassis control values. */
49 #define IPMI_CHASSIS_POWER_DOWN 0 /* power down, the default. */
50 #define IPMI_CHASSIS_POWER_CYCLE 0x02 /* power cycle */
52 /* the IPMI data command */
53 static int poweroff_powercycle
;
55 /* parameter definition to allow user to flag power cycle */
56 module_param(poweroff_powercycle
, int, 0644);
57 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.");
59 /* Stuff from the get device id command. */
60 static unsigned int mfg_id
;
61 static unsigned int prod_id
;
62 static unsigned char capabilities
;
63 static unsigned char ipmi_version
;
65 /* We use our own messages for this operation, we don't let the system
66 allocate them, since we may be in a panic situation. The whole
67 thing is single-threaded, anyway, so multiple messages are not
69 static void dummy_smi_free(struct ipmi_smi_msg
*msg
)
72 static void dummy_recv_free(struct ipmi_recv_msg
*msg
)
75 static struct ipmi_smi_msg halt_smi_msg
=
77 .done
= dummy_smi_free
79 static struct ipmi_recv_msg halt_recv_msg
=
81 .done
= dummy_recv_free
86 * Code to send a message and wait for the reponse.
89 static void receive_handler(struct ipmi_recv_msg
*recv_msg
, void *handler_data
)
91 struct completion
*comp
= recv_msg
->user_msg_data
;
97 static struct ipmi_user_hndl ipmi_poweroff_handler
=
99 .ipmi_recv_hndl
= receive_handler
103 static int ipmi_request_wait_for_response(ipmi_user_t user
,
104 struct ipmi_addr
*addr
,
105 struct kernel_ipmi_msg
*send_msg
)
108 struct completion comp
;
110 init_completion(&comp
);
112 rv
= ipmi_request_supply_msgs(user
, addr
, 0, send_msg
, &comp
,
113 &halt_smi_msg
, &halt_recv_msg
, 0);
117 wait_for_completion(&comp
);
119 return halt_recv_msg
.msg
.data
[0];
122 /* We are in run-to-completion mode, no completion is desired. */
123 static int ipmi_request_in_rc_mode(ipmi_user_t user
,
124 struct ipmi_addr
*addr
,
125 struct kernel_ipmi_msg
*send_msg
)
129 rv
= ipmi_request_supply_msgs(user
, addr
, 0, send_msg
, NULL
,
130 &halt_smi_msg
, &halt_recv_msg
, 0);
134 return halt_recv_msg
.msg
.data
[0];
141 #define IPMI_NETFN_ATCA 0x2c
142 #define IPMI_ATCA_SET_POWER_CMD 0x11
143 #define IPMI_ATCA_GET_ADDR_INFO_CMD 0x01
144 #define IPMI_PICMG_ID 0
146 static int ipmi_atca_detect (ipmi_user_t user
)
148 struct ipmi_system_interface_addr smi_addr
;
149 struct kernel_ipmi_msg send_msg
;
151 unsigned char data
[1];
154 * Configure IPMI address for local access
156 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
157 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
161 * Use get address info to check and see if we are ATCA
163 send_msg
.netfn
= IPMI_NETFN_ATCA
;
164 send_msg
.cmd
= IPMI_ATCA_GET_ADDR_INFO_CMD
;
165 data
[0] = IPMI_PICMG_ID
;
166 send_msg
.data
= data
;
167 send_msg
.data_len
= sizeof(data
);
168 rv
= ipmi_request_wait_for_response(user
,
169 (struct ipmi_addr
*) &smi_addr
,
174 static void ipmi_poweroff_atca (ipmi_user_t user
)
176 struct ipmi_system_interface_addr smi_addr
;
177 struct kernel_ipmi_msg send_msg
;
179 unsigned char data
[4];
182 * Configure IPMI address for local access
184 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
185 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
188 printk(KERN_INFO PFX
"Powering down via ATCA power command\n");
193 send_msg
.netfn
= IPMI_NETFN_ATCA
;
194 send_msg
.cmd
= IPMI_ATCA_SET_POWER_CMD
;
195 data
[0] = IPMI_PICMG_ID
;
196 data
[1] = 0; /* FRU id */
197 data
[2] = 0; /* Power Level */
198 data
[3] = 0; /* Don't change saved presets */
199 send_msg
.data
= data
;
200 send_msg
.data_len
= sizeof (data
);
201 rv
= ipmi_request_in_rc_mode(user
,
202 (struct ipmi_addr
*) &smi_addr
,
205 printk(KERN_ERR PFX
"Unable to send ATCA powerdown message,"
206 " IPMI error 0x%x\n", rv
);
218 #define IPMI_NETFN_OEM_1 0xf8
219 #define OEM_GRP_CMD_SET_RESET_STATE 0x84
220 #define OEM_GRP_CMD_SET_POWER_STATE 0x82
221 #define IPMI_NETFN_OEM_8 0xf8
222 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
223 #define OEM_GRP_CMD_GET_SLOT_GA 0xa3
224 #define IPMI_NETFN_SENSOR_EVT 0x10
225 #define IPMI_CMD_GET_EVENT_RECEIVER 0x01
227 #define IPMI_CPI1_PRODUCT_ID 0x000157
228 #define IPMI_CPI1_MANUFACTURER_ID 0x0108
230 static int ipmi_cpi1_detect (ipmi_user_t user
)
232 return ((mfg_id
== IPMI_CPI1_MANUFACTURER_ID
)
233 && (prod_id
== IPMI_CPI1_PRODUCT_ID
));
236 static void ipmi_poweroff_cpi1 (ipmi_user_t user
)
238 struct ipmi_system_interface_addr smi_addr
;
239 struct ipmi_ipmb_addr ipmb_addr
;
240 struct kernel_ipmi_msg send_msg
;
242 unsigned char data
[1];
244 unsigned char hotswap_ipmb
;
245 unsigned char aer_addr
;
246 unsigned char aer_lun
;
249 * Configure IPMI address for local access
251 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
252 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
255 printk(KERN_INFO PFX
"Powering down via CPI1 power command\n");
258 * Get IPMI ipmb address
260 send_msg
.netfn
= IPMI_NETFN_OEM_8
>> 2;
261 send_msg
.cmd
= OEM_GRP_CMD_GET_SLOT_GA
;
262 send_msg
.data
= NULL
;
263 send_msg
.data_len
= 0;
264 rv
= ipmi_request_in_rc_mode(user
,
265 (struct ipmi_addr
*) &smi_addr
,
269 slot
= halt_recv_msg
.msg
.data
[1];
270 hotswap_ipmb
= (slot
> 9) ? (0xb0 + 2 * slot
) : (0xae + 2 * slot
);
273 * Get active event receiver
275 send_msg
.netfn
= IPMI_NETFN_SENSOR_EVT
>> 2;
276 send_msg
.cmd
= IPMI_CMD_GET_EVENT_RECEIVER
;
277 send_msg
.data
= NULL
;
278 send_msg
.data_len
= 0;
279 rv
= ipmi_request_in_rc_mode(user
,
280 (struct ipmi_addr
*) &smi_addr
,
284 aer_addr
= halt_recv_msg
.msg
.data
[1];
285 aer_lun
= halt_recv_msg
.msg
.data
[2];
288 * Setup IPMB address target instead of local target
290 ipmb_addr
.addr_type
= IPMI_IPMB_ADDR_TYPE
;
291 ipmb_addr
.channel
= 0;
292 ipmb_addr
.slave_addr
= aer_addr
;
293 ipmb_addr
.lun
= aer_lun
;
296 * Send request hotswap control to remove blade from dpv
298 send_msg
.netfn
= IPMI_NETFN_OEM_8
>> 2;
299 send_msg
.cmd
= OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL
;
300 send_msg
.data
= &hotswap_ipmb
;
301 send_msg
.data_len
= 1;
302 ipmi_request_in_rc_mode(user
,
303 (struct ipmi_addr
*) &ipmb_addr
,
309 send_msg
.netfn
= IPMI_NETFN_OEM_1
>> 2;
310 send_msg
.cmd
= OEM_GRP_CMD_SET_RESET_STATE
;
311 send_msg
.data
= data
;
312 data
[0] = 1; /* Reset asserted state */
313 send_msg
.data_len
= 1;
314 rv
= ipmi_request_in_rc_mode(user
,
315 (struct ipmi_addr
*) &smi_addr
,
323 send_msg
.netfn
= IPMI_NETFN_OEM_1
>> 2;
324 send_msg
.cmd
= OEM_GRP_CMD_SET_POWER_STATE
;
325 send_msg
.data
= data
;
326 data
[0] = 1; /* Power down state */
327 send_msg
.data_len
= 1;
328 rv
= ipmi_request_in_rc_mode(user
,
329 (struct ipmi_addr
*) &smi_addr
,
339 * ipmi_dell_chassis_detect()
340 * Dell systems with IPMI < 1.5 don't set the chassis capability bit
341 * but they can handle a chassis poweroff or powercycle command.
344 #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
345 static int ipmi_dell_chassis_detect (ipmi_user_t user
)
347 const char ipmi_version_major
= ipmi_version
& 0xF;
348 const char ipmi_version_minor
= (ipmi_version
>> 4) & 0xF;
349 const char mfr
[3] = DELL_IANA_MFR_ID
;
350 if (!memcmp(mfr
, &mfg_id
, sizeof(mfr
)) &&
351 ipmi_version_major
<= 1 &&
352 ipmi_version_minor
< 5)
358 * Standard chassis support
361 #define IPMI_NETFN_CHASSIS_REQUEST 0
362 #define IPMI_CHASSIS_CONTROL_CMD 0x02
364 static int ipmi_chassis_detect (ipmi_user_t user
)
366 /* Chassis support, use it. */
367 return (capabilities
& 0x80);
370 static void ipmi_poweroff_chassis (ipmi_user_t user
)
372 struct ipmi_system_interface_addr smi_addr
;
373 struct kernel_ipmi_msg send_msg
;
375 unsigned char data
[1];
378 * Configure IPMI address for local access
380 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
381 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
385 printk(KERN_INFO PFX
"Powering %s via IPMI chassis control command\n",
386 (poweroff_powercycle
? "cycle" : "down"));
391 send_msg
.netfn
= IPMI_NETFN_CHASSIS_REQUEST
;
392 send_msg
.cmd
= IPMI_CHASSIS_CONTROL_CMD
;
393 if (poweroff_powercycle
)
394 data
[0] = IPMI_CHASSIS_POWER_CYCLE
;
396 data
[0] = IPMI_CHASSIS_POWER_DOWN
;
397 send_msg
.data
= data
;
398 send_msg
.data_len
= sizeof(data
);
399 rv
= ipmi_request_in_rc_mode(user
,
400 (struct ipmi_addr
*) &smi_addr
,
403 if (poweroff_powercycle
) {
404 /* power cycle failed, default to power down */
405 printk(KERN_ERR PFX
"Unable to send chassis power " \
406 "cycle message, IPMI error 0x%x\n", rv
);
407 poweroff_powercycle
= 0;
408 goto powercyclefailed
;
411 printk(KERN_ERR PFX
"Unable to send chassis power " \
412 "down message, IPMI error 0x%x\n", rv
);
417 /* Table of possible power off functions. */
418 struct poweroff_function
{
420 int (*detect
)(ipmi_user_t user
);
421 void (*poweroff_func
)(ipmi_user_t user
);
424 static struct poweroff_function poweroff_functions
[] = {
425 { .platform_type
= "ATCA",
426 .detect
= ipmi_atca_detect
,
427 .poweroff_func
= ipmi_poweroff_atca
},
428 { .platform_type
= "CPI1",
429 .detect
= ipmi_cpi1_detect
,
430 .poweroff_func
= ipmi_poweroff_cpi1
},
431 { .platform_type
= "chassis",
432 .detect
= ipmi_dell_chassis_detect
,
433 .poweroff_func
= ipmi_poweroff_chassis
},
434 /* Chassis should generally be last, other things should override
436 { .platform_type
= "chassis",
437 .detect
= ipmi_chassis_detect
,
438 .poweroff_func
= ipmi_poweroff_chassis
},
440 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
441 / sizeof(struct poweroff_function))
444 /* Our local state. */
445 static int ready
= 0;
446 static ipmi_user_t ipmi_user
;
447 static void (*specific_poweroff_func
)(ipmi_user_t user
) = NULL
;
449 /* Holds the old poweroff function so we can restore it on removal. */
450 static void (*old_poweroff_func
)(void);
453 /* Called on a powerdown request. */
454 static void ipmi_poweroff_function (void)
459 /* Use run-to-completion mode, since interrupts may be off. */
460 ipmi_user_set_run_to_completion(ipmi_user
, 1);
461 specific_poweroff_func(ipmi_user
);
462 ipmi_user_set_run_to_completion(ipmi_user
, 0);
465 /* Wait for an IPMI interface to be installed, the first one installed
466 will be grabbed by this code and used to perform the powerdown. */
467 static void ipmi_po_new_smi(int if_num
, struct device
*device
)
469 struct ipmi_system_interface_addr smi_addr
;
470 struct kernel_ipmi_msg send_msg
;
477 rv
= ipmi_create_user(if_num
, &ipmi_poweroff_handler
, NULL
,
480 printk(KERN_ERR PFX
"could not create IPMI user, error %d\n",
486 * Do a get device ide and store some results, since this is
487 * used by several functions.
489 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
490 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
493 send_msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
494 send_msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
495 send_msg
.data
= NULL
;
496 send_msg
.data_len
= 0;
497 rv
= ipmi_request_wait_for_response(ipmi_user
,
498 (struct ipmi_addr
*) &smi_addr
,
501 printk(KERN_ERR PFX
"Unable to send IPMI get device id info,"
502 " IPMI error 0x%x\n", rv
);
506 if (halt_recv_msg
.msg
.data_len
< 12) {
507 printk(KERN_ERR PFX
"(chassis) IPMI get device id info too,"
508 " short, was %d bytes, needed %d bytes\n",
509 halt_recv_msg
.msg
.data_len
, 12);
513 mfg_id
= (halt_recv_msg
.msg
.data
[7]
514 | (halt_recv_msg
.msg
.data
[8] << 8)
515 | (halt_recv_msg
.msg
.data
[9] << 16));
516 prod_id
= (halt_recv_msg
.msg
.data
[10]
517 | (halt_recv_msg
.msg
.data
[11] << 8));
518 capabilities
= halt_recv_msg
.msg
.data
[6];
519 ipmi_version
= halt_recv_msg
.msg
.data
[5];
522 /* Scan for a poweroff method */
523 for (i
= 0; i
< NUM_PO_FUNCS
; i
++) {
524 if (poweroff_functions
[i
].detect(ipmi_user
))
529 printk(KERN_ERR PFX
"Unable to find a poweroff function that"
530 " will work, giving up\n");
531 ipmi_destroy_user(ipmi_user
);
535 printk(KERN_INFO PFX
"Found a %s style poweroff function\n",
536 poweroff_functions
[i
].platform_type
);
537 specific_poweroff_func
= poweroff_functions
[i
].poweroff_func
;
538 old_poweroff_func
= pm_power_off
;
539 pm_power_off
= ipmi_poweroff_function
;
543 static void ipmi_po_smi_gone(int if_num
)
545 /* This can never be called, because once poweroff driver is
546 registered, the interface can't go away until the power
547 driver is unregistered. */
550 static struct ipmi_smi_watcher smi_watcher
=
552 .owner
= THIS_MODULE
,
553 .new_smi
= ipmi_po_new_smi
,
554 .smi_gone
= ipmi_po_smi_gone
558 #ifdef CONFIG_PROC_FS
559 #include <linux/sysctl.h>
561 static ctl_table ipmi_table
[] = {
562 { .ctl_name
= DEV_IPMI_POWEROFF_POWERCYCLE
,
563 .procname
= "poweroff_powercycle",
564 .data
= &poweroff_powercycle
,
565 .maxlen
= sizeof(poweroff_powercycle
),
567 .proc_handler
= &proc_dointvec
},
571 static ctl_table ipmi_dir_table
[] = {
572 { .ctl_name
= DEV_IPMI
,
575 .child
= ipmi_table
},
579 static ctl_table ipmi_root_table
[] = {
580 { .ctl_name
= CTL_DEV
,
583 .child
= ipmi_dir_table
},
587 static struct ctl_table_header
*ipmi_table_header
;
588 #endif /* CONFIG_PROC_FS */
591 * Startup and shutdown functions.
593 static int ipmi_poweroff_init (void)
597 printk ("Copyright (C) 2004 MontaVista Software -"
598 " IPMI Powerdown via sys_reboot.\n");
600 if (poweroff_powercycle
)
601 printk(KERN_INFO PFX
"Power cycle is enabled.\n");
603 #ifdef CONFIG_PROC_FS
604 ipmi_table_header
= register_sysctl_table(ipmi_root_table
, 1);
605 if (!ipmi_table_header
) {
606 printk(KERN_ERR PFX
"Unable to register powercycle sysctl\n");
612 rv
= ipmi_smi_watcher_register(&smi_watcher
);
614 #ifdef CONFIG_PROC_FS
616 unregister_sysctl_table(ipmi_table_header
);
617 printk(KERN_ERR PFX
"Unable to register SMI watcher: %d\n", rv
);
627 static __exit
void ipmi_poweroff_cleanup(void)
631 #ifdef CONFIG_PROC_FS
632 unregister_sysctl_table(ipmi_table_header
);
635 ipmi_smi_watcher_unregister(&smi_watcher
);
638 rv
= ipmi_destroy_user(ipmi_user
);
640 printk(KERN_ERR PFX
"could not cleanup the IPMI"
641 " user: 0x%x\n", rv
);
642 pm_power_off
= old_poweroff_func
;
645 module_exit(ipmi_poweroff_cleanup
);
648 module_init(ipmi_poweroff_init
);
649 MODULE_LICENSE("GPL");
650 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
651 MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");