1 // SPDX-License-Identifier: GPL-2.0+
5 * Handling for dynamically adding/removing IPMI devices through
6 * a module parameter (and thus sysfs).
9 #define pr_fmt(fmt) "ipmi_hotmod: " fmt
11 #include <linux/moduleparam.h>
12 #include <linux/ipmi.h>
13 #include <linux/atomic.h>
15 #include "ipmi_plat_data.h"
17 static int hotmod_handler(const char *val, const struct kernel_param *kp);
19 module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
20 MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
21 " Documentation/IPMI.txt in the kernel sources for the"
25 * Parms come in as <op1>[:op2[:op3...]]. ops are:
26 * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
34 enum hotmod_op { HM_ADD, HM_REMOVE };
40 static const struct hotmod_vals hotmod_ops[] = {
42 { "remove", HM_REMOVE },
46 static const struct hotmod_vals hotmod_si[] = {
53 static const struct hotmod_vals hotmod_as[] = {
54 { "mem", IPMI_MEM_ADDR_SPACE },
55 { "i/o", IPMI_IO_ADDR_SPACE },
59 static int parse_str(const struct hotmod_vals *v, unsigned int *val, char *name,
65 s = strchr(*curr, ',');
67 pr_warn("No hotmod %s given\n", name);
72 for (i = 0; v[i].name; i++) {
73 if (strcmp(*curr, v[i].name) == 0) {
80 pr_warn("Invalid hotmod %s '%s'\n", name, *curr);
84 static int check_hotmod_int_op(const char *curr, const char *option,
85 const char *name, unsigned int *val)
89 if (strcmp(curr, name) == 0) {
91 pr_warn("No option given for '%s'\n", curr);
94 *val = simple_strtoul(option, &n, 0);
95 if ((*n != '\0') || (*option == '\0')) {
96 pr_warn("Bad option given for '%s'\n", curr);
104 static int parse_hotmod_str(const char *curr, enum hotmod_op *op,
105 struct ipmi_plat_data *h)
111 rv = parse_str(hotmod_ops, &ival, "operation", &curr);
116 rv = parse_str(hotmod_si, &ival, "interface type", &curr);
121 rv = parse_str(hotmod_as, &ival, "address space", &curr);
126 s = strchr(curr, ',');
131 rv = kstrtoul(curr, 0, &h->addr);
133 pr_warn("Invalid hotmod address '%s': %d\n", curr, rv);
139 s = strchr(curr, ',');
144 o = strchr(curr, '=');
149 rv = check_hotmod_int_op(curr, o, "rsp", &h->regspacing);
154 rv = check_hotmod_int_op(curr, o, "rsi", &h->regsize);
159 rv = check_hotmod_int_op(curr, o, "rsh", &h->regshift);
164 rv = check_hotmod_int_op(curr, o, "irq", &h->irq);
169 rv = check_hotmod_int_op(curr, o, "ipmb", &h->slave_addr);
175 pr_warn("Invalid hotmod option '%s'\n", curr);
179 h->addr_source = SI_HOTMOD;
183 static atomic_t hotmod_nr;
185 static int hotmod_handler(const char *val, const struct kernel_param *kp)
187 char *str = kstrdup(val, GFP_KERNEL), *curr, *next;
189 struct ipmi_plat_data h;
190 unsigned int len, ival;
195 /* Kill any trailing spaces, as we can get a "\n" from echo. */
198 while ((ival >= 0) && isspace(str[ival])) {
203 for (curr = str; curr; curr = next) {
206 next = strchr(curr, ':');
212 memset(&h, 0, sizeof(h));
213 rv = parse_hotmod_str(curr, &op, &h);
218 ipmi_platform_add("hotmod-ipmi-si",
219 atomic_inc_return(&hotmod_nr),
222 ipmi_si_remove_by_data(h.space, h.type, h.addr);