Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / s390 / net / smsgiucv.c
CommitLineData
ab9953ff 1// SPDX-License-Identifier: GPL-2.0+
1da177e4
LT
2/*
3 * IUCV special message driver
4 *
6a1d96dc
MS
5 * Copyright IBM Corp. 2003, 2009
6 *
1da177e4 7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
1da177e4
LT
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/device.h>
5a0e3ad6 14#include <linux/slab.h>
5da5e658 15#include <net/iucv/iucv.h>
1da177e4
LT
16#include <asm/cpcmd.h>
17#include <asm/ebcdic.h>
5da5e658 18#include "smsgiucv.h"
1da177e4
LT
19
20struct smsg_callback {
21 struct list_head list;
09003ed9 22 const char *prefix;
1da177e4 23 int len;
09003ed9 24 void (*callback)(const char *from, char *str);
1da177e4
LT
25};
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
5da5e658 31static struct iucv_path *smsg_path;
6a1d96dc
MS
32/* dummy device used as trigger for PM functions */
33static struct device *smsg_dev;
5da5e658 34
1da177e4 35static DEFINE_SPINLOCK(smsg_list_lock);
c11ca97e 36static LIST_HEAD(smsg_list);
59b60e97 37static int iucv_path_connected;
1da177e4 38
91e60eb6 39static int smsg_path_pending(struct iucv_path *, u8 *, u8 *);
5da5e658
MS
40static 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
91e60eb6 47static int smsg_path_pending(struct iucv_path *path, u8 *ipvmid, u8 *ipuser)
1da177e4 48{
08b01832 49 if (strncmp(ipvmid, "*MSG ", 8) != 0)
5da5e658
MS
50 return -EINVAL;
51 /* Path pending from *MSG. */
52 return iucv_path_accept(path, &smsg_handler, "SMSGIUCV ", NULL);
1da177e4
LT
53}
54
5da5e658
MS
55static void smsg_message_pending(struct iucv_path *path,
56 struct iucv_message *msg)
1da177e4
LT
57{
58 struct smsg_callback *cb;
5da5e658 59 unsigned char *buffer;
15439d74 60 unsigned char sender[9];
15439d74 61 int rc, i;
1da177e4 62
5da5e658
MS
63 buffer = kmalloc(msg->length + 1, GFP_ATOMIC | GFP_DMA);
64 if (!buffer) {
65 iucv_message_reject(path, msg);
1da177e4
LT
66 return;
67 }
5da5e658 68 rc = iucv_message_receive(path, msg, 0, buffer, msg->length, NULL);
1da177e4 69 if (rc == 0) {
5da5e658
MS
70 buffer[msg->length] = 0;
71 EBCASC(buffer, msg->length);
72 memcpy(sender, buffer, 8);
15439d74
MS
73 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 }
1da177e4
LT
80 spin_lock(&smsg_list_lock);
81 list_for_each_entry(cb, &smsg_list, list)
5da5e658
MS
82 if (strncmp(buffer + 8, cb->prefix, cb->len) == 0) {
83 cb->callback(sender, buffer + 8);
1da177e4
LT
84 break;
85 }
86 spin_unlock(&smsg_list_lock);
87 }
5da5e658 88 kfree(buffer);
1da177e4
LT
89}
90
09003ed9
HB
91int smsg_register_callback(const char *prefix,
92 void (*callback)(const char *from, char *str))
1da177e4
LT
93{
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;
5da5e658 102 spin_lock_bh(&smsg_list_lock);
1da177e4 103 list_add_tail(&cb->list, &smsg_list);
5da5e658 104 spin_unlock_bh(&smsg_list_lock);
1da177e4
LT
105 return 0;
106}
107
09003ed9
HB
108void smsg_unregister_callback(const char *prefix,
109 void (*callback)(const char *from,
110 char *str))
1da177e4
LT
111{
112 struct smsg_callback *cb, *tmp;
113
5da5e658 114 spin_lock_bh(&smsg_list_lock);
d2c993d8 115 cb = NULL;
1da177e4
LT
116 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 }
5da5e658 123 spin_unlock_bh(&smsg_list_lock);
1da177e4
LT
124 kfree(cb);
125}
126
6a1d96dc
MS
127static int smsg_pm_freeze(struct device *dev)
128{
129#ifdef CONFIG_PM_DEBUG
130 printk(KERN_WARNING "smsg_pm_freeze\n");
131#endif
59b60e97 132 if (smsg_path && iucv_path_connected) {
6a1d96dc 133 iucv_path_sever(smsg_path, NULL);
59b60e97
UB
134 iucv_path_connected = 0;
135 }
6a1d96dc
MS
136 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
1c8161a8 146 if (smsg_path && !iucv_path_connected) {
6a1d96dc
MS
147 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);
8ca45667
MS
152#ifdef CONFIG_PM_DEBUG
153 if (rc)
154 printk(KERN_ERR
155 "iucv_path_connect returned with rc %i\n", rc);
156#endif
59b60e97
UB
157 if (!rc)
158 iucv_path_connected = 1;
8ca45667 159 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
6a1d96dc
MS
160 }
161 return 0;
162}
163
47145210 164static const struct dev_pm_ops smsg_pm_ops = {
6a1d96dc
MS
165 .freeze = smsg_pm_freeze,
166 .thaw = smsg_pm_restore_thaw,
167 .restore = smsg_pm_restore_thaw,
168};
169
5da5e658 170static struct device_driver smsg_driver = {
6a1d96dc 171 .owner = THIS_MODULE,
1ffaa640 172 .name = SMSGIUCV_DRV_NAME,
5da5e658 173 .bus = &iucv_bus,
6a1d96dc 174 .pm = &smsg_pm_ops,
5da5e658
MS
175};
176
177static void __exit smsg_exit(void)
1da177e4 178{
760dd0ee 179 cpcmd("SET SMSG OFF", NULL, 0, NULL);
6a1d96dc 180 device_unregister(smsg_dev);
5da5e658
MS
181 iucv_unregister(&smsg_handler, 1);
182 driver_unregister(&smsg_driver);
1da177e4
LT
183}
184
5da5e658 185static int __init smsg_init(void)
1da177e4 186{
1da177e4
LT
187 int rc;
188
0fc3ddd6
CB
189 if (!MACHINE_IS_VM) {
190 rc = -EPROTONOSUPPORT;
191 goto out;
192 }
1da177e4 193 rc = driver_register(&smsg_driver);
5da5e658
MS
194 if (rc != 0)
195 goto out;
196 rc = iucv_register(&smsg_handler, 1);
d5ddc809 197 if (rc)
5da5e658 198 goto out_driver;
5da5e658
MS
199 smsg_path = iucv_path_alloc(255, 0, GFP_KERNEL);
200 if (!smsg_path) {
201 rc = -ENOMEM;
202 goto out_register;
1da177e4 203 }
5da5e658
MS
204 rc = iucv_path_connect(smsg_path, &smsg_handler, "*MSG ",
205 NULL, NULL, NULL);
d5ddc809 206 if (rc)
6a1d96dc 207 goto out_free_path;
59b60e97
UB
208 else
209 iucv_path_connected = 1;
6a1d96dc
MS
210 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)
c6304933 222 goto out_put;
6a1d96dc 223
6b979de3 224 cpcmd("SET SMSG IUCV", NULL, 0, NULL);
1da177e4 225 return 0;
5da5e658 226
c6304933
SO
227out_put:
228 put_device(smsg_dev);
6a1d96dc 229out_free_path:
5da5e658 230 iucv_path_free(smsg_path);
6a1d96dc 231 smsg_path = NULL;
5da5e658
MS
232out_register:
233 iucv_unregister(&smsg_handler, 1);
234out_driver:
235 driver_unregister(&smsg_driver);
236out:
237 return rc;
1da177e4
LT
238}
239
240module_init(smsg_init);
241module_exit(smsg_exit);
242MODULE_LICENSE("GPL");
243
244EXPORT_SYMBOL(smsg_register_callback);
245EXPORT_SYMBOL(smsg_unregister_callback);