This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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 <asm/semaphore.h>
35 #include <linux/kdev_t.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/ipmi.h>
39 #include <linux/ipmi_smi.h>
40
41 #define PFX "IPMI poweroff: "
42 #define IPMI_POWEROFF_VERSION   "v33"
43
44 /* Where to we insert our poweroff function? */
45 extern void (*pm_power_off)(void);
46
47 /* Stuff from the get device id command. */
48 unsigned int mfg_id;
49 unsigned int prod_id;
50 unsigned char capabilities;
51
52 /* We use our own messages for this operation, we don't let the system
53    allocate them, since we may be in a panic situation.  The whole
54    thing is single-threaded, anyway, so multiple messages are not
55    required. */
56 static void dummy_smi_free(struct ipmi_smi_msg *msg)
57 {
58 }
59 static void dummy_recv_free(struct ipmi_recv_msg *msg)
60 {
61 }
62 static struct ipmi_smi_msg halt_smi_msg =
63 {
64         .done = dummy_smi_free
65 };
66 static struct ipmi_recv_msg halt_recv_msg =
67 {
68         .done = dummy_recv_free
69 };
70
71
72 /*
73  * Code to send a message and wait for the reponse.
74  */
75
76 static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
77 {
78         struct semaphore *sem = recv_msg->user_msg_data;
79
80         if (sem)
81                 up(sem);
82 }
83
84 static struct ipmi_user_hndl ipmi_poweroff_handler =
85 {
86         .ipmi_recv_hndl = receive_handler
87 };
88
89
90 static int ipmi_request_wait_for_response(ipmi_user_t            user,
91                                           struct ipmi_addr       *addr,
92                                           struct kernel_ipmi_msg *send_msg)
93 {
94         int              rv;
95         struct semaphore sem;
96
97         sema_init (&sem, 0);
98
99         rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, &sem,
100                                       &halt_smi_msg, &halt_recv_msg, 0);
101         if (rv)
102                 return rv;
103
104         down (&sem);
105
106         return halt_recv_msg.msg.data[0];
107 }
108
109 /* We are in run-to-completion mode, no semaphore is desired. */
110 static int ipmi_request_in_rc_mode(ipmi_user_t            user,
111                                    struct ipmi_addr       *addr,
112                                    struct kernel_ipmi_msg *send_msg)
113 {
114         int              rv;
115
116         rv = ipmi_request_supply_msgs(user, addr, 0, send_msg, NULL,
117                                       &halt_smi_msg, &halt_recv_msg, 0);
118         if (rv)
119                 return rv;
120
121         return halt_recv_msg.msg.data[0];
122 }
123
124 /*
125  * ATCA Support
126  */
127
128 #define IPMI_NETFN_ATCA                 0x2c
129 #define IPMI_ATCA_SET_POWER_CMD         0x11
130 #define IPMI_ATCA_GET_ADDR_INFO_CMD     0x01
131 #define IPMI_PICMG_ID                   0
132
133 static int ipmi_atca_detect (ipmi_user_t user)
134 {
135         struct ipmi_system_interface_addr smi_addr;
136         struct kernel_ipmi_msg            send_msg;
137         int                               rv;
138         unsigned char                     data[1];
139
140         /*
141          * Configure IPMI address for local access
142          */
143         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
144         smi_addr.channel = IPMI_BMC_CHANNEL;
145         smi_addr.lun = 0;
146
147         /*
148          * Use get address info to check and see if we are ATCA
149          */
150         send_msg.netfn = IPMI_NETFN_ATCA;
151         send_msg.cmd = IPMI_ATCA_GET_ADDR_INFO_CMD;
152         data[0] = IPMI_PICMG_ID;
153         send_msg.data = data;
154         send_msg.data_len = sizeof(data);
155         rv = ipmi_request_wait_for_response(user,
156                                             (struct ipmi_addr *) &smi_addr,
157                                             &send_msg);
158         return !rv;
159 }
160
161 static void ipmi_poweroff_atca (ipmi_user_t user)
162 {
163         struct ipmi_system_interface_addr smi_addr;
164         struct kernel_ipmi_msg            send_msg;
165         int                               rv;
166         unsigned char                     data[4];
167
168         /*
169          * Configure IPMI address for local access
170          */
171         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
172         smi_addr.channel = IPMI_BMC_CHANNEL;
173         smi_addr.lun = 0;
174
175         printk(KERN_INFO PFX "Powering down via ATCA power command\n");
176
177         /*
178          * Power down
179          */
180         send_msg.netfn = IPMI_NETFN_ATCA;
181         send_msg.cmd = IPMI_ATCA_SET_POWER_CMD;
182         data[0] = IPMI_PICMG_ID;
183         data[1] = 0; /* FRU id */
184         data[2] = 0; /* Power Level */
185         data[3] = 0; /* Don't change saved presets */
186         send_msg.data = data;
187         send_msg.data_len = sizeof (data);
188         rv = ipmi_request_in_rc_mode(user,
189                                      (struct ipmi_addr *) &smi_addr,
190                                      &send_msg);
191         if (rv) {
192                 printk(KERN_ERR PFX "Unable to send ATCA powerdown message,"
193                        " IPMI error 0x%x\n", rv);
194                 goto out;
195         }
196
197  out:
198         return;
199 }
200
201 /*
202  * CPI1 Support
203  */
204
205 #define IPMI_NETFN_OEM_1                                0xf8
206 #define OEM_GRP_CMD_SET_RESET_STATE             0x84
207 #define OEM_GRP_CMD_SET_POWER_STATE             0x82
208 #define IPMI_NETFN_OEM_8                                0xf8
209 #define OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL        0x80
210 #define OEM_GRP_CMD_GET_SLOT_GA                 0xa3
211 #define IPMI_NETFN_SENSOR_EVT                   0x10
212 #define IPMI_CMD_GET_EVENT_RECEIVER             0x01
213
214 #define IPMI_CPI1_PRODUCT_ID            0x000157
215 #define IPMI_CPI1_MANUFACTURER_ID       0x0108
216
217 static int ipmi_cpi1_detect (ipmi_user_t user)
218 {
219         return ((mfg_id == IPMI_CPI1_MANUFACTURER_ID)
220                 && (prod_id == IPMI_CPI1_PRODUCT_ID));
221 }
222
223 static void ipmi_poweroff_cpi1 (ipmi_user_t user)
224 {
225         struct ipmi_system_interface_addr smi_addr;
226         struct ipmi_ipmb_addr             ipmb_addr;
227         struct kernel_ipmi_msg            send_msg;
228         int                               rv;
229         unsigned char                     data[1];
230         int                               slot;
231         unsigned char                     hotswap_ipmb;
232         unsigned char                     aer_addr;
233         unsigned char                     aer_lun;
234
235         /*
236          * Configure IPMI address for local access
237          */
238         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
239         smi_addr.channel = IPMI_BMC_CHANNEL;
240         smi_addr.lun = 0;
241
242         printk(KERN_INFO PFX "Powering down via CPI1 power command\n");
243
244         /*
245          * Get IPMI ipmb address
246          */
247         send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
248         send_msg.cmd = OEM_GRP_CMD_GET_SLOT_GA;
249         send_msg.data = NULL;
250         send_msg.data_len = 0;
251         rv = ipmi_request_in_rc_mode(user,
252                                      (struct ipmi_addr *) &smi_addr,
253                                      &send_msg);
254         if (rv)
255                 goto out;
256         slot = halt_recv_msg.msg.data[1];
257         hotswap_ipmb = (slot > 9) ? (0xb0 + 2 * slot) : (0xae + 2 * slot);
258
259         /*
260          * Get active event receiver
261          */
262         send_msg.netfn = IPMI_NETFN_SENSOR_EVT >> 2;
263         send_msg.cmd = IPMI_CMD_GET_EVENT_RECEIVER;
264         send_msg.data = NULL;
265         send_msg.data_len = 0;
266         rv = ipmi_request_in_rc_mode(user,
267                                      (struct ipmi_addr *) &smi_addr,
268                                      &send_msg);
269         if (rv)
270                 goto out;
271         aer_addr = halt_recv_msg.msg.data[1];
272         aer_lun = halt_recv_msg.msg.data[2];
273
274         /*
275          * Setup IPMB address target instead of local target
276          */
277         ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
278         ipmb_addr.channel = 0;
279         ipmb_addr.slave_addr = aer_addr;
280         ipmb_addr.lun = aer_lun;
281
282         /*
283          * Send request hotswap control to remove blade from dpv
284          */
285         send_msg.netfn = IPMI_NETFN_OEM_8 >> 2;
286         send_msg.cmd = OEM_GRP_CMD_REQUEST_HOTSWAP_CTRL;
287         send_msg.data = &hotswap_ipmb;
288         send_msg.data_len = 1;
289         ipmi_request_in_rc_mode(user,
290                                 (struct ipmi_addr *) &ipmb_addr,
291                                 &send_msg);
292
293         /*
294          * Set reset asserted
295          */
296         send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
297         send_msg.cmd = OEM_GRP_CMD_SET_RESET_STATE;
298         send_msg.data = data;
299         data[0] = 1; /* Reset asserted state */
300         send_msg.data_len = 1;
301         rv = ipmi_request_in_rc_mode(user,
302                                      (struct ipmi_addr *) &smi_addr,
303                                      &send_msg);
304         if (rv)
305                 goto out;
306
307         /*
308          * Power down
309          */
310         send_msg.netfn = IPMI_NETFN_OEM_1 >> 2;
311         send_msg.cmd = OEM_GRP_CMD_SET_POWER_STATE;
312         send_msg.data = data;
313         data[0] = 1; /* Power down state */
314         send_msg.data_len = 1;
315         rv = ipmi_request_in_rc_mode(user,
316                                      (struct ipmi_addr *) &smi_addr,
317                                      &send_msg);
318         if (rv)
319                 goto out;
320
321  out:
322         return;
323 }
324
325 /*
326  * Standard chassis support
327  */
328
329 #define IPMI_NETFN_CHASSIS_REQUEST      0
330 #define IPMI_CHASSIS_CONTROL_CMD        0x02
331
332 static int ipmi_chassis_detect (ipmi_user_t user)
333 {
334         /* Chassis support, use it. */
335         return (capabilities & 0x80);
336 }
337
338 static void ipmi_poweroff_chassis (ipmi_user_t user)
339 {
340         struct ipmi_system_interface_addr smi_addr;
341         struct kernel_ipmi_msg            send_msg;
342         int                               rv;
343         unsigned char                     data[1];
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 IPMI chassis control command\n");
353
354         /*
355          * Power down
356          */
357         send_msg.netfn = IPMI_NETFN_CHASSIS_REQUEST;
358         send_msg.cmd = IPMI_CHASSIS_CONTROL_CMD;
359         data[0] = 0; /* Power down */
360         send_msg.data = data;
361         send_msg.data_len = sizeof(data);
362         rv = ipmi_request_in_rc_mode(user,
363                                      (struct ipmi_addr *) &smi_addr,
364                                      &send_msg);
365         if (rv) {
366                 printk(KERN_ERR PFX "Unable to send chassis powerdown message,"
367                        " IPMI error 0x%x\n", rv);
368                 goto out;
369         }
370
371  out:
372         return;
373 }
374
375
376 /* Table of possible power off functions. */
377 struct poweroff_function {
378         char *platform_type;
379         int  (*detect)(ipmi_user_t user);
380         void (*poweroff_func)(ipmi_user_t user);
381 };
382
383 static struct poweroff_function poweroff_functions[] = {
384         { "ATCA",    ipmi_atca_detect, ipmi_poweroff_atca },
385         { "CPI1",    ipmi_cpi1_detect, ipmi_poweroff_cpi1 },
386         /* Chassis should generally be last, other things should override
387            it. */
388         { "chassis", ipmi_chassis_detect, ipmi_poweroff_chassis },
389 };
390 #define NUM_PO_FUNCS (sizeof(poweroff_functions) \
391                       / sizeof(struct poweroff_function))
392
393
394 /* Our local state. */
395 static int ready = 0;
396 static ipmi_user_t ipmi_user;
397 static void (*specific_poweroff_func)(ipmi_user_t user) = NULL;
398
399 /* Holds the old poweroff function so we can restore it on removal. */
400 static void (*old_poweroff_func)(void);
401
402
403 /* Called on a powerdown request. */
404 static void ipmi_poweroff_function (void)
405 {
406         if (!ready)
407                 return;
408
409         /* Use run-to-completion mode, since interrupts may be off. */
410         ipmi_user_set_run_to_completion(ipmi_user, 1);
411         specific_poweroff_func(ipmi_user);
412         ipmi_user_set_run_to_completion(ipmi_user, 0);
413 }
414
415 /* Wait for an IPMI interface to be installed, the first one installed
416    will be grabbed by this code and used to perform the powerdown. */
417 static void ipmi_po_new_smi(int if_num)
418 {
419         struct ipmi_system_interface_addr smi_addr;
420         struct kernel_ipmi_msg            send_msg;
421         int                               rv;
422         int                               i;
423
424         if (ready)
425                 return;
426
427         rv = ipmi_create_user(if_num, &ipmi_poweroff_handler, NULL, &ipmi_user);
428         if (rv) {
429                 printk(KERN_ERR PFX "could not create IPMI user, error %d\n",
430                        rv);
431                 return;
432         }
433
434         /*
435          * Do a get device ide and store some results, since this is
436          * used by several functions.
437          */
438         smi_addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
439         smi_addr.channel = IPMI_BMC_CHANNEL;
440         smi_addr.lun = 0;
441
442         send_msg.netfn = IPMI_NETFN_APP_REQUEST;
443         send_msg.cmd = IPMI_GET_DEVICE_ID_CMD;
444         send_msg.data = NULL;
445         send_msg.data_len = 0;
446         rv = ipmi_request_wait_for_response(ipmi_user,
447                                             (struct ipmi_addr *) &smi_addr,
448                                             &send_msg);
449         if (rv) {
450                 printk(KERN_ERR PFX "Unable to send IPMI get device id info,"
451                        " IPMI error 0x%x\n", rv);
452                 goto out_err;
453         }
454
455         if (halt_recv_msg.msg.data_len < 12) {
456                 printk(KERN_ERR PFX "(chassis) IPMI get device id info too,"
457                        " short, was %d bytes, needed %d bytes\n",
458                        halt_recv_msg.msg.data_len, 12);
459                 goto out_err;
460         }
461
462         mfg_id = (halt_recv_msg.msg.data[7]
463                   | (halt_recv_msg.msg.data[8] << 8)
464                   | (halt_recv_msg.msg.data[9] << 16));
465         prod_id = (halt_recv_msg.msg.data[10]
466                    | (halt_recv_msg.msg.data[11] << 8));
467         capabilities = halt_recv_msg.msg.data[6];
468
469
470         /* Scan for a poweroff method */
471         for (i=0; i<NUM_PO_FUNCS; i++) {
472                 if (poweroff_functions[i].detect(ipmi_user))
473                         goto found;
474         }
475
476  out_err:
477         printk(KERN_ERR PFX "Unable to find a poweroff function that"
478                " will work, giving up\n");
479         ipmi_destroy_user(ipmi_user);
480         return;
481
482  found:
483         printk(KERN_INFO PFX "Found a %s style poweroff function\n",
484                poweroff_functions[i].platform_type);
485         specific_poweroff_func = poweroff_functions[i].poweroff_func;
486         old_poweroff_func = pm_power_off;
487         pm_power_off = ipmi_poweroff_function;
488         ready = 1;
489 }
490
491 static void ipmi_po_smi_gone(int if_num)
492 {
493         /* This can never be called, because once poweroff driver is
494            registered, the interface can't go away until the power
495            driver is unregistered. */
496 }
497
498 static struct ipmi_smi_watcher smi_watcher =
499 {
500         .owner    = THIS_MODULE,
501         .new_smi  = ipmi_po_new_smi,
502         .smi_gone = ipmi_po_smi_gone
503 };
504
505
506 /*
507  * Startup and shutdown functions.
508  */
509 static int ipmi_poweroff_init (void)
510 {
511         int rv;
512
513         printk ("Copyright (C) 2004 MontaVista Software -"
514                 " IPMI Powerdown via sys_reboot version "
515                 IPMI_POWEROFF_VERSION ".\n");
516
517         rv = ipmi_smi_watcher_register(&smi_watcher);
518         if (rv)
519                 printk(KERN_ERR PFX "Unable to register SMI watcher: %d\n", rv);
520
521         return rv;
522 }
523
524 #ifdef MODULE
525 static __exit void ipmi_poweroff_cleanup(void)
526 {
527         int rv;
528
529         ipmi_smi_watcher_unregister(&smi_watcher);
530
531         if (ready) {
532                 rv = ipmi_destroy_user(ipmi_user);
533                 if (rv)
534                         printk(KERN_ERR PFX "could not cleanup the IPMI"
535                                " user: 0x%x\n", rv);
536                 pm_power_off = old_poweroff_func;
537         }
538 }
539 module_exit(ipmi_poweroff_cleanup);
540 #endif
541
542 module_init(ipmi_poweroff_init);
543 MODULE_LICENSE("GPL");