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