blob: 3b0c8b8a7634d18df62ece8f94936ed39666a2af [file] [log] [blame]
Greg Kroah-Hartmanab9953f2017-11-14 18:38:04 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IUCV special message driver
4 *
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +02005 * Copyright IBM Corp. 2003, 2009
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080015#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/cpcmd.h>
17#include <asm/ebcdic.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080018#include "smsgiucv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20struct smsg_callback {
21 struct list_head list;
Hendrik Brueckner09003ed2010-03-08 12:26:25 +010022 const char *prefix;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 int len;
Hendrik Brueckner09003ed2010-03-08 12:26:25 +010024 void (*callback)(const char *from, char *str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27MODULE_AUTHOR
28 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
29MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
30
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080031static struct iucv_path *smsg_path;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +020032/* dummy device used as trigger for PM functions */
33static struct device *smsg_dev;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static DEFINE_SPINLOCK(smsg_list_lock);
Denis Chengc11ca972008-01-26 14:11:13 +010036static LIST_HEAD(smsg_list);
Ursula Braun59b60e92010-06-21 22:57:02 +000037static int iucv_path_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Ursula Braun91e60eb2015-09-18 16:06:52 +020039static int smsg_path_pending(struct iucv_path *, u8 *, u8 *);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080040static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
41
42static struct iucv_handler smsg_handler = {
43 .path_pending = smsg_path_pending,
44 .message_pending = smsg_message_pending,
45};
46
Ursula Braun91e60eb2015-09-18 16:06:52 +020047static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Stefan Weil08b01832011-02-02 06:04:36 +000049 if (strncmp(ipvmid, "*MSG ", 8) != 0)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080050 return -EINVAL;
51 /* Path pending from *MSG. */
52 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080055static void smsg_message_pending(struct iucv_path *path,
56 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct smsg_callback *cb;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080059 unsigned char *buffer;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070060 unsigned char sender[9];
Martin Schwidefsky15439d72005-05-01 08:58:58 -070061 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080063 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
64 if (!buffer) {
65 iucv_message_reject(path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return;
67 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080068 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (rc == 0) {
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080070 buffer[msg->length] = 0;
71 EBCASC(buffer, msg->length);
72 memcpy(sender, buffer, 8);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070073 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 Torvalds1da177e2005-04-16 15:20:36 -070080 spin_lock(&smsg_list_lock);
81 list_for_each_entry(cb, &smsg_list, list)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080082 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
83 cb->callback(sender, buffer + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 break;
85 }
86 spin_unlock(&smsg_list_lock);
87 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080088 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Hendrik Brueckner09003ed2010-03-08 12:26:25 +010091int smsg_register_callback(const char *prefix,
92 void (*callback)(const char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
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 Schwidefsky5da5e652007-02-08 13:51:11 -0800102 spin_lock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 list_add_tail(&cb->list, &smsg_list);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800104 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
Hendrik Brueckner09003ed2010-03-08 12:26:25 +0100108void smsg_unregister_callback(const char *prefix,
109 void (*callback)(const char *from,
110 char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct smsg_callback *cb, *tmp;
113
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800114 spin_lock_bh(&smsg_list_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200115 cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 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 Schwidefsky5da5e652007-02-08 13:51:11 -0800123 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 kfree(cb);
125}
126
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200127static int smsg_pm_freeze(struct device *dev)
128{
129#ifdef CONFIG_PM_DEBUG
130 printk(KERN_WARNING "smsg_pm_freeze\n");
131#endif
Ursula Braun59b60e92010-06-21 22:57:02 +0000132 if (smsg_path && iucv_path_connected) {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200133 iucv_path_sever(smsg_path, NULL);
Ursula Braun59b60e92010-06-21 22:57:02 +0000134 iucv_path_connected = 0;
135 }
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200136 return 0;
137}
138
139static 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 Brueckner1c8161a2012-10-15 19:21:17 +0000146 if (smsg_path && !iucv_path_connected) {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200147 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 Schwidefsky8ca45662009-10-29 15:04:08 +0100152#ifdef CONFIG_PM_DEBUG
153 if (rc)
154 printk(KERN_ERR
155 "iucv_path_connect returned with rc %i\n", rc);
156#endif
Ursula Braun59b60e92010-06-21 22:57:02 +0000157 if (!rc)
158 iucv_path_connected = 1;
Martin Schwidefsky8ca45662009-10-29 15:04:08 +0100159 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200160 }
161 return 0;
162}
163
Alexey Dobriyan47145212009-12-14 18:00:08 -0800164static const struct dev_pm_ops smsg_pm_ops = {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200165 .freeze = smsg_pm_freeze,
166 .thaw = smsg_pm_restore_thaw,
167 .restore = smsg_pm_restore_thaw,
168};
169
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800170static struct device_driver smsg_driver = {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200171 .owner = THIS_MODULE,
Hendrik Brueckner1ffaa642010-03-08 12:26:26 +0100172 .name = SMSGIUCV_DRV_NAME,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800173 .bus = &iucv_bus,
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200174 .pm = &smsg_pm_ops,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800175};
176
177static void __exit smsg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800179 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200180 device_unregister(smsg_dev);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800181 iucv_unregister(&smsg_handler, 1);
182 driver_unregister(&smsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800185static int __init smsg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int rc;
188
Christian Borntraeger0fc3ddd2007-11-05 11:10:08 +0100189 if (!MACHINE_IS_VM) {
190 rc = -EPROTONOSUPPORT;
191 goto out;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 rc = driver_register(&smsg_driver);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800194 if (rc != 0)
195 goto out;
196 rc = iucv_register(&smsg_handler, 1);
Martin Schwidefskyd5ddc802008-07-14 09:59:33 +0200197 if (rc)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800198 goto out_driver;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800199 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 Schwidefskyd5ddc802008-07-14 09:59:33 +0200206 if (rc)
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200207 goto out_free_path;
Ursula Braun59b60e92010-06-21 22:57:02 +0000208 else
209 iucv_path_connected = 1;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200210 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 Ottc6304932009-09-11 10:28:38 +0200222 goto out_put;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200223
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700224 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800226
Sebastian Ottc6304932009-09-11 10:28:38 +0200227out_put:
228 put_device(smsg_dev);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200229out_free_path:
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800230 iucv_path_free(smsg_path);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200231 smsg_path = NULL;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800232out_register:
233 iucv_unregister(&smsg_handler, 1);
234out_driver:
235 driver_unregister(&smsg_driver);
236out:
237 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240module_init(smsg_init);
241module_exit(smsg_exit);
242MODULE_LICENSE("GPL");
243
244EXPORT_SYMBOL(smsg_register_callback);
245EXPORT_SYMBOL(smsg_unregister_callback);