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 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/errno.h> 13 #include <linux/device.h> 14 #include <linux/slab.h> 15 #include <net/iucv/iucv.h> 16 #include <asm/machine.h> 17 #include <asm/cpcmd.h> 18 #include <asm/ebcdic.h> 19 #include "smsgiucv.h" 20 21 struct smsg_callback { 22 struct list_head list; 23 const char *prefix; 24 int len; 25 void (*callback)(const char *from, char *str); 26 }; 27 28 MODULE_AUTHOR 29 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)"); 30 MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver"); 31 32 static struct iucv_path *smsg_path; 33 34 static DEFINE_SPINLOCK(smsg_list_lock); 35 static LIST_HEAD(smsg_list); 36 37 static int smsg_path_pending(struct iucv_path *, u8 *, u8 *); 38 static void smsg_message_pending(struct iucv_path *, struct iucv_message *); 39 40 static struct iucv_handler smsg_handler = { 41 .path_pending = smsg_path_pending, 42 .message_pending = smsg_message_pending, 43 }; 44 45 static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser) 46 { 47 if (strncmp(ipvmid, "*MSG ", 8) != 0) 48 return -EINVAL; 49 /* Path pending from *MSG. */ 50 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL); 51 } 52 53 static void smsg_message_pending(struct iucv_path *path, 54 struct iucv_message *msg) 55 { 56 struct smsg_callback *cb; 57 unsigned char *buffer; 58 unsigned char sender[9]; 59 int rc, i; 60 61 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA); 62 if (!buffer) { 63 iucv_message_reject(path, msg); 64 return; 65 } 66 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL); 67 if (rc == 0) { 68 buffer[msg->length] = 0; 69 EBCASC(buffer, msg->length); 70 memcpy(sender, buffer, 8); 71 sender[8] = 0; 72 /* Remove trailing whitespace from the sender name. */ 73 for (i = 7; i >= 0; i--) { 74 if (sender[i] != ' ' && sender[i] != '\t') 75 break; 76 sender[i] = 0; 77 } 78 spin_lock(&smsg_list_lock); 79 list_for_each_entry(cb, &smsg_list, list) 80 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) { 81 cb->callback(sender, buffer + 8); 82 break; 83 } 84 spin_unlock(&smsg_list_lock); 85 } 86 kfree(buffer); 87 } 88 89 int smsg_register_callback(const char *prefix, 90 void (*callback)(const char *from, char *str)) 91 { 92 struct smsg_callback *cb; 93 94 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL); 95 if (!cb) 96 return -ENOMEM; 97 cb->prefix = prefix; 98 cb->len = strlen(prefix); 99 cb->callback = callback; 100 spin_lock_bh(&smsg_list_lock); 101 list_add_tail(&cb->list, &smsg_list); 102 spin_unlock_bh(&smsg_list_lock); 103 return 0; 104 } 105 106 void smsg_unregister_callback(const char *prefix, 107 void (*callback)(const char *from, 108 char *str)) 109 { 110 struct smsg_callback *cb, *tmp; 111 112 spin_lock_bh(&smsg_list_lock); 113 cb = NULL; 114 list_for_each_entry(tmp, &smsg_list, list) 115 if (tmp->callback == callback && 116 strcmp(tmp->prefix, prefix) == 0) { 117 cb = tmp; 118 list_del(&cb->list); 119 break; 120 } 121 spin_unlock_bh(&smsg_list_lock); 122 kfree(cb); 123 } 124 125 static struct device_driver smsg_driver = { 126 .owner = THIS_MODULE, 127 .name = SMSGIUCV_DRV_NAME, 128 .bus = &iucv_bus, 129 }; 130 131 static void __exit smsg_exit(void) 132 { 133 cpcmd("SET SMSG OFF", NULL, 0, NULL); 134 iucv_unregister(&smsg_handler, 1); 135 driver_unregister(&smsg_driver); 136 } 137 138 static int __init smsg_init(void) 139 { 140 int rc; 141 142 if (!machine_is_vm()) { 143 rc = -EPROTONOSUPPORT; 144 goto out; 145 } 146 rc = driver_register(&smsg_driver); 147 if (rc != 0) 148 goto out; 149 rc = iucv_register(&smsg_handler, 1); 150 if (rc) 151 goto out_driver; 152 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL); 153 if (!smsg_path) { 154 rc = -ENOMEM; 155 goto out_register; 156 } 157 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ", 158 NULL, NULL, NULL); 159 if (rc) 160 goto out_free_path; 161 162 cpcmd("SET SMSG IUCV", NULL, 0, NULL); 163 return 0; 164 165 out_free_path: 166 iucv_path_free(smsg_path); 167 smsg_path = NULL; 168 out_register: 169 iucv_unregister(&smsg_handler, 1); 170 out_driver: 171 driver_unregister(&smsg_driver); 172 out: 173 return rc; 174 } 175 176 module_init(smsg_init); 177 module_exit(smsg_exit); 178 MODULE_LICENSE("GPL"); 179 180 EXPORT_SYMBOL(smsg_register_callback); 181 EXPORT_SYMBOL(smsg_unregister_callback); 182