blob: 13cb60162e429675a2f0233e903f48ccea9d89ed [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/proc_fs.h>
Cornelia Huck678a3952006-01-06 00:19:24 -080018#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ctype.h>
20#include <linux/device.h>
21
22#include <asm/cio.h>
23#include <asm/uaccess.h>
24
25#include "blacklist.h"
26#include "cio.h"
27#include "cio_debug.h"
28#include "css.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020029#include "device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * "Blacklisting" of certain devices:
33 * Device numbers given in the commandline as cio_ignore=... won't be known
34 * to Linux.
35 *
36 * These can be single devices or ranges of devices
37 */
38
Cornelia Huckfb6958a2006-01-06 00:19:25 -080039/* 65536 bits for each set to indicate if a devno is blacklisted or not */
Cornelia Hucka8237fc2006-01-06 00:19:21 -080040#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 (8*sizeof(long)))
Cornelia Huckfb6958a2006-01-06 00:19:25 -080042static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043typedef enum {add, free} range_action;
44
45/*
46 * Function: blacklist_range
47 * (Un-)blacklist the devices from-to
48 */
Michael Ernst5b890982008-05-07 09:22:55 +020049static int blacklist_range(range_action action, unsigned int from_ssid,
50 unsigned int to_ssid, unsigned int from,
51 unsigned int to, int msgtrigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Michael Ernst5b890982008-05-07 09:22:55 +020053 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
54 if (msgtrigger)
Michael Ernste6d5a422008-12-25 13:39:36 +010055 pr_warning("0.%x.%04x to 0.%x.%04x is not a valid "
56 "range for cio_ignore\n", from_ssid, from,
57 to_ssid, to);
58
Michael Ernst5b890982008-05-07 09:22:55 +020059 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
Michael Ernst5b890982008-05-07 09:22:55 +020061
62 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
63 (from <= to))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (action == add)
Michael Ernst5b890982008-05-07 09:22:55 +020065 set_bit(from, bl_dev[from_ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 else
Michael Ernst5b890982008-05-07 09:22:55 +020067 clear_bit(from, bl_dev[from_ssid]);
68 from++;
69 if (from > __MAX_SUBCHANNEL) {
70 from_ssid++;
71 from = 0;
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Michael Ernst5b890982008-05-07 09:22:55 +020078static int pure_hex(char **cp, unsigned int *val, int min_digit,
79 int max_digit, int max_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Michael Ernst5b890982008-05-07 09:22:55 +020081 int diff;
82 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Michael Ernst5b890982008-05-07 09:22:55 +020084 diff = 0;
85 *val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Michael Ernst5b890982008-05-07 09:22:55 +020087 while (isxdigit(**cp) && (diff <= max_digit)) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080088
Michael Ernst5b890982008-05-07 09:22:55 +020089 if (isdigit(**cp))
90 value = **cp - '0';
91 else
92 value = tolower(**cp) - 'a' + 10;
93 *val = *val * 16 + value;
94 (*cp)++;
95 diff++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Michael Ernst5b890982008-05-07 09:22:55 +020097
98 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
99 return 1;
100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Cornelia Huck12829122008-06-10 10:03:19 +0200104static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
105 unsigned int *devno, int msgtrigger)
Michael Ernst5b890982008-05-07 09:22:55 +0200106{
107 char *str_work;
108 int val, rc, ret;
109
110 rc = 1;
111
112 if (*str == '\0')
113 goto out;
114
115 /* old style */
116 str_work = str;
117 val = simple_strtoul(str, &str_work, 16);
118
119 if (*str_work == '\0') {
120 if (val <= __MAX_SUBCHANNEL) {
121 *devno = val;
122 *ssid = 0;
123 *cssid = 0;
124 rc = 0;
125 }
126 goto out;
127 }
128
129 /* new style */
130 str_work = str;
131 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
132 if (ret || (str_work[0] != '.'))
133 goto out;
134 str_work++;
135 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
136 if (ret || (str_work[0] != '.'))
137 goto out;
138 str_work++;
139 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
140 if (ret || (str_work[0] != '\0'))
141 goto out;
142
143 rc = 0;
144out:
145 if (rc && msgtrigger)
Michael Ernste6d5a422008-12-25 13:39:36 +0100146 pr_warning("%s is not a valid device for the cio_ignore "
147 "kernel parameter\n", str);
Michael Ernst5b890982008-05-07 09:22:55 +0200148
149 return rc;
150}
151
152static int blacklist_parse_parameters(char *str, range_action action,
153 int msgtrigger)
154{
Cornelia Huck12829122008-06-10 10:03:19 +0200155 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
Michael Ernst5b890982008-05-07 09:22:55 +0200156 int rc, totalrc;
157 char *parm;
158 range_action ra;
159
160 totalrc = 0;
161
162 while ((parm = strsep(&str, ","))) {
163 rc = 0;
164 ra = action;
165 if (*parm == '!') {
166 if (ra == add)
167 ra = free;
168 else
169 ra = add;
170 parm++;
171 }
172 if (strcmp(parm, "all") == 0) {
173 from_cssid = 0;
174 from_ssid = 0;
175 from = 0;
176 to_cssid = __MAX_CSSID;
177 to_ssid = __MAX_SSID;
178 to = __MAX_SUBCHANNEL;
179 } else {
180 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
181 &from_ssid, &from, msgtrigger);
182 if (!rc) {
183 if (parm != NULL)
184 rc = parse_busid(parm, &to_cssid,
185 &to_ssid, &to,
186 msgtrigger);
187 else {
188 to_cssid = from_cssid;
189 to_ssid = from_ssid;
190 to = from;
191 }
192 }
193 }
194 if (!rc) {
195 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
196 msgtrigger);
197 if (rc)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200198 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200199 } else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200200 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200201 }
202
203 return totalrc;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static int __init
207blacklist_setup (char *str)
208{
Michael Ernst5b890982008-05-07 09:22:55 +0200209 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
210 if (blacklist_parse_parameters(str, add, 1))
211 return 0;
212 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215__setup ("cio_ignore=", blacklist_setup);
216
217/* Checking if devices are blacklisted */
218
219/*
220 * Function: is_blacklisted
221 * Returns 1 if the given devicenumber can be found in the blacklist,
222 * otherwise 0.
223 * Used by validate_subchannel()
224 */
225int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800226is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800228 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * Function: blacklist_parse_proc_parameters
234 * parse the stuff which is piped to /proc/cio_ignore
235 */
Michael Ernst5b890982008-05-07 09:22:55 +0200236static int blacklist_parse_proc_parameters(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Michael Ernst5b890982008-05-07 09:22:55 +0200238 int rc;
239 char *parm;
240
241 parm = strsep(&buf, " ");
242
243 if (strcmp("free", parm) == 0)
244 rc = blacklist_parse_parameters(buf, free, 0);
245 else if (strcmp("add", parm) == 0)
246 rc = blacklist_parse_parameters(buf, add, 0);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200247 else if (strcmp("purge", parm) == 0)
248 return ccw_purge_blacklisted();
Michael Ernst5b890982008-05-07 09:22:55 +0200249 else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200250 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200252 css_schedule_reprobe();
Michael Ernst5b890982008-05-07 09:22:55 +0200253
254 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Cornelia Huck678a3952006-01-06 00:19:24 -0800257/* Iterator struct for all devices. */
258struct ccwdev_iter {
259 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800260 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800261 int in_range;
262};
263
264static void *
265cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200267 struct ccwdev_iter *iter = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800269 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800270 return NULL;
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200271 memset(iter, 0, sizeof(*iter));
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800272 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
273 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800274 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Cornelia Huck678a3952006-01-06 00:19:24 -0800277static void
278cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
279{
Cornelia Huck678a3952006-01-06 00:19:24 -0800280}
281
282static void *
283cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
284{
285 struct ccwdev_iter *iter;
286
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800287 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800288 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800289 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800290 if (iter->devno == __MAX_SUBCHANNEL) {
291 iter->devno = 0;
292 iter->ssid++;
293 if (iter->ssid > __MAX_SSID)
294 return NULL;
295 } else
296 iter->devno++;
Cornelia Huck678a3952006-01-06 00:19:24 -0800297 (*offset)++;
298 return iter;
299}
300
301static int
302cio_ignore_proc_seq_show(struct seq_file *s, void *it)
303{
304 struct ccwdev_iter *iter;
305
Cornelia Huck3b793062006-01-06 00:19:26 -0800306 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800307 if (!is_blacklisted(iter->ssid, iter->devno))
Cornelia Huck678a3952006-01-06 00:19:24 -0800308 /* Not blacklisted, nothing to output. */
309 return 0;
310 if (!iter->in_range) {
311 /* First device in range. */
312 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800313 !is_blacklisted(iter->ssid, iter->devno + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800314 /* Singular device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800315 return seq_printf(s, "0.%x.%04x\n",
316 iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800317 iter->in_range = 1;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800318 return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800319 }
320 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800321 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800322 /* Last device in range. */
323 iter->in_range = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800324 return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800325 }
326 return 0;
327}
328
329static ssize_t
330cio_ignore_write(struct file *file, const char __user *user_buf,
331 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 char *buf;
Sebastian Ott94cbc202009-03-26 15:24:16 +0100334 ssize_t rc, ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Cornelia Huck678a3952006-01-06 00:19:24 -0800336 if (*offset)
337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (user_len > 65536)
339 user_len = 65536;
340 buf = vmalloc (user_len + 1); /* maybe better use the stack? */
341 if (buf == NULL)
342 return -ENOMEM;
Michael Ernst5b890982008-05-07 09:22:55 +0200343 memset(buf, 0, user_len + 1);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200346 rc = -EFAULT;
347 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Michael Ernst5b890982008-05-07 09:22:55 +0200350 i = user_len - 1;
351 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
352 buf[i] = '\0';
353 i--;
354 }
355 ret = blacklist_parse_proc_parameters(buf);
356 if (ret)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200357 rc = ret;
Michael Ernst5b890982008-05-07 09:22:55 +0200358 else
359 rc = user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Michael Ernst5b890982008-05-07 09:22:55 +0200361out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 vfree (buf);
Michael Ernst5b890982008-05-07 09:22:55 +0200363 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100366static const struct seq_operations cio_ignore_proc_seq_ops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800367 .start = cio_ignore_proc_seq_start,
368 .stop = cio_ignore_proc_seq_stop,
369 .next = cio_ignore_proc_seq_next,
370 .show = cio_ignore_proc_seq_show,
371};
372
373static int
374cio_ignore_proc_open(struct inode *inode, struct file *file)
375{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200376 return seq_open_private(file, &cio_ignore_proc_seq_ops,
377 sizeof(struct ccwdev_iter));
Cornelia Huck678a3952006-01-06 00:19:24 -0800378}
379
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800380static const struct file_operations cio_ignore_proc_fops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800381 .open = cio_ignore_proc_open,
382 .read = seq_read,
383 .llseek = seq_lseek,
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200384 .release = seq_release_private,
Cornelia Huck678a3952006-01-06 00:19:24 -0800385 .write = cio_ignore_write,
386};
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static int
389cio_ignore_proc_init (void)
390{
391 struct proc_dir_entry *entry;
392
Denis V. Lunev8b594002008-04-29 01:02:20 -0700393 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
394 &cio_ignore_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700396 return -ENOENT;
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400__initcall (cio_ignore_proc_init);
401
402#endif /* CONFIG_PROC_FS */