]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
34 | #include <linux/module.h> |
35 | #include <linux/errno.h> | |
1da177e4 | 36 | #include <linux/poll.h> |
a99bbaf5 | 37 | #include <linux/sched.h> |
07412736 | 38 | #include <linux/seq_file.h> |
1da177e4 | 39 | #include <linux/spinlock.h> |
d6dfd131 | 40 | #include <linux/mutex.h> |
1da177e4 LT |
41 | #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> | |
393d2cc3 | 47 | #include <linux/rcupdate.h> |
7adf579c | 48 | #include <linux/interrupt.h> |
1da177e4 LT |
49 | |
50 | #define PFX "IPMI message handler: " | |
1fdd75bd | 51 | |
f7caa1b5 | 52 | #define IPMI_DRIVER_VERSION "39.2" |
1da177e4 LT |
53 | |
54 | static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void); | |
55 | static int ipmi_init_msghandler(void); | |
7adf579c CM |
56 | static void smi_recv_tasklet(unsigned long); |
57 | static void handle_new_recv_msgs(ipmi_smi_t intf); | |
89986496 | 58 | static void need_waiter(ipmi_smi_t intf); |
7ea0ed2b CM |
59 | static int handle_one_recv_msg(ipmi_smi_t intf, |
60 | struct ipmi_smi_msg *msg); | |
1da177e4 | 61 | |
0c8204b3 | 62 | static int initialized; |
1da177e4 | 63 | |
3b625943 | 64 | #ifdef CONFIG_PROC_FS |
0c8204b3 | 65 | static struct proc_dir_entry *proc_ipmi_root; |
3b625943 | 66 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 67 | |
b9675136 CM |
68 | /* Remain in auto-maintenance mode for this amount of time (in ms). */ |
69 | #define IPMI_MAINTENANCE_MODE_TIMEOUT 30000 | |
70 | ||
1da177e4 LT |
71 | #define MAX_EVENTS_IN_QUEUE 25 |
72 | ||
c70d7499 CM |
73 | /* |
74 | * Don't let a message sit in a queue forever, always time it with at lest | |
75 | * the max message timer. This is in milliseconds. | |
76 | */ | |
1da177e4 LT |
77 | #define MAX_MSG_TIMEOUT 60000 |
78 | ||
89986496 CM |
79 | /* Call every ~1000 ms. */ |
80 | #define IPMI_TIMEOUT_TIME 1000 | |
81 | ||
82 | /* How many jiffies does it take to get to the timeout time. */ | |
83 | #define IPMI_TIMEOUT_JIFFIES ((IPMI_TIMEOUT_TIME * HZ) / 1000) | |
84 | ||
85 | /* | |
86 | * Request events from the queue every second (this is the number of | |
87 | * IPMI_TIMEOUT_TIMES between event requests). Hopefully, in the | |
88 | * future, IPMI will add a way to know immediately if an event is in | |
89 | * the queue and this silliness can go away. | |
90 | */ | |
91 | #define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME)) | |
92 | ||
393d2cc3 CM |
93 | /* |
94 | * The main "user" data structure. | |
95 | */ | |
c70d7499 | 96 | struct ipmi_user { |
1da177e4 LT |
97 | struct list_head link; |
98 | ||
7aefac26 CM |
99 | /* Set to false when the user is destroyed. */ |
100 | bool valid; | |
393d2cc3 CM |
101 | |
102 | struct kref refcount; | |
103 | ||
1da177e4 | 104 | /* The upper layer that handles receive messages. */ |
210af2a5 | 105 | const struct ipmi_user_hndl *handler; |
1da177e4 LT |
106 | void *handler_data; |
107 | ||
108 | /* The interface this user is bound to. */ | |
109 | ipmi_smi_t intf; | |
110 | ||
111 | /* Does this interface receive IPMI events? */ | |
89986496 | 112 | bool gets_events; |
1da177e4 LT |
113 | }; |
114 | ||
c70d7499 | 115 | struct cmd_rcvr { |
1da177e4 LT |
116 | struct list_head link; |
117 | ||
118 | ipmi_user_t user; | |
119 | unsigned char netfn; | |
120 | unsigned char cmd; | |
c69c3127 | 121 | unsigned int chans; |
393d2cc3 CM |
122 | |
123 | /* | |
124 | * This is used to form a linked lised during mass deletion. | |
125 | * Since this is in an RCU list, we cannot use the link above | |
126 | * or change any data until the RCU period completes. So we | |
127 | * use this next variable during mass deletion so we can have | |
128 | * a list and don't have to wait and restart the search on | |
c70d7499 CM |
129 | * every individual deletion of a command. |
130 | */ | |
393d2cc3 | 131 | struct cmd_rcvr *next; |
1da177e4 LT |
132 | }; |
133 | ||
c70d7499 | 134 | struct seq_table { |
1da177e4 LT |
135 | unsigned int inuse : 1; |
136 | unsigned int broadcast : 1; | |
137 | ||
138 | unsigned long timeout; | |
139 | unsigned long orig_timeout; | |
140 | unsigned int retries_left; | |
141 | ||
c70d7499 CM |
142 | /* |
143 | * To verify on an incoming send message response that this is | |
144 | * the message that the response is for, we keep a sequence id | |
145 | * and increment it every time we send a message. | |
146 | */ | |
1da177e4 LT |
147 | long seqid; |
148 | ||
c70d7499 CM |
149 | /* |
150 | * This is held so we can properly respond to the message on a | |
151 | * timeout, and it is used to hold the temporary data for | |
152 | * retransmission, too. | |
153 | */ | |
1da177e4 LT |
154 | struct ipmi_recv_msg *recv_msg; |
155 | }; | |
156 | ||
c70d7499 CM |
157 | /* |
158 | * Store the information in a msgid (long) to allow us to find a | |
159 | * sequence table entry from the msgid. | |
160 | */ | |
a24b5dd5 CM |
161 | #define STORE_SEQ_IN_MSGID(seq, seqid) \ |
162 | ((((seq) & 0x3f) << 26) | ((seqid) & 0x3ffffff)) | |
1da177e4 LT |
163 | |
164 | #define GET_SEQ_FROM_MSGID(msgid, seq, seqid) \ | |
165 | do { \ | |
a24b5dd5 CM |
166 | seq = (((msgid) >> 26) & 0x3f); \ |
167 | seqid = ((msgid) & 0x3ffffff); \ | |
c70d7499 | 168 | } while (0) |
1da177e4 | 169 | |
a24b5dd5 | 170 | #define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff) |
1da177e4 | 171 | |
c70d7499 | 172 | struct ipmi_channel { |
1da177e4 LT |
173 | unsigned char medium; |
174 | unsigned char protocol; | |
c14979b9 | 175 | |
c70d7499 CM |
176 | /* |
177 | * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR, | |
178 | * but may be changed by the user. | |
179 | */ | |
c14979b9 CM |
180 | unsigned char address; |
181 | ||
c70d7499 CM |
182 | /* |
183 | * My LUN. This should generally stay the SMS LUN, but just in | |
184 | * case... | |
185 | */ | |
c14979b9 | 186 | unsigned char lun; |
1da177e4 LT |
187 | }; |
188 | ||
3b625943 | 189 | #ifdef CONFIG_PROC_FS |
c70d7499 | 190 | struct ipmi_proc_entry { |
1da177e4 LT |
191 | char *name; |
192 | struct ipmi_proc_entry *next; | |
193 | }; | |
3b625943 | 194 | #endif |
1da177e4 | 195 | |
c70d7499 | 196 | struct bmc_device { |
16639eb0 | 197 | struct platform_device pdev; |
50c812b2 CM |
198 | struct ipmi_device_id id; |
199 | unsigned char guid[16]; | |
200 | int guid_set; | |
16639eb0 CM |
201 | char name[16]; |
202 | struct kref usecount; | |
50c812b2 | 203 | }; |
16639eb0 | 204 | #define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev) |
50c812b2 | 205 | |
b2655f26 KB |
206 | /* |
207 | * Various statistics for IPMI, these index stats[] in the ipmi_smi | |
208 | * structure. | |
209 | */ | |
73f2bdb9 CM |
210 | enum ipmi_stat_indexes { |
211 | /* Commands we got from the user that were invalid. */ | |
212 | IPMI_STAT_sent_invalid_commands = 0, | |
b2655f26 | 213 | |
73f2bdb9 CM |
214 | /* Commands we sent to the MC. */ |
215 | IPMI_STAT_sent_local_commands, | |
b2655f26 | 216 | |
73f2bdb9 CM |
217 | /* Responses from the MC that were delivered to a user. */ |
218 | IPMI_STAT_handled_local_responses, | |
b2655f26 | 219 | |
73f2bdb9 CM |
220 | /* Responses from the MC that were not delivered to a user. */ |
221 | IPMI_STAT_unhandled_local_responses, | |
b2655f26 | 222 | |
73f2bdb9 CM |
223 | /* Commands we sent out to the IPMB bus. */ |
224 | IPMI_STAT_sent_ipmb_commands, | |
b2655f26 | 225 | |
73f2bdb9 CM |
226 | /* Commands sent on the IPMB that had errors on the SEND CMD */ |
227 | IPMI_STAT_sent_ipmb_command_errs, | |
b2655f26 | 228 | |
73f2bdb9 CM |
229 | /* Each retransmit increments this count. */ |
230 | IPMI_STAT_retransmitted_ipmb_commands, | |
b2655f26 | 231 | |
73f2bdb9 CM |
232 | /* |
233 | * When a message times out (runs out of retransmits) this is | |
234 | * incremented. | |
235 | */ | |
236 | IPMI_STAT_timed_out_ipmb_commands, | |
b2655f26 | 237 | |
73f2bdb9 CM |
238 | /* |
239 | * This is like above, but for broadcasts. Broadcasts are | |
240 | * *not* included in the above count (they are expected to | |
241 | * time out). | |
242 | */ | |
243 | IPMI_STAT_timed_out_ipmb_broadcasts, | |
b2655f26 | 244 | |
73f2bdb9 CM |
245 | /* Responses I have sent to the IPMB bus. */ |
246 | IPMI_STAT_sent_ipmb_responses, | |
b2655f26 | 247 | |
73f2bdb9 CM |
248 | /* The response was delivered to the user. */ |
249 | IPMI_STAT_handled_ipmb_responses, | |
b2655f26 | 250 | |
73f2bdb9 CM |
251 | /* The response had invalid data in it. */ |
252 | IPMI_STAT_invalid_ipmb_responses, | |
b2655f26 | 253 | |
73f2bdb9 CM |
254 | /* The response didn't have anyone waiting for it. */ |
255 | IPMI_STAT_unhandled_ipmb_responses, | |
b2655f26 | 256 | |
73f2bdb9 CM |
257 | /* Commands we sent out to the IPMB bus. */ |
258 | IPMI_STAT_sent_lan_commands, | |
b2655f26 | 259 | |
73f2bdb9 CM |
260 | /* Commands sent on the IPMB that had errors on the SEND CMD */ |
261 | IPMI_STAT_sent_lan_command_errs, | |
b2655f26 | 262 | |
73f2bdb9 CM |
263 | /* Each retransmit increments this count. */ |
264 | IPMI_STAT_retransmitted_lan_commands, | |
b2655f26 | 265 | |
73f2bdb9 CM |
266 | /* |
267 | * When a message times out (runs out of retransmits) this is | |
268 | * incremented. | |
269 | */ | |
270 | IPMI_STAT_timed_out_lan_commands, | |
271 | ||
272 | /* Responses I have sent to the IPMB bus. */ | |
273 | IPMI_STAT_sent_lan_responses, | |
b2655f26 | 274 | |
73f2bdb9 CM |
275 | /* The response was delivered to the user. */ |
276 | IPMI_STAT_handled_lan_responses, | |
b2655f26 | 277 | |
73f2bdb9 CM |
278 | /* The response had invalid data in it. */ |
279 | IPMI_STAT_invalid_lan_responses, | |
b2655f26 | 280 | |
73f2bdb9 CM |
281 | /* The response didn't have anyone waiting for it. */ |
282 | IPMI_STAT_unhandled_lan_responses, | |
b2655f26 | 283 | |
73f2bdb9 CM |
284 | /* The command was delivered to the user. */ |
285 | IPMI_STAT_handled_commands, | |
b2655f26 | 286 | |
73f2bdb9 CM |
287 | /* The command had invalid data in it. */ |
288 | IPMI_STAT_invalid_commands, | |
b2655f26 | 289 | |
73f2bdb9 CM |
290 | /* The command didn't have anyone waiting for it. */ |
291 | IPMI_STAT_unhandled_commands, | |
b2655f26 | 292 | |
73f2bdb9 CM |
293 | /* Invalid data in an event. */ |
294 | IPMI_STAT_invalid_events, | |
b2655f26 | 295 | |
73f2bdb9 CM |
296 | /* Events that were received with the proper format. */ |
297 | IPMI_STAT_events, | |
b2655f26 | 298 | |
25176ed6 CM |
299 | /* Retransmissions on IPMB that failed. */ |
300 | IPMI_STAT_dropped_rexmit_ipmb_commands, | |
301 | ||
302 | /* Retransmissions on LAN that failed. */ | |
303 | IPMI_STAT_dropped_rexmit_lan_commands, | |
b2655f26 | 304 | |
73f2bdb9 CM |
305 | /* This *must* remain last, add new values above this. */ |
306 | IPMI_NUM_STATS | |
307 | }; | |
b2655f26 KB |
308 | |
309 | ||
1da177e4 | 310 | #define IPMI_IPMB_NUM_SEQ 64 |
c14979b9 | 311 | #define IPMI_MAX_CHANNELS 16 |
c70d7499 | 312 | struct ipmi_smi { |
1da177e4 LT |
313 | /* What interface number are we? */ |
314 | int intf_num; | |
315 | ||
393d2cc3 CM |
316 | struct kref refcount; |
317 | ||
7ea0ed2b CM |
318 | /* Set when the interface is being unregistered. */ |
319 | bool in_shutdown; | |
320 | ||
bca0324d CM |
321 | /* Used for a list of interfaces. */ |
322 | struct list_head link; | |
323 | ||
c70d7499 CM |
324 | /* |
325 | * The list of upper layers that are using me. seq_lock | |
326 | * protects this. | |
327 | */ | |
393d2cc3 | 328 | struct list_head users; |
1da177e4 | 329 | |
b2c03941 CM |
330 | /* Information to supply to users. */ |
331 | unsigned char ipmi_version_major; | |
332 | unsigned char ipmi_version_minor; | |
333 | ||
1da177e4 LT |
334 | /* Used for wake ups at startup. */ |
335 | wait_queue_head_t waitq; | |
336 | ||
50c812b2 CM |
337 | struct bmc_device *bmc; |
338 | char *my_dev_name; | |
1da177e4 | 339 | |
c70d7499 CM |
340 | /* |
341 | * This is the lower-layer's sender routine. Note that you | |
b2c03941 CM |
342 | * must either be holding the ipmi_interfaces_mutex or be in |
343 | * an umpreemptible region to use this. You must fetch the | |
c70d7499 CM |
344 | * value into a local variable and make sure it is not NULL. |
345 | */ | |
81d02b7f | 346 | const struct ipmi_smi_handlers *handlers; |
1da177e4 LT |
347 | void *send_info; |
348 | ||
3b625943 | 349 | #ifdef CONFIG_PROC_FS |
ac019151 CM |
350 | /* A list of proc entries for this interface. */ |
351 | struct mutex proc_entry_lock; | |
1da177e4 | 352 | struct ipmi_proc_entry *proc_entries; |
3b625943 | 353 | #endif |
1da177e4 | 354 | |
50c812b2 CM |
355 | /* Driver-model device for the system interface. */ |
356 | struct device *si_dev; | |
357 | ||
c70d7499 CM |
358 | /* |
359 | * A table of sequence numbers for this interface. We use the | |
360 | * sequence numbers for IPMB messages that go out of the | |
361 | * interface to match them up with their responses. A routine | |
362 | * is called periodically to time the items in this list. | |
363 | */ | |
1da177e4 LT |
364 | spinlock_t seq_lock; |
365 | struct seq_table seq_table[IPMI_IPMB_NUM_SEQ]; | |
366 | int curr_seq; | |
367 | ||
c70d7499 | 368 | /* |
7adf579c CM |
369 | * Messages queued for delivery. If delivery fails (out of memory |
370 | * for instance), They will stay in here to be processed later in a | |
371 | * periodic timer interrupt. The tasklet is for handling received | |
372 | * messages directly from the handler. | |
c70d7499 | 373 | */ |
65be7544 CM |
374 | spinlock_t waiting_rcv_msgs_lock; |
375 | struct list_head waiting_rcv_msgs; | |
7adf579c CM |
376 | atomic_t watchdog_pretimeouts_to_deliver; |
377 | struct tasklet_struct recv_tasklet; | |
1da177e4 | 378 | |
7ea0ed2b CM |
379 | spinlock_t xmit_msgs_lock; |
380 | struct list_head xmit_msgs; | |
381 | struct ipmi_smi_msg *curr_msg; | |
382 | struct list_head hp_xmit_msgs; | |
383 | ||
c70d7499 CM |
384 | /* |
385 | * The list of command receivers that are registered for commands | |
386 | * on this interface. | |
387 | */ | |
d6dfd131 | 388 | struct mutex cmd_rcvrs_mutex; |
1da177e4 LT |
389 | struct list_head cmd_rcvrs; |
390 | ||
c70d7499 CM |
391 | /* |
392 | * Events that were queues because no one was there to receive | |
393 | * them. | |
394 | */ | |
1da177e4 LT |
395 | spinlock_t events_lock; /* For dealing with event stuff. */ |
396 | struct list_head waiting_events; | |
397 | unsigned int waiting_events_count; /* How many events in queue? */ | |
87ebd06f CM |
398 | char delivering_events; |
399 | char event_msg_printed; | |
89986496 CM |
400 | atomic_t event_waiters; |
401 | unsigned int ticks_to_req_ev; | |
402 | int last_needs_timer; | |
1da177e4 | 403 | |
c70d7499 CM |
404 | /* |
405 | * The event receiver for my BMC, only really used at panic | |
406 | * shutdown as a place to store this. | |
407 | */ | |
1da177e4 LT |
408 | unsigned char event_receiver; |
409 | unsigned char event_receiver_lun; | |
410 | unsigned char local_sel_device; | |
411 | unsigned char local_event_generator; | |
412 | ||
b9675136 CM |
413 | /* For handling of maintenance mode. */ |
414 | int maintenance_mode; | |
7aefac26 | 415 | bool maintenance_mode_enable; |
b9675136 CM |
416 | int auto_maintenance_timeout; |
417 | spinlock_t maintenance_mode_lock; /* Used in a timer... */ | |
418 | ||
c70d7499 CM |
419 | /* |
420 | * A cheap hack, if this is non-null and a message to an | |
421 | * interface comes in with a NULL user, call this routine with | |
422 | * it. Note that the message will still be freed by the | |
423 | * caller. This only works on the system interface. | |
424 | */ | |
56a55ec6 | 425 | void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg); |
1da177e4 | 426 | |
c70d7499 CM |
427 | /* |
428 | * When we are scanning the channels for an SMI, this will | |
429 | * tell which channel we are scanning. | |
430 | */ | |
1da177e4 LT |
431 | int curr_channel; |
432 | ||
433 | /* Channel information */ | |
434 | struct ipmi_channel channels[IPMI_MAX_CHANNELS]; | |
435 | ||
436 | /* Proc FS stuff. */ | |
437 | struct proc_dir_entry *proc_dir; | |
438 | char proc_dir_name[10]; | |
439 | ||
b2655f26 | 440 | atomic_t stats[IPMI_NUM_STATS]; |
5956dce1 KB |
441 | |
442 | /* | |
443 | * run_to_completion duplicate of smb_info, smi_info | |
444 | * and ipmi_serial_info structures. Used to decrease numbers of | |
445 | * parameters passed by "low" level IPMI code. | |
446 | */ | |
447 | int run_to_completion; | |
1da177e4 | 448 | }; |
50c812b2 | 449 | #define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev) |
1da177e4 | 450 | |
50c812b2 CM |
451 | /** |
452 | * The driver model view of the IPMI messaging driver. | |
453 | */ | |
fe2d5ffc DW |
454 | static struct platform_driver ipmidriver = { |
455 | .driver = { | |
456 | .name = "ipmi", | |
457 | .bus = &platform_bus_type | |
458 | } | |
50c812b2 CM |
459 | }; |
460 | static DEFINE_MUTEX(ipmidriver_mutex); | |
461 | ||
bed9759b | 462 | static LIST_HEAD(ipmi_interfaces); |
bca0324d | 463 | static DEFINE_MUTEX(ipmi_interfaces_mutex); |
1da177e4 | 464 | |
c70d7499 CM |
465 | /* |
466 | * List of watchers that want to know when smi's are added and deleted. | |
467 | */ | |
bed9759b | 468 | static LIST_HEAD(smi_watchers); |
b2c03941 | 469 | static DEFINE_MUTEX(smi_watchers_mutex); |
1da177e4 | 470 | |
b2655f26 KB |
471 | #define ipmi_inc_stat(intf, stat) \ |
472 | atomic_inc(&(intf)->stats[IPMI_STAT_ ## stat]) | |
473 | #define ipmi_get_stat(intf, stat) \ | |
474 | ((unsigned int) atomic_read(&(intf)->stats[IPMI_STAT_ ## stat])) | |
475 | ||
99ee6735 LC |
476 | static const char * const addr_src_to_str[] = { |
477 | "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI", | |
b07b58a3 | 478 | "device-tree" |
99ee6735 | 479 | }; |
7e50387b CM |
480 | |
481 | const char *ipmi_addr_src_to_str(enum ipmi_addr_src src) | |
482 | { | |
b07b58a3 | 483 | if (src >= SI_LAST) |
7e50387b CM |
484 | src = 0; /* Invalid */ |
485 | return addr_src_to_str[src]; | |
486 | } | |
487 | EXPORT_SYMBOL(ipmi_addr_src_to_str); | |
488 | ||
25176ed6 CM |
489 | static int is_lan_addr(struct ipmi_addr *addr) |
490 | { | |
491 | return addr->addr_type == IPMI_LAN_ADDR_TYPE; | |
492 | } | |
493 | ||
494 | static int is_ipmb_addr(struct ipmi_addr *addr) | |
495 | { | |
496 | return addr->addr_type == IPMI_IPMB_ADDR_TYPE; | |
497 | } | |
498 | ||
499 | static int is_ipmb_bcast_addr(struct ipmi_addr *addr) | |
500 | { | |
501 | return addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE; | |
502 | } | |
b2655f26 | 503 | |
393d2cc3 CM |
504 | static void free_recv_msg_list(struct list_head *q) |
505 | { | |
506 | struct ipmi_recv_msg *msg, *msg2; | |
507 | ||
508 | list_for_each_entry_safe(msg, msg2, q, link) { | |
509 | list_del(&msg->link); | |
510 | ipmi_free_recv_msg(msg); | |
511 | } | |
512 | } | |
513 | ||
f3ce6a0e CM |
514 | static void free_smi_msg_list(struct list_head *q) |
515 | { | |
516 | struct ipmi_smi_msg *msg, *msg2; | |
517 | ||
518 | list_for_each_entry_safe(msg, msg2, q, link) { | |
519 | list_del(&msg->link); | |
520 | ipmi_free_smi_msg(msg); | |
521 | } | |
522 | } | |
523 | ||
393d2cc3 CM |
524 | static void clean_up_interface_data(ipmi_smi_t intf) |
525 | { | |
526 | int i; | |
527 | struct cmd_rcvr *rcvr, *rcvr2; | |
393d2cc3 CM |
528 | struct list_head list; |
529 | ||
7adf579c CM |
530 | tasklet_kill(&intf->recv_tasklet); |
531 | ||
65be7544 | 532 | free_smi_msg_list(&intf->waiting_rcv_msgs); |
393d2cc3 CM |
533 | free_recv_msg_list(&intf->waiting_events); |
534 | ||
78ba2faf CM |
535 | /* |
536 | * Wholesale remove all the entries from the list in the | |
537 | * interface and wait for RCU to know that none are in use. | |
538 | */ | |
d6dfd131 | 539 | mutex_lock(&intf->cmd_rcvrs_mutex); |
78ba2faf CM |
540 | INIT_LIST_HEAD(&list); |
541 | list_splice_init_rcu(&intf->cmd_rcvrs, &list, synchronize_rcu); | |
d6dfd131 | 542 | mutex_unlock(&intf->cmd_rcvrs_mutex); |
393d2cc3 CM |
543 | |
544 | list_for_each_entry_safe(rcvr, rcvr2, &list, link) | |
545 | kfree(rcvr); | |
546 | ||
547 | for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { | |
548 | if ((intf->seq_table[i].inuse) | |
c70d7499 | 549 | && (intf->seq_table[i].recv_msg)) |
393d2cc3 | 550 | ipmi_free_recv_msg(intf->seq_table[i].recv_msg); |
393d2cc3 CM |
551 | } |
552 | } | |
553 | ||
554 | static void intf_free(struct kref *ref) | |
555 | { | |
556 | ipmi_smi_t intf = container_of(ref, struct ipmi_smi, refcount); | |
557 | ||
558 | clean_up_interface_data(intf); | |
559 | kfree(intf); | |
560 | } | |
561 | ||
bca0324d | 562 | struct watcher_entry { |
b2c03941 CM |
563 | int intf_num; |
564 | ipmi_smi_t intf; | |
bca0324d | 565 | struct list_head link; |
bca0324d CM |
566 | }; |
567 | ||
1da177e4 LT |
568 | int ipmi_smi_watcher_register(struct ipmi_smi_watcher *watcher) |
569 | { | |
bca0324d | 570 | ipmi_smi_t intf; |
e381d1c4 | 571 | LIST_HEAD(to_deliver); |
bca0324d CM |
572 | struct watcher_entry *e, *e2; |
573 | ||
b2c03941 CM |
574 | mutex_lock(&smi_watchers_mutex); |
575 | ||
bca0324d CM |
576 | mutex_lock(&ipmi_interfaces_mutex); |
577 | ||
b2c03941 | 578 | /* Build a list of things to deliver. */ |
78ba2faf | 579 | list_for_each_entry(intf, &ipmi_interfaces, link) { |
bca0324d CM |
580 | if (intf->intf_num == -1) |
581 | continue; | |
582 | e = kmalloc(sizeof(*e), GFP_KERNEL); | |
583 | if (!e) | |
584 | goto out_err; | |
b2c03941 CM |
585 | kref_get(&intf->refcount); |
586 | e->intf = intf; | |
bca0324d CM |
587 | e->intf_num = intf->intf_num; |
588 | list_add_tail(&e->link, &to_deliver); | |
589 | } | |
1da177e4 | 590 | |
b2c03941 CM |
591 | /* We will succeed, so add it to the list. */ |
592 | list_add(&watcher->link, &smi_watchers); | |
bca0324d CM |
593 | |
594 | mutex_unlock(&ipmi_interfaces_mutex); | |
595 | ||
596 | list_for_each_entry_safe(e, e2, &to_deliver, link) { | |
597 | list_del(&e->link); | |
b2c03941 CM |
598 | watcher->new_smi(e->intf_num, e->intf->si_dev); |
599 | kref_put(&e->intf->refcount, intf_free); | |
bca0324d | 600 | kfree(e); |
1da177e4 | 601 | } |
bca0324d | 602 | |
b2c03941 | 603 | mutex_unlock(&smi_watchers_mutex); |
bca0324d | 604 | |
1da177e4 | 605 | return 0; |
bca0324d CM |
606 | |
607 | out_err: | |
b2c03941 CM |
608 | mutex_unlock(&ipmi_interfaces_mutex); |
609 | mutex_unlock(&smi_watchers_mutex); | |
bca0324d CM |
610 | list_for_each_entry_safe(e, e2, &to_deliver, link) { |
611 | list_del(&e->link); | |
b2c03941 | 612 | kref_put(&e->intf->refcount, intf_free); |
bca0324d CM |
613 | kfree(e); |
614 | } | |
615 | return -ENOMEM; | |
1da177e4 | 616 | } |
c70d7499 | 617 | EXPORT_SYMBOL(ipmi_smi_watcher_register); |
1da177e4 LT |
618 | |
619 | int ipmi_smi_watcher_unregister(struct ipmi_smi_watcher *watcher) | |
620 | { | |
b2c03941 | 621 | mutex_lock(&smi_watchers_mutex); |
1da177e4 | 622 | list_del(&(watcher->link)); |
b2c03941 | 623 | mutex_unlock(&smi_watchers_mutex); |
1da177e4 LT |
624 | return 0; |
625 | } | |
c70d7499 | 626 | EXPORT_SYMBOL(ipmi_smi_watcher_unregister); |
1da177e4 | 627 | |
b2c03941 CM |
628 | /* |
629 | * Must be called with smi_watchers_mutex held. | |
630 | */ | |
1da177e4 | 631 | static void |
50c812b2 | 632 | call_smi_watchers(int i, struct device *dev) |
1da177e4 LT |
633 | { |
634 | struct ipmi_smi_watcher *w; | |
635 | ||
1da177e4 LT |
636 | list_for_each_entry(w, &smi_watchers, link) { |
637 | if (try_module_get(w->owner)) { | |
50c812b2 | 638 | w->new_smi(i, dev); |
1da177e4 LT |
639 | module_put(w->owner); |
640 | } | |
641 | } | |
1da177e4 LT |
642 | } |
643 | ||
644 | static int | |
645 | ipmi_addr_equal(struct ipmi_addr *addr1, struct ipmi_addr *addr2) | |
646 | { | |
647 | if (addr1->addr_type != addr2->addr_type) | |
648 | return 0; | |
649 | ||
650 | if (addr1->channel != addr2->channel) | |
651 | return 0; | |
652 | ||
653 | if (addr1->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { | |
654 | struct ipmi_system_interface_addr *smi_addr1 | |
655 | = (struct ipmi_system_interface_addr *) addr1; | |
656 | struct ipmi_system_interface_addr *smi_addr2 | |
657 | = (struct ipmi_system_interface_addr *) addr2; | |
658 | return (smi_addr1->lun == smi_addr2->lun); | |
659 | } | |
660 | ||
25176ed6 | 661 | if (is_ipmb_addr(addr1) || is_ipmb_bcast_addr(addr1)) { |
1da177e4 LT |
662 | struct ipmi_ipmb_addr *ipmb_addr1 |
663 | = (struct ipmi_ipmb_addr *) addr1; | |
664 | struct ipmi_ipmb_addr *ipmb_addr2 | |
665 | = (struct ipmi_ipmb_addr *) addr2; | |
666 | ||
667 | return ((ipmb_addr1->slave_addr == ipmb_addr2->slave_addr) | |
668 | && (ipmb_addr1->lun == ipmb_addr2->lun)); | |
669 | } | |
670 | ||
25176ed6 | 671 | if (is_lan_addr(addr1)) { |
1da177e4 LT |
672 | struct ipmi_lan_addr *lan_addr1 |
673 | = (struct ipmi_lan_addr *) addr1; | |
674 | struct ipmi_lan_addr *lan_addr2 | |
675 | = (struct ipmi_lan_addr *) addr2; | |
676 | ||
677 | return ((lan_addr1->remote_SWID == lan_addr2->remote_SWID) | |
678 | && (lan_addr1->local_SWID == lan_addr2->local_SWID) | |
679 | && (lan_addr1->session_handle | |
680 | == lan_addr2->session_handle) | |
681 | && (lan_addr1->lun == lan_addr2->lun)); | |
682 | } | |
683 | ||
684 | return 1; | |
685 | } | |
686 | ||
687 | int ipmi_validate_addr(struct ipmi_addr *addr, int len) | |
688 | { | |
c70d7499 | 689 | if (len < sizeof(struct ipmi_system_interface_addr)) |
1da177e4 | 690 | return -EINVAL; |
1da177e4 LT |
691 | |
692 | if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { | |
693 | if (addr->channel != IPMI_BMC_CHANNEL) | |
694 | return -EINVAL; | |
695 | return 0; | |
696 | } | |
697 | ||
698 | if ((addr->channel == IPMI_BMC_CHANNEL) | |
12fc1d7b | 699 | || (addr->channel >= IPMI_MAX_CHANNELS) |
1da177e4 LT |
700 | || (addr->channel < 0)) |
701 | return -EINVAL; | |
702 | ||
25176ed6 | 703 | if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) { |
c70d7499 | 704 | if (len < sizeof(struct ipmi_ipmb_addr)) |
1da177e4 | 705 | return -EINVAL; |
1da177e4 LT |
706 | return 0; |
707 | } | |
708 | ||
25176ed6 | 709 | if (is_lan_addr(addr)) { |
c70d7499 | 710 | if (len < sizeof(struct ipmi_lan_addr)) |
1da177e4 | 711 | return -EINVAL; |
1da177e4 LT |
712 | return 0; |
713 | } | |
714 | ||
715 | return -EINVAL; | |
716 | } | |
c70d7499 | 717 | EXPORT_SYMBOL(ipmi_validate_addr); |
1da177e4 LT |
718 | |
719 | unsigned int ipmi_addr_length(int addr_type) | |
720 | { | |
721 | if (addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) | |
722 | return sizeof(struct ipmi_system_interface_addr); | |
723 | ||
724 | if ((addr_type == IPMI_IPMB_ADDR_TYPE) | |
c70d7499 | 725 | || (addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE)) |
1da177e4 | 726 | return sizeof(struct ipmi_ipmb_addr); |
1da177e4 LT |
727 | |
728 | if (addr_type == IPMI_LAN_ADDR_TYPE) | |
729 | return sizeof(struct ipmi_lan_addr); | |
730 | ||
731 | return 0; | |
732 | } | |
c70d7499 | 733 | EXPORT_SYMBOL(ipmi_addr_length); |
1da177e4 LT |
734 | |
735 | static void deliver_response(struct ipmi_recv_msg *msg) | |
736 | { | |
8a3628d5 | 737 | if (!msg->user) { |
56a55ec6 | 738 | ipmi_smi_t intf = msg->user_msg_data; |
56a55ec6 CM |
739 | |
740 | /* Special handling for NULL users. */ | |
741 | if (intf->null_user_handler) { | |
742 | intf->null_user_handler(intf, msg); | |
b2655f26 | 743 | ipmi_inc_stat(intf, handled_local_responses); |
56a55ec6 CM |
744 | } else { |
745 | /* No handler, so give up. */ | |
b2655f26 | 746 | ipmi_inc_stat(intf, unhandled_local_responses); |
56a55ec6 CM |
747 | } |
748 | ipmi_free_recv_msg(msg); | |
c49c0976 HK |
749 | } else if (!oops_in_progress) { |
750 | /* | |
751 | * If we are running in the panic context, calling the | |
752 | * receive handler doesn't much meaning and has a deadlock | |
753 | * risk. At this moment, simply skip it in that case. | |
754 | */ | |
755 | ||
393d2cc3 CM |
756 | ipmi_user_t user = msg->user; |
757 | user->handler->ipmi_recv_hndl(msg, user->handler_data); | |
56a55ec6 | 758 | } |
1da177e4 LT |
759 | } |
760 | ||
b2c03941 CM |
761 | static void |
762 | deliver_err_response(struct ipmi_recv_msg *msg, int err) | |
763 | { | |
764 | msg->recv_type = IPMI_RESPONSE_RECV_TYPE; | |
765 | msg->msg_data[0] = err; | |
766 | msg->msg.netfn |= 1; /* Convert to a response. */ | |
767 | msg->msg.data_len = 1; | |
768 | msg->msg.data = msg->msg_data; | |
769 | deliver_response(msg); | |
770 | } | |
771 | ||
c70d7499 CM |
772 | /* |
773 | * Find the next sequence number not being used and add the given | |
774 | * message with the given timeout to the sequence table. This must be | |
775 | * called with the interface's seq_lock held. | |
776 | */ | |
1da177e4 LT |
777 | static int intf_next_seq(ipmi_smi_t intf, |
778 | struct ipmi_recv_msg *recv_msg, | |
779 | unsigned long timeout, | |
780 | int retries, | |
781 | int broadcast, | |
782 | unsigned char *seq, | |
783 | long *seqid) | |
784 | { | |
785 | int rv = 0; | |
786 | unsigned int i; | |
787 | ||
c70d7499 CM |
788 | for (i = intf->curr_seq; (i+1)%IPMI_IPMB_NUM_SEQ != intf->curr_seq; |
789 | i = (i+1)%IPMI_IPMB_NUM_SEQ) { | |
8a3628d5 | 790 | if (!intf->seq_table[i].inuse) |
1da177e4 LT |
791 | break; |
792 | } | |
793 | ||
8a3628d5 | 794 | if (!intf->seq_table[i].inuse) { |
1da177e4 LT |
795 | intf->seq_table[i].recv_msg = recv_msg; |
796 | ||
c70d7499 CM |
797 | /* |
798 | * Start with the maximum timeout, when the send response | |
799 | * comes in we will start the real timer. | |
800 | */ | |
1da177e4 LT |
801 | intf->seq_table[i].timeout = MAX_MSG_TIMEOUT; |
802 | intf->seq_table[i].orig_timeout = timeout; | |
803 | intf->seq_table[i].retries_left = retries; | |
804 | intf->seq_table[i].broadcast = broadcast; | |
805 | intf->seq_table[i].inuse = 1; | |
806 | intf->seq_table[i].seqid = NEXT_SEQID(intf->seq_table[i].seqid); | |
807 | *seq = i; | |
808 | *seqid = intf->seq_table[i].seqid; | |
809 | intf->curr_seq = (i+1)%IPMI_IPMB_NUM_SEQ; | |
89986496 | 810 | need_waiter(intf); |
1da177e4 LT |
811 | } else { |
812 | rv = -EAGAIN; | |
813 | } | |
c70d7499 | 814 | |
1da177e4 LT |
815 | return rv; |
816 | } | |
817 | ||
c70d7499 CM |
818 | /* |
819 | * Return the receive message for the given sequence number and | |
820 | * release the sequence number so it can be reused. Some other data | |
821 | * is passed in to be sure the message matches up correctly (to help | |
822 | * guard against message coming in after their timeout and the | |
823 | * sequence number being reused). | |
824 | */ | |
1da177e4 LT |
825 | static int intf_find_seq(ipmi_smi_t intf, |
826 | unsigned char seq, | |
827 | short channel, | |
828 | unsigned char cmd, | |
829 | unsigned char netfn, | |
830 | struct ipmi_addr *addr, | |
831 | struct ipmi_recv_msg **recv_msg) | |
832 | { | |
833 | int rv = -ENODEV; | |
834 | unsigned long flags; | |
835 | ||
836 | if (seq >= IPMI_IPMB_NUM_SEQ) | |
837 | return -EINVAL; | |
838 | ||
839 | spin_lock_irqsave(&(intf->seq_lock), flags); | |
840 | if (intf->seq_table[seq].inuse) { | |
841 | struct ipmi_recv_msg *msg = intf->seq_table[seq].recv_msg; | |
842 | ||
c70d7499 CM |
843 | if ((msg->addr.channel == channel) && (msg->msg.cmd == cmd) |
844 | && (msg->msg.netfn == netfn) | |
845 | && (ipmi_addr_equal(addr, &(msg->addr)))) { | |
1da177e4 LT |
846 | *recv_msg = msg; |
847 | intf->seq_table[seq].inuse = 0; | |
848 | rv = 0; | |
849 | } | |
850 | } | |
851 | spin_unlock_irqrestore(&(intf->seq_lock), flags); | |
852 | ||
853 | return rv; | |
854 | } | |
855 | ||
856 | ||
857 | /* Start the timer for a specific sequence table entry. */ | |
858 | static int intf_start_seq_timer(ipmi_smi_t intf, | |
859 | long msgid) | |
860 | { | |
861 | int rv = -ENODEV; | |
862 | unsigned long flags; | |
863 | unsigned char seq; | |
864 | unsigned long seqid; | |
865 | ||
866 | ||
867 | GET_SEQ_FROM_MSGID(msgid, seq, seqid); | |
868 | ||
869 | spin_lock_irqsave(&(intf->seq_lock), flags); | |
c70d7499 CM |
870 | /* |
871 | * We do this verification because the user can be deleted | |
872 | * while a message is outstanding. | |
873 | */ | |
1da177e4 | 874 | if ((intf->seq_table[seq].inuse) |
c70d7499 | 875 | && (intf->seq_table[seq].seqid == seqid)) { |
1da177e4 LT |
876 | struct seq_table *ent = &(intf->seq_table[seq]); |
877 | ent->timeout = ent->orig_timeout; | |
878 | rv = 0; | |
879 | } | |
880 | spin_unlock_irqrestore(&(intf->seq_lock), flags); | |
881 | ||
882 | return rv; | |
883 | } | |
884 | ||
885 | /* Got an error for the send message for a specific sequence number. */ | |
886 | static int intf_err_seq(ipmi_smi_t intf, | |
887 | long msgid, | |
888 | unsigned int err) | |
889 | { | |
890 | int rv = -ENODEV; | |
891 | unsigned long flags; | |
892 | unsigned char seq; | |
893 | unsigned long seqid; | |
894 | struct ipmi_recv_msg *msg = NULL; | |
895 | ||
896 | ||
897 | GET_SEQ_FROM_MSGID(msgid, seq, seqid); | |
898 | ||
899 | spin_lock_irqsave(&(intf->seq_lock), flags); | |
c70d7499 CM |
900 | /* |
901 | * We do this verification because the user can be deleted | |
902 | * while a message is outstanding. | |
903 | */ | |
1da177e4 | 904 | if ((intf->seq_table[seq].inuse) |
c70d7499 | 905 | && (intf->seq_table[seq].seqid == seqid)) { |
1da177e4 LT |
906 | struct seq_table *ent = &(intf->seq_table[seq]); |
907 | ||
908 | ent->inuse = 0; | |
909 | msg = ent->recv_msg; | |
910 | rv = 0; | |
911 | } | |
912 | spin_unlock_irqrestore(&(intf->seq_lock), flags); | |
913 | ||
b2c03941 CM |
914 | if (msg) |
915 | deliver_err_response(msg, err); | |
1da177e4 LT |
916 | |
917 | return rv; | |
918 | } | |
919 | ||
920 | ||
921 | int ipmi_create_user(unsigned int if_num, | |
210af2a5 | 922 | const struct ipmi_user_hndl *handler, |
1da177e4 LT |
923 | void *handler_data, |
924 | ipmi_user_t *user) | |
925 | { | |
926 | unsigned long flags; | |
927 | ipmi_user_t new_user; | |
928 | int rv = 0; | |
929 | ipmi_smi_t intf; | |
930 | ||
c70d7499 CM |
931 | /* |
932 | * There is no module usecount here, because it's not | |
933 | * required. Since this can only be used by and called from | |
934 | * other modules, they will implicitly use this module, and | |
935 | * thus this can't be removed unless the other modules are | |
936 | * removed. | |
937 | */ | |
1da177e4 LT |
938 | |
939 | if (handler == NULL) | |
940 | return -EINVAL; | |
941 | ||
c70d7499 CM |
942 | /* |
943 | * Make sure the driver is actually initialized, this handles | |
944 | * problems with initialization order. | |
945 | */ | |
1da177e4 LT |
946 | if (!initialized) { |
947 | rv = ipmi_init_msghandler(); | |
948 | if (rv) | |
949 | return rv; | |
950 | ||
c70d7499 CM |
951 | /* |
952 | * The init code doesn't return an error if it was turned | |
953 | * off, but it won't initialize. Check that. | |
954 | */ | |
1da177e4 LT |
955 | if (!initialized) |
956 | return -ENODEV; | |
957 | } | |
958 | ||
959 | new_user = kmalloc(sizeof(*new_user), GFP_KERNEL); | |
8a3628d5 | 960 | if (!new_user) |
1da177e4 LT |
961 | return -ENOMEM; |
962 | ||
b2c03941 | 963 | mutex_lock(&ipmi_interfaces_mutex); |
bca0324d CM |
964 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { |
965 | if (intf->intf_num == if_num) | |
966 | goto found; | |
1da177e4 | 967 | } |
b2c03941 | 968 | /* Not found, return an error */ |
bca0324d CM |
969 | rv = -EINVAL; |
970 | goto out_kfree; | |
1da177e4 | 971 | |
bca0324d | 972 | found: |
393d2cc3 CM |
973 | /* Note that each existing user holds a refcount to the interface. */ |
974 | kref_get(&intf->refcount); | |
1da177e4 | 975 | |
393d2cc3 | 976 | kref_init(&new_user->refcount); |
1da177e4 LT |
977 | new_user->handler = handler; |
978 | new_user->handler_data = handler_data; | |
979 | new_user->intf = intf; | |
89986496 | 980 | new_user->gets_events = false; |
1da177e4 LT |
981 | |
982 | if (!try_module_get(intf->handlers->owner)) { | |
983 | rv = -ENODEV; | |
5c98d29a | 984 | goto out_kref; |
1da177e4 LT |
985 | } |
986 | ||
987 | if (intf->handlers->inc_usecount) { | |
988 | rv = intf->handlers->inc_usecount(intf->send_info); | |
989 | if (rv) { | |
990 | module_put(intf->handlers->owner); | |
5c98d29a | 991 | goto out_kref; |
1da177e4 LT |
992 | } |
993 | } | |
994 | ||
c70d7499 CM |
995 | /* |
996 | * Hold the lock so intf->handlers is guaranteed to be good | |
997 | * until now | |
998 | */ | |
b2c03941 CM |
999 | mutex_unlock(&ipmi_interfaces_mutex); |
1000 | ||
7aefac26 | 1001 | new_user->valid = true; |
393d2cc3 CM |
1002 | spin_lock_irqsave(&intf->seq_lock, flags); |
1003 | list_add_rcu(&new_user->link, &intf->users); | |
1004 | spin_unlock_irqrestore(&intf->seq_lock, flags); | |
89986496 CM |
1005 | if (handler->ipmi_watchdog_pretimeout) { |
1006 | /* User wants pretimeouts, so make sure to watch for them. */ | |
1007 | if (atomic_inc_return(&intf->event_waiters) == 1) | |
1008 | need_waiter(intf); | |
1009 | } | |
393d2cc3 CM |
1010 | *user = new_user; |
1011 | return 0; | |
1da177e4 | 1012 | |
5c98d29a | 1013 | out_kref: |
393d2cc3 | 1014 | kref_put(&intf->refcount, intf_free); |
5c98d29a | 1015 | out_kfree: |
b2c03941 | 1016 | mutex_unlock(&ipmi_interfaces_mutex); |
5c98d29a | 1017 | kfree(new_user); |
1da177e4 LT |
1018 | return rv; |
1019 | } | |
c70d7499 | 1020 | EXPORT_SYMBOL(ipmi_create_user); |
1da177e4 | 1021 | |
16f4232c ZY |
1022 | int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data) |
1023 | { | |
1024 | int rv = 0; | |
1025 | ipmi_smi_t intf; | |
81d02b7f | 1026 | const struct ipmi_smi_handlers *handlers; |
16f4232c ZY |
1027 | |
1028 | mutex_lock(&ipmi_interfaces_mutex); | |
1029 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { | |
1030 | if (intf->intf_num == if_num) | |
1031 | goto found; | |
1032 | } | |
1033 | /* Not found, return an error */ | |
1034 | rv = -EINVAL; | |
1035 | mutex_unlock(&ipmi_interfaces_mutex); | |
1036 | return rv; | |
1037 | ||
1038 | found: | |
1039 | handlers = intf->handlers; | |
1040 | rv = -ENOSYS; | |
1041 | if (handlers->get_smi_info) | |
1042 | rv = handlers->get_smi_info(intf->send_info, data); | |
1043 | mutex_unlock(&ipmi_interfaces_mutex); | |
1044 | ||
1045 | return rv; | |
1046 | } | |
1047 | EXPORT_SYMBOL(ipmi_get_smi_info); | |
1048 | ||
393d2cc3 CM |
1049 | static void free_user(struct kref *ref) |
1050 | { | |
1051 | ipmi_user_t user = container_of(ref, struct ipmi_user, refcount); | |
1052 | kfree(user); | |
1053 | } | |
1054 | ||
1055 | int ipmi_destroy_user(ipmi_user_t user) | |
1da177e4 | 1056 | { |
393d2cc3 | 1057 | ipmi_smi_t intf = user->intf; |
1da177e4 LT |
1058 | int i; |
1059 | unsigned long flags; | |
393d2cc3 | 1060 | struct cmd_rcvr *rcvr; |
393d2cc3 | 1061 | struct cmd_rcvr *rcvrs = NULL; |
1da177e4 | 1062 | |
7aefac26 | 1063 | user->valid = false; |
1da177e4 | 1064 | |
89986496 CM |
1065 | if (user->handler->ipmi_watchdog_pretimeout) |
1066 | atomic_dec(&intf->event_waiters); | |
1067 | ||
1068 | if (user->gets_events) | |
1069 | atomic_dec(&intf->event_waiters); | |
1070 | ||
393d2cc3 CM |
1071 | /* Remove the user from the interface's sequence table. */ |
1072 | spin_lock_irqsave(&intf->seq_lock, flags); | |
1073 | list_del_rcu(&user->link); | |
1da177e4 | 1074 | |
e8b33617 | 1075 | for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { |
393d2cc3 | 1076 | if (intf->seq_table[i].inuse |
c70d7499 | 1077 | && (intf->seq_table[i].recv_msg->user == user)) { |
393d2cc3 | 1078 | intf->seq_table[i].inuse = 0; |
b2c03941 | 1079 | ipmi_free_recv_msg(intf->seq_table[i].recv_msg); |
1da177e4 LT |
1080 | } |
1081 | } | |
393d2cc3 CM |
1082 | spin_unlock_irqrestore(&intf->seq_lock, flags); |
1083 | ||
1084 | /* | |
1085 | * Remove the user from the command receiver's table. First | |
1086 | * we build a list of everything (not using the standard link, | |
1087 | * since other things may be using it till we do | |
1088 | * synchronize_rcu()) then free everything in that list. | |
1089 | */ | |
d6dfd131 | 1090 | mutex_lock(&intf->cmd_rcvrs_mutex); |
066bb8d0 | 1091 | list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { |
1da177e4 | 1092 | if (rcvr->user == user) { |
393d2cc3 CM |
1093 | list_del_rcu(&rcvr->link); |
1094 | rcvr->next = rcvrs; | |
1095 | rcvrs = rcvr; | |
1da177e4 LT |
1096 | } |
1097 | } | |
d6dfd131 | 1098 | mutex_unlock(&intf->cmd_rcvrs_mutex); |
393d2cc3 CM |
1099 | synchronize_rcu(); |
1100 | while (rcvrs) { | |
1101 | rcvr = rcvrs; | |
1102 | rcvrs = rcvr->next; | |
1103 | kfree(rcvr); | |
1104 | } | |
1da177e4 | 1105 | |
b2c03941 CM |
1106 | mutex_lock(&ipmi_interfaces_mutex); |
1107 | if (intf->handlers) { | |
1108 | module_put(intf->handlers->owner); | |
1109 | if (intf->handlers->dec_usecount) | |
1110 | intf->handlers->dec_usecount(intf->send_info); | |
1111 | } | |
1112 | mutex_unlock(&ipmi_interfaces_mutex); | |
1da177e4 | 1113 | |
393d2cc3 | 1114 | kref_put(&intf->refcount, intf_free); |
1da177e4 | 1115 | |
393d2cc3 | 1116 | kref_put(&user->refcount, free_user); |
1da177e4 | 1117 | |
8a3628d5 | 1118 | return 0; |
1da177e4 | 1119 | } |
c70d7499 | 1120 | EXPORT_SYMBOL(ipmi_destroy_user); |
1da177e4 LT |
1121 | |
1122 | void ipmi_get_version(ipmi_user_t user, | |
1123 | unsigned char *major, | |
1124 | unsigned char *minor) | |
1125 | { | |
b2c03941 CM |
1126 | *major = user->intf->ipmi_version_major; |
1127 | *minor = user->intf->ipmi_version_minor; | |
1da177e4 | 1128 | } |
c70d7499 | 1129 | EXPORT_SYMBOL(ipmi_get_version); |
1da177e4 | 1130 | |
c14979b9 CM |
1131 | int ipmi_set_my_address(ipmi_user_t user, |
1132 | unsigned int channel, | |
1133 | unsigned char address) | |
1da177e4 | 1134 | { |
c14979b9 CM |
1135 | if (channel >= IPMI_MAX_CHANNELS) |
1136 | return -EINVAL; | |
1137 | user->intf->channels[channel].address = address; | |
1138 | return 0; | |
1da177e4 | 1139 | } |
c70d7499 | 1140 | EXPORT_SYMBOL(ipmi_set_my_address); |
1da177e4 | 1141 | |
c14979b9 CM |
1142 | int ipmi_get_my_address(ipmi_user_t user, |
1143 | unsigned int channel, | |
1144 | unsigned char *address) | |
1da177e4 | 1145 | { |
c14979b9 CM |
1146 | if (channel >= IPMI_MAX_CHANNELS) |
1147 | return -EINVAL; | |
1148 | *address = user->intf->channels[channel].address; | |
1149 | return 0; | |
1da177e4 | 1150 | } |
c70d7499 | 1151 | EXPORT_SYMBOL(ipmi_get_my_address); |
1da177e4 | 1152 | |
c14979b9 CM |
1153 | int ipmi_set_my_LUN(ipmi_user_t user, |
1154 | unsigned int channel, | |
1155 | unsigned char LUN) | |
1da177e4 | 1156 | { |
c14979b9 CM |
1157 | if (channel >= IPMI_MAX_CHANNELS) |
1158 | return -EINVAL; | |
1159 | user->intf->channels[channel].lun = LUN & 0x3; | |
1160 | return 0; | |
1da177e4 | 1161 | } |
c70d7499 | 1162 | EXPORT_SYMBOL(ipmi_set_my_LUN); |
1da177e4 | 1163 | |
c14979b9 CM |
1164 | int ipmi_get_my_LUN(ipmi_user_t user, |
1165 | unsigned int channel, | |
1166 | unsigned char *address) | |
1da177e4 | 1167 | { |
c14979b9 CM |
1168 | if (channel >= IPMI_MAX_CHANNELS) |
1169 | return -EINVAL; | |
1170 | *address = user->intf->channels[channel].lun; | |
1171 | return 0; | |
1da177e4 | 1172 | } |
c70d7499 | 1173 | EXPORT_SYMBOL(ipmi_get_my_LUN); |
1da177e4 | 1174 | |
b9675136 CM |
1175 | int ipmi_get_maintenance_mode(ipmi_user_t user) |
1176 | { | |
1177 | int mode; | |
1178 | unsigned long flags; | |
1179 | ||
1180 | spin_lock_irqsave(&user->intf->maintenance_mode_lock, flags); | |
1181 | mode = user->intf->maintenance_mode; | |
1182 | spin_unlock_irqrestore(&user->intf->maintenance_mode_lock, flags); | |
1183 | ||
1184 | return mode; | |
1185 | } | |
1186 | EXPORT_SYMBOL(ipmi_get_maintenance_mode); | |
1187 | ||
1188 | static void maintenance_mode_update(ipmi_smi_t intf) | |
1189 | { | |
1190 | if (intf->handlers->set_maintenance_mode) | |
1191 | intf->handlers->set_maintenance_mode( | |
1192 | intf->send_info, intf->maintenance_mode_enable); | |
1193 | } | |
1194 | ||
1195 | int ipmi_set_maintenance_mode(ipmi_user_t user, int mode) | |
1196 | { | |
1197 | int rv = 0; | |
1198 | unsigned long flags; | |
1199 | ipmi_smi_t intf = user->intf; | |
1200 | ||
1201 | spin_lock_irqsave(&intf->maintenance_mode_lock, flags); | |
1202 | if (intf->maintenance_mode != mode) { | |
1203 | switch (mode) { | |
1204 | case IPMI_MAINTENANCE_MODE_AUTO: | |
b9675136 CM |
1205 | intf->maintenance_mode_enable |
1206 | = (intf->auto_maintenance_timeout > 0); | |
1207 | break; | |
1208 | ||
1209 | case IPMI_MAINTENANCE_MODE_OFF: | |
7aefac26 | 1210 | intf->maintenance_mode_enable = false; |
b9675136 CM |
1211 | break; |
1212 | ||
1213 | case IPMI_MAINTENANCE_MODE_ON: | |
7aefac26 | 1214 | intf->maintenance_mode_enable = true; |
b9675136 CM |
1215 | break; |
1216 | ||
1217 | default: | |
1218 | rv = -EINVAL; | |
1219 | goto out_unlock; | |
1220 | } | |
7aefac26 | 1221 | intf->maintenance_mode = mode; |
b9675136 CM |
1222 | |
1223 | maintenance_mode_update(intf); | |
1224 | } | |
1225 | out_unlock: | |
1226 | spin_unlock_irqrestore(&intf->maintenance_mode_lock, flags); | |
1227 | ||
1228 | return rv; | |
1229 | } | |
1230 | EXPORT_SYMBOL(ipmi_set_maintenance_mode); | |
1231 | ||
89986496 | 1232 | int ipmi_set_gets_events(ipmi_user_t user, bool val) |
1da177e4 | 1233 | { |
393d2cc3 CM |
1234 | unsigned long flags; |
1235 | ipmi_smi_t intf = user->intf; | |
1236 | struct ipmi_recv_msg *msg, *msg2; | |
1237 | struct list_head msgs; | |
1da177e4 | 1238 | |
393d2cc3 CM |
1239 | INIT_LIST_HEAD(&msgs); |
1240 | ||
1241 | spin_lock_irqsave(&intf->events_lock, flags); | |
89986496 CM |
1242 | if (user->gets_events == val) |
1243 | goto out; | |
1244 | ||
1da177e4 LT |
1245 | user->gets_events = val; |
1246 | ||
89986496 CM |
1247 | if (val) { |
1248 | if (atomic_inc_return(&intf->event_waiters) == 1) | |
1249 | need_waiter(intf); | |
1250 | } else { | |
1251 | atomic_dec(&intf->event_waiters); | |
1252 | } | |
1253 | ||
b2c03941 CM |
1254 | if (intf->delivering_events) |
1255 | /* | |
1256 | * Another thread is delivering events for this, so | |
1257 | * let it handle any new events. | |
1258 | */ | |
1259 | goto out; | |
1260 | ||
1261 | /* Deliver any queued events. */ | |
1262 | while (user->gets_events && !list_empty(&intf->waiting_events)) { | |
179e0917 AM |
1263 | list_for_each_entry_safe(msg, msg2, &intf->waiting_events, link) |
1264 | list_move_tail(&msg->link, &msgs); | |
4791c03d | 1265 | intf->waiting_events_count = 0; |
87ebd06f CM |
1266 | if (intf->event_msg_printed) { |
1267 | printk(KERN_WARNING PFX "Event queue no longer" | |
1268 | " full\n"); | |
1269 | intf->event_msg_printed = 0; | |
1270 | } | |
393d2cc3 | 1271 | |
b2c03941 CM |
1272 | intf->delivering_events = 1; |
1273 | spin_unlock_irqrestore(&intf->events_lock, flags); | |
1274 | ||
1275 | list_for_each_entry_safe(msg, msg2, &msgs, link) { | |
1276 | msg->user = user; | |
1277 | kref_get(&user->refcount); | |
1278 | deliver_response(msg); | |
1279 | } | |
1280 | ||
1281 | spin_lock_irqsave(&intf->events_lock, flags); | |
1282 | intf->delivering_events = 0; | |
393d2cc3 CM |
1283 | } |
1284 | ||
b2c03941 | 1285 | out: |
393d2cc3 | 1286 | spin_unlock_irqrestore(&intf->events_lock, flags); |
1da177e4 LT |
1287 | |
1288 | return 0; | |
1289 | } | |
c70d7499 | 1290 | EXPORT_SYMBOL(ipmi_set_gets_events); |
1da177e4 | 1291 | |
393d2cc3 CM |
1292 | static struct cmd_rcvr *find_cmd_rcvr(ipmi_smi_t intf, |
1293 | unsigned char netfn, | |
c69c3127 CM |
1294 | unsigned char cmd, |
1295 | unsigned char chan) | |
393d2cc3 CM |
1296 | { |
1297 | struct cmd_rcvr *rcvr; | |
1298 | ||
1299 | list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { | |
c69c3127 CM |
1300 | if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd) |
1301 | && (rcvr->chans & (1 << chan))) | |
393d2cc3 CM |
1302 | return rcvr; |
1303 | } | |
1304 | return NULL; | |
1305 | } | |
1306 | ||
c69c3127 CM |
1307 | static int is_cmd_rcvr_exclusive(ipmi_smi_t intf, |
1308 | unsigned char netfn, | |
1309 | unsigned char cmd, | |
1310 | unsigned int chans) | |
1311 | { | |
1312 | struct cmd_rcvr *rcvr; | |
1313 | ||
1314 | list_for_each_entry_rcu(rcvr, &intf->cmd_rcvrs, link) { | |
1315 | if ((rcvr->netfn == netfn) && (rcvr->cmd == cmd) | |
1316 | && (rcvr->chans & chans)) | |
1317 | return 0; | |
1318 | } | |
1319 | return 1; | |
1320 | } | |
1321 | ||
1da177e4 LT |
1322 | int ipmi_register_for_cmd(ipmi_user_t user, |
1323 | unsigned char netfn, | |
c69c3127 CM |
1324 | unsigned char cmd, |
1325 | unsigned int chans) | |
1da177e4 | 1326 | { |
393d2cc3 CM |
1327 | ipmi_smi_t intf = user->intf; |
1328 | struct cmd_rcvr *rcvr; | |
393d2cc3 | 1329 | int rv = 0; |
1da177e4 LT |
1330 | |
1331 | ||
1332 | rcvr = kmalloc(sizeof(*rcvr), GFP_KERNEL); | |
8a3628d5 | 1333 | if (!rcvr) |
1da177e4 | 1334 | return -ENOMEM; |
393d2cc3 CM |
1335 | rcvr->cmd = cmd; |
1336 | rcvr->netfn = netfn; | |
c69c3127 | 1337 | rcvr->chans = chans; |
393d2cc3 | 1338 | rcvr->user = user; |
1da177e4 | 1339 | |
d6dfd131 | 1340 | mutex_lock(&intf->cmd_rcvrs_mutex); |
1da177e4 | 1341 | /* Make sure the command/netfn is not already registered. */ |
c69c3127 | 1342 | if (!is_cmd_rcvr_exclusive(intf, netfn, cmd, chans)) { |
393d2cc3 CM |
1343 | rv = -EBUSY; |
1344 | goto out_unlock; | |
1da177e4 | 1345 | } |
877197ef | 1346 | |
89986496 CM |
1347 | if (atomic_inc_return(&intf->event_waiters) == 1) |
1348 | need_waiter(intf); | |
1349 | ||
393d2cc3 | 1350 | list_add_rcu(&rcvr->link, &intf->cmd_rcvrs); |
1da177e4 | 1351 | |
393d2cc3 | 1352 | out_unlock: |
d6dfd131 | 1353 | mutex_unlock(&intf->cmd_rcvrs_mutex); |
1da177e4 LT |
1354 | if (rv) |
1355 | kfree(rcvr); | |
1356 | ||
1357 | return rv; | |
1358 | } | |
c70d7499 | 1359 | EXPORT_SYMBOL(ipmi_register_for_cmd); |
1da177e4 LT |
1360 | |
1361 | int ipmi_unregister_for_cmd(ipmi_user_t user, | |
1362 | unsigned char netfn, | |
c69c3127 CM |
1363 | unsigned char cmd, |
1364 | unsigned int chans) | |
1da177e4 | 1365 | { |
393d2cc3 CM |
1366 | ipmi_smi_t intf = user->intf; |
1367 | struct cmd_rcvr *rcvr; | |
c69c3127 CM |
1368 | struct cmd_rcvr *rcvrs = NULL; |
1369 | int i, rv = -ENOENT; | |
1da177e4 | 1370 | |
d6dfd131 | 1371 | mutex_lock(&intf->cmd_rcvrs_mutex); |
c69c3127 CM |
1372 | for (i = 0; i < IPMI_NUM_CHANNELS; i++) { |
1373 | if (((1 << i) & chans) == 0) | |
1374 | continue; | |
1375 | rcvr = find_cmd_rcvr(intf, netfn, cmd, i); | |
1376 | if (rcvr == NULL) | |
1377 | continue; | |
1378 | if (rcvr->user == user) { | |
1379 | rv = 0; | |
1380 | rcvr->chans &= ~chans; | |
1381 | if (rcvr->chans == 0) { | |
1382 | list_del_rcu(&rcvr->link); | |
1383 | rcvr->next = rcvrs; | |
1384 | rcvrs = rcvr; | |
1385 | } | |
1386 | } | |
1387 | } | |
1388 | mutex_unlock(&intf->cmd_rcvrs_mutex); | |
1389 | synchronize_rcu(); | |
1390 | while (rcvrs) { | |
89986496 | 1391 | atomic_dec(&intf->event_waiters); |
c69c3127 CM |
1392 | rcvr = rcvrs; |
1393 | rcvrs = rcvr->next; | |
393d2cc3 | 1394 | kfree(rcvr); |
1da177e4 | 1395 | } |
c69c3127 | 1396 | return rv; |
1da177e4 | 1397 | } |
c70d7499 | 1398 | EXPORT_SYMBOL(ipmi_unregister_for_cmd); |
1da177e4 | 1399 | |
1da177e4 LT |
1400 | static unsigned char |
1401 | ipmb_checksum(unsigned char *data, int size) | |
1402 | { | |
1403 | unsigned char csum = 0; | |
c70d7499 | 1404 | |
1da177e4 LT |
1405 | for (; size > 0; size--, data++) |
1406 | csum += *data; | |
1407 | ||
1408 | return -csum; | |
1409 | } | |
1410 | ||
1411 | static inline void format_ipmb_msg(struct ipmi_smi_msg *smi_msg, | |
1412 | struct kernel_ipmi_msg *msg, | |
1413 | struct ipmi_ipmb_addr *ipmb_addr, | |
1414 | long msgid, | |
1415 | unsigned char ipmb_seq, | |
1416 | int broadcast, | |
1417 | unsigned char source_address, | |
1418 | unsigned char source_lun) | |
1419 | { | |
1420 | int i = broadcast; | |
1421 | ||
1422 | /* Format the IPMB header data. */ | |
1423 | smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); | |
1424 | smi_msg->data[1] = IPMI_SEND_MSG_CMD; | |
1425 | smi_msg->data[2] = ipmb_addr->channel; | |
1426 | if (broadcast) | |
1427 | smi_msg->data[3] = 0; | |
1428 | smi_msg->data[i+3] = ipmb_addr->slave_addr; | |
1429 | smi_msg->data[i+4] = (msg->netfn << 2) | (ipmb_addr->lun & 0x3); | |
1430 | smi_msg->data[i+5] = ipmb_checksum(&(smi_msg->data[i+3]), 2); | |
1431 | smi_msg->data[i+6] = source_address; | |
1432 | smi_msg->data[i+7] = (ipmb_seq << 2) | source_lun; | |
1433 | smi_msg->data[i+8] = msg->cmd; | |
1434 | ||
1435 | /* Now tack on the data to the message. */ | |
1436 | if (msg->data_len > 0) | |
1437 | memcpy(&(smi_msg->data[i+9]), msg->data, | |
1438 | msg->data_len); | |
1439 | smi_msg->data_size = msg->data_len + 9; | |
1440 | ||
1441 | /* Now calculate the checksum and tack it on. */ | |
1442 | smi_msg->data[i+smi_msg->data_size] | |
1443 | = ipmb_checksum(&(smi_msg->data[i+6]), | |
1444 | smi_msg->data_size-6); | |
1445 | ||
c70d7499 CM |
1446 | /* |
1447 | * Add on the checksum size and the offset from the | |
1448 | * broadcast. | |
1449 | */ | |
1da177e4 LT |
1450 | smi_msg->data_size += 1 + i; |
1451 | ||
1452 | smi_msg->msgid = msgid; | |
1453 | } | |
1454 | ||
1455 | static inline void format_lan_msg(struct ipmi_smi_msg *smi_msg, | |
1456 | struct kernel_ipmi_msg *msg, | |
1457 | struct ipmi_lan_addr *lan_addr, | |
1458 | long msgid, | |
1459 | unsigned char ipmb_seq, | |
1460 | unsigned char source_lun) | |
1461 | { | |
1462 | /* Format the IPMB header data. */ | |
1463 | smi_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); | |
1464 | smi_msg->data[1] = IPMI_SEND_MSG_CMD; | |
1465 | smi_msg->data[2] = lan_addr->channel; | |
1466 | smi_msg->data[3] = lan_addr->session_handle; | |
1467 | smi_msg->data[4] = lan_addr->remote_SWID; | |
1468 | smi_msg->data[5] = (msg->netfn << 2) | (lan_addr->lun & 0x3); | |
1469 | smi_msg->data[6] = ipmb_checksum(&(smi_msg->data[4]), 2); | |
1470 | smi_msg->data[7] = lan_addr->local_SWID; | |
1471 | smi_msg->data[8] = (ipmb_seq << 2) | source_lun; | |
1472 | smi_msg->data[9] = msg->cmd; | |
1473 | ||
1474 | /* Now tack on the data to the message. */ | |
1475 | if (msg->data_len > 0) | |
1476 | memcpy(&(smi_msg->data[10]), msg->data, | |
1477 | msg->data_len); | |
1478 | smi_msg->data_size = msg->data_len + 10; | |
1479 | ||
1480 | /* Now calculate the checksum and tack it on. */ | |
1481 | smi_msg->data[smi_msg->data_size] | |
1482 | = ipmb_checksum(&(smi_msg->data[7]), | |
1483 | smi_msg->data_size-7); | |
1484 | ||
c70d7499 CM |
1485 | /* |
1486 | * Add on the checksum size and the offset from the | |
1487 | * broadcast. | |
1488 | */ | |
1da177e4 LT |
1489 | smi_msg->data_size += 1; |
1490 | ||
1491 | smi_msg->msgid = msgid; | |
1492 | } | |
1493 | ||
191cc414 AB |
1494 | static struct ipmi_smi_msg *smi_add_send_msg(ipmi_smi_t intf, |
1495 | struct ipmi_smi_msg *smi_msg, | |
1496 | int priority) | |
7f4a1c84 | 1497 | { |
7ea0ed2b CM |
1498 | if (intf->curr_msg) { |
1499 | if (priority > 0) | |
1500 | list_add_tail(&smi_msg->link, &intf->hp_xmit_msgs); | |
1501 | else | |
1502 | list_add_tail(&smi_msg->link, &intf->xmit_msgs); | |
1503 | smi_msg = NULL; | |
1504 | } else { | |
1505 | intf->curr_msg = smi_msg; | |
1506 | } | |
191cc414 AB |
1507 | |
1508 | return smi_msg; | |
1509 | } | |
1510 | ||
1511 | ||
81d02b7f | 1512 | static void smi_send(ipmi_smi_t intf, const struct ipmi_smi_handlers *handlers, |
191cc414 AB |
1513 | struct ipmi_smi_msg *smi_msg, int priority) |
1514 | { | |
1515 | int run_to_completion = intf->run_to_completion; | |
1516 | ||
1517 | if (run_to_completion) { | |
1518 | smi_msg = smi_add_send_msg(intf, smi_msg, priority); | |
1519 | } else { | |
1520 | unsigned long flags; | |
1521 | ||
1522 | spin_lock_irqsave(&intf->xmit_msgs_lock, flags); | |
1523 | smi_msg = smi_add_send_msg(intf, smi_msg, priority); | |
7ea0ed2b | 1524 | spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); |
191cc414 | 1525 | } |
7ea0ed2b CM |
1526 | |
1527 | if (smi_msg) | |
99ab32f3 | 1528 | handlers->sender(intf->send_info, smi_msg); |
7f4a1c84 CM |
1529 | } |
1530 | ||
c70d7499 CM |
1531 | /* |
1532 | * Separate from ipmi_request so that the user does not have to be | |
1533 | * supplied in certain circumstances (mainly at panic time). If | |
1534 | * messages are supplied, they will be freed, even if an error | |
1535 | * occurs. | |
1536 | */ | |
393d2cc3 CM |
1537 | static int i_ipmi_request(ipmi_user_t user, |
1538 | ipmi_smi_t intf, | |
1539 | struct ipmi_addr *addr, | |
1540 | long msgid, | |
1541 | struct kernel_ipmi_msg *msg, | |
1542 | void *user_msg_data, | |
1543 | void *supplied_smi, | |
1544 | struct ipmi_recv_msg *supplied_recv, | |
1545 | int priority, | |
1546 | unsigned char source_address, | |
1547 | unsigned char source_lun, | |
1548 | int retries, | |
1549 | unsigned int retry_time_ms) | |
1da177e4 | 1550 | { |
b2c03941 CM |
1551 | int rv = 0; |
1552 | struct ipmi_smi_msg *smi_msg; | |
1553 | struct ipmi_recv_msg *recv_msg; | |
1554 | unsigned long flags; | |
1da177e4 LT |
1555 | |
1556 | ||
c70d7499 | 1557 | if (supplied_recv) |
1da177e4 | 1558 | recv_msg = supplied_recv; |
c70d7499 | 1559 | else { |
1da177e4 | 1560 | recv_msg = ipmi_alloc_recv_msg(); |
c70d7499 | 1561 | if (recv_msg == NULL) |
1da177e4 | 1562 | return -ENOMEM; |
1da177e4 LT |
1563 | } |
1564 | recv_msg->user_msg_data = user_msg_data; | |
1565 | ||
c70d7499 | 1566 | if (supplied_smi) |
1da177e4 | 1567 | smi_msg = (struct ipmi_smi_msg *) supplied_smi; |
c70d7499 | 1568 | else { |
1da177e4 LT |
1569 | smi_msg = ipmi_alloc_smi_msg(); |
1570 | if (smi_msg == NULL) { | |
1571 | ipmi_free_recv_msg(recv_msg); | |
1572 | return -ENOMEM; | |
1573 | } | |
1574 | } | |
1575 | ||
b2c03941 | 1576 | rcu_read_lock(); |
7ea0ed2b | 1577 | if (intf->in_shutdown) { |
b2c03941 CM |
1578 | rv = -ENODEV; |
1579 | goto out_err; | |
1580 | } | |
1581 | ||
1da177e4 | 1582 | recv_msg->user = user; |
393d2cc3 CM |
1583 | if (user) |
1584 | kref_get(&user->refcount); | |
1da177e4 | 1585 | recv_msg->msgid = msgid; |
c70d7499 CM |
1586 | /* |
1587 | * Store the message to send in the receive message so timeout | |
1588 | * responses can get the proper response data. | |
1589 | */ | |
1da177e4 LT |
1590 | recv_msg->msg = *msg; |
1591 | ||
1592 | if (addr->addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) { | |
1593 | struct ipmi_system_interface_addr *smi_addr; | |
1594 | ||
1595 | if (msg->netfn & 1) { | |
1596 | /* Responses are not allowed to the SMI. */ | |
1597 | rv = -EINVAL; | |
1598 | goto out_err; | |
1599 | } | |
1600 | ||
1601 | smi_addr = (struct ipmi_system_interface_addr *) addr; | |
1602 | if (smi_addr->lun > 3) { | |
b2655f26 | 1603 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1604 | rv = -EINVAL; |
1605 | goto out_err; | |
1606 | } | |
1607 | ||
1608 | memcpy(&recv_msg->addr, smi_addr, sizeof(*smi_addr)); | |
1609 | ||
1610 | if ((msg->netfn == IPMI_NETFN_APP_REQUEST) | |
1611 | && ((msg->cmd == IPMI_SEND_MSG_CMD) | |
1612 | || (msg->cmd == IPMI_GET_MSG_CMD) | |
c70d7499 CM |
1613 | || (msg->cmd == IPMI_READ_EVENT_MSG_BUFFER_CMD))) { |
1614 | /* | |
1615 | * We don't let the user do these, since we manage | |
1616 | * the sequence numbers. | |
1617 | */ | |
b2655f26 | 1618 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1619 | rv = -EINVAL; |
1620 | goto out_err; | |
1621 | } | |
1622 | ||
b9675136 CM |
1623 | if (((msg->netfn == IPMI_NETFN_APP_REQUEST) |
1624 | && ((msg->cmd == IPMI_COLD_RESET_CMD) | |
1625 | || (msg->cmd == IPMI_WARM_RESET_CMD))) | |
c70d7499 | 1626 | || (msg->netfn == IPMI_NETFN_FIRMWARE_REQUEST)) { |
b9675136 CM |
1627 | spin_lock_irqsave(&intf->maintenance_mode_lock, flags); |
1628 | intf->auto_maintenance_timeout | |
1629 | = IPMI_MAINTENANCE_MODE_TIMEOUT; | |
1630 | if (!intf->maintenance_mode | |
c70d7499 | 1631 | && !intf->maintenance_mode_enable) { |
7aefac26 | 1632 | intf->maintenance_mode_enable = true; |
b9675136 CM |
1633 | maintenance_mode_update(intf); |
1634 | } | |
1635 | spin_unlock_irqrestore(&intf->maintenance_mode_lock, | |
1636 | flags); | |
1637 | } | |
1638 | ||
1da177e4 | 1639 | if ((msg->data_len + 2) > IPMI_MAX_MSG_LENGTH) { |
b2655f26 | 1640 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1641 | rv = -EMSGSIZE; |
1642 | goto out_err; | |
1643 | } | |
1644 | ||
1645 | smi_msg->data[0] = (msg->netfn << 2) | (smi_addr->lun & 0x3); | |
1646 | smi_msg->data[1] = msg->cmd; | |
1647 | smi_msg->msgid = msgid; | |
1648 | smi_msg->user_data = recv_msg; | |
1649 | if (msg->data_len > 0) | |
1650 | memcpy(&(smi_msg->data[2]), msg->data, msg->data_len); | |
1651 | smi_msg->data_size = msg->data_len + 2; | |
b2655f26 | 1652 | ipmi_inc_stat(intf, sent_local_commands); |
25176ed6 | 1653 | } else if (is_ipmb_addr(addr) || is_ipmb_bcast_addr(addr)) { |
1da177e4 LT |
1654 | struct ipmi_ipmb_addr *ipmb_addr; |
1655 | unsigned char ipmb_seq; | |
1656 | long seqid; | |
1657 | int broadcast = 0; | |
1658 | ||
9c101fd4 | 1659 | if (addr->channel >= IPMI_MAX_CHANNELS) { |
b2655f26 | 1660 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1661 | rv = -EINVAL; |
1662 | goto out_err; | |
1663 | } | |
1664 | ||
1665 | if (intf->channels[addr->channel].medium | |
c70d7499 | 1666 | != IPMI_CHANNEL_MEDIUM_IPMB) { |
b2655f26 | 1667 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1668 | rv = -EINVAL; |
1669 | goto out_err; | |
1670 | } | |
1671 | ||
1672 | if (retries < 0) { | |
1673 | if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) | |
1674 | retries = 0; /* Don't retry broadcasts. */ | |
1675 | else | |
1676 | retries = 4; | |
1677 | } | |
1678 | if (addr->addr_type == IPMI_IPMB_BROADCAST_ADDR_TYPE) { | |
c70d7499 CM |
1679 | /* |
1680 | * Broadcasts add a zero at the beginning of the | |
1681 | * message, but otherwise is the same as an IPMB | |
1682 | * address. | |
1683 | */ | |
1da177e4 LT |
1684 | addr->addr_type = IPMI_IPMB_ADDR_TYPE; |
1685 | broadcast = 1; | |
1686 | } | |
1687 | ||
1688 | ||
1689 | /* Default to 1 second retries. */ | |
1690 | if (retry_time_ms == 0) | |
1691 | retry_time_ms = 1000; | |
1692 | ||
c70d7499 CM |
1693 | /* |
1694 | * 9 for the header and 1 for the checksum, plus | |
1695 | * possibly one for the broadcast. | |
1696 | */ | |
1da177e4 | 1697 | if ((msg->data_len + 10 + broadcast) > IPMI_MAX_MSG_LENGTH) { |
b2655f26 | 1698 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1699 | rv = -EMSGSIZE; |
1700 | goto out_err; | |
1701 | } | |
1702 | ||
1703 | ipmb_addr = (struct ipmi_ipmb_addr *) addr; | |
1704 | if (ipmb_addr->lun > 3) { | |
b2655f26 | 1705 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1706 | rv = -EINVAL; |
1707 | goto out_err; | |
1708 | } | |
1709 | ||
1710 | memcpy(&recv_msg->addr, ipmb_addr, sizeof(*ipmb_addr)); | |
1711 | ||
1712 | if (recv_msg->msg.netfn & 0x1) { | |
c70d7499 CM |
1713 | /* |
1714 | * It's a response, so use the user's sequence | |
1715 | * from msgid. | |
1716 | */ | |
b2655f26 | 1717 | ipmi_inc_stat(intf, sent_ipmb_responses); |
1da177e4 LT |
1718 | format_ipmb_msg(smi_msg, msg, ipmb_addr, msgid, |
1719 | msgid, broadcast, | |
1720 | source_address, source_lun); | |
1721 | ||
c70d7499 CM |
1722 | /* |
1723 | * Save the receive message so we can use it | |
1724 | * to deliver the response. | |
1725 | */ | |
1da177e4 LT |
1726 | smi_msg->user_data = recv_msg; |
1727 | } else { | |
1728 | /* It's a command, so get a sequence for it. */ | |
1729 | ||
1730 | spin_lock_irqsave(&(intf->seq_lock), flags); | |
1731 | ||
c70d7499 CM |
1732 | /* |
1733 | * Create a sequence number with a 1 second | |
1734 | * timeout and 4 retries. | |
1735 | */ | |
1da177e4 LT |
1736 | rv = intf_next_seq(intf, |
1737 | recv_msg, | |
1738 | retry_time_ms, | |
1739 | retries, | |
1740 | broadcast, | |
1741 | &ipmb_seq, | |
1742 | &seqid); | |
1743 | if (rv) { | |
c70d7499 CM |
1744 | /* |
1745 | * We have used up all the sequence numbers, | |
1746 | * probably, so abort. | |
1747 | */ | |
1da177e4 LT |
1748 | spin_unlock_irqrestore(&(intf->seq_lock), |
1749 | flags); | |
1750 | goto out_err; | |
1751 | } | |
1752 | ||
25176ed6 CM |
1753 | ipmi_inc_stat(intf, sent_ipmb_commands); |
1754 | ||
c70d7499 CM |
1755 | /* |
1756 | * Store the sequence number in the message, | |
1757 | * so that when the send message response | |
1758 | * comes back we can start the timer. | |
1759 | */ | |
1da177e4 LT |
1760 | format_ipmb_msg(smi_msg, msg, ipmb_addr, |
1761 | STORE_SEQ_IN_MSGID(ipmb_seq, seqid), | |
1762 | ipmb_seq, broadcast, | |
1763 | source_address, source_lun); | |
1764 | ||
c70d7499 CM |
1765 | /* |
1766 | * Copy the message into the recv message data, so we | |
1767 | * can retransmit it later if necessary. | |
1768 | */ | |
1da177e4 LT |
1769 | memcpy(recv_msg->msg_data, smi_msg->data, |
1770 | smi_msg->data_size); | |
1771 | recv_msg->msg.data = recv_msg->msg_data; | |
1772 | recv_msg->msg.data_len = smi_msg->data_size; | |
1773 | ||
c70d7499 CM |
1774 | /* |
1775 | * We don't unlock until here, because we need | |
1776 | * to copy the completed message into the | |
1777 | * recv_msg before we release the lock. | |
1778 | * Otherwise, race conditions may bite us. I | |
1779 | * know that's pretty paranoid, but I prefer | |
1780 | * to be correct. | |
1781 | */ | |
1da177e4 LT |
1782 | spin_unlock_irqrestore(&(intf->seq_lock), flags); |
1783 | } | |
25176ed6 | 1784 | } else if (is_lan_addr(addr)) { |
1da177e4 LT |
1785 | struct ipmi_lan_addr *lan_addr; |
1786 | unsigned char ipmb_seq; | |
1787 | long seqid; | |
1788 | ||
12fc1d7b | 1789 | if (addr->channel >= IPMI_MAX_CHANNELS) { |
b2655f26 | 1790 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1791 | rv = -EINVAL; |
1792 | goto out_err; | |
1793 | } | |
1794 | ||
1795 | if ((intf->channels[addr->channel].medium | |
c70d7499 | 1796 | != IPMI_CHANNEL_MEDIUM_8023LAN) |
1da177e4 | 1797 | && (intf->channels[addr->channel].medium |
c70d7499 | 1798 | != IPMI_CHANNEL_MEDIUM_ASYNC)) { |
b2655f26 | 1799 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1800 | rv = -EINVAL; |
1801 | goto out_err; | |
1802 | } | |
1803 | ||
1804 | retries = 4; | |
1805 | ||
1806 | /* Default to 1 second retries. */ | |
1807 | if (retry_time_ms == 0) | |
1808 | retry_time_ms = 1000; | |
1809 | ||
1810 | /* 11 for the header and 1 for the checksum. */ | |
1811 | if ((msg->data_len + 12) > IPMI_MAX_MSG_LENGTH) { | |
b2655f26 | 1812 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1813 | rv = -EMSGSIZE; |
1814 | goto out_err; | |
1815 | } | |
1816 | ||
1817 | lan_addr = (struct ipmi_lan_addr *) addr; | |
1818 | if (lan_addr->lun > 3) { | |
b2655f26 | 1819 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1820 | rv = -EINVAL; |
1821 | goto out_err; | |
1822 | } | |
1823 | ||
1824 | memcpy(&recv_msg->addr, lan_addr, sizeof(*lan_addr)); | |
1825 | ||
1826 | if (recv_msg->msg.netfn & 0x1) { | |
c70d7499 CM |
1827 | /* |
1828 | * It's a response, so use the user's sequence | |
1829 | * from msgid. | |
1830 | */ | |
b2655f26 | 1831 | ipmi_inc_stat(intf, sent_lan_responses); |
1da177e4 LT |
1832 | format_lan_msg(smi_msg, msg, lan_addr, msgid, |
1833 | msgid, source_lun); | |
1834 | ||
c70d7499 CM |
1835 | /* |
1836 | * Save the receive message so we can use it | |
1837 | * to deliver the response. | |
1838 | */ | |
1da177e4 LT |
1839 | smi_msg->user_data = recv_msg; |
1840 | } else { | |
1841 | /* It's a command, so get a sequence for it. */ | |
1842 | ||
1843 | spin_lock_irqsave(&(intf->seq_lock), flags); | |
1844 | ||
c70d7499 CM |
1845 | /* |
1846 | * Create a sequence number with a 1 second | |
1847 | * timeout and 4 retries. | |
1848 | */ | |
1da177e4 LT |
1849 | rv = intf_next_seq(intf, |
1850 | recv_msg, | |
1851 | retry_time_ms, | |
1852 | retries, | |
1853 | 0, | |
1854 | &ipmb_seq, | |
1855 | &seqid); | |
1856 | if (rv) { | |
c70d7499 CM |
1857 | /* |
1858 | * We have used up all the sequence numbers, | |
1859 | * probably, so abort. | |
1860 | */ | |
1da177e4 LT |
1861 | spin_unlock_irqrestore(&(intf->seq_lock), |
1862 | flags); | |
1863 | goto out_err; | |
1864 | } | |
1865 | ||
25176ed6 CM |
1866 | ipmi_inc_stat(intf, sent_lan_commands); |
1867 | ||
c70d7499 CM |
1868 | /* |
1869 | * Store the sequence number in the message, | |
1870 | * so that when the send message response | |
1871 | * comes back we can start the timer. | |
1872 | */ | |
1da177e4 LT |
1873 | format_lan_msg(smi_msg, msg, lan_addr, |
1874 | STORE_SEQ_IN_MSGID(ipmb_seq, seqid), | |
1875 | ipmb_seq, source_lun); | |
1876 | ||
c70d7499 CM |
1877 | /* |
1878 | * Copy the message into the recv message data, so we | |
1879 | * can retransmit it later if necessary. | |
1880 | */ | |
1da177e4 LT |
1881 | memcpy(recv_msg->msg_data, smi_msg->data, |
1882 | smi_msg->data_size); | |
1883 | recv_msg->msg.data = recv_msg->msg_data; | |
1884 | recv_msg->msg.data_len = smi_msg->data_size; | |
1885 | ||
c70d7499 CM |
1886 | /* |
1887 | * We don't unlock until here, because we need | |
1888 | * to copy the completed message into the | |
1889 | * recv_msg before we release the lock. | |
1890 | * Otherwise, race conditions may bite us. I | |
1891 | * know that's pretty paranoid, but I prefer | |
1892 | * to be correct. | |
1893 | */ | |
1da177e4 LT |
1894 | spin_unlock_irqrestore(&(intf->seq_lock), flags); |
1895 | } | |
1896 | } else { | |
1897 | /* Unknown address type. */ | |
b2655f26 | 1898 | ipmi_inc_stat(intf, sent_invalid_commands); |
1da177e4 LT |
1899 | rv = -EINVAL; |
1900 | goto out_err; | |
1901 | } | |
1902 | ||
1903 | #ifdef DEBUG_MSGING | |
1904 | { | |
1905 | int m; | |
e8b33617 | 1906 | for (m = 0; m < smi_msg->data_size; m++) |
1da177e4 LT |
1907 | printk(" %2.2x", smi_msg->data[m]); |
1908 | printk("\n"); | |
1909 | } | |
1910 | #endif | |
b2c03941 | 1911 | |
7ea0ed2b | 1912 | smi_send(intf, intf->handlers, smi_msg, priority); |
b2c03941 | 1913 | rcu_read_unlock(); |
1da177e4 LT |
1914 | |
1915 | return 0; | |
1916 | ||
1917 | out_err: | |
b2c03941 | 1918 | rcu_read_unlock(); |
1da177e4 LT |
1919 | ipmi_free_smi_msg(smi_msg); |
1920 | ipmi_free_recv_msg(recv_msg); | |
1921 | return rv; | |
1922 | } | |
1923 | ||
c14979b9 CM |
1924 | static int check_addr(ipmi_smi_t intf, |
1925 | struct ipmi_addr *addr, | |
1926 | unsigned char *saddr, | |
1927 | unsigned char *lun) | |
1928 | { | |
1929 | if (addr->channel >= IPMI_MAX_CHANNELS) | |
1930 | return -EINVAL; | |
1931 | *lun = intf->channels[addr->channel].lun; | |
1932 | *saddr = intf->channels[addr->channel].address; | |
1933 | return 0; | |
1934 | } | |
1935 | ||
1da177e4 LT |
1936 | int ipmi_request_settime(ipmi_user_t user, |
1937 | struct ipmi_addr *addr, | |
1938 | long msgid, | |
1939 | struct kernel_ipmi_msg *msg, | |
1940 | void *user_msg_data, | |
1941 | int priority, | |
1942 | int retries, | |
1943 | unsigned int retry_time_ms) | |
1944 | { | |
f0ba9390 | 1945 | unsigned char saddr = 0, lun = 0; |
c14979b9 CM |
1946 | int rv; |
1947 | ||
8a3628d5 | 1948 | if (!user) |
56a55ec6 | 1949 | return -EINVAL; |
c14979b9 CM |
1950 | rv = check_addr(user->intf, addr, &saddr, &lun); |
1951 | if (rv) | |
1952 | return rv; | |
1da177e4 LT |
1953 | return i_ipmi_request(user, |
1954 | user->intf, | |
1955 | addr, | |
1956 | msgid, | |
1957 | msg, | |
1958 | user_msg_data, | |
1959 | NULL, NULL, | |
1960 | priority, | |
c14979b9 CM |
1961 | saddr, |
1962 | lun, | |
1da177e4 LT |
1963 | retries, |
1964 | retry_time_ms); | |
1965 | } | |
c70d7499 | 1966 | EXPORT_SYMBOL(ipmi_request_settime); |
1da177e4 LT |
1967 | |
1968 | int ipmi_request_supply_msgs(ipmi_user_t user, | |
1969 | struct ipmi_addr *addr, | |
1970 | long msgid, | |
1971 | struct kernel_ipmi_msg *msg, | |
1972 | void *user_msg_data, | |
1973 | void *supplied_smi, | |
1974 | struct ipmi_recv_msg *supplied_recv, | |
1975 | int priority) | |
1976 | { | |
9ebca93b | 1977 | unsigned char saddr = 0, lun = 0; |
c14979b9 CM |
1978 | int rv; |
1979 | ||
8a3628d5 | 1980 | if (!user) |
56a55ec6 | 1981 | return -EINVAL; |
c14979b9 CM |
1982 | rv = check_addr(user->intf, addr, &saddr, &lun); |
1983 | if (rv) | |
1984 | return rv; | |
1da177e4 LT |
1985 | return i_ipmi_request(user, |
1986 | user->intf, | |
1987 | addr, | |
1988 | msgid, | |
1989 | msg, | |
1990 | user_msg_data, | |
1991 | supplied_smi, | |
1992 | supplied_recv, | |
1993 | priority, | |
c14979b9 CM |
1994 | saddr, |
1995 | lun, | |
1da177e4 LT |
1996 | -1, 0); |
1997 | } | |
c70d7499 | 1998 | EXPORT_SYMBOL(ipmi_request_supply_msgs); |
1da177e4 | 1999 | |
1aa16eea | 2000 | #ifdef CONFIG_PROC_FS |
07412736 | 2001 | static int smi_ipmb_proc_show(struct seq_file *m, void *v) |
1da177e4 | 2002 | { |
07412736 | 2003 | ipmi_smi_t intf = m->private; |
c14979b9 | 2004 | int i; |
1da177e4 | 2005 | |
07412736 AD |
2006 | seq_printf(m, "%x", intf->channels[0].address); |
2007 | for (i = 1; i < IPMI_MAX_CHANNELS; i++) | |
2008 | seq_printf(m, " %x", intf->channels[i].address); | |
d6c5dc18 JP |
2009 | seq_putc(m, '\n'); |
2010 | ||
5e33cd0c | 2011 | return 0; |
1da177e4 LT |
2012 | } |
2013 | ||
07412736 | 2014 | static int smi_ipmb_proc_open(struct inode *inode, struct file *file) |
1da177e4 | 2015 | { |
d9dda78b | 2016 | return single_open(file, smi_ipmb_proc_show, PDE_DATA(inode)); |
07412736 | 2017 | } |
1da177e4 | 2018 | |
07412736 AD |
2019 | static const struct file_operations smi_ipmb_proc_ops = { |
2020 | .open = smi_ipmb_proc_open, | |
2021 | .read = seq_read, | |
2022 | .llseek = seq_lseek, | |
2023 | .release = single_release, | |
2024 | }; | |
2025 | ||
2026 | static int smi_version_proc_show(struct seq_file *m, void *v) | |
2027 | { | |
2028 | ipmi_smi_t intf = m->private; | |
2029 | ||
d6c5dc18 JP |
2030 | seq_printf(m, "%u.%u\n", |
2031 | ipmi_version_major(&intf->bmc->id), | |
2032 | ipmi_version_minor(&intf->bmc->id)); | |
2033 | ||
5e33cd0c | 2034 | return 0; |
1da177e4 LT |
2035 | } |
2036 | ||
07412736 | 2037 | static int smi_version_proc_open(struct inode *inode, struct file *file) |
1da177e4 | 2038 | { |
d9dda78b | 2039 | return single_open(file, smi_version_proc_show, PDE_DATA(inode)); |
07412736 AD |
2040 | } |
2041 | ||
2042 | static const struct file_operations smi_version_proc_ops = { | |
2043 | .open = smi_version_proc_open, | |
2044 | .read = seq_read, | |
2045 | .llseek = seq_lseek, | |
2046 | .release = single_release, | |
2047 | }; | |
1da177e4 | 2048 | |
07412736 AD |
2049 | static int smi_stats_proc_show(struct seq_file *m, void *v) |
2050 | { | |
2051 | ipmi_smi_t intf = m->private; | |
2052 | ||
2053 | seq_printf(m, "sent_invalid_commands: %u\n", | |
b2655f26 | 2054 | ipmi_get_stat(intf, sent_invalid_commands)); |
07412736 | 2055 | seq_printf(m, "sent_local_commands: %u\n", |
b2655f26 | 2056 | ipmi_get_stat(intf, sent_local_commands)); |
07412736 | 2057 | seq_printf(m, "handled_local_responses: %u\n", |
b2655f26 | 2058 | ipmi_get_stat(intf, handled_local_responses)); |
07412736 | 2059 | seq_printf(m, "unhandled_local_responses: %u\n", |
b2655f26 | 2060 | ipmi_get_stat(intf, unhandled_local_responses)); |
07412736 | 2061 | seq_printf(m, "sent_ipmb_commands: %u\n", |
b2655f26 | 2062 | ipmi_get_stat(intf, sent_ipmb_commands)); |
07412736 | 2063 | seq_printf(m, "sent_ipmb_command_errs: %u\n", |
b2655f26 | 2064 | ipmi_get_stat(intf, sent_ipmb_command_errs)); |
07412736 | 2065 | seq_printf(m, "retransmitted_ipmb_commands: %u\n", |
b2655f26 | 2066 | ipmi_get_stat(intf, retransmitted_ipmb_commands)); |
07412736 | 2067 | seq_printf(m, "timed_out_ipmb_commands: %u\n", |
b2655f26 | 2068 | ipmi_get_stat(intf, timed_out_ipmb_commands)); |
07412736 | 2069 | seq_printf(m, "timed_out_ipmb_broadcasts: %u\n", |
b2655f26 | 2070 | ipmi_get_stat(intf, timed_out_ipmb_broadcasts)); |
07412736 | 2071 | seq_printf(m, "sent_ipmb_responses: %u\n", |
b2655f26 | 2072 | ipmi_get_stat(intf, sent_ipmb_responses)); |
07412736 | 2073 | seq_printf(m, "handled_ipmb_responses: %u\n", |
b2655f26 | 2074 | ipmi_get_stat(intf, handled_ipmb_responses)); |
07412736 | 2075 | seq_printf(m, "invalid_ipmb_responses: %u\n", |
b2655f26 | 2076 | ipmi_get_stat(intf, invalid_ipmb_responses)); |
07412736 | 2077 | seq_printf(m, "unhandled_ipmb_responses: %u\n", |
b2655f26 | 2078 | ipmi_get_stat(intf, unhandled_ipmb_responses)); |
07412736 | 2079 | seq_printf(m, "sent_lan_commands: %u\n", |
b2655f26 | 2080 | ipmi_get_stat(intf, sent_lan_commands)); |
07412736 | 2081 | seq_printf(m, "sent_lan_command_errs: %u\n", |
b2655f26 | 2082 | ipmi_get_stat(intf, sent_lan_command_errs)); |
07412736 | 2083 | seq_printf(m, "retransmitted_lan_commands: %u\n", |
b2655f26 | 2084 | ipmi_get_stat(intf, retransmitted_lan_commands)); |
07412736 | 2085 | seq_printf(m, "timed_out_lan_commands: %u\n", |
b2655f26 | 2086 | ipmi_get_stat(intf, timed_out_lan_commands)); |
07412736 | 2087 | seq_printf(m, "sent_lan_responses: %u\n", |
b2655f26 | 2088 | ipmi_get_stat(intf, sent_lan_responses)); |
07412736 | 2089 | seq_printf(m, "handled_lan_responses: %u\n", |
b2655f26 | 2090 | ipmi_get_stat(intf, handled_lan_responses)); |
07412736 | 2091 | seq_printf(m, "invalid_lan_responses: %u\n", |
b2655f26 | 2092 | ipmi_get_stat(intf, invalid_lan_responses)); |
07412736 | 2093 | seq_printf(m, "unhandled_lan_responses: %u\n", |
b2655f26 | 2094 | ipmi_get_stat(intf, unhandled_lan_responses)); |
07412736 | 2095 | seq_printf(m, "handled_commands: %u\n", |
b2655f26 | 2096 | ipmi_get_stat(intf, handled_commands)); |
07412736 | 2097 | seq_printf(m, "invalid_commands: %u\n", |
b2655f26 | 2098 | ipmi_get_stat(intf, invalid_commands)); |
07412736 | 2099 | seq_printf(m, "unhandled_commands: %u\n", |
b2655f26 | 2100 | ipmi_get_stat(intf, unhandled_commands)); |
07412736 | 2101 | seq_printf(m, "invalid_events: %u\n", |
b2655f26 | 2102 | ipmi_get_stat(intf, invalid_events)); |
07412736 | 2103 | seq_printf(m, "events: %u\n", |
b2655f26 | 2104 | ipmi_get_stat(intf, events)); |
07412736 | 2105 | seq_printf(m, "failed rexmit LAN msgs: %u\n", |
25176ed6 | 2106 | ipmi_get_stat(intf, dropped_rexmit_lan_commands)); |
07412736 | 2107 | seq_printf(m, "failed rexmit IPMB msgs: %u\n", |
25176ed6 | 2108 | ipmi_get_stat(intf, dropped_rexmit_ipmb_commands)); |
07412736 AD |
2109 | return 0; |
2110 | } | |
1da177e4 | 2111 | |
07412736 AD |
2112 | static int smi_stats_proc_open(struct inode *inode, struct file *file) |
2113 | { | |
d9dda78b | 2114 | return single_open(file, smi_stats_proc_show, PDE_DATA(inode)); |
1da177e4 | 2115 | } |
07412736 AD |
2116 | |
2117 | static const struct file_operations smi_stats_proc_ops = { | |
2118 | .open = smi_stats_proc_open, | |
2119 | .read = seq_read, | |
2120 | .llseek = seq_lseek, | |
2121 | .release = single_release, | |
2122 | }; | |
1aa16eea | 2123 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
2124 | |
2125 | int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name, | |
07412736 | 2126 | const struct file_operations *proc_ops, |
99b76233 | 2127 | void *data) |
1da177e4 | 2128 | { |
1da177e4 | 2129 | int rv = 0; |
3b625943 CM |
2130 | #ifdef CONFIG_PROC_FS |
2131 | struct proc_dir_entry *file; | |
1da177e4 LT |
2132 | struct ipmi_proc_entry *entry; |
2133 | ||
2134 | /* Create a list element. */ | |
2135 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
2136 | if (!entry) | |
2137 | return -ENOMEM; | |
1b6b698f | 2138 | entry->name = kstrdup(name, GFP_KERNEL); |
1da177e4 LT |
2139 | if (!entry->name) { |
2140 | kfree(entry); | |
2141 | return -ENOMEM; | |
2142 | } | |
1da177e4 | 2143 | |
07412736 | 2144 | file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data); |
1da177e4 LT |
2145 | if (!file) { |
2146 | kfree(entry->name); | |
2147 | kfree(entry); | |
2148 | rv = -ENOMEM; | |
2149 | } else { | |
ac019151 | 2150 | mutex_lock(&smi->proc_entry_lock); |
1da177e4 LT |
2151 | /* Stick it on the list. */ |
2152 | entry->next = smi->proc_entries; | |
2153 | smi->proc_entries = entry; | |
ac019151 | 2154 | mutex_unlock(&smi->proc_entry_lock); |
1da177e4 | 2155 | } |
3b625943 | 2156 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
2157 | |
2158 | return rv; | |
2159 | } | |
c70d7499 | 2160 | EXPORT_SYMBOL(ipmi_smi_add_proc_entry); |
1da177e4 LT |
2161 | |
2162 | static int add_proc_entries(ipmi_smi_t smi, int num) | |
2163 | { | |
2164 | int rv = 0; | |
2165 | ||
3b625943 | 2166 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
2167 | sprintf(smi->proc_dir_name, "%d", num); |
2168 | smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root); | |
2169 | if (!smi->proc_dir) | |
2170 | rv = -ENOMEM; | |
1da177e4 LT |
2171 | |
2172 | if (rv == 0) | |
2173 | rv = ipmi_smi_add_proc_entry(smi, "stats", | |
07412736 | 2174 | &smi_stats_proc_ops, |
99b76233 | 2175 | smi); |
1da177e4 LT |
2176 | |
2177 | if (rv == 0) | |
2178 | rv = ipmi_smi_add_proc_entry(smi, "ipmb", | |
07412736 | 2179 | &smi_ipmb_proc_ops, |
99b76233 | 2180 | smi); |
1da177e4 LT |
2181 | |
2182 | if (rv == 0) | |
2183 | rv = ipmi_smi_add_proc_entry(smi, "version", | |
07412736 | 2184 | &smi_version_proc_ops, |
99b76233 | 2185 | smi); |
3b625943 | 2186 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
2187 | |
2188 | return rv; | |
2189 | } | |
2190 | ||
2191 | static void remove_proc_entries(ipmi_smi_t smi) | |
2192 | { | |
3b625943 | 2193 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
2194 | struct ipmi_proc_entry *entry; |
2195 | ||
ac019151 | 2196 | mutex_lock(&smi->proc_entry_lock); |
1da177e4 LT |
2197 | while (smi->proc_entries) { |
2198 | entry = smi->proc_entries; | |
2199 | smi->proc_entries = entry->next; | |
2200 | ||
2201 | remove_proc_entry(entry->name, smi->proc_dir); | |
2202 | kfree(entry->name); | |
2203 | kfree(entry); | |
2204 | } | |
ac019151 | 2205 | mutex_unlock(&smi->proc_entry_lock); |
1da177e4 | 2206 | remove_proc_entry(smi->proc_dir_name, proc_ipmi_root); |
3b625943 | 2207 | #endif /* CONFIG_PROC_FS */ |
1da177e4 LT |
2208 | } |
2209 | ||
50c812b2 CM |
2210 | static int __find_bmc_guid(struct device *dev, void *data) |
2211 | { | |
2212 | unsigned char *id = data; | |
16639eb0 | 2213 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2214 | return memcmp(bmc->guid, id, 16) == 0; |
2215 | } | |
2216 | ||
2217 | static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv, | |
2218 | unsigned char *guid) | |
2219 | { | |
2220 | struct device *dev; | |
2221 | ||
2222 | dev = driver_find_device(drv, NULL, guid, __find_bmc_guid); | |
2223 | if (dev) | |
16639eb0 | 2224 | return to_bmc_device(dev); |
50c812b2 CM |
2225 | else |
2226 | return NULL; | |
2227 | } | |
2228 | ||
2229 | struct prod_dev_id { | |
2230 | unsigned int product_id; | |
2231 | unsigned char device_id; | |
2232 | }; | |
2233 | ||
2234 | static int __find_bmc_prod_dev_id(struct device *dev, void *data) | |
2235 | { | |
2236 | struct prod_dev_id *id = data; | |
16639eb0 | 2237 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2238 | |
2239 | return (bmc->id.product_id == id->product_id | |
50c812b2 CM |
2240 | && bmc->id.device_id == id->device_id); |
2241 | } | |
2242 | ||
2243 | static struct bmc_device *ipmi_find_bmc_prod_dev_id( | |
2244 | struct device_driver *drv, | |
f0b55da0 | 2245 | unsigned int product_id, unsigned char device_id) |
50c812b2 CM |
2246 | { |
2247 | struct prod_dev_id id = { | |
2248 | .product_id = product_id, | |
2249 | .device_id = device_id, | |
2250 | }; | |
2251 | struct device *dev; | |
2252 | ||
2253 | dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id); | |
2254 | if (dev) | |
16639eb0 | 2255 | return to_bmc_device(dev); |
50c812b2 CM |
2256 | else |
2257 | return NULL; | |
2258 | } | |
2259 | ||
2260 | static ssize_t device_id_show(struct device *dev, | |
2261 | struct device_attribute *attr, | |
2262 | char *buf) | |
2263 | { | |
16639eb0 | 2264 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2265 | |
2266 | return snprintf(buf, 10, "%u\n", bmc->id.device_id); | |
2267 | } | |
9c633317 | 2268 | static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL); |
50c812b2 | 2269 | |
16639eb0 CM |
2270 | static ssize_t provides_device_sdrs_show(struct device *dev, |
2271 | struct device_attribute *attr, | |
2272 | char *buf) | |
50c812b2 | 2273 | { |
16639eb0 | 2274 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2275 | |
2276 | return snprintf(buf, 10, "%u\n", | |
7947d2cc | 2277 | (bmc->id.device_revision & 0x80) >> 7); |
50c812b2 | 2278 | } |
9c633317 CM |
2279 | static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show, |
2280 | NULL); | |
50c812b2 CM |
2281 | |
2282 | static ssize_t revision_show(struct device *dev, struct device_attribute *attr, | |
2283 | char *buf) | |
2284 | { | |
16639eb0 | 2285 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2286 | |
2287 | return snprintf(buf, 20, "%u\n", | |
7947d2cc | 2288 | bmc->id.device_revision & 0x0F); |
50c812b2 | 2289 | } |
9c633317 | 2290 | static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL); |
50c812b2 | 2291 | |
16639eb0 CM |
2292 | static ssize_t firmware_revision_show(struct device *dev, |
2293 | struct device_attribute *attr, | |
2294 | char *buf) | |
50c812b2 | 2295 | { |
16639eb0 | 2296 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2297 | |
2298 | return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1, | |
2299 | bmc->id.firmware_revision_2); | |
2300 | } | |
9c633317 | 2301 | static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL); |
50c812b2 CM |
2302 | |
2303 | static ssize_t ipmi_version_show(struct device *dev, | |
2304 | struct device_attribute *attr, | |
2305 | char *buf) | |
2306 | { | |
16639eb0 | 2307 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2308 | |
2309 | return snprintf(buf, 20, "%u.%u\n", | |
2310 | ipmi_version_major(&bmc->id), | |
2311 | ipmi_version_minor(&bmc->id)); | |
2312 | } | |
9c633317 | 2313 | static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL); |
50c812b2 CM |
2314 | |
2315 | static ssize_t add_dev_support_show(struct device *dev, | |
2316 | struct device_attribute *attr, | |
2317 | char *buf) | |
2318 | { | |
16639eb0 | 2319 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2320 | |
2321 | return snprintf(buf, 10, "0x%02x\n", | |
2322 | bmc->id.additional_device_support); | |
2323 | } | |
9c633317 CM |
2324 | static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show, |
2325 | NULL); | |
50c812b2 CM |
2326 | |
2327 | static ssize_t manufacturer_id_show(struct device *dev, | |
2328 | struct device_attribute *attr, | |
2329 | char *buf) | |
2330 | { | |
16639eb0 | 2331 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2332 | |
2333 | return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id); | |
2334 | } | |
9c633317 | 2335 | static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL); |
50c812b2 CM |
2336 | |
2337 | static ssize_t product_id_show(struct device *dev, | |
2338 | struct device_attribute *attr, | |
2339 | char *buf) | |
2340 | { | |
16639eb0 | 2341 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2342 | |
2343 | return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id); | |
2344 | } | |
9c633317 | 2345 | static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL); |
50c812b2 CM |
2346 | |
2347 | static ssize_t aux_firmware_rev_show(struct device *dev, | |
2348 | struct device_attribute *attr, | |
2349 | char *buf) | |
2350 | { | |
16639eb0 | 2351 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2352 | |
2353 | return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n", | |
2354 | bmc->id.aux_firmware_revision[3], | |
2355 | bmc->id.aux_firmware_revision[2], | |
2356 | bmc->id.aux_firmware_revision[1], | |
2357 | bmc->id.aux_firmware_revision[0]); | |
2358 | } | |
9c633317 | 2359 | static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL); |
50c812b2 CM |
2360 | |
2361 | static ssize_t guid_show(struct device *dev, struct device_attribute *attr, | |
2362 | char *buf) | |
2363 | { | |
16639eb0 | 2364 | struct bmc_device *bmc = to_bmc_device(dev); |
50c812b2 CM |
2365 | |
2366 | return snprintf(buf, 100, "%Lx%Lx\n", | |
2367 | (long long) bmc->guid[0], | |
2368 | (long long) bmc->guid[8]); | |
2369 | } | |
9c633317 | 2370 | static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL); |
16639eb0 CM |
2371 | |
2372 | static struct attribute *bmc_dev_attrs[] = { | |
2373 | &dev_attr_device_id.attr, | |
2374 | &dev_attr_provides_device_sdrs.attr, | |
2375 | &dev_attr_revision.attr, | |
2376 | &dev_attr_firmware_revision.attr, | |
2377 | &dev_attr_ipmi_version.attr, | |
2378 | &dev_attr_additional_device_support.attr, | |
2379 | &dev_attr_manufacturer_id.attr, | |
2380 | &dev_attr_product_id.attr, | |
2d06a0c9 TI |
2381 | &dev_attr_aux_firmware_revision.attr, |
2382 | &dev_attr_guid.attr, | |
16639eb0 CM |
2383 | NULL |
2384 | }; | |
50c812b2 | 2385 | |
2d06a0c9 TI |
2386 | static umode_t bmc_dev_attr_is_visible(struct kobject *kobj, |
2387 | struct attribute *attr, int idx) | |
2388 | { | |
2389 | struct device *dev = kobj_to_dev(kobj); | |
2390 | struct bmc_device *bmc = to_bmc_device(dev); | |
2391 | umode_t mode = attr->mode; | |
2392 | ||
2393 | if (attr == &dev_attr_aux_firmware_revision.attr) | |
2394 | return bmc->id.aux_firmware_revision_set ? mode : 0; | |
2395 | if (attr == &dev_attr_guid.attr) | |
2396 | return bmc->guid_set ? mode : 0; | |
2397 | return mode; | |
2398 | } | |
2399 | ||
1e7a75f7 | 2400 | static const struct attribute_group bmc_dev_attr_group = { |
16639eb0 | 2401 | .attrs = bmc_dev_attrs, |
2d06a0c9 | 2402 | .is_visible = bmc_dev_attr_is_visible, |
16639eb0 | 2403 | }; |
5e59393e | 2404 | |
16639eb0 CM |
2405 | static const struct attribute_group *bmc_dev_attr_groups[] = { |
2406 | &bmc_dev_attr_group, | |
2407 | NULL | |
2408 | }; | |
2409 | ||
1e7a75f7 | 2410 | static const struct device_type bmc_device_type = { |
16639eb0 CM |
2411 | .groups = bmc_dev_attr_groups, |
2412 | }; | |
2413 | ||
2414 | static void | |
2415 | release_bmc_device(struct device *dev) | |
2416 | { | |
2417 | kfree(to_bmc_device(dev)); | |
5e59393e JG |
2418 | } |
2419 | ||
2420 | static void | |
2421 | cleanup_bmc_device(struct kref *ref) | |
2422 | { | |
16639eb0 | 2423 | struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount); |
5e59393e | 2424 | |
16639eb0 | 2425 | platform_device_unregister(&bmc->pdev); |
50c812b2 CM |
2426 | } |
2427 | ||
2428 | static void ipmi_bmc_unregister(ipmi_smi_t intf) | |
2429 | { | |
2430 | struct bmc_device *bmc = intf->bmc; | |
2431 | ||
5a0e10ec | 2432 | sysfs_remove_link(&intf->si_dev->kobj, "bmc"); |
50c812b2 | 2433 | if (intf->my_dev_name) { |
16639eb0 | 2434 | sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name); |
50c812b2 CM |
2435 | kfree(intf->my_dev_name); |
2436 | intf->my_dev_name = NULL; | |
2437 | } | |
2438 | ||
2439 | mutex_lock(&ipmidriver_mutex); | |
16639eb0 | 2440 | kref_put(&bmc->usecount, cleanup_bmc_device); |
f0b55da0 | 2441 | intf->bmc = NULL; |
50c812b2 CM |
2442 | mutex_unlock(&ipmidriver_mutex); |
2443 | } | |
2444 | ||
5a0e10ec | 2445 | static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum) |
50c812b2 CM |
2446 | { |
2447 | int rv; | |
2448 | struct bmc_device *bmc = intf->bmc; | |
2449 | struct bmc_device *old_bmc; | |
50c812b2 CM |
2450 | |
2451 | mutex_lock(&ipmidriver_mutex); | |
2452 | ||
2453 | /* | |
2454 | * Try to find if there is an bmc_device struct | |
2455 | * representing the interfaced BMC already | |
2456 | */ | |
2457 | if (bmc->guid_set) | |
fe2d5ffc | 2458 | old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid); |
50c812b2 | 2459 | else |
fe2d5ffc | 2460 | old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver, |
50c812b2 CM |
2461 | bmc->id.product_id, |
2462 | bmc->id.device_id); | |
2463 | ||
2464 | /* | |
2465 | * If there is already an bmc_device, free the new one, | |
2466 | * otherwise register the new BMC device | |
2467 | */ | |
2468 | if (old_bmc) { | |
2469 | kfree(bmc); | |
2470 | intf->bmc = old_bmc; | |
2471 | bmc = old_bmc; | |
2472 | ||
16639eb0 | 2473 | kref_get(&bmc->usecount); |
50c812b2 CM |
2474 | mutex_unlock(&ipmidriver_mutex); |
2475 | ||
2476 | printk(KERN_INFO | |
2477 | "ipmi: interfacing existing BMC (man_id: 0x%6.6x," | |
2478 | " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", | |
2479 | bmc->id.manufacturer_id, | |
2480 | bmc->id.product_id, | |
2481 | bmc->id.device_id); | |
2482 | } else { | |
f0b55da0 CM |
2483 | unsigned char orig_dev_id = bmc->id.device_id; |
2484 | int warn_printed = 0; | |
2485 | ||
16639eb0 | 2486 | snprintf(bmc->name, sizeof(bmc->name), |
f0b55da0 | 2487 | "ipmi_bmc.%4.4x", bmc->id.product_id); |
16639eb0 | 2488 | bmc->pdev.name = bmc->name; |
f0b55da0 | 2489 | |
fe2d5ffc | 2490 | while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver, |
f0b55da0 | 2491 | bmc->id.product_id, |
1d5636cc | 2492 | bmc->id.device_id)) { |
f0b55da0 CM |
2493 | if (!warn_printed) { |
2494 | printk(KERN_WARNING PFX | |
2495 | "This machine has two different BMCs" | |
2496 | " with the same product id and device" | |
2497 | " id. This is an error in the" | |
2498 | " firmware, but incrementing the" | |
2499 | " device id to work around the problem." | |
2500 | " Prod ID = 0x%x, Dev ID = 0x%x\n", | |
2501 | bmc->id.product_id, bmc->id.device_id); | |
2502 | warn_printed = 1; | |
2503 | } | |
2504 | bmc->id.device_id++; /* Wraps at 255 */ | |
2505 | if (bmc->id.device_id == orig_dev_id) { | |
2506 | printk(KERN_ERR PFX | |
2507 | "Out of device ids!\n"); | |
2508 | break; | |
2509 | } | |
2510 | } | |
2511 | ||
16639eb0 CM |
2512 | bmc->pdev.dev.driver = &ipmidriver.driver; |
2513 | bmc->pdev.id = bmc->id.device_id; | |
2514 | bmc->pdev.dev.release = release_bmc_device; | |
2515 | bmc->pdev.dev.type = &bmc_device_type; | |
5a0e10ec | 2516 | kref_init(&bmc->usecount); |
50c812b2 | 2517 | |
16639eb0 | 2518 | rv = platform_device_register(&bmc->pdev); |
50c812b2 CM |
2519 | mutex_unlock(&ipmidriver_mutex); |
2520 | if (rv) { | |
16639eb0 | 2521 | put_device(&bmc->pdev.dev); |
50c812b2 CM |
2522 | printk(KERN_ERR |
2523 | "ipmi_msghandler:" | |
2524 | " Unable to register bmc device: %d\n", | |
2525 | rv); | |
c70d7499 CM |
2526 | /* |
2527 | * Don't go to out_err, you can only do that if | |
2528 | * the device is registered already. | |
2529 | */ | |
50c812b2 CM |
2530 | return rv; |
2531 | } | |
2532 | ||
279fbd0c MS |
2533 | dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, " |
2534 | "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", | |
2535 | bmc->id.manufacturer_id, | |
2536 | bmc->id.product_id, | |
2537 | bmc->id.device_id); | |
50c812b2 CM |
2538 | } |
2539 | ||
2540 | /* | |
2541 | * create symlink from system interface device to bmc device | |
2542 | * and back. | |
2543 | */ | |
5a0e10ec | 2544 | rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc"); |
50c812b2 CM |
2545 | if (rv) { |
2546 | printk(KERN_ERR | |
2547 | "ipmi_msghandler: Unable to create bmc symlink: %d\n", | |
2548 | rv); | |
2549 | goto out_err; | |
2550 | } | |
2551 | ||
16639eb0 | 2552 | intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum); |
50c812b2 CM |
2553 | if (!intf->my_dev_name) { |
2554 | rv = -ENOMEM; | |
2555 | printk(KERN_ERR | |
2556 | "ipmi_msghandler: allocate link from BMC: %d\n", | |
2557 | rv); | |
2558 | goto out_err; | |
2559 | } | |
50c812b2 | 2560 | |
16639eb0 | 2561 | rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj, |
50c812b2 CM |
2562 | intf->my_dev_name); |
2563 | if (rv) { | |
2564 | kfree(intf->my_dev_name); | |
2565 | intf->my_dev_name = NULL; | |
2566 | printk(KERN_ERR | |
2567 | "ipmi_msghandler:" | |
2568 | " Unable to create symlink to bmc: %d\n", | |
2569 | rv); | |
2570 | goto out_err; | |
2571 | } | |
2572 | ||
2573 | return 0; | |
2574 | ||
2575 | out_err: | |
2576 | ipmi_bmc_unregister(intf); | |
2577 | return rv; | |
2578 | } | |
2579 | ||
2580 | static int | |
2581 | send_guid_cmd(ipmi_smi_t intf, int chan) | |
2582 | { | |
2583 | struct kernel_ipmi_msg msg; | |
2584 | struct ipmi_system_interface_addr si; | |
2585 | ||
2586 | si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
2587 | si.channel = IPMI_BMC_CHANNEL; | |
2588 | si.lun = 0; | |
2589 | ||
2590 | msg.netfn = IPMI_NETFN_APP_REQUEST; | |
2591 | msg.cmd = IPMI_GET_DEVICE_GUID_CMD; | |
2592 | msg.data = NULL; | |
2593 | msg.data_len = 0; | |
2594 | return i_ipmi_request(NULL, | |
2595 | intf, | |
2596 | (struct ipmi_addr *) &si, | |
2597 | 0, | |
2598 | &msg, | |
2599 | intf, | |
2600 | NULL, | |
2601 | NULL, | |
2602 | 0, | |
2603 | intf->channels[0].address, | |
2604 | intf->channels[0].lun, | |
2605 | -1, 0); | |
2606 | } | |
2607 | ||
2608 | static void | |
2609 | guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg) | |
2610 | { | |
2611 | if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE) | |
2612 | || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE) | |
2613 | || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD)) | |
2614 | /* Not for me */ | |
2615 | return; | |
2616 | ||
2617 | if (msg->msg.data[0] != 0) { | |
2618 | /* Error from getting the GUID, the BMC doesn't have one. */ | |
2619 | intf->bmc->guid_set = 0; | |
2620 | goto out; | |
2621 | } | |
2622 | ||
2623 | if (msg->msg.data_len < 17) { | |
2624 | intf->bmc->guid_set = 0; | |
2625 | printk(KERN_WARNING PFX | |
2626 | "guid_handler: The GUID response from the BMC was too" | |
2627 | " short, it was %d but should have been 17. Assuming" | |
2628 | " GUID is not available.\n", | |
2629 | msg->msg.data_len); | |
2630 | goto out; | |
2631 | } | |
2632 | ||
2633 | memcpy(intf->bmc->guid, msg->msg.data, 16); | |
2634 | intf->bmc->guid_set = 1; | |
2635 | out: | |
2636 | wake_up(&intf->waitq); | |
2637 | } | |
2638 | ||
2639 | static void | |
2640 | get_guid(ipmi_smi_t intf) | |
2641 | { | |
2642 | int rv; | |
2643 | ||
2644 | intf->bmc->guid_set = 0x2; | |
2645 | intf->null_user_handler = guid_handler; | |
2646 | rv = send_guid_cmd(intf, 0); | |
2647 | if (rv) | |
2648 | /* Send failed, no GUID available. */ | |
2649 | intf->bmc->guid_set = 0; | |
2650 | wait_event(intf->waitq, intf->bmc->guid_set != 2); | |
2651 | intf->null_user_handler = NULL; | |
2652 | } | |
2653 | ||
1da177e4 LT |
2654 | static int |
2655 | send_channel_info_cmd(ipmi_smi_t intf, int chan) | |
2656 | { | |
2657 | struct kernel_ipmi_msg msg; | |
2658 | unsigned char data[1]; | |
2659 | struct ipmi_system_interface_addr si; | |
2660 | ||
2661 | si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
2662 | si.channel = IPMI_BMC_CHANNEL; | |
2663 | si.lun = 0; | |
2664 | ||
2665 | msg.netfn = IPMI_NETFN_APP_REQUEST; | |
2666 | msg.cmd = IPMI_GET_CHANNEL_INFO_CMD; | |
2667 | msg.data = data; | |
2668 | msg.data_len = 1; | |
2669 | data[0] = chan; | |
2670 | return i_ipmi_request(NULL, | |
2671 | intf, | |
2672 | (struct ipmi_addr *) &si, | |
2673 | 0, | |
2674 | &msg, | |
56a55ec6 | 2675 | intf, |
1da177e4 LT |
2676 | NULL, |
2677 | NULL, | |
2678 | 0, | |
c14979b9 CM |
2679 | intf->channels[0].address, |
2680 | intf->channels[0].lun, | |
1da177e4 LT |
2681 | -1, 0); |
2682 | } | |
2683 | ||
2684 | static void | |
56a55ec6 | 2685 | channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg) |
1da177e4 LT |
2686 | { |
2687 | int rv = 0; | |
2688 | int chan; | |
2689 | ||
56a55ec6 CM |
2690 | if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) |
2691 | && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE) | |
c70d7499 | 2692 | && (msg->msg.cmd == IPMI_GET_CHANNEL_INFO_CMD)) { |
1da177e4 | 2693 | /* It's the one we want */ |
56a55ec6 | 2694 | if (msg->msg.data[0] != 0) { |
1da177e4 LT |
2695 | /* Got an error from the channel, just go on. */ |
2696 | ||
56a55ec6 | 2697 | if (msg->msg.data[0] == IPMI_INVALID_COMMAND_ERR) { |
c70d7499 CM |
2698 | /* |
2699 | * If the MC does not support this | |
2700 | * command, that is legal. We just | |
2701 | * assume it has one IPMB at channel | |
2702 | * zero. | |
2703 | */ | |
1da177e4 LT |
2704 | intf->channels[0].medium |
2705 | = IPMI_CHANNEL_MEDIUM_IPMB; | |
2706 | intf->channels[0].protocol | |
2707 | = IPMI_CHANNEL_PROTOCOL_IPMB; | |
1da177e4 LT |
2708 | |
2709 | intf->curr_channel = IPMI_MAX_CHANNELS; | |
2710 | wake_up(&intf->waitq); | |
2711 | goto out; | |
2712 | } | |
2713 | goto next_channel; | |
2714 | } | |
56a55ec6 | 2715 | if (msg->msg.data_len < 4) { |
1da177e4 LT |
2716 | /* Message not big enough, just go on. */ |
2717 | goto next_channel; | |
2718 | } | |
2719 | chan = intf->curr_channel; | |
56a55ec6 CM |
2720 | intf->channels[chan].medium = msg->msg.data[2] & 0x7f; |
2721 | intf->channels[chan].protocol = msg->msg.data[3] & 0x1f; | |
1da177e4 | 2722 | |
c70d7499 | 2723 | next_channel: |
1da177e4 LT |
2724 | intf->curr_channel++; |
2725 | if (intf->curr_channel >= IPMI_MAX_CHANNELS) | |
2726 | wake_up(&intf->waitq); | |
2727 | else | |
2728 | rv = send_channel_info_cmd(intf, intf->curr_channel); | |
2729 | ||
2730 | if (rv) { | |
2731 | /* Got an error somehow, just give up. */ | |
1f668423 CM |
2732 | printk(KERN_WARNING PFX |
2733 | "Error sending channel information for channel" | |
2734 | " %d: %d\n", intf->curr_channel, rv); | |
2735 | ||
1da177e4 LT |
2736 | intf->curr_channel = IPMI_MAX_CHANNELS; |
2737 | wake_up(&intf->waitq); | |
1da177e4 LT |
2738 | } |
2739 | } | |
2740 | out: | |
2741 | return; | |
2742 | } | |
2743 | ||
895dcfd1 | 2744 | static void ipmi_poll(ipmi_smi_t intf) |
fcfa4724 | 2745 | { |
fcfa4724 CM |
2746 | if (intf->handlers->poll) |
2747 | intf->handlers->poll(intf->send_info); | |
7adf579c CM |
2748 | /* In case something came in */ |
2749 | handle_new_recv_msgs(intf); | |
fcfa4724 | 2750 | } |
895dcfd1 CM |
2751 | |
2752 | void ipmi_poll_interface(ipmi_user_t user) | |
2753 | { | |
2754 | ipmi_poll(user->intf); | |
fcfa4724 | 2755 | } |
c70d7499 | 2756 | EXPORT_SYMBOL(ipmi_poll_interface); |
fcfa4724 | 2757 | |
81d02b7f | 2758 | int ipmi_register_smi(const struct ipmi_smi_handlers *handlers, |
1da177e4 | 2759 | void *send_info, |
50c812b2 CM |
2760 | struct ipmi_device_id *device_id, |
2761 | struct device *si_dev, | |
453823ba | 2762 | unsigned char slave_addr) |
1da177e4 LT |
2763 | { |
2764 | int i, j; | |
2765 | int rv; | |
393d2cc3 | 2766 | ipmi_smi_t intf; |
bca0324d | 2767 | ipmi_smi_t tintf; |
bca0324d | 2768 | struct list_head *link; |
1da177e4 | 2769 | |
c70d7499 CM |
2770 | /* |
2771 | * Make sure the driver is actually initialized, this handles | |
2772 | * problems with initialization order. | |
2773 | */ | |
1da177e4 LT |
2774 | if (!initialized) { |
2775 | rv = ipmi_init_msghandler(); | |
2776 | if (rv) | |
2777 | return rv; | |
c70d7499 CM |
2778 | /* |
2779 | * The init code doesn't return an error if it was turned | |
2780 | * off, but it won't initialize. Check that. | |
2781 | */ | |
1da177e4 LT |
2782 | if (!initialized) |
2783 | return -ENODEV; | |
2784 | } | |
2785 | ||
dd00cc48 | 2786 | intf = kzalloc(sizeof(*intf), GFP_KERNEL); |
393d2cc3 | 2787 | if (!intf) |
1da177e4 | 2788 | return -ENOMEM; |
b2c03941 CM |
2789 | |
2790 | intf->ipmi_version_major = ipmi_version_major(device_id); | |
2791 | intf->ipmi_version_minor = ipmi_version_minor(device_id); | |
2792 | ||
50c812b2 CM |
2793 | intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL); |
2794 | if (!intf->bmc) { | |
2795 | kfree(intf); | |
2796 | return -ENOMEM; | |
2797 | } | |
bca0324d | 2798 | intf->intf_num = -1; /* Mark it invalid for now. */ |
393d2cc3 | 2799 | kref_init(&intf->refcount); |
50c812b2 CM |
2800 | intf->bmc->id = *device_id; |
2801 | intf->si_dev = si_dev; | |
393d2cc3 CM |
2802 | for (j = 0; j < IPMI_MAX_CHANNELS; j++) { |
2803 | intf->channels[j].address = IPMI_BMC_SLAVE_ADDR; | |
2804 | intf->channels[j].lun = 2; | |
2805 | } | |
2806 | if (slave_addr != 0) | |
2807 | intf->channels[0].address = slave_addr; | |
2808 | INIT_LIST_HEAD(&intf->users); | |
2809 | intf->handlers = handlers; | |
2810 | intf->send_info = send_info; | |
2811 | spin_lock_init(&intf->seq_lock); | |
2812 | for (j = 0; j < IPMI_IPMB_NUM_SEQ; j++) { | |
2813 | intf->seq_table[j].inuse = 0; | |
2814 | intf->seq_table[j].seqid = 0; | |
2815 | } | |
2816 | intf->curr_seq = 0; | |
2817 | #ifdef CONFIG_PROC_FS | |
ac019151 | 2818 | mutex_init(&intf->proc_entry_lock); |
393d2cc3 | 2819 | #endif |
65be7544 CM |
2820 | spin_lock_init(&intf->waiting_rcv_msgs_lock); |
2821 | INIT_LIST_HEAD(&intf->waiting_rcv_msgs); | |
7adf579c CM |
2822 | tasklet_init(&intf->recv_tasklet, |
2823 | smi_recv_tasklet, | |
2824 | (unsigned long) intf); | |
2825 | atomic_set(&intf->watchdog_pretimeouts_to_deliver, 0); | |
7ea0ed2b CM |
2826 | spin_lock_init(&intf->xmit_msgs_lock); |
2827 | INIT_LIST_HEAD(&intf->xmit_msgs); | |
2828 | INIT_LIST_HEAD(&intf->hp_xmit_msgs); | |
393d2cc3 | 2829 | spin_lock_init(&intf->events_lock); |
89986496 CM |
2830 | atomic_set(&intf->event_waiters, 0); |
2831 | intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME; | |
393d2cc3 CM |
2832 | INIT_LIST_HEAD(&intf->waiting_events); |
2833 | intf->waiting_events_count = 0; | |
d6dfd131 | 2834 | mutex_init(&intf->cmd_rcvrs_mutex); |
b9675136 | 2835 | spin_lock_init(&intf->maintenance_mode_lock); |
393d2cc3 CM |
2836 | INIT_LIST_HEAD(&intf->cmd_rcvrs); |
2837 | init_waitqueue_head(&intf->waitq); | |
b2655f26 KB |
2838 | for (i = 0; i < IPMI_NUM_STATS; i++) |
2839 | atomic_set(&intf->stats[i], 0); | |
393d2cc3 | 2840 | |
393d2cc3 | 2841 | intf->proc_dir = NULL; |
1da177e4 | 2842 | |
b2c03941 | 2843 | mutex_lock(&smi_watchers_mutex); |
bca0324d CM |
2844 | mutex_lock(&ipmi_interfaces_mutex); |
2845 | /* Look for a hole in the numbers. */ | |
2846 | i = 0; | |
2847 | link = &ipmi_interfaces; | |
2848 | list_for_each_entry_rcu(tintf, &ipmi_interfaces, link) { | |
2849 | if (tintf->intf_num != i) { | |
2850 | link = &tintf->link; | |
1da177e4 LT |
2851 | break; |
2852 | } | |
bca0324d | 2853 | i++; |
1da177e4 | 2854 | } |
bca0324d CM |
2855 | /* Add the new interface in numeric order. */ |
2856 | if (i == 0) | |
2857 | list_add_rcu(&intf->link, &ipmi_interfaces); | |
2858 | else | |
2859 | list_add_tail_rcu(&intf->link, link); | |
1da177e4 | 2860 | |
453823ba CM |
2861 | rv = handlers->start_processing(send_info, intf); |
2862 | if (rv) | |
2863 | goto out; | |
1da177e4 | 2864 | |
50c812b2 CM |
2865 | get_guid(intf); |
2866 | ||
b2c03941 | 2867 | if ((intf->ipmi_version_major > 1) |
c70d7499 CM |
2868 | || ((intf->ipmi_version_major == 1) |
2869 | && (intf->ipmi_version_minor >= 5))) { | |
2870 | /* | |
2871 | * Start scanning the channels to see what is | |
2872 | * available. | |
2873 | */ | |
393d2cc3 CM |
2874 | intf->null_user_handler = channel_handler; |
2875 | intf->curr_channel = 0; | |
2876 | rv = send_channel_info_cmd(intf, 0); | |
1f668423 CM |
2877 | if (rv) { |
2878 | printk(KERN_WARNING PFX | |
2879 | "Error sending channel information for channel" | |
2880 | " 0, %d\n", rv); | |
393d2cc3 | 2881 | goto out; |
1f668423 | 2882 | } |
1da177e4 | 2883 | |
393d2cc3 CM |
2884 | /* Wait for the channel info to be read. */ |
2885 | wait_event(intf->waitq, | |
2886 | intf->curr_channel >= IPMI_MAX_CHANNELS); | |
50c812b2 | 2887 | intf->null_user_handler = NULL; |
393d2cc3 CM |
2888 | } else { |
2889 | /* Assume a single IPMB channel at zero. */ | |
2890 | intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB; | |
2891 | intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB; | |
9a2845c4 | 2892 | intf->curr_channel = IPMI_MAX_CHANNELS; |
1da177e4 LT |
2893 | } |
2894 | ||
bd85f4b3 XX |
2895 | rv = ipmi_bmc_register(intf, i); |
2896 | ||
393d2cc3 CM |
2897 | if (rv == 0) |
2898 | rv = add_proc_entries(intf, i); | |
1da177e4 | 2899 | |
393d2cc3 | 2900 | out: |
1da177e4 | 2901 | if (rv) { |
393d2cc3 CM |
2902 | if (intf->proc_dir) |
2903 | remove_proc_entries(intf); | |
b2c03941 | 2904 | intf->handlers = NULL; |
bca0324d CM |
2905 | list_del_rcu(&intf->link); |
2906 | mutex_unlock(&ipmi_interfaces_mutex); | |
b2c03941 | 2907 | mutex_unlock(&smi_watchers_mutex); |
bca0324d | 2908 | synchronize_rcu(); |
393d2cc3 | 2909 | kref_put(&intf->refcount, intf_free); |
393d2cc3 | 2910 | } else { |
78ba2faf CM |
2911 | /* |
2912 | * Keep memory order straight for RCU readers. Make | |
2913 | * sure everything else is committed to memory before | |
2914 | * setting intf_num to mark the interface valid. | |
2915 | */ | |
2916 | smp_wmb(); | |
bca0324d CM |
2917 | intf->intf_num = i; |
2918 | mutex_unlock(&ipmi_interfaces_mutex); | |
78ba2faf | 2919 | /* After this point the interface is legal to use. */ |
50c812b2 | 2920 | call_smi_watchers(i, intf->si_dev); |
b2c03941 | 2921 | mutex_unlock(&smi_watchers_mutex); |
1da177e4 LT |
2922 | } |
2923 | ||
2924 | return rv; | |
2925 | } | |
c70d7499 | 2926 | EXPORT_SYMBOL(ipmi_register_smi); |
1da177e4 | 2927 | |
7ea0ed2b CM |
2928 | static void deliver_smi_err_response(ipmi_smi_t intf, |
2929 | struct ipmi_smi_msg *msg, | |
2930 | unsigned char err) | |
2931 | { | |
2932 | msg->rsp[0] = msg->data[0] | 4; | |
2933 | msg->rsp[1] = msg->data[1]; | |
2934 | msg->rsp[2] = err; | |
2935 | msg->rsp_size = 3; | |
2936 | /* It's an error, so it will never requeue, no need to check return. */ | |
2937 | handle_one_recv_msg(intf, msg); | |
2938 | } | |
2939 | ||
b2c03941 CM |
2940 | static void cleanup_smi_msgs(ipmi_smi_t intf) |
2941 | { | |
2942 | int i; | |
2943 | struct seq_table *ent; | |
7ea0ed2b CM |
2944 | struct ipmi_smi_msg *msg; |
2945 | struct list_head *entry; | |
2946 | struct list_head tmplist; | |
2947 | ||
2948 | /* Clear out our transmit queues and hold the messages. */ | |
2949 | INIT_LIST_HEAD(&tmplist); | |
2950 | list_splice_tail(&intf->hp_xmit_msgs, &tmplist); | |
2951 | list_splice_tail(&intf->xmit_msgs, &tmplist); | |
2952 | ||
2953 | /* Current message first, to preserve order */ | |
2954 | while (intf->curr_msg && !list_empty(&intf->waiting_rcv_msgs)) { | |
2955 | /* Wait for the message to clear out. */ | |
2956 | schedule_timeout(1); | |
2957 | } | |
b2c03941 CM |
2958 | |
2959 | /* No need for locks, the interface is down. */ | |
7ea0ed2b CM |
2960 | |
2961 | /* | |
2962 | * Return errors for all pending messages in queue and in the | |
2963 | * tables waiting for remote responses. | |
2964 | */ | |
2965 | while (!list_empty(&tmplist)) { | |
2966 | entry = tmplist.next; | |
2967 | list_del(entry); | |
2968 | msg = list_entry(entry, struct ipmi_smi_msg, link); | |
2969 | deliver_smi_err_response(intf, msg, IPMI_ERR_UNSPECIFIED); | |
2970 | } | |
2971 | ||
b2c03941 CM |
2972 | for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) { |
2973 | ent = &(intf->seq_table[i]); | |
2974 | if (!ent->inuse) | |
2975 | continue; | |
2976 | deliver_err_response(ent->recv_msg, IPMI_ERR_UNSPECIFIED); | |
2977 | } | |
2978 | } | |
2979 | ||
1da177e4 LT |
2980 | int ipmi_unregister_smi(ipmi_smi_t intf) |
2981 | { | |
1da177e4 | 2982 | struct ipmi_smi_watcher *w; |
7ea0ed2b CM |
2983 | int intf_num = intf->intf_num; |
2984 | ipmi_user_t user; | |
1da177e4 | 2985 | |
b2c03941 | 2986 | mutex_lock(&smi_watchers_mutex); |
bca0324d | 2987 | mutex_lock(&ipmi_interfaces_mutex); |
b2c03941 | 2988 | intf->intf_num = -1; |
7ea0ed2b | 2989 | intf->in_shutdown = true; |
bca0324d CM |
2990 | list_del_rcu(&intf->link); |
2991 | mutex_unlock(&ipmi_interfaces_mutex); | |
2992 | synchronize_rcu(); | |
1da177e4 | 2993 | |
b2c03941 CM |
2994 | cleanup_smi_msgs(intf); |
2995 | ||
7ea0ed2b CM |
2996 | /* Clean up the effects of users on the lower-level software. */ |
2997 | mutex_lock(&ipmi_interfaces_mutex); | |
2998 | rcu_read_lock(); | |
2999 | list_for_each_entry_rcu(user, &intf->users, link) { | |
3000 | module_put(intf->handlers->owner); | |
3001 | if (intf->handlers->dec_usecount) | |
3002 | intf->handlers->dec_usecount(intf->send_info); | |
3003 | } | |
3004 | rcu_read_unlock(); | |
3005 | intf->handlers = NULL; | |
3006 | mutex_unlock(&ipmi_interfaces_mutex); | |
3007 | ||
393d2cc3 | 3008 | remove_proc_entries(intf); |
bd85f4b3 | 3009 | ipmi_bmc_unregister(intf); |
1da177e4 | 3010 | |
c70d7499 CM |
3011 | /* |
3012 | * Call all the watcher interfaces to tell them that | |
3013 | * an interface is gone. | |
3014 | */ | |
393d2cc3 | 3015 | list_for_each_entry(w, &smi_watchers, link) |
b2c03941 CM |
3016 | w->smi_gone(intf_num); |
3017 | mutex_unlock(&smi_watchers_mutex); | |
393d2cc3 | 3018 | |
393d2cc3 | 3019 | kref_put(&intf->refcount, intf_free); |
1da177e4 LT |
3020 | return 0; |
3021 | } | |
c70d7499 | 3022 | EXPORT_SYMBOL(ipmi_unregister_smi); |
1da177e4 LT |
3023 | |
3024 | static int handle_ipmb_get_msg_rsp(ipmi_smi_t intf, | |
3025 | struct ipmi_smi_msg *msg) | |
3026 | { | |
3027 | struct ipmi_ipmb_addr ipmb_addr; | |
3028 | struct ipmi_recv_msg *recv_msg; | |
1da177e4 | 3029 | |
c70d7499 CM |
3030 | /* |
3031 | * This is 11, not 10, because the response must contain a | |
3032 | * completion code. | |
3033 | */ | |
1da177e4 LT |
3034 | if (msg->rsp_size < 11) { |
3035 | /* Message not big enough, just ignore it. */ | |
b2655f26 | 3036 | ipmi_inc_stat(intf, invalid_ipmb_responses); |
1da177e4 LT |
3037 | return 0; |
3038 | } | |
3039 | ||
3040 | if (msg->rsp[2] != 0) { | |
3041 | /* An error getting the response, just ignore it. */ | |
3042 | return 0; | |
3043 | } | |
3044 | ||
3045 | ipmb_addr.addr_type = IPMI_IPMB_ADDR_TYPE; | |
3046 | ipmb_addr.slave_addr = msg->rsp[6]; | |
3047 | ipmb_addr.channel = msg->rsp[3] & 0x0f; | |
3048 | ipmb_addr.lun = msg->rsp[7] & 3; | |
3049 | ||
c70d7499 CM |
3050 | /* |
3051 | * It's a response from a remote entity. Look up the sequence | |
3052 | * number and handle the response. | |
3053 | */ | |
1da177e4 LT |
3054 | if (intf_find_seq(intf, |
3055 | msg->rsp[7] >> 2, | |
3056 | msg->rsp[3] & 0x0f, | |
3057 | msg->rsp[8], | |
3058 | (msg->rsp[4] >> 2) & (~1), | |
3059 | (struct ipmi_addr *) &(ipmb_addr), | |
c70d7499 CM |
3060 | &recv_msg)) { |
3061 | /* | |
3062 | * We were unable to find the sequence number, | |
3063 | * so just nuke the message. | |
3064 | */ | |
b2655f26 | 3065 | ipmi_inc_stat(intf, unhandled_ipmb_responses); |
1da177e4 LT |
3066 | return 0; |
3067 | } | |
3068 | ||
3069 | memcpy(recv_msg->msg_data, | |
3070 | &(msg->rsp[9]), | |
3071 | msg->rsp_size - 9); | |
c70d7499 CM |
3072 | /* |
3073 | * The other fields matched, so no need to set them, except | |
3074 | * for netfn, which needs to be the response that was | |
3075 | * returned, not the request value. | |
3076 | */ | |
1da177e4 LT |
3077 | recv_msg->msg.netfn = msg->rsp[4] >> 2; |
3078 | recv_msg->msg.data = recv_msg->msg_data; | |
3079 | recv_msg->msg.data_len = msg->rsp_size - 10; | |
3080 | recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; | |
b2655f26 | 3081 | ipmi_inc_stat(intf, handled_ipmb_responses); |
1da177e4 LT |
3082 | deliver_response(recv_msg); |
3083 | ||
3084 | return 0; | |
3085 | } | |
3086 | ||
3087 | static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf, | |
3088 | struct ipmi_smi_msg *msg) | |
3089 | { | |
393d2cc3 CM |
3090 | struct cmd_rcvr *rcvr; |
3091 | int rv = 0; | |
3092 | unsigned char netfn; | |
3093 | unsigned char cmd; | |
c69c3127 | 3094 | unsigned char chan; |
393d2cc3 CM |
3095 | ipmi_user_t user = NULL; |
3096 | struct ipmi_ipmb_addr *ipmb_addr; | |
3097 | struct ipmi_recv_msg *recv_msg; | |
1da177e4 LT |
3098 | |
3099 | if (msg->rsp_size < 10) { | |
3100 | /* Message not big enough, just ignore it. */ | |
b2655f26 | 3101 | ipmi_inc_stat(intf, invalid_commands); |
1da177e4 LT |
3102 | return 0; |
3103 | } | |
3104 | ||
3105 | if (msg->rsp[2] != 0) { | |
3106 | /* An error getting the response, just ignore it. */ | |
3107 | return 0; | |
3108 | } | |
3109 | ||
3110 | netfn = msg->rsp[4] >> 2; | |
3111 | cmd = msg->rsp[8]; | |
c69c3127 | 3112 | chan = msg->rsp[3] & 0xf; |
1da177e4 | 3113 | |
e61fb5b6 | 3114 | rcu_read_lock(); |
c69c3127 | 3115 | rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); |
393d2cc3 CM |
3116 | if (rcvr) { |
3117 | user = rcvr->user; | |
3118 | kref_get(&user->refcount); | |
3119 | } else | |
3120 | user = NULL; | |
e61fb5b6 | 3121 | rcu_read_unlock(); |
1da177e4 LT |
3122 | |
3123 | if (user == NULL) { | |
3124 | /* We didn't find a user, deliver an error response. */ | |
b2655f26 | 3125 | ipmi_inc_stat(intf, unhandled_commands); |
1da177e4 LT |
3126 | |
3127 | msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2); | |
3128 | msg->data[1] = IPMI_SEND_MSG_CMD; | |
3129 | msg->data[2] = msg->rsp[3]; | |
3130 | msg->data[3] = msg->rsp[6]; | |
c70d7499 | 3131 | msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3); |
1da177e4 | 3132 | msg->data[5] = ipmb_checksum(&(msg->data[3]), 2); |
c14979b9 | 3133 | msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address; |
c70d7499 CM |
3134 | /* rqseq/lun */ |
3135 | msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3); | |
1da177e4 LT |
3136 | msg->data[8] = msg->rsp[8]; /* cmd */ |
3137 | msg->data[9] = IPMI_INVALID_CMD_COMPLETION_CODE; | |
3138 | msg->data[10] = ipmb_checksum(&(msg->data[6]), 4); | |
3139 | msg->data_size = 11; | |
3140 | ||
3141 | #ifdef DEBUG_MSGING | |
3142 | { | |
3143 | int m; | |
3144 | printk("Invalid command:"); | |
e8b33617 | 3145 | for (m = 0; m < msg->data_size; m++) |
1da177e4 LT |
3146 | printk(" %2.2x", msg->data[m]); |
3147 | printk("\n"); | |
3148 | } | |
3149 | #endif | |
b2c03941 | 3150 | rcu_read_lock(); |
7ea0ed2b CM |
3151 | if (!intf->in_shutdown) { |
3152 | smi_send(intf, intf->handlers, msg, 0); | |
c70d7499 CM |
3153 | /* |
3154 | * We used the message, so return the value | |
3155 | * that causes it to not be freed or | |
3156 | * queued. | |
3157 | */ | |
b2c03941 CM |
3158 | rv = -1; |
3159 | } | |
3160 | rcu_read_unlock(); | |
1da177e4 LT |
3161 | } else { |
3162 | /* Deliver the message to the user. */ | |
b2655f26 | 3163 | ipmi_inc_stat(intf, handled_commands); |
1da177e4 LT |
3164 | |
3165 | recv_msg = ipmi_alloc_recv_msg(); | |
8a3628d5 | 3166 | if (!recv_msg) { |
c70d7499 CM |
3167 | /* |
3168 | * We couldn't allocate memory for the | |
3169 | * message, so requeue it for handling | |
3170 | * later. | |
3171 | */ | |
1da177e4 | 3172 | rv = 1; |
393d2cc3 | 3173 | kref_put(&user->refcount, free_user); |
1da177e4 LT |
3174 | } else { |
3175 | /* Extract the source address from the data. */ | |
3176 | ipmb_addr = (struct ipmi_ipmb_addr *) &recv_msg->addr; | |
3177 | ipmb_addr->addr_type = IPMI_IPMB_ADDR_TYPE; | |
3178 | ipmb_addr->slave_addr = msg->rsp[6]; | |
3179 | ipmb_addr->lun = msg->rsp[7] & 3; | |
3180 | ipmb_addr->channel = msg->rsp[3] & 0xf; | |
3181 | ||
c70d7499 CM |
3182 | /* |
3183 | * Extract the rest of the message information | |
3184 | * from the IPMB header. | |
3185 | */ | |
1da177e4 LT |
3186 | recv_msg->user = user; |
3187 | recv_msg->recv_type = IPMI_CMD_RECV_TYPE; | |
3188 | recv_msg->msgid = msg->rsp[7] >> 2; | |
3189 | recv_msg->msg.netfn = msg->rsp[4] >> 2; | |
3190 | recv_msg->msg.cmd = msg->rsp[8]; | |
3191 | recv_msg->msg.data = recv_msg->msg_data; | |
3192 | ||
c70d7499 CM |
3193 | /* |
3194 | * We chop off 10, not 9 bytes because the checksum | |
3195 | * at the end also needs to be removed. | |
3196 | */ | |
1da177e4 LT |
3197 | recv_msg->msg.data_len = msg->rsp_size - 10; |
3198 | memcpy(recv_msg->msg_data, | |
3199 | &(msg->rsp[9]), | |
3200 | msg->rsp_size - 10); | |
3201 | deliver_response(recv_msg); | |
3202 | } | |
3203 | } | |
3204 | ||
3205 | return rv; | |
3206 | } | |
3207 | ||
3208 | static int handle_lan_get_msg_rsp(ipmi_smi_t intf, | |
3209 | struct ipmi_smi_msg *msg) | |
3210 | { | |
3211 | struct ipmi_lan_addr lan_addr; | |
3212 | struct ipmi_recv_msg *recv_msg; | |
1da177e4 LT |
3213 | |
3214 | ||
c70d7499 CM |
3215 | /* |
3216 | * This is 13, not 12, because the response must contain a | |
3217 | * completion code. | |
3218 | */ | |
1da177e4 LT |
3219 | if (msg->rsp_size < 13) { |
3220 | /* Message not big enough, just ignore it. */ | |
b2655f26 | 3221 | ipmi_inc_stat(intf, invalid_lan_responses); |
1da177e4 LT |
3222 | return 0; |
3223 | } | |
3224 | ||
3225 | if (msg->rsp[2] != 0) { | |
3226 | /* An error getting the response, just ignore it. */ | |
3227 | return 0; | |
3228 | } | |
3229 | ||
3230 | lan_addr.addr_type = IPMI_LAN_ADDR_TYPE; | |
3231 | lan_addr.session_handle = msg->rsp[4]; | |
3232 | lan_addr.remote_SWID = msg->rsp[8]; | |
3233 | lan_addr.local_SWID = msg->rsp[5]; | |
3234 | lan_addr.channel = msg->rsp[3] & 0x0f; | |
3235 | lan_addr.privilege = msg->rsp[3] >> 4; | |
3236 | lan_addr.lun = msg->rsp[9] & 3; | |
3237 | ||
c70d7499 CM |
3238 | /* |
3239 | * It's a response from a remote entity. Look up the sequence | |
3240 | * number and handle the response. | |
3241 | */ | |
1da177e4 LT |
3242 | if (intf_find_seq(intf, |
3243 | msg->rsp[9] >> 2, | |
3244 | msg->rsp[3] & 0x0f, | |
3245 | msg->rsp[10], | |
3246 | (msg->rsp[6] >> 2) & (~1), | |
3247 | (struct ipmi_addr *) &(lan_addr), | |
c70d7499 CM |
3248 | &recv_msg)) { |
3249 | /* | |
3250 | * We were unable to find the sequence number, | |
3251 | * so just nuke the message. | |
3252 | */ | |
b2655f26 | 3253 | ipmi_inc_stat(intf, unhandled_lan_responses); |
1da177e4 LT |
3254 | return 0; |
3255 | } | |
3256 | ||
3257 | memcpy(recv_msg->msg_data, | |
3258 | &(msg->rsp[11]), | |
3259 | msg->rsp_size - 11); | |
c70d7499 CM |
3260 | /* |
3261 | * The other fields matched, so no need to set them, except | |
3262 | * for netfn, which needs to be the response that was | |
3263 | * returned, not the request value. | |
3264 | */ | |
1da177e4 LT |
3265 | recv_msg->msg.netfn = msg->rsp[6] >> 2; |
3266 | recv_msg->msg.data = recv_msg->msg_data; | |
3267 | recv_msg->msg.data_len = msg->rsp_size - 12; | |
3268 | recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; | |
b2655f26 | 3269 | ipmi_inc_stat(intf, handled_lan_responses); |
1da177e4 LT |
3270 | deliver_response(recv_msg); |
3271 | ||
3272 | return 0; | |
3273 | } | |
3274 | ||
3275 | static int handle_lan_get_msg_cmd(ipmi_smi_t intf, | |
3276 | struct ipmi_smi_msg *msg) | |
3277 | { | |
393d2cc3 CM |
3278 | struct cmd_rcvr *rcvr; |
3279 | int rv = 0; | |
3280 | unsigned char netfn; | |
3281 | unsigned char cmd; | |
c69c3127 | 3282 | unsigned char chan; |
393d2cc3 CM |
3283 | ipmi_user_t user = NULL; |
3284 | struct ipmi_lan_addr *lan_addr; | |
3285 | struct ipmi_recv_msg *recv_msg; | |
1da177e4 LT |
3286 | |
3287 | if (msg->rsp_size < 12) { | |
3288 | /* Message not big enough, just ignore it. */ | |
b2655f26 | 3289 | ipmi_inc_stat(intf, invalid_commands); |
1da177e4 LT |
3290 | return 0; |
3291 | } | |
3292 | ||
3293 | if (msg->rsp[2] != 0) { | |
3294 | /* An error getting the response, just ignore it. */ | |
3295 | return 0; | |
3296 | } | |
3297 | ||
3298 | netfn = msg->rsp[6] >> 2; | |
3299 | cmd = msg->rsp[10]; | |
c69c3127 | 3300 | chan = msg->rsp[3] & 0xf; |
1da177e4 | 3301 | |
e61fb5b6 | 3302 | rcu_read_lock(); |
c69c3127 | 3303 | rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); |
393d2cc3 CM |
3304 | if (rcvr) { |
3305 | user = rcvr->user; | |
3306 | kref_get(&user->refcount); | |
3307 | } else | |
3308 | user = NULL; | |
e61fb5b6 | 3309 | rcu_read_unlock(); |
1da177e4 LT |
3310 | |
3311 | if (user == NULL) { | |
393d2cc3 | 3312 | /* We didn't find a user, just give up. */ |
b2655f26 | 3313 | ipmi_inc_stat(intf, unhandled_commands); |
1da177e4 | 3314 | |
c70d7499 CM |
3315 | /* |
3316 | * Don't do anything with these messages, just allow | |
3317 | * them to be freed. | |
3318 | */ | |
3319 | rv = 0; | |
1da177e4 LT |
3320 | } else { |
3321 | /* Deliver the message to the user. */ | |
b2655f26 | 3322 | ipmi_inc_stat(intf, handled_commands); |
1da177e4 LT |
3323 | |
3324 | recv_msg = ipmi_alloc_recv_msg(); | |
8a3628d5 | 3325 | if (!recv_msg) { |
c70d7499 CM |
3326 | /* |
3327 | * We couldn't allocate memory for the | |
3328 | * message, so requeue it for handling later. | |
3329 | */ | |
1da177e4 | 3330 | rv = 1; |
393d2cc3 | 3331 | kref_put(&user->refcount, free_user); |
1da177e4 LT |
3332 | } else { |
3333 | /* Extract the source address from the data. */ | |
3334 | lan_addr = (struct ipmi_lan_addr *) &recv_msg->addr; | |
3335 | lan_addr->addr_type = IPMI_LAN_ADDR_TYPE; | |
3336 | lan_addr->session_handle = msg->rsp[4]; | |
3337 | lan_addr->remote_SWID = msg->rsp[8]; | |
3338 | lan_addr->local_SWID = msg->rsp[5]; | |
3339 | lan_addr->lun = msg->rsp[9] & 3; | |
3340 | lan_addr->channel = msg->rsp[3] & 0xf; | |
3341 | lan_addr->privilege = msg->rsp[3] >> 4; | |
3342 | ||
c70d7499 CM |
3343 | /* |
3344 | * Extract the rest of the message information | |
3345 | * from the IPMB header. | |
3346 | */ | |
1da177e4 LT |
3347 | recv_msg->user = user; |
3348 | recv_msg->recv_type = IPMI_CMD_RECV_TYPE; | |
3349 | recv_msg->msgid = msg->rsp[9] >> 2; | |
3350 | recv_msg->msg.netfn = msg->rsp[6] >> 2; | |
3351 | recv_msg->msg.cmd = msg->rsp[10]; | |
3352 | recv_msg->msg.data = recv_msg->msg_data; | |
3353 | ||
c70d7499 CM |
3354 | /* |
3355 | * We chop off 12, not 11 bytes because the checksum | |
3356 | * at the end also needs to be removed. | |
3357 | */ | |
1da177e4 LT |
3358 | recv_msg->msg.data_len = msg->rsp_size - 12; |
3359 | memcpy(recv_msg->msg_data, | |
3360 | &(msg->rsp[11]), | |
3361 | msg->rsp_size - 12); | |
3362 | deliver_response(recv_msg); | |
3363 | } | |
3364 | } | |
3365 | ||
3366 | return rv; | |
3367 | } | |
3368 | ||
4dec302f | 3369 | /* |
3370 | * This routine will handle "Get Message" command responses with | |
3371 | * channels that use an OEM Medium. The message format belongs to | |
3372 | * the OEM. See IPMI 2.0 specification, Chapter 6 and | |
3373 | * Chapter 22, sections 22.6 and 22.24 for more details. | |
3374 | */ | |
3375 | static int handle_oem_get_msg_cmd(ipmi_smi_t intf, | |
3376 | struct ipmi_smi_msg *msg) | |
3377 | { | |
3378 | struct cmd_rcvr *rcvr; | |
3379 | int rv = 0; | |
3380 | unsigned char netfn; | |
3381 | unsigned char cmd; | |
3382 | unsigned char chan; | |
3383 | ipmi_user_t user = NULL; | |
3384 | struct ipmi_system_interface_addr *smi_addr; | |
3385 | struct ipmi_recv_msg *recv_msg; | |
3386 | ||
3387 | /* | |
3388 | * We expect the OEM SW to perform error checking | |
3389 | * so we just do some basic sanity checks | |
3390 | */ | |
3391 | if (msg->rsp_size < 4) { | |
3392 | /* Message not big enough, just ignore it. */ | |
3393 | ipmi_inc_stat(intf, invalid_commands); | |
3394 | return 0; | |
3395 | } | |
3396 | ||
3397 | if (msg->rsp[2] != 0) { | |
3398 | /* An error getting the response, just ignore it. */ | |
3399 | return 0; | |
3400 | } | |
3401 | ||
3402 | /* | |
3403 | * This is an OEM Message so the OEM needs to know how | |
3404 | * handle the message. We do no interpretation. | |
3405 | */ | |
3406 | netfn = msg->rsp[0] >> 2; | |
3407 | cmd = msg->rsp[1]; | |
3408 | chan = msg->rsp[3] & 0xf; | |
3409 | ||
3410 | rcu_read_lock(); | |
3411 | rcvr = find_cmd_rcvr(intf, netfn, cmd, chan); | |
3412 | if (rcvr) { | |
3413 | user = rcvr->user; | |
3414 | kref_get(&user->refcount); | |
3415 | } else | |
3416 | user = NULL; | |
3417 | rcu_read_unlock(); | |
3418 | ||
3419 | if (user == NULL) { | |
3420 | /* We didn't find a user, just give up. */ | |
3421 | ipmi_inc_stat(intf, unhandled_commands); | |
3422 | ||
3423 | /* | |
3424 | * Don't do anything with these messages, just allow | |
3425 | * them to be freed. | |
3426 | */ | |
3427 | ||
3428 | rv = 0; | |
3429 | } else { | |
3430 | /* Deliver the message to the user. */ | |
3431 | ipmi_inc_stat(intf, handled_commands); | |
3432 | ||
3433 | recv_msg = ipmi_alloc_recv_msg(); | |
3434 | if (!recv_msg) { | |
3435 | /* | |
3436 | * We couldn't allocate memory for the | |
3437 | * message, so requeue it for handling | |
3438 | * later. | |
3439 | */ | |
3440 | rv = 1; | |
3441 | kref_put(&user->refcount, free_user); | |
3442 | } else { | |
3443 | /* | |
3444 | * OEM Messages are expected to be delivered via | |
3445 | * the system interface to SMS software. We might | |
3446 | * need to visit this again depending on OEM | |
3447 | * requirements | |
3448 | */ | |
3449 | smi_addr = ((struct ipmi_system_interface_addr *) | |
3450 | &(recv_msg->addr)); | |
3451 | smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
3452 | smi_addr->channel = IPMI_BMC_CHANNEL; | |
3453 | smi_addr->lun = msg->rsp[0] & 3; | |
3454 | ||
3455 | recv_msg->user = user; | |
3456 | recv_msg->user_msg_data = NULL; | |
3457 | recv_msg->recv_type = IPMI_OEM_RECV_TYPE; | |
3458 | recv_msg->msg.netfn = msg->rsp[0] >> 2; | |
3459 | recv_msg->msg.cmd = msg->rsp[1]; | |
3460 | recv_msg->msg.data = recv_msg->msg_data; | |
3461 | ||
3462 | /* | |
3463 | * The message starts at byte 4 which follows the | |
3464 | * the Channel Byte in the "GET MESSAGE" command | |
3465 | */ | |
3466 | recv_msg->msg.data_len = msg->rsp_size - 4; | |
3467 | memcpy(recv_msg->msg_data, | |
3468 | &(msg->rsp[4]), | |
3469 | msg->rsp_size - 4); | |
3470 | deliver_response(recv_msg); | |
3471 | } | |
3472 | } | |
3473 | ||
3474 | return rv; | |
3475 | } | |
3476 | ||
1da177e4 LT |
3477 | static void copy_event_into_recv_msg(struct ipmi_recv_msg *recv_msg, |
3478 | struct ipmi_smi_msg *msg) | |
3479 | { | |
3480 | struct ipmi_system_interface_addr *smi_addr; | |
c70d7499 | 3481 | |
1da177e4 LT |
3482 | recv_msg->msgid = 0; |
3483 | smi_addr = (struct ipmi_system_interface_addr *) &(recv_msg->addr); | |
3484 | smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
3485 | smi_addr->channel = IPMI_BMC_CHANNEL; | |
3486 | smi_addr->lun = msg->rsp[0] & 3; | |
3487 | recv_msg->recv_type = IPMI_ASYNC_EVENT_RECV_TYPE; | |
3488 | recv_msg->msg.netfn = msg->rsp[0] >> 2; | |
3489 | recv_msg->msg.cmd = msg->rsp[1]; | |
3490 | memcpy(recv_msg->msg_data, &(msg->rsp[3]), msg->rsp_size - 3); | |
3491 | recv_msg->msg.data = recv_msg->msg_data; | |
3492 | recv_msg->msg.data_len = msg->rsp_size - 3; | |
3493 | } | |
3494 | ||
1da177e4 LT |
3495 | static int handle_read_event_rsp(ipmi_smi_t intf, |
3496 | struct ipmi_smi_msg *msg) | |
3497 | { | |
3498 | struct ipmi_recv_msg *recv_msg, *recv_msg2; | |
3499 | struct list_head msgs; | |
3500 | ipmi_user_t user; | |
3501 | int rv = 0; | |
3502 | int deliver_count = 0; | |
3503 | unsigned long flags; | |
3504 | ||
3505 | if (msg->rsp_size < 19) { | |
3506 | /* Message is too small to be an IPMB event. */ | |
b2655f26 | 3507 | ipmi_inc_stat(intf, invalid_events); |
1da177e4 LT |
3508 | return 0; |
3509 | } | |
3510 | ||
3511 | if (msg->rsp[2] != 0) { | |
3512 | /* An error getting the event, just ignore it. */ | |
3513 | return 0; | |
3514 | } | |
3515 | ||
3516 | INIT_LIST_HEAD(&msgs); | |
3517 | ||
393d2cc3 | 3518 | spin_lock_irqsave(&intf->events_lock, flags); |
1da177e4 | 3519 | |
b2655f26 | 3520 | ipmi_inc_stat(intf, events); |
1da177e4 | 3521 | |
c70d7499 CM |
3522 | /* |
3523 | * Allocate and fill in one message for every user that is | |
3524 | * getting events. | |
3525 | */ | |
393d2cc3 CM |
3526 | rcu_read_lock(); |
3527 | list_for_each_entry_rcu(user, &intf->users, link) { | |
8a3628d5 | 3528 | if (!user->gets_events) |
1da177e4 LT |
3529 | continue; |
3530 | ||
3531 | recv_msg = ipmi_alloc_recv_msg(); | |
8a3628d5 | 3532 | if (!recv_msg) { |
393d2cc3 | 3533 | rcu_read_unlock(); |
8a3628d5 CM |
3534 | list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, |
3535 | link) { | |
1da177e4 LT |
3536 | list_del(&recv_msg->link); |
3537 | ipmi_free_recv_msg(recv_msg); | |
3538 | } | |
c70d7499 CM |
3539 | /* |
3540 | * We couldn't allocate memory for the | |
3541 | * message, so requeue it for handling | |
3542 | * later. | |
3543 | */ | |
1da177e4 LT |
3544 | rv = 1; |
3545 | goto out; | |
3546 | } | |
3547 | ||
3548 | deliver_count++; | |
3549 | ||
3550 | copy_event_into_recv_msg(recv_msg, msg); | |
3551 | recv_msg->user = user; | |
393d2cc3 | 3552 | kref_get(&user->refcount); |
1da177e4 LT |
3553 | list_add_tail(&(recv_msg->link), &msgs); |
3554 | } | |
393d2cc3 | 3555 | rcu_read_unlock(); |
1da177e4 LT |
3556 | |
3557 | if (deliver_count) { | |
3558 | /* Now deliver all the messages. */ | |
3559 | list_for_each_entry_safe(recv_msg, recv_msg2, &msgs, link) { | |
3560 | list_del(&recv_msg->link); | |
3561 | deliver_response(recv_msg); | |
3562 | } | |
3563 | } else if (intf->waiting_events_count < MAX_EVENTS_IN_QUEUE) { | |
c70d7499 CM |
3564 | /* |
3565 | * No one to receive the message, put it in queue if there's | |
3566 | * not already too many things in the queue. | |
3567 | */ | |
1da177e4 | 3568 | recv_msg = ipmi_alloc_recv_msg(); |
8a3628d5 | 3569 | if (!recv_msg) { |
c70d7499 CM |
3570 | /* |
3571 | * We couldn't allocate memory for the | |
3572 | * message, so requeue it for handling | |
3573 | * later. | |
3574 | */ | |
1da177e4 LT |
3575 | rv = 1; |
3576 | goto out; | |
3577 | } | |
3578 | ||
3579 | copy_event_into_recv_msg(recv_msg, msg); | |
3580 | list_add_tail(&(recv_msg->link), &(intf->waiting_events)); | |
4791c03d | 3581 | intf->waiting_events_count++; |
87ebd06f | 3582 | } else if (!intf->event_msg_printed) { |
c70d7499 CM |
3583 | /* |
3584 | * There's too many things in the queue, discard this | |
3585 | * message. | |
3586 | */ | |
87ebd06f CM |
3587 | printk(KERN_WARNING PFX "Event queue full, discarding" |
3588 | " incoming events\n"); | |
3589 | intf->event_msg_printed = 1; | |
1da177e4 LT |
3590 | } |
3591 | ||
3592 | out: | |
3593 | spin_unlock_irqrestore(&(intf->events_lock), flags); | |
3594 | ||
3595 | return rv; | |
3596 | } | |
3597 | ||
3598 | static int handle_bmc_rsp(ipmi_smi_t intf, | |
3599 | struct ipmi_smi_msg *msg) | |
3600 | { | |
3601 | struct ipmi_recv_msg *recv_msg; | |
393d2cc3 | 3602 | struct ipmi_user *user; |
1da177e4 LT |
3603 | |
3604 | recv_msg = (struct ipmi_recv_msg *) msg->user_data; | |
c70d7499 CM |
3605 | if (recv_msg == NULL) { |
3606 | printk(KERN_WARNING | |
3607 | "IPMI message received with no owner. This\n" | |
3608 | "could be because of a malformed message, or\n" | |
3609 | "because of a hardware error. Contact your\n" | |
3610 | "hardware vender for assistance\n"); | |
56a55ec6 CM |
3611 | return 0; |
3612 | } | |
1da177e4 | 3613 | |
393d2cc3 | 3614 | user = recv_msg->user; |
1da177e4 | 3615 | /* Make sure the user still exists. */ |
393d2cc3 | 3616 | if (user && !user->valid) { |
56a55ec6 | 3617 | /* The user for the message went away, so give up. */ |
b2655f26 | 3618 | ipmi_inc_stat(intf, unhandled_local_responses); |
1da177e4 LT |
3619 | ipmi_free_recv_msg(recv_msg); |
3620 | } else { | |
3621 | struct ipmi_system_interface_addr *smi_addr; | |
3622 | ||
b2655f26 | 3623 | ipmi_inc_stat(intf, handled_local_responses); |
1da177e4 LT |
3624 | recv_msg->recv_type = IPMI_RESPONSE_RECV_TYPE; |
3625 | recv_msg->msgid = msg->msgid; | |
3626 | smi_addr = ((struct ipmi_system_interface_addr *) | |
3627 | &(recv_msg->addr)); | |
3628 | smi_addr->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
3629 | smi_addr->channel = IPMI_BMC_CHANNEL; | |
3630 | smi_addr->lun = msg->rsp[0] & 3; | |
3631 | recv_msg->msg.netfn = msg->rsp[0] >> 2; | |
3632 | recv_msg->msg.cmd = msg->rsp[1]; | |
3633 | memcpy(recv_msg->msg_data, | |
3634 | &(msg->rsp[2]), | |
3635 | msg->rsp_size - 2); | |
3636 | recv_msg->msg.data = recv_msg->msg_data; | |
3637 | recv_msg->msg.data_len = msg->rsp_size - 2; | |
3638 | deliver_response(recv_msg); | |
3639 | } | |
3640 | ||
3641 | return 0; | |
3642 | } | |
3643 | ||
c70d7499 | 3644 | /* |
7adf579c | 3645 | * Handle a received message. Return 1 if the message should be requeued, |
c70d7499 CM |
3646 | * 0 if the message should be freed, or -1 if the message should not |
3647 | * be freed or requeued. | |
3648 | */ | |
7adf579c | 3649 | static int handle_one_recv_msg(ipmi_smi_t intf, |
1da177e4 LT |
3650 | struct ipmi_smi_msg *msg) |
3651 | { | |
3652 | int requeue; | |
3653 | int chan; | |
3654 | ||
3655 | #ifdef DEBUG_MSGING | |
3656 | int m; | |
3657 | printk("Recv:"); | |
e8b33617 | 3658 | for (m = 0; m < msg->rsp_size; m++) |
1da177e4 LT |
3659 | printk(" %2.2x", msg->rsp[m]); |
3660 | printk("\n"); | |
3661 | #endif | |
3662 | if (msg->rsp_size < 2) { | |
3663 | /* Message is too small to be correct. */ | |
3664 | printk(KERN_WARNING PFX "BMC returned to small a message" | |
3665 | " for netfn %x cmd %x, got %d bytes\n", | |
3666 | (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size); | |
3667 | ||
3668 | /* Generate an error response for the message. */ | |
3669 | msg->rsp[0] = msg->data[0] | (1 << 2); | |
3670 | msg->rsp[1] = msg->data[1]; | |
3671 | msg->rsp[2] = IPMI_ERR_UNSPECIFIED; | |
3672 | msg->rsp_size = 3; | |
c70d7499 CM |
3673 | } else if (((msg->rsp[0] >> 2) != ((msg->data[0] >> 2) | 1)) |
3674 | || (msg->rsp[1] != msg->data[1])) { | |
3675 | /* | |
3676 | * The NetFN and Command in the response is not even | |
3677 | * marginally correct. | |
3678 | */ | |
1da177e4 LT |
3679 | printk(KERN_WARNING PFX "BMC returned incorrect response," |
3680 | " expected netfn %x cmd %x, got netfn %x cmd %x\n", | |
3681 | (msg->data[0] >> 2) | 1, msg->data[1], | |
3682 | msg->rsp[0] >> 2, msg->rsp[1]); | |
3683 | ||
3684 | /* Generate an error response for the message. */ | |
3685 | msg->rsp[0] = msg->data[0] | (1 << 2); | |
3686 | msg->rsp[1] = msg->data[1]; | |
3687 | msg->rsp[2] = IPMI_ERR_UNSPECIFIED; | |
3688 | msg->rsp_size = 3; | |
3689 | } | |
3690 | ||
3691 | if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) | |
3692 | && (msg->rsp[1] == IPMI_SEND_MSG_CMD) | |
c70d7499 CM |
3693 | && (msg->user_data != NULL)) { |
3694 | /* | |
3695 | * It's a response to a response we sent. For this we | |
3696 | * deliver a send message response to the user. | |
3697 | */ | |
393d2cc3 | 3698 | struct ipmi_recv_msg *recv_msg = msg->user_data; |
1da177e4 LT |
3699 | |
3700 | requeue = 0; | |
3701 | if (msg->rsp_size < 2) | |
3702 | /* Message is too small to be correct. */ | |
3703 | goto out; | |
3704 | ||
3705 | chan = msg->data[2] & 0x0f; | |
3706 | if (chan >= IPMI_MAX_CHANNELS) | |
3707 | /* Invalid channel number */ | |
3708 | goto out; | |
3709 | ||
393d2cc3 CM |
3710 | if (!recv_msg) |
3711 | goto out; | |
3712 | ||
3713 | /* Make sure the user still exists. */ | |
3714 | if (!recv_msg->user || !recv_msg->user->valid) | |
3715 | goto out; | |
3716 | ||
3717 | recv_msg->recv_type = IPMI_RESPONSE_RESPONSE_TYPE; | |
3718 | recv_msg->msg.data = recv_msg->msg_data; | |
3719 | recv_msg->msg.data_len = 1; | |
3720 | recv_msg->msg_data[0] = msg->rsp[2]; | |
3721 | deliver_response(recv_msg); | |
1da177e4 | 3722 | } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) |
c70d7499 | 3723 | && (msg->rsp[1] == IPMI_GET_MSG_CMD)) { |
1da177e4 LT |
3724 | /* It's from the receive queue. */ |
3725 | chan = msg->rsp[3] & 0xf; | |
3726 | if (chan >= IPMI_MAX_CHANNELS) { | |
3727 | /* Invalid channel number */ | |
3728 | requeue = 0; | |
3729 | goto out; | |
3730 | } | |
3731 | ||
4dec302f | 3732 | /* |
9a2845c4 CM |
3733 | * We need to make sure the channels have been initialized. |
3734 | * The channel_handler routine will set the "curr_channel" | |
3735 | * equal to or greater than IPMI_MAX_CHANNELS when all the | |
3736 | * channels for this interface have been initialized. | |
3737 | */ | |
4dec302f | 3738 | if (intf->curr_channel < IPMI_MAX_CHANNELS) { |
9a2845c4 | 3739 | requeue = 0; /* Throw the message away */ |
4dec302f | 3740 | goto out; |
3741 | } | |
3742 | ||
1da177e4 LT |
3743 | switch (intf->channels[chan].medium) { |
3744 | case IPMI_CHANNEL_MEDIUM_IPMB: | |
3745 | if (msg->rsp[4] & 0x04) { | |
c70d7499 CM |
3746 | /* |
3747 | * It's a response, so find the | |
3748 | * requesting message and send it up. | |
3749 | */ | |
1da177e4 LT |
3750 | requeue = handle_ipmb_get_msg_rsp(intf, msg); |
3751 | } else { | |
c70d7499 CM |
3752 | /* |
3753 | * It's a command to the SMS from some other | |
3754 | * entity. Handle that. | |
3755 | */ | |
1da177e4 LT |
3756 | requeue = handle_ipmb_get_msg_cmd(intf, msg); |
3757 | } | |
3758 | break; | |
3759 | ||
3760 | case IPMI_CHANNEL_MEDIUM_8023LAN: | |
3761 | case IPMI_CHANNEL_MEDIUM_ASYNC: | |
3762 | if (msg->rsp[6] & 0x04) { | |
c70d7499 CM |
3763 | /* |
3764 | * It's a response, so find the | |
3765 | * requesting message and send it up. | |
3766 | */ | |
1da177e4 LT |
3767 | requeue = handle_lan_get_msg_rsp(intf, msg); |
3768 | } else { | |
c70d7499 CM |
3769 | /* |
3770 | * It's a command to the SMS from some other | |
3771 | * entity. Handle that. | |
3772 | */ | |
1da177e4 LT |
3773 | requeue = handle_lan_get_msg_cmd(intf, msg); |
3774 | } | |
3775 | break; | |
3776 | ||
3777 | default: | |
4dec302f | 3778 | /* Check for OEM Channels. Clients had better |
3779 | register for these commands. */ | |
3780 | if ((intf->channels[chan].medium | |
3781 | >= IPMI_CHANNEL_MEDIUM_OEM_MIN) | |
3782 | && (intf->channels[chan].medium | |
3783 | <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) { | |
3784 | requeue = handle_oem_get_msg_cmd(intf, msg); | |
3785 | } else { | |
3786 | /* | |
3787 | * We don't handle the channel type, so just | |
3788 | * free the message. | |
3789 | */ | |
3790 | requeue = 0; | |
3791 | } | |
1da177e4 LT |
3792 | } |
3793 | ||
3794 | } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) | |
c70d7499 | 3795 | && (msg->rsp[1] == IPMI_READ_EVENT_MSG_BUFFER_CMD)) { |
b3834be5 | 3796 | /* It's an asynchronous event. */ |
1da177e4 LT |
3797 | requeue = handle_read_event_rsp(intf, msg); |
3798 | } else { | |
3799 | /* It's a response from the local BMC. */ | |
3800 | requeue = handle_bmc_rsp(intf, msg); | |
3801 | } | |
3802 | ||
3803 | out: | |
3804 | return requeue; | |
3805 | } | |
3806 | ||
7adf579c CM |
3807 | /* |
3808 | * If there are messages in the queue or pretimeouts, handle them. | |
3809 | */ | |
3810 | static void handle_new_recv_msgs(ipmi_smi_t intf) | |
3811 | { | |
3812 | struct ipmi_smi_msg *smi_msg; | |
3813 | unsigned long flags = 0; | |
3814 | int rv; | |
3815 | int run_to_completion = intf->run_to_completion; | |
3816 | ||
3817 | /* See if any waiting messages need to be processed. */ | |
3818 | if (!run_to_completion) | |
65be7544 CM |
3819 | spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); |
3820 | while (!list_empty(&intf->waiting_rcv_msgs)) { | |
3821 | smi_msg = list_entry(intf->waiting_rcv_msgs.next, | |
7adf579c | 3822 | struct ipmi_smi_msg, link); |
ae4ea9a2 | 3823 | list_del(&smi_msg->link); |
7adf579c | 3824 | if (!run_to_completion) |
65be7544 CM |
3825 | spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, |
3826 | flags); | |
7adf579c CM |
3827 | rv = handle_one_recv_msg(intf, smi_msg); |
3828 | if (!run_to_completion) | |
65be7544 | 3829 | spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); |
7ea0ed2b | 3830 | if (rv > 0) { |
7adf579c CM |
3831 | /* |
3832 | * To preserve message order, quit if we | |
ae4ea9a2 JN |
3833 | * can't handle a message. Add the message |
3834 | * back at the head, this is safe because this | |
3835 | * tasklet is the only thing that pulls the | |
3836 | * messages. | |
7adf579c | 3837 | */ |
ae4ea9a2 | 3838 | list_add(&smi_msg->link, &intf->waiting_rcv_msgs); |
7adf579c | 3839 | break; |
7ea0ed2b | 3840 | } else { |
7ea0ed2b CM |
3841 | if (rv == 0) |
3842 | /* Message handled */ | |
3843 | ipmi_free_smi_msg(smi_msg); | |
3844 | /* If rv < 0, fatal error, del but don't free. */ | |
7adf579c CM |
3845 | } |
3846 | } | |
3847 | if (!run_to_completion) | |
65be7544 | 3848 | spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, flags); |
7adf579c CM |
3849 | |
3850 | /* | |
3851 | * If the pretimout count is non-zero, decrement one from it and | |
3852 | * deliver pretimeouts to all the users. | |
3853 | */ | |
3854 | if (atomic_add_unless(&intf->watchdog_pretimeouts_to_deliver, -1, 0)) { | |
3855 | ipmi_user_t user; | |
3856 | ||
3857 | rcu_read_lock(); | |
3858 | list_for_each_entry_rcu(user, &intf->users, link) { | |
3859 | if (user->handler->ipmi_watchdog_pretimeout) | |
3860 | user->handler->ipmi_watchdog_pretimeout( | |
3861 | user->handler_data); | |
3862 | } | |
3863 | rcu_read_unlock(); | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | static void smi_recv_tasklet(unsigned long val) | |
3868 | { | |
7ea0ed2b CM |
3869 | unsigned long flags = 0; /* keep us warning-free. */ |
3870 | ipmi_smi_t intf = (ipmi_smi_t) val; | |
3871 | int run_to_completion = intf->run_to_completion; | |
3872 | struct ipmi_smi_msg *newmsg = NULL; | |
3873 | ||
3874 | /* | |
3875 | * Start the next message if available. | |
3876 | * | |
3877 | * Do this here, not in the actual receiver, because we may deadlock | |
3878 | * because the lower layer is allowed to hold locks while calling | |
3879 | * message delivery. | |
3880 | */ | |
cdea4656 TC |
3881 | |
3882 | rcu_read_lock(); | |
3883 | ||
7ea0ed2b CM |
3884 | if (!run_to_completion) |
3885 | spin_lock_irqsave(&intf->xmit_msgs_lock, flags); | |
3886 | if (intf->curr_msg == NULL && !intf->in_shutdown) { | |
3887 | struct list_head *entry = NULL; | |
3888 | ||
3889 | /* Pick the high priority queue first. */ | |
3890 | if (!list_empty(&intf->hp_xmit_msgs)) | |
3891 | entry = intf->hp_xmit_msgs.next; | |
3892 | else if (!list_empty(&intf->xmit_msgs)) | |
3893 | entry = intf->xmit_msgs.next; | |
3894 | ||
3895 | if (entry) { | |
3896 | list_del(entry); | |
3897 | newmsg = list_entry(entry, struct ipmi_smi_msg, link); | |
3898 | intf->curr_msg = newmsg; | |
3899 | } | |
3900 | } | |
3901 | if (!run_to_completion) | |
3902 | spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); | |
3903 | if (newmsg) | |
99ab32f3 | 3904 | intf->handlers->sender(intf->send_info, newmsg); |
7ea0ed2b | 3905 | |
cdea4656 TC |
3906 | rcu_read_unlock(); |
3907 | ||
7ea0ed2b | 3908 | handle_new_recv_msgs(intf); |
7adf579c CM |
3909 | } |
3910 | ||
1da177e4 LT |
3911 | /* Handle a new message from the lower layer. */ |
3912 | void ipmi_smi_msg_received(ipmi_smi_t intf, | |
3913 | struct ipmi_smi_msg *msg) | |
3914 | { | |
5956dce1 | 3915 | unsigned long flags = 0; /* keep us warning-free. */ |
7ea0ed2b | 3916 | int run_to_completion = intf->run_to_completion; |
1da177e4 | 3917 | |
1da177e4 LT |
3918 | if ((msg->data_size >= 2) |
3919 | && (msg->data[0] == (IPMI_NETFN_APP_REQUEST << 2)) | |
3920 | && (msg->data[1] == IPMI_SEND_MSG_CMD) | |
c70d7499 | 3921 | && (msg->user_data == NULL)) { |
7ea0ed2b CM |
3922 | |
3923 | if (intf->in_shutdown) | |
3924 | goto free_msg; | |
3925 | ||
c70d7499 CM |
3926 | /* |
3927 | * This is the local response to a command send, start | |
3928 | * the timer for these. The user_data will not be | |
3929 | * NULL if this is a response send, and we will let | |
3930 | * response sends just go through. | |
3931 | */ | |
3932 | ||
3933 | /* | |
3934 | * Check for errors, if we get certain errors (ones | |
3935 | * that mean basically we can try again later), we | |
3936 | * ignore them and start the timer. Otherwise we | |
3937 | * report the error immediately. | |
3938 | */ | |
1da177e4 LT |
3939 | if ((msg->rsp_size >= 3) && (msg->rsp[2] != 0) |
3940 | && (msg->rsp[2] != IPMI_NODE_BUSY_ERR) | |
46d52b09 CM |
3941 | && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR) |
3942 | && (msg->rsp[2] != IPMI_BUS_ERR) | |
c70d7499 | 3943 | && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) { |
1da177e4 LT |
3944 | int chan = msg->rsp[3] & 0xf; |
3945 | ||
3946 | /* Got an error sending the message, handle it. */ | |
1da177e4 LT |
3947 | if (chan >= IPMI_MAX_CHANNELS) |
3948 | ; /* This shouldn't happen */ | |
3949 | else if ((intf->channels[chan].medium | |
3950 | == IPMI_CHANNEL_MEDIUM_8023LAN) | |
3951 | || (intf->channels[chan].medium | |
3952 | == IPMI_CHANNEL_MEDIUM_ASYNC)) | |
b2655f26 | 3953 | ipmi_inc_stat(intf, sent_lan_command_errs); |
1da177e4 | 3954 | else |
b2655f26 | 3955 | ipmi_inc_stat(intf, sent_ipmb_command_errs); |
1da177e4 | 3956 | intf_err_seq(intf, msg->msgid, msg->rsp[2]); |
c70d7499 | 3957 | } else |
1da177e4 LT |
3958 | /* The message was sent, start the timer. */ |
3959 | intf_start_seq_timer(intf, msg->msgid); | |
1da177e4 | 3960 | |
7ea0ed2b | 3961 | free_msg: |
1da177e4 | 3962 | ipmi_free_smi_msg(msg); |
7ea0ed2b CM |
3963 | } else { |
3964 | /* | |
3965 | * To preserve message order, we keep a queue and deliver from | |
3966 | * a tasklet. | |
3967 | */ | |
3968 | if (!run_to_completion) | |
3969 | spin_lock_irqsave(&intf->waiting_rcv_msgs_lock, flags); | |
3970 | list_add_tail(&msg->link, &intf->waiting_rcv_msgs); | |
3971 | if (!run_to_completion) | |
3972 | spin_unlock_irqrestore(&intf->waiting_rcv_msgs_lock, | |
3973 | flags); | |
1da177e4 LT |
3974 | } |
3975 | ||
5956dce1 | 3976 | if (!run_to_completion) |
7ea0ed2b | 3977 | spin_lock_irqsave(&intf->xmit_msgs_lock, flags); |
b2234ee9 CM |
3978 | /* |
3979 | * We can get an asynchronous event or receive message in addition | |
3980 | * to commands we send. | |
3981 | */ | |
7ea0ed2b CM |
3982 | if (msg == intf->curr_msg) |
3983 | intf->curr_msg = NULL; | |
5956dce1 | 3984 | if (!run_to_completion) |
7ea0ed2b | 3985 | spin_unlock_irqrestore(&intf->xmit_msgs_lock, flags); |
c70d7499 | 3986 | |
7ea0ed2b CM |
3987 | if (run_to_completion) |
3988 | smi_recv_tasklet((unsigned long) intf); | |
3989 | else | |
3990 | tasklet_schedule(&intf->recv_tasklet); | |
1da177e4 | 3991 | } |
c70d7499 | 3992 | EXPORT_SYMBOL(ipmi_smi_msg_received); |
1da177e4 LT |
3993 | |
3994 | void ipmi_smi_watchdog_pretimeout(ipmi_smi_t intf) | |
3995 | { | |
7ea0ed2b CM |
3996 | if (intf->in_shutdown) |
3997 | return; | |
3998 | ||
7adf579c CM |
3999 | atomic_set(&intf->watchdog_pretimeouts_to_deliver, 1); |
4000 | tasklet_schedule(&intf->recv_tasklet); | |
1da177e4 | 4001 | } |
c70d7499 | 4002 | EXPORT_SYMBOL(ipmi_smi_watchdog_pretimeout); |
1da177e4 | 4003 | |
882fe011 CM |
4004 | static struct ipmi_smi_msg * |
4005 | smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg, | |
4006 | unsigned char seq, long seqid) | |
1da177e4 | 4007 | { |
882fe011 | 4008 | struct ipmi_smi_msg *smi_msg = ipmi_alloc_smi_msg(); |
1da177e4 | 4009 | if (!smi_msg) |
c70d7499 CM |
4010 | /* |
4011 | * If we can't allocate the message, then just return, we | |
4012 | * get 4 retries, so this should be ok. | |
4013 | */ | |
882fe011 | 4014 | return NULL; |
1da177e4 LT |
4015 | |
4016 | memcpy(smi_msg->data, recv_msg->msg.data, recv_msg->msg.data_len); | |
4017 | smi_msg->data_size = recv_msg->msg.data_len; | |
4018 | smi_msg->msgid = STORE_SEQ_IN_MSGID(seq, seqid); | |
c70d7499 | 4019 | |
1da177e4 LT |
4020 | #ifdef DEBUG_MSGING |
4021 | { | |
4022 | int m; | |
4023 | printk("Resend: "); | |
e8b33617 | 4024 | for (m = 0; m < smi_msg->data_size; m++) |
1da177e4 LT |
4025 | printk(" %2.2x", smi_msg->data[m]); |
4026 | printk("\n"); | |
4027 | } | |
4028 | #endif | |
882fe011 | 4029 | return smi_msg; |
1da177e4 LT |
4030 | } |
4031 | ||
393d2cc3 | 4032 | static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent, |
ffb3a56f CM |
4033 | struct list_head *timeouts, |
4034 | unsigned long timeout_period, | |
89986496 CM |
4035 | int slot, unsigned long *flags, |
4036 | unsigned int *waiting_msgs) | |
393d2cc3 | 4037 | { |
b2c03941 | 4038 | struct ipmi_recv_msg *msg; |
81d02b7f | 4039 | const struct ipmi_smi_handlers *handlers; |
b2c03941 | 4040 | |
7ea0ed2b | 4041 | if (intf->in_shutdown) |
b2c03941 | 4042 | return; |
393d2cc3 CM |
4043 | |
4044 | if (!ent->inuse) | |
4045 | return; | |
4046 | ||
ffb3a56f CM |
4047 | if (timeout_period < ent->timeout) { |
4048 | ent->timeout -= timeout_period; | |
89986496 | 4049 | (*waiting_msgs)++; |
393d2cc3 | 4050 | return; |
89986496 | 4051 | } |
393d2cc3 CM |
4052 | |
4053 | if (ent->retries_left == 0) { | |
4054 | /* The message has used all its retries. */ | |
4055 | ent->inuse = 0; | |
4056 | msg = ent->recv_msg; | |
4057 | list_add_tail(&msg->link, timeouts); | |
393d2cc3 | 4058 | if (ent->broadcast) |
b2655f26 | 4059 | ipmi_inc_stat(intf, timed_out_ipmb_broadcasts); |
25176ed6 | 4060 | else if (is_lan_addr(&ent->recv_msg->addr)) |
b2655f26 | 4061 | ipmi_inc_stat(intf, timed_out_lan_commands); |
393d2cc3 | 4062 | else |
b2655f26 | 4063 | ipmi_inc_stat(intf, timed_out_ipmb_commands); |
393d2cc3 CM |
4064 | } else { |
4065 | struct ipmi_smi_msg *smi_msg; | |
4066 | /* More retries, send again. */ | |
4067 | ||
89986496 CM |
4068 | (*waiting_msgs)++; |
4069 | ||
c70d7499 CM |
4070 | /* |
4071 | * Start with the max timer, set to normal timer after | |
4072 | * the message is sent. | |
4073 | */ | |
393d2cc3 CM |
4074 | ent->timeout = MAX_MSG_TIMEOUT; |
4075 | ent->retries_left--; | |
393d2cc3 CM |
4076 | smi_msg = smi_from_recv_msg(intf, ent->recv_msg, slot, |
4077 | ent->seqid); | |
25176ed6 CM |
4078 | if (!smi_msg) { |
4079 | if (is_lan_addr(&ent->recv_msg->addr)) | |
4080 | ipmi_inc_stat(intf, | |
4081 | dropped_rexmit_lan_commands); | |
4082 | else | |
4083 | ipmi_inc_stat(intf, | |
4084 | dropped_rexmit_ipmb_commands); | |
393d2cc3 | 4085 | return; |
25176ed6 | 4086 | } |
393d2cc3 CM |
4087 | |
4088 | spin_unlock_irqrestore(&intf->seq_lock, *flags); | |
b2c03941 | 4089 | |
c70d7499 CM |
4090 | /* |
4091 | * Send the new message. We send with a zero | |
4092 | * priority. It timed out, I doubt time is that | |
4093 | * critical now, and high priority messages are really | |
4094 | * only for messages to the local MC, which don't get | |
4095 | * resent. | |
4096 | */ | |
b2c03941 | 4097 | handlers = intf->handlers; |
25176ed6 CM |
4098 | if (handlers) { |
4099 | if (is_lan_addr(&ent->recv_msg->addr)) | |
4100 | ipmi_inc_stat(intf, | |
4101 | retransmitted_lan_commands); | |
4102 | else | |
4103 | ipmi_inc_stat(intf, | |
4104 | retransmitted_ipmb_commands); | |
4105 | ||
81d02b7f | 4106 | smi_send(intf, handlers, smi_msg, 0); |
25176ed6 | 4107 | } else |
b2c03941 CM |
4108 | ipmi_free_smi_msg(smi_msg); |
4109 | ||
393d2cc3 CM |
4110 | spin_lock_irqsave(&intf->seq_lock, *flags); |
4111 | } | |
4112 | } | |
4113 | ||
ffb3a56f CM |
4114 | static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, |
4115 | unsigned long timeout_period) | |
1da177e4 | 4116 | { |
1da177e4 LT |
4117 | struct list_head timeouts; |
4118 | struct ipmi_recv_msg *msg, *msg2; | |
1da177e4 | 4119 | unsigned long flags; |
bca0324d | 4120 | int i; |
89986496 | 4121 | unsigned int waiting_msgs = 0; |
1da177e4 | 4122 | |
89986496 CM |
4123 | /* |
4124 | * Go through the seq table and find any messages that | |
4125 | * have timed out, putting them in the timeouts | |
4126 | * list. | |
4127 | */ | |
4128 | INIT_LIST_HEAD(&timeouts); | |
4129 | spin_lock_irqsave(&intf->seq_lock, flags); | |
4130 | for (i = 0; i < IPMI_IPMB_NUM_SEQ; i++) | |
4131 | check_msg_timeout(intf, &(intf->seq_table[i]), | |
4132 | &timeouts, timeout_period, i, | |
4133 | &flags, &waiting_msgs); | |
4134 | spin_unlock_irqrestore(&intf->seq_lock, flags); | |
393d2cc3 | 4135 | |
89986496 CM |
4136 | list_for_each_entry_safe(msg, msg2, &timeouts, link) |
4137 | deliver_err_response(msg, IPMI_TIMEOUT_COMPLETION_CODE); | |
b9675136 | 4138 | |
89986496 CM |
4139 | /* |
4140 | * Maintenance mode handling. Check the timeout | |
4141 | * optimistically before we claim the lock. It may | |
4142 | * mean a timeout gets missed occasionally, but that | |
4143 | * only means the timeout gets extended by one period | |
4144 | * in that case. No big deal, and it avoids the lock | |
4145 | * most of the time. | |
4146 | */ | |
4147 | if (intf->auto_maintenance_timeout > 0) { | |
4148 | spin_lock_irqsave(&intf->maintenance_mode_lock, flags); | |
b9675136 | 4149 | if (intf->auto_maintenance_timeout > 0) { |
89986496 CM |
4150 | intf->auto_maintenance_timeout |
4151 | -= timeout_period; | |
4152 | if (!intf->maintenance_mode | |
4153 | && (intf->auto_maintenance_timeout <= 0)) { | |
7aefac26 | 4154 | intf->maintenance_mode_enable = false; |
89986496 | 4155 | maintenance_mode_update(intf); |
b9675136 | 4156 | } |
b9675136 | 4157 | } |
89986496 CM |
4158 | spin_unlock_irqrestore(&intf->maintenance_mode_lock, |
4159 | flags); | |
1da177e4 | 4160 | } |
89986496 CM |
4161 | |
4162 | tasklet_schedule(&intf->recv_tasklet); | |
4163 | ||
4164 | return waiting_msgs; | |
1da177e4 LT |
4165 | } |
4166 | ||
89986496 | 4167 | static void ipmi_request_event(ipmi_smi_t intf) |
1da177e4 | 4168 | { |
89986496 CM |
4169 | /* No event requests when in maintenance mode. */ |
4170 | if (intf->maintenance_mode_enable) | |
4171 | return; | |
b9675136 | 4172 | |
7ea0ed2b CM |
4173 | if (!intf->in_shutdown) |
4174 | intf->handlers->request_events(intf->send_info); | |
1da177e4 LT |
4175 | } |
4176 | ||
4177 | static struct timer_list ipmi_timer; | |
4178 | ||
8f43f84f | 4179 | static atomic_t stop_operation; |
1da177e4 LT |
4180 | |
4181 | static void ipmi_timeout(unsigned long data) | |
4182 | { | |
89986496 CM |
4183 | ipmi_smi_t intf; |
4184 | int nt = 0; | |
4185 | ||
8f43f84f | 4186 | if (atomic_read(&stop_operation)) |
1da177e4 | 4187 | return; |
1da177e4 | 4188 | |
89986496 CM |
4189 | rcu_read_lock(); |
4190 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { | |
4191 | int lnt = 0; | |
4192 | ||
4193 | if (atomic_read(&intf->event_waiters)) { | |
4194 | intf->ticks_to_req_ev--; | |
4195 | if (intf->ticks_to_req_ev == 0) { | |
4196 | ipmi_request_event(intf); | |
4197 | intf->ticks_to_req_ev = IPMI_REQUEST_EV_TIME; | |
4198 | } | |
4199 | lnt++; | |
4200 | } | |
4201 | ||
4202 | lnt += ipmi_timeout_handler(intf, IPMI_TIMEOUT_TIME); | |
1da177e4 | 4203 | |
89986496 CM |
4204 | lnt = !!lnt; |
4205 | if (lnt != intf->last_needs_timer && | |
4206 | intf->handlers->set_need_watch) | |
4207 | intf->handlers->set_need_watch(intf->send_info, lnt); | |
4208 | intf->last_needs_timer = lnt; | |
1da177e4 | 4209 | |
89986496 CM |
4210 | nt += lnt; |
4211 | } | |
4212 | rcu_read_unlock(); | |
4213 | ||
4214 | if (nt) | |
4215 | mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); | |
1da177e4 LT |
4216 | } |
4217 | ||
89986496 CM |
4218 | static void need_waiter(ipmi_smi_t intf) |
4219 | { | |
4220 | /* Racy, but worst case we start the timer twice. */ | |
4221 | if (!timer_pending(&ipmi_timer)) | |
4222 | mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); | |
4223 | } | |
1da177e4 LT |
4224 | |
4225 | static atomic_t smi_msg_inuse_count = ATOMIC_INIT(0); | |
4226 | static atomic_t recv_msg_inuse_count = ATOMIC_INIT(0); | |
4227 | ||
1da177e4 LT |
4228 | static void free_smi_msg(struct ipmi_smi_msg *msg) |
4229 | { | |
4230 | atomic_dec(&smi_msg_inuse_count); | |
4231 | kfree(msg); | |
4232 | } | |
4233 | ||
4234 | struct ipmi_smi_msg *ipmi_alloc_smi_msg(void) | |
4235 | { | |
4236 | struct ipmi_smi_msg *rv; | |
4237 | rv = kmalloc(sizeof(struct ipmi_smi_msg), GFP_ATOMIC); | |
4238 | if (rv) { | |
4239 | rv->done = free_smi_msg; | |
4240 | rv->user_data = NULL; | |
4241 | atomic_inc(&smi_msg_inuse_count); | |
4242 | } | |
4243 | return rv; | |
4244 | } | |
c70d7499 | 4245 | EXPORT_SYMBOL(ipmi_alloc_smi_msg); |
1da177e4 LT |
4246 | |
4247 | static void free_recv_msg(struct ipmi_recv_msg *msg) | |
4248 | { | |
4249 | atomic_dec(&recv_msg_inuse_count); | |
4250 | kfree(msg); | |
4251 | } | |
4252 | ||
74006309 | 4253 | static struct ipmi_recv_msg *ipmi_alloc_recv_msg(void) |
1da177e4 LT |
4254 | { |
4255 | struct ipmi_recv_msg *rv; | |
4256 | ||
4257 | rv = kmalloc(sizeof(struct ipmi_recv_msg), GFP_ATOMIC); | |
4258 | if (rv) { | |
a9eec556 | 4259 | rv->user = NULL; |
1da177e4 LT |
4260 | rv->done = free_recv_msg; |
4261 | atomic_inc(&recv_msg_inuse_count); | |
4262 | } | |
4263 | return rv; | |
4264 | } | |
4265 | ||
393d2cc3 CM |
4266 | void ipmi_free_recv_msg(struct ipmi_recv_msg *msg) |
4267 | { | |
4268 | if (msg->user) | |
4269 | kref_put(&msg->user->refcount, free_user); | |
4270 | msg->done(msg); | |
4271 | } | |
c70d7499 | 4272 | EXPORT_SYMBOL(ipmi_free_recv_msg); |
393d2cc3 | 4273 | |
1da177e4 LT |
4274 | #ifdef CONFIG_IPMI_PANIC_EVENT |
4275 | ||
895dcfd1 CM |
4276 | static atomic_t panic_done_count = ATOMIC_INIT(0); |
4277 | ||
1da177e4 LT |
4278 | static void dummy_smi_done_handler(struct ipmi_smi_msg *msg) |
4279 | { | |
895dcfd1 | 4280 | atomic_dec(&panic_done_count); |
1da177e4 LT |
4281 | } |
4282 | ||
4283 | static void dummy_recv_done_handler(struct ipmi_recv_msg *msg) | |
4284 | { | |
895dcfd1 CM |
4285 | atomic_dec(&panic_done_count); |
4286 | } | |
4287 | ||
4288 | /* | |
4289 | * Inside a panic, send a message and wait for a response. | |
4290 | */ | |
4291 | static void ipmi_panic_request_and_wait(ipmi_smi_t intf, | |
4292 | struct ipmi_addr *addr, | |
4293 | struct kernel_ipmi_msg *msg) | |
4294 | { | |
4295 | struct ipmi_smi_msg smi_msg; | |
4296 | struct ipmi_recv_msg recv_msg; | |
4297 | int rv; | |
4298 | ||
4299 | smi_msg.done = dummy_smi_done_handler; | |
4300 | recv_msg.done = dummy_recv_done_handler; | |
4301 | atomic_add(2, &panic_done_count); | |
4302 | rv = i_ipmi_request(NULL, | |
4303 | intf, | |
4304 | addr, | |
4305 | 0, | |
4306 | msg, | |
4307 | intf, | |
4308 | &smi_msg, | |
4309 | &recv_msg, | |
4310 | 0, | |
4311 | intf->channels[0].address, | |
4312 | intf->channels[0].lun, | |
4313 | 0, 1); /* Don't retry, and don't wait. */ | |
4314 | if (rv) | |
4315 | atomic_sub(2, &panic_done_count); | |
82802f96 HK |
4316 | else if (intf->handlers->flush_messages) |
4317 | intf->handlers->flush_messages(intf->send_info); | |
4318 | ||
895dcfd1 CM |
4319 | while (atomic_read(&panic_done_count) != 0) |
4320 | ipmi_poll(intf); | |
1da177e4 LT |
4321 | } |
4322 | ||
4323 | #ifdef CONFIG_IPMI_PANIC_STRING | |
56a55ec6 | 4324 | static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg) |
1da177e4 | 4325 | { |
56a55ec6 CM |
4326 | if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) |
4327 | && (msg->msg.netfn == IPMI_NETFN_SENSOR_EVENT_RESPONSE) | |
4328 | && (msg->msg.cmd == IPMI_GET_EVENT_RECEIVER_CMD) | |
c70d7499 | 4329 | && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) { |
1da177e4 | 4330 | /* A get event receiver command, save it. */ |
56a55ec6 CM |
4331 | intf->event_receiver = msg->msg.data[1]; |
4332 | intf->event_receiver_lun = msg->msg.data[2] & 0x3; | |
1da177e4 LT |
4333 | } |
4334 | } | |
4335 | ||
56a55ec6 | 4336 | static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg) |
1da177e4 | 4337 | { |
56a55ec6 CM |
4338 | if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) |
4339 | && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE) | |
4340 | && (msg->msg.cmd == IPMI_GET_DEVICE_ID_CMD) | |
c70d7499 CM |
4341 | && (msg->msg.data[0] == IPMI_CC_NO_ERROR)) { |
4342 | /* | |
4343 | * A get device id command, save if we are an event | |
4344 | * receiver or generator. | |
4345 | */ | |
56a55ec6 CM |
4346 | intf->local_sel_device = (msg->msg.data[6] >> 2) & 1; |
4347 | intf->local_event_generator = (msg->msg.data[6] >> 5) & 1; | |
1da177e4 LT |
4348 | } |
4349 | } | |
4350 | #endif | |
4351 | ||
4352 | static void send_panic_events(char *str) | |
4353 | { | |
4354 | struct kernel_ipmi_msg msg; | |
4355 | ipmi_smi_t intf; | |
4356 | unsigned char data[16]; | |
1da177e4 LT |
4357 | struct ipmi_system_interface_addr *si; |
4358 | struct ipmi_addr addr; | |
1da177e4 LT |
4359 | |
4360 | si = (struct ipmi_system_interface_addr *) &addr; | |
4361 | si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
4362 | si->channel = IPMI_BMC_CHANNEL; | |
4363 | si->lun = 0; | |
4364 | ||
4365 | /* Fill in an event telling that we have failed. */ | |
4366 | msg.netfn = 0x04; /* Sensor or Event. */ | |
4367 | msg.cmd = 2; /* Platform event command. */ | |
4368 | msg.data = data; | |
4369 | msg.data_len = 8; | |
cda315ab | 4370 | data[0] = 0x41; /* Kernel generator ID, IPMI table 5-4 */ |
1da177e4 LT |
4371 | data[1] = 0x03; /* This is for IPMI 1.0. */ |
4372 | data[2] = 0x20; /* OS Critical Stop, IPMI table 36-3 */ | |
4373 | data[4] = 0x6f; /* Sensor specific, IPMI table 36-1 */ | |
4374 | data[5] = 0xa1; /* Runtime stop OEM bytes 2 & 3. */ | |
4375 | ||
c70d7499 CM |
4376 | /* |
4377 | * Put a few breadcrumbs in. Hopefully later we can add more things | |
4378 | * to make the panic events more useful. | |
4379 | */ | |
1da177e4 LT |
4380 | if (str) { |
4381 | data[3] = str[0]; | |
4382 | data[6] = str[1]; | |
4383 | data[7] = str[2]; | |
4384 | } | |
4385 | ||
1da177e4 | 4386 | /* For every registered interface, send the event. */ |
bca0324d | 4387 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { |
b2c03941 CM |
4388 | if (!intf->handlers) |
4389 | /* Interface is not ready. */ | |
1da177e4 LT |
4390 | continue; |
4391 | ||
4392 | /* Send the event announcing the panic. */ | |
895dcfd1 | 4393 | ipmi_panic_request_and_wait(intf, &addr, &msg); |
1da177e4 LT |
4394 | } |
4395 | ||
4396 | #ifdef CONFIG_IPMI_PANIC_STRING | |
c70d7499 CM |
4397 | /* |
4398 | * On every interface, dump a bunch of OEM event holding the | |
4399 | * string. | |
4400 | */ | |
4401 | if (!str) | |
1da177e4 LT |
4402 | return; |
4403 | ||
bca0324d CM |
4404 | /* For every registered interface, send the event. */ |
4405 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { | |
1da177e4 LT |
4406 | char *p = str; |
4407 | struct ipmi_ipmb_addr *ipmb; | |
4408 | int j; | |
4409 | ||
bca0324d CM |
4410 | if (intf->intf_num == -1) |
4411 | /* Interface was not ready yet. */ | |
1da177e4 LT |
4412 | continue; |
4413 | ||
78ba2faf CM |
4414 | /* |
4415 | * intf_num is used as an marker to tell if the | |
4416 | * interface is valid. Thus we need a read barrier to | |
4417 | * make sure data fetched before checking intf_num | |
4418 | * won't be used. | |
4419 | */ | |
4420 | smp_rmb(); | |
4421 | ||
c70d7499 CM |
4422 | /* |
4423 | * First job here is to figure out where to send the | |
4424 | * OEM events. There's no way in IPMI to send OEM | |
4425 | * events using an event send command, so we have to | |
4426 | * find the SEL to put them in and stick them in | |
4427 | * there. | |
4428 | */ | |
1da177e4 LT |
4429 | |
4430 | /* Get capabilities from the get device id. */ | |
4431 | intf->local_sel_device = 0; | |
4432 | intf->local_event_generator = 0; | |
4433 | intf->event_receiver = 0; | |
4434 | ||
4435 | /* Request the device info from the local MC. */ | |
4436 | msg.netfn = IPMI_NETFN_APP_REQUEST; | |
4437 | msg.cmd = IPMI_GET_DEVICE_ID_CMD; | |
4438 | msg.data = NULL; | |
4439 | msg.data_len = 0; | |
4440 | intf->null_user_handler = device_id_fetcher; | |
895dcfd1 | 4441 | ipmi_panic_request_and_wait(intf, &addr, &msg); |
1da177e4 LT |
4442 | |
4443 | if (intf->local_event_generator) { | |
4444 | /* Request the event receiver from the local MC. */ | |
4445 | msg.netfn = IPMI_NETFN_SENSOR_EVENT_REQUEST; | |
4446 | msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD; | |
4447 | msg.data = NULL; | |
4448 | msg.data_len = 0; | |
4449 | intf->null_user_handler = event_receiver_fetcher; | |
895dcfd1 | 4450 | ipmi_panic_request_and_wait(intf, &addr, &msg); |
1da177e4 LT |
4451 | } |
4452 | intf->null_user_handler = NULL; | |
4453 | ||
c70d7499 CM |
4454 | /* |
4455 | * Validate the event receiver. The low bit must not | |
4456 | * be 1 (it must be a valid IPMB address), it cannot | |
4457 | * be zero, and it must not be my address. | |
4458 | */ | |
4459 | if (((intf->event_receiver & 1) == 0) | |
1da177e4 | 4460 | && (intf->event_receiver != 0) |
c70d7499 CM |
4461 | && (intf->event_receiver != intf->channels[0].address)) { |
4462 | /* | |
4463 | * The event receiver is valid, send an IPMB | |
4464 | * message. | |
4465 | */ | |
1da177e4 LT |
4466 | ipmb = (struct ipmi_ipmb_addr *) &addr; |
4467 | ipmb->addr_type = IPMI_IPMB_ADDR_TYPE; | |
4468 | ipmb->channel = 0; /* FIXME - is this right? */ | |
4469 | ipmb->lun = intf->event_receiver_lun; | |
4470 | ipmb->slave_addr = intf->event_receiver; | |
4471 | } else if (intf->local_sel_device) { | |
c70d7499 CM |
4472 | /* |
4473 | * The event receiver was not valid (or was | |
4474 | * me), but I am an SEL device, just dump it | |
4475 | * in my SEL. | |
4476 | */ | |
1da177e4 LT |
4477 | si = (struct ipmi_system_interface_addr *) &addr; |
4478 | si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; | |
4479 | si->channel = IPMI_BMC_CHANNEL; | |
4480 | si->lun = 0; | |
4481 | } else | |
4482 | continue; /* No where to send the event. */ | |
4483 | ||
1da177e4 LT |
4484 | msg.netfn = IPMI_NETFN_STORAGE_REQUEST; /* Storage. */ |
4485 | msg.cmd = IPMI_ADD_SEL_ENTRY_CMD; | |
4486 | msg.data = data; | |
4487 | msg.data_len = 16; | |
4488 | ||
4489 | j = 0; | |
4490 | while (*p) { | |
4491 | int size = strlen(p); | |
4492 | ||
4493 | if (size > 11) | |
4494 | size = 11; | |
4495 | data[0] = 0; | |
4496 | data[1] = 0; | |
4497 | data[2] = 0xf0; /* OEM event without timestamp. */ | |
c14979b9 | 4498 | data[3] = intf->channels[0].address; |
1da177e4 | 4499 | data[4] = j++; /* sequence # */ |
c70d7499 CM |
4500 | /* |
4501 | * Always give 11 bytes, so strncpy will fill | |
4502 | * it with zeroes for me. | |
4503 | */ | |
1da177e4 LT |
4504 | strncpy(data+5, p, 11); |
4505 | p += size; | |
4506 | ||
895dcfd1 | 4507 | ipmi_panic_request_and_wait(intf, &addr, &msg); |
1da177e4 | 4508 | } |
c70d7499 | 4509 | } |
1da177e4 LT |
4510 | #endif /* CONFIG_IPMI_PANIC_STRING */ |
4511 | } | |
4512 | #endif /* CONFIG_IPMI_PANIC_EVENT */ | |
4513 | ||
0c8204b3 | 4514 | static int has_panicked; |
1da177e4 LT |
4515 | |
4516 | static int panic_event(struct notifier_block *this, | |
4517 | unsigned long event, | |
c70d7499 | 4518 | void *ptr) |
1da177e4 | 4519 | { |
1da177e4 LT |
4520 | ipmi_smi_t intf; |
4521 | ||
f18190bd | 4522 | if (has_panicked) |
1da177e4 | 4523 | return NOTIFY_DONE; |
f18190bd | 4524 | has_panicked = 1; |
1da177e4 LT |
4525 | |
4526 | /* For every registered interface, set it to run to completion. */ | |
bca0324d | 4527 | list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { |
b2c03941 CM |
4528 | if (!intf->handlers) |
4529 | /* Interface is not ready. */ | |
1da177e4 LT |
4530 | continue; |
4531 | ||
06e5e345 HK |
4532 | /* |
4533 | * If we were interrupted while locking xmit_msgs_lock or | |
4534 | * waiting_rcv_msgs_lock, the corresponding list may be | |
4535 | * corrupted. In this case, drop items on the list for | |
4536 | * the safety. | |
4537 | */ | |
4538 | if (!spin_trylock(&intf->xmit_msgs_lock)) { | |
4539 | INIT_LIST_HEAD(&intf->xmit_msgs); | |
4540 | INIT_LIST_HEAD(&intf->hp_xmit_msgs); | |
4541 | } else | |
4542 | spin_unlock(&intf->xmit_msgs_lock); | |
4543 | ||
4544 | if (!spin_trylock(&intf->waiting_rcv_msgs_lock)) | |
4545 | INIT_LIST_HEAD(&intf->waiting_rcv_msgs); | |
4546 | else | |
4547 | spin_unlock(&intf->waiting_rcv_msgs_lock); | |
4548 | ||
5956dce1 | 4549 | intf->run_to_completion = 1; |
1da177e4 LT |
4550 | intf->handlers->set_run_to_completion(intf->send_info, 1); |
4551 | } | |
4552 | ||
4553 | #ifdef CONFIG_IPMI_PANIC_EVENT | |
4554 | send_panic_events(ptr); | |
4555 | #endif | |
4556 | ||
4557 | return NOTIFY_DONE; | |
4558 | } | |
4559 | ||
4560 | static struct notifier_block panic_block = { | |
4561 | .notifier_call = panic_event, | |
4562 | .next = NULL, | |
4563 | .priority = 200 /* priority: INT_MAX >= x >= 0 */ | |
4564 | }; | |
4565 | ||
4566 | static int ipmi_init_msghandler(void) | |
4567 | { | |
50c812b2 | 4568 | int rv; |
1da177e4 LT |
4569 | |
4570 | if (initialized) | |
4571 | return 0; | |
4572 | ||
fe2d5ffc | 4573 | rv = driver_register(&ipmidriver.driver); |
50c812b2 CM |
4574 | if (rv) { |
4575 | printk(KERN_ERR PFX "Could not register IPMI driver\n"); | |
4576 | return rv; | |
4577 | } | |
4578 | ||
1da177e4 | 4579 | printk(KERN_INFO "ipmi message handler version " |
1fdd75bd | 4580 | IPMI_DRIVER_VERSION "\n"); |
1da177e4 | 4581 | |
3b625943 | 4582 | #ifdef CONFIG_PROC_FS |
1da177e4 LT |
4583 | proc_ipmi_root = proc_mkdir("ipmi", NULL); |
4584 | if (!proc_ipmi_root) { | |
4585 | printk(KERN_ERR PFX "Unable to create IPMI proc dir"); | |
80fad5b9 | 4586 | driver_unregister(&ipmidriver.driver); |
1da177e4 LT |
4587 | return -ENOMEM; |
4588 | } | |
4589 | ||
3b625943 | 4590 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 4591 | |
409035e0 CM |
4592 | setup_timer(&ipmi_timer, ipmi_timeout, 0); |
4593 | mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); | |
1da177e4 | 4594 | |
e041c683 | 4595 | atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
1da177e4 LT |
4596 | |
4597 | initialized = 1; | |
4598 | ||
4599 | return 0; | |
4600 | } | |
4601 | ||
60ee6d5f | 4602 | static int __init ipmi_init_msghandler_mod(void) |
1da177e4 LT |
4603 | { |
4604 | ipmi_init_msghandler(); | |
4605 | return 0; | |
4606 | } | |
4607 | ||
60ee6d5f | 4608 | static void __exit cleanup_ipmi(void) |
1da177e4 LT |
4609 | { |
4610 | int count; | |
4611 | ||
4612 | if (!initialized) | |
4613 | return; | |
4614 | ||
e041c683 | 4615 | atomic_notifier_chain_unregister(&panic_notifier_list, &panic_block); |
1da177e4 | 4616 | |
c70d7499 CM |
4617 | /* |
4618 | * This can't be called if any interfaces exist, so no worry | |
4619 | * about shutting down the interfaces. | |
4620 | */ | |
1da177e4 | 4621 | |
c70d7499 CM |
4622 | /* |
4623 | * Tell the timer to stop, then wait for it to stop. This | |
4624 | * avoids problems with race conditions removing the timer | |
4625 | * here. | |
4626 | */ | |
8f43f84f CM |
4627 | atomic_inc(&stop_operation); |
4628 | del_timer_sync(&ipmi_timer); | |
1da177e4 | 4629 | |
3b625943 | 4630 | #ifdef CONFIG_PROC_FS |
a8ca16ea | 4631 | proc_remove(proc_ipmi_root); |
3b625943 | 4632 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 4633 | |
fe2d5ffc | 4634 | driver_unregister(&ipmidriver.driver); |
50c812b2 | 4635 | |
1da177e4 LT |
4636 | initialized = 0; |
4637 | ||
4638 | /* Check for buffer leaks. */ | |
4639 | count = atomic_read(&smi_msg_inuse_count); | |
4640 | if (count != 0) | |
4641 | printk(KERN_WARNING PFX "SMI message count %d at exit\n", | |
4642 | count); | |
4643 | count = atomic_read(&recv_msg_inuse_count); | |
4644 | if (count != 0) | |
4645 | printk(KERN_WARNING PFX "recv message count %d at exit\n", | |
4646 | count); | |
4647 | } | |
4648 | module_exit(cleanup_ipmi); | |
4649 | ||
4650 | module_init(ipmi_init_msghandler_mod); | |
4651 | MODULE_LICENSE("GPL"); | |
1fdd75bd | 4652 | MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>"); |
c70d7499 CM |
4653 | MODULE_DESCRIPTION("Incoming and outgoing message routing for an IPMI" |
4654 | " interface."); | |
1fdd75bd | 4655 | MODULE_VERSION(IPMI_DRIVER_VERSION); |
070cbd1d | 4656 | MODULE_SOFTDEP("post: ipmi_devintf"); |