summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-11-15 18:12:28 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-11-15 18:12:28 -0500
commit6363b3f3ac5be096d08c8c504128befa0c033529 (patch)
tree8bef82ead96f39bc09f4d1a1fe17dfa5d0c39d49
parent1b6115fbe3b3db746d7baa11399dd617fc75e1c4 (diff)
parent6297fabd93f93182245383ba7de56bef829a796b (diff)
Merge tag 'ipmi-for-4.15' of git://github.com/cminyard/linux-ipmi
Pull IPMI updates from Corey Minyard: "This is a fairly large rework of the IPMI code, along with a bunch of smaller fixes. The major changes have been in the next tree for a couple of months, so they should be good to do in. - Some users had IPMI systems where the GUID of the IPMI controller could change. So rescanning of the GUID was added. The naming of some sysfs things was dependent on the GUID, however, so this resulted in the sysfs interface code in IPMI changing to remove that dependency and name the IPMI BMCs like other sysfs devices. - The ipmi_si_intf.c code was fairly bloated with all the different discovery methods (PCI, ACPI, SMBIOS, OF, platform, module parameters, hot add). The structure of how the interfaces were added was redone to make them more modular, then the individual methods were pulled out into their own files" * tag 'ipmi-for-4.15' of git://github.com/cminyard/linux-ipmi: (48 commits) ipmi_si: Delete an error message for a failed memory allocation in try_smi_init() ipmi_si: fix memory leak on new_smi ipmi: remove redundant initialization of bmc ipmi: pr_err() strings should end with newlines ipmi: Clean up some print operations ipmi: Make the DMI probe into a generic platform probe ipmi: Make the IPMI proc interface configurable ipmi_ssif: Add device attrs for the things in proc ipmi_si: Add device attrs for the things in proc ipmi_si: remove ipmi_smi_alloc() function ipmi_si: Move port and mem I/O handling to their own files ipmi_si: Get rid of unused spacing and port fields ipmi_si: Move PARISC handling to another file ipmi_si: Move PCI setup to another file ipmi_si: Move platform device handling to another file ipmi_si: Move hardcode handling to a separate file. ipmi_si: Move the hotmod handling to another file. ipmi_si: Change ipmi_si_add_smi() to take just I/O info ipmi_si: Move io setup into io structure ipmi_si: Move irq setup handling into the io struct ...
-rw-r--r--Documentation/IPMI.txt4
-rw-r--r--drivers/char/ipmi/Kconfig35
-rw-r--r--drivers/char/ipmi/Makefile10
-rw-r--r--drivers/char/ipmi/ipmi_dmi.c76
-rw-r--r--drivers/char/ipmi/ipmi_dmi.h8
-rw-r--r--drivers/char/ipmi/ipmi_msghandler.c1275
-rw-r--r--drivers/char/ipmi/ipmi_powernv.c4
-rw-r--r--drivers/char/ipmi/ipmi_poweroff.c2
-rw-r--r--drivers/char/ipmi/ipmi_si.h49
-rw-r--r--drivers/char/ipmi/ipmi_si_hardcode.c146
-rw-r--r--drivers/char/ipmi/ipmi_si_hotmod.c242
-rw-r--r--drivers/char/ipmi/ipmi_si_intf.c2122
-rw-r--r--drivers/char/ipmi/ipmi_si_mem_io.c144
-rw-r--r--drivers/char/ipmi/ipmi_si_parisc.c58
-rw-r--r--drivers/char/ipmi/ipmi_si_pci.c166
-rw-r--r--drivers/char/ipmi/ipmi_si_platform.c593
-rw-r--r--drivers/char/ipmi/ipmi_si_port_io.c112
-rw-r--r--drivers/char/ipmi/ipmi_si_sm.h23
-rw-r--r--drivers/char/ipmi/ipmi_ssif.c112
-rw-r--r--drivers/char/ipmi/ipmi_watchdog.c11
-rw-r--r--include/linux/ipmi.h8
-rw-r--r--include/linux/ipmi_smi.h27
22 files changed, 3008 insertions, 2219 deletions
diff --git a/Documentation/IPMI.txt b/Documentation/IPMI.txt
index aa77a25a0940..5ef1047e2e66 100644
--- a/Documentation/IPMI.txt
+++ b/Documentation/IPMI.txt
@@ -81,7 +81,9 @@ If you want the driver to put an event into the event log on a panic,
81enable the 'Generate a panic event to all BMCs on a panic' option. If 81enable the 'Generate a panic event to all BMCs on a panic' option. If
82you want the whole panic string put into the event log using OEM 82you want the whole panic string put into the event log using OEM
83events, enable the 'Generate OEM events containing the panic string' 83events, enable the 'Generate OEM events containing the panic string'
84option. 84option. You can also enable these dynamically by setting the module
85parameter named "panic_op" in the ipmi_msghandler module to "event"
86or "string". Setting that parameter to "none" disables this function.
85 87
86Basic Design 88Basic Design
87------------ 89------------
diff --git a/drivers/char/ipmi/Kconfig b/drivers/char/ipmi/Kconfig
index f6fa056a52fc..3544abc0f9f9 100644
--- a/drivers/char/ipmi/Kconfig
+++ b/drivers/char/ipmi/Kconfig
@@ -22,24 +22,39 @@ config IPMI_DMI_DECODE
22 22
23if IPMI_HANDLER 23if IPMI_HANDLER
24 24
25config IPMI_PROC_INTERFACE
26 bool 'Provide an interface for IPMI stats in /proc (deprecated)'
27 depends on PROC_FS
28 default y
29 help
30 Do not use this any more, use sysfs for this info. It will be
31 removed in future kernel versions.
32
25config IPMI_PANIC_EVENT 33config IPMI_PANIC_EVENT
26 bool 'Generate a panic event to all BMCs on a panic' 34 bool 'Generate a panic event to all BMCs on a panic'
27 help 35 help
28 When a panic occurs, this will cause the IPMI message handler to 36 When a panic occurs, this will cause the IPMI message handler to,
29 generate an IPMI event describing the panic to each interface 37 by default, generate an IPMI event describing the panic to each
30 registered with the message handler. 38 interface registered with the message handler. This is always
39 available, the module parameter for ipmi_msghandler named
40 panic_op can be set to "event" to chose this value, this config
41 simply causes the default value to be set to "event".
31 42
32config IPMI_PANIC_STRING 43config IPMI_PANIC_STRING
33 bool 'Generate OEM events containing the panic string' 44 bool 'Generate OEM events containing the panic string'
34 depends on IPMI_PANIC_EVENT 45 depends on IPMI_PANIC_EVENT
35 help 46 help
36 When a panic occurs, this will cause the IPMI message handler to 47 When a panic occurs, this will cause the IPMI message handler to,
37 generate IPMI OEM type f0 events holding the IPMB address of the 48 by default, generate IPMI OEM type f0 events holding the IPMB
38 panic generator (byte 4 of the event), a sequence number for the 49 address of the panic generator (byte 4 of the event), a sequence
39 string (byte 5 of the event) and part of the string (the rest of the 50 number for the string (byte 5 of the event) and part of the
40 event). Bytes 1, 2, and 3 are the normal usage for an OEM event. 51 string (the rest of the event). Bytes 1, 2, and 3 are the normal
41 You can fetch these events and use the sequence numbers to piece the 52 usage for an OEM event. You can fetch these events and use the
42 string together. 53 sequence numbers to piece the string together. This config
54 parameter sets the default value to generate these events,
55 the module parameter for ipmi_msghandler named panic_op can
56 be set to "string" to chose this value, this config simply
57 causes the default value to be set to "string".
43 58
44config IPMI_DEVICE_INTERFACE 59config IPMI_DEVICE_INTERFACE
45 tristate 'Device interface for IPMI' 60 tristate 'Device interface for IPMI'
diff --git a/drivers/char/ipmi/Makefile b/drivers/char/ipmi/Makefile
index 43b7d86cc5f2..33b899fcf14a 100644
--- a/drivers/char/ipmi/Makefile
+++ b/drivers/char/ipmi/Makefile
@@ -3,7 +3,15 @@
3# Makefile for the ipmi drivers. 3# Makefile for the ipmi drivers.
4# 4#
5 5
6ipmi_si-y := ipmi_si_intf.o ipmi_kcs_sm.o ipmi_smic_sm.o ipmi_bt_sm.o 6ipmi_si-y := ipmi_si_intf.o ipmi_kcs_sm.o ipmi_smic_sm.o ipmi_bt_sm.o \
7 ipmi_si_hotmod.o ipmi_si_hardcode.o ipmi_si_platform.o \
8 ipmi_si_port_io.o ipmi_si_mem_io.o
9ifdef CONFIG_PCI
10ipmi_si-y += ipmi_si_pci.o
11endif
12ifdef CONFIG_PARISC
13ipmi_si-y += ipmi_si_parisc.o
14endif
7 15
8obj-$(CONFIG_IPMI_HANDLER) += ipmi_msghandler.o 16obj-$(CONFIG_IPMI_HANDLER) += ipmi_msghandler.o
9obj-$(CONFIG_IPMI_DEVICE_INTERFACE) += ipmi_devintf.o 17obj-$(CONFIG_IPMI_DEVICE_INTERFACE) += ipmi_devintf.o
diff --git a/drivers/char/ipmi/ipmi_dmi.c b/drivers/char/ipmi/ipmi_dmi.c
index 2059f79d669a..ab78b3be7e33 100644
--- a/drivers/char/ipmi/ipmi_dmi.c
+++ b/drivers/char/ipmi/ipmi_dmi.c
@@ -9,10 +9,16 @@
9#include <linux/dmi.h> 9#include <linux/dmi.h>
10#include <linux/platform_device.h> 10#include <linux/platform_device.h>
11#include <linux/property.h> 11#include <linux/property.h>
12#include "ipmi_si_sm.h"
12#include "ipmi_dmi.h" 13#include "ipmi_dmi.h"
13 14
15#define IPMI_DMI_TYPE_KCS 0x01
16#define IPMI_DMI_TYPE_SMIC 0x02
17#define IPMI_DMI_TYPE_BT 0x03
18#define IPMI_DMI_TYPE_SSIF 0x04
19
14struct ipmi_dmi_info { 20struct ipmi_dmi_info {
15 int type; 21 enum si_type si_type;
16 u32 flags; 22 u32 flags;
17 unsigned long addr; 23 unsigned long addr;
18 u8 slave_addr; 24 u8 slave_addr;
@@ -23,6 +29,15 @@ static struct ipmi_dmi_info *ipmi_dmi_infos;
23 29
24static int ipmi_dmi_nr __initdata; 30static int ipmi_dmi_nr __initdata;
25 31
32#define set_prop_entry(_p_, _name_, type, val) \
33do { \
34 struct property_entry *_p = &_p_; \
35 _p->name = _name_; \
36 _p->length = sizeof(type); \
37 _p->is_string = false; \
38 _p->value.type##_data = val; \
39} while(0)
40
26static void __init dmi_add_platform_ipmi(unsigned long base_addr, 41static void __init dmi_add_platform_ipmi(unsigned long base_addr,
27 u32 flags, 42 u32 flags,
28 u8 slave_addr, 43 u8 slave_addr,
@@ -33,27 +48,14 @@ static void __init dmi_add_platform_ipmi(unsigned long base_addr,
33 struct platform_device *pdev; 48 struct platform_device *pdev;
34 struct resource r[4]; 49 struct resource r[4];
35 unsigned int num_r = 1, size; 50 unsigned int num_r = 1, size;
36 struct property_entry p[4] = { 51 struct property_entry p[5];
37 PROPERTY_ENTRY_U8("slave-addr", slave_addr), 52 unsigned int pidx = 0;
38 PROPERTY_ENTRY_U8("ipmi-type", type),
39 PROPERTY_ENTRY_U16("i2c-addr", base_addr),
40 { }
41 };
42 char *name, *override; 53 char *name, *override;
43 int rv; 54 int rv;
55 enum si_type si_type;
44 struct ipmi_dmi_info *info; 56 struct ipmi_dmi_info *info;
45 57
46 info = kmalloc(sizeof(*info), GFP_KERNEL); 58 memset(p, 0, sizeof(p));
47 if (!info) {
48 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
49 } else {
50 info->type = type;
51 info->flags = flags;
52 info->addr = base_addr;
53 info->slave_addr = slave_addr;
54 info->next = ipmi_dmi_infos;
55 ipmi_dmi_infos = info;
56 }
57 59
58 name = "dmi-ipmi-si"; 60 name = "dmi-ipmi-si";
59 override = "ipmi_si"; 61 override = "ipmi_si";
@@ -63,28 +65,53 @@ static void __init dmi_add_platform_ipmi(unsigned long base_addr,
63 override = "ipmi_ssif"; 65 override = "ipmi_ssif";
64 offset = 1; 66 offset = 1;
65 size = 1; 67 size = 1;
68 si_type = SI_TYPE_INVALID;
66 break; 69 break;
67 case IPMI_DMI_TYPE_BT: 70 case IPMI_DMI_TYPE_BT:
68 size = 3; 71 size = 3;
72 si_type = SI_BT;
69 break; 73 break;
70 case IPMI_DMI_TYPE_KCS: 74 case IPMI_DMI_TYPE_KCS:
75 size = 2;
76 si_type = SI_KCS;
77 break;
71 case IPMI_DMI_TYPE_SMIC: 78 case IPMI_DMI_TYPE_SMIC:
72 size = 2; 79 size = 2;
80 si_type = SI_SMIC;
73 break; 81 break;
74 default: 82 default:
75 pr_err("ipmi:dmi: Invalid IPMI type: %d", type); 83 pr_err("ipmi:dmi: Invalid IPMI type: %d\n", type);
76 return; 84 return;
77 } 85 }
78 86
87 if (si_type != SI_TYPE_INVALID)
88 set_prop_entry(p[pidx++], "ipmi-type", u8, si_type);
89 set_prop_entry(p[pidx++], "slave-addr", u8, slave_addr);
90 set_prop_entry(p[pidx++], "addr-source", u8, SI_SMBIOS);
91
92 info = kmalloc(sizeof(*info), GFP_KERNEL);
93 if (!info) {
94 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
95 } else {
96 info->si_type = si_type;
97 info->flags = flags;
98 info->addr = base_addr;
99 info->slave_addr = slave_addr;
100 info->next = ipmi_dmi_infos;
101 ipmi_dmi_infos = info;
102 }
103
79 pdev = platform_device_alloc(name, ipmi_dmi_nr); 104 pdev = platform_device_alloc(name, ipmi_dmi_nr);
80 if (!pdev) { 105 if (!pdev) {
81 pr_err("ipmi:dmi: Error allocation IPMI platform device"); 106 pr_err("ipmi:dmi: Error allocation IPMI platform device\n");
82 return; 107 return;
83 } 108 }
84 pdev->driver_override = override; 109 pdev->driver_override = override;
85 110
86 if (type == IPMI_DMI_TYPE_SSIF) 111 if (type == IPMI_DMI_TYPE_SSIF) {
112 set_prop_entry(p[pidx++], "i2c-addr", u16, base_addr);
87 goto add_properties; 113 goto add_properties;
114 }
88 115
89 memset(r, 0, sizeof(r)); 116 memset(r, 0, sizeof(r));
90 117
@@ -152,12 +179,13 @@ err:
152 * This function allows an ACPI-specified IPMI device to look up the 179 * This function allows an ACPI-specified IPMI device to look up the
153 * slave address from the DMI table. 180 * slave address from the DMI table.
154 */ 181 */
155int ipmi_dmi_get_slave_addr(int type, u32 flags, unsigned long base_addr) 182int ipmi_dmi_get_slave_addr(enum si_type si_type, u32 flags,
183 unsigned long base_addr)
156{ 184{
157 struct ipmi_dmi_info *info = ipmi_dmi_infos; 185 struct ipmi_dmi_info *info = ipmi_dmi_infos;
158 186
159 while (info) { 187 while (info) {
160 if (info->type == type && 188 if (info->si_type == si_type &&
161 info->flags == flags && 189 info->flags == flags &&
162 info->addr == base_addr) 190 info->addr == base_addr)
163 return info->slave_addr; 191 return info->slave_addr;
@@ -240,7 +268,7 @@ static void __init dmi_decode_ipmi(const struct dmi_header *dm)
240 offset = 16; 268 offset = 16;
241 break; 269 break;
242 default: 270 default:
243 pr_err("ipmi:dmi: Invalid offset: 0"); 271 pr_err("ipmi:dmi: Invalid offset: 0\n");
244 return; 272 return;
245 } 273 }
246 } 274 }
diff --git a/drivers/char/ipmi/ipmi_dmi.h b/drivers/char/ipmi/ipmi_dmi.h
index ea990a8e3b09..6c21018e3668 100644
--- a/drivers/char/ipmi/ipmi_dmi.h
+++ b/drivers/char/ipmi/ipmi_dmi.h
@@ -3,11 +3,7 @@
3 * DMI defines for use by IPMI 3 * DMI defines for use by IPMI
4 */ 4 */
5 5
6#define IPMI_DMI_TYPE_KCS 0x01
7#define IPMI_DMI_TYPE_SMIC 0x02
8#define IPMI_DMI_TYPE_BT 0x03
9#define IPMI_DMI_TYPE_SSIF 0x04
10
11#ifdef CONFIG_IPMI_DMI_DECODE 6#ifdef CONFIG_IPMI_DMI_DECODE
12int ipmi_dmi_get_slave_addr(int type, u32 flags, unsigned long base_addr); 7int ipmi_dmi_get_slave_addr(enum si_type si_type, u32 flags,
8 unsigned long base_addr);
13#endif 9#endif
diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
index 810b138f5897..9de189db2cc3 100644
--- a/drivers/char/ipmi/ipmi_msghandler.c
+++ b/drivers/char/ipmi/ipmi_msghandler.c
@@ -46,6 +46,9 @@
46#include <linux/proc_fs.h> 46#include <linux/proc_fs.h>
47#include <linux/rcupdate.h> 47#include <linux/rcupdate.h>
48#include <linux/interrupt.h> 48#include <linux/interrupt.h>
49#include <linux/moduleparam.h>
50#include <linux/workqueue.h>
51#include <linux/uuid.h>
49 52
50#define PFX "IPMI message handler: " 53#define PFX "IPMI message handler: "
51 54
@@ -61,9 +64,77 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
61 64
62static int initialized; 65static int initialized;
63 66
64#ifdef CONFIG_PROC_FS 67enum ipmi_panic_event_op {
68 IPMI_SEND_PANIC_EVENT_NONE,
69 IPMI_SEND_PANIC_EVENT,
70 IPMI_SEND_PANIC_EVENT_STRING
71};
72#ifdef CONFIG_IPMI_PANIC_STRING
73#define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
74#elif defined(CONFIG_IPMI_PANIC_EVENT)
75#define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT
76#else
77#define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_NONE
78#endif
79static enum ipmi_panic_event_op ipmi_send_panic_event = IPMI_PANIC_DEFAULT;
80
81static int panic_op_write_handler(const char *val,
82 const struct kernel_param *kp)
83{
84 char valcp[16];
85 char *s;
86
87 strncpy(valcp, val, 16);
88 valcp[15] = '\0';
89
90 s = strstrip(valcp);
91
92 if (strcmp(s, "none") == 0)
93 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
94 else if (strcmp(s, "event") == 0)
95 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
96 else if (strcmp(s, "string") == 0)
97 ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
98 else
99 return -EINVAL;
100
101 return 0;
102}
103
104static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
105{
106 switch (ipmi_send_panic_event) {
107 case IPMI_SEND_PANIC_EVENT_NONE:
108 strcpy(buffer, "none");
109 break;
110
111 case IPMI_SEND_PANIC_EVENT:
112 strcpy(buffer, "event");
113 break;
114
115 case IPMI_SEND_PANIC_EVENT_STRING:
116 strcpy(buffer, "string");
117 break;
118
119 default:
120 strcpy(buffer, "???");
121 break;
122 }
123
124 return strlen(buffer);
125}
126
127static const struct kernel_param_ops panic_op_ops = {
128 .set = panic_op_write_handler,
129 .get = panic_op_read_handler
130};
131module_param_cb(panic_op, &panic_op_ops, NULL, 0600);
132MODULE_PARM_DESC(panic_op, "Sets if the IPMI driver will attempt to store panic information in the event log in the event of a panic. Set to 'none' for no, 'event' for a single event, or 'string' for a generic event and the panic string in IPMI OEM events.");
133
134
135#ifdef CONFIG_IPMI_PROC_INTERFACE
65static struct proc_dir_entry *proc_ipmi_root; 136static struct proc_dir_entry *proc_ipmi_root;
66#endif /* CONFIG_PROC_FS */ 137#endif /* CONFIG_IPMI_PROC_INTERFACE */
67 138
68/* Remain in auto-maintenance mode for this amount of time (in ms). */ 139/* Remain in auto-maintenance mode for this amount of time (in ms). */
69#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000 140#define IPMI_MAINTENANCE_MODE_TIMEOUT 30000
@@ -90,6 +161,9 @@ static struct proc_dir_entry *proc_ipmi_root;
90 */ 161 */
91#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME)) 162#define IPMI_REQUEST_EV_TIME (1000 / (IPMI_TIMEOUT_TIME))
92 163
164/* How long should we cache dynamic device IDs? */
165#define IPMI_DYN_DEV_ID_EXPIRY (10 * HZ)
166
93/* 167/*
94 * The main "user" data structure. 168 * The main "user" data structure.
95 */ 169 */
@@ -169,10 +243,17 @@ struct seq_table {
169 243
170#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff) 244#define NEXT_SEQID(seqid) (((seqid) + 1) & 0x3ffffff)
171 245
246#define IPMI_MAX_CHANNELS 16
172struct ipmi_channel { 247struct ipmi_channel {
173 unsigned char medium; 248 unsigned char medium;
174 unsigned char protocol; 249 unsigned char protocol;
250};
175 251
252struct ipmi_channel_set {
253 struct ipmi_channel c[IPMI_MAX_CHANNELS];
254};
255
256struct ipmi_my_addrinfo {
176 /* 257 /*
177 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR, 258 * My slave address. This is initialized to IPMI_BMC_SLAVE_ADDR,
178 * but may be changed by the user. 259 * but may be changed by the user.
@@ -186,23 +267,38 @@ struct ipmi_channel {
186 unsigned char lun; 267 unsigned char lun;
187}; 268};
188 269
189#ifdef CONFIG_PROC_FS 270#ifdef CONFIG_IPMI_PROC_INTERFACE
190struct ipmi_proc_entry { 271struct ipmi_proc_entry {
191 char *name; 272 char *name;
192 struct ipmi_proc_entry *next; 273 struct ipmi_proc_entry *next;
193}; 274};
194#endif 275#endif
195 276
277/*
278 * Note that the product id, manufacturer id, guid, and device id are
279 * immutable in this structure, so dyn_mutex is not required for
280 * accessing those. If those change on a BMC, a new BMC is allocated.
281 */
196struct bmc_device { 282struct bmc_device {
197 struct platform_device pdev; 283 struct platform_device pdev;
284 struct list_head intfs; /* Interfaces on this BMC. */
198 struct ipmi_device_id id; 285 struct ipmi_device_id id;
199 unsigned char guid[16]; 286 struct ipmi_device_id fetch_id;
200 int guid_set; 287 int dyn_id_set;
201 char name[16]; 288 unsigned long dyn_id_expiry;
289 struct mutex dyn_mutex; /* Protects id, intfs, & dyn* */
290 guid_t guid;
291 guid_t fetch_guid;
292 int dyn_guid_set;
202 struct kref usecount; 293 struct kref usecount;
294 struct work_struct remove_work;
203}; 295};
204#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev) 296#define to_bmc_device(x) container_of((x), struct bmc_device, pdev.dev)
205 297
298static int bmc_get_device_id(ipmi_smi_t intf, struct bmc_device *bmc,
299 struct ipmi_device_id *id,
300 bool *guid_set, guid_t *guid);
301
206/* 302/*
207 * Various statistics for IPMI, these index stats[] in the ipmi_smi 303 * Various statistics for IPMI, these index stats[] in the ipmi_smi
208 * structure. 304 * structure.
@@ -308,7 +404,6 @@ enum ipmi_stat_indexes {
308 404
309 405
310#define IPMI_IPMB_NUM_SEQ 64 406#define IPMI_IPMB_NUM_SEQ 64
311#define IPMI_MAX_CHANNELS 16
312struct ipmi_smi { 407struct ipmi_smi {
313 /* What interface number are we? */ 408 /* What interface number are we? */
314 int intf_num; 409 int intf_num;
@@ -327,15 +422,23 @@ struct ipmi_smi {
327 */ 422 */
328 struct list_head users; 423 struct list_head users;
329 424
330 /* Information to supply to users. */
331 unsigned char ipmi_version_major;
332 unsigned char ipmi_version_minor;
333
334 /* Used for wake ups at startup. */ 425 /* Used for wake ups at startup. */
335 wait_queue_head_t waitq; 426 wait_queue_head_t waitq;
336 427
428 /*
429 * Prevents the interface from being unregistered when the
430 * interface is used by being looked up through the BMC
431 * structure.
432 */
433 struct mutex bmc_reg_mutex;
434
435 struct bmc_device tmp_bmc;
337 struct bmc_device *bmc; 436 struct bmc_device *bmc;
437 bool bmc_registered;
438 struct list_head bmc_link;
338 char *my_dev_name; 439 char *my_dev_name;
440 bool in_bmc_register; /* Handle recursive situations. Yuck. */
441 struct work_struct bmc_reg_work;
339 442
340 /* 443 /*
341 * This is the lower-layer's sender routine. Note that you 444 * This is the lower-layer's sender routine. Note that you
@@ -346,10 +449,13 @@ struct ipmi_smi {
346 const struct ipmi_smi_handlers *handlers; 449 const struct ipmi_smi_handlers *handlers;
347 void *send_info; 450 void *send_info;
348 451
349#ifdef CONFIG_PROC_FS 452#ifdef CONFIG_IPMI_PROC_INTERFACE
350 /* A list of proc entries for this interface. */ 453 /* A list of proc entries for this interface. */
351 struct mutex proc_entry_lock; 454 struct mutex proc_entry_lock;
352 struct ipmi_proc_entry *proc_entries; 455 struct ipmi_proc_entry *proc_entries;
456
457 struct proc_dir_entry *proc_dir;
458 char proc_dir_name[10];
353#endif 459#endif
354 460
355 /* Driver-model device for the system interface. */ 461 /* Driver-model device for the system interface. */
@@ -421,6 +527,8 @@ struct ipmi_smi {
421 * interface comes in with a NULL user, call this routine with 527 * interface comes in with a NULL user, call this routine with
422 * it. Note that the message will still be freed by the 528 * it. Note that the message will still be freed by the
423 * caller. This only works on the system interface. 529 * caller. This only works on the system interface.
530 *
531 * Protected by bmc_reg_mutex.
424 */ 532 */
425 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg); 533 void (*null_user_handler)(ipmi_smi_t intf, struct ipmi_recv_msg *msg);
426 534
@@ -431,11 +539,11 @@ struct ipmi_smi {
431 int curr_channel; 539 int curr_channel;
432 540
433 /* Channel information */ 541 /* Channel information */
434 struct ipmi_channel channels[IPMI_MAX_CHANNELS]; 542 struct ipmi_channel_set *channel_list;
435 543 unsigned int curr_working_cset; /* First index into the following. */
436 /* Proc FS stuff. */ 544 struct ipmi_channel_set wchannels[2];
437 struct proc_dir_entry *proc_dir; 545 struct ipmi_my_addrinfo addrinfo[IPMI_MAX_CHANNELS];
438 char proc_dir_name[10]; 546 bool channels_ready;
439 547
440 atomic_t stats[IPMI_NUM_STATS]; 548 atomic_t stats[IPMI_NUM_STATS];
441 549
@@ -448,6 +556,14 @@ struct ipmi_smi {
448}; 556};
449#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev) 557#define to_si_intf_from_dev(device) container_of(device, struct ipmi_smi, dev)
450 558
559static void __get_guid(ipmi_smi_t intf);
560static void __ipmi_bmc_unregister(ipmi_smi_t intf);
561static int __ipmi_bmc_register(ipmi_smi_t intf,
562 struct ipmi_device_id *id,
563 bool guid_set, guid_t *guid, int intf_num);
564static int __scan_channels(ipmi_smi_t intf, struct ipmi_device_id *id);
565
566
451/** 567/**
452 * The driver model view of the IPMI messaging driver. 568 * The driver model view of the IPMI messaging driver.
453 */ 569 */
@@ -457,6 +573,9 @@ static struct platform_driver ipmidriver = {
457 .bus = &platform_bus_type 573 .bus = &platform_bus_type
458 } 574 }
459}; 575};
576/*
577 * This mutex keeps us from adding the same BMC twice.
578 */
460static DEFINE_MUTEX(ipmidriver_mutex); 579static DEFINE_MUTEX(ipmidriver_mutex);
461 580
462static LIST_HEAD(ipmi_interfaces); 581static LIST_HEAD(ipmi_interfaces);
@@ -475,7 +594,7 @@ static DEFINE_MUTEX(smi_watchers_mutex);
475 594
476static const char * const addr_src_to_str[] = { 595static const char * const addr_src_to_str[] = {
477 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI", 596 "invalid", "hotmod", "hardcoded", "SPMI", "ACPI", "SMBIOS", "PCI",
478 "device-tree" 597 "device-tree", "platform"
479}; 598};
480 599
481const char *ipmi_addr_src_to_str(enum ipmi_addr_src src) 600const char *ipmi_addr_src_to_str(enum ipmi_addr_src src)
@@ -1119,12 +1238,21 @@ int ipmi_destroy_user(ipmi_user_t user)
1119} 1238}
1120EXPORT_SYMBOL(ipmi_destroy_user); 1239EXPORT_SYMBOL(ipmi_destroy_user);
1121 1240
1122void ipmi_get_version(ipmi_user_t user, 1241int ipmi_get_version(ipmi_user_t user,
1123 unsigned char *major, 1242 unsigned char *major,
1124 unsigned char *minor) 1243 unsigned char *minor)
1125{ 1244{
1126 *major = user->intf->ipmi_version_major; 1245 struct ipmi_device_id id;
1127 *minor = user->intf->ipmi_version_minor; 1246 int rv;
1247
1248 rv = bmc_get_device_id(user->intf, NULL, &id, NULL, NULL);
1249 if (rv)
1250 return rv;
1251
1252 *major = ipmi_version_major(&id);
1253 *minor = ipmi_version_minor(&id);
1254
1255 return 0;
1128} 1256}
1129EXPORT_SYMBOL(ipmi_get_version); 1257EXPORT_SYMBOL(ipmi_get_version);
1130 1258
@@ -1134,7 +1262,7 @@ int ipmi_set_my_address(ipmi_user_t user,
1134{ 1262{
1135 if (channel >= IPMI_MAX_CHANNELS) 1263 if (channel >= IPMI_MAX_CHANNELS)
1136 return -EINVAL; 1264 return -EINVAL;
1137 user->intf->channels[channel].address = address; 1265 user->intf->addrinfo[channel].address = address;
1138 return 0; 1266 return 0;
1139} 1267}
1140EXPORT_SYMBOL(ipmi_set_my_address); 1268EXPORT_SYMBOL(ipmi_set_my_address);
@@ -1145,7 +1273,7 @@ int ipmi_get_my_address(ipmi_user_t user,
1145{ 1273{
1146 if (channel >= IPMI_MAX_CHANNELS) 1274 if (channel >= IPMI_MAX_CHANNELS)
1147 return -EINVAL; 1275 return -EINVAL;
1148 *address = user->intf->channels[channel].address; 1276 *address = user->intf->addrinfo[channel].address;
1149 return 0; 1277 return 0;
1150} 1278}
1151EXPORT_SYMBOL(ipmi_get_my_address); 1279EXPORT_SYMBOL(ipmi_get_my_address);
@@ -1156,7 +1284,7 @@ int ipmi_set_my_LUN(ipmi_user_t user,
1156{ 1284{
1157 if (channel >= IPMI_MAX_CHANNELS) 1285 if (channel >= IPMI_MAX_CHANNELS)
1158 return -EINVAL; 1286 return -EINVAL;
1159 user->intf->channels[channel].lun = LUN & 0x3; 1287 user->intf->addrinfo[channel].lun = LUN & 0x3;
1160 return 0; 1288 return 0;
1161} 1289}
1162EXPORT_SYMBOL(ipmi_set_my_LUN); 1290EXPORT_SYMBOL(ipmi_set_my_LUN);
@@ -1167,7 +1295,7 @@ int ipmi_get_my_LUN(ipmi_user_t user,
1167{ 1295{
1168 if (channel >= IPMI_MAX_CHANNELS) 1296 if (channel >= IPMI_MAX_CHANNELS)
1169 return -EINVAL; 1297 return -EINVAL;
1170 *address = user->intf->channels[channel].lun; 1298 *address = user->intf->addrinfo[channel].lun;
1171 return 0; 1299 return 0;
1172} 1300}
1173EXPORT_SYMBOL(ipmi_get_my_LUN); 1301EXPORT_SYMBOL(ipmi_get_my_LUN);
@@ -1264,8 +1392,8 @@ int ipmi_set_gets_events(ipmi_user_t user, bool val)
1264 list_move_tail(&msg->link, &msgs); 1392 list_move_tail(&msg->link, &msgs);
1265 intf->waiting_events_count = 0; 1393 intf->waiting_events_count = 0;
1266 if (intf->event_msg_printed) { 1394 if (intf->event_msg_printed) {
1267 printk(KERN_WARNING PFX "Event queue no longer" 1395 dev_warn(intf->si_dev,
1268 " full\n"); 1396 PFX "Event queue no longer full\n");
1269 intf->event_msg_printed = 0; 1397 intf->event_msg_printed = 0;
1270 } 1398 }
1271 1399
@@ -1655,6 +1783,7 @@ static int i_ipmi_request(ipmi_user_t user,
1655 unsigned char ipmb_seq; 1783 unsigned char ipmb_seq;
1656 long seqid; 1784 long seqid;
1657 int broadcast = 0; 1785 int broadcast = 0;
1786 struct ipmi_channel *chans;
1658 1787
1659 if (addr->channel >= IPMI_MAX_CHANNELS) { 1788 if (addr->channel >= IPMI_MAX_CHANNELS) {
1660 ipmi_inc_stat(intf, sent_invalid_commands); 1789 ipmi_inc_stat(intf, sent_invalid_commands);
@@ -1662,8 +1791,9 @@ static int i_ipmi_request(ipmi_user_t user,
1662 goto out_err; 1791 goto out_err;
1663 } 1792 }
1664 1793
1665 if (intf->channels[addr->channel].medium 1794 chans = READ_ONCE(intf->channel_list)->c;
1666 != IPMI_CHANNEL_MEDIUM_IPMB) { 1795
1796 if (chans[addr->channel].medium != IPMI_CHANNEL_MEDIUM_IPMB) {
1667 ipmi_inc_stat(intf, sent_invalid_commands); 1797 ipmi_inc_stat(intf, sent_invalid_commands);
1668 rv = -EINVAL; 1798 rv = -EINVAL;
1669 goto out_err; 1799 goto out_err;
@@ -1785,6 +1915,7 @@ static int i_ipmi_request(ipmi_user_t user,
1785 struct ipmi_lan_addr *lan_addr; 1915 struct ipmi_lan_addr *lan_addr;
1786 unsigned char ipmb_seq; 1916 unsigned char ipmb_seq;
1787 long seqid; 1917 long seqid;
1918 struct ipmi_channel *chans;
1788 1919
1789 if (addr->channel >= IPMI_MAX_CHANNELS) { 1920 if (addr->channel >= IPMI_MAX_CHANNELS) {
1790 ipmi_inc_stat(intf, sent_invalid_commands); 1921 ipmi_inc_stat(intf, sent_invalid_commands);
@@ -1792,9 +1923,11 @@ static int i_ipmi_request(ipmi_user_t user,
1792 goto out_err; 1923 goto out_err;
1793 } 1924 }
1794 1925
1795 if ((intf->channels[addr->channel].medium 1926 chans = READ_ONCE(intf->channel_list)->c;
1927
1928 if ((chans[addr->channel].medium
1796 != IPMI_CHANNEL_MEDIUM_8023LAN) 1929 != IPMI_CHANNEL_MEDIUM_8023LAN)
1797 && (intf->channels[addr->channel].medium 1930 && (chans[addr->channel].medium
1798 != IPMI_CHANNEL_MEDIUM_ASYNC)) { 1931 != IPMI_CHANNEL_MEDIUM_ASYNC)) {
1799 ipmi_inc_stat(intf, sent_invalid_commands); 1932 ipmi_inc_stat(intf, sent_invalid_commands);
1800 rv = -EINVAL; 1933 rv = -EINVAL;
@@ -1928,8 +2061,8 @@ static int check_addr(ipmi_smi_t intf,
1928{ 2061{
1929 if (addr->channel >= IPMI_MAX_CHANNELS) 2062 if (addr->channel >= IPMI_MAX_CHANNELS)
1930 return -EINVAL; 2063 return -EINVAL;
1931 *lun = intf->channels[addr->channel].lun; 2064 *lun = intf->addrinfo[addr->channel].lun;
1932 *saddr = intf->channels[addr->channel].address; 2065 *saddr = intf->addrinfo[addr->channel].address;
1933 return 0; 2066 return 0;
1934} 2067}
1935 2068
@@ -1997,15 +2130,249 @@ int ipmi_request_supply_msgs(ipmi_user_t user,
1997} 2130}
1998EXPORT_SYMBOL(ipmi_request_supply_msgs); 2131EXPORT_SYMBOL(ipmi_request_supply_msgs);
1999 2132
2000#ifdef CONFIG_PROC_FS 2133static void bmc_device_id_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2134{
2135 int rv;
2136
2137 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2138 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2139 || (msg->msg.cmd != IPMI_GET_DEVICE_ID_CMD)) {
2140 dev_warn(intf->si_dev,
2141 PFX "invalid device_id msg: addr_type=%d netfn=%x cmd=%x\n",
2142 msg->addr.addr_type, msg->msg.netfn, msg->msg.cmd);
2143 return;
2144 }
2145
2146 rv = ipmi_demangle_device_id(msg->msg.netfn, msg->msg.cmd,
2147 msg->msg.data, msg->msg.data_len, &intf->bmc->fetch_id);
2148 if (rv) {
2149 dev_warn(intf->si_dev,
2150 PFX "device id demangle failed: %d\n", rv);
2151 intf->bmc->dyn_id_set = 0;
2152 } else {
2153 /*
2154 * Make sure the id data is available before setting
2155 * dyn_id_set.
2156 */
2157 smp_wmb();
2158 intf->bmc->dyn_id_set = 1;
2159 }
2160
2161 wake_up(&intf->waitq);
2162}
2163
2164static int
2165send_get_device_id_cmd(ipmi_smi_t intf)
2166{
2167 struct ipmi_system_interface_addr si;
2168 struct kernel_ipmi_msg msg;
2169
2170 si.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
2171 si.channel = IPMI_BMC_CHANNEL;
2172 si.lun = 0;
2173
2174 msg.netfn = IPMI_NETFN_APP_REQUEST;
2175 msg.cmd = IPMI_GET_DEVICE_ID_CMD;
2176 msg.data = NULL;
2177 msg.data_len = 0;
2178
2179 return i_ipmi_request(NULL,
2180 intf,
2181 (struct ipmi_addr *) &si,
2182 0,
2183 &msg,
2184 intf,
2185 NULL,
2186 NULL,
2187 0,
2188 intf->addrinfo[0].address,
2189 intf->addrinfo[0].lun,
2190 -1, 0);
2191}
2192
2193static int __get_device_id(ipmi_smi_t intf, struct bmc_device *bmc)
2194{
2195 int rv;
2196
2197 bmc->dyn_id_set = 2;
2198
2199 intf->null_user_handler = bmc_device_id_handler;
2200
2201 rv = send_get_device_id_cmd(intf);
2202 if (rv)
2203 return rv;
2204
2205 wait_event(intf->waitq, bmc->dyn_id_set != 2);
2206
2207 if (!bmc->dyn_id_set)
2208 rv = -EIO; /* Something went wrong in the fetch. */
2209
2210 /* dyn_id_set makes the id data available. */
2211 smp_rmb();
2212
2213 intf->null_user_handler = NULL;
2214
2215 return rv;
2216}
2217
2218/*
2219 * Fetch the device id for the bmc/interface. You must pass in either
2220 * bmc or intf, this code will get the other one. If the data has
2221 * been recently fetched, this will just use the cached data. Otherwise
2222 * it will run a new fetch.
2223 *
2224 * Except for the first time this is called (in ipmi_register_smi()),
2225 * this will always return good data;
2226 */
2227static int __bmc_get_device_id(ipmi_smi_t intf, struct bmc_device *bmc,
2228 struct ipmi_device_id *id,
2229 bool *guid_set, guid_t *guid, int intf_num)
2230{
2231 int rv = 0;
2232 int prev_dyn_id_set, prev_guid_set;
2233 bool intf_set = intf != NULL;
2234
2235 if (!intf) {
2236 mutex_lock(&bmc->dyn_mutex);
2237retry_bmc_lock:
2238 if (list_empty(&bmc->intfs)) {
2239 mutex_unlock(&bmc->dyn_mutex);
2240 return -ENOENT;
2241 }
2242 intf = list_first_entry(&bmc->intfs, struct ipmi_smi,
2243 bmc_link);
2244 kref_get(&intf->refcount);
2245 mutex_unlock(&bmc->dyn_mutex);
2246 mutex_lock(&intf->bmc_reg_mutex);
2247 mutex_lock(&bmc->dyn_mutex);
2248 if (intf != list_first_entry(&bmc->intfs, struct ipmi_smi,
2249 bmc_link)) {
2250 mutex_unlock(&intf->bmc_reg_mutex);
2251 kref_put(&intf->refcount, intf_free);
2252 goto retry_bmc_lock;
2253 }
2254 } else {
2255 mutex_lock(&intf->bmc_reg_mutex);
2256 bmc = intf->bmc;
2257 mutex_lock(&bmc->dyn_mutex);
2258 kref_get(&intf->refcount);
2259 }
2260
2261 /* If we have a valid and current ID, just return that. */
2262 if (intf->in_bmc_register ||
2263 (bmc->dyn_id_set && time_is_after_jiffies(bmc->dyn_id_expiry)))
2264 goto out_noprocessing;
2265
2266 prev_guid_set = bmc->dyn_guid_set;
2267 __get_guid(intf);
2268
2269 prev_dyn_id_set = bmc->dyn_id_set;
2270 rv = __get_device_id(intf, bmc);
2271 if (rv)
2272 goto out;
2273
2274 /*
2275 * The guid, device id, manufacturer id, and product id should
2276 * not change on a BMC. If it does we have to do some dancing.
2277 */
2278 if (!intf->bmc_registered
2279 || (!prev_guid_set && bmc->dyn_guid_set)
2280 || (!prev_dyn_id_set && bmc->dyn_id_set)
2281 || (prev_guid_set && bmc->dyn_guid_set
2282 && !guid_equal(&bmc->guid, &bmc->fetch_guid))
2283 || bmc->id.device_id != bmc->fetch_id.device_id
2284 || bmc->id.manufacturer_id != bmc->fetch_id.manufacturer_id
2285 || bmc->id.product_id != bmc->fetch_id.product_id) {
2286 struct ipmi_device_id id = bmc->fetch_id;
2287 int guid_set = bmc->dyn_guid_set;
2288 guid_t guid;
2289
2290 guid = bmc->fetch_guid;
2291 mutex_unlock(&bmc->dyn_mutex);
2292
2293 __ipmi_bmc_unregister(intf);
2294 /* Fill in the temporary BMC for good measure. */
2295 intf->bmc->id = id;
2296 intf->bmc->dyn_guid_set = guid_set;
2297 intf->bmc->guid = guid;
2298 if (__ipmi_bmc_register(intf, &id, guid_set, &guid, intf_num))
2299 need_waiter(intf); /* Retry later on an error. */
2300 else
2301 __scan_channels(intf, &id);
2302
2303
2304 if (!intf_set) {
2305 /*
2306 * We weren't given the interface on the
2307 * command line, so restart the operation on
2308 * the next interface for the BMC.
2309 */
2310 mutex_unlock(&intf->bmc_reg_mutex);
2311 mutex_lock(&bmc->dyn_mutex);
2312 goto retry_bmc_lock;
2313 }
2314
2315 /* We have a new BMC, set it up. */
2316 bmc = intf->bmc;
2317 mutex_lock(&bmc->dyn_mutex);
2318 goto out_noprocessing;
2319 } else if (memcmp(&bmc->fetch_id, &bmc->id, sizeof(bmc->id)))
2320 /* Version info changes, scan the channels again. */
2321 __scan_channels(intf, &bmc->fetch_id);
2322
2323 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
2324
2325out:
2326 if (rv && prev_dyn_id_set) {
2327 rv = 0; /* Ignore failures if we have previous data. */
2328 bmc->dyn_id_set = prev_dyn_id_set;
2329 }
2330 if (!rv) {
2331 bmc->id = bmc->fetch_id;
2332 if (bmc->dyn_guid_set)
2333 bmc->guid = bmc->fetch_guid;
2334 else if (prev_guid_set)
2335 /*
2336 * The guid used to be valid and it failed to fetch,
2337 * just use the cached value.
2338 */
2339 bmc->dyn_guid_set = prev_guid_set;
2340 }
2341out_noprocessing:
2342 if (!rv) {
2343 if (id)
2344 *id = bmc->id;
2345
2346 if (guid_set)
2347 *guid_set = bmc->dyn_guid_set;
2348
2349 if (guid && bmc->dyn_guid_set)
2350 *guid = bmc->guid;
2351 }
2352
2353 mutex_unlock(&bmc->dyn_mutex);
2354 mutex_unlock(&intf->bmc_reg_mutex);
2355
2356 kref_put(&intf->refcount, intf_free);
2357 return rv;
2358}
2359
2360static int bmc_get_device_id(ipmi_smi_t intf, struct bmc_device *bmc,
2361 struct ipmi_device_id *id,
2362 bool *guid_set, guid_t *guid)
2363{
2364 return __bmc_get_device_id(intf, bmc, id, guid_set, guid, -1);
2365}
2366
2367#ifdef CONFIG_IPMI_PROC_INTERFACE
2001static int smi_ipmb_proc_show(struct seq_file *m, void *v) 2368static int smi_ipmb_proc_show(struct seq_file *m, void *v)
2002{ 2369{
2003 ipmi_smi_t intf = m->private; 2370 ipmi_smi_t intf = m->private;
2004 int i; 2371 int i;
2005 2372
2006 seq_printf(m, "%x", intf->channels[0].address); 2373 seq_printf(m, "%x", intf->addrinfo[0].address);
2007 for (i = 1; i < IPMI_MAX_CHANNELS; i++) 2374 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
2008 seq_printf(m, " %x", intf->channels[i].address); 2375 seq_printf(m, " %x", intf->addrinfo[i].address);
2009 seq_putc(m, '\n'); 2376 seq_putc(m, '\n');
2010 2377
2011 return 0; 2378 return 0;
@@ -2026,10 +2393,16 @@ static const struct file_operations smi_ipmb_proc_ops = {
2026static int smi_version_proc_show(struct seq_file *m, void *v) 2393static int smi_version_proc_show(struct seq_file *m, void *v)
2027{ 2394{
2028 ipmi_smi_t intf = m->private; 2395 ipmi_smi_t intf = m->private;
2396 struct ipmi_device_id id;
2397 int rv;
2398
2399 rv = bmc_get_device_id(intf, NULL, &id, NULL, NULL);
2400 if (rv)
2401 return rv;
2029 2402
2030 seq_printf(m, "%u.%u\n", 2403 seq_printf(m, "%u.%u\n",
2031 ipmi_version_major(&intf->bmc->id), 2404 ipmi_version_major(&id),
2032 ipmi_version_minor(&intf->bmc->id)); 2405 ipmi_version_minor(&id));
2033 2406
2034 return 0; 2407 return 0;
2035} 2408}
@@ -2120,14 +2493,12 @@ static const struct file_operations smi_stats_proc_ops = {
2120 .llseek = seq_lseek, 2493 .llseek = seq_lseek,
2121 .release = single_release, 2494 .release = single_release,
2122}; 2495};
2123#endif /* CONFIG_PROC_FS */
2124 2496
2125int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name, 2497int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2126 const struct file_operations *proc_ops, 2498 const struct file_operations *proc_ops,
2127 void *data) 2499 void *data)
2128{ 2500{
2129 int rv = 0; 2501 int rv = 0;
2130#ifdef CONFIG_PROC_FS
2131 struct proc_dir_entry *file; 2502 struct proc_dir_entry *file;
2132 struct ipmi_proc_entry *entry; 2503 struct ipmi_proc_entry *entry;
2133 2504
@@ -2153,7 +2524,6 @@ int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
2153 smi->proc_entries = entry; 2524 smi->proc_entries = entry;
2154 mutex_unlock(&smi->proc_entry_lock); 2525 mutex_unlock(&smi->proc_entry_lock);
2155 } 2526 }
2156#endif /* CONFIG_PROC_FS */
2157 2527
2158 return rv; 2528 return rv;
2159} 2529}
@@ -2163,7 +2533,6 @@ static int add_proc_entries(ipmi_smi_t smi, int num)
2163{ 2533{
2164 int rv = 0; 2534 int rv = 0;
2165 2535
2166#ifdef CONFIG_PROC_FS
2167 sprintf(smi->proc_dir_name, "%d", num); 2536 sprintf(smi->proc_dir_name, "%d", num);
2168 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root); 2537 smi->proc_dir = proc_mkdir(smi->proc_dir_name, proc_ipmi_root);
2169 if (!smi->proc_dir) 2538 if (!smi->proc_dir)
@@ -2183,14 +2552,12 @@ static int add_proc_entries(ipmi_smi_t smi, int num)
2183 rv = ipmi_smi_add_proc_entry(smi, "version", 2552 rv = ipmi_smi_add_proc_entry(smi, "version",
2184 &smi_version_proc_ops, 2553 &smi_version_proc_ops,
2185 smi); 2554 smi);
2186#endif /* CONFIG_PROC_FS */
2187 2555
2188 return rv; 2556 return rv;
2189} 2557}
2190 2558
2191static void remove_proc_entries(ipmi_smi_t smi) 2559static void remove_proc_entries(ipmi_smi_t smi)
2192{ 2560{
2193#ifdef CONFIG_PROC_FS
2194 struct ipmi_proc_entry *entry; 2561 struct ipmi_proc_entry *entry;
2195 2562
2196 mutex_lock(&smi->proc_entry_lock); 2563 mutex_lock(&smi->proc_entry_lock);
@@ -2204,66 +2571,22 @@ static void remove_proc_entries(ipmi_smi_t smi)
2204 } 2571 }
2205 mutex_unlock(&smi->proc_entry_lock); 2572 mutex_unlock(&smi->proc_entry_lock);
2206 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root); 2573 remove_proc_entry(smi->proc_dir_name, proc_ipmi_root);
2207#endif /* CONFIG_PROC_FS */
2208}
2209
2210static int __find_bmc_guid(struct device *dev, void *data)
2211{
2212 unsigned char *id = data;
2213 struct bmc_device *bmc = to_bmc_device(dev);
2214 return memcmp(bmc->guid, id, 16) == 0;
2215}
2216
2217static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2218 unsigned char *guid)
2219{
2220 struct device *dev;
2221
2222 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2223 if (dev)
2224 return to_bmc_device(dev);
2225 else
2226 return NULL;
2227}
2228
2229struct prod_dev_id {
2230 unsigned int product_id;
2231 unsigned char device_id;
2232};
2233
2234static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2235{
2236 struct prod_dev_id *id = data;
2237 struct bmc_device *bmc = to_bmc_device(dev);
2238
2239 return (bmc->id.product_id == id->product_id
2240 && bmc->id.device_id == id->device_id);
2241}
2242
2243static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2244 struct device_driver *drv,
2245 unsigned int product_id, unsigned char device_id)
2246{
2247 struct prod_dev_id id = {
2248 .product_id = product_id,
2249 .device_id = device_id,
2250 };
2251 struct device *dev;
2252
2253 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2254 if (dev)
2255 return to_bmc_device(dev);
2256 else
2257 return NULL;
2258} 2574}
2575#endif /* CONFIG_IPMI_PROC_INTERFACE */
2259 2576
2260static ssize_t device_id_show(struct device *dev, 2577static ssize_t device_id_show(struct device *dev,
2261 struct device_attribute *attr, 2578 struct device_attribute *attr,
2262 char *buf) 2579 char *buf)
2263{ 2580{
2264 struct bmc_device *bmc = to_bmc_device(dev); 2581 struct bmc_device *bmc = to_bmc_device(dev);
2582 struct ipmi_device_id id;
2583 int rv;
2265 2584
2266 return snprintf(buf, 10, "%u\n", bmc->id.device_id); 2585 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2586 if (rv)
2587 return rv;
2588
2589 return snprintf(buf, 10, "%u\n", id.device_id);
2267} 2590}
2268static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL); 2591static DEVICE_ATTR(device_id, S_IRUGO, device_id_show, NULL);
2269 2592
@@ -2272,9 +2595,14 @@ static ssize_t provides_device_sdrs_show(struct device *dev,
2272 char *buf) 2595 char *buf)
2273{ 2596{
2274 struct bmc_device *bmc = to_bmc_device(dev); 2597 struct bmc_device *bmc = to_bmc_device(dev);
2598 struct ipmi_device_id id;
2599 int rv;
2275 2600
2276 return snprintf(buf, 10, "%u\n", 2601 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2277 (bmc->id.device_revision & 0x80) >> 7); 2602 if (rv)
2603 return rv;
2604
2605 return snprintf(buf, 10, "%u\n", (id.device_revision & 0x80) >> 7);
2278} 2606}
2279static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show, 2607static DEVICE_ATTR(provides_device_sdrs, S_IRUGO, provides_device_sdrs_show,
2280 NULL); 2608 NULL);
@@ -2283,9 +2611,14 @@ static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
2283 char *buf) 2611 char *buf)
2284{ 2612{
2285 struct bmc_device *bmc = to_bmc_device(dev); 2613 struct bmc_device *bmc = to_bmc_device(dev);
2614 struct ipmi_device_id id;
2615 int rv;
2616
2617 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2618 if (rv)
2619 return rv;
2286 2620
2287 return snprintf(buf, 20, "%u\n", 2621 return snprintf(buf, 20, "%u\n", id.device_revision & 0x0F);
2288 bmc->id.device_revision & 0x0F);
2289} 2622}
2290static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL); 2623static DEVICE_ATTR(revision, S_IRUGO, revision_show, NULL);
2291 2624
@@ -2294,9 +2627,15 @@ static ssize_t firmware_revision_show(struct device *dev,
2294 char *buf) 2627 char *buf)
2295{ 2628{
2296 struct bmc_device *bmc = to_bmc_device(dev); 2629 struct bmc_device *bmc = to_bmc_device(dev);
2630 struct ipmi_device_id id;
2631 int rv;
2632
2633 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2634 if (rv)
2635 return rv;
2297 2636
2298 return snprintf(buf, 20, "%u.%x\n", bmc->id.firmware_revision_1, 2637 return snprintf(buf, 20, "%u.%x\n", id.firmware_revision_1,
2299 bmc->id.firmware_revision_2); 2638 id.firmware_revision_2);
2300} 2639}
2301static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL); 2640static DEVICE_ATTR(firmware_revision, S_IRUGO, firmware_revision_show, NULL);
2302 2641
@@ -2305,10 +2644,16 @@ static ssize_t ipmi_version_show(struct device *dev,
2305 char *buf) 2644 char *buf)
2306{ 2645{
2307 struct bmc_device *bmc = to_bmc_device(dev); 2646 struct bmc_device *bmc = to_bmc_device(dev);
2647 struct ipmi_device_id id;
2648 int rv;
2649
2650 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2651 if (rv)
2652 return rv;
2308 2653
2309 return snprintf(buf, 20, "%u.%u\n", 2654 return snprintf(buf, 20, "%u.%u\n",
2310 ipmi_version_major(&bmc->id), 2655 ipmi_version_major(&id),
2311 ipmi_version_minor(&bmc->id)); 2656 ipmi_version_minor(&id));
2312} 2657}
2313static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL); 2658static DEVICE_ATTR(ipmi_version, S_IRUGO, ipmi_version_show, NULL);
2314 2659
@@ -2317,9 +2662,14 @@ static ssize_t add_dev_support_show(struct device *dev,
2317 char *buf) 2662 char *buf)
2318{ 2663{
2319 struct bmc_device *bmc = to_bmc_device(dev); 2664 struct bmc_device *bmc = to_bmc_device(dev);
2665 struct ipmi_device_id id;
2666 int rv;
2320 2667
2321 return snprintf(buf, 10, "0x%02x\n", 2668 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2322 bmc->id.additional_device_support); 2669 if (rv)
2670 return rv;
2671
2672 return snprintf(buf, 10, "0x%02x\n", id.additional_device_support);
2323} 2673}
2324static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show, 2674static DEVICE_ATTR(additional_device_support, S_IRUGO, add_dev_support_show,
2325 NULL); 2675 NULL);
@@ -2329,8 +2679,14 @@ static ssize_t manufacturer_id_show(struct device *dev,
2329 char *buf) 2679 char *buf)
2330{ 2680{
2331 struct bmc_device *bmc = to_bmc_device(dev); 2681 struct bmc_device *bmc = to_bmc_device(dev);
2682 struct ipmi_device_id id;
2683 int rv;
2684
2685 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2686 if (rv)
2687 return rv;
2332 2688
2333 return snprintf(buf, 20, "0x%6.6x\n", bmc->id.manufacturer_id); 2689 return snprintf(buf, 20, "0x%6.6x\n", id.manufacturer_id);
2334} 2690}
2335static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL); 2691static DEVICE_ATTR(manufacturer_id, S_IRUGO, manufacturer_id_show, NULL);
2336 2692
@@ -2339,8 +2695,14 @@ static ssize_t product_id_show(struct device *dev,
2339 char *buf) 2695 char *buf)
2340{ 2696{
2341 struct bmc_device *bmc = to_bmc_device(dev); 2697 struct bmc_device *bmc = to_bmc_device(dev);
2698 struct ipmi_device_id id;
2699 int rv;
2700
2701 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2702 if (rv)
2703 return rv;
2342 2704
2343 return snprintf(buf, 10, "0x%4.4x\n", bmc->id.product_id); 2705 return snprintf(buf, 10, "0x%4.4x\n", id.product_id);
2344} 2706}
2345static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL); 2707static DEVICE_ATTR(product_id, S_IRUGO, product_id_show, NULL);
2346 2708
@@ -2349,12 +2711,18 @@ static ssize_t aux_firmware_rev_show(struct device *dev,
2349 char *buf) 2711 char *buf)
2350{ 2712{
2351 struct bmc_device *bmc = to_bmc_device(dev); 2713 struct bmc_device *bmc = to_bmc_device(dev);
2714 struct ipmi_device_id id;
2715 int rv;
2716
2717 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2718 if (rv)
2719 return rv;
2352 2720
2353 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n", 2721 return snprintf(buf, 21, "0x%02x 0x%02x 0x%02x 0x%02x\n",
2354 bmc->id.aux_firmware_revision[3], 2722 id.aux_firmware_revision[3],
2355 bmc->id.aux_firmware_revision[2], 2723 id.aux_firmware_revision[2],
2356 bmc->id.aux_firmware_revision[1], 2724 id.aux_firmware_revision[1],
2357 bmc->id.aux_firmware_revision[0]); 2725 id.aux_firmware_revision[0]);
2358} 2726}
2359static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL); 2727static DEVICE_ATTR(aux_firmware_revision, S_IRUGO, aux_firmware_rev_show, NULL);
2360 2728
@@ -2362,10 +2730,17 @@ static ssize_t guid_show(struct device *dev, struct device_attribute *attr,
2362 char *buf) 2730 char *buf)
2363{ 2731{
2364 struct bmc_device *bmc = to_bmc_device(dev); 2732 struct bmc_device *bmc = to_bmc_device(dev);
2733 bool guid_set;
2734 guid_t guid;
2735 int rv;
2365 2736
2366 return snprintf(buf, 100, "%Lx%Lx\n", 2737 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, &guid);
2367 (long long) bmc->guid[0], 2738 if (rv)
2368 (long long) bmc->guid[8]); 2739 return rv;
2740 if (!guid_set)
2741 return -ENOENT;
2742
2743 return snprintf(buf, 38, "%pUl\n", guid.b);
2369} 2744}
2370static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL); 2745static DEVICE_ATTR(guid, S_IRUGO, guid_show, NULL);
2371 2746
@@ -2389,11 +2764,20 @@ static umode_t bmc_dev_attr_is_visible(struct kobject *kobj,
2389 struct device *dev = kobj_to_dev(kobj); 2764 struct device *dev = kobj_to_dev(kobj);
2390 struct bmc_device *bmc = to_bmc_device(dev); 2765 struct bmc_device *bmc = to_bmc_device(dev);
2391 umode_t mode = attr->mode; 2766 umode_t mode = attr->mode;
2767 int rv;
2392 2768
2393 if (attr == &dev_attr_aux_firmware_revision.attr) 2769 if (attr == &dev_attr_aux_firmware_revision.attr) {
2394 return bmc->id.aux_firmware_revision_set ? mode : 0; 2770 struct ipmi_device_id id;
2395 if (attr == &dev_attr_guid.attr) 2771
2396 return bmc->guid_set ? mode : 0; 2772 rv = bmc_get_device_id(NULL, bmc, &id, NULL, NULL);
2773 return (!rv && id.aux_firmware_revision_set) ? mode : 0;
2774 }
2775 if (attr == &dev_attr_guid.attr) {
2776 bool guid_set;
2777
2778 rv = bmc_get_device_id(NULL, bmc, NULL, &guid_set, NULL);
2779 return (!rv && guid_set) ? mode : 0;
2780 }
2397 return mode; 2781 return mode;
2398} 2782}
2399 2783
@@ -2411,127 +2795,239 @@ static const struct device_type bmc_device_type = {
2411 .groups = bmc_dev_attr_groups, 2795 .groups = bmc_dev_attr_groups,
2412}; 2796};
2413 2797
2798static int __find_bmc_guid(struct device *dev, void *data)
2799{
2800 guid_t *guid = data;
2801 struct bmc_device *bmc;
2802 int rv;
2803
2804 if (dev->type != &bmc_device_type)
2805 return 0;
2806
2807 bmc = to_bmc_device(dev);
2808 rv = bmc->dyn_guid_set && guid_equal(&bmc->guid, guid);
2809 if (rv)
2810 rv = kref_get_unless_zero(&bmc->usecount);
2811 return rv;
2812}
2813
2814/*
2815 * Returns with the bmc's usecount incremented, if it is non-NULL.
2816 */
2817static struct bmc_device *ipmi_find_bmc_guid(struct device_driver *drv,
2818 guid_t *guid)
2819{
2820 struct device *dev;
2821 struct bmc_device *bmc = NULL;
2822
2823 dev = driver_find_device(drv, NULL, guid, __find_bmc_guid);
2824 if (dev) {
2825 bmc = to_bmc_device(dev);
2826 put_device(dev);
2827 }
2828 return bmc;
2829}
2830
2831struct prod_dev_id {
2832 unsigned int product_id;
2833 unsigned char device_id;
2834};
2835
2836static int __find_bmc_prod_dev_id(struct device *dev, void *data)
2837{
2838 struct prod_dev_id *cid = data;
2839 struct bmc_device *bmc;
2840 int rv;
2841
2842 if (dev->type != &bmc_device_type)
2843 return 0;
2844
2845 bmc = to_bmc_device(dev);
2846 rv = (bmc->id.product_id == cid->product_id
2847 && bmc->id.device_id == cid->device_id);
2848 if (rv)
2849 rv = kref_get_unless_zero(&bmc->usecount);
2850 return rv;
2851}
2852
2853/*
2854 * Returns with the bmc's usecount incremented, if it is non-NULL.
2855 */
2856static struct bmc_device *ipmi_find_bmc_prod_dev_id(
2857 struct device_driver *drv,
2858 unsigned int product_id, unsigned char device_id)
2859{
2860 struct prod_dev_id id = {
2861 .product_id = product_id,
2862 .device_id = device_id,
2863 };
2864 struct device *dev;
2865 struct bmc_device *bmc = NULL;
2866
2867 dev = driver_find_device(drv, NULL, &id, __find_bmc_prod_dev_id);
2868 if (dev) {
2869 bmc = to_bmc_device(dev);
2870 put_device(dev);
2871 }
2872 return bmc;
2873}
2874
2875static DEFINE_IDA(ipmi_bmc_ida);
2876
2414static void 2877static void
2415release_bmc_device(struct device *dev) 2878release_bmc_device(struct device *dev)
2416{ 2879{
2417 kfree(to_bmc_device(dev)); 2880 kfree(to_bmc_device(dev));
2418} 2881}
2419 2882
2883static void cleanup_bmc_work(struct work_struct *work)
2884{
2885 struct bmc_device *bmc = container_of(work, struct bmc_device,
2886 remove_work);
2887 int id = bmc->pdev.id; /* Unregister overwrites id */
2888
2889 platform_device_unregister(&bmc->pdev);
2890 ida_simple_remove(&ipmi_bmc_ida, id);
2891}
2892
2420static void 2893static void
2421cleanup_bmc_device(struct kref *ref) 2894cleanup_bmc_device(struct kref *ref)
2422{ 2895{
2423 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount); 2896 struct bmc_device *bmc = container_of(ref, struct bmc_device, usecount);
2424 2897
2425 platform_device_unregister(&bmc->pdev); 2898 /*
2899 * Remove the platform device in a work queue to avoid issues
2900 * with removing the device attributes while reading a device
2901 * attribute.
2902 */
2903 schedule_work(&bmc->remove_work);
2426} 2904}
2427 2905
2428static void ipmi_bmc_unregister(ipmi_smi_t intf) 2906/*
2907 * Must be called with intf->bmc_reg_mutex held.
2908 */
2909static void __ipmi_bmc_unregister(ipmi_smi_t intf)
2429{ 2910{
2430 struct bmc_device *bmc = intf->bmc; 2911 struct bmc_device *bmc = intf->bmc;
2431 2912
2432 sysfs_remove_link(&intf->si_dev->kobj, "bmc"); 2913 if (!intf->bmc_registered)
2433 if (intf->my_dev_name) { 2914 return;
2434 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2435 kfree(intf->my_dev_name);
2436 intf->my_dev_name = NULL;
2437 }
2438 2915
2439 mutex_lock(&ipmidriver_mutex); 2916 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
2917 sysfs_remove_link(&bmc->pdev.dev.kobj, intf->my_dev_name);
2918 kfree(intf->my_dev_name);
2919 intf->my_dev_name = NULL;
2920
2921 mutex_lock(&bmc->dyn_mutex);
2922 list_del(&intf->bmc_link);
2923 mutex_unlock(&bmc->dyn_mutex);
2924 intf->bmc = &intf->tmp_bmc;
2440 kref_put(&bmc->usecount, cleanup_bmc_device); 2925 kref_put(&bmc->usecount, cleanup_bmc_device);
2441 intf->bmc = NULL; 2926 intf->bmc_registered = false;
2442 mutex_unlock(&ipmidriver_mutex);
2443} 2927}
2444 2928
2445static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum) 2929static void ipmi_bmc_unregister(ipmi_smi_t intf)
2930{
2931 mutex_lock(&intf->bmc_reg_mutex);
2932 __ipmi_bmc_unregister(intf);
2933 mutex_unlock(&intf->bmc_reg_mutex);
2934}
2935
2936/*
2937 * Must be called with intf->bmc_reg_mutex held.
2938 */
2939static int __ipmi_bmc_register(ipmi_smi_t intf,
2940 struct ipmi_device_id *id,
2941 bool guid_set, guid_t *guid, int intf_num)
2446{ 2942{
2447 int rv; 2943 int rv;
2448 struct bmc_device *bmc = intf->bmc; 2944 struct bmc_device *bmc;
2449 struct bmc_device *old_bmc; 2945 struct bmc_device *old_bmc;
2450 2946
2451 mutex_lock(&ipmidriver_mutex); 2947 /*
2948 * platform_device_register() can cause bmc_reg_mutex to
2949 * be claimed because of the is_visible functions of
2950 * the attributes. Eliminate possible recursion and
2951 * release the lock.
2952 */
2953 intf->in_bmc_register = true;
2954 mutex_unlock(&intf->bmc_reg_mutex);
2452 2955
2453 /* 2956 /*
2454 * Try to find if there is an bmc_device struct 2957 * Try to find if there is an bmc_device struct
2455 * representing the interfaced BMC already 2958 * representing the interfaced BMC already
2456 */ 2959 */
2457 if (bmc->guid_set) 2960 mutex_lock(&ipmidriver_mutex);
2458 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, bmc->guid); 2961 if (guid_set)
2962 old_bmc = ipmi_find_bmc_guid(&ipmidriver.driver, guid);
2459 else 2963 else
2460 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver, 2964 old_bmc = ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2461 bmc->id.product_id, 2965 id->product_id,
2462 bmc->id.device_id); 2966 id->device_id);
2463 2967
2464 /* 2968 /*
2465 * If there is already an bmc_device, free the new one, 2969 * If there is already an bmc_device, free the new one,
2466 * otherwise register the new BMC device 2970 * otherwise register the new BMC device
2467 */ 2971 */
2468 if (old_bmc) { 2972 if (old_bmc) {
2469 kfree(bmc);
2470 intf->bmc = old_bmc;
2471 bmc = old_bmc; 2973 bmc = old_bmc;
2974 /*
2975 * Note: old_bmc already has usecount incremented by
2976 * the BMC find functions.
2977 */
2978 intf->bmc = old_bmc;
2979 mutex_lock(&bmc->dyn_mutex);
2980 list_add_tail(&intf->bmc_link, &bmc->intfs);
2981 mutex_unlock(&bmc->dyn_mutex);
2472 2982
2473 kref_get(&bmc->usecount); 2983 dev_info(intf->si_dev,
2474 mutex_unlock(&ipmidriver_mutex); 2984 "ipmi: interfacing existing BMC (man_id: 0x%6.6x,"
2475 2985 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2476 printk(KERN_INFO 2986 bmc->id.manufacturer_id,
2477 "ipmi: interfacing existing BMC (man_id: 0x%6.6x," 2987 bmc->id.product_id,
2478 " prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", 2988 bmc->id.device_id);
2479 bmc->id.manufacturer_id,
2480 bmc->id.product_id,
2481 bmc->id.device_id);
2482 } else { 2989 } else {
2483 unsigned char orig_dev_id = bmc->id.device_id; 2990 bmc = kzalloc(sizeof(*bmc), GFP_KERNEL);
2484 int warn_printed = 0; 2991 if (!bmc) {
2485 2992 rv = -ENOMEM;
2486 snprintf(bmc->name, sizeof(bmc->name), 2993 goto out;
2487 "ipmi_bmc.%4.4x", bmc->id.product_id);
2488 bmc->pdev.name = bmc->name;
2489
2490 while (ipmi_find_bmc_prod_dev_id(&ipmidriver.driver,
2491 bmc->id.product_id,
2492 bmc->id.device_id)) {
2493 if (!warn_printed) {
2494 printk(KERN_WARNING PFX
2495 "This machine has two different BMCs"
2496 " with the same product id and device"
2497 " id. This is an error in the"
2498 " firmware, but incrementing the"
2499 " device id to work around the problem."
2500 " Prod ID = 0x%x, Dev ID = 0x%x\n",
2501 bmc->id.product_id, bmc->id.device_id);
2502 warn_printed = 1;
2503 }
2504 bmc->id.device_id++; /* Wraps at 255 */
2505 if (bmc->id.device_id == orig_dev_id) {
2506 printk(KERN_ERR PFX
2507 "Out of device ids!\n");
2508 break;
2509 }
2510 } 2994 }
2995 INIT_LIST_HEAD(&bmc->intfs);
2996 mutex_init(&bmc->dyn_mutex);
2997 INIT_WORK(&bmc->remove_work, cleanup_bmc_work);
2998
2999 bmc->id = *id;
3000 bmc->dyn_id_set = 1;
3001 bmc->dyn_guid_set = guid_set;
3002 bmc->guid = *guid;
3003 bmc->dyn_id_expiry = jiffies + IPMI_DYN_DEV_ID_EXPIRY;
3004
3005 bmc->pdev.name = "ipmi_bmc";
2511 3006
3007 rv = ida_simple_get(&ipmi_bmc_ida, 0, 0, GFP_KERNEL);
3008 if (rv < 0)
3009 goto out;
2512 bmc->pdev.dev.driver = &ipmidriver.driver; 3010 bmc->pdev.dev.driver = &ipmidriver.driver;
2513 bmc->pdev.id = bmc->id.device_id; 3011 bmc->pdev.id = rv;
2514 bmc->pdev.dev.release = release_bmc_device; 3012 bmc->pdev.dev.release = release_bmc_device;
2515 bmc->pdev.dev.type = &bmc_device_type; 3013 bmc->pdev.dev.type = &bmc_device_type;
2516 kref_init(&bmc->usecount); 3014 kref_init(&bmc->usecount);
2517 3015
3016 intf->bmc = bmc;
3017 mutex_lock(&bmc->dyn_mutex);
3018 list_add_tail(&intf->bmc_link, &bmc->intfs);
3019 mutex_unlock(&bmc->dyn_mutex);
3020
2518 rv = platform_device_register(&bmc->pdev); 3021 rv = platform_device_register(&bmc->pdev);
2519 mutex_unlock(&ipmidriver_mutex);
2520 if (rv) { 3022 if (rv) {
2521 put_device(&bmc->pdev.dev); 3023 dev_err(intf->si_dev,
2522 printk(KERN_ERR 3024 PFX " Unable to register bmc device: %d\n",
2523 "ipmi_msghandler:" 3025 rv);
2524 " Unable to register bmc device: %d\n", 3026 goto out_list_del;
2525 rv);
2526 /*
2527 * Don't go to out_err, you can only do that if
2528 * the device is registered already.
2529 */
2530 return rv;
2531 } 3027 }
2532 3028
2533 dev_info(intf->si_dev, "Found new BMC (man_id: 0x%6.6x, " 3029 dev_info(intf->si_dev,
2534 "prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n", 3030 "Found new BMC (man_id: 0x%6.6x, prod_id: 0x%4.4x, dev_id: 0x%2.2x)\n",
2535 bmc->id.manufacturer_id, 3031 bmc->id.manufacturer_id,
2536 bmc->id.product_id, 3032 bmc->id.product_id,
2537 bmc->id.device_id); 3033 bmc->id.device_id);
@@ -2543,19 +3039,19 @@ static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
2543 */ 3039 */
2544 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc"); 3040 rv = sysfs_create_link(&intf->si_dev->kobj, &bmc->pdev.dev.kobj, "bmc");
2545 if (rv) { 3041 if (rv) {
2546 printk(KERN_ERR 3042 dev_err(intf->si_dev,
2547 "ipmi_msghandler: Unable to create bmc symlink: %d\n", 3043 PFX "Unable to create bmc symlink: %d\n", rv);
2548 rv); 3044 goto out_put_bmc;
2549 goto out_err;
2550 } 3045 }
2551 3046
2552 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", ifnum); 3047 if (intf_num == -1)
3048 intf_num = intf->intf_num;
3049 intf->my_dev_name = kasprintf(GFP_KERNEL, "ipmi%d", intf_num);
2553 if (!intf->my_dev_name) { 3050 if (!intf->my_dev_name) {
2554 rv = -ENOMEM; 3051 rv = -ENOMEM;
2555 printk(KERN_ERR 3052 dev_err(intf->si_dev,
2556 "ipmi_msghandler: allocate link from BMC: %d\n", 3053 PFX "Unable to allocate link from BMC: %d\n", rv);
2557 rv); 3054 goto out_unlink1;
2558 goto out_err;
2559 } 3055 }
2560 3056
2561 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj, 3057 rv = sysfs_create_link(&bmc->pdev.dev.kobj, &intf->si_dev->kobj,
@@ -2563,18 +3059,42 @@ static int ipmi_bmc_register(ipmi_smi_t intf, int ifnum)
2563 if (rv) { 3059 if (rv) {
2564 kfree(intf->my_dev_name); 3060 kfree(intf->my_dev_name);
2565 intf->my_dev_name = NULL; 3061 intf->my_dev_name = NULL;
2566 printk(KERN_ERR 3062 dev_err(intf->si_dev,
2567 "ipmi_msghandler:" 3063 PFX "Unable to create symlink to bmc: %d\n", rv);
2568 " Unable to create symlink to bmc: %d\n", 3064 goto out_free_my_dev_name;
2569 rv);
2570 goto out_err;
2571 } 3065 }
2572 3066
2573 return 0; 3067 intf->bmc_registered = true;
2574 3068
2575out_err: 3069out:
2576 ipmi_bmc_unregister(intf); 3070 mutex_unlock(&ipmidriver_mutex);
3071 mutex_lock(&intf->bmc_reg_mutex);
3072 intf->in_bmc_register = false;
2577 return rv; 3073 return rv;
3074
3075
3076out_free_my_dev_name:
3077 kfree(intf->my_dev_name);
3078 intf->my_dev_name = NULL;
3079
3080out_unlink1:
3081 sysfs_remove_link(&intf->si_dev->kobj, "bmc");
3082
3083out_put_bmc:
3084 mutex_lock(&bmc->dyn_mutex);
3085 list_del(&intf->bmc_link);
3086 mutex_unlock(&bmc->dyn_mutex);
3087 intf->bmc = &intf->tmp_bmc;
3088 kref_put(&bmc->usecount, cleanup_bmc_device);
3089 goto out;
3090
3091out_list_del:
3092 mutex_lock(&bmc->dyn_mutex);
3093 list_del(&intf->bmc_link);
3094 mutex_unlock(&bmc->dyn_mutex);
3095 intf->bmc = &intf->tmp_bmc;
3096 put_device(&bmc->pdev.dev);
3097 goto out;
2578} 3098}
2579 3099
2580static int 3100static int
@@ -2600,14 +3120,15 @@ send_guid_cmd(ipmi_smi_t intf, int chan)
2600 NULL, 3120 NULL,
2601 NULL, 3121 NULL,
2602 0, 3122 0,
2603 intf->channels[0].address, 3123 intf->addrinfo[0].address,
2604 intf->channels[0].lun, 3124 intf->addrinfo[0].lun,
2605 -1, 0); 3125 -1, 0);
2606} 3126}
2607 3127
2608static void 3128static void guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2609guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2610{ 3129{
3130 struct bmc_device *bmc = intf->bmc;
3131
2611 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 3132 if ((msg->addr.addr_type != IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2612 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE) 3133 || (msg->msg.netfn != IPMI_NETFN_APP_RESPONSE)
2613 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD)) 3134 || (msg->msg.cmd != IPMI_GET_DEVICE_GUID_CMD))
@@ -2616,38 +3137,46 @@ guid_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2616 3137
2617 if (msg->msg.data[0] != 0) { 3138 if (msg->msg.data[0] != 0) {
2618 /* Error from getting the GUID, the BMC doesn't have one. */ 3139 /* Error from getting the GUID, the BMC doesn't have one. */
2619 intf->bmc->guid_set = 0; 3140 bmc->dyn_guid_set = 0;
2620 goto out; 3141 goto out;
2621 } 3142 }
2622 3143
2623 if (msg->msg.data_len < 17) { 3144 if (msg->msg.data_len < 17) {
2624 intf->bmc->guid_set = 0; 3145 bmc->dyn_guid_set = 0;
2625 printk(KERN_WARNING PFX 3146 dev_warn(intf->si_dev,
2626 "guid_handler: The GUID response from the BMC was too" 3147 PFX "The GUID response from the BMC was too short, it was %d but should have been 17. Assuming GUID is not available.\n",
2627 " short, it was %d but should have been 17. Assuming" 3148 msg->msg.data_len);
2628 " GUID is not available.\n",
2629 msg->msg.data_len);
2630 goto out; 3149 goto out;
2631 } 3150 }
2632 3151
2633 memcpy(intf->bmc->guid, msg->msg.data, 16); 3152 memcpy(bmc->fetch_guid.b, msg->msg.data + 1, 16);
2634 intf->bmc->guid_set = 1; 3153 /*
3154 * Make sure the guid data is available before setting
3155 * dyn_guid_set.
3156 */
3157 smp_wmb();
3158 bmc->dyn_guid_set = 1;
2635 out: 3159 out:
2636 wake_up(&intf->waitq); 3160 wake_up(&intf->waitq);
2637} 3161}
2638 3162
2639static void 3163static void __get_guid(ipmi_smi_t intf)
2640get_guid(ipmi_smi_t intf)
2641{ 3164{
2642 int rv; 3165 int rv;
3166 struct bmc_device *bmc = intf->bmc;
2643 3167
2644 intf->bmc->guid_set = 0x2; 3168 bmc->dyn_guid_set = 2;
2645 intf->null_user_handler = guid_handler; 3169 intf->null_user_handler = guid_handler;
2646 rv = send_guid_cmd(intf, 0); 3170 rv = send_guid_cmd(intf, 0);
2647 if (rv) 3171 if (rv)
2648 /* Send failed, no GUID available. */ 3172 /* Send failed, no GUID available. */
2649 intf->bmc->guid_set = 0; 3173 bmc->dyn_guid_set = 0;
2650 wait_event(intf->waitq, intf->bmc->guid_set != 2); 3174
3175 wait_event(intf->waitq, bmc->dyn_guid_set != 2);
3176
3177 /* dyn_guid_set makes the guid data available. */
3178 smp_rmb();
3179
2651 intf->null_user_handler = NULL; 3180 intf->null_user_handler = NULL;
2652} 3181}
2653 3182
@@ -2676,8 +3205,8 @@ send_channel_info_cmd(ipmi_smi_t intf, int chan)
2676 NULL, 3205 NULL,
2677 NULL, 3206 NULL,
2678 0, 3207 0,
2679 intf->channels[0].address, 3208 intf->addrinfo[0].address,
2680 intf->channels[0].lun, 3209 intf->addrinfo[0].lun,
2681 -1, 0); 3210 -1, 0);
2682} 3211}
2683 3212
@@ -2685,7 +3214,9 @@ static void
2685channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg) 3214channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2686{ 3215{
2687 int rv = 0; 3216 int rv = 0;
2688 int chan; 3217 int ch;
3218 unsigned int set = intf->curr_working_cset;
3219 struct ipmi_channel *chans;
2689 3220
2690 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 3221 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
2691 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE) 3222 && (msg->msg.netfn == IPMI_NETFN_APP_RESPONSE)
@@ -2701,12 +3232,13 @@ channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2701 * assume it has one IPMB at channel 3232 * assume it has one IPMB at channel
2702 * zero. 3233 * zero.
2703 */ 3234 */
2704 intf->channels[0].medium 3235 intf->wchannels[set].c[0].medium
2705 = IPMI_CHANNEL_MEDIUM_IPMB; 3236 = IPMI_CHANNEL_MEDIUM_IPMB;
2706 intf->channels[0].protocol 3237 intf->wchannels[set].c[0].protocol
2707 = IPMI_CHANNEL_PROTOCOL_IPMB; 3238 = IPMI_CHANNEL_PROTOCOL_IPMB;
2708 3239
2709 intf->curr_channel = IPMI_MAX_CHANNELS; 3240 intf->channel_list = intf->wchannels + set;
3241 intf->channels_ready = true;
2710 wake_up(&intf->waitq); 3242 wake_up(&intf->waitq);
2711 goto out; 3243 goto out;
2712 } 3244 }
@@ -2716,24 +3248,31 @@ channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2716 /* Message not big enough, just go on. */ 3248 /* Message not big enough, just go on. */
2717 goto next_channel; 3249 goto next_channel;
2718 } 3250 }
2719 chan = intf->curr_channel; 3251 ch = intf->curr_channel;
2720 intf->channels[chan].medium = msg->msg.data[2] & 0x7f; 3252 chans = intf->wchannels[set].c;
2721 intf->channels[chan].protocol = msg->msg.data[3] & 0x1f; 3253 chans[ch].medium = msg->msg.data[2] & 0x7f;
3254 chans[ch].protocol = msg->msg.data[3] & 0x1f;
2722 3255
2723 next_channel: 3256 next_channel:
2724 intf->curr_channel++; 3257 intf->curr_channel++;
2725 if (intf->curr_channel >= IPMI_MAX_CHANNELS) 3258 if (intf->curr_channel >= IPMI_MAX_CHANNELS) {
3259 intf->channel_list = intf->wchannels + set;
3260 intf->channels_ready = true;
2726 wake_up(&intf->waitq); 3261 wake_up(&intf->waitq);
2727 else 3262 } else {
3263 intf->channel_list = intf->wchannels + set;
3264 intf->channels_ready = true;
2728 rv = send_channel_info_cmd(intf, intf->curr_channel); 3265 rv = send_channel_info_cmd(intf, intf->curr_channel);
3266 }
2729 3267
2730 if (rv) { 3268 if (rv) {
2731 /* Got an error somehow, just give up. */ 3269 /* Got an error somehow, just give up. */
2732 printk(KERN_WARNING PFX 3270 dev_warn(intf->si_dev,
2733 "Error sending channel information for channel" 3271 PFX "Error sending channel information for channel %d: %d\n",
2734 " %d: %d\n", intf->curr_channel, rv); 3272 intf->curr_channel, rv);
2735 3273
2736 intf->curr_channel = IPMI_MAX_CHANNELS; 3274 intf->channel_list = intf->wchannels + set;
3275 intf->channels_ready = true;
2737 wake_up(&intf->waitq); 3276 wake_up(&intf->waitq);
2738 } 3277 }
2739 } 3278 }
@@ -2741,6 +3280,53 @@ channel_handler(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
2741 return; 3280 return;
2742} 3281}
2743 3282
3283/*
3284 * Must be holding intf->bmc_reg_mutex to call this.
3285 */
3286static int __scan_channels(ipmi_smi_t intf, struct ipmi_device_id *id)
3287{
3288 int rv;
3289
3290 if (ipmi_version_major(id) > 1
3291 || (ipmi_version_major(id) == 1
3292 && ipmi_version_minor(id) >= 5)) {
3293 unsigned int set;
3294
3295 /*
3296 * Start scanning the channels to see what is
3297 * available.
3298 */
3299 set = !intf->curr_working_cset;
3300 intf->curr_working_cset = set;
3301 memset(&intf->wchannels[set], 0,
3302 sizeof(struct ipmi_channel_set));
3303
3304 intf->null_user_handler = channel_handler;
3305 intf->curr_channel = 0;
3306 rv = send_channel_info_cmd(intf, 0);
3307 if (rv) {
3308 dev_warn(intf->si_dev,
3309 "Error sending channel information for channel 0, %d\n",
3310 rv);
3311 return -EIO;
3312 }
3313
3314 /* Wait for the channel info to be read. */
3315 wait_event(intf->waitq, intf->channels_ready);
3316 intf->null_user_handler = NULL;
3317 } else {
3318 unsigned int set = intf->curr_working_cset;
3319
3320 /* Assume a single IPMB channel at zero. */
3321 intf->wchannels[set].c[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
3322 intf->wchannels[set].c[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
3323 intf->channel_list = intf->wchannels + set;
3324 intf->channels_ready = true;
3325 }
3326
3327 return 0;
3328}
3329
2744static void ipmi_poll(ipmi_smi_t intf) 3330static void ipmi_poll(ipmi_smi_t intf)
2745{ 3331{
2746 if (intf->handlers->poll) 3332 if (intf->handlers->poll)
@@ -2755,9 +3341,18 @@ void ipmi_poll_interface(ipmi_user_t user)
2755} 3341}
2756EXPORT_SYMBOL(ipmi_poll_interface); 3342EXPORT_SYMBOL(ipmi_poll_interface);
2757 3343
3344static void redo_bmc_reg(struct work_struct *work)
3345{
3346 ipmi_smi_t intf = container_of(work, struct ipmi_smi, bmc_reg_work);
3347
3348 if (!intf->in_shutdown)
3349 bmc_get_device_id(intf, NULL, NULL, NULL, NULL);
3350
3351 kref_put(&intf->refcount, intf_free);
3352}
3353
2758int ipmi_register_smi(const struct ipmi_smi_handlers *handlers, 3354int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2759 void *send_info, 3355 void *send_info,
2760 struct ipmi_device_id *device_id,
2761 struct device *si_dev, 3356 struct device *si_dev,
2762 unsigned char slave_addr) 3357 unsigned char slave_addr)
2763{ 3358{
@@ -2766,6 +3361,7 @@ int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2766 ipmi_smi_t intf; 3361 ipmi_smi_t intf;
2767 ipmi_smi_t tintf; 3362 ipmi_smi_t tintf;
2768 struct list_head *link; 3363 struct list_head *link;
3364 struct ipmi_device_id id;
2769 3365
2770 /* 3366 /*
2771 * Make sure the driver is actually initialized, this handles 3367 * Make sure the driver is actually initialized, this handles
@@ -2787,24 +3383,21 @@ int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2787 if (!intf) 3383 if (!intf)
2788 return -ENOMEM; 3384 return -ENOMEM;
2789 3385
2790 intf->ipmi_version_major = ipmi_version_major(device_id); 3386 intf->bmc = &intf->tmp_bmc;
2791 intf->ipmi_version_minor = ipmi_version_minor(device_id); 3387 INIT_LIST_HEAD(&intf->bmc->intfs);
2792 3388 mutex_init(&intf->bmc->dyn_mutex);
2793 intf->bmc = kzalloc(sizeof(*intf->bmc), GFP_KERNEL); 3389 INIT_LIST_HEAD(&intf->bmc_link);
2794 if (!intf->bmc) { 3390 mutex_init(&intf->bmc_reg_mutex);
2795 kfree(intf);
2796 return -ENOMEM;
2797 }
2798 intf->intf_num = -1; /* Mark it invalid for now. */ 3391 intf->intf_num = -1; /* Mark it invalid for now. */
2799 kref_init(&intf->refcount); 3392 kref_init(&intf->refcount);
2800 intf->bmc->id = *device_id; 3393 INIT_WORK(&intf->bmc_reg_work, redo_bmc_reg);
2801 intf->si_dev = si_dev; 3394 intf->si_dev = si_dev;
2802 for (j = 0; j < IPMI_MAX_CHANNELS; j++) { 3395 for (j = 0; j < IPMI_MAX_CHANNELS; j++) {
2803 intf->channels[j].address = IPMI_BMC_SLAVE_ADDR; 3396 intf->addrinfo[j].address = IPMI_BMC_SLAVE_ADDR;
2804 intf->channels[j].lun = 2; 3397 intf->addrinfo[j].lun = 2;
2805 } 3398 }
2806 if (slave_addr != 0) 3399 if (slave_addr != 0)
2807 intf->channels[0].address = slave_addr; 3400 intf->addrinfo[0].address = slave_addr;
2808 INIT_LIST_HEAD(&intf->users); 3401 INIT_LIST_HEAD(&intf->users);
2809 intf->handlers = handlers; 3402 intf->handlers = handlers;
2810 intf->send_info = send_info; 3403 intf->send_info = send_info;
@@ -2814,7 +3407,7 @@ int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2814 intf->seq_table[j].seqid = 0; 3407 intf->seq_table[j].seqid = 0;
2815 } 3408 }
2816 intf->curr_seq = 0; 3409 intf->curr_seq = 0;
2817#ifdef CONFIG_PROC_FS 3410#ifdef CONFIG_IPMI_PROC_INTERFACE
2818 mutex_init(&intf->proc_entry_lock); 3411 mutex_init(&intf->proc_entry_lock);
2819#endif 3412#endif
2820 spin_lock_init(&intf->waiting_rcv_msgs_lock); 3413 spin_lock_init(&intf->waiting_rcv_msgs_lock);
@@ -2838,7 +3431,9 @@ int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2838 for (i = 0; i < IPMI_NUM_STATS; i++) 3431 for (i = 0; i < IPMI_NUM_STATS; i++)
2839 atomic_set(&intf->stats[i], 0); 3432 atomic_set(&intf->stats[i], 0);
2840 3433
3434#ifdef CONFIG_IPMI_PROC_INTERFACE
2841 intf->proc_dir = NULL; 3435 intf->proc_dir = NULL;
3436#endif
2842 3437
2843 mutex_lock(&smi_watchers_mutex); 3438 mutex_lock(&smi_watchers_mutex);
2844 mutex_lock(&ipmi_interfaces_mutex); 3439 mutex_lock(&ipmi_interfaces_mutex);
@@ -2862,45 +3457,29 @@ int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
2862 if (rv) 3457 if (rv)
2863 goto out; 3458 goto out;
2864 3459
2865 get_guid(intf); 3460 rv = __bmc_get_device_id(intf, NULL, &id, NULL, NULL, i);
2866 3461 if (rv) {
2867 if ((intf->ipmi_version_major > 1) 3462 dev_err(si_dev, "Unable to get the device id: %d\n", rv);
2868 || ((intf->ipmi_version_major == 1) 3463 goto out;
2869 && (intf->ipmi_version_minor >= 5))) {
2870 /*
2871 * Start scanning the channels to see what is
2872 * available.
2873 */
2874 intf->null_user_handler = channel_handler;
2875 intf->curr_channel = 0;
2876 rv = send_channel_info_cmd(intf, 0);
2877 if (rv) {
2878 printk(KERN_WARNING PFX
2879 "Error sending channel information for channel"
2880 " 0, %d\n", rv);
2881 goto out;
2882 }
2883
2884 /* Wait for the channel info to be read. */
2885 wait_event(intf->waitq,
2886 intf->curr_channel >= IPMI_MAX_CHANNELS);
2887 intf->null_user_handler = NULL;
2888 } else {
2889 /* Assume a single IPMB channel at zero. */
2890 intf->channels[0].medium = IPMI_CHANNEL_MEDIUM_IPMB;
2891 intf->channels[0].protocol = IPMI_CHANNEL_PROTOCOL_IPMB;
2892 intf->curr_channel = IPMI_MAX_CHANNELS;
2893 } 3464 }
2894 3465
2895 rv = ipmi_bmc_register(intf, i); 3466 mutex_lock(&intf->bmc_reg_mutex);
3467 rv = __scan_channels(intf, &id);
3468 mutex_unlock(&intf->bmc_reg_mutex);
3469 if (rv)
3470 goto out;
2896 3471
2897 if (rv == 0) 3472#ifdef CONFIG_IPMI_PROC_INTERFACE
2898 rv = add_proc_entries(intf, i); 3473 rv = add_proc_entries(intf, i);
3474#endif
2899 3475
2900 out: 3476 out:
2901 if (rv) { 3477 if (rv) {
3478 ipmi_bmc_unregister(intf);
3479#ifdef CONFIG_IPMI_PROC_INTERFACE
2902 if (intf->proc_dir) 3480 if (intf->proc_dir)
2903 remove_proc_entries(intf); 3481 remove_proc_entries(intf);
3482#endif
2904 intf->handlers = NULL; 3483 intf->handlers = NULL;
2905 list_del_rcu(&intf->link); 3484 list_del_rcu(&intf->link);
2906 mutex_unlock(&ipmi_interfaces_mutex); 3485 mutex_unlock(&ipmi_interfaces_mutex);
@@ -3005,7 +3584,9 @@ int ipmi_unregister_smi(ipmi_smi_t intf)
3005 intf->handlers = NULL; 3584 intf->handlers = NULL;
3006 mutex_unlock(&ipmi_interfaces_mutex); 3585 mutex_unlock(&ipmi_interfaces_mutex);
3007 3586
3587#ifdef CONFIG_IPMI_PROC_INTERFACE
3008 remove_proc_entries(intf); 3588 remove_proc_entries(intf);
3589#endif
3009 ipmi_bmc_unregister(intf); 3590 ipmi_bmc_unregister(intf);
3010 3591
3011 /* 3592 /*
@@ -3130,7 +3711,7 @@ static int handle_ipmb_get_msg_cmd(ipmi_smi_t intf,
3130 msg->data[3] = msg->rsp[6]; 3711 msg->data[3] = msg->rsp[6];
3131 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3); 3712 msg->data[4] = ((netfn + 1) << 2) | (msg->rsp[7] & 0x3);
3132 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2); 3713 msg->data[5] = ipmb_checksum(&(msg->data[3]), 2);
3133 msg->data[6] = intf->channels[msg->rsp[3] & 0xf].address; 3714 msg->data[6] = intf->addrinfo[msg->rsp[3] & 0xf].address;
3134 /* rqseq/lun */ 3715 /* rqseq/lun */
3135 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3); 3716 msg->data[7] = (msg->rsp[7] & 0xfc) | (msg->rsp[4] & 0x3);
3136 msg->data[8] = msg->rsp[8]; /* cmd */ 3717 msg->data[8] = msg->rsp[8]; /* cmd */
@@ -3584,8 +4165,8 @@ static int handle_read_event_rsp(ipmi_smi_t intf,
3584 * There's too many things in the queue, discard this 4165 * There's too many things in the queue, discard this
3585 * message. 4166 * message.
3586 */ 4167 */
3587 printk(KERN_WARNING PFX "Event queue full, discarding" 4168 dev_warn(intf->si_dev,
3588 " incoming events\n"); 4169 PFX "Event queue full, discarding incoming events\n");
3589 intf->event_msg_printed = 1; 4170 intf->event_msg_printed = 1;
3590 } 4171 }
3591 4172
@@ -3603,11 +4184,8 @@ static int handle_bmc_rsp(ipmi_smi_t intf,
3603 4184
3604 recv_msg = (struct ipmi_recv_msg *) msg->user_data; 4185 recv_msg = (struct ipmi_recv_msg *) msg->user_data;
3605 if (recv_msg == NULL) { 4186 if (recv_msg == NULL) {
3606 printk(KERN_WARNING 4187 dev_warn(intf->si_dev,
3607 "IPMI message received with no owner. This\n" 4188 "IPMI message received with no owner. This could be because of a malformed message, or because of a hardware error. Contact your hardware vender for assistance\n");
3608 "could be because of a malformed message, or\n"
3609 "because of a hardware error. Contact your\n"
3610 "hardware vender for assistance\n");
3611 return 0; 4189 return 0;
3612 } 4190 }
3613 4191
@@ -3661,9 +4239,9 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
3661#endif 4239#endif
3662 if (msg->rsp_size < 2) { 4240 if (msg->rsp_size < 2) {
3663 /* Message is too small to be correct. */ 4241 /* Message is too small to be correct. */
3664 printk(KERN_WARNING PFX "BMC returned to small a message" 4242 dev_warn(intf->si_dev,
3665 " for netfn %x cmd %x, got %d bytes\n", 4243 PFX "BMC returned to small a message for netfn %x cmd %x, got %d bytes\n",
3666 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size); 4244 (msg->data[0] >> 2) | 1, msg->data[1], msg->rsp_size);
3667 4245
3668 /* Generate an error response for the message. */ 4246 /* Generate an error response for the message. */
3669 msg->rsp[0] = msg->data[0] | (1 << 2); 4247 msg->rsp[0] = msg->data[0] | (1 << 2);
@@ -3676,10 +4254,10 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
3676 * The NetFN and Command in the response is not even 4254 * The NetFN and Command in the response is not even
3677 * marginally correct. 4255 * marginally correct.
3678 */ 4256 */
3679 printk(KERN_WARNING PFX "BMC returned incorrect response," 4257 dev_warn(intf->si_dev,
3680 " expected netfn %x cmd %x, got netfn %x cmd %x\n", 4258 PFX "BMC returned incorrect response, expected netfn %x cmd %x, got netfn %x cmd %x\n",
3681 (msg->data[0] >> 2) | 1, msg->data[1], 4259 (msg->data[0] >> 2) | 1, msg->data[1],
3682 msg->rsp[0] >> 2, msg->rsp[1]); 4260 msg->rsp[0] >> 2, msg->rsp[1]);
3683 4261
3684 /* Generate an error response for the message. */ 4262 /* Generate an error response for the message. */
3685 msg->rsp[0] = msg->data[0] | (1 << 2); 4263 msg->rsp[0] = msg->data[0] | (1 << 2);
@@ -3721,6 +4299,8 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
3721 deliver_response(recv_msg); 4299 deliver_response(recv_msg);
3722 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2)) 4300 } else if ((msg->rsp[0] == ((IPMI_NETFN_APP_REQUEST|1) << 2))
3723 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) { 4301 && (msg->rsp[1] == IPMI_GET_MSG_CMD)) {
4302 struct ipmi_channel *chans;
4303
3724 /* It's from the receive queue. */ 4304 /* It's from the receive queue. */
3725 chan = msg->rsp[3] & 0xf; 4305 chan = msg->rsp[3] & 0xf;
3726 if (chan >= IPMI_MAX_CHANNELS) { 4306 if (chan >= IPMI_MAX_CHANNELS) {
@@ -3735,12 +4315,14 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
3735 * equal to or greater than IPMI_MAX_CHANNELS when all the 4315 * equal to or greater than IPMI_MAX_CHANNELS when all the
3736 * channels for this interface have been initialized. 4316 * channels for this interface have been initialized.
3737 */ 4317 */
3738 if (intf->curr_channel < IPMI_MAX_CHANNELS) { 4318 if (!intf->channels_ready) {
3739 requeue = 0; /* Throw the message away */ 4319 requeue = 0; /* Throw the message away */
3740 goto out; 4320 goto out;
3741 } 4321 }
3742 4322
3743 switch (intf->channels[chan].medium) { 4323 chans = READ_ONCE(intf->channel_list)->c;
4324
4325 switch (chans[chan].medium) {
3744 case IPMI_CHANNEL_MEDIUM_IPMB: 4326 case IPMI_CHANNEL_MEDIUM_IPMB:
3745 if (msg->rsp[4] & 0x04) { 4327 if (msg->rsp[4] & 0x04) {
3746 /* 4328 /*
@@ -3777,9 +4359,8 @@ static int handle_one_recv_msg(ipmi_smi_t intf,
3777 default: 4359 default:
3778 /* Check for OEM Channels. Clients had better 4360 /* Check for OEM Channels. Clients had better
3779 register for these commands. */ 4361 register for these commands. */
3780 if ((intf->channels[chan].medium 4362 if ((chans[chan].medium >= IPMI_CHANNEL_MEDIUM_OEM_MIN)
3781 >= IPMI_CHANNEL_MEDIUM_OEM_MIN) 4363 && (chans[chan].medium
3782 && (intf->channels[chan].medium
3783 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) { 4364 <= IPMI_CHANNEL_MEDIUM_OEM_MAX)) {
3784 requeue = handle_oem_get_msg_cmd(intf, msg); 4365 requeue = handle_oem_get_msg_cmd(intf, msg);
3785 } else { 4366 } else {
@@ -3941,15 +4522,14 @@ void ipmi_smi_msg_received(ipmi_smi_t intf,
3941 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR) 4522 && (msg->rsp[2] != IPMI_LOST_ARBITRATION_ERR)
3942 && (msg->rsp[2] != IPMI_BUS_ERR) 4523 && (msg->rsp[2] != IPMI_BUS_ERR)
3943 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) { 4524 && (msg->rsp[2] != IPMI_NAK_ON_WRITE_ERR)) {
3944 int chan = msg->rsp[3] & 0xf; 4525 int ch = msg->rsp[3] & 0xf;
4526 struct ipmi_channel *chans;
3945 4527
3946 /* Got an error sending the message, handle it. */ 4528 /* Got an error sending the message, handle it. */
3947 if (chan >= IPMI_MAX_CHANNELS) 4529
3948 ; /* This shouldn't happen */ 4530 chans = READ_ONCE(intf->channel_list)->c;
3949 else if ((intf->channels[chan].medium 4531 if ((chans[ch].medium == IPMI_CHANNEL_MEDIUM_8023LAN)
3950 == IPMI_CHANNEL_MEDIUM_8023LAN) 4532 || (chans[ch].medium == IPMI_CHANNEL_MEDIUM_ASYNC))
3951 || (intf->channels[chan].medium
3952 == IPMI_CHANNEL_MEDIUM_ASYNC))
3953 ipmi_inc_stat(intf, sent_lan_command_errs); 4533 ipmi_inc_stat(intf, sent_lan_command_errs);
3954 else 4534 else
3955 ipmi_inc_stat(intf, sent_ipmb_command_errs); 4535 ipmi_inc_stat(intf, sent_ipmb_command_errs);
@@ -4030,7 +4610,8 @@ smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
4030} 4610}
4031 4611
4032static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent, 4612static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4033 struct list_head *timeouts, long timeout_period, 4613 struct list_head *timeouts,
4614 unsigned long timeout_period,
4034 int slot, unsigned long *flags, 4615 int slot, unsigned long *flags,
4035 unsigned int *waiting_msgs) 4616 unsigned int *waiting_msgs)
4036{ 4617{
@@ -4043,8 +4624,8 @@ static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4043 if (!ent->inuse) 4624 if (!ent->inuse)
4044 return; 4625 return;
4045 4626
4046 ent->timeout -= timeout_period; 4627 if (timeout_period < ent->timeout) {
4047 if (ent->timeout > 0) { 4628 ent->timeout -= timeout_period;
4048 (*waiting_msgs)++; 4629 (*waiting_msgs)++;
4049 return; 4630 return;
4050 } 4631 }
@@ -4110,7 +4691,8 @@ static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
4110 } 4691 }
4111} 4692}
4112 4693
4113static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period) 4694static unsigned int ipmi_timeout_handler(ipmi_smi_t intf,
4695 unsigned long timeout_period)
4114{ 4696{
4115 struct list_head timeouts; 4697 struct list_head timeouts;
4116 struct ipmi_recv_msg *msg, *msg2; 4698 struct ipmi_recv_msg *msg, *msg2;
@@ -4118,6 +4700,14 @@ static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
4118 int i; 4700 int i;
4119 unsigned int waiting_msgs = 0; 4701 unsigned int waiting_msgs = 0;
4120 4702
4703 if (!intf->bmc_registered) {
4704 kref_get(&intf->refcount);
4705 if (!schedule_work(&intf->bmc_reg_work)) {
4706 kref_put(&intf->refcount, intf_free);
4707 waiting_msgs++;
4708 }
4709 }
4710
4121 /* 4711 /*
4122 * Go through the seq table and find any messages that 4712 * Go through the seq table and find any messages that
4123 * have timed out, putting them in the timeouts 4713 * have timed out, putting them in the timeouts
@@ -4269,8 +4859,6 @@ void ipmi_free_recv_msg(struct ipmi_recv_msg *msg)
4269} 4859}
4270EXPORT_SYMBOL(ipmi_free_recv_msg); 4860EXPORT_SYMBOL(ipmi_free_recv_msg);
4271 4861
4272#ifdef CONFIG_IPMI_PANIC_EVENT
4273
4274static atomic_t panic_done_count = ATOMIC_INIT(0); 4862static atomic_t panic_done_count = ATOMIC_INIT(0);
4275 4863
4276static void dummy_smi_done_handler(struct ipmi_smi_msg *msg) 4864static void dummy_smi_done_handler(struct ipmi_smi_msg *msg)
@@ -4306,8 +4894,8 @@ static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4306 &smi_msg, 4894 &smi_msg,
4307 &recv_msg, 4895 &recv_msg,
4308 0, 4896 0,
4309 intf->channels[0].address, 4897 intf->addrinfo[0].address,
4310 intf->channels[0].lun, 4898 intf->addrinfo[0].lun,
4311 0, 1); /* Don't retry, and don't wait. */ 4899 0, 1); /* Don't retry, and don't wait. */
4312 if (rv) 4900 if (rv)
4313 atomic_sub(2, &panic_done_count); 4901 atomic_sub(2, &panic_done_count);
@@ -4318,7 +4906,6 @@ static void ipmi_panic_request_and_wait(ipmi_smi_t intf,
4318 ipmi_poll(intf); 4906 ipmi_poll(intf);
4319} 4907}
4320 4908
4321#ifdef CONFIG_IPMI_PANIC_STRING
4322static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg) 4909static void event_receiver_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4323{ 4910{
4324 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) 4911 if ((msg->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
@@ -4345,7 +4932,6 @@ static void device_id_fetcher(ipmi_smi_t intf, struct ipmi_recv_msg *msg)
4345 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1; 4932 intf->local_event_generator = (msg->msg.data[6] >> 5) & 1;
4346 } 4933 }
4347} 4934}
4348#endif
4349 4935
4350static void send_panic_events(char *str) 4936static void send_panic_events(char *str)
4351{ 4937{
@@ -4355,6 +4941,9 @@ static void send_panic_events(char *str)
4355 struct ipmi_system_interface_addr *si; 4941 struct ipmi_system_interface_addr *si;
4356 struct ipmi_addr addr; 4942 struct ipmi_addr addr;
4357 4943
4944 if (ipmi_send_panic_event == IPMI_SEND_PANIC_EVENT_NONE)
4945 return;
4946
4358 si = (struct ipmi_system_interface_addr *) &addr; 4947 si = (struct ipmi_system_interface_addr *) &addr;
4359 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 4948 si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
4360 si->channel = IPMI_BMC_CHANNEL; 4949 si->channel = IPMI_BMC_CHANNEL;
@@ -4383,20 +4972,19 @@ static void send_panic_events(char *str)
4383 4972
4384 /* For every registered interface, send the event. */ 4973 /* For every registered interface, send the event. */
4385 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) { 4974 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
4386 if (!intf->handlers) 4975 if (!intf->handlers || !intf->handlers->poll)
4387 /* Interface is not ready. */ 4976 /* Interface is not ready or can't run at panic time. */
4388 continue; 4977 continue;
4389 4978
4390 /* Send the event announcing the panic. */ 4979 /* Send the event announcing the panic. */
4391 ipmi_panic_request_and_wait(intf, &addr, &msg); 4980 ipmi_panic_request_and_wait(intf, &addr, &msg);
4392 } 4981 }
4393 4982
4394#ifdef CONFIG_IPMI_PANIC_STRING
4395 /* 4983 /*
4396 * On every interface, dump a bunch of OEM event holding the 4984 * On every interface, dump a bunch of OEM event holding the
4397 * string. 4985 * string.
4398 */ 4986 */
4399 if (!str) 4987 if (ipmi_send_panic_event != IPMI_SEND_PANIC_EVENT_STRING || !str)
4400 return; 4988 return;
4401 4989
4402 /* For every registered interface, send the event. */ 4990 /* For every registered interface, send the event. */
@@ -4456,7 +5044,7 @@ static void send_panic_events(char *str)
4456 */ 5044 */
4457 if (((intf->event_receiver & 1) == 0) 5045 if (((intf->event_receiver & 1) == 0)
4458 && (intf->event_receiver != 0) 5046 && (intf->event_receiver != 0)
4459 && (intf->event_receiver != intf->channels[0].address)) { 5047 && (intf->event_receiver != intf->addrinfo[0].address)) {
4460 /* 5048 /*
4461 * The event receiver is valid, send an IPMB 5049 * The event receiver is valid, send an IPMB
4462 * message. 5050 * message.
@@ -4493,7 +5081,7 @@ static void send_panic_events(char *str)
4493 data[0] = 0; 5081 data[0] = 0;
4494 data[1] = 0; 5082 data[1] = 0;
4495 data[2] = 0xf0; /* OEM event without timestamp. */ 5083 data[2] = 0xf0; /* OEM event without timestamp. */
4496 data[3] = intf->channels[0].address; 5084 data[3] = intf->addrinfo[0].address;
4497 data[4] = j++; /* sequence # */ 5085 data[4] = j++; /* sequence # */
4498 /* 5086 /*
4499 * Always give 11 bytes, so strncpy will fill 5087 * Always give 11 bytes, so strncpy will fill
@@ -4505,9 +5093,7 @@ static void send_panic_events(char *str)
4505 ipmi_panic_request_and_wait(intf, &addr, &msg); 5093 ipmi_panic_request_and_wait(intf, &addr, &msg);
4506 } 5094 }
4507 } 5095 }
4508#endif /* CONFIG_IPMI_PANIC_STRING */
4509} 5096}
4510#endif /* CONFIG_IPMI_PANIC_EVENT */
4511 5097
4512static int has_panicked; 5098static int has_panicked;
4513 5099
@@ -4545,12 +5131,12 @@ static int panic_event(struct notifier_block *this,
4545 spin_unlock(&intf->waiting_rcv_msgs_lock); 5131 spin_unlock(&intf->waiting_rcv_msgs_lock);
4546 5132
4547 intf->run_to_completion = 1; 5133 intf->run_to_completion = 1;
4548 intf->handlers->set_run_to_completion(intf->send_info, 1); 5134 if (intf->handlers->set_run_to_completion)
5135 intf->handlers->set_run_to_completion(intf->send_info,
5136 1);
4549 } 5137 }
4550 5138
4551#ifdef CONFIG_IPMI_PANIC_EVENT
4552 send_panic_events(ptr); 5139 send_panic_events(ptr);
4553#endif
4554 5140
4555 return NOTIFY_DONE; 5141 return NOTIFY_DONE;
4556} 5142}
@@ -4570,22 +5156,21 @@ static int ipmi_init_msghandler(void)
4570 5156
4571 rv = driver_register(&ipmidriver.driver); 5157 rv = driver_register(&ipmidriver.driver);
4572 if (rv) { 5158 if (rv) {
4573 printk(KERN_ERR PFX "Could not register IPMI driver\n"); 5159 pr_err(PFX "Could not register IPMI driver\n");
4574 return rv; 5160 return rv;
4575 } 5161 }
4576 5162
4577 printk(KERN_INFO "ipmi message handler version " 5163 pr_info("ipmi message handler version " IPMI_DRIVER_VERSION "\n");
4578 IPMI_DRIVER_VERSION "\n");
4579 5164
4580#ifdef CONFIG_PROC_FS 5165#ifdef CONFIG_IPMI_PROC_INTERFACE
4581 proc_ipmi_root = proc_mkdir("ipmi", NULL); 5166 proc_ipmi_root = proc_mkdir("ipmi", NULL);
4582 if (!proc_ipmi_root) { 5167 if (!proc_ipmi_root) {
4583 printk(KERN_ERR PFX "Unable to create IPMI proc dir"); 5168 pr_err(PFX "Unable to create IPMI proc dir");
4584 driver_unregister(&ipmidriver.driver); 5169 driver_unregister(&ipmidriver.driver);
4585 return -ENOMEM; 5170 return -ENOMEM;
4586 } 5171 }
4587 5172
4588#endif /* CONFIG_PROC_FS */ 5173#endif /* CONFIG_IPMI_PROC_INTERFACE */
4589 5174
4590 setup_timer(&ipmi_timer, ipmi_timeout, 0); 5175 setup_timer(&ipmi_timer, ipmi_timeout, 0);
4591 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES); 5176 mod_timer(&ipmi_timer, jiffies + IPMI_TIMEOUT_JIFFIES);
@@ -4625,9 +5210,9 @@ static void __exit cleanup_ipmi(void)
4625 atomic_inc(&stop_operation); 5210 atomic_inc(&stop_operation);
4626 del_timer_sync(&ipmi_timer); 5211 del_timer_sync(&ipmi_timer);
4627 5212
4628#ifdef CONFIG_PROC_FS 5213#ifdef CONFIG_IPMI_PROC_INTERFACE
4629 proc_remove(proc_ipmi_root); 5214 proc_remove(proc_ipmi_root);
4630#endif /* CONFIG_PROC_FS */ 5215#endif /* CONFIG_IPMI_PROC_INTERFACE */
4631 5216
4632 driver_unregister(&ipmidriver.driver); 5217 driver_unregister(&ipmidriver.driver);
4633 5218
@@ -4636,12 +5221,10 @@ static void __exit cleanup_ipmi(void)
4636 /* Check for buffer leaks. */ 5221 /* Check for buffer leaks. */
4637 count = atomic_read(&smi_msg_inuse_count); 5222 count = atomic_read(&smi_msg_inuse_count);
4638 if (count != 0) 5223 if (count != 0)
4639 printk(KERN_WARNING PFX "SMI message count %d at exit\n", 5224 pr_warn(PFX "SMI message count %d at exit\n", count);
4640 count);
4641 count = atomic_read(&recv_msg_inuse_count); 5225 count = atomic_read(&recv_msg_inuse_count);
4642 if (count != 0) 5226 if (count != 0)
4643 printk(KERN_WARNING PFX "recv message count %d at exit\n", 5227 pr_warn(PFX "recv message count %d at exit\n", count);
4644 count);
4645} 5228}
4646module_exit(cleanup_ipmi); 5229module_exit(cleanup_ipmi);
4647 5230
diff --git a/drivers/char/ipmi/ipmi_powernv.c b/drivers/char/ipmi/ipmi_powernv.c
index b338a4becbf8..07fddbefefe4 100644
--- a/drivers/char/ipmi/ipmi_powernv.c
+++ b/drivers/char/ipmi/ipmi_powernv.c
@@ -23,7 +23,6 @@
23 23
24struct ipmi_smi_powernv { 24struct ipmi_smi_powernv {
25 u64 interface_id; 25 u64 interface_id;
26 struct ipmi_device_id ipmi_id;
27 ipmi_smi_t intf; 26 ipmi_smi_t intf;
28 unsigned int irq; 27 unsigned int irq;
29 28
@@ -266,8 +265,7 @@ static int ipmi_powernv_probe(struct platform_device *pdev)
266 } 265 }
267 266
268 /* todo: query actual ipmi_device_id */ 267 /* todo: query actual ipmi_device_id */
269 rc = ipmi_register_smi(&ipmi_powernv_smi_handlers, ipmi, 268 rc = ipmi_register_smi(&ipmi_powernv_smi_handlers, ipmi, dev, 0);
270 &ipmi->ipmi_id, dev, 0);
271 if (rc) { 269 if (rc) {
272 dev_warn(dev, "IPMI SMI registration failed (%d)\n", rc); 270 dev_warn(dev, "IPMI SMI registration failed (%d)\n", rc);
273 goto err_free_msg; 271 goto err_free_msg;
diff --git a/drivers/char/ipmi/ipmi_poweroff.c b/drivers/char/ipmi/ipmi_poweroff.c
index 676c910e990f..38e6af1c8e38 100644
--- a/drivers/char/ipmi/ipmi_poweroff.c
+++ b/drivers/char/ipmi/ipmi_poweroff.c
@@ -133,7 +133,7 @@ static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
133 complete(comp); 133 complete(comp);
134} 134}
135 135
136static struct ipmi_user_hndl ipmi_poweroff_handler = { 136static const struct ipmi_user_hndl ipmi_poweroff_handler = {
137 .ipmi_recv_hndl = receive_handler 137 .ipmi_recv_hndl = receive_handler
138}; 138};
139 139
diff --git a/drivers/char/ipmi/ipmi_si.h b/drivers/char/ipmi/ipmi_si.h
new file mode 100644
index 000000000000..17ce5f7b89ab
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si.h
@@ -0,0 +1,49 @@
1/*
2 * ipmi_si.h
3 *
4 * Interface from the device-specific interfaces (OF, DMI, ACPI, PCI,
5 * etc) to the base ipmi system interface code.
6 */
7
8#include <linux/interrupt.h>
9#include "ipmi_si_sm.h"
10
11#define IPMI_IO_ADDR_SPACE 0
12#define IPMI_MEM_ADDR_SPACE 1
13
14#define DEFAULT_REGSPACING 1
15#define DEFAULT_REGSIZE 1
16
17#define DEVICE_NAME "ipmi_si"
18
19int ipmi_si_add_smi(struct si_sm_io *io);
20irqreturn_t ipmi_si_irq_handler(int irq, void *data);
21void ipmi_irq_start_cleanup(struct si_sm_io *io);
22int ipmi_std_irq_setup(struct si_sm_io *io);
23void ipmi_irq_finish_setup(struct si_sm_io *io);
24int ipmi_si_remove_by_dev(struct device *dev);
25void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
26 unsigned long addr);
27int ipmi_si_hardcode_find_bmc(void);
28void ipmi_si_platform_init(void);
29void ipmi_si_platform_shutdown(void);
30
31extern struct platform_driver ipmi_platform_driver;
32
33#ifdef CONFIG_PCI
34void ipmi_si_pci_init(void);
35void ipmi_si_pci_shutdown(void);
36#else
37static inline void ipmi_si_pci_init(void) { }
38static inline void ipmi_si_pci_shutdown(void) { }
39#endif
40#ifdef CONFIG_PARISC
41void ipmi_si_parisc_init(void);
42void ipmi_si_parisc_shutdown(void);
43#else
44static inline void ipmi_si_parisc_init(void) { }
45static inline void ipmi_si_parisc_shutdown(void) { }
46#endif
47
48int ipmi_si_port_setup(struct si_sm_io *io);
49int ipmi_si_mem_setup(struct si_sm_io *io);
diff --git a/drivers/char/ipmi/ipmi_si_hardcode.c b/drivers/char/ipmi/ipmi_si_hardcode.c
new file mode 100644
index 000000000000..fa9a4780de36
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_hardcode.c
@@ -0,0 +1,146 @@
1
2#include <linux/moduleparam.h>
3#include "ipmi_si.h"
4
5#define PFX "ipmi_hardcode: "
6/*
7 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
8 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
9 */
10
11#define SI_MAX_PARMS 4
12
13static char *si_type[SI_MAX_PARMS];
14#define MAX_SI_TYPE_STR 30
15static char si_type_str[MAX_SI_TYPE_STR];
16static unsigned long addrs[SI_MAX_PARMS];
17static unsigned int num_addrs;
18static unsigned int ports[SI_MAX_PARMS];
19static unsigned int num_ports;
20static int irqs[SI_MAX_PARMS];
21static unsigned int num_irqs;
22static int regspacings[SI_MAX_PARMS];
23static unsigned int num_regspacings;
24static int regsizes[SI_MAX_PARMS];
25static unsigned int num_regsizes;
26static int regshifts[SI_MAX_PARMS];
27static unsigned int num_regshifts;
28static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
29static unsigned int num_slave_addrs;
30
31module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
32MODULE_PARM_DESC(type, "Defines the type of each interface, each"
33 " interface separated by commas. The types are 'kcs',"
34 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
35 " the first interface to kcs and the second to bt");
36module_param_hw_array(addrs, ulong, iomem, &num_addrs, 0);
37MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
38 " addresses separated by commas. Only use if an interface"
39 " is in memory. Otherwise, set it to zero or leave"
40 " it blank.");
41module_param_hw_array(ports, uint, ioport, &num_ports, 0);
42MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
43 " addresses separated by commas. Only use if an interface"
44 " is a port. Otherwise, set it to zero or leave"
45 " it blank.");
46module_param_hw_array(irqs, int, irq, &num_irqs, 0);
47MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
48 " addresses separated by commas. Only use if an interface"
49 " has an interrupt. Otherwise, set it to zero or leave"
50 " it blank.");
51module_param_hw_array(regspacings, int, other, &num_regspacings, 0);
52MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
53 " and each successive register used by the interface. For"
54 " instance, if the start address is 0xca2 and the spacing"
55 " is 2, then the second address is at 0xca4. Defaults"
56 " to 1.");
57module_param_hw_array(regsizes, int, other, &num_regsizes, 0);
58MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
59 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
60 " 16-bit, 32-bit, or 64-bit register. Use this if you"
61 " the 8-bit IPMI register has to be read from a larger"
62 " register.");
63module_param_hw_array(regshifts, int, other, &num_regshifts, 0);
64MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
65 " IPMI register, in bits. For instance, if the data"
66 " is read from a 32-bit word and the IPMI data is in"
67 " bit 8-15, then the shift would be 8");
68module_param_hw_array(slave_addrs, int, other, &num_slave_addrs, 0);
69MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
70 " the controller. Normally this is 0x20, but can be"
71 " overridden by this parm. This is an array indexed"
72 " by interface number.");
73
74int ipmi_si_hardcode_find_bmc(void)
75{
76 int ret = -ENODEV;
77 int i;
78 struct si_sm_io io;
79 char *str;
80
81 /* Parse out the si_type string into its components. */
82 str = si_type_str;
83 if (*str != '\0') {
84 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
85 si_type[i] = str;
86 str = strchr(str, ',');
87 if (str) {
88 *str = '\0';
89 str++;
90 } else {
91 break;
92 }
93 }
94 }
95
96 memset(&io, 0, sizeof(io));
97 for (i = 0; i < SI_MAX_PARMS; i++) {
98 if (!ports[i] && !addrs[i])
99 continue;
100
101 io.addr_source = SI_HARDCODED;
102 pr_info(PFX "probing via hardcoded address\n");
103
104 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
105 io.si_type = SI_KCS;
106 } else if (strcmp(si_type[i], "smic") == 0) {
107 io.si_type = SI_SMIC;
108 } else if (strcmp(si_type[i], "bt") == 0) {
109 io.si_type = SI_BT;
110 } else {
111 pr_warn(PFX "Interface type specified for interface %d, was invalid: %s\n",
112 i, si_type[i]);
113 continue;
114 }
115
116 if (ports[i]) {
117 /* An I/O port */
118 io.addr_data = ports[i];
119 io.addr_type = IPMI_IO_ADDR_SPACE;
120 } else if (addrs[i]) {
121 /* A memory port */
122 io.addr_data = addrs[i];
123 io.addr_type = IPMI_MEM_ADDR_SPACE;
124 } else {
125 pr_warn(PFX "Interface type specified for interface %d, but port and address were not set or set to zero.\n",
126 i);
127 continue;
128 }
129
130 io.addr = NULL;
131 io.regspacing = regspacings[i];
132 if (!io.regspacing)
133 io.regspacing = DEFAULT_REGSPACING;
134 io.regsize = regsizes[i];
135 if (!io.regsize)
136 io.regsize = DEFAULT_REGSIZE;
137 io.regshift = regshifts[i];
138 io.irq = irqs[i];
139 if (io.irq)
140 io.irq_setup = ipmi_std_irq_setup;
141 io.slave_addr = slave_addrs[i];
142
143 ret = ipmi_si_add_smi(&io);
144 }
145 return ret;
146}
diff --git a/drivers/char/ipmi/ipmi_si_hotmod.c b/drivers/char/ipmi/ipmi_si_hotmod.c
new file mode 100644
index 000000000000..fc03b9be2f3d
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_hotmod.c
@@ -0,0 +1,242 @@
1/*
2 * ipmi_si_hotmod.c
3 *
4 * Handling for dynamically adding/removing IPMI devices through
5 * a module parameter (and thus sysfs).
6 */
7#include <linux/moduleparam.h>
8#include <linux/ipmi.h>
9#include "ipmi_si.h"
10
11#define PFX "ipmi_hotmod: "
12
13static int hotmod_handler(const char *val, const struct kernel_param *kp);
14
15module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
16MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
17 " Documentation/IPMI.txt in the kernel sources for the"
18 " gory details.");
19
20/*
21 * Parms come in as <op1>[:op2[:op3...]]. ops are:
22 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
23 * Options are:
24 * rsp=<regspacing>
25 * rsi=<regsize>
26 * rsh=<regshift>
27 * irq=<irq>
28 * ipmb=<ipmb addr>
29 */
30enum hotmod_op { HM_ADD, HM_REMOVE };
31struct hotmod_vals {
32 const char *name;
33 const int val;
34};
35
36static const struct hotmod_vals hotmod_ops[] = {
37 { "add", HM_ADD },
38 { "remove", HM_REMOVE },
39 { NULL }
40};
41
42static const struct hotmod_vals hotmod_si[] = {
43 { "kcs", SI_KCS },
44 { "smic", SI_SMIC },
45 { "bt", SI_BT },
46 { NULL }
47};
48
49static const struct hotmod_vals hotmod_as[] = {
50 { "mem", IPMI_MEM_ADDR_SPACE },
51 { "i/o", IPMI_IO_ADDR_SPACE },
52 { NULL }
53};
54
55static int parse_str(const struct hotmod_vals *v, int *val, char *name,
56 char **curr)
57{
58 char *s;
59 int i;
60
61 s = strchr(*curr, ',');
62 if (!s) {
63 pr_warn(PFX "No hotmod %s given.\n", name);
64 return -EINVAL;
65 }
66 *s = '\0';
67 s++;
68 for (i = 0; v[i].name; i++) {
69 if (strcmp(*curr, v[i].name) == 0) {
70 *val = v[i].val;
71 *curr = s;
72 return 0;
73 }
74 }
75
76 pr_warn(PFX "Invalid hotmod %s '%s'\n", name, *curr);
77 return -EINVAL;
78}
79
80static int check_hotmod_int_op(const char *curr, const char *option,
81 const char *name, int *val)
82{
83 char *n;
84
85 if (strcmp(curr, name) == 0) {
86 if (!option) {
87 pr_warn(PFX "No option given for '%s'\n", curr);
88 return -EINVAL;
89 }
90 *val = simple_strtoul(option, &n, 0);
91 if ((*n != '\0') || (*option == '\0')) {
92 pr_warn(PFX "Bad option given for '%s'\n", curr);
93 return -EINVAL;
94 }
95 return 1;
96 }
97 return 0;
98}
99
100static int hotmod_handler(const char *val, const struct kernel_param *kp)
101{
102 char *str = kstrdup(val, GFP_KERNEL);
103 int rv;
104 char *next, *curr, *s, *n, *o;
105 enum hotmod_op op;
106 enum si_type si_type;
107 int addr_space;
108 unsigned long addr;
109 int regspacing;
110 int regsize;
111 int regshift;
112 int irq;
113 int ipmb;
114 int ival;
115 int len;
116
117 if (!str)
118 return -ENOMEM;
119
120 /* Kill any trailing spaces, as we can get a "\n" from echo. */
121 len = strlen(str);
122 ival = len - 1;
123 while ((ival >= 0) && isspace(str[ival])) {
124 str[ival] = '\0';
125 ival--;
126 }
127
128 for (curr = str; curr; curr = next) {
129 regspacing = 1;
130 regsize = 1;
131 regshift = 0;
132 irq = 0;
133 ipmb = 0; /* Choose the default if not specified */
134
135 next = strchr(curr, ':');
136 if (next) {
137 *next = '\0';
138 next++;
139 }
140
141 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
142 if (rv)
143 break;
144 op = ival;
145
146 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
147 if (rv)
148 break;
149 si_type = ival;
150
151 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
152 if (rv)
153 break;
154
155 s = strchr(curr, ',');
156 if (s) {
157 *s = '\0';
158 s++;
159 }
160 addr = simple_strtoul(curr, &n, 0);
161 if ((*n != '\0') || (*curr == '\0')) {
162 pr_warn(PFX "Invalid hotmod address '%s'\n", curr);
163 break;
164 }
165
166 while (s) {
167 curr = s;
168 s = strchr(curr, ',');
169 if (s) {
170 *s = '\0';
171 s++;
172 }
173 o = strchr(curr, '=');
174 if (o) {
175 *o = '\0';
176 o++;
177 }
178 rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
179 if (rv < 0)
180 goto out;
181 else if (rv)
182 continue;
183 rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
184 if (rv < 0)
185 goto out;
186 else if (rv)
187 continue;
188 rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
189 if (rv < 0)
190 goto out;
191 else if (rv)
192 continue;
193 rv = check_hotmod_int_op(curr, o, "irq", &irq);
194 if (rv < 0)
195 goto out;
196 else if (rv)
197 continue;
198 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
199 if (rv < 0)
200 goto out;
201 else if (rv)
202 continue;
203
204 rv = -EINVAL;
205 pr_warn(PFX "Invalid hotmod option '%s'\n", curr);
206 goto out;
207 }
208
209 if (op == HM_ADD) {
210 struct si_sm_io io;
211
212 memset(&io, 0, sizeof(io));
213 io.addr_source = SI_HOTMOD;
214 io.si_type = si_type;
215 io.addr_data = addr;
216 io.addr_type = addr_space;
217
218 io.addr = NULL;
219 io.regspacing = regspacing;
220 if (!io.regspacing)
221 io.regspacing = DEFAULT_REGSPACING;
222 io.regsize = regsize;
223 if (!io.regsize)
224 io.regsize = DEFAULT_REGSIZE;
225 io.regshift = regshift;
226 io.irq = irq;
227 if (io.irq)
228 io.irq_setup = ipmi_std_irq_setup;
229 io.slave_addr = ipmb;
230
231 rv = ipmi_si_add_smi(&io);
232 if (rv)
233 goto out;
234 } else {
235 ipmi_si_remove_by_data(addr_space, si_type, addr);
236 }
237 }
238 rv = len;
239out:
240 kfree(str);
241 return rv;
242}
diff --git a/drivers/char/ipmi/ipmi_si_intf.c b/drivers/char/ipmi/ipmi_si_intf.c
index d2ac66fe0645..71d33a1807e4 100644
--- a/drivers/char/ipmi/ipmi_si_intf.c
+++ b/drivers/char/ipmi/ipmi_si_intf.c
@@ -49,8 +49,6 @@
49#include <linux/slab.h> 49#include <linux/slab.h>
50#include <linux/delay.h> 50#include <linux/delay.h>
51#include <linux/list.h> 51#include <linux/list.h>
52#include <linux/pci.h>
53#include <linux/ioport.h>
54#include <linux/notifier.h> 52#include <linux/notifier.h>
55#include <linux/mutex.h> 53#include <linux/mutex.h>
56#include <linux/kthread.h> 54#include <linux/kthread.h>
@@ -59,22 +57,9 @@
59#include <linux/rcupdate.h> 57#include <linux/rcupdate.h>
60#include <linux/ipmi.h> 58#include <linux/ipmi.h>
61#include <linux/ipmi_smi.h> 59#include <linux/ipmi_smi.h>
62#include <asm/io.h> 60#include "ipmi_si.h"
63#include "ipmi_si_sm.h"
64#include "ipmi_dmi.h"
65#include <linux/dmi.h>
66#include <linux/string.h> 61#include <linux/string.h>
67#include <linux/ctype.h> 62#include <linux/ctype.h>
68#include <linux/of_device.h>
69#include <linux/of_platform.h>
70#include <linux/of_address.h>
71#include <linux/of_irq.h>
72#include <linux/acpi.h>
73
74#ifdef CONFIG_PARISC
75#include <asm/hardware.h> /* for register_parisc_driver() stuff */
76#include <asm/parisc-device.h>
77#endif
78 63
79#define PFX "ipmi_si: " 64#define PFX "ipmi_si: "
80 65
@@ -104,15 +89,9 @@ enum si_intf_state {
104#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2 89#define IPMI_BT_INTMASK_CLEAR_IRQ_BIT 2
105#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1 90#define IPMI_BT_INTMASK_ENABLE_IRQ_BIT 1
106 91
107enum si_type { 92static const char * const si_to_str[] = { "invalid", "kcs", "smic", "bt" };
108 SI_KCS, SI_SMIC, SI_BT
109};
110
111static const char * const si_to_str[] = { "kcs", "smic", "bt" };
112 93
113#define DEVICE_NAME "ipmi_si" 94static int initialized;
114
115static struct platform_driver ipmi_driver;
116 95
117/* 96/*
118 * Indexes into stats[] in smi_info below. 97 * Indexes into stats[] in smi_info below.
@@ -167,7 +146,6 @@ struct smi_info {
167 ipmi_smi_t intf; 146 ipmi_smi_t intf;
168 struct si_sm_data *si_sm; 147 struct si_sm_data *si_sm;
169 const struct si_sm_handlers *handlers; 148 const struct si_sm_handlers *handlers;
170 enum si_type si_type;
171 spinlock_t si_lock; 149 spinlock_t si_lock;
172 struct ipmi_smi_msg *waiting_msg; 150 struct ipmi_smi_msg *waiting_msg;
173 struct ipmi_smi_msg *curr_msg; 151 struct ipmi_smi_msg *curr_msg;
@@ -178,14 +156,6 @@ struct smi_info {
178 * IPMI 156 * IPMI
179 */ 157 */
180 struct si_sm_io io; 158 struct si_sm_io io;
181 int (*io_setup)(struct smi_info *info);
182 void (*io_cleanup)(struct smi_info *info);
183 int (*irq_setup)(struct smi_info *info);
184 void (*irq_cleanup)(struct smi_info *info);
185 unsigned int io_size;
186 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
187 void (*addr_source_cleanup)(struct smi_info *info);
188 void *addr_source_data;
189 159
190 /* 160 /*
191 * Per-OEM handler, called from handle_flags(). Returns 1 161 * Per-OEM handler, called from handle_flags(). Returns 1
@@ -226,19 +196,6 @@ struct smi_info {
226 */ 196 */
227 bool run_to_completion; 197 bool run_to_completion;
228 198
229 /* The I/O port of an SI interface. */
230 int port;
231
232 /*
233 * The space between start addresses of the two ports. For
234 * instance, if the first port is 0xca2 and the spacing is 4, then
235 * the second port is 0xca6.
236 */
237 unsigned int spacing;
238
239 /* zero if no irq; */
240 int irq;
241
242 /* The timer for this si. */ 199 /* The timer for this si. */
243 struct timer_list si_timer; 200 struct timer_list si_timer;
244 201
@@ -289,26 +246,15 @@ struct smi_info {
289 /* From the get device id response... */ 246 /* From the get device id response... */
290 struct ipmi_device_id device_id; 247 struct ipmi_device_id device_id;
291 248
292 /* Driver model stuff. */ 249 /* Default driver model device. */
293 struct device *dev;
294 struct platform_device *pdev; 250 struct platform_device *pdev;
295 251
296 /*
297 * True if we allocated the device, false if it came from
298 * someplace else (like PCI).
299 */
300 bool dev_registered;
301
302 /* Slave address, could be reported from DMI. */
303 unsigned char slave_addr;
304
305 /* Counters and things for the proc filesystem. */ 252 /* Counters and things for the proc filesystem. */
306 atomic_t stats[SI_NUM_STATS]; 253 atomic_t stats[SI_NUM_STATS];
307 254
308 struct task_struct *thread; 255 struct task_struct *thread;
309 256
310 struct list_head link; 257 struct list_head link;
311 union ipmi_smi_info_union addr_info;
312}; 258};
313 259
314#define smi_inc_stat(smi, stat) \ 260#define smi_inc_stat(smi, stat) \
@@ -316,23 +262,15 @@ struct smi_info {
316#define smi_get_stat(smi, stat) \ 262#define smi_get_stat(smi, stat) \
317 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat])) 263 ((unsigned int) atomic_read(&(smi)->stats[SI_STAT_ ## stat]))
318 264
319#define SI_MAX_PARMS 4 265#define IPMI_MAX_INTFS 4
320 266static int force_kipmid[IPMI_MAX_INTFS];
321static int force_kipmid[SI_MAX_PARMS];
322static int num_force_kipmid; 267static int num_force_kipmid;
323#ifdef CONFIG_PCI
324static bool pci_registered;
325#endif
326#ifdef CONFIG_PARISC
327static bool parisc_registered;
328#endif
329 268
330static unsigned int kipmid_max_busy_us[SI_MAX_PARMS]; 269static unsigned int kipmid_max_busy_us[IPMI_MAX_INTFS];
331static int num_max_busy_us; 270static int num_max_busy_us;
332 271
333static bool unload_when_empty = true; 272static bool unload_when_empty = true;
334 273
335static int add_smi(struct smi_info *smi);
336static int try_smi_init(struct smi_info *smi); 274static int try_smi_init(struct smi_info *smi);
337static void cleanup_one_si(struct smi_info *to_clean); 275static void cleanup_one_si(struct smi_info *to_clean);
338static void cleanup_ipmi_si(void); 276static void cleanup_ipmi_si(void);
@@ -499,7 +437,7 @@ static void start_getting_events(struct smi_info *smi_info)
499 */ 437 */
500static inline bool disable_si_irq(struct smi_info *smi_info, bool start_timer) 438static inline bool disable_si_irq(struct smi_info *smi_info, bool start_timer)
501{ 439{
502 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) { 440 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
503 smi_info->interrupt_disabled = true; 441 smi_info->interrupt_disabled = true;
504 start_check_enables(smi_info, start_timer); 442 start_check_enables(smi_info, start_timer);
505 return true; 443 return true;
@@ -509,7 +447,7 @@ static inline bool disable_si_irq(struct smi_info *smi_info, bool start_timer)
509 447
510static inline bool enable_si_irq(struct smi_info *smi_info) 448static inline bool enable_si_irq(struct smi_info *smi_info)
511{ 449{
512 if ((smi_info->irq) && (smi_info->interrupt_disabled)) { 450 if ((smi_info->io.irq) && (smi_info->interrupt_disabled)) {
513 smi_info->interrupt_disabled = false; 451 smi_info->interrupt_disabled = false;
514 start_check_enables(smi_info, true); 452 start_check_enables(smi_info, true);
515 return true; 453 return true;
@@ -585,13 +523,13 @@ static u8 current_global_enables(struct smi_info *smi_info, u8 base,
585 if (smi_info->supports_event_msg_buff) 523 if (smi_info->supports_event_msg_buff)
586 enables |= IPMI_BMC_EVT_MSG_BUFF; 524 enables |= IPMI_BMC_EVT_MSG_BUFF;
587 525
588 if (((smi_info->irq && !smi_info->interrupt_disabled) || 526 if (((smi_info->io.irq && !smi_info->interrupt_disabled) ||
589 smi_info->cannot_disable_irq) && 527 smi_info->cannot_disable_irq) &&
590 !smi_info->irq_enable_broken) 528 !smi_info->irq_enable_broken)
591 enables |= IPMI_BMC_RCV_MSG_INTR; 529 enables |= IPMI_BMC_RCV_MSG_INTR;
592 530
593 if (smi_info->supports_event_msg_buff && 531 if (smi_info->supports_event_msg_buff &&
594 smi_info->irq && !smi_info->interrupt_disabled && 532 smi_info->io.irq && !smi_info->interrupt_disabled &&
595 !smi_info->irq_enable_broken) 533 !smi_info->irq_enable_broken)
596 enables |= IPMI_BMC_EVT_MSG_INTR; 534 enables |= IPMI_BMC_EVT_MSG_INTR;
597 535
@@ -673,7 +611,7 @@ static void handle_transaction_done(struct smi_info *smi_info)
673 smi_info->handlers->get_result(smi_info->si_sm, msg, 3); 611 smi_info->handlers->get_result(smi_info->si_sm, msg, 3);
674 if (msg[2] != 0) { 612 if (msg[2] != 0) {
675 /* Error clearing flags */ 613 /* Error clearing flags */
676 dev_warn(smi_info->dev, 614 dev_warn(smi_info->io.dev,
677 "Error clearing flags: %2.2x\n", msg[2]); 615 "Error clearing flags: %2.2x\n", msg[2]);
678 } 616 }
679 smi_info->si_state = SI_NORMAL; 617 smi_info->si_state = SI_NORMAL;
@@ -765,15 +703,15 @@ static void handle_transaction_done(struct smi_info *smi_info)
765 /* We got the flags from the SMI, now handle them. */ 703 /* We got the flags from the SMI, now handle them. */
766 smi_info->handlers->get_result(smi_info->si_sm, msg, 4); 704 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
767 if (msg[2] != 0) { 705 if (msg[2] != 0) {
768 dev_warn(smi_info->dev, 706 dev_warn(smi_info->io.dev,
769 "Couldn't get irq info: %x.\n", msg[2]); 707 "Couldn't get irq info: %x.\n", msg[2]);
770 dev_warn(smi_info->dev, 708 dev_warn(smi_info->io.dev,
771 "Maybe ok, but ipmi might run very slowly.\n"); 709 "Maybe ok, but ipmi might run very slowly.\n");
772 smi_info->si_state = SI_NORMAL; 710 smi_info->si_state = SI_NORMAL;
773 break; 711 break;
774 } 712 }
775 enables = current_global_enables(smi_info, 0, &irq_on); 713 enables = current_global_enables(smi_info, 0, &irq_on);
776 if (smi_info->si_type == SI_BT) 714 if (smi_info->io.si_type == SI_BT)
777 /* BT has its own interrupt enable bit. */ 715 /* BT has its own interrupt enable bit. */
778 check_bt_irq(smi_info, irq_on); 716 check_bt_irq(smi_info, irq_on);
779 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) { 717 if (enables != (msg[3] & GLOBAL_ENABLES_MASK)) {
@@ -803,7 +741,7 @@ static void handle_transaction_done(struct smi_info *smi_info)
803 741
804 smi_info->handlers->get_result(smi_info->si_sm, msg, 4); 742 smi_info->handlers->get_result(smi_info->si_sm, msg, 4);
805 if (msg[2] != 0) 743 if (msg[2] != 0)
806 dev_warn(smi_info->dev, 744 dev_warn(smi_info->io.dev,
807 "Could not set the global enables: 0x%x.\n", 745 "Could not set the global enables: 0x%x.\n",
808 msg[2]); 746 msg[2]);
809 747
@@ -927,7 +865,7 @@ restart:
927 * asynchronously reset, and may thus get interrupts 865 * asynchronously reset, and may thus get interrupts
928 * disable and messages disabled. 866 * disable and messages disabled.
929 */ 867 */
930 if (smi_info->supports_event_msg_buff || smi_info->irq) { 868 if (smi_info->supports_event_msg_buff || smi_info->io.irq) {
931 start_check_enables(smi_info, true); 869 start_check_enables(smi_info, true);
932 } else { 870 } else {
933 smi_info->curr_msg = alloc_msg_handle_irq(smi_info); 871 smi_info->curr_msg = alloc_msg_handle_irq(smi_info);
@@ -1153,8 +1091,6 @@ static void set_need_watch(void *send_info, bool enable)
1153 spin_unlock_irqrestore(&smi_info->si_lock, flags); 1091 spin_unlock_irqrestore(&smi_info->si_lock, flags);
1154} 1092}
1155 1093
1156static int initialized;
1157
1158static void smi_timeout(unsigned long data) 1094static void smi_timeout(unsigned long data)
1159{ 1095{
1160 struct smi_info *smi_info = (struct smi_info *) data; 1096 struct smi_info *smi_info = (struct smi_info *) data;
@@ -1172,7 +1108,7 @@ static void smi_timeout(unsigned long data)
1172 * SI_USEC_PER_JIFFY); 1108 * SI_USEC_PER_JIFFY);
1173 smi_result = smi_event_handler(smi_info, time_diff); 1109 smi_result = smi_event_handler(smi_info, time_diff);
1174 1110
1175 if ((smi_info->irq) && (!smi_info->interrupt_disabled)) { 1111 if ((smi_info->io.irq) && (!smi_info->interrupt_disabled)) {
1176 /* Running with interrupts, only do long timeouts. */ 1112 /* Running with interrupts, only do long timeouts. */
1177 timeout = jiffies + SI_TIMEOUT_JIFFIES; 1113 timeout = jiffies + SI_TIMEOUT_JIFFIES;
1178 smi_inc_stat(smi_info, long_timeouts); 1114 smi_inc_stat(smi_info, long_timeouts);
@@ -1199,11 +1135,17 @@ do_mod_timer:
1199 spin_unlock_irqrestore(&(smi_info->si_lock), flags); 1135 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
1200} 1136}
1201 1137
1202static irqreturn_t si_irq_handler(int irq, void *data) 1138irqreturn_t ipmi_si_irq_handler(int irq, void *data)
1203{ 1139{
1204 struct smi_info *smi_info = data; 1140 struct smi_info *smi_info = data;
1205 unsigned long flags; 1141 unsigned long flags;
1206 1142
1143 if (smi_info->io.si_type == SI_BT)
1144 /* We need to clear the IRQ flag for the BT interface. */
1145 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1146 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1147 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1148
1207 spin_lock_irqsave(&(smi_info->si_lock), flags); 1149 spin_lock_irqsave(&(smi_info->si_lock), flags);
1208 1150
1209 smi_inc_stat(smi_info, interrupts); 1151 smi_inc_stat(smi_info, interrupts);
@@ -1215,16 +1157,6 @@ static irqreturn_t si_irq_handler(int irq, void *data)
1215 return IRQ_HANDLED; 1157 return IRQ_HANDLED;
1216} 1158}
1217 1159
1218static irqreturn_t si_bt_irq_handler(int irq, void *data)
1219{
1220 struct smi_info *smi_info = data;
1221 /* We need to clear the IRQ flag for the BT interface. */
1222 smi_info->io.outputb(&smi_info->io, IPMI_BT_INTMASK_REG,
1223 IPMI_BT_INTMASK_CLEAR_IRQ_BIT
1224 | IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1225 return si_irq_handler(irq, data);
1226}
1227
1228static int smi_start_processing(void *send_info, 1160static int smi_start_processing(void *send_info,
1229 ipmi_smi_t intf) 1161 ipmi_smi_t intf)
1230{ 1162{
@@ -1238,8 +1170,10 @@ static int smi_start_processing(void *send_info,
1238 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES); 1170 smi_mod_timer(new_smi, jiffies + SI_TIMEOUT_JIFFIES);
1239 1171
1240 /* Try to claim any interrupts. */ 1172 /* Try to claim any interrupts. */
1241 if (new_smi->irq_setup) 1173 if (new_smi->io.irq_setup) {
1242 new_smi->irq_setup(new_smi); 1174 new_smi->io.irq_handler_data = new_smi;
1175 new_smi->io.irq_setup(&new_smi->io);
1176 }
1243 1177
1244 /* 1178 /*
1245 * Check if the user forcefully enabled the daemon. 1179 * Check if the user forcefully enabled the daemon.
@@ -1250,14 +1184,14 @@ static int smi_start_processing(void *send_info,
1250 * The BT interface is efficient enough to not need a thread, 1184 * The BT interface is efficient enough to not need a thread,
1251 * and there is no need for a thread if we have interrupts. 1185 * and there is no need for a thread if we have interrupts.
1252 */ 1186 */
1253 else if ((new_smi->si_type != SI_BT) && (!new_smi->irq)) 1187 else if ((new_smi->io.si_type != SI_BT) && (!new_smi->io.irq))
1254 enable = 1; 1188 enable = 1;
1255 1189
1256 if (enable) { 1190 if (enable) {
1257 new_smi->thread = kthread_run(ipmi_thread, new_smi, 1191 new_smi->thread = kthread_run(ipmi_thread, new_smi,
1258 "kipmi%d", new_smi->intf_num); 1192 "kipmi%d", new_smi->intf_num);
1259 if (IS_ERR(new_smi->thread)) { 1193 if (IS_ERR(new_smi->thread)) {
1260 dev_notice(new_smi->dev, "Could not start" 1194 dev_notice(new_smi->io.dev, "Could not start"
1261 " kernel thread due to error %ld, only using" 1195 " kernel thread due to error %ld, only using"
1262 " timers to drive the interface\n", 1196 " timers to drive the interface\n",
1263 PTR_ERR(new_smi->thread)); 1197 PTR_ERR(new_smi->thread));
@@ -1272,10 +1206,10 @@ static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1272{ 1206{
1273 struct smi_info *smi = send_info; 1207 struct smi_info *smi = send_info;
1274 1208
1275 data->addr_src = smi->addr_source; 1209 data->addr_src = smi->io.addr_source;
1276 data->dev = smi->dev; 1210 data->dev = smi->io.dev;
1277 data->addr_info = smi->addr_info; 1211 data->addr_info = smi->io.addr_info;
1278 get_device(smi->dev); 1212 get_device(smi->io.dev);
1279 1213
1280 return 0; 1214 return 0;
1281} 1215}
@@ -1301,118 +1235,12 @@ static const struct ipmi_smi_handlers handlers = {
1301 .poll = poll, 1235 .poll = poll,
1302}; 1236};
1303 1237
1304/*
1305 * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
1306 * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
1307 */
1308
1309static LIST_HEAD(smi_infos); 1238static LIST_HEAD(smi_infos);
1310static DEFINE_MUTEX(smi_infos_lock); 1239static DEFINE_MUTEX(smi_infos_lock);
1311static int smi_num; /* Used to sequence the SMIs */ 1240static int smi_num; /* Used to sequence the SMIs */
1312 1241
1313#define DEFAULT_REGSPACING 1
1314#define DEFAULT_REGSIZE 1
1315
1316#ifdef CONFIG_ACPI
1317static bool si_tryacpi = true;
1318#endif
1319#ifdef CONFIG_DMI
1320static bool si_trydmi = true;
1321#endif
1322static bool si_tryplatform = true;
1323#ifdef CONFIG_PCI
1324static bool si_trypci = true;
1325#endif
1326static char *si_type[SI_MAX_PARMS];
1327#define MAX_SI_TYPE_STR 30
1328static char si_type_str[MAX_SI_TYPE_STR];
1329static unsigned long addrs[SI_MAX_PARMS];
1330static unsigned int num_addrs;
1331static unsigned int ports[SI_MAX_PARMS];
1332static unsigned int num_ports;
1333static int irqs[SI_MAX_PARMS];
1334static unsigned int num_irqs;
1335static int regspacings[SI_MAX_PARMS];
1336static unsigned int num_regspacings;
1337static int regsizes[SI_MAX_PARMS];
1338static unsigned int num_regsizes;
1339static int regshifts[SI_MAX_PARMS];
1340static unsigned int num_regshifts;
1341static int slave_addrs[SI_MAX_PARMS]; /* Leaving 0 chooses the default value */
1342static unsigned int num_slave_addrs;
1343
1344#define IPMI_IO_ADDR_SPACE 0
1345#define IPMI_MEM_ADDR_SPACE 1
1346static const char * const addr_space_to_str[] = { "i/o", "mem" }; 1242static const char * const addr_space_to_str[] = { "i/o", "mem" };
1347 1243
1348static int hotmod_handler(const char *val, const struct kernel_param *kp);
1349
1350module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
1351MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
1352 " Documentation/IPMI.txt in the kernel sources for the"
1353 " gory details.");
1354
1355#ifdef CONFIG_ACPI
1356module_param_named(tryacpi, si_tryacpi, bool, 0);
1357MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
1358 " default scan of the interfaces identified via ACPI");
1359#endif
1360#ifdef CONFIG_DMI
1361module_param_named(trydmi, si_trydmi, bool, 0);
1362MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
1363 " default scan of the interfaces identified via DMI");
1364#endif
1365module_param_named(tryplatform, si_tryplatform, bool, 0);
1366MODULE_PARM_DESC(tryplatform, "Setting this to zero will disable the"
1367 " default scan of the interfaces identified via platform"
1368 " interfaces like openfirmware");
1369#ifdef CONFIG_PCI
1370module_param_named(trypci, si_trypci, bool, 0);
1371MODULE_PARM_DESC(trypci, "Setting this to zero will disable the"
1372 " default scan of the interfaces identified via pci");
1373#endif
1374module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
1375MODULE_PARM_DESC(type, "Defines the type of each interface, each"
1376 " interface separated by commas. The types are 'kcs',"
1377 " 'smic', and 'bt'. For example si_type=kcs,bt will set"
1378 " the first interface to kcs and the second to bt");
1379module_param_hw_array(addrs, ulong, iomem, &num_addrs, 0);
1380MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
1381 " addresses separated by commas. Only use if an interface"
1382 " is in memory. Otherwise, set it to zero or leave"
1383 " it blank.");
1384module_param_hw_array(ports, uint, ioport, &num_ports, 0);
1385MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
1386 " addresses separated by commas. Only use if an interface"
1387 " is a port. Otherwise, set it to zero or leave"
1388 " it blank.");
1389module_param_hw_array(irqs, int, irq, &num_irqs, 0);
1390MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
1391 " addresses separated by commas. Only use if an interface"
1392 " has an interrupt. Otherwise, set it to zero or leave"
1393 " it blank.");
1394module_param_hw_array(regspacings, int, other, &num_regspacings, 0);
1395MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
1396 " and each successive register used by the interface. For"
1397 " instance, if the start address is 0xca2 and the spacing"
1398 " is 2, then the second address is at 0xca4. Defaults"
1399 " to 1.");
1400module_param_hw_array(regsizes, int, other, &num_regsizes, 0);
1401MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
1402 " This should generally be 1, 2, 4, or 8 for an 8-bit,"
1403 " 16-bit, 32-bit, or 64-bit register. Use this if you"
1404 " the 8-bit IPMI register has to be read from a larger"
1405 " register.");
1406module_param_hw_array(regshifts, int, other, &num_regshifts, 0);
1407MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
1408 " IPMI register, in bits. For instance, if the data"
1409 " is read from a 32-bit word and the IPMI data is in"
1410 " bit 8-15, then the shift would be 8");
1411module_param_hw_array(slave_addrs, int, other, &num_slave_addrs, 0);
1412MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
1413 " the controller. Normally this is 0x20, but can be"
1414 " overridden by this parm. This is an array indexed"
1415 " by interface number.");
1416module_param_array(force_kipmid, int, &num_force_kipmid, 0); 1244module_param_array(force_kipmid, int, &num_force_kipmid, 0);
1417MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or" 1245MODULE_PARM_DESC(force_kipmid, "Force the kipmi daemon to be enabled (1) or"
1418 " disabled(0). Normally the IPMI driver auto-detects" 1246 " disabled(0). Normally the IPMI driver auto-detects"
@@ -1427,1450 +1255,53 @@ MODULE_PARM_DESC(kipmid_max_busy_us,
1427 " sleeping. 0 (default) means to wait forever. Set to 100-500" 1255 " sleeping. 0 (default) means to wait forever. Set to 100-500"
1428 " if kipmid is using up a lot of CPU time."); 1256 " if kipmid is using up a lot of CPU time.");
1429 1257
1430 1258void ipmi_irq_finish_setup(struct si_sm_io *io)
1431static void std_irq_cleanup(struct smi_info *info)
1432{
1433 if (info->si_type == SI_BT)
1434 /* Disable the interrupt in the BT interface. */
1435 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG, 0);
1436 free_irq(info->irq, info);
1437}
1438
1439static int std_irq_setup(struct smi_info *info)
1440{
1441 int rv;
1442
1443 if (!info->irq)
1444 return 0;
1445
1446 if (info->si_type == SI_BT) {
1447 rv = request_irq(info->irq,
1448 si_bt_irq_handler,
1449 IRQF_SHARED,
1450 DEVICE_NAME,
1451 info);
1452 if (!rv)
1453 /* Enable the interrupt in the BT interface. */
1454 info->io.outputb(&info->io, IPMI_BT_INTMASK_REG,
1455 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
1456 } else
1457 rv = request_irq(info->irq,
1458 si_irq_handler,
1459 IRQF_SHARED,
1460 DEVICE_NAME,
1461 info);
1462 if (rv) {
1463 dev_warn(info->dev, "%s unable to claim interrupt %d,"
1464 " running polled\n",
1465 DEVICE_NAME, info->irq);
1466 info->irq = 0;
1467 } else {
1468 info->irq_cleanup = std_irq_cleanup;
1469 dev_info(info->dev, "Using irq %d\n", info->irq);
1470 }
1471
1472 return rv;
1473}
1474
1475static unsigned char port_inb(const struct si_sm_io *io, unsigned int offset)
1476{
1477 unsigned int addr = io->addr_data;
1478
1479 return inb(addr + (offset * io->regspacing));
1480}
1481
1482static void port_outb(const struct si_sm_io *io, unsigned int offset,
1483 unsigned char b)
1484{
1485 unsigned int addr = io->addr_data;
1486
1487 outb(b, addr + (offset * io->regspacing));
1488}
1489
1490static unsigned char port_inw(const struct si_sm_io *io, unsigned int offset)
1491{
1492 unsigned int addr = io->addr_data;
1493
1494 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1495}
1496
1497static void port_outw(const struct si_sm_io *io, unsigned int offset,
1498 unsigned char b)
1499{
1500 unsigned int addr = io->addr_data;
1501
1502 outw(b << io->regshift, addr + (offset * io->regspacing));
1503}
1504
1505static unsigned char port_inl(const struct si_sm_io *io, unsigned int offset)
1506{
1507 unsigned int addr = io->addr_data;
1508
1509 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
1510}
1511
1512static void port_outl(const struct si_sm_io *io, unsigned int offset,
1513 unsigned char b)
1514{
1515 unsigned int addr = io->addr_data;
1516
1517 outl(b << io->regshift, addr+(offset * io->regspacing));
1518}
1519
1520static void port_cleanup(struct smi_info *info)
1521{
1522 unsigned int addr = info->io.addr_data;
1523 int idx;
1524
1525 if (addr) {
1526 for (idx = 0; idx < info->io_size; idx++)
1527 release_region(addr + idx * info->io.regspacing,
1528 info->io.regsize);
1529 }
1530}
1531
1532static int port_setup(struct smi_info *info)
1533{
1534 unsigned int addr = info->io.addr_data;
1535 int idx;
1536
1537 if (!addr)
1538 return -ENODEV;
1539
1540 info->io_cleanup = port_cleanup;
1541
1542 /*
1543 * Figure out the actual inb/inw/inl/etc routine to use based
1544 * upon the register size.
1545 */
1546 switch (info->io.regsize) {
1547 case 1:
1548 info->io.inputb = port_inb;
1549 info->io.outputb = port_outb;
1550 break;
1551 case 2:
1552 info->io.inputb = port_inw;
1553 info->io.outputb = port_outw;
1554 break;
1555 case 4:
1556 info->io.inputb = port_inl;
1557 info->io.outputb = port_outl;
1558 break;
1559 default:
1560 dev_warn(info->dev, "Invalid register size: %d\n",
1561 info->io.regsize);
1562 return -EINVAL;
1563 }
1564
1565 /*
1566 * Some BIOSes reserve disjoint I/O regions in their ACPI
1567 * tables. This causes problems when trying to register the
1568 * entire I/O region. Therefore we must register each I/O
1569 * port separately.
1570 */
1571 for (idx = 0; idx < info->io_size; idx++) {
1572 if (request_region(addr + idx * info->io.regspacing,
1573 info->io.regsize, DEVICE_NAME) == NULL) {
1574 /* Undo allocations */
1575 while (idx--)
1576 release_region(addr + idx * info->io.regspacing,
1577 info->io.regsize);
1578 return -EIO;
1579 }
1580 }
1581 return 0;
1582}
1583
1584static unsigned char intf_mem_inb(const struct si_sm_io *io,
1585 unsigned int offset)
1586{
1587 return readb((io->addr)+(offset * io->regspacing));
1588}
1589
1590static void intf_mem_outb(const struct si_sm_io *io, unsigned int offset,
1591 unsigned char b)
1592{
1593 writeb(b, (io->addr)+(offset * io->regspacing));
1594}
1595
1596static unsigned char intf_mem_inw(const struct si_sm_io *io,
1597 unsigned int offset)
1598{
1599 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
1600 & 0xff;
1601}
1602
1603static void intf_mem_outw(const struct si_sm_io *io, unsigned int offset,
1604 unsigned char b)
1605{
1606 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
1607}
1608
1609static unsigned char intf_mem_inl(const struct si_sm_io *io,
1610 unsigned int offset)
1611{
1612 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
1613 & 0xff;
1614}
1615
1616static void intf_mem_outl(const struct si_sm_io *io, unsigned int offset,
1617 unsigned char b)
1618{
1619 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
1620}
1621
1622#ifdef readq
1623static unsigned char mem_inq(const struct si_sm_io *io, unsigned int offset)
1624{
1625 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
1626 & 0xff;
1627}
1628
1629static void mem_outq(const struct si_sm_io *io, unsigned int offset,
1630 unsigned char b)
1631{
1632 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
1633}
1634#endif
1635
1636static void mem_region_cleanup(struct smi_info *info, int num)
1637{
1638 unsigned long addr = info->io.addr_data;
1639 int idx;
1640
1641 for (idx = 0; idx < num; idx++)
1642 release_mem_region(addr + idx * info->io.regspacing,
1643 info->io.regsize);
1644}
1645
1646static void mem_cleanup(struct smi_info *info)
1647{
1648 if (info->io.addr) {
1649 iounmap(info->io.addr);
1650 mem_region_cleanup(info, info->io_size);
1651 }
1652}
1653
1654static int mem_setup(struct smi_info *info)
1655{
1656 unsigned long addr = info->io.addr_data;
1657 int mapsize, idx;
1658
1659 if (!addr)
1660 return -ENODEV;
1661
1662 info->io_cleanup = mem_cleanup;
1663
1664 /*
1665 * Figure out the actual readb/readw/readl/etc routine to use based
1666 * upon the register size.
1667 */
1668 switch (info->io.regsize) {
1669 case 1:
1670 info->io.inputb = intf_mem_inb;
1671 info->io.outputb = intf_mem_outb;
1672 break;
1673 case 2:
1674 info->io.inputb = intf_mem_inw;
1675 info->io.outputb = intf_mem_outw;
1676 break;
1677 case 4:
1678 info->io.inputb = intf_mem_inl;
1679 info->io.outputb = intf_mem_outl;
1680 break;
1681#ifdef readq
1682 case 8:
1683 info->io.inputb = mem_inq;
1684 info->io.outputb = mem_outq;
1685 break;
1686#endif
1687 default:
1688 dev_warn(info->dev, "Invalid register size: %d\n",
1689 info->io.regsize);
1690 return -EINVAL;
1691 }
1692
1693 /*
1694 * Some BIOSes reserve disjoint memory regions in their ACPI
1695 * tables. This causes problems when trying to request the
1696 * entire region. Therefore we must request each register
1697 * separately.
1698 */
1699 for (idx = 0; idx < info->io_size; idx++) {
1700 if (request_mem_region(addr + idx * info->io.regspacing,
1701 info->io.regsize, DEVICE_NAME) == NULL) {
1702 /* Undo allocations */
1703 mem_region_cleanup(info, idx);
1704 return -EIO;
1705 }
1706 }
1707
1708 /*
1709 * Calculate the total amount of memory to claim. This is an
1710 * unusual looking calculation, but it avoids claiming any
1711 * more memory than it has to. It will claim everything
1712 * between the first address to the end of the last full
1713 * register.
1714 */
1715 mapsize = ((info->io_size * info->io.regspacing)
1716 - (info->io.regspacing - info->io.regsize));
1717 info->io.addr = ioremap(addr, mapsize);
1718 if (info->io.addr == NULL) {
1719 mem_region_cleanup(info, info->io_size);
1720 return -EIO;
1721 }
1722 return 0;
1723}
1724
1725/*
1726 * Parms come in as <op1>[:op2[:op3...]]. ops are:
1727 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
1728 * Options are:
1729 * rsp=<regspacing>
1730 * rsi=<regsize>
1731 * rsh=<regshift>
1732 * irq=<irq>
1733 * ipmb=<ipmb addr>
1734 */
1735enum hotmod_op { HM_ADD, HM_REMOVE };
1736struct hotmod_vals {
1737 const char *name;
1738 const int val;
1739};
1740
1741static const struct hotmod_vals hotmod_ops[] = {
1742 { "add", HM_ADD },
1743 { "remove", HM_REMOVE },
1744 { NULL }
1745};
1746
1747static const struct hotmod_vals hotmod_si[] = {
1748 { "kcs", SI_KCS },
1749 { "smic", SI_SMIC },
1750 { "bt", SI_BT },
1751 { NULL }
1752};
1753
1754static const struct hotmod_vals hotmod_as[] = {
1755 { "mem", IPMI_MEM_ADDR_SPACE },
1756 { "i/o", IPMI_IO_ADDR_SPACE },
1757 { NULL }
1758};
1759
1760static int parse_str(const struct hotmod_vals *v, int *val, char *name,
1761 char **curr)
1762{
1763 char *s;
1764 int i;
1765
1766 s = strchr(*curr, ',');
1767 if (!s) {
1768 pr_warn(PFX "No hotmod %s given.\n", name);
1769 return -EINVAL;
1770 }
1771 *s = '\0';
1772 s++;
1773 for (i = 0; v[i].name; i++) {
1774 if (strcmp(*curr, v[i].name) == 0) {
1775 *val = v[i].val;
1776 *curr = s;
1777 return 0;
1778 }
1779 }
1780
1781 pr_warn(PFX "Invalid hotmod %s '%s'\n", name, *curr);
1782 return -EINVAL;
1783}
1784
1785static int check_hotmod_int_op(const char *curr, const char *option,
1786 const char *name, int *val)
1787{
1788 char *n;
1789
1790 if (strcmp(curr, name) == 0) {
1791 if (!option) {
1792 pr_warn(PFX "No option given for '%s'\n", curr);
1793 return -EINVAL;
1794 }
1795 *val = simple_strtoul(option, &n, 0);
1796 if ((*n != '\0') || (*option == '\0')) {
1797 pr_warn(PFX "Bad option given for '%s'\n", curr);
1798 return -EINVAL;
1799 }
1800 return 1;
1801 }
1802 return 0;
1803}
1804
1805static struct smi_info *smi_info_alloc(void)
1806{
1807 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1808
1809 if (info)
1810 spin_lock_init(&info->si_lock);
1811 return info;
1812}
1813
1814static int hotmod_handler(const char *val, const struct kernel_param *kp)
1815{
1816 char *str = kstrdup(val, GFP_KERNEL);
1817 int rv;
1818 char *next, *curr, *s, *n, *o;
1819 enum hotmod_op op;
1820 enum si_type si_type;
1821 int addr_space;
1822 unsigned long addr;
1823 int regspacing;
1824 int regsize;
1825 int regshift;
1826 int irq;
1827 int ipmb;
1828 int ival;
1829 int len;
1830 struct smi_info *info;
1831
1832 if (!str)
1833 return -ENOMEM;
1834
1835 /* Kill any trailing spaces, as we can get a "\n" from echo. */
1836 len = strlen(str);
1837 ival = len - 1;
1838 while ((ival >= 0) && isspace(str[ival])) {
1839 str[ival] = '\0';
1840 ival--;
1841 }
1842
1843 for (curr = str; curr; curr = next) {
1844 regspacing = 1;
1845 regsize = 1;
1846 regshift = 0;
1847 irq = 0;
1848 ipmb = 0; /* Choose the default if not specified */
1849
1850 next = strchr(curr, ':');
1851 if (next) {
1852 *next = '\0';
1853 next++;
1854 }
1855
1856 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
1857 if (rv)
1858 break;
1859 op = ival;
1860
1861 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
1862 if (rv)
1863 break;
1864 si_type = ival;
1865
1866 rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
1867 if (rv)
1868 break;
1869
1870 s = strchr(curr, ',');
1871 if (s) {
1872 *s = '\0';
1873 s++;
1874 }
1875 addr = simple_strtoul(curr, &n, 0);
1876 if ((*n != '\0') || (*curr == '\0')) {
1877 pr_warn(PFX "Invalid hotmod address '%s'\n", curr);
1878 break;
1879 }
1880
1881 while (s) {
1882 curr = s;
1883 s = strchr(curr, ',');
1884 if (s) {
1885 *s = '\0';
1886 s++;
1887 }
1888 o = strchr(curr, '=');
1889 if (o) {
1890 *o = '\0';
1891 o++;
1892 }
1893 rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
1894 if (rv < 0)
1895 goto out;
1896 else if (rv)
1897 continue;
1898 rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
1899 if (rv < 0)
1900 goto out;
1901 else if (rv)
1902 continue;
1903 rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
1904 if (rv < 0)
1905 goto out;
1906 else if (rv)
1907 continue;
1908 rv = check_hotmod_int_op(curr, o, "irq", &irq);
1909 if (rv < 0)
1910 goto out;
1911 else if (rv)
1912 continue;
1913 rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
1914 if (rv < 0)
1915 goto out;
1916 else if (rv)
1917 continue;
1918
1919 rv = -EINVAL;
1920 pr_warn(PFX "Invalid hotmod option '%s'\n", curr);
1921 goto out;
1922 }
1923
1924 if (op == HM_ADD) {
1925 info = smi_info_alloc();
1926 if (!info) {
1927 rv = -ENOMEM;
1928 goto out;
1929 }
1930
1931 info->addr_source = SI_HOTMOD;
1932 info->si_type = si_type;
1933 info->io.addr_data = addr;
1934 info->io.addr_type = addr_space;
1935 if (addr_space == IPMI_MEM_ADDR_SPACE)
1936 info->io_setup = mem_setup;
1937 else
1938 info->io_setup = port_setup;
1939
1940 info->io.addr = NULL;
1941 info->io.regspacing = regspacing;
1942 if (!info->io.regspacing)
1943 info->io.regspacing = DEFAULT_REGSPACING;
1944 info->io.regsize = regsize;
1945 if (!info->io.regsize)
1946 info->io.regsize = DEFAULT_REGSIZE;
1947 info->io.regshift = regshift;
1948 info->irq = irq;
1949 if (info->irq)
1950 info->irq_setup = std_irq_setup;
1951 info->slave_addr = ipmb;
1952
1953 rv = add_smi(info);
1954 if (rv) {
1955 kfree(info);
1956 goto out;
1957 }
1958 mutex_lock(&smi_infos_lock);
1959 rv = try_smi_init(info);
1960 mutex_unlock(&smi_infos_lock);
1961 if (rv) {
1962 cleanup_one_si(info);
1963 goto out;
1964 }
1965 } else {
1966 /* remove */
1967 struct smi_info *e, *tmp_e;
1968
1969 mutex_lock(&smi_infos_lock);
1970 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
1971 if (e->io.addr_type != addr_space)
1972 continue;
1973 if (e->si_type != si_type)
1974 continue;
1975 if (e->io.addr_data == addr)
1976 cleanup_one_si(e);
1977 }
1978 mutex_unlock(&smi_infos_lock);
1979 }
1980 }
1981 rv = len;
1982out:
1983 kfree(str);
1984 return rv;
1985}
1986
1987static int hardcode_find_bmc(void)
1988{
1989 int ret = -ENODEV;
1990 int i;
1991 struct smi_info *info;
1992
1993 for (i = 0; i < SI_MAX_PARMS; i++) {
1994 if (!ports[i] && !addrs[i])
1995 continue;
1996
1997 info = smi_info_alloc();
1998 if (!info)
1999 return -ENOMEM;
2000
2001 info->addr_source = SI_HARDCODED;
2002 pr_info(PFX "probing via hardcoded address\n");
2003
2004 if (!si_type[i] || strcmp(si_type[i], "kcs") == 0) {
2005 info->si_type = SI_KCS;
2006 } else if (strcmp(si_type[i], "smic") == 0) {
2007 info->si_type = SI_SMIC;
2008 } else if (strcmp(si_type[i], "bt") == 0) {
2009 info->si_type = SI_BT;
2010 } else {
2011 pr_warn(PFX "Interface type specified for interface %d, was invalid: %s\n",
2012 i, si_type[i]);
2013 kfree(info);
2014 continue;
2015 }
2016
2017 if (ports[i]) {
2018 /* An I/O port */
2019 info->io_setup = port_setup;
2020 info->io.addr_data = ports[i];
2021 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2022 } else if (addrs[i]) {
2023 /* A memory port */
2024 info->io_setup = mem_setup;
2025 info->io.addr_data = addrs[i];
2026 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2027 } else {
2028 pr_warn(PFX "Interface type specified for interface %d, but port and address were not set or set to zero.\n",
2029 i);
2030 kfree(info);
2031 continue;
2032 }
2033
2034 info->io.addr = NULL;
2035 info->io.regspacing = regspacings[i];
2036 if (!info->io.regspacing)
2037 info->io.regspacing = DEFAULT_REGSPACING;
2038 info->io.regsize = regsizes[i];
2039 if (!info->io.regsize)
2040 info->io.regsize = DEFAULT_REGSIZE;
2041 info->io.regshift = regshifts[i];
2042 info->irq = irqs[i];
2043 if (info->irq)
2044 info->irq_setup = std_irq_setup;
2045 info->slave_addr = slave_addrs[i];
2046
2047 if (!add_smi(info)) {
2048 mutex_lock(&smi_infos_lock);
2049 if (try_smi_init(info))
2050 cleanup_one_si(info);
2051 mutex_unlock(&smi_infos_lock);
2052 ret = 0;
2053 } else {
2054 kfree(info);
2055 }
2056 }
2057 return ret;
2058}
2059
2060#ifdef CONFIG_ACPI
2061
2062/*
2063 * Once we get an ACPI failure, we don't try any more, because we go
2064 * through the tables sequentially. Once we don't find a table, there
2065 * are no more.
2066 */
2067static int acpi_failure;
2068
2069/* For GPE-type interrupts. */
2070static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
2071 u32 gpe_number, void *context)
2072{
2073 struct smi_info *smi_info = context;
2074 unsigned long flags;
2075
2076 spin_lock_irqsave(&(smi_info->si_lock), flags);
2077
2078 smi_inc_stat(smi_info, interrupts);
2079
2080 debug_timestamp("ACPI_GPE");
2081
2082 smi_event_handler(smi_info, 0);
2083 spin_unlock_irqrestore(&(smi_info->si_lock), flags);
2084
2085 return ACPI_INTERRUPT_HANDLED;
2086}
2087
2088static void acpi_gpe_irq_cleanup(struct smi_info *info)
2089{
2090 if (!info->irq)
2091 return;
2092
2093 acpi_remove_gpe_handler(NULL, info->irq, &ipmi_acpi_gpe);
2094}
2095
2096static int acpi_gpe_irq_setup(struct smi_info *info)
2097{
2098 acpi_status status;
2099
2100 if (!info->irq)
2101 return 0;
2102
2103 status = acpi_install_gpe_handler(NULL,
2104 info->irq,
2105 ACPI_GPE_LEVEL_TRIGGERED,
2106 &ipmi_acpi_gpe,
2107 info);
2108 if (status != AE_OK) {
2109 dev_warn(info->dev, "%s unable to claim ACPI GPE %d,"
2110 " running polled\n", DEVICE_NAME, info->irq);
2111 info->irq = 0;
2112 return -EINVAL;
2113 } else {
2114 info->irq_cleanup = acpi_gpe_irq_cleanup;
2115 dev_info(info->dev, "Using ACPI GPE %d\n", info->irq);
2116 return 0;
2117 }
2118}
2119
2120/*
2121 * Defined at
2122 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
2123 */
2124struct SPMITable {
2125 s8 Signature[4];
2126 u32 Length;
2127 u8 Revision;
2128 u8 Checksum;
2129 s8 OEMID[6];
2130 s8 OEMTableID[8];
2131 s8 OEMRevision[4];
2132 s8 CreatorID[4];
2133 s8 CreatorRevision[4];
2134 u8 InterfaceType;
2135 u8 IPMIlegacy;
2136 s16 SpecificationRevision;
2137
2138 /*
2139 * Bit 0 - SCI interrupt supported
2140 * Bit 1 - I/O APIC/SAPIC
2141 */
2142 u8 InterruptType;
2143
2144 /*
2145 * If bit 0 of InterruptType is set, then this is the SCI
2146 * interrupt in the GPEx_STS register.
2147 */
2148 u8 GPE;
2149
2150 s16 Reserved;
2151
2152 /*
2153 * If bit 1 of InterruptType is set, then this is the I/O
2154 * APIC/SAPIC interrupt.
2155 */
2156 u32 GlobalSystemInterrupt;
2157
2158 /* The actual register address. */
2159 struct acpi_generic_address addr;
2160
2161 u8 UID[4];
2162
2163 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2164};
2165
2166static int try_init_spmi(struct SPMITable *spmi)
2167{
2168 struct smi_info *info;
2169 int rv;
2170
2171 if (spmi->IPMIlegacy != 1) {
2172 pr_info(PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
2173 return -ENODEV;
2174 }
2175
2176 info = smi_info_alloc();
2177 if (!info) {
2178 pr_err(PFX "Could not allocate SI data (3)\n");
2179 return -ENOMEM;
2180 }
2181
2182 info->addr_source = SI_SPMI;
2183 pr_info(PFX "probing via SPMI\n");
2184
2185 /* Figure out the interface type. */
2186 switch (spmi->InterfaceType) {
2187 case 1: /* KCS */
2188 info->si_type = SI_KCS;
2189 break;
2190 case 2: /* SMIC */
2191 info->si_type = SI_SMIC;
2192 break;
2193 case 3: /* BT */
2194 info->si_type = SI_BT;
2195 break;
2196 case 4: /* SSIF, just ignore */
2197 kfree(info);
2198 return -EIO;
2199 default:
2200 pr_info(PFX "Unknown ACPI/SPMI SI type %d\n",
2201 spmi->InterfaceType);
2202 kfree(info);
2203 return -EIO;
2204 }
2205
2206 if (spmi->InterruptType & 1) {
2207 /* We've got a GPE interrupt. */
2208 info->irq = spmi->GPE;
2209 info->irq_setup = acpi_gpe_irq_setup;
2210 } else if (spmi->InterruptType & 2) {
2211 /* We've got an APIC/SAPIC interrupt. */
2212 info->irq = spmi->GlobalSystemInterrupt;
2213 info->irq_setup = std_irq_setup;
2214 } else {
2215 /* Use the default interrupt setting. */
2216 info->irq = 0;
2217 info->irq_setup = NULL;
2218 }
2219
2220 if (spmi->addr.bit_width) {
2221 /* A (hopefully) properly formed register bit width. */
2222 info->io.regspacing = spmi->addr.bit_width / 8;
2223 } else {
2224 info->io.regspacing = DEFAULT_REGSPACING;
2225 }
2226 info->io.regsize = info->io.regspacing;
2227 info->io.regshift = spmi->addr.bit_offset;
2228
2229 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
2230 info->io_setup = mem_setup;
2231 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2232 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
2233 info->io_setup = port_setup;
2234 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2235 } else {
2236 kfree(info);
2237 pr_warn(PFX "Unknown ACPI I/O Address type\n");
2238 return -EIO;
2239 }
2240 info->io.addr_data = spmi->addr.address;
2241
2242 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
2243 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2244 info->io.addr_data, info->io.regsize, info->io.regspacing,
2245 info->irq);
2246
2247 rv = add_smi(info);
2248 if (rv)
2249 kfree(info);
2250
2251 return rv;
2252}
2253
2254static void spmi_find_bmc(void)
2255{
2256 acpi_status status;
2257 struct SPMITable *spmi;
2258 int i;
2259
2260 if (acpi_disabled)
2261 return;
2262
2263 if (acpi_failure)
2264 return;
2265
2266 for (i = 0; ; i++) {
2267 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
2268 (struct acpi_table_header **)&spmi);
2269 if (status != AE_OK)
2270 return;
2271
2272 try_init_spmi(spmi);
2273 }
2274}
2275#endif
2276
2277#if defined(CONFIG_DMI) || defined(CONFIG_ACPI)
2278struct resource *ipmi_get_info_from_resources(struct platform_device *pdev,
2279 struct smi_info *info)
2280{
2281 struct resource *res, *res_second;
2282
2283 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2284 if (res) {
2285 info->io_setup = port_setup;
2286 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2287 } else {
2288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2289 if (res) {
2290 info->io_setup = mem_setup;
2291 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2292 }
2293 }
2294 if (!res) {
2295 dev_err(&pdev->dev, "no I/O or memory address\n");
2296 return NULL;
2297 }
2298 info->io.addr_data = res->start;
2299
2300 info->io.regspacing = DEFAULT_REGSPACING;
2301 res_second = platform_get_resource(pdev,
2302 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ?
2303 IORESOURCE_IO : IORESOURCE_MEM,
2304 1);
2305 if (res_second) {
2306 if (res_second->start > info->io.addr_data)
2307 info->io.regspacing =
2308 res_second->start - info->io.addr_data;
2309 }
2310 info->io.regsize = DEFAULT_REGSIZE;
2311 info->io.regshift = 0;
2312
2313 return res;
2314}
2315
2316#endif
2317
2318#ifdef CONFIG_DMI
2319static int dmi_ipmi_probe(struct platform_device *pdev)
2320{
2321 struct smi_info *info;
2322 u8 type, slave_addr;
2323 int rv;
2324
2325 if (!si_trydmi)
2326 return -ENODEV;
2327
2328 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
2329 if (rv)
2330 return -ENODEV;
2331
2332 info = smi_info_alloc();
2333 if (!info) {
2334 pr_err(PFX "Could not allocate SI data\n");
2335 return -ENOMEM;
2336 }
2337
2338 info->addr_source = SI_SMBIOS;
2339 pr_info(PFX "probing via SMBIOS\n");
2340
2341 switch (type) {
2342 case IPMI_DMI_TYPE_KCS:
2343 info->si_type = SI_KCS;
2344 break;
2345 case IPMI_DMI_TYPE_SMIC:
2346 info->si_type = SI_SMIC;
2347 break;
2348 case IPMI_DMI_TYPE_BT:
2349 info->si_type = SI_BT;
2350 break;
2351 default:
2352 kfree(info);
2353 return -EINVAL;
2354 }
2355
2356 if (!ipmi_get_info_from_resources(pdev, info)) {
2357 rv = -EINVAL;
2358 goto err_free;
2359 }
2360
2361 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
2362 if (rv) {
2363 dev_warn(&pdev->dev, "device has no slave-addr property");
2364 info->slave_addr = 0x20;
2365 } else {
2366 info->slave_addr = slave_addr;
2367 }
2368
2369 info->irq = platform_get_irq(pdev, 0);
2370 if (info->irq > 0)
2371 info->irq_setup = std_irq_setup;
2372 else
2373 info->irq = 0;
2374
2375 info->dev = &pdev->dev;
2376
2377 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
2378 (info->io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
2379 info->io.addr_data, info->io.regsize, info->io.regspacing,
2380 info->irq);
2381
2382 if (add_smi(info))
2383 kfree(info);
2384
2385 return 0;
2386
2387err_free:
2388 kfree(info);
2389 return rv;
2390}
2391#else
2392static int dmi_ipmi_probe(struct platform_device *pdev)
2393{ 1259{
2394 return -ENODEV; 1260 if (io->si_type == SI_BT)
1261 /* Enable the interrupt in the BT interface. */
1262 io->outputb(io, IPMI_BT_INTMASK_REG,
1263 IPMI_BT_INTMASK_ENABLE_IRQ_BIT);
2395} 1264}
2396#endif /* CONFIG_DMI */
2397
2398#ifdef CONFIG_PCI
2399 1265
2400#define PCI_ERMC_CLASSCODE 0x0C0700 1266void ipmi_irq_start_cleanup(struct si_sm_io *io)
2401#define PCI_ERMC_CLASSCODE_MASK 0xffffff00
2402#define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
2403#define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
2404#define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
2405#define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
2406
2407#define PCI_HP_VENDOR_ID 0x103C
2408#define PCI_MMC_DEVICE_ID 0x121A
2409#define PCI_MMC_ADDR_CW 0x10
2410
2411static void ipmi_pci_cleanup(struct smi_info *info)
2412{ 1267{
2413 struct pci_dev *pdev = info->addr_source_data; 1268 if (io->si_type == SI_BT)
2414 1269 /* Disable the interrupt in the BT interface. */
2415 pci_disable_device(pdev); 1270 io->outputb(io, IPMI_BT_INTMASK_REG, 0);
2416} 1271}
2417 1272
2418static int ipmi_pci_probe_regspacing(struct smi_info *info) 1273static void std_irq_cleanup(struct si_sm_io *io)
2419{ 1274{
2420 if (info->si_type == SI_KCS) { 1275 ipmi_irq_start_cleanup(io);
2421 unsigned char status; 1276 free_irq(io->irq, io->irq_handler_data);
2422 int regspacing;
2423
2424 info->io.regsize = DEFAULT_REGSIZE;
2425 info->io.regshift = 0;
2426 info->io_size = 2;
2427 info->handlers = &kcs_smi_handlers;
2428
2429 /* detect 1, 4, 16byte spacing */
2430 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
2431 info->io.regspacing = regspacing;
2432 if (info->io_setup(info)) {
2433 dev_err(info->dev,
2434 "Could not setup I/O space\n");
2435 return DEFAULT_REGSPACING;
2436 }
2437 /* write invalid cmd */
2438 info->io.outputb(&info->io, 1, 0x10);
2439 /* read status back */
2440 status = info->io.inputb(&info->io, 1);
2441 info->io_cleanup(info);
2442 if (status)
2443 return regspacing;
2444 regspacing *= 4;
2445 }
2446 }
2447 return DEFAULT_REGSPACING;
2448} 1277}
2449 1278
2450static int ipmi_pci_probe(struct pci_dev *pdev, 1279int ipmi_std_irq_setup(struct si_sm_io *io)
2451 const struct pci_device_id *ent)
2452{ 1280{
2453 int rv; 1281 int rv;
2454 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2455 struct smi_info *info;
2456
2457 info = smi_info_alloc();
2458 if (!info)
2459 return -ENOMEM;
2460
2461 info->addr_source = SI_PCI;
2462 dev_info(&pdev->dev, "probing via PCI");
2463
2464 switch (class_type) {
2465 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
2466 info->si_type = SI_SMIC;
2467 break;
2468
2469 case PCI_ERMC_CLASSCODE_TYPE_KCS:
2470 info->si_type = SI_KCS;
2471 break;
2472
2473 case PCI_ERMC_CLASSCODE_TYPE_BT:
2474 info->si_type = SI_BT;
2475 break;
2476
2477 default:
2478 kfree(info);
2479 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
2480 return -ENOMEM;
2481 }
2482
2483 rv = pci_enable_device(pdev);
2484 if (rv) {
2485 dev_err(&pdev->dev, "couldn't enable PCI device\n");
2486 kfree(info);
2487 return rv;
2488 }
2489
2490 info->addr_source_cleanup = ipmi_pci_cleanup;
2491 info->addr_source_data = pdev;
2492
2493 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
2494 info->io_setup = port_setup;
2495 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2496 } else {
2497 info->io_setup = mem_setup;
2498 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2499 }
2500 info->io.addr_data = pci_resource_start(pdev, 0);
2501
2502 info->io.regspacing = ipmi_pci_probe_regspacing(info);
2503 info->io.regsize = DEFAULT_REGSIZE;
2504 info->io.regshift = 0;
2505
2506 info->irq = pdev->irq;
2507 if (info->irq)
2508 info->irq_setup = std_irq_setup;
2509 1282
2510 info->dev = &pdev->dev; 1283 if (!io->irq)
2511 pci_set_drvdata(pdev, info); 1284 return 0;
2512
2513 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
2514 &pdev->resource[0], info->io.regsize, info->io.regspacing,
2515 info->irq);
2516 1285
2517 rv = add_smi(info); 1286 rv = request_irq(io->irq,
1287 ipmi_si_irq_handler,
1288 IRQF_SHARED,
1289 DEVICE_NAME,
1290 io->irq_handler_data);
2518 if (rv) { 1291 if (rv) {
2519 kfree(info); 1292 dev_warn(io->dev, "%s unable to claim interrupt %d,"
2520 pci_disable_device(pdev); 1293 " running polled\n",
2521 } 1294 DEVICE_NAME, io->irq);
2522 1295 io->irq = 0;
2523 return rv;
2524}
2525
2526static void ipmi_pci_remove(struct pci_dev *pdev)
2527{
2528 struct smi_info *info = pci_get_drvdata(pdev);
2529 cleanup_one_si(info);
2530}
2531
2532static const struct pci_device_id ipmi_pci_devices[] = {
2533 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
2534 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
2535 { 0, }
2536};
2537MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
2538
2539static struct pci_driver ipmi_pci_driver = {
2540 .name = DEVICE_NAME,
2541 .id_table = ipmi_pci_devices,
2542 .probe = ipmi_pci_probe,
2543 .remove = ipmi_pci_remove,
2544};
2545#endif /* CONFIG_PCI */
2546
2547#ifdef CONFIG_OF
2548static const struct of_device_id of_ipmi_match[] = {
2549 { .type = "ipmi", .compatible = "ipmi-kcs",
2550 .data = (void *)(unsigned long) SI_KCS },
2551 { .type = "ipmi", .compatible = "ipmi-smic",
2552 .data = (void *)(unsigned long) SI_SMIC },
2553 { .type = "ipmi", .compatible = "ipmi-bt",
2554 .data = (void *)(unsigned long) SI_BT },
2555 {},
2556};
2557MODULE_DEVICE_TABLE(of, of_ipmi_match);
2558
2559static int of_ipmi_probe(struct platform_device *dev)
2560{
2561 const struct of_device_id *match;
2562 struct smi_info *info;
2563 struct resource resource;
2564 const __be32 *regsize, *regspacing, *regshift;
2565 struct device_node *np = dev->dev.of_node;
2566 int ret;
2567 int proplen;
2568
2569 dev_info(&dev->dev, "probing via device tree\n");
2570
2571 match = of_match_device(of_ipmi_match, &dev->dev);
2572 if (!match)
2573 return -ENODEV;
2574
2575 if (!of_device_is_available(np))
2576 return -EINVAL;
2577
2578 ret = of_address_to_resource(np, 0, &resource);
2579 if (ret) {
2580 dev_warn(&dev->dev, PFX "invalid address from OF\n");
2581 return ret;
2582 }
2583
2584 regsize = of_get_property(np, "reg-size", &proplen);
2585 if (regsize && proplen != 4) {
2586 dev_warn(&dev->dev, PFX "invalid regsize from OF\n");
2587 return -EINVAL;
2588 }
2589
2590 regspacing = of_get_property(np, "reg-spacing", &proplen);
2591 if (regspacing && proplen != 4) {
2592 dev_warn(&dev->dev, PFX "invalid regspacing from OF\n");
2593 return -EINVAL;
2594 }
2595
2596 regshift = of_get_property(np, "reg-shift", &proplen);
2597 if (regshift && proplen != 4) {
2598 dev_warn(&dev->dev, PFX "invalid regshift from OF\n");
2599 return -EINVAL;
2600 }
2601
2602 info = smi_info_alloc();
2603
2604 if (!info) {
2605 dev_err(&dev->dev,
2606 "could not allocate memory for OF probe\n");
2607 return -ENOMEM;
2608 }
2609
2610 info->si_type = (enum si_type) match->data;
2611 info->addr_source = SI_DEVICETREE;
2612 info->irq_setup = std_irq_setup;
2613
2614 if (resource.flags & IORESOURCE_IO) {
2615 info->io_setup = port_setup;
2616 info->io.addr_type = IPMI_IO_ADDR_SPACE;
2617 } else {
2618 info->io_setup = mem_setup;
2619 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2620 }
2621
2622 info->io.addr_data = resource.start;
2623
2624 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2625 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2626 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
2627
2628 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
2629 info->dev = &dev->dev;
2630
2631 dev_dbg(&dev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
2632 info->io.addr_data, info->io.regsize, info->io.regspacing,
2633 info->irq);
2634
2635 dev_set_drvdata(&dev->dev, info);
2636
2637 ret = add_smi(info);
2638 if (ret) {
2639 kfree(info);
2640 return ret;
2641 }
2642 return 0;
2643}
2644#else
2645#define of_ipmi_match NULL
2646static int of_ipmi_probe(struct platform_device *dev)
2647{
2648 return -ENODEV;
2649}
2650#endif
2651
2652#ifdef CONFIG_ACPI
2653static int find_slave_address(struct smi_info *info, int slave_addr)
2654{
2655#ifdef CONFIG_IPMI_DMI_DECODE
2656 if (!slave_addr) {
2657 int type = -1;
2658 u32 flags = IORESOURCE_IO;
2659
2660 switch (info->si_type) {
2661 case SI_KCS:
2662 type = IPMI_DMI_TYPE_KCS;
2663 break;
2664 case SI_BT:
2665 type = IPMI_DMI_TYPE_BT;
2666 break;
2667 case SI_SMIC:
2668 type = IPMI_DMI_TYPE_SMIC;
2669 break;
2670 }
2671
2672 if (info->io.addr_type == IPMI_MEM_ADDR_SPACE)
2673 flags = IORESOURCE_MEM;
2674
2675 slave_addr = ipmi_dmi_get_slave_addr(type, flags,
2676 info->io.addr_data);
2677 }
2678#endif
2679
2680 return slave_addr;
2681}
2682
2683static int acpi_ipmi_probe(struct platform_device *dev)
2684{
2685 struct smi_info *info;
2686 acpi_handle handle;
2687 acpi_status status;
2688 unsigned long long tmp;
2689 struct resource *res;
2690 int rv = -EINVAL;
2691
2692 if (!si_tryacpi)
2693 return -ENODEV;
2694
2695 handle = ACPI_HANDLE(&dev->dev);
2696 if (!handle)
2697 return -ENODEV;
2698
2699 info = smi_info_alloc();
2700 if (!info)
2701 return -ENOMEM;
2702
2703 info->addr_source = SI_ACPI;
2704 dev_info(&dev->dev, PFX "probing via ACPI\n");
2705
2706 info->addr_info.acpi_info.acpi_handle = handle;
2707
2708 /* _IFT tells us the interface type: KCS, BT, etc */
2709 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
2710 if (ACPI_FAILURE(status)) {
2711 dev_err(&dev->dev, "Could not find ACPI IPMI interface type\n");
2712 goto err_free;
2713 }
2714
2715 switch (tmp) {
2716 case 1:
2717 info->si_type = SI_KCS;
2718 break;
2719 case 2:
2720 info->si_type = SI_SMIC;
2721 break;
2722 case 3:
2723 info->si_type = SI_BT;
2724 break;
2725 case 4: /* SSIF, just ignore */
2726 rv = -ENODEV;
2727 goto err_free;
2728 default:
2729 dev_info(&dev->dev, "unknown IPMI type %lld\n", tmp);
2730 goto err_free;
2731 }
2732
2733 res = ipmi_get_info_from_resources(dev, info);
2734 if (!res) {
2735 rv = -EINVAL;
2736 goto err_free;
2737 }
2738
2739 /* If _GPE exists, use it; otherwise use standard interrupts */
2740 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
2741 if (ACPI_SUCCESS(status)) {
2742 info->irq = tmp;
2743 info->irq_setup = acpi_gpe_irq_setup;
2744 } else { 1296 } else {
2745 int irq = platform_get_irq(dev, 0); 1297 io->irq_cleanup = std_irq_cleanup;
2746 1298 ipmi_irq_finish_setup(io);
2747 if (irq > 0) { 1299 dev_info(io->dev, "Using irq %d\n", io->irq);
2748 info->irq = irq;
2749 info->irq_setup = std_irq_setup;
2750 }
2751 } 1300 }
2752 1301
2753 info->slave_addr = find_slave_address(info, info->slave_addr);
2754
2755 info->dev = &dev->dev;
2756 platform_set_drvdata(dev, info);
2757
2758 dev_info(info->dev, "%pR regsize %d spacing %d irq %d\n",
2759 res, info->io.regsize, info->io.regspacing,
2760 info->irq);
2761
2762 rv = add_smi(info);
2763 if (rv)
2764 kfree(info);
2765
2766 return rv;
2767
2768err_free:
2769 kfree(info);
2770 return rv; 1302 return rv;
2771} 1303}
2772 1304
2773static const struct acpi_device_id acpi_ipmi_match[] = {
2774 { "IPI0001", 0 },
2775 { },
2776};
2777MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
2778#else
2779static int acpi_ipmi_probe(struct platform_device *dev)
2780{
2781 return -ENODEV;
2782}
2783#endif
2784
2785static int ipmi_probe(struct platform_device *dev)
2786{
2787 if (of_ipmi_probe(dev) == 0)
2788 return 0;
2789
2790 if (acpi_ipmi_probe(dev) == 0)
2791 return 0;
2792
2793 return dmi_ipmi_probe(dev);
2794}
2795
2796static int ipmi_remove(struct platform_device *dev)
2797{
2798 struct smi_info *info = dev_get_drvdata(&dev->dev);
2799
2800 cleanup_one_si(info);
2801 return 0;
2802}
2803
2804static struct platform_driver ipmi_driver = {
2805 .driver = {
2806 .name = DEVICE_NAME,
2807 .of_match_table = of_ipmi_match,
2808 .acpi_match_table = ACPI_PTR(acpi_ipmi_match),
2809 },
2810 .probe = ipmi_probe,
2811 .remove = ipmi_remove,
2812};
2813
2814#ifdef CONFIG_PARISC
2815static int __init ipmi_parisc_probe(struct parisc_device *dev)
2816{
2817 struct smi_info *info;
2818 int rv;
2819
2820 info = smi_info_alloc();
2821
2822 if (!info) {
2823 dev_err(&dev->dev,
2824 "could not allocate memory for PARISC probe\n");
2825 return -ENOMEM;
2826 }
2827
2828 info->si_type = SI_KCS;
2829 info->addr_source = SI_DEVICETREE;
2830 info->io_setup = mem_setup;
2831 info->io.addr_type = IPMI_MEM_ADDR_SPACE;
2832 info->io.addr_data = dev->hpa.start;
2833 info->io.regsize = 1;
2834 info->io.regspacing = 1;
2835 info->io.regshift = 0;
2836 info->irq = 0; /* no interrupt */
2837 info->irq_setup = NULL;
2838 info->dev = &dev->dev;
2839
2840 dev_dbg(&dev->dev, "addr 0x%lx\n", info->io.addr_data);
2841
2842 dev_set_drvdata(&dev->dev, info);
2843
2844 rv = add_smi(info);
2845 if (rv) {
2846 kfree(info);
2847 return rv;
2848 }
2849
2850 return 0;
2851}
2852
2853static int __exit ipmi_parisc_remove(struct parisc_device *dev)
2854{
2855 cleanup_one_si(dev_get_drvdata(&dev->dev));
2856 return 0;
2857}
2858
2859static const struct parisc_device_id ipmi_parisc_tbl[] __initconst = {
2860 { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
2861 { 0, }
2862};
2863
2864MODULE_DEVICE_TABLE(parisc, ipmi_parisc_tbl);
2865
2866static struct parisc_driver ipmi_parisc_driver __refdata = {
2867 .name = "ipmi",
2868 .id_table = ipmi_parisc_tbl,
2869 .probe = ipmi_parisc_probe,
2870 .remove = __exit_p(ipmi_parisc_remove),
2871};
2872#endif /* CONFIG_PARISC */
2873
2874static int wait_for_msg_done(struct smi_info *smi_info) 1305static int wait_for_msg_done(struct smi_info *smi_info)
2875{ 1306{
2876 enum si_sm_result smi_result; 1307 enum si_sm_result smi_result;
@@ -2925,7 +1356,8 @@ static int try_get_dev_id(struct smi_info *smi_info)
2925 resp, IPMI_MAX_MSG_LENGTH); 1356 resp, IPMI_MAX_MSG_LENGTH);
2926 1357
2927 /* Check and record info from the get device id, in case we need it. */ 1358 /* Check and record info from the get device id, in case we need it. */
2928 rv = ipmi_demangle_device_id(resp, resp_len, &smi_info->device_id); 1359 rv = ipmi_demangle_device_id(resp[0] >> 2, resp[1],
1360 resp + 2, resp_len - 2, &smi_info->device_id);
2929 1361
2930out: 1362out:
2931 kfree(resp); 1363 kfree(resp);
@@ -2949,7 +1381,7 @@ static int get_global_enables(struct smi_info *smi_info, u8 *enables)
2949 1381
2950 rv = wait_for_msg_done(smi_info); 1382 rv = wait_for_msg_done(smi_info);
2951 if (rv) { 1383 if (rv) {
2952 dev_warn(smi_info->dev, 1384 dev_warn(smi_info->io.dev,
2953 "Error getting response from get global enables command: %d\n", 1385 "Error getting response from get global enables command: %d\n",
2954 rv); 1386 rv);
2955 goto out; 1387 goto out;
@@ -2962,7 +1394,7 @@ static int get_global_enables(struct smi_info *smi_info, u8 *enables)
2962 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 || 1394 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
2963 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD || 1395 resp[1] != IPMI_GET_BMC_GLOBAL_ENABLES_CMD ||
2964 resp[2] != 0) { 1396 resp[2] != 0) {
2965 dev_warn(smi_info->dev, 1397 dev_warn(smi_info->io.dev,
2966 "Invalid return from get global enables command: %ld %x %x %x\n", 1398 "Invalid return from get global enables command: %ld %x %x %x\n",
2967 resp_len, resp[0], resp[1], resp[2]); 1399 resp_len, resp[0], resp[1], resp[2]);
2968 rv = -EINVAL; 1400 rv = -EINVAL;
@@ -2997,7 +1429,7 @@ static int set_global_enables(struct smi_info *smi_info, u8 enables)
2997 1429
2998 rv = wait_for_msg_done(smi_info); 1430 rv = wait_for_msg_done(smi_info);
2999 if (rv) { 1431 if (rv) {
3000 dev_warn(smi_info->dev, 1432 dev_warn(smi_info->io.dev,
3001 "Error getting response from set global enables command: %d\n", 1433 "Error getting response from set global enables command: %d\n",
3002 rv); 1434 rv);
3003 goto out; 1435 goto out;
@@ -3009,7 +1441,7 @@ static int set_global_enables(struct smi_info *smi_info, u8 enables)
3009 if (resp_len < 3 || 1441 if (resp_len < 3 ||
3010 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 || 1442 resp[0] != (IPMI_NETFN_APP_REQUEST | 1) << 2 ||
3011 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) { 1443 resp[1] != IPMI_SET_BMC_GLOBAL_ENABLES_CMD) {
3012 dev_warn(smi_info->dev, 1444 dev_warn(smi_info->io.dev,
3013 "Invalid return from set global enables command: %ld %x %x\n", 1445 "Invalid return from set global enables command: %ld %x %x\n",
3014 resp_len, resp[0], resp[1]); 1446 resp_len, resp[0], resp[1]);
3015 rv = -EINVAL; 1447 rv = -EINVAL;
@@ -3045,7 +1477,7 @@ static void check_clr_rcv_irq(struct smi_info *smi_info)
3045 } 1477 }
3046 1478
3047 if (rv < 0) { 1479 if (rv < 0) {
3048 dev_err(smi_info->dev, 1480 dev_err(smi_info->io.dev,
3049 "Cannot check clearing the rcv irq: %d\n", rv); 1481 "Cannot check clearing the rcv irq: %d\n", rv);
3050 return; 1482 return;
3051 } 1483 }
@@ -3055,7 +1487,7 @@ static void check_clr_rcv_irq(struct smi_info *smi_info)
3055 * An error when setting the event buffer bit means 1487 * An error when setting the event buffer bit means
3056 * clearing the bit is not supported. 1488 * clearing the bit is not supported.
3057 */ 1489 */
3058 dev_warn(smi_info->dev, 1490 dev_warn(smi_info->io.dev,
3059 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n"); 1491 "The BMC does not support clearing the recv irq bit, compensating, but the BMC needs to be fixed.\n");
3060 smi_info->cannot_disable_irq = true; 1492 smi_info->cannot_disable_irq = true;
3061 } 1493 }
@@ -3071,7 +1503,7 @@ static void check_set_rcv_irq(struct smi_info *smi_info)
3071 u8 enables = 0; 1503 u8 enables = 0;
3072 int rv; 1504 int rv;
3073 1505
3074 if (!smi_info->irq) 1506 if (!smi_info->io.irq)
3075 return; 1507 return;
3076 1508
3077 rv = get_global_enables(smi_info, &enables); 1509 rv = get_global_enables(smi_info, &enables);
@@ -3081,7 +1513,7 @@ static void check_set_rcv_irq(struct smi_info *smi_info)
3081 } 1513 }
3082 1514
3083 if (rv < 0) { 1515 if (rv < 0) {
3084 dev_err(smi_info->dev, 1516 dev_err(smi_info->io.dev,
3085 "Cannot check setting the rcv irq: %d\n", rv); 1517 "Cannot check setting the rcv irq: %d\n", rv);
3086 return; 1518 return;
3087 } 1519 }
@@ -3091,7 +1523,7 @@ static void check_set_rcv_irq(struct smi_info *smi_info)
3091 * An error when setting the event buffer bit means 1523 * An error when setting the event buffer bit means
3092 * setting the bit is not supported. 1524 * setting the bit is not supported.
3093 */ 1525 */
3094 dev_warn(smi_info->dev, 1526 dev_warn(smi_info->io.dev,
3095 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n"); 1527 "The BMC does not support setting the recv irq bit, compensating, but the BMC needs to be fixed.\n");
3096 smi_info->cannot_disable_irq = true; 1528 smi_info->cannot_disable_irq = true;
3097 smi_info->irq_enable_broken = true; 1529 smi_info->irq_enable_broken = true;
@@ -3173,11 +1605,12 @@ out:
3173 return rv; 1605 return rv;
3174} 1606}
3175 1607
1608#ifdef CONFIG_IPMI_PROC_INTERFACE
3176static int smi_type_proc_show(struct seq_file *m, void *v) 1609static int smi_type_proc_show(struct seq_file *m, void *v)
3177{ 1610{
3178 struct smi_info *smi = m->private; 1611 struct smi_info *smi = m->private;
3179 1612
3180 seq_printf(m, "%s\n", si_to_str[smi->si_type]); 1613 seq_printf(m, "%s\n", si_to_str[smi->io.si_type]);
3181 1614
3182 return 0; 1615 return 0;
3183} 1616}
@@ -3199,7 +1632,7 @@ static int smi_si_stats_proc_show(struct seq_file *m, void *v)
3199 struct smi_info *smi = m->private; 1632 struct smi_info *smi = m->private;
3200 1633
3201 seq_printf(m, "interrupts_enabled: %d\n", 1634 seq_printf(m, "interrupts_enabled: %d\n",
3202 smi->irq && !smi->interrupt_disabled); 1635 smi->io.irq && !smi->interrupt_disabled);
3203 seq_printf(m, "short_timeouts: %u\n", 1636 seq_printf(m, "short_timeouts: %u\n",
3204 smi_get_stat(smi, short_timeouts)); 1637 smi_get_stat(smi, short_timeouts));
3205 seq_printf(m, "long_timeouts: %u\n", 1638 seq_printf(m, "long_timeouts: %u\n",
@@ -3243,14 +1676,14 @@ static int smi_params_proc_show(struct seq_file *m, void *v)
3243 1676
3244 seq_printf(m, 1677 seq_printf(m,
3245 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n", 1678 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
3246 si_to_str[smi->si_type], 1679 si_to_str[smi->io.si_type],
3247 addr_space_to_str[smi->io.addr_type], 1680 addr_space_to_str[smi->io.addr_type],
3248 smi->io.addr_data, 1681 smi->io.addr_data,
3249 smi->io.regspacing, 1682 smi->io.regspacing,
3250 smi->io.regsize, 1683 smi->io.regsize,
3251 smi->io.regshift, 1684 smi->io.regshift,
3252 smi->irq, 1685 smi->io.irq,
3253 smi->slave_addr); 1686 smi->io.slave_addr);
3254 1687
3255 return 0; 1688 return 0;
3256} 1689}
@@ -3266,6 +1699,93 @@ static const struct file_operations smi_params_proc_ops = {
3266 .llseek = seq_lseek, 1699 .llseek = seq_lseek,
3267 .release = single_release, 1700 .release = single_release,
3268}; 1701};
1702#endif
1703
1704#define IPMI_SI_ATTR(name) \
1705static ssize_t ipmi_##name##_show(struct device *dev, \
1706 struct device_attribute *attr, \
1707 char *buf) \
1708{ \
1709 struct smi_info *smi_info = dev_get_drvdata(dev); \
1710 \
1711 return snprintf(buf, 10, "%u\n", smi_get_stat(smi_info, name)); \
1712} \
1713static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1714
1715static ssize_t ipmi_type_show(struct device *dev,
1716 struct device_attribute *attr,
1717 char *buf)
1718{
1719 struct smi_info *smi_info = dev_get_drvdata(dev);
1720
1721 return snprintf(buf, 10, "%s\n", si_to_str[smi_info->io.si_type]);
1722}
1723static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1724
1725static ssize_t ipmi_interrupts_enabled_show(struct device *dev,
1726 struct device_attribute *attr,
1727 char *buf)
1728{
1729 struct smi_info *smi_info = dev_get_drvdata(dev);
1730 int enabled = smi_info->io.irq && !smi_info->interrupt_disabled;
1731
1732 return snprintf(buf, 10, "%d\n", enabled);
1733}
1734static DEVICE_ATTR(interrupts_enabled, S_IRUGO,
1735 ipmi_interrupts_enabled_show, NULL);
1736
1737IPMI_SI_ATTR(short_timeouts);
1738IPMI_SI_ATTR(long_timeouts);
1739IPMI_SI_ATTR(idles);
1740IPMI_SI_ATTR(interrupts);
1741IPMI_SI_ATTR(attentions);
1742IPMI_SI_ATTR(flag_fetches);
1743IPMI_SI_ATTR(hosed_count);
1744IPMI_SI_ATTR(complete_transactions);
1745IPMI_SI_ATTR(events);
1746IPMI_SI_ATTR(watchdog_pretimeouts);
1747IPMI_SI_ATTR(incoming_messages);
1748
1749static ssize_t ipmi_params_show(struct device *dev,
1750 struct device_attribute *attr,
1751 char *buf)
1752{
1753 struct smi_info *smi_info = dev_get_drvdata(dev);
1754
1755 return snprintf(buf, 200,
1756 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
1757 si_to_str[smi_info->io.si_type],
1758 addr_space_to_str[smi_info->io.addr_type],
1759 smi_info->io.addr_data,
1760 smi_info->io.regspacing,
1761 smi_info->io.regsize,
1762 smi_info->io.regshift,
1763 smi_info->io.irq,
1764 smi_info->io.slave_addr);
1765}
1766static DEVICE_ATTR(params, S_IRUGO, ipmi_params_show, NULL);
1767
1768static struct attribute *ipmi_si_dev_attrs[] = {
1769 &dev_attr_type.attr,
1770 &dev_attr_interrupts_enabled.attr,
1771 &dev_attr_short_timeouts.attr,
1772 &dev_attr_long_timeouts.attr,
1773 &dev_attr_idles.attr,
1774 &dev_attr_interrupts.attr,
1775 &dev_attr_attentions.attr,
1776 &dev_attr_flag_fetches.attr,
1777 &dev_attr_hosed_count.attr,
1778 &dev_attr_complete_transactions.attr,
1779 &dev_attr_events.attr,
1780 &dev_attr_watchdog_pretimeouts.attr,
1781 &dev_attr_incoming_messages.attr,
1782 &dev_attr_params.attr,
1783 NULL
1784};
1785
1786static const struct attribute_group ipmi_si_dev_attr_group = {
1787 .attrs = ipmi_si_dev_attrs,
1788};
3269 1789
3270/* 1790/*
3271 * oem_data_avail_to_receive_msg_avail 1791 * oem_data_avail_to_receive_msg_avail
@@ -3388,7 +1908,7 @@ setup_dell_poweredge_bt_xaction_handler(struct smi_info *smi_info)
3388{ 1908{
3389 struct ipmi_device_id *id = &smi_info->device_id; 1909 struct ipmi_device_id *id = &smi_info->device_id;
3390 if (id->manufacturer_id == DELL_IANA_MFR_ID && 1910 if (id->manufacturer_id == DELL_IANA_MFR_ID &&
3391 smi_info->si_type == SI_BT) 1911 smi_info->io.si_type == SI_BT)
3392 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier); 1912 register_xaction_notifier(&dell_poweredge_bt_xaction_notifier);
3393} 1913}
3394 1914
@@ -3424,7 +1944,7 @@ static inline void wait_for_timer_and_thread(struct smi_info *smi_info)
3424 del_timer_sync(&smi_info->si_timer); 1944 del_timer_sync(&smi_info->si_timer);
3425} 1945}
3426 1946
3427static int is_new_interface(struct smi_info *info) 1947static struct smi_info *find_dup_si(struct smi_info *info)
3428{ 1948{
3429 struct smi_info *e; 1949 struct smi_info *e;
3430 1950
@@ -3437,31 +1957,61 @@ static int is_new_interface(struct smi_info *info)
3437 * slave address but SMBIOS does. Pick it up from 1957 * slave address but SMBIOS does. Pick it up from
3438 * any source that has it available. 1958 * any source that has it available.
3439 */ 1959 */
3440 if (info->slave_addr && !e->slave_addr) 1960 if (info->io.slave_addr && !e->io.slave_addr)
3441 e->slave_addr = info->slave_addr; 1961 e->io.slave_addr = info->io.slave_addr;
3442 return 0; 1962 return e;
3443 } 1963 }
3444 } 1964 }
3445 1965
3446 return 1; 1966 return NULL;
3447} 1967}
3448 1968
3449static int add_smi(struct smi_info *new_smi) 1969int ipmi_si_add_smi(struct si_sm_io *io)
3450{ 1970{
3451 int rv = 0; 1971 int rv = 0;
1972 struct smi_info *new_smi, *dup;
1973
1974 if (!io->io_setup) {
1975 if (io->addr_type == IPMI_IO_ADDR_SPACE) {
1976 io->io_setup = ipmi_si_port_setup;
1977 } else if (io->addr_type == IPMI_MEM_ADDR_SPACE) {
1978 io->io_setup = ipmi_si_mem_setup;
1979 } else {
1980 return -EINVAL;
1981 }
1982 }
1983
1984 new_smi = kzalloc(sizeof(*new_smi), GFP_KERNEL);
1985 if (!new_smi)
1986 return -ENOMEM;
1987 spin_lock_init(&new_smi->si_lock);
1988
1989 new_smi->io = *io;
3452 1990
3453 mutex_lock(&smi_infos_lock); 1991 mutex_lock(&smi_infos_lock);
3454 if (!is_new_interface(new_smi)) { 1992 dup = find_dup_si(new_smi);
3455 pr_info(PFX "%s-specified %s state machine: duplicate\n", 1993 if (dup) {
3456 ipmi_addr_src_to_str(new_smi->addr_source), 1994 if (new_smi->io.addr_source == SI_ACPI &&
3457 si_to_str[new_smi->si_type]); 1995 dup->io.addr_source == SI_SMBIOS) {
3458 rv = -EBUSY; 1996 /* We prefer ACPI over SMBIOS. */
3459 goto out_err; 1997 dev_info(dup->io.dev,
1998 "Removing SMBIOS-specified %s state machine in favor of ACPI\n",
1999 si_to_str[new_smi->io.si_type]);
2000 cleanup_one_si(dup);
2001 } else {
2002 dev_info(new_smi->io.dev,
2003 "%s-specified %s state machine: duplicate\n",
2004 ipmi_addr_src_to_str(new_smi->io.addr_source),
2005 si_to_str[new_smi->io.si_type]);
2006 rv = -EBUSY;
2007 kfree(new_smi);
2008 goto out_err;
2009 }
3460 } 2010 }
3461 2011
3462 pr_info(PFX "Adding %s-specified %s state machine\n", 2012 pr_info(PFX "Adding %s-specified %s state machine\n",
3463 ipmi_addr_src_to_str(new_smi->addr_source), 2013 ipmi_addr_src_to_str(new_smi->io.addr_source),
3464 si_to_str[new_smi->si_type]); 2014 si_to_str[new_smi->io.si_type]);
3465 2015
3466 /* So we know not to free it unless we have allocated one. */ 2016 /* So we know not to free it unless we have allocated one. */
3467 new_smi->intf = NULL; 2017 new_smi->intf = NULL;
@@ -3470,6 +2020,14 @@ static int add_smi(struct smi_info *new_smi)
3470 2020
3471 list_add_tail(&new_smi->link, &smi_infos); 2021 list_add_tail(&new_smi->link, &smi_infos);
3472 2022
2023 if (initialized) {
2024 rv = try_smi_init(new_smi);
2025 if (rv) {
2026 mutex_unlock(&smi_infos_lock);
2027 cleanup_one_si(new_smi);
2028 return rv;
2029 }
2030 }
3473out_err: 2031out_err:
3474 mutex_unlock(&smi_infos_lock); 2032 mutex_unlock(&smi_infos_lock);
3475 return rv; 2033 return rv;
@@ -3487,13 +2045,13 @@ static int try_smi_init(struct smi_info *new_smi)
3487 char *init_name = NULL; 2045 char *init_name = NULL;
3488 2046
3489 pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n", 2047 pr_info(PFX "Trying %s-specified %s state machine at %s address 0x%lx, slave address 0x%x, irq %d\n",
3490 ipmi_addr_src_to_str(new_smi->addr_source), 2048 ipmi_addr_src_to_str(new_smi->io.addr_source),
3491 si_to_str[new_smi->si_type], 2049 si_to_str[new_smi->io.si_type],
3492 addr_space_to_str[new_smi->io.addr_type], 2050 addr_space_to_str[new_smi->io.addr_type],
3493 new_smi->io.addr_data, 2051 new_smi->io.addr_data,
3494 new_smi->slave_addr, new_smi->irq); 2052 new_smi->io.slave_addr, new_smi->io.irq);
3495 2053
3496 switch (new_smi->si_type) { 2054 switch (new_smi->io.si_type) {
3497 case SI_KCS: 2055 case SI_KCS:
3498 new_smi->handlers = &kcs_smi_handlers; 2056 new_smi->handlers = &kcs_smi_handlers;
3499 break; 2057 break;
@@ -3515,7 +2073,7 @@ static int try_smi_init(struct smi_info *new_smi)
3515 new_smi->intf_num = smi_num; 2073 new_smi->intf_num = smi_num;
3516 2074
3517 /* Do this early so it's available for logs. */ 2075 /* Do this early so it's available for logs. */
3518 if (!new_smi->dev) { 2076 if (!new_smi->io.dev) {
3519 init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d", 2077 init_name = kasprintf(GFP_KERNEL, "ipmi_si.%d",
3520 new_smi->intf_num); 2078 new_smi->intf_num);
3521 2079
@@ -3529,33 +2087,33 @@ static int try_smi_init(struct smi_info *new_smi)
3529 pr_err(PFX "Unable to allocate platform device\n"); 2087 pr_err(PFX "Unable to allocate platform device\n");
3530 goto out_err; 2088 goto out_err;
3531 } 2089 }
3532 new_smi->dev = &new_smi->pdev->dev; 2090 new_smi->io.dev = &new_smi->pdev->dev;
3533 new_smi->dev->driver = &ipmi_driver.driver; 2091 new_smi->io.dev->driver = &ipmi_platform_driver.driver;
3534 /* Nulled by device_add() */ 2092 /* Nulled by device_add() */
3535 new_smi->dev->init_name = init_name; 2093 new_smi->io.dev->init_name = init_name;
3536 } 2094 }
3537 2095
3538 /* Allocate the state machine's data and initialize it. */ 2096 /* Allocate the state machine's data and initialize it. */
3539 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL); 2097 new_smi->si_sm = kmalloc(new_smi->handlers->size(), GFP_KERNEL);
3540 if (!new_smi->si_sm) { 2098 if (!new_smi->si_sm) {
3541 pr_err(PFX "Could not allocate state machine memory\n");
3542 rv = -ENOMEM; 2099 rv = -ENOMEM;
3543 goto out_err; 2100 goto out_err;
3544 } 2101 }
3545 new_smi->io_size = new_smi->handlers->init_data(new_smi->si_sm, 2102 new_smi->io.io_size = new_smi->handlers->init_data(new_smi->si_sm,
3546 &new_smi->io); 2103 &new_smi->io);
3547 2104
3548 /* Now that we know the I/O size, we can set up the I/O. */ 2105 /* Now that we know the I/O size, we can set up the I/O. */
3549 rv = new_smi->io_setup(new_smi); 2106 rv = new_smi->io.io_setup(&new_smi->io);
3550 if (rv) { 2107 if (rv) {
3551 dev_err(new_smi->dev, "Could not set up I/O space\n"); 2108 dev_err(new_smi->io.dev, "Could not set up I/O space\n");
3552 goto out_err; 2109 goto out_err;
3553 } 2110 }
3554 2111
3555 /* Do low-level detection first. */ 2112 /* Do low-level detection first. */
3556 if (new_smi->handlers->detect(new_smi->si_sm)) { 2113 if (new_smi->handlers->detect(new_smi->si_sm)) {
3557 if (new_smi->addr_source) 2114 if (new_smi->io.addr_source)
3558 dev_err(new_smi->dev, "Interface detection failed\n"); 2115 dev_err(new_smi->io.dev,
2116 "Interface detection failed\n");
3559 rv = -ENODEV; 2117 rv = -ENODEV;
3560 goto out_err; 2118 goto out_err;
3561 } 2119 }
@@ -3566,8 +2124,9 @@ static int try_smi_init(struct smi_info *new_smi)
3566 */ 2124 */
3567 rv = try_get_dev_id(new_smi); 2125 rv = try_get_dev_id(new_smi);
3568 if (rv) { 2126 if (rv) {
3569 if (new_smi->addr_source) 2127 if (new_smi->io.addr_source)
3570 dev_err(new_smi->dev, "There appears to be no BMC at this location\n"); 2128 dev_err(new_smi->io.dev,
2129 "There appears to be no BMC at this location\n");
3571 goto out_err; 2130 goto out_err;
3572 } 2131 }
3573 2132
@@ -3599,7 +2158,7 @@ static int try_smi_init(struct smi_info *new_smi)
3599 * IRQ is defined to be set when non-zero. req_events will 2158 * IRQ is defined to be set when non-zero. req_events will
3600 * cause a global flags check that will enable interrupts. 2159 * cause a global flags check that will enable interrupts.
3601 */ 2160 */
3602 if (new_smi->irq) { 2161 if (new_smi->io.irq) {
3603 new_smi->interrupt_disabled = false; 2162 new_smi->interrupt_disabled = false;
3604 atomic_set(&new_smi->req_events, 1); 2163 atomic_set(&new_smi->req_events, 1);
3605 } 2164 }
@@ -3607,30 +2166,40 @@ static int try_smi_init(struct smi_info *new_smi)
3607 if (new_smi->pdev) { 2166 if (new_smi->pdev) {
3608 rv = platform_device_add(new_smi->pdev); 2167 rv = platform_device_add(new_smi->pdev);
3609 if (rv) { 2168 if (rv) {
3610 dev_err(new_smi->dev, 2169 dev_err(new_smi->io.dev,
3611 "Unable to register system interface device: %d\n", 2170 "Unable to register system interface device: %d\n",
3612 rv); 2171 rv);
3613 goto out_err; 2172 goto out_err;
3614 } 2173 }
3615 new_smi->dev_registered = true; 2174 }
2175
2176 dev_set_drvdata(new_smi->io.dev, new_smi);
2177 rv = device_add_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2178 if (rv) {
2179 dev_err(new_smi->io.dev,
2180 "Unable to add device attributes: error %d\n",
2181 rv);
2182 goto out_err_stop_timer;
3616 } 2183 }
3617 2184
3618 rv = ipmi_register_smi(&handlers, 2185 rv = ipmi_register_smi(&handlers,
3619 new_smi, 2186 new_smi,
3620 &new_smi->device_id, 2187 new_smi->io.dev,
3621 new_smi->dev, 2188 new_smi->io.slave_addr);
3622 new_smi->slave_addr);
3623 if (rv) { 2189 if (rv) {
3624 dev_err(new_smi->dev, "Unable to register device: error %d\n", 2190 dev_err(new_smi->io.dev,
2191 "Unable to register device: error %d\n",
3625 rv); 2192 rv);
3626 goto out_err_stop_timer; 2193 goto out_err_remove_attrs;
3627 } 2194 }
3628 2195
2196#ifdef CONFIG_IPMI_PROC_INTERFACE
3629 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type", 2197 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
3630 &smi_type_proc_ops, 2198 &smi_type_proc_ops,
3631 new_smi); 2199 new_smi);
3632 if (rv) { 2200 if (rv) {
3633 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 2201 dev_err(new_smi->io.dev,
2202 "Unable to create proc entry: %d\n", rv);
3634 goto out_err_stop_timer; 2203 goto out_err_stop_timer;
3635 } 2204 }
3636 2205
@@ -3638,7 +2207,8 @@ static int try_smi_init(struct smi_info *new_smi)
3638 &smi_si_stats_proc_ops, 2207 &smi_si_stats_proc_ops,
3639 new_smi); 2208 new_smi);
3640 if (rv) { 2209 if (rv) {
3641 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 2210 dev_err(new_smi->io.dev,
2211 "Unable to create proc entry: %d\n", rv);
3642 goto out_err_stop_timer; 2212 goto out_err_stop_timer;
3643 } 2213 }
3644 2214
@@ -3646,21 +2216,27 @@ static int try_smi_init(struct smi_info *new_smi)
3646 &smi_params_proc_ops, 2216 &smi_params_proc_ops,
3647 new_smi); 2217 new_smi);
3648 if (rv) { 2218 if (rv) {
3649 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 2219 dev_err(new_smi->io.dev,
2220 "Unable to create proc entry: %d\n", rv);
3650 goto out_err_stop_timer; 2221 goto out_err_stop_timer;
3651 } 2222 }
2223#endif
3652 2224
3653 /* Don't increment till we know we have succeeded. */ 2225 /* Don't increment till we know we have succeeded. */
3654 smi_num++; 2226 smi_num++;
3655 2227
3656 dev_info(new_smi->dev, "IPMI %s interface initialized\n", 2228 dev_info(new_smi->io.dev, "IPMI %s interface initialized\n",
3657 si_to_str[new_smi->si_type]); 2229 si_to_str[new_smi->io.si_type]);
3658 2230
3659 WARN_ON(new_smi->dev->init_name != NULL); 2231 WARN_ON(new_smi->io.dev->init_name != NULL);
3660 kfree(init_name); 2232 kfree(init_name);
3661 2233
3662 return 0; 2234 return 0;
3663 2235
2236out_err_remove_attrs:
2237 device_remove_group(new_smi->io.dev, &ipmi_si_dev_attr_group);
2238 dev_set_drvdata(new_smi->io.dev, NULL);
2239
3664out_err_stop_timer: 2240out_err_stop_timer:
3665 wait_for_timer_and_thread(new_smi); 2241 wait_for_timer_and_thread(new_smi);
3666 2242
@@ -3673,9 +2249,9 @@ out_err:
3673 ipmi_unregister_smi(intf); 2249 ipmi_unregister_smi(intf);
3674 } 2250 }
3675 2251
3676 if (new_smi->irq_cleanup) { 2252 if (new_smi->io.irq_cleanup) {
3677 new_smi->irq_cleanup(new_smi); 2253 new_smi->io.irq_cleanup(&new_smi->io);
3678 new_smi->irq_cleanup = NULL; 2254 new_smi->io.irq_cleanup = NULL;
3679 } 2255 }
3680 2256
3681 /* 2257 /*
@@ -3691,22 +2267,20 @@ out_err:
3691 kfree(new_smi->si_sm); 2267 kfree(new_smi->si_sm);
3692 new_smi->si_sm = NULL; 2268 new_smi->si_sm = NULL;
3693 } 2269 }
3694 if (new_smi->addr_source_cleanup) { 2270 if (new_smi->io.addr_source_cleanup) {
3695 new_smi->addr_source_cleanup(new_smi); 2271 new_smi->io.addr_source_cleanup(&new_smi->io);
3696 new_smi->addr_source_cleanup = NULL; 2272 new_smi->io.addr_source_cleanup = NULL;
3697 } 2273 }
3698 if (new_smi->io_cleanup) { 2274 if (new_smi->io.io_cleanup) {
3699 new_smi->io_cleanup(new_smi); 2275 new_smi->io.io_cleanup(&new_smi->io);
3700 new_smi->io_cleanup = NULL; 2276 new_smi->io.io_cleanup = NULL;
3701 } 2277 }
3702 2278
3703 if (new_smi->dev_registered) { 2279 if (new_smi->pdev) {
3704 platform_device_unregister(new_smi->pdev); 2280 platform_device_unregister(new_smi->pdev);
3705 new_smi->dev_registered = false;
3706 new_smi->pdev = NULL; 2281 new_smi->pdev = NULL;
3707 } else if (new_smi->pdev) { 2282 } else if (new_smi->pdev) {
3708 platform_device_put(new_smi->pdev); 2283 platform_device_put(new_smi->pdev);
3709 new_smi->pdev = NULL;
3710 } 2284 }
3711 2285
3712 kfree(init_name); 2286 kfree(init_name);
@@ -3716,97 +2290,57 @@ out_err:
3716 2290
3717static int init_ipmi_si(void) 2291static int init_ipmi_si(void)
3718{ 2292{
3719 int i;
3720 char *str;
3721 int rv;
3722 struct smi_info *e; 2293 struct smi_info *e;
3723 enum ipmi_addr_src type = SI_INVALID; 2294 enum ipmi_addr_src type = SI_INVALID;
3724 2295
3725 if (initialized) 2296 if (initialized)
3726 return 0; 2297 return 0;
3727 initialized = 1;
3728
3729 if (si_tryplatform) {
3730 rv = platform_driver_register(&ipmi_driver);
3731 if (rv) {
3732 pr_err(PFX "Unable to register driver: %d\n", rv);
3733 return rv;
3734 }
3735 }
3736
3737 /* Parse out the si_type string into its components. */
3738 str = si_type_str;
3739 if (*str != '\0') {
3740 for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
3741 si_type[i] = str;
3742 str = strchr(str, ',');
3743 if (str) {
3744 *str = '\0';
3745 str++;
3746 } else {
3747 break;
3748 }
3749 }
3750 }
3751 2298
3752 pr_info("IPMI System Interface driver.\n"); 2299 pr_info("IPMI System Interface driver.\n");
3753 2300
3754 /* If the user gave us a device, they presumably want us to use it */ 2301 /* If the user gave us a device, they presumably want us to use it */
3755 if (!hardcode_find_bmc()) 2302 if (!ipmi_si_hardcode_find_bmc())
3756 return 0; 2303 goto do_scan;
3757 2304
3758#ifdef CONFIG_PCI 2305 ipmi_si_platform_init();
3759 if (si_trypci) {
3760 rv = pci_register_driver(&ipmi_pci_driver);
3761 if (rv)
3762 pr_err(PFX "Unable to register PCI driver: %d\n", rv);
3763 else
3764 pci_registered = true;
3765 }
3766#endif
3767 2306
3768#ifdef CONFIG_ACPI 2307 ipmi_si_pci_init();
3769 if (si_tryacpi)
3770 spmi_find_bmc();
3771#endif
3772 2308
3773#ifdef CONFIG_PARISC 2309 ipmi_si_parisc_init();
3774 register_parisc_driver(&ipmi_parisc_driver);
3775 parisc_registered = true;
3776#endif
3777 2310
3778 /* We prefer devices with interrupts, but in the case of a machine 2311 /* We prefer devices with interrupts, but in the case of a machine
3779 with multiple BMCs we assume that there will be several instances 2312 with multiple BMCs we assume that there will be several instances
3780 of a given type so if we succeed in registering a type then also 2313 of a given type so if we succeed in registering a type then also
3781 try to register everything else of the same type */ 2314 try to register everything else of the same type */
3782 2315do_scan:
3783 mutex_lock(&smi_infos_lock); 2316 mutex_lock(&smi_infos_lock);
3784 list_for_each_entry(e, &smi_infos, link) { 2317 list_for_each_entry(e, &smi_infos, link) {
3785 /* Try to register a device if it has an IRQ and we either 2318 /* Try to register a device if it has an IRQ and we either
3786 haven't successfully registered a device yet or this 2319 haven't successfully registered a device yet or this
3787 device has the same type as one we successfully registered */ 2320 device has the same type as one we successfully registered */
3788 if (e->irq && (!type || e->addr_source == type)) { 2321 if (e->io.irq && (!type || e->io.addr_source == type)) {
3789 if (!try_smi_init(e)) { 2322 if (!try_smi_init(e)) {
3790 type = e->addr_source; 2323 type = e->io.addr_source;
3791 } 2324 }
3792 } 2325 }
3793 } 2326 }
3794 2327
3795 /* type will only have been set if we successfully registered an si */ 2328 /* type will only have been set if we successfully registered an si */
3796 if (type) { 2329 if (type)
3797 mutex_unlock(&smi_infos_lock); 2330 goto skip_fallback_noirq;
3798 return 0;
3799 }
3800 2331
3801 /* Fall back to the preferred device */ 2332 /* Fall back to the preferred device */
3802 2333
3803 list_for_each_entry(e, &smi_infos, link) { 2334 list_for_each_entry(e, &smi_infos, link) {
3804 if (!e->irq && (!type || e->addr_source == type)) { 2335 if (!e->io.irq && (!type || e->io.addr_source == type)) {
3805 if (!try_smi_init(e)) { 2336 if (!try_smi_init(e)) {
3806 type = e->addr_source; 2337 type = e->io.addr_source;
3807 } 2338 }
3808 } 2339 }
3809 } 2340 }
2341
2342skip_fallback_noirq:
2343 initialized = 1;
3810 mutex_unlock(&smi_infos_lock); 2344 mutex_unlock(&smi_infos_lock);
3811 2345
3812 if (type) 2346 if (type)
@@ -3843,8 +2377,8 @@ static void cleanup_one_si(struct smi_info *to_clean)
3843 } 2377 }
3844 } 2378 }
3845 2379
3846 if (to_clean->dev) 2380 device_remove_group(to_clean->io.dev, &ipmi_si_dev_attr_group);
3847 dev_set_drvdata(to_clean->dev, NULL); 2381 dev_set_drvdata(to_clean->io.dev, NULL);
3848 2382
3849 list_del(&to_clean->link); 2383 list_del(&to_clean->link);
3850 2384
@@ -3852,8 +2386,8 @@ static void cleanup_one_si(struct smi_info *to_clean)
3852 * Make sure that interrupts, the timer and the thread are 2386 * Make sure that interrupts, the timer and the thread are
3853 * stopped and will not run again. 2387 * stopped and will not run again.
3854 */ 2388 */
3855 if (to_clean->irq_cleanup) 2389 if (to_clean->io.irq_cleanup)
3856 to_clean->irq_cleanup(to_clean); 2390 to_clean->io.irq_cleanup(&to_clean->io);
3857 wait_for_timer_and_thread(to_clean); 2391 wait_for_timer_and_thread(to_clean);
3858 2392
3859 /* 2393 /*
@@ -3865,7 +2399,8 @@ static void cleanup_one_si(struct smi_info *to_clean)
3865 poll(to_clean); 2399 poll(to_clean);
3866 schedule_timeout_uninterruptible(1); 2400 schedule_timeout_uninterruptible(1);
3867 } 2401 }
3868 disable_si_irq(to_clean, false); 2402 if (to_clean->handlers)
2403 disable_si_irq(to_clean, false);
3869 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) { 2404 while (to_clean->curr_msg || (to_clean->si_state != SI_NORMAL)) {
3870 poll(to_clean); 2405 poll(to_clean);
3871 schedule_timeout_uninterruptible(1); 2406 schedule_timeout_uninterruptible(1);
@@ -3876,17 +2411,53 @@ static void cleanup_one_si(struct smi_info *to_clean)
3876 2411
3877 kfree(to_clean->si_sm); 2412 kfree(to_clean->si_sm);
3878 2413
3879 if (to_clean->addr_source_cleanup) 2414 if (to_clean->io.addr_source_cleanup)
3880 to_clean->addr_source_cleanup(to_clean); 2415 to_clean->io.addr_source_cleanup(&to_clean->io);
3881 if (to_clean->io_cleanup) 2416 if (to_clean->io.io_cleanup)
3882 to_clean->io_cleanup(to_clean); 2417 to_clean->io.io_cleanup(&to_clean->io);
3883 2418
3884 if (to_clean->dev_registered) 2419 if (to_clean->pdev)
3885 platform_device_unregister(to_clean->pdev); 2420 platform_device_unregister(to_clean->pdev);
3886 2421
3887 kfree(to_clean); 2422 kfree(to_clean);
3888} 2423}
3889 2424
2425int ipmi_si_remove_by_dev(struct device *dev)
2426{
2427 struct smi_info *e;
2428 int rv = -ENOENT;
2429
2430 mutex_lock(&smi_infos_lock);
2431 list_for_each_entry(e, &smi_infos, link) {
2432 if (e->io.dev == dev) {
2433 cleanup_one_si(e);
2434 rv = 0;
2435 break;
2436 }
2437 }
2438 mutex_unlock(&smi_infos_lock);
2439
2440 return rv;
2441}
2442
2443void ipmi_si_remove_by_data(int addr_space, enum si_type si_type,
2444 unsigned long addr)
2445{
2446 /* remove */
2447 struct smi_info *e, *tmp_e;
2448
2449 mutex_lock(&smi_infos_lock);
2450 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) {
2451 if (e->io.addr_type != addr_space)
2452 continue;
2453 if (e->io.si_type != si_type)
2454 continue;
2455 if (e->io.addr_data == addr)
2456 cleanup_one_si(e);
2457 }
2458 mutex_unlock(&smi_infos_lock);
2459}
2460
3890static void cleanup_ipmi_si(void) 2461static void cleanup_ipmi_si(void)
3891{ 2462{
3892 struct smi_info *e, *tmp_e; 2463 struct smi_info *e, *tmp_e;
@@ -3894,16 +2465,11 @@ static void cleanup_ipmi_si(void)
3894 if (!initialized) 2465 if (!initialized)
3895 return; 2466 return;
3896 2467
3897#ifdef CONFIG_PCI 2468 ipmi_si_pci_shutdown();
3898 if (pci_registered) 2469
3899 pci_unregister_driver(&ipmi_pci_driver); 2470 ipmi_si_parisc_shutdown();
3900#endif
3901#ifdef CONFIG_PARISC
3902 if (parisc_registered)
3903 unregister_parisc_driver(&ipmi_parisc_driver);
3904#endif
3905 2471
3906 platform_driver_unregister(&ipmi_driver); 2472 ipmi_si_platform_shutdown();
3907 2473
3908 mutex_lock(&smi_infos_lock); 2474 mutex_lock(&smi_infos_lock);
3909 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) 2475 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
diff --git a/drivers/char/ipmi/ipmi_si_mem_io.c b/drivers/char/ipmi/ipmi_si_mem_io.c
new file mode 100644
index 000000000000..8796396ecd0f
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_mem_io.c
@@ -0,0 +1,144 @@
1
2#include <linux/io.h>
3#include "ipmi_si.h"
4
5static unsigned char intf_mem_inb(const struct si_sm_io *io,
6 unsigned int offset)
7{
8 return readb((io->addr)+(offset * io->regspacing));
9}
10
11static void intf_mem_outb(const struct si_sm_io *io, unsigned int offset,
12 unsigned char b)
13{
14 writeb(b, (io->addr)+(offset * io->regspacing));
15}
16
17static unsigned char intf_mem_inw(const struct si_sm_io *io,
18 unsigned int offset)
19{
20 return (readw((io->addr)+(offset * io->regspacing)) >> io->regshift)
21 & 0xff;
22}
23
24static void intf_mem_outw(const struct si_sm_io *io, unsigned int offset,
25 unsigned char b)
26{
27 writeb(b << io->regshift, (io->addr)+(offset * io->regspacing));
28}
29
30static unsigned char intf_mem_inl(const struct si_sm_io *io,
31 unsigned int offset)
32{
33 return (readl((io->addr)+(offset * io->regspacing)) >> io->regshift)
34 & 0xff;
35}
36
37static void intf_mem_outl(const struct si_sm_io *io, unsigned int offset,
38 unsigned char b)
39{
40 writel(b << io->regshift, (io->addr)+(offset * io->regspacing));
41}
42
43#ifdef readq
44static unsigned char mem_inq(const struct si_sm_io *io, unsigned int offset)
45{
46 return (readq((io->addr)+(offset * io->regspacing)) >> io->regshift)
47 & 0xff;
48}
49
50static void mem_outq(const struct si_sm_io *io, unsigned int offset,
51 unsigned char b)
52{
53 writeq(b << io->regshift, (io->addr)+(offset * io->regspacing));
54}
55#endif
56
57static void mem_region_cleanup(struct si_sm_io *io, int num)
58{
59 unsigned long addr = io->addr_data;
60 int idx;
61
62 for (idx = 0; idx < num; idx++)
63 release_mem_region(addr + idx * io->regspacing,
64 io->regsize);
65}
66
67static void mem_cleanup(struct si_sm_io *io)
68{
69 if (io->addr) {
70 iounmap(io->addr);
71 mem_region_cleanup(io, io->io_size);
72 }
73}
74
75int ipmi_si_mem_setup(struct si_sm_io *io)
76{
77 unsigned long addr = io->addr_data;
78 int mapsize, idx;
79
80 if (!addr)
81 return -ENODEV;
82
83 io->io_cleanup = mem_cleanup;
84
85 /*
86 * Figure out the actual readb/readw/readl/etc routine to use based
87 * upon the register size.
88 */
89 switch (io->regsize) {
90 case 1:
91 io->inputb = intf_mem_inb;
92 io->outputb = intf_mem_outb;
93 break;
94 case 2:
95 io->inputb = intf_mem_inw;
96 io->outputb = intf_mem_outw;
97 break;
98 case 4:
99 io->inputb = intf_mem_inl;
100 io->outputb = intf_mem_outl;
101 break;
102#ifdef readq
103 case 8:
104 io->inputb = mem_inq;
105 io->outputb = mem_outq;
106 break;
107#endif
108 default:
109 dev_warn(io->dev, "Invalid register size: %d\n",
110 io->regsize);
111 return -EINVAL;
112 }
113
114 /*
115 * Some BIOSes reserve disjoint memory regions in their ACPI
116 * tables. This causes problems when trying to request the
117 * entire region. Therefore we must request each register
118 * separately.
119 */
120 for (idx = 0; idx < io->io_size; idx++) {
121 if (request_mem_region(addr + idx * io->regspacing,
122 io->regsize, DEVICE_NAME) == NULL) {
123 /* Undo allocations */
124 mem_region_cleanup(io, idx);
125 return -EIO;
126 }
127 }
128
129 /*
130 * Calculate the total amount of memory to claim. This is an
131 * unusual looking calculation, but it avoids claiming any
132 * more memory than it has to. It will claim everything
133 * between the first address to the end of the last full
134 * register.
135 */
136 mapsize = ((io->io_size * io->regspacing)
137 - (io->regspacing - io->regsize));
138 io->addr = ioremap(addr, mapsize);
139 if (io->addr == NULL) {
140 mem_region_cleanup(io, io->io_size);
141 return -EIO;
142 }
143 return 0;
144}
diff --git a/drivers/char/ipmi/ipmi_si_parisc.c b/drivers/char/ipmi/ipmi_si_parisc.c
new file mode 100644
index 000000000000..090b073ab441
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_parisc.c
@@ -0,0 +1,58 @@
1
2#include <linux/module.h>
3#include <asm/hardware.h> /* for register_parisc_driver() stuff */
4#include <asm/parisc-device.h>
5#include "ipmi_si.h"
6
7static bool parisc_registered;
8
9static int __init ipmi_parisc_probe(struct parisc_device *dev)
10{
11 struct si_sm_io io;
12
13 io.si_type = SI_KCS;
14 io.addr_source = SI_DEVICETREE;
15 io.addr_type = IPMI_MEM_ADDR_SPACE;
16 io.addr_data = dev->hpa.start;
17 io.regsize = 1;
18 io.regspacing = 1;
19 io.regshift = 0;
20 io.irq = 0; /* no interrupt */
21 io.irq_setup = NULL;
22 io.dev = &dev->dev;
23
24 dev_dbg(&dev->dev, "addr 0x%lx\n", io.addr_data);
25
26 return ipmi_si_add_smi(&io);
27}
28
29static int __exit ipmi_parisc_remove(struct parisc_device *dev)
30{
31 return ipmi_si_remove_by_dev(&dev->dev);
32}
33
34static const struct parisc_device_id ipmi_parisc_tbl[] __initconst = {
35 { HPHW_MC, HVERSION_REV_ANY_ID, 0x004, 0xC0 },
36 { 0, }
37};
38
39MODULE_DEVICE_TABLE(parisc, ipmi_parisc_tbl);
40
41static struct parisc_driver ipmi_parisc_driver __refdata = {
42 .name = "ipmi",
43 .id_table = ipmi_parisc_tbl,
44 .probe = ipmi_parisc_probe,
45 .remove = __exit_p(ipmi_parisc_remove),
46};
47
48void ipmi_si_parisc_init(void)
49{
50 register_parisc_driver(&ipmi_parisc_driver);
51 parisc_registered = true;
52}
53
54void ipmi_si_parisc_shutdown(void)
55{
56 if (parisc_registered)
57 unregister_parisc_driver(&ipmi_parisc_driver);
58}
diff --git a/drivers/char/ipmi/ipmi_si_pci.c b/drivers/char/ipmi/ipmi_si_pci.c
new file mode 100644
index 000000000000..99771f5cad07
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_pci.c
@@ -0,0 +1,166 @@
1/*
2 * ipmi_si_pci.c
3 *
4 * Handling for IPMI devices on the PCI bus.
5 */
6#include <linux/module.h>
7#include <linux/pci.h>
8#include "ipmi_si.h"
9
10#define PFX "ipmi_pci: "
11
12static bool pci_registered;
13
14static bool si_trypci = true;
15
16module_param_named(trypci, si_trypci, bool, 0);
17MODULE_PARM_DESC(trypci, "Setting this to zero will disable the"
18 " default scan of the interfaces identified via pci");
19
20#define PCI_ERMC_CLASSCODE 0x0C0700
21#define PCI_ERMC_CLASSCODE_MASK 0xffffff00
22#define PCI_ERMC_CLASSCODE_TYPE_MASK 0xff
23#define PCI_ERMC_CLASSCODE_TYPE_SMIC 0x00
24#define PCI_ERMC_CLASSCODE_TYPE_KCS 0x01
25#define PCI_ERMC_CLASSCODE_TYPE_BT 0x02
26
27#define PCI_HP_VENDOR_ID 0x103C
28#define PCI_MMC_DEVICE_ID 0x121A
29#define PCI_MMC_ADDR_CW 0x10
30
31static void ipmi_pci_cleanup(struct si_sm_io *io)
32{
33 struct pci_dev *pdev = io->addr_source_data;
34
35 pci_disable_device(pdev);
36}
37
38static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
39{
40 if (io->si_type == SI_KCS) {
41 unsigned char status;
42 int regspacing;
43
44 io->regsize = DEFAULT_REGSIZE;
45 io->regshift = 0;
46
47 /* detect 1, 4, 16byte spacing */
48 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
49 io->regspacing = regspacing;
50 if (io->io_setup(io)) {
51 dev_err(io->dev,
52 "Could not setup I/O space\n");
53 return DEFAULT_REGSPACING;
54 }
55 /* write invalid cmd */
56 io->outputb(io, 1, 0x10);
57 /* read status back */
58 status = io->inputb(io, 1);
59 io->io_cleanup(io);
60 if (status)
61 return regspacing;
62 regspacing *= 4;
63 }
64 }
65 return DEFAULT_REGSPACING;
66}
67
68static int ipmi_pci_probe(struct pci_dev *pdev,
69 const struct pci_device_id *ent)
70{
71 int rv;
72 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
73 struct si_sm_io io;
74
75 memset(&io, 0, sizeof(io));
76 io.addr_source = SI_PCI;
77 dev_info(&pdev->dev, "probing via PCI");
78
79 switch (class_type) {
80 case PCI_ERMC_CLASSCODE_TYPE_SMIC:
81 io.si_type = SI_SMIC;
82 break;
83
84 case PCI_ERMC_CLASSCODE_TYPE_KCS:
85 io.si_type = SI_KCS;
86 break;
87
88 case PCI_ERMC_CLASSCODE_TYPE_BT:
89 io.si_type = SI_BT;
90 break;
91
92 default:
93 dev_info(&pdev->dev, "Unknown IPMI type: %d\n", class_type);
94 return -ENOMEM;
95 }
96
97 rv = pci_enable_device(pdev);
98 if (rv) {
99 dev_err(&pdev->dev, "couldn't enable PCI device\n");
100 return rv;
101 }
102
103 io.addr_source_cleanup = ipmi_pci_cleanup;
104 io.addr_source_data = pdev;
105
106 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO)
107 io.addr_type = IPMI_IO_ADDR_SPACE;
108 else
109 io.addr_type = IPMI_MEM_ADDR_SPACE;
110 io.addr_data = pci_resource_start(pdev, 0);
111
112 io.regspacing = ipmi_pci_probe_regspacing(&io);
113 io.regsize = DEFAULT_REGSIZE;
114 io.regshift = 0;
115
116 io.irq = pdev->irq;
117 if (io.irq)
118 io.irq_setup = ipmi_std_irq_setup;
119
120 io.dev = &pdev->dev;
121
122 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
123 &pdev->resource[0], io.regsize, io.regspacing, io.irq);
124
125 rv = ipmi_si_add_smi(&io);
126 if (rv)
127 pci_disable_device(pdev);
128
129 return rv;
130}
131
132static void ipmi_pci_remove(struct pci_dev *pdev)
133{
134 ipmi_si_remove_by_dev(&pdev->dev);
135}
136
137static const struct pci_device_id ipmi_pci_devices[] = {
138 { PCI_DEVICE(PCI_HP_VENDOR_ID, PCI_MMC_DEVICE_ID) },
139 { PCI_DEVICE_CLASS(PCI_ERMC_CLASSCODE, PCI_ERMC_CLASSCODE_MASK) },
140 { 0, }
141};
142MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
143
144static struct pci_driver ipmi_pci_driver = {
145 .name = DEVICE_NAME,
146 .id_table = ipmi_pci_devices,
147 .probe = ipmi_pci_probe,
148 .remove = ipmi_pci_remove,
149};
150
151void ipmi_si_pci_init(void)
152{
153 if (si_trypci) {
154 int rv = pci_register_driver(&ipmi_pci_driver);
155 if (rv)
156 pr_err(PFX "Unable to register PCI driver: %d\n", rv);
157 else
158 pci_registered = true;
159 }
160}
161
162void ipmi_si_pci_shutdown(void)
163{
164 if (pci_registered)
165 pci_unregister_driver(&ipmi_pci_driver);
166}
diff --git a/drivers/char/ipmi/ipmi_si_platform.c b/drivers/char/ipmi/ipmi_si_platform.c
new file mode 100644
index 000000000000..9573f1116450
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_platform.c
@@ -0,0 +1,593 @@
1/*
2 * ipmi_si_platform.c
3 *
4 * Handling for platform devices in IPMI (ACPI, OF, and things
5 * coming from the platform.
6 */
7#include <linux/types.h>
8#include <linux/module.h>
9#include <linux/of_device.h>
10#include <linux/of_platform.h>
11#include <linux/of_address.h>
12#include <linux/of_irq.h>
13#include <linux/acpi.h>
14#include "ipmi_si.h"
15#include "ipmi_dmi.h"
16
17#define PFX "ipmi_platform: "
18
19static bool si_tryplatform = true;
20#ifdef CONFIG_ACPI
21static bool si_tryacpi = true;
22#endif
23#ifdef CONFIG_OF
24static bool si_tryopenfirmware = true;
25#endif
26#ifdef CONFIG_DMI
27static bool si_trydmi = true;
28#else
29static bool si_trydmi = false;
30#endif
31
32module_param_named(tryplatform, si_tryplatform, bool, 0);
33MODULE_PARM_DESC(tryplatform, "Setting this to zero will disable the"
34 " default scan of the interfaces identified via platform"
35 " interfaces besides ACPI, OpenFirmware, and DMI");
36#ifdef CONFIG_ACPI
37module_param_named(tryacpi, si_tryacpi, bool, 0);
38MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
39 " default scan of the interfaces identified via ACPI");
40#endif
41#ifdef CONFIG_OF
42module_param_named(tryopenfirmware, si_tryopenfirmware, bool, 0);
43MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
44 " default scan of the interfaces identified via OpenFirmware");
45#endif
46#ifdef CONFIG_DMI
47module_param_named(trydmi, si_trydmi, bool, 0);
48MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
49 " default scan of the interfaces identified via DMI");
50#endif
51
52#ifdef CONFIG_ACPI
53
54/*
55 * Once we get an ACPI failure, we don't try any more, because we go
56 * through the tables sequentially. Once we don't find a table, there
57 * are no more.
58 */
59static int acpi_failure;
60
61/* For GPE-type interrupts. */
62static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
63 u32 gpe_number, void *context)
64{
65 struct si_sm_io *io = context;
66
67 ipmi_si_irq_handler(io->irq, io->irq_handler_data);
68 return ACPI_INTERRUPT_HANDLED;
69}
70
71static void acpi_gpe_irq_cleanup(struct si_sm_io *io)
72{
73 if (!io->irq)
74 return;
75
76 ipmi_irq_start_cleanup(io);
77 acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe);
78}
79
80static int acpi_gpe_irq_setup(struct si_sm_io *io)
81{
82 acpi_status status;
83
84 if (!io->irq)
85 return 0;
86
87 status = acpi_install_gpe_handler(NULL,
88 io->irq,
89 ACPI_GPE_LEVEL_TRIGGERED,
90 &ipmi_acpi_gpe,
91 io);
92 if (status != AE_OK) {
93 dev_warn(io->dev,
94 "Unable to claim ACPI GPE %d, running polled\n",
95 io->irq);
96 io->irq = 0;
97 return -EINVAL;
98 } else {
99 io->irq_cleanup = acpi_gpe_irq_cleanup;
100 ipmi_irq_finish_setup(io);
101 dev_info(io->dev, "Using ACPI GPE %d\n", io->irq);
102 return 0;
103 }
104}
105
106/*
107 * Defined at
108 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
109 */
110struct SPMITable {
111 s8 Signature[4];
112 u32 Length;
113 u8 Revision;
114 u8 Checksum;
115 s8 OEMID[6];
116 s8 OEMTableID[8];
117 s8 OEMRevision[4];
118 s8 CreatorID[4];
119 s8 CreatorRevision[4];
120 u8 InterfaceType;
121 u8 IPMIlegacy;
122 s16 SpecificationRevision;
123
124 /*
125 * Bit 0 - SCI interrupt supported
126 * Bit 1 - I/O APIC/SAPIC
127 */
128 u8 InterruptType;
129
130 /*
131 * If bit 0 of InterruptType is set, then this is the SCI
132 * interrupt in the GPEx_STS register.
133 */
134 u8 GPE;
135
136 s16 Reserved;
137
138 /*
139 * If bit 1 of InterruptType is set, then this is the I/O
140 * APIC/SAPIC interrupt.
141 */
142 u32 GlobalSystemInterrupt;
143
144 /* The actual register address. */
145 struct acpi_generic_address addr;
146
147 u8 UID[4];
148
149 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
150};
151
152static int try_init_spmi(struct SPMITable *spmi)
153{
154 struct si_sm_io io;
155
156 if (spmi->IPMIlegacy != 1) {
157 pr_info(PFX "Bad SPMI legacy %d\n", spmi->IPMIlegacy);
158 return -ENODEV;
159 }
160
161 memset(&io, 0, sizeof(io));
162 io.addr_source = SI_SPMI;
163 pr_info(PFX "probing via SPMI\n");
164
165 /* Figure out the interface type. */
166 switch (spmi->InterfaceType) {
167 case 1: /* KCS */
168 io.si_type = SI_KCS;
169 break;
170 case 2: /* SMIC */
171 io.si_type = SI_SMIC;
172 break;
173 case 3: /* BT */
174 io.si_type = SI_BT;
175 break;
176 case 4: /* SSIF, just ignore */
177 return -EIO;
178 default:
179 pr_info(PFX "Unknown ACPI/SPMI SI type %d\n",
180 spmi->InterfaceType);
181 return -EIO;
182 }
183
184 if (spmi->InterruptType & 1) {
185 /* We've got a GPE interrupt. */
186 io.irq = spmi->GPE;
187 io.irq_setup = acpi_gpe_irq_setup;
188 } else if (spmi->InterruptType & 2) {
189 /* We've got an APIC/SAPIC interrupt. */
190 io.irq = spmi->GlobalSystemInterrupt;
191 io.irq_setup = ipmi_std_irq_setup;
192 } else {
193 /* Use the default interrupt setting. */
194 io.irq = 0;
195 io.irq_setup = NULL;
196 }
197
198 if (spmi->addr.bit_width) {
199 /* A (hopefully) properly formed register bit width. */
200 io.regspacing = spmi->addr.bit_width / 8;
201 } else {
202 io.regspacing = DEFAULT_REGSPACING;
203 }
204 io.regsize = io.regspacing;
205 io.regshift = spmi->addr.bit_offset;
206
207 if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
208 io.addr_type = IPMI_MEM_ADDR_SPACE;
209 } else if (spmi->addr.space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
210 io.addr_type = IPMI_IO_ADDR_SPACE;
211 } else {
212 pr_warn(PFX "Unknown ACPI I/O Address type\n");
213 return -EIO;
214 }
215 io.addr_data = spmi->addr.address;
216
217 pr_info("ipmi_si: SPMI: %s %#lx regsize %d spacing %d irq %d\n",
218 (io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
219 io.addr_data, io.regsize, io.regspacing, io.irq);
220
221 return ipmi_si_add_smi(&io);
222}
223
224static void spmi_find_bmc(void)
225{
226 acpi_status status;
227 struct SPMITable *spmi;
228 int i;
229
230 if (acpi_disabled)
231 return;
232
233 if (acpi_failure)
234 return;
235
236 for (i = 0; ; i++) {
237 status = acpi_get_table(ACPI_SIG_SPMI, i+1,
238 (struct acpi_table_header **)&spmi);
239 if (status != AE_OK)
240 return;
241
242 try_init_spmi(spmi);
243 }
244}
245#endif
246
247static struct resource *
248ipmi_get_info_from_resources(struct platform_device *pdev,
249 struct si_sm_io *io)
250{
251 struct resource *res, *res_second;
252
253 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
254 if (res) {
255 io->addr_type = IPMI_IO_ADDR_SPACE;
256 } else {
257 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 if (res)
259 io->addr_type = IPMI_MEM_ADDR_SPACE;
260 }
261 if (!res) {
262 dev_err(&pdev->dev, "no I/O or memory address\n");
263 return NULL;
264 }
265 io->addr_data = res->start;
266
267 io->regspacing = DEFAULT_REGSPACING;
268 res_second = platform_get_resource(pdev,
269 (io->addr_type == IPMI_IO_ADDR_SPACE) ?
270 IORESOURCE_IO : IORESOURCE_MEM,
271 1);
272 if (res_second) {
273 if (res_second->start > io->addr_data)
274 io->regspacing = res_second->start - io->addr_data;
275 }
276 io->regsize = DEFAULT_REGSIZE;
277 io->regshift = 0;
278
279 return res;
280}
281
282static int platform_ipmi_probe(struct platform_device *pdev)
283{
284 struct si_sm_io io;
285 u8 type, slave_addr, addr_source;
286 int rv;
287
288 rv = device_property_read_u8(&pdev->dev, "addr-source", &addr_source);
289 if (rv)
290 addr_source = SI_PLATFORM;
291 if (addr_source >= SI_LAST)
292 return -EINVAL;
293
294 if (addr_source == SI_SMBIOS) {
295 if (!si_trydmi)
296 return -ENODEV;
297 } else {
298 if (!si_tryplatform)
299 return -ENODEV;
300 }
301
302 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
303 if (rv)
304 return -ENODEV;
305
306 memset(&io, 0, sizeof(io));
307 io.addr_source = addr_source;
308 dev_info(&pdev->dev, PFX "probing via %s\n",
309 ipmi_addr_src_to_str(addr_source));
310
311 switch (type) {
312 case SI_KCS:
313 case SI_SMIC:
314 case SI_BT:
315 io.si_type = type;
316 break;
317 default:
318 dev_err(&pdev->dev, "ipmi-type property is invalid\n");
319 return -EINVAL;
320 }
321
322 if (!ipmi_get_info_from_resources(pdev, &io))
323 return -EINVAL;
324
325 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
326 if (rv) {
327 dev_warn(&pdev->dev, "device has no slave-addr property\n");
328 io.slave_addr = 0x20;
329 } else {
330 io.slave_addr = slave_addr;
331 }
332
333 io.irq = platform_get_irq(pdev, 0);
334 if (io.irq > 0)
335 io.irq_setup = ipmi_std_irq_setup;
336 else
337 io.irq = 0;
338
339 io.dev = &pdev->dev;
340
341 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
342 (io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
343 io.addr_data, io.regsize, io.regspacing, io.irq);
344
345 ipmi_si_add_smi(&io);
346
347 return 0;
348}
349
350#ifdef CONFIG_OF
351static const struct of_device_id of_ipmi_match[] = {
352 { .type = "ipmi", .compatible = "ipmi-kcs",
353 .data = (void *)(unsigned long) SI_KCS },
354 { .type = "ipmi", .compatible = "ipmi-smic",
355 .data = (void *)(unsigned long) SI_SMIC },
356 { .type = "ipmi", .compatible = "ipmi-bt",
357 .data = (void *)(unsigned long) SI_BT },
358 {},
359};
360MODULE_DEVICE_TABLE(of, of_ipmi_match);
361
362static int of_ipmi_probe(struct platform_device *pdev)
363{
364 const struct of_device_id *match;
365 struct si_sm_io io;
366 struct resource resource;
367 const __be32 *regsize, *regspacing, *regshift;
368 struct device_node *np = pdev->dev.of_node;
369 int ret;
370 int proplen;
371
372 if (!si_tryopenfirmware)
373 return -ENODEV;
374
375 dev_info(&pdev->dev, "probing via device tree\n");
376
377 match = of_match_device(of_ipmi_match, &pdev->dev);
378 if (!match)
379 return -ENODEV;
380
381 if (!of_device_is_available(np))
382 return -EINVAL;
383
384 ret = of_address_to_resource(np, 0, &resource);
385 if (ret) {
386 dev_warn(&pdev->dev, PFX "invalid address from OF\n");
387 return ret;
388 }
389
390 regsize = of_get_property(np, "reg-size", &proplen);
391 if (regsize && proplen != 4) {
392 dev_warn(&pdev->dev, PFX "invalid regsize from OF\n");
393 return -EINVAL;
394 }
395
396 regspacing = of_get_property(np, "reg-spacing", &proplen);
397 if (regspacing && proplen != 4) {
398 dev_warn(&pdev->dev, PFX "invalid regspacing from OF\n");
399 return -EINVAL;
400 }
401
402 regshift = of_get_property(np, "reg-shift", &proplen);
403 if (regshift && proplen != 4) {
404 dev_warn(&pdev->dev, PFX "invalid regshift from OF\n");
405 return -EINVAL;
406 }
407
408 memset(&io, 0, sizeof(io));
409 io.si_type = (enum si_type) match->data;
410 io.addr_source = SI_DEVICETREE;
411 io.irq_setup = ipmi_std_irq_setup;
412
413 if (resource.flags & IORESOURCE_IO)
414 io.addr_type = IPMI_IO_ADDR_SPACE;
415 else
416 io.addr_type = IPMI_MEM_ADDR_SPACE;
417
418 io.addr_data = resource.start;
419
420 io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
421 io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
422 io.regshift = regshift ? be32_to_cpup(regshift) : 0;
423
424 io.irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
425 io.dev = &pdev->dev;
426
427 dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
428 io.addr_data, io.regsize, io.regspacing, io.irq);
429
430 return ipmi_si_add_smi(&io);
431}
432#else
433#define of_ipmi_match NULL
434static int of_ipmi_probe(struct platform_device *dev)
435{
436 return -ENODEV;
437}
438#endif
439
440#ifdef CONFIG_ACPI
441static int find_slave_address(struct si_sm_io *io, int slave_addr)
442{
443#ifdef CONFIG_IPMI_DMI_DECODE
444 if (!slave_addr) {
445 u32 flags = IORESOURCE_IO;
446
447 if (io->addr_type == IPMI_MEM_ADDR_SPACE)
448 flags = IORESOURCE_MEM;
449
450 slave_addr = ipmi_dmi_get_slave_addr(io->si_type, flags,
451 io->addr_data);
452 }
453#endif
454
455 return slave_addr;
456}
457
458static int acpi_ipmi_probe(struct platform_device *pdev)
459{
460 struct si_sm_io io;
461 acpi_handle handle;
462 acpi_status status;
463 unsigned long long tmp;
464 struct resource *res;
465 int rv = -EINVAL;
466
467 if (!si_tryacpi)
468 return -ENODEV;
469
470 handle = ACPI_HANDLE(&pdev->dev);
471 if (!handle)
472 return -ENODEV;
473
474 memset(&io, 0, sizeof(io));
475 io.addr_source = SI_ACPI;
476 dev_info(&pdev->dev, PFX "probing via ACPI\n");
477
478 io.addr_info.acpi_info.acpi_handle = handle;
479
480 /* _IFT tells us the interface type: KCS, BT, etc */
481 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
482 if (ACPI_FAILURE(status)) {
483 dev_err(&pdev->dev,
484 "Could not find ACPI IPMI interface type\n");
485 goto err_free;
486 }
487
488 switch (tmp) {
489 case 1:
490 io.si_type = SI_KCS;
491 break;
492 case 2:
493 io.si_type = SI_SMIC;
494 break;
495 case 3:
496 io.si_type = SI_BT;
497 break;
498 case 4: /* SSIF, just ignore */
499 rv = -ENODEV;
500 goto err_free;
501 default:
502 dev_info(&pdev->dev, "unknown IPMI type %lld\n", tmp);
503 goto err_free;
504 }
505
506 res = ipmi_get_info_from_resources(pdev, &io);
507 if (!res) {
508 rv = -EINVAL;
509 goto err_free;
510 }
511
512 /* If _GPE exists, use it; otherwise use standard interrupts */
513 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
514 if (ACPI_SUCCESS(status)) {
515 io.irq = tmp;
516 io.irq_setup = acpi_gpe_irq_setup;
517 } else {
518 int irq = platform_get_irq(pdev, 0);
519
520 if (irq > 0) {
521 io.irq = irq;
522 io.irq_setup = ipmi_std_irq_setup;
523 }
524 }
525
526 io.slave_addr = find_slave_address(&io, io.slave_addr);
527
528 io.dev = &pdev->dev;
529
530 dev_info(io.dev, "%pR regsize %d spacing %d irq %d\n",
531 res, io.regsize, io.regspacing, io.irq);
532
533 return ipmi_si_add_smi(&io);
534
535err_free:
536 return rv;
537}
538
539static const struct acpi_device_id acpi_ipmi_match[] = {
540 { "IPI0001", 0 },
541 { },
542};
543MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
544#else
545static int acpi_ipmi_probe(struct platform_device *dev)
546{
547 return -ENODEV;
548}
549#endif
550
551static int ipmi_probe(struct platform_device *pdev)
552{
553 if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
554 return 0;
555
556 if (acpi_ipmi_probe(pdev) == 0)
557 return 0;
558
559 return platform_ipmi_probe(pdev);
560}
561
562static int ipmi_remove(struct platform_device *pdev)
563{
564 return ipmi_si_remove_by_dev(&pdev->dev);
565}
566
567struct platform_driver ipmi_platform_driver = {
568 .driver = {
569 .name = DEVICE_NAME,
570 .of_match_table = of_ipmi_match,
571 .acpi_match_table = ACPI_PTR(acpi_ipmi_match),
572 },
573 .probe = ipmi_probe,
574 .remove = ipmi_remove,
575};
576
577void ipmi_si_platform_init(void)
578{
579 int rv = platform_driver_register(&ipmi_platform_driver);
580 if (rv)
581 pr_err(PFX "Unable to register driver: %d\n", rv);
582
583#ifdef CONFIG_ACPI
584 if (si_tryacpi)
585 spmi_find_bmc();
586#endif
587
588}
589
590void ipmi_si_platform_shutdown(void)
591{
592 platform_driver_unregister(&ipmi_platform_driver);
593}
diff --git a/drivers/char/ipmi/ipmi_si_port_io.c b/drivers/char/ipmi/ipmi_si_port_io.c
new file mode 100644
index 000000000000..e5ce174fbeeb
--- /dev/null
+++ b/drivers/char/ipmi/ipmi_si_port_io.c
@@ -0,0 +1,112 @@
1
2#include <linux/io.h>
3#include "ipmi_si.h"
4
5static unsigned char port_inb(const struct si_sm_io *io, unsigned int offset)
6{
7 unsigned int addr = io->addr_data;
8
9 return inb(addr + (offset * io->regspacing));
10}
11
12static void port_outb(const struct si_sm_io *io, unsigned int offset,
13 unsigned char b)
14{
15 unsigned int addr = io->addr_data;
16
17 outb(b, addr + (offset * io->regspacing));
18}
19
20static unsigned char port_inw(const struct si_sm_io *io, unsigned int offset)
21{
22 unsigned int addr = io->addr_data;
23
24 return (inw(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
25}
26
27static void port_outw(const struct si_sm_io *io, unsigned int offset,
28 unsigned char b)
29{
30 unsigned int addr = io->addr_data;
31
32 outw(b << io->regshift, addr + (offset * io->regspacing));
33}
34
35static unsigned char port_inl(const struct si_sm_io *io, unsigned int offset)
36{
37 unsigned int addr = io->addr_data;
38
39 return (inl(addr + (offset * io->regspacing)) >> io->regshift) & 0xff;
40}
41
42static void port_outl(const struct si_sm_io *io, unsigned int offset,
43 unsigned char b)
44{
45 unsigned int addr = io->addr_data;
46
47 outl(b << io->regshift, addr+(offset * io->regspacing));
48}
49
50static void port_cleanup(struct si_sm_io *io)
51{
52 unsigned int addr = io->addr_data;
53 int idx;
54
55 if (addr) {
56 for (idx = 0; idx < io->io_size; idx++)
57 release_region(addr + idx * io->regspacing,
58 io->regsize);
59 }
60}
61
62int ipmi_si_port_setup(struct si_sm_io *io)
63{
64 unsigned int addr = io->addr_data;
65 int idx;
66
67 if (!addr)
68 return -ENODEV;
69
70 io->io_cleanup = port_cleanup;
71
72 /*
73 * Figure out the actual inb/inw/inl/etc routine to use based
74 * upon the register size.
75 */
76 switch (io->regsize) {
77 case 1:
78 io->inputb = port_inb;
79 io->outputb = port_outb;
80 break;
81 case 2:
82 io->inputb = port_inw;
83 io->outputb = port_outw;
84 break;
85 case 4:
86 io->inputb = port_inl;
87 io->outputb = port_outl;
88 break;
89 default:
90 dev_warn(io->dev, "Invalid register size: %d\n",
91 io->regsize);
92 return -EINVAL;
93 }
94
95 /*
96 * Some BIOSes reserve disjoint I/O regions in their ACPI
97 * tables. This causes problems when trying to register the
98 * entire I/O region. Therefore we must register each I/O
99 * port separately.
100 */
101 for (idx = 0; idx < io->io_size; idx++) {
102 if (request_region(addr + idx * io->regspacing,
103 io->regsize, DEVICE_NAME) == NULL) {
104 /* Undo allocations */
105 while (idx--)
106 release_region(addr + idx * io->regspacing,
107 io->regsize);
108 return -EIO;
109 }
110 }
111 return 0;
112}
diff --git a/drivers/char/ipmi/ipmi_si_sm.h b/drivers/char/ipmi/ipmi_si_sm.h
index a705027c0493..aa8d88ab4433 100644
--- a/drivers/char/ipmi/ipmi_si_sm.h
+++ b/drivers/char/ipmi/ipmi_si_sm.h
@@ -34,12 +34,18 @@
34 * 675 Mass Ave, Cambridge, MA 02139, USA. 34 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 */ 35 */
36 36
37#include <linux/ipmi.h>
38
37/* 39/*
38 * This is defined by the state machines themselves, it is an opaque 40 * This is defined by the state machines themselves, it is an opaque
39 * data type for them to use. 41 * data type for them to use.
40 */ 42 */
41struct si_sm_data; 43struct si_sm_data;
42 44
45enum si_type {
46 SI_TYPE_INVALID, SI_KCS, SI_SMIC, SI_BT
47};
48
43/* 49/*
44 * The structure for doing I/O in the state machine. The state 50 * The structure for doing I/O in the state machine. The state
45 * machine doesn't have the actual I/O routines, they are done through 51 * machine doesn't have the actual I/O routines, they are done through
@@ -61,6 +67,23 @@ struct si_sm_io {
61 int regshift; 67 int regshift;
62 int addr_type; 68 int addr_type;
63 long addr_data; 69 long addr_data;
70 enum ipmi_addr_src addr_source; /* ACPI, PCI, SMBIOS, hardcode, etc. */
71 void (*addr_source_cleanup)(struct si_sm_io *io);
72 void *addr_source_data;
73 union ipmi_smi_info_union addr_info;
74
75 int (*io_setup)(struct si_sm_io *info);
76 void (*io_cleanup)(struct si_sm_io *info);
77 unsigned int io_size;
78
79 int irq;
80 int (*irq_setup)(struct si_sm_io *io);
81 void *irq_handler_data;
82 void (*irq_cleanup)(struct si_sm_io *io);
83
84 u8 slave_addr;
85 enum si_type si_type;
86 struct device *dev;
64}; 87};
65 88
66/* Results of SMI events. */ 89/* Results of SMI events. */
diff --git a/drivers/char/ipmi/ipmi_ssif.c b/drivers/char/ipmi/ipmi_ssif.c
index 0aea3bcb6158..466b3a1c0adf 100644
--- a/drivers/char/ipmi/ipmi_ssif.c
+++ b/drivers/char/ipmi/ipmi_ssif.c
@@ -53,6 +53,7 @@
53#include <linux/acpi.h> 53#include <linux/acpi.h>
54#include <linux/ctype.h> 54#include <linux/ctype.h>
55#include <linux/time64.h> 55#include <linux/time64.h>
56#include "ipmi_si_sm.h"
56#include "ipmi_dmi.h" 57#include "ipmi_dmi.h"
57 58
58#define PFX "ipmi_ssif: " 59#define PFX "ipmi_ssif: "
@@ -267,9 +268,6 @@ struct ssif_info {
267 unsigned char *i2c_data; 268 unsigned char *i2c_data;
268 unsigned int i2c_size; 269 unsigned int i2c_size;
269 270
270 /* From the device id response. */
271 struct ipmi_device_id device_id;
272
273 struct timer_list retry_timer; 271 struct timer_list retry_timer;
274 int retries_left; 272 int retries_left;
275 273
@@ -1176,6 +1174,61 @@ MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the default scan of
1176static DEFINE_MUTEX(ssif_infos_mutex); 1174static DEFINE_MUTEX(ssif_infos_mutex);
1177static LIST_HEAD(ssif_infos); 1175static LIST_HEAD(ssif_infos);
1178 1176
1177#define IPMI_SSIF_ATTR(name) \
1178static ssize_t ipmi_##name##_show(struct device *dev, \
1179 struct device_attribute *attr, \
1180 char *buf) \
1181{ \
1182 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1183 \
1184 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1185} \
1186static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1187
1188static ssize_t ipmi_type_show(struct device *dev,
1189 struct device_attribute *attr,
1190 char *buf)
1191{
1192 return snprintf(buf, 10, "ssif\n");
1193}
1194static DEVICE_ATTR(type, S_IRUGO, ipmi_type_show, NULL);
1195
1196IPMI_SSIF_ATTR(sent_messages);
1197IPMI_SSIF_ATTR(sent_messages_parts);
1198IPMI_SSIF_ATTR(send_retries);
1199IPMI_SSIF_ATTR(send_errors);
1200IPMI_SSIF_ATTR(received_messages);
1201IPMI_SSIF_ATTR(received_message_parts);
1202IPMI_SSIF_ATTR(receive_retries);
1203IPMI_SSIF_ATTR(receive_errors);
1204IPMI_SSIF_ATTR(flag_fetches);
1205IPMI_SSIF_ATTR(hosed);
1206IPMI_SSIF_ATTR(events);
1207IPMI_SSIF_ATTR(watchdog_pretimeouts);
1208IPMI_SSIF_ATTR(alerts);
1209
1210static struct attribute *ipmi_ssif_dev_attrs[] = {
1211 &dev_attr_type.attr,
1212 &dev_attr_sent_messages.attr,
1213 &dev_attr_sent_messages_parts.attr,
1214 &dev_attr_send_retries.attr,
1215 &dev_attr_send_errors.attr,
1216 &dev_attr_received_messages.attr,
1217 &dev_attr_received_message_parts.attr,
1218 &dev_attr_receive_retries.attr,
1219 &dev_attr_receive_errors.attr,
1220 &dev_attr_flag_fetches.attr,
1221 &dev_attr_hosed.attr,
1222 &dev_attr_events.attr,
1223 &dev_attr_watchdog_pretimeouts.attr,
1224 &dev_attr_alerts.attr,
1225 NULL
1226};
1227
1228static const struct attribute_group ipmi_ssif_dev_attr_group = {
1229 .attrs = ipmi_ssif_dev_attrs,
1230};
1231
1179static int ssif_remove(struct i2c_client *client) 1232static int ssif_remove(struct i2c_client *client)
1180{ 1233{
1181 struct ssif_info *ssif_info = i2c_get_clientdata(client); 1234 struct ssif_info *ssif_info = i2c_get_clientdata(client);
@@ -1196,6 +1249,9 @@ static int ssif_remove(struct i2c_client *client)
1196 } 1249 }
1197 ssif_info->intf = NULL; 1250 ssif_info->intf = NULL;
1198 1251
1252 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1253 dev_set_drvdata(&ssif_info->client->dev, NULL);
1254
1199 /* make sure the driver is not looking for flags any more. */ 1255 /* make sure the driver is not looking for flags any more. */
1200 while (ssif_info->ssif_state != SSIF_NORMAL) 1256 while (ssif_info->ssif_state != SSIF_NORMAL)
1201 schedule_timeout(1); 1257 schedule_timeout(1);
@@ -1289,6 +1345,7 @@ static int ssif_detect(struct i2c_client *client, struct i2c_board_info *info)
1289 return rv; 1345 return rv;
1290} 1346}
1291 1347
1348#ifdef CONFIG_IPMI_PROC_INTERFACE
1292static int smi_type_proc_show(struct seq_file *m, void *v) 1349static int smi_type_proc_show(struct seq_file *m, void *v)
1293{ 1350{
1294 seq_puts(m, "ssif\n"); 1351 seq_puts(m, "ssif\n");
@@ -1352,6 +1409,7 @@ static const struct file_operations smi_stats_proc_ops = {
1352 .llseek = seq_lseek, 1409 .llseek = seq_lseek,
1353 .release = single_release, 1410 .release = single_release,
1354}; 1411};
1412#endif
1355 1413
1356static int strcmp_nospace(char *s1, char *s2) 1414static int strcmp_nospace(char *s1, char *s2)
1357{ 1415{
@@ -1425,7 +1483,7 @@ static int find_slave_address(struct i2c_client *client, int slave_addr)
1425#ifdef CONFIG_IPMI_DMI_DECODE 1483#ifdef CONFIG_IPMI_DMI_DECODE
1426 if (!slave_addr) 1484 if (!slave_addr)
1427 slave_addr = ipmi_dmi_get_slave_addr( 1485 slave_addr = ipmi_dmi_get_slave_addr(
1428 IPMI_DMI_TYPE_SSIF, 1486 SI_TYPE_INVALID,
1429 i2c_adapter_id(client->adapter), 1487 i2c_adapter_id(client->adapter),
1430 client->addr); 1488 client->addr);
1431#endif 1489#endif
@@ -1481,20 +1539,6 @@ static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1481 ipmi_addr_src_to_str(ssif_info->addr_source), 1539 ipmi_addr_src_to_str(ssif_info->addr_source),
1482 client->addr, client->adapter->name, slave_addr); 1540 client->addr, client->adapter->name, slave_addr);
1483 1541
1484 /*
1485 * Do a Get Device ID command, since it comes back with some
1486 * useful info.
1487 */
1488 msg[0] = IPMI_NETFN_APP_REQUEST << 2;
1489 msg[1] = IPMI_GET_DEVICE_ID_CMD;
1490 rv = do_cmd(client, 2, msg, &len, resp);
1491 if (rv)
1492 goto out;
1493
1494 rv = ipmi_demangle_device_id(resp, len, &ssif_info->device_id);
1495 if (rv)
1496 goto out;
1497
1498 ssif_info->client = client; 1542 ssif_info->client = client;
1499 i2c_set_clientdata(client, ssif_info); 1543 i2c_set_clientdata(client, ssif_info);
1500 1544
@@ -1682,16 +1726,26 @@ static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1682 } 1726 }
1683 } 1727 }
1684 1728
1729 dev_set_drvdata(&ssif_info->client->dev, ssif_info);
1730 rv = device_add_group(&ssif_info->client->dev,
1731 &ipmi_ssif_dev_attr_group);
1732 if (rv) {
1733 dev_err(&ssif_info->client->dev,
1734 "Unable to add device attributes: error %d\n",
1735 rv);
1736 goto out;
1737 }
1738
1685 rv = ipmi_register_smi(&ssif_info->handlers, 1739 rv = ipmi_register_smi(&ssif_info->handlers,
1686 ssif_info, 1740 ssif_info,
1687 &ssif_info->device_id,
1688 &ssif_info->client->dev, 1741 &ssif_info->client->dev,
1689 slave_addr); 1742 slave_addr);
1690 if (rv) { 1743 if (rv) {
1691 pr_err(PFX "Unable to register device: error %d\n", rv); 1744 pr_err(PFX "Unable to register device: error %d\n", rv);
1692 goto out; 1745 goto out_remove_attr;
1693 } 1746 }
1694 1747
1748#ifdef CONFIG_IPMI_PROC_INTERFACE
1695 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type", 1749 rv = ipmi_smi_add_proc_entry(ssif_info->intf, "type",
1696 &smi_type_proc_ops, 1750 &smi_type_proc_ops,
1697 ssif_info); 1751 ssif_info);
@@ -1707,6 +1761,7 @@ static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1707 pr_err(PFX "Unable to create proc entry: %d\n", rv); 1761 pr_err(PFX "Unable to create proc entry: %d\n", rv);
1708 goto out_err_unreg; 1762 goto out_err_unreg;
1709 } 1763 }
1764#endif
1710 1765
1711 out: 1766 out:
1712 if (rv) { 1767 if (rv) {
@@ -1725,8 +1780,14 @@ static int ssif_probe(struct i2c_client *client, const struct i2c_device_id *id)
1725 kfree(resp); 1780 kfree(resp);
1726 return rv; 1781 return rv;
1727 1782
1728 out_err_unreg: 1783#ifdef CONFIG_IPMI_PROC_INTERFACE
1784out_err_unreg:
1729 ipmi_unregister_smi(ssif_info->intf); 1785 ipmi_unregister_smi(ssif_info->intf);
1786#endif
1787
1788out_remove_attr:
1789 device_remove_group(&ssif_info->client->dev, &ipmi_ssif_dev_attr_group);
1790 dev_set_drvdata(&ssif_info->client->dev, NULL);
1730 goto out; 1791 goto out;
1731} 1792}
1732 1793
@@ -1953,20 +2014,13 @@ static void spmi_find_bmc(void) { }
1953#ifdef CONFIG_DMI 2014#ifdef CONFIG_DMI
1954static int dmi_ipmi_probe(struct platform_device *pdev) 2015static int dmi_ipmi_probe(struct platform_device *pdev)
1955{ 2016{
1956 u8 type, slave_addr = 0; 2017 u8 slave_addr = 0;
1957 u16 i2c_addr; 2018 u16 i2c_addr;
1958 int rv; 2019 int rv;
1959 2020
1960 if (!ssif_trydmi) 2021 if (!ssif_trydmi)
1961 return -ENODEV; 2022 return -ENODEV;
1962 2023
1963 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
1964 if (rv)
1965 return -ENODEV;
1966
1967 if (type != IPMI_DMI_TYPE_SSIF)
1968 return -ENODEV;
1969
1970 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr); 2024 rv = device_property_read_u16(&pdev->dev, "i2c-addr", &i2c_addr);
1971 if (rv) { 2025 if (rv) {
1972 dev_warn(&pdev->dev, PFX "No i2c-addr property\n"); 2026 dev_warn(&pdev->dev, PFX "No i2c-addr property\n");
diff --git a/drivers/char/ipmi/ipmi_watchdog.c b/drivers/char/ipmi/ipmi_watchdog.c
index 3d832d0362a4..76b270678b50 100644
--- a/drivers/char/ipmi/ipmi_watchdog.c
+++ b/drivers/char/ipmi/ipmi_watchdog.c
@@ -1009,9 +1009,14 @@ static void ipmi_register_watchdog(int ipmi_intf)
1009 goto out; 1009 goto out;
1010 } 1010 }
1011 1011
1012 ipmi_get_version(watchdog_user, 1012 rv = ipmi_get_version(watchdog_user,
1013 &ipmi_version_major, 1013 &ipmi_version_major,
1014 &ipmi_version_minor); 1014 &ipmi_version_minor);
1015 if (rv) {
1016 pr_warn(PFX "Unable to get IPMI version, assuming 1.0\n");
1017 ipmi_version_major = 1;
1018 ipmi_version_minor = 0;
1019 }
1015 1020
1016 rv = misc_register(&ipmi_wdog_miscdev); 1021 rv = misc_register(&ipmi_wdog_miscdev);
1017 if (rv < 0) { 1022 if (rv < 0) {
diff --git a/include/linux/ipmi.h b/include/linux/ipmi.h
index f1045b2c6a00..f4ffacf4fe9d 100644
--- a/include/linux/ipmi.h
+++ b/include/linux/ipmi.h
@@ -113,9 +113,9 @@ int ipmi_create_user(unsigned int if_num,
113int ipmi_destroy_user(ipmi_user_t user); 113int ipmi_destroy_user(ipmi_user_t user);
114 114
115/* Get the IPMI version of the BMC we are talking to. */ 115/* Get the IPMI version of the BMC we are talking to. */
116void ipmi_get_version(ipmi_user_t user, 116int ipmi_get_version(ipmi_user_t user,
117 unsigned char *major, 117 unsigned char *major,
118 unsigned char *minor); 118 unsigned char *minor);
119 119
120/* Set and get the slave address and LUN that we will use for our 120/* Set and get the slave address and LUN that we will use for our
121 source messages. Note that this affects the interface, not just 121 source messages. Note that this affects the interface, not just
@@ -277,7 +277,7 @@ int ipmi_validate_addr(struct ipmi_addr *addr, int len);
277 */ 277 */
278enum ipmi_addr_src { 278enum ipmi_addr_src {
279 SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS, 279 SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS,
280 SI_PCI, SI_DEVICETREE, SI_LAST 280 SI_PCI, SI_DEVICETREE, SI_PLATFORM, SI_LAST
281}; 281};
282const char *ipmi_addr_src_to_str(enum ipmi_addr_src src); 282const char *ipmi_addr_src_to_str(enum ipmi_addr_src src);
283 283
diff --git a/include/linux/ipmi_smi.h b/include/linux/ipmi_smi.h
index f8cea14485dd..5be51281e14d 100644
--- a/include/linux/ipmi_smi.h
+++ b/include/linux/ipmi_smi.h
@@ -162,27 +162,27 @@ struct ipmi_device_id {
162#define ipmi_version_major(v) ((v)->ipmi_version & 0xf) 162#define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
163#define ipmi_version_minor(v) ((v)->ipmi_version >> 4) 163#define ipmi_version_minor(v) ((v)->ipmi_version >> 4)
164 164
165/* Take a pointer to a raw data buffer and a length and extract device 165/* Take a pointer to an IPMI response and extract device id information from
166 id information from it. The first byte of data must point to the 166 * it. @netfn is in the IPMI_NETFN_ format, so may need to be shifted from
167 netfn << 2, the data should be of the format: 167 * a SI response.
168 netfn << 2, cmd, completion code, data 168 */
169 as normally comes from a device interface. */ 169static inline int ipmi_demangle_device_id(uint8_t netfn, uint8_t cmd,
170static inline int ipmi_demangle_device_id(const unsigned char *data, 170 const unsigned char *data,
171 unsigned int data_len, 171 unsigned int data_len,
172 struct ipmi_device_id *id) 172 struct ipmi_device_id *id)
173{ 173{
174 if (data_len < 9) 174 if (data_len < 7)
175 return -EINVAL; 175 return -EINVAL;
176 if (data[0] != IPMI_NETFN_APP_RESPONSE << 2 || 176 if (netfn != IPMI_NETFN_APP_RESPONSE || cmd != IPMI_GET_DEVICE_ID_CMD)
177 data[1] != IPMI_GET_DEVICE_ID_CMD)
178 /* Strange, didn't get the response we expected. */ 177 /* Strange, didn't get the response we expected. */
179 return -EINVAL; 178 return -EINVAL;
180 if (data[2] != 0) 179 if (data[0] != 0)
181 /* That's odd, it shouldn't be able to fail. */ 180 /* That's odd, it shouldn't be able to fail. */
182 return -EINVAL; 181 return -EINVAL;
183 182
184 data += 3; 183 data++;
185 data_len -= 3; 184 data_len--;
185
186 id->device_id = data[0]; 186 id->device_id = data[0];
187 id->device_revision = data[1]; 187 id->device_revision = data[1];
188 id->firmware_revision_1 = data[2]; 188 id->firmware_revision_1 = data[2];
@@ -214,7 +214,6 @@ static inline int ipmi_demangle_device_id(const unsigned char *data,
214 call. */ 214 call. */
215int ipmi_register_smi(const struct ipmi_smi_handlers *handlers, 215int ipmi_register_smi(const struct ipmi_smi_handlers *handlers,
216 void *send_info, 216 void *send_info,
217 struct ipmi_device_id *device_id,
218 struct device *dev, 217 struct device *dev,
219 unsigned char slave_addr); 218 unsigned char slave_addr);
220 219
@@ -242,11 +241,13 @@ static inline void ipmi_free_smi_msg(struct ipmi_smi_msg *msg)
242 msg->done(msg); 241 msg->done(msg);
243} 242}
244 243
244#ifdef CONFIG_IPMI_PROC_INTERFACE
245/* Allow the lower layer to add things to the proc filesystem 245/* Allow the lower layer to add things to the proc filesystem
246 directory for this interface. Note that the entry will 246 directory for this interface. Note that the entry will
247 automatically be dstroyed when the interface is destroyed. */ 247 automatically be dstroyed when the interface is destroyed. */
248int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name, 248int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
249 const struct file_operations *proc_ops, 249 const struct file_operations *proc_ops,
250 void *data); 250 void *data);
251#endif
251 252
252#endif /* __LINUX_IPMI_SMI_H */ 253#endif /* __LINUX_IPMI_SMI_H */