blob: 7eab9ab9f40650902c1405a98551e43362c9c21b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/blacklist.c
3 * S/390 common I/O routines -- blacklisting of specific devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Arnd Bergmann (arndb@de.ibm.com)
10 */
11
Michael Ernste6d5a422008-12-25 13:39:36 +010012#define KMSG_COMPONENT "cio"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/vmalloc.h>
17#include <linux/slab.h>
18#include <linux/proc_fs.h>
Cornelia Huck678a3952006-01-06 00:19:24 -080019#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020030#include "device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
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
Cornelia Huckfb6958a2006-01-06 00:19:25 -080040/* 65536 bits for each set to indicate if a devno is blacklisted or not */
Cornelia Hucka8237fc2006-01-06 00:19:21 -080041#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 (8*sizeof(long)))
Cornelia Huckfb6958a2006-01-06 00:19:25 -080043static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044typedef enum {add, free} range_action;
45
46/*
47 * Function: blacklist_range
48 * (Un-)blacklist the devices from-to
49 */
Michael Ernst5b890982008-05-07 09:22:55 +020050static int blacklist_range(range_action action, unsigned int from_ssid,
51 unsigned int to_ssid, unsigned int from,
52 unsigned int to, int msgtrigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Michael Ernst5b890982008-05-07 09:22:55 +020054 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
55 if (msgtrigger)
Michael Ernste6d5a422008-12-25 13:39:36 +010056 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
Michael Ernst5b890982008-05-07 09:22:55 +020060 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
Michael Ernst5b890982008-05-07 09:22:55 +020062
63 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
64 (from <= to))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (action == add)
Michael Ernst5b890982008-05-07 09:22:55 +020066 set_bit(from, bl_dev[from_ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 else
Michael Ernst5b890982008-05-07 09:22:55 +020068 clear_bit(from, bl_dev[from_ssid]);
69 from++;
70 if (from > __MAX_SUBCHANNEL) {
71 from_ssid++;
72 from = 0;
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Michael Ernst5b890982008-05-07 09:22:55 +020079static int pure_hex(char **cp, unsigned int *val, int min_digit,
80 int max_digit, int max_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Michael Ernst5b890982008-05-07 09:22:55 +020082 int diff;
83 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Michael Ernst5b890982008-05-07 09:22:55 +020085 diff = 0;
86 *val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Michael Ernst5b890982008-05-07 09:22:55 +020088 while (isxdigit(**cp) && (diff <= max_digit)) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080089
Michael Ernst5b890982008-05-07 09:22:55 +020090 if (isdigit(**cp))
91 value = **cp - '0';
92 else
93 value = tolower(**cp) - 'a' + 10;
94 *val = *val * 16 + value;
95 (*cp)++;
96 diff++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Michael Ernst5b890982008-05-07 09:22:55 +020098
99 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
100 return 1;
101
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Cornelia Huck12829122008-06-10 10:03:19 +0200105static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
106 unsigned int *devno, int msgtrigger)
Michael Ernst5b890982008-05-07 09:22:55 +0200107{
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;
126 }
127 goto out;
128 }
129
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)
Michael Ernste6d5a422008-12-25 13:39:36 +0100147 pr_warning("%s is not a valid device for the cio_ignore "
148 "kernel parameter\n", str);
Michael Ernst5b890982008-05-07 09:22:55 +0200149
150 return rc;
151}
152
153static int blacklist_parse_parameters(char *str, range_action action,
154 int msgtrigger)
155{
Cornelia Huck12829122008-06-10 10:03:19 +0200156 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
Michael Ernst5b890982008-05-07 09:22:55 +0200157 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 }
193 }
194 }
195 if (!rc) {
196 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
197 msgtrigger);
198 if (rc)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200199 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200200 } else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200201 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200202 }
203
204 return totalrc;
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static int __init
208blacklist_setup (char *str)
209{
Michael Ernst5b890982008-05-07 09:22:55 +0200210 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
211 if (blacklist_parse_parameters(str, add, 1))
212 return 0;
213 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
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
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800227is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800229 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * Function: blacklist_parse_proc_parameters
235 * parse the stuff which is piped to /proc/cio_ignore
236 */
Michael Ernst5b890982008-05-07 09:22:55 +0200237static int blacklist_parse_proc_parameters(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Michael Ernst5b890982008-05-07 09:22:55 +0200239 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);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200248 else if (strcmp("purge", parm) == 0)
249 return ccw_purge_blacklisted();
Michael Ernst5b890982008-05-07 09:22:55 +0200250 else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200253 css_schedule_reprobe();
Michael Ernst5b890982008-05-07 09:22:55 +0200254
255 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Cornelia Huck678a3952006-01-06 00:19:24 -0800258/* Iterator struct for all devices. */
259struct ccwdev_iter {
260 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800261 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800262 int in_range;
263};
264
265static void *
266cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200268 struct ccwdev_iter *iter = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800270 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800271 return NULL;
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200272 memset(iter, 0, sizeof(*iter));
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800273 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
274 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800275 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Cornelia Huck678a3952006-01-06 00:19:24 -0800278static void
279cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
280{
Cornelia Huck678a3952006-01-06 00:19:24 -0800281}
282
283static void *
284cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
285{
286 struct ccwdev_iter *iter;
287
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800288 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800289 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800290 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800291 if (iter->devno == __MAX_SUBCHANNEL) {
292 iter->devno = 0;
293 iter->ssid++;
294 if (iter->ssid > __MAX_SSID)
295 return NULL;
296 } else
297 iter->devno++;
Cornelia Huck678a3952006-01-06 00:19:24 -0800298 (*offset)++;
299 return iter;
300}
301
302static int
303cio_ignore_proc_seq_show(struct seq_file *s, void *it)
304{
305 struct ccwdev_iter *iter;
306
Cornelia Huck3b793062006-01-06 00:19:26 -0800307 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800308 if (!is_blacklisted(iter->ssid, iter->devno))
Cornelia Huck678a3952006-01-06 00:19:24 -0800309 /* Not blacklisted, nothing to output. */
310 return 0;
311 if (!iter->in_range) {
312 /* First device in range. */
313 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800314 !is_blacklisted(iter->ssid, iter->devno + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800315 /* Singular device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800316 return seq_printf(s, "0.%x.%04x\n",
317 iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800318 iter->in_range = 1;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800319 return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800320 }
321 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800322 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800323 /* Last device in range. */
324 iter->in_range = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800325 return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800326 }
327 return 0;
328}
329
330static ssize_t
331cio_ignore_write(struct file *file, const char __user *user_buf,
332 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 char *buf;
Sebastian Ott94cbc202009-03-26 15:24:16 +0100335 ssize_t rc, ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Cornelia Huck678a3952006-01-06 00:19:24 -0800337 if (*offset)
338 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (user_len > 65536)
340 user_len = 65536;
341 buf = vmalloc (user_len + 1); /* maybe better use the stack? */
342 if (buf == NULL)
343 return -ENOMEM;
Michael Ernst5b890982008-05-07 09:22:55 +0200344 memset(buf, 0, user_len + 1);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200347 rc = -EFAULT;
348 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Michael Ernst5b890982008-05-07 09:22:55 +0200351 i = user_len - 1;
352 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
353 buf[i] = '\0';
354 i--;
355 }
356 ret = blacklist_parse_proc_parameters(buf);
357 if (ret)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200358 rc = ret;
Michael Ernst5b890982008-05-07 09:22:55 +0200359 else
360 rc = user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Michael Ernst5b890982008-05-07 09:22:55 +0200362out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 vfree (buf);
Michael Ernst5b890982008-05-07 09:22:55 +0200364 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100367static const struct seq_operations cio_ignore_proc_seq_ops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800368 .start = cio_ignore_proc_seq_start,
369 .stop = cio_ignore_proc_seq_stop,
370 .next = cio_ignore_proc_seq_next,
371 .show = cio_ignore_proc_seq_show,
372};
373
374static int
375cio_ignore_proc_open(struct inode *inode, struct file *file)
376{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200377 return seq_open_private(file, &cio_ignore_proc_seq_ops,
378 sizeof(struct ccwdev_iter));
Cornelia Huck678a3952006-01-06 00:19:24 -0800379}
380
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800381static const struct file_operations cio_ignore_proc_fops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800382 .open = cio_ignore_proc_open,
383 .read = seq_read,
384 .llseek = seq_lseek,
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200385 .release = seq_release_private,
Cornelia Huck678a3952006-01-06 00:19:24 -0800386 .write = cio_ignore_write,
387};
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int
390cio_ignore_proc_init (void)
391{
392 struct proc_dir_entry *entry;
393
Denis V. Lunev8b594002008-04-29 01:02:20 -0700394 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
395 &cio_ignore_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700397 return -ENOENT;
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401__initcall (cio_ignore_proc_init);
402
403#endif /* CONFIG_PROC_FS */