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