]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/ipmi/ipmi_poweroff.c
ipmi: run to completion fixes
[linux-2.6-omap-h63xx.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 static void ipmi_po_smi_gone(int if_num);
47 static void ipmi_po_new_smi(int if_num, struct device *device);
48
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 */
55 static int poweroff_powercycle;
56
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;
62 static ipmi_user_t ipmi_user;
63 static int ipmi_ifnum;
64 static void (*specific_poweroff_func)(ipmi_user_t user);
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
88 /* parameter definition to allow user to flag power cycle */
89 module_param(poweroff_powercycle, int, 0644);
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.");
91
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;
96 static unsigned char ipmi_version;
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 atomic_t dummy_count = ATOMIC_INIT(0);
103 static void dummy_smi_free(struct ipmi_smi_msg *msg)
104 {
105         atomic_dec(&dummy_count);
106 }
107 static void dummy_recv_free(struct ipmi_recv_msg *msg)
108 {
109         atomic_dec(&dummy_count);
110 }
111 static struct ipmi_smi_msg halt_smi_msg =
112 {
113         .done = dummy_smi_free
114 };
115 static struct ipmi_recv_msg halt_recv_msg =
116 {
117         .done = dummy_recv_free
118 };
119
120
121 /*
122  * Code to send a message and wait for the reponse.
123  */
124
125 static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
126 {
127         struct completion *comp = recv_msg->user_msg_data;
128
129         if (comp)
130                 complete(comp);
131 }
132
133 static struct ipmi_user_hndl ipmi_poweroff_handler =
134 {
135         .ipmi_recv_hndl = receive_handler
136 };
137
138
139 static int ipmi_request_wait_for_response(ipmi_user_t            user,
140                                           struct ipmi_addr       *addr,
141                                           struct kernel_ipmi_msg *send_msg)
142 {
143         int               rv;
144         struct completion comp;
145
146         init_completion(&comp);
147
148         rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &comp,
149                                       &halt_smi_msg, &halt_recv_msg, 0);
150         if (rv)
151                 return rv;
152
153         wait_for_completion(&comp);
154
155         return halt_recv_msg.msg.data[0];
156 }
157
158 /* Wait for message to complete, spinning. */
159 static int ipmi_request_in_rc_mode(ipmi_user_t            user,
160                                    struct ipmi_addr       *addr,
161                                    struct kernel_ipmi_msg *send_msg)
162 {
163         int rv;
164
165         atomic_set(&dummy_count, 2);
166         rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL,
167                                       &halt_smi_msg, &halt_recv_msg, 0);
168         if (rv) {
169                 atomic_set(&dummy_count, 0);
170                 return rv;
171         }
172
173         /*
174          * Spin until our message is done.
175          */
176         while (atomic_read(&dummy_count) > 0) {
177                 ipmi_poll_interface(user);
178                 cpu_relax();
179         }
180
181         return halt_recv_msg.msg.data[0];
182 }
183
184 /*
185  * ATCA Support
186  */
187
188 #define IPMI_NETFN_ATCA                 0x2c
189 #define IPMI_ATCA_SET_POWER_CMD         0x11
190 #define IPMI_ATCA_GET_ADDR_INFO_CMD     0x01
191 #define IPMI_PICMG_ID                   0
192
193 #define IPMI_NETFN_OEM                          0x2e
194 #define IPMI_ATCA_PPS_GRACEFUL_RESTART          0x11
195 #define IPMI_ATCA_PPS_IANA                      "\x00\x40\x0A"
196 #define IPMI_MOTOROLA_MANUFACTURER_ID           0x0000A1
197 #define IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID       0x0051
198
199 static void (*atca_oem_poweroff_hook)(ipmi_user_t user);
200
201 static void pps_poweroff_atca (ipmi_user_t user)
202 {
203         struct ipmi_system_interface_addr smi_addr;
204         struct kernel_ipmi_msg            send_msg;
205         int                               rv;
206         /*
207          * Configure IPMI address for local access
208          */
209         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
210         smi_addr.channel = IPMI_BMC_CHANNEL;
211         smi_addr.lun = 0;
212
213         printk(KERN_INFO PFX "PPS powerdown hook used");
214
215         send_msg.netfn = IPMI_NETFN_OEM;
216         send_msg.cmd = IPMI_ATCA_PPS_GRACEFUL_RESTART;
217         send_msg.data = IPMI_ATCA_PPS_IANA;
218         send_msg.data_len = 3;
219         rv = ipmi_request_in_rc_mode(user,
220                                   (struct ipmi_addr *) &smi_addr,
221                                    &send_msg);
222         if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
223                 printk(KERN_ERR PFX "Unable to send ATCA ,"
224                        " IPMI error 0x%x\n", rv);
225         }
226         return;
227 }
228
229 static int ipmi_atca_detect (ipmi_user_t user)
230 {
231         struct ipmi_system_interface_addr smi_addr;
232         struct kernel_ipmi_msg            send_msg;
233         int                               rv;
234         unsigned char                     data[1];
235
236         /*
237          * Configure IPMI address for local access
238          */
239         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
240         smi_addr.channel = IPMI_BMC_CHANNEL;
241         smi_addr.lun = 0;
242
243         /*
244          * Use get address info to check and see if we are ATCA
245          */
246         send_msg.netfn = IPMI_NETFN_ATCA;
247         send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
248         data[0] = IPMI_PICMG_ID;
249         send_msg.data = data;
250         send_msg.data_len = sizeof(data);
251         rv = ipmi_request_wait_for_response(user,
252                                             (struct ipmi_addr *) &smi_addr,
253                                             &send_msg);
254
255         printk(KERN_INFO PFX "ATCA Detect mfg 0x%X prod 0x%X\n", mfg_id, prod_id);
256         if((mfg_id == IPMI_MOTOROLA_MANUFACTURER_ID)
257             && (prod_id == IPMI_MOTOROLA_PPS_IPMC_PRODUCT_ID)) {
258                 printk(KERN_INFO PFX "Installing Pigeon Point Systems Poweroff Hook\n");
259                 atca_oem_poweroff_hook = pps_poweroff_atca;
260         }
261         return !rv;
262 }
263
264 static void ipmi_poweroff_atca (ipmi_user_t user)
265 {
266         struct ipmi_system_interface_addr smi_addr;
267         struct kernel_ipmi_msg            send_msg;
268         int                               rv;
269         unsigned char                     data[4];
270
271         /*
272          * Configure IPMI address for local access
273          */
274         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
275         smi_addr.channel = IPMI_BMC_CHANNEL;
276         smi_addr.lun = 0;
277
278         printk(KERN_INFO PFX "Powering down via ATCA power command\n");
279
280         /*
281          * Power down
282          */
283         send_msg.netfn = IPMI_NETFN_ATCA;
284         send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
285         data[0] = IPMI_PICMG_ID;
286         data[1] = 0; /* FRU id */
287         data[2] = 0; /* Power Level */
288         data[3] = 0; /* Don't change saved presets */
289         send_msg.data = data;
290         send_msg.data_len = sizeof (data);
291         rv = ipmi_request_in_rc_mode(user,
292                                      (struct ipmi_addr *) &smi_addr,
293                                      &send_msg);
294         /** At this point, the system may be shutting down, and most
295          ** serial drivers (if used) will have interrupts turned off
296          ** it may be better to ignore IPMI_UNKNOWN_ERR_COMPLETION_CODE
297          ** return code
298          **/
299         if (rv && rv != IPMI_UNKNOWN_ERR_COMPLETION_CODE) {
300                 printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
301                        " IPMI error 0x%x\n", rv);
302                 goto out;
303         }
304
305         if(atca_oem_poweroff_hook)
306                 return atca_oem_poweroff_hook(user);
307  out:
308         return;
309 }
310
311 /*
312  * CPI1 Support
313  */
314
315 #define IPMI_NETFN_OEM_1                                0xf8
316 #define OEM_GRP_CMD_SET_RESET_STATE             0x84
317 #define OEM_GRP_CMD_SET_POWER_STATE             0x82
318 #define IPMI_NETFN_OEM_8                                0xf8
319 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL        0x80
320 #define OEM_GRP_CMD_GET_SLOT_GA                 0xa3
321 #define IPMI_NETFN_SENSOR_EVT                   0x10
322 #define IPMI_CMD_GET_EVENT_RECEIVER             0x01
323
324 #define IPMI_CPI1_PRODUCT_ID            0x000157
325 #define IPMI_CPI1_MANUFACTURER_ID       0x0108
326
327 static int ipmi_cpi1_detect (ipmi_user_t user)
328 {
329         return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
330                 && (prod_id == IPMI_CPI1_PRODUCT_ID));
331 }
332
333 static void ipmi_poweroff_cpi1 (ipmi_user_t user)
334 {
335         struct ipmi_system_interface_addr smi_addr;
336         struct ipmi_ipmb_addr             ipmb_addr;
337         struct kernel_ipmi_msg            send_msg;
338         int                               rv;
339         unsigned char                     data[1];
340         int                               slot;
341         unsigned char                     hotswap_ipmb;
342         unsigned char                     aer_addr;
343         unsigned char                     aer_lun;
344
345         /*
346          * Configure IPMI address for local access
347          */
348         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
349         smi_addr.channel = IPMI_BMC_CHANNEL;
350         smi_addr.lun = 0;
351
352         printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
353
354         /*
355          * Get IPMI ipmb address
356          */
357         send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
358         send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
359         send_msg.data = NULL;
360         send_msg.data_len = 0;
361         rv = ipmi_request_in_rc_mode(user,
362                                      (struct ipmi_addr *) &smi_addr,
363                                      &send_msg);
364         if (rv)
365                 goto out;
366         slot = halt_recv_msg.msg.data[1];
367         hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
368
369         /*
370          * Get active event receiver
371          */
372         send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
373         send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
374         send_msg.data = NULL;
375         send_msg.data_len = 0;
376         rv = ipmi_request_in_rc_mode(user,
377                                      (struct ipmi_addr *) &smi_addr,
378                                      &send_msg);
379         if (rv)
380                 goto out;
381         aer_addr = halt_recv_msg.msg.data[1];
382         aer_lun = halt_recv_msg.msg.data[2];
383
384         /*
385          * Setup IPMB address target instead of local target
386          */
387         ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
388         ipmb_addr.channel = 0;
389         ipmb_addr.slave_addr = aer_addr;
390         ipmb_addr.lun = aer_lun;
391
392         /*
393          * Send request hotswap control to remove blade from dpv
394          */
395         send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
396         send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
397         send_msg.data = &hotswap_ipmb;
398         send_msg.data_len = 1;
399         ipmi_request_in_rc_mode(user,
400                                 (struct ipmi_addr *) &ipmb_addr,
401                                 &send_msg);
402
403         /*
404          * Set reset asserted
405          */
406         send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
407         send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
408         send_msg.data = data;
409         data[0] = 1; /* Reset asserted 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         /*
418          * Power down
419          */
420         send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
421         send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
422         send_msg.data = data;
423         data[0] = 1; /* Power down state */
424         send_msg.data_len = 1;
425         rv = ipmi_request_in_rc_mode(user,
426                                      (struct ipmi_addr *) &smi_addr,
427                                      &send_msg);
428         if (rv)
429                 goto out;
430
431  out:
432         return;
433 }
434
435 /*
436  * ipmi_dell_chassis_detect()
437  * Dell systems with IPMI < 1.5 don't set the chassis capability bit
438  * but they can handle a chassis poweroff or powercycle command.
439  */
440
441 #define DELL_IANA_MFR_ID {0xA2, 0x02, 0x00}
442 static int ipmi_dell_chassis_detect (ipmi_user_t user)
443 {
444         const char ipmi_version_major = ipmi_version & 0xF;
445         const char ipmi_version_minor = (ipmi_version >> 4) & 0xF;
446         const char mfr[3] = DELL_IANA_MFR_ID;
447         if (!memcmp(mfr, &mfg_id, sizeof(mfr)) &&
448             ipmi_version_major <= 1 &&
449             ipmi_version_minor < 5)
450                 return 1;
451         return 0;
452 }
453
454 /*
455  * Standard chassis support
456  */
457
458 #define IPMI_NETFN_CHASSIS_REQUEST      0
459 #define IPMI_CHASSIS_CONTROL_CMD        0x02
460
461 static int ipmi_chassis_detect (ipmi_user_t user)
462 {
463         /* Chassis support, use it. */
464         return (capabilities & 0x80);
465 }
466
467 static void ipmi_poweroff_chassis (ipmi_user_t user)
468 {
469         struct ipmi_system_interface_addr smi_addr;
470         struct kernel_ipmi_msg            send_msg;
471         int                               rv;
472         unsigned char                     data[1];
473
474         /*
475          * Configure IPMI address for local access
476          */
477         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
478         smi_addr.channel = IPMI_BMC_CHANNEL;
479         smi_addr.lun = 0;
480
481  powercyclefailed:
482         printk(KERN_INFO PFX "Powering %s via IPMI chassis control command\n",
483                 (poweroff_powercycle ? "cycle" : "down"));
484
485         /*
486          * Power down
487          */
488         send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
489         send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
490         if (poweroff_powercycle)
491                 data[0] = IPMI_CHASSIS_POWER_CYCLE;
492         else
493                 data[0] = IPMI_CHASSIS_POWER_DOWN;
494         send_msg.data = data;
495         send_msg.data_len = sizeof(data);
496         rv = ipmi_request_in_rc_mode(user,
497                                      (struct ipmi_addr *) &smi_addr,
498                                      &send_msg);
499         if (rv) {
500                 if (poweroff_powercycle) {
501                         /* power cycle failed, default to power down */
502                         printk(KERN_ERR PFX "Unable to send chassis power " \
503                                "cycle message, IPMI error 0x%x\n", rv);
504                         poweroff_powercycle = 0;
505                         goto powercyclefailed;
506                 }
507
508                 printk(KERN_ERR PFX "Unable to send chassis power " \
509                        "down message, IPMI error 0x%x\n", rv);
510         }
511 }
512
513
514 /* Table of possible power off functions. */
515 struct poweroff_function {
516         char *platform_type;
517         int  (*detect)(ipmi_user_t user);
518         void (*poweroff_func)(ipmi_user_t user);
519 };
520
521 static struct poweroff_function poweroff_functions[] = {
522         { .platform_type        = "ATCA",
523           .detect               = ipmi_atca_detect,
524           .poweroff_func        = ipmi_poweroff_atca },
525         { .platform_type        = "CPI1",
526           .detect               = ipmi_cpi1_detect,
527           .poweroff_func        = ipmi_poweroff_cpi1 },
528         { .platform_type        = "chassis",
529           .detect               = ipmi_dell_chassis_detect,
530           .poweroff_func        = ipmi_poweroff_chassis },
531         /* Chassis should generally be last, other things should override
532            it. */
533         { .platform_type        = "chassis",
534           .detect               = ipmi_chassis_detect,
535           .poweroff_func        = ipmi_poweroff_chassis },
536 };
537 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
538                       / sizeof(struct poweroff_function))
539
540
541 /* Called on a powerdown request. */
542 static void ipmi_poweroff_function (void)
543 {
544         if (!ready)
545                 return;
546
547         /* Use run-to-completion mode, since interrupts may be off. */
548         specific_poweroff_func(ipmi_user);
549 }
550
551 /* Wait for an IPMI interface to be installed, the first one installed
552    will be grabbed by this code and used to perform the powerdown. */
553 static void ipmi_po_new_smi(int if_num, struct device *device)
554 {
555         struct ipmi_system_interface_addr smi_addr;
556         struct kernel_ipmi_msg            send_msg;
557         int                               rv;
558         int                               i;
559
560         if (ready)
561                 return;
562
563         if ((ifnum_to_use >= 0) && (ifnum_to_use != if_num))
564                 return;
565
566         rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL,
567                               &ipmi_user);
568         if (rv) {
569                 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
570                        rv);
571                 return;
572         }
573
574         ipmi_ifnum = if_num;
575
576         /*
577          * Do a get device ide and store some results, since this is
578          * used by several functions.
579          */
580         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
581         smi_addr.channel = IPMI_BMC_CHANNEL;
582         smi_addr.lun = 0;
583
584         send_msg.netfn = IPMI_NETFN_APP_REQUEST;
585         send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
586         send_msg.data = NULL;
587         send_msg.data_len = 0;
588         rv = ipmi_request_wait_for_response(ipmi_user,
589                                             (struct ipmi_addr *) &smi_addr,
590                                             &send_msg);
591         if (rv) {
592                 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
593                        " IPMI error 0x%x\n", rv);
594                 goto out_err;
595         }
596
597         if (halt_recv_msg.msg.data_len < 12) {
598                 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
599                        " short, was %d bytes, needed %d bytes\n",
600                        halt_recv_msg.msg.data_len, 12);
601                 goto out_err;
602         }
603
604         mfg_id = (halt_recv_msg.msg.data[7]
605                   | (halt_recv_msg.msg.data[8] << 8)
606                   | (halt_recv_msg.msg.data[9] << 16));
607         prod_id = (halt_recv_msg.msg.data[10]
608                    | (halt_recv_msg.msg.data[11] << 8));
609         capabilities = halt_recv_msg.msg.data[6];
610         ipmi_version = halt_recv_msg.msg.data[5];
611
612
613         /* Scan for a poweroff method */
614         for (i = 0; i < NUM_PO_FUNCS; i++) {
615                 if (poweroff_functions[i].detect(ipmi_user))
616                         goto found;
617         }
618
619  out_err:
620         printk(KERN_ERR PFX "Unable to find a poweroff function that"
621                " will work, giving up\n");
622         ipmi_destroy_user(ipmi_user);
623         return;
624
625  found:
626         printk(KERN_INFO PFX "Found a %s style poweroff function\n",
627                poweroff_functions[i].platform_type);
628         specific_poweroff_func = poweroff_functions[i].poweroff_func;
629         old_poweroff_func = pm_power_off;
630         pm_power_off = ipmi_poweroff_function;
631         ready = 1;
632 }
633
634 static void ipmi_po_smi_gone(int if_num)
635 {
636         if (!ready)
637                 return;
638
639         if (ipmi_ifnum != if_num)
640                 return;
641
642         ready = 0;
643         ipmi_destroy_user(ipmi_user);
644         pm_power_off = old_poweroff_func;
645 }
646
647 static struct ipmi_smi_watcher smi_watcher =
648 {
649         .owner    = THIS_MODULE,
650         .new_smi  = ipmi_po_new_smi,
651         .smi_gone = ipmi_po_smi_gone
652 };
653
654
655 #ifdef CONFIG_PROC_FS
656 #include <linux/sysctl.h>
657
658 static ctl_table ipmi_table[] = {
659         { .ctl_name     = DEV_IPMI_POWEROFF_POWERCYCLE,
660           .procname     = "poweroff_powercycle",
661           .data         = &poweroff_powercycle,
662           .maxlen       = sizeof(poweroff_powercycle),
663           .mode         = 0644,
664           .proc_handler = &proc_dointvec },
665         { }
666 };
667
668 static ctl_table ipmi_dir_table[] = {
669         { .ctl_name     = DEV_IPMI,
670           .procname     = "ipmi",
671           .mode         = 0555,
672           .child        = ipmi_table },
673         { }
674 };
675
676 static ctl_table ipmi_root_table[] = {
677         { .ctl_name     = CTL_DEV,
678           .procname     = "dev",
679           .mode         = 0555,
680           .child        = ipmi_dir_table },
681         { }
682 };
683
684 static struct ctl_table_header *ipmi_table_header;
685 #endif /* CONFIG_PROC_FS */
686
687 /*
688  * Startup and shutdown functions.
689  */
690 static int ipmi_poweroff_init (void)
691 {
692         int rv;
693
694         printk (KERN_INFO "Copyright (C) 2004 MontaVista Software -"
695                 " IPMI Powerdown via sys_reboot.\n");
696
697         if (poweroff_powercycle)
698                 printk(KERN_INFO PFX "Power cycle is enabled.\n");
699
700 #ifdef CONFIG_PROC_FS
701         ipmi_table_header = register_sysctl_table(ipmi_root_table);
702         if (!ipmi_table_header) {
703                 printk(KERN_ERR PFX "Unable to register powercycle sysctl\n");
704                 rv = -ENOMEM;
705                 goto out_err;
706         }
707 #endif
708
709         rv = ipmi_smi_watcher_register(&smi_watcher);
710
711 #ifdef CONFIG_PROC_FS
712         if (rv) {
713                 unregister_sysctl_table(ipmi_table_header);
714                 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
715                 goto out_err;
716         }
717
718  out_err:
719 #endif
720         return rv;
721 }
722
723 #ifdef MODULE
724 static __exit void ipmi_poweroff_cleanup(void)
725 {
726         int rv;
727
728 #ifdef CONFIG_PROC_FS
729         unregister_sysctl_table(ipmi_table_header);
730 #endif
731
732         ipmi_smi_watcher_unregister(&smi_watcher);
733
734         if (ready) {
735                 rv = ipmi_destroy_user(ipmi_user);
736                 if (rv)
737                         printk(KERN_ERR PFX "could not cleanup the IPMI"
738                                " user: 0x%x\n", rv);
739                 pm_power_off = old_poweroff_func;
740         }
741 }
742 module_exit(ipmi_poweroff_cleanup);
743 #endif
744
745 module_init(ipmi_poweroff_init);
746 MODULE_LICENSE("GPL");
747 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
748 MODULE_DESCRIPTION("IPMI Poweroff extension to sys_reboot");