sclp_cpi_sys.c 8.6 KB

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