sclp_cpi_sys.c 8.1 KB

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