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