sclp_cpi_sys.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * drivers/s390/char/sclp_cpi_sys.c
  3. * SCLP control program identification sysfs interface
  4. *
  5. * Copyright IBM Corp. 2001, 2007
  6. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  7. * Michael Ernst <mernst@de.ibm.com>
  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 = 0x0b;
  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_warning("request failed (status=0x%02x)\n",
  131. req->status);
  132. rc = -EIO;
  133. goto out_free_req;
  134. }
  135. response = ((struct cpi_sccb *) req->sccb)->header.response_code;
  136. if (response != 0x0020) {
  137. pr_warning("request failed with response code 0x%x\n",
  138. response);
  139. rc = -EIO;
  140. }
  141. out_free_req:
  142. cpi_free_req(req);
  143. out_unregister:
  144. sclp_unregister(&sclp_cpi_event);
  145. out:
  146. return rc;
  147. }
  148. static int check_string(const char *attr, const char *str)
  149. {
  150. size_t len;
  151. size_t i;
  152. len = strlen(str);
  153. if ((len > 0) && (str[len - 1] == '\n'))
  154. len--;
  155. if (len > CPI_LENGTH_NAME)
  156. return -EINVAL;
  157. for (i = 0; i < len ; i++) {
  158. if (isalpha(str[i]) || isdigit(str[i]) ||
  159. strchr("$@# ", str[i]))
  160. continue;
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. static void set_string(char *attr, const char *value)
  166. {
  167. size_t len;
  168. size_t i;
  169. len = strlen(value);
  170. if ((len > 0) && (value[len - 1] == '\n'))
  171. len--;
  172. for (i = 0; i < CPI_LENGTH_NAME; i++) {
  173. if (i < len)
  174. attr[i] = toupper(value[i]);
  175. else
  176. attr[i] = ' ';
  177. }
  178. }
  179. static ssize_t system_name_show(struct kobject *kobj,
  180. struct kobj_attribute *attr, char *page)
  181. {
  182. int rc;
  183. mutex_lock(&sclp_cpi_mutex);
  184. rc = snprintf(page, PAGE_SIZE, "%s\n", system_name);
  185. mutex_unlock(&sclp_cpi_mutex);
  186. return rc;
  187. }
  188. static ssize_t system_name_store(struct kobject *kobj,
  189. struct kobj_attribute *attr,
  190. const char *buf,
  191. size_t len)
  192. {
  193. int rc;
  194. rc = check_string("system_name", buf);
  195. if (rc)
  196. return rc;
  197. mutex_lock(&sclp_cpi_mutex);
  198. set_string(system_name, buf);
  199. mutex_unlock(&sclp_cpi_mutex);
  200. return len;
  201. }
  202. static struct kobj_attribute system_name_attr =
  203. __ATTR(system_name, 0644, system_name_show, system_name_store);
  204. static ssize_t sysplex_name_show(struct kobject *kobj,
  205. struct kobj_attribute *attr, char *page)
  206. {
  207. int rc;
  208. mutex_lock(&sclp_cpi_mutex);
  209. rc = snprintf(page, PAGE_SIZE, "%s\n", sysplex_name);
  210. mutex_unlock(&sclp_cpi_mutex);
  211. return rc;
  212. }
  213. static ssize_t sysplex_name_store(struct kobject *kobj,
  214. struct kobj_attribute *attr,
  215. const char *buf,
  216. size_t len)
  217. {
  218. int rc;
  219. rc = check_string("sysplex_name", buf);
  220. if (rc)
  221. return rc;
  222. mutex_lock(&sclp_cpi_mutex);
  223. set_string(sysplex_name, buf);
  224. mutex_unlock(&sclp_cpi_mutex);
  225. return len;
  226. }
  227. static struct kobj_attribute sysplex_name_attr =
  228. __ATTR(sysplex_name, 0644, sysplex_name_show, sysplex_name_store);
  229. static ssize_t system_type_show(struct kobject *kobj,
  230. struct kobj_attribute *attr, char *page)
  231. {
  232. int rc;
  233. mutex_lock(&sclp_cpi_mutex);
  234. rc = snprintf(page, PAGE_SIZE, "%s\n", system_type);
  235. mutex_unlock(&sclp_cpi_mutex);
  236. return rc;
  237. }
  238. static ssize_t system_type_store(struct kobject *kobj,
  239. struct kobj_attribute *attr,
  240. const char *buf,
  241. size_t len)
  242. {
  243. int rc;
  244. rc = check_string("system_type", buf);
  245. if (rc)
  246. return rc;
  247. mutex_lock(&sclp_cpi_mutex);
  248. set_string(system_type, buf);
  249. mutex_unlock(&sclp_cpi_mutex);
  250. return len;
  251. }
  252. static struct kobj_attribute system_type_attr =
  253. __ATTR(system_type, 0644, system_type_show, system_type_store);
  254. static ssize_t system_level_show(struct kobject *kobj,
  255. struct kobj_attribute *attr, char *page)
  256. {
  257. unsigned long long level;
  258. mutex_lock(&sclp_cpi_mutex);
  259. level = system_level;
  260. mutex_unlock(&sclp_cpi_mutex);
  261. return snprintf(page, PAGE_SIZE, "%#018llx\n", level);
  262. }
  263. static ssize_t system_level_store(struct kobject *kobj,
  264. struct kobj_attribute *attr,
  265. const char *buf,
  266. size_t len)
  267. {
  268. unsigned long long level;
  269. char *endp;
  270. level = simple_strtoull(buf, &endp, 16);
  271. if (endp == buf)
  272. return -EINVAL;
  273. if (*endp == '\n')
  274. endp++;
  275. if (*endp)
  276. return -EINVAL;
  277. mutex_lock(&sclp_cpi_mutex);
  278. system_level = level;
  279. mutex_unlock(&sclp_cpi_mutex);
  280. return len;
  281. }
  282. static struct kobj_attribute system_level_attr =
  283. __ATTR(system_level, 0644, system_level_show, system_level_store);
  284. static ssize_t set_store(struct kobject *kobj,
  285. struct kobj_attribute *attr,
  286. const char *buf, size_t len)
  287. {
  288. int rc;
  289. mutex_lock(&sclp_cpi_mutex);
  290. rc = cpi_req();
  291. mutex_unlock(&sclp_cpi_mutex);
  292. if (rc)
  293. return rc;
  294. return len;
  295. }
  296. static struct kobj_attribute set_attr = __ATTR(set, 0200, NULL, set_store);
  297. static struct attribute *cpi_attrs[] = {
  298. &system_name_attr.attr,
  299. &sysplex_name_attr.attr,
  300. &system_type_attr.attr,
  301. &system_level_attr.attr,
  302. &set_attr.attr,
  303. NULL,
  304. };
  305. static struct attribute_group cpi_attr_group = {
  306. .attrs = cpi_attrs,
  307. };
  308. static struct kset *cpi_kset;
  309. int sclp_cpi_set_data(const char *system, const char *sysplex, const char *type,
  310. const u64 level)
  311. {
  312. int rc;
  313. rc = check_string("system_name", system);
  314. if (rc)
  315. return rc;
  316. rc = check_string("sysplex_name", sysplex);
  317. if (rc)
  318. return rc;
  319. rc = check_string("system_type", type);
  320. if (rc)
  321. return rc;
  322. mutex_lock(&sclp_cpi_mutex);
  323. set_string(system_name, system);
  324. set_string(sysplex_name, sysplex);
  325. set_string(system_type, type);
  326. system_level = level;
  327. rc = cpi_req();
  328. mutex_unlock(&sclp_cpi_mutex);
  329. return rc;
  330. }
  331. EXPORT_SYMBOL(sclp_cpi_set_data);
  332. static int __init cpi_init(void)
  333. {
  334. int rc;
  335. cpi_kset = kset_create_and_add("cpi", NULL, firmware_kobj);
  336. if (!cpi_kset)
  337. return -ENOMEM;
  338. rc = sysfs_create_group(&cpi_kset->kobj, &cpi_attr_group);
  339. if (rc)
  340. kset_unregister(cpi_kset);
  341. return rc;
  342. }
  343. __initcall(cpi_init);