1 // SPDX-License-Identifier: GPL-2.0
3 * S/390 common I/O routines -- blacklisting of specific devices
5 * Copyright IBM Corp. 1999, 2013
6 * Author(s): Ingo Adlung (adlung@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
8 * Arnd Bergmann (arndb@de.ibm.com)
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/init.h>
15 #include <linux/vmalloc.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/ctype.h>
19 #include <linux/device.h>
21 #include <linux/uaccess.h>
25 #include "blacklist.h"
27 #include "cio_debug.h"
32 * "Blacklisting" of certain devices:
33 * Device numbers given in the commandline as cio_ignore=... won't be known
36 * These can be single devices or ranges of devices
39 /* 65536 bits for each set to indicate if a devno is blacklisted or not */
40 #define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
42 static unsigned long bl_dev
[__MAX_SSID
+ 1][__BL_DEV_WORDS
];
43 typedef enum {add
, free
} range_action
;
46 * Function: blacklist_range
47 * (Un-)blacklist the devices from-to
49 static int blacklist_range(range_action action
, unsigned int from_ssid
,
50 unsigned int to_ssid
, unsigned int from
,
51 unsigned int to
, int msgtrigger
)
53 if ((from_ssid
> to_ssid
) || ((from_ssid
== to_ssid
) && (from
> to
))) {
55 pr_warn("0.%x.%04x to 0.%x.%04x is not a valid range for cio_ignore\n",
56 from_ssid
, from
, to_ssid
, to
);
61 while ((from_ssid
< to_ssid
) || ((from_ssid
== to_ssid
) &&
64 set_bit(from
, bl_dev
[from_ssid
]);
66 clear_bit(from
, bl_dev
[from_ssid
]);
68 if (from
> __MAX_SUBCHANNEL
) {
77 static int pure_hex(char **cp
, unsigned int *val
, int min_digit
,
78 int max_digit
, int max_val
)
85 while (diff
<= max_digit
) {
86 int value
= hex_to_bin(**cp
);
90 *val
= *val
* 16 + value
;
95 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
101 static int parse_busid(char *str
, unsigned int *cssid
, unsigned int *ssid
,
102 unsigned int *devno
, int msgtrigger
)
114 val
= simple_strtoul(str
, &str_work
, 16);
116 if (*str_work
== '\0') {
117 if (val
<= __MAX_SUBCHANNEL
) {
128 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
129 if (ret
|| (str_work
[0] != '.'))
132 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
133 if (ret
|| (str_work
[0] != '.'))
136 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
137 if (ret
|| (str_work
[0] != '\0'))
142 if (rc
&& msgtrigger
)
143 pr_warn("%s is not a valid device for the cio_ignore kernel parameter\n",
149 static int blacklist_parse_parameters(char *str
, range_action action
,
152 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
159 while ((parm
= strsep(&str
, ","))) {
169 if (strcmp(parm
, "all") == 0) {
173 to_cssid
= __MAX_CSSID
;
174 to_ssid
= __MAX_SSID
;
175 to
= __MAX_SUBCHANNEL
;
176 } else if (strcmp(parm
, "ipldev") == 0) {
177 if (ipl_info
.type
== IPL_TYPE_CCW
) {
179 from_ssid
= ipl_info
.data
.ccw
.dev_id
.ssid
;
180 from
= ipl_info
.data
.ccw
.dev_id
.devno
;
181 } else if (ipl_info
.type
== IPL_TYPE_FCP
||
182 ipl_info
.type
== IPL_TYPE_FCP_DUMP
) {
184 from_ssid
= ipl_info
.data
.fcp
.dev_id
.ssid
;
185 from
= ipl_info
.data
.fcp
.dev_id
.devno
;
189 to_cssid
= from_cssid
;
192 } else if (strcmp(parm
, "condev") == 0) {
193 if (console_devno
== -1)
196 from_cssid
= to_cssid
= 0;
197 from_ssid
= to_ssid
= 0;
198 from
= to
= console_devno
;
200 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
201 &from_ssid
, &from
, msgtrigger
);
204 rc
= parse_busid(parm
, &to_cssid
,
208 to_cssid
= from_cssid
;
215 rc
= blacklist_range(ra
, from_ssid
, to_ssid
, from
, to
,
227 blacklist_setup (char *str
)
229 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
230 if (blacklist_parse_parameters(str
, add
, 1))
235 __setup ("cio_ignore=", blacklist_setup
);
237 /* Checking if devices are blacklisted */
240 * Function: is_blacklisted
241 * Returns 1 if the given devicenumber can be found in the blacklist,
243 * Used by validate_subchannel()
246 is_blacklisted (int ssid
, int devno
)
248 return test_bit (devno
, bl_dev
[ssid
]);
251 #ifdef CONFIG_PROC_FS
253 * Function: blacklist_parse_proc_parameters
254 * parse the stuff which is piped to /proc/cio_ignore
256 static int blacklist_parse_proc_parameters(char *buf
)
261 parm
= strsep(&buf
, " ");
263 if (strcmp("free", parm
) == 0) {
264 rc
= blacklist_parse_parameters(buf
, free
, 0);
266 * Evaluate the subchannels without an online device. This way,
267 * no path-verification will be triggered on those subchannels
268 * and it avoids unnecessary delays.
270 css_schedule_eval_cond(CSS_EVAL_NOT_ONLINE
, 0);
271 } else if (strcmp("add", parm
) == 0)
272 rc
= blacklist_parse_parameters(buf
, add
, 0);
273 else if (strcmp("purge", parm
) == 0)
274 return ccw_purge_blacklisted();
282 /* Iterator struct for all devices. */
290 cio_ignore_proc_seq_start(struct seq_file
*s
, loff_t
*offset
)
292 struct ccwdev_iter
*iter
= s
->private;
294 if (*offset
>= (__MAX_SUBCHANNEL
+ 1) * (__MAX_SSID
+ 1))
296 memset(iter
, 0, sizeof(*iter
));
297 iter
->ssid
= *offset
/ (__MAX_SUBCHANNEL
+ 1);
298 iter
->devno
= *offset
% (__MAX_SUBCHANNEL
+ 1);
303 cio_ignore_proc_seq_stop(struct seq_file
*s
, void *it
)
308 cio_ignore_proc_seq_next(struct seq_file
*s
, void *it
, loff_t
*offset
)
310 struct ccwdev_iter
*iter
;
314 if (p
>= (__MAX_SUBCHANNEL
+ 1) * (__MAX_SSID
+ 1))
317 if (iter
->devno
== __MAX_SUBCHANNEL
) {
320 if (iter
->ssid
> __MAX_SSID
)
328 cio_ignore_proc_seq_show(struct seq_file
*s
, void *it
)
330 struct ccwdev_iter
*iter
;
333 if (!is_blacklisted(iter
->ssid
, iter
->devno
))
334 /* Not blacklisted, nothing to output. */
336 if (!iter
->in_range
) {
337 /* First device in range. */
338 if ((iter
->devno
== __MAX_SUBCHANNEL
) ||
339 !is_blacklisted(iter
->ssid
, iter
->devno
+ 1)) {
340 /* Singular device. */
341 seq_printf(s
, "0.%x.%04x\n", iter
->ssid
, iter
->devno
);
345 seq_printf(s
, "0.%x.%04x-", iter
->ssid
, iter
->devno
);
348 if ((iter
->devno
== __MAX_SUBCHANNEL
) ||
349 !is_blacklisted(iter
->ssid
, iter
->devno
+ 1)) {
350 /* Last device in range. */
352 seq_printf(s
, "0.%x.%04x\n", iter
->ssid
, iter
->devno
);
358 cio_ignore_write(struct file
*file
, const char __user
*user_buf
,
359 size_t user_len
, loff_t
*offset
)
366 if (user_len
> 65536)
368 buf
= vzalloc(user_len
+ 1); /* maybe better use the stack? */
372 if (strncpy_from_user (buf
, user_buf
, user_len
) < 0) {
378 while ((i
>= 0) && (isspace(buf
[i
]) || (buf
[i
] == 0))) {
382 ret
= blacklist_parse_proc_parameters(buf
);
393 static const struct seq_operations cio_ignore_proc_seq_ops
= {
394 .start
= cio_ignore_proc_seq_start
,
395 .stop
= cio_ignore_proc_seq_stop
,
396 .next
= cio_ignore_proc_seq_next
,
397 .show
= cio_ignore_proc_seq_show
,
401 cio_ignore_proc_open(struct inode
*inode
, struct file
*file
)
403 return seq_open_private(file
, &cio_ignore_proc_seq_ops
,
404 sizeof(struct ccwdev_iter
));
407 static const struct proc_ops cio_ignore_proc_ops
= {
408 .proc_open
= cio_ignore_proc_open
,
409 .proc_read
= seq_read
,
410 .proc_lseek
= seq_lseek
,
411 .proc_release
= seq_release_private
,
412 .proc_write
= cio_ignore_write
,
416 cio_ignore_proc_init (void)
418 struct proc_dir_entry
*entry
;
420 entry
= proc_create("cio_ignore", S_IFREG
| S_IRUGO
| S_IWUSR
, NULL
,
421 &cio_ignore_proc_ops
);
427 __initcall (cio_ignore_proc_init
);
429 #endif /* CONFIG_PROC_FS */