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