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