Greg Kroah-Hartman | ab9953f | 2017-11-14 18:38:04 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * IUCV special message driver |
| 4 | * |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2003, 2009 |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 15 | #include <net/iucv/iucv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/cpcmd.h> |
| 17 | #include <asm/ebcdic.h> |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 18 | #include "smsgiucv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | struct smsg_callback { |
| 21 | struct list_head list; |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 22 | const char *prefix; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | int len; |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 24 | void (*callback)(const char *from, char *str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 31 | static struct iucv_path *smsg_path; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 32 | /* dummy device used as trigger for PM functions */ |
| 33 | static struct device *smsg_dev; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(smsg_list_lock); |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 36 | static LIST_HEAD(smsg_list); |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 37 | static int iucv_path_connected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 39 | static int smsg_path_pending(struct iucv_path *, u8 *, u8 *); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 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 | |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 47 | static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Stefan Weil | 08b0183 | 2011-02-02 06:04:36 +0000 | [diff] [blame] | 49 | if (strncmp(ipvmid, "*MSG ", 8) != 0) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | /* Path pending from *MSG. */ |
| 52 | return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 55 | static void smsg_message_pending(struct iucv_path *path, |
| 56 | struct iucv_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | struct smsg_callback *cb; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 59 | unsigned char *buffer; |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 60 | unsigned char sender[9]; |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 61 | int rc, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 63 | buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA); |
| 64 | if (!buffer) { |
| 65 | iucv_message_reject(path, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return; |
| 67 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 68 | rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (rc == 0) { |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 70 | buffer[msg->length] = 0; |
| 71 | EBCASC(buffer, msg->length); |
| 72 | memcpy(sender, buffer, 8); |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | spin_lock(&smsg_list_lock); |
| 81 | list_for_each_entry(cb, &smsg_list, list) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 82 | if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) { |
| 83 | cb->callback(sender, buffer + 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | spin_unlock(&smsg_list_lock); |
| 87 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 88 | kfree(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 91 | int smsg_register_callback(const char *prefix, |
| 92 | void (*callback)(const char *from, char *str)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 102 | spin_lock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | list_add_tail(&cb->list, &smsg_list); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 104 | spin_unlock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 108 | void smsg_unregister_callback(const char *prefix, |
| 109 | void (*callback)(const char *from, |
| 110 | char *str)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | struct smsg_callback *cb, *tmp; |
| 113 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 114 | spin_lock_bh(&smsg_list_lock); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 115 | cb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 123 | spin_unlock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | kfree(cb); |
| 125 | } |
| 126 | |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 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 |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 132 | if (smsg_path && iucv_path_connected) { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 133 | iucv_path_sever(smsg_path, NULL); |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 134 | iucv_path_connected = 0; |
| 135 | } |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 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 |
Hendrik Brueckner | 1c8161a | 2012-10-15 19:21:17 +0000 | [diff] [blame] | 146 | if (smsg_path && !iucv_path_connected) { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 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); |
Martin Schwidefsky | 8ca4566 | 2009-10-29 15:04:08 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_PM_DEBUG |
| 153 | if (rc) |
| 154 | printk(KERN_ERR |
| 155 | "iucv_path_connect returned with rc %i\n", rc); |
| 156 | #endif |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 157 | if (!rc) |
| 158 | iucv_path_connected = 1; |
Martin Schwidefsky | 8ca4566 | 2009-10-29 15:04:08 +0100 | [diff] [blame] | 159 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 164 | static const struct dev_pm_ops smsg_pm_ops = { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 165 | .freeze = smsg_pm_freeze, |
| 166 | .thaw = smsg_pm_restore_thaw, |
| 167 | .restore = smsg_pm_restore_thaw, |
| 168 | }; |
| 169 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 170 | static struct device_driver smsg_driver = { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 171 | .owner = THIS_MODULE, |
Hendrik Brueckner | 1ffaa64 | 2010-03-08 12:26:26 +0100 | [diff] [blame] | 172 | .name = SMSGIUCV_DRV_NAME, |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 173 | .bus = &iucv_bus, |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 174 | .pm = &smsg_pm_ops, |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static void __exit smsg_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 179 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 180 | device_unregister(smsg_dev); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 181 | iucv_unregister(&smsg_handler, 1); |
| 182 | driver_unregister(&smsg_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 185 | static int __init smsg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | int rc; |
| 188 | |
Christian Borntraeger | 0fc3ddd | 2007-11-05 11:10:08 +0100 | [diff] [blame] | 189 | if (!MACHINE_IS_VM) { |
| 190 | rc = -EPROTONOSUPPORT; |
| 191 | goto out; |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | rc = driver_register(&smsg_driver); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 194 | if (rc != 0) |
| 195 | goto out; |
| 196 | rc = iucv_register(&smsg_handler, 1); |
Martin Schwidefsky | d5ddc80 | 2008-07-14 09:59:33 +0200 | [diff] [blame] | 197 | if (rc) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 198 | goto out_driver; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 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); |
Martin Schwidefsky | d5ddc80 | 2008-07-14 09:59:33 +0200 | [diff] [blame] | 206 | if (rc) |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 207 | goto out_free_path; |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 208 | else |
| 209 | iucv_path_connected = 1; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 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) |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 222 | goto out_put; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 223 | |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 224 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return 0; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 226 | |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 227 | out_put: |
| 228 | put_device(smsg_dev); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 229 | out_free_path: |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 230 | iucv_path_free(smsg_path); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 231 | smsg_path = NULL; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 232 | out_register: |
| 233 | iucv_unregister(&smsg_handler, 1); |
| 234 | out_driver: |
| 235 | driver_unregister(&smsg_driver); |
| 236 | out: |
| 237 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |