blob: 12c2d6b746e697b5e017d6414f2281a509aae1ff [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 */
46static inline void
Cornelia Huckfb6958a2006-01-06 00:19:25 -080047blacklist_range (range_action action, unsigned int from, unsigned int to,
48 unsigned int ssid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 if (!to)
51 to = from;
52
Cornelia Huckfb6958a2006-01-06 00:19:25 -080053 if (from > to || to > __MAX_SUBCHANNEL || ssid > __MAX_SSID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 printk (KERN_WARNING "Invalid blacklist range "
Cornelia Huckfb6958a2006-01-06 00:19:25 -080055 "0.%x.%04x to 0.%x.%04x, skipping\n",
56 ssid, from, ssid, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return;
58 }
59 for (; from <= to; from++) {
60 if (action == add)
Cornelia Huckfb6958a2006-01-06 00:19:25 -080061 set_bit (from, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 else
Cornelia Huckfb6958a2006-01-06 00:19:25 -080063 clear_bit (from, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65}
66
67/*
68 * Function: blacklist_busid
69 * Get devno/busid from given string.
70 * Shamelessly grabbed from dasd_devmap.c.
71 */
72static inline int
Cornelia Huckfb6958a2006-01-06 00:19:25 -080073blacklist_busid(char **str, int *id0, int *ssid, int *devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 int val, old_style;
76 char *sav;
77
78 sav = *str;
79
80 /* check for leading '0x' */
81 old_style = 0;
82 if ((*str)[0] == '0' && (*str)[1] == 'x') {
83 *str += 2;
84 old_style = 1;
85 }
86 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
87 goto confused;
88 val = simple_strtoul(*str, str, 16);
89 if (old_style || (*str)[0] != '.') {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080090 *id0 = *ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (val < 0 || val > 0xffff)
92 goto confused;
93 *devno = val;
94 if ((*str)[0] != ',' && (*str)[0] != '-' &&
95 (*str)[0] != '\n' && (*str)[0] != '\0')
96 goto confused;
97 return 0;
98 }
99 /* New style x.y.z busid */
100 if (val < 0 || val > 0xff)
101 goto confused;
102 *id0 = val;
103 (*str)++;
104 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
105 goto confused;
106 val = simple_strtoul(*str, str, 16);
107 if (val < 0 || val > 0xff || (*str)++[0] != '.')
108 goto confused;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800109 *ssid = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
111 goto confused;
112 val = simple_strtoul(*str, str, 16);
113 if (val < 0 || val > 0xffff)
114 goto confused;
115 *devno = val;
116 if ((*str)[0] != ',' && (*str)[0] != '-' &&
117 (*str)[0] != '\n' && (*str)[0] != '\0')
118 goto confused;
119 return 0;
120confused:
121 strsep(str, ",\n");
122 printk(KERN_WARNING "Invalid cio_ignore parameter '%s'\n", sav);
123 return 1;
124}
125
126static inline int
127blacklist_parse_parameters (char *str, range_action action)
128{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800129 unsigned int from, to, from_id0, to_id0, from_ssid, to_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 while (*str != 0 && *str != '\n') {
132 range_action ra = action;
133 while(*str == ',')
134 str++;
135 if (*str == '!') {
136 ra = !action;
137 ++str;
138 }
139
140 /*
141 * Since we have to parse the proc commands and the
142 * kernel arguments we have to check four cases
143 */
144 if (strncmp(str,"all,",4) == 0 || strcmp(str,"all") == 0 ||
145 strncmp(str,"all\n",4) == 0 || strncmp(str,"all ",4) == 0) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800146 int j;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 str += 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800149 for (j=0; j <= __MAX_SSID; j++)
150 blacklist_range(ra, 0, __MAX_SUBCHANNEL, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
152 int rc;
153
154 rc = blacklist_busid(&str, &from_id0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800155 &from_ssid, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (rc)
157 continue;
158 to = from;
159 to_id0 = from_id0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800160 to_ssid = from_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (*str == '-') {
162 str++;
163 rc = blacklist_busid(&str, &to_id0,
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800164 &to_ssid, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (rc)
166 continue;
167 }
168 if (*str == '-') {
169 printk(KERN_WARNING "invalid cio_ignore "
170 "parameter '%s'\n",
171 strsep(&str, ",\n"));
172 continue;
173 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800174 if ((from_id0 != to_id0) ||
175 (from_ssid != to_ssid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 printk(KERN_WARNING "invalid cio_ignore range "
177 "%x.%x.%04x-%x.%x.%04x\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800178 from_id0, from_ssid, from,
179 to_id0, to_ssid, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 continue;
181 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800182 pr_debug("blacklist_setup: adding range "
183 "from %x.%x.%04x to %x.%x.%04x\n",
184 from_id0, from_ssid, from, to_id0, to_ssid, to);
185 blacklist_range (ra, from, to, to_ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 return 1;
189}
190
191/* Parsing the commandline for blacklist parameters, e.g. to blacklist
192 * bus ids 0.0.1234, 0.0.1235 and 0.0.1236, you could use any of:
193 * - cio_ignore=1234-1236
194 * - cio_ignore=0x1234-0x1235,1236
195 * - cio_ignore=0x1234,1235-1236
196 * - cio_ignore=1236 cio_ignore=1234-0x1236
197 * - cio_ignore=1234 cio_ignore=1236 cio_ignore=0x1235
198 * - cio_ignore=0.0.1234-0.0.1236
199 * - cio_ignore=0.0.1234,0x1235,1236
200 * - ...
201 */
202static int __init
203blacklist_setup (char *str)
204{
205 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
206 return blacklist_parse_parameters (str, add);
207}
208
209__setup ("cio_ignore=", blacklist_setup);
210
211/* Checking if devices are blacklisted */
212
213/*
214 * Function: is_blacklisted
215 * Returns 1 if the given devicenumber can be found in the blacklist,
216 * otherwise 0.
217 * Used by validate_subchannel()
218 */
219int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800220is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800222 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
227 * Function: blacklist_parse_proc_parameters
228 * parse the stuff which is piped to /proc/cio_ignore
229 */
230static inline void
231blacklist_parse_proc_parameters (char *buf)
232{
233 if (strncmp (buf, "free ", 5) == 0) {
234 blacklist_parse_parameters (buf + 5, free);
235 } else if (strncmp (buf, "add ", 4) == 0) {
236 /*
237 * We don't need to check for known devices since
238 * css_probe_device will handle this correctly.
239 */
240 blacklist_parse_parameters (buf + 4, add);
241 } else {
242 printk (KERN_WARNING "cio_ignore: Parse error; \n"
243 KERN_WARNING "try using 'free all|<devno-range>,"
244 "<devno-range>,...'\n"
245 KERN_WARNING "or 'add <devno-range>,"
246 "<devno-range>,...'\n");
247 return;
248 }
249
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200250 css_schedule_reprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Cornelia Huck678a3952006-01-06 00:19:24 -0800253/* Iterator struct for all devices. */
254struct ccwdev_iter {
255 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800256 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800257 int in_range;
258};
259
260static void *
261cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Cornelia Huck678a3952006-01-06 00:19:24 -0800263 struct ccwdev_iter *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800265 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800266 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800267 iter = kzalloc(sizeof(struct ccwdev_iter), GFP_KERNEL);
Cornelia Huck678a3952006-01-06 00:19:24 -0800268 if (!iter)
269 return ERR_PTR(-ENOMEM);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800270 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
271 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800272 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Cornelia Huck678a3952006-01-06 00:19:24 -0800275static void
276cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
277{
278 if (!IS_ERR(it))
279 kfree(it);
280}
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;
334
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;
342 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
343 vfree (buf);
344 return -EFAULT;
345 }
346 buf[user_len] = '\0';
347
348 blacklist_parse_proc_parameters (buf);
349
350 vfree (buf);
351 return user_len;
352}
353
Cornelia Huck678a3952006-01-06 00:19:24 -0800354static struct seq_operations cio_ignore_proc_seq_ops = {
355 .start = cio_ignore_proc_seq_start,
356 .stop = cio_ignore_proc_seq_stop,
357 .next = cio_ignore_proc_seq_next,
358 .show = cio_ignore_proc_seq_show,
359};
360
361static int
362cio_ignore_proc_open(struct inode *inode, struct file *file)
363{
364 return seq_open(file, &cio_ignore_proc_seq_ops);
365}
366
367static struct file_operations cio_ignore_proc_fops = {
368 .open = cio_ignore_proc_open,
369 .read = seq_read,
370 .llseek = seq_lseek,
371 .release = seq_release,
372 .write = cio_ignore_write,
373};
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static int
376cio_ignore_proc_init (void)
377{
378 struct proc_dir_entry *entry;
379
380 entry = create_proc_entry ("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR,
381 &proc_root);
382 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700383 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Cornelia Huck678a3952006-01-06 00:19:24 -0800385 entry->proc_fops = &cio_ignore_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390__initcall (cio_ignore_proc_init);
391
392#endif /* CONFIG_PROC_FS */