smsgiucv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IUCV special message driver
  4. *
  5. * Copyright IBM Corp. 2003, 2009
  6. *
  7. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/errno.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <net/iucv/iucv.h>
  15. #include <asm/cpcmd.h>
  16. #include <asm/ebcdic.h>
  17. #include "smsgiucv.h"
  18. struct smsg_callback {
  19. struct list_head list;
  20. const char *prefix;
  21. int len;
  22. void (*callback)(const char *from, char *str);
  23. };
  24. MODULE_AUTHOR
  25. ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
  26. MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
  27. static struct iucv_path *smsg_path;
  28. /* dummy device used as trigger for PM functions */
  29. static struct device *smsg_dev;
  30. static DEFINE_SPINLOCK(smsg_list_lock);
  31. static LIST_HEAD(smsg_list);
  32. static int iucv_path_connected;
  33. static int smsg_path_pending(struct iucv_path *, u8 *, u8 *);
  34. static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
  35. static struct iucv_handler smsg_handler = {
  36. .path_pending = smsg_path_pending,
  37. .message_pending = smsg_message_pending,
  38. };
  39. static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser)
  40. {
  41. if (strncmp(ipvmid, "*MSG ", 8) != 0)
  42. return -EINVAL;
  43. /* Path pending from *MSG. */
  44. return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
  45. }
  46. static void smsg_message_pending(struct iucv_path *path,
  47. struct iucv_message *msg)
  48. {
  49. struct smsg_callback *cb;
  50. unsigned char *buffer;
  51. unsigned char sender[9];
  52. int rc, i;
  53. buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
  54. if (!buffer) {
  55. iucv_message_reject(path, msg);
  56. return;
  57. }
  58. rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
  59. if (rc == 0) {
  60. buffer[msg->length] = 0;
  61. EBCASC(buffer, msg->length);
  62. memcpy(sender, buffer, 8);
  63. sender[8] = 0;
  64. /* Remove trailing whitespace from the sender name. */
  65. for (i = 7; i >= 0; i--) {
  66. if (sender[i] != ' ' && sender[i] != '\t')
  67. break;
  68. sender[i] = 0;
  69. }
  70. spin_lock(&smsg_list_lock);
  71. list_for_each_entry(cb, &smsg_list, list)
  72. if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
  73. cb->callback(sender, buffer + 8);
  74. break;
  75. }
  76. spin_unlock(&smsg_list_lock);
  77. }
  78. kfree(buffer);
  79. }
  80. int smsg_register_callback(const char *prefix,
  81. void (*callback)(const char *from, char *str))
  82. {
  83. struct smsg_callback *cb;
  84. cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
  85. if (!cb)
  86. return -ENOMEM;
  87. cb->prefix = prefix;
  88. cb->len = strlen(prefix);
  89. cb->callback = callback;
  90. spin_lock_bh(&smsg_list_lock);
  91. list_add_tail(&cb->list, &smsg_list);
  92. spin_unlock_bh(&smsg_list_lock);
  93. return 0;
  94. }
  95. void smsg_unregister_callback(const char *prefix,
  96. void (*callback)(const char *from,
  97. char *str))
  98. {
  99. struct smsg_callback *cb, *tmp;
  100. spin_lock_bh(&smsg_list_lock);
  101. cb = NULL;
  102. list_for_each_entry(tmp, &smsg_list, list)
  103. if (tmp->callback == callback &&
  104. strcmp(tmp->prefix, prefix) == 0) {
  105. cb = tmp;
  106. list_del(&cb->list);
  107. break;
  108. }
  109. spin_unlock_bh(&smsg_list_lock);
  110. kfree(cb);
  111. }
  112. static int smsg_pm_freeze(struct device *dev)
  113. {
  114. #ifdef CONFIG_PM_DEBUG
  115. printk(KERN_WARNING "smsg_pm_freeze\n");
  116. #endif
  117. if (smsg_path && iucv_path_connected) {
  118. iucv_path_sever(smsg_path, NULL);
  119. iucv_path_connected = 0;
  120. }
  121. return 0;
  122. }
  123. static int smsg_pm_restore_thaw(struct device *dev)
  124. {
  125. int rc;
  126. #ifdef CONFIG_PM_DEBUG
  127. printk(KERN_WARNING "smsg_pm_restore_thaw\n");
  128. #endif
  129. if (smsg_path && !iucv_path_connected) {
  130. memset(smsg_path, 0, sizeof(*smsg_path));
  131. smsg_path->msglim = 255;
  132. smsg_path->flags = 0;
  133. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  134. NULL, NULL, NULL);
  135. #ifdef CONFIG_PM_DEBUG
  136. if (rc)
  137. printk(KERN_ERR
  138. "iucv_path_connect returned with rc %i\n", rc);
  139. #endif
  140. if (!rc)
  141. iucv_path_connected = 1;
  142. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  143. }
  144. return 0;
  145. }
  146. static const struct dev_pm_ops smsg_pm_ops = {
  147. .freeze = smsg_pm_freeze,
  148. .thaw = smsg_pm_restore_thaw,
  149. .restore = smsg_pm_restore_thaw,
  150. };
  151. static struct device_driver smsg_driver = {
  152. .owner = THIS_MODULE,
  153. .name = SMSGIUCV_DRV_NAME,
  154. .bus = &iucv_bus,
  155. .pm = &smsg_pm_ops,
  156. };
  157. static void __exit smsg_exit(void)
  158. {
  159. cpcmd("SET SMSG OFF", NULL, 0, NULL);
  160. device_unregister(smsg_dev);
  161. iucv_unregister(&smsg_handler, 1);
  162. driver_unregister(&smsg_driver);
  163. }
  164. static int __init smsg_init(void)
  165. {
  166. int rc;
  167. if (!MACHINE_IS_VM) {
  168. rc = -EPROTONOSUPPORT;
  169. goto out;
  170. }
  171. rc = driver_register(&smsg_driver);
  172. if (rc != 0)
  173. goto out;
  174. rc = iucv_register(&smsg_handler, 1);
  175. if (rc)
  176. goto out_driver;
  177. smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
  178. if (!smsg_path) {
  179. rc = -ENOMEM;
  180. goto out_register;
  181. }
  182. rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
  183. NULL, NULL, NULL);
  184. if (rc)
  185. goto out_free_path;
  186. else
  187. iucv_path_connected = 1;
  188. smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  189. if (!smsg_dev) {
  190. rc = -ENOMEM;
  191. goto out_free_path;
  192. }
  193. dev_set_name(smsg_dev, "smsg_iucv");
  194. smsg_dev->bus = &iucv_bus;
  195. smsg_dev->parent = iucv_root;
  196. smsg_dev->release = (void (*)(struct device *))kfree;
  197. smsg_dev->driver = &smsg_driver;
  198. rc = device_register(smsg_dev);
  199. if (rc)
  200. goto out_put;
  201. cpcmd("SET SMSG IUCV", NULL, 0, NULL);
  202. return 0;
  203. out_put:
  204. put_device(smsg_dev);
  205. out_free_path:
  206. iucv_path_free(smsg_path);
  207. smsg_path = NULL;
  208. out_register:
  209. iucv_unregister(&smsg_handler, 1);
  210. out_driver:
  211. driver_unregister(&smsg_driver);
  212. out:
  213. return rc;
  214. }
  215. module_init(smsg_init);
  216. module_exit(smsg_exit);
  217. MODULE_LICENSE("GPL");
  218. EXPORT_SYMBOL(smsg_register_callback);
  219. EXPORT_SYMBOL(smsg_unregister_callback);