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