blob: d8f990b6b332c62f41c90b747347c71f56ef1709 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IUCV special message driver
3 *
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +02004 * Copyright IBM Corp. 2003, 2009
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080028#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/cpcmd.h>
30#include <asm/ebcdic.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080031#include "smsgiucv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33struct smsg_callback {
34 struct list_head list;
Hendrik Brueckner09003ed2010-03-08 12:26:25 +010035 const char *prefix;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 int len;
Hendrik Brueckner09003ed2010-03-08 12:26:25 +010037 void (*callback)(const char *from, char *str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40MODULE_AUTHOR
41 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
42MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
43
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080044static struct iucv_path *smsg_path;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +020045/* dummy device used as trigger for PM functions */
46static struct device *smsg_dev;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(smsg_list_lock);
Denis Chengc11ca972008-01-26 14:11:13 +010049static LIST_HEAD(smsg_list);
Ursula Braun59b60e92010-06-21 22:57:02 +000050static int iucv_path_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080052static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
53static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
54
55static struct iucv_handler smsg_handler = {
56 .path_pending = smsg_path_pending,
57 .message_pending = smsg_message_pending,
58};
59
60static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
61 u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Stefan Weil08b01832011-02-02 06:04:36 +000063 if (strncmp(ipvmid, "*MSG ", 8) != 0)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080064 return -EINVAL;
65 /* Path pending from *MSG. */
66 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080069static void smsg_message_pending(struct iucv_path *path,
70 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct smsg_callback *cb;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080073 unsigned char *buffer;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070074 unsigned char sender[9];
Martin Schwidefsky15439d72005-05-01 08:58:58 -070075 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080077 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
78 if (!buffer) {
79 iucv_message_reject(path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
81 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080082 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (rc == 0) {
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080084 buffer[msg->length] = 0;
85 EBCASC(buffer, msg->length);
86 memcpy(sender, buffer, 8);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070087 sender[8] = 0;
88 /* Remove trailing whitespace from the sender name. */
89 for (i = 7; i >= 0; i--) {
90 if (sender[i] != ' ' && sender[i] != '\t')
91 break;
92 sender[i] = 0;
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_lock(&smsg_list_lock);
95 list_for_each_entry(cb, &smsg_list, list)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080096 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
97 cb->callback(sender, buffer + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
99 }
100 spin_unlock(&smsg_list_lock);
101 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800102 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Hendrik Brueckner09003ed2010-03-08 12:26:25 +0100105int smsg_register_callback(const char *prefix,
106 void (*callback)(const char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct smsg_callback *cb;
109
110 cb = kmalloc(sizeof(struct smsg_callback), GFP_KERNEL);
111 if (!cb)
112 return -ENOMEM;
113 cb->prefix = prefix;
114 cb->len = strlen(prefix);
115 cb->callback = callback;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800116 spin_lock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 list_add_tail(&cb->list, &smsg_list);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800118 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 0;
120}
121
Hendrik Brueckner09003ed2010-03-08 12:26:25 +0100122void smsg_unregister_callback(const char *prefix,
123 void (*callback)(const char *from,
124 char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct smsg_callback *cb, *tmp;
127
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800128 spin_lock_bh(&smsg_list_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200129 cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 list_for_each_entry(tmp, &smsg_list, list)
131 if (tmp->callback == callback &&
132 strcmp(tmp->prefix, prefix) == 0) {
133 cb = tmp;
134 list_del(&cb->list);
135 break;
136 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800137 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree(cb);
139}
140
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200141static int smsg_pm_freeze(struct device *dev)
142{
143#ifdef CONFIG_PM_DEBUG
144 printk(KERN_WARNING "smsg_pm_freeze\n");
145#endif
Ursula Braun59b60e92010-06-21 22:57:02 +0000146 if (smsg_path && iucv_path_connected) {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200147 iucv_path_sever(smsg_path, NULL);
Ursula Braun59b60e92010-06-21 22:57:02 +0000148 iucv_path_connected = 0;
149 }
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200150 return 0;
151}
152
153static int smsg_pm_restore_thaw(struct device *dev)
154{
155 int rc;
156
157#ifdef CONFIG_PM_DEBUG
158 printk(KERN_WARNING "smsg_pm_restore_thaw\n");
159#endif
Hendrik Brueckner1c8161a2012-10-15 19:21:17 +0000160 if (smsg_path && !iucv_path_connected) {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200161 memset(smsg_path, 0, sizeof(*smsg_path));
162 smsg_path->msglim = 255;
163 smsg_path->flags = 0;
164 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
165 NULL, NULL, NULL);
Martin Schwidefsky8ca45662009-10-29 15:04:08 +0100166#ifdef CONFIG_PM_DEBUG
167 if (rc)
168 printk(KERN_ERR
169 "iucv_path_connect returned with rc %i\n", rc);
170#endif
Ursula Braun59b60e92010-06-21 22:57:02 +0000171 if (!rc)
172 iucv_path_connected = 1;
Martin Schwidefsky8ca45662009-10-29 15:04:08 +0100173 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200174 }
175 return 0;
176}
177
Alexey Dobriyan47145212009-12-14 18:00:08 -0800178static const struct dev_pm_ops smsg_pm_ops = {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200179 .freeze = smsg_pm_freeze,
180 .thaw = smsg_pm_restore_thaw,
181 .restore = smsg_pm_restore_thaw,
182};
183
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800184static struct device_driver smsg_driver = {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200185 .owner = THIS_MODULE,
Hendrik Brueckner1ffaa642010-03-08 12:26:26 +0100186 .name = SMSGIUCV_DRV_NAME,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800187 .bus = &iucv_bus,
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200188 .pm = &smsg_pm_ops,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800189};
190
191static void __exit smsg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800193 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200194 device_unregister(smsg_dev);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800195 iucv_unregister(&smsg_handler, 1);
196 driver_unregister(&smsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800199static int __init smsg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int rc;
202
Christian Borntraeger0fc3ddd2007-11-05 11:10:08 +0100203 if (!MACHINE_IS_VM) {
204 rc = -EPROTONOSUPPORT;
205 goto out;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 rc = driver_register(&smsg_driver);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800208 if (rc != 0)
209 goto out;
210 rc = iucv_register(&smsg_handler, 1);
Martin Schwidefskyd5ddc802008-07-14 09:59:33 +0200211 if (rc)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800212 goto out_driver;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800213 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
214 if (!smsg_path) {
215 rc = -ENOMEM;
216 goto out_register;
217 }
218 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
219 NULL, NULL, NULL);
Martin Schwidefskyd5ddc802008-07-14 09:59:33 +0200220 if (rc)
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200221 goto out_free_path;
Ursula Braun59b60e92010-06-21 22:57:02 +0000222 else
223 iucv_path_connected = 1;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200224 smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
225 if (!smsg_dev) {
226 rc = -ENOMEM;
227 goto out_free_path;
228 }
229 dev_set_name(smsg_dev, "smsg_iucv");
230 smsg_dev->bus = &iucv_bus;
231 smsg_dev->parent = iucv_root;
232 smsg_dev->release = (void (*)(struct device *))kfree;
233 smsg_dev->driver = &smsg_driver;
234 rc = device_register(smsg_dev);
235 if (rc)
Sebastian Ottc6304932009-09-11 10:28:38 +0200236 goto out_put;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200237
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700238 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800240
Sebastian Ottc6304932009-09-11 10:28:38 +0200241out_put:
242 put_device(smsg_dev);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200243out_free_path:
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800244 iucv_path_free(smsg_path);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200245 smsg_path = NULL;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800246out_register:
247 iucv_unregister(&smsg_handler, 1);
248out_driver:
249 driver_unregister(&smsg_driver);
250out:
251 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254module_init(smsg_init);
255module_exit(smsg_exit);
256MODULE_LICENSE("GPL");
257
258EXPORT_SYMBOL(smsg_register_callback);
259EXPORT_SYMBOL(smsg_unregister_callback);