blob: 0bfcbbe375c4b4640f50bf14e61ce1f6beae3062 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/vmalloc.h>
14#include <linux/slab.h>
15#include <linux/proc_fs.h>
Cornelia Huck678a3952006-01-06 00:19:24 -080016#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ctype.h>
18#include <linux/device.h>
19
20#include <asm/cio.h>
21#include <asm/uaccess.h>
22
23#include "blacklist.h"
24#include "cio.h"
25#include "cio_debug.h"
26#include "css.h"
27
28/*
29 * "Blacklisting" of certain devices:
30 * Device numbers given in the commandline as cio_ignore=... won't be known
31 * to Linux.
32 *
33 * These can be single devices or ranges of devices
34 */
35
Cornelia Huckfb6958a2006-01-06 00:19:25 -080036/* 65536 bits for each set to indicate if a devno is blacklisted or not */
Cornelia Hucka8237fc2006-01-06 00:19:21 -080037#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 (8*sizeof(long)))
Cornelia Huckfb6958a2006-01-06 00:19:25 -080039static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040typedef enum {add, free} range_action;
41
42/*
43 * Function: blacklist_range
44 * (Un-)blacklist the devices from-to
45 */
Michael Ernst5b890982008-05-07 09:22:55 +020046static int blacklist_range(range_action action, unsigned int from_ssid,
47 unsigned int to_ssid, unsigned int from,
48 unsigned int to, int msgtrigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Michael Ernst5b890982008-05-07 09:22:55 +020050 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
51 if (msgtrigger)
52 printk(KERN_WARNING "cio: Invalid cio_ignore range "
53 "0.%x.%04x-0.%x.%04x\n", from_ssid, from,
54 to_ssid, to);
55 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Michael Ernst5b890982008-05-07 09:22:55 +020057
58 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
59 (from <= to))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (action == add)
Michael Ernst5b890982008-05-07 09:22:55 +020061 set_bit(from, bl_dev[from_ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 else
Michael Ernst5b890982008-05-07 09:22:55 +020063 clear_bit(from, bl_dev[from_ssid]);
64 from++;
65 if (from > __MAX_SUBCHANNEL) {
66 from_ssid++;
67 from = 0;
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Michael Ernst5b890982008-05-07 09:22:55 +020074static int pure_hex(char **cp, unsigned int *val, int min_digit,
75 int max_digit, int max_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Michael Ernst5b890982008-05-07 09:22:55 +020077 int diff;
78 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Michael Ernst5b890982008-05-07 09:22:55 +020080 diff = 0;
81 *val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Michael Ernst5b890982008-05-07 09:22:55 +020083 while (isxdigit(**cp) && (diff <= max_digit)) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080084
Michael Ernst5b890982008-05-07 09:22:55 +020085 if (isdigit(**cp))
86 value = **cp - '0';
87 else
88 value = tolower(**cp) - 'a' + 10;
89 *val = *val * 16 + value;
90 (*cp)++;
91 diff++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Michael Ernst5b890982008-05-07 09:22:55 +020093
94 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
95 return 1;
96
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Cornelia Huck12829122008-06-10 10:03:19 +0200100static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
101 unsigned int *devno, int msgtrigger)
Michael Ernst5b890982008-05-07 09:22:55 +0200102{
103 char *str_work;
104 int val, rc, ret;
105
106 rc = 1;
107
108 if (*str == '\0')
109 goto out;
110
111 /* old style */
112 str_work = str;
113 val = simple_strtoul(str, &str_work, 16);
114
115 if (*str_work == '\0') {
116 if (val <= __MAX_SUBCHANNEL) {
117 *devno = val;
118 *ssid = 0;
119 *cssid = 0;
120 rc = 0;
121 }
122 goto out;
123 }
124
125 /* new style */
126 str_work = str;
127 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
128 if (ret || (str_work[0] != '.'))
129 goto out;
130 str_work++;
131 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
132 if (ret || (str_work[0] != '.'))
133 goto out;
134 str_work++;
135 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
136 if (ret || (str_work[0] != '\0'))
137 goto out;
138
139 rc = 0;
140out:
141 if (rc && msgtrigger)
142 printk(KERN_WARNING "cio: Invalid cio_ignore device '%s'\n",
143 str);
144
145 return rc;
146}
147
148static int blacklist_parse_parameters(char *str, range_action action,
149 int msgtrigger)
150{
Cornelia Huck12829122008-06-10 10:03:19 +0200151 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
Michael Ernst5b890982008-05-07 09:22:55 +0200152 int rc, totalrc;
153 char *parm;
154 range_action ra;
155
156 totalrc = 0;
157
158 while ((parm = strsep(&str, ","))) {
159 rc = 0;
160 ra = action;
161 if (*parm == '!') {
162 if (ra == add)
163 ra = free;
164 else
165 ra = add;
166 parm++;
167 }
168 if (strcmp(parm, "all") == 0) {
169 from_cssid = 0;
170 from_ssid = 0;
171 from = 0;
172 to_cssid = __MAX_CSSID;
173 to_ssid = __MAX_SSID;
174 to = __MAX_SUBCHANNEL;
175 } else {
176 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
177 &from_ssid, &from, msgtrigger);
178 if (!rc) {
179 if (parm != NULL)
180 rc = parse_busid(parm, &to_cssid,
181 &to_ssid, &to,
182 msgtrigger);
183 else {
184 to_cssid = from_cssid;
185 to_ssid = from_ssid;
186 to = from;
187 }
188 }
189 }
190 if (!rc) {
191 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
192 msgtrigger);
193 if (rc)
194 totalrc = 1;
195 } else
196 totalrc = 1;
197 }
198
199 return totalrc;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int __init
203blacklist_setup (char *str)
204{
Michael Ernst5b890982008-05-07 09:22:55 +0200205 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
206 if (blacklist_parse_parameters(str, add, 1))
207 return 0;
208 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211__setup ("cio_ignore=", blacklist_setup);
212
213/* Checking if devices are blacklisted */
214
215/*
216 * Function: is_blacklisted
217 * Returns 1 if the given devicenumber can be found in the blacklist,
218 * otherwise 0.
219 * Used by validate_subchannel()
220 */
221int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800222is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800224 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * Function: blacklist_parse_proc_parameters
230 * parse the stuff which is piped to /proc/cio_ignore
231 */
Michael Ernst5b890982008-05-07 09:22:55 +0200232static int blacklist_parse_proc_parameters(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Michael Ernst5b890982008-05-07 09:22:55 +0200234 int rc;
235 char *parm;
236
237 parm = strsep(&buf, " ");
238
239 if (strcmp("free", parm) == 0)
240 rc = blacklist_parse_parameters(buf, free, 0);
241 else if (strcmp("add", parm) == 0)
242 rc = blacklist_parse_parameters(buf, add, 0);
243 else
244 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200246 css_schedule_reprobe();
Michael Ernst5b890982008-05-07 09:22:55 +0200247
248 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Cornelia Huck678a3952006-01-06 00:19:24 -0800251/* Iterator struct for all devices. */
252struct ccwdev_iter {
253 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800254 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800255 int in_range;
256};
257
258static void *
259cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Cornelia Huck678a3952006-01-06 00:19:24 -0800261 struct ccwdev_iter *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800263 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800264 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800265 iter = kzalloc(sizeof(struct ccwdev_iter), GFP_KERNEL);
Cornelia Huck678a3952006-01-06 00:19:24 -0800266 if (!iter)
267 return ERR_PTR(-ENOMEM);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800268 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
269 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800270 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Cornelia Huck678a3952006-01-06 00:19:24 -0800273static void
274cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
275{
276 if (!IS_ERR(it))
277 kfree(it);
278}
279
280static void *
281cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
282{
283 struct ccwdev_iter *iter;
284
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800285 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800286 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800287 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800288 if (iter->devno == __MAX_SUBCHANNEL) {
289 iter->devno = 0;
290 iter->ssid++;
291 if (iter->ssid > __MAX_SSID)
292 return NULL;
293 } else
294 iter->devno++;
Cornelia Huck678a3952006-01-06 00:19:24 -0800295 (*offset)++;
296 return iter;
297}
298
299static int
300cio_ignore_proc_seq_show(struct seq_file *s, void *it)
301{
302 struct ccwdev_iter *iter;
303
Cornelia Huck3b793062006-01-06 00:19:26 -0800304 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800305 if (!is_blacklisted(iter->ssid, iter->devno))
Cornelia Huck678a3952006-01-06 00:19:24 -0800306 /* Not blacklisted, nothing to output. */
307 return 0;
308 if (!iter->in_range) {
309 /* First device in range. */
310 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800311 !is_blacklisted(iter->ssid, iter->devno + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800312 /* Singular device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800313 return seq_printf(s, "0.%x.%04x\n",
314 iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800315 iter->in_range = 1;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800316 return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800317 }
318 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800319 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800320 /* Last device in range. */
321 iter->in_range = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800322 return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800323 }
324 return 0;
325}
326
327static ssize_t
328cio_ignore_write(struct file *file, const char __user *user_buf,
329 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 char *buf;
Michael Ernst5b890982008-05-07 09:22:55 +0200332 size_t i;
333 ssize_t rc, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Cornelia Huck678a3952006-01-06 00:19:24 -0800335 if (*offset)
336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (user_len > 65536)
338 user_len = 65536;
339 buf = vmalloc (user_len + 1); /* maybe better use the stack? */
340 if (buf == NULL)
341 return -ENOMEM;
Michael Ernst5b890982008-05-07 09:22:55 +0200342 memset(buf, 0, user_len + 1);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200345 rc = -EFAULT;
346 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Michael Ernst5b890982008-05-07 09:22:55 +0200349 i = user_len - 1;
350 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
351 buf[i] = '\0';
352 i--;
353 }
354 ret = blacklist_parse_proc_parameters(buf);
355 if (ret)
356 rc = -EINVAL;
357 else
358 rc = user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Michael Ernst5b890982008-05-07 09:22:55 +0200360out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 vfree (buf);
Michael Ernst5b890982008-05-07 09:22:55 +0200362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100365static const struct seq_operations cio_ignore_proc_seq_ops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800366 .start = cio_ignore_proc_seq_start,
367 .stop = cio_ignore_proc_seq_stop,
368 .next = cio_ignore_proc_seq_next,
369 .show = cio_ignore_proc_seq_show,
370};
371
372static int
373cio_ignore_proc_open(struct inode *inode, struct file *file)
374{
375 return seq_open(file, &cio_ignore_proc_seq_ops);
376}
377
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800378static const struct file_operations cio_ignore_proc_fops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800379 .open = cio_ignore_proc_open,
380 .read = seq_read,
381 .llseek = seq_lseek,
382 .release = seq_release,
383 .write = cio_ignore_write,
384};
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static int
387cio_ignore_proc_init (void)
388{
389 struct proc_dir_entry *entry;
390
Denis V. Lunev8b594002008-04-29 01:02:20 -0700391 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
392 &cio_ignore_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700394 return -ENOENT;
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398__initcall (cio_ignore_proc_init);
399
400#endif /* CONFIG_PROC_FS */