sclp_cpi_sys.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCLP control program identification sysfs interface
  4. *
  5. * Copyright IBM Corp. 2001, 2007
  6. * Author(s): Martin Peschke <[email protected]>
  7. * Michael Ernst <[email protected]>
  8. */
  9. #define KMSG_COMPONENT "sclp_cpi"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/stat.h>
  14. #include <linux/device.h>
  15. #include <linux/string.h>
  16. #include <linux/ctype.h>
  17. #include <linux/kmod.h>
  18. #include <linux/timer.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/completion.h>
  22. #include <linux/export.h>
  23. #include <asm/ebcdic.h>
  24. #include <asm/sclp.h>
  25. #include "sclp.h"
  26. #include "sclp_rw.h"
  27. #include "sclp_cpi_sys.h"
  28. #define CPI_LENGTH_NAME 8
  29. #define CPI_LENGTH_LEVEL 16
  30. static DEFINE_MUTEX(sclp_cpi_mutex);
  31. struct cpi_evbuf {
  32. struct evbuf_header header;
  33. u8 id_format;
  34. u8 reserved0;
  35. u8 system_type[CPI_LENGTH_NAME];
  36. u64 reserved1;
  37. u8 system_name[CPI_LENGTH_NAME];
  38. u64 reserved2;
  39. u64 system_level;
  40. u64 reserved3;
  41. u8 sysplex_name[CPI_LENGTH_NAME];
  42. u8 reserved4[16];
  43. } __attribute__((packed));
  44. struct cpi_sccb {
  45. struct sccb_header header;
  46. struct cpi_evbuf cpi_evbuf;
  47. } __attribute__((packed));
  48. static struct sclp_register sclp_cpi_event = {
  49. .send_mask = EVTYP_CTLPROGIDENT_MASK,
  50. };
  51. static char system_name[CPI_LENGTH_NAME + 1];
  52. static char sysplex_name[CPI_LENGTH_NAME + 1];
  53. static char system_type[CPI_LENGTH_NAME + 1];
  54. static u64 system_level;
  55. static void set_data(char *field, char *data)
  56. {
  57. memset(field, ' ', CPI_LENGTH_NAME);
  58. memcpy(field, data, strlen(data));
  59. sclp_ascebc_str(field, CPI_LENGTH_NAME);
  60. }
  61. static void cpi_callback(struct sclp_req *req, void *data)
  62. {
  63. struct completion *completion = data;
  64. complete(completion);
  65. }
  66. static struct sclp_req *cpi_prepare_req(void)
  67. {
  68. struct sclp_req *req;
  69. struct cpi_sccb *sccb;
  70. struct cpi_evbuf *evb;
  71. req = kzalloc(sizeof(struct sclp_req), GFP_KERNEL);
  72. if (!req)
  73. return ERR_PTR(-ENOMEM);
  74. sccb = (struct cpi_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  75. if (!sccb) {
  76. kfree(req);
  77. return ERR_PTR(-ENOMEM);
  78. }
  79. /* setup SCCB for Control-Program Identification */
  80. sccb->header.length = sizeof(struct cpi_sccb);
  81. sccb->cpi_evbuf.header.length = sizeof(struct cpi_evbuf);
  82. sccb->cpi_evbuf.header.type = EVTYP_CTLPROGIDENT;
  83. evb = &sccb->cpi_evbuf;
  84. /* set system type */
  85. set_data(evb->system_type, system_type);
  86. /* set system name */
  87. set_data(evb->system_name, system_name);
  88. /* set system level */
  89. evb->system_level = system_level;
  90. /* set sysplex name */
  91. set_data(evb->sysplex_name, sysplex_name);
  92. /* prepare request data structure presented to SCLP driver */
  93. req->command = SCLP_CMDW_WRITE_EVENT_DATA;
  94. req->sccb = sccb;
  95. req->status = SCLP_REQ_FILLED;
  96. req->callback = cpi_callback;
  97. return req;
  98. }
  99. static void cpi_free_req(struct sclp_req *req)
  100. {
  101. free_page((unsigned long) req->sccb);
  102. kfree(req);
  103. }
  104. static int cpi_req(void)
  105. {
  106. struct completion completion;
  107. struct sclp_req *req;
  108. int rc;
  109. int response;
  110. rc = sclp_register(&sclp_cpi_event);
  111. if (rc)
  112. goto out;
  113. if (!(sclp_cpi_event.sclp_receive_mask & EVTYP_CTLPROGIDENT_MASK)) {
  114. rc = -EOPNOTSUPP;
  115. goto out_unregister;
  116. }
  117. req = cpi_prepare_req();
  118. if (IS_ERR(req)) {
  119. rc = PTR_ERR(req);
  120. goto out_unregister;
  121. }
  122. init_completion(&completion);
  123. req->callback_data = &completion;
  124. /* Add request to sclp queue */
  125. rc = sclp_add_request(req);
  126. if (rc)
  127. goto out_free_req;
  128. wait_for_completion(&completion);
  129. if (req->status != SCLP_REQ_DONE) {
  130. pr_warn("request failed (status=0x%02x)\n", req->status);
  131. rc = -EIO;
  132. goto out_free_req;
  133. }
  134. response = ((struct cpi_sccb *) req->sccb)->header.response_code;
  135. if (response != 0x0020) {
  136. pr_warn("request failed with response code 0x%x\n", response);
  137. rc = -EIO;
  138. }
  139. out_free_req:
  140. cpi_free_req(req);
  141. out_unregister:
  142. sclp_unregister(&sclp_cpi_event);
  143. out:
  144. return rc;
  145. }
  146. static int check_string(const char *attr, const char *str)
  147. {
  148. size_t len;
  149. size_t i;
  150. len = strlen(str);
  151. if ((len > 0) && (str[len - 1] == '\n'))
  152. len--;
  153. if (len > CPI_LENGTH_NAME)
  154. return -EINVAL;
  155. for (i = 0; i < len ; i++) {
  156. if (isalpha(str[i]) || isdigit(str[i]) ||
  157. strchr("$@# ", str[i]))
  158. continue;
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. static void set_string(char *attr, const char *value)
  164. {
  165. size_t len;
  166. size_t i;
  167. len = strlen(value);
  168. if ((len > 0) && (value[len - 1] == '\n'))
  169. len--;
  170. for (i = 0; i < CPI_LENGTH_NAME; i++) {
  171. if (i < len)
  172. attr[i] = toupper(value[i]);
  173. else
  174. attr[i] = ' ';
  175. }
  176. }
  177. static ssize_t system_name_show(struct kobject *kobj,
  178. struct kobj_attribute *attr, char *page)
  179. {
  180. int rc;
  181. mutex_lock(&sclp_cpi_mutex);
  182. rc = snprintf(page, PAGE_SIZE, "%s\n", system_name);
  183. mutex_unlock(&sclp_cpi_mutex);
  184. return rc;
  185. }
  186. static ssize_t system_name_store(struct kobject *kobj,
  187. struct kobj_attribute *attr,
  188. const char *buf,
  189. size_t len)
  190. {
  191. int rc;
  192. rc = check_string("system_name", buf);
  193. if (rc)
  194. return rc;
  195. mutex_lock(&sclp_cpi_mutex);
  196. set_string(system_name, buf);
  197. mutex_unlock(&sclp_cpi_mutex);
  198. return len;
  199. }
  200. static struct kobj_attribute system_name_attr =
  201. __ATTR(system_name, 0644, system_name_show, system_name_store);
  202. static ssize_t sysplex_name_show(struct kobject *kobj,
  203. struct kobj_attribute *attr, char *page)
  204. {
  205. int rc;
  206. mutex_lock(&sclp_cpi_mutex);
  207. rc = snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
  208. mutex_unlock(&sclp_cpi_mutex);
  209. return rc;
  210. }
  211. static ssize_t sysplex_name_store(struct kobject *kobj,
  212. struct kobj_attribute *attr,
  213. const char *buf,
  214. size_t len)
  215. {
  216. int rc;
  217. rc = check_string("sysplex_name", buf);
  218. if (rc)
  219. return rc;
  220. mutex_lock(&sclp_cpi_mutex);
  221. set_string(sysplex_name, buf);
  222. mutex_unlock(&sclp_cpi_mutex);
  223. return len;
  224. }
  225. static struct kobj_attribute sysplex_name_attr =
  226. __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
  227. static ssize_t system_type_show(struct kobject *kobj,
  228. struct kobj_attribute *attr, char *page)
  229. {
  230. int rc;
  231. mutex_lock(&sclp_cpi_mutex);
  232. rc = snprintf(page, PAGE_SIZE, "%s\n", system_type);
  233. mutex_unlock(&sclp_cpi_mutex);
  234. return rc;
  235. }
  236. static ssize_t system_type_store(struct kobject *kobj,
  237. struct kobj_attribute *attr,
  238. const char *buf,
  239. size_t len)
  240. {
  241. int rc;
  242. rc = check_string("system_type", buf);
  243. if (rc)
  244. return rc;
  245. mutex_lock(&sclp_cpi_mutex);
  246. set_string(system_type, buf);
  247. mutex_unlock(&sclp_cpi_mutex);
  248. return len;
  249. }
  250. static struct kobj_attribute system_type_attr =
  251. __ATTR(system_type, 0644, system_type_show, system_type_store);
  252. static ssize_t system_level_show(struct kobject *kobj,
  253. struct kobj_attribute *attr, char *page)
  254. {
  255. unsigned long long level;
  256. mutex_lock(&sclp_cpi_mutex);
  257. level = system_level;
  258. mutex_unlock(&sclp_cpi_mutex);
  259. return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
  260. }
  261. static ssize_t system_level_store(struct kobject *kobj,
  262. struct kobj_attribute *attr,
  263. const char *buf,
  264. size_t len)
  265. {
  266. unsigned long long level;
  267. char *endp;
  268. level = simple_strtoull(buf, &endp, 16);
  269. if (endp == buf)
  270. return -EINVAL;
  271. if (*endp == '\n')
  272. endp++;
  273. if (*endp)
  274. return -EINVAL;
  275. mutex_lock(&sclp_cpi_mutex);
  276. system_level = level;
  277. mutex_unlock(&sclp_cpi_mutex);
  278. return len;
  279. }
  280. static struct kobj_attribute system_level_attr =
  281. __ATTR(system_level, 0644, system_level_show, system_level_store);
  282. static ssize_t set_store(struct kobject *kobj,
  283. struct kobj_attribute *attr,
  284. const char *buf, size_t len)
  285. {
  286. int rc;
  287. mutex_lock(&sclp_cpi_mutex);
  288. rc = cpi_req();
  289. mutex_unlock(&sclp_cpi_mutex);
  290. if (rc)
  291. return rc;
  292. return len;
  293. }
  294. static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
  295. static struct attribute *cpi_attrs[] = {
  296. &system_name_attr.attr,
  297. &sysplex_name_attr.attr,
  298. &system_type_attr.attr,
  299. &system_level_attr.attr,
  300. &set_attr.attr,
  301. NULL,
  302. };
  303. static struct attribute_group cpi_attr_group = {
  304. .attrs = cpi_attrs,
  305. };
  306. static struct kset *cpi_kset;
  307. int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
  308. const u64 level)
  309. {
  310. int rc;
  311. rc = check_string("system_name", system);
  312. if (rc)
  313. return rc;
  314. rc = check_string("sysplex_name", sysplex);
  315. if (rc)
  316. return rc;
  317. rc = check_string("system_type", type);
  318. if (rc)
  319. return rc;
  320. mutex_lock(&sclp_cpi_mutex);
  321. set_string(system_name, system);
  322. set_string(sysplex_name, sysplex);
  323. set_string(system_type, type);
  324. system_level = level;
  325. rc = cpi_req();
  326. mutex_unlock(&sclp_cpi_mutex);
  327. return rc;
  328. }
  329. EXPORT_SYMBOL(sclp_cpi_set_data);
  330. static int __init cpi_init(void)
  331. {
  332. int rc;
  333. cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
  334. if (!cpi_kset)
  335. return -ENOMEM;
  336. rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
  337. if (rc)
  338. kset_unregister(cpi_kset);
  339. return rc;
  340. }
  341. __initcall(cpi_init);