blob: e02893b7b300907e8a1d18555c155c3b7283c8dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
Corey Minyard3b625942005-06-23 22:01:42 -070035#include <linux/moduleparam.h>
36#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/string.h>
Corey Minyard77cf3972005-06-23 22:01:44 -070038#include <linux/completion.h>
Olaf Heringe933b6d2006-03-24 03:16:15 -080039#include <linux/pm.h>
Corey Minyard77cf3972005-06-23 22:01:44 -070040#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/ipmi.h>
42#include <linux/ipmi_smi.h>
43
44#define PFX "IPMI poweroff: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Corey Minyardb2c03942006-12-06 20:41:00 -080046static void ipmi_po_smi_gone(int if_num);
47static void ipmi_po_new_smi(int if_num, struct device *device);
48
Corey Minyard3b625942005-06-23 22:01:42 -070049/* 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 Minyard8c702e12005-09-06 15:18:46 -070055static int poweroff_powercycle;
Corey Minyard3b625942005-06-23 22:01:42 -070056
Corey Minyardb2c03942006-12-06 20:41:00 -080057/* Which interface to use, -1 means the first we see. */
58static int ifnum_to_use = -1;
59
60/* Our local state. */
Randy Dunlap0c8204b2006-12-10 02:19:06 -080061static int ready;
Corey Minyardb2c03942006-12-06 20:41:00 -080062static ipmi_user_t ipmi_user;
63static int ipmi_ifnum;
Randy Dunlap0c8204b2006-12-10 02:19:06 -080064static void (*specific_poweroff_func)(ipmi_user_t user);
Corey Minyardb2c03942006-12-06 20:41:00 -080065
66/* Holds the old poweroff function so we can restore it on removal. */
67static void (*old_poweroff_func)(void);
68
69static 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
82module_param_call(ifnum_to_use, set_param_ifnum, param_get_int,
83 &ifnum_to_use, 0644);
84MODULE_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 Minyard3b625942005-06-23 22:01:42 -070088/* parameter definition to allow user to flag power cycle */
Corey Minyarda9d014a2005-09-27 21:45:35 -070089module_param(poweroff_powercycle, int, 0644);
Corey Minyard21d6c542005-11-07 00:59:57 -080090MODULE_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 Minyard3b625942005-06-23 22:01:42 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Stuff from the get device id command. */
93static unsigned int mfg_id;
94static unsigned int prod_id;
95static unsigned char capabilities;
Corey Minyard168524d2005-09-06 15:18:43 -070096static unsigned char ipmi_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
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. */
102static void dummy_smi_free(struct ipmi_smi_msg *msg)
103{
104}
105static void dummy_recv_free(struct ipmi_recv_msg *msg)
106{
107}
108static struct ipmi_smi_msg halt_smi_msg =
109{
110 .done = dummy_smi_free
111};
112static 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
122static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
123{
Corey Minyard77cf3972005-06-23 22:01:44 -0700124 struct completion *comp = recv_msg->user_msg_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Corey Minyard77cf3972005-06-23 22:01:44 -0700126 if (comp)
127 complete(comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static struct ipmi_user_hndl ipmi_poweroff_handler =
131{
132 .ipmi_recv_hndl = receive_handler
133};
134
135
136static int ipmi_request_wait_for_response(ipmi_user_t user,
137 struct ipmi_addr *addr,
138 struct kernel_ipmi_msg *send_msg)
139{
Corey Minyard77cf3972005-06-23 22:01:44 -0700140 int rv;
141 struct completion comp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Corey Minyard77cf3972005-06-23 22:01:44 -0700143 init_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Corey Minyard77cf3972005-06-23 22:01:44 -0700145 rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 &halt_smi_msg, &halt_recv_msg, 0);
147 if (rv)
148 return rv;
149
Corey Minyard77cf3972005-06-23 22:01:44 -0700150 wait_for_completion(&comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 return halt_recv_msg.msg.data[0];
153}
154
Corey Minyard77cf3972005-06-23 22:01:44 -0700155/* We are in run-to-completion mode, no completion is desired. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static int ipmi_request_in_rc_mode(ipmi_user_t user,
157 struct ipmi_addr *addr,
158 struct kernel_ipmi_msg *send_msg)
159{
Corey Minyard77cf3972005-06-23 22:01:44 -0700160 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
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
Corey Minyard7d8ba962006-12-06 20:41:09 -0800179#define IPMI_NETFN_OEM 0x2e
180#define IPMI_ATCA_PPS_GRACEFUL_RESTART 0x11
181#define IPMI_ATCA_PPS_IANA "\x00\x40\x0A"
182#define IPMI_MOTOROLA_MANUFACTURER_ID 0x0000A1
183#define IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID 0x0051
184
Randy Dunlap0c8204b2006-12-10 02:19:06 -0800185static void (*atca_oem_poweroff_hook)(ipmi_user_t user);
Corey Minyard7d8ba962006-12-06 20:41:09 -0800186
187static void pps_poweroff_atca (ipmi_user_t user)
188{
189 struct ipmi_system_interface_addr smi_addr;
190 struct kernel_ipmi_msg send_msg;
191 int rv;
192 /*
193 * Configure IPMI address for local access
194 */
195 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
196 smi_addr.channel = IPMI_BMC_CHANNEL;
197 smi_addr.lun = 0;
198
199 printk(KERN_INFO PFX "PPS powerdown hook used");
200
201 send_msg.netfn = IPMI_NETFN_OEM;
202 send_msg.cmd = IPMI_ATCA_PPS_GRACEFUL_RESTART;
203 send_msg.data = IPMI_ATCA_PPS_IANA;
204 send_msg.data_len = 3;
205 rv = ipmi_request_in_rc_mode(user,
206 (struct ipmi_addr *) &smi_addr,
207 &send_msg);
208 if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
209 printk(KERN_ERR PFX "Unable to send ATCA ,"
210 " IPMI error 0x%x\n", rv);
211 }
212 return;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static int ipmi_atca_detect (ipmi_user_t user)
216{
217 struct ipmi_system_interface_addr smi_addr;
218 struct kernel_ipmi_msg send_msg;
219 int rv;
220 unsigned char data[1];
221
222 /*
223 * Configure IPMI address for local access
224 */
225 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
226 smi_addr.channel = IPMI_BMC_CHANNEL;
227 smi_addr.lun = 0;
228
229 /*
230 * Use get address info to check and see if we are ATCA
231 */
232 send_msg.netfn = IPMI_NETFN_ATCA;
233 send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
234 data[0] = IPMI_PICMG_ID;
235 send_msg.data = data;
236 send_msg.data_len = sizeof(data);
237 rv = ipmi_request_wait_for_response(user,
238 (struct ipmi_addr *) &smi_addr,
239 &send_msg);
Corey Minyard7d8ba962006-12-06 20:41:09 -0800240
241 printk(KERN_INFO PFX "ATCA Detect mfg 0x%X prod 0x%X\n", mfg_id, prod_id);
242 if((mfg_id == IPMI_MOTOROLA_MANUFACTURER_ID)
243 && (prod_id == IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID)) {
244 printk(KERN_INFO PFX "Installing Pigeon Point Systems Poweroff Hook\n");
245 atca_oem_poweroff_hook = pps_poweroff_atca;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return !rv;
248}
249
250static void ipmi_poweroff_atca (ipmi_user_t user)
251{
252 struct ipmi_system_interface_addr smi_addr;
253 struct kernel_ipmi_msg send_msg;
254 int rv;
255 unsigned char data[4];
256
257 /*
258 * Configure IPMI address for local access
259 */
260 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
261 smi_addr.channel = IPMI_BMC_CHANNEL;
262 smi_addr.lun = 0;
263
264 printk(KERN_INFO PFX "Powering down via ATCA power command\n");
265
266 /*
267 * Power down
268 */
269 send_msg.netfn = IPMI_NETFN_ATCA;
270 send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
271 data[0] = IPMI_PICMG_ID;
272 data[1] = 0; /* FRU id */
273 data[2] = 0; /* Power Level */
274 data[3] = 0; /* Don't change saved presets */
275 send_msg.data = data;
276 send_msg.data_len = sizeof (data);
277 rv = ipmi_request_in_rc_mode(user,
278 (struct ipmi_addr *) &smi_addr,
279 &send_msg);
Corey Minyard7d8ba962006-12-06 20:41:09 -0800280 /** At this point, the system may be shutting down, and most
281 ** serial drivers (if used) will have interrupts turned off
282 ** it may be better to ignore IPMI_UNKNOWN_ERR_COMPLETION_CODE
283 ** return code
284 **/
285 if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
287 " IPMI error 0x%x\n", rv);
288 goto out;
289 }
290
Corey Minyard7d8ba962006-12-06 20:41:09 -0800291 if(atca_oem_poweroff_hook)
292 return atca_oem_poweroff_hook(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 out:
294 return;
295}
296
297/*
298 * CPI1 Support
299 */
300
301#define IPMI_NETFN_OEM_1 0xf8
302#define OEM_GRP_CMD_SET_RESET_STATE 0x84
303#define OEM_GRP_CMD_SET_POWER_STATE 0x82
304#define IPMI_NETFN_OEM_8 0xf8
305#define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL 0x80
306#define OEM_GRP_CMD_GET_SLOT_GA 0xa3
307#define IPMI_NETFN_SENSOR_EVT 0x10
308#define IPMI_CMD_GET_EVENT_RECEIVER 0x01
309
310#define IPMI_CPI1_PRODUCT_ID 0x000157
311#define IPMI_CPI1_MANUFACTURER_ID 0x0108
312
313static int ipmi_cpi1_detect (ipmi_user_t user)
314{
315 return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
316 && (prod_id == IPMI_CPI1_PRODUCT_ID));
317}
318
319static void ipmi_poweroff_cpi1 (ipmi_user_t user)
320{
321 struct ipmi_system_interface_addr smi_addr;
322 struct ipmi_ipmb_addr ipmb_addr;
323 struct kernel_ipmi_msg send_msg;
324 int rv;
325 unsigned char data[1];
326 int slot;
327 unsigned char hotswap_ipmb;
328 unsigned char aer_addr;
329 unsigned char aer_lun;
330
331 /*
332 * Configure IPMI address for local access
333 */
334 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
335 smi_addr.channel = IPMI_BMC_CHANNEL;
336 smi_addr.lun = 0;
337
338 printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
339
340 /*
341 * Get IPMI ipmb address
342 */
343 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
344 send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
345 send_msg.data = NULL;
346 send_msg.data_len = 0;
347 rv = ipmi_request_in_rc_mode(user,
348 (struct ipmi_addr *) &smi_addr,
349 &send_msg);
350 if (rv)
351 goto out;
352 slot = halt_recv_msg.msg.data[1];
353 hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
354
355 /*
356 * Get active event receiver
357 */
358 send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
359 send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
360 send_msg.data = NULL;
361 send_msg.data_len = 0;
362 rv = ipmi_request_in_rc_mode(user,
363 (struct ipmi_addr *) &smi_addr,
364 &send_msg);
365 if (rv)
366 goto out;
367 aer_addr = halt_recv_msg.msg.data[1];
368 aer_lun = halt_recv_msg.msg.data[2];
369
370 /*
371 * Setup IPMB address target instead of local target
372 */
373 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
374 ipmb_addr.channel = 0;
375 ipmb_addr.slave_addr = aer_addr;
376 ipmb_addr.lun = aer_lun;
377
378 /*
379 * Send request hotswap control to remove blade from dpv
380 */
381 send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
382 send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
383 send_msg.data = &hotswap_ipmb;
384 send_msg.data_len = 1;
385 ipmi_request_in_rc_mode(user,
386 (struct ipmi_addr *) &ipmb_addr,
387 &send_msg);
388
389 /*
390 * Set reset asserted
391 */
392 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
393 send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
394 send_msg.data = data;
395 data[0] = 1; /* Reset asserted state */
396 send_msg.data_len = 1;
397 rv = ipmi_request_in_rc_mode(user,
398 (struct ipmi_addr *) &smi_addr,
399 &send_msg);
400 if (rv)
401 goto out;
402
403 /*
404 * Power down
405 */
406 send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
407 send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
408 send_msg.data = data;
409 data[0] = 1; /* Power down state */
410 send_msg.data_len = 1;
411 rv = ipmi_request_in_rc_mode(user,
412 (struct ipmi_addr *) &smi_addr,
413 &send_msg);
414 if (rv)
415 goto out;
416
417 out:
418 return;
419}
420
421/*
Corey Minyard168524d2005-09-06 15:18:43 -0700422 * ipmi_dell_chassis_detect()
423 * Dell systems with IPMI < 1.5 don't set the chassis capability bit
424 * but they can handle a chassis poweroff or powercycle command.
425 */
426
427#define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
428static int ipmi_dell_chassis_detect (ipmi_user_t user)
429{
430 const char ipmi_version_major = ipmi_version & 0xF;
431 const char ipmi_version_minor = (ipmi_version >> 4) & 0xF;
Corey Minyard8a3628d2006-03-31 02:30:40 -0800432 const char mfr[3] = DELL_IANA_MFR_ID;
Corey Minyard168524d2005-09-06 15:18:43 -0700433 if (!memcmp(mfr, &mfg_id, sizeof(mfr)) &&
434 ipmi_version_major <= 1 &&
435 ipmi_version_minor < 5)
436 return 1;
437 return 0;
438}
439
440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * Standard chassis support
442 */
443
444#define IPMI_NETFN_CHASSIS_REQUEST 0
445#define IPMI_CHASSIS_CONTROL_CMD 0x02
446
447static int ipmi_chassis_detect (ipmi_user_t user)
448{
449 /* Chassis support, use it. */
450 return (capabilities & 0x80);
451}
452
453static void ipmi_poweroff_chassis (ipmi_user_t user)
454{
455 struct ipmi_system_interface_addr smi_addr;
456 struct kernel_ipmi_msg send_msg;
457 int rv;
458 unsigned char data[1];
459
460 /*
461 * Configure IPMI address for local access
462 */
463 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
464 smi_addr.channel = IPMI_BMC_CHANNEL;
465 smi_addr.lun = 0;
466
Corey Minyard3b625942005-06-23 22:01:42 -0700467 powercyclefailed:
468 printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
Corey Minyard8c702e12005-09-06 15:18:46 -0700469 (poweroff_powercycle ? "cycle" : "down"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /*
472 * Power down
473 */
474 send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
475 send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
Corey Minyard8c702e12005-09-06 15:18:46 -0700476 if (poweroff_powercycle)
477 data[0] = IPMI_CHASSIS_POWER_CYCLE;
478 else
479 data[0] = IPMI_CHASSIS_POWER_DOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 send_msg.data = data;
481 send_msg.data_len = sizeof(data);
482 rv = ipmi_request_in_rc_mode(user,
483 (struct ipmi_addr *) &smi_addr,
484 &send_msg);
485 if (rv) {
Corey Minyard8c702e12005-09-06 15:18:46 -0700486 if (poweroff_powercycle) {
487 /* power cycle failed, default to power down */
488 printk(KERN_ERR PFX "Unable to send chassis power " \
489 "cycle message, IPMI error 0x%x\n", rv);
490 poweroff_powercycle = 0;
491 goto powercyclefailed;
Corey Minyard3b625942005-06-23 22:01:42 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Corey Minyard8c702e12005-09-06 15:18:46 -0700494 printk(KERN_ERR PFX "Unable to send chassis power " \
495 "down message, IPMI error 0x%x\n", rv);
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499
500/* Table of possible power off functions. */
501struct poweroff_function {
502 char *platform_type;
503 int (*detect)(ipmi_user_t user);
504 void (*poweroff_func)(ipmi_user_t user);
505};
506
507static struct poweroff_function poweroff_functions[] = {
508 { .platform_type = "ATCA",
509 .detect = ipmi_atca_detect,
510 .poweroff_func = ipmi_poweroff_atca },
511 { .platform_type = "CPI1",
512 .detect = ipmi_cpi1_detect,
513 .poweroff_func = ipmi_poweroff_cpi1 },
Corey Minyard168524d2005-09-06 15:18:43 -0700514 { .platform_type = "chassis",
515 .detect = ipmi_dell_chassis_detect,
516 .poweroff_func = ipmi_poweroff_chassis },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* Chassis should generally be last, other things should override
518 it. */
519 { .platform_type = "chassis",
520 .detect = ipmi_chassis_detect,
521 .poweroff_func = ipmi_poweroff_chassis },
522};
523#define NUM_PO_FUNCS (sizeof(poweroff_functions) \
524 / sizeof(struct poweroff_function))
525
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/* Called on a powerdown request. */
528static void ipmi_poweroff_function (void)
529{
530 if (!ready)
531 return;
532
533 /* Use run-to-completion mode, since interrupts may be off. */
534 ipmi_user_set_run_to_completion(ipmi_user, 1);
535 specific_poweroff_func(ipmi_user);
536 ipmi_user_set_run_to_completion(ipmi_user, 0);
537}
538
539/* Wait for an IPMI interface to be installed, the first one installed
540 will be grabbed by this code and used to perform the powerdown. */
Corey Minyard50c812b2006-03-26 01:37:21 -0800541static void ipmi_po_new_smi(int if_num, struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct ipmi_system_interface_addr smi_addr;
544 struct kernel_ipmi_msg send_msg;
545 int rv;
546 int i;
547
548 if (ready)
549 return;
550
Corey Minyardb2c03942006-12-06 20:41:00 -0800551 if ((ifnum_to_use >= 0) && (ifnum_to_use != if_num))
552 return;
553
Corey Minyard3b625942005-06-23 22:01:42 -0700554 rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
555 &ipmi_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (rv) {
557 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
558 rv);
559 return;
560 }
561
Corey Minyardb2c03942006-12-06 20:41:00 -0800562 ipmi_ifnum = if_num;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Do a get device ide and store some results, since this is
566 * used by several functions.
567 */
568 smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
569 smi_addr.channel = IPMI_BMC_CHANNEL;
570 smi_addr.lun = 0;
571
572 send_msg.netfn = IPMI_NETFN_APP_REQUEST;
573 send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
574 send_msg.data = NULL;
575 send_msg.data_len = 0;
576 rv = ipmi_request_wait_for_response(ipmi_user,
577 (struct ipmi_addr *) &smi_addr,
578 &send_msg);
579 if (rv) {
580 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
581 " IPMI error 0x%x\n", rv);
582 goto out_err;
583 }
584
585 if (halt_recv_msg.msg.data_len < 12) {
586 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
587 " short, was %d bytes, needed %d bytes\n",
588 halt_recv_msg.msg.data_len, 12);
589 goto out_err;
590 }
591
592 mfg_id = (halt_recv_msg.msg.data[7]
593 | (halt_recv_msg.msg.data[8] << 8)
594 | (halt_recv_msg.msg.data[9] << 16));
595 prod_id = (halt_recv_msg.msg.data[10]
596 | (halt_recv_msg.msg.data[11] << 8));
597 capabilities = halt_recv_msg.msg.data[6];
Corey Minyard168524d2005-09-06 15:18:43 -0700598 ipmi_version = halt_recv_msg.msg.data[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600
601 /* Scan for a poweroff method */
Corey Minyarde8b33612005-09-06 15:18:45 -0700602 for (i = 0; i < NUM_PO_FUNCS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (poweroff_functions[i].detect(ipmi_user))
604 goto found;
605 }
606
607 out_err:
608 printk(KERN_ERR PFX "Unable to find a poweroff function that"
609 " will work, giving up\n");
610 ipmi_destroy_user(ipmi_user);
611 return;
612
613 found:
614 printk(KERN_INFO PFX "Found a %s style poweroff function\n",
615 poweroff_functions[i].platform_type);
616 specific_poweroff_func = poweroff_functions[i].poweroff_func;
617 old_poweroff_func = pm_power_off;
618 pm_power_off = ipmi_poweroff_function;
619 ready = 1;
620}
621
622static void ipmi_po_smi_gone(int if_num)
623{
Corey Minyardb2c03942006-12-06 20:41:00 -0800624 if (!ready)
625 return;
626
627 if (ipmi_ifnum != if_num)
628 return;
629
630 ready = 0;
631 ipmi_destroy_user(ipmi_user);
632 pm_power_off = old_poweroff_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635static struct ipmi_smi_watcher smi_watcher =
636{
637 .owner = THIS_MODULE,
638 .new_smi = ipmi_po_new_smi,
639 .smi_gone = ipmi_po_smi_gone
640};
641
642
Corey Minyard3b625942005-06-23 22:01:42 -0700643#ifdef CONFIG_PROC_FS
Corey Minyard8c702e12005-09-06 15:18:46 -0700644#include <linux/sysctl.h>
Corey Minyard3b625942005-06-23 22:01:42 -0700645
Corey Minyard8c702e12005-09-06 15:18:46 -0700646static ctl_table ipmi_table[] = {
647 { .ctl_name = DEV_IPMI_POWEROFF_POWERCYCLE,
648 .procname = "poweroff_powercycle",
649 .data = &poweroff_powercycle,
650 .maxlen = sizeof(poweroff_powercycle),
651 .mode = 0644,
652 .proc_handler = &proc_dointvec },
653 { }
654};
Corey Minyard3b625942005-06-23 22:01:42 -0700655
Corey Minyard8c702e12005-09-06 15:18:46 -0700656static ctl_table ipmi_dir_table[] = {
657 { .ctl_name = DEV_IPMI,
658 .procname = "ipmi",
659 .mode = 0555,
660 .child = ipmi_table },
661 { }
662};
Corey Minyard3b625942005-06-23 22:01:42 -0700663
Corey Minyard8c702e12005-09-06 15:18:46 -0700664static ctl_table ipmi_root_table[] = {
665 { .ctl_name = CTL_DEV,
666 .procname = "dev",
667 .mode = 0555,
668 .child = ipmi_dir_table },
669 { }
670};
Corey Minyard3b625942005-06-23 22:01:42 -0700671
Corey Minyard8c702e12005-09-06 15:18:46 -0700672static struct ctl_table_header *ipmi_table_header;
Corey Minyard3b625942005-06-23 22:01:42 -0700673#endif /* CONFIG_PROC_FS */
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/*
676 * Startup and shutdown functions.
677 */
678static int ipmi_poweroff_init (void)
679{
Corey Minyard8c702e12005-09-06 15:18:46 -0700680 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 printk ("Copyright (C) 2004 MontaVista Software -"
Corey Minyard1fdd75b2005-09-06 15:18:42 -0700683 " IPMI Powerdown via sys_reboot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Corey Minyard8c702e12005-09-06 15:18:46 -0700685 if (poweroff_powercycle)
686 printk(KERN_INFO PFX "Power cycle is enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Corey Minyard8c702e12005-09-06 15:18:46 -0700688#ifdef CONFIG_PROC_FS
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800689 ipmi_table_header = register_sysctl_table(ipmi_root_table);
Corey Minyard8c702e12005-09-06 15:18:46 -0700690 if (!ipmi_table_header) {
691 printk(KERN_ERR PFX "Unable to register powercycle sysctl\n");
692 rv = -ENOMEM;
693 goto out_err;
Corey Minyard3b625942005-06-23 22:01:42 -0700694 }
Corey Minyard8c702e12005-09-06 15:18:46 -0700695#endif
Corey Minyard3b625942005-06-23 22:01:42 -0700696
697 rv = ipmi_smi_watcher_register(&smi_watcher);
Adrian Bunkbe4f1bb2006-01-09 20:51:36 -0800698
699#ifdef CONFIG_PROC_FS
Corey Minyard3b625942005-06-23 22:01:42 -0700700 if (rv) {
Corey Minyard8c702e12005-09-06 15:18:46 -0700701 unregister_sysctl_table(ipmi_table_header);
Corey Minyard3b625942005-06-23 22:01:42 -0700702 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
703 goto out_err;
704 }
705
Corey Minyard3b625942005-06-23 22:01:42 -0700706 out_err:
Randy Dunlap1aa16ee2006-12-06 20:41:20 -0800707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return rv;
709}
710
711#ifdef MODULE
712static __exit void ipmi_poweroff_cleanup(void)
713{
714 int rv;
715
Corey Minyard3b625942005-06-23 22:01:42 -0700716#ifdef CONFIG_PROC_FS
Corey Minyard8c702e12005-09-06 15:18:46 -0700717 unregister_sysctl_table(ipmi_table_header);
Corey Minyard3b625942005-06-23 22:01:42 -0700718#endif
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ipmi_smi_watcher_unregister(&smi_watcher);
721
722 if (ready) {
723 rv = ipmi_destroy_user(ipmi_user);
724 if (rv)
725 printk(KERN_ERR PFX "could not cleanup the IPMI"
726 " user: 0x%x\n", rv);
727 pm_power_off = old_poweroff_func;
728 }
729}
730module_exit(ipmi_poweroff_cleanup);
731#endif
732
733module_init(ipmi_poweroff_init);
734MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -0700735MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
736MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");