Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ipmi_poweroff.c |
| 3 | * |
| 4 | * MontaVista IPMI Poweroff extension to sys_reboot |
| 5 | * |
| 6 | * Author: MontaVista Software, Inc. |
| 7 | * Steven Dake <sdake@mvista.com> |
| 8 | * Corey Minyard <cminyard@mvista.com> |
| 9 | * source@mvista.com |
| 10 | * |
| 11 | * Copyright 2002,2004 MontaVista Software Inc. |
| 12 | * |
| 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. |
| 17 | * |
| 18 | * |
| 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. |
| 29 | * |
| 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. |
| 33 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 35 | #include <linux/moduleparam.h> |
| 36 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/string.h> |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 38 | #include <linux/completion.h> |
Olaf Hering | e933b6d | 2006-03-24 03:16:15 -0800 | [diff] [blame] | 39 | #include <linux/pm.h> |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 40 | #include <linux/kdev_t.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/ipmi.h> |
| 42 | #include <linux/ipmi_smi.h> |
| 43 | |
| 44 | #define PFX "IPMI poweroff: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Corey Minyard | b2c0394 | 2006-12-06 20:41:00 -0800 | [diff] [blame^] | 46 | static void ipmi_po_smi_gone(int if_num); |
| 47 | static void ipmi_po_new_smi(int if_num, struct device *device); |
| 48 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 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 */ |
| 53 | |
| 54 | /* the IPMI data command */ |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 55 | static int poweroff_powercycle; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 56 | |
Corey Minyard | b2c0394 | 2006-12-06 20:41:00 -0800 | [diff] [blame^] | 57 | /* Which interface to use, -1 means the first we see. */ |
| 58 | static int ifnum_to_use = -1; |
| 59 | |
| 60 | /* Our local state. */ |
| 61 | static int ready = 0; |
| 62 | static ipmi_user_t ipmi_user; |
| 63 | static int ipmi_ifnum; |
| 64 | static void (*specific_poweroff_func)(ipmi_user_t user) = NULL; |
| 65 | |
| 66 | /* Holds the old poweroff function so we can restore it on removal. */ |
| 67 | static void (*old_poweroff_func)(void); |
| 68 | |
| 69 | static int set_param_ifnum(const char *val, struct kernel_param *kp) |
| 70 | { |
| 71 | int rv = param_set_int(val, kp); |
| 72 | if (rv) |
| 73 | return rv; |
| 74 | if ((ifnum_to_use < 0) || (ifnum_to_use == ipmi_ifnum)) |
| 75 | return 0; |
| 76 | |
| 77 | ipmi_po_smi_gone(ipmi_ifnum); |
| 78 | ipmi_po_new_smi(ifnum_to_use, NULL); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | module_param_call(ifnum_to_use, set_param_ifnum, param_get_int, |
| 83 | &ifnum_to_use, 0644); |
| 84 | MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog " |
| 85 | "timer. Setting to -1 defaults to the first registered " |
| 86 | "interface"); |
| 87 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 88 | /* parameter definition to allow user to flag power cycle */ |
Corey Minyard | a9d014a | 2005-09-27 21:45:35 -0700 | [diff] [blame] | 89 | module_param(poweroff_powercycle, int, 0644); |
Corey Minyard | 21d6c54 | 2005-11-07 00:59:57 -0800 | [diff] [blame] | 90 | 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."); |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* Stuff from the get device id command. */ |
| 93 | static unsigned int mfg_id; |
| 94 | static unsigned int prod_id; |
| 95 | static unsigned char capabilities; |
Corey Minyard | 168524d | 2005-09-06 15:18:43 -0700 | [diff] [blame] | 96 | static unsigned char ipmi_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* We use our own messages for this operation, we don't let the system |
| 99 | allocate them, since we may be in a panic situation. The whole |
| 100 | thing is single-threaded, anyway, so multiple messages are not |
| 101 | required. */ |
| 102 | static void dummy_smi_free(struct ipmi_smi_msg *msg) |
| 103 | { |
| 104 | } |
| 105 | static void dummy_recv_free(struct ipmi_recv_msg *msg) |
| 106 | { |
| 107 | } |
| 108 | static struct ipmi_smi_msg halt_smi_msg = |
| 109 | { |
| 110 | .done = dummy_smi_free |
| 111 | }; |
| 112 | static struct ipmi_recv_msg halt_recv_msg = |
| 113 | { |
| 114 | .done = dummy_recv_free |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | /* |
| 119 | * Code to send a message and wait for the reponse. |
| 120 | */ |
| 121 | |
| 122 | static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data) |
| 123 | { |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 124 | struct completion *comp = recv_msg->user_msg_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 126 | if (comp) |
| 127 | complete(comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static struct ipmi_user_hndl ipmi_poweroff_handler = |
| 131 | { |
| 132 | .ipmi_recv_hndl = receive_handler |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | static int ipmi_request_wait_for_response(ipmi_user_t user, |
| 137 | struct ipmi_addr *addr, |
| 138 | struct kernel_ipmi_msg *send_msg) |
| 139 | { |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 140 | int rv; |
| 141 | struct completion comp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 143 | init_completion(&comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 145 | rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | &halt_smi_msg, &halt_recv_msg, 0); |
| 147 | if (rv) |
| 148 | return rv; |
| 149 | |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 150 | wait_for_completion(&comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | return halt_recv_msg.msg.data[0]; |
| 153 | } |
| 154 | |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 155 | /* We are in run-to-completion mode, no completion is desired. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | static int ipmi_request_in_rc_mode(ipmi_user_t user, |
| 157 | struct ipmi_addr *addr, |
| 158 | struct kernel_ipmi_msg *send_msg) |
| 159 | { |
Corey Minyard | 77cf397 | 2005-06-23 22:01:44 -0700 | [diff] [blame] | 160 | int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL, |
| 163 | &halt_smi_msg, &halt_recv_msg, 0); |
| 164 | if (rv) |
| 165 | return rv; |
| 166 | |
| 167 | return halt_recv_msg.msg.data[0]; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * ATCA Support |
| 172 | */ |
| 173 | |
| 174 | #define IPMI_NETFN_ATCA 0x2c |
| 175 | #define IPMI_ATCA_SET_POWER_CMD 0x11 |
| 176 | #define IPMI_ATCA_GET_ADDR_INFO_CMD 0x01 |
| 177 | #define IPMI_PICMG_ID 0 |
| 178 | |
| 179 | static int ipmi_atca_detect (ipmi_user_t user) |
| 180 | { |
| 181 | struct ipmi_system_interface_addr smi_addr; |
| 182 | struct kernel_ipmi_msg send_msg; |
| 183 | int rv; |
| 184 | unsigned char data[1]; |
| 185 | |
| 186 | /* |
| 187 | * Configure IPMI address for local access |
| 188 | */ |
| 189 | smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 190 | smi_addr.channel = IPMI_BMC_CHANNEL; |
| 191 | smi_addr.lun = 0; |
| 192 | |
| 193 | /* |
| 194 | * Use get address info to check and see if we are ATCA |
| 195 | */ |
| 196 | send_msg.netfn = IPMI_NETFN_ATCA; |
| 197 | send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD; |
| 198 | data[0] = IPMI_PICMG_ID; |
| 199 | send_msg.data = data; |
| 200 | send_msg.data_len = sizeof(data); |
| 201 | rv = ipmi_request_wait_for_response(user, |
| 202 | (struct ipmi_addr *) &smi_addr, |
| 203 | &send_msg); |
| 204 | return !rv; |
| 205 | } |
| 206 | |
| 207 | static void ipmi_poweroff_atca (ipmi_user_t user) |
| 208 | { |
| 209 | struct ipmi_system_interface_addr smi_addr; |
| 210 | struct kernel_ipmi_msg send_msg; |
| 211 | int rv; |
| 212 | unsigned char data[4]; |
| 213 | |
| 214 | /* |
| 215 | * Configure IPMI address for local access |
| 216 | */ |
| 217 | smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 218 | smi_addr.channel = IPMI_BMC_CHANNEL; |
| 219 | smi_addr.lun = 0; |
| 220 | |
| 221 | printk(KERN_INFO PFX "Powering down via ATCA power command\n"); |
| 222 | |
| 223 | /* |
| 224 | * Power down |
| 225 | */ |
| 226 | send_msg.netfn = IPMI_NETFN_ATCA; |
| 227 | send_msg.cmd = IPMI_ATCA_SET_POWER_CMD; |
| 228 | data[0] = IPMI_PICMG_ID; |
| 229 | data[1] = 0; /* FRU id */ |
| 230 | data[2] = 0; /* Power Level */ |
| 231 | data[3] = 0; /* Don't change saved presets */ |
| 232 | send_msg.data = data; |
| 233 | send_msg.data_len = sizeof (data); |
| 234 | rv = ipmi_request_in_rc_mode(user, |
| 235 | (struct ipmi_addr *) &smi_addr, |
| 236 | &send_msg); |
| 237 | if (rv) { |
| 238 | printk(KERN_ERR PFX "Unable to send ATCA powerdown message," |
| 239 | " IPMI error 0x%x\n", rv); |
| 240 | goto out; |
| 241 | } |
| 242 | |
| 243 | out: |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * CPI1 Support |
| 249 | */ |
| 250 | |
| 251 | #define IPMI_NETFN_OEM_1 0xf8 |
| 252 | #define OEM_GRP_CMD_SET_RESET_STATE 0x84 |
| 253 | #define OEM_GRP_CMD_SET_POWER_STATE 0x82 |
| 254 | #define IPMI_NETFN_OEM_8 0xf8 |
| 255 | #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80 |
| 256 | #define OEM_GRP_CMD_GET_SLOT_GA 0xa3 |
| 257 | #define IPMI_NETFN_SENSOR_EVT 0x10 |
| 258 | #define IPMI_CMD_GET_EVENT_RECEIVER 0x01 |
| 259 | |
| 260 | #define IPMI_CPI1_PRODUCT_ID 0x000157 |
| 261 | #define IPMI_CPI1_MANUFACTURER_ID 0x0108 |
| 262 | |
| 263 | static int ipmi_cpi1_detect (ipmi_user_t user) |
| 264 | { |
| 265 | return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID) |
| 266 | && (prod_id == IPMI_CPI1_PRODUCT_ID)); |
| 267 | } |
| 268 | |
| 269 | static void ipmi_poweroff_cpi1 (ipmi_user_t user) |
| 270 | { |
| 271 | struct ipmi_system_interface_addr smi_addr; |
| 272 | struct ipmi_ipmb_addr ipmb_addr; |
| 273 | struct kernel_ipmi_msg send_msg; |
| 274 | int rv; |
| 275 | unsigned char data[1]; |
| 276 | int slot; |
| 277 | unsigned char hotswap_ipmb; |
| 278 | unsigned char aer_addr; |
| 279 | unsigned char aer_lun; |
| 280 | |
| 281 | /* |
| 282 | * Configure IPMI address for local access |
| 283 | */ |
| 284 | smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 285 | smi_addr.channel = IPMI_BMC_CHANNEL; |
| 286 | smi_addr.lun = 0; |
| 287 | |
| 288 | printk(KERN_INFO PFX "Powering down via CPI1 power command\n"); |
| 289 | |
| 290 | /* |
| 291 | * Get IPMI ipmb address |
| 292 | */ |
| 293 | send_msg.netfn = IPMI_NETFN_OEM_8 >> 2; |
| 294 | send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA; |
| 295 | send_msg.data = NULL; |
| 296 | send_msg.data_len = 0; |
| 297 | rv = ipmi_request_in_rc_mode(user, |
| 298 | (struct ipmi_addr *) &smi_addr, |
| 299 | &send_msg); |
| 300 | if (rv) |
| 301 | goto out; |
| 302 | slot = halt_recv_msg.msg.data[1]; |
| 303 | hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot); |
| 304 | |
| 305 | /* |
| 306 | * Get active event receiver |
| 307 | */ |
| 308 | send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2; |
| 309 | send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER; |
| 310 | send_msg.data = NULL; |
| 311 | send_msg.data_len = 0; |
| 312 | rv = ipmi_request_in_rc_mode(user, |
| 313 | (struct ipmi_addr *) &smi_addr, |
| 314 | &send_msg); |
| 315 | if (rv) |
| 316 | goto out; |
| 317 | aer_addr = halt_recv_msg.msg.data[1]; |
| 318 | aer_lun = halt_recv_msg.msg.data[2]; |
| 319 | |
| 320 | /* |
| 321 | * Setup IPMB address target instead of local target |
| 322 | */ |
| 323 | ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE; |
| 324 | ipmb_addr.channel = 0; |
| 325 | ipmb_addr.slave_addr = aer_addr; |
| 326 | ipmb_addr.lun = aer_lun; |
| 327 | |
| 328 | /* |
| 329 | * Send request hotswap control to remove blade from dpv |
| 330 | */ |
| 331 | send_msg.netfn = IPMI_NETFN_OEM_8 >> 2; |
| 332 | send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL; |
| 333 | send_msg.data = &hotswap_ipmb; |
| 334 | send_msg.data_len = 1; |
| 335 | ipmi_request_in_rc_mode(user, |
| 336 | (struct ipmi_addr *) &ipmb_addr, |
| 337 | &send_msg); |
| 338 | |
| 339 | /* |
| 340 | * Set reset asserted |
| 341 | */ |
| 342 | send_msg.netfn = IPMI_NETFN_OEM_1 >> 2; |
| 343 | send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE; |
| 344 | send_msg.data = data; |
| 345 | data[0] = 1; /* Reset asserted state */ |
| 346 | send_msg.data_len = 1; |
| 347 | rv = ipmi_request_in_rc_mode(user, |
| 348 | (struct ipmi_addr *) &smi_addr, |
| 349 | &send_msg); |
| 350 | if (rv) |
| 351 | goto out; |
| 352 | |
| 353 | /* |
| 354 | * Power down |
| 355 | */ |
| 356 | send_msg.netfn = IPMI_NETFN_OEM_1 >> 2; |
| 357 | send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE; |
| 358 | send_msg.data = data; |
| 359 | data[0] = 1; /* Power down state */ |
| 360 | send_msg.data_len = 1; |
| 361 | rv = ipmi_request_in_rc_mode(user, |
| 362 | (struct ipmi_addr *) &smi_addr, |
| 363 | &send_msg); |
| 364 | if (rv) |
| 365 | goto out; |
| 366 | |
| 367 | out: |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | /* |
Corey Minyard | 168524d | 2005-09-06 15:18:43 -0700 | [diff] [blame] | 372 | * ipmi_dell_chassis_detect() |
| 373 | * Dell systems with IPMI < 1.5 don't set the chassis capability bit |
| 374 | * but they can handle a chassis poweroff or powercycle command. |
| 375 | */ |
| 376 | |
| 377 | #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00} |
| 378 | static int ipmi_dell_chassis_detect (ipmi_user_t user) |
| 379 | { |
| 380 | const char ipmi_version_major = ipmi_version & 0xF; |
| 381 | const char ipmi_version_minor = (ipmi_version >> 4) & 0xF; |
Corey Minyard | 8a3628d | 2006-03-31 02:30:40 -0800 | [diff] [blame] | 382 | const char mfr[3] = DELL_IANA_MFR_ID; |
Corey Minyard | 168524d | 2005-09-06 15:18:43 -0700 | [diff] [blame] | 383 | if (!memcmp(mfr, &mfg_id, sizeof(mfr)) && |
| 384 | ipmi_version_major <= 1 && |
| 385 | ipmi_version_minor < 5) |
| 386 | return 1; |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * Standard chassis support |
| 392 | */ |
| 393 | |
| 394 | #define IPMI_NETFN_CHASSIS_REQUEST 0 |
| 395 | #define IPMI_CHASSIS_CONTROL_CMD 0x02 |
| 396 | |
| 397 | static int ipmi_chassis_detect (ipmi_user_t user) |
| 398 | { |
| 399 | /* Chassis support, use it. */ |
| 400 | return (capabilities & 0x80); |
| 401 | } |
| 402 | |
| 403 | static void ipmi_poweroff_chassis (ipmi_user_t user) |
| 404 | { |
| 405 | struct ipmi_system_interface_addr smi_addr; |
| 406 | struct kernel_ipmi_msg send_msg; |
| 407 | int rv; |
| 408 | unsigned char data[1]; |
| 409 | |
| 410 | /* |
| 411 | * Configure IPMI address for local access |
| 412 | */ |
| 413 | smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 414 | smi_addr.channel = IPMI_BMC_CHANNEL; |
| 415 | smi_addr.lun = 0; |
| 416 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 417 | powercyclefailed: |
| 418 | printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n", |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 419 | (poweroff_powercycle ? "cycle" : "down")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * Power down |
| 423 | */ |
| 424 | send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST; |
| 425 | send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD; |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 426 | if (poweroff_powercycle) |
| 427 | data[0] = IPMI_CHASSIS_POWER_CYCLE; |
| 428 | else |
| 429 | data[0] = IPMI_CHASSIS_POWER_DOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | send_msg.data = data; |
| 431 | send_msg.data_len = sizeof(data); |
| 432 | rv = ipmi_request_in_rc_mode(user, |
| 433 | (struct ipmi_addr *) &smi_addr, |
| 434 | &send_msg); |
| 435 | if (rv) { |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 436 | if (poweroff_powercycle) { |
| 437 | /* power cycle failed, default to power down */ |
| 438 | printk(KERN_ERR PFX "Unable to send chassis power " \ |
| 439 | "cycle message, IPMI error 0x%x\n", rv); |
| 440 | poweroff_powercycle = 0; |
| 441 | goto powercyclefailed; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 444 | printk(KERN_ERR PFX "Unable to send chassis power " \ |
| 445 | "down message, IPMI error 0x%x\n", rv); |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | |
| 450 | /* Table of possible power off functions. */ |
| 451 | struct poweroff_function { |
| 452 | char *platform_type; |
| 453 | int (*detect)(ipmi_user_t user); |
| 454 | void (*poweroff_func)(ipmi_user_t user); |
| 455 | }; |
| 456 | |
| 457 | static struct poweroff_function poweroff_functions[] = { |
| 458 | { .platform_type = "ATCA", |
| 459 | .detect = ipmi_atca_detect, |
| 460 | .poweroff_func = ipmi_poweroff_atca }, |
| 461 | { .platform_type = "CPI1", |
| 462 | .detect = ipmi_cpi1_detect, |
| 463 | .poweroff_func = ipmi_poweroff_cpi1 }, |
Corey Minyard | 168524d | 2005-09-06 15:18:43 -0700 | [diff] [blame] | 464 | { .platform_type = "chassis", |
| 465 | .detect = ipmi_dell_chassis_detect, |
| 466 | .poweroff_func = ipmi_poweroff_chassis }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* Chassis should generally be last, other things should override |
| 468 | it. */ |
| 469 | { .platform_type = "chassis", |
| 470 | .detect = ipmi_chassis_detect, |
| 471 | .poweroff_func = ipmi_poweroff_chassis }, |
| 472 | }; |
| 473 | #define NUM_PO_FUNCS (sizeof(poweroff_functions) \ |
| 474 | / sizeof(struct poweroff_function)) |
| 475 | |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | /* Called on a powerdown request. */ |
| 478 | static void ipmi_poweroff_function (void) |
| 479 | { |
| 480 | if (!ready) |
| 481 | return; |
| 482 | |
| 483 | /* Use run-to-completion mode, since interrupts may be off. */ |
| 484 | ipmi_user_set_run_to_completion(ipmi_user, 1); |
| 485 | specific_poweroff_func(ipmi_user); |
| 486 | ipmi_user_set_run_to_completion(ipmi_user, 0); |
| 487 | } |
| 488 | |
| 489 | /* Wait for an IPMI interface to be installed, the first one installed |
| 490 | will be grabbed by this code and used to perform the powerdown. */ |
Corey Minyard | 50c812b | 2006-03-26 01:37:21 -0800 | [diff] [blame] | 491 | static void ipmi_po_new_smi(int if_num, struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | struct ipmi_system_interface_addr smi_addr; |
| 494 | struct kernel_ipmi_msg send_msg; |
| 495 | int rv; |
| 496 | int i; |
| 497 | |
| 498 | if (ready) |
| 499 | return; |
| 500 | |
Corey Minyard | b2c0394 | 2006-12-06 20:41:00 -0800 | [diff] [blame^] | 501 | if ((ifnum_to_use >= 0) && (ifnum_to_use != if_num)) |
| 502 | return; |
| 503 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 504 | rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL, |
| 505 | &ipmi_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (rv) { |
| 507 | printk(KERN_ERR PFX "could not create IPMI user, error %d\n", |
| 508 | rv); |
| 509 | return; |
| 510 | } |
| 511 | |
Corey Minyard | b2c0394 | 2006-12-06 20:41:00 -0800 | [diff] [blame^] | 512 | ipmi_ifnum = if_num; |
| 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /* |
| 515 | * Do a get device ide and store some results, since this is |
| 516 | * used by several functions. |
| 517 | */ |
| 518 | smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 519 | smi_addr.channel = IPMI_BMC_CHANNEL; |
| 520 | smi_addr.lun = 0; |
| 521 | |
| 522 | send_msg.netfn = IPMI_NETFN_APP_REQUEST; |
| 523 | send_msg.cmd = IPMI_GET_DEVICE_ID_CMD; |
| 524 | send_msg.data = NULL; |
| 525 | send_msg.data_len = 0; |
| 526 | rv = ipmi_request_wait_for_response(ipmi_user, |
| 527 | (struct ipmi_addr *) &smi_addr, |
| 528 | &send_msg); |
| 529 | if (rv) { |
| 530 | printk(KERN_ERR PFX "Unable to send IPMI get device id info," |
| 531 | " IPMI error 0x%x\n", rv); |
| 532 | goto out_err; |
| 533 | } |
| 534 | |
| 535 | if (halt_recv_msg.msg.data_len < 12) { |
| 536 | printk(KERN_ERR PFX "(chassis) IPMI get device id info too," |
| 537 | " short, was %d bytes, needed %d bytes\n", |
| 538 | halt_recv_msg.msg.data_len, 12); |
| 539 | goto out_err; |
| 540 | } |
| 541 | |
| 542 | mfg_id = (halt_recv_msg.msg.data[7] |
| 543 | | (halt_recv_msg.msg.data[8] << 8) |
| 544 | | (halt_recv_msg.msg.data[9] << 16)); |
| 545 | prod_id = (halt_recv_msg.msg.data[10] |
| 546 | | (halt_recv_msg.msg.data[11] << 8)); |
| 547 | capabilities = halt_recv_msg.msg.data[6]; |
Corey Minyard | 168524d | 2005-09-06 15:18:43 -0700 | [diff] [blame] | 548 | ipmi_version = halt_recv_msg.msg.data[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | |
| 551 | /* Scan for a poweroff method */ |
Corey Minyard | e8b3361 | 2005-09-06 15:18:45 -0700 | [diff] [blame] | 552 | for (i = 0; i < NUM_PO_FUNCS; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (poweroff_functions[i].detect(ipmi_user)) |
| 554 | goto found; |
| 555 | } |
| 556 | |
| 557 | out_err: |
| 558 | printk(KERN_ERR PFX "Unable to find a poweroff function that" |
| 559 | " will work, giving up\n"); |
| 560 | ipmi_destroy_user(ipmi_user); |
| 561 | return; |
| 562 | |
| 563 | found: |
| 564 | printk(KERN_INFO PFX "Found a %s style poweroff function\n", |
| 565 | poweroff_functions[i].platform_type); |
| 566 | specific_poweroff_func = poweroff_functions[i].poweroff_func; |
| 567 | old_poweroff_func = pm_power_off; |
| 568 | pm_power_off = ipmi_poweroff_function; |
| 569 | ready = 1; |
| 570 | } |
| 571 | |
| 572 | static void ipmi_po_smi_gone(int if_num) |
| 573 | { |
Corey Minyard | b2c0394 | 2006-12-06 20:41:00 -0800 | [diff] [blame^] | 574 | if (!ready) |
| 575 | return; |
| 576 | |
| 577 | if (ipmi_ifnum != if_num) |
| 578 | return; |
| 579 | |
| 580 | ready = 0; |
| 581 | ipmi_destroy_user(ipmi_user); |
| 582 | pm_power_off = old_poweroff_func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static struct ipmi_smi_watcher smi_watcher = |
| 586 | { |
| 587 | .owner = THIS_MODULE, |
| 588 | .new_smi = ipmi_po_new_smi, |
| 589 | .smi_gone = ipmi_po_smi_gone |
| 590 | }; |
| 591 | |
| 592 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 593 | #ifdef CONFIG_PROC_FS |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 594 | #include <linux/sysctl.h> |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 595 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 596 | static ctl_table ipmi_table[] = { |
| 597 | { .ctl_name = DEV_IPMI_POWEROFF_POWERCYCLE, |
| 598 | .procname = "poweroff_powercycle", |
| 599 | .data = &poweroff_powercycle, |
| 600 | .maxlen = sizeof(poweroff_powercycle), |
| 601 | .mode = 0644, |
| 602 | .proc_handler = &proc_dointvec }, |
| 603 | { } |
| 604 | }; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 605 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 606 | static ctl_table ipmi_dir_table[] = { |
| 607 | { .ctl_name = DEV_IPMI, |
| 608 | .procname = "ipmi", |
| 609 | .mode = 0555, |
| 610 | .child = ipmi_table }, |
| 611 | { } |
| 612 | }; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 613 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 614 | static ctl_table ipmi_root_table[] = { |
| 615 | { .ctl_name = CTL_DEV, |
| 616 | .procname = "dev", |
| 617 | .mode = 0555, |
| 618 | .child = ipmi_dir_table }, |
| 619 | { } |
| 620 | }; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 621 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 622 | static struct ctl_table_header *ipmi_table_header; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 623 | #endif /* CONFIG_PROC_FS */ |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Startup and shutdown functions. |
| 627 | */ |
| 628 | static int ipmi_poweroff_init (void) |
| 629 | { |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 630 | int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | printk ("Copyright (C) 2004 MontaVista Software -" |
Corey Minyard | 1fdd75b | 2005-09-06 15:18:42 -0700 | [diff] [blame] | 633 | " IPMI Powerdown via sys_reboot.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 635 | if (poweroff_powercycle) |
| 636 | printk(KERN_INFO PFX "Power cycle is enabled.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 638 | #ifdef CONFIG_PROC_FS |
| 639 | ipmi_table_header = register_sysctl_table(ipmi_root_table, 1); |
| 640 | if (!ipmi_table_header) { |
| 641 | printk(KERN_ERR PFX "Unable to register powercycle sysctl\n"); |
| 642 | rv = -ENOMEM; |
| 643 | goto out_err; |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 644 | } |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 645 | #endif |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 646 | |
| 647 | rv = ipmi_smi_watcher_register(&smi_watcher); |
Adrian Bunk | be4f1bb | 2006-01-09 20:51:36 -0800 | [diff] [blame] | 648 | |
| 649 | #ifdef CONFIG_PROC_FS |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 650 | if (rv) { |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 651 | unregister_sysctl_table(ipmi_table_header); |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 652 | printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv); |
| 653 | goto out_err; |
| 654 | } |
Adrian Bunk | be4f1bb | 2006-01-09 20:51:36 -0800 | [diff] [blame] | 655 | #endif |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 656 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 657 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return rv; |
| 659 | } |
| 660 | |
| 661 | #ifdef MODULE |
| 662 | static __exit void ipmi_poweroff_cleanup(void) |
| 663 | { |
| 664 | int rv; |
| 665 | |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 666 | #ifdef CONFIG_PROC_FS |
Corey Minyard | 8c702e1 | 2005-09-06 15:18:46 -0700 | [diff] [blame] | 667 | unregister_sysctl_table(ipmi_table_header); |
Corey Minyard | 3b62594 | 2005-06-23 22:01:42 -0700 | [diff] [blame] | 668 | #endif |
| 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | ipmi_smi_watcher_unregister(&smi_watcher); |
| 671 | |
| 672 | if (ready) { |
| 673 | rv = ipmi_destroy_user(ipmi_user); |
| 674 | if (rv) |
| 675 | printk(KERN_ERR PFX "could not cleanup the IPMI" |
| 676 | " user: 0x%x\n", rv); |
| 677 | pm_power_off = old_poweroff_func; |
| 678 | } |
| 679 | } |
| 680 | module_exit(ipmi_poweroff_cleanup); |
| 681 | #endif |
| 682 | |
| 683 | module_init(ipmi_poweroff_init); |
| 684 | MODULE_LICENSE("GPL"); |
Corey Minyard | 1fdd75b | 2005-09-06 15:18:42 -0700 | [diff] [blame] | 685 | MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>"); |
| 686 | MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot"); |