sclp_cpi_sys.c 8.6 KB

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