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