blob: 3012355f8304e875953a732d2ab4b4e6dde1681f [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>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080027#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/cpcmd.h>
29#include <asm/ebcdic.h>
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080030#include "smsgiucv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32struct smsg_callback {
33 struct list_head list;
34 char *prefix;
35 int len;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070036 void (*callback)(char *from, char *str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39MODULE_AUTHOR
40 ("(C) 2003 IBM Corporation by Martin Schwidefsky (schwidefsky@de.ibm.com)");
41MODULE_DESCRIPTION ("Linux for S/390 IUCV special message driver");
42
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080043static struct iucv_path *smsg_path;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +020044/* dummy device used as trigger for PM functions */
45static struct device *smsg_dev;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_SPINLOCK(smsg_list_lock);
Denis Chengc11ca972008-01-26 14:11:13 +010048static LIST_HEAD(smsg_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080050static int smsg_path_pending(struct iucv_path *, u8 ipvmid[8], u8 ipuser[16]);
51static void smsg_message_pending(struct iucv_path *, struct iucv_message *);
52
53static struct iucv_handler smsg_handler = {
54 .path_pending = smsg_path_pending,
55 .message_pending = smsg_message_pending,
56};
57
58static int smsg_path_pending(struct iucv_path *path, u8 ipvmid[8],
59 u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080061 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 Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080067static void smsg_message_pending(struct iucv_path *path,
68 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct smsg_callback *cb;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080071 unsigned char *buffer;
Martin Schwidefsky15439d72005-05-01 08:58:58 -070072 unsigned char sender[9];
Martin Schwidefsky15439d72005-05-01 08:58:58 -070073 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080075 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
76 if (!buffer) {
77 iucv_message_reject(path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return;
79 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080080 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (rc == 0) {
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080082 buffer[msg->length] = 0;
83 EBCASC(buffer, msg->length);
84 memcpy(sender, buffer, 8);
Martin Schwidefsky15439d72005-05-01 08:58:58 -070085 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 Torvalds1da177e2005-04-16 15:20:36 -070092 spin_lock(&smsg_list_lock);
93 list_for_each_entry(cb, &smsg_list, list)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -080094 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
95 cb->callback(sender, buffer + 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 }
98 spin_unlock(&smsg_list_lock);
99 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800100 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800103int smsg_register_callback(char *prefix,
104 void (*callback)(char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
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 Schwidefsky5da5e652007-02-08 13:51:11 -0800114 spin_lock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 list_add_tail(&cb->list, &smsg_list);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800116 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800120void smsg_unregister_callback(char *prefix,
121 void (*callback)(char *from, char *str))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct smsg_callback *cb, *tmp;
124
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800125 spin_lock_bh(&smsg_list_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200126 cb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 list_for_each_entry(tmp, &smsg_list, list)
128 if (tmp->callback == callback &&
129 strcmp(tmp->prefix, prefix) == 0) {
130 cb = tmp;
131 list_del(&cb->list);
132 break;
133 }
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800134 spin_unlock_bh(&smsg_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 kfree(cb);
136}
137
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200138static int smsg_pm_freeze(struct device *dev)
139{
140#ifdef CONFIG_PM_DEBUG
141 printk(KERN_WARNING "smsg_pm_freeze\n");
142#endif
143 if (smsg_path)
144 iucv_path_sever(smsg_path, NULL);
145 return 0;
146}
147
148static int smsg_pm_restore_thaw(struct device *dev)
149{
150 int rc;
151
152#ifdef CONFIG_PM_DEBUG
153 printk(KERN_WARNING "smsg_pm_restore_thaw\n");
154#endif
155 if (smsg_path) {
156 memset(smsg_path, 0, sizeof(*smsg_path));
157 smsg_path->msglim = 255;
158 smsg_path->flags = 0;
159 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
160 NULL, NULL, NULL);
Martin Schwidefsky8ca45662009-10-29 15:04:08 +0100161#ifdef CONFIG_PM_DEBUG
162 if (rc)
163 printk(KERN_ERR
164 "iucv_path_connect returned with rc %i\n", rc);
165#endif
166 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200167 }
168 return 0;
169}
170
171static struct dev_pm_ops smsg_pm_ops = {
172 .freeze = smsg_pm_freeze,
173 .thaw = smsg_pm_restore_thaw,
174 .restore = smsg_pm_restore_thaw,
175};
176
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800177static struct device_driver smsg_driver = {
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200178 .owner = THIS_MODULE,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800179 .name = "SMSGIUCV",
180 .bus = &iucv_bus,
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200181 .pm = &smsg_pm_ops,
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800182};
183
184static void __exit smsg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800186 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200187 device_unregister(smsg_dev);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800188 iucv_unregister(&smsg_handler, 1);
189 driver_unregister(&smsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800192static int __init smsg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int rc;
195
Christian Borntraeger0fc3ddd2007-11-05 11:10:08 +0100196 if (!MACHINE_IS_VM) {
197 rc = -EPROTONOSUPPORT;
198 goto out;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 rc = driver_register(&smsg_driver);
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800201 if (rc != 0)
202 goto out;
203 rc = iucv_register(&smsg_handler, 1);
Martin Schwidefskyd5ddc802008-07-14 09:59:33 +0200204 if (rc)
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800205 goto out_driver;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800206 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
207 if (!smsg_path) {
208 rc = -ENOMEM;
209 goto out_register;
210 }
211 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
212 NULL, NULL, NULL);
Martin Schwidefskyd5ddc802008-07-14 09:59:33 +0200213 if (rc)
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200214 goto out_free_path;
215 smsg_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
216 if (!smsg_dev) {
217 rc = -ENOMEM;
218 goto out_free_path;
219 }
220 dev_set_name(smsg_dev, "smsg_iucv");
221 smsg_dev->bus = &iucv_bus;
222 smsg_dev->parent = iucv_root;
223 smsg_dev->release = (void (*)(struct device *))kfree;
224 smsg_dev->driver = &smsg_driver;
225 rc = device_register(smsg_dev);
226 if (rc)
Sebastian Ottc6304932009-09-11 10:28:38 +0200227 goto out_put;
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200228
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700229 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800231
Sebastian Ottc6304932009-09-11 10:28:38 +0200232out_put:
233 put_device(smsg_dev);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200234out_free_path:
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800235 iucv_path_free(smsg_path);
Martin Schwidefsky6a1d96d2009-06-16 10:30:46 +0200236 smsg_path = NULL;
Martin Schwidefsky5da5e652007-02-08 13:51:11 -0800237out_register:
238 iucv_unregister(&smsg_handler, 1);
239out_driver:
240 driver_unregister(&smsg_driver);
241out:
242 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245module_init(smsg_init);
246module_exit(smsg_exit);
247MODULE_LICENSE("GPL");
248
249EXPORT_SYMBOL(smsg_register_callback);
250EXPORT_SYMBOL(smsg_unregister_callback);