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 <asm/semaphore.h>
35 #include <linux/kdev_t.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/proc_fs.h>
39 #include <linux/string.h>
40 #include <linux/ipmi.h>
41 #include <linux/ipmi_smi.h>
43 #define PFX "IPMI poweroff: "
44 #define IPMI_POWEROFF_VERSION "v33"
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_control
= IPMI_CHASSIS_POWER_DOWN
;
57 /* parameter definition to allow user to flag power cycle */
58 module_param(poweroff_control
, int, IPMI_CHASSIS_POWER_DOWN
);
59 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.");
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
;
66 /* We use our own messages for this operation, we don't let the system
67 allocate them, since we may be in a panic situation. The whole
68 thing is single-threaded, anyway, so multiple messages are not
70 static void dummy_smi_free(struct ipmi_smi_msg
*msg
)
73 static void dummy_recv_free(struct ipmi_recv_msg
*msg
)
76 static struct ipmi_smi_msg halt_smi_msg
=
78 .done
= dummy_smi_free
80 static struct ipmi_recv_msg halt_recv_msg
=
82 .done
= dummy_recv_free
87 * Code to send a message and wait for the reponse.
90 static void receive_handler(struct ipmi_recv_msg
*recv_msg
, void *handler_data
)
92 struct semaphore
*sem
= recv_msg
->user_msg_data
;
98 static struct ipmi_user_hndl ipmi_poweroff_handler
=
100 .ipmi_recv_hndl
= receive_handler
104 static int ipmi_request_wait_for_response(ipmi_user_t user
,
105 struct ipmi_addr
*addr
,
106 struct kernel_ipmi_msg
*send_msg
)
109 struct semaphore sem
;
113 rv
= ipmi_request_supply_msgs(user
, addr
, 0, send_msg
, &sem
,
114 &halt_smi_msg
, &halt_recv_msg
, 0);
120 return halt_recv_msg
.msg
.data
[0];
123 /* We are in run-to-completion mode, no semaphore is desired. */
124 static int ipmi_request_in_rc_mode(ipmi_user_t user
,
125 struct ipmi_addr
*addr
,
126 struct kernel_ipmi_msg
*send_msg
)
130 rv
= ipmi_request_supply_msgs(user
, addr
, 0, send_msg
, NULL
,
131 &halt_smi_msg
, &halt_recv_msg
, 0);
135 return halt_recv_msg
.msg
.data
[0];
142 #define IPMI_NETFN_ATCA 0x2c
143 #define IPMI_ATCA_SET_POWER_CMD 0x11
144 #define IPMI_ATCA_GET_ADDR_INFO_CMD 0x01
145 #define IPMI_PICMG_ID 0
147 static int ipmi_atca_detect (ipmi_user_t user
)
149 struct ipmi_system_interface_addr smi_addr
;
150 struct kernel_ipmi_msg send_msg
;
152 unsigned char data
[1];
155 * Configure IPMI address for local access
157 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
158 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
162 * Use get address info to check and see if we are ATCA
164 send_msg
.netfn
= IPMI_NETFN_ATCA
;
165 send_msg
.cmd
= IPMI_ATCA_GET_ADDR_INFO_CMD
;
166 data
[0] = IPMI_PICMG_ID
;
167 send_msg
.data
= data
;
168 send_msg
.data_len
= sizeof(data
);
169 rv
= ipmi_request_wait_for_response(user
,
170 (struct ipmi_addr
*) &smi_addr
,
175 static void ipmi_poweroff_atca (ipmi_user_t user
)
177 struct ipmi_system_interface_addr smi_addr
;
178 struct kernel_ipmi_msg send_msg
;
180 unsigned char data
[4];
183 * Configure IPMI address for local access
185 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
186 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
189 printk(KERN_INFO PFX
"Powering down via ATCA power command\n");
194 send_msg
.netfn
= IPMI_NETFN_ATCA
;
195 send_msg
.cmd
= IPMI_ATCA_SET_POWER_CMD
;
196 data
[0] = IPMI_PICMG_ID
;
197 data
[1] = 0; /* FRU id */
198 data
[2] = 0; /* Power Level */
199 data
[3] = 0; /* Don't change saved presets */
200 send_msg
.data
= data
;
201 send_msg
.data_len
= sizeof (data
);
202 rv
= ipmi_request_in_rc_mode(user
,
203 (struct ipmi_addr
*) &smi_addr
,
206 printk(KERN_ERR PFX
"Unable to send ATCA powerdown message,"
207 " IPMI error 0x%x\n", rv
);
219 #define IPMI_NETFN_OEM_1 0xf8
220 #define OEM_GRP_CMD_SET_RESET_STATE 0x84
221 #define OEM_GRP_CMD_SET_POWER_STATE 0x82
222 #define IPMI_NETFN_OEM_8 0xf8
223 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
224 #define OEM_GRP_CMD_GET_SLOT_GA 0xa3
225 #define IPMI_NETFN_SENSOR_EVT 0x10
226 #define IPMI_CMD_GET_EVENT_RECEIVER 0x01
228 #define IPMI_CPI1_PRODUCT_ID 0x000157
229 #define IPMI_CPI1_MANUFACTURER_ID 0x0108
231 static int ipmi_cpi1_detect (ipmi_user_t user
)
233 return ((mfg_id
== IPMI_CPI1_MANUFACTURER_ID
)
234 && (prod_id
== IPMI_CPI1_PRODUCT_ID
));
237 static void ipmi_poweroff_cpi1 (ipmi_user_t user
)
239 struct ipmi_system_interface_addr smi_addr
;
240 struct ipmi_ipmb_addr ipmb_addr
;
241 struct kernel_ipmi_msg send_msg
;
243 unsigned char data
[1];
245 unsigned char hotswap_ipmb
;
246 unsigned char aer_addr
;
247 unsigned char aer_lun
;
250 * Configure IPMI address for local access
252 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
253 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
256 printk(KERN_INFO PFX
"Powering down via CPI1 power command\n");
259 * Get IPMI ipmb address
261 send_msg
.netfn
= IPMI_NETFN_OEM_8
>> 2;
262 send_msg
.cmd
= OEM_GRP_CMD_GET_SLOT_GA
;
263 send_msg
.data
= NULL
;
264 send_msg
.data_len
= 0;
265 rv
= ipmi_request_in_rc_mode(user
,
266 (struct ipmi_addr
*) &smi_addr
,
270 slot
= halt_recv_msg
.msg
.data
[1];
271 hotswap_ipmb
= (slot
> 9) ? (0xb0 + 2 * slot
) : (0xae + 2 * slot
);
274 * Get active event receiver
276 send_msg
.netfn
= IPMI_NETFN_SENSOR_EVT
>> 2;
277 send_msg
.cmd
= IPMI_CMD_GET_EVENT_RECEIVER
;
278 send_msg
.data
= NULL
;
279 send_msg
.data_len
= 0;
280 rv
= ipmi_request_in_rc_mode(user
,
281 (struct ipmi_addr
*) &smi_addr
,
285 aer_addr
= halt_recv_msg
.msg
.data
[1];
286 aer_lun
= halt_recv_msg
.msg
.data
[2];
289 * Setup IPMB address target instead of local target
291 ipmb_addr
.addr_type
= IPMI_IPMB_ADDR_TYPE
;
292 ipmb_addr
.channel
= 0;
293 ipmb_addr
.slave_addr
= aer_addr
;
294 ipmb_addr
.lun
= aer_lun
;
297 * Send request hotswap control to remove blade from dpv
299 send_msg
.netfn
= IPMI_NETFN_OEM_8
>> 2;
300 send_msg
.cmd
= OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL
;
301 send_msg
.data
= &hotswap_ipmb
;
302 send_msg
.data_len
= 1;
303 ipmi_request_in_rc_mode(user
,
304 (struct ipmi_addr
*) &ipmb_addr
,
310 send_msg
.netfn
= IPMI_NETFN_OEM_1
>> 2;
311 send_msg
.cmd
= OEM_GRP_CMD_SET_RESET_STATE
;
312 send_msg
.data
= data
;
313 data
[0] = 1; /* Reset asserted state */
314 send_msg
.data_len
= 1;
315 rv
= ipmi_request_in_rc_mode(user
,
316 (struct ipmi_addr
*) &smi_addr
,
324 send_msg
.netfn
= IPMI_NETFN_OEM_1
>> 2;
325 send_msg
.cmd
= OEM_GRP_CMD_SET_POWER_STATE
;
326 send_msg
.data
= data
;
327 data
[0] = 1; /* Power down state */
328 send_msg
.data_len
= 1;
329 rv
= ipmi_request_in_rc_mode(user
,
330 (struct ipmi_addr
*) &smi_addr
,
340 * Standard chassis support
343 #define IPMI_NETFN_CHASSIS_REQUEST 0
344 #define IPMI_CHASSIS_CONTROL_CMD 0x02
346 static int ipmi_chassis_detect (ipmi_user_t user
)
348 /* Chassis support, use it. */
349 return (capabilities
& 0x80);
352 static void ipmi_poweroff_chassis (ipmi_user_t user
)
354 struct ipmi_system_interface_addr smi_addr
;
355 struct kernel_ipmi_msg send_msg
;
357 unsigned char data
[1];
360 * Configure IPMI address for local access
362 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
363 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
367 printk(KERN_INFO PFX
"Powering %s via IPMI chassis control command\n",
368 ((poweroff_control
!= IPMI_CHASSIS_POWER_CYCLE
) ? "down" : "cycle"));
373 send_msg
.netfn
= IPMI_NETFN_CHASSIS_REQUEST
;
374 send_msg
.cmd
= IPMI_CHASSIS_CONTROL_CMD
;
375 data
[0] = poweroff_control
;
376 send_msg
.data
= data
;
377 send_msg
.data_len
= sizeof(data
);
378 rv
= ipmi_request_in_rc_mode(user
,
379 (struct ipmi_addr
*) &smi_addr
,
382 switch (poweroff_control
) {
383 case IPMI_CHASSIS_POWER_CYCLE
:
384 /* power cycle failed, default to power down */
385 printk(KERN_ERR PFX
"Unable to send chassis power " \
386 "cycle message, IPMI error 0x%x\n", rv
);
387 poweroff_control
= IPMI_CHASSIS_POWER_DOWN
;
388 goto powercyclefailed
;
390 case IPMI_CHASSIS_POWER_DOWN
:
392 printk(KERN_ERR PFX
"Unable to send chassis power " \
393 "down message, IPMI error 0x%x\n", rv
);
402 /* Table of possible power off functions. */
403 struct poweroff_function
{
405 int (*detect
)(ipmi_user_t user
);
406 void (*poweroff_func
)(ipmi_user_t user
);
409 static struct poweroff_function poweroff_functions
[] = {
410 { .platform_type
= "ATCA",
411 .detect
= ipmi_atca_detect
,
412 .poweroff_func
= ipmi_poweroff_atca
},
413 { .platform_type
= "CPI1",
414 .detect
= ipmi_cpi1_detect
,
415 .poweroff_func
= ipmi_poweroff_cpi1
},
416 /* Chassis should generally be last, other things should override
418 { .platform_type
= "chassis",
419 .detect
= ipmi_chassis_detect
,
420 .poweroff_func
= ipmi_poweroff_chassis
},
422 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
423 / sizeof(struct poweroff_function))
426 /* Our local state. */
427 static int ready
= 0;
428 static ipmi_user_t ipmi_user
;
429 static void (*specific_poweroff_func
)(ipmi_user_t user
) = NULL
;
431 /* Holds the old poweroff function so we can restore it on removal. */
432 static void (*old_poweroff_func
)(void);
435 /* Called on a powerdown request. */
436 static void ipmi_poweroff_function (void)
441 /* Use run-to-completion mode, since interrupts may be off. */
442 ipmi_user_set_run_to_completion(ipmi_user
, 1);
443 specific_poweroff_func(ipmi_user
);
444 ipmi_user_set_run_to_completion(ipmi_user
, 0);
447 /* Wait for an IPMI interface to be installed, the first one installed
448 will be grabbed by this code and used to perform the powerdown. */
449 static void ipmi_po_new_smi(int if_num
)
451 struct ipmi_system_interface_addr smi_addr
;
452 struct kernel_ipmi_msg send_msg
;
459 rv
= ipmi_create_user(if_num
, &ipmi_poweroff_handler
, NULL
,
462 printk(KERN_ERR PFX
"could not create IPMI user, error %d\n",
468 * Do a get device ide and store some results, since this is
469 * used by several functions.
471 smi_addr
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
472 smi_addr
.channel
= IPMI_BMC_CHANNEL
;
475 send_msg
.netfn
= IPMI_NETFN_APP_REQUEST
;
476 send_msg
.cmd
= IPMI_GET_DEVICE_ID_CMD
;
477 send_msg
.data
= NULL
;
478 send_msg
.data_len
= 0;
479 rv
= ipmi_request_wait_for_response(ipmi_user
,
480 (struct ipmi_addr
*) &smi_addr
,
483 printk(KERN_ERR PFX
"Unable to send IPMI get device id info,"
484 " IPMI error 0x%x\n", rv
);
488 if (halt_recv_msg
.msg
.data_len
< 12) {
489 printk(KERN_ERR PFX
"(chassis) IPMI get device id info too,"
490 " short, was %d bytes, needed %d bytes\n",
491 halt_recv_msg
.msg
.data_len
, 12);
495 mfg_id
= (halt_recv_msg
.msg
.data
[7]
496 | (halt_recv_msg
.msg
.data
[8] << 8)
497 | (halt_recv_msg
.msg
.data
[9] << 16));
498 prod_id
= (halt_recv_msg
.msg
.data
[10]
499 | (halt_recv_msg
.msg
.data
[11] << 8));
500 capabilities
= halt_recv_msg
.msg
.data
[6];
503 /* Scan for a poweroff method */
504 for (i
=0; i
<NUM_PO_FUNCS
; i
++) {
505 if (poweroff_functions
[i
].detect(ipmi_user
))
510 printk(KERN_ERR PFX
"Unable to find a poweroff function that"
511 " will work, giving up\n");
512 ipmi_destroy_user(ipmi_user
);
516 printk(KERN_INFO PFX
"Found a %s style poweroff function\n",
517 poweroff_functions
[i
].platform_type
);
518 specific_poweroff_func
= poweroff_functions
[i
].poweroff_func
;
519 old_poweroff_func
= pm_power_off
;
520 pm_power_off
= ipmi_poweroff_function
;
524 static void ipmi_po_smi_gone(int if_num
)
526 /* This can never be called, because once poweroff driver is
527 registered, the interface can't go away until the power
528 driver is unregistered. */
531 static struct ipmi_smi_watcher smi_watcher
=
533 .owner
= THIS_MODULE
,
534 .new_smi
= ipmi_po_new_smi
,
535 .smi_gone
= ipmi_po_smi_gone
539 #ifdef CONFIG_PROC_FS
540 /* displays properties to proc */
541 static int proc_read_chassctrl(char *page
, char **start
, off_t off
, int count
,
542 int *eof
, void *data
)
544 return sprintf(page
, "%d\t[ 0=powerdown 2=powercycle ]\n",
548 /* process property writes from proc */
549 static int proc_write_chassctrl(struct file
*file
, const char *buffer
,
550 unsigned long count
, void *data
)
553 unsigned int newval
= 0;
555 sscanf(buffer
, "%d", &newval
);
557 case IPMI_CHASSIS_POWER_CYCLE
:
558 printk(KERN_INFO PFX
"power cycle is now enabled\n");
559 poweroff_control
= newval
;
562 case IPMI_CHASSIS_POWER_DOWN
:
563 poweroff_control
= IPMI_CHASSIS_POWER_DOWN
;
573 #endif /* CONFIG_PROC_FS */
576 * Startup and shutdown functions.
578 static int ipmi_poweroff_init (void)
581 struct proc_dir_entry
*file
;
583 printk ("Copyright (C) 2004 MontaVista Software -"
584 " IPMI Powerdown via sys_reboot version "
585 IPMI_POWEROFF_VERSION
".\n");
587 switch (poweroff_control
) {
588 case IPMI_CHASSIS_POWER_CYCLE
:
589 printk(KERN_INFO PFX
"Power cycle is enabled.\n");
592 case IPMI_CHASSIS_POWER_DOWN
:
594 poweroff_control
= IPMI_CHASSIS_POWER_DOWN
;
598 rv
= ipmi_smi_watcher_register(&smi_watcher
);
600 printk(KERN_ERR PFX
"Unable to register SMI watcher: %d\n", rv
);
604 #ifdef CONFIG_PROC_FS
605 file
= create_proc_entry("poweroff_control", 0, proc_ipmi_root
);
607 printk(KERN_ERR PFX
"Unable to create proc power control\n");
610 file
->read_proc
= proc_read_chassctrl
;
611 file
->write_proc
= proc_write_chassctrl
;
612 file
->owner
= THIS_MODULE
;
621 static __exit
void ipmi_poweroff_cleanup(void)
625 #ifdef CONFIG_PROC_FS
626 remove_proc_entry("poweroff_control", proc_ipmi_root
);
629 ipmi_smi_watcher_unregister(&smi_watcher
);
632 rv
= ipmi_destroy_user(ipmi_user
);
634 printk(KERN_ERR PFX
"could not cleanup the IPMI"
635 " user: 0x%x\n", rv
);
636 pm_power_off
= old_poweroff_func
;
639 module_exit(ipmi_poweroff_cleanup
);
642 module_init(ipmi_poweroff_init
);
643 MODULE_LICENSE("GPL");