blob: 09050797c76a26546ca5fa2d4ed3e165cc9455b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ipmi_msghandler.c
3 *
4 * Incoming and outgoing message routing for an IPMI interface.
5 *
6 * Author: MontaVista Software, Inc.
7 * Corey Minyard <minyard@mvista.com>
8 * source@mvista.com
9 *
10 * Copyright 2002 MontaVista Software Inc.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/errno.h>
36#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/poll.h>
38#include <linux/spinlock.h>
Corey Minyardd6dfd132006-03-31 02:30:41 -080039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/slab.h>
41#include <linux/ipmi.h>
42#include <linux/ipmi_smi.h>
43#include <linux/notifier.h>
44#include <linux/init.h>
45#include <linux/proc_fs.h>
Corey Minyard393d2cc2005-11-07 00:59:54 -080046#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define PFX "IPMI message handler: "
Corey Minyard1fdd75b2005-09-06 15:18:42 -070049
Corey Minyardf7caa1b2008-04-29 01:01:04 -070050#define IPMI_DRIVER_VERSION "39.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
53static int ipmi_init_msghandler(void);
54
Randy Dunlap0c8204b2006-12-10 02:19:06 -080055static int initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Corey Minyard3b625942005-06-23 22:01:42 -070057#ifdef CONFIG_PROC_FS
Randy Dunlap0c8204b2006-12-10 02:19:06 -080058static struct proc_dir_entry *proc_ipmi_root;
Corey Minyard3b625942005-06-23 22:01:42 -070059#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Corey Minyardb9675132006-12-06 20:41:02 -080061/* Remain in auto-maintenance mode for this amount of time (in ms). */
62#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define MAX_EVENTS_IN_QUEUE 25
65
Corey Minyardc70d7492008-04-29 01:01:09 -070066/*
67 * Don't let a message sit in a queue forever, always time it with at lest
68 * the max message timer. This is in milliseconds.
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define MAX_MSG_TIMEOUT 60000
71
Corey Minyard393d2cc2005-11-07 00:59:54 -080072/*
73 * The main "user" data structure.
74 */
Corey Minyardc70d7492008-04-29 01:01:09 -070075struct ipmi_user {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct list_head link;
77
Corey Minyard393d2cc2005-11-07 00:59:54 -080078 /* Set to "0" when the user is destroyed. */
79 int valid;
80
81 struct kref refcount;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* The upper layer that handles receive messages. */
84 struct ipmi_user_hndl *handler;
85 void *handler_data;
86
87 /* The interface this user is bound to. */
88 ipmi_smi_t intf;
89
90 /* Does this interface receive IPMI events? */
91 int gets_events;
92};
93
Corey Minyardc70d7492008-04-29 01:01:09 -070094struct cmd_rcvr {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct list_head link;
96
97 ipmi_user_t user;
98 unsigned char netfn;
99 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -0700100 unsigned int chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800101
102 /*
103 * This is used to form a linked lised during mass deletion.
104 * Since this is in an RCU list, we cannot use the link above
105 * or change any data until the RCU period completes. So we
106 * use this next variable during mass deletion so we can have
107 * a list and don't have to wait and restart the search on
Corey Minyardc70d7492008-04-29 01:01:09 -0700108 * every individual deletion of a command.
109 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800110 struct cmd_rcvr *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Corey Minyardc70d7492008-04-29 01:01:09 -0700113struct seq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int inuse : 1;
115 unsigned int broadcast : 1;
116
117 unsigned long timeout;
118 unsigned long orig_timeout;
119 unsigned int retries_left;
120
Corey Minyardc70d7492008-04-29 01:01:09 -0700121 /*
122 * To verify on an incoming send message response that this is
123 * the message that the response is for, we keep a sequence id
124 * and increment it every time we send a message.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 long seqid;
127
Corey Minyardc70d7492008-04-29 01:01:09 -0700128 /*
129 * This is held so we can properly respond to the message on a
130 * timeout, and it is used to hold the temporary data for
131 * retransmission, too.
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct ipmi_recv_msg *recv_msg;
134};
135
Corey Minyardc70d7492008-04-29 01:01:09 -0700136/*
137 * Store the information in a msgid (long) to allow us to find a
138 * sequence table entry from the msgid.
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
141
142#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
143 do { \
144 seq = ((msgid >> 26) & 0x3f); \
145 seqid = (msgid & 0x3fffff); \
Corey Minyardc70d7492008-04-29 01:01:09 -0700146 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
149
Corey Minyardc70d7492008-04-29 01:01:09 -0700150struct ipmi_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned char medium;
152 unsigned char protocol;
Corey Minyardc14979b2005-09-06 15:18:38 -0700153
Corey Minyardc70d7492008-04-29 01:01:09 -0700154 /*
155 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
156 * but may be changed by the user.
157 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700158 unsigned char address;
159
Corey Minyardc70d7492008-04-29 01:01:09 -0700160 /*
161 * My LUN. This should generally stay the SMS LUN, but just in
162 * case...
163 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700164 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Corey Minyard3b625942005-06-23 22:01:42 -0700167#ifdef CONFIG_PROC_FS
Corey Minyardc70d7492008-04-29 01:01:09 -0700168struct ipmi_proc_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 char *name;
170 struct ipmi_proc_entry *next;
171};
Corey Minyard3b625942005-06-23 22:01:42 -0700172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Corey Minyardc70d7492008-04-29 01:01:09 -0700174struct bmc_device {
Corey Minyard50c812b2006-03-26 01:37:21 -0800175 struct platform_device *dev;
176 struct ipmi_device_id id;
177 unsigned char guid[16];
178 int guid_set;
179
180 struct kref refcount;
181
182 /* bmc device attributes */
183 struct device_attribute device_id_attr;
184 struct device_attribute provides_dev_sdrs_attr;
185 struct device_attribute revision_attr;
186 struct device_attribute firmware_rev_attr;
187 struct device_attribute version_attr;
188 struct device_attribute add_dev_support_attr;
189 struct device_attribute manufacturer_id_attr;
190 struct device_attribute product_id_attr;
191 struct device_attribute guid_attr;
192 struct device_attribute aux_firmware_rev_attr;
193};
194
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700195/*
196 * Various statistics for IPMI, these index stats[] in the ipmi_smi
197 * structure.
198 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700199enum ipmi_stat_indexes {
200 /* Commands we got from the user that were invalid. */
201 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700202
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700203 /* Commands we sent to the MC. */
204 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700205
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700206 /* Responses from the MC that were delivered to a user. */
207 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700208
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700209 /* Responses from the MC that were not delivered to a user. */
210 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700211
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700212 /* Commands we sent out to the IPMB bus. */
213 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700214
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700215 /* Commands sent on the IPMB that had errors on the SEND CMD */
216 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700217
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700218 /* Each retransmit increments this count. */
219 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700220
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700221 /*
222 * When a message times out (runs out of retransmits) this is
223 * incremented.
224 */
225 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700226
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700227 /*
228 * This is like above, but for broadcasts. Broadcasts are
229 * *not* included in the above count (they are expected to
230 * time out).
231 */
232 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700233
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700234 /* Responses I have sent to the IPMB bus. */
235 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700236
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700237 /* The response was delivered to the user. */
238 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700239
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700240 /* The response had invalid data in it. */
241 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700242
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700243 /* The response didn't have anyone waiting for it. */
244 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700245
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700246 /* Commands we sent out to the IPMB bus. */
247 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700248
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700249 /* Commands sent on the IPMB that had errors on the SEND CMD */
250 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700251
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700252 /* Each retransmit increments this count. */
253 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700254
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700255 /*
256 * When a message times out (runs out of retransmits) this is
257 * incremented.
258 */
259 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700260
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700261 /* Responses I have sent to the IPMB bus. */
262 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700263
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700264 /* The response was delivered to the user. */
265 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700266
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700267 /* The response had invalid data in it. */
268 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700269
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700270 /* The response didn't have anyone waiting for it. */
271 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700272
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700273 /* The command was delivered to the user. */
274 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700275
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700276 /* The command had invalid data in it. */
277 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700278
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700279 /* The command didn't have anyone waiting for it. */
280 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700281
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700282 /* Invalid data in an event. */
283 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700284
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700285 /* Events that were received with the proper format. */
286 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700287
Corey Minyard25176ed2009-04-21 12:24:04 -0700288 /* Retransmissions on IPMB that failed. */
289 IPMI_STAT_dropped_rexmit_ipmb_commands,
290
291 /* Retransmissions on LAN that failed. */
292 IPMI_STAT_dropped_rexmit_lan_commands,
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700293
294 /* This *must* remain last, add new values above this. */
295 IPMI_NUM_STATS
296};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700297
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700300#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700301struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* What interface number are we? */
303 int intf_num;
304
Corey Minyard393d2cc2005-11-07 00:59:54 -0800305 struct kref refcount;
306
Corey Minyardbca03242006-12-06 20:40:57 -0800307 /* Used for a list of interfaces. */
308 struct list_head link;
309
Corey Minyardc70d7492008-04-29 01:01:09 -0700310 /*
311 * The list of upper layers that are using me. seq_lock
312 * protects this.
313 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800314 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Corey Minyardb2c03942006-12-06 20:41:00 -0800316 /* Information to supply to users. */
317 unsigned char ipmi_version_major;
318 unsigned char ipmi_version_minor;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* Used for wake ups at startup. */
321 wait_queue_head_t waitq;
322
Corey Minyard50c812b2006-03-26 01:37:21 -0800323 struct bmc_device *bmc;
324 char *my_dev_name;
Corey Minyard759643b2006-12-06 20:40:59 -0800325 char *sysfs_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Corey Minyardc70d7492008-04-29 01:01:09 -0700327 /*
328 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800329 * must either be holding the ipmi_interfaces_mutex or be in
330 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700331 * value into a local variable and make sure it is not NULL.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct ipmi_smi_handlers *handlers;
334 void *send_info;
335
Corey Minyard3b625942005-06-23 22:01:42 -0700336#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700337 /* A list of proc entries for this interface. */
338 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Corey Minyard50c812b2006-03-26 01:37:21 -0800342 /* Driver-model device for the system interface. */
343 struct device *si_dev;
344
Corey Minyardc70d7492008-04-29 01:01:09 -0700345 /*
346 * A table of sequence numbers for this interface. We use the
347 * sequence numbers for IPMB messages that go out of the
348 * interface to match them up with their responses. A routine
349 * is called periodically to time the items in this list.
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 spinlock_t seq_lock;
352 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
353 int curr_seq;
354
Corey Minyardc70d7492008-04-29 01:01:09 -0700355 /*
356 * Messages that were delayed for some reason (out of memory,
357 * for instance), will go in here to be processed later in a
358 * periodic timer interrupt.
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spinlock_t waiting_msgs_lock;
361 struct list_head waiting_msgs;
362
Corey Minyardc70d7492008-04-29 01:01:09 -0700363 /*
364 * The list of command receivers that are registered for commands
365 * on this interface.
366 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800367 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct list_head cmd_rcvrs;
369
Corey Minyardc70d7492008-04-29 01:01:09 -0700370 /*
371 * Events that were queues because no one was there to receive
372 * them.
373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spinlock_t events_lock; /* For dealing with event stuff. */
375 struct list_head waiting_events;
376 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700377 char delivering_events;
378 char event_msg_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Corey Minyardc70d7492008-04-29 01:01:09 -0700380 /*
381 * The event receiver for my BMC, only really used at panic
382 * shutdown as a place to store this.
383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned char event_receiver;
385 unsigned char event_receiver_lun;
386 unsigned char local_sel_device;
387 unsigned char local_event_generator;
388
Corey Minyardb9675132006-12-06 20:41:02 -0800389 /* For handling of maintenance mode. */
390 int maintenance_mode;
391 int maintenance_mode_enable;
392 int auto_maintenance_timeout;
393 spinlock_t maintenance_mode_lock; /* Used in a timer... */
394
Corey Minyardc70d7492008-04-29 01:01:09 -0700395 /*
396 * A cheap hack, if this is non-null and a message to an
397 * interface comes in with a NULL user, call this routine with
398 * it. Note that the message will still be freed by the
399 * caller. This only works on the system interface.
400 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700401 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Corey Minyardc70d7492008-04-29 01:01:09 -0700403 /*
404 * When we are scanning the channels for an SMI, this will
405 * tell which channel we are scanning.
406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int curr_channel;
408
409 /* Channel information */
410 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
411
412 /* Proc FS stuff. */
413 struct proc_dir_entry *proc_dir;
414 char proc_dir_name[10];
415
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700416 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700417
418 /*
419 * run_to_completion duplicate of smb_info, smi_info
420 * and ipmi_serial_info structures. Used to decrease numbers of
421 * parameters passed by "low" level IPMI code.
422 */
423 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424};
Corey Minyard50c812b2006-03-26 01:37:21 -0800425#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Corey Minyard50c812b2006-03-26 01:37:21 -0800427/**
428 * The driver model view of the IPMI messaging driver.
429 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800430static struct platform_driver ipmidriver = {
431 .driver = {
432 .name = "ipmi",
433 .bus = &platform_bus_type
434 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800435};
436static DEFINE_MUTEX(ipmidriver_mutex);
437
Denis Chengbed97592008-02-06 01:37:35 -0800438static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800439static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Corey Minyardc70d7492008-04-29 01:01:09 -0700441/*
442 * List of watchers that want to know when smi's are added and deleted.
443 */
Denis Chengbed97592008-02-06 01:37:35 -0800444static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800445static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700448#define ipmi_inc_stat(intf, stat) \
449 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
450#define ipmi_get_stat(intf, stat) \
451 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
452
Corey Minyard25176ed2009-04-21 12:24:04 -0700453static int is_lan_addr(struct ipmi_addr *addr)
454{
455 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
456}
457
458static int is_ipmb_addr(struct ipmi_addr *addr)
459{
460 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
461}
462
463static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
464{
465 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
466}
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700467
Corey Minyard393d2cc2005-11-07 00:59:54 -0800468static void free_recv_msg_list(struct list_head *q)
469{
470 struct ipmi_recv_msg *msg, *msg2;
471
472 list_for_each_entry_safe(msg, msg2, q, link) {
473 list_del(&msg->link);
474 ipmi_free_recv_msg(msg);
475 }
476}
477
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800478static void free_smi_msg_list(struct list_head *q)
479{
480 struct ipmi_smi_msg *msg, *msg2;
481
482 list_for_each_entry_safe(msg, msg2, q, link) {
483 list_del(&msg->link);
484 ipmi_free_smi_msg(msg);
485 }
486}
487
Corey Minyard393d2cc2005-11-07 00:59:54 -0800488static void clean_up_interface_data(ipmi_smi_t intf)
489{
490 int i;
491 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800492 struct list_head list;
493
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800494 free_smi_msg_list(&intf->waiting_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800495 free_recv_msg_list(&intf->waiting_events);
496
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800497 /*
498 * Wholesale remove all the entries from the list in the
499 * interface and wait for RCU to know that none are in use.
500 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800501 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800502 INIT_LIST_HEAD(&list);
503 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800504 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800505
506 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
507 kfree(rcvr);
508
509 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
510 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700511 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800512 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800514}
515
516static void intf_free(struct kref *ref)
517{
518 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
519
520 clean_up_interface_data(intf);
521 kfree(intf);
522}
523
Corey Minyardbca03242006-12-06 20:40:57 -0800524struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800525 int intf_num;
526 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800527 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800528};
529
Corey Minyard393d2cc2005-11-07 00:59:54 -0800530int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
531{
Corey Minyardbca03242006-12-06 20:40:57 -0800532 ipmi_smi_t intf;
Denis Chenge381d1c2008-02-06 01:37:39 -0800533 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800534 struct watcher_entry *e, *e2;
535
Corey Minyardb2c03942006-12-06 20:41:00 -0800536 mutex_lock(&smi_watchers_mutex);
537
Corey Minyardbca03242006-12-06 20:40:57 -0800538 mutex_lock(&ipmi_interfaces_mutex);
539
Corey Minyardb2c03942006-12-06 20:41:00 -0800540 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800541 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800542 if (intf->intf_num == -1)
543 continue;
544 e = kmalloc(sizeof(*e), GFP_KERNEL);
545 if (!e)
546 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800547 kref_get(&intf->refcount);
548 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800549 e->intf_num = intf->intf_num;
550 list_add_tail(&e->link, &to_deliver);
551 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800552
Corey Minyardb2c03942006-12-06 20:41:00 -0800553 /* We will succeed, so add it to the list. */
554 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800555
556 mutex_unlock(&ipmi_interfaces_mutex);
557
558 list_for_each_entry_safe(e, e2, &to_deliver, link) {
559 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800560 watcher->new_smi(e->intf_num, e->intf->si_dev);
561 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800562 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800563 }
Corey Minyardbca03242006-12-06 20:40:57 -0800564
Corey Minyardb2c03942006-12-06 20:41:00 -0800565 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800568
569 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800570 mutex_unlock(&ipmi_interfaces_mutex);
571 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800572 list_for_each_entry_safe(e, e2, &to_deliver, link) {
573 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800574 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800575 kfree(e);
576 }
577 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
Corey Minyardc70d7492008-04-29 01:01:09 -0700579EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
582{
Corey Minyardb2c03942006-12-06 20:41:00 -0800583 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800585 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
587}
Corey Minyardc70d7492008-04-29 01:01:09 -0700588EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Corey Minyardb2c03942006-12-06 20:41:00 -0800590/*
591 * Must be called with smi_watchers_mutex held.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800594call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct ipmi_smi_watcher *w;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 list_for_each_entry(w, &smi_watchers, link) {
599 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800600 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 module_put(w->owner);
602 }
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606static int
607ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
608{
609 if (addr1->addr_type != addr2->addr_type)
610 return 0;
611
612 if (addr1->channel != addr2->channel)
613 return 0;
614
615 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
616 struct ipmi_system_interface_addr *smi_addr1
617 = (struct ipmi_system_interface_addr *) addr1;
618 struct ipmi_system_interface_addr *smi_addr2
619 = (struct ipmi_system_interface_addr *) addr2;
620 return (smi_addr1->lun == smi_addr2->lun);
621 }
622
Corey Minyard25176ed2009-04-21 12:24:04 -0700623 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct ipmi_ipmb_addr *ipmb_addr1
625 = (struct ipmi_ipmb_addr *) addr1;
626 struct ipmi_ipmb_addr *ipmb_addr2
627 = (struct ipmi_ipmb_addr *) addr2;
628
629 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
630 && (ipmb_addr1->lun == ipmb_addr2->lun));
631 }
632
Corey Minyard25176ed2009-04-21 12:24:04 -0700633 if (is_lan_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct ipmi_lan_addr *lan_addr1
635 = (struct ipmi_lan_addr *) addr1;
636 struct ipmi_lan_addr *lan_addr2
637 = (struct ipmi_lan_addr *) addr2;
638
639 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
640 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
641 && (lan_addr1->session_handle
642 == lan_addr2->session_handle)
643 && (lan_addr1->lun == lan_addr2->lun));
644 }
645
646 return 1;
647}
648
649int ipmi_validate_addr(struct ipmi_addr *addr, int len)
650{
Corey Minyardc70d7492008-04-29 01:01:09 -0700651 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
655 if (addr->channel != IPMI_BMC_CHANNEL)
656 return -EINVAL;
657 return 0;
658 }
659
660 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800661 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 || (addr->channel < 0))
663 return -EINVAL;
664
Corey Minyard25176ed2009-04-21 12:24:04 -0700665 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700666 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669 }
670
Corey Minyard25176ed2009-04-21 12:24:04 -0700671 if (is_lan_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700672 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675 }
676
677 return -EINVAL;
678}
Corey Minyardc70d7492008-04-29 01:01:09 -0700679EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681unsigned int ipmi_addr_length(int addr_type)
682{
683 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
684 return sizeof(struct ipmi_system_interface_addr);
685
686 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700687 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (addr_type == IPMI_LAN_ADDR_TYPE)
691 return sizeof(struct ipmi_lan_addr);
692
693 return 0;
694}
Corey Minyardc70d7492008-04-29 01:01:09 -0700695EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697static void deliver_response(struct ipmi_recv_msg *msg)
698{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800699 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700700 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700701
702 /* Special handling for NULL users. */
703 if (intf->null_user_handler) {
704 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700705 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700706 } else {
707 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700708 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700709 }
710 ipmi_free_recv_msg(msg);
711 } else {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800712 ipmi_user_t user = msg->user;
713 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Corey Minyardb2c03942006-12-06 20:41:00 -0800717static void
718deliver_err_response(struct ipmi_recv_msg *msg, int err)
719{
720 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
721 msg->msg_data[0] = err;
722 msg->msg.netfn |= 1; /* Convert to a response. */
723 msg->msg.data_len = 1;
724 msg->msg.data = msg->msg_data;
725 deliver_response(msg);
726}
727
Corey Minyardc70d7492008-04-29 01:01:09 -0700728/*
729 * Find the next sequence number not being used and add the given
730 * message with the given timeout to the sequence table. This must be
731 * called with the interface's seq_lock held.
732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int intf_next_seq(ipmi_smi_t intf,
734 struct ipmi_recv_msg *recv_msg,
735 unsigned long timeout,
736 int retries,
737 int broadcast,
738 unsigned char *seq,
739 long *seqid)
740{
741 int rv = 0;
742 unsigned int i;
743
Corey Minyardc70d7492008-04-29 01:01:09 -0700744 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
745 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800746 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 }
749
Corey Minyard8a3628d2006-03-31 02:30:40 -0800750 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 intf->seq_table[i].recv_msg = recv_msg;
752
Corey Minyardc70d7492008-04-29 01:01:09 -0700753 /*
754 * Start with the maximum timeout, when the send response
755 * comes in we will start the real timer.
756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
758 intf->seq_table[i].orig_timeout = timeout;
759 intf->seq_table[i].retries_left = retries;
760 intf->seq_table[i].broadcast = broadcast;
761 intf->seq_table[i].inuse = 1;
762 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
763 *seq = i;
764 *seqid = intf->seq_table[i].seqid;
765 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
766 } else {
767 rv = -EAGAIN;
768 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return rv;
771}
772
Corey Minyardc70d7492008-04-29 01:01:09 -0700773/*
774 * Return the receive message for the given sequence number and
775 * release the sequence number so it can be reused. Some other data
776 * is passed in to be sure the message matches up correctly (to help
777 * guard against message coming in after their timeout and the
778 * sequence number being reused).
779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static int intf_find_seq(ipmi_smi_t intf,
781 unsigned char seq,
782 short channel,
783 unsigned char cmd,
784 unsigned char netfn,
785 struct ipmi_addr *addr,
786 struct ipmi_recv_msg **recv_msg)
787{
788 int rv = -ENODEV;
789 unsigned long flags;
790
791 if (seq >= IPMI_IPMB_NUM_SEQ)
792 return -EINVAL;
793
794 spin_lock_irqsave(&(intf->seq_lock), flags);
795 if (intf->seq_table[seq].inuse) {
796 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
797
Corey Minyardc70d7492008-04-29 01:01:09 -0700798 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
799 && (msg->msg.netfn == netfn)
800 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *recv_msg = msg;
802 intf->seq_table[seq].inuse = 0;
803 rv = 0;
804 }
805 }
806 spin_unlock_irqrestore(&(intf->seq_lock), flags);
807
808 return rv;
809}
810
811
812/* Start the timer for a specific sequence table entry. */
813static int intf_start_seq_timer(ipmi_smi_t intf,
814 long msgid)
815{
816 int rv = -ENODEV;
817 unsigned long flags;
818 unsigned char seq;
819 unsigned long seqid;
820
821
822 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
823
824 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700825 /*
826 * We do this verification because the user can be deleted
827 * while a message is outstanding.
828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700830 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct seq_table *ent = &(intf->seq_table[seq]);
832 ent->timeout = ent->orig_timeout;
833 rv = 0;
834 }
835 spin_unlock_irqrestore(&(intf->seq_lock), flags);
836
837 return rv;
838}
839
840/* Got an error for the send message for a specific sequence number. */
841static int intf_err_seq(ipmi_smi_t intf,
842 long msgid,
843 unsigned int err)
844{
845 int rv = -ENODEV;
846 unsigned long flags;
847 unsigned char seq;
848 unsigned long seqid;
849 struct ipmi_recv_msg *msg = NULL;
850
851
852 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
853
854 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700855 /*
856 * We do this verification because the user can be deleted
857 * while a message is outstanding.
858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700860 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct seq_table *ent = &(intf->seq_table[seq]);
862
863 ent->inuse = 0;
864 msg = ent->recv_msg;
865 rv = 0;
866 }
867 spin_unlock_irqrestore(&(intf->seq_lock), flags);
868
Corey Minyardb2c03942006-12-06 20:41:00 -0800869 if (msg)
870 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return rv;
873}
874
875
876int ipmi_create_user(unsigned int if_num,
877 struct ipmi_user_hndl *handler,
878 void *handler_data,
879 ipmi_user_t *user)
880{
881 unsigned long flags;
882 ipmi_user_t new_user;
883 int rv = 0;
884 ipmi_smi_t intf;
885
Corey Minyardc70d7492008-04-29 01:01:09 -0700886 /*
887 * There is no module usecount here, because it's not
888 * required. Since this can only be used by and called from
889 * other modules, they will implicitly use this module, and
890 * thus this can't be removed unless the other modules are
891 * removed.
892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (handler == NULL)
895 return -EINVAL;
896
Corey Minyardc70d7492008-04-29 01:01:09 -0700897 /*
898 * Make sure the driver is actually initialized, this handles
899 * problems with initialization order.
900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (!initialized) {
902 rv = ipmi_init_msghandler();
903 if (rv)
904 return rv;
905
Corey Minyardc70d7492008-04-29 01:01:09 -0700906 /*
907 * The init code doesn't return an error if it was turned
908 * off, but it won't initialize. Check that.
909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!initialized)
911 return -ENODEV;
912 }
913
914 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800915 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENOMEM;
917
Corey Minyardb2c03942006-12-06 20:41:00 -0800918 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800919 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
920 if (intf->intf_num == if_num)
921 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800923 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800924 rv = -EINVAL;
925 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Corey Minyardbca03242006-12-06 20:40:57 -0800927 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800928 /* Note that each existing user holds a refcount to the interface. */
929 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Corey Minyard393d2cc2005-11-07 00:59:54 -0800931 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 new_user->handler = handler;
933 new_user->handler_data = handler_data;
934 new_user->intf = intf;
935 new_user->gets_events = 0;
936
937 if (!try_module_get(intf->handlers->owner)) {
938 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800939 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 if (intf->handlers->inc_usecount) {
943 rv = intf->handlers->inc_usecount(intf->send_info);
944 if (rv) {
945 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800946 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 }
949
Corey Minyardc70d7492008-04-29 01:01:09 -0700950 /*
951 * Hold the lock so intf->handlers is guaranteed to be good
952 * until now
953 */
Corey Minyardb2c03942006-12-06 20:41:00 -0800954 mutex_unlock(&ipmi_interfaces_mutex);
955
Corey Minyard393d2cc2005-11-07 00:59:54 -0800956 new_user->valid = 1;
957 spin_lock_irqsave(&intf->seq_lock, flags);
958 list_add_rcu(&new_user->link, &intf->users);
959 spin_unlock_irqrestore(&intf->seq_lock, flags);
960 *user = new_user;
961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Adrian Bunk5c98d292006-03-25 03:07:52 -0800963out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800964 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800965out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -0800966 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800967 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return rv;
969}
Corey Minyardc70d7492008-04-29 01:01:09 -0700970EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Corey Minyard393d2cc2005-11-07 00:59:54 -0800972static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800974 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978int ipmi_destroy_user(ipmi_user_t user)
979{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800980 ipmi_smi_t intf = user->intf;
981 int i;
982 unsigned long flags;
983 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800984 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Corey Minyard8a3628d2006-03-31 02:30:40 -0800986 user->valid = 0;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800987
988 /* Remove the user from the interface's sequence table. */
989 spin_lock_irqsave(&intf->seq_lock, flags);
990 list_del_rcu(&user->link);
991
992 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
993 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -0700994 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800995 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -0800996 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800999 spin_unlock_irqrestore(&intf->seq_lock, flags);
1000
1001 /*
1002 * Remove the user from the command receiver's table. First
1003 * we build a list of everything (not using the standard link,
1004 * since other things may be using it till we do
1005 * synchronize_rcu()) then free everything in that list.
1006 */
Corey Minyardd6dfd132006-03-31 02:30:41 -08001007 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -08001008 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001009 if (rcvr->user == user) {
1010 list_del_rcu(&rcvr->link);
1011 rcvr->next = rcvrs;
1012 rcvrs = rcvr;
1013 }
1014 }
Corey Minyardd6dfd132006-03-31 02:30:41 -08001015 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001016 synchronize_rcu();
1017 while (rcvrs) {
1018 rcvr = rcvrs;
1019 rcvrs = rcvr->next;
1020 kfree(rcvr);
1021 }
1022
Corey Minyardb2c03942006-12-06 20:41:00 -08001023 mutex_lock(&ipmi_interfaces_mutex);
1024 if (intf->handlers) {
1025 module_put(intf->handlers->owner);
1026 if (intf->handlers->dec_usecount)
1027 intf->handlers->dec_usecount(intf->send_info);
1028 }
1029 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001030
1031 kref_put(&intf->refcount, intf_free);
1032
1033 kref_put(&user->refcount, free_user);
1034
Corey Minyard8a3628d2006-03-31 02:30:40 -08001035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
Corey Minyardc70d7492008-04-29 01:01:09 -07001037EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039void ipmi_get_version(ipmi_user_t user,
1040 unsigned char *major,
1041 unsigned char *minor)
1042{
Corey Minyardb2c03942006-12-06 20:41:00 -08001043 *major = user->intf->ipmi_version_major;
1044 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
Corey Minyardc70d7492008-04-29 01:01:09 -07001046EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Corey Minyardc14979b2005-09-06 15:18:38 -07001048int ipmi_set_my_address(ipmi_user_t user,
1049 unsigned int channel,
1050 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Corey Minyardc14979b2005-09-06 15:18:38 -07001052 if (channel >= IPMI_MAX_CHANNELS)
1053 return -EINVAL;
1054 user->intf->channels[channel].address = address;
1055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
Corey Minyardc70d7492008-04-29 01:01:09 -07001057EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Corey Minyardc14979b2005-09-06 15:18:38 -07001059int ipmi_get_my_address(ipmi_user_t user,
1060 unsigned int channel,
1061 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Corey Minyardc14979b2005-09-06 15:18:38 -07001063 if (channel >= IPMI_MAX_CHANNELS)
1064 return -EINVAL;
1065 *address = user->intf->channels[channel].address;
1066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Corey Minyardc70d7492008-04-29 01:01:09 -07001068EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Corey Minyardc14979b2005-09-06 15:18:38 -07001070int ipmi_set_my_LUN(ipmi_user_t user,
1071 unsigned int channel,
1072 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Corey Minyardc14979b2005-09-06 15:18:38 -07001074 if (channel >= IPMI_MAX_CHANNELS)
1075 return -EINVAL;
1076 user->intf->channels[channel].lun = LUN & 0x3;
1077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
Corey Minyardc70d7492008-04-29 01:01:09 -07001079EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Corey Minyardc14979b2005-09-06 15:18:38 -07001081int ipmi_get_my_LUN(ipmi_user_t user,
1082 unsigned int channel,
1083 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Corey Minyardc14979b2005-09-06 15:18:38 -07001085 if (channel >= IPMI_MAX_CHANNELS)
1086 return -EINVAL;
1087 *address = user->intf->channels[channel].lun;
1088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
Corey Minyardc70d7492008-04-29 01:01:09 -07001090EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Corey Minyardb9675132006-12-06 20:41:02 -08001092int ipmi_get_maintenance_mode(ipmi_user_t user)
1093{
1094 int mode;
1095 unsigned long flags;
1096
1097 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1098 mode = user->intf->maintenance_mode;
1099 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1100
1101 return mode;
1102}
1103EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1104
1105static void maintenance_mode_update(ipmi_smi_t intf)
1106{
1107 if (intf->handlers->set_maintenance_mode)
1108 intf->handlers->set_maintenance_mode(
1109 intf->send_info, intf->maintenance_mode_enable);
1110}
1111
1112int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1113{
1114 int rv = 0;
1115 unsigned long flags;
1116 ipmi_smi_t intf = user->intf;
1117
1118 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1119 if (intf->maintenance_mode != mode) {
1120 switch (mode) {
1121 case IPMI_MAINTENANCE_MODE_AUTO:
1122 intf->maintenance_mode = mode;
1123 intf->maintenance_mode_enable
1124 = (intf->auto_maintenance_timeout > 0);
1125 break;
1126
1127 case IPMI_MAINTENANCE_MODE_OFF:
1128 intf->maintenance_mode = mode;
1129 intf->maintenance_mode_enable = 0;
1130 break;
1131
1132 case IPMI_MAINTENANCE_MODE_ON:
1133 intf->maintenance_mode = mode;
1134 intf->maintenance_mode_enable = 1;
1135 break;
1136
1137 default:
1138 rv = -EINVAL;
1139 goto out_unlock;
1140 }
1141
1142 maintenance_mode_update(intf);
1143 }
1144 out_unlock:
1145 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1146
1147 return rv;
1148}
1149EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151int ipmi_set_gets_events(ipmi_user_t user, int val)
1152{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001153 unsigned long flags;
1154 ipmi_smi_t intf = user->intf;
1155 struct ipmi_recv_msg *msg, *msg2;
1156 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Corey Minyard393d2cc2005-11-07 00:59:54 -08001158 INIT_LIST_HEAD(&msgs);
1159
1160 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 user->gets_events = val;
1162
Corey Minyardb2c03942006-12-06 20:41:00 -08001163 if (intf->delivering_events)
1164 /*
1165 * Another thread is delivering events for this, so
1166 * let it handle any new events.
1167 */
1168 goto out;
1169
1170 /* Deliver any queued events. */
1171 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001172 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1173 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001174 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001175 if (intf->event_msg_printed) {
1176 printk(KERN_WARNING PFX "Event queue no longer"
1177 " full\n");
1178 intf->event_msg_printed = 0;
1179 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001180
1181 intf->delivering_events = 1;
1182 spin_unlock_irqrestore(&intf->events_lock, flags);
1183
1184 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1185 msg->user = user;
1186 kref_get(&user->refcount);
1187 deliver_response(msg);
1188 }
1189
1190 spin_lock_irqsave(&intf->events_lock, flags);
1191 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001193
Corey Minyardb2c03942006-12-06 20:41:00 -08001194 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001195 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 return 0;
1198}
Corey Minyardc70d7492008-04-29 01:01:09 -07001199EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Corey Minyard393d2cc2005-11-07 00:59:54 -08001201static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1202 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001203 unsigned char cmd,
1204 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001205{
1206 struct cmd_rcvr *rcvr;
1207
1208 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001209 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1210 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001211 return rcvr;
1212 }
1213 return NULL;
1214}
1215
Corey Minyardc69c3122006-09-30 23:27:56 -07001216static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1217 unsigned char netfn,
1218 unsigned char cmd,
1219 unsigned int chans)
1220{
1221 struct cmd_rcvr *rcvr;
1222
1223 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1224 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1225 && (rcvr->chans & chans))
1226 return 0;
1227 }
1228 return 1;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231int ipmi_register_for_cmd(ipmi_user_t user,
1232 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001233 unsigned char cmd,
1234 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001236 ipmi_smi_t intf = user->intf;
1237 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001238 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240
1241 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001242 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001244 rcvr->cmd = cmd;
1245 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001246 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001247 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Corey Minyardd6dfd132006-03-31 02:30:41 -08001249 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001251 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001252 rv = -EBUSY;
1253 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Corey Minyard393d2cc2005-11-07 00:59:54 -08001256 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001257
Corey Minyard393d2cc2005-11-07 00:59:54 -08001258 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001259 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (rv)
1261 kfree(rcvr);
1262
1263 return rv;
1264}
Corey Minyardc70d7492008-04-29 01:01:09 -07001265EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267int ipmi_unregister_for_cmd(ipmi_user_t user,
1268 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001269 unsigned char cmd,
1270 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001272 ipmi_smi_t intf = user->intf;
1273 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001274 struct cmd_rcvr *rcvrs = NULL;
1275 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Corey Minyardd6dfd132006-03-31 02:30:41 -08001277 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001278 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1279 if (((1 << i) & chans) == 0)
1280 continue;
1281 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1282 if (rcvr == NULL)
1283 continue;
1284 if (rcvr->user == user) {
1285 rv = 0;
1286 rcvr->chans &= ~chans;
1287 if (rcvr->chans == 0) {
1288 list_del_rcu(&rcvr->link);
1289 rcvr->next = rcvrs;
1290 rcvrs = rcvr;
1291 }
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001294 mutex_unlock(&intf->cmd_rcvrs_mutex);
1295 synchronize_rcu();
1296 while (rcvrs) {
1297 rcvr = rcvrs;
1298 rcvrs = rcvr->next;
1299 kfree(rcvr);
1300 }
1301 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Corey Minyardc70d7492008-04-29 01:01:09 -07001303EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static unsigned char
1306ipmb_checksum(unsigned char *data, int size)
1307{
1308 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 for (; size > 0; size--, data++)
1311 csum += *data;
1312
1313 return -csum;
1314}
1315
1316static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1317 struct kernel_ipmi_msg *msg,
1318 struct ipmi_ipmb_addr *ipmb_addr,
1319 long msgid,
1320 unsigned char ipmb_seq,
1321 int broadcast,
1322 unsigned char source_address,
1323 unsigned char source_lun)
1324{
1325 int i = broadcast;
1326
1327 /* Format the IPMB header data. */
1328 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1329 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1330 smi_msg->data[2] = ipmb_addr->channel;
1331 if (broadcast)
1332 smi_msg->data[3] = 0;
1333 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1334 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1335 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1336 smi_msg->data[i+6] = source_address;
1337 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1338 smi_msg->data[i+8] = msg->cmd;
1339
1340 /* Now tack on the data to the message. */
1341 if (msg->data_len > 0)
1342 memcpy(&(smi_msg->data[i+9]), msg->data,
1343 msg->data_len);
1344 smi_msg->data_size = msg->data_len + 9;
1345
1346 /* Now calculate the checksum and tack it on. */
1347 smi_msg->data[i+smi_msg->data_size]
1348 = ipmb_checksum(&(smi_msg->data[i+6]),
1349 smi_msg->data_size-6);
1350
Corey Minyardc70d7492008-04-29 01:01:09 -07001351 /*
1352 * Add on the checksum size and the offset from the
1353 * broadcast.
1354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 smi_msg->data_size += 1 + i;
1356
1357 smi_msg->msgid = msgid;
1358}
1359
1360static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1361 struct kernel_ipmi_msg *msg,
1362 struct ipmi_lan_addr *lan_addr,
1363 long msgid,
1364 unsigned char ipmb_seq,
1365 unsigned char source_lun)
1366{
1367 /* Format the IPMB header data. */
1368 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1369 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1370 smi_msg->data[2] = lan_addr->channel;
1371 smi_msg->data[3] = lan_addr->session_handle;
1372 smi_msg->data[4] = lan_addr->remote_SWID;
1373 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1374 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1375 smi_msg->data[7] = lan_addr->local_SWID;
1376 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1377 smi_msg->data[9] = msg->cmd;
1378
1379 /* Now tack on the data to the message. */
1380 if (msg->data_len > 0)
1381 memcpy(&(smi_msg->data[10]), msg->data,
1382 msg->data_len);
1383 smi_msg->data_size = msg->data_len + 10;
1384
1385 /* Now calculate the checksum and tack it on. */
1386 smi_msg->data[smi_msg->data_size]
1387 = ipmb_checksum(&(smi_msg->data[7]),
1388 smi_msg->data_size-7);
1389
Corey Minyardc70d7492008-04-29 01:01:09 -07001390 /*
1391 * Add on the checksum size and the offset from the
1392 * broadcast.
1393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 smi_msg->data_size += 1;
1395
1396 smi_msg->msgid = msgid;
1397}
1398
Corey Minyardc70d7492008-04-29 01:01:09 -07001399/*
1400 * Separate from ipmi_request so that the user does not have to be
1401 * supplied in certain circumstances (mainly at panic time). If
1402 * messages are supplied, they will be freed, even if an error
1403 * occurs.
1404 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001405static int i_ipmi_request(ipmi_user_t user,
1406 ipmi_smi_t intf,
1407 struct ipmi_addr *addr,
1408 long msgid,
1409 struct kernel_ipmi_msg *msg,
1410 void *user_msg_data,
1411 void *supplied_smi,
1412 struct ipmi_recv_msg *supplied_recv,
1413 int priority,
1414 unsigned char source_address,
1415 unsigned char source_lun,
1416 int retries,
1417 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Corey Minyardb2c03942006-12-06 20:41:00 -08001419 int rv = 0;
1420 struct ipmi_smi_msg *smi_msg;
1421 struct ipmi_recv_msg *recv_msg;
1422 unsigned long flags;
1423 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425
Corey Minyardc70d7492008-04-29 01:01:09 -07001426 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001428 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001430 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 recv_msg->user_msg_data = user_msg_data;
1434
Corey Minyardc70d7492008-04-29 01:01:09 -07001435 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001437 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 smi_msg = ipmi_alloc_smi_msg();
1439 if (smi_msg == NULL) {
1440 ipmi_free_recv_msg(recv_msg);
1441 return -ENOMEM;
1442 }
1443 }
1444
Corey Minyardb2c03942006-12-06 20:41:00 -08001445 rcu_read_lock();
1446 handlers = intf->handlers;
1447 if (!handlers) {
1448 rv = -ENODEV;
1449 goto out_err;
1450 }
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001453 if (user)
1454 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001456 /*
1457 * Store the message to send in the receive message so timeout
1458 * responses can get the proper response data.
1459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 recv_msg->msg = *msg;
1461
1462 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1463 struct ipmi_system_interface_addr *smi_addr;
1464
1465 if (msg->netfn & 1) {
1466 /* Responses are not allowed to the SMI. */
1467 rv = -EINVAL;
1468 goto out_err;
1469 }
1470
1471 smi_addr = (struct ipmi_system_interface_addr *) addr;
1472 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001473 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 rv = -EINVAL;
1475 goto out_err;
1476 }
1477
1478 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1479
1480 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1481 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1482 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001483 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1484 /*
1485 * We don't let the user do these, since we manage
1486 * the sequence numbers.
1487 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001488 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rv = -EINVAL;
1490 goto out_err;
1491 }
1492
Corey Minyardb9675132006-12-06 20:41:02 -08001493 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1494 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1495 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001496 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001497 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1498 intf->auto_maintenance_timeout
1499 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1500 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001501 && !intf->maintenance_mode_enable) {
Corey Minyardb9675132006-12-06 20:41:02 -08001502 intf->maintenance_mode_enable = 1;
1503 maintenance_mode_update(intf);
1504 }
1505 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1506 flags);
1507 }
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001510 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 rv = -EMSGSIZE;
1512 goto out_err;
1513 }
1514
1515 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1516 smi_msg->data[1] = msg->cmd;
1517 smi_msg->msgid = msgid;
1518 smi_msg->user_data = recv_msg;
1519 if (msg->data_len > 0)
1520 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1521 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001522 ipmi_inc_stat(intf, sent_local_commands);
Corey Minyard25176ed2009-04-21 12:24:04 -07001523 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct ipmi_ipmb_addr *ipmb_addr;
1525 unsigned char ipmb_seq;
1526 long seqid;
1527 int broadcast = 0;
1528
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001529 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001530 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 rv = -EINVAL;
1532 goto out_err;
1533 }
1534
1535 if (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001536 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001537 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 rv = -EINVAL;
1539 goto out_err;
1540 }
1541
1542 if (retries < 0) {
1543 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1544 retries = 0; /* Don't retry broadcasts. */
1545 else
1546 retries = 4;
1547 }
1548 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001549 /*
1550 * Broadcasts add a zero at the beginning of the
1551 * message, but otherwise is the same as an IPMB
1552 * address.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1555 broadcast = 1;
1556 }
1557
1558
1559 /* Default to 1 second retries. */
1560 if (retry_time_ms == 0)
1561 retry_time_ms = 1000;
1562
Corey Minyardc70d7492008-04-29 01:01:09 -07001563 /*
1564 * 9 for the header and 1 for the checksum, plus
1565 * possibly one for the broadcast.
1566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001568 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 rv = -EMSGSIZE;
1570 goto out_err;
1571 }
1572
1573 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1574 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001575 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 rv = -EINVAL;
1577 goto out_err;
1578 }
1579
1580 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1581
1582 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001583 /*
1584 * It's a response, so use the user's sequence
1585 * from msgid.
1586 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001587 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1589 msgid, broadcast,
1590 source_address, source_lun);
1591
Corey Minyardc70d7492008-04-29 01:01:09 -07001592 /*
1593 * Save the receive message so we can use it
1594 * to deliver the response.
1595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 smi_msg->user_data = recv_msg;
1597 } else {
1598 /* It's a command, so get a sequence for it. */
1599
1600 spin_lock_irqsave(&(intf->seq_lock), flags);
1601
Corey Minyardc70d7492008-04-29 01:01:09 -07001602 /*
1603 * Create a sequence number with a 1 second
1604 * timeout and 4 retries.
1605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 rv = intf_next_seq(intf,
1607 recv_msg,
1608 retry_time_ms,
1609 retries,
1610 broadcast,
1611 &ipmb_seq,
1612 &seqid);
1613 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001614 /*
1615 * We have used up all the sequence numbers,
1616 * probably, so abort.
1617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 spin_unlock_irqrestore(&(intf->seq_lock),
1619 flags);
1620 goto out_err;
1621 }
1622
Corey Minyard25176ed2009-04-21 12:24:04 -07001623 ipmi_inc_stat(intf, sent_ipmb_commands);
1624
Corey Minyardc70d7492008-04-29 01:01:09 -07001625 /*
1626 * Store the sequence number in the message,
1627 * so that when the send message response
1628 * comes back we can start the timer.
1629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1631 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1632 ipmb_seq, broadcast,
1633 source_address, source_lun);
1634
Corey Minyardc70d7492008-04-29 01:01:09 -07001635 /*
1636 * Copy the message into the recv message data, so we
1637 * can retransmit it later if necessary.
1638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 memcpy(recv_msg->msg_data, smi_msg->data,
1640 smi_msg->data_size);
1641 recv_msg->msg.data = recv_msg->msg_data;
1642 recv_msg->msg.data_len = smi_msg->data_size;
1643
Corey Minyardc70d7492008-04-29 01:01:09 -07001644 /*
1645 * We don't unlock until here, because we need
1646 * to copy the completed message into the
1647 * recv_msg before we release the lock.
1648 * Otherwise, race conditions may bite us. I
1649 * know that's pretty paranoid, but I prefer
1650 * to be correct.
1651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1653 }
Corey Minyard25176ed2009-04-21 12:24:04 -07001654 } else if (is_lan_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct ipmi_lan_addr *lan_addr;
1656 unsigned char ipmb_seq;
1657 long seqid;
1658
Jayachandran C12fc1d72006-02-03 03:04:51 -08001659 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001660 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 rv = -EINVAL;
1662 goto out_err;
1663 }
1664
1665 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001666 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001668 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001669 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 rv = -EINVAL;
1671 goto out_err;
1672 }
1673
1674 retries = 4;
1675
1676 /* Default to 1 second retries. */
1677 if (retry_time_ms == 0)
1678 retry_time_ms = 1000;
1679
1680 /* 11 for the header and 1 for the checksum. */
1681 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001682 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 rv = -EMSGSIZE;
1684 goto out_err;
1685 }
1686
1687 lan_addr = (struct ipmi_lan_addr *) addr;
1688 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001689 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 rv = -EINVAL;
1691 goto out_err;
1692 }
1693
1694 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1695
1696 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001697 /*
1698 * It's a response, so use the user's sequence
1699 * from msgid.
1700 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001701 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1703 msgid, source_lun);
1704
Corey Minyardc70d7492008-04-29 01:01:09 -07001705 /*
1706 * Save the receive message so we can use it
1707 * to deliver the response.
1708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 smi_msg->user_data = recv_msg;
1710 } else {
1711 /* It's a command, so get a sequence for it. */
1712
1713 spin_lock_irqsave(&(intf->seq_lock), flags);
1714
Corey Minyardc70d7492008-04-29 01:01:09 -07001715 /*
1716 * Create a sequence number with a 1 second
1717 * timeout and 4 retries.
1718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 rv = intf_next_seq(intf,
1720 recv_msg,
1721 retry_time_ms,
1722 retries,
1723 0,
1724 &ipmb_seq,
1725 &seqid);
1726 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001727 /*
1728 * We have used up all the sequence numbers,
1729 * probably, so abort.
1730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 spin_unlock_irqrestore(&(intf->seq_lock),
1732 flags);
1733 goto out_err;
1734 }
1735
Corey Minyard25176ed2009-04-21 12:24:04 -07001736 ipmi_inc_stat(intf, sent_lan_commands);
1737
Corey Minyardc70d7492008-04-29 01:01:09 -07001738 /*
1739 * Store the sequence number in the message,
1740 * so that when the send message response
1741 * comes back we can start the timer.
1742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 format_lan_msg(smi_msg, msg, lan_addr,
1744 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1745 ipmb_seq, source_lun);
1746
Corey Minyardc70d7492008-04-29 01:01:09 -07001747 /*
1748 * Copy the message into the recv message data, so we
1749 * can retransmit it later if necessary.
1750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 memcpy(recv_msg->msg_data, smi_msg->data,
1752 smi_msg->data_size);
1753 recv_msg->msg.data = recv_msg->msg_data;
1754 recv_msg->msg.data_len = smi_msg->data_size;
1755
Corey Minyardc70d7492008-04-29 01:01:09 -07001756 /*
1757 * We don't unlock until here, because we need
1758 * to copy the completed message into the
1759 * recv_msg before we release the lock.
1760 * Otherwise, race conditions may bite us. I
1761 * know that's pretty paranoid, but I prefer
1762 * to be correct.
1763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1765 }
1766 } else {
1767 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001768 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 rv = -EINVAL;
1770 goto out_err;
1771 }
1772
1773#ifdef DEBUG_MSGING
1774 {
1775 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001776 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 printk(" %2.2x", smi_msg->data[m]);
1778 printk("\n");
1779 }
1780#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001781
1782 handlers->sender(intf->send_info, smi_msg, priority);
1783 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 return 0;
1786
1787 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001788 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ipmi_free_smi_msg(smi_msg);
1790 ipmi_free_recv_msg(recv_msg);
1791 return rv;
1792}
1793
Corey Minyardc14979b2005-09-06 15:18:38 -07001794static int check_addr(ipmi_smi_t intf,
1795 struct ipmi_addr *addr,
1796 unsigned char *saddr,
1797 unsigned char *lun)
1798{
1799 if (addr->channel >= IPMI_MAX_CHANNELS)
1800 return -EINVAL;
1801 *lun = intf->channels[addr->channel].lun;
1802 *saddr = intf->channels[addr->channel].address;
1803 return 0;
1804}
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806int ipmi_request_settime(ipmi_user_t user,
1807 struct ipmi_addr *addr,
1808 long msgid,
1809 struct kernel_ipmi_msg *msg,
1810 void *user_msg_data,
1811 int priority,
1812 int retries,
1813 unsigned int retry_time_ms)
1814{
Corey Minyardc14979b2005-09-06 15:18:38 -07001815 unsigned char saddr, lun;
1816 int rv;
1817
Corey Minyard8a3628d2006-03-31 02:30:40 -08001818 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001819 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001820 rv = check_addr(user->intf, addr, &saddr, &lun);
1821 if (rv)
1822 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return i_ipmi_request(user,
1824 user->intf,
1825 addr,
1826 msgid,
1827 msg,
1828 user_msg_data,
1829 NULL, NULL,
1830 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001831 saddr,
1832 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 retries,
1834 retry_time_ms);
1835}
Corey Minyardc70d7492008-04-29 01:01:09 -07001836EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838int ipmi_request_supply_msgs(ipmi_user_t user,
1839 struct ipmi_addr *addr,
1840 long msgid,
1841 struct kernel_ipmi_msg *msg,
1842 void *user_msg_data,
1843 void *supplied_smi,
1844 struct ipmi_recv_msg *supplied_recv,
1845 int priority)
1846{
Corey Minyardc14979b2005-09-06 15:18:38 -07001847 unsigned char saddr, lun;
1848 int rv;
1849
Corey Minyard8a3628d2006-03-31 02:30:40 -08001850 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001851 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001852 rv = check_addr(user->intf, addr, &saddr, &lun);
1853 if (rv)
1854 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return i_ipmi_request(user,
1856 user->intf,
1857 addr,
1858 msgid,
1859 msg,
1860 user_msg_data,
1861 supplied_smi,
1862 supplied_recv,
1863 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001864 saddr,
1865 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 -1, 0);
1867}
Corey Minyardc70d7492008-04-29 01:01:09 -07001868EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001870#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871static int ipmb_file_read_proc(char *page, char **start, off_t off,
1872 int count, int *eof, void *data)
1873{
1874 char *out = (char *) page;
1875 ipmi_smi_t intf = data;
Corey Minyardc14979b2005-09-06 15:18:38 -07001876 int i;
Corey Minyard8a3628d2006-03-31 02:30:40 -08001877 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Corey Minyarde8b33612005-09-06 15:18:45 -07001879 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
Corey Minyardc14979b2005-09-06 15:18:38 -07001880 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1881 out[rv-1] = '\n'; /* Replace the final space with a newline */
1882 out[rv] = '\0';
1883 rv++;
1884 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
1887static int version_file_read_proc(char *page, char **start, off_t off,
1888 int count, int *eof, void *data)
1889{
1890 char *out = (char *) page;
1891 ipmi_smi_t intf = data;
1892
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001893 return sprintf(out, "%u.%u\n",
Corey Minyard50c812b2006-03-26 01:37:21 -08001894 ipmi_version_major(&intf->bmc->id),
1895 ipmi_version_minor(&intf->bmc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898static int stat_file_read_proc(char *page, char **start, off_t off,
1899 int count, int *eof, void *data)
1900{
1901 char *out = (char *) page;
1902 ipmi_smi_t intf = data;
1903
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001904 out += sprintf(out, "sent_invalid_commands: %u\n",
1905 ipmi_get_stat(intf, sent_invalid_commands));
1906 out += sprintf(out, "sent_local_commands: %u\n",
1907 ipmi_get_stat(intf, sent_local_commands));
1908 out += sprintf(out, "handled_local_responses: %u\n",
1909 ipmi_get_stat(intf, handled_local_responses));
1910 out += sprintf(out, "unhandled_local_responses: %u\n",
1911 ipmi_get_stat(intf, unhandled_local_responses));
1912 out += sprintf(out, "sent_ipmb_commands: %u\n",
1913 ipmi_get_stat(intf, sent_ipmb_commands));
1914 out += sprintf(out, "sent_ipmb_command_errs: %u\n",
1915 ipmi_get_stat(intf, sent_ipmb_command_errs));
1916 out += sprintf(out, "retransmitted_ipmb_commands: %u\n",
1917 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1918 out += sprintf(out, "timed_out_ipmb_commands: %u\n",
1919 ipmi_get_stat(intf, timed_out_ipmb_commands));
1920 out += sprintf(out, "timed_out_ipmb_broadcasts: %u\n",
1921 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1922 out += sprintf(out, "sent_ipmb_responses: %u\n",
1923 ipmi_get_stat(intf, sent_ipmb_responses));
1924 out += sprintf(out, "handled_ipmb_responses: %u\n",
1925 ipmi_get_stat(intf, handled_ipmb_responses));
1926 out += sprintf(out, "invalid_ipmb_responses: %u\n",
1927 ipmi_get_stat(intf, invalid_ipmb_responses));
1928 out += sprintf(out, "unhandled_ipmb_responses: %u\n",
1929 ipmi_get_stat(intf, unhandled_ipmb_responses));
1930 out += sprintf(out, "sent_lan_commands: %u\n",
1931 ipmi_get_stat(intf, sent_lan_commands));
1932 out += sprintf(out, "sent_lan_command_errs: %u\n",
1933 ipmi_get_stat(intf, sent_lan_command_errs));
1934 out += sprintf(out, "retransmitted_lan_commands: %u\n",
1935 ipmi_get_stat(intf, retransmitted_lan_commands));
1936 out += sprintf(out, "timed_out_lan_commands: %u\n",
1937 ipmi_get_stat(intf, timed_out_lan_commands));
1938 out += sprintf(out, "sent_lan_responses: %u\n",
1939 ipmi_get_stat(intf, sent_lan_responses));
1940 out += sprintf(out, "handled_lan_responses: %u\n",
1941 ipmi_get_stat(intf, handled_lan_responses));
1942 out += sprintf(out, "invalid_lan_responses: %u\n",
1943 ipmi_get_stat(intf, invalid_lan_responses));
1944 out += sprintf(out, "unhandled_lan_responses: %u\n",
1945 ipmi_get_stat(intf, unhandled_lan_responses));
1946 out += sprintf(out, "handled_commands: %u\n",
1947 ipmi_get_stat(intf, handled_commands));
1948 out += sprintf(out, "invalid_commands: %u\n",
1949 ipmi_get_stat(intf, invalid_commands));
1950 out += sprintf(out, "unhandled_commands: %u\n",
1951 ipmi_get_stat(intf, unhandled_commands));
1952 out += sprintf(out, "invalid_events: %u\n",
1953 ipmi_get_stat(intf, invalid_events));
1954 out += sprintf(out, "events: %u\n",
1955 ipmi_get_stat(intf, events));
Corey Minyard25176ed2009-04-21 12:24:04 -07001956 out += sprintf(out, "failed rexmit LAN msgs: %u\n",
1957 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
1958 out += sprintf(out, "failed rexmit IPMB msgs: %u\n",
1959 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 return (out - ((char *) page));
1962}
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001963#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07001966 read_proc_t *read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03001967 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07001970#ifdef CONFIG_PROC_FS
1971 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct ipmi_proc_entry *entry;
1973
1974 /* Create a list element. */
1975 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1976 if (!entry)
1977 return -ENOMEM;
1978 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1979 if (!entry->name) {
1980 kfree(entry);
1981 return -ENOMEM;
1982 }
1983 strcpy(entry->name, name);
1984
1985 file = create_proc_entry(name, 0, smi->proc_dir);
1986 if (!file) {
1987 kfree(entry->name);
1988 kfree(entry);
1989 rv = -ENOMEM;
1990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 file->data = data;
1992 file->read_proc = read_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Corey Minyardac019152007-10-18 03:07:11 -07001994 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* Stick it on the list. */
1996 entry->next = smi->proc_entries;
1997 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07001998 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Corey Minyard3b625942005-06-23 22:01:42 -07002000#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 return rv;
2003}
Corey Minyardc70d7492008-04-29 01:01:09 -07002004EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006static int add_proc_entries(ipmi_smi_t smi, int num)
2007{
2008 int rv = 0;
2009
Corey Minyard3b625942005-06-23 22:01:42 -07002010#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 sprintf(smi->proc_dir_name, "%d", num);
2012 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2013 if (!smi->proc_dir)
2014 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (rv == 0)
2017 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002018 stat_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002019 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (rv == 0)
2022 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002023 ipmb_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002024 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 if (rv == 0)
2027 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002028 version_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002029 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002030#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 return rv;
2033}
2034
2035static void remove_proc_entries(ipmi_smi_t smi)
2036{
Corey Minyard3b625942005-06-23 22:01:42 -07002037#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct ipmi_proc_entry *entry;
2039
Corey Minyardac019152007-10-18 03:07:11 -07002040 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 while (smi->proc_entries) {
2042 entry = smi->proc_entries;
2043 smi->proc_entries = entry->next;
2044
2045 remove_proc_entry(entry->name, smi->proc_dir);
2046 kfree(entry->name);
2047 kfree(entry);
2048 }
Corey Minyardac019152007-10-18 03:07:11 -07002049 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002051#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Corey Minyard50c812b2006-03-26 01:37:21 -08002054static int __find_bmc_guid(struct device *dev, void *data)
2055{
2056 unsigned char *id = data;
2057 struct bmc_device *bmc = dev_get_drvdata(dev);
2058 return memcmp(bmc->guid, id, 16) == 0;
2059}
2060
2061static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2062 unsigned char *guid)
2063{
2064 struct device *dev;
2065
2066 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2067 if (dev)
2068 return dev_get_drvdata(dev);
2069 else
2070 return NULL;
2071}
2072
2073struct prod_dev_id {
2074 unsigned int product_id;
2075 unsigned char device_id;
2076};
2077
2078static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2079{
2080 struct prod_dev_id *id = data;
2081 struct bmc_device *bmc = dev_get_drvdata(dev);
2082
2083 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002084 && bmc->id.device_id == id->device_id);
2085}
2086
2087static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2088 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002089 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002090{
2091 struct prod_dev_id id = {
2092 .product_id = product_id,
2093 .device_id = device_id,
2094 };
2095 struct device *dev;
2096
2097 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2098 if (dev)
2099 return dev_get_drvdata(dev);
2100 else
2101 return NULL;
2102}
2103
2104static ssize_t device_id_show(struct device *dev,
2105 struct device_attribute *attr,
2106 char *buf)
2107{
2108 struct bmc_device *bmc = dev_get_drvdata(dev);
2109
2110 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2111}
2112
2113static ssize_t provides_dev_sdrs_show(struct device *dev,
2114 struct device_attribute *attr,
2115 char *buf)
2116{
2117 struct bmc_device *bmc = dev_get_drvdata(dev);
2118
2119 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002120 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002121}
2122
2123static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2124 char *buf)
2125{
2126 struct bmc_device *bmc = dev_get_drvdata(dev);
2127
2128 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002129 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002130}
2131
2132static ssize_t firmware_rev_show(struct device *dev,
2133 struct device_attribute *attr,
2134 char *buf)
2135{
2136 struct bmc_device *bmc = dev_get_drvdata(dev);
2137
2138 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2139 bmc->id.firmware_revision_2);
2140}
2141
2142static ssize_t ipmi_version_show(struct device *dev,
2143 struct device_attribute *attr,
2144 char *buf)
2145{
2146 struct bmc_device *bmc = dev_get_drvdata(dev);
2147
2148 return snprintf(buf, 20, "%u.%u\n",
2149 ipmi_version_major(&bmc->id),
2150 ipmi_version_minor(&bmc->id));
2151}
2152
2153static ssize_t add_dev_support_show(struct device *dev,
2154 struct device_attribute *attr,
2155 char *buf)
2156{
2157 struct bmc_device *bmc = dev_get_drvdata(dev);
2158
2159 return snprintf(buf, 10, "0x%02x\n",
2160 bmc->id.additional_device_support);
2161}
2162
2163static ssize_t manufacturer_id_show(struct device *dev,
2164 struct device_attribute *attr,
2165 char *buf)
2166{
2167 struct bmc_device *bmc = dev_get_drvdata(dev);
2168
2169 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2170}
2171
2172static ssize_t product_id_show(struct device *dev,
2173 struct device_attribute *attr,
2174 char *buf)
2175{
2176 struct bmc_device *bmc = dev_get_drvdata(dev);
2177
2178 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2179}
2180
2181static ssize_t aux_firmware_rev_show(struct device *dev,
2182 struct device_attribute *attr,
2183 char *buf)
2184{
2185 struct bmc_device *bmc = dev_get_drvdata(dev);
2186
2187 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2188 bmc->id.aux_firmware_revision[3],
2189 bmc->id.aux_firmware_revision[2],
2190 bmc->id.aux_firmware_revision[1],
2191 bmc->id.aux_firmware_revision[0]);
2192}
2193
2194static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2195 char *buf)
2196{
2197 struct bmc_device *bmc = dev_get_drvdata(dev);
2198
2199 return snprintf(buf, 100, "%Lx%Lx\n",
2200 (long long) bmc->guid[0],
2201 (long long) bmc->guid[8]);
2202}
2203
Jeff Garzik5e593932006-10-11 01:22:21 -07002204static void remove_files(struct bmc_device *bmc)
Corey Minyard50c812b2006-03-26 01:37:21 -08002205{
Corey Minyardf0b55da2006-12-06 20:40:54 -08002206 if (!bmc->dev)
2207 return;
2208
Corey Minyard50c812b2006-03-26 01:37:21 -08002209 device_remove_file(&bmc->dev->dev,
2210 &bmc->device_id_attr);
2211 device_remove_file(&bmc->dev->dev,
2212 &bmc->provides_dev_sdrs_attr);
2213 device_remove_file(&bmc->dev->dev,
2214 &bmc->revision_attr);
2215 device_remove_file(&bmc->dev->dev,
2216 &bmc->firmware_rev_attr);
2217 device_remove_file(&bmc->dev->dev,
2218 &bmc->version_attr);
2219 device_remove_file(&bmc->dev->dev,
2220 &bmc->add_dev_support_attr);
2221 device_remove_file(&bmc->dev->dev,
2222 &bmc->manufacturer_id_attr);
2223 device_remove_file(&bmc->dev->dev,
2224 &bmc->product_id_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002225
Corey Minyard50c812b2006-03-26 01:37:21 -08002226 if (bmc->id.aux_firmware_revision_set)
2227 device_remove_file(&bmc->dev->dev,
2228 &bmc->aux_firmware_rev_attr);
2229 if (bmc->guid_set)
2230 device_remove_file(&bmc->dev->dev,
2231 &bmc->guid_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002232}
2233
2234static void
2235cleanup_bmc_device(struct kref *ref)
2236{
2237 struct bmc_device *bmc;
2238
2239 bmc = container_of(ref, struct bmc_device, refcount);
2240
2241 remove_files(bmc);
Corey Minyard1d5636c2006-12-10 02:19:08 -08002242 platform_device_unregister(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002243 kfree(bmc);
2244}
2245
2246static void ipmi_bmc_unregister(ipmi_smi_t intf)
2247{
2248 struct bmc_device *bmc = intf->bmc;
2249
Corey Minyard759643b2006-12-06 20:40:59 -08002250 if (intf->sysfs_name) {
2251 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2252 kfree(intf->sysfs_name);
2253 intf->sysfs_name = NULL;
2254 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002255 if (intf->my_dev_name) {
2256 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2257 kfree(intf->my_dev_name);
2258 intf->my_dev_name = NULL;
2259 }
2260
2261 mutex_lock(&ipmidriver_mutex);
2262 kref_put(&bmc->refcount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002263 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002264 mutex_unlock(&ipmidriver_mutex);
2265}
2266
Jeff Garzik5e593932006-10-11 01:22:21 -07002267static int create_files(struct bmc_device *bmc)
2268{
2269 int err;
2270
Corey Minyardf0b55da2006-12-06 20:40:54 -08002271 bmc->device_id_attr.attr.name = "device_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002272 bmc->device_id_attr.attr.mode = S_IRUGO;
2273 bmc->device_id_attr.show = device_id_show;
2274
2275 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002276 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2277 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2278
2279 bmc->revision_attr.attr.name = "revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002280 bmc->revision_attr.attr.mode = S_IRUGO;
2281 bmc->revision_attr.show = revision_show;
2282
2283 bmc->firmware_rev_attr.attr.name = "firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002284 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2285 bmc->firmware_rev_attr.show = firmware_rev_show;
2286
2287 bmc->version_attr.attr.name = "ipmi_version";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002288 bmc->version_attr.attr.mode = S_IRUGO;
2289 bmc->version_attr.show = ipmi_version_show;
2290
2291 bmc->add_dev_support_attr.attr.name = "additional_device_support";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002292 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2293 bmc->add_dev_support_attr.show = add_dev_support_show;
2294
2295 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002296 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2297 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2298
2299 bmc->product_id_attr.attr.name = "product_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002300 bmc->product_id_attr.attr.mode = S_IRUGO;
2301 bmc->product_id_attr.show = product_id_show;
2302
2303 bmc->guid_attr.attr.name = "guid";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002304 bmc->guid_attr.attr.mode = S_IRUGO;
2305 bmc->guid_attr.show = guid_show;
2306
2307 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002308 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2309 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2310
Jeff Garzik5e593932006-10-11 01:22:21 -07002311 err = device_create_file(&bmc->dev->dev,
2312 &bmc->device_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002313 if (err)
2314 goto out;
Jeff Garzik5e593932006-10-11 01:22:21 -07002315 err = device_create_file(&bmc->dev->dev,
2316 &bmc->provides_dev_sdrs_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002317 if (err)
2318 goto out_devid;
Jeff Garzik5e593932006-10-11 01:22:21 -07002319 err = device_create_file(&bmc->dev->dev,
2320 &bmc->revision_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002321 if (err)
2322 goto out_sdrs;
Jeff Garzik5e593932006-10-11 01:22:21 -07002323 err = device_create_file(&bmc->dev->dev,
2324 &bmc->firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002325 if (err)
2326 goto out_rev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002327 err = device_create_file(&bmc->dev->dev,
2328 &bmc->version_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002329 if (err)
2330 goto out_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002331 err = device_create_file(&bmc->dev->dev,
2332 &bmc->add_dev_support_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002333 if (err)
2334 goto out_version;
Jeff Garzik5e593932006-10-11 01:22:21 -07002335 err = device_create_file(&bmc->dev->dev,
2336 &bmc->manufacturer_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002337 if (err)
2338 goto out_add_dev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002339 err = device_create_file(&bmc->dev->dev,
2340 &bmc->product_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002341 if (err)
2342 goto out_manu;
Jeff Garzik5e593932006-10-11 01:22:21 -07002343 if (bmc->id.aux_firmware_revision_set) {
2344 err = device_create_file(&bmc->dev->dev,
2345 &bmc->aux_firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002346 if (err)
2347 goto out_prod_id;
Jeff Garzik5e593932006-10-11 01:22:21 -07002348 }
2349 if (bmc->guid_set) {
2350 err = device_create_file(&bmc->dev->dev,
2351 &bmc->guid_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002352 if (err)
2353 goto out_aux_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002354 }
2355
2356 return 0;
2357
2358out_aux_firm:
2359 if (bmc->id.aux_firmware_revision_set)
2360 device_remove_file(&bmc->dev->dev,
2361 &bmc->aux_firmware_rev_attr);
2362out_prod_id:
2363 device_remove_file(&bmc->dev->dev,
2364 &bmc->product_id_attr);
2365out_manu:
2366 device_remove_file(&bmc->dev->dev,
2367 &bmc->manufacturer_id_attr);
2368out_add_dev:
2369 device_remove_file(&bmc->dev->dev,
2370 &bmc->add_dev_support_attr);
2371out_version:
2372 device_remove_file(&bmc->dev->dev,
2373 &bmc->version_attr);
2374out_firm:
2375 device_remove_file(&bmc->dev->dev,
2376 &bmc->firmware_rev_attr);
2377out_rev:
2378 device_remove_file(&bmc->dev->dev,
2379 &bmc->revision_attr);
2380out_sdrs:
2381 device_remove_file(&bmc->dev->dev,
2382 &bmc->provides_dev_sdrs_attr);
2383out_devid:
2384 device_remove_file(&bmc->dev->dev,
2385 &bmc->device_id_attr);
2386out:
2387 return err;
2388}
2389
Corey Minyard759643b2006-12-06 20:40:59 -08002390static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2391 const char *sysfs_name)
Corey Minyard50c812b2006-03-26 01:37:21 -08002392{
2393 int rv;
2394 struct bmc_device *bmc = intf->bmc;
2395 struct bmc_device *old_bmc;
2396 int size;
2397 char dummy[1];
2398
2399 mutex_lock(&ipmidriver_mutex);
2400
2401 /*
2402 * Try to find if there is an bmc_device struct
2403 * representing the interfaced BMC already
2404 */
2405 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002406 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002407 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002408 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002409 bmc->id.product_id,
2410 bmc->id.device_id);
2411
2412 /*
2413 * If there is already an bmc_device, free the new one,
2414 * otherwise register the new BMC device
2415 */
2416 if (old_bmc) {
2417 kfree(bmc);
2418 intf->bmc = old_bmc;
2419 bmc = old_bmc;
2420
2421 kref_get(&bmc->refcount);
2422 mutex_unlock(&ipmidriver_mutex);
2423
2424 printk(KERN_INFO
2425 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2426 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2427 bmc->id.manufacturer_id,
2428 bmc->id.product_id,
2429 bmc->id.device_id);
2430 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002431 char name[14];
2432 unsigned char orig_dev_id = bmc->id.device_id;
2433 int warn_printed = 0;
2434
2435 snprintf(name, sizeof(name),
2436 "ipmi_bmc.%4.4x", bmc->id.product_id);
2437
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002438 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002439 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002440 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002441 if (!warn_printed) {
2442 printk(KERN_WARNING PFX
2443 "This machine has two different BMCs"
2444 " with the same product id and device"
2445 " id. This is an error in the"
2446 " firmware, but incrementing the"
2447 " device id to work around the problem."
2448 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2449 bmc->id.product_id, bmc->id.device_id);
2450 warn_printed = 1;
2451 }
2452 bmc->id.device_id++; /* Wraps at 255 */
2453 if (bmc->id.device_id == orig_dev_id) {
2454 printk(KERN_ERR PFX
2455 "Out of device ids!\n");
2456 break;
2457 }
2458 }
2459
2460 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
Corey Minyard8a3628d2006-03-31 02:30:40 -08002461 if (!bmc->dev) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002462 mutex_unlock(&ipmidriver_mutex);
Corey Minyard50c812b2006-03-26 01:37:21 -08002463 printk(KERN_ERR
2464 "ipmi_msghandler:"
2465 " Unable to allocate platform device\n");
2466 return -ENOMEM;
2467 }
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002468 bmc->dev->dev.driver = &ipmidriver.driver;
Corey Minyard50c812b2006-03-26 01:37:21 -08002469 dev_set_drvdata(&bmc->dev->dev, bmc);
2470 kref_init(&bmc->refcount);
2471
Zhang, Yanminb48f5452006-11-16 01:19:08 -08002472 rv = platform_device_add(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002473 mutex_unlock(&ipmidriver_mutex);
2474 if (rv) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002475 platform_device_put(bmc->dev);
2476 bmc->dev = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002477 printk(KERN_ERR
2478 "ipmi_msghandler:"
2479 " Unable to register bmc device: %d\n",
2480 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002481 /*
2482 * Don't go to out_err, you can only do that if
2483 * the device is registered already.
2484 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002485 return rv;
2486 }
2487
Jeff Garzik5e593932006-10-11 01:22:21 -07002488 rv = create_files(bmc);
2489 if (rv) {
2490 mutex_lock(&ipmidriver_mutex);
2491 platform_device_unregister(bmc->dev);
2492 mutex_unlock(&ipmidriver_mutex);
2493
2494 return rv;
2495 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002496
2497 printk(KERN_INFO
2498 "ipmi: Found new BMC (man_id: 0x%6.6x, "
2499 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2500 bmc->id.manufacturer_id,
2501 bmc->id.product_id,
2502 bmc->id.device_id);
2503 }
2504
2505 /*
2506 * create symlink from system interface device to bmc device
2507 * and back.
2508 */
Corey Minyard759643b2006-12-06 20:40:59 -08002509 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2510 if (!intf->sysfs_name) {
2511 rv = -ENOMEM;
2512 printk(KERN_ERR
2513 "ipmi_msghandler: allocate link to BMC: %d\n",
2514 rv);
2515 goto out_err;
2516 }
2517
Corey Minyard50c812b2006-03-26 01:37:21 -08002518 rv = sysfs_create_link(&intf->si_dev->kobj,
Corey Minyard759643b2006-12-06 20:40:59 -08002519 &bmc->dev->dev.kobj, intf->sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002520 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002521 kfree(intf->sysfs_name);
2522 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002523 printk(KERN_ERR
2524 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2525 rv);
2526 goto out_err;
2527 }
2528
Corey Minyard759643b2006-12-06 20:40:59 -08002529 size = snprintf(dummy, 0, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002530 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2531 if (!intf->my_dev_name) {
Corey Minyard759643b2006-12-06 20:40:59 -08002532 kfree(intf->sysfs_name);
2533 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002534 rv = -ENOMEM;
2535 printk(KERN_ERR
2536 "ipmi_msghandler: allocate link from BMC: %d\n",
2537 rv);
2538 goto out_err;
2539 }
Corey Minyard759643b2006-12-06 20:40:59 -08002540 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002541
2542 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2543 intf->my_dev_name);
2544 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002545 kfree(intf->sysfs_name);
2546 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002547 kfree(intf->my_dev_name);
2548 intf->my_dev_name = NULL;
2549 printk(KERN_ERR
2550 "ipmi_msghandler:"
2551 " Unable to create symlink to bmc: %d\n",
2552 rv);
2553 goto out_err;
2554 }
2555
2556 return 0;
2557
2558out_err:
2559 ipmi_bmc_unregister(intf);
2560 return rv;
2561}
2562
2563static int
2564send_guid_cmd(ipmi_smi_t intf, int chan)
2565{
2566 struct kernel_ipmi_msg msg;
2567 struct ipmi_system_interface_addr si;
2568
2569 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2570 si.channel = IPMI_BMC_CHANNEL;
2571 si.lun = 0;
2572
2573 msg.netfn = IPMI_NETFN_APP_REQUEST;
2574 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2575 msg.data = NULL;
2576 msg.data_len = 0;
2577 return i_ipmi_request(NULL,
2578 intf,
2579 (struct ipmi_addr *) &si,
2580 0,
2581 &msg,
2582 intf,
2583 NULL,
2584 NULL,
2585 0,
2586 intf->channels[0].address,
2587 intf->channels[0].lun,
2588 -1, 0);
2589}
2590
2591static void
2592guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2593{
2594 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2595 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2596 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2597 /* Not for me */
2598 return;
2599
2600 if (msg->msg.data[0] != 0) {
2601 /* Error from getting the GUID, the BMC doesn't have one. */
2602 intf->bmc->guid_set = 0;
2603 goto out;
2604 }
2605
2606 if (msg->msg.data_len < 17) {
2607 intf->bmc->guid_set = 0;
2608 printk(KERN_WARNING PFX
2609 "guid_handler: The GUID response from the BMC was too"
2610 " short, it was %d but should have been 17. Assuming"
2611 " GUID is not available.\n",
2612 msg->msg.data_len);
2613 goto out;
2614 }
2615
2616 memcpy(intf->bmc->guid, msg->msg.data, 16);
2617 intf->bmc->guid_set = 1;
2618 out:
2619 wake_up(&intf->waitq);
2620}
2621
2622static void
2623get_guid(ipmi_smi_t intf)
2624{
2625 int rv;
2626
2627 intf->bmc->guid_set = 0x2;
2628 intf->null_user_handler = guid_handler;
2629 rv = send_guid_cmd(intf, 0);
2630 if (rv)
2631 /* Send failed, no GUID available. */
2632 intf->bmc->guid_set = 0;
2633 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2634 intf->null_user_handler = NULL;
2635}
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637static int
2638send_channel_info_cmd(ipmi_smi_t intf, int chan)
2639{
2640 struct kernel_ipmi_msg msg;
2641 unsigned char data[1];
2642 struct ipmi_system_interface_addr si;
2643
2644 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2645 si.channel = IPMI_BMC_CHANNEL;
2646 si.lun = 0;
2647
2648 msg.netfn = IPMI_NETFN_APP_REQUEST;
2649 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2650 msg.data = data;
2651 msg.data_len = 1;
2652 data[0] = chan;
2653 return i_ipmi_request(NULL,
2654 intf,
2655 (struct ipmi_addr *) &si,
2656 0,
2657 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002658 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 NULL,
2660 NULL,
2661 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002662 intf->channels[0].address,
2663 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 -1, 0);
2665}
2666
2667static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002668channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 int rv = 0;
2671 int chan;
2672
Corey Minyard56a55ec2005-09-06 15:18:42 -07002673 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2674 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002675 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002677 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 /* Got an error from the channel, just go on. */
2679
Corey Minyard56a55ec2005-09-06 15:18:42 -07002680 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002681 /*
2682 * If the MC does not support this
2683 * command, that is legal. We just
2684 * assume it has one IPMB at channel
2685 * zero.
2686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 intf->channels[0].medium
2688 = IPMI_CHANNEL_MEDIUM_IPMB;
2689 intf->channels[0].protocol
2690 = IPMI_CHANNEL_PROTOCOL_IPMB;
2691 rv = -ENOSYS;
2692
2693 intf->curr_channel = IPMI_MAX_CHANNELS;
2694 wake_up(&intf->waitq);
2695 goto out;
2696 }
2697 goto next_channel;
2698 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002699 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* Message not big enough, just go on. */
2701 goto next_channel;
2702 }
2703 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002704 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2705 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Corey Minyardc70d7492008-04-29 01:01:09 -07002707 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 intf->curr_channel++;
2709 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2710 wake_up(&intf->waitq);
2711 else
2712 rv = send_channel_info_cmd(intf, intf->curr_channel);
2713
2714 if (rv) {
2715 /* Got an error somehow, just give up. */
2716 intf->curr_channel = IPMI_MAX_CHANNELS;
2717 wake_up(&intf->waitq);
2718
2719 printk(KERN_WARNING PFX
2720 "Error sending channel information: %d\n",
2721 rv);
2722 }
2723 }
2724 out:
2725 return;
2726}
2727
Corey Minyardfcfa4722007-10-18 03:07:09 -07002728void ipmi_poll_interface(ipmi_user_t user)
2729{
2730 ipmi_smi_t intf = user->intf;
2731
2732 if (intf->handlers->poll)
2733 intf->handlers->poll(intf->send_info);
2734}
Corey Minyardc70d7492008-04-29 01:01:09 -07002735EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2738 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002739 struct ipmi_device_id *device_id,
2740 struct device *si_dev,
Corey Minyard759643b2006-12-06 20:40:59 -08002741 const char *sysfs_name,
Corey Minyard453823b2006-03-31 02:30:39 -08002742 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 int i, j;
2745 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002746 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002747 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002748 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Corey Minyardc70d7492008-04-29 01:01:09 -07002750 /*
2751 * Make sure the driver is actually initialized, this handles
2752 * problems with initialization order.
2753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (!initialized) {
2755 rv = ipmi_init_msghandler();
2756 if (rv)
2757 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002758 /*
2759 * The init code doesn't return an error if it was turned
2760 * off, but it won't initialize. Check that.
2761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (!initialized)
2763 return -ENODEV;
2764 }
2765
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002766 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002767 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002769
2770 intf->ipmi_version_major = ipmi_version_major(device_id);
2771 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2772
Corey Minyard50c812b2006-03-26 01:37:21 -08002773 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2774 if (!intf->bmc) {
2775 kfree(intf);
2776 return -ENOMEM;
2777 }
Corey Minyardbca03242006-12-06 20:40:57 -08002778 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002779 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002780 intf->bmc->id = *device_id;
2781 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002782 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2783 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2784 intf->channels[j].lun = 2;
2785 }
2786 if (slave_addr != 0)
2787 intf->channels[0].address = slave_addr;
2788 INIT_LIST_HEAD(&intf->users);
2789 intf->handlers = handlers;
2790 intf->send_info = send_info;
2791 spin_lock_init(&intf->seq_lock);
2792 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2793 intf->seq_table[j].inuse = 0;
2794 intf->seq_table[j].seqid = 0;
2795 }
2796 intf->curr_seq = 0;
2797#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002798 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002799#endif
2800 spin_lock_init(&intf->waiting_msgs_lock);
2801 INIT_LIST_HEAD(&intf->waiting_msgs);
2802 spin_lock_init(&intf->events_lock);
2803 INIT_LIST_HEAD(&intf->waiting_events);
2804 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002805 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002806 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002807 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2808 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002809 for (i = 0; i < IPMI_NUM_STATS; i++)
2810 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Corey Minyard393d2cc2005-11-07 00:59:54 -08002812 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Corey Minyardb2c03942006-12-06 20:41:00 -08002814 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002815 mutex_lock(&ipmi_interfaces_mutex);
2816 /* Look for a hole in the numbers. */
2817 i = 0;
2818 link = &ipmi_interfaces;
2819 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2820 if (tintf->intf_num != i) {
2821 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 break;
2823 }
Corey Minyardbca03242006-12-06 20:40:57 -08002824 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Corey Minyardbca03242006-12-06 20:40:57 -08002826 /* Add the new interface in numeric order. */
2827 if (i == 0)
2828 list_add_rcu(&intf->link, &ipmi_interfaces);
2829 else
2830 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Corey Minyard453823b2006-03-31 02:30:39 -08002832 rv = handlers->start_processing(send_info, intf);
2833 if (rv)
2834 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002835
Corey Minyard50c812b2006-03-26 01:37:21 -08002836 get_guid(intf);
2837
Corey Minyardb2c03942006-12-06 20:41:00 -08002838 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002839 || ((intf->ipmi_version_major == 1)
2840 && (intf->ipmi_version_minor >= 5))) {
2841 /*
2842 * Start scanning the channels to see what is
2843 * available.
2844 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002845 intf->null_user_handler = channel_handler;
2846 intf->curr_channel = 0;
2847 rv = send_channel_info_cmd(intf, 0);
2848 if (rv)
2849 goto out;
2850
2851 /* Wait for the channel info to be read. */
2852 wait_event(intf->waitq,
2853 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002854 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002855 } else {
2856 /* Assume a single IPMB channel at zero. */
2857 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2858 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
Corey Minyard9a2845c2009-05-20 13:36:17 -05002859 intf->curr_channel = IPMI_MAX_CHANNELS;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002863 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Corey Minyard759643b2006-12-06 20:40:59 -08002865 rv = ipmi_bmc_register(intf, i, sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002866
Corey Minyard393d2cc2005-11-07 00:59:54 -08002867 out:
2868 if (rv) {
2869 if (intf->proc_dir)
2870 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002871 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002872 list_del_rcu(&intf->link);
2873 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002874 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002875 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002876 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002877 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002878 /*
2879 * Keep memory order straight for RCU readers. Make
2880 * sure everything else is committed to memory before
2881 * setting intf_num to mark the interface valid.
2882 */
2883 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002884 intf->intf_num = i;
2885 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002886 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002887 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002888 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return rv;
2892}
Corey Minyardc70d7492008-04-29 01:01:09 -07002893EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Corey Minyardb2c03942006-12-06 20:41:00 -08002895static void cleanup_smi_msgs(ipmi_smi_t intf)
2896{
2897 int i;
2898 struct seq_table *ent;
2899
2900 /* No need for locks, the interface is down. */
2901 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2902 ent = &(intf->seq_table[i]);
2903 if (!ent->inuse)
2904 continue;
2905 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2906 }
2907}
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909int ipmi_unregister_smi(ipmi_smi_t intf)
2910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 struct ipmi_smi_watcher *w;
Corey Minyardb2c03942006-12-06 20:41:00 -08002912 int intf_num = intf->intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Corey Minyard50c812b2006-03-26 01:37:21 -08002914 ipmi_bmc_unregister(intf);
2915
Corey Minyardb2c03942006-12-06 20:41:00 -08002916 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002917 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002918 intf->intf_num = -1;
2919 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002920 list_del_rcu(&intf->link);
2921 mutex_unlock(&ipmi_interfaces_mutex);
2922 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Corey Minyardb2c03942006-12-06 20:41:00 -08002924 cleanup_smi_msgs(intf);
2925
Corey Minyard393d2cc2005-11-07 00:59:54 -08002926 remove_proc_entries(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Corey Minyardc70d7492008-04-29 01:01:09 -07002928 /*
2929 * Call all the watcher interfaces to tell them that
2930 * an interface is gone.
2931 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002932 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08002933 w->smi_gone(intf_num);
2934 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002935
Corey Minyard393d2cc2005-11-07 00:59:54 -08002936 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return 0;
2938}
Corey Minyardc70d7492008-04-29 01:01:09 -07002939EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
2942 struct ipmi_smi_msg *msg)
2943{
2944 struct ipmi_ipmb_addr ipmb_addr;
2945 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Corey Minyardc70d7492008-04-29 01:01:09 -07002947 /*
2948 * This is 11, not 10, because the response must contain a
2949 * completion code.
2950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (msg->rsp_size < 11) {
2952 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002953 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return 0;
2955 }
2956
2957 if (msg->rsp[2] != 0) {
2958 /* An error getting the response, just ignore it. */
2959 return 0;
2960 }
2961
2962 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
2963 ipmb_addr.slave_addr = msg->rsp[6];
2964 ipmb_addr.channel = msg->rsp[3] & 0x0f;
2965 ipmb_addr.lun = msg->rsp[7] & 3;
2966
Corey Minyardc70d7492008-04-29 01:01:09 -07002967 /*
2968 * It's a response from a remote entity. Look up the sequence
2969 * number and handle the response.
2970 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (intf_find_seq(intf,
2972 msg->rsp[7] >> 2,
2973 msg->rsp[3] & 0x0f,
2974 msg->rsp[8],
2975 (msg->rsp[4] >> 2) & (~1),
2976 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07002977 &recv_msg)) {
2978 /*
2979 * We were unable to find the sequence number,
2980 * so just nuke the message.
2981 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002982 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return 0;
2984 }
2985
2986 memcpy(recv_msg->msg_data,
2987 &(msg->rsp[9]),
2988 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07002989 /*
2990 * The other fields matched, so no need to set them, except
2991 * for netfn, which needs to be the response that was
2992 * returned, not the request value.
2993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2995 recv_msg->msg.data = recv_msg->msg_data;
2996 recv_msg->msg.data_len = msg->rsp_size - 10;
2997 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002998 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 deliver_response(recv_msg);
3000
3001 return 0;
3002}
3003
3004static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3005 struct ipmi_smi_msg *msg)
3006{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003007 struct cmd_rcvr *rcvr;
3008 int rv = 0;
3009 unsigned char netfn;
3010 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003011 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003012 ipmi_user_t user = NULL;
3013 struct ipmi_ipmb_addr *ipmb_addr;
3014 struct ipmi_recv_msg *recv_msg;
Corey Minyardb2c03942006-12-06 20:41:00 -08003015 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 if (msg->rsp_size < 10) {
3018 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003019 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
3021 }
3022
3023 if (msg->rsp[2] != 0) {
3024 /* An error getting the response, just ignore it. */
3025 return 0;
3026 }
3027
3028 netfn = msg->rsp[4] >> 2;
3029 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003030 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003032 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003033 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003034 if (rcvr) {
3035 user = rcvr->user;
3036 kref_get(&user->refcount);
3037 } else
3038 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003039 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 if (user == NULL) {
3042 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003043 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3046 msg->data[1] = IPMI_SEND_MSG_CMD;
3047 msg->data[2] = msg->rsp[3];
3048 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003049 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003051 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003052 /* rqseq/lun */
3053 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 msg->data[8] = msg->rsp[8]; /* cmd */
3055 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3056 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3057 msg->data_size = 11;
3058
3059#ifdef DEBUG_MSGING
3060 {
3061 int m;
3062 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003063 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 printk(" %2.2x", msg->data[m]);
3065 printk("\n");
3066 }
3067#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003068 rcu_read_lock();
3069 handlers = intf->handlers;
3070 if (handlers) {
3071 handlers->sender(intf->send_info, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003072 /*
3073 * We used the message, so return the value
3074 * that causes it to not be freed or
3075 * queued.
3076 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003077 rv = -1;
3078 }
3079 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 } else {
3081 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003082 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003085 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003086 /*
3087 * We couldn't allocate memory for the
3088 * message, so requeue it for handling
3089 * later.
3090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003092 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 } else {
3094 /* Extract the source address from the data. */
3095 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3096 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3097 ipmb_addr->slave_addr = msg->rsp[6];
3098 ipmb_addr->lun = msg->rsp[7] & 3;
3099 ipmb_addr->channel = msg->rsp[3] & 0xf;
3100
Corey Minyardc70d7492008-04-29 01:01:09 -07003101 /*
3102 * Extract the rest of the message information
3103 * from the IPMB header.
3104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 recv_msg->user = user;
3106 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3107 recv_msg->msgid = msg->rsp[7] >> 2;
3108 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3109 recv_msg->msg.cmd = msg->rsp[8];
3110 recv_msg->msg.data = recv_msg->msg_data;
3111
Corey Minyardc70d7492008-04-29 01:01:09 -07003112 /*
3113 * We chop off 10, not 9 bytes because the checksum
3114 * at the end also needs to be removed.
3115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 recv_msg->msg.data_len = msg->rsp_size - 10;
3117 memcpy(recv_msg->msg_data,
3118 &(msg->rsp[9]),
3119 msg->rsp_size - 10);
3120 deliver_response(recv_msg);
3121 }
3122 }
3123
3124 return rv;
3125}
3126
3127static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3128 struct ipmi_smi_msg *msg)
3129{
3130 struct ipmi_lan_addr lan_addr;
3131 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133
Corey Minyardc70d7492008-04-29 01:01:09 -07003134 /*
3135 * This is 13, not 12, because the response must contain a
3136 * completion code.
3137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (msg->rsp_size < 13) {
3139 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003140 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return 0;
3142 }
3143
3144 if (msg->rsp[2] != 0) {
3145 /* An error getting the response, just ignore it. */
3146 return 0;
3147 }
3148
3149 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3150 lan_addr.session_handle = msg->rsp[4];
3151 lan_addr.remote_SWID = msg->rsp[8];
3152 lan_addr.local_SWID = msg->rsp[5];
3153 lan_addr.channel = msg->rsp[3] & 0x0f;
3154 lan_addr.privilege = msg->rsp[3] >> 4;
3155 lan_addr.lun = msg->rsp[9] & 3;
3156
Corey Minyardc70d7492008-04-29 01:01:09 -07003157 /*
3158 * It's a response from a remote entity. Look up the sequence
3159 * number and handle the response.
3160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (intf_find_seq(intf,
3162 msg->rsp[9] >> 2,
3163 msg->rsp[3] & 0x0f,
3164 msg->rsp[10],
3165 (msg->rsp[6] >> 2) & (~1),
3166 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003167 &recv_msg)) {
3168 /*
3169 * We were unable to find the sequence number,
3170 * so just nuke the message.
3171 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003172 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return 0;
3174 }
3175
3176 memcpy(recv_msg->msg_data,
3177 &(msg->rsp[11]),
3178 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003179 /*
3180 * The other fields matched, so no need to set them, except
3181 * for netfn, which needs to be the response that was
3182 * returned, not the request value.
3183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3185 recv_msg->msg.data = recv_msg->msg_data;
3186 recv_msg->msg.data_len = msg->rsp_size - 12;
3187 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003188 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 deliver_response(recv_msg);
3190
3191 return 0;
3192}
3193
3194static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3195 struct ipmi_smi_msg *msg)
3196{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003197 struct cmd_rcvr *rcvr;
3198 int rv = 0;
3199 unsigned char netfn;
3200 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003201 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003202 ipmi_user_t user = NULL;
3203 struct ipmi_lan_addr *lan_addr;
3204 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206 if (msg->rsp_size < 12) {
3207 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003208 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return 0;
3210 }
3211
3212 if (msg->rsp[2] != 0) {
3213 /* An error getting the response, just ignore it. */
3214 return 0;
3215 }
3216
3217 netfn = msg->rsp[6] >> 2;
3218 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003219 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003221 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003222 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003223 if (rcvr) {
3224 user = rcvr->user;
3225 kref_get(&user->refcount);
3226 } else
3227 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003231 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003232 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Corey Minyardc70d7492008-04-29 01:01:09 -07003234 /*
3235 * Don't do anything with these messages, just allow
3236 * them to be freed.
3237 */
3238 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 } else {
3240 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003241 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003244 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003245 /*
3246 * We couldn't allocate memory for the
3247 * message, so requeue it for handling later.
3248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003250 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 } else {
3252 /* Extract the source address from the data. */
3253 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3254 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3255 lan_addr->session_handle = msg->rsp[4];
3256 lan_addr->remote_SWID = msg->rsp[8];
3257 lan_addr->local_SWID = msg->rsp[5];
3258 lan_addr->lun = msg->rsp[9] & 3;
3259 lan_addr->channel = msg->rsp[3] & 0xf;
3260 lan_addr->privilege = msg->rsp[3] >> 4;
3261
Corey Minyardc70d7492008-04-29 01:01:09 -07003262 /*
3263 * Extract the rest of the message information
3264 * from the IPMB header.
3265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 recv_msg->user = user;
3267 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3268 recv_msg->msgid = msg->rsp[9] >> 2;
3269 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3270 recv_msg->msg.cmd = msg->rsp[10];
3271 recv_msg->msg.data = recv_msg->msg_data;
3272
Corey Minyardc70d7492008-04-29 01:01:09 -07003273 /*
3274 * We chop off 12, not 11 bytes because the checksum
3275 * at the end also needs to be removed.
3276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 recv_msg->msg.data_len = msg->rsp_size - 12;
3278 memcpy(recv_msg->msg_data,
3279 &(msg->rsp[11]),
3280 msg->rsp_size - 12);
3281 deliver_response(recv_msg);
3282 }
3283 }
3284
3285 return rv;
3286}
3287
dann frazier4dec3022009-04-21 12:24:05 -07003288/*
3289 * This routine will handle "Get Message" command responses with
3290 * channels that use an OEM Medium. The message format belongs to
3291 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3292 * Chapter 22, sections 22.6 and 22.24 for more details.
3293 */
3294static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3295 struct ipmi_smi_msg *msg)
3296{
3297 struct cmd_rcvr *rcvr;
3298 int rv = 0;
3299 unsigned char netfn;
3300 unsigned char cmd;
3301 unsigned char chan;
3302 ipmi_user_t user = NULL;
3303 struct ipmi_system_interface_addr *smi_addr;
3304 struct ipmi_recv_msg *recv_msg;
3305
3306 /*
3307 * We expect the OEM SW to perform error checking
3308 * so we just do some basic sanity checks
3309 */
3310 if (msg->rsp_size < 4) {
3311 /* Message not big enough, just ignore it. */
3312 ipmi_inc_stat(intf, invalid_commands);
3313 return 0;
3314 }
3315
3316 if (msg->rsp[2] != 0) {
3317 /* An error getting the response, just ignore it. */
3318 return 0;
3319 }
3320
3321 /*
3322 * This is an OEM Message so the OEM needs to know how
3323 * handle the message. We do no interpretation.
3324 */
3325 netfn = msg->rsp[0] >> 2;
3326 cmd = msg->rsp[1];
3327 chan = msg->rsp[3] & 0xf;
3328
3329 rcu_read_lock();
3330 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3331 if (rcvr) {
3332 user = rcvr->user;
3333 kref_get(&user->refcount);
3334 } else
3335 user = NULL;
3336 rcu_read_unlock();
3337
3338 if (user == NULL) {
3339 /* We didn't find a user, just give up. */
3340 ipmi_inc_stat(intf, unhandled_commands);
3341
3342 /*
3343 * Don't do anything with these messages, just allow
3344 * them to be freed.
3345 */
3346
3347 rv = 0;
3348 } else {
3349 /* Deliver the message to the user. */
3350 ipmi_inc_stat(intf, handled_commands);
3351
3352 recv_msg = ipmi_alloc_recv_msg();
3353 if (!recv_msg) {
3354 /*
3355 * We couldn't allocate memory for the
3356 * message, so requeue it for handling
3357 * later.
3358 */
3359 rv = 1;
3360 kref_put(&user->refcount, free_user);
3361 } else {
3362 /*
3363 * OEM Messages are expected to be delivered via
3364 * the system interface to SMS software. We might
3365 * need to visit this again depending on OEM
3366 * requirements
3367 */
3368 smi_addr = ((struct ipmi_system_interface_addr *)
3369 &(recv_msg->addr));
3370 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3371 smi_addr->channel = IPMI_BMC_CHANNEL;
3372 smi_addr->lun = msg->rsp[0] & 3;
3373
3374 recv_msg->user = user;
3375 recv_msg->user_msg_data = NULL;
3376 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3377 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3378 recv_msg->msg.cmd = msg->rsp[1];
3379 recv_msg->msg.data = recv_msg->msg_data;
3380
3381 /*
3382 * The message starts at byte 4 which follows the
3383 * the Channel Byte in the "GET MESSAGE" command
3384 */
3385 recv_msg->msg.data_len = msg->rsp_size - 4;
3386 memcpy(recv_msg->msg_data,
3387 &(msg->rsp[4]),
3388 msg->rsp_size - 4);
3389 deliver_response(recv_msg);
3390 }
3391 }
3392
3393 return rv;
3394}
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3397 struct ipmi_smi_msg *msg)
3398{
3399 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 recv_msg->msgid = 0;
3402 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3403 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3404 smi_addr->channel = IPMI_BMC_CHANNEL;
3405 smi_addr->lun = msg->rsp[0] & 3;
3406 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3407 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3408 recv_msg->msg.cmd = msg->rsp[1];
3409 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3410 recv_msg->msg.data = recv_msg->msg_data;
3411 recv_msg->msg.data_len = msg->rsp_size - 3;
3412}
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414static int handle_read_event_rsp(ipmi_smi_t intf,
3415 struct ipmi_smi_msg *msg)
3416{
3417 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3418 struct list_head msgs;
3419 ipmi_user_t user;
3420 int rv = 0;
3421 int deliver_count = 0;
3422 unsigned long flags;
3423
3424 if (msg->rsp_size < 19) {
3425 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003426 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return 0;
3428 }
3429
3430 if (msg->rsp[2] != 0) {
3431 /* An error getting the event, just ignore it. */
3432 return 0;
3433 }
3434
3435 INIT_LIST_HEAD(&msgs);
3436
Corey Minyard393d2cc2005-11-07 00:59:54 -08003437 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003439 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Corey Minyardc70d7492008-04-29 01:01:09 -07003441 /*
3442 * Allocate and fill in one message for every user that is
3443 * getting events.
3444 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003445 rcu_read_lock();
3446 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003447 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 continue;
3449
3450 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003451 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003452 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003453 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3454 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 list_del(&recv_msg->link);
3456 ipmi_free_recv_msg(recv_msg);
3457 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003458 /*
3459 * We couldn't allocate memory for the
3460 * message, so requeue it for handling
3461 * later.
3462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 rv = 1;
3464 goto out;
3465 }
3466
3467 deliver_count++;
3468
3469 copy_event_into_recv_msg(recv_msg, msg);
3470 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003471 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 list_add_tail(&(recv_msg->link), &msgs);
3473 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003474 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 if (deliver_count) {
3477 /* Now deliver all the messages. */
3478 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3479 list_del(&recv_msg->link);
3480 deliver_response(recv_msg);
3481 }
3482 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003483 /*
3484 * No one to receive the message, put it in queue if there's
3485 * not already too many things in the queue.
3486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003488 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003489 /*
3490 * We couldn't allocate memory for the
3491 * message, so requeue it for handling
3492 * later.
3493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 rv = 1;
3495 goto out;
3496 }
3497
3498 copy_event_into_recv_msg(recv_msg, msg);
3499 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003500 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003501 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003502 /*
3503 * There's too many things in the queue, discard this
3504 * message.
3505 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003506 printk(KERN_WARNING PFX "Event queue full, discarding"
3507 " incoming events\n");
3508 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
3510
3511 out:
3512 spin_unlock_irqrestore(&(intf->events_lock), flags);
3513
3514 return rv;
3515}
3516
3517static int handle_bmc_rsp(ipmi_smi_t intf,
3518 struct ipmi_smi_msg *msg)
3519{
3520 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003521 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003524 if (recv_msg == NULL) {
3525 printk(KERN_WARNING
3526 "IPMI message received with no owner. This\n"
3527 "could be because of a malformed message, or\n"
3528 "because of a hardware error. Contact your\n"
3529 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003530 return 0;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Corey Minyard393d2cc2005-11-07 00:59:54 -08003533 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003535 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003536 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003537 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 ipmi_free_recv_msg(recv_msg);
3539 } else {
3540 struct ipmi_system_interface_addr *smi_addr;
3541
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003542 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3544 recv_msg->msgid = msg->msgid;
3545 smi_addr = ((struct ipmi_system_interface_addr *)
3546 &(recv_msg->addr));
3547 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3548 smi_addr->channel = IPMI_BMC_CHANNEL;
3549 smi_addr->lun = msg->rsp[0] & 3;
3550 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3551 recv_msg->msg.cmd = msg->rsp[1];
3552 memcpy(recv_msg->msg_data,
3553 &(msg->rsp[2]),
3554 msg->rsp_size - 2);
3555 recv_msg->msg.data = recv_msg->msg_data;
3556 recv_msg->msg.data_len = msg->rsp_size - 2;
3557 deliver_response(recv_msg);
3558 }
3559
3560 return 0;
3561}
3562
Corey Minyardc70d7492008-04-29 01:01:09 -07003563/*
3564 * Handle a new message. Return 1 if the message should be requeued,
3565 * 0 if the message should be freed, or -1 if the message should not
3566 * be freed or requeued.
3567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568static int handle_new_recv_msg(ipmi_smi_t intf,
3569 struct ipmi_smi_msg *msg)
3570{
3571 int requeue;
3572 int chan;
3573
3574#ifdef DEBUG_MSGING
3575 int m;
3576 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003577 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 printk(" %2.2x", msg->rsp[m]);
3579 printk("\n");
3580#endif
3581 if (msg->rsp_size < 2) {
3582 /* Message is too small to be correct. */
3583 printk(KERN_WARNING PFX "BMC returned to small a message"
3584 " for netfn %x cmd %x, got %d bytes\n",
3585 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3586
3587 /* Generate an error response for the message. */
3588 msg->rsp[0] = msg->data[0] | (1 << 2);
3589 msg->rsp[1] = msg->data[1];
3590 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3591 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003592 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3593 || (msg->rsp[1] != msg->data[1])) {
3594 /*
3595 * The NetFN and Command in the response is not even
3596 * marginally correct.
3597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3599 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3600 (msg->data[0] >> 2) | 1, msg->data[1],
3601 msg->rsp[0] >> 2, msg->rsp[1]);
3602
3603 /* Generate an error response for the message. */
3604 msg->rsp[0] = msg->data[0] | (1 << 2);
3605 msg->rsp[1] = msg->data[1];
3606 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3607 msg->rsp_size = 3;
3608 }
3609
3610 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3611 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003612 && (msg->user_data != NULL)) {
3613 /*
3614 * It's a response to a response we sent. For this we
3615 * deliver a send message response to the user.
3616 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003617 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 requeue = 0;
3620 if (msg->rsp_size < 2)
3621 /* Message is too small to be correct. */
3622 goto out;
3623
3624 chan = msg->data[2] & 0x0f;
3625 if (chan >= IPMI_MAX_CHANNELS)
3626 /* Invalid channel number */
3627 goto out;
3628
Corey Minyard393d2cc2005-11-07 00:59:54 -08003629 if (!recv_msg)
3630 goto out;
3631
3632 /* Make sure the user still exists. */
3633 if (!recv_msg->user || !recv_msg->user->valid)
3634 goto out;
3635
3636 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3637 recv_msg->msg.data = recv_msg->msg_data;
3638 recv_msg->msg.data_len = 1;
3639 recv_msg->msg_data[0] = msg->rsp[2];
3640 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003642 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* It's from the receive queue. */
3644 chan = msg->rsp[3] & 0xf;
3645 if (chan >= IPMI_MAX_CHANNELS) {
3646 /* Invalid channel number */
3647 requeue = 0;
3648 goto out;
3649 }
3650
dann frazier4dec3022009-04-21 12:24:05 -07003651 /*
Corey Minyard9a2845c2009-05-20 13:36:17 -05003652 * We need to make sure the channels have been initialized.
3653 * The channel_handler routine will set the "curr_channel"
3654 * equal to or greater than IPMI_MAX_CHANNELS when all the
3655 * channels for this interface have been initialized.
3656 */
dann frazier4dec3022009-04-21 12:24:05 -07003657 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
Corey Minyard9a2845c2009-05-20 13:36:17 -05003658 requeue = 0; /* Throw the message away */
dann frazier4dec3022009-04-21 12:24:05 -07003659 goto out;
3660 }
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 switch (intf->channels[chan].medium) {
3663 case IPMI_CHANNEL_MEDIUM_IPMB:
3664 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003665 /*
3666 * It's a response, so find the
3667 * requesting message and send it up.
3668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3670 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003671 /*
3672 * It's a command to the SMS from some other
3673 * entity. Handle that.
3674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3676 }
3677 break;
3678
3679 case IPMI_CHANNEL_MEDIUM_8023LAN:
3680 case IPMI_CHANNEL_MEDIUM_ASYNC:
3681 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003682 /*
3683 * It's a response, so find the
3684 * requesting message and send it up.
3685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 requeue = handle_lan_get_msg_rsp(intf, msg);
3687 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003688 /*
3689 * It's a command to the SMS from some other
3690 * entity. Handle that.
3691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 requeue = handle_lan_get_msg_cmd(intf, msg);
3693 }
3694 break;
3695
3696 default:
dann frazier4dec3022009-04-21 12:24:05 -07003697 /* Check for OEM Channels. Clients had better
3698 register for these commands. */
3699 if ((intf->channels[chan].medium
3700 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3701 && (intf->channels[chan].medium
3702 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3703 requeue = handle_oem_get_msg_cmd(intf, msg);
3704 } else {
3705 /*
3706 * We don't handle the channel type, so just
3707 * free the message.
3708 */
3709 requeue = 0;
3710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 }
3712
3713 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003714 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 /* It's an asyncronous event. */
3716 requeue = handle_read_event_rsp(intf, msg);
3717 } else {
3718 /* It's a response from the local BMC. */
3719 requeue = handle_bmc_rsp(intf, msg);
3720 }
3721
3722 out:
3723 return requeue;
3724}
3725
3726/* Handle a new message from the lower layer. */
3727void ipmi_smi_msg_received(ipmi_smi_t intf,
3728 struct ipmi_smi_msg *msg)
3729{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003730 unsigned long flags = 0; /* keep us warning-free. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 int rv;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003732 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if ((msg->data_size >= 2)
3736 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3737 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003738 && (msg->user_data == NULL)) {
3739 /*
3740 * This is the local response to a command send, start
3741 * the timer for these. The user_data will not be
3742 * NULL if this is a response send, and we will let
3743 * response sends just go through.
3744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Corey Minyardc70d7492008-04-29 01:01:09 -07003746 /*
3747 * Check for errors, if we get certain errors (ones
3748 * that mean basically we can try again later), we
3749 * ignore them and start the timer. Otherwise we
3750 * report the error immediately.
3751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3753 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003754 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3755 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003756 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 int chan = msg->rsp[3] & 0xf;
3758
3759 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 if (chan >= IPMI_MAX_CHANNELS)
3761 ; /* This shouldn't happen */
3762 else if ((intf->channels[chan].medium
3763 == IPMI_CHANNEL_MEDIUM_8023LAN)
3764 || (intf->channels[chan].medium
3765 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003766 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003768 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003770 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 /* The message was sent, start the timer. */
3772 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 ipmi_free_smi_msg(msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777
Corey Minyardc70d7492008-04-29 01:01:09 -07003778 /*
3779 * To preserve message order, if the list is not empty, we
3780 * tack this message onto the end of the list.
3781 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003782 run_to_completion = intf->run_to_completion;
3783 if (!run_to_completion)
3784 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003785 if (!list_empty(&intf->waiting_msgs)) {
3786 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003787 if (!run_to_completion)
3788 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003789 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 }
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003791 if (!run_to_completion)
3792 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 rv = handle_new_recv_msg(intf, msg);
3795 if (rv > 0) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003796 /*
3797 * Could not handle the message now, just add it to a
3798 * list to handle later.
3799 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003800 run_to_completion = intf->run_to_completion;
3801 if (!run_to_completion)
3802 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003803 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003804 if (!run_to_completion)
3805 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 } else if (rv == 0) {
3807 ipmi_free_smi_msg(msg);
3808 }
3809
Corey Minyard393d2cc2005-11-07 00:59:54 -08003810 out:
3811 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
Corey Minyardc70d7492008-04-29 01:01:09 -07003813EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3816{
3817 ipmi_user_t user;
3818
Corey Minyard393d2cc2005-11-07 00:59:54 -08003819 rcu_read_lock();
3820 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003821 if (!user->handler->ipmi_watchdog_pretimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 continue;
3823
3824 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
3825 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003826 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
Corey Minyardc70d7492008-04-29 01:01:09 -07003828EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Corey Minyard882fe012005-05-01 08:59:12 -07003830static struct ipmi_smi_msg *
3831smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3832 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
Corey Minyard882fe012005-05-01 08:59:12 -07003834 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07003836 /*
3837 * If we can't allocate the message, then just return, we
3838 * get 4 retries, so this should be ok.
3839 */
Corey Minyard882fe012005-05-01 08:59:12 -07003840 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3843 smi_msg->data_size = recv_msg->msg.data_len;
3844 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07003845
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846#ifdef DEBUG_MSGING
3847 {
3848 int m;
3849 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07003850 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 printk(" %2.2x", smi_msg->data[m]);
3852 printk("\n");
3853 }
3854#endif
Corey Minyard882fe012005-05-01 08:59:12 -07003855 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
3857
Corey Minyard393d2cc2005-11-07 00:59:54 -08003858static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3859 struct list_head *timeouts, long timeout_period,
3860 int slot, unsigned long *flags)
3861{
Corey Minyardb2c03942006-12-06 20:41:00 -08003862 struct ipmi_recv_msg *msg;
3863 struct ipmi_smi_handlers *handlers;
3864
3865 if (intf->intf_num == -1)
3866 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003867
3868 if (!ent->inuse)
3869 return;
3870
3871 ent->timeout -= timeout_period;
3872 if (ent->timeout > 0)
3873 return;
3874
3875 if (ent->retries_left == 0) {
3876 /* The message has used all its retries. */
3877 ent->inuse = 0;
3878 msg = ent->recv_msg;
3879 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003880 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003881 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard25176ed2009-04-21 12:24:04 -07003882 else if (is_lan_addr(&ent->recv_msg->addr))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003883 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003884 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003885 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003886 } else {
3887 struct ipmi_smi_msg *smi_msg;
3888 /* More retries, send again. */
3889
Corey Minyardc70d7492008-04-29 01:01:09 -07003890 /*
3891 * Start with the max timer, set to normal timer after
3892 * the message is sent.
3893 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003894 ent->timeout = MAX_MSG_TIMEOUT;
3895 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003896 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
3897 ent->seqid);
Corey Minyard25176ed2009-04-21 12:24:04 -07003898 if (!smi_msg) {
3899 if (is_lan_addr(&ent->recv_msg->addr))
3900 ipmi_inc_stat(intf,
3901 dropped_rexmit_lan_commands);
3902 else
3903 ipmi_inc_stat(intf,
3904 dropped_rexmit_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003905 return;
Corey Minyard25176ed2009-04-21 12:24:04 -07003906 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003907
3908 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08003909
Corey Minyardc70d7492008-04-29 01:01:09 -07003910 /*
3911 * Send the new message. We send with a zero
3912 * priority. It timed out, I doubt time is that
3913 * critical now, and high priority messages are really
3914 * only for messages to the local MC, which don't get
3915 * resent.
3916 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003917 handlers = intf->handlers;
Corey Minyard25176ed2009-04-21 12:24:04 -07003918 if (handlers) {
3919 if (is_lan_addr(&ent->recv_msg->addr))
3920 ipmi_inc_stat(intf,
3921 retransmitted_lan_commands);
3922 else
3923 ipmi_inc_stat(intf,
3924 retransmitted_ipmb_commands);
3925
Corey Minyardb2c03942006-12-06 20:41:00 -08003926 intf->handlers->sender(intf->send_info,
3927 smi_msg, 0);
Corey Minyard25176ed2009-04-21 12:24:04 -07003928 } else
Corey Minyardb2c03942006-12-06 20:41:00 -08003929 ipmi_free_smi_msg(smi_msg);
3930
Corey Minyard393d2cc2005-11-07 00:59:54 -08003931 spin_lock_irqsave(&intf->seq_lock, *flags);
3932 }
3933}
3934
3935static void ipmi_timeout_handler(long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
3937 ipmi_smi_t intf;
3938 struct list_head timeouts;
3939 struct ipmi_recv_msg *msg, *msg2;
3940 struct ipmi_smi_msg *smi_msg, *smi_msg2;
3941 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08003942 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Corey Minyardbca03242006-12-06 20:40:57 -08003944 rcu_read_lock();
3945 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 /* See if any waiting messages need to be processed. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003947 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard8a3628d2006-03-31 02:30:40 -08003948 list_for_each_entry_safe(smi_msg, smi_msg2,
3949 &intf->waiting_msgs, link) {
3950 if (!handle_new_recv_msg(intf, smi_msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 list_del(&smi_msg->link);
3952 ipmi_free_smi_msg(smi_msg);
3953 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003954 /*
3955 * To preserve message order, quit if we
3956 * can't handle a message.
3957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 break;
3959 }
3960 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003961 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Corey Minyardc70d7492008-04-29 01:01:09 -07003963 /*
3964 * Go through the seq table and find any messages that
3965 * have timed out, putting them in the timeouts
3966 * list.
3967 */
David Barksdale41c57a82007-01-30 14:36:25 -08003968 INIT_LIST_HEAD(&timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003969 spin_lock_irqsave(&intf->seq_lock, flags);
Corey Minyardbca03242006-12-06 20:40:57 -08003970 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
3971 check_msg_timeout(intf, &(intf->seq_table[i]),
3972 &timeouts, timeout_period, i,
Corey Minyard393d2cc2005-11-07 00:59:54 -08003973 &flags);
3974 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Corey Minyard393d2cc2005-11-07 00:59:54 -08003976 list_for_each_entry_safe(msg, msg2, &timeouts, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003977 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Corey Minyardb9675132006-12-06 20:41:02 -08003978
3979 /*
3980 * Maintenance mode handling. Check the timeout
3981 * optimistically before we claim the lock. It may
3982 * mean a timeout gets missed occasionally, but that
3983 * only means the timeout gets extended by one period
3984 * in that case. No big deal, and it avoids the lock
3985 * most of the time.
3986 */
3987 if (intf->auto_maintenance_timeout > 0) {
3988 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
3989 if (intf->auto_maintenance_timeout > 0) {
3990 intf->auto_maintenance_timeout
3991 -= timeout_period;
3992 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07003993 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyardb9675132006-12-06 20:41:02 -08003994 intf->maintenance_mode_enable = 0;
3995 maintenance_mode_update(intf);
3996 }
3997 }
3998 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
3999 flags);
4000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
Corey Minyardbca03242006-12-06 20:40:57 -08004002 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
4005static void ipmi_request_event(void)
4006{
Corey Minyardb2c03942006-12-06 20:41:00 -08004007 ipmi_smi_t intf;
4008 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Corey Minyardbca03242006-12-06 20:40:57 -08004010 rcu_read_lock();
Corey Minyardc70d7492008-04-29 01:01:09 -07004011 /*
4012 * Called from the timer, no need to check if handlers is
4013 * valid.
4014 */
Corey Minyardb2c03942006-12-06 20:41:00 -08004015 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb9675132006-12-06 20:41:02 -08004016 /* No event requests when in maintenance mode. */
4017 if (intf->maintenance_mode_enable)
4018 continue;
4019
Corey Minyardb2c03942006-12-06 20:41:00 -08004020 handlers = intf->handlers;
4021 if (handlers)
4022 handlers->request_events(intf->send_info);
4023 }
Corey Minyardbca03242006-12-06 20:40:57 -08004024 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
4027static struct timer_list ipmi_timer;
4028
4029/* Call every ~100 ms. */
4030#define IPMI_TIMEOUT_TIME 100
4031
4032/* How many jiffies does it take to get to the timeout time. */
4033#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
4034
Corey Minyardc70d7492008-04-29 01:01:09 -07004035/*
4036 * Request events from the queue every second (this is the number of
4037 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
4038 * future, IPMI will add a way to know immediately if an event is in
4039 * the queue and this silliness can go away.
4040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
4042
Corey Minyard8f43f842005-06-23 22:01:40 -07004043static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4045
4046static void ipmi_timeout(unsigned long data)
4047{
Corey Minyard8f43f842005-06-23 22:01:40 -07004048 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 ticks_to_req_ev--;
4052 if (ticks_to_req_ev == 0) {
4053 ipmi_request_event();
4054 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4055 }
4056
4057 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
4058
Corey Minyard8f43f842005-06-23 22:01:40 -07004059 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
4062
4063static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4064static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4065
4066/* FIXME - convert these to slabs. */
4067static void free_smi_msg(struct ipmi_smi_msg *msg)
4068{
4069 atomic_dec(&smi_msg_inuse_count);
4070 kfree(msg);
4071}
4072
4073struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4074{
4075 struct ipmi_smi_msg *rv;
4076 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4077 if (rv) {
4078 rv->done = free_smi_msg;
4079 rv->user_data = NULL;
4080 atomic_inc(&smi_msg_inuse_count);
4081 }
4082 return rv;
4083}
Corey Minyardc70d7492008-04-29 01:01:09 -07004084EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086static void free_recv_msg(struct ipmi_recv_msg *msg)
4087{
4088 atomic_dec(&recv_msg_inuse_count);
4089 kfree(msg);
4090}
4091
Adrian Bunk74006302008-04-29 01:01:14 -07004092static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094 struct ipmi_recv_msg *rv;
4095
4096 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4097 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07004098 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 rv->done = free_recv_msg;
4100 atomic_inc(&recv_msg_inuse_count);
4101 }
4102 return rv;
4103}
4104
Corey Minyard393d2cc2005-11-07 00:59:54 -08004105void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4106{
4107 if (msg->user)
4108 kref_put(&msg->user->refcount, free_user);
4109 msg->done(msg);
4110}
Corey Minyardc70d7492008-04-29 01:01:09 -07004111EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113#ifdef CONFIG_IPMI_PANIC_EVENT
4114
4115static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4116{
4117}
4118
4119static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4120{
4121}
4122
4123#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07004124static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004126 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4127 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4128 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004129 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004131 intf->event_receiver = msg->msg.data[1];
4132 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
4134}
4135
Corey Minyard56a55ec2005-09-06 15:18:42 -07004136static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004138 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4139 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4140 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004141 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4142 /*
4143 * A get device id command, save if we are an event
4144 * receiver or generator.
4145 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004146 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4147 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
4149}
4150#endif
4151
4152static void send_panic_events(char *str)
4153{
4154 struct kernel_ipmi_msg msg;
4155 ipmi_smi_t intf;
4156 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 struct ipmi_system_interface_addr *si;
4158 struct ipmi_addr addr;
4159 struct ipmi_smi_msg smi_msg;
4160 struct ipmi_recv_msg recv_msg;
4161
4162 si = (struct ipmi_system_interface_addr *) &addr;
4163 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4164 si->channel = IPMI_BMC_CHANNEL;
4165 si->lun = 0;
4166
4167 /* Fill in an event telling that we have failed. */
4168 msg.netfn = 0x04; /* Sensor or Event. */
4169 msg.cmd = 2; /* Platform event command. */
4170 msg.data = data;
4171 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004172 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 data[1] = 0x03; /* This is for IPMI 1.0. */
4174 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4175 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4176 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4177
Corey Minyardc70d7492008-04-29 01:01:09 -07004178 /*
4179 * Put a few breadcrumbs in. Hopefully later we can add more things
4180 * to make the panic events more useful.
4181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 if (str) {
4183 data[3] = str[0];
4184 data[6] = str[1];
4185 data[7] = str[2];
4186 }
4187
4188 smi_msg.done = dummy_smi_done_handler;
4189 recv_msg.done = dummy_recv_done_handler;
4190
4191 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004192 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004193 if (!intf->handlers)
4194 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 continue;
4196
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004197 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 /* Send the event announcing the panic. */
4199 intf->handlers->set_run_to_completion(intf->send_info, 1);
4200 i_ipmi_request(NULL,
4201 intf,
4202 &addr,
4203 0,
4204 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004205 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 &smi_msg,
4207 &recv_msg,
4208 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004209 intf->channels[0].address,
4210 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 0, 1); /* Don't retry, and don't wait. */
4212 }
4213
4214#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004215 /*
4216 * On every interface, dump a bunch of OEM event holding the
4217 * string.
4218 */
4219 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return;
4221
Corey Minyardbca03242006-12-06 20:40:57 -08004222 /* For every registered interface, send the event. */
4223 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 char *p = str;
4225 struct ipmi_ipmb_addr *ipmb;
4226 int j;
4227
Corey Minyardbca03242006-12-06 20:40:57 -08004228 if (intf->intf_num == -1)
4229 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 continue;
4231
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004232 /*
4233 * intf_num is used as an marker to tell if the
4234 * interface is valid. Thus we need a read barrier to
4235 * make sure data fetched before checking intf_num
4236 * won't be used.
4237 */
4238 smp_rmb();
4239
Corey Minyardc70d7492008-04-29 01:01:09 -07004240 /*
4241 * First job here is to figure out where to send the
4242 * OEM events. There's no way in IPMI to send OEM
4243 * events using an event send command, so we have to
4244 * find the SEL to put them in and stick them in
4245 * there.
4246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 /* Get capabilities from the get device id. */
4249 intf->local_sel_device = 0;
4250 intf->local_event_generator = 0;
4251 intf->event_receiver = 0;
4252
4253 /* Request the device info from the local MC. */
4254 msg.netfn = IPMI_NETFN_APP_REQUEST;
4255 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4256 msg.data = NULL;
4257 msg.data_len = 0;
4258 intf->null_user_handler = device_id_fetcher;
4259 i_ipmi_request(NULL,
4260 intf,
4261 &addr,
4262 0,
4263 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004264 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 &smi_msg,
4266 &recv_msg,
4267 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004268 intf->channels[0].address,
4269 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 0, 1); /* Don't retry, and don't wait. */
4271
4272 if (intf->local_event_generator) {
4273 /* Request the event receiver from the local MC. */
4274 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4275 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4276 msg.data = NULL;
4277 msg.data_len = 0;
4278 intf->null_user_handler = event_receiver_fetcher;
4279 i_ipmi_request(NULL,
4280 intf,
4281 &addr,
4282 0,
4283 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004284 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 &smi_msg,
4286 &recv_msg,
4287 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004288 intf->channels[0].address,
4289 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 0, 1); /* no retry, and no wait. */
4291 }
4292 intf->null_user_handler = NULL;
4293
Corey Minyardc70d7492008-04-29 01:01:09 -07004294 /*
4295 * Validate the event receiver. The low bit must not
4296 * be 1 (it must be a valid IPMB address), it cannot
4297 * be zero, and it must not be my address.
4298 */
4299 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004301 && (intf->event_receiver != intf->channels[0].address)) {
4302 /*
4303 * The event receiver is valid, send an IPMB
4304 * message.
4305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 ipmb = (struct ipmi_ipmb_addr *) &addr;
4307 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4308 ipmb->channel = 0; /* FIXME - is this right? */
4309 ipmb->lun = intf->event_receiver_lun;
4310 ipmb->slave_addr = intf->event_receiver;
4311 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004312 /*
4313 * The event receiver was not valid (or was
4314 * me), but I am an SEL device, just dump it
4315 * in my SEL.
4316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 si = (struct ipmi_system_interface_addr *) &addr;
4318 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4319 si->channel = IPMI_BMC_CHANNEL;
4320 si->lun = 0;
4321 } else
4322 continue; /* No where to send the event. */
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4325 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4326 msg.data = data;
4327 msg.data_len = 16;
4328
4329 j = 0;
4330 while (*p) {
4331 int size = strlen(p);
4332
4333 if (size > 11)
4334 size = 11;
4335 data[0] = 0;
4336 data[1] = 0;
4337 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004338 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004340 /*
4341 * Always give 11 bytes, so strncpy will fill
4342 * it with zeroes for me.
4343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 strncpy(data+5, p, 11);
4345 p += size;
4346
4347 i_ipmi_request(NULL,
4348 intf,
4349 &addr,
4350 0,
4351 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004352 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 &smi_msg,
4354 &recv_msg,
4355 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004356 intf->channels[0].address,
4357 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 0, 1); /* no retry, and no wait. */
4359 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361#endif /* CONFIG_IPMI_PANIC_STRING */
4362}
4363#endif /* CONFIG_IPMI_PANIC_EVENT */
4364
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004365static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
4367static int panic_event(struct notifier_block *this,
4368 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004369 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 ipmi_smi_t intf;
4372
Lee Revellf18190b2006-06-26 18:30:00 +02004373 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004375 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004378 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004379 if (!intf->handlers)
4380 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 continue;
4382
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004383 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 intf->handlers->set_run_to_completion(intf->send_info, 1);
4385 }
4386
4387#ifdef CONFIG_IPMI_PANIC_EVENT
4388 send_panic_events(ptr);
4389#endif
4390
4391 return NOTIFY_DONE;
4392}
4393
4394static struct notifier_block panic_block = {
4395 .notifier_call = panic_event,
4396 .next = NULL,
4397 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4398};
4399
4400static int ipmi_init_msghandler(void)
4401{
Corey Minyard50c812b2006-03-26 01:37:21 -08004402 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404 if (initialized)
4405 return 0;
4406
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004407 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004408 if (rv) {
4409 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4410 return rv;
4411 }
4412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004414 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Corey Minyard3b625942005-06-23 22:01:42 -07004416#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4418 if (!proc_ipmi_root) {
4419 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4420 return -ENOMEM;
4421 }
4422
Corey Minyard3b625942005-06-23 22:01:42 -07004423#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Corey Minyard409035e2006-06-28 04:26:53 -07004425 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4426 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Alan Sterne041c682006-03-27 01:16:30 -08004428 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 initialized = 1;
4431
4432 return 0;
4433}
4434
4435static __init int ipmi_init_msghandler_mod(void)
4436{
4437 ipmi_init_msghandler();
4438 return 0;
4439}
4440
4441static __exit void cleanup_ipmi(void)
4442{
4443 int count;
4444
4445 if (!initialized)
4446 return;
4447
Alan Sterne041c682006-03-27 01:16:30 -08004448 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Corey Minyardc70d7492008-04-29 01:01:09 -07004450 /*
4451 * This can't be called if any interfaces exist, so no worry
4452 * about shutting down the interfaces.
4453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Corey Minyardc70d7492008-04-29 01:01:09 -07004455 /*
4456 * Tell the timer to stop, then wait for it to stop. This
4457 * avoids problems with race conditions removing the timer
4458 * here.
4459 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004460 atomic_inc(&stop_operation);
4461 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Corey Minyard3b625942005-06-23 22:01:42 -07004463#ifdef CONFIG_PROC_FS
Alexey Dobriyan3542ae42007-10-16 23:26:50 -07004464 remove_proc_entry(proc_ipmi_root->name, NULL);
Corey Minyard3b625942005-06-23 22:01:42 -07004465#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004467 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 initialized = 0;
4470
4471 /* Check for buffer leaks. */
4472 count = atomic_read(&smi_msg_inuse_count);
4473 if (count != 0)
4474 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4475 count);
4476 count = atomic_read(&recv_msg_inuse_count);
4477 if (count != 0)
4478 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4479 count);
4480}
4481module_exit(cleanup_ipmi);
4482
4483module_init(ipmi_init_msghandler_mod);
4484MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004485MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004486MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4487 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004488MODULE_VERSION(IPMI_DRIVER_VERSION);