[S390] cio: ensure single load of irq handler pointer
[linux-2.6-block.git] / drivers / s390 / cio / blacklist.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/cio/blacklist.c
3 * S/390 common I/O routines -- blacklisting of specific devices
1da177e4
LT
4 *
5 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
4ce3b30c 8 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
9 * Arnd Bergmann (arndb@de.ibm.com)
10 */
11
e6d5a428
ME
12#define KMSG_COMPONENT "cio"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
1da177e4
LT
15#include <linux/init.h>
16#include <linux/vmalloc.h>
17#include <linux/slab.h>
18#include <linux/proc_fs.h>
678a395b 19#include <linux/seq_file.h>
1da177e4
LT
20#include <linux/ctype.h>
21#include <linux/device.h>
22
23#include <asm/cio.h>
24#include <asm/uaccess.h>
25
26#include "blacklist.h"
27#include "cio.h"
28#include "cio_debug.h"
29#include "css.h"
ecf5d9ef 30#include "device.h"
1da177e4
LT
31
32/*
33 * "Blacklisting" of certain devices:
34 * Device numbers given in the commandline as cio_ignore=... won't be known
35 * to Linux.
36 *
37 * These can be single devices or ranges of devices
38 */
39
fb6958a5 40/* 65536 bits for each set to indicate if a devno is blacklisted or not */
a8237fc4 41#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1da177e4 42 (8*sizeof(long)))
fb6958a5 43static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
1da177e4
LT
44typedef enum {add, free} range_action;
45
46/*
47 * Function: blacklist_range
48 * (Un-)blacklist the devices from-to
49 */
5b890987
ME
50static int blacklist_range(range_action action, unsigned int from_ssid,
51 unsigned int to_ssid, unsigned int from,
52 unsigned int to, int msgtrigger)
1da177e4 53{
5b890987
ME
54 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
55 if (msgtrigger)
e6d5a428
ME
56 pr_warning("0.%x.%04x to 0.%x.%04x is not a valid "
57 "range for cio_ignore\n", from_ssid, from,
58 to_ssid, to);
59
5b890987 60 return 1;
1da177e4 61 }
5b890987
ME
62
63 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
64 (from <= to))) {
1da177e4 65 if (action == add)
5b890987 66 set_bit(from, bl_dev[from_ssid]);
1da177e4 67 else
5b890987
ME
68 clear_bit(from, bl_dev[from_ssid]);
69 from++;
70 if (from > __MAX_SUBCHANNEL) {
71 from_ssid++;
72 from = 0;
73 }
1da177e4 74 }
5b890987
ME
75
76 return 0;
1da177e4
LT
77}
78
5b890987
ME
79static int pure_hex(char **cp, unsigned int *val, int min_digit,
80 int max_digit, int max_val)
1da177e4 81{
5b890987
ME
82 int diff;
83 unsigned int value;
1da177e4 84
5b890987
ME
85 diff = 0;
86 *val = 0;
1da177e4 87
5b890987
ME
88 while (isxdigit(**cp) && (diff <= max_digit)) {
89
90 if (isdigit(**cp))
91 value = **cp - '0';
92 else
93 value = tolower(**cp) - 'a' + 10;
94 *val = *val * 16 + value;
95 (*cp)++;
96 diff++;
1da177e4 97 }
5b890987
ME
98
99 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
100 return 1;
101
1da177e4 102 return 0;
1da177e4
LT
103}
104
12829126
CH
105static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
106 unsigned int *devno, int msgtrigger)
1da177e4 107{
5b890987
ME
108 char *str_work;
109 int val, rc, ret;
110
111 rc = 1;
112
113 if (*str == '\0')
114 goto out;
115
116 /* old style */
117 str_work = str;
118 val = simple_strtoul(str, &str_work, 16);
119
120 if (*str_work == '\0') {
121 if (val <= __MAX_SUBCHANNEL) {
122 *devno = val;
123 *ssid = 0;
124 *cssid = 0;
125 rc = 0;
1da177e4 126 }
5b890987
ME
127 goto out;
128 }
1da177e4 129
5b890987
ME
130 /* new style */
131 str_work = str;
132 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
133 if (ret || (str_work[0] != '.'))
134 goto out;
135 str_work++;
136 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
137 if (ret || (str_work[0] != '.'))
138 goto out;
139 str_work++;
140 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
141 if (ret || (str_work[0] != '\0'))
142 goto out;
143
144 rc = 0;
145out:
146 if (rc && msgtrigger)
e6d5a428
ME
147 pr_warning("%s is not a valid device for the cio_ignore "
148 "kernel parameter\n", str);
5b890987
ME
149
150 return rc;
151}
1da177e4 152
5b890987
ME
153static int blacklist_parse_parameters(char *str, range_action action,
154 int msgtrigger)
155{
12829126 156 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
5b890987
ME
157 int rc, totalrc;
158 char *parm;
159 range_action ra;
160
161 totalrc = 0;
162
163 while ((parm = strsep(&str, ","))) {
164 rc = 0;
165 ra = action;
166 if (*parm == '!') {
167 if (ra == add)
168 ra = free;
169 else
170 ra = add;
171 parm++;
172 }
173 if (strcmp(parm, "all") == 0) {
174 from_cssid = 0;
175 from_ssid = 0;
176 from = 0;
177 to_cssid = __MAX_CSSID;
178 to_ssid = __MAX_SSID;
179 to = __MAX_SUBCHANNEL;
180 } else {
181 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
182 &from_ssid, &from, msgtrigger);
183 if (!rc) {
184 if (parm != NULL)
185 rc = parse_busid(parm, &to_cssid,
186 &to_ssid, &to,
187 msgtrigger);
188 else {
189 to_cssid = from_cssid;
190 to_ssid = from_ssid;
191 to = from;
192 }
1da177e4
LT
193 }
194 }
5b890987
ME
195 if (!rc) {
196 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
197 msgtrigger);
198 if (rc)
ecf5d9ef 199 totalrc = -EINVAL;
5b890987 200 } else
ecf5d9ef 201 totalrc = -EINVAL;
1da177e4 202 }
5b890987
ME
203
204 return totalrc;
1da177e4
LT
205}
206
1da177e4
LT
207static int __init
208blacklist_setup (char *str)
209{
5b890987
ME
210 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
211 if (blacklist_parse_parameters(str, add, 1))
212 return 0;
213 return 1;
1da177e4
LT
214}
215
216__setup ("cio_ignore=", blacklist_setup);
217
218/* Checking if devices are blacklisted */
219
220/*
221 * Function: is_blacklisted
222 * Returns 1 if the given devicenumber can be found in the blacklist,
223 * otherwise 0.
224 * Used by validate_subchannel()
225 */
226int
fb6958a5 227is_blacklisted (int ssid, int devno)
1da177e4 228{
fb6958a5 229 return test_bit (devno, bl_dev[ssid]);
1da177e4
LT
230}
231
232#ifdef CONFIG_PROC_FS
1da177e4
LT
233/*
234 * Function: blacklist_parse_proc_parameters
235 * parse the stuff which is piped to /proc/cio_ignore
236 */
5b890987 237static int blacklist_parse_proc_parameters(char *buf)
1da177e4 238{
5b890987
ME
239 int rc;
240 char *parm;
241
242 parm = strsep(&buf, " ");
243
244 if (strcmp("free", parm) == 0)
245 rc = blacklist_parse_parameters(buf, free, 0);
246 else if (strcmp("add", parm) == 0)
247 rc = blacklist_parse_parameters(buf, add, 0);
ecf5d9ef
PO
248 else if (strcmp("purge", parm) == 0)
249 return ccw_purge_blacklisted();
5b890987 250 else
ecf5d9ef 251 return -EINVAL;
1da177e4 252
40154b82 253 css_schedule_reprobe();
5b890987
ME
254
255 return rc;
1da177e4
LT
256}
257
678a395b
CH
258/* Iterator struct for all devices. */
259struct ccwdev_iter {
260 int devno;
fb6958a5 261 int ssid;
678a395b
CH
262 int in_range;
263};
264
265static void *
266cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
1da177e4 267{
678a395b
CH
268 struct ccwdev_iter *iter;
269
fb6958a5 270 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
678a395b 271 return NULL;
3b793060 272 iter = kzalloc(sizeof(struct ccwdev_iter), GFP_KERNEL);
678a395b
CH
273 if (!iter)
274 return ERR_PTR(-ENOMEM);
fb6958a5
CH
275 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
276 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
678a395b
CH
277 return iter;
278}
279
280static void
281cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
282{
283 if (!IS_ERR(it))
284 kfree(it);
285}
1da177e4 286
678a395b
CH
287static void *
288cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
289{
290 struct ccwdev_iter *iter;
291
fb6958a5 292 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
678a395b 293 return NULL;
3b793060 294 iter = it;
fb6958a5
CH
295 if (iter->devno == __MAX_SUBCHANNEL) {
296 iter->devno = 0;
297 iter->ssid++;
298 if (iter->ssid > __MAX_SSID)
299 return NULL;
300 } else
301 iter->devno++;
678a395b
CH
302 (*offset)++;
303 return iter;
1da177e4
LT
304}
305
678a395b
CH
306static int
307cio_ignore_proc_seq_show(struct seq_file *s, void *it)
308{
309 struct ccwdev_iter *iter;
310
3b793060 311 iter = it;
fb6958a5 312 if (!is_blacklisted(iter->ssid, iter->devno))
678a395b
CH
313 /* Not blacklisted, nothing to output. */
314 return 0;
315 if (!iter->in_range) {
316 /* First device in range. */
317 if ((iter->devno == __MAX_SUBCHANNEL) ||
fb6958a5 318 !is_blacklisted(iter->ssid, iter->devno + 1))
678a395b 319 /* Singular device. */
fb6958a5
CH
320 return seq_printf(s, "0.%x.%04x\n",
321 iter->ssid, iter->devno);
678a395b 322 iter->in_range = 1;
fb6958a5 323 return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
678a395b
CH
324 }
325 if ((iter->devno == __MAX_SUBCHANNEL) ||
fb6958a5 326 !is_blacklisted(iter->ssid, iter->devno + 1)) {
678a395b
CH
327 /* Last device in range. */
328 iter->in_range = 0;
fb6958a5 329 return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
678a395b
CH
330 }
331 return 0;
332}
333
334static ssize_t
335cio_ignore_write(struct file *file, const char __user *user_buf,
336 size_t user_len, loff_t *offset)
1da177e4
LT
337{
338 char *buf;
5b890987
ME
339 size_t i;
340 ssize_t rc, ret;
1da177e4 341
678a395b
CH
342 if (*offset)
343 return -EINVAL;
1da177e4
LT
344 if (user_len > 65536)
345 user_len = 65536;
346 buf = vmalloc (user_len + 1); /* maybe better use the stack? */
347 if (buf == NULL)
348 return -ENOMEM;
5b890987
ME
349 memset(buf, 0, user_len + 1);
350
1da177e4 351 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
5b890987
ME
352 rc = -EFAULT;
353 goto out_free;
1da177e4 354 }
1da177e4 355
5b890987
ME
356 i = user_len - 1;
357 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
358 buf[i] = '\0';
359 i--;
360 }
361 ret = blacklist_parse_proc_parameters(buf);
362 if (ret)
ecf5d9ef 363 rc = ret;
5b890987
ME
364 else
365 rc = user_len;
1da177e4 366
5b890987 367out_free:
1da177e4 368 vfree (buf);
5b890987 369 return rc;
1da177e4
LT
370}
371
5c81cdbe 372static const struct seq_operations cio_ignore_proc_seq_ops = {
678a395b
CH
373 .start = cio_ignore_proc_seq_start,
374 .stop = cio_ignore_proc_seq_stop,
375 .next = cio_ignore_proc_seq_next,
376 .show = cio_ignore_proc_seq_show,
377};
378
379static int
380cio_ignore_proc_open(struct inode *inode, struct file *file)
381{
382 return seq_open(file, &cio_ignore_proc_seq_ops);
383}
384
d54b1fdb 385static const struct file_operations cio_ignore_proc_fops = {
678a395b
CH
386 .open = cio_ignore_proc_open,
387 .read = seq_read,
388 .llseek = seq_lseek,
389 .release = seq_release,
390 .write = cio_ignore_write,
391};
392
1da177e4
LT
393static int
394cio_ignore_proc_init (void)
395{
396 struct proc_dir_entry *entry;
397
8b594007
DL
398 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
399 &cio_ignore_proc_fops);
1da177e4 400 if (!entry)
a7fbf6bb 401 return -ENOENT;
a7fbf6bb 402 return 0;
1da177e4
LT
403}
404
405__initcall (cio_ignore_proc_init);
406
407#endif /* CONFIG_PROC_FS */