blob: ec5e3f8df648ba1e719836a9b8349246005ba685 [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>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040038#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/spinlock.h>
Corey Minyardd6dfd132006-03-31 02:30:41 -080040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/ipmi.h>
43#include <linux/ipmi_smi.h>
44#include <linux/notifier.h>
45#include <linux/init.h>
46#include <linux/proc_fs.h>
Corey Minyard393d2cc2005-11-07 00:59:54 -080047#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define PFX "IPMI message handler: "
Corey Minyard1fdd75b2005-09-06 15:18:42 -070050
Corey Minyardf7caa1b2008-04-29 01:01:04 -070051#define IPMI_DRIVER_VERSION "39.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void);
54static int ipmi_init_msghandler(void);
55
Randy Dunlap0c8204b2006-12-10 02:19:06 -080056static int initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Corey Minyard3b625942005-06-23 22:01:42 -070058#ifdef CONFIG_PROC_FS
Randy Dunlap0c8204b2006-12-10 02:19:06 -080059static struct proc_dir_entry *proc_ipmi_root;
Corey Minyard3b625942005-06-23 22:01:42 -070060#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Corey Minyardb9675132006-12-06 20:41:02 -080062/* Remain in auto-maintenance mode for this amount of time (in ms). */
63#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define MAX_EVENTS_IN_QUEUE 25
66
Corey Minyardc70d7492008-04-29 01:01:09 -070067/*
68 * Don't let a message sit in a queue forever, always time it with at lest
69 * the max message timer. This is in milliseconds.
70 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define MAX_MSG_TIMEOUT 60000
72
Corey Minyard393d2cc2005-11-07 00:59:54 -080073/*
74 * The main "user" data structure.
75 */
Corey Minyardc70d7492008-04-29 01:01:09 -070076struct ipmi_user {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct list_head link;
78
Corey Minyard393d2cc2005-11-07 00:59:54 -080079 /* Set to "0" when the user is destroyed. */
80 int valid;
81
82 struct kref refcount;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* The upper layer that handles receive messages. */
85 struct ipmi_user_hndl *handler;
86 void *handler_data;
87
88 /* The interface this user is bound to. */
89 ipmi_smi_t intf;
90
91 /* Does this interface receive IPMI events? */
92 int gets_events;
93};
94
Corey Minyardc70d7492008-04-29 01:01:09 -070095struct cmd_rcvr {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct list_head link;
97
98 ipmi_user_t user;
99 unsigned char netfn;
100 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -0700101 unsigned int chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800102
103 /*
104 * This is used to form a linked lised during mass deletion.
105 * Since this is in an RCU list, we cannot use the link above
106 * or change any data until the RCU period completes. So we
107 * use this next variable during mass deletion so we can have
108 * a list and don't have to wait and restart the search on
Corey Minyardc70d7492008-04-29 01:01:09 -0700109 * every individual deletion of a command.
110 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800111 struct cmd_rcvr *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Corey Minyardc70d7492008-04-29 01:01:09 -0700114struct seq_table {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned int inuse : 1;
116 unsigned int broadcast : 1;
117
118 unsigned long timeout;
119 unsigned long orig_timeout;
120 unsigned int retries_left;
121
Corey Minyardc70d7492008-04-29 01:01:09 -0700122 /*
123 * To verify on an incoming send message response that this is
124 * the message that the response is for, we keep a sequence id
125 * and increment it every time we send a message.
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 long seqid;
128
Corey Minyardc70d7492008-04-29 01:01:09 -0700129 /*
130 * This is held so we can properly respond to the message on a
131 * timeout, and it is used to hold the temporary data for
132 * retransmission, too.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct ipmi_recv_msg *recv_msg;
135};
136
Corey Minyardc70d7492008-04-29 01:01:09 -0700137/*
138 * Store the information in a msgid (long) to allow us to find a
139 * sequence table entry from the msgid.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define STORE_SEQ_IN_MSGID(seq, seqid) (((seq&0xff)<<26) | (seqid&0x3ffffff))
142
143#define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \
144 do { \
145 seq = ((msgid >> 26) & 0x3f); \
146 seqid = (msgid & 0x3fffff); \
Corey Minyardc70d7492008-04-29 01:01:09 -0700147 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3fffff)
150
Corey Minyardc70d7492008-04-29 01:01:09 -0700151struct ipmi_channel {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned char medium;
153 unsigned char protocol;
Corey Minyardc14979b2005-09-06 15:18:38 -0700154
Corey Minyardc70d7492008-04-29 01:01:09 -0700155 /*
156 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
157 * but may be changed by the user.
158 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700159 unsigned char address;
160
Corey Minyardc70d7492008-04-29 01:01:09 -0700161 /*
162 * My LUN. This should generally stay the SMS LUN, but just in
163 * case...
164 */
Corey Minyardc14979b2005-09-06 15:18:38 -0700165 unsigned char lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
Corey Minyard3b625942005-06-23 22:01:42 -0700168#ifdef CONFIG_PROC_FS
Corey Minyardc70d7492008-04-29 01:01:09 -0700169struct ipmi_proc_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 char *name;
171 struct ipmi_proc_entry *next;
172};
Corey Minyard3b625942005-06-23 22:01:42 -0700173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Corey Minyardc70d7492008-04-29 01:01:09 -0700175struct bmc_device {
Corey Minyard50c812b2006-03-26 01:37:21 -0800176 struct platform_device *dev;
177 struct ipmi_device_id id;
178 unsigned char guid[16];
179 int guid_set;
180
181 struct kref refcount;
182
183 /* bmc device attributes */
184 struct device_attribute device_id_attr;
185 struct device_attribute provides_dev_sdrs_attr;
186 struct device_attribute revision_attr;
187 struct device_attribute firmware_rev_attr;
188 struct device_attribute version_attr;
189 struct device_attribute add_dev_support_attr;
190 struct device_attribute manufacturer_id_attr;
191 struct device_attribute product_id_attr;
192 struct device_attribute guid_attr;
193 struct device_attribute aux_firmware_rev_attr;
194};
195
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700196/*
197 * Various statistics for IPMI, these index stats[] in the ipmi_smi
198 * structure.
199 */
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700200enum ipmi_stat_indexes {
201 /* Commands we got from the user that were invalid. */
202 IPMI_STAT_sent_invalid_commands = 0,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700203
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700204 /* Commands we sent to the MC. */
205 IPMI_STAT_sent_local_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700206
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700207 /* Responses from the MC that were delivered to a user. */
208 IPMI_STAT_handled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700209
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700210 /* Responses from the MC that were not delivered to a user. */
211 IPMI_STAT_unhandled_local_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700212
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700213 /* Commands we sent out to the IPMB bus. */
214 IPMI_STAT_sent_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700215
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700216 /* Commands sent on the IPMB that had errors on the SEND CMD */
217 IPMI_STAT_sent_ipmb_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700218
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700219 /* Each retransmit increments this count. */
220 IPMI_STAT_retransmitted_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700221
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700222 /*
223 * When a message times out (runs out of retransmits) this is
224 * incremented.
225 */
226 IPMI_STAT_timed_out_ipmb_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700227
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700228 /*
229 * This is like above, but for broadcasts. Broadcasts are
230 * *not* included in the above count (they are expected to
231 * time out).
232 */
233 IPMI_STAT_timed_out_ipmb_broadcasts,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700234
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700235 /* Responses I have sent to the IPMB bus. */
236 IPMI_STAT_sent_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700237
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700238 /* The response was delivered to the user. */
239 IPMI_STAT_handled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700240
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700241 /* The response had invalid data in it. */
242 IPMI_STAT_invalid_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700243
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700244 /* The response didn't have anyone waiting for it. */
245 IPMI_STAT_unhandled_ipmb_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700246
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700247 /* Commands we sent out to the IPMB bus. */
248 IPMI_STAT_sent_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700249
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700250 /* Commands sent on the IPMB that had errors on the SEND CMD */
251 IPMI_STAT_sent_lan_command_errs,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700252
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700253 /* Each retransmit increments this count. */
254 IPMI_STAT_retransmitted_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700255
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700256 /*
257 * When a message times out (runs out of retransmits) this is
258 * incremented.
259 */
260 IPMI_STAT_timed_out_lan_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700261
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700262 /* Responses I have sent to the IPMB bus. */
263 IPMI_STAT_sent_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700264
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700265 /* The response was delivered to the user. */
266 IPMI_STAT_handled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700267
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700268 /* The response had invalid data in it. */
269 IPMI_STAT_invalid_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700270
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700271 /* The response didn't have anyone waiting for it. */
272 IPMI_STAT_unhandled_lan_responses,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700273
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700274 /* The command was delivered to the user. */
275 IPMI_STAT_handled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700276
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700277 /* The command had invalid data in it. */
278 IPMI_STAT_invalid_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700279
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700280 /* The command didn't have anyone waiting for it. */
281 IPMI_STAT_unhandled_commands,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700282
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700283 /* Invalid data in an event. */
284 IPMI_STAT_invalid_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700285
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700286 /* Events that were received with the proper format. */
287 IPMI_STAT_events,
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700288
Corey Minyard25176ed2009-04-21 12:24:04 -0700289 /* Retransmissions on IPMB that failed. */
290 IPMI_STAT_dropped_rexmit_ipmb_commands,
291
292 /* Retransmissions on LAN that failed. */
293 IPMI_STAT_dropped_rexmit_lan_commands,
Corey Minyard73f2bdb2008-04-29 01:01:06 -0700294
295 /* This *must* remain last, add new values above this. */
296 IPMI_NUM_STATS
297};
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700298
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#define IPMI_IPMB_NUM_SEQ 64
Corey Minyardc14979b2005-09-06 15:18:38 -0700301#define IPMI_MAX_CHANNELS 16
Corey Minyardc70d7492008-04-29 01:01:09 -0700302struct ipmi_smi {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* What interface number are we? */
304 int intf_num;
305
Corey Minyard393d2cc2005-11-07 00:59:54 -0800306 struct kref refcount;
307
Corey Minyardbca03242006-12-06 20:40:57 -0800308 /* Used for a list of interfaces. */
309 struct list_head link;
310
Corey Minyardc70d7492008-04-29 01:01:09 -0700311 /*
312 * The list of upper layers that are using me. seq_lock
313 * protects this.
314 */
Corey Minyard393d2cc2005-11-07 00:59:54 -0800315 struct list_head users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Corey Minyardb2c03942006-12-06 20:41:00 -0800317 /* Information to supply to users. */
318 unsigned char ipmi_version_major;
319 unsigned char ipmi_version_minor;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Used for wake ups at startup. */
322 wait_queue_head_t waitq;
323
Corey Minyard50c812b2006-03-26 01:37:21 -0800324 struct bmc_device *bmc;
325 char *my_dev_name;
Corey Minyard759643b2006-12-06 20:40:59 -0800326 char *sysfs_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Corey Minyardc70d7492008-04-29 01:01:09 -0700328 /*
329 * This is the lower-layer's sender routine. Note that you
Corey Minyardb2c03942006-12-06 20:41:00 -0800330 * must either be holding the ipmi_interfaces_mutex or be in
331 * an umpreemptible region to use this. You must fetch the
Corey Minyardc70d7492008-04-29 01:01:09 -0700332 * value into a local variable and make sure it is not NULL.
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ipmi_smi_handlers *handlers;
335 void *send_info;
336
Corey Minyard3b625942005-06-23 22:01:42 -0700337#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -0700338 /* A list of proc entries for this interface. */
339 struct mutex proc_entry_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct ipmi_proc_entry *proc_entries;
Corey Minyard3b625942005-06-23 22:01:42 -0700341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Corey Minyard50c812b2006-03-26 01:37:21 -0800343 /* Driver-model device for the system interface. */
344 struct device *si_dev;
345
Corey Minyardc70d7492008-04-29 01:01:09 -0700346 /*
347 * A table of sequence numbers for this interface. We use the
348 * sequence numbers for IPMB messages that go out of the
349 * interface to match them up with their responses. A routine
350 * is called periodically to time the items in this list.
351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 spinlock_t seq_lock;
353 struct seq_table seq_table[IPMI_IPMB_NUM_SEQ];
354 int curr_seq;
355
Corey Minyardc70d7492008-04-29 01:01:09 -0700356 /*
357 * Messages that were delayed for some reason (out of memory,
358 * for instance), will go in here to be processed later in a
359 * periodic timer interrupt.
360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spinlock_t waiting_msgs_lock;
362 struct list_head waiting_msgs;
363
Corey Minyardc70d7492008-04-29 01:01:09 -0700364 /*
365 * The list of command receivers that are registered for commands
366 * on this interface.
367 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800368 struct mutex cmd_rcvrs_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct list_head cmd_rcvrs;
370
Corey Minyardc70d7492008-04-29 01:01:09 -0700371 /*
372 * Events that were queues because no one was there to receive
373 * them.
374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spinlock_t events_lock; /* For dealing with event stuff. */
376 struct list_head waiting_events;
377 unsigned int waiting_events_count; /* How many events in queue? */
Corey Minyard87ebd062008-04-29 01:01:04 -0700378 char delivering_events;
379 char event_msg_printed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Corey Minyardc70d7492008-04-29 01:01:09 -0700381 /*
382 * The event receiver for my BMC, only really used at panic
383 * shutdown as a place to store this.
384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned char event_receiver;
386 unsigned char event_receiver_lun;
387 unsigned char local_sel_device;
388 unsigned char local_event_generator;
389
Corey Minyardb9675132006-12-06 20:41:02 -0800390 /* For handling of maintenance mode. */
391 int maintenance_mode;
392 int maintenance_mode_enable;
393 int auto_maintenance_timeout;
394 spinlock_t maintenance_mode_lock; /* Used in a timer... */
395
Corey Minyardc70d7492008-04-29 01:01:09 -0700396 /*
397 * A cheap hack, if this is non-null and a message to an
398 * interface comes in with a NULL user, call this routine with
399 * it. Note that the message will still be freed by the
400 * caller. This only works on the system interface.
401 */
Corey Minyard56a55ec2005-09-06 15:18:42 -0700402 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Corey Minyardc70d7492008-04-29 01:01:09 -0700404 /*
405 * When we are scanning the channels for an SMI, this will
406 * tell which channel we are scanning.
407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int curr_channel;
409
410 /* Channel information */
411 struct ipmi_channel channels[IPMI_MAX_CHANNELS];
412
413 /* Proc FS stuff. */
414 struct proc_dir_entry *proc_dir;
415 char proc_dir_name[10];
416
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700417 atomic_t stats[IPMI_NUM_STATS];
Konstantin Baydarov5956dce2008-04-29 01:01:03 -0700418
419 /*
420 * run_to_completion duplicate of smb_info, smi_info
421 * and ipmi_serial_info structures. Used to decrease numbers of
422 * parameters passed by "low" level IPMI code.
423 */
424 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
Corey Minyard50c812b2006-03-26 01:37:21 -0800426#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Corey Minyard50c812b2006-03-26 01:37:21 -0800428/**
429 * The driver model view of the IPMI messaging driver.
430 */
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800431static struct platform_driver ipmidriver = {
432 .driver = {
433 .name = "ipmi",
434 .bus = &platform_bus_type
435 }
Corey Minyard50c812b2006-03-26 01:37:21 -0800436};
437static DEFINE_MUTEX(ipmidriver_mutex);
438
Denis Chengbed97592008-02-06 01:37:35 -0800439static LIST_HEAD(ipmi_interfaces);
Corey Minyardbca03242006-12-06 20:40:57 -0800440static DEFINE_MUTEX(ipmi_interfaces_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Corey Minyardc70d7492008-04-29 01:01:09 -0700442/*
443 * List of watchers that want to know when smi's are added and deleted.
444 */
Denis Chengbed97592008-02-06 01:37:35 -0800445static LIST_HEAD(smi_watchers);
Corey Minyardb2c03942006-12-06 20:41:00 -0800446static DEFINE_MUTEX(smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700449#define ipmi_inc_stat(intf, stat) \
450 atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat])
451#define ipmi_get_stat(intf, stat) \
452 ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat]))
453
Corey Minyard25176ed2009-04-21 12:24:04 -0700454static int is_lan_addr(struct ipmi_addr *addr)
455{
456 return addr->addr_type == IPMI_LAN_ADDR_TYPE;
457}
458
459static int is_ipmb_addr(struct ipmi_addr *addr)
460{
461 return addr->addr_type == IPMI_IPMB_ADDR_TYPE;
462}
463
464static int is_ipmb_bcast_addr(struct ipmi_addr *addr)
465{
466 return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE;
467}
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700468
Corey Minyard393d2cc2005-11-07 00:59:54 -0800469static void free_recv_msg_list(struct list_head *q)
470{
471 struct ipmi_recv_msg *msg, *msg2;
472
473 list_for_each_entry_safe(msg, msg2, q, link) {
474 list_del(&msg->link);
475 ipmi_free_recv_msg(msg);
476 }
477}
478
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800479static void free_smi_msg_list(struct list_head *q)
480{
481 struct ipmi_smi_msg *msg, *msg2;
482
483 list_for_each_entry_safe(msg, msg2, q, link) {
484 list_del(&msg->link);
485 ipmi_free_smi_msg(msg);
486 }
487}
488
Corey Minyard393d2cc2005-11-07 00:59:54 -0800489static void clean_up_interface_data(ipmi_smi_t intf)
490{
491 int i;
492 struct cmd_rcvr *rcvr, *rcvr2;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800493 struct list_head list;
494
Corey Minyardf3ce6a02006-11-08 17:44:52 -0800495 free_smi_msg_list(&intf->waiting_msgs);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800496 free_recv_msg_list(&intf->waiting_events);
497
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800498 /*
499 * Wholesale remove all the entries from the list in the
500 * interface and wait for RCU to know that none are in use.
501 */
Corey Minyardd6dfd132006-03-31 02:30:41 -0800502 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800503 INIT_LIST_HEAD(&list);
504 list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu);
Corey Minyardd6dfd132006-03-31 02:30:41 -0800505 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800506
507 list_for_each_entry_safe(rcvr, rcvr2, &list, link)
508 kfree(rcvr);
509
510 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
511 if ((intf->seq_table[i].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700512 && (intf->seq_table[i].recv_msg))
Corey Minyard393d2cc2005-11-07 00:59:54 -0800513 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800515}
516
517static void intf_free(struct kref *ref)
518{
519 ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount);
520
521 clean_up_interface_data(intf);
522 kfree(intf);
523}
524
Corey Minyardbca03242006-12-06 20:40:57 -0800525struct watcher_entry {
Corey Minyardb2c03942006-12-06 20:41:00 -0800526 int intf_num;
527 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800528 struct list_head link;
Corey Minyardbca03242006-12-06 20:40:57 -0800529};
530
Corey Minyard393d2cc2005-11-07 00:59:54 -0800531int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher)
532{
Corey Minyardbca03242006-12-06 20:40:57 -0800533 ipmi_smi_t intf;
Denis Chenge381d1c2008-02-06 01:37:39 -0800534 LIST_HEAD(to_deliver);
Corey Minyardbca03242006-12-06 20:40:57 -0800535 struct watcher_entry *e, *e2;
536
Corey Minyardb2c03942006-12-06 20:41:00 -0800537 mutex_lock(&smi_watchers_mutex);
538
Corey Minyardbca03242006-12-06 20:40:57 -0800539 mutex_lock(&ipmi_interfaces_mutex);
540
Corey Minyardb2c03942006-12-06 20:41:00 -0800541 /* Build a list of things to deliver. */
Corey Minyard78ba2fa2007-02-10 01:45:45 -0800542 list_for_each_entry(intf, &ipmi_interfaces, link) {
Corey Minyardbca03242006-12-06 20:40:57 -0800543 if (intf->intf_num == -1)
544 continue;
545 e = kmalloc(sizeof(*e), GFP_KERNEL);
546 if (!e)
547 goto out_err;
Corey Minyardb2c03942006-12-06 20:41:00 -0800548 kref_get(&intf->refcount);
549 e->intf = intf;
Corey Minyardbca03242006-12-06 20:40:57 -0800550 e->intf_num = intf->intf_num;
551 list_add_tail(&e->link, &to_deliver);
552 }
Corey Minyard393d2cc2005-11-07 00:59:54 -0800553
Corey Minyardb2c03942006-12-06 20:41:00 -0800554 /* We will succeed, so add it to the list. */
555 list_add(&watcher->link, &smi_watchers);
Corey Minyardbca03242006-12-06 20:40:57 -0800556
557 mutex_unlock(&ipmi_interfaces_mutex);
558
559 list_for_each_entry_safe(e, e2, &to_deliver, link) {
560 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800561 watcher->new_smi(e->intf_num, e->intf->si_dev);
562 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800563 kfree(e);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800564 }
Corey Minyardbca03242006-12-06 20:40:57 -0800565
Corey Minyardb2c03942006-12-06 20:41:00 -0800566 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
Corey Minyardbca03242006-12-06 20:40:57 -0800569
570 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -0800571 mutex_unlock(&ipmi_interfaces_mutex);
572 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800573 list_for_each_entry_safe(e, e2, &to_deliver, link) {
574 list_del(&e->link);
Corey Minyardb2c03942006-12-06 20:41:00 -0800575 kref_put(&e->intf->refcount, intf_free);
Corey Minyardbca03242006-12-06 20:40:57 -0800576 kfree(e);
577 }
578 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
Corey Minyardc70d7492008-04-29 01:01:09 -0700580EXPORT_SYMBOL(ipmi_smi_watcher_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher)
583{
Corey Minyardb2c03942006-12-06 20:41:00 -0800584 mutex_lock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_del(&(watcher->link));
Corey Minyardb2c03942006-12-06 20:41:00 -0800586 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588}
Corey Minyardc70d7492008-04-29 01:01:09 -0700589EXPORT_SYMBOL(ipmi_smi_watcher_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Corey Minyardb2c03942006-12-06 20:41:00 -0800591/*
592 * Must be called with smi_watchers_mutex held.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static void
Corey Minyard50c812b2006-03-26 01:37:21 -0800595call_smi_watchers(int i, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct ipmi_smi_watcher *w;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_for_each_entry(w, &smi_watchers, link) {
600 if (try_module_get(w->owner)) {
Corey Minyard50c812b2006-03-26 01:37:21 -0800601 w->new_smi(i, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 module_put(w->owner);
603 }
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int
608ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2)
609{
610 if (addr1->addr_type != addr2->addr_type)
611 return 0;
612
613 if (addr1->channel != addr2->channel)
614 return 0;
615
616 if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
617 struct ipmi_system_interface_addr *smi_addr1
618 = (struct ipmi_system_interface_addr *) addr1;
619 struct ipmi_system_interface_addr *smi_addr2
620 = (struct ipmi_system_interface_addr *) addr2;
621 return (smi_addr1->lun == smi_addr2->lun);
622 }
623
Corey Minyard25176ed2009-04-21 12:24:04 -0700624 if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct ipmi_ipmb_addr *ipmb_addr1
626 = (struct ipmi_ipmb_addr *) addr1;
627 struct ipmi_ipmb_addr *ipmb_addr2
628 = (struct ipmi_ipmb_addr *) addr2;
629
630 return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr)
631 && (ipmb_addr1->lun == ipmb_addr2->lun));
632 }
633
Corey Minyard25176ed2009-04-21 12:24:04 -0700634 if (is_lan_addr(addr1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct ipmi_lan_addr *lan_addr1
636 = (struct ipmi_lan_addr *) addr1;
637 struct ipmi_lan_addr *lan_addr2
638 = (struct ipmi_lan_addr *) addr2;
639
640 return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID)
641 && (lan_addr1->local_SWID == lan_addr2->local_SWID)
642 && (lan_addr1->session_handle
643 == lan_addr2->session_handle)
644 && (lan_addr1->lun == lan_addr2->lun));
645 }
646
647 return 1;
648}
649
650int ipmi_validate_addr(struct ipmi_addr *addr, int len)
651{
Corey Minyardc70d7492008-04-29 01:01:09 -0700652 if (len < sizeof(struct ipmi_system_interface_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
656 if (addr->channel != IPMI_BMC_CHANNEL)
657 return -EINVAL;
658 return 0;
659 }
660
661 if ((addr->channel == IPMI_BMC_CHANNEL)
Jayachandran C12fc1d72006-02-03 03:04:51 -0800662 || (addr->channel >= IPMI_MAX_CHANNELS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 || (addr->channel < 0))
664 return -EINVAL;
665
Corey Minyard25176ed2009-04-21 12:24:04 -0700666 if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700667 if (len < sizeof(struct ipmi_ipmb_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 0;
670 }
671
Corey Minyard25176ed2009-04-21 12:24:04 -0700672 if (is_lan_addr(addr)) {
Corey Minyardc70d7492008-04-29 01:01:09 -0700673 if (len < sizeof(struct ipmi_lan_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676 }
677
678 return -EINVAL;
679}
Corey Minyardc70d7492008-04-29 01:01:09 -0700680EXPORT_SYMBOL(ipmi_validate_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682unsigned int ipmi_addr_length(int addr_type)
683{
684 if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
685 return sizeof(struct ipmi_system_interface_addr);
686
687 if ((addr_type == IPMI_IPMB_ADDR_TYPE)
Corey Minyardc70d7492008-04-29 01:01:09 -0700688 || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return sizeof(struct ipmi_ipmb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (addr_type == IPMI_LAN_ADDR_TYPE)
692 return sizeof(struct ipmi_lan_addr);
693
694 return 0;
695}
Corey Minyardc70d7492008-04-29 01:01:09 -0700696EXPORT_SYMBOL(ipmi_addr_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698static void deliver_response(struct ipmi_recv_msg *msg)
699{
Corey Minyard8a3628d2006-03-31 02:30:40 -0800700 if (!msg->user) {
Corey Minyard56a55ec2005-09-06 15:18:42 -0700701 ipmi_smi_t intf = msg->user_msg_data;
Corey Minyard56a55ec2005-09-06 15:18:42 -0700702
703 /* Special handling for NULL users. */
704 if (intf->null_user_handler) {
705 intf->null_user_handler(intf, msg);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700706 ipmi_inc_stat(intf, handled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700707 } else {
708 /* No handler, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -0700709 ipmi_inc_stat(intf, unhandled_local_responses);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700710 }
711 ipmi_free_recv_msg(msg);
712 } else {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800713 ipmi_user_t user = msg->user;
714 user->handler->ipmi_recv_hndl(msg, user->handler_data);
Corey Minyard56a55ec2005-09-06 15:18:42 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Corey Minyardb2c03942006-12-06 20:41:00 -0800718static void
719deliver_err_response(struct ipmi_recv_msg *msg, int err)
720{
721 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
722 msg->msg_data[0] = err;
723 msg->msg.netfn |= 1; /* Convert to a response. */
724 msg->msg.data_len = 1;
725 msg->msg.data = msg->msg_data;
726 deliver_response(msg);
727}
728
Corey Minyardc70d7492008-04-29 01:01:09 -0700729/*
730 * Find the next sequence number not being used and add the given
731 * message with the given timeout to the sequence table. This must be
732 * called with the interface's seq_lock held.
733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static int intf_next_seq(ipmi_smi_t intf,
735 struct ipmi_recv_msg *recv_msg,
736 unsigned long timeout,
737 int retries,
738 int broadcast,
739 unsigned char *seq,
740 long *seqid)
741{
742 int rv = 0;
743 unsigned int i;
744
Corey Minyardc70d7492008-04-29 01:01:09 -0700745 for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq;
746 i = (i+1)%IPMI_IPMB_NUM_SEQ) {
Corey Minyard8a3628d2006-03-31 02:30:40 -0800747 if (!intf->seq_table[i].inuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 }
750
Corey Minyard8a3628d2006-03-31 02:30:40 -0800751 if (!intf->seq_table[i].inuse) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 intf->seq_table[i].recv_msg = recv_msg;
753
Corey Minyardc70d7492008-04-29 01:01:09 -0700754 /*
755 * Start with the maximum timeout, when the send response
756 * comes in we will start the real timer.
757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 intf->seq_table[i].timeout = MAX_MSG_TIMEOUT;
759 intf->seq_table[i].orig_timeout = timeout;
760 intf->seq_table[i].retries_left = retries;
761 intf->seq_table[i].broadcast = broadcast;
762 intf->seq_table[i].inuse = 1;
763 intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid);
764 *seq = i;
765 *seqid = intf->seq_table[i].seqid;
766 intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ;
767 } else {
768 rv = -EAGAIN;
769 }
Corey Minyardc70d7492008-04-29 01:01:09 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return rv;
772}
773
Corey Minyardc70d7492008-04-29 01:01:09 -0700774/*
775 * Return the receive message for the given sequence number and
776 * release the sequence number so it can be reused. Some other data
777 * is passed in to be sure the message matches up correctly (to help
778 * guard against message coming in after their timeout and the
779 * sequence number being reused).
780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int intf_find_seq(ipmi_smi_t intf,
782 unsigned char seq,
783 short channel,
784 unsigned char cmd,
785 unsigned char netfn,
786 struct ipmi_addr *addr,
787 struct ipmi_recv_msg **recv_msg)
788{
789 int rv = -ENODEV;
790 unsigned long flags;
791
792 if (seq >= IPMI_IPMB_NUM_SEQ)
793 return -EINVAL;
794
795 spin_lock_irqsave(&(intf->seq_lock), flags);
796 if (intf->seq_table[seq].inuse) {
797 struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg;
798
Corey Minyardc70d7492008-04-29 01:01:09 -0700799 if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd)
800 && (msg->msg.netfn == netfn)
801 && (ipmi_addr_equal(addr, &(msg->addr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *recv_msg = msg;
803 intf->seq_table[seq].inuse = 0;
804 rv = 0;
805 }
806 }
807 spin_unlock_irqrestore(&(intf->seq_lock), flags);
808
809 return rv;
810}
811
812
813/* Start the timer for a specific sequence table entry. */
814static int intf_start_seq_timer(ipmi_smi_t intf,
815 long msgid)
816{
817 int rv = -ENODEV;
818 unsigned long flags;
819 unsigned char seq;
820 unsigned long seqid;
821
822
823 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
824
825 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700826 /*
827 * We do this verification because the user can be deleted
828 * while a message is outstanding.
829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700831 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct seq_table *ent = &(intf->seq_table[seq]);
833 ent->timeout = ent->orig_timeout;
834 rv = 0;
835 }
836 spin_unlock_irqrestore(&(intf->seq_lock), flags);
837
838 return rv;
839}
840
841/* Got an error for the send message for a specific sequence number. */
842static int intf_err_seq(ipmi_smi_t intf,
843 long msgid,
844 unsigned int err)
845{
846 int rv = -ENODEV;
847 unsigned long flags;
848 unsigned char seq;
849 unsigned long seqid;
850 struct ipmi_recv_msg *msg = NULL;
851
852
853 GET_SEQ_FROM_MSGID(msgid, seq, seqid);
854
855 spin_lock_irqsave(&(intf->seq_lock), flags);
Corey Minyardc70d7492008-04-29 01:01:09 -0700856 /*
857 * We do this verification because the user can be deleted
858 * while a message is outstanding.
859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if ((intf->seq_table[seq].inuse)
Corey Minyardc70d7492008-04-29 01:01:09 -0700861 && (intf->seq_table[seq].seqid == seqid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct seq_table *ent = &(intf->seq_table[seq]);
863
864 ent->inuse = 0;
865 msg = ent->recv_msg;
866 rv = 0;
867 }
868 spin_unlock_irqrestore(&(intf->seq_lock), flags);
869
Corey Minyardb2c03942006-12-06 20:41:00 -0800870 if (msg)
871 deliver_err_response(msg, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 return rv;
874}
875
876
877int ipmi_create_user(unsigned int if_num,
878 struct ipmi_user_hndl *handler,
879 void *handler_data,
880 ipmi_user_t *user)
881{
882 unsigned long flags;
883 ipmi_user_t new_user;
884 int rv = 0;
885 ipmi_smi_t intf;
886
Corey Minyardc70d7492008-04-29 01:01:09 -0700887 /*
888 * There is no module usecount here, because it's not
889 * required. Since this can only be used by and called from
890 * other modules, they will implicitly use this module, and
891 * thus this can't be removed unless the other modules are
892 * removed.
893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (handler == NULL)
896 return -EINVAL;
897
Corey Minyardc70d7492008-04-29 01:01:09 -0700898 /*
899 * Make sure the driver is actually initialized, this handles
900 * problems with initialization order.
901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (!initialized) {
903 rv = ipmi_init_msghandler();
904 if (rv)
905 return rv;
906
Corey Minyardc70d7492008-04-29 01:01:09 -0700907 /*
908 * The init code doesn't return an error if it was turned
909 * off, but it won't initialize. Check that.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!initialized)
912 return -ENODEV;
913 }
914
915 new_user = kmalloc(sizeof(*new_user), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -0800916 if (!new_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -ENOMEM;
918
Corey Minyardb2c03942006-12-06 20:41:00 -0800919 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -0800920 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
921 if (intf->intf_num == if_num)
922 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Corey Minyardb2c03942006-12-06 20:41:00 -0800924 /* Not found, return an error */
Corey Minyardbca03242006-12-06 20:40:57 -0800925 rv = -EINVAL;
926 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Corey Minyardbca03242006-12-06 20:40:57 -0800928 found:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800929 /* Note that each existing user holds a refcount to the interface. */
930 kref_get(&intf->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Corey Minyard393d2cc2005-11-07 00:59:54 -0800932 kref_init(&new_user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 new_user->handler = handler;
934 new_user->handler_data = handler_data;
935 new_user->intf = intf;
936 new_user->gets_events = 0;
937
938 if (!try_module_get(intf->handlers->owner)) {
939 rv = -ENODEV;
Adrian Bunk5c98d292006-03-25 03:07:52 -0800940 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 if (intf->handlers->inc_usecount) {
944 rv = intf->handlers->inc_usecount(intf->send_info);
945 if (rv) {
946 module_put(intf->handlers->owner);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800947 goto out_kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 }
950
Corey Minyardc70d7492008-04-29 01:01:09 -0700951 /*
952 * Hold the lock so intf->handlers is guaranteed to be good
953 * until now
954 */
Corey Minyardb2c03942006-12-06 20:41:00 -0800955 mutex_unlock(&ipmi_interfaces_mutex);
956
Corey Minyard393d2cc2005-11-07 00:59:54 -0800957 new_user->valid = 1;
958 spin_lock_irqsave(&intf->seq_lock, flags);
959 list_add_rcu(&new_user->link, &intf->users);
960 spin_unlock_irqrestore(&intf->seq_lock, flags);
961 *user = new_user;
962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Adrian Bunk5c98d292006-03-25 03:07:52 -0800964out_kref:
Corey Minyard393d2cc2005-11-07 00:59:54 -0800965 kref_put(&intf->refcount, intf_free);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800966out_kfree:
Corey Minyardb2c03942006-12-06 20:41:00 -0800967 mutex_unlock(&ipmi_interfaces_mutex);
Adrian Bunk5c98d292006-03-25 03:07:52 -0800968 kfree(new_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return rv;
970}
Corey Minyardc70d7492008-04-29 01:01:09 -0700971EXPORT_SYMBOL(ipmi_create_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Corey Minyard393d2cc2005-11-07 00:59:54 -0800973static void free_user(struct kref *ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800975 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 kfree(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979int ipmi_destroy_user(ipmi_user_t user)
980{
Corey Minyard393d2cc2005-11-07 00:59:54 -0800981 ipmi_smi_t intf = user->intf;
982 int i;
983 unsigned long flags;
984 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800985 struct cmd_rcvr *rcvrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Corey Minyard8a3628d2006-03-31 02:30:40 -0800987 user->valid = 0;
Corey Minyard393d2cc2005-11-07 00:59:54 -0800988
989 /* Remove the user from the interface's sequence table. */
990 spin_lock_irqsave(&intf->seq_lock, flags);
991 list_del_rcu(&user->link);
992
993 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
994 if (intf->seq_table[i].inuse
Corey Minyardc70d7492008-04-29 01:01:09 -0700995 && (intf->seq_table[i].recv_msg->user == user)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -0800996 intf->seq_table[i].inuse = 0;
Corey Minyardb2c03942006-12-06 20:41:00 -0800997 ipmi_free_recv_msg(intf->seq_table[i].recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -0800998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001000 spin_unlock_irqrestore(&intf->seq_lock, flags);
1001
1002 /*
1003 * Remove the user from the command receiver's table. First
1004 * we build a list of everything (not using the standard link,
1005 * since other things may be using it till we do
1006 * synchronize_rcu()) then free everything in that list.
1007 */
Corey Minyardd6dfd132006-03-31 02:30:41 -08001008 mutex_lock(&intf->cmd_rcvrs_mutex);
Paul E. McKenney066bb8d2006-01-06 00:19:53 -08001009 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001010 if (rcvr->user == user) {
1011 list_del_rcu(&rcvr->link);
1012 rcvr->next = rcvrs;
1013 rcvrs = rcvr;
1014 }
1015 }
Corey Minyardd6dfd132006-03-31 02:30:41 -08001016 mutex_unlock(&intf->cmd_rcvrs_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001017 synchronize_rcu();
1018 while (rcvrs) {
1019 rcvr = rcvrs;
1020 rcvrs = rcvr->next;
1021 kfree(rcvr);
1022 }
1023
Corey Minyardb2c03942006-12-06 20:41:00 -08001024 mutex_lock(&ipmi_interfaces_mutex);
1025 if (intf->handlers) {
1026 module_put(intf->handlers->owner);
1027 if (intf->handlers->dec_usecount)
1028 intf->handlers->dec_usecount(intf->send_info);
1029 }
1030 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08001031
1032 kref_put(&intf->refcount, intf_free);
1033
1034 kref_put(&user->refcount, free_user);
1035
Corey Minyard8a3628d2006-03-31 02:30:40 -08001036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
Corey Minyardc70d7492008-04-29 01:01:09 -07001038EXPORT_SYMBOL(ipmi_destroy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040void ipmi_get_version(ipmi_user_t user,
1041 unsigned char *major,
1042 unsigned char *minor)
1043{
Corey Minyardb2c03942006-12-06 20:41:00 -08001044 *major = user->intf->ipmi_version_major;
1045 *minor = user->intf->ipmi_version_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Corey Minyardc70d7492008-04-29 01:01:09 -07001047EXPORT_SYMBOL(ipmi_get_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Corey Minyardc14979b2005-09-06 15:18:38 -07001049int ipmi_set_my_address(ipmi_user_t user,
1050 unsigned int channel,
1051 unsigned char address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Corey Minyardc14979b2005-09-06 15:18:38 -07001053 if (channel >= IPMI_MAX_CHANNELS)
1054 return -EINVAL;
1055 user->intf->channels[channel].address = address;
1056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
Corey Minyardc70d7492008-04-29 01:01:09 -07001058EXPORT_SYMBOL(ipmi_set_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Corey Minyardc14979b2005-09-06 15:18:38 -07001060int ipmi_get_my_address(ipmi_user_t user,
1061 unsigned int channel,
1062 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Corey Minyardc14979b2005-09-06 15:18:38 -07001064 if (channel >= IPMI_MAX_CHANNELS)
1065 return -EINVAL;
1066 *address = user->intf->channels[channel].address;
1067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
Corey Minyardc70d7492008-04-29 01:01:09 -07001069EXPORT_SYMBOL(ipmi_get_my_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Corey Minyardc14979b2005-09-06 15:18:38 -07001071int ipmi_set_my_LUN(ipmi_user_t user,
1072 unsigned int channel,
1073 unsigned char LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Corey Minyardc14979b2005-09-06 15:18:38 -07001075 if (channel >= IPMI_MAX_CHANNELS)
1076 return -EINVAL;
1077 user->intf->channels[channel].lun = LUN & 0x3;
1078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
Corey Minyardc70d7492008-04-29 01:01:09 -07001080EXPORT_SYMBOL(ipmi_set_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Corey Minyardc14979b2005-09-06 15:18:38 -07001082int ipmi_get_my_LUN(ipmi_user_t user,
1083 unsigned int channel,
1084 unsigned char *address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Corey Minyardc14979b2005-09-06 15:18:38 -07001086 if (channel >= IPMI_MAX_CHANNELS)
1087 return -EINVAL;
1088 *address = user->intf->channels[channel].lun;
1089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Corey Minyardc70d7492008-04-29 01:01:09 -07001091EXPORT_SYMBOL(ipmi_get_my_LUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Corey Minyardb9675132006-12-06 20:41:02 -08001093int ipmi_get_maintenance_mode(ipmi_user_t user)
1094{
1095 int mode;
1096 unsigned long flags;
1097
1098 spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags);
1099 mode = user->intf->maintenance_mode;
1100 spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags);
1101
1102 return mode;
1103}
1104EXPORT_SYMBOL(ipmi_get_maintenance_mode);
1105
1106static void maintenance_mode_update(ipmi_smi_t intf)
1107{
1108 if (intf->handlers->set_maintenance_mode)
1109 intf->handlers->set_maintenance_mode(
1110 intf->send_info, intf->maintenance_mode_enable);
1111}
1112
1113int ipmi_set_maintenance_mode(ipmi_user_t user, int mode)
1114{
1115 int rv = 0;
1116 unsigned long flags;
1117 ipmi_smi_t intf = user->intf;
1118
1119 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1120 if (intf->maintenance_mode != mode) {
1121 switch (mode) {
1122 case IPMI_MAINTENANCE_MODE_AUTO:
1123 intf->maintenance_mode = mode;
1124 intf->maintenance_mode_enable
1125 = (intf->auto_maintenance_timeout > 0);
1126 break;
1127
1128 case IPMI_MAINTENANCE_MODE_OFF:
1129 intf->maintenance_mode = mode;
1130 intf->maintenance_mode_enable = 0;
1131 break;
1132
1133 case IPMI_MAINTENANCE_MODE_ON:
1134 intf->maintenance_mode = mode;
1135 intf->maintenance_mode_enable = 1;
1136 break;
1137
1138 default:
1139 rv = -EINVAL;
1140 goto out_unlock;
1141 }
1142
1143 maintenance_mode_update(intf);
1144 }
1145 out_unlock:
1146 spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags);
1147
1148 return rv;
1149}
1150EXPORT_SYMBOL(ipmi_set_maintenance_mode);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152int ipmi_set_gets_events(ipmi_user_t user, int val)
1153{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001154 unsigned long flags;
1155 ipmi_smi_t intf = user->intf;
1156 struct ipmi_recv_msg *msg, *msg2;
1157 struct list_head msgs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Corey Minyard393d2cc2005-11-07 00:59:54 -08001159 INIT_LIST_HEAD(&msgs);
1160
1161 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 user->gets_events = val;
1163
Corey Minyardb2c03942006-12-06 20:41:00 -08001164 if (intf->delivering_events)
1165 /*
1166 * Another thread is delivering events for this, so
1167 * let it handle any new events.
1168 */
1169 goto out;
1170
1171 /* Deliver any queued events. */
1172 while (user->gets_events && !list_empty(&intf->waiting_events)) {
Akinobu Mita179e0912006-06-26 00:24:41 -07001173 list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link)
1174 list_move_tail(&msg->link, &msgs);
Corey Minyard4791c032006-04-10 22:54:31 -07001175 intf->waiting_events_count = 0;
Corey Minyard87ebd062008-04-29 01:01:04 -07001176 if (intf->event_msg_printed) {
1177 printk(KERN_WARNING PFX "Event queue no longer"
1178 " full\n");
1179 intf->event_msg_printed = 0;
1180 }
Corey Minyardb2c03942006-12-06 20:41:00 -08001181
1182 intf->delivering_events = 1;
1183 spin_unlock_irqrestore(&intf->events_lock, flags);
1184
1185 list_for_each_entry_safe(msg, msg2, &msgs, link) {
1186 msg->user = user;
1187 kref_get(&user->refcount);
1188 deliver_response(msg);
1189 }
1190
1191 spin_lock_irqsave(&intf->events_lock, flags);
1192 intf->delivering_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08001194
Corey Minyardb2c03942006-12-06 20:41:00 -08001195 out:
Corey Minyard393d2cc2005-11-07 00:59:54 -08001196 spin_unlock_irqrestore(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 return 0;
1199}
Corey Minyardc70d7492008-04-29 01:01:09 -07001200EXPORT_SYMBOL(ipmi_set_gets_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Corey Minyard393d2cc2005-11-07 00:59:54 -08001202static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf,
1203 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001204 unsigned char cmd,
1205 unsigned char chan)
Corey Minyard393d2cc2005-11-07 00:59:54 -08001206{
1207 struct cmd_rcvr *rcvr;
1208
1209 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
Corey Minyardc69c3122006-09-30 23:27:56 -07001210 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1211 && (rcvr->chans & (1 << chan)))
Corey Minyard393d2cc2005-11-07 00:59:54 -08001212 return rcvr;
1213 }
1214 return NULL;
1215}
1216
Corey Minyardc69c3122006-09-30 23:27:56 -07001217static int is_cmd_rcvr_exclusive(ipmi_smi_t intf,
1218 unsigned char netfn,
1219 unsigned char cmd,
1220 unsigned int chans)
1221{
1222 struct cmd_rcvr *rcvr;
1223
1224 list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) {
1225 if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd)
1226 && (rcvr->chans & chans))
1227 return 0;
1228 }
1229 return 1;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232int ipmi_register_for_cmd(ipmi_user_t user,
1233 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001234 unsigned char cmd,
1235 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001237 ipmi_smi_t intf = user->intf;
1238 struct cmd_rcvr *rcvr;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001239 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241
1242 rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001243 if (!rcvr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -ENOMEM;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001245 rcvr->cmd = cmd;
1246 rcvr->netfn = netfn;
Corey Minyardc69c3122006-09-30 23:27:56 -07001247 rcvr->chans = chans;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001248 rcvr->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Corey Minyardd6dfd132006-03-31 02:30:41 -08001250 mutex_lock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* Make sure the command/netfn is not already registered. */
Corey Minyardc69c3122006-09-30 23:27:56 -07001252 if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08001253 rv = -EBUSY;
1254 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Corey Minyard393d2cc2005-11-07 00:59:54 -08001257 list_add_rcu(&rcvr->link, &intf->cmd_rcvrs);
Corey Minyard877197e2005-09-06 15:18:45 -07001258
Corey Minyard393d2cc2005-11-07 00:59:54 -08001259 out_unlock:
Corey Minyardd6dfd132006-03-31 02:30:41 -08001260 mutex_unlock(&intf->cmd_rcvrs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (rv)
1262 kfree(rcvr);
1263
1264 return rv;
1265}
Corey Minyardc70d7492008-04-29 01:01:09 -07001266EXPORT_SYMBOL(ipmi_register_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268int ipmi_unregister_for_cmd(ipmi_user_t user,
1269 unsigned char netfn,
Corey Minyardc69c3122006-09-30 23:27:56 -07001270 unsigned char cmd,
1271 unsigned int chans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Corey Minyard393d2cc2005-11-07 00:59:54 -08001273 ipmi_smi_t intf = user->intf;
1274 struct cmd_rcvr *rcvr;
Corey Minyardc69c3122006-09-30 23:27:56 -07001275 struct cmd_rcvr *rcvrs = NULL;
1276 int i, rv = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Corey Minyardd6dfd132006-03-31 02:30:41 -08001278 mutex_lock(&intf->cmd_rcvrs_mutex);
Corey Minyardc69c3122006-09-30 23:27:56 -07001279 for (i = 0; i < IPMI_NUM_CHANNELS; i++) {
1280 if (((1 << i) & chans) == 0)
1281 continue;
1282 rcvr = find_cmd_rcvr(intf, netfn, cmd, i);
1283 if (rcvr == NULL)
1284 continue;
1285 if (rcvr->user == user) {
1286 rv = 0;
1287 rcvr->chans &= ~chans;
1288 if (rcvr->chans == 0) {
1289 list_del_rcu(&rcvr->link);
1290 rcvr->next = rcvrs;
1291 rcvrs = rcvr;
1292 }
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Corey Minyardc69c3122006-09-30 23:27:56 -07001295 mutex_unlock(&intf->cmd_rcvrs_mutex);
1296 synchronize_rcu();
1297 while (rcvrs) {
1298 rcvr = rcvrs;
1299 rcvrs = rcvr->next;
1300 kfree(rcvr);
1301 }
1302 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
Corey Minyardc70d7492008-04-29 01:01:09 -07001304EXPORT_SYMBOL(ipmi_unregister_for_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static unsigned char
1307ipmb_checksum(unsigned char *data, int size)
1308{
1309 unsigned char csum = 0;
Corey Minyardc70d7492008-04-29 01:01:09 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 for (; size > 0; size--, data++)
1312 csum += *data;
1313
1314 return -csum;
1315}
1316
1317static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg,
1318 struct kernel_ipmi_msg *msg,
1319 struct ipmi_ipmb_addr *ipmb_addr,
1320 long msgid,
1321 unsigned char ipmb_seq,
1322 int broadcast,
1323 unsigned char source_address,
1324 unsigned char source_lun)
1325{
1326 int i = broadcast;
1327
1328 /* Format the IPMB header data. */
1329 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1330 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1331 smi_msg->data[2] = ipmb_addr->channel;
1332 if (broadcast)
1333 smi_msg->data[3] = 0;
1334 smi_msg->data[i+3] = ipmb_addr->slave_addr;
1335 smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3);
1336 smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2);
1337 smi_msg->data[i+6] = source_address;
1338 smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun;
1339 smi_msg->data[i+8] = msg->cmd;
1340
1341 /* Now tack on the data to the message. */
1342 if (msg->data_len > 0)
1343 memcpy(&(smi_msg->data[i+9]), msg->data,
1344 msg->data_len);
1345 smi_msg->data_size = msg->data_len + 9;
1346
1347 /* Now calculate the checksum and tack it on. */
1348 smi_msg->data[i+smi_msg->data_size]
1349 = ipmb_checksum(&(smi_msg->data[i+6]),
1350 smi_msg->data_size-6);
1351
Corey Minyardc70d7492008-04-29 01:01:09 -07001352 /*
1353 * Add on the checksum size and the offset from the
1354 * broadcast.
1355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 smi_msg->data_size += 1 + i;
1357
1358 smi_msg->msgid = msgid;
1359}
1360
1361static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg,
1362 struct kernel_ipmi_msg *msg,
1363 struct ipmi_lan_addr *lan_addr,
1364 long msgid,
1365 unsigned char ipmb_seq,
1366 unsigned char source_lun)
1367{
1368 /* Format the IPMB header data. */
1369 smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
1370 smi_msg->data[1] = IPMI_SEND_MSG_CMD;
1371 smi_msg->data[2] = lan_addr->channel;
1372 smi_msg->data[3] = lan_addr->session_handle;
1373 smi_msg->data[4] = lan_addr->remote_SWID;
1374 smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3);
1375 smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2);
1376 smi_msg->data[7] = lan_addr->local_SWID;
1377 smi_msg->data[8] = (ipmb_seq << 2) | source_lun;
1378 smi_msg->data[9] = msg->cmd;
1379
1380 /* Now tack on the data to the message. */
1381 if (msg->data_len > 0)
1382 memcpy(&(smi_msg->data[10]), msg->data,
1383 msg->data_len);
1384 smi_msg->data_size = msg->data_len + 10;
1385
1386 /* Now calculate the checksum and tack it on. */
1387 smi_msg->data[smi_msg->data_size]
1388 = ipmb_checksum(&(smi_msg->data[7]),
1389 smi_msg->data_size-7);
1390
Corey Minyardc70d7492008-04-29 01:01:09 -07001391 /*
1392 * Add on the checksum size and the offset from the
1393 * broadcast.
1394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 smi_msg->data_size += 1;
1396
1397 smi_msg->msgid = msgid;
1398}
1399
Corey Minyardc70d7492008-04-29 01:01:09 -07001400/*
1401 * Separate from ipmi_request so that the user does not have to be
1402 * supplied in certain circumstances (mainly at panic time). If
1403 * messages are supplied, they will be freed, even if an error
1404 * occurs.
1405 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08001406static int i_ipmi_request(ipmi_user_t user,
1407 ipmi_smi_t intf,
1408 struct ipmi_addr *addr,
1409 long msgid,
1410 struct kernel_ipmi_msg *msg,
1411 void *user_msg_data,
1412 void *supplied_smi,
1413 struct ipmi_recv_msg *supplied_recv,
1414 int priority,
1415 unsigned char source_address,
1416 unsigned char source_lun,
1417 int retries,
1418 unsigned int retry_time_ms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Corey Minyardb2c03942006-12-06 20:41:00 -08001420 int rv = 0;
1421 struct ipmi_smi_msg *smi_msg;
1422 struct ipmi_recv_msg *recv_msg;
1423 unsigned long flags;
1424 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426
Corey Minyardc70d7492008-04-29 01:01:09 -07001427 if (supplied_recv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 recv_msg = supplied_recv;
Corey Minyardc70d7492008-04-29 01:01:09 -07001429 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 recv_msg = ipmi_alloc_recv_msg();
Corey Minyardc70d7492008-04-29 01:01:09 -07001431 if (recv_msg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 recv_msg->user_msg_data = user_msg_data;
1435
Corey Minyardc70d7492008-04-29 01:01:09 -07001436 if (supplied_smi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 smi_msg = (struct ipmi_smi_msg *) supplied_smi;
Corey Minyardc70d7492008-04-29 01:01:09 -07001438 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 smi_msg = ipmi_alloc_smi_msg();
1440 if (smi_msg == NULL) {
1441 ipmi_free_recv_msg(recv_msg);
1442 return -ENOMEM;
1443 }
1444 }
1445
Corey Minyardb2c03942006-12-06 20:41:00 -08001446 rcu_read_lock();
1447 handlers = intf->handlers;
1448 if (!handlers) {
1449 rv = -ENODEV;
1450 goto out_err;
1451 }
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08001454 if (user)
1455 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 recv_msg->msgid = msgid;
Corey Minyardc70d7492008-04-29 01:01:09 -07001457 /*
1458 * Store the message to send in the receive message so timeout
1459 * responses can get the proper response data.
1460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 recv_msg->msg = *msg;
1462
1463 if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
1464 struct ipmi_system_interface_addr *smi_addr;
1465
1466 if (msg->netfn & 1) {
1467 /* Responses are not allowed to the SMI. */
1468 rv = -EINVAL;
1469 goto out_err;
1470 }
1471
1472 smi_addr = (struct ipmi_system_interface_addr *) addr;
1473 if (smi_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001474 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 rv = -EINVAL;
1476 goto out_err;
1477 }
1478
1479 memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr));
1480
1481 if ((msg->netfn == IPMI_NETFN_APP_REQUEST)
1482 && ((msg->cmd == IPMI_SEND_MSG_CMD)
1483 || (msg->cmd == IPMI_GET_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07001484 || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) {
1485 /*
1486 * We don't let the user do these, since we manage
1487 * the sequence numbers.
1488 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001489 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 rv = -EINVAL;
1491 goto out_err;
1492 }
1493
Corey Minyardb9675132006-12-06 20:41:02 -08001494 if (((msg->netfn == IPMI_NETFN_APP_REQUEST)
1495 && ((msg->cmd == IPMI_COLD_RESET_CMD)
1496 || (msg->cmd == IPMI_WARM_RESET_CMD)))
Corey Minyardc70d7492008-04-29 01:01:09 -07001497 || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) {
Corey Minyardb9675132006-12-06 20:41:02 -08001498 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
1499 intf->auto_maintenance_timeout
1500 = IPMI_MAINTENANCE_MODE_TIMEOUT;
1501 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07001502 && !intf->maintenance_mode_enable) {
Corey Minyardb9675132006-12-06 20:41:02 -08001503 intf->maintenance_mode_enable = 1;
1504 maintenance_mode_update(intf);
1505 }
1506 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
1507 flags);
1508 }
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001511 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 rv = -EMSGSIZE;
1513 goto out_err;
1514 }
1515
1516 smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3);
1517 smi_msg->data[1] = msg->cmd;
1518 smi_msg->msgid = msgid;
1519 smi_msg->user_data = recv_msg;
1520 if (msg->data_len > 0)
1521 memcpy(&(smi_msg->data[2]), msg->data, msg->data_len);
1522 smi_msg->data_size = msg->data_len + 2;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001523 ipmi_inc_stat(intf, sent_local_commands);
Corey Minyard25176ed2009-04-21 12:24:04 -07001524 } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct ipmi_ipmb_addr *ipmb_addr;
1526 unsigned char ipmb_seq;
1527 long seqid;
1528 int broadcast = 0;
1529
KAMBAROV, ZAUR9c101fd2005-06-28 20:45:08 -07001530 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001531 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 rv = -EINVAL;
1533 goto out_err;
1534 }
1535
1536 if (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001537 != IPMI_CHANNEL_MEDIUM_IPMB) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001538 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 rv = -EINVAL;
1540 goto out_err;
1541 }
1542
1543 if (retries < 0) {
1544 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)
1545 retries = 0; /* Don't retry broadcasts. */
1546 else
1547 retries = 4;
1548 }
1549 if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001550 /*
1551 * Broadcasts add a zero at the beginning of the
1552 * message, but otherwise is the same as an IPMB
1553 * address.
1554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 addr->addr_type = IPMI_IPMB_ADDR_TYPE;
1556 broadcast = 1;
1557 }
1558
1559
1560 /* Default to 1 second retries. */
1561 if (retry_time_ms == 0)
1562 retry_time_ms = 1000;
1563
Corey Minyardc70d7492008-04-29 01:01:09 -07001564 /*
1565 * 9 for the header and 1 for the checksum, plus
1566 * possibly one for the broadcast.
1567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001569 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 rv = -EMSGSIZE;
1571 goto out_err;
1572 }
1573
1574 ipmb_addr = (struct ipmi_ipmb_addr *) addr;
1575 if (ipmb_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001576 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 rv = -EINVAL;
1578 goto out_err;
1579 }
1580
1581 memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr));
1582
1583 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001584 /*
1585 * It's a response, so use the user's sequence
1586 * from msgid.
1587 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001588 ipmi_inc_stat(intf, sent_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid,
1590 msgid, broadcast,
1591 source_address, source_lun);
1592
Corey Minyardc70d7492008-04-29 01:01:09 -07001593 /*
1594 * Save the receive message so we can use it
1595 * to deliver the response.
1596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 smi_msg->user_data = recv_msg;
1598 } else {
1599 /* It's a command, so get a sequence for it. */
1600
1601 spin_lock_irqsave(&(intf->seq_lock), flags);
1602
Corey Minyardc70d7492008-04-29 01:01:09 -07001603 /*
1604 * Create a sequence number with a 1 second
1605 * timeout and 4 retries.
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 rv = intf_next_seq(intf,
1608 recv_msg,
1609 retry_time_ms,
1610 retries,
1611 broadcast,
1612 &ipmb_seq,
1613 &seqid);
1614 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001615 /*
1616 * We have used up all the sequence numbers,
1617 * probably, so abort.
1618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 spin_unlock_irqrestore(&(intf->seq_lock),
1620 flags);
1621 goto out_err;
1622 }
1623
Corey Minyard25176ed2009-04-21 12:24:04 -07001624 ipmi_inc_stat(intf, sent_ipmb_commands);
1625
Corey Minyardc70d7492008-04-29 01:01:09 -07001626 /*
1627 * Store the sequence number in the message,
1628 * so that when the send message response
1629 * comes back we can start the timer.
1630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 format_ipmb_msg(smi_msg, msg, ipmb_addr,
1632 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1633 ipmb_seq, broadcast,
1634 source_address, source_lun);
1635
Corey Minyardc70d7492008-04-29 01:01:09 -07001636 /*
1637 * Copy the message into the recv message data, so we
1638 * can retransmit it later if necessary.
1639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 memcpy(recv_msg->msg_data, smi_msg->data,
1641 smi_msg->data_size);
1642 recv_msg->msg.data = recv_msg->msg_data;
1643 recv_msg->msg.data_len = smi_msg->data_size;
1644
Corey Minyardc70d7492008-04-29 01:01:09 -07001645 /*
1646 * We don't unlock until here, because we need
1647 * to copy the completed message into the
1648 * recv_msg before we release the lock.
1649 * Otherwise, race conditions may bite us. I
1650 * know that's pretty paranoid, but I prefer
1651 * to be correct.
1652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1654 }
Corey Minyard25176ed2009-04-21 12:24:04 -07001655 } else if (is_lan_addr(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct ipmi_lan_addr *lan_addr;
1657 unsigned char ipmb_seq;
1658 long seqid;
1659
Jayachandran C12fc1d72006-02-03 03:04:51 -08001660 if (addr->channel >= IPMI_MAX_CHANNELS) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001661 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 rv = -EINVAL;
1663 goto out_err;
1664 }
1665
1666 if ((intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001667 != IPMI_CHANNEL_MEDIUM_8023LAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 && (intf->channels[addr->channel].medium
Corey Minyardc70d7492008-04-29 01:01:09 -07001669 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001670 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rv = -EINVAL;
1672 goto out_err;
1673 }
1674
1675 retries = 4;
1676
1677 /* Default to 1 second retries. */
1678 if (retry_time_ms == 0)
1679 retry_time_ms = 1000;
1680
1681 /* 11 for the header and 1 for the checksum. */
1682 if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001683 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 rv = -EMSGSIZE;
1685 goto out_err;
1686 }
1687
1688 lan_addr = (struct ipmi_lan_addr *) addr;
1689 if (lan_addr->lun > 3) {
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001690 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 rv = -EINVAL;
1692 goto out_err;
1693 }
1694
1695 memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr));
1696
1697 if (recv_msg->msg.netfn & 0x1) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001698 /*
1699 * It's a response, so use the user's sequence
1700 * from msgid.
1701 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001702 ipmi_inc_stat(intf, sent_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 format_lan_msg(smi_msg, msg, lan_addr, msgid,
1704 msgid, source_lun);
1705
Corey Minyardc70d7492008-04-29 01:01:09 -07001706 /*
1707 * Save the receive message so we can use it
1708 * to deliver the response.
1709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 smi_msg->user_data = recv_msg;
1711 } else {
1712 /* It's a command, so get a sequence for it. */
1713
1714 spin_lock_irqsave(&(intf->seq_lock), flags);
1715
Corey Minyardc70d7492008-04-29 01:01:09 -07001716 /*
1717 * Create a sequence number with a 1 second
1718 * timeout and 4 retries.
1719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rv = intf_next_seq(intf,
1721 recv_msg,
1722 retry_time_ms,
1723 retries,
1724 0,
1725 &ipmb_seq,
1726 &seqid);
1727 if (rv) {
Corey Minyardc70d7492008-04-29 01:01:09 -07001728 /*
1729 * We have used up all the sequence numbers,
1730 * probably, so abort.
1731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 spin_unlock_irqrestore(&(intf->seq_lock),
1733 flags);
1734 goto out_err;
1735 }
1736
Corey Minyard25176ed2009-04-21 12:24:04 -07001737 ipmi_inc_stat(intf, sent_lan_commands);
1738
Corey Minyardc70d7492008-04-29 01:01:09 -07001739 /*
1740 * Store the sequence number in the message,
1741 * so that when the send message response
1742 * comes back we can start the timer.
1743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 format_lan_msg(smi_msg, msg, lan_addr,
1745 STORE_SEQ_IN_MSGID(ipmb_seq, seqid),
1746 ipmb_seq, source_lun);
1747
Corey Minyardc70d7492008-04-29 01:01:09 -07001748 /*
1749 * Copy the message into the recv message data, so we
1750 * can retransmit it later if necessary.
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 memcpy(recv_msg->msg_data, smi_msg->data,
1753 smi_msg->data_size);
1754 recv_msg->msg.data = recv_msg->msg_data;
1755 recv_msg->msg.data_len = smi_msg->data_size;
1756
Corey Minyardc70d7492008-04-29 01:01:09 -07001757 /*
1758 * We don't unlock until here, because we need
1759 * to copy the completed message into the
1760 * recv_msg before we release the lock.
1761 * Otherwise, race conditions may bite us. I
1762 * know that's pretty paranoid, but I prefer
1763 * to be correct.
1764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 spin_unlock_irqrestore(&(intf->seq_lock), flags);
1766 }
1767 } else {
1768 /* Unknown address type. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001769 ipmi_inc_stat(intf, sent_invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 rv = -EINVAL;
1771 goto out_err;
1772 }
1773
1774#ifdef DEBUG_MSGING
1775 {
1776 int m;
Corey Minyarde8b33612005-09-06 15:18:45 -07001777 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 printk(" %2.2x", smi_msg->data[m]);
1779 printk("\n");
1780 }
1781#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08001782
1783 handlers->sender(intf->send_info, smi_msg, priority);
1784 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 return 0;
1787
1788 out_err:
Corey Minyardb2c03942006-12-06 20:41:00 -08001789 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ipmi_free_smi_msg(smi_msg);
1791 ipmi_free_recv_msg(recv_msg);
1792 return rv;
1793}
1794
Corey Minyardc14979b2005-09-06 15:18:38 -07001795static int check_addr(ipmi_smi_t intf,
1796 struct ipmi_addr *addr,
1797 unsigned char *saddr,
1798 unsigned char *lun)
1799{
1800 if (addr->channel >= IPMI_MAX_CHANNELS)
1801 return -EINVAL;
1802 *lun = intf->channels[addr->channel].lun;
1803 *saddr = intf->channels[addr->channel].address;
1804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807int ipmi_request_settime(ipmi_user_t user,
1808 struct ipmi_addr *addr,
1809 long msgid,
1810 struct kernel_ipmi_msg *msg,
1811 void *user_msg_data,
1812 int priority,
1813 int retries,
1814 unsigned int retry_time_ms)
1815{
Corey Minyardc14979b2005-09-06 15:18:38 -07001816 unsigned char saddr, lun;
1817 int rv;
1818
Corey Minyard8a3628d2006-03-31 02:30:40 -08001819 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001820 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001821 rv = check_addr(user->intf, addr, &saddr, &lun);
1822 if (rv)
1823 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return i_ipmi_request(user,
1825 user->intf,
1826 addr,
1827 msgid,
1828 msg,
1829 user_msg_data,
1830 NULL, NULL,
1831 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001832 saddr,
1833 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 retries,
1835 retry_time_ms);
1836}
Corey Minyardc70d7492008-04-29 01:01:09 -07001837EXPORT_SYMBOL(ipmi_request_settime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839int ipmi_request_supply_msgs(ipmi_user_t user,
1840 struct ipmi_addr *addr,
1841 long msgid,
1842 struct kernel_ipmi_msg *msg,
1843 void *user_msg_data,
1844 void *supplied_smi,
1845 struct ipmi_recv_msg *supplied_recv,
1846 int priority)
1847{
Corey Minyardc14979b2005-09-06 15:18:38 -07001848 unsigned char saddr, lun;
1849 int rv;
1850
Corey Minyard8a3628d2006-03-31 02:30:40 -08001851 if (!user)
Corey Minyard56a55ec2005-09-06 15:18:42 -07001852 return -EINVAL;
Corey Minyardc14979b2005-09-06 15:18:38 -07001853 rv = check_addr(user->intf, addr, &saddr, &lun);
1854 if (rv)
1855 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return i_ipmi_request(user,
1857 user->intf,
1858 addr,
1859 msgid,
1860 msg,
1861 user_msg_data,
1862 supplied_smi,
1863 supplied_recv,
1864 priority,
Corey Minyardc14979b2005-09-06 15:18:38 -07001865 saddr,
1866 lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 -1, 0);
1868}
Corey Minyardc70d7492008-04-29 01:01:09 -07001869EXPORT_SYMBOL(ipmi_request_supply_msgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001871#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872static int ipmb_file_read_proc(char *page, char **start, off_t off,
1873 int count, int *eof, void *data)
1874{
1875 char *out = (char *) page;
1876 ipmi_smi_t intf = data;
Corey Minyardc14979b2005-09-06 15:18:38 -07001877 int i;
Corey Minyard8a3628d2006-03-31 02:30:40 -08001878 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Corey Minyarde8b33612005-09-06 15:18:45 -07001880 for (i = 0; i < IPMI_MAX_CHANNELS; i++)
Corey Minyardc14979b2005-09-06 15:18:38 -07001881 rv += sprintf(out+rv, "%x ", intf->channels[i].address);
1882 out[rv-1] = '\n'; /* Replace the final space with a newline */
1883 out[rv] = '\0';
1884 rv++;
1885 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888static int version_file_read_proc(char *page, char **start, off_t off,
1889 int count, int *eof, void *data)
1890{
1891 char *out = (char *) page;
1892 ipmi_smi_t intf = data;
1893
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001894 return sprintf(out, "%u.%u\n",
Corey Minyard50c812b2006-03-26 01:37:21 -08001895 ipmi_version_major(&intf->bmc->id),
1896 ipmi_version_minor(&intf->bmc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899static int stat_file_read_proc(char *page, char **start, off_t off,
1900 int count, int *eof, void *data)
1901{
1902 char *out = (char *) page;
1903 ipmi_smi_t intf = data;
1904
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07001905 out += sprintf(out, "sent_invalid_commands: %u\n",
1906 ipmi_get_stat(intf, sent_invalid_commands));
1907 out += sprintf(out, "sent_local_commands: %u\n",
1908 ipmi_get_stat(intf, sent_local_commands));
1909 out += sprintf(out, "handled_local_responses: %u\n",
1910 ipmi_get_stat(intf, handled_local_responses));
1911 out += sprintf(out, "unhandled_local_responses: %u\n",
1912 ipmi_get_stat(intf, unhandled_local_responses));
1913 out += sprintf(out, "sent_ipmb_commands: %u\n",
1914 ipmi_get_stat(intf, sent_ipmb_commands));
1915 out += sprintf(out, "sent_ipmb_command_errs: %u\n",
1916 ipmi_get_stat(intf, sent_ipmb_command_errs));
1917 out += sprintf(out, "retransmitted_ipmb_commands: %u\n",
1918 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1919 out += sprintf(out, "timed_out_ipmb_commands: %u\n",
1920 ipmi_get_stat(intf, timed_out_ipmb_commands));
1921 out += sprintf(out, "timed_out_ipmb_broadcasts: %u\n",
1922 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1923 out += sprintf(out, "sent_ipmb_responses: %u\n",
1924 ipmi_get_stat(intf, sent_ipmb_responses));
1925 out += sprintf(out, "handled_ipmb_responses: %u\n",
1926 ipmi_get_stat(intf, handled_ipmb_responses));
1927 out += sprintf(out, "invalid_ipmb_responses: %u\n",
1928 ipmi_get_stat(intf, invalid_ipmb_responses));
1929 out += sprintf(out, "unhandled_ipmb_responses: %u\n",
1930 ipmi_get_stat(intf, unhandled_ipmb_responses));
1931 out += sprintf(out, "sent_lan_commands: %u\n",
1932 ipmi_get_stat(intf, sent_lan_commands));
1933 out += sprintf(out, "sent_lan_command_errs: %u\n",
1934 ipmi_get_stat(intf, sent_lan_command_errs));
1935 out += sprintf(out, "retransmitted_lan_commands: %u\n",
1936 ipmi_get_stat(intf, retransmitted_lan_commands));
1937 out += sprintf(out, "timed_out_lan_commands: %u\n",
1938 ipmi_get_stat(intf, timed_out_lan_commands));
1939 out += sprintf(out, "sent_lan_responses: %u\n",
1940 ipmi_get_stat(intf, sent_lan_responses));
1941 out += sprintf(out, "handled_lan_responses: %u\n",
1942 ipmi_get_stat(intf, handled_lan_responses));
1943 out += sprintf(out, "invalid_lan_responses: %u\n",
1944 ipmi_get_stat(intf, invalid_lan_responses));
1945 out += sprintf(out, "unhandled_lan_responses: %u\n",
1946 ipmi_get_stat(intf, unhandled_lan_responses));
1947 out += sprintf(out, "handled_commands: %u\n",
1948 ipmi_get_stat(intf, handled_commands));
1949 out += sprintf(out, "invalid_commands: %u\n",
1950 ipmi_get_stat(intf, invalid_commands));
1951 out += sprintf(out, "unhandled_commands: %u\n",
1952 ipmi_get_stat(intf, unhandled_commands));
1953 out += sprintf(out, "invalid_events: %u\n",
1954 ipmi_get_stat(intf, invalid_events));
1955 out += sprintf(out, "events: %u\n",
1956 ipmi_get_stat(intf, events));
Corey Minyard25176ed2009-04-21 12:24:04 -07001957 out += sprintf(out, "failed rexmit LAN msgs: %u\n",
1958 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
1959 out += sprintf(out, "failed rexmit IPMB msgs: %u\n",
1960 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 return (out - ((char *) page));
1963}
Randy Dunlap1aa16ee2006-12-06 20:41:20 -08001964#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07001967 read_proc_t *read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03001968 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 int rv = 0;
Corey Minyard3b625942005-06-23 22:01:42 -07001971#ifdef CONFIG_PROC_FS
1972 struct proc_dir_entry *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct ipmi_proc_entry *entry;
1974
1975 /* Create a list element. */
1976 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1977 if (!entry)
1978 return -ENOMEM;
1979 entry->name = kmalloc(strlen(name)+1, GFP_KERNEL);
1980 if (!entry->name) {
1981 kfree(entry);
1982 return -ENOMEM;
1983 }
1984 strcpy(entry->name, name);
1985
1986 file = create_proc_entry(name, 0, smi->proc_dir);
1987 if (!file) {
1988 kfree(entry->name);
1989 kfree(entry);
1990 rv = -ENOMEM;
1991 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 file->data = data;
1993 file->read_proc = read_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Corey Minyardac019152007-10-18 03:07:11 -07001995 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* Stick it on the list. */
1997 entry->next = smi->proc_entries;
1998 smi->proc_entries = entry;
Corey Minyardac019152007-10-18 03:07:11 -07001999 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Corey Minyard3b625942005-06-23 22:01:42 -07002001#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 return rv;
2004}
Corey Minyardc70d7492008-04-29 01:01:09 -07002005EXPORT_SYMBOL(ipmi_smi_add_proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007static int add_proc_entries(ipmi_smi_t smi, int num)
2008{
2009 int rv = 0;
2010
Corey Minyard3b625942005-06-23 22:01:42 -07002011#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 sprintf(smi->proc_dir_name, "%d", num);
2013 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2014 if (!smi->proc_dir)
2015 rv = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 if (rv == 0)
2018 rv = ipmi_smi_add_proc_entry(smi, "stats",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002019 stat_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002020 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 if (rv == 0)
2023 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002024 ipmb_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002025 smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if (rv == 0)
2028 rv = ipmi_smi_add_proc_entry(smi, "version",
Alexey Dobriyanfa68be02008-04-29 01:01:13 -07002029 version_file_read_proc,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03002030 smi);
Corey Minyard3b625942005-06-23 22:01:42 -07002031#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 return rv;
2034}
2035
2036static void remove_proc_entries(ipmi_smi_t smi)
2037{
Corey Minyard3b625942005-06-23 22:01:42 -07002038#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct ipmi_proc_entry *entry;
2040
Corey Minyardac019152007-10-18 03:07:11 -07002041 mutex_lock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 while (smi->proc_entries) {
2043 entry = smi->proc_entries;
2044 smi->proc_entries = entry->next;
2045
2046 remove_proc_entry(entry->name, smi->proc_dir);
2047 kfree(entry->name);
2048 kfree(entry);
2049 }
Corey Minyardac019152007-10-18 03:07:11 -07002050 mutex_unlock(&smi->proc_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
Corey Minyard3b625942005-06-23 22:01:42 -07002052#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Corey Minyard50c812b2006-03-26 01:37:21 -08002055static int __find_bmc_guid(struct device *dev, void *data)
2056{
2057 unsigned char *id = data;
2058 struct bmc_device *bmc = dev_get_drvdata(dev);
2059 return memcmp(bmc->guid, id, 16) == 0;
2060}
2061
2062static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2063 unsigned char *guid)
2064{
2065 struct device *dev;
2066
2067 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2068 if (dev)
2069 return dev_get_drvdata(dev);
2070 else
2071 return NULL;
2072}
2073
2074struct prod_dev_id {
2075 unsigned int product_id;
2076 unsigned char device_id;
2077};
2078
2079static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2080{
2081 struct prod_dev_id *id = data;
2082 struct bmc_device *bmc = dev_get_drvdata(dev);
2083
2084 return (bmc->id.product_id == id->product_id
Corey Minyard50c812b2006-03-26 01:37:21 -08002085 && bmc->id.device_id == id->device_id);
2086}
2087
2088static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2089 struct device_driver *drv,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002090 unsigned int product_id, unsigned char device_id)
Corey Minyard50c812b2006-03-26 01:37:21 -08002091{
2092 struct prod_dev_id id = {
2093 .product_id = product_id,
2094 .device_id = device_id,
2095 };
2096 struct device *dev;
2097
2098 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2099 if (dev)
2100 return dev_get_drvdata(dev);
2101 else
2102 return NULL;
2103}
2104
2105static ssize_t device_id_show(struct device *dev,
2106 struct device_attribute *attr,
2107 char *buf)
2108{
2109 struct bmc_device *bmc = dev_get_drvdata(dev);
2110
2111 return snprintf(buf, 10, "%u\n", bmc->id.device_id);
2112}
2113
2114static ssize_t provides_dev_sdrs_show(struct device *dev,
2115 struct device_attribute *attr,
2116 char *buf)
2117{
2118 struct bmc_device *bmc = dev_get_drvdata(dev);
2119
2120 return snprintf(buf, 10, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002121 (bmc->id.device_revision & 0x80) >> 7);
Corey Minyard50c812b2006-03-26 01:37:21 -08002122}
2123
2124static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2125 char *buf)
2126{
2127 struct bmc_device *bmc = dev_get_drvdata(dev);
2128
2129 return snprintf(buf, 20, "%u\n",
Corey Minyard7947d2c2006-11-10 12:27:50 -08002130 bmc->id.device_revision & 0x0F);
Corey Minyard50c812b2006-03-26 01:37:21 -08002131}
2132
2133static ssize_t firmware_rev_show(struct device *dev,
2134 struct device_attribute *attr,
2135 char *buf)
2136{
2137 struct bmc_device *bmc = dev_get_drvdata(dev);
2138
2139 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1,
2140 bmc->id.firmware_revision_2);
2141}
2142
2143static ssize_t ipmi_version_show(struct device *dev,
2144 struct device_attribute *attr,
2145 char *buf)
2146{
2147 struct bmc_device *bmc = dev_get_drvdata(dev);
2148
2149 return snprintf(buf, 20, "%u.%u\n",
2150 ipmi_version_major(&bmc->id),
2151 ipmi_version_minor(&bmc->id));
2152}
2153
2154static ssize_t add_dev_support_show(struct device *dev,
2155 struct device_attribute *attr,
2156 char *buf)
2157{
2158 struct bmc_device *bmc = dev_get_drvdata(dev);
2159
2160 return snprintf(buf, 10, "0x%02x\n",
2161 bmc->id.additional_device_support);
2162}
2163
2164static ssize_t manufacturer_id_show(struct device *dev,
2165 struct device_attribute *attr,
2166 char *buf)
2167{
2168 struct bmc_device *bmc = dev_get_drvdata(dev);
2169
2170 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id);
2171}
2172
2173static ssize_t product_id_show(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2176{
2177 struct bmc_device *bmc = dev_get_drvdata(dev);
2178
2179 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id);
2180}
2181
2182static ssize_t aux_firmware_rev_show(struct device *dev,
2183 struct device_attribute *attr,
2184 char *buf)
2185{
2186 struct bmc_device *bmc = dev_get_drvdata(dev);
2187
2188 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2189 bmc->id.aux_firmware_revision[3],
2190 bmc->id.aux_firmware_revision[2],
2191 bmc->id.aux_firmware_revision[1],
2192 bmc->id.aux_firmware_revision[0]);
2193}
2194
2195static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2196 char *buf)
2197{
2198 struct bmc_device *bmc = dev_get_drvdata(dev);
2199
2200 return snprintf(buf, 100, "%Lx%Lx\n",
2201 (long long) bmc->guid[0],
2202 (long long) bmc->guid[8]);
2203}
2204
Jeff Garzik5e593932006-10-11 01:22:21 -07002205static void remove_files(struct bmc_device *bmc)
Corey Minyard50c812b2006-03-26 01:37:21 -08002206{
Corey Minyardf0b55da2006-12-06 20:40:54 -08002207 if (!bmc->dev)
2208 return;
2209
Corey Minyard50c812b2006-03-26 01:37:21 -08002210 device_remove_file(&bmc->dev->dev,
2211 &bmc->device_id_attr);
2212 device_remove_file(&bmc->dev->dev,
2213 &bmc->provides_dev_sdrs_attr);
2214 device_remove_file(&bmc->dev->dev,
2215 &bmc->revision_attr);
2216 device_remove_file(&bmc->dev->dev,
2217 &bmc->firmware_rev_attr);
2218 device_remove_file(&bmc->dev->dev,
2219 &bmc->version_attr);
2220 device_remove_file(&bmc->dev->dev,
2221 &bmc->add_dev_support_attr);
2222 device_remove_file(&bmc->dev->dev,
2223 &bmc->manufacturer_id_attr);
2224 device_remove_file(&bmc->dev->dev,
2225 &bmc->product_id_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002226
Corey Minyard50c812b2006-03-26 01:37:21 -08002227 if (bmc->id.aux_firmware_revision_set)
2228 device_remove_file(&bmc->dev->dev,
2229 &bmc->aux_firmware_rev_attr);
2230 if (bmc->guid_set)
2231 device_remove_file(&bmc->dev->dev,
2232 &bmc->guid_attr);
Jeff Garzik5e593932006-10-11 01:22:21 -07002233}
2234
2235static void
2236cleanup_bmc_device(struct kref *ref)
2237{
2238 struct bmc_device *bmc;
2239
2240 bmc = container_of(ref, struct bmc_device, refcount);
2241
2242 remove_files(bmc);
Corey Minyard1d5636c2006-12-10 02:19:08 -08002243 platform_device_unregister(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002244 kfree(bmc);
2245}
2246
2247static void ipmi_bmc_unregister(ipmi_smi_t intf)
2248{
2249 struct bmc_device *bmc = intf->bmc;
2250
Corey Minyard759643b2006-12-06 20:40:59 -08002251 if (intf->sysfs_name) {
2252 sysfs_remove_link(&intf->si_dev->kobj, intf->sysfs_name);
2253 kfree(intf->sysfs_name);
2254 intf->sysfs_name = NULL;
2255 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002256 if (intf->my_dev_name) {
2257 sysfs_remove_link(&bmc->dev->dev.kobj, intf->my_dev_name);
2258 kfree(intf->my_dev_name);
2259 intf->my_dev_name = NULL;
2260 }
2261
2262 mutex_lock(&ipmidriver_mutex);
2263 kref_put(&bmc->refcount, cleanup_bmc_device);
Corey Minyardf0b55da2006-12-06 20:40:54 -08002264 intf->bmc = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002265 mutex_unlock(&ipmidriver_mutex);
2266}
2267
Jeff Garzik5e593932006-10-11 01:22:21 -07002268static int create_files(struct bmc_device *bmc)
2269{
2270 int err;
2271
Corey Minyardf0b55da2006-12-06 20:40:54 -08002272 bmc->device_id_attr.attr.name = "device_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002273 bmc->device_id_attr.attr.mode = S_IRUGO;
2274 bmc->device_id_attr.show = device_id_show;
2275
2276 bmc->provides_dev_sdrs_attr.attr.name = "provides_device_sdrs";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002277 bmc->provides_dev_sdrs_attr.attr.mode = S_IRUGO;
2278 bmc->provides_dev_sdrs_attr.show = provides_dev_sdrs_show;
2279
2280 bmc->revision_attr.attr.name = "revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002281 bmc->revision_attr.attr.mode = S_IRUGO;
2282 bmc->revision_attr.show = revision_show;
2283
2284 bmc->firmware_rev_attr.attr.name = "firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002285 bmc->firmware_rev_attr.attr.mode = S_IRUGO;
2286 bmc->firmware_rev_attr.show = firmware_rev_show;
2287
2288 bmc->version_attr.attr.name = "ipmi_version";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002289 bmc->version_attr.attr.mode = S_IRUGO;
2290 bmc->version_attr.show = ipmi_version_show;
2291
2292 bmc->add_dev_support_attr.attr.name = "additional_device_support";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002293 bmc->add_dev_support_attr.attr.mode = S_IRUGO;
2294 bmc->add_dev_support_attr.show = add_dev_support_show;
2295
2296 bmc->manufacturer_id_attr.attr.name = "manufacturer_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002297 bmc->manufacturer_id_attr.attr.mode = S_IRUGO;
2298 bmc->manufacturer_id_attr.show = manufacturer_id_show;
2299
2300 bmc->product_id_attr.attr.name = "product_id";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002301 bmc->product_id_attr.attr.mode = S_IRUGO;
2302 bmc->product_id_attr.show = product_id_show;
2303
2304 bmc->guid_attr.attr.name = "guid";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002305 bmc->guid_attr.attr.mode = S_IRUGO;
2306 bmc->guid_attr.show = guid_show;
2307
2308 bmc->aux_firmware_rev_attr.attr.name = "aux_firmware_revision";
Corey Minyardf0b55da2006-12-06 20:40:54 -08002309 bmc->aux_firmware_rev_attr.attr.mode = S_IRUGO;
2310 bmc->aux_firmware_rev_attr.show = aux_firmware_rev_show;
2311
Jeff Garzik5e593932006-10-11 01:22:21 -07002312 err = device_create_file(&bmc->dev->dev,
2313 &bmc->device_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002314 if (err)
2315 goto out;
Jeff Garzik5e593932006-10-11 01:22:21 -07002316 err = device_create_file(&bmc->dev->dev,
2317 &bmc->provides_dev_sdrs_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002318 if (err)
2319 goto out_devid;
Jeff Garzik5e593932006-10-11 01:22:21 -07002320 err = device_create_file(&bmc->dev->dev,
2321 &bmc->revision_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002322 if (err)
2323 goto out_sdrs;
Jeff Garzik5e593932006-10-11 01:22:21 -07002324 err = device_create_file(&bmc->dev->dev,
2325 &bmc->firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002326 if (err)
2327 goto out_rev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002328 err = device_create_file(&bmc->dev->dev,
2329 &bmc->version_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002330 if (err)
2331 goto out_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002332 err = device_create_file(&bmc->dev->dev,
2333 &bmc->add_dev_support_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002334 if (err)
2335 goto out_version;
Jeff Garzik5e593932006-10-11 01:22:21 -07002336 err = device_create_file(&bmc->dev->dev,
2337 &bmc->manufacturer_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002338 if (err)
2339 goto out_add_dev;
Jeff Garzik5e593932006-10-11 01:22:21 -07002340 err = device_create_file(&bmc->dev->dev,
2341 &bmc->product_id_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002342 if (err)
2343 goto out_manu;
Jeff Garzik5e593932006-10-11 01:22:21 -07002344 if (bmc->id.aux_firmware_revision_set) {
2345 err = device_create_file(&bmc->dev->dev,
2346 &bmc->aux_firmware_rev_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002347 if (err)
2348 goto out_prod_id;
Jeff Garzik5e593932006-10-11 01:22:21 -07002349 }
2350 if (bmc->guid_set) {
2351 err = device_create_file(&bmc->dev->dev,
2352 &bmc->guid_attr);
Corey Minyardc70d7492008-04-29 01:01:09 -07002353 if (err)
2354 goto out_aux_firm;
Jeff Garzik5e593932006-10-11 01:22:21 -07002355 }
2356
2357 return 0;
2358
2359out_aux_firm:
2360 if (bmc->id.aux_firmware_revision_set)
2361 device_remove_file(&bmc->dev->dev,
2362 &bmc->aux_firmware_rev_attr);
2363out_prod_id:
2364 device_remove_file(&bmc->dev->dev,
2365 &bmc->product_id_attr);
2366out_manu:
2367 device_remove_file(&bmc->dev->dev,
2368 &bmc->manufacturer_id_attr);
2369out_add_dev:
2370 device_remove_file(&bmc->dev->dev,
2371 &bmc->add_dev_support_attr);
2372out_version:
2373 device_remove_file(&bmc->dev->dev,
2374 &bmc->version_attr);
2375out_firm:
2376 device_remove_file(&bmc->dev->dev,
2377 &bmc->firmware_rev_attr);
2378out_rev:
2379 device_remove_file(&bmc->dev->dev,
2380 &bmc->revision_attr);
2381out_sdrs:
2382 device_remove_file(&bmc->dev->dev,
2383 &bmc->provides_dev_sdrs_attr);
2384out_devid:
2385 device_remove_file(&bmc->dev->dev,
2386 &bmc->device_id_attr);
2387out:
2388 return err;
2389}
2390
Corey Minyard759643b2006-12-06 20:40:59 -08002391static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum,
2392 const char *sysfs_name)
Corey Minyard50c812b2006-03-26 01:37:21 -08002393{
2394 int rv;
2395 struct bmc_device *bmc = intf->bmc;
2396 struct bmc_device *old_bmc;
2397 int size;
2398 char dummy[1];
2399
2400 mutex_lock(&ipmidriver_mutex);
2401
2402 /*
2403 * Try to find if there is an bmc_device struct
2404 * representing the interfaced BMC already
2405 */
2406 if (bmc->guid_set)
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002407 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid);
Corey Minyard50c812b2006-03-26 01:37:21 -08002408 else
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002409 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyard50c812b2006-03-26 01:37:21 -08002410 bmc->id.product_id,
2411 bmc->id.device_id);
2412
2413 /*
2414 * If there is already an bmc_device, free the new one,
2415 * otherwise register the new BMC device
2416 */
2417 if (old_bmc) {
2418 kfree(bmc);
2419 intf->bmc = old_bmc;
2420 bmc = old_bmc;
2421
2422 kref_get(&bmc->refcount);
2423 mutex_unlock(&ipmidriver_mutex);
2424
2425 printk(KERN_INFO
2426 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2427 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2428 bmc->id.manufacturer_id,
2429 bmc->id.product_id,
2430 bmc->id.device_id);
2431 } else {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002432 char name[14];
2433 unsigned char orig_dev_id = bmc->id.device_id;
2434 int warn_printed = 0;
2435
2436 snprintf(name, sizeof(name),
2437 "ipmi_bmc.%4.4x", bmc->id.product_id);
2438
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002439 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
Corey Minyardf0b55da2006-12-06 20:40:54 -08002440 bmc->id.product_id,
Corey Minyard1d5636c2006-12-10 02:19:08 -08002441 bmc->id.device_id)) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002442 if (!warn_printed) {
2443 printk(KERN_WARNING PFX
2444 "This machine has two different BMCs"
2445 " with the same product id and device"
2446 " id. This is an error in the"
2447 " firmware, but incrementing the"
2448 " device id to work around the problem."
2449 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2450 bmc->id.product_id, bmc->id.device_id);
2451 warn_printed = 1;
2452 }
2453 bmc->id.device_id++; /* Wraps at 255 */
2454 if (bmc->id.device_id == orig_dev_id) {
2455 printk(KERN_ERR PFX
2456 "Out of device ids!\n");
2457 break;
2458 }
2459 }
2460
2461 bmc->dev = platform_device_alloc(name, bmc->id.device_id);
Corey Minyard8a3628d2006-03-31 02:30:40 -08002462 if (!bmc->dev) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002463 mutex_unlock(&ipmidriver_mutex);
Corey Minyard50c812b2006-03-26 01:37:21 -08002464 printk(KERN_ERR
2465 "ipmi_msghandler:"
2466 " Unable to allocate platform device\n");
2467 return -ENOMEM;
2468 }
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08002469 bmc->dev->dev.driver = &ipmidriver.driver;
Corey Minyard50c812b2006-03-26 01:37:21 -08002470 dev_set_drvdata(&bmc->dev->dev, bmc);
2471 kref_init(&bmc->refcount);
2472
Zhang, Yanminb48f5452006-11-16 01:19:08 -08002473 rv = platform_device_add(bmc->dev);
Corey Minyard50c812b2006-03-26 01:37:21 -08002474 mutex_unlock(&ipmidriver_mutex);
2475 if (rv) {
Corey Minyardf0b55da2006-12-06 20:40:54 -08002476 platform_device_put(bmc->dev);
2477 bmc->dev = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002478 printk(KERN_ERR
2479 "ipmi_msghandler:"
2480 " Unable to register bmc device: %d\n",
2481 rv);
Corey Minyardc70d7492008-04-29 01:01:09 -07002482 /*
2483 * Don't go to out_err, you can only do that if
2484 * the device is registered already.
2485 */
Corey Minyard50c812b2006-03-26 01:37:21 -08002486 return rv;
2487 }
2488
Jeff Garzik5e593932006-10-11 01:22:21 -07002489 rv = create_files(bmc);
2490 if (rv) {
2491 mutex_lock(&ipmidriver_mutex);
2492 platform_device_unregister(bmc->dev);
2493 mutex_unlock(&ipmidriver_mutex);
2494
2495 return rv;
2496 }
Corey Minyard50c812b2006-03-26 01:37:21 -08002497
2498 printk(KERN_INFO
2499 "ipmi: Found new BMC (man_id: 0x%6.6x, "
2500 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2501 bmc->id.manufacturer_id,
2502 bmc->id.product_id,
2503 bmc->id.device_id);
2504 }
2505
2506 /*
2507 * create symlink from system interface device to bmc device
2508 * and back.
2509 */
Corey Minyard759643b2006-12-06 20:40:59 -08002510 intf->sysfs_name = kstrdup(sysfs_name, GFP_KERNEL);
2511 if (!intf->sysfs_name) {
2512 rv = -ENOMEM;
2513 printk(KERN_ERR
2514 "ipmi_msghandler: allocate link to BMC: %d\n",
2515 rv);
2516 goto out_err;
2517 }
2518
Corey Minyard50c812b2006-03-26 01:37:21 -08002519 rv = sysfs_create_link(&intf->si_dev->kobj,
Corey Minyard759643b2006-12-06 20:40:59 -08002520 &bmc->dev->dev.kobj, intf->sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002521 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002522 kfree(intf->sysfs_name);
2523 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002524 printk(KERN_ERR
2525 "ipmi_msghandler: Unable to create bmc symlink: %d\n",
2526 rv);
2527 goto out_err;
2528 }
2529
Corey Minyard759643b2006-12-06 20:40:59 -08002530 size = snprintf(dummy, 0, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002531 intf->my_dev_name = kmalloc(size+1, GFP_KERNEL);
2532 if (!intf->my_dev_name) {
Corey Minyard759643b2006-12-06 20:40:59 -08002533 kfree(intf->sysfs_name);
2534 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002535 rv = -ENOMEM;
2536 printk(KERN_ERR
2537 "ipmi_msghandler: allocate link from BMC: %d\n",
2538 rv);
2539 goto out_err;
2540 }
Corey Minyard759643b2006-12-06 20:40:59 -08002541 snprintf(intf->my_dev_name, size+1, "ipmi%d", ifnum);
Corey Minyard50c812b2006-03-26 01:37:21 -08002542
2543 rv = sysfs_create_link(&bmc->dev->dev.kobj, &intf->si_dev->kobj,
2544 intf->my_dev_name);
2545 if (rv) {
Corey Minyard759643b2006-12-06 20:40:59 -08002546 kfree(intf->sysfs_name);
2547 intf->sysfs_name = NULL;
Corey Minyard50c812b2006-03-26 01:37:21 -08002548 kfree(intf->my_dev_name);
2549 intf->my_dev_name = NULL;
2550 printk(KERN_ERR
2551 "ipmi_msghandler:"
2552 " Unable to create symlink to bmc: %d\n",
2553 rv);
2554 goto out_err;
2555 }
2556
2557 return 0;
2558
2559out_err:
2560 ipmi_bmc_unregister(intf);
2561 return rv;
2562}
2563
2564static int
2565send_guid_cmd(ipmi_smi_t intf, int chan)
2566{
2567 struct kernel_ipmi_msg msg;
2568 struct ipmi_system_interface_addr si;
2569
2570 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2571 si.channel = IPMI_BMC_CHANNEL;
2572 si.lun = 0;
2573
2574 msg.netfn = IPMI_NETFN_APP_REQUEST;
2575 msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
2576 msg.data = NULL;
2577 msg.data_len = 0;
2578 return i_ipmi_request(NULL,
2579 intf,
2580 (struct ipmi_addr *) &si,
2581 0,
2582 &msg,
2583 intf,
2584 NULL,
2585 NULL,
2586 0,
2587 intf->channels[0].address,
2588 intf->channels[0].lun,
2589 -1, 0);
2590}
2591
2592static void
2593guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2594{
2595 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2596 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2597 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
2598 /* Not for me */
2599 return;
2600
2601 if (msg->msg.data[0] != 0) {
2602 /* Error from getting the GUID, the BMC doesn't have one. */
2603 intf->bmc->guid_set = 0;
2604 goto out;
2605 }
2606
2607 if (msg->msg.data_len < 17) {
2608 intf->bmc->guid_set = 0;
2609 printk(KERN_WARNING PFX
2610 "guid_handler: The GUID response from the BMC was too"
2611 " short, it was %d but should have been 17. Assuming"
2612 " GUID is not available.\n",
2613 msg->msg.data_len);
2614 goto out;
2615 }
2616
2617 memcpy(intf->bmc->guid, msg->msg.data, 16);
2618 intf->bmc->guid_set = 1;
2619 out:
2620 wake_up(&intf->waitq);
2621}
2622
2623static void
2624get_guid(ipmi_smi_t intf)
2625{
2626 int rv;
2627
2628 intf->bmc->guid_set = 0x2;
2629 intf->null_user_handler = guid_handler;
2630 rv = send_guid_cmd(intf, 0);
2631 if (rv)
2632 /* Send failed, no GUID available. */
2633 intf->bmc->guid_set = 0;
2634 wait_event(intf->waitq, intf->bmc->guid_set != 2);
2635 intf->null_user_handler = NULL;
2636}
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638static int
2639send_channel_info_cmd(ipmi_smi_t intf, int chan)
2640{
2641 struct kernel_ipmi_msg msg;
2642 unsigned char data[1];
2643 struct ipmi_system_interface_addr si;
2644
2645 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2646 si.channel = IPMI_BMC_CHANNEL;
2647 si.lun = 0;
2648
2649 msg.netfn = IPMI_NETFN_APP_REQUEST;
2650 msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
2651 msg.data = data;
2652 msg.data_len = 1;
2653 data[0] = chan;
2654 return i_ipmi_request(NULL,
2655 intf,
2656 (struct ipmi_addr *) &si,
2657 0,
2658 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07002659 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 NULL,
2661 NULL,
2662 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07002663 intf->channels[0].address,
2664 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 -1, 0);
2666}
2667
2668static void
Corey Minyard56a55ec2005-09-06 15:18:42 -07002669channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 int rv = 0;
2672 int chan;
2673
Corey Minyard56a55ec2005-09-06 15:18:42 -07002674 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2675 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
Corey Minyardc70d7492008-04-29 01:01:09 -07002676 && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* It's the one we want */
Corey Minyard56a55ec2005-09-06 15:18:42 -07002678 if (msg->msg.data[0] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /* Got an error from the channel, just go on. */
2680
Corey Minyard56a55ec2005-09-06 15:18:42 -07002681 if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) {
Corey Minyardc70d7492008-04-29 01:01:09 -07002682 /*
2683 * If the MC does not support this
2684 * command, that is legal. We just
2685 * assume it has one IPMB at channel
2686 * zero.
2687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 intf->channels[0].medium
2689 = IPMI_CHANNEL_MEDIUM_IPMB;
2690 intf->channels[0].protocol
2691 = IPMI_CHANNEL_PROTOCOL_IPMB;
2692 rv = -ENOSYS;
2693
2694 intf->curr_channel = IPMI_MAX_CHANNELS;
2695 wake_up(&intf->waitq);
2696 goto out;
2697 }
2698 goto next_channel;
2699 }
Corey Minyard56a55ec2005-09-06 15:18:42 -07002700 if (msg->msg.data_len < 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /* Message not big enough, just go on. */
2702 goto next_channel;
2703 }
2704 chan = intf->curr_channel;
Corey Minyard56a55ec2005-09-06 15:18:42 -07002705 intf->channels[chan].medium = msg->msg.data[2] & 0x7f;
2706 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Corey Minyardc70d7492008-04-29 01:01:09 -07002708 next_channel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 intf->curr_channel++;
2710 if (intf->curr_channel >= IPMI_MAX_CHANNELS)
2711 wake_up(&intf->waitq);
2712 else
2713 rv = send_channel_info_cmd(intf, intf->curr_channel);
2714
2715 if (rv) {
2716 /* Got an error somehow, just give up. */
2717 intf->curr_channel = IPMI_MAX_CHANNELS;
2718 wake_up(&intf->waitq);
2719
2720 printk(KERN_WARNING PFX
2721 "Error sending channel information: %d\n",
2722 rv);
2723 }
2724 }
2725 out:
2726 return;
2727}
2728
Corey Minyardfcfa4722007-10-18 03:07:09 -07002729void ipmi_poll_interface(ipmi_user_t user)
2730{
2731 ipmi_smi_t intf = user->intf;
2732
2733 if (intf->handlers->poll)
2734 intf->handlers->poll(intf->send_info);
2735}
Corey Minyardc70d7492008-04-29 01:01:09 -07002736EXPORT_SYMBOL(ipmi_poll_interface);
Corey Minyardfcfa4722007-10-18 03:07:09 -07002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738int ipmi_register_smi(struct ipmi_smi_handlers *handlers,
2739 void *send_info,
Corey Minyard50c812b2006-03-26 01:37:21 -08002740 struct ipmi_device_id *device_id,
2741 struct device *si_dev,
Corey Minyard759643b2006-12-06 20:40:59 -08002742 const char *sysfs_name,
Corey Minyard453823b2006-03-31 02:30:39 -08002743 unsigned char slave_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 int i, j;
2746 int rv;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002747 ipmi_smi_t intf;
Corey Minyardbca03242006-12-06 20:40:57 -08002748 ipmi_smi_t tintf;
Corey Minyardbca03242006-12-06 20:40:57 -08002749 struct list_head *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Corey Minyardc70d7492008-04-29 01:01:09 -07002751 /*
2752 * Make sure the driver is actually initialized, this handles
2753 * problems with initialization order.
2754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (!initialized) {
2756 rv = ipmi_init_msghandler();
2757 if (rv)
2758 return rv;
Corey Minyardc70d7492008-04-29 01:01:09 -07002759 /*
2760 * The init code doesn't return an error if it was turned
2761 * off, but it won't initialize. Check that.
2762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (!initialized)
2764 return -ENODEV;
2765 }
2766
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002767 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002768 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return -ENOMEM;
Corey Minyardb2c03942006-12-06 20:41:00 -08002770
2771 intf->ipmi_version_major = ipmi_version_major(device_id);
2772 intf->ipmi_version_minor = ipmi_version_minor(device_id);
2773
Corey Minyard50c812b2006-03-26 01:37:21 -08002774 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL);
2775 if (!intf->bmc) {
2776 kfree(intf);
2777 return -ENOMEM;
2778 }
Corey Minyardbca03242006-12-06 20:40:57 -08002779 intf->intf_num = -1; /* Mark it invalid for now. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002780 kref_init(&intf->refcount);
Corey Minyard50c812b2006-03-26 01:37:21 -08002781 intf->bmc->id = *device_id;
2782 intf->si_dev = si_dev;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002783 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2784 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR;
2785 intf->channels[j].lun = 2;
2786 }
2787 if (slave_addr != 0)
2788 intf->channels[0].address = slave_addr;
2789 INIT_LIST_HEAD(&intf->users);
2790 intf->handlers = handlers;
2791 intf->send_info = send_info;
2792 spin_lock_init(&intf->seq_lock);
2793 for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) {
2794 intf->seq_table[j].inuse = 0;
2795 intf->seq_table[j].seqid = 0;
2796 }
2797 intf->curr_seq = 0;
2798#ifdef CONFIG_PROC_FS
Corey Minyardac019152007-10-18 03:07:11 -07002799 mutex_init(&intf->proc_entry_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002800#endif
2801 spin_lock_init(&intf->waiting_msgs_lock);
2802 INIT_LIST_HEAD(&intf->waiting_msgs);
2803 spin_lock_init(&intf->events_lock);
2804 INIT_LIST_HEAD(&intf->waiting_events);
2805 intf->waiting_events_count = 0;
Corey Minyardd6dfd132006-03-31 02:30:41 -08002806 mutex_init(&intf->cmd_rcvrs_mutex);
Corey Minyardb9675132006-12-06 20:41:02 -08002807 spin_lock_init(&intf->maintenance_mode_lock);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002808 INIT_LIST_HEAD(&intf->cmd_rcvrs);
2809 init_waitqueue_head(&intf->waitq);
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002810 for (i = 0; i < IPMI_NUM_STATS; i++)
2811 atomic_set(&intf->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Corey Minyard393d2cc2005-11-07 00:59:54 -08002813 intf->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Corey Minyardb2c03942006-12-06 20:41:00 -08002815 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002816 mutex_lock(&ipmi_interfaces_mutex);
2817 /* Look for a hole in the numbers. */
2818 i = 0;
2819 link = &ipmi_interfaces;
2820 list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) {
2821 if (tintf->intf_num != i) {
2822 link = &tintf->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 break;
2824 }
Corey Minyardbca03242006-12-06 20:40:57 -08002825 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Corey Minyardbca03242006-12-06 20:40:57 -08002827 /* Add the new interface in numeric order. */
2828 if (i == 0)
2829 list_add_rcu(&intf->link, &ipmi_interfaces);
2830 else
2831 list_add_tail_rcu(&intf->link, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Corey Minyard453823b2006-03-31 02:30:39 -08002833 rv = handlers->start_processing(send_info, intf);
2834 if (rv)
2835 goto out;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002836
Corey Minyard50c812b2006-03-26 01:37:21 -08002837 get_guid(intf);
2838
Corey Minyardb2c03942006-12-06 20:41:00 -08002839 if ((intf->ipmi_version_major > 1)
Corey Minyardc70d7492008-04-29 01:01:09 -07002840 || ((intf->ipmi_version_major == 1)
2841 && (intf->ipmi_version_minor >= 5))) {
2842 /*
2843 * Start scanning the channels to see what is
2844 * available.
2845 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002846 intf->null_user_handler = channel_handler;
2847 intf->curr_channel = 0;
2848 rv = send_channel_info_cmd(intf, 0);
2849 if (rv)
2850 goto out;
2851
2852 /* Wait for the channel info to be read. */
2853 wait_event(intf->waitq,
2854 intf->curr_channel >= IPMI_MAX_CHANNELS);
Corey Minyard50c812b2006-03-26 01:37:21 -08002855 intf->null_user_handler = NULL;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002856 } else {
2857 /* Assume a single IPMB channel at zero. */
2858 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2859 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
Corey Minyard9a2845c2009-05-20 13:36:17 -05002860 intf->curr_channel = IPMI_MAX_CHANNELS;
Corey Minyard393d2cc2005-11-07 00:59:54 -08002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 if (rv == 0)
Corey Minyard393d2cc2005-11-07 00:59:54 -08002864 rv = add_proc_entries(intf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Corey Minyard759643b2006-12-06 20:40:59 -08002866 rv = ipmi_bmc_register(intf, i, sysfs_name);
Corey Minyard50c812b2006-03-26 01:37:21 -08002867
Corey Minyard393d2cc2005-11-07 00:59:54 -08002868 out:
2869 if (rv) {
2870 if (intf->proc_dir)
2871 remove_proc_entries(intf);
Corey Minyardb2c03942006-12-06 20:41:00 -08002872 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002873 list_del_rcu(&intf->link);
2874 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002875 mutex_unlock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002876 synchronize_rcu();
Corey Minyard393d2cc2005-11-07 00:59:54 -08002877 kref_put(&intf->refcount, intf_free);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002878 } else {
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002879 /*
2880 * Keep memory order straight for RCU readers. Make
2881 * sure everything else is committed to memory before
2882 * setting intf_num to mark the interface valid.
2883 */
2884 smp_wmb();
Corey Minyardbca03242006-12-06 20:40:57 -08002885 intf->intf_num = i;
2886 mutex_unlock(&ipmi_interfaces_mutex);
Corey Minyard78ba2fa2007-02-10 01:45:45 -08002887 /* After this point the interface is legal to use. */
Corey Minyard50c812b2006-03-26 01:37:21 -08002888 call_smi_watchers(i, intf->si_dev);
Corey Minyardb2c03942006-12-06 20:41:00 -08002889 mutex_unlock(&smi_watchers_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return rv;
2893}
Corey Minyardc70d7492008-04-29 01:01:09 -07002894EXPORT_SYMBOL(ipmi_register_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Corey Minyardb2c03942006-12-06 20:41:00 -08002896static void cleanup_smi_msgs(ipmi_smi_t intf)
2897{
2898 int i;
2899 struct seq_table *ent;
2900
2901 /* No need for locks, the interface is down. */
2902 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) {
2903 ent = &(intf->seq_table[i]);
2904 if (!ent->inuse)
2905 continue;
2906 deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED);
2907 }
2908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910int ipmi_unregister_smi(ipmi_smi_t intf)
2911{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 struct ipmi_smi_watcher *w;
Corey Minyardb2c03942006-12-06 20:41:00 -08002913 int intf_num = intf->intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Corey Minyard50c812b2006-03-26 01:37:21 -08002915 ipmi_bmc_unregister(intf);
2916
Corey Minyardb2c03942006-12-06 20:41:00 -08002917 mutex_lock(&smi_watchers_mutex);
Corey Minyardbca03242006-12-06 20:40:57 -08002918 mutex_lock(&ipmi_interfaces_mutex);
Corey Minyardb2c03942006-12-06 20:41:00 -08002919 intf->intf_num = -1;
2920 intf->handlers = NULL;
Corey Minyardbca03242006-12-06 20:40:57 -08002921 list_del_rcu(&intf->link);
2922 mutex_unlock(&ipmi_interfaces_mutex);
2923 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Corey Minyardb2c03942006-12-06 20:41:00 -08002925 cleanup_smi_msgs(intf);
2926
Corey Minyard393d2cc2005-11-07 00:59:54 -08002927 remove_proc_entries(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Corey Minyardc70d7492008-04-29 01:01:09 -07002929 /*
2930 * Call all the watcher interfaces to tell them that
2931 * an interface is gone.
2932 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08002933 list_for_each_entry(w, &smi_watchers, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08002934 w->smi_gone(intf_num);
2935 mutex_unlock(&smi_watchers_mutex);
Corey Minyard393d2cc2005-11-07 00:59:54 -08002936
Corey Minyard393d2cc2005-11-07 00:59:54 -08002937 kref_put(&intf->refcount, intf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return 0;
2939}
Corey Minyardc70d7492008-04-29 01:01:09 -07002940EXPORT_SYMBOL(ipmi_unregister_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf,
2943 struct ipmi_smi_msg *msg)
2944{
2945 struct ipmi_ipmb_addr ipmb_addr;
2946 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Corey Minyardc70d7492008-04-29 01:01:09 -07002948 /*
2949 * This is 11, not 10, because the response must contain a
2950 * completion code.
2951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (msg->rsp_size < 11) {
2953 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002954 ipmi_inc_stat(intf, invalid_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return 0;
2956 }
2957
2958 if (msg->rsp[2] != 0) {
2959 /* An error getting the response, just ignore it. */
2960 return 0;
2961 }
2962
2963 ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE;
2964 ipmb_addr.slave_addr = msg->rsp[6];
2965 ipmb_addr.channel = msg->rsp[3] & 0x0f;
2966 ipmb_addr.lun = msg->rsp[7] & 3;
2967
Corey Minyardc70d7492008-04-29 01:01:09 -07002968 /*
2969 * It's a response from a remote entity. Look up the sequence
2970 * number and handle the response.
2971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (intf_find_seq(intf,
2973 msg->rsp[7] >> 2,
2974 msg->rsp[3] & 0x0f,
2975 msg->rsp[8],
2976 (msg->rsp[4] >> 2) & (~1),
2977 (struct ipmi_addr *) &(ipmb_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07002978 &recv_msg)) {
2979 /*
2980 * We were unable to find the sequence number,
2981 * so just nuke the message.
2982 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002983 ipmi_inc_stat(intf, unhandled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return 0;
2985 }
2986
2987 memcpy(recv_msg->msg_data,
2988 &(msg->rsp[9]),
2989 msg->rsp_size - 9);
Corey Minyardc70d7492008-04-29 01:01:09 -07002990 /*
2991 * The other fields matched, so no need to set them, except
2992 * for netfn, which needs to be the response that was
2993 * returned, not the request value.
2994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 recv_msg->msg.netfn = msg->rsp[4] >> 2;
2996 recv_msg->msg.data = recv_msg->msg_data;
2997 recv_msg->msg.data_len = msg->rsp_size - 10;
2998 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07002999 ipmi_inc_stat(intf, handled_ipmb_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 deliver_response(recv_msg);
3001
3002 return 0;
3003}
3004
3005static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3006 struct ipmi_smi_msg *msg)
3007{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003008 struct cmd_rcvr *rcvr;
3009 int rv = 0;
3010 unsigned char netfn;
3011 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003012 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003013 ipmi_user_t user = NULL;
3014 struct ipmi_ipmb_addr *ipmb_addr;
3015 struct ipmi_recv_msg *recv_msg;
Corey Minyardb2c03942006-12-06 20:41:00 -08003016 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 if (msg->rsp_size < 10) {
3019 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003020 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return 0;
3022 }
3023
3024 if (msg->rsp[2] != 0) {
3025 /* An error getting the response, just ignore it. */
3026 return 0;
3027 }
3028
3029 netfn = msg->rsp[4] >> 2;
3030 cmd = msg->rsp[8];
Corey Minyardc69c3122006-09-30 23:27:56 -07003031 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003033 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003034 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003035 if (rcvr) {
3036 user = rcvr->user;
3037 kref_get(&user->refcount);
3038 } else
3039 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003040 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 if (user == NULL) {
3043 /* We didn't find a user, deliver an error response. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003044 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
3047 msg->data[1] = IPMI_SEND_MSG_CMD;
3048 msg->data[2] = msg->rsp[3];
3049 msg->data[3] = msg->rsp[6];
Corey Minyardc70d7492008-04-29 01:01:09 -07003050 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
Corey Minyardc14979b2005-09-06 15:18:38 -07003052 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address;
Corey Minyardc70d7492008-04-29 01:01:09 -07003053 /* rqseq/lun */
3054 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 msg->data[8] = msg->rsp[8]; /* cmd */
3056 msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE;
3057 msg->data[10] = ipmb_checksum(&(msg->data[6]), 4);
3058 msg->data_size = 11;
3059
3060#ifdef DEBUG_MSGING
3061 {
3062 int m;
3063 printk("Invalid command:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003064 for (m = 0; m < msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 printk(" %2.2x", msg->data[m]);
3066 printk("\n");
3067 }
3068#endif
Corey Minyardb2c03942006-12-06 20:41:00 -08003069 rcu_read_lock();
3070 handlers = intf->handlers;
3071 if (handlers) {
3072 handlers->sender(intf->send_info, msg, 0);
Corey Minyardc70d7492008-04-29 01:01:09 -07003073 /*
3074 * We used the message, so return the value
3075 * that causes it to not be freed or
3076 * queued.
3077 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003078 rv = -1;
3079 }
3080 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 } else {
3082 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003083 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003086 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003087 /*
3088 * We couldn't allocate memory for the
3089 * message, so requeue it for handling
3090 * later.
3091 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003093 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 } else {
3095 /* Extract the source address from the data. */
3096 ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr;
3097 ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE;
3098 ipmb_addr->slave_addr = msg->rsp[6];
3099 ipmb_addr->lun = msg->rsp[7] & 3;
3100 ipmb_addr->channel = msg->rsp[3] & 0xf;
3101
Corey Minyardc70d7492008-04-29 01:01:09 -07003102 /*
3103 * Extract the rest of the message information
3104 * from the IPMB header.
3105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 recv_msg->user = user;
3107 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3108 recv_msg->msgid = msg->rsp[7] >> 2;
3109 recv_msg->msg.netfn = msg->rsp[4] >> 2;
3110 recv_msg->msg.cmd = msg->rsp[8];
3111 recv_msg->msg.data = recv_msg->msg_data;
3112
Corey Minyardc70d7492008-04-29 01:01:09 -07003113 /*
3114 * We chop off 10, not 9 bytes because the checksum
3115 * at the end also needs to be removed.
3116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 recv_msg->msg.data_len = msg->rsp_size - 10;
3118 memcpy(recv_msg->msg_data,
3119 &(msg->rsp[9]),
3120 msg->rsp_size - 10);
3121 deliver_response(recv_msg);
3122 }
3123 }
3124
3125 return rv;
3126}
3127
3128static int handle_lan_get_msg_rsp(ipmi_smi_t intf,
3129 struct ipmi_smi_msg *msg)
3130{
3131 struct ipmi_lan_addr lan_addr;
3132 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134
Corey Minyardc70d7492008-04-29 01:01:09 -07003135 /*
3136 * This is 13, not 12, because the response must contain a
3137 * completion code.
3138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (msg->rsp_size < 13) {
3140 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003141 ipmi_inc_stat(intf, invalid_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return 0;
3143 }
3144
3145 if (msg->rsp[2] != 0) {
3146 /* An error getting the response, just ignore it. */
3147 return 0;
3148 }
3149
3150 lan_addr.addr_type = IPMI_LAN_ADDR_TYPE;
3151 lan_addr.session_handle = msg->rsp[4];
3152 lan_addr.remote_SWID = msg->rsp[8];
3153 lan_addr.local_SWID = msg->rsp[5];
3154 lan_addr.channel = msg->rsp[3] & 0x0f;
3155 lan_addr.privilege = msg->rsp[3] >> 4;
3156 lan_addr.lun = msg->rsp[9] & 3;
3157
Corey Minyardc70d7492008-04-29 01:01:09 -07003158 /*
3159 * It's a response from a remote entity. Look up the sequence
3160 * number and handle the response.
3161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (intf_find_seq(intf,
3163 msg->rsp[9] >> 2,
3164 msg->rsp[3] & 0x0f,
3165 msg->rsp[10],
3166 (msg->rsp[6] >> 2) & (~1),
3167 (struct ipmi_addr *) &(lan_addr),
Corey Minyardc70d7492008-04-29 01:01:09 -07003168 &recv_msg)) {
3169 /*
3170 * We were unable to find the sequence number,
3171 * so just nuke the message.
3172 */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003173 ipmi_inc_stat(intf, unhandled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return 0;
3175 }
3176
3177 memcpy(recv_msg->msg_data,
3178 &(msg->rsp[11]),
3179 msg->rsp_size - 11);
Corey Minyardc70d7492008-04-29 01:01:09 -07003180 /*
3181 * The other fields matched, so no need to set them, except
3182 * for netfn, which needs to be the response that was
3183 * returned, not the request value.
3184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3186 recv_msg->msg.data = recv_msg->msg_data;
3187 recv_msg->msg.data_len = msg->rsp_size - 12;
3188 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003189 ipmi_inc_stat(intf, handled_lan_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 deliver_response(recv_msg);
3191
3192 return 0;
3193}
3194
3195static int handle_lan_get_msg_cmd(ipmi_smi_t intf,
3196 struct ipmi_smi_msg *msg)
3197{
Corey Minyard393d2cc2005-11-07 00:59:54 -08003198 struct cmd_rcvr *rcvr;
3199 int rv = 0;
3200 unsigned char netfn;
3201 unsigned char cmd;
Corey Minyardc69c3122006-09-30 23:27:56 -07003202 unsigned char chan;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003203 ipmi_user_t user = NULL;
3204 struct ipmi_lan_addr *lan_addr;
3205 struct ipmi_recv_msg *recv_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 if (msg->rsp_size < 12) {
3208 /* Message not big enough, just ignore it. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003209 ipmi_inc_stat(intf, invalid_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
3211 }
3212
3213 if (msg->rsp[2] != 0) {
3214 /* An error getting the response, just ignore it. */
3215 return 0;
3216 }
3217
3218 netfn = msg->rsp[6] >> 2;
3219 cmd = msg->rsp[10];
Corey Minyardc69c3122006-09-30 23:27:56 -07003220 chan = msg->rsp[3] & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003222 rcu_read_lock();
Corey Minyardc69c3122006-09-30 23:27:56 -07003223 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003224 if (rcvr) {
3225 user = rcvr->user;
3226 kref_get(&user->refcount);
3227 } else
3228 user = NULL;
Corey Minyarde61fb5b2005-11-07 01:00:05 -08003229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 if (user == NULL) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003232 /* We didn't find a user, just give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003233 ipmi_inc_stat(intf, unhandled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Corey Minyardc70d7492008-04-29 01:01:09 -07003235 /*
3236 * Don't do anything with these messages, just allow
3237 * them to be freed.
3238 */
3239 rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 } else {
3241 /* Deliver the message to the user. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003242 ipmi_inc_stat(intf, handled_commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003245 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003246 /*
3247 * We couldn't allocate memory for the
3248 * message, so requeue it for handling later.
3249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 rv = 1;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003251 kref_put(&user->refcount, free_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 } else {
3253 /* Extract the source address from the data. */
3254 lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr;
3255 lan_addr->addr_type = IPMI_LAN_ADDR_TYPE;
3256 lan_addr->session_handle = msg->rsp[4];
3257 lan_addr->remote_SWID = msg->rsp[8];
3258 lan_addr->local_SWID = msg->rsp[5];
3259 lan_addr->lun = msg->rsp[9] & 3;
3260 lan_addr->channel = msg->rsp[3] & 0xf;
3261 lan_addr->privilege = msg->rsp[3] >> 4;
3262
Corey Minyardc70d7492008-04-29 01:01:09 -07003263 /*
3264 * Extract the rest of the message information
3265 * from the IPMB header.
3266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 recv_msg->user = user;
3268 recv_msg->recv_type = IPMI_CMD_RECV_TYPE;
3269 recv_msg->msgid = msg->rsp[9] >> 2;
3270 recv_msg->msg.netfn = msg->rsp[6] >> 2;
3271 recv_msg->msg.cmd = msg->rsp[10];
3272 recv_msg->msg.data = recv_msg->msg_data;
3273
Corey Minyardc70d7492008-04-29 01:01:09 -07003274 /*
3275 * We chop off 12, not 11 bytes because the checksum
3276 * at the end also needs to be removed.
3277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 recv_msg->msg.data_len = msg->rsp_size - 12;
3279 memcpy(recv_msg->msg_data,
3280 &(msg->rsp[11]),
3281 msg->rsp_size - 12);
3282 deliver_response(recv_msg);
3283 }
3284 }
3285
3286 return rv;
3287}
3288
dann frazier4dec3022009-04-21 12:24:05 -07003289/*
3290 * This routine will handle "Get Message" command responses with
3291 * channels that use an OEM Medium. The message format belongs to
3292 * the OEM. See IPMI 2.0 specification, Chapter 6 and
3293 * Chapter 22, sections 22.6 and 22.24 for more details.
3294 */
3295static int handle_oem_get_msg_cmd(ipmi_smi_t intf,
3296 struct ipmi_smi_msg *msg)
3297{
3298 struct cmd_rcvr *rcvr;
3299 int rv = 0;
3300 unsigned char netfn;
3301 unsigned char cmd;
3302 unsigned char chan;
3303 ipmi_user_t user = NULL;
3304 struct ipmi_system_interface_addr *smi_addr;
3305 struct ipmi_recv_msg *recv_msg;
3306
3307 /*
3308 * We expect the OEM SW to perform error checking
3309 * so we just do some basic sanity checks
3310 */
3311 if (msg->rsp_size < 4) {
3312 /* Message not big enough, just ignore it. */
3313 ipmi_inc_stat(intf, invalid_commands);
3314 return 0;
3315 }
3316
3317 if (msg->rsp[2] != 0) {
3318 /* An error getting the response, just ignore it. */
3319 return 0;
3320 }
3321
3322 /*
3323 * This is an OEM Message so the OEM needs to know how
3324 * handle the message. We do no interpretation.
3325 */
3326 netfn = msg->rsp[0] >> 2;
3327 cmd = msg->rsp[1];
3328 chan = msg->rsp[3] & 0xf;
3329
3330 rcu_read_lock();
3331 rcvr = find_cmd_rcvr(intf, netfn, cmd, chan);
3332 if (rcvr) {
3333 user = rcvr->user;
3334 kref_get(&user->refcount);
3335 } else
3336 user = NULL;
3337 rcu_read_unlock();
3338
3339 if (user == NULL) {
3340 /* We didn't find a user, just give up. */
3341 ipmi_inc_stat(intf, unhandled_commands);
3342
3343 /*
3344 * Don't do anything with these messages, just allow
3345 * them to be freed.
3346 */
3347
3348 rv = 0;
3349 } else {
3350 /* Deliver the message to the user. */
3351 ipmi_inc_stat(intf, handled_commands);
3352
3353 recv_msg = ipmi_alloc_recv_msg();
3354 if (!recv_msg) {
3355 /*
3356 * We couldn't allocate memory for the
3357 * message, so requeue it for handling
3358 * later.
3359 */
3360 rv = 1;
3361 kref_put(&user->refcount, free_user);
3362 } else {
3363 /*
3364 * OEM Messages are expected to be delivered via
3365 * the system interface to SMS software. We might
3366 * need to visit this again depending on OEM
3367 * requirements
3368 */
3369 smi_addr = ((struct ipmi_system_interface_addr *)
3370 &(recv_msg->addr));
3371 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3372 smi_addr->channel = IPMI_BMC_CHANNEL;
3373 smi_addr->lun = msg->rsp[0] & 3;
3374
3375 recv_msg->user = user;
3376 recv_msg->user_msg_data = NULL;
3377 recv_msg->recv_type = IPMI_OEM_RECV_TYPE;
3378 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3379 recv_msg->msg.cmd = msg->rsp[1];
3380 recv_msg->msg.data = recv_msg->msg_data;
3381
3382 /*
3383 * The message starts at byte 4 which follows the
3384 * the Channel Byte in the "GET MESSAGE" command
3385 */
3386 recv_msg->msg.data_len = msg->rsp_size - 4;
3387 memcpy(recv_msg->msg_data,
3388 &(msg->rsp[4]),
3389 msg->rsp_size - 4);
3390 deliver_response(recv_msg);
3391 }
3392 }
3393
3394 return rv;
3395}
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg,
3398 struct ipmi_smi_msg *msg)
3399{
3400 struct ipmi_system_interface_addr *smi_addr;
Corey Minyardc70d7492008-04-29 01:01:09 -07003401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 recv_msg->msgid = 0;
3403 smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr);
3404 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3405 smi_addr->channel = IPMI_BMC_CHANNEL;
3406 smi_addr->lun = msg->rsp[0] & 3;
3407 recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE;
3408 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3409 recv_msg->msg.cmd = msg->rsp[1];
3410 memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3);
3411 recv_msg->msg.data = recv_msg->msg_data;
3412 recv_msg->msg.data_len = msg->rsp_size - 3;
3413}
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415static int handle_read_event_rsp(ipmi_smi_t intf,
3416 struct ipmi_smi_msg *msg)
3417{
3418 struct ipmi_recv_msg *recv_msg, *recv_msg2;
3419 struct list_head msgs;
3420 ipmi_user_t user;
3421 int rv = 0;
3422 int deliver_count = 0;
3423 unsigned long flags;
3424
3425 if (msg->rsp_size < 19) {
3426 /* Message is too small to be an IPMB event. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003427 ipmi_inc_stat(intf, invalid_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return 0;
3429 }
3430
3431 if (msg->rsp[2] != 0) {
3432 /* An error getting the event, just ignore it. */
3433 return 0;
3434 }
3435
3436 INIT_LIST_HEAD(&msgs);
3437
Corey Minyard393d2cc2005-11-07 00:59:54 -08003438 spin_lock_irqsave(&intf->events_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003440 ipmi_inc_stat(intf, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Corey Minyardc70d7492008-04-29 01:01:09 -07003442 /*
3443 * Allocate and fill in one message for every user that is
3444 * getting events.
3445 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003446 rcu_read_lock();
3447 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003448 if (!user->gets_events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 continue;
3450
3451 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003452 if (!recv_msg) {
Corey Minyard393d2cc2005-11-07 00:59:54 -08003453 rcu_read_unlock();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003454 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs,
3455 link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 list_del(&recv_msg->link);
3457 ipmi_free_recv_msg(recv_msg);
3458 }
Corey Minyardc70d7492008-04-29 01:01:09 -07003459 /*
3460 * We couldn't allocate memory for the
3461 * message, so requeue it for handling
3462 * later.
3463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 rv = 1;
3465 goto out;
3466 }
3467
3468 deliver_count++;
3469
3470 copy_event_into_recv_msg(recv_msg, msg);
3471 recv_msg->user = user;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003472 kref_get(&user->refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 list_add_tail(&(recv_msg->link), &msgs);
3474 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003475 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 if (deliver_count) {
3478 /* Now deliver all the messages. */
3479 list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) {
3480 list_del(&recv_msg->link);
3481 deliver_response(recv_msg);
3482 }
3483 } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003484 /*
3485 * No one to receive the message, put it in queue if there's
3486 * not already too many things in the queue.
3487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 recv_msg = ipmi_alloc_recv_msg();
Corey Minyard8a3628d2006-03-31 02:30:40 -08003489 if (!recv_msg) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003490 /*
3491 * We couldn't allocate memory for the
3492 * message, so requeue it for handling
3493 * later.
3494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 rv = 1;
3496 goto out;
3497 }
3498
3499 copy_event_into_recv_msg(recv_msg, msg);
3500 list_add_tail(&(recv_msg->link), &(intf->waiting_events));
Corey Minyard4791c032006-04-10 22:54:31 -07003501 intf->waiting_events_count++;
Corey Minyard87ebd062008-04-29 01:01:04 -07003502 } else if (!intf->event_msg_printed) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003503 /*
3504 * There's too many things in the queue, discard this
3505 * message.
3506 */
Corey Minyard87ebd062008-04-29 01:01:04 -07003507 printk(KERN_WARNING PFX "Event queue full, discarding"
3508 " incoming events\n");
3509 intf->event_msg_printed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 }
3511
3512 out:
3513 spin_unlock_irqrestore(&(intf->events_lock), flags);
3514
3515 return rv;
3516}
3517
3518static int handle_bmc_rsp(ipmi_smi_t intf,
3519 struct ipmi_smi_msg *msg)
3520{
3521 struct ipmi_recv_msg *recv_msg;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003522 struct ipmi_user *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
Corey Minyardc70d7492008-04-29 01:01:09 -07003525 if (recv_msg == NULL) {
3526 printk(KERN_WARNING
3527 "IPMI message received with no owner. This\n"
3528 "could be because of a malformed message, or\n"
3529 "because of a hardware error. Contact your\n"
3530 "hardware vender for assistance\n");
Corey Minyard56a55ec2005-09-06 15:18:42 -07003531 return 0;
3532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Corey Minyard393d2cc2005-11-07 00:59:54 -08003534 user = recv_msg->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 /* Make sure the user still exists. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003536 if (user && !user->valid) {
Corey Minyard56a55ec2005-09-06 15:18:42 -07003537 /* The user for the message went away, so give up. */
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003538 ipmi_inc_stat(intf, unhandled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 ipmi_free_recv_msg(recv_msg);
3540 } else {
3541 struct ipmi_system_interface_addr *smi_addr;
3542
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003543 ipmi_inc_stat(intf, handled_local_responses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
3545 recv_msg->msgid = msg->msgid;
3546 smi_addr = ((struct ipmi_system_interface_addr *)
3547 &(recv_msg->addr));
3548 smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
3549 smi_addr->channel = IPMI_BMC_CHANNEL;
3550 smi_addr->lun = msg->rsp[0] & 3;
3551 recv_msg->msg.netfn = msg->rsp[0] >> 2;
3552 recv_msg->msg.cmd = msg->rsp[1];
3553 memcpy(recv_msg->msg_data,
3554 &(msg->rsp[2]),
3555 msg->rsp_size - 2);
3556 recv_msg->msg.data = recv_msg->msg_data;
3557 recv_msg->msg.data_len = msg->rsp_size - 2;
3558 deliver_response(recv_msg);
3559 }
3560
3561 return 0;
3562}
3563
Corey Minyardc70d7492008-04-29 01:01:09 -07003564/*
3565 * Handle a new message. Return 1 if the message should be requeued,
3566 * 0 if the message should be freed, or -1 if the message should not
3567 * be freed or requeued.
3568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569static int handle_new_recv_msg(ipmi_smi_t intf,
3570 struct ipmi_smi_msg *msg)
3571{
3572 int requeue;
3573 int chan;
3574
3575#ifdef DEBUG_MSGING
3576 int m;
3577 printk("Recv:");
Corey Minyarde8b33612005-09-06 15:18:45 -07003578 for (m = 0; m < msg->rsp_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 printk(" %2.2x", msg->rsp[m]);
3580 printk("\n");
3581#endif
3582 if (msg->rsp_size < 2) {
3583 /* Message is too small to be correct. */
3584 printk(KERN_WARNING PFX "BMC returned to small a message"
3585 " for netfn %x cmd %x, got %d bytes\n",
3586 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3587
3588 /* Generate an error response for the message. */
3589 msg->rsp[0] = msg->data[0] | (1 << 2);
3590 msg->rsp[1] = msg->data[1];
3591 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3592 msg->rsp_size = 3;
Corey Minyardc70d7492008-04-29 01:01:09 -07003593 } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1))
3594 || (msg->rsp[1] != msg->data[1])) {
3595 /*
3596 * The NetFN and Command in the response is not even
3597 * marginally correct.
3598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 printk(KERN_WARNING PFX "BMC returned incorrect response,"
3600 " expected netfn %x cmd %x, got netfn %x cmd %x\n",
3601 (msg->data[0] >> 2) | 1, msg->data[1],
3602 msg->rsp[0] >> 2, msg->rsp[1]);
3603
3604 /* Generate an error response for the message. */
3605 msg->rsp[0] = msg->data[0] | (1 << 2);
3606 msg->rsp[1] = msg->data[1];
3607 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
3608 msg->rsp_size = 3;
3609 }
3610
3611 if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3612 && (msg->rsp[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003613 && (msg->user_data != NULL)) {
3614 /*
3615 * It's a response to a response we sent. For this we
3616 * deliver a send message response to the user.
3617 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003618 struct ipmi_recv_msg *recv_msg = msg->user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 requeue = 0;
3621 if (msg->rsp_size < 2)
3622 /* Message is too small to be correct. */
3623 goto out;
3624
3625 chan = msg->data[2] & 0x0f;
3626 if (chan >= IPMI_MAX_CHANNELS)
3627 /* Invalid channel number */
3628 goto out;
3629
Corey Minyard393d2cc2005-11-07 00:59:54 -08003630 if (!recv_msg)
3631 goto out;
3632
3633 /* Make sure the user still exists. */
3634 if (!recv_msg->user || !recv_msg->user->valid)
3635 goto out;
3636
3637 recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE;
3638 recv_msg->msg.data = recv_msg->msg_data;
3639 recv_msg->msg.data_len = 1;
3640 recv_msg->msg_data[0] = msg->rsp[2];
3641 deliver_response(recv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003643 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* It's from the receive queue. */
3645 chan = msg->rsp[3] & 0xf;
3646 if (chan >= IPMI_MAX_CHANNELS) {
3647 /* Invalid channel number */
3648 requeue = 0;
3649 goto out;
3650 }
3651
dann frazier4dec3022009-04-21 12:24:05 -07003652 /*
Corey Minyard9a2845c2009-05-20 13:36:17 -05003653 * We need to make sure the channels have been initialized.
3654 * The channel_handler routine will set the "curr_channel"
3655 * equal to or greater than IPMI_MAX_CHANNELS when all the
3656 * channels for this interface have been initialized.
3657 */
dann frazier4dec3022009-04-21 12:24:05 -07003658 if (intf->curr_channel < IPMI_MAX_CHANNELS) {
Corey Minyard9a2845c2009-05-20 13:36:17 -05003659 requeue = 0; /* Throw the message away */
dann frazier4dec3022009-04-21 12:24:05 -07003660 goto out;
3661 }
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 switch (intf->channels[chan].medium) {
3664 case IPMI_CHANNEL_MEDIUM_IPMB:
3665 if (msg->rsp[4] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003666 /*
3667 * It's a response, so find the
3668 * requesting message and send it up.
3669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 requeue = handle_ipmb_get_msg_rsp(intf, msg);
3671 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003672 /*
3673 * It's a command to the SMS from some other
3674 * entity. Handle that.
3675 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 requeue = handle_ipmb_get_msg_cmd(intf, msg);
3677 }
3678 break;
3679
3680 case IPMI_CHANNEL_MEDIUM_8023LAN:
3681 case IPMI_CHANNEL_MEDIUM_ASYNC:
3682 if (msg->rsp[6] & 0x04) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003683 /*
3684 * It's a response, so find the
3685 * requesting message and send it up.
3686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 requeue = handle_lan_get_msg_rsp(intf, msg);
3688 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003689 /*
3690 * It's a command to the SMS from some other
3691 * entity. Handle that.
3692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 requeue = handle_lan_get_msg_cmd(intf, msg);
3694 }
3695 break;
3696
3697 default:
dann frazier4dec3022009-04-21 12:24:05 -07003698 /* Check for OEM Channels. Clients had better
3699 register for these commands. */
3700 if ((intf->channels[chan].medium
3701 >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3702 && (intf->channels[chan].medium
3703 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3704 requeue = handle_oem_get_msg_cmd(intf, msg);
3705 } else {
3706 /*
3707 * We don't handle the channel type, so just
3708 * free the message.
3709 */
3710 requeue = 0;
3711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 }
3713
3714 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
Corey Minyardc70d7492008-04-29 01:01:09 -07003715 && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 /* It's an asyncronous event. */
3717 requeue = handle_read_event_rsp(intf, msg);
3718 } else {
3719 /* It's a response from the local BMC. */
3720 requeue = handle_bmc_rsp(intf, msg);
3721 }
3722
3723 out:
3724 return requeue;
3725}
3726
3727/* Handle a new message from the lower layer. */
3728void ipmi_smi_msg_received(ipmi_smi_t intf,
3729 struct ipmi_smi_msg *msg)
3730{
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003731 unsigned long flags = 0; /* keep us warning-free. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 int rv;
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003733 int run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 if ((msg->data_size >= 2)
3737 && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2))
3738 && (msg->data[1] == IPMI_SEND_MSG_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07003739 && (msg->user_data == NULL)) {
3740 /*
3741 * This is the local response to a command send, start
3742 * the timer for these. The user_data will not be
3743 * NULL if this is a response send, and we will let
3744 * response sends just go through.
3745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Corey Minyardc70d7492008-04-29 01:01:09 -07003747 /*
3748 * Check for errors, if we get certain errors (ones
3749 * that mean basically we can try again later), we
3750 * ignore them and start the timer. Otherwise we
3751 * report the error immediately.
3752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0)
3754 && (msg->rsp[2] != IPMI_NODE_BUSY_ERR)
Corey Minyard46d52b02006-11-08 17:44:55 -08003755 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3756 && (msg->rsp[2] != IPMI_BUS_ERR)
Corey Minyardc70d7492008-04-29 01:01:09 -07003757 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 int chan = msg->rsp[3] & 0xf;
3759
3760 /* Got an error sending the message, handle it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (chan >= IPMI_MAX_CHANNELS)
3762 ; /* This shouldn't happen */
3763 else if ((intf->channels[chan].medium
3764 == IPMI_CHANNEL_MEDIUM_8023LAN)
3765 || (intf->channels[chan].medium
3766 == IPMI_CHANNEL_MEDIUM_ASYNC))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003767 ipmi_inc_stat(intf, sent_lan_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003769 ipmi_inc_stat(intf, sent_ipmb_command_errs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 intf_err_seq(intf, msg->msgid, msg->rsp[2]);
Corey Minyardc70d7492008-04-29 01:01:09 -07003771 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /* The message was sent, start the timer. */
3773 intf_start_seq_timer(intf, msg->msgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 ipmi_free_smi_msg(msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 }
3778
Corey Minyardc70d7492008-04-29 01:01:09 -07003779 /*
3780 * To preserve message order, if the list is not empty, we
3781 * tack this message onto the end of the list.
3782 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003783 run_to_completion = intf->run_to_completion;
3784 if (!run_to_completion)
3785 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003786 if (!list_empty(&intf->waiting_msgs)) {
3787 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003788 if (!run_to_completion)
3789 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003792 if (!run_to_completion)
3793 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Corey Minyardc70d7492008-04-29 01:01:09 -07003794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 rv = handle_new_recv_msg(intf, msg);
3796 if (rv > 0) {
Corey Minyardc70d7492008-04-29 01:01:09 -07003797 /*
3798 * Could not handle the message now, just add it to a
3799 * list to handle later.
3800 */
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003801 run_to_completion = intf->run_to_completion;
3802 if (!run_to_completion)
3803 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003804 list_add_tail(&msg->link, &intf->waiting_msgs);
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07003805 if (!run_to_completion)
3806 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 } else if (rv == 0) {
3808 ipmi_free_smi_msg(msg);
3809 }
3810
Corey Minyard393d2cc2005-11-07 00:59:54 -08003811 out:
3812 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
Corey Minyardc70d7492008-04-29 01:01:09 -07003814EXPORT_SYMBOL(ipmi_smi_msg_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf)
3817{
3818 ipmi_user_t user;
3819
Corey Minyard393d2cc2005-11-07 00:59:54 -08003820 rcu_read_lock();
3821 list_for_each_entry_rcu(user, &intf->users, link) {
Corey Minyard8a3628d2006-03-31 02:30:40 -08003822 if (!user->handler->ipmi_watchdog_pretimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 continue;
3824
3825 user->handler->ipmi_watchdog_pretimeout(user->handler_data);
3826 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003827 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
Corey Minyardc70d7492008-04-29 01:01:09 -07003829EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Corey Minyard882fe012005-05-01 08:59:12 -07003831static struct ipmi_smi_msg *
3832smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
3833 unsigned char seq, long seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Corey Minyard882fe012005-05-01 08:59:12 -07003835 struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 if (!smi_msg)
Corey Minyardc70d7492008-04-29 01:01:09 -07003837 /*
3838 * If we can't allocate the message, then just return, we
3839 * get 4 retries, so this should be ok.
3840 */
Corey Minyard882fe012005-05-01 08:59:12 -07003841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len);
3844 smi_msg->data_size = recv_msg->msg.data_len;
3845 smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid);
Corey Minyardc70d7492008-04-29 01:01:09 -07003846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847#ifdef DEBUG_MSGING
3848 {
3849 int m;
3850 printk("Resend: ");
Corey Minyarde8b33612005-09-06 15:18:45 -07003851 for (m = 0; m < smi_msg->data_size; m++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 printk(" %2.2x", smi_msg->data[m]);
3853 printk("\n");
3854 }
3855#endif
Corey Minyard882fe012005-05-01 08:59:12 -07003856 return smi_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
Corey Minyard393d2cc2005-11-07 00:59:54 -08003859static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
3860 struct list_head *timeouts, long timeout_period,
3861 int slot, unsigned long *flags)
3862{
Corey Minyardb2c03942006-12-06 20:41:00 -08003863 struct ipmi_recv_msg *msg;
3864 struct ipmi_smi_handlers *handlers;
3865
3866 if (intf->intf_num == -1)
3867 return;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003868
3869 if (!ent->inuse)
3870 return;
3871
3872 ent->timeout -= timeout_period;
3873 if (ent->timeout > 0)
3874 return;
3875
3876 if (ent->retries_left == 0) {
3877 /* The message has used all its retries. */
3878 ent->inuse = 0;
3879 msg = ent->recv_msg;
3880 list_add_tail(&msg->link, timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003881 if (ent->broadcast)
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003882 ipmi_inc_stat(intf, timed_out_ipmb_broadcasts);
Corey Minyard25176ed2009-04-21 12:24:04 -07003883 else if (is_lan_addr(&ent->recv_msg->addr))
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003884 ipmi_inc_stat(intf, timed_out_lan_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003885 else
Konstantin Baydarovb2655f22008-04-29 01:01:05 -07003886 ipmi_inc_stat(intf, timed_out_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003887 } else {
3888 struct ipmi_smi_msg *smi_msg;
3889 /* More retries, send again. */
3890
Corey Minyardc70d7492008-04-29 01:01:09 -07003891 /*
3892 * Start with the max timer, set to normal timer after
3893 * the message is sent.
3894 */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003895 ent->timeout = MAX_MSG_TIMEOUT;
3896 ent->retries_left--;
Corey Minyard393d2cc2005-11-07 00:59:54 -08003897 smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot,
3898 ent->seqid);
Corey Minyard25176ed2009-04-21 12:24:04 -07003899 if (!smi_msg) {
3900 if (is_lan_addr(&ent->recv_msg->addr))
3901 ipmi_inc_stat(intf,
3902 dropped_rexmit_lan_commands);
3903 else
3904 ipmi_inc_stat(intf,
3905 dropped_rexmit_ipmb_commands);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003906 return;
Corey Minyard25176ed2009-04-21 12:24:04 -07003907 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003908
3909 spin_unlock_irqrestore(&intf->seq_lock, *flags);
Corey Minyardb2c03942006-12-06 20:41:00 -08003910
Corey Minyardc70d7492008-04-29 01:01:09 -07003911 /*
3912 * Send the new message. We send with a zero
3913 * priority. It timed out, I doubt time is that
3914 * critical now, and high priority messages are really
3915 * only for messages to the local MC, which don't get
3916 * resent.
3917 */
Corey Minyardb2c03942006-12-06 20:41:00 -08003918 handlers = intf->handlers;
Corey Minyard25176ed2009-04-21 12:24:04 -07003919 if (handlers) {
3920 if (is_lan_addr(&ent->recv_msg->addr))
3921 ipmi_inc_stat(intf,
3922 retransmitted_lan_commands);
3923 else
3924 ipmi_inc_stat(intf,
3925 retransmitted_ipmb_commands);
3926
Corey Minyardb2c03942006-12-06 20:41:00 -08003927 intf->handlers->sender(intf->send_info,
3928 smi_msg, 0);
Corey Minyard25176ed2009-04-21 12:24:04 -07003929 } else
Corey Minyardb2c03942006-12-06 20:41:00 -08003930 ipmi_free_smi_msg(smi_msg);
3931
Corey Minyard393d2cc2005-11-07 00:59:54 -08003932 spin_lock_irqsave(&intf->seq_lock, *flags);
3933 }
3934}
3935
3936static void ipmi_timeout_handler(long timeout_period)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
3938 ipmi_smi_t intf;
3939 struct list_head timeouts;
3940 struct ipmi_recv_msg *msg, *msg2;
3941 struct ipmi_smi_msg *smi_msg, *smi_msg2;
3942 unsigned long flags;
Corey Minyardbca03242006-12-06 20:40:57 -08003943 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Corey Minyardbca03242006-12-06 20:40:57 -08003945 rcu_read_lock();
3946 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 /* See if any waiting messages need to be processed. */
Corey Minyard393d2cc2005-11-07 00:59:54 -08003948 spin_lock_irqsave(&intf->waiting_msgs_lock, flags);
Corey Minyard8a3628d2006-03-31 02:30:40 -08003949 list_for_each_entry_safe(smi_msg, smi_msg2,
3950 &intf->waiting_msgs, link) {
3951 if (!handle_new_recv_msg(intf, smi_msg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 list_del(&smi_msg->link);
3953 ipmi_free_smi_msg(smi_msg);
3954 } else {
Corey Minyardc70d7492008-04-29 01:01:09 -07003955 /*
3956 * To preserve message order, quit if we
3957 * can't handle a message.
3958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 break;
3960 }
3961 }
Corey Minyard393d2cc2005-11-07 00:59:54 -08003962 spin_unlock_irqrestore(&intf->waiting_msgs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Corey Minyardc70d7492008-04-29 01:01:09 -07003964 /*
3965 * Go through the seq table and find any messages that
3966 * have timed out, putting them in the timeouts
3967 * list.
3968 */
David Barksdale41c57a82007-01-30 14:36:25 -08003969 INIT_LIST_HEAD(&timeouts);
Corey Minyard393d2cc2005-11-07 00:59:54 -08003970 spin_lock_irqsave(&intf->seq_lock, flags);
Corey Minyardbca03242006-12-06 20:40:57 -08003971 for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++)
3972 check_msg_timeout(intf, &(intf->seq_table[i]),
3973 &timeouts, timeout_period, i,
Corey Minyard393d2cc2005-11-07 00:59:54 -08003974 &flags);
3975 spin_unlock_irqrestore(&intf->seq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Corey Minyard393d2cc2005-11-07 00:59:54 -08003977 list_for_each_entry_safe(msg, msg2, &timeouts, link)
Corey Minyardb2c03942006-12-06 20:41:00 -08003978 deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE);
Corey Minyardb9675132006-12-06 20:41:02 -08003979
3980 /*
3981 * Maintenance mode handling. Check the timeout
3982 * optimistically before we claim the lock. It may
3983 * mean a timeout gets missed occasionally, but that
3984 * only means the timeout gets extended by one period
3985 * in that case. No big deal, and it avoids the lock
3986 * most of the time.
3987 */
3988 if (intf->auto_maintenance_timeout > 0) {
3989 spin_lock_irqsave(&intf->maintenance_mode_lock, flags);
3990 if (intf->auto_maintenance_timeout > 0) {
3991 intf->auto_maintenance_timeout
3992 -= timeout_period;
3993 if (!intf->maintenance_mode
Corey Minyardc70d7492008-04-29 01:01:09 -07003994 && (intf->auto_maintenance_timeout <= 0)) {
Corey Minyardb9675132006-12-06 20:41:02 -08003995 intf->maintenance_mode_enable = 0;
3996 maintenance_mode_update(intf);
3997 }
3998 }
3999 spin_unlock_irqrestore(&intf->maintenance_mode_lock,
4000 flags);
4001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
Corey Minyardbca03242006-12-06 20:40:57 -08004003 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
4006static void ipmi_request_event(void)
4007{
Corey Minyardb2c03942006-12-06 20:41:00 -08004008 ipmi_smi_t intf;
4009 struct ipmi_smi_handlers *handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Corey Minyardbca03242006-12-06 20:40:57 -08004011 rcu_read_lock();
Corey Minyardc70d7492008-04-29 01:01:09 -07004012 /*
4013 * Called from the timer, no need to check if handlers is
4014 * valid.
4015 */
Corey Minyardb2c03942006-12-06 20:41:00 -08004016 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb9675132006-12-06 20:41:02 -08004017 /* No event requests when in maintenance mode. */
4018 if (intf->maintenance_mode_enable)
4019 continue;
4020
Corey Minyardb2c03942006-12-06 20:41:00 -08004021 handlers = intf->handlers;
4022 if (handlers)
4023 handlers->request_events(intf->send_info);
4024 }
Corey Minyardbca03242006-12-06 20:40:57 -08004025 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
4028static struct timer_list ipmi_timer;
4029
4030/* Call every ~100 ms. */
4031#define IPMI_TIMEOUT_TIME 100
4032
4033/* How many jiffies does it take to get to the timeout time. */
4034#define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000)
4035
Corey Minyardc70d7492008-04-29 01:01:09 -07004036/*
4037 * Request events from the queue every second (this is the number of
4038 * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the
4039 * future, IPMI will add a way to know immediately if an event is in
4040 * the queue and this silliness can go away.
4041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
4043
Corey Minyard8f43f842005-06-23 22:01:40 -07004044static atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045static unsigned int ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4046
4047static void ipmi_timeout(unsigned long data)
4048{
Corey Minyard8f43f842005-06-23 22:01:40 -07004049 if (atomic_read(&stop_operation))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 ticks_to_req_ev--;
4053 if (ticks_to_req_ev == 0) {
4054 ipmi_request_event();
4055 ticks_to_req_ev = IPMI_REQUEST_EV_TIME;
4056 }
4057
4058 ipmi_timeout_handler(IPMI_TIMEOUT_TIME);
4059
Corey Minyard8f43f842005-06-23 22:01:40 -07004060 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
4063
4064static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0);
4065static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0);
4066
4067/* FIXME - convert these to slabs. */
4068static void free_smi_msg(struct ipmi_smi_msg *msg)
4069{
4070 atomic_dec(&smi_msg_inuse_count);
4071 kfree(msg);
4072}
4073
4074struct ipmi_smi_msg *ipmi_alloc_smi_msg(void)
4075{
4076 struct ipmi_smi_msg *rv;
4077 rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC);
4078 if (rv) {
4079 rv->done = free_smi_msg;
4080 rv->user_data = NULL;
4081 atomic_inc(&smi_msg_inuse_count);
4082 }
4083 return rv;
4084}
Corey Minyardc70d7492008-04-29 01:01:09 -07004085EXPORT_SYMBOL(ipmi_alloc_smi_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
4087static void free_recv_msg(struct ipmi_recv_msg *msg)
4088{
4089 atomic_dec(&recv_msg_inuse_count);
4090 kfree(msg);
4091}
4092
Adrian Bunk74006302008-04-29 01:01:14 -07004093static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
4095 struct ipmi_recv_msg *rv;
4096
4097 rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC);
4098 if (rv) {
Corey Minyarda9eec552006-08-31 21:27:45 -07004099 rv->user = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 rv->done = free_recv_msg;
4101 atomic_inc(&recv_msg_inuse_count);
4102 }
4103 return rv;
4104}
4105
Corey Minyard393d2cc2005-11-07 00:59:54 -08004106void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4107{
4108 if (msg->user)
4109 kref_put(&msg->user->refcount, free_user);
4110 msg->done(msg);
4111}
Corey Minyardc70d7492008-04-29 01:01:09 -07004112EXPORT_SYMBOL(ipmi_free_recv_msg);
Corey Minyard393d2cc2005-11-07 00:59:54 -08004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114#ifdef CONFIG_IPMI_PANIC_EVENT
4115
4116static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
4117{
4118}
4119
4120static void dummy_recv_done_handler(struct ipmi_recv_msg *msg)
4121{
4122}
4123
4124#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyard56a55ec2005-09-06 15:18:42 -07004125static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004127 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4128 && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE)
4129 && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004130 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 /* A get event receiver command, save it. */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004132 intf->event_receiver = msg->msg.data[1];
4133 intf->event_receiver_lun = msg->msg.data[2] & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135}
4136
Corey Minyard56a55ec2005-09-06 15:18:42 -07004137static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Corey Minyard56a55ec2005-09-06 15:18:42 -07004139 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
4140 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
4141 && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD)
Corey Minyardc70d7492008-04-29 01:01:09 -07004142 && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) {
4143 /*
4144 * A get device id command, save if we are an event
4145 * receiver or generator.
4146 */
Corey Minyard56a55ec2005-09-06 15:18:42 -07004147 intf->local_sel_device = (msg->msg.data[6] >> 2) & 1;
4148 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
4150}
4151#endif
4152
4153static void send_panic_events(char *str)
4154{
4155 struct kernel_ipmi_msg msg;
4156 ipmi_smi_t intf;
4157 unsigned char data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct ipmi_system_interface_addr *si;
4159 struct ipmi_addr addr;
4160 struct ipmi_smi_msg smi_msg;
4161 struct ipmi_recv_msg recv_msg;
4162
4163 si = (struct ipmi_system_interface_addr *) &addr;
4164 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4165 si->channel = IPMI_BMC_CHANNEL;
4166 si->lun = 0;
4167
4168 /* Fill in an event telling that we have failed. */
4169 msg.netfn = 0x04; /* Sensor or Event. */
4170 msg.cmd = 2; /* Platform event command. */
4171 msg.data = data;
4172 msg.data_len = 8;
Matt Domschcda315a2005-12-12 00:37:32 -08004173 data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 data[1] = 0x03; /* This is for IPMI 1.0. */
4175 data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */
4176 data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */
4177 data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */
4178
Corey Minyardc70d7492008-04-29 01:01:09 -07004179 /*
4180 * Put a few breadcrumbs in. Hopefully later we can add more things
4181 * to make the panic events more useful.
4182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 if (str) {
4184 data[3] = str[0];
4185 data[6] = str[1];
4186 data[7] = str[2];
4187 }
4188
4189 smi_msg.done = dummy_smi_done_handler;
4190 recv_msg.done = dummy_recv_done_handler;
4191
4192 /* For every registered interface, send the event. */
Corey Minyardbca03242006-12-06 20:40:57 -08004193 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004194 if (!intf->handlers)
4195 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 continue;
4197
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004198 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /* Send the event announcing the panic. */
4200 intf->handlers->set_run_to_completion(intf->send_info, 1);
4201 i_ipmi_request(NULL,
4202 intf,
4203 &addr,
4204 0,
4205 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004206 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 &smi_msg,
4208 &recv_msg,
4209 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004210 intf->channels[0].address,
4211 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 0, 1); /* Don't retry, and don't wait. */
4213 }
4214
4215#ifdef CONFIG_IPMI_PANIC_STRING
Corey Minyardc70d7492008-04-29 01:01:09 -07004216 /*
4217 * On every interface, dump a bunch of OEM event holding the
4218 * string.
4219 */
4220 if (!str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return;
4222
Corey Minyardbca03242006-12-06 20:40:57 -08004223 /* For every registered interface, send the event. */
4224 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 char *p = str;
4226 struct ipmi_ipmb_addr *ipmb;
4227 int j;
4228
Corey Minyardbca03242006-12-06 20:40:57 -08004229 if (intf->intf_num == -1)
4230 /* Interface was not ready yet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 continue;
4232
Corey Minyard78ba2fa2007-02-10 01:45:45 -08004233 /*
4234 * intf_num is used as an marker to tell if the
4235 * interface is valid. Thus we need a read barrier to
4236 * make sure data fetched before checking intf_num
4237 * won't be used.
4238 */
4239 smp_rmb();
4240
Corey Minyardc70d7492008-04-29 01:01:09 -07004241 /*
4242 * First job here is to figure out where to send the
4243 * OEM events. There's no way in IPMI to send OEM
4244 * events using an event send command, so we have to
4245 * find the SEL to put them in and stick them in
4246 * there.
4247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 /* Get capabilities from the get device id. */
4250 intf->local_sel_device = 0;
4251 intf->local_event_generator = 0;
4252 intf->event_receiver = 0;
4253
4254 /* Request the device info from the local MC. */
4255 msg.netfn = IPMI_NETFN_APP_REQUEST;
4256 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
4257 msg.data = NULL;
4258 msg.data_len = 0;
4259 intf->null_user_handler = device_id_fetcher;
4260 i_ipmi_request(NULL,
4261 intf,
4262 &addr,
4263 0,
4264 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004265 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 &smi_msg,
4267 &recv_msg,
4268 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004269 intf->channels[0].address,
4270 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 0, 1); /* Don't retry, and don't wait. */
4272
4273 if (intf->local_event_generator) {
4274 /* Request the event receiver from the local MC. */
4275 msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST;
4276 msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
4277 msg.data = NULL;
4278 msg.data_len = 0;
4279 intf->null_user_handler = event_receiver_fetcher;
4280 i_ipmi_request(NULL,
4281 intf,
4282 &addr,
4283 0,
4284 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004285 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 &smi_msg,
4287 &recv_msg,
4288 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004289 intf->channels[0].address,
4290 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 0, 1); /* no retry, and no wait. */
4292 }
4293 intf->null_user_handler = NULL;
4294
Corey Minyardc70d7492008-04-29 01:01:09 -07004295 /*
4296 * Validate the event receiver. The low bit must not
4297 * be 1 (it must be a valid IPMB address), it cannot
4298 * be zero, and it must not be my address.
4299 */
4300 if (((intf->event_receiver & 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 && (intf->event_receiver != 0)
Corey Minyardc70d7492008-04-29 01:01:09 -07004302 && (intf->event_receiver != intf->channels[0].address)) {
4303 /*
4304 * The event receiver is valid, send an IPMB
4305 * message.
4306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 ipmb = (struct ipmi_ipmb_addr *) &addr;
4308 ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
4309 ipmb->channel = 0; /* FIXME - is this right? */
4310 ipmb->lun = intf->event_receiver_lun;
4311 ipmb->slave_addr = intf->event_receiver;
4312 } else if (intf->local_sel_device) {
Corey Minyardc70d7492008-04-29 01:01:09 -07004313 /*
4314 * The event receiver was not valid (or was
4315 * me), but I am an SEL device, just dump it
4316 * in my SEL.
4317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 si = (struct ipmi_system_interface_addr *) &addr;
4319 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4320 si->channel = IPMI_BMC_CHANNEL;
4321 si->lun = 0;
4322 } else
4323 continue; /* No where to send the event. */
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */
4326 msg.cmd = IPMI_ADD_SEL_ENTRY_CMD;
4327 msg.data = data;
4328 msg.data_len = 16;
4329
4330 j = 0;
4331 while (*p) {
4332 int size = strlen(p);
4333
4334 if (size > 11)
4335 size = 11;
4336 data[0] = 0;
4337 data[1] = 0;
4338 data[2] = 0xf0; /* OEM event without timestamp. */
Corey Minyardc14979b2005-09-06 15:18:38 -07004339 data[3] = intf->channels[0].address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 data[4] = j++; /* sequence # */
Corey Minyardc70d7492008-04-29 01:01:09 -07004341 /*
4342 * Always give 11 bytes, so strncpy will fill
4343 * it with zeroes for me.
4344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 strncpy(data+5, p, 11);
4346 p += size;
4347
4348 i_ipmi_request(NULL,
4349 intf,
4350 &addr,
4351 0,
4352 &msg,
Corey Minyard56a55ec2005-09-06 15:18:42 -07004353 intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 &smi_msg,
4355 &recv_msg,
4356 0,
Corey Minyardc14979b2005-09-06 15:18:38 -07004357 intf->channels[0].address,
4358 intf->channels[0].lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 0, 1); /* no retry, and no wait. */
4360 }
Corey Minyardc70d7492008-04-29 01:01:09 -07004361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362#endif /* CONFIG_IPMI_PANIC_STRING */
4363}
4364#endif /* CONFIG_IPMI_PANIC_EVENT */
4365
Randy Dunlap0c8204b2006-12-10 02:19:06 -08004366static int has_panicked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368static int panic_event(struct notifier_block *this,
4369 unsigned long event,
Corey Minyardc70d7492008-04-29 01:01:09 -07004370 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 ipmi_smi_t intf;
4373
Lee Revellf18190b2006-06-26 18:30:00 +02004374 if (has_panicked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return NOTIFY_DONE;
Lee Revellf18190b2006-06-26 18:30:00 +02004376 has_panicked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
4378 /* For every registered interface, set it to run to completion. */
Corey Minyardbca03242006-12-06 20:40:57 -08004379 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
Corey Minyardb2c03942006-12-06 20:41:00 -08004380 if (!intf->handlers)
4381 /* Interface is not ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 continue;
4383
Konstantin Baydarov5956dce2008-04-29 01:01:03 -07004384 intf->run_to_completion = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 intf->handlers->set_run_to_completion(intf->send_info, 1);
4386 }
4387
4388#ifdef CONFIG_IPMI_PANIC_EVENT
4389 send_panic_events(ptr);
4390#endif
4391
4392 return NOTIFY_DONE;
4393}
4394
4395static struct notifier_block panic_block = {
4396 .notifier_call = panic_event,
4397 .next = NULL,
4398 .priority = 200 /* priority: INT_MAX >= x >= 0 */
4399};
4400
4401static int ipmi_init_msghandler(void)
4402{
Corey Minyard50c812b2006-03-26 01:37:21 -08004403 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 if (initialized)
4406 return 0;
4407
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004408 rv = driver_register(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004409 if (rv) {
4410 printk(KERN_ERR PFX "Could not register IPMI driver\n");
4411 return rv;
4412 }
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 printk(KERN_INFO "ipmi message handler version "
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004415 IPMI_DRIVER_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Corey Minyard3b625942005-06-23 22:01:42 -07004417#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4419 if (!proc_ipmi_root) {
4420 printk(KERN_ERR PFX "Unable to create IPMI proc dir");
4421 return -ENOMEM;
4422 }
4423
Corey Minyard3b625942005-06-23 22:01:42 -07004424#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Corey Minyard409035e2006-06-28 04:26:53 -07004426 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4427 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Alan Sterne041c682006-03-27 01:16:30 -08004429 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 initialized = 1;
4432
4433 return 0;
4434}
4435
4436static __init int ipmi_init_msghandler_mod(void)
4437{
4438 ipmi_init_msghandler();
4439 return 0;
4440}
4441
4442static __exit void cleanup_ipmi(void)
4443{
4444 int count;
4445
4446 if (!initialized)
4447 return;
4448
Alan Sterne041c682006-03-27 01:16:30 -08004449 atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Corey Minyardc70d7492008-04-29 01:01:09 -07004451 /*
4452 * This can't be called if any interfaces exist, so no worry
4453 * about shutting down the interfaces.
4454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Corey Minyardc70d7492008-04-29 01:01:09 -07004456 /*
4457 * Tell the timer to stop, then wait for it to stop. This
4458 * avoids problems with race conditions removing the timer
4459 * here.
4460 */
Corey Minyard8f43f842005-06-23 22:01:40 -07004461 atomic_inc(&stop_operation);
4462 del_timer_sync(&ipmi_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Corey Minyard3b625942005-06-23 22:01:42 -07004464#ifdef CONFIG_PROC_FS
Alexey Dobriyan3542ae42007-10-16 23:26:50 -07004465 remove_proc_entry(proc_ipmi_root->name, NULL);
Corey Minyard3b625942005-06-23 22:01:42 -07004466#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08004468 driver_unregister(&ipmidriver.driver);
Corey Minyard50c812b2006-03-26 01:37:21 -08004469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 initialized = 0;
4471
4472 /* Check for buffer leaks. */
4473 count = atomic_read(&smi_msg_inuse_count);
4474 if (count != 0)
4475 printk(KERN_WARNING PFX "SMI message count %d at exit\n",
4476 count);
4477 count = atomic_read(&recv_msg_inuse_count);
4478 if (count != 0)
4479 printk(KERN_WARNING PFX "recv message count %d at exit\n",
4480 count);
4481}
4482module_exit(cleanup_ipmi);
4483
4484module_init(ipmi_init_msghandler_mod);
4485MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004486MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc70d7492008-04-29 01:01:09 -07004487MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI"
4488 " interface.");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07004489MODULE_VERSION(IPMI_DRIVER_VERSION);