blacklist.c 9.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  drivers/s390/cio/blacklist.c
 *   S/390 common I/O routines -- blacklisting of specific devices
4
 *   $Revision: 1.39 $
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *    Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
 *			      IBM Corporation
 *    Author(s): Ingo Adlung (adlung@de.ibm.com)
 *		 Cornelia Huck (cohuck@de.ibm.com)
 *		 Arnd Bergmann (arndb@de.ibm.com)
 */

#include <linux/config.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
18
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <linux/ctype.h>
#include <linux/device.h>

#include <asm/cio.h>
#include <asm/uaccess.h>

#include "blacklist.h"
#include "cio.h"
#include "cio_debug.h"
#include "css.h"

/*
 * "Blacklisting" of certain devices:
 * Device numbers given in the commandline as cio_ignore=... won't be known
 * to Linux.
 *
 * These can be single devices or ranges of devices
 */

38
/* 65536 bits for each set to indicate if a devno is blacklisted or not */
39
#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
L
Linus Torvalds 已提交
40
			 (8*sizeof(long)))
41
static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
L
Linus Torvalds 已提交
42 43 44 45 46 47 48
typedef enum {add, free} range_action;

/*
 * Function: blacklist_range
 * (Un-)blacklist the devices from-to
 */
static inline void
49 50
blacklist_range (range_action action, unsigned int from, unsigned int to,
		 unsigned int ssid)
L
Linus Torvalds 已提交
51 52 53 54
{
	if (!to)
		to = from;

55
	if (from > to || to > __MAX_SUBCHANNEL || ssid > __MAX_SSID) {
L
Linus Torvalds 已提交
56
		printk (KERN_WARNING "Invalid blacklist range "
57 58
			"0.%x.%04x to 0.%x.%04x, skipping\n",
			ssid, from, ssid, to);
L
Linus Torvalds 已提交
59 60 61 62
		return;
	}
	for (; from <= to; from++) {
		if (action == add)
63
			set_bit (from, bl_dev[ssid]);
L
Linus Torvalds 已提交
64
		else
65
			clear_bit (from, bl_dev[ssid]);
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74
	}
}

/*
 * Function: blacklist_busid
 * Get devno/busid from given string.
 * Shamelessly grabbed from dasd_devmap.c.
 */
static inline int
75
blacklist_busid(char **str, int *id0, int *ssid, int *devno)
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	int val, old_style;
	char *sav;

	sav = *str;

	/* check for leading '0x' */
	old_style = 0;
	if ((*str)[0] == '0' && (*str)[1] == 'x') {
		*str += 2;
		old_style = 1;
	}
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		goto confused;
	val = simple_strtoul(*str, str, 16);
	if (old_style || (*str)[0] != '.') {
92
		*id0 = *ssid = 0;
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		if (val < 0 || val > 0xffff)
			goto confused;
		*devno = val;
		if ((*str)[0] != ',' && (*str)[0] != '-' &&
		    (*str)[0] != '\n' && (*str)[0] != '\0')
			goto confused;
		return 0;
	}
	/* New style x.y.z busid */
	if (val < 0 || val > 0xff)
		goto confused;
	*id0 = val;
	(*str)++;
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		goto confused;
	val = simple_strtoul(*str, str, 16);
	if (val < 0 || val > 0xff || (*str)++[0] != '.')
		goto confused;
111
	*ssid = val;
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	if (!isxdigit((*str)[0]))	/* We require at least one hex digit */
		goto confused;
	val = simple_strtoul(*str, str, 16);
	if (val < 0 || val > 0xffff)
		goto confused;
	*devno = val;
	if ((*str)[0] != ',' && (*str)[0] != '-' &&
	    (*str)[0] != '\n' && (*str)[0] != '\0')
		goto confused;
	return 0;
confused:
	strsep(str, ",\n");
	printk(KERN_WARNING "Invalid cio_ignore parameter '%s'\n", sav);
	return 1;
}

