ipmi_si_hotmod.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ipmi_si_hotmod.c
  4. *
  5. * Handling for dynamically adding/removing IPMI devices through
  6. * a module parameter (and thus sysfs).
  7. */
  8. #define pr_fmt(fmt) "ipmi_hotmod: " fmt
  9. #include <linux/moduleparam.h>
  10. #include <linux/ipmi.h>
  11. #include "ipmi_si.h"
  12. static int hotmod_handler(const char *val, const struct kernel_param *kp);
  13. module_param_call(hotmod, hotmod_handler, NULL, NULL, 0200);
  14. MODULE_PARM_DESC(hotmod, "Add and remove interfaces. See"
  15. " Documentation/IPMI.txt in the kernel sources for the"
  16. " gory details.");
  17. /*
  18. * Parms come in as <op1>[:op2[:op3...]]. ops are:
  19. * add|remove,kcs|bt|smic,mem|i/o,<address>[,<opt1>[,<opt2>[,...]]]
  20. * Options are:
  21. * rsp=<regspacing>
  22. * rsi=<regsize>
  23. * rsh=<regshift>
  24. * irq=<irq>
  25. * ipmb=<ipmb addr>
  26. */
  27. enum hotmod_op { HM_ADD, HM_REMOVE };
  28. struct hotmod_vals {
  29. const char *name;
  30. const int val;
  31. };
  32. static const struct hotmod_vals hotmod_ops[] = {
  33. { "add", HM_ADD },
  34. { "remove", HM_REMOVE },
  35. { NULL }
  36. };
  37. static const struct hotmod_vals hotmod_si[] = {
  38. { "kcs", SI_KCS },
  39. { "smic", SI_SMIC },
  40. { "bt", SI_BT },
  41. { NULL }
  42. };
  43. static const struct hotmod_vals hotmod_as[] = {
  44. { "mem", IPMI_MEM_ADDR_SPACE },
  45. { "i/o", IPMI_IO_ADDR_SPACE },
  46. { NULL }
  47. };
  48. static int parse_str(const struct hotmod_vals *v, int *val, char *name,
  49. char **curr)
  50. {
  51. char *s;
  52. int i;
  53. s = strchr(*curr, ',');
  54. if (!s) {
  55. pr_warn("No hotmod %s given\n", name);
  56. return -EINVAL;
  57. }
  58. *s = '\0';
  59. s++;
  60. for (i = 0; v[i].name; i++) {
  61. if (strcmp(*curr, v[i].name) == 0) {
  62. *val = v[i].val;
  63. *curr = s;
  64. return 0;
  65. }
  66. }
  67. pr_warn("Invalid hotmod %s '%s'\n", name, *curr);
  68. return -EINVAL;
  69. }
  70. static int check_hotmod_int_op(const char *curr, const char *option,
  71. const char *name, int *val)
  72. {
  73. char *n;
  74. if (strcmp(curr, name) == 0) {
  75. if (!option) {
  76. pr_warn("No option given for '%s'\n", curr);
  77. return -EINVAL;
  78. }
  79. *val = simple_strtoul(option, &n, 0);
  80. if ((*n != '\0') || (*option == '\0')) {
  81. pr_warn("Bad option given for '%s'\n", curr);
  82. return -EINVAL;
  83. }
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. static int hotmod_handler(const char *val, const struct kernel_param *kp)
  89. {
  90. char *str = kstrdup(val, GFP_KERNEL);
  91. int rv;
  92. char *next, *curr, *s, *n, *o;
  93. enum hotmod_op op;
  94. enum si_type si_type;
  95. int addr_space;
  96. unsigned long addr;
  97. int regspacing;
  98. int regsize;
  99. int regshift;
  100. int irq;
  101. int ipmb;
  102. int ival;
  103. int len;
  104. if (!str)
  105. return -ENOMEM;
  106. /* Kill any trailing spaces, as we can get a "\n" from echo. */
  107. len = strlen(str);
  108. ival = len - 1;
  109. while ((ival >= 0) && isspace(str[ival])) {
  110. str[ival] = '\0';
  111. ival--;
  112. }
  113. for (curr = str; curr; curr = next) {
  114. regspacing = 1;
  115. regsize = 1;
  116. regshift = 0;
  117. irq = 0;
  118. ipmb = 0; /* Choose the default if not specified */
  119. next = strchr(curr, ':');
  120. if (next) {
  121. *next = '\0';
  122. next++;
  123. }
  124. rv = parse_str(hotmod_ops, &ival, "operation", &curr);
  125. if (rv)
  126. break;
  127. op = ival;
  128. rv = parse_str(hotmod_si, &ival, "interface type", &curr);
  129. if (rv)
  130. break;
  131. si_type = ival;
  132. rv = parse_str(hotmod_as, &addr_space, "address space", &curr);
  133. if (rv)
  134. break;
  135. s = strchr(curr, ',');
  136. if (s) {
  137. *s = '\0';
  138. s++;
  139. }
  140. addr = simple_strtoul(curr, &n, 0);
  141. if ((*n != '\0') || (*curr == '\0')) {
  142. pr_warn("Invalid hotmod address '%s'\n", curr);
  143. break;
  144. }
  145. while (s) {
  146. curr = s;
  147. s = strchr(curr, ',');
  148. if (s) {
  149. *s = '\0';
  150. s++;
  151. }
  152. o = strchr(curr, '=');
  153. if (o) {
  154. *o = '\0';
  155. o++;
  156. }
  157. rv = check_hotmod_int_op(curr, o, "rsp", &regspacing);
  158. if (rv < 0)
  159. goto out;
  160. else if (rv)
  161. continue;
  162. rv = check_hotmod_int_op(curr, o, "rsi", &regsize);
  163. if (rv < 0)
  164. goto out;
  165. else if (rv)
  166. continue;
  167. rv = check_hotmod_int_op(curr, o, "rsh", &regshift);
  168. if (rv < 0)
  169. goto out;
  170. else if (rv)
  171. continue;
  172. rv = check_hotmod_int_op(curr, o, "irq", &irq);
  173. if (rv < 0)
  174. goto out;
  175. else if (rv)
  176. continue;
  177. rv = check_hotmod_int_op(curr, o, "ipmb", &ipmb);
  178. if (rv < 0)
  179. goto out;
  180. else if (rv)
  181. continue;
  182. rv = -EINVAL;
  183. pr_warn("Invalid hotmod option '%s'\n", curr);
  184. goto out;
  185. }
  186. if (op == HM_ADD) {
  187. struct si_sm_io io;
  188. memset(&io, 0, sizeof(io));
  189. io.addr_source = SI_HOTMOD;
  190. io.si_type = si_type;
  191. io.addr_data = addr;
  192. io.addr_type = addr_space;
  193. io.addr = NULL;
  194. io.regspacing = regspacing;
  195. if (!io.regspacing)
  196. io.regspacing = DEFAULT_REGSPACING;
  197. io.regsize = regsize;
  198. if (!io.regsize)
  199. io.regsize = DEFAULT_REGSIZE;
  200. io.regshift = regshift;
  201. io.irq = irq;
  202. if (io.irq)
  203. io.irq_setup = ipmi_std_irq_setup;
  204. io.slave_addr = ipmb;
  205. rv = ipmi_si_add_smi(&io);
  206. if (rv)
  207. goto out;
  208. } else {
  209. ipmi_si_remove_by_data(addr_space, si_type, addr);
  210. }
  211. }
  212. rv = len;
  213. out:
  214. kfree(str);
  215. return rv;
  216. }