blob: 25a1436a429181f0526879c0d184c9418a28a1fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ipmi_si.c
3 *
4 * The interface to the IPMI driver for the system interfaces (KCS, SMIC,
5 * BT).
6 *
7 * Author: MontaVista Software, Inc.
8 * Corey Minyard <minyard@mvista.com>
9 * source@mvista.com
10 *
11 * Copyright 2002 MontaVista Software Inc.
Corey Minyarddba9b4f2007-05-08 00:23:51 -070012 * Copyright 2006 IBM Corp., Christian Krafft <krafft@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 */
35
36/*
37 * This file holds the "policy" for the interface to the SMI state
38 * machine. It does the configuration, handles timers and interrupts,
39 * and drives the real SMI state machine.
40 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
43#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/sched.h>
Alexey Dobriyan07412732011-05-26 16:25:55 -070045#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/timer.h>
47#include <linux/errno.h>
48#include <linux/spinlock.h>
49#include <linux/slab.h>
50#include <linux/delay.h>
51#include <linux/list.h>
52#include <linux/pci.h>
53#include <linux/ioport.h>
Corey Minyardea940272005-11-07 00:59:59 -080054#include <linux/notifier.h>
Corey Minyardb0defcd2006-03-26 01:37:20 -080055#include <linux/mutex.h>
Matt Domsche9a705a2005-11-07 01:00:04 -080056#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/interrupt.h>
59#include <linux/rcupdate.h>
Zhao Yakui16f42322010-12-08 10:10:16 +080060#include <linux/ipmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/ipmi_smi.h>
62#include <asm/io.h>
63#include "ipmi_si_sm.h"
Andrey Paninb224cd32005-09-06 15:18:37 -070064#include <linux/dmi.h>
Corey Minyardb361e272006-12-06 20:41:07 -080065#include <linux/string.h>
66#include <linux/ctype.h>
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -070067#include <linux/pnp.h>
Stephen Rothwell11c675c2008-05-23 16:22:42 +100068#include <linux/of_device.h>
69#include <linux/of_platform.h>
Rob Herring672d8ea2010-11-16 14:33:51 -060070#include <linux/of_address.h>
71#include <linux/of_irq.h>
Corey Minyarddba9b4f2007-05-08 00:23:51 -070072
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -050073#ifdef CONFIG_PARISC
74#include <asm/hardware.h> /* for register_parisc_driver() stuff */
75#include <asm/parisc-device.h>
76#endif
77
Corey Minyardb361e272006-12-06 20:41:07 -080078#define PFX "ipmi_si: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* Measure times between events in the driver. */
81#undef DEBUG_TIMING
82
83/* Call every 10 ms. */
84#define SI_TIMEOUT_TIME_USEC 10000
85#define SI_USEC_PER_JIFFY (1000000/HZ)
86#define SI_TIMEOUT_JIFFIES (SI_TIMEOUT_TIME_USEC/SI_USEC_PER_JIFFY)
87#define SI_SHORT_TIMEOUT_USEC 250 /* .25ms when the SM request a
Corey Minyardc305e3d2008-04-29 01:01:10 -070088 short timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90enum si_intf_state {
91 SI_NORMAL,
92 SI_GETTING_FLAGS,
93 SI_GETTING_EVENTS,
94 SI_CLEARING_FLAGS,
95 SI_CLEARING_FLAGS_THEN_SET_IRQ,
96 SI_GETTING_MESSAGES,
97 SI_ENABLE_INTERRUPTS1,
Corey Minyardee6cd5f2007-05-08 00:23:54 -070098 SI_ENABLE_INTERRUPTS2,
99 SI_DISABLE_INTERRUPTS1,
100 SI_DISABLE_INTERRUPTS2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* FIXME - add watchdog stuff. */
102};
103
Corey Minyard9dbf68f2005-05-01 08:59:11 -0700104/* Some BT-specific defines we need here. */
105#define IPMI_BT_INTMASK_REG 2
106#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
107#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109enum si_type {
110 SI_KCS, SI_SMIC, SI_BT
111};
Corey Minyardb361e272006-12-06 20:41:07 -0800112static char *si_to_str[] = { "kcs", "smic", "bt" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Matthew Garrett5fedc4a2010-05-26 14:43:45 -0700114static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
115 "ACPI", "SMBIOS", "PCI",
116 "device-tree", "default" };
117
Corey Minyard50c812b2006-03-26 01:37:21 -0800118#define DEVICE_NAME "ipmi_si"
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700119
Rob Herringa1e9c9d2011-02-23 15:37:59 -0600120static struct platform_driver ipmi_driver;
Corey Minyard64959e22008-04-29 01:01:07 -0700121
122/*
123 * Indexes into stats[] in smi_info below.
124 */
Corey Minyardba8ff1c2008-04-29 01:01:08 -0700125enum si_stat_indexes {
126 /*
127 * Number of times the driver requested a timer while an operation
128 * was in progress.
129 */
130 SI_STAT_short_timeouts = 0,
Corey Minyard64959e22008-04-29 01:01:07 -0700131
Corey Minyardba8ff1c2008-04-29 01:01:08 -0700132 /*
133 * Number of times the driver requested a timer while nothing was in
134 * progress.
135 */
136 SI_STAT_long_timeouts,
Corey Minyard64959e22008-04-29 01:01:07 -0700137
Corey Minyardba8ff1c2008-04-29 01:01:08 -0700138 /* Number of times the interface was idle while being polled. */
139 SI_STAT_idles,
140
141 /* Number of interrupts the driver handled. */
142 SI_STAT_interrupts,
143
144 /* Number of time the driver got an ATTN from the hardware. */
145 SI_STAT_attentions,
146
147 /* Number of times the driver requested flags from the hardware. */
148 SI_STAT_flag_fetches,
149
150 /* Number of times the hardware didn't follow the state machine. */
151 SI_STAT_hosed_count,
152
153 /* Number of completed messages. */
154 SI_STAT_complete_transactions,
155
156 /* Number of IPMI events received from the hardware. */
157 SI_STAT_events,
158
159 /* Number of watchdog pretimeouts. */
160 SI_STAT_watchdog_pretimeouts,
161
Adam Buchbinderb3834be2012-09-19 21:48:02 -0400162 /* Number of asynchronous messages received. */
Corey Minyardba8ff1c2008-04-29 01:01:08 -0700163 SI_STAT_incoming_messages,
164
165
166 /* This *must* remain last, add new values above this. */
167 SI_NUM_STATS
168};
Corey Minyard64959e22008-04-29 01:01:07 -0700169
Corey Minyardc305e3d2008-04-29 01:01:10 -0700170struct smi_info {
Corey Minyarda9a2c442005-11-07 01:00:03 -0800171 int intf_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ipmi_smi_t intf;
173 struct si_sm_data *si_sm;
174 struct si_sm_handlers *handlers;
175 enum si_type si_type;
176 spinlock_t si_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct list_head xmit_msgs;
178 struct list_head hp_xmit_msgs;
179 struct ipmi_smi_msg *curr_msg;
180 enum si_intf_state si_state;
181
Corey Minyardc305e3d2008-04-29 01:01:10 -0700182 /*
183 * Used to handle the various types of I/O that can occur with
184 * IPMI
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct si_sm_io io;
187 int (*io_setup)(struct smi_info *info);
188 void (*io_cleanup)(struct smi_info *info);
189 int (*irq_setup)(struct smi_info *info);
190 void (*irq_cleanup)(struct smi_info *info);
191 unsigned int io_size;
Matthew Garrett5fedc4a2010-05-26 14:43:45 -0700192 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
Corey Minyardb0defcd2006-03-26 01:37:20 -0800193 void (*addr_source_cleanup)(struct smi_info *info);
194 void *addr_source_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Corey Minyardc305e3d2008-04-29 01:01:10 -0700196 /*
197 * Per-OEM handler, called from handle_flags(). Returns 1
198 * when handle_flags() needs to be re-run or 0 indicating it
199 * set si_state itself.
200 */
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700201 int (*oem_data_avail_handler)(struct smi_info *smi_info);
202
Corey Minyardc305e3d2008-04-29 01:01:10 -0700203 /*
204 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
205 * is set to hold the flags until we are done handling everything
206 * from the flags.
207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define RECEIVE_MSG_AVAIL 0x01
209#define EVENT_MSG_BUFFER_FULL 0x02
210#define WDT_PRE_TIMEOUT_INT 0x08
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700211#define OEM0_DATA_AVAIL 0x20
212#define OEM1_DATA_AVAIL 0x40
213#define OEM2_DATA_AVAIL 0x80
214#define OEM_DATA_AVAIL (OEM0_DATA_AVAIL | \
Corey Minyardc305e3d2008-04-29 01:01:10 -0700215 OEM1_DATA_AVAIL | \
216 OEM2_DATA_AVAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned char msg_flags;
218
Corey Minyard40112ae2009-04-21 12:24:03 -0700219 /* Does the BMC have an event buffer? */
220 char has_event_buffer;
221
Corey Minyardc305e3d2008-04-29 01:01:10 -0700222 /*
223 * If set to true, this will request events the next time the
224 * state machine is idle.
225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 atomic_t req_events;
227
Corey Minyardc305e3d2008-04-29 01:01:10 -0700228 /*
229 * If true, run the state machine to completion on every send
230 * call. Generally used after a panic to make sure stuff goes
231 * out.
232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int run_to_completion;
234
235 /* The I/O port of an SI interface. */
236 int port;
237
Corey Minyardc305e3d2008-04-29 01:01:10 -0700238 /*
239 * The space between start addresses of the two ports. For
240 * instance, if the first port is 0xca2 and the spacing is 4, then
241 * the second port is 0xca6.
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int spacing;
244
245 /* zero if no irq; */
246 int irq;
247
248 /* The timer for this si. */
249 struct timer_list si_timer;
250
Bodo Stroesser48e8ac22014-04-14 09:46:51 -0500251 /* This flag is set, if the timer is running (timer_pending() isn't enough) */
252 bool timer_running;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* The time (in jiffies) the last timeout occurred at. */
255 unsigned long last_timeout_jiffies;
256
257 /* Used to gracefully stop the timer without race conditions. */
Corey Minyarda9a2c442005-11-07 01:00:03 -0800258 atomic_t stop_operation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Corey Minyardc305e3d2008-04-29 01:01:10 -0700260 /*
261 * The driver will disable interrupts when it gets into a
262 * situation where it cannot handle messages due to lack of
263 * memory. Once that situation clears up, it will re-enable
264 * interrupts.
265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int interrupt_disabled;
267
Corey Minyard50c812b2006-03-26 01:37:21 -0800268 /* From the get device id response... */
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700269 struct ipmi_device_id device_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Corey Minyard50c812b2006-03-26 01:37:21 -0800271 /* Driver model stuff. */
272 struct device *dev;
273 struct platform_device *pdev;
274
Corey Minyardc305e3d2008-04-29 01:01:10 -0700275 /*
276 * True if we allocated the device, false if it came from
277 * someplace else (like PCI).
278 */
Corey Minyard50c812b2006-03-26 01:37:21 -0800279 int dev_registered;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Slave address, could be reported from DMI. */
282 unsigned char slave_addr;
283
284 /* Counters and things for the proc filesystem. */
Corey Minyard64959e22008-04-29 01:01:07 -0700285 atomic_t stats[SI_NUM_STATS];
Corey Minyarda9a2c442005-11-07 01:00:03 -0800286
Corey Minyardc305e3d2008-04-29 01:01:10 -0700287 struct task_struct *thread;
Corey Minyardb0defcd2006-03-26 01:37:20 -0800288
289 struct list_head link;
Zhao Yakui16f42322010-12-08 10:10:16 +0800290 union ipmi_smi_info_union addr_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
Corey Minyard64959e22008-04-29 01:01:07 -0700293#define smi_inc_stat(smi, stat) \
294 atomic_inc(&(smi)->stats[SI_STAT_ ## stat])
295#define smi_get_stat(smi, stat) \
296 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
297
Corey Minyarda51f4a82006-10-03 01:13:59 -0700298#define SI_MAX_PARMS 4
299
300static int force_kipmid[SI_MAX_PARMS];
301static int num_force_kipmid;
Matthew Garrett56480282010-06-29 15:05:29 -0700302#ifdef CONFIG_PCI
303static int pci_registered;
304#endif
Yinghai Lu561f8182010-09-22 13:05:15 -0700305#ifdef CONFIG_ACPI
306static int pnp_registered;
307#endif
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -0500308#ifdef CONFIG_PARISC
309static int parisc_registered;
310#endif
Corey Minyarda51f4a82006-10-03 01:13:59 -0700311
Martin Wilckae74e822010-03-10 15:23:06 -0800312static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
313static int num_max_busy_us;
314
Corey Minyardb361e272006-12-06 20:41:07 -0800315static int unload_when_empty = 1;
316
Matthew Garrett2407d772010-05-26 14:43:46 -0700317static int add_smi(struct smi_info *smi);
Corey Minyardb0defcd2006-03-26 01:37:20 -0800318static int try_smi_init(struct smi_info *smi);
Corey Minyardb361e272006-12-06 20:41:07 -0800319static void cleanup_one_si(struct smi_info *to_clean);
Corey Minyardd2478522011-02-10 16:08:38 -0600320static void cleanup_ipmi_si(void);
Corey Minyardb0defcd2006-03-26 01:37:20 -0800321
Alan Sterne041c682006-03-27 01:16:30 -0800322static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700323static int register_xaction_notifier(struct notifier_block *nb)
Corey Minyardea940272005-11-07 00:59:59 -0800324{
Alan Sterne041c682006-03-27 01:16:30 -0800325 return atomic_notifier_chain_register(&xaction_notifier_list, nb);
Corey Minyardea940272005-11-07 00:59:59 -0800326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static void deliver_recv_msg(struct smi_info *smi_info,
329 struct ipmi_smi_msg *msg)
330{
Corey Minyard7adf5792012-03-28 14:42:49 -0700331 /* Deliver the message to the upper layer. */
332 ipmi_smi_msg_received(smi_info->intf, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800335static void return_hosed_msg(struct smi_info *smi_info, int cCode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct ipmi_smi_msg *msg = smi_info->curr_msg;
338
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800339 if (cCode < 0 || cCode > IPMI_ERR_UNSPECIFIED)
340 cCode = IPMI_ERR_UNSPECIFIED;
341 /* else use it as is */
342
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300343 /* Make it a response */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 msg->rsp[0] = msg->data[0] | 4;
345 msg->rsp[1] = msg->data[1];
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800346 msg->rsp[2] = cCode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 msg->rsp_size = 3;
348
349 smi_info->curr_msg = NULL;
350 deliver_recv_msg(smi_info, msg);
351}
352
353static enum si_sm_result start_next_msg(struct smi_info *smi_info)
354{
355 int rv;
356 struct list_head *entry = NULL;
357#ifdef DEBUG_TIMING
358 struct timeval t;
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Pick the high priority queue first. */
Corey Minyardb0defcd2006-03-26 01:37:20 -0800362 if (!list_empty(&(smi_info->hp_xmit_msgs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 entry = smi_info->hp_xmit_msgs.next;
Corey Minyardb0defcd2006-03-26 01:37:20 -0800364 } else if (!list_empty(&(smi_info->xmit_msgs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 entry = smi_info->xmit_msgs.next;
366 }
367
Corey Minyardb0defcd2006-03-26 01:37:20 -0800368 if (!entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 smi_info->curr_msg = NULL;
370 rv = SI_SM_IDLE;
371 } else {
372 int err;
373
374 list_del(entry);
375 smi_info->curr_msg = list_entry(entry,
376 struct ipmi_smi_msg,
377 link);
378#ifdef DEBUG_TIMING
379 do_gettimeofday(&t);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700380 printk(KERN_DEBUG "**Start2: %d.%9.9d\n", t.tv_sec, t.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
Alan Sterne041c682006-03-27 01:16:30 -0800382 err = atomic_notifier_call_chain(&xaction_notifier_list,
383 0, smi_info);
Corey Minyardea940272005-11-07 00:59:59 -0800384 if (err & NOTIFY_STOP_MASK) {
385 rv = SI_SM_CALL_WITHOUT_DELAY;
386 goto out;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 err = smi_info->handlers->start_transaction(
389 smi_info->si_sm,
390 smi_info->curr_msg->data,
391 smi_info->curr_msg->data_size);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700392 if (err)
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800393 return_hosed_msg(smi_info, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 rv = SI_SM_CALL_WITHOUT_DELAY;
396 }
Corey Minyardc305e3d2008-04-29 01:01:10 -0700397 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return rv;
399}
400
401static void start_enable_irq(struct smi_info *smi_info)
402{
403 unsigned char msg[2];
404
Corey Minyardc305e3d2008-04-29 01:01:10 -0700405 /*
406 * If we are enabling interrupts, we have to tell the
407 * BMC to use them.
408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
410 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
411
412 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
413 smi_info->si_state = SI_ENABLE_INTERRUPTS1;
414}
415
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700416static void start_disable_irq(struct smi_info *smi_info)
417{
418 unsigned char msg[2];
419
420 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
421 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
422
423 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
424 smi_info->si_state = SI_DISABLE_INTERRUPTS1;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void start_clear_flags(struct smi_info *smi_info)
428{
429 unsigned char msg[3];
430
431 /* Make sure the watchdog pre-timeout flag is not set at startup. */
432 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
433 msg[1] = IPMI_CLEAR_MSG_FLAGS_CMD;
434 msg[2] = WDT_PRE_TIMEOUT_INT;
435
436 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
437 smi_info->si_state = SI_CLEARING_FLAGS;
438}
439
Bodo Stroesser48e8ac22014-04-14 09:46:51 -0500440static void smi_mod_timer(struct smi_info *smi_info, unsigned long new_val)
441{
442 smi_info->last_timeout_jiffies = jiffies;
443 mod_timer(&smi_info->si_timer, new_val);
444 smi_info->timer_running = true;
445}
446
Corey Minyardc305e3d2008-04-29 01:01:10 -0700447/*
448 * When we have a situtaion where we run out of memory and cannot
449 * allocate messages, we just leave them in the BMC and run the system
450 * polled until we can allocate some memory. Once we have some
451 * memory, we will re-enable the interrupt.
452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static inline void disable_si_irq(struct smi_info *smi_info)
454{
Corey Minyardb0defcd2006-03-26 01:37:20 -0800455 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700456 start_disable_irq(smi_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 smi_info->interrupt_disabled = 1;
Matthew Garrettea4078c2010-05-26 14:43:48 -0700458 if (!atomic_read(&smi_info->stop_operation))
Bodo Stroesser48e8ac22014-04-14 09:46:51 -0500459 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461}
462
463static inline void enable_si_irq(struct smi_info *smi_info)
464{
465 if ((smi_info->irq) && (smi_info->interrupt_disabled)) {
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700466 start_enable_irq(smi_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 smi_info->interrupt_disabled = 0;
468 }
469}
470
471static void handle_flags(struct smi_info *smi_info)
472{
Corey Minyard3ae0e0f2005-09-06 15:18:41 -0700473 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (smi_info->msg_flags & WDT_PRE_TIMEOUT_INT) {
475 /* Watchdog pre-timeout */
Corey Minyard64959e22008-04-29 01:01:07 -0700476 smi_inc_stat(smi_info, watchdog_pretimeouts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 start_clear_flags(smi_info);
479 smi_info->msg_flags &= ~WDT_PRE_TIMEOUT_INT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ipmi_smi_watchdog_pretimeout(smi_info->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else if (smi_info->msg_flags & RECEIVE_MSG_AVAIL) {
482 /* Messages available. */
483 smi_info->curr_msg = ipmi_alloc_smi_msg();
Corey Minyardb0defcd2006-03-26 01:37:20 -0800484 if (!smi_info->curr_msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 disable_si_irq(smi_info);
486 smi_info->si_state = SI_NORMAL;
487 return;
488 }
489 enable_si_irq(smi_info);
490
491 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
492 smi_info->curr_msg->data[1] = IPMI_GET_MSG_CMD;
493 smi_info->curr_msg->data_size = 2;
494
495 smi_info->handlers->start_transaction(
496 smi_info->si_sm,
497 smi_info->curr_msg->data,
498 smi_info->curr_msg->data_size);
499 smi_info->si_state = SI_GETTING_MESSAGES;
500 } else if (smi_info->msg_flags & EVENT_MSG_BUFFER_FULL) {
501 /* Events available. */
502 smi_info->curr_msg = ipmi_alloc_smi_msg();
Corey Minyardb0defcd2006-03-26 01:37:20 -0800503 if (!smi_info->curr_msg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 disable_si_irq(smi_info);
505 smi_info->si_state = SI_NORMAL;
506 return;
507 }
508 enable_si_irq(smi_info);
509
510 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
511 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
512 smi_info->curr_msg->data_size = 2;
513
514 smi_info->handlers->start_transaction(
515 smi_info->si_sm,
516 smi_info->curr_msg->data,
517 smi_info->curr_msg->data_size);
518 smi_info->si_state = SI_GETTING_EVENTS;
Corey Minyard4064d5e2006-09-16 12:15:41 -0700519 } else if (smi_info->msg_flags & OEM_DATA_AVAIL &&
Corey Minyardc305e3d2008-04-29 01:01:10 -0700520 smi_info->oem_data_avail_handler) {
Corey Minyard4064d5e2006-09-16 12:15:41 -0700521 if (smi_info->oem_data_avail_handler(smi_info))
522 goto retry;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700523 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 smi_info->si_state = SI_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527static void handle_transaction_done(struct smi_info *smi_info)
528{
529 struct ipmi_smi_msg *msg;
530#ifdef DEBUG_TIMING
531 struct timeval t;
532
533 do_gettimeofday(&t);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700534 printk(KERN_DEBUG "**Done: %d.%9.9d\n", t.tv_sec, t.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif
536 switch (smi_info->si_state) {
537 case SI_NORMAL:
Corey Minyardb0defcd2006-03-26 01:37:20 -0800538 if (!smi_info->curr_msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540
541 smi_info->curr_msg->rsp_size
542 = smi_info->handlers->get_result(
543 smi_info->si_sm,
544 smi_info->curr_msg->rsp,
545 IPMI_MAX_MSG_LENGTH);
546
Corey Minyardc305e3d2008-04-29 01:01:10 -0700547 /*
548 * Do this here becase deliver_recv_msg() releases the
549 * lock, and a new message can be put in during the
550 * time the lock is released.
551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 msg = smi_info->curr_msg;
553 smi_info->curr_msg = NULL;
554 deliver_recv_msg(smi_info, msg);
555 break;
556
557 case SI_GETTING_FLAGS:
558 {
559 unsigned char msg[4];
560 unsigned int len;
561
562 /* We got the flags from the SMI, now handle them. */
563 len = smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
564 if (msg[2] != 0) {
Corey Minyardc305e3d2008-04-29 01:01:10 -0700565 /* Error fetching flags, just give up for now. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 smi_info->si_state = SI_NORMAL;
567 } else if (len < 4) {
Corey Minyardc305e3d2008-04-29 01:01:10 -0700568 /*
569 * Hmm, no flags. That's technically illegal, but
570 * don't use uninitialized data.
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 smi_info->si_state = SI_NORMAL;
573 } else {
574 smi_info->msg_flags = msg[3];
575 handle_flags(smi_info);
576 }
577 break;
578 }
579
580 case SI_CLEARING_FLAGS:
581 case SI_CLEARING_FLAGS_THEN_SET_IRQ:
582 {
583 unsigned char msg[3];
584
585 /* We cleared the flags. */
586 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
587 if (msg[2] != 0) {
588 /* Error clearing flags */
Myron Stowe279fbd02010-05-26 14:43:52 -0700589 dev_warn(smi_info->dev,
590 "Error clearing flags: %2.2x\n", msg[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 if (smi_info->si_state == SI_CLEARING_FLAGS_THEN_SET_IRQ)
593 start_enable_irq(smi_info);
594 else
595 smi_info->si_state = SI_NORMAL;
596 break;
597 }
598
599 case SI_GETTING_EVENTS:
600 {
601 smi_info->curr_msg->rsp_size
602 = smi_info->handlers->get_result(
603 smi_info->si_sm,
604 smi_info->curr_msg->rsp,
605 IPMI_MAX_MSG_LENGTH);
606
Corey Minyardc305e3d2008-04-29 01:01:10 -0700607 /*
608 * Do this here becase deliver_recv_msg() releases the
609 * lock, and a new message can be put in during the
610 * time the lock is released.
611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 msg = smi_info->curr_msg;
613 smi_info->curr_msg = NULL;
614 if (msg->rsp[2] != 0) {
615 /* Error getting event, probably done. */
616 msg->done(msg);
617
618 /* Take off the event flag. */
619 smi_info->msg_flags &= ~EVENT_MSG_BUFFER_FULL;
620 handle_flags(smi_info);
621 } else {
Corey Minyard64959e22008-04-29 01:01:07 -0700622 smi_inc_stat(smi_info, events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Corey Minyardc305e3d2008-04-29 01:01:10 -0700624 /*
625 * Do this before we deliver the message
626 * because delivering the message releases the
627 * lock and something else can mess with the
628 * state.
629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 handle_flags(smi_info);
631
632 deliver_recv_msg(smi_info, msg);
633 }
634 break;
635 }
636
637 case SI_GETTING_MESSAGES:
638 {
639 smi_info->curr_msg->rsp_size
640 = smi_info->handlers->get_result(
641 smi_info->si_sm,
642 smi_info->curr_msg->rsp,
643 IPMI_MAX_MSG_LENGTH);
644
Corey Minyardc305e3d2008-04-29 01:01:10 -0700645 /*
646 * Do this here becase deliver_recv_msg() releases the
647 * lock, and a new message can be put in during the
648 * time the lock is released.
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 msg = smi_info->curr_msg;
651 smi_info->curr_msg = NULL;
652 if (msg->rsp[2] != 0) {
653 /* Error getting event, probably done. */
654 msg->done(msg);
655
656 /* Take off the msg flag. */
657 smi_info->msg_flags &= ~RECEIVE_MSG_AVAIL;
658 handle_flags(smi_info);
659 } else {
Corey Minyard64959e22008-04-29 01:01:07 -0700660 smi_inc_stat(smi_info, incoming_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Corey Minyardc305e3d2008-04-29 01:01:10 -0700662 /*
663 * Do this before we deliver the message
664 * because delivering the message releases the
665 * lock and something else can mess with the
666 * state.
667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 handle_flags(smi_info);
669
670 deliver_recv_msg(smi_info, msg);
671 }
672 break;
673 }
674
675 case SI_ENABLE_INTERRUPTS1:
676 {
677 unsigned char msg[4];
678
679 /* We got the flags from the SMI, now handle them. */
680 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
681 if (msg[2] != 0) {
Corey Minyard0849bfe2013-05-16 14:04:26 -0500682 dev_warn(smi_info->dev,
683 "Couldn't get irq info: %x.\n", msg[2]);
684 dev_warn(smi_info->dev,
685 "Maybe ok, but ipmi might run very slowly.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 smi_info->si_state = SI_NORMAL;
687 } else {
688 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
689 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700690 msg[2] = (msg[3] |
691 IPMI_BMC_RCV_MSG_INTR |
692 IPMI_BMC_EVT_MSG_INTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 smi_info->handlers->start_transaction(
694 smi_info->si_sm, msg, 3);
695 smi_info->si_state = SI_ENABLE_INTERRUPTS2;
696 }
697 break;
698 }
699
700 case SI_ENABLE_INTERRUPTS2:
701 {
702 unsigned char msg[4];
703
704 /* We got the flags from the SMI, now handle them. */
705 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
Corey Minyard0849bfe2013-05-16 14:04:26 -0500706 if (msg[2] != 0) {
707 dev_warn(smi_info->dev,
708 "Couldn't set irq info: %x.\n", msg[2]);
709 dev_warn(smi_info->dev,
710 "Maybe ok, but ipmi might run very slowly.\n");
711 } else
Matthew Garrettea4078c2010-05-26 14:43:48 -0700712 smi_info->interrupt_disabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 smi_info->si_state = SI_NORMAL;
714 break;
715 }
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700716
717 case SI_DISABLE_INTERRUPTS1:
718 {
719 unsigned char msg[4];
720
721 /* We got the flags from the SMI, now handle them. */
722 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
723 if (msg[2] != 0) {
Myron Stowe279fbd02010-05-26 14:43:52 -0700724 dev_warn(smi_info->dev, "Could not disable interrupts"
725 ", failed get.\n");
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700726 smi_info->si_state = SI_NORMAL;
727 } else {
728 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
729 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
730 msg[2] = (msg[3] &
731 ~(IPMI_BMC_RCV_MSG_INTR |
732 IPMI_BMC_EVT_MSG_INTR));
733 smi_info->handlers->start_transaction(
734 smi_info->si_sm, msg, 3);
735 smi_info->si_state = SI_DISABLE_INTERRUPTS2;
736 }
737 break;
738 }
739
740 case SI_DISABLE_INTERRUPTS2:
741 {
742 unsigned char msg[4];
743
744 /* We got the flags from the SMI, now handle them. */
745 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
746 if (msg[2] != 0) {
Myron Stowe279fbd02010-05-26 14:43:52 -0700747 dev_warn(smi_info->dev, "Could not disable interrupts"
748 ", failed set.\n");
Corey Minyardee6cd5f2007-05-08 00:23:54 -0700749 }
750 smi_info->si_state = SI_NORMAL;
751 break;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754}
755
Corey Minyardc305e3d2008-04-29 01:01:10 -0700756/*
757 * Called on timeouts and events. Timeouts should pass the elapsed
758 * time, interrupts should pass in zero. Must be called with
759 * si_lock held and interrupts disabled.
760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static enum si_sm_result smi_event_handler(struct smi_info *smi_info,
762 int time)
763{
764 enum si_sm_result si_sm_result;
765
766 restart:
Corey Minyardc305e3d2008-04-29 01:01:10 -0700767 /*
768 * There used to be a loop here that waited a little while
769 * (around 25us) before giving up. That turned out to be
770 * pointless, the minimum delays I was seeing were in the 300us
771 * range, which is far too long to wait in an interrupt. So
772 * we just run until the state machine tells us something
773 * happened or it needs a delay.
774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 si_sm_result = smi_info->handlers->event(smi_info->si_sm, time);
776 time = 0;
777 while (si_sm_result == SI_SM_CALL_WITHOUT_DELAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Corey Minyardc305e3d2008-04-29 01:01:10 -0700780 if (si_sm_result == SI_SM_TRANSACTION_COMPLETE) {
Corey Minyard64959e22008-04-29 01:01:07 -0700781 smi_inc_stat(smi_info, complete_transactions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 handle_transaction_done(smi_info);
784 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
Corey Minyardc305e3d2008-04-29 01:01:10 -0700785 } else if (si_sm_result == SI_SM_HOSED) {
Corey Minyard64959e22008-04-29 01:01:07 -0700786 smi_inc_stat(smi_info, hosed_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Corey Minyardc305e3d2008-04-29 01:01:10 -0700788 /*
789 * Do the before return_hosed_msg, because that
790 * releases the lock.
791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 smi_info->si_state = SI_NORMAL;
793 if (smi_info->curr_msg != NULL) {
Corey Minyardc305e3d2008-04-29 01:01:10 -0700794 /*
795 * If we were handling a user message, format
796 * a response to send to the upper layer to
797 * tell it about the error.
798 */
Corey Minyard4d7cbac2006-12-06 20:41:14 -0800799 return_hosed_msg(smi_info, IPMI_ERR_UNSPECIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 si_sm_result = smi_info->handlers->event(smi_info->si_sm, 0);
802 }
803
Corey Minyard4ea18422008-04-29 01:01:01 -0700804 /*
805 * We prefer handling attn over new messages. But don't do
806 * this if there is not yet an upper layer to handle anything.
807 */
Corey Minyardc305e3d2008-04-29 01:01:10 -0700808 if (likely(smi_info->intf) && si_sm_result == SI_SM_ATTN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned char msg[2];
810
Corey Minyard64959e22008-04-29 01:01:07 -0700811 smi_inc_stat(smi_info, attentions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Corey Minyardc305e3d2008-04-29 01:01:10 -0700813 /*
814 * Got a attn, send down a get message flags to see
815 * what's causing it. It would be better to handle
816 * this in the upper layer, but due to the way
817 * interrupts work with the SMI, that's not really
818 * possible.
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 msg[0] = (IPMI_NETFN_APP_REQUEST << 2);
821 msg[1] = IPMI_GET_MSG_FLAGS_CMD;
822
823 smi_info->handlers->start_transaction(
824 smi_info->si_sm, msg, 2);
825 smi_info->si_state = SI_GETTING_FLAGS;
826 goto restart;
827 }
828
829 /* If we are currently idle, try to start the next message. */
830 if (si_sm_result == SI_SM_IDLE) {
Corey Minyard64959e22008-04-29 01:01:07 -0700831 smi_inc_stat(smi_info, idles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 si_sm_result = start_next_msg(smi_info);
834 if (si_sm_result != SI_SM_IDLE)
835 goto restart;
Corey Minyardc305e3d2008-04-29 01:01:10 -0700836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if ((si_sm_result == SI_SM_IDLE)
Corey Minyardc305e3d2008-04-29 01:01:10 -0700839 && (atomic_read(&smi_info->req_events))) {
840 /*
841 * We are idle and the upper layer requested that I fetch
842 * events, so do so.
843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 atomic_set(&smi_info->req_events, 0);
Corey Minyard55162fb2006-12-06 20:41:04 -0800845
846 smi_info->curr_msg = ipmi_alloc_smi_msg();
847 if (!smi_info->curr_msg)
848 goto out;
849
850 smi_info->curr_msg->data[0] = (IPMI_NETFN_APP_REQUEST << 2);
851 smi_info->curr_msg->data[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD;
852 smi_info->curr_msg->data_size = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 smi_info->handlers->start_transaction(
Corey Minyard55162fb2006-12-06 20:41:04 -0800855 smi_info->si_sm,
856 smi_info->curr_msg->data,
857 smi_info->curr_msg->data_size);
858 smi_info->si_state = SI_GETTING_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto restart;
860 }
Corey Minyard55162fb2006-12-06 20:41:04 -0800861 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return si_sm_result;
863}
864
865static void sender(void *send_info,
866 struct ipmi_smi_msg *msg,
867 int priority)
868{
869 struct smi_info *smi_info = send_info;
870 enum si_sm_result result;
871 unsigned long flags;
872#ifdef DEBUG_TIMING
873 struct timeval t;
874#endif
875
Corey Minyardb361e272006-12-06 20:41:07 -0800876 if (atomic_read(&smi_info->stop_operation)) {
877 msg->rsp[0] = msg->data[0] | 4;
878 msg->rsp[1] = msg->data[1];
879 msg->rsp[2] = IPMI_ERR_UNSPECIFIED;
880 msg->rsp_size = 3;
881 deliver_recv_msg(smi_info, msg);
882 return;
883 }
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#ifdef DEBUG_TIMING
886 do_gettimeofday(&t);
887 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
888#endif
889
890 if (smi_info->run_to_completion) {
Corey Minyardbda4c302008-04-29 01:01:02 -0700891 /*
892 * If we are running to completion, then throw it in
893 * the list and run transactions until everything is
894 * clear. Priority doesn't matter here.
895 */
896
897 /*
898 * Run to completion means we are single-threaded, no
899 * need for locks.
900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 list_add_tail(&(msg->link), &(smi_info->xmit_msgs));
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 result = smi_event_handler(smi_info, 0);
904 while (result != SI_SM_IDLE) {
905 udelay(SI_SHORT_TIMEOUT_USEC);
906 result = smi_event_handler(smi_info,
907 SI_SHORT_TIMEOUT_USEC);
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Corey Minyardf60adf42012-03-28 14:42:50 -0700912 spin_lock_irqsave(&smi_info->si_lock, flags);
Corey Minyardbda4c302008-04-29 01:01:02 -0700913 if (priority > 0)
914 list_add_tail(&msg->link, &smi_info->hp_xmit_msgs);
915 else
916 list_add_tail(&msg->link, &smi_info->xmit_msgs);
Corey Minyardbda4c302008-04-29 01:01:02 -0700917
Srinivas_Gowdab88e7692012-03-28 14:42:48 -0700918 if (smi_info->si_state == SI_NORMAL && smi_info->curr_msg == NULL) {
Bodo Stroesser48e8ac22014-04-14 09:46:51 -0500919 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
Corey Minyardf60adf42012-03-28 14:42:50 -0700920
921 if (smi_info->thread)
922 wake_up_process(smi_info->thread);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 start_next_msg(smi_info);
Srinivas_Gowdab88e7692012-03-28 14:42:48 -0700925 smi_event_handler(smi_info, 0);
926 }
Corey Minyardbda4c302008-04-29 01:01:02 -0700927 spin_unlock_irqrestore(&smi_info->si_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static void set_run_to_completion(void *send_info, int i_run_to_completion)
931{
932 struct smi_info *smi_info = send_info;
933 enum si_sm_result result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 smi_info->run_to_completion = i_run_to_completion;
936 if (i_run_to_completion) {
937 result = smi_event_handler(smi_info, 0);
938 while (result != SI_SM_IDLE) {
939 udelay(SI_SHORT_TIMEOUT_USEC);
940 result = smi_event_handler(smi_info,
941 SI_SHORT_TIMEOUT_USEC);
942 }
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Martin Wilckae74e822010-03-10 15:23:06 -0800946/*
947 * Use -1 in the nsec value of the busy waiting timespec to tell that
948 * we are spinning in kipmid looking for something and not delaying
949 * between checks
950 */
951static inline void ipmi_si_set_not_busy(struct timespec *ts)
952{
953 ts->tv_nsec = -1;
954}
955static inline int ipmi_si_is_busy(struct timespec *ts)
956{
957 return ts->tv_nsec != -1;
958}
959
960static int ipmi_thread_busy_wait(enum si_sm_result smi_result,
961 const struct smi_info *smi_info,
962 struct timespec *busy_until)
963{
964 unsigned int max_busy_us = 0;
965
966 if (smi_info->intf_num < num_max_busy_us)
967 max_busy_us = kipmid_max_busy_us[smi_info->intf_num];
968 if (max_busy_us == 0 || smi_result != SI_SM_CALL_WITH_DELAY)
969 ipmi_si_set_not_busy(busy_until);
970 else if (!ipmi_si_is_busy(busy_until)) {
971 getnstimeofday(busy_until);
972 timespec_add_ns(busy_until, max_busy_us*NSEC_PER_USEC);
973 } else {
974 struct timespec now;
975 getnstimeofday(&now);
976 if (unlikely(timespec_compare(&now, busy_until) > 0)) {
977 ipmi_si_set_not_busy(busy_until);
978 return 0;
979 }
980 }
981 return 1;
982}
983
984
985/*
986 * A busy-waiting loop for speeding up IPMI operation.
987 *
988 * Lousy hardware makes this hard. This is only enabled for systems
989 * that are not BT and do not have interrupts. It starts spinning
990 * when an operation is complete or until max_busy tells it to stop
991 * (if that is enabled). See the paragraph on kimid_max_busy_us in
992 * Documentation/IPMI.txt for details.
993 */
Corey Minyarda9a2c442005-11-07 01:00:03 -0800994static int ipmi_thread(void *data)
995{
996 struct smi_info *smi_info = data;
Matt Domsche9a705a2005-11-07 01:00:04 -0800997 unsigned long flags;
Corey Minyarda9a2c442005-11-07 01:00:03 -0800998 enum si_sm_result smi_result;
Martin Wilckae74e822010-03-10 15:23:06 -0800999 struct timespec busy_until;
Corey Minyarda9a2c442005-11-07 01:00:03 -08001000
Martin Wilckae74e822010-03-10 15:23:06 -08001001 ipmi_si_set_not_busy(&busy_until);
Corey Minyarda9a2c442005-11-07 01:00:03 -08001002 set_user_nice(current, 19);
Matt Domsche9a705a2005-11-07 01:00:04 -08001003 while (!kthread_should_stop()) {
Martin Wilckae74e822010-03-10 15:23:06 -08001004 int busy_wait;
1005
Corey Minyarda9a2c442005-11-07 01:00:03 -08001006 spin_lock_irqsave(&(smi_info->si_lock), flags);
Corey Minyard8a3628d2006-03-31 02:30:40 -08001007 smi_result = smi_event_handler(smi_info, 0);
Bodo Stroesser48e8ac22014-04-14 09:46:51 -05001008
1009 /*
1010 * If the driver is doing something, there is a possible
1011 * race with the timer. If the timer handler see idle,
1012 * and the thread here sees something else, the timer
1013 * handler won't restart the timer even though it is
1014 * required. So start it here if necessary.
1015 */
1016 if (smi_result != SI_SM_IDLE && !smi_info->timer_running)
1017 smi_mod_timer(smi_info, jiffies + SI_TIMEOUT_JIFFIES);
1018
Corey Minyarda9a2c442005-11-07 01:00:03 -08001019 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
Martin Wilckae74e822010-03-10 15:23:06 -08001020 busy_wait = ipmi_thread_busy_wait(smi_result, smi_info,
1021 &busy_until);
Corey Minyardc305e3d2008-04-29 01:01:10 -07001022 if (smi_result == SI_SM_CALL_WITHOUT_DELAY)
1023 ; /* do nothing */
Martin Wilckae74e822010-03-10 15:23:06 -08001024 else if (smi_result == SI_SM_CALL_WITH_DELAY && busy_wait)
akpm@osdl.org33979732006-06-27 02:54:04 -07001025 schedule();
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001026 else if (smi_result == SI_SM_IDLE)
1027 schedule_timeout_interruptible(100);
Matt Domsche9a705a2005-11-07 01:00:04 -08001028 else
Martin Wilck8d1f66d2010-06-29 15:05:31 -07001029 schedule_timeout_interruptible(1);
Corey Minyarda9a2c442005-11-07 01:00:03 -08001030 }
Corey Minyarda9a2c442005-11-07 01:00:03 -08001031 return 0;
1032}
1033
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static void poll(void *send_info)
1036{
1037 struct smi_info *smi_info = send_info;
Corey Minyardf60adf42012-03-28 14:42:50 -07001038 unsigned long flags = 0;
1039 int run_to_completion = smi_info->run_to_completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Corey Minyard15c62e12006-12-06 20:41:06 -08001041 /*
1042 * Make sure there is some delay in the poll loop so we can
1043 * drive time forward and timeout things.
1044 */
1045 udelay(10);
Corey Minyardf60adf42012-03-28 14:42:50 -07001046 if (!run_to_completion)
1047 spin_lock_irqsave(&smi_info->si_lock, flags);
Corey Minyard15c62e12006-12-06 20:41:06 -08001048 smi_event_handler(smi_info, 10);
Corey Minyardf60adf42012-03-28 14:42:50 -07001049 if (!run_to_completion)
1050 spin_unlock_irqrestore(&smi_info->si_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static void request_events(void *send_info)
1054{
1055 struct smi_info *smi_info = send_info;
1056
Corey Minyard40112ae2009-04-21 12:24:03 -07001057 if (atomic_read(&smi_info->stop_operation) ||
1058 !smi_info->has_event_buffer)
Corey Minyardb361e272006-12-06 20:41:07 -08001059 return;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 atomic_set(&smi_info->req_events, 1);
1062}
1063
Randy Dunlap0c8204b2006-12-10 02:19:06 -08001064static int initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void smi_timeout(unsigned long data)
1067{
1068 struct smi_info *smi_info = (struct smi_info *) data;
1069 enum si_sm_result smi_result;
1070 unsigned long flags;
1071 unsigned long jiffies_now;
Corey Minyardc4edff12005-11-07 00:59:56 -08001072 long time_diff;
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001073 long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#ifdef DEBUG_TIMING
1075 struct timeval t;
1076#endif
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 spin_lock_irqsave(&(smi_info->si_lock), flags);
1079#ifdef DEBUG_TIMING
1080 do_gettimeofday(&t);
Corey Minyardc305e3d2008-04-29 01:01:10 -07001081 printk(KERN_DEBUG "**Timer: %d.%9.9d\n", t.tv_sec, t.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#endif
1083 jiffies_now = jiffies;
Corey Minyardc4edff12005-11-07 00:59:56 -08001084 time_diff = (((long)jiffies_now - (long)smi_info->last_timeout_jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * SI_USEC_PER_JIFFY);
1086 smi_result = smi_event_handler(smi_info, time_diff);
1087
Corey Minyardb0defcd2006-03-26 01:37:20 -08001088 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* Running with interrupts, only do long timeouts. */
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001090 timeout = jiffies + SI_TIMEOUT_JIFFIES;
Corey Minyard64959e22008-04-29 01:01:07 -07001091 smi_inc_stat(smi_info, long_timeouts);
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001092 goto do_mod_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Corey Minyardc305e3d2008-04-29 01:01:10 -07001095 /*
1096 * If the state machine asks for a short delay, then shorten
1097 * the timer timeout.
1098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (smi_result == SI_SM_CALL_WITH_DELAY) {
Corey Minyard64959e22008-04-29 01:01:07 -07001100 smi_inc_stat(smi_info, short_timeouts);
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001101 timeout = jiffies + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 } else {
Corey Minyard64959e22008-04-29 01:01:07 -07001103 smi_inc_stat(smi_info, long_timeouts);
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001104 timeout = jiffies + SI_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Matthew Garrett3326f4f2010-05-26 14:43:49 -07001107 do_mod_timer:
1108 if (smi_result != SI_SM_IDLE)
Bodo Stroesser48e8ac22014-04-14 09:46:51 -05001109 smi_mod_timer(smi_info, timeout);
1110 else
1111 smi_info->timer_running = false;
1112 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
David Howells7d12e782006-10-05 14:55:46 +01001115static irqreturn_t si_irq_handler(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct smi_info *smi_info = data;
1118 unsigned long flags;
1119#ifdef DEBUG_TIMING
1120 struct timeval t;
1121#endif
1122
1123 spin_lock_irqsave(&(smi_info->si_lock), flags);
1124
Corey Minyard64959e22008-04-29 01:01:07 -07001125 smi_inc_stat(smi_info, interrupts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127#ifdef DEBUG_TIMING
1128 do_gettimeofday(&t);
Corey Minyardc305e3d2008-04-29 01:01:10 -07001129 printk(KERN_DEBUG "**Interrupt: %d.%9.9d\n", t.tv_sec, t.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#endif
1131 smi_event_handler(smi_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1133 return IRQ_HANDLED;
1134}
1135
David Howells7d12e782006-10-05 14:55:46 +01001136static irqreturn_t si_bt_irq_handler(int irq, void *data)
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001137{
1138 struct smi_info *smi_info = data;
1139 /* We need to clear the IRQ flag for the BT interface. */
1140 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1141 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1142 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
David Howells7d12e782006-10-05 14:55:46 +01001143 return si_irq_handler(irq, data);
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001144}
1145
Corey Minyard453823b2006-03-31 02:30:39 -08001146static int smi_start_processing(void *send_info,
1147 ipmi_smi_t intf)
1148{
1149 struct smi_info *new_smi = send_info;
Corey Minyarda51f4a82006-10-03 01:13:59 -07001150 int enable = 0;
Corey Minyard453823b2006-03-31 02:30:39 -08001151
1152 new_smi->intf = intf;
1153
Corey Minyardc45adc32007-10-18 03:07:08 -07001154 /* Try to claim any interrupts. */
1155 if (new_smi->irq_setup)
1156 new_smi->irq_setup(new_smi);
1157
Corey Minyard453823b2006-03-31 02:30:39 -08001158 /* Set up the timer that drives the interface. */
1159 setup_timer(&new_smi->si_timer, smi_timeout, (long)new_smi);
Bodo Stroesser48e8ac22014-04-14 09:46:51 -05001160 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
Corey Minyard453823b2006-03-31 02:30:39 -08001161
Corey Minyarddf3fe8d2006-09-30 23:28:20 -07001162 /*
Corey Minyarda51f4a82006-10-03 01:13:59 -07001163 * Check if the user forcefully enabled the daemon.
1164 */
1165 if (new_smi->intf_num < num_force_kipmid)
1166 enable = force_kipmid[new_smi->intf_num];
1167 /*
Corey Minyarddf3fe8d2006-09-30 23:28:20 -07001168 * The BT interface is efficient enough to not need a thread,
1169 * and there is no need for a thread if we have interrupts.
1170 */
Corey Minyardc305e3d2008-04-29 01:01:10 -07001171 else if ((new_smi->si_type != SI_BT) && (!new_smi->irq))
Corey Minyarda51f4a82006-10-03 01:13:59 -07001172 enable = 1;
1173
1174 if (enable) {
Corey Minyard453823b2006-03-31 02:30:39 -08001175 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1176 "kipmi%d", new_smi->intf_num);
1177 if (IS_ERR(new_smi->thread)) {
Myron Stowe279fbd02010-05-26 14:43:52 -07001178 dev_notice(new_smi->dev, "Could not start"
1179 " kernel thread due to error %ld, only using"
1180 " timers to drive the interface\n",
1181 PTR_ERR(new_smi->thread));
Corey Minyard453823b2006-03-31 02:30:39 -08001182 new_smi->thread = NULL;
1183 }
1184 }
1185
1186 return 0;
1187}
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001188
Zhao Yakui16f42322010-12-08 10:10:16 +08001189static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1190{
1191 struct smi_info *smi = send_info;
1192
1193 data->addr_src = smi->addr_source;
1194 data->dev = smi->dev;
1195 data->addr_info = smi->addr_info;
1196 get_device(smi->dev);
1197
1198 return 0;
1199}
1200
Corey Minyardb9675132006-12-06 20:41:02 -08001201static void set_maintenance_mode(void *send_info, int enable)
1202{
1203 struct smi_info *smi_info = send_info;
1204
1205 if (!enable)
1206 atomic_set(&smi_info->req_events, 0);
1207}
1208
Corey Minyardc305e3d2008-04-29 01:01:10 -07001209static struct ipmi_smi_handlers handlers = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 .owner = THIS_MODULE,
Corey Minyard453823b2006-03-31 02:30:39 -08001211 .start_processing = smi_start_processing,
Zhao Yakui16f42322010-12-08 10:10:16 +08001212 .get_smi_info = get_smi_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 .sender = sender,
1214 .request_events = request_events,
Corey Minyardb9675132006-12-06 20:41:02 -08001215 .set_maintenance_mode = set_maintenance_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .set_run_to_completion = set_run_to_completion,
1217 .poll = poll,
1218};
1219
Corey Minyardc305e3d2008-04-29 01:01:10 -07001220/*
1221 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1222 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
1223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Corey Minyardb0defcd2006-03-26 01:37:20 -08001225static LIST_HEAD(smi_infos);
Corey Minyardd6dfd132006-03-31 02:30:41 -08001226static DEFINE_MUTEX(smi_infos_lock);
Corey Minyardb0defcd2006-03-26 01:37:20 -08001227static int smi_num; /* Used to sequence the SMIs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229#define DEFAULT_REGSPACING 1
Corey Minyarddba9b4f2007-05-08 00:23:51 -07001230#define DEFAULT_REGSIZE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Corey Minyardd941aea2013-02-27 17:05:12 -08001232#ifdef CONFIG_ACPI
1233static bool si_tryacpi = 1;
1234#endif
1235#ifdef CONFIG_DMI
1236static bool si_trydmi = 1;
1237#endif
Corey Minyardf2afae42013-02-27 17:05:13 -08001238static bool si_tryplatform = 1;
1239#ifdef CONFIG_PCI
1240static bool si_trypci = 1;
1241#endif
Corey Minyard0dfe6e72014-04-14 09:46:53 -05001242static bool si_trydefaults = IS_ENABLED(CONFIG_IPMI_SI_PROBE_DEFAULTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static char *si_type[SI_MAX_PARMS];
1244#define MAX_SI_TYPE_STR 30
1245static char si_type_str[MAX_SI_TYPE_STR];
1246static unsigned long addrs[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001247static unsigned int num_addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static unsigned int ports[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001249static unsigned int num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static int irqs[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001251static unsigned int num_irqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static int regspacings[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001253static unsigned int num_regspacings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static int regsizes[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001255static unsigned int num_regsizes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256static int regshifts[SI_MAX_PARMS];
Al Viro64a6f952007-10-14 19:35:30 +01001257static unsigned int num_regshifts;
Bela Lubkin2f95d512010-03-10 15:23:07 -08001258static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
Al Viro64a6f952007-10-14 19:35:30 +01001259static unsigned int num_slave_addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Corey Minyardb361e272006-12-06 20:41:07 -08001261#define IPMI_IO_ADDR_SPACE 0
1262#define IPMI_MEM_ADDR_SPACE 1
Corey Minyard1d5636c2006-12-10 02:19:08 -08001263static char *addr_space_to_str[] = { "i/o", "mem" };
Corey Minyardb361e272006-12-06 20:41:07 -08001264
1265static int hotmod_handler(const char *val, struct kernel_param *kp);
1266
1267module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1268MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
1269 " Documentation/IPMI.txt in the kernel sources for the"
1270 " gory details.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Corey Minyardd941aea2013-02-27 17:05:12 -08001272#ifdef CONFIG_ACPI
1273module_param_named(tryacpi, si_tryacpi, bool, 0);
1274MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1275 " default scan of the interfaces identified via ACPI");
1276#endif
1277#ifdef CONFIG_DMI
1278module_param_named(trydmi, si_trydmi, bool, 0);
1279MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
1280 " default scan of the interfaces identified via DMI");
1281#endif
Corey Minyardf2afae42013-02-27 17:05:13 -08001282module_param_named(tryplatform, si_tryplatform, bool, 0);
1283MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1284 " default scan of the interfaces identified via platform"
1285 " interfaces like openfirmware");
1286#ifdef CONFIG_PCI
1287module_param_named(trypci, si_trypci, bool, 0);
1288MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1289 " default scan of the interfaces identified via pci");
1290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291module_param_named(trydefaults, si_trydefaults, bool, 0);
1292MODULE_PARM_DESC(trydefaults, "Setting this to 'false' will disable the"
1293 " default scan of the KCS and SMIC interface at the standard"
1294 " address");
1295module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1296MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1297 " interface separated by commas. The types are 'kcs',"
1298 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1299 " the first interface to kcs and the second to bt");
Al Viro64a6f952007-10-14 19:35:30 +01001300module_param_array(addrs, ulong, &num_addrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1302 " addresses separated by commas. Only use if an interface"
1303 " is in memory. Otherwise, set it to zero or leave"
1304 " it blank.");
Al Viro64a6f952007-10-14 19:35:30 +01001305module_param_array(ports, uint, &num_ports, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1307 " addresses separated by commas. Only use if an interface"
1308 " is a port. Otherwise, set it to zero or leave"
1309 " it blank.");
1310module_param_array(irqs, int, &num_irqs, 0);
1311MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1312 " addresses separated by commas. Only use if an interface"
1313 " has an interrupt. Otherwise, set it to zero or leave"
1314 " it blank.");
1315module_param_array(regspacings, int, &num_regspacings, 0);
1316MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1317 " and each successive register used by the interface. For"
1318 " instance, if the start address is 0xca2 and the spacing"
1319 " is 2, then the second address is at 0xca4. Defaults"
1320 " to 1.");
1321module_param_array(regsizes, int, &num_regsizes, 0);
1322MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1323 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1324 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1325 " the 8-bit IPMI register has to be read from a larger"
1326 " register.");
1327module_param_array(regshifts, int, &num_regshifts, 0);
1328MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1329 " IPMI register, in bits. For instance, if the data"
1330 " is read from a 32-bit word and the IPMI data is in"
1331 " bit 8-15, then the shift would be 8");
1332module_param_array(slave_addrs, int, &num_slave_addrs, 0);
1333MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1334 " the controller. Normally this is 0x20, but can be"
1335 " overridden by this parm. This is an array indexed"
1336 " by interface number.");
Corey Minyarda51f4a82006-10-03 01:13:59 -07001337module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1338MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1339 " disabled(0). Normally the IPMI driver auto-detects"
1340 " this, but the value may be overridden by this parm.");
Corey Minyardb361e272006-12-06 20:41:07 -08001341module_param(unload_when_empty, int, 0);
1342MODULE_PARM_DESC(unload_when_empty, "Unload the module if no interfaces are"
1343 " specified or found, default is 1. Setting to 0"
1344 " is useful for hot add of devices using hotmod.");
Martin Wilckae74e822010-03-10 15:23:06 -08001345module_param_array(kipmid_max_busy_us, uint, &num_max_busy_us, 0644);
1346MODULE_PARM_DESC(kipmid_max_busy_us,
1347 "Max time (in microseconds) to busy-wait for IPMI data before"
1348 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1349 " if kipmid is using up a lot of CPU time.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351
Corey Minyardb0defcd2006-03-26 01:37:20 -08001352static void std_irq_cleanup(struct smi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001354 if (info->si_type == SI_BT)
1355 /* Disable the interrupt in the BT interface. */
1356 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1357 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360static int std_irq_setup(struct smi_info *info)
1361{
1362 int rv;
1363
Corey Minyardb0defcd2006-03-26 01:37:20 -08001364 if (!info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001367 if (info->si_type == SI_BT) {
1368 rv = request_irq(info->irq,
1369 si_bt_irq_handler,
Michael Opdenackeraa5b2ba2014-01-24 14:00:50 -06001370 IRQF_SHARED,
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001371 DEVICE_NAME,
1372 info);
Corey Minyardb0defcd2006-03-26 01:37:20 -08001373 if (!rv)
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001374 /* Enable the interrupt in the BT interface. */
1375 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1376 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1377 } else
1378 rv = request_irq(info->irq,
1379 si_irq_handler,
Michael Opdenackeraa5b2ba2014-01-24 14:00:50 -06001380 IRQF_SHARED,
Corey Minyard9dbf68f2005-05-01 08:59:11 -07001381 DEVICE_NAME,
1382 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07001384 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1385 " running polled\n",
1386 DEVICE_NAME, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 info->irq = 0;
1388 } else {
Corey Minyardb0defcd2006-03-26 01:37:20 -08001389 info->irq_cleanup = std_irq_cleanup;
Myron Stowe279fbd02010-05-26 14:43:52 -07001390 dev_info(info->dev, "Using irq %d\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 return rv;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static unsigned char port_inb(struct si_sm_io *io, unsigned int offset)
1397{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001398 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Corey Minyardb0defcd2006-03-26 01:37:20 -08001400 return inb(addr + (offset * io->regspacing));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403static void port_outb(struct si_sm_io *io, unsigned int offset,
1404 unsigned char b)
1405{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001406 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Corey Minyardb0defcd2006-03-26 01:37:20 -08001408 outb(b, addr + (offset * io->regspacing));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411static unsigned char port_inw(struct si_sm_io *io, unsigned int offset)
1412{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001413 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Corey Minyardb0defcd2006-03-26 01:37:20 -08001415 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
1418static void port_outw(struct si_sm_io *io, unsigned int offset,
1419 unsigned char b)
1420{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001421 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Corey Minyardb0defcd2006-03-26 01:37:20 -08001423 outw(b << io->regshift, addr + (offset * io->regspacing));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426static unsigned char port_inl(struct si_sm_io *io, unsigned int offset)
1427{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001428 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Corey Minyardb0defcd2006-03-26 01:37:20 -08001430 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433static void port_outl(struct si_sm_io *io, unsigned int offset,
1434 unsigned char b)
1435{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001436 unsigned int addr = io->addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Corey Minyardb0defcd2006-03-26 01:37:20 -08001438 outl(b << io->regshift, addr+(offset * io->regspacing));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441static void port_cleanup(struct smi_info *info)
1442{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001443 unsigned int addr = info->io.addr_data;
Corey Minyardd61a3ea2006-05-30 21:25:57 -07001444 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Corey Minyardb0defcd2006-03-26 01:37:20 -08001446 if (addr) {
Corey Minyardc305e3d2008-04-29 01:01:10 -07001447 for (idx = 0; idx < info->io_size; idx++)
Corey Minyardd61a3ea2006-05-30 21:25:57 -07001448 release_region(addr + idx * info->io.regspacing,
1449 info->io.regsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453static int port_setup(struct smi_info *info)
1454{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001455 unsigned int addr = info->io.addr_data;
Corey Minyardd61a3ea2006-05-30 21:25:57 -07001456 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Corey Minyardb0defcd2006-03-26 01:37:20 -08001458 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -ENODEV;
1460
1461 info->io_cleanup = port_cleanup;
1462
Corey Minyardc305e3d2008-04-29 01:01:10 -07001463 /*
1464 * Figure out the actual inb/inw/inl/etc routine to use based
1465 * upon the register size.
1466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 switch (info->io.regsize) {
1468 case 1:
1469 info->io.inputb = port_inb;
1470 info->io.outputb = port_outb;
1471 break;
1472 case 2:
1473 info->io.inputb = port_inw;
1474 info->io.outputb = port_outw;
1475 break;
1476 case 4:
1477 info->io.inputb = port_inl;
1478 info->io.outputb = port_outl;
1479 break;
1480 default:
Myron Stowe279fbd02010-05-26 14:43:52 -07001481 dev_warn(info->dev, "Invalid register size: %d\n",
1482 info->io.regsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
1484 }
1485
Corey Minyardc305e3d2008-04-29 01:01:10 -07001486 /*
1487 * Some BIOSes reserve disjoint I/O regions in their ACPI
Corey Minyardd61a3ea2006-05-30 21:25:57 -07001488 * tables. This causes problems when trying to register the
1489 * entire I/O region. Therefore we must register each I/O
1490 * port separately.
1491 */
Corey Minyardc305e3d2008-04-29 01:01:10 -07001492 for (idx = 0; idx < info->io_size; idx++) {
Corey Minyardd61a3ea2006-05-30 21:25:57 -07001493 if (request_region(addr + idx * info->io.regspacing,
1494 info->io.regsize, DEVICE_NAME) == NULL) {
1495 /* Undo allocations */
1496 while (idx--) {
1497 release_region(addr + idx * info->io.regspacing,
1498 info->io.regsize);
1499 }
1500 return -EIO;
1501 }
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
1504}
1505
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001506static unsigned char intf_mem_inb(struct si_sm_io *io, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 return readb((io->addr)+(offset * io->regspacing));
1509}
1510
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001511static void intf_mem_outb(struct si_sm_io *io, unsigned int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned char b)
1513{
1514 writeb(b, (io->addr)+(offset * io->regspacing));
1515}
1516
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001517static unsigned char intf_mem_inw(struct si_sm_io *io, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
Alexey Dobriyan64d9fe62006-11-08 17:44:56 -08001520 & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001523static void intf_mem_outw(struct si_sm_io *io, unsigned int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 unsigned char b)
1525{
1526 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1527}
1528
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001529static unsigned char intf_mem_inl(struct si_sm_io *io, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
Alexey Dobriyan64d9fe62006-11-08 17:44:56 -08001532 & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001535static void intf_mem_outl(struct si_sm_io *io, unsigned int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unsigned char b)
1537{
1538 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1539}
1540
1541#ifdef readq
1542static unsigned char mem_inq(struct si_sm_io *io, unsigned int offset)
1543{
1544 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
Alexey Dobriyan64d9fe62006-11-08 17:44:56 -08001545 & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548static void mem_outq(struct si_sm_io *io, unsigned int offset,
1549 unsigned char b)
1550{
1551 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1552}
1553#endif
1554
1555static void mem_cleanup(struct smi_info *info)
1556{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001557 unsigned long addr = info->io.addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int mapsize;
1559
1560 if (info->io.addr) {
1561 iounmap(info->io.addr);
1562
1563 mapsize = ((info->io_size * info->io.regspacing)
1564 - (info->io.regspacing - info->io.regsize));
1565
Corey Minyardb0defcd2006-03-26 01:37:20 -08001566 release_mem_region(addr, mapsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570static int mem_setup(struct smi_info *info)
1571{
Corey Minyardb0defcd2006-03-26 01:37:20 -08001572 unsigned long addr = info->io.addr_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int mapsize;
1574
Corey Minyardb0defcd2006-03-26 01:37:20 -08001575 if (!addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -ENODEV;
1577
1578 info->io_cleanup = mem_cleanup;
1579
Corey Minyardc305e3d2008-04-29 01:01:10 -07001580 /*
1581 * Figure out the actual readb/readw/readl/etc routine to use based
1582 * upon the register size.
1583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 switch (info->io.regsize) {
1585 case 1:
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001586 info->io.inputb = intf_mem_inb;
1587 info->io.outputb = intf_mem_outb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589 case 2:
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001590 info->io.inputb = intf_mem_inw;
1591 info->io.outputb = intf_mem_outw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 break;
1593 case 4:
Alexey Dobriyan546cfdf2006-02-03 03:04:40 -08001594 info->io.inputb = intf_mem_inl;
1595 info->io.outputb = intf_mem_outl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597#ifdef readq
1598 case 8:
1599 info->io.inputb = mem_inq;
1600 info->io.outputb = mem_outq;
1601 break;
1602#endif
1603 default:
Myron Stowe279fbd02010-05-26 14:43:52 -07001604 dev_warn(info->dev, "Invalid register size: %d\n",
1605 info->io.regsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -EINVAL;
1607 }
1608
Corey Minyardc305e3d2008-04-29 01:01:10 -07001609 /*
1610 * Calculate the total amount of memory to claim. This is an
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 * unusual looking calculation, but it avoids claiming any
1612 * more memory than it has to. It will claim everything
1613 * between the first address to the end of the last full
Corey Minyardc305e3d2008-04-29 01:01:10 -07001614 * register.
1615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 mapsize = ((info->io_size * info->io.regspacing)
1617 - (info->io.regspacing - info->io.regsize));
1618
Corey Minyardb0defcd2006-03-26 01:37:20 -08001619 if (request_mem_region(addr, mapsize, DEVICE_NAME) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EIO;
1621
Corey Minyardb0defcd2006-03-26 01:37:20 -08001622 info->io.addr = ioremap(addr, mapsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (info->io.addr == NULL) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08001624 release_mem_region(addr, mapsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return -EIO;
1626 }
1627 return 0;
1628}
1629
Corey Minyardb361e272006-12-06 20:41:07 -08001630/*
1631 * Parms come in as <op1>[:op2[:op3...]]. ops are:
1632 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1633 * Options are:
1634 * rsp=<regspacing>
1635 * rsi=<regsize>
1636 * rsh=<regshift>
1637 * irq=<irq>
1638 * ipmb=<ipmb addr>
1639 */
1640enum hotmod_op { HM_ADD, HM_REMOVE };
1641struct hotmod_vals {
1642 char *name;
1643 int val;
1644};
1645static struct hotmod_vals hotmod_ops[] = {
1646 { "add", HM_ADD },
1647 { "remove", HM_REMOVE },
1648 { NULL }
1649};
1650static struct hotmod_vals hotmod_si[] = {
1651 { "kcs", SI_KCS },
1652 { "smic", SI_SMIC },
1653 { "bt", SI_BT },
1654 { NULL }
1655};
1656static struct hotmod_vals hotmod_as[] = {
1657 { "mem", IPMI_MEM_ADDR_SPACE },
1658 { "i/o", IPMI_IO_ADDR_SPACE },
1659 { NULL }
1660};
Corey Minyard1d5636c2006-12-10 02:19:08 -08001661
Corey Minyardb361e272006-12-06 20:41:07 -08001662static int parse_str(struct hotmod_vals *v, int *val, char *name, char **curr)
1663{
1664 char *s;
1665 int i;
1666
1667 s = strchr(*curr, ',');
1668 if (!s) {
1669 printk(KERN_WARNING PFX "No hotmod %s given.\n", name);
1670 return -EINVAL;
1671 }
1672 *s = '\0';
1673 s++;
1674 for (i = 0; hotmod_ops[i].name; i++) {
Corey Minyard1d5636c2006-12-10 02:19:08 -08001675 if (strcmp(*curr, v[i].name) == 0) {
Corey Minyardb361e272006-12-06 20:41:07 -08001676 *val = v[i].val;
1677 *curr = s;
1678 return 0;
1679 }
1680 }
1681
1682 printk(KERN_WARNING PFX "Invalid hotmod %s '%s'\n", name, *curr);
1683 return -EINVAL;
1684}
1685
Corey Minyard1d5636c2006-12-10 02:19:08 -08001686static int check_hotmod_int_op(const char *curr, const char *option,
1687 const char *name, int *val)
1688{
1689 char *n;
1690
1691 if (strcmp(curr, name) == 0) {
1692 if (!option) {
1693 printk(KERN_WARNING PFX
1694 "No option given for '%s'\n",
1695 curr);
1696 return -EINVAL;
1697 }
1698 *val = simple_strtoul(option, &n, 0);
1699 if ((*n != '\0') || (*option == '\0')) {
1700 printk(KERN_WARNING PFX
1701 "Bad option given for '%s'\n",
1702 curr);
1703 return -EINVAL;
1704 }
1705 return 1;
1706 }
1707 return 0;
1708}
1709
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07001710static struct smi_info *smi_info_alloc(void)
1711{
1712 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1713
Corey Minyardf60adf42012-03-28 14:42:50 -07001714 if (info)
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07001715 spin_lock_init(&info->si_lock);
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07001716 return info;
1717}
1718
Corey Minyardb361e272006-12-06 20:41:07 -08001719static int hotmod_handler(const char *val, struct kernel_param *kp)
1720{
1721 char *str = kstrdup(val, GFP_KERNEL);
Corey Minyard1d5636c2006-12-10 02:19:08 -08001722 int rv;
Corey Minyardb361e272006-12-06 20:41:07 -08001723 char *next, *curr, *s, *n, *o;
1724 enum hotmod_op op;
1725 enum si_type si_type;
1726 int addr_space;
1727 unsigned long addr;
1728 int regspacing;
1729 int regsize;
1730 int regshift;
1731 int irq;
1732 int ipmb;
1733 int ival;
Corey Minyard1d5636c2006-12-10 02:19:08 -08001734 int len;
Corey Minyardb361e272006-12-06 20:41:07 -08001735 struct smi_info *info;
1736
1737 if (!str)
1738 return -ENOMEM;
1739
1740 /* Kill any trailing spaces, as we can get a "\n" from echo. */
Corey Minyard1d5636c2006-12-10 02:19:08 -08001741 len = strlen(str);
1742 ival = len - 1;
Corey Minyardb361e272006-12-06 20:41:07 -08001743 while ((ival >= 0) && isspace(str[ival])) {
1744 str[ival] = '\0';
1745 ival--;
1746 }
1747
1748 for (curr = str; curr; curr = next) {
1749 regspacing = 1;
1750 regsize = 1;
1751 regshift = 0;
1752 irq = 0;
Bela Lubkin2f95d512010-03-10 15:23:07 -08001753 ipmb = 0; /* Choose the default if not specified */
Corey Minyardb361e272006-12-06 20:41:07 -08001754
1755 next = strchr(curr, ':');
1756 if (next) {
1757 *next = '\0';
1758 next++;
1759 }
1760
1761 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1762 if (rv)
1763 break;
1764 op = ival;
1765
1766 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1767 if (rv)
1768 break;
1769 si_type = ival;
1770
1771 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1772 if (rv)
1773 break;
1774
1775 s = strchr(curr, ',');
1776 if (s) {
1777 *s = '\0';
1778 s++;
1779 }
1780 addr = simple_strtoul(curr, &n, 0);
1781 if ((*n != '\0') || (*curr == '\0')) {
1782 printk(KERN_WARNING PFX "Invalid hotmod address"
1783 " '%s'\n", curr);
1784 break;
1785 }
1786
1787 while (s) {
1788 curr = s;
1789 s = strchr(curr, ',');
1790 if (s) {
1791 *s = '\0';
1792 s++;
1793 }
1794 o = strchr(curr, '=');
1795 if (o) {
1796 *o = '\0';
1797 o++;
1798 }
Corey Minyard1d5636c2006-12-10 02:19:08 -08001799 rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
1800 if (rv < 0)
Corey Minyardb361e272006-12-06 20:41:07 -08001801 goto out;
Corey Minyard1d5636c2006-12-10 02:19:08 -08001802 else if (rv)
1803 continue;
1804 rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
1805 if (rv < 0)
1806 goto out;
1807 else if (rv)
1808 continue;
1809 rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
1810 if (rv < 0)
1811 goto out;
1812 else if (rv)
1813 continue;
1814 rv = check_hotmod_int_op(curr, o, "irq", &irq);
1815 if (rv < 0)
1816 goto out;
1817 else if (rv)
1818 continue;
1819 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1820 if (rv < 0)
1821 goto out;
1822 else if (rv)
1823 continue;
1824
1825 rv = -EINVAL;
1826 printk(KERN_WARNING PFX
1827 "Invalid hotmod option '%s'\n",
1828 curr);
1829 goto out;
Corey Minyardb361e272006-12-06 20:41:07 -08001830 }
1831
1832 if (op == HM_ADD) {
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07001833 info = smi_info_alloc();
Corey Minyardb361e272006-12-06 20:41:07 -08001834 if (!info) {
1835 rv = -ENOMEM;
1836 goto out;
1837 }
1838
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07001839 info->addr_source = SI_HOTMOD;
Corey Minyardb361e272006-12-06 20:41:07 -08001840 info->si_type = si_type;
1841 info->io.addr_data = addr;
1842 info->io.addr_type = addr_space;
1843 if (addr_space == IPMI_MEM_ADDR_SPACE)
1844 info->io_setup = mem_setup;
1845 else
1846 info->io_setup = port_setup;
1847
1848 info->io.addr = NULL;
1849 info->io.regspacing = regspacing;
1850 if (!info->io.regspacing)
1851 info->io.regspacing = DEFAULT_REGSPACING;
1852 info->io.regsize = regsize;
1853 if (!info->io.regsize)
1854 info->io.regsize = DEFAULT_REGSPACING;
1855 info->io.regshift = regshift;
1856 info->irq = irq;
1857 if (info->irq)
1858 info->irq_setup = std_irq_setup;
1859 info->slave_addr = ipmb;
1860
Corey Minyardd02b3702014-01-24 14:00:53 -06001861 rv = add_smi(info);
1862 if (rv) {
Yinghai Lu7faefea2010-08-10 18:03:10 -07001863 kfree(info);
Corey Minyardd02b3702014-01-24 14:00:53 -06001864 goto out;
1865 }
1866 rv = try_smi_init(info);
1867 if (rv) {
1868 cleanup_one_si(info);
1869 goto out;
Yinghai Lu7faefea2010-08-10 18:03:10 -07001870 }
Corey Minyardb361e272006-12-06 20:41:07 -08001871 } else {
1872 /* remove */
1873 struct smi_info *e, *tmp_e;
1874
1875 mutex_lock(&smi_infos_lock);
1876 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1877 if (e->io.addr_type != addr_space)
1878 continue;
1879 if (e->si_type != si_type)
1880 continue;
1881 if (e->io.addr_data == addr)
1882 cleanup_one_si(e);
1883 }
1884 mutex_unlock(&smi_infos_lock);
1885 }
1886 }
Corey Minyard1d5636c2006-12-10 02:19:08 -08001887 rv = len;
Corey Minyardb361e272006-12-06 20:41:07 -08001888 out:
1889 kfree(str);
1890 return rv;
1891}
Corey Minyardb0defcd2006-03-26 01:37:20 -08001892
Bill Pemberton2223cbe2012-11-19 13:22:51 -05001893static int hardcode_find_bmc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Rob Herringa1e9c9d2011-02-23 15:37:59 -06001895 int ret = -ENODEV;
Corey Minyardb0defcd2006-03-26 01:37:20 -08001896 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct smi_info *info;
1898
Corey Minyardb0defcd2006-03-26 01:37:20 -08001899 for (i = 0; i < SI_MAX_PARMS; i++) {
1900 if (!ports[i] && !addrs[i])
1901 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07001903 info = smi_info_alloc();
Corey Minyardb0defcd2006-03-26 01:37:20 -08001904 if (!info)
Rob Herringa1e9c9d2011-02-23 15:37:59 -06001905 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07001907 info->addr_source = SI_HARDCODED;
Myron Stowe279fbd02010-05-26 14:43:52 -07001908 printk(KERN_INFO PFX "probing via hardcoded address\n");
Corey Minyardb0defcd2006-03-26 01:37:20 -08001909
Corey Minyard1d5636c2006-12-10 02:19:08 -08001910 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08001911 info->si_type = SI_KCS;
Corey Minyard1d5636c2006-12-10 02:19:08 -08001912 } else if (strcmp(si_type[i], "smic") == 0) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08001913 info->si_type = SI_SMIC;
Corey Minyard1d5636c2006-12-10 02:19:08 -08001914 } else if (strcmp(si_type[i], "bt") == 0) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08001915 info->si_type = SI_BT;
1916 } else {
Myron Stowe279fbd02010-05-26 14:43:52 -07001917 printk(KERN_WARNING PFX "Interface type specified "
Corey Minyardb0defcd2006-03-26 01:37:20 -08001918 "for interface %d, was invalid: %s\n",
1919 i, si_type[i]);
1920 kfree(info);
1921 continue;
1922 }
1923
1924 if (ports[i]) {
1925 /* An I/O port */
1926 info->io_setup = port_setup;
1927 info->io.addr_data = ports[i];
1928 info->io.addr_type = IPMI_IO_ADDR_SPACE;
1929 } else if (addrs[i]) {
1930 /* A memory port */
1931 info->io_setup = mem_setup;
1932 info->io.addr_data = addrs[i];
1933 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
1934 } else {
Myron Stowe279fbd02010-05-26 14:43:52 -07001935 printk(KERN_WARNING PFX "Interface type specified "
1936 "for interface %d, but port and address were "
1937 "not set or set to zero.\n", i);
Corey Minyardb0defcd2006-03-26 01:37:20 -08001938 kfree(info);
1939 continue;
1940 }
1941
1942 info->io.addr = NULL;
1943 info->io.regspacing = regspacings[i];
1944 if (!info->io.regspacing)
1945 info->io.regspacing = DEFAULT_REGSPACING;
1946 info->io.regsize = regsizes[i];
1947 if (!info->io.regsize)
1948 info->io.regsize = DEFAULT_REGSPACING;
1949 info->io.regshift = regshifts[i];
1950 info->irq = irqs[i];
1951 if (info->irq)
1952 info->irq_setup = std_irq_setup;
Bela Lubkin2f95d512010-03-10 15:23:07 -08001953 info->slave_addr = slave_addrs[i];
Corey Minyardb0defcd2006-03-26 01:37:20 -08001954
Yinghai Lu7faefea2010-08-10 18:03:10 -07001955 if (!add_smi(info)) {
Matthew Garrett2407d772010-05-26 14:43:46 -07001956 if (try_smi_init(info))
1957 cleanup_one_si(info);
Rob Herringa1e9c9d2011-02-23 15:37:59 -06001958 ret = 0;
Yinghai Lu7faefea2010-08-10 18:03:10 -07001959 } else {
1960 kfree(info);
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Rob Herringa1e9c9d2011-02-23 15:37:59 -06001963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Len Brown84663612005-08-24 12:09:07 -04001966#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968#include <linux/acpi.h>
1969
Corey Minyardc305e3d2008-04-29 01:01:10 -07001970/*
1971 * Once we get an ACPI failure, we don't try any more, because we go
1972 * through the tables sequentially. Once we don't find a table, there
1973 * are no more.
1974 */
Randy Dunlap0c8204b2006-12-10 02:19:06 -08001975static int acpi_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977/* For GPE-type interrupts. */
Lin Ming8b6cd8a2010-12-13 13:38:46 +08001978static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
1979 u32 gpe_number, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct smi_info *smi_info = context;
1982 unsigned long flags;
1983#ifdef DEBUG_TIMING
1984 struct timeval t;
1985#endif
1986
1987 spin_lock_irqsave(&(smi_info->si_lock), flags);
1988
Corey Minyard64959e22008-04-29 01:01:07 -07001989 smi_inc_stat(smi_info, interrupts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991#ifdef DEBUG_TIMING
1992 do_gettimeofday(&t);
1993 printk("**ACPI_GPE: %d.%9.9d\n", t.tv_sec, t.tv_usec);
1994#endif
1995 smi_event_handler(smi_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1997
1998 return ACPI_INTERRUPT_HANDLED;
1999}
2000
Corey Minyardb0defcd2006-03-26 01:37:20 -08002001static void acpi_gpe_irq_cleanup(struct smi_info *info)
2002{
2003 if (!info->irq)
2004 return;
2005
2006 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
2007}
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009static int acpi_gpe_irq_setup(struct smi_info *info)
2010{
2011 acpi_status status;
2012
Corey Minyardb0defcd2006-03-26 01:37:20 -08002013 if (!info->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015
2016 /* FIXME - is level triggered right? */
2017 status = acpi_install_gpe_handler(NULL,
2018 info->irq,
2019 ACPI_GPE_LEVEL_TRIGGERED,
2020 &ipmi_acpi_gpe,
2021 info);
2022 if (status != AE_OK) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002023 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
2024 " running polled\n", DEVICE_NAME, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 info->irq = 0;
2026 return -EINVAL;
2027 } else {
Corey Minyardb0defcd2006-03-26 01:37:20 -08002028 info->irq_cleanup = acpi_gpe_irq_cleanup;
Myron Stowe279fbd02010-05-26 14:43:52 -07002029 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031 }
2032}
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
2035 * Defined at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02002036 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038struct SPMITable {
2039 s8 Signature[4];
2040 u32 Length;
2041 u8 Revision;
2042 u8 Checksum;
2043 s8 OEMID[6];
2044 s8 OEMTableID[8];
2045 s8 OEMRevision[4];
2046 s8 CreatorID[4];
2047 s8 CreatorRevision[4];
2048 u8 InterfaceType;
2049 u8 IPMIlegacy;
2050 s16 SpecificationRevision;
2051
2052 /*
2053 * Bit 0 - SCI interrupt supported
2054 * Bit 1 - I/O APIC/SAPIC
2055 */
2056 u8 InterruptType;
2057
Corey Minyardc305e3d2008-04-29 01:01:10 -07002058 /*
2059 * If bit 0 of InterruptType is set, then this is the SCI
2060 * interrupt in the GPEx_STS register.
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 u8 GPE;
2063
2064 s16 Reserved;
2065
Corey Minyardc305e3d2008-04-29 01:01:10 -07002066 /*
2067 * If bit 1 of InterruptType is set, then this is the I/O
2068 * APIC/SAPIC interrupt.
2069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 u32 GlobalSystemInterrupt;
2071
2072 /* The actual register address. */
2073 struct acpi_generic_address addr;
2074
2075 u8 UID[4];
2076
2077 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2078};
2079
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002080static int try_init_spmi(struct SPMITable *spmi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct smi_info *info;
Corey Minyardd02b3702014-01-24 14:00:53 -06002083 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (spmi->IPMIlegacy != 1) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002086 printk(KERN_INFO PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2087 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07002090 info = smi_info_alloc();
Corey Minyardb0defcd2006-03-26 01:37:20 -08002091 if (!info) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002092 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
Corey Minyardb0defcd2006-03-26 01:37:20 -08002093 return -ENOMEM;
2094 }
2095
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07002096 info->addr_source = SI_SPMI;
Myron Stowe279fbd02010-05-26 14:43:52 -07002097 printk(KERN_INFO PFX "probing via SPMI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Figure out the interface type. */
Corey Minyardc305e3d2008-04-29 01:01:10 -07002100 switch (spmi->InterfaceType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 case 1: /* KCS */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002102 info->si_type = SI_KCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 case 2: /* SMIC */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002105 info->si_type = SI_SMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 case 3: /* BT */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002108 info->si_type = SI_BT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 default:
Myron Stowe279fbd02010-05-26 14:43:52 -07002111 printk(KERN_INFO PFX "Unknown ACPI/SPMI SI type %d\n",
2112 spmi->InterfaceType);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002113 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return -EIO;
2115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (spmi->InterruptType & 1) {
2118 /* We've got a GPE interrupt. */
2119 info->irq = spmi->GPE;
2120 info->irq_setup = acpi_gpe_irq_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 } else if (spmi->InterruptType & 2) {
2122 /* We've got an APIC/SAPIC interrupt. */
2123 info->irq = spmi->GlobalSystemInterrupt;
2124 info->irq_setup = std_irq_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 } else {
2126 /* Use the default interrupt setting. */
2127 info->irq = 0;
2128 info->irq_setup = NULL;
2129 }
2130
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002131 if (spmi->addr.bit_width) {
Corey Minyard35bc37a2005-05-01 08:59:10 -07002132 /* A (hopefully) properly formed register bit width. */
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002133 info->io.regspacing = spmi->addr.bit_width / 8;
Corey Minyard35bc37a2005-05-01 08:59:10 -07002134 } else {
Corey Minyard35bc37a2005-05-01 08:59:10 -07002135 info->io.regspacing = DEFAULT_REGSPACING;
2136 }
Corey Minyardb0defcd2006-03-26 01:37:20 -08002137 info->io.regsize = info->io.regspacing;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002138 info->io.regshift = spmi->addr.bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002140 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 info->io_setup = mem_setup;
Corey Minyard8fe14252007-05-12 10:36:58 -07002142 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002143 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 info->io_setup = port_setup;
Corey Minyard8fe14252007-05-12 10:36:58 -07002145 info->io.addr_type = IPMI_IO_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 } else {
2147 kfree(info);
Myron Stowe279fbd02010-05-26 14:43:52 -07002148 printk(KERN_WARNING PFX "Unknown ACPI I/O Address type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return -EIO;
2150 }
Corey Minyardb0defcd2006-03-26 01:37:20 -08002151 info->io.addr_data = spmi->addr.address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Yinghai Lu7bb671e2010-08-10 18:03:10 -07002153 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2154 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2155 info->io.addr_data, info->io.regsize, info->io.regspacing,
2156 info->irq);
2157
Corey Minyardd02b3702014-01-24 14:00:53 -06002158 rv = add_smi(info);
2159 if (rv)
Yinghai Lu7faefea2010-08-10 18:03:10 -07002160 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Corey Minyardd02b3702014-01-24 14:00:53 -06002162 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
Corey Minyardb0defcd2006-03-26 01:37:20 -08002164
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002165static void spmi_find_bmc(void)
Corey Minyardb0defcd2006-03-26 01:37:20 -08002166{
2167 acpi_status status;
2168 struct SPMITable *spmi;
2169 int i;
2170
2171 if (acpi_disabled)
2172 return;
2173
2174 if (acpi_failure)
2175 return;
2176
2177 for (i = 0; ; i++) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +03002178 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2179 (struct acpi_table_header **)&spmi);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002180 if (status != AE_OK)
2181 return;
2182
Bjorn Helgaas18a3e0b2009-11-17 17:05:29 -07002183 try_init_spmi(spmi);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002184 }
2185}
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002186
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002187static int ipmi_pnp_probe(struct pnp_dev *dev,
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002188 const struct pnp_device_id *dev_id)
2189{
2190 struct acpi_device *acpi_dev;
2191 struct smi_info *info;
Yinghai Lua9e31762010-09-22 13:04:53 -07002192 struct resource *res, *res_second;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002193 acpi_handle handle;
2194 acpi_status status;
2195 unsigned long long tmp;
Corey Minyardd02b3702014-01-24 14:00:53 -06002196 int rv;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002197
2198 acpi_dev = pnp_acpi_device(dev);
2199 if (!acpi_dev)
2200 return -ENODEV;
2201
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07002202 info = smi_info_alloc();
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002203 if (!info)
2204 return -ENOMEM;
2205
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07002206 info->addr_source = SI_ACPI;
Myron Stowe279fbd02010-05-26 14:43:52 -07002207 printk(KERN_INFO PFX "probing via ACPI\n");
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002208
2209 handle = acpi_dev->handle;
Zhao Yakui16f42322010-12-08 10:10:16 +08002210 info->addr_info.acpi_info.acpi_handle = handle;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002211
2212 /* _IFT tells us the interface type: KCS, BT, etc */
2213 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2214 if (ACPI_FAILURE(status))
2215 goto err_free;
2216
2217 switch (tmp) {
2218 case 1:
2219 info->si_type = SI_KCS;
2220 break;
2221 case 2:
2222 info->si_type = SI_SMIC;
2223 break;
2224 case 3:
2225 info->si_type = SI_BT;
2226 break;
2227 default:
Myron Stowe279fbd02010-05-26 14:43:52 -07002228 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002229 goto err_free;
2230 }
2231
Myron Stowe279fbd02010-05-26 14:43:52 -07002232 res = pnp_get_resource(dev, IORESOURCE_IO, 0);
2233 if (res) {
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002234 info->io_setup = port_setup;
2235 info->io.addr_type = IPMI_IO_ADDR_SPACE;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002236 } else {
Myron Stowe279fbd02010-05-26 14:43:52 -07002237 res = pnp_get_resource(dev, IORESOURCE_MEM, 0);
2238 if (res) {
2239 info->io_setup = mem_setup;
2240 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2241 }
2242 }
2243 if (!res) {
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002244 dev_err(&dev->dev, "no I/O or memory address\n");
2245 goto err_free;
2246 }
Myron Stowe279fbd02010-05-26 14:43:52 -07002247 info->io.addr_data = res->start;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002248
2249 info->io.regspacing = DEFAULT_REGSPACING;
Yinghai Lua9e31762010-09-22 13:04:53 -07002250 res_second = pnp_get_resource(dev,
Yinghai Lud9e1b6c2010-08-09 17:18:22 -07002251 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2252 IORESOURCE_IO : IORESOURCE_MEM,
2253 1);
Yinghai Lua9e31762010-09-22 13:04:53 -07002254 if (res_second) {
2255 if (res_second->start > info->io.addr_data)
2256 info->io.regspacing = res_second->start - info->io.addr_data;
Yinghai Lud9e1b6c2010-08-09 17:18:22 -07002257 }
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002258 info->io.regsize = DEFAULT_REGSPACING;
2259 info->io.regshift = 0;
2260
2261 /* If _GPE exists, use it; otherwise use standard interrupts */
2262 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2263 if (ACPI_SUCCESS(status)) {
2264 info->irq = tmp;
2265 info->irq_setup = acpi_gpe_irq_setup;
2266 } else if (pnp_irq_valid(dev, 0)) {
2267 info->irq = pnp_irq(dev, 0);
2268 info->irq_setup = std_irq_setup;
2269 }
2270
Myron Stowe8c8eae22010-05-26 14:43:51 -07002271 info->dev = &dev->dev;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002272 pnp_set_drvdata(dev, info);
2273
Myron Stowe279fbd02010-05-26 14:43:52 -07002274 dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2275 res, info->io.regsize, info->io.regspacing,
2276 info->irq);
2277
Corey Minyardd02b3702014-01-24 14:00:53 -06002278 rv = add_smi(info);
2279 if (rv)
2280 kfree(info);
Yinghai Lu7faefea2010-08-10 18:03:10 -07002281
Corey Minyardd02b3702014-01-24 14:00:53 -06002282 return rv;
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002283
2284err_free:
2285 kfree(info);
2286 return -EINVAL;
2287}
2288
Bill Pemberton39af33f2012-11-19 13:26:26 -05002289static void ipmi_pnp_remove(struct pnp_dev *dev)
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002290{
2291 struct smi_info *info = pnp_get_drvdata(dev);
2292
2293 cleanup_one_si(info);
2294}
2295
2296static const struct pnp_device_id pnp_dev_table[] = {
2297 {"IPI0001", 0},
2298 {"", 0},
2299};
2300
2301static struct pnp_driver ipmi_pnp_driver = {
2302 .name = DEVICE_NAME,
2303 .probe = ipmi_pnp_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -08002304 .remove = ipmi_pnp_remove,
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07002305 .id_table = pnp_dev_table,
2306};
Jordan_Hargrave@Dell.coma798e2d2013-09-05 06:36:35 -05002307
2308MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309#endif
2310
Matt Domscha9fad4c2006-01-11 12:17:44 -08002311#ifdef CONFIG_DMI
Corey Minyardc305e3d2008-04-29 01:01:10 -07002312struct dmi_ipmi_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 u8 type;
2314 u8 addr_space;
2315 unsigned long base_addr;
2316 u8 irq;
2317 u8 offset;
2318 u8 slave_addr;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002319};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002321static int decode_dmi(const struct dmi_header *dm,
Corey Minyardb0defcd2006-03-26 01:37:20 -08002322 struct dmi_ipmi_data *dmi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Jeff Garzik18552562007-10-03 15:15:40 -04002324 const u8 *data = (const u8 *)dm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 unsigned long base_addr;
2326 u8 reg_spacing;
Andrey Paninb224cd32005-09-06 15:18:37 -07002327 u8 len = dm->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Corey Minyardb0defcd2006-03-26 01:37:20 -08002329 dmi->type = data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 memcpy(&base_addr, data+8, sizeof(unsigned long));
2332 if (len >= 0x11) {
2333 if (base_addr & 1) {
2334 /* I/O */
2335 base_addr &= 0xFFFE;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002336 dmi->addr_space = IPMI_IO_ADDR_SPACE;
Corey Minyardc305e3d2008-04-29 01:01:10 -07002337 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* Memory */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002339 dmi->addr_space = IPMI_MEM_ADDR_SPACE;
Corey Minyardc305e3d2008-04-29 01:01:10 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /* If bit 4 of byte 0x10 is set, then the lsb for the address
2342 is odd. */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002343 dmi->base_addr = base_addr | ((data[0x10] & 0x10) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Corey Minyardb0defcd2006-03-26 01:37:20 -08002345 dmi->irq = data[0x11];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 /* The top two bits of byte 0x10 hold the register spacing. */
Andrey Paninb224cd32005-09-06 15:18:37 -07002348 reg_spacing = (data[0x10] & 0xC0) >> 6;
Corey Minyardc305e3d2008-04-29 01:01:10 -07002349 switch (reg_spacing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 case 0x00: /* Byte boundaries */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002351 dmi->offset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 case 0x01: /* 32-bit boundaries */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002354 dmi->offset = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 break;
2356 case 0x02: /* 16-byte boundaries */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002357 dmi->offset = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 break;
2359 default:
2360 /* Some other interface, just ignore it. */
2361 return -EIO;
2362 }
2363 } else {
2364 /* Old DMI spec. */
Corey Minyardc305e3d2008-04-29 01:01:10 -07002365 /*
2366 * Note that technically, the lower bit of the base
Corey Minyard92068802005-05-01 08:59:10 -07002367 * address should be 1 if the address is I/O and 0 if
2368 * the address is in memory. So many systems get that
2369 * wrong (and all that I have seen are I/O) so we just
2370 * ignore that bit and assume I/O. Systems that use
Corey Minyardc305e3d2008-04-29 01:01:10 -07002371 * memory should use the newer spec, anyway.
2372 */
Corey Minyardb0defcd2006-03-26 01:37:20 -08002373 dmi->base_addr = base_addr & 0xfffe;
2374 dmi->addr_space = IPMI_IO_ADDR_SPACE;
2375 dmi->offset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377
Corey Minyardb0defcd2006-03-26 01:37:20 -08002378 dmi->slave_addr = data[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Corey Minyardb0defcd2006-03-26 01:37:20 -08002380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002383static void try_init_dmi(struct dmi_ipmi_data *ipmi_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Corey Minyarde8b33612005-09-06 15:18:45 -07002385 struct smi_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07002387 info = smi_info_alloc();
Corey Minyardb0defcd2006-03-26 01:37:20 -08002388 if (!info) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002389 printk(KERN_ERR PFX "Could not allocate SI data\n");
Corey Minyardb0defcd2006-03-26 01:37:20 -08002390 return;
2391 }
2392
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07002393 info->addr_source = SI_SMBIOS;
Myron Stowe279fbd02010-05-26 14:43:52 -07002394 printk(KERN_INFO PFX "probing via SMBIOS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Corey Minyarde8b33612005-09-06 15:18:45 -07002396 switch (ipmi_data->type) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08002397 case 0x01: /* KCS */
2398 info->si_type = SI_KCS;
2399 break;
2400 case 0x02: /* SMIC */
2401 info->si_type = SI_SMIC;
2402 break;
2403 case 0x03: /* BT */
2404 info->si_type = SI_BT;
2405 break;
2406 default:
Jesper Juhl80cd6922007-07-31 00:39:05 -07002407 kfree(info);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002408 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410
Corey Minyardb0defcd2006-03-26 01:37:20 -08002411 switch (ipmi_data->addr_space) {
2412 case IPMI_MEM_ADDR_SPACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 info->io_setup = mem_setup;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002414 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Corey Minyardb0defcd2006-03-26 01:37:20 -08002417 case IPMI_IO_ADDR_SPACE:
2418 info->io_setup = port_setup;
2419 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2420 break;
2421
2422 default:
2423 kfree(info);
Myron Stowe279fbd02010-05-26 14:43:52 -07002424 printk(KERN_WARNING PFX "Unknown SMBIOS I/O Address type: %d\n",
Corey Minyardb0defcd2006-03-26 01:37:20 -08002425 ipmi_data->addr_space);
2426 return;
2427 }
2428 info->io.addr_data = ipmi_data->base_addr;
2429
2430 info->io.regspacing = ipmi_data->offset;
2431 if (!info->io.regspacing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 info->io.regspacing = DEFAULT_REGSPACING;
2433 info->io.regsize = DEFAULT_REGSPACING;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002434 info->io.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 info->slave_addr = ipmi_data->slave_addr;
2437
Corey Minyardb0defcd2006-03-26 01:37:20 -08002438 info->irq = ipmi_data->irq;
2439 if (info->irq)
2440 info->irq_setup = std_irq_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Yinghai Lu7bb671e2010-08-10 18:03:10 -07002442 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2443 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2444 info->io.addr_data, info->io.regsize, info->io.regspacing,
2445 info->irq);
2446
Yinghai Lu7faefea2010-08-10 18:03:10 -07002447 if (add_smi(info))
2448 kfree(info);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002451static void dmi_find_bmc(void)
Corey Minyardb0defcd2006-03-26 01:37:20 -08002452{
Jeff Garzik18552562007-10-03 15:15:40 -04002453 const struct dmi_device *dev = NULL;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002454 struct dmi_ipmi_data data;
2455 int rv;
2456
2457 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev))) {
Jeff Garzik397f4eb2006-10-03 01:13:52 -07002458 memset(&data, 0, sizeof(data));
Jeff Garzik18552562007-10-03 15:15:40 -04002459 rv = decode_dmi((const struct dmi_header *) dev->device_data,
2460 &data);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002461 if (!rv)
2462 try_init_dmi(&data);
2463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
Matt Domscha9fad4c2006-01-11 12:17:44 -08002465#endif /* CONFIG_DMI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467#ifdef CONFIG_PCI
2468
Corey Minyardb0defcd2006-03-26 01:37:20 -08002469#define PCI_ERMC_CLASSCODE 0x0C0700
2470#define PCI_ERMC_CLASSCODE_MASK 0xffffff00
2471#define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
2472#define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
2473#define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
2474#define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476#define PCI_HP_VENDOR_ID 0x103C
2477#define PCI_MMC_DEVICE_ID 0x121A
2478#define PCI_MMC_ADDR_CW 0x10
2479
Corey Minyardb0defcd2006-03-26 01:37:20 -08002480static void ipmi_pci_cleanup(struct smi_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Corey Minyardb0defcd2006-03-26 01:37:20 -08002482 struct pci_dev *pdev = info->addr_source_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Corey Minyardb0defcd2006-03-26 01:37:20 -08002484 pci_disable_device(pdev);
2485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002487static int ipmi_pci_probe_regspacing(struct smi_info *info)
Corey Minyarda6c16c22012-10-16 15:53:40 -05002488{
2489 if (info->si_type == SI_KCS) {
2490 unsigned char status;
2491 int regspacing;
2492
2493 info->io.regsize = DEFAULT_REGSIZE;
2494 info->io.regshift = 0;
2495 info->io_size = 2;
2496 info->handlers = &kcs_smi_handlers;
2497
2498 /* detect 1, 4, 16byte spacing */
2499 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
2500 info->io.regspacing = regspacing;
2501 if (info->io_setup(info)) {
2502 dev_err(info->dev,
2503 "Could not setup I/O space\n");
2504 return DEFAULT_REGSPACING;
2505 }
2506 /* write invalid cmd */
2507 info->io.outputb(&info->io, 1, 0x10);
2508 /* read status back */
2509 status = info->io.inputb(&info->io, 1);
2510 info->io_cleanup(info);
2511 if (status)
2512 return regspacing;
2513 regspacing *= 4;
2514 }
2515 }
2516 return DEFAULT_REGSPACING;
2517}
2518
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002519static int ipmi_pci_probe(struct pci_dev *pdev,
Corey Minyardb0defcd2006-03-26 01:37:20 -08002520 const struct pci_device_id *ent)
2521{
2522 int rv;
2523 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2524 struct smi_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07002526 info = smi_info_alloc();
Corey Minyardb0defcd2006-03-26 01:37:20 -08002527 if (!info)
Dave Jones1cd441f2006-10-19 23:29:09 -07002528 return -ENOMEM;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002529
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07002530 info->addr_source = SI_PCI;
Myron Stowe279fbd02010-05-26 14:43:52 -07002531 dev_info(&pdev->dev, "probing via PCI");
Corey Minyardb0defcd2006-03-26 01:37:20 -08002532
2533 switch (class_type) {
2534 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2535 info->si_type = SI_SMIC;
2536 break;
2537
2538 case PCI_ERMC_CLASSCODE_TYPE_KCS:
2539 info->si_type = SI_KCS;
2540 break;
2541
2542 case PCI_ERMC_CLASSCODE_TYPE_BT:
2543 info->si_type = SI_BT;
2544 break;
2545
2546 default:
2547 kfree(info);
Myron Stowe279fbd02010-05-26 14:43:52 -07002548 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
Dave Jones1cd441f2006-10-19 23:29:09 -07002549 return -ENOMEM;
Corey Minyarde8b33612005-09-06 15:18:45 -07002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Corey Minyardb0defcd2006-03-26 01:37:20 -08002552 rv = pci_enable_device(pdev);
2553 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002554 dev_err(&pdev->dev, "couldn't enable PCI device\n");
Corey Minyardb0defcd2006-03-26 01:37:20 -08002555 kfree(info);
2556 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558
Corey Minyardb0defcd2006-03-26 01:37:20 -08002559 info->addr_source_cleanup = ipmi_pci_cleanup;
2560 info->addr_source_data = pdev;
2561
Corey Minyardb0defcd2006-03-26 01:37:20 -08002562 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2563 info->io_setup = port_setup;
2564 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2565 } else {
2566 info->io_setup = mem_setup;
2567 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
Corey Minyardb0defcd2006-03-26 01:37:20 -08002569 info->io.addr_data = pci_resource_start(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Corey Minyarda6c16c22012-10-16 15:53:40 -05002571 info->io.regspacing = ipmi_pci_probe_regspacing(info);
2572 info->io.regsize = DEFAULT_REGSIZE;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002573 info->io.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Corey Minyardb0defcd2006-03-26 01:37:20 -08002575 info->irq = pdev->irq;
2576 if (info->irq)
2577 info->irq_setup = std_irq_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Corey Minyard50c812b2006-03-26 01:37:21 -08002579 info->dev = &pdev->dev;
Corey Minyardfca3b742007-05-08 00:23:59 -07002580 pci_set_drvdata(pdev, info);
Corey Minyard50c812b2006-03-26 01:37:21 -08002581
Myron Stowe279fbd02010-05-26 14:43:52 -07002582 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2583 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2584 info->irq);
2585
Corey Minyardd02b3702014-01-24 14:00:53 -06002586 rv = add_smi(info);
2587 if (rv) {
Yinghai Lu7faefea2010-08-10 18:03:10 -07002588 kfree(info);
Corey Minyardd02b3702014-01-24 14:00:53 -06002589 pci_disable_device(pdev);
2590 }
Yinghai Lu7faefea2010-08-10 18:03:10 -07002591
Corey Minyardd02b3702014-01-24 14:00:53 -06002592 return rv;
Corey Minyardb0defcd2006-03-26 01:37:20 -08002593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Bill Pemberton39af33f2012-11-19 13:26:26 -05002595static void ipmi_pci_remove(struct pci_dev *pdev)
Corey Minyardb0defcd2006-03-26 01:37:20 -08002596{
Corey Minyardfca3b742007-05-08 00:23:59 -07002597 struct smi_info *info = pci_get_drvdata(pdev);
2598 cleanup_one_si(info);
Corey Minyardd02b3702014-01-24 14:00:53 -06002599 pci_disable_device(pdev);
Corey Minyardb0defcd2006-03-26 01:37:20 -08002600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Corey Minyardb0defcd2006-03-26 01:37:20 -08002602static struct pci_device_id ipmi_pci_devices[] = {
2603 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
Kees Cook248bdd52007-09-18 22:46:32 -07002604 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2605 { 0, }
Corey Minyardb0defcd2006-03-26 01:37:20 -08002606};
2607MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2608
2609static struct pci_driver ipmi_pci_driver = {
Corey Minyardc305e3d2008-04-29 01:01:10 -07002610 .name = DEVICE_NAME,
2611 .id_table = ipmi_pci_devices,
2612 .probe = ipmi_pci_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -08002613 .remove = ipmi_pci_remove,
Corey Minyardb0defcd2006-03-26 01:37:20 -08002614};
2615#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Grant Likelyb1608d62011-05-18 11:19:24 -06002617static struct of_device_id ipmi_match[];
Bill Pemberton2223cbe2012-11-19 13:22:51 -05002618static int ipmi_probe(struct platform_device *dev)
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002619{
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002620#ifdef CONFIG_OF
Grant Likelyb1608d62011-05-18 11:19:24 -06002621 const struct of_device_id *match;
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002622 struct smi_info *info;
2623 struct resource resource;
Rob Herringda81c3b2010-11-16 14:33:50 -06002624 const __be32 *regsize, *regspacing, *regshift;
Grant Likely61c7a082010-04-13 16:12:29 -07002625 struct device_node *np = dev->dev.of_node;
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002626 int ret;
2627 int proplen;
2628
Myron Stowe279fbd02010-05-26 14:43:52 -07002629 dev_info(&dev->dev, "probing via device tree\n");
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002630
Grant Likelyb1608d62011-05-18 11:19:24 -06002631 match = of_match_device(ipmi_match, &dev->dev);
2632 if (!match)
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002633 return -EINVAL;
2634
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002635 ret = of_address_to_resource(np, 0, &resource);
2636 if (ret) {
2637 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2638 return ret;
2639 }
2640
Stephen Rothwell9c250992007-08-15 16:42:12 +10002641 regsize = of_get_property(np, "reg-size", &proplen);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002642 if (regsize && proplen != 4) {
2643 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2644 return -EINVAL;
2645 }
2646
Stephen Rothwell9c250992007-08-15 16:42:12 +10002647 regspacing = of_get_property(np, "reg-spacing", &proplen);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002648 if (regspacing && proplen != 4) {
2649 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2650 return -EINVAL;
2651 }
2652
Stephen Rothwell9c250992007-08-15 16:42:12 +10002653 regshift = of_get_property(np, "reg-shift", &proplen);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002654 if (regshift && proplen != 4) {
2655 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2656 return -EINVAL;
2657 }
2658
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07002659 info = smi_info_alloc();
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002660
2661 if (!info) {
2662 dev_err(&dev->dev,
Myron Stowe279fbd02010-05-26 14:43:52 -07002663 "could not allocate memory for OF probe\n");
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002664 return -ENOMEM;
2665 }
2666
Grant Likelyb1608d62011-05-18 11:19:24 -06002667 info->si_type = (enum si_type) match->data;
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07002668 info->addr_source = SI_DEVICETREE;
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002669 info->irq_setup = std_irq_setup;
2670
Nate Case3b7ec112008-05-14 16:05:39 -07002671 if (resource.flags & IORESOURCE_IO) {
2672 info->io_setup = port_setup;
2673 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2674 } else {
2675 info->io_setup = mem_setup;
2676 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2677 }
2678
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002679 info->io.addr_data = resource.start;
2680
Rob Herringda81c3b2010-11-16 14:33:50 -06002681 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2682 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2683 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002684
Grant Likely61c7a082010-04-13 16:12:29 -07002685 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002686 info->dev = &dev->dev;
2687
Myron Stowe279fbd02010-05-26 14:43:52 -07002688 dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002689 info->io.addr_data, info->io.regsize, info->io.regspacing,
2690 info->irq);
2691
Greg Kroah-Hartman9de33df2009-05-04 12:40:54 -07002692 dev_set_drvdata(&dev->dev, info);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002693
Corey Minyardd02b3702014-01-24 14:00:53 -06002694 ret = add_smi(info);
2695 if (ret) {
Yinghai Lu7faefea2010-08-10 18:03:10 -07002696 kfree(info);
Corey Minyardd02b3702014-01-24 14:00:53 -06002697 return ret;
Yinghai Lu7faefea2010-08-10 18:03:10 -07002698 }
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002699#endif
Yinghai Lu7faefea2010-08-10 18:03:10 -07002700 return 0;
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002701}
2702
Bill Pemberton39af33f2012-11-19 13:26:26 -05002703static int ipmi_remove(struct platform_device *dev)
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002704{
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002705#ifdef CONFIG_OF
Greg Kroah-Hartman9de33df2009-05-04 12:40:54 -07002706 cleanup_one_si(dev_get_drvdata(&dev->dev));
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002707#endif
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002708 return 0;
2709}
2710
2711static struct of_device_id ipmi_match[] =
2712{
Corey Minyardc305e3d2008-04-29 01:01:10 -07002713 { .type = "ipmi", .compatible = "ipmi-kcs",
2714 .data = (void *)(unsigned long) SI_KCS },
2715 { .type = "ipmi", .compatible = "ipmi-smic",
2716 .data = (void *)(unsigned long) SI_SMIC },
2717 { .type = "ipmi", .compatible = "ipmi-bt",
2718 .data = (void *)(unsigned long) SI_BT },
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002719 {},
2720};
2721
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002722static struct platform_driver ipmi_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002723 .driver = {
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002724 .name = DEVICE_NAME,
Grant Likely40182942010-04-13 16:13:02 -07002725 .owner = THIS_MODULE,
2726 .of_match_table = ipmi_match,
2727 },
Rob Herringa1e9c9d2011-02-23 15:37:59 -06002728 .probe = ipmi_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -08002729 .remove = ipmi_remove,
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002730};
Corey Minyarddba9b4f2007-05-08 00:23:51 -07002731
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05002732#ifdef CONFIG_PARISC
2733static int ipmi_parisc_probe(struct parisc_device *dev)
2734{
2735 struct smi_info *info;
Geert Uytterhoevendfa19422014-01-28 20:12:35 +01002736 int rv;
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05002737
2738 info = smi_info_alloc();
2739
2740 if (!info) {
2741 dev_err(&dev->dev,
2742 "could not allocate memory for PARISC probe\n");
2743 return -ENOMEM;
2744 }
2745
2746 info->si_type = SI_KCS;
2747 info->addr_source = SI_DEVICETREE;
2748 info->io_setup = mem_setup;
2749 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2750 info->io.addr_data = dev->hpa.start;
2751 info->io.regsize = 1;
2752 info->io.regspacing = 1;
2753 info->io.regshift = 0;
2754 info->irq = 0; /* no interrupt */
2755 info->irq_setup = NULL;
2756 info->dev = &dev->dev;
2757
2758 dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
2759
2760 dev_set_drvdata(&dev->dev, info);
2761
Corey Minyardd02b3702014-01-24 14:00:53 -06002762 rv = add_smi(info);
2763 if (rv) {
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05002764 kfree(info);
Corey Minyardd02b3702014-01-24 14:00:53 -06002765 return rv;
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05002766 }
2767
2768 return 0;
2769}
2770
2771static int ipmi_parisc_remove(struct parisc_device *dev)
2772{
2773 cleanup_one_si(dev_get_drvdata(&dev->dev));
2774 return 0;
2775}
2776
2777static struct parisc_device_id ipmi_parisc_tbl[] = {
2778 { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
2779 { 0, }
2780};
2781
2782static struct parisc_driver ipmi_parisc_driver = {
2783 .name = "ipmi",
2784 .id_table = ipmi_parisc_tbl,
2785 .probe = ipmi_parisc_probe,
2786 .remove = ipmi_parisc_remove,
2787};
2788#endif /* CONFIG_PARISC */
2789
Corey Minyard40112ae2009-04-21 12:24:03 -07002790static int wait_for_msg_done(struct smi_info *smi_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Corey Minyard50c812b2006-03-26 01:37:21 -08002792 enum si_sm_result smi_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 smi_result = smi_info->handlers->event(smi_info->si_sm, 0);
Corey Minyardc305e3d2008-04-29 01:01:10 -07002795 for (;;) {
Corey Minyardc3e7e792005-11-07 01:00:02 -08002796 if (smi_result == SI_SM_CALL_WITH_DELAY ||
2797 smi_result == SI_SM_CALL_WITH_TICK_DELAY) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002798 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 smi_result = smi_info->handlers->event(
Xie XiuQie21404d2014-01-24 14:00:52 -06002800 smi_info->si_sm, jiffies_to_usecs(1));
Corey Minyardc305e3d2008-04-29 01:01:10 -07002801 } else if (smi_result == SI_SM_CALL_WITHOUT_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 smi_result = smi_info->handlers->event(
2803 smi_info->si_sm, 0);
Corey Minyardc305e3d2008-04-29 01:01:10 -07002804 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 break;
2806 }
Corey Minyard40112ae2009-04-21 12:24:03 -07002807 if (smi_result == SI_SM_HOSED)
Corey Minyardc305e3d2008-04-29 01:01:10 -07002808 /*
2809 * We couldn't get the state machine to run, so whatever's at
2810 * the port is probably not an IPMI SMI interface.
2811 */
Corey Minyard40112ae2009-04-21 12:24:03 -07002812 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Corey Minyard40112ae2009-04-21 12:24:03 -07002814 return 0;
2815}
2816
2817static int try_get_dev_id(struct smi_info *smi_info)
2818{
2819 unsigned char msg[2];
2820 unsigned char *resp;
2821 unsigned long resp_len;
2822 int rv = 0;
2823
2824 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2825 if (!resp)
2826 return -ENOMEM;
2827
2828 /*
2829 * Do a Get Device ID command, since it comes back with some
2830 * useful info.
2831 */
2832 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2833 msg[1] = IPMI_GET_DEVICE_ID_CMD;
2834 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2835
2836 rv = wait_for_msg_done(smi_info);
2837 if (rv)
2838 goto out;
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2841 resp, IPMI_MAX_MSG_LENGTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Corey Minyardd8c98612007-10-18 03:07:11 -07002843 /* Check and record info from the get device id, in case we need it. */
2844 rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 out:
2847 kfree(resp);
2848 return rv;
2849}
2850
Corey Minyard40112ae2009-04-21 12:24:03 -07002851static int try_enable_event_buffer(struct smi_info *smi_info)
2852{
2853 unsigned char msg[3];
2854 unsigned char *resp;
2855 unsigned long resp_len;
2856 int rv = 0;
2857
2858 resp = kmalloc(IPMI_MAX_MSG_LENGTH, GFP_KERNEL);
2859 if (!resp)
2860 return -ENOMEM;
2861
2862 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2863 msg[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
2864 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 2);
2865
2866 rv = wait_for_msg_done(smi_info);
2867 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002868 printk(KERN_WARNING PFX "Error getting response from get"
2869 " global enables command, the event buffer is not"
Corey Minyard40112ae2009-04-21 12:24:03 -07002870 " enabled.\n");
2871 goto out;
2872 }
2873
2874 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2875 resp, IPMI_MAX_MSG_LENGTH);
2876
2877 if (resp_len < 4 ||
2878 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2879 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
2880 resp[2] != 0) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002881 printk(KERN_WARNING PFX "Invalid return from get global"
2882 " enables command, cannot enable the event buffer.\n");
Corey Minyard40112ae2009-04-21 12:24:03 -07002883 rv = -EINVAL;
2884 goto out;
2885 }
2886
2887 if (resp[3] & IPMI_BMC_EVT_MSG_BUFF)
2888 /* buffer is already enabled, nothing to do. */
2889 goto out;
2890
2891 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
2892 msg[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
2893 msg[2] = resp[3] | IPMI_BMC_EVT_MSG_BUFF;
2894 smi_info->handlers->start_transaction(smi_info->si_sm, msg, 3);
2895
2896 rv = wait_for_msg_done(smi_info);
2897 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002898 printk(KERN_WARNING PFX "Error getting response from set"
2899 " global, enables command, the event buffer is not"
Corey Minyard40112ae2009-04-21 12:24:03 -07002900 " enabled.\n");
2901 goto out;
2902 }
2903
2904 resp_len = smi_info->handlers->get_result(smi_info->si_sm,
2905 resp, IPMI_MAX_MSG_LENGTH);
2906
2907 if (resp_len < 3 ||
2908 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2909 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
Myron Stowe279fbd02010-05-26 14:43:52 -07002910 printk(KERN_WARNING PFX "Invalid return from get global,"
2911 "enables command, not enable the event buffer.\n");
Corey Minyard40112ae2009-04-21 12:24:03 -07002912 rv = -EINVAL;
2913 goto out;
2914 }
2915
2916 if (resp[2] != 0)
2917 /*
2918 * An error when setting the event buffer bit means
2919 * that the event buffer is not supported.
2920 */
2921 rv = -ENOENT;
2922 out:
2923 kfree(resp);
2924 return rv;
2925}
2926
Alexey Dobriyan07412732011-05-26 16:25:55 -07002927static int smi_type_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Alexey Dobriyan07412732011-05-26 16:25:55 -07002929 struct smi_info *smi = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Alexey Dobriyan07412732011-05-26 16:25:55 -07002931 return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Alexey Dobriyan07412732011-05-26 16:25:55 -07002934static int smi_type_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Al Virod9dda782013-03-31 18:16:14 -04002936 return single_open(file, smi_type_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Alexey Dobriyan07412732011-05-26 16:25:55 -07002939static const struct file_operations smi_type_proc_ops = {
2940 .open = smi_type_proc_open,
2941 .read = seq_read,
2942 .llseek = seq_lseek,
2943 .release = single_release,
2944};
2945
2946static int smi_si_stats_proc_show(struct seq_file *m, void *v)
2947{
2948 struct smi_info *smi = m->private;
2949
2950 seq_printf(m, "interrupts_enabled: %d\n",
Corey Minyardb0defcd2006-03-26 01:37:20 -08002951 smi->irq && !smi->interrupt_disabled);
Alexey Dobriyan07412732011-05-26 16:25:55 -07002952 seq_printf(m, "short_timeouts: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002953 smi_get_stat(smi, short_timeouts));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002954 seq_printf(m, "long_timeouts: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002955 smi_get_stat(smi, long_timeouts));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002956 seq_printf(m, "idles: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002957 smi_get_stat(smi, idles));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002958 seq_printf(m, "interrupts: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002959 smi_get_stat(smi, interrupts));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002960 seq_printf(m, "attentions: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002961 smi_get_stat(smi, attentions));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002962 seq_printf(m, "flag_fetches: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002963 smi_get_stat(smi, flag_fetches));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002964 seq_printf(m, "hosed_count: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002965 smi_get_stat(smi, hosed_count));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002966 seq_printf(m, "complete_transactions: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002967 smi_get_stat(smi, complete_transactions));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002968 seq_printf(m, "events: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002969 smi_get_stat(smi, events));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002970 seq_printf(m, "watchdog_pretimeouts: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002971 smi_get_stat(smi, watchdog_pretimeouts));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002972 seq_printf(m, "incoming_messages: %u\n",
Corey Minyard64959e22008-04-29 01:01:07 -07002973 smi_get_stat(smi, incoming_messages));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002974 return 0;
Corey Minyardb361e272006-12-06 20:41:07 -08002975}
2976
Alexey Dobriyan07412732011-05-26 16:25:55 -07002977static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
Corey Minyardb361e272006-12-06 20:41:07 -08002978{
Al Virod9dda782013-03-31 18:16:14 -04002979 return single_open(file, smi_si_stats_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07002980}
Corey Minyardb361e272006-12-06 20:41:07 -08002981
Alexey Dobriyan07412732011-05-26 16:25:55 -07002982static const struct file_operations smi_si_stats_proc_ops = {
2983 .open = smi_si_stats_proc_open,
2984 .read = seq_read,
2985 .llseek = seq_lseek,
2986 .release = single_release,
2987};
2988
2989static int smi_params_proc_show(struct seq_file *m, void *v)
2990{
2991 struct smi_info *smi = m->private;
2992
2993 return seq_printf(m,
Corey Minyardb361e272006-12-06 20:41:07 -08002994 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
2995 si_to_str[smi->si_type],
2996 addr_space_to_str[smi->io.addr_type],
2997 smi->io.addr_data,
2998 smi->io.regspacing,
2999 smi->io.regsize,
3000 smi->io.regshift,
3001 smi->irq,
3002 smi->slave_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Alexey Dobriyan07412732011-05-26 16:25:55 -07003005static int smi_params_proc_open(struct inode *inode, struct file *file)
3006{
Al Virod9dda782013-03-31 18:16:14 -04003007 return single_open(file, smi_params_proc_show, PDE_DATA(inode));
Alexey Dobriyan07412732011-05-26 16:25:55 -07003008}
3009
3010static const struct file_operations smi_params_proc_ops = {
3011 .open = smi_params_proc_open,
3012 .read = seq_read,
3013 .llseek = seq_lseek,
3014 .release = single_release,
3015};
3016
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003017/*
3018 * oem_data_avail_to_receive_msg_avail
3019 * @info - smi_info structure with msg_flags set
3020 *
3021 * Converts flags from OEM_DATA_AVAIL to RECEIVE_MSG_AVAIL
3022 * Returns 1 indicating need to re-run handle_flags().
3023 */
3024static int oem_data_avail_to_receive_msg_avail(struct smi_info *smi_info)
3025{
Corey Minyarde8b33612005-09-06 15:18:45 -07003026 smi_info->msg_flags = ((smi_info->msg_flags & ~OEM_DATA_AVAIL) |
Corey Minyardc305e3d2008-04-29 01:01:10 -07003027 RECEIVE_MSG_AVAIL);
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003028 return 1;
3029}
3030
3031/*
3032 * setup_dell_poweredge_oem_data_handler
3033 * @info - smi_info.device_id must be populated
3034 *
3035 * Systems that match, but have firmware version < 1.40 may assert
3036 * OEM0_DATA_AVAIL on their own, without being told via Set Flags that
3037 * it's safe to do so. Such systems will de-assert OEM1_DATA_AVAIL
3038 * upon receipt of IPMI_GET_MSG_CMD, so we should treat these flags
3039 * as RECEIVE_MSG_AVAIL instead.
3040 *
3041 * As Dell has no plans to release IPMI 1.5 firmware that *ever*
3042 * assert the OEM[012] bits, and if it did, the driver would have to
3043 * change to handle that properly, we don't actually check for the
3044 * firmware version.
3045 * Device ID = 0x20 BMC on PowerEdge 8G servers
3046 * Device Revision = 0x80
3047 * Firmware Revision1 = 0x01 BMC version 1.40
3048 * Firmware Revision2 = 0x40 BCD encoded
3049 * IPMI Version = 0x51 IPMI 1.5
3050 * Manufacturer ID = A2 02 00 Dell IANA
3051 *
Corey Minyardd5a2b892005-11-07 00:59:58 -08003052 * Additionally, PowerEdge systems with IPMI < 1.5 may also assert
3053 * OEM0_DATA_AVAIL and needs to be treated as RECEIVE_MSG_AVAIL.
3054 *
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003055 */
3056#define DELL_POWEREDGE_8G_BMC_DEVICE_ID 0x20
3057#define DELL_POWEREDGE_8G_BMC_DEVICE_REV 0x80
3058#define DELL_POWEREDGE_8G_BMC_IPMI_VERSION 0x51
Corey Minyard50c812b2006-03-26 01:37:21 -08003059#define DELL_IANA_MFR_ID 0x0002a2
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003060static void setup_dell_poweredge_oem_data_handler(struct smi_info *smi_info)
3061{
3062 struct ipmi_device_id *id = &smi_info->device_id;
Corey Minyard50c812b2006-03-26 01:37:21 -08003063 if (id->manufacturer_id == DELL_IANA_MFR_ID) {
Corey Minyardd5a2b892005-11-07 00:59:58 -08003064 if (id->device_id == DELL_POWEREDGE_8G_BMC_DEVICE_ID &&
3065 id->device_revision == DELL_POWEREDGE_8G_BMC_DEVICE_REV &&
Corey Minyard50c812b2006-03-26 01:37:21 -08003066 id->ipmi_version == DELL_POWEREDGE_8G_BMC_IPMI_VERSION) {
Corey Minyardd5a2b892005-11-07 00:59:58 -08003067 smi_info->oem_data_avail_handler =
3068 oem_data_avail_to_receive_msg_avail;
Corey Minyardc305e3d2008-04-29 01:01:10 -07003069 } else if (ipmi_version_major(id) < 1 ||
3070 (ipmi_version_major(id) == 1 &&
3071 ipmi_version_minor(id) < 5)) {
Corey Minyardd5a2b892005-11-07 00:59:58 -08003072 smi_info->oem_data_avail_handler =
3073 oem_data_avail_to_receive_msg_avail;
3074 }
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003075 }
3076}
3077
Corey Minyardea940272005-11-07 00:59:59 -08003078#define CANNOT_RETURN_REQUESTED_LENGTH 0xCA
3079static void return_hosed_msg_badsize(struct smi_info *smi_info)
3080{
3081 struct ipmi_smi_msg *msg = smi_info->curr_msg;
3082
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003083 /* Make it a response */
Corey Minyardea940272005-11-07 00:59:59 -08003084 msg->rsp[0] = msg->data[0] | 4;
3085 msg->rsp[1] = msg->data[1];
3086 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
3087 msg->rsp_size = 3;
3088 smi_info->curr_msg = NULL;
3089 deliver_recv_msg(smi_info, msg);
3090}
3091
3092/*
3093 * dell_poweredge_bt_xaction_handler
3094 * @info - smi_info.device_id must be populated
3095 *
3096 * Dell PowerEdge servers with the BT interface (x6xx and 1750) will
3097 * not respond to a Get SDR command if the length of the data
3098 * requested is exactly 0x3A, which leads to command timeouts and no
3099 * data returned. This intercepts such commands, and causes userspace
3100 * callers to try again with a different-sized buffer, which succeeds.
3101 */
3102
3103#define STORAGE_NETFN 0x0A
3104#define STORAGE_CMD_GET_SDR 0x23
3105static int dell_poweredge_bt_xaction_handler(struct notifier_block *self,
3106 unsigned long unused,
3107 void *in)
3108{
3109 struct smi_info *smi_info = in;
3110 unsigned char *data = smi_info->curr_msg->data;
3111 unsigned int size = smi_info->curr_msg->data_size;
3112 if (size >= 8 &&
3113 (data[0]>>2) == STORAGE_NETFN &&
3114 data[1] == STORAGE_CMD_GET_SDR &&
3115 data[7] == 0x3A) {
3116 return_hosed_msg_badsize(smi_info);
3117 return NOTIFY_STOP;
3118 }
3119 return NOTIFY_DONE;
3120}
3121
3122static struct notifier_block dell_poweredge_bt_xaction_notifier = {
3123 .notifier_call = dell_poweredge_bt_xaction_handler,
3124};
3125
3126/*
3127 * setup_dell_poweredge_bt_xaction_handler
3128 * @info - smi_info.device_id must be filled in already
3129 *
3130 * Fills in smi_info.device_id.start_transaction_pre_hook
3131 * when we know what function to use there.
3132 */
3133static void
3134setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3135{
3136 struct ipmi_device_id *id = &smi_info->device_id;
Corey Minyard50c812b2006-03-26 01:37:21 -08003137 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
Corey Minyardea940272005-11-07 00:59:59 -08003138 smi_info->si_type == SI_BT)
3139 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3140}
3141
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003142/*
3143 * setup_oem_data_handler
3144 * @info - smi_info.device_id must be filled in already
3145 *
3146 * Fills in smi_info.device_id.oem_data_available_handler
3147 * when we know what function to use there.
3148 */
3149
3150static void setup_oem_data_handler(struct smi_info *smi_info)
3151{
3152 setup_dell_poweredge_oem_data_handler(smi_info);
3153}
3154
Corey Minyardea940272005-11-07 00:59:59 -08003155static void setup_xaction_handlers(struct smi_info *smi_info)
3156{
3157 setup_dell_poweredge_bt_xaction_handler(smi_info);
3158}
3159
Corey Minyarda9a2c442005-11-07 01:00:03 -08003160static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3161{
Corey Minyard453823b2006-03-31 02:30:39 -08003162 if (smi_info->intf) {
Corey Minyardc305e3d2008-04-29 01:01:10 -07003163 /*
3164 * The timer and thread are only running if the
3165 * interface has been started up and registered.
3166 */
Corey Minyard453823b2006-03-31 02:30:39 -08003167 if (smi_info->thread != NULL)
3168 kthread_stop(smi_info->thread);
3169 del_timer_sync(&smi_info->si_timer);
3170 }
Corey Minyarda9a2c442005-11-07 01:00:03 -08003171}
3172
Bill Pemberton0bbed202012-11-19 13:24:36 -05003173static struct ipmi_default_vals
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Corey Minyardb0defcd2006-03-26 01:37:20 -08003175 int type;
3176 int port;
Randy Dunlap74208842006-04-18 22:21:52 -07003177} ipmi_defaults[] =
Corey Minyardb0defcd2006-03-26 01:37:20 -08003178{
3179 { .type = SI_KCS, .port = 0xca2 },
3180 { .type = SI_SMIC, .port = 0xca9 },
3181 { .type = SI_BT, .port = 0xe4 },
3182 { .port = 0 }
3183};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Bill Pemberton2223cbe2012-11-19 13:22:51 -05003185static void default_find_bmc(void)
Corey Minyardb0defcd2006-03-26 01:37:20 -08003186{
3187 struct smi_info *info;
3188 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Corey Minyardb0defcd2006-03-26 01:37:20 -08003190 for (i = 0; ; i++) {
3191 if (!ipmi_defaults[i].port)
3192 break;
Kumar Gala68e1ee62008-09-22 14:41:31 -07003193#ifdef CONFIG_PPC
Christian Krafft4ff31d72007-03-05 00:30:48 -08003194 if (check_legacy_ioport(ipmi_defaults[i].port))
3195 continue;
3196#endif
Eric Dumazetde5e2dd2010-10-26 14:21:17 -07003197 info = smi_info_alloc();
Andrew Mortona09f4852008-08-20 14:09:14 -07003198 if (!info)
3199 return;
Christian Krafft4ff31d72007-03-05 00:30:48 -08003200
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07003201 info->addr_source = SI_DEFAULT;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003202
3203 info->si_type = ipmi_defaults[i].type;
3204 info->io_setup = port_setup;
3205 info->io.addr_data = ipmi_defaults[i].port;
3206 info->io.addr_type = IPMI_IO_ADDR_SPACE;
3207
3208 info->io.addr = NULL;
3209 info->io.regspacing = DEFAULT_REGSPACING;
3210 info->io.regsize = DEFAULT_REGSPACING;
3211 info->io.regshift = 0;
3212
Matthew Garrett2407d772010-05-26 14:43:46 -07003213 if (add_smi(info) == 0) {
3214 if ((try_smi_init(info)) == 0) {
3215 /* Found one... */
Myron Stowe279fbd02010-05-26 14:43:52 -07003216 printk(KERN_INFO PFX "Found default %s"
Matthew Garrett2407d772010-05-26 14:43:46 -07003217 " state machine at %s address 0x%lx\n",
3218 si_to_str[info->si_type],
3219 addr_space_to_str[info->io.addr_type],
3220 info->io.addr_data);
3221 } else
3222 cleanup_one_si(info);
Yinghai Lu7faefea2010-08-10 18:03:10 -07003223 } else {
3224 kfree(info);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003225 }
3226 }
3227}
3228
3229static int is_new_interface(struct smi_info *info)
3230{
3231 struct smi_info *e;
3232
3233 list_for_each_entry(e, &smi_infos, link) {
3234 if (e->io.addr_type != info->io.addr_type)
3235 continue;
3236 if (e->io.addr_data == info->io.addr_data)
3237 return 0;
3238 }
3239
3240 return 1;
3241}
3242
Matthew Garrett2407d772010-05-26 14:43:46 -07003243static int add_smi(struct smi_info *new_smi)
3244{
3245 int rv = 0;
3246
Myron Stowe279fbd02010-05-26 14:43:52 -07003247 printk(KERN_INFO PFX "Adding %s-specified %s state machine",
Matthew Garrett2407d772010-05-26 14:43:46 -07003248 ipmi_addr_src_to_str[new_smi->addr_source],
3249 si_to_str[new_smi->si_type]);
3250 mutex_lock(&smi_infos_lock);
3251 if (!is_new_interface(new_smi)) {
Yinghai Lu7bb671e2010-08-10 18:03:10 -07003252 printk(KERN_CONT " duplicate interface\n");
Matthew Garrett2407d772010-05-26 14:43:46 -07003253 rv = -EBUSY;
3254 goto out_err;
3255 }
3256
3257 printk(KERN_CONT "\n");
3258
3259 /* So we know not to free it unless we have allocated one. */
3260 new_smi->intf = NULL;
3261 new_smi->si_sm = NULL;
3262 new_smi->handlers = NULL;
3263
3264 list_add_tail(&new_smi->link, &smi_infos);
3265
3266out_err:
3267 mutex_unlock(&smi_infos_lock);
3268 return rv;
3269}
3270
Corey Minyardb0defcd2006-03-26 01:37:20 -08003271static int try_smi_init(struct smi_info *new_smi)
3272{
Matthew Garrett2407d772010-05-26 14:43:46 -07003273 int rv = 0;
Corey Minyard64959e22008-04-29 01:01:07 -07003274 int i;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003275
Myron Stowe279fbd02010-05-26 14:43:52 -07003276 printk(KERN_INFO PFX "Trying %s-specified %s state"
Matthew Garrett5fedc4a2010-05-26 14:43:45 -07003277 " machine at %s address 0x%lx, slave address 0x%x,"
3278 " irq %d\n",
3279 ipmi_addr_src_to_str[new_smi->addr_source],
3280 si_to_str[new_smi->si_type],
3281 addr_space_to_str[new_smi->io.addr_type],
3282 new_smi->io.addr_data,
3283 new_smi->slave_addr, new_smi->irq);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003284
Corey Minyardb0defcd2006-03-26 01:37:20 -08003285 switch (new_smi->si_type) {
3286 case SI_KCS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 new_smi->handlers = &kcs_smi_handlers;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003288 break;
3289
3290 case SI_SMIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 new_smi->handlers = &smic_smi_handlers;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003292 break;
3293
3294 case SI_BT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 new_smi->handlers = &bt_smi_handlers;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003296 break;
3297
3298 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /* No support for anything else yet. */
3300 rv = -EIO;
3301 goto out_err;
3302 }
3303
3304 /* Allocate the state machine's data and initialize it. */
3305 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003306 if (!new_smi->si_sm) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003307 printk(KERN_ERR PFX
3308 "Could not allocate state machine memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 rv = -ENOMEM;
3310 goto out_err;
3311 }
3312 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm,
3313 &new_smi->io);
3314
3315 /* Now that we know the I/O size, we can set up the I/O. */
3316 rv = new_smi->io_setup(new_smi);
3317 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003318 printk(KERN_ERR PFX "Could not set up I/O space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 goto out_err;
3320 }
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /* Do low-level detection first. */
3323 if (new_smi->handlers->detect(new_smi->si_sm)) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08003324 if (new_smi->addr_source)
Myron Stowe279fbd02010-05-26 14:43:52 -07003325 printk(KERN_INFO PFX "Interface detection failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 rv = -ENODEV;
3327 goto out_err;
3328 }
3329
Corey Minyardc305e3d2008-04-29 01:01:10 -07003330 /*
3331 * Attempt a get device id command. If it fails, we probably
3332 * don't have a BMC here.
3333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 rv = try_get_dev_id(new_smi);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003335 if (rv) {
3336 if (new_smi->addr_source)
Myron Stowe279fbd02010-05-26 14:43:52 -07003337 printk(KERN_INFO PFX "There appears to be no BMC"
Corey Minyardb0defcd2006-03-26 01:37:20 -08003338 " at this location\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto out_err;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003342 setup_oem_data_handler(new_smi);
Corey Minyardea940272005-11-07 00:59:59 -08003343 setup_xaction_handlers(new_smi);
Corey Minyard3ae0e0f2005-09-06 15:18:41 -07003344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 INIT_LIST_HEAD(&(new_smi->xmit_msgs));
3346 INIT_LIST_HEAD(&(new_smi->hp_xmit_msgs));
3347 new_smi->curr_msg = NULL;
3348 atomic_set(&new_smi->req_events, 0);
3349 new_smi->run_to_completion = 0;
Corey Minyard64959e22008-04-29 01:01:07 -07003350 for (i = 0; i < SI_NUM_STATS; i++)
3351 atomic_set(&new_smi->stats[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Matthew Garrettea4078c2010-05-26 14:43:48 -07003353 new_smi->interrupt_disabled = 1;
Corey Minyarda9a2c442005-11-07 01:00:03 -08003354 atomic_set(&new_smi->stop_operation, 0);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003355 new_smi->intf_num = smi_num;
3356 smi_num++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Corey Minyard40112ae2009-04-21 12:24:03 -07003358 rv = try_enable_event_buffer(new_smi);
3359 if (rv == 0)
3360 new_smi->has_event_buffer = 1;
3361
Corey Minyardc305e3d2008-04-29 01:01:10 -07003362 /*
3363 * Start clearing the flags before we enable interrupts or the
3364 * timer to avoid racing with the timer.
3365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 start_clear_flags(new_smi);
3367 /* IRQ is defined to be set when non-zero. */
3368 if (new_smi->irq)
3369 new_smi->si_state = SI_CLEARING_FLAGS_THEN_SET_IRQ;
3370
Corey Minyard50c812b2006-03-26 01:37:21 -08003371 if (!new_smi->dev) {
Corey Minyardc305e3d2008-04-29 01:01:10 -07003372 /*
3373 * If we don't already have a device from something
3374 * else (like PCI), then register a new one.
3375 */
Corey Minyard50c812b2006-03-26 01:37:21 -08003376 new_smi->pdev = platform_device_alloc("ipmi_si",
3377 new_smi->intf_num);
Corey Minyard8b32b5d2009-04-21 12:24:02 -07003378 if (!new_smi->pdev) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003379 printk(KERN_ERR PFX
3380 "Unable to allocate platform device\n");
Corey Minyard453823b2006-03-31 02:30:39 -08003381 goto out_err;
Corey Minyard50c812b2006-03-26 01:37:21 -08003382 }
3383 new_smi->dev = &new_smi->pdev->dev;
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08003384 new_smi->dev->driver = &ipmi_driver.driver;
Corey Minyard50c812b2006-03-26 01:37:21 -08003385
Zhang, Yanminb48f5452006-11-16 01:19:08 -08003386 rv = platform_device_add(new_smi->pdev);
Corey Minyard50c812b2006-03-26 01:37:21 -08003387 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003388 printk(KERN_ERR PFX
3389 "Unable to register system interface device:"
Corey Minyard50c812b2006-03-26 01:37:21 -08003390 " %d\n",
3391 rv);
Corey Minyard453823b2006-03-31 02:30:39 -08003392 goto out_err;
Corey Minyard50c812b2006-03-26 01:37:21 -08003393 }
3394 new_smi->dev_registered = 1;
3395 }
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 rv = ipmi_register_smi(&handlers,
3398 new_smi,
Corey Minyard50c812b2006-03-26 01:37:21 -08003399 &new_smi->device_id,
3400 new_smi->dev,
Corey Minyard759643b2006-12-06 20:40:59 -08003401 "bmc",
Corey Minyard453823b2006-03-31 02:30:39 -08003402 new_smi->slave_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003404 dev_err(new_smi->dev, "Unable to register device: error %d\n",
3405 rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 goto out_err_stop_timer;
3407 }
3408
3409 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
Alexey Dobriyan07412732011-05-26 16:25:55 -07003410 &smi_type_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003411 new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003413 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 goto out_err_stop_timer;
3415 }
3416
3417 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
Alexey Dobriyan07412732011-05-26 16:25:55 -07003418 &smi_si_stats_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003419 new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003421 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 goto out_err_stop_timer;
3423 }
3424
Corey Minyardb361e272006-12-06 20:41:07 -08003425 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
Alexey Dobriyan07412732011-05-26 16:25:55 -07003426 &smi_params_proc_ops,
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003427 new_smi);
Corey Minyardb361e272006-12-06 20:41:07 -08003428 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003429 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
Corey Minyardb361e272006-12-06 20:41:07 -08003430 goto out_err_stop_timer;
3431 }
3432
Myron Stowe279fbd02010-05-26 14:43:52 -07003433 dev_info(new_smi->dev, "IPMI %s interface initialized\n",
3434 si_to_str[new_smi->si_type]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 return 0;
3437
3438 out_err_stop_timer:
Corey Minyarda9a2c442005-11-07 01:00:03 -08003439 atomic_inc(&new_smi->stop_operation);
3440 wait_for_timer_and_thread(new_smi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 out_err:
Matthew Garrett2407d772010-05-26 14:43:46 -07003443 new_smi->interrupt_disabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Matthew Garrett2407d772010-05-26 14:43:46 -07003445 if (new_smi->intf) {
3446 ipmi_unregister_smi(new_smi->intf);
3447 new_smi->intf = NULL;
3448 }
3449
3450 if (new_smi->irq_cleanup) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08003451 new_smi->irq_cleanup(new_smi);
Matthew Garrett2407d772010-05-26 14:43:46 -07003452 new_smi->irq_cleanup = NULL;
3453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Corey Minyardc305e3d2008-04-29 01:01:10 -07003455 /*
3456 * Wait until we know that we are out of any interrupt
3457 * handlers might have been running before we freed the
3458 * interrupt.
3459 */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003460 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 if (new_smi->si_sm) {
3463 if (new_smi->handlers)
3464 new_smi->handlers->cleanup(new_smi->si_sm);
3465 kfree(new_smi->si_sm);
Matthew Garrett2407d772010-05-26 14:43:46 -07003466 new_smi->si_sm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Matthew Garrett2407d772010-05-26 14:43:46 -07003468 if (new_smi->addr_source_cleanup) {
Corey Minyardb0defcd2006-03-26 01:37:20 -08003469 new_smi->addr_source_cleanup(new_smi);
Matthew Garrett2407d772010-05-26 14:43:46 -07003470 new_smi->addr_source_cleanup = NULL;
3471 }
3472 if (new_smi->io_cleanup) {
Paolo Galtieri7767e122005-12-15 12:34:28 -08003473 new_smi->io_cleanup(new_smi);
Matthew Garrett2407d772010-05-26 14:43:46 -07003474 new_smi->io_cleanup = NULL;
3475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Matthew Garrett2407d772010-05-26 14:43:46 -07003477 if (new_smi->dev_registered) {
Corey Minyard50c812b2006-03-26 01:37:21 -08003478 platform_device_unregister(new_smi->pdev);
Matthew Garrett2407d772010-05-26 14:43:46 -07003479 new_smi->dev_registered = 0;
3480 }
Corey Minyardb0defcd2006-03-26 01:37:20 -08003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 return rv;
3483}
3484
Bill Pemberton2223cbe2012-11-19 13:22:51 -05003485static int init_ipmi_si(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 int i;
3488 char *str;
Corey Minyard50c812b2006-03-26 01:37:21 -08003489 int rv;
Matthew Garrett2407d772010-05-26 14:43:46 -07003490 struct smi_info *e;
Matthew Garrett06ee4592010-05-26 14:43:49 -07003491 enum ipmi_addr_src type = SI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 if (initialized)
3494 return 0;
3495 initialized = 1;
3496
Corey Minyardf2afae42013-02-27 17:05:13 -08003497 if (si_tryplatform) {
3498 rv = platform_driver_register(&ipmi_driver);
3499 if (rv) {
3500 printk(KERN_ERR PFX "Unable to register "
3501 "driver: %d\n", rv);
3502 return rv;
3503 }
Corey Minyard50c812b2006-03-26 01:37:21 -08003504 }
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 /* Parse out the si_type string into its components. */
3507 str = si_type_str;
3508 if (*str != '\0') {
Corey Minyarde8b33612005-09-06 15:18:45 -07003509 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 si_type[i] = str;
3511 str = strchr(str, ',');
3512 if (str) {
3513 *str = '\0';
3514 str++;
3515 } else {
3516 break;
3517 }
3518 }
3519 }
3520
Corey Minyard1fdd75b2005-09-06 15:18:42 -07003521 printk(KERN_INFO "IPMI System Interface driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003523 /* If the user gave us a device, they presumably want us to use it */
Rob Herringa1e9c9d2011-02-23 15:37:59 -06003524 if (!hardcode_find_bmc())
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003525 return 0;
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003526
Corey Minyardb0defcd2006-03-26 01:37:20 -08003527#ifdef CONFIG_PCI
Corey Minyardf2afae42013-02-27 17:05:13 -08003528 if (si_trypci) {
3529 rv = pci_register_driver(&ipmi_pci_driver);
3530 if (rv)
3531 printk(KERN_ERR PFX "Unable to register "
3532 "PCI driver: %d\n", rv);
3533 else
3534 pci_registered = 1;
3535 }
Corey Minyardb0defcd2006-03-26 01:37:20 -08003536#endif
3537
Matthew Garrett754d4532010-05-26 14:43:47 -07003538#ifdef CONFIG_ACPI
Corey Minyardd941aea2013-02-27 17:05:12 -08003539 if (si_tryacpi) {
3540 pnp_register_driver(&ipmi_pnp_driver);
3541 pnp_registered = 1;
3542 }
Matthew Garrett754d4532010-05-26 14:43:47 -07003543#endif
3544
3545#ifdef CONFIG_DMI
Corey Minyardd941aea2013-02-27 17:05:12 -08003546 if (si_trydmi)
3547 dmi_find_bmc();
Matthew Garrett754d4532010-05-26 14:43:47 -07003548#endif
3549
3550#ifdef CONFIG_ACPI
Corey Minyardd941aea2013-02-27 17:05:12 -08003551 if (si_tryacpi)
3552 spmi_find_bmc();
Matthew Garrett754d4532010-05-26 14:43:47 -07003553#endif
3554
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05003555#ifdef CONFIG_PARISC
3556 register_parisc_driver(&ipmi_parisc_driver);
3557 parisc_registered = 1;
3558 /* poking PC IO addresses will crash machine, don't do it */
3559 si_trydefaults = 0;
3560#endif
3561
Matthew Garrett06ee4592010-05-26 14:43:49 -07003562 /* We prefer devices with interrupts, but in the case of a machine
3563 with multiple BMCs we assume that there will be several instances
3564 of a given type so if we succeed in registering a type then also
3565 try to register everything else of the same type */
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003566
Matthew Garrett2407d772010-05-26 14:43:46 -07003567 mutex_lock(&smi_infos_lock);
3568 list_for_each_entry(e, &smi_infos, link) {
Matthew Garrett06ee4592010-05-26 14:43:49 -07003569 /* Try to register a device if it has an IRQ and we either
3570 haven't successfully registered a device yet or this
3571 device has the same type as one we successfully registered */
3572 if (e->irq && (!type || e->addr_source == type)) {
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003573 if (!try_smi_init(e)) {
Matthew Garrett06ee4592010-05-26 14:43:49 -07003574 type = e->addr_source;
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003575 }
3576 }
3577 }
3578
Matthew Garrett06ee4592010-05-26 14:43:49 -07003579 /* type will only have been set if we successfully registered an si */
3580 if (type) {
3581 mutex_unlock(&smi_infos_lock);
3582 return 0;
3583 }
3584
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003585 /* Fall back to the preferred device */
3586
3587 list_for_each_entry(e, &smi_infos, link) {
Matthew Garrett06ee4592010-05-26 14:43:49 -07003588 if (!e->irq && (!type || e->addr_source == type)) {
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003589 if (!try_smi_init(e)) {
Matthew Garrett06ee4592010-05-26 14:43:49 -07003590 type = e->addr_source;
Matthew Garrettd8cc5262010-05-26 14:43:46 -07003591 }
3592 }
Matthew Garrett2407d772010-05-26 14:43:46 -07003593 }
3594 mutex_unlock(&smi_infos_lock);
3595
Matthew Garrett06ee4592010-05-26 14:43:49 -07003596 if (type)
3597 return 0;
3598
Corey Minyardb0defcd2006-03-26 01:37:20 -08003599 if (si_trydefaults) {
Corey Minyardd6dfd132006-03-31 02:30:41 -08003600 mutex_lock(&smi_infos_lock);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003601 if (list_empty(&smi_infos)) {
3602 /* No BMC was found, try defaults. */
Corey Minyardd6dfd132006-03-31 02:30:41 -08003603 mutex_unlock(&smi_infos_lock);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003604 default_find_bmc();
Matthew Garrett2407d772010-05-26 14:43:46 -07003605 } else
Corey Minyardd6dfd132006-03-31 02:30:41 -08003606 mutex_unlock(&smi_infos_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Corey Minyardd6dfd132006-03-31 02:30:41 -08003609 mutex_lock(&smi_infos_lock);
Corey Minyardb361e272006-12-06 20:41:07 -08003610 if (unload_when_empty && list_empty(&smi_infos)) {
Corey Minyardd6dfd132006-03-31 02:30:41 -08003611 mutex_unlock(&smi_infos_lock);
Corey Minyardd2478522011-02-10 16:08:38 -06003612 cleanup_ipmi_si();
Myron Stowe279fbd02010-05-26 14:43:52 -07003613 printk(KERN_WARNING PFX
3614 "Unable to find any System Interface(s)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return -ENODEV;
Corey Minyardb0defcd2006-03-26 01:37:20 -08003616 } else {
Corey Minyardd6dfd132006-03-31 02:30:41 -08003617 mutex_unlock(&smi_infos_lock);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621module_init(init_ipmi_si);
3622
Corey Minyardb361e272006-12-06 20:41:07 -08003623static void cleanup_one_si(struct smi_info *to_clean)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Matthew Garrett2407d772010-05-26 14:43:46 -07003625 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 unsigned long flags;
3627
Corey Minyardb0defcd2006-03-26 01:37:20 -08003628 if (!to_clean)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return;
3630
Corey Minyardb0defcd2006-03-26 01:37:20 -08003631 list_del(&to_clean->link);
3632
Corey Minyardee6cd5f2007-05-08 00:23:54 -07003633 /* Tell the driver that we are shutting down. */
Corey Minyarda9a2c442005-11-07 01:00:03 -08003634 atomic_inc(&to_clean->stop_operation);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003635
Corey Minyardc305e3d2008-04-29 01:01:10 -07003636 /*
3637 * Make sure the timer and thread are stopped and will not run
3638 * again.
3639 */
Corey Minyarda9a2c442005-11-07 01:00:03 -08003640 wait_for_timer_and_thread(to_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Corey Minyardc305e3d2008-04-29 01:01:10 -07003642 /*
3643 * Timeouts are stopped, now make sure the interrupts are off
3644 * for the device. A little tricky with locks to make sure
3645 * there are no races.
3646 */
Corey Minyardee6cd5f2007-05-08 00:23:54 -07003647 spin_lock_irqsave(&to_clean->si_lock, flags);
3648 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3649 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3650 poll(to_clean);
3651 schedule_timeout_uninterruptible(1);
3652 spin_lock_irqsave(&to_clean->si_lock, flags);
3653 }
3654 disable_si_irq(to_clean);
3655 spin_unlock_irqrestore(&to_clean->si_lock, flags);
3656 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3657 poll(to_clean);
3658 schedule_timeout_uninterruptible(1);
3659 }
3660
3661 /* Clean up interrupts and make sure that everything is done. */
3662 if (to_clean->irq_cleanup)
3663 to_clean->irq_cleanup(to_clean);
Corey Minyarde8b33612005-09-06 15:18:45 -07003664 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 poll(to_clean);
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07003666 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
3668
Matthew Garrett2407d772010-05-26 14:43:46 -07003669 if (to_clean->intf)
3670 rv = ipmi_unregister_smi(to_clean->intf);
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (rv) {
Myron Stowe279fbd02010-05-26 14:43:52 -07003673 printk(KERN_ERR PFX "Unable to unregister device: errno=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 rv);
3675 }
3676
Matthew Garrett2407d772010-05-26 14:43:46 -07003677 if (to_clean->handlers)
3678 to_clean->handlers->cleanup(to_clean->si_sm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680 kfree(to_clean->si_sm);
3681
Corey Minyardb0defcd2006-03-26 01:37:20 -08003682 if (to_clean->addr_source_cleanup)
3683 to_clean->addr_source_cleanup(to_clean);
Paolo Galtieri7767e122005-12-15 12:34:28 -08003684 if (to_clean->io_cleanup)
3685 to_clean->io_cleanup(to_clean);
Corey Minyard50c812b2006-03-26 01:37:21 -08003686
3687 if (to_clean->dev_registered)
3688 platform_device_unregister(to_clean->pdev);
3689
3690 kfree(to_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Sergey Senozhatsky0dcf3342011-03-23 16:42:54 -07003693static void cleanup_ipmi_si(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Corey Minyardb0defcd2006-03-26 01:37:20 -08003695 struct smi_info *e, *tmp_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Corey Minyardb0defcd2006-03-26 01:37:20 -08003697 if (!initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return;
3699
Corey Minyardb0defcd2006-03-26 01:37:20 -08003700#ifdef CONFIG_PCI
Matthew Garrett56480282010-06-29 15:05:29 -07003701 if (pci_registered)
3702 pci_unregister_driver(&ipmi_pci_driver);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003703#endif
Ingo Molnar27d05672009-12-17 08:50:25 +01003704#ifdef CONFIG_ACPI
Yinghai Lu561f8182010-09-22 13:05:15 -07003705 if (pnp_registered)
3706 pnp_unregister_driver(&ipmi_pnp_driver);
Bjorn Helgaas9e368fa2009-11-17 17:05:34 -07003707#endif
Thomas Bogendoerferfdbeb7d2013-09-05 06:36:36 -05003708#ifdef CONFIG_PARISC
3709 if (parisc_registered)
3710 unregister_parisc_driver(&ipmi_parisc_driver);
3711#endif
Corey Minyardb0defcd2006-03-26 01:37:20 -08003712
Rob Herringa1e9c9d2011-02-23 15:37:59 -06003713 platform_driver_unregister(&ipmi_driver);
Corey Minyarddba9b4f2007-05-08 00:23:51 -07003714
Corey Minyardd6dfd132006-03-31 02:30:41 -08003715 mutex_lock(&smi_infos_lock);
Corey Minyardb0defcd2006-03-26 01:37:20 -08003716 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3717 cleanup_one_si(e);
Corey Minyardd6dfd132006-03-31 02:30:41 -08003718 mutex_unlock(&smi_infos_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720module_exit(cleanup_ipmi_si);
3721
3722MODULE_LICENSE("GPL");
Corey Minyard1fdd75b2005-09-06 15:18:42 -07003723MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
Corey Minyardc305e3d2008-04-29 01:01:10 -07003724MODULE_DESCRIPTION("Interface to the IPMI driver for the KCS, SMIC, and BT"
3725 " system interfaces.");