Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IUCV special message driver |
| 3 | * |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2003, 2009 |
| 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 28 | #include <net/iucv/iucv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/cpcmd.h> |
| 30 | #include <asm/ebcdic.h> |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 31 | #include "smsgiucv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | struct smsg_callback { |
| 34 | struct list_head list; |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 35 | const char *prefix; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int len; |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 37 | void (*callback)(const char *from, char *str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | MODULE_AUTHOR |
| 41 | ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)"); |
| 42 | MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver"); |
| 43 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 44 | static struct iucv_path *smsg_path; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 45 | /* dummy device used as trigger for PM functions */ |
| 46 | static struct device *smsg_dev; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static DEFINE_SPINLOCK(smsg_list_lock); |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 49 | static LIST_HEAD(smsg_list); |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 50 | static int iucv_path_connected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 52 | static int smsg_path_pending(struct iucv_path *, u8 *, u8 *); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 53 | static void smsg_message_pending(struct iucv_path *, struct iucv_message *); |
| 54 | |
| 55 | static struct iucv_handler smsg_handler = { |
| 56 | .path_pending = smsg_path_pending, |
| 57 | .message_pending = smsg_message_pending, |
| 58 | }; |
| 59 | |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 60 | 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] | 61 | { |
Stefan Weil | 08b0183 | 2011-02-02 06:04:36 +0000 | [diff] [blame] | 62 | if (strncmp(ipvmid, "*MSG ", 8) != 0) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 63 | return -EINVAL; |
| 64 | /* Path pending from *MSG. */ |
| 65 | return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 68 | static void smsg_message_pending(struct iucv_path *path, |
| 69 | struct iucv_message *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | struct smsg_callback *cb; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 72 | unsigned char *buffer; |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 73 | unsigned char sender[9]; |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 74 | int rc, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 76 | buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA); |
| 77 | if (!buffer) { |
| 78 | iucv_message_reject(path, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return; |
| 80 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 81 | rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (rc == 0) { |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 83 | buffer[msg->length] = 0; |
| 84 | EBCASC(buffer, msg->length); |
| 85 | memcpy(sender, buffer, 8); |
Martin Schwidefsky | 15439d7 | 2005-05-01 08:58:58 -0700 | [diff] [blame] | 86 | sender[8] = 0; |
| 87 | /* Remove trailing whitespace from the sender name. */ |
| 88 | for (i = 7; i >= 0; i--) { |
| 89 | if (sender[i] != ' ' && sender[i] != '\t') |
| 90 | break; |
| 91 | sender[i] = 0; |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | spin_lock(&smsg_list_lock); |
| 94 | list_for_each_entry(cb, &smsg_list, list) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 95 | if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) { |
| 96 | cb->callback(sender, buffer + 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | spin_unlock(&smsg_list_lock); |
| 100 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 101 | kfree(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 104 | int smsg_register_callback(const char *prefix, |
| 105 | void (*callback)(const char *from, char *str)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | struct smsg_callback *cb; |
| 108 | |
| 109 | cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL); |
| 110 | if (!cb) |
| 111 | return -ENOMEM; |
| 112 | cb->prefix = prefix; |
| 113 | cb->len = strlen(prefix); |
| 114 | cb->callback = callback; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 115 | spin_lock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | list_add_tail(&cb->list, &smsg_list); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 117 | spin_unlock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Hendrik Brueckner | 09003ed | 2010-03-08 12:26:25 +0100 | [diff] [blame] | 121 | void smsg_unregister_callback(const char *prefix, |
| 122 | void (*callback)(const char *from, |
| 123 | char *str)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | struct smsg_callback *cb, *tmp; |
| 126 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 127 | spin_lock_bh(&smsg_list_lock); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 128 | cb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | list_for_each_entry(tmp, &smsg_list, list) |
| 130 | if (tmp->callback == callback && |
| 131 | strcmp(tmp->prefix, prefix) == 0) { |
| 132 | cb = tmp; |
| 133 | list_del(&cb->list); |
| 134 | break; |
| 135 | } |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 136 | spin_unlock_bh(&smsg_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | kfree(cb); |
| 138 | } |
| 139 | |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 140 | static int smsg_pm_freeze(struct device *dev) |
| 141 | { |
| 142 | #ifdef CONFIG_PM_DEBUG |
| 143 | printk(KERN_WARNING "smsg_pm_freeze\n"); |
| 144 | #endif |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 145 | if (smsg_path && iucv_path_connected) { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 146 | iucv_path_sever(smsg_path, NULL); |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 147 | iucv_path_connected = 0; |
| 148 | } |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int smsg_pm_restore_thaw(struct device *dev) |
| 153 | { |
| 154 | int rc; |
| 155 | |
| 156 | #ifdef CONFIG_PM_DEBUG |
| 157 | printk(KERN_WARNING "smsg_pm_restore_thaw\n"); |
| 158 | #endif |
Hendrik Brueckner | 1c8161a | 2012-10-15 19:21:17 +0000 | [diff] [blame] | 159 | if (smsg_path && !iucv_path_connected) { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 160 | memset(smsg_path, 0, sizeof(*smsg_path)); |
| 161 | smsg_path->msglim = 255; |
| 162 | smsg_path->flags = 0; |
| 163 | rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ", |
| 164 | NULL, NULL, NULL); |
Martin Schwidefsky | 8ca4566 | 2009-10-29 15:04:08 +0100 | [diff] [blame] | 165 | #ifdef CONFIG_PM_DEBUG |
| 166 | if (rc) |
| 167 | printk(KERN_ERR |
| 168 | "iucv_path_connect returned with rc %i\n", rc); |
| 169 | #endif |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 170 | if (!rc) |
| 171 | iucv_path_connected = 1; |
Martin Schwidefsky | 8ca4566 | 2009-10-29 15:04:08 +0100 | [diff] [blame] | 172 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 177 | static const struct dev_pm_ops smsg_pm_ops = { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 178 | .freeze = smsg_pm_freeze, |
| 179 | .thaw = smsg_pm_restore_thaw, |
| 180 | .restore = smsg_pm_restore_thaw, |
| 181 | }; |
| 182 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 183 | static struct device_driver smsg_driver = { |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 184 | .owner = THIS_MODULE, |
Hendrik Brueckner | 1ffaa64 | 2010-03-08 12:26:26 +0100 | [diff] [blame] | 185 | .name = SMSGIUCV_DRV_NAME, |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 186 | .bus = &iucv_bus, |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 187 | .pm = &smsg_pm_ops, |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static void __exit smsg_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 192 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 193 | device_unregister(smsg_dev); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 194 | iucv_unregister(&smsg_handler, 1); |
| 195 | driver_unregister(&smsg_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 198 | static int __init smsg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | int rc; |
| 201 | |
Christian Borntraeger | 0fc3ddd | 2007-11-05 11:10:08 +0100 | [diff] [blame] | 202 | if (!MACHINE_IS_VM) { |
| 203 | rc = -EPROTONOSUPPORT; |
| 204 | goto out; |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | rc = driver_register(&smsg_driver); |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 207 | if (rc != 0) |
| 208 | goto out; |
| 209 | rc = iucv_register(&smsg_handler, 1); |
Martin Schwidefsky | d5ddc80 | 2008-07-14 09:59:33 +0200 | [diff] [blame] | 210 | if (rc) |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 211 | goto out_driver; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 212 | smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL); |
| 213 | if (!smsg_path) { |
| 214 | rc = -ENOMEM; |
| 215 | goto out_register; |
| 216 | } |
| 217 | rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ", |
| 218 | NULL, NULL, NULL); |
Martin Schwidefsky | d5ddc80 | 2008-07-14 09:59:33 +0200 | [diff] [blame] | 219 | if (rc) |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 220 | goto out_free_path; |
Ursula Braun | 59b60e9 | 2010-06-21 22:57:02 +0000 | [diff] [blame] | 221 | else |
| 222 | iucv_path_connected = 1; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 223 | smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 224 | if (!smsg_dev) { |
| 225 | rc = -ENOMEM; |
| 226 | goto out_free_path; |
| 227 | } |
| 228 | dev_set_name(smsg_dev, "smsg_iucv"); |
| 229 | smsg_dev->bus = &iucv_bus; |
| 230 | smsg_dev->parent = iucv_root; |
| 231 | smsg_dev->release = (void (*)(struct device *))kfree; |
| 232 | smsg_dev->driver = &smsg_driver; |
| 233 | rc = device_register(smsg_dev); |
| 234 | if (rc) |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 235 | goto out_put; |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 236 | |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 237 | cpcmd("SET SMSG IUCV", NULL, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return 0; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 239 | |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 240 | out_put: |
| 241 | put_device(smsg_dev); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 242 | out_free_path: |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 243 | iucv_path_free(smsg_path); |
Martin Schwidefsky | 6a1d96d | 2009-06-16 10:30:46 +0200 | [diff] [blame] | 244 | smsg_path = NULL; |
Martin Schwidefsky | 5da5e65 | 2007-02-08 13:51:11 -0800 | [diff] [blame] | 245 | out_register: |
| 246 | iucv_unregister(&smsg_handler, 1); |
| 247 | out_driver: |
| 248 | driver_unregister(&smsg_driver); |
| 249 | out: |
| 250 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | module_init(smsg_init); |
| 254 | module_exit(smsg_exit); |
| 255 | MODULE_LICENSE("GPL"); |
| 256 | |
| 257 | EXPORT_SYMBOL(smsg_register_callback); |
| 258 | EXPORT_SYMBOL(smsg_unregister_callback); |