static inline int
blacklist_parse_parameters (char *str, range_action action)
{
131
	unsigned int from, to, from_id0, to_id0, from_ssid, to_ssid;
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	while (*str != 0 && *str != '\n') {
		range_action ra = action;
		while(*str == ',')
			str++;
		if (*str == '!') {
			ra = !action;
			++str;
		}

		/*
		 * Since we have to parse the proc commands and the
		 * kernel arguments we have to check four cases
		 */
		if (strncmp(str,"all,",4) == 0 || strcmp(str,"all") == 0 ||
		    strncmp(str,"all\n",4) == 0 || strncmp(str,"all ",4) == 0) {
148 149
			int j;

L
Linus Torvalds 已提交
150
			str += 3;
151 152
			for (j=0; j <= __MAX_SSID; j++)
				blacklist_range(ra, 0, __MAX_SUBCHANNEL, j);
L
Linus Torvalds 已提交
153 154 155 156
		} else {
			int rc;

			rc = blacklist_busid(&str, &from_id0,
157
					     &from_ssid, &from);
L
Linus Torvalds 已提交
158 159 160 161
			if (rc)
				continue;
			to = from;
			to_id0 = from_id0;
162
			to_ssid = from_ssid;
L
Linus Torvalds 已提交
163 164 165
			if (*str == '-') {
				str++;
				rc = blacklist_busid(&str, &to_id0,
166
						     &to_ssid, &to);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175
				if (rc)
					continue;
			}
			if (*str == '-') {
				printk(KERN_WARNING "invalid cio_ignore "
					"parameter '%s'\n",
					strsep(&str, ",\n"));
				continue;
			}
176 177
			if ((from_id0 != to_id0) ||
			    (from_ssid != to_ssid)) {
L
Linus Torvalds 已提交
178 179
				printk(KERN_WARNING "invalid cio_ignore range "
					"%x.%x.%04x-%x.%x.%04x\n",
180 181
					from_id0, from_ssid, from,
					to_id0, to_ssid, to);
L
Linus Torvalds 已提交
182 183
				continue;
			}
184 185 186 187
			pr_debug("blacklist_setup: adding range "
				 "from %x.%x.%04x to %x.%x.%04x\n",
				 from_id0, from_ssid, from, to_id0, to_ssid, to);
			blacklist_range (ra, from, to, to_ssid);
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		}
	}
	return 1;
}

/* Parsing the commandline for blacklist parameters, e.g. to blacklist
 * bus ids 0.0.1234, 0.0.1235 and 0.0.1236, you could use any of:
 * - cio_ignore=1234-1236
 * - cio_ignore=0x1234-0x1235,1236
 * - cio_ignore=0x1234,1235-1236
 * - cio_ignore=1236 cio_ignore=1234-0x1236
 * - cio_ignore=1234 cio_ignore=1236 cio_ignore=0x1235
 * - cio_ignore=0.0.1234-0.0.1236
 * - cio_ignore=0.0.1234,0x1235,1236
 * - ...
 */
static int __init
blacklist_setup (char *str)
{
	CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
	return blacklist_parse_parameters (str, add);
}

__setup ("cio_ignore=", blacklist_setup);

/* Checking if devices are blacklisted */

/*
 * Function: is_blacklisted
 * Returns 1 if the given devicenumber can be found in the blacklist,
 * otherwise 0.
 * Used by validate_subchannel()
 */
int
222
is_blacklisted (int ssid, int devno)
L
Linus Torvalds 已提交
223
{
224
	return test_bit (devno, bl_dev[ssid]);
L
Linus Torvalds 已提交
225 226 227
}

#ifdef CONFIG_PROC_FS
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static int
__s390_redo_validation(struct subchannel_id schid, void *data)
{
	int ret;
	struct subchannel *sch;

	sch = get_subchannel_by_schid(schid);
	if (sch) {
		/* Already known. */
		put_device(&sch->dev);
		return 0;
	}
	ret = css_probe_device(schid);
	if (ret == -ENXIO)
		return ret; /* We're through. */
	if (ret == -ENOMEM)
		/* Stop validation for now. Bad, but no need for a panic. */
		return ret;
	return 0;
}

L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257
/*
 * Function: s390_redo_validation
 * Look for no longer blacklisted devices
 * FIXME: there must be a better way to do this */
static inline void
s390_redo_validation (void)
{
	CIO_TRACE_EVENT (0, "redoval");

258
	for_each_subchannel(__s390_redo_validation, NULL);
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
}

/*
 * Function: blacklist_parse_proc_parameters
 * parse the stuff which is piped to /proc/cio_ignore
 */
static inline void
blacklist_parse_proc_parameters (char *buf)
{
	if (strncmp (buf, "free ", 5) == 0) {
		blacklist_parse_parameters (buf + 5, free);
	} else if (strncmp (buf, "add ", 4) == 0) {
		/* 
		 * We don't need to check for known devices since
		 * css_probe_device will handle this correctly. 
		 */
		blacklist_parse_parameters (buf + 4, add);
	} else {
		printk (KERN_WARNING "cio_ignore: Parse error; \n"
			KERN_WARNING "try using 'free all|<devno-range>,"
				     "<devno-range>,...'\n"
			KERN_WARNING "or 'add <devno-range>,"
				     "<devno-range>,...'\n");
		return;
	}

	s390_redo_validation ();
}

288 289 290
/* Iterator struct for all devices. */
struct ccwdev_iter {
	int devno;
291
	int ssid;
292 293 294 295 296
	int in_range;
};

static void *
cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
L
Linus Torvalds 已提交
297
{
298 299
	struct ccwdev_iter *iter;

300
	if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
301
		return NULL;
302
	iter = kzalloc(sizeof(struct ccwdev_iter), GFP_KERNEL);
303 304
	if (!iter)
		return ERR_PTR(-ENOMEM);
305 306
	iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
	iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
307 308 309 310 311 312 313 314 315
	return iter;
}

static void
cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
{
	if (!IS_ERR(it))
		kfree(it);
}
L
Linus Torvalds 已提交
316

317 318 319 320 321
static void *
cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
{
	struct ccwdev_iter *iter;

322
	if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
323
		return NULL;
324
	iter = it;
325 326 327 328 329 330 331
	if (iter->devno == __MAX_SUBCHANNEL) {
		iter->devno = 0;
		iter->ssid++;
		if (iter->ssid > __MAX_SSID)
			return NULL;
	} else
		iter->devno++;
332 333
	(*offset)++;
	return iter;
L
Linus Torvalds 已提交
334 335
}

336 337 338 339 340
static int
cio_ignore_proc_seq_show(struct seq_file *s, void *it)
{
	struct ccwdev_iter *iter;

341
	iter = it;
342
	if (!is_blacklisted(iter->ssid, iter->devno))
343 344 345 346 347
		/* Not blacklisted, nothing to output. */
		return 0;
	if (!iter->in_range) {
		/* First device in range. */
		if ((iter->devno == __MAX_SUBCHANNEL) ||
348
		    !is_blacklisted(iter->ssid, iter->devno + 1))
349
			/* Singular device. */
350 351
			return seq_printf(s, "0.%x.%04x\n",
					  iter->ssid, iter->devno);
352
		iter->in_range = 1;
353
		return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
354 355
	}
	if ((iter->devno == __MAX_SUBCHANNEL) ||
356
	    !is_blacklisted(iter->ssid, iter->devno + 1)) {
357 358
		/* Last device in range. */
		iter->in_range = 0;
359
		return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
360 361 362 363 364 365 366
	}
	return 0;
}

static ssize_t
cio_ignore_write(struct file *file, const char __user *user_buf,
		 size_t user_len, loff_t *offset)
L
Linus Torvalds 已提交
367 368 369
{
	char *buf;

370 371
	if (*offset)
		return -EINVAL;
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	if (user_len > 65536)
		user_len = 65536;
	buf = vmalloc (user_len + 1); /* maybe better use the stack? */
	if (buf == NULL)
		return -ENOMEM;
	if (strncpy_from_user (buf, user_buf, user_len) < 0) {
		vfree (buf);
		return -EFAULT;
	}
	buf[user_len] = '\0';

	blacklist_parse_proc_parameters (buf);

	vfree (buf);
	return user_len;
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
static struct seq_operations cio_ignore_proc_seq_ops = {
	.start = cio_ignore_proc_seq_start,
	.stop  = cio_ignore_proc_seq_stop,
	.next  = cio_ignore_proc_seq_next,
	.show  = cio_ignore_proc_seq_show,
};

static int
cio_ignore_proc_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &cio_ignore_proc_seq_ops);
}

static struct file_operations cio_ignore_proc_fops = {
	.open    = cio_ignore_proc_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release,
	.write   = cio_ignore_write,
};

L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418 419
static int
cio_ignore_proc_init (void)
{
	struct proc_dir_entry *entry;

	entry = create_proc_entry ("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR,
				   &proc_root);
	if (!entry)
		return 0;

420
	entry->proc_fops = &cio_ignore_proc_fops;
L
Linus Torvalds 已提交
421 422 423 424 425 426 427

	return 1;
}

__initcall (cio_ignore_proc_init);

#endif /* CONFIG_PROC_FS */