]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/s390/cio/blacklist.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-eoan-kernel.git] / drivers / s390 / cio / blacklist.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4 3 * S/390 common I/O routines -- blacklisting of specific devices
1da177e4 4 *
0e6c83d1 5 * Copyright IBM Corp. 1999, 2013
1da177e4 6 * Author(s): Ingo Adlung (adlung@de.ibm.com)
4ce3b30c 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
8 * Arnd Bergmann (arndb@de.ibm.com)
9 */
10
e6d5a428
ME
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
1da177e4
LT
14#include <linux/init.h>
15#include <linux/vmalloc.h>
1da177e4 16#include <linux/proc_fs.h>
678a395b 17#include <linux/seq_file.h>
1da177e4
LT
18#include <linux/ctype.h>
19#include <linux/device.h>
20
7c0f6ba6 21#include <linux/uaccess.h>
0e6c83d1
SO
22#include <asm/cio.h>
23#include <asm/ipl.h>
1da177e4
LT
24
25#include "blacklist.h"
26#include "cio.h"
27#include "cio_debug.h"
28#include "css.h"
ecf5d9ef 29#include "device.h"
1da177e4
LT
30
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
fb6958a5 39/* 65536 bits for each set to indicate if a devno is blacklisted or not */
a8237fc4 40#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1da177e4 41 (8*sizeof(long)))
fb6958a5 42static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
1da177e4
LT
43typedef enum {add, free} range_action;
44
45/*
46 * Function: blacklist_range
47 * (Un-)blacklist the devices from-to
48 */
5b890987
ME
49static int blacklist_range(range_action action, unsigned int from_ssid,
50 unsigned int to_ssid, unsigned int from,
51 unsigned int to, int msgtrigger)
1da177e4 52{
5b890987
ME
53 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
54 if (msgtrigger)
baebc70a
JP
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);
e6d5a428 57
5b890987 58 return 1;
1da177e4 59 }
5b890987
ME
60
61 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
62 (from <= to))) {
1da177e4 63 if (action == add)
5b890987 64 set_bit(from, bl_dev[from_ssid]);
1da177e4 65 else
5b890987
ME
66 clear_bit(from, bl_dev[from_ssid]);
67 from++;
68 if (from > __MAX_SUBCHANNEL) {
69 from_ssid++;
70 from = 0;
71 }
1da177e4 72 }
5b890987
ME
73
74 return 0;
1da177e4
LT
75}
76
5b890987
ME
77static int pure_hex(char **cp, unsigned int *val, int min_digit,
78 int max_digit, int max_val)
1da177e4 79{
5b890987 80 int diff;
1da177e4 81
5b890987
ME
82 diff = 0;
83 *val = 0;
1da177e4 84
f2777077
AS
85 while (diff <= max_digit) {
86 int value = hex_to_bin(**cp);
5b890987 87
f2777077
AS
88 if (value < 0)
89 break;
5b890987
ME
90 *val = *val * 16 + value;
91 (*cp)++;
92 diff++;
1da177e4 93 }
5b890987
ME
94
95 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
96 return 1;
97
1da177e4 98 return 0;
1da177e4
LT
99}
100
12829126
CH
101static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
102 unsigned int *devno, int msgtrigger)
1da177e4 103{
5b890987
ME
104 char *str_work;
105 int val, rc, ret;
106
107 rc = 1;
108
109 if (*str == '\0')
110 goto out;
111
112 /* old style */
113 str_work = str;
114 val = simple_strtoul(str, &str_work, 16);
115
116 if (*str_work == '\0') {
117 if (val <= __MAX_SUBCHANNEL) {
118 *devno = val;
119 *ssid = 0;
120 *cssid = 0;
121 rc = 0;
1da177e4 122 }
5b890987
ME
123 goto out;
124 }
1da177e4 125
5b890987
ME
126 /* new style */
127 str_work = str;
128 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
129 if (ret || (str_work[0] != '.'))
130 goto out;
131 str_work++;
132 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
133 if (ret || (str_work[0] != '.'))
134 goto out;
135 str_work++;
136 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
137 if (ret || (str_work[0] != '\0'))
138 goto out;
139
140 rc = 0;
141out:
142 if (rc && msgtrigger)
baebc70a
JP
143 pr_warn("%s is not a valid device for the cio_ignore kernel parameter\n",
144 str);
5b890987
ME
145
146 return rc;
147}
1da177e4 148
5b890987
ME
149static int blacklist_parse_parameters(char *str, range_action action,
150 int msgtrigger)
151{
12829126 152 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
5b890987
ME
153 int rc, totalrc;
154 char *parm;
155 range_action ra;
156
157 totalrc = 0;
158
159 while ((parm = strsep(&str, ","))) {
160 rc = 0;
161 ra = action;
162 if (*parm == '!') {
163 if (ra == add)
164 ra = free;
165 else
166 ra = add;
167 parm++;
168 }
169 if (strcmp(parm, "all") == 0) {
170 from_cssid = 0;
171 from_ssid = 0;
172 from = 0;
173 to_cssid = __MAX_CSSID;
174 to_ssid = __MAX_SSID;
175 to = __MAX_SUBCHANNEL;
0e6c83d1
SO
176 } else if (strcmp(parm, "ipldev") == 0) {
177 if (ipl_info.type == IPL_TYPE_CCW) {
178 from_cssid = 0;
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) {
183 from_cssid = 0;
184 from_ssid = ipl_info.data.fcp.dev_id.ssid;
185 from = ipl_info.data.fcp.dev_id.devno;
186 } else {
187 continue;
188 }
189 to_cssid = from_cssid;
190 to_ssid = from_ssid;
191 to = from;
d1eb16e6
SO
192 } else if (strcmp(parm, "condev") == 0) {
193 if (console_devno == -1)
194 continue;
195
196 from_cssid = to_cssid = 0;
197 from_ssid = to_ssid = 0;
198 from = to = console_devno;
5b890987
ME
199 } else {
200 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
201 &from_ssid, &from, msgtrigger);
202 if (!rc) {
203 if (parm != NULL)
204 rc = parse_busid(parm, &to_cssid,
205 &to_ssid, &to,
206 msgtrigger);
207 else {
208 to_cssid = from_cssid;
209 to_ssid = from_ssid;
210 to = from;
211 }
1da177e4
LT
212 }
213 }
5b890987
ME
214 if (!rc) {
215 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
216 msgtrigger);
217 if (rc)
ecf5d9ef 218 totalrc = -EINVAL;
5b890987 219 } else
ecf5d9ef 220 totalrc = -EINVAL;
1da177e4 221 }
5b890987
ME
222
223 return totalrc;
1da177e4
LT
224}
225
1da177e4
LT
226static int __init
227blacklist_setup (char *str)
228{
5b890987
ME
229 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
230 if (blacklist_parse_parameters(str, add, 1))
231 return 0;
232 return 1;
1da177e4
LT
233}
234
235__setup ("cio_ignore=", blacklist_setup);
236
237/* Checking if devices are blacklisted */
238
239/*
240 * Function: is_blacklisted
241 * Returns 1 if the given devicenumber can be found in the blacklist,
242 * otherwise 0.
243 * Used by validate_subchannel()
244 */
245int
fb6958a5 246is_blacklisted (int ssid, int devno)
1da177e4 247{
fb6958a5 248 return test_bit (devno, bl_dev[ssid]);
1da177e4
LT
249}
250
251#ifdef CONFIG_PROC_FS
1da177e4
LT
252/*
253 * Function: blacklist_parse_proc_parameters
254 * parse the stuff which is piped to /proc/cio_ignore
255 */
5b890987 256static int blacklist_parse_proc_parameters(char *buf)
1da177e4 257{
5b890987
ME
258 int rc;
259 char *parm;
260
261 parm = strsep(&buf, " ");
262
e6b25514 263 if (strcmp("free", parm) == 0) {
5b890987 264 rc = blacklist_parse_parameters(buf, free, 0);
e6b25514
PO
265 css_schedule_eval_all_unreg(0);
266 } else if (strcmp("add", parm) == 0)
5b890987 267 rc = blacklist_parse_parameters(buf, add, 0);
ecf5d9ef
PO
268 else if (strcmp("purge", parm) == 0)
269 return ccw_purge_blacklisted();
5b890987 270 else
ecf5d9ef 271 return -EINVAL;
1da177e4 272
5b890987
ME
273
274 return rc;
1da177e4
LT
275}
276
678a395b
CH
277/* Iterator struct for all devices. */
278struct ccwdev_iter {
279 int devno;
fb6958a5 280 int ssid;
678a395b
CH
281 int in_range;
282};
283
284static void *
285cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
1da177e4 286{
05d419b1 287 struct ccwdev_iter *iter = s->private;
678a395b 288
fb6958a5 289 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
678a395b 290 return NULL;
05d419b1 291 memset(iter, 0, sizeof(*iter));
fb6958a5
CH
292 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
293 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
678a395b
CH
294 return iter;
295}
296
297static void
298cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
299{
678a395b 300}
1da177e4 301
678a395b
CH
302static void *
303cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
304{
305 struct ccwdev_iter *iter;
306
fb6958a5 307 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
678a395b 308 return NULL;
3b793060 309 iter = it;
fb6958a5
CH
310 if (iter->devno == __MAX_SUBCHANNEL) {
311 iter->devno = 0;
312 iter->ssid++;
313 if (iter->ssid > __MAX_SSID)
314 return NULL;
315 } else
316 iter->devno++;
678a395b
CH
317 (*offset)++;
318 return iter;
1da177e4
LT
319}
320
678a395b
CH
321static int
322cio_ignore_proc_seq_show(struct seq_file *s, void *it)
323{
324 struct ccwdev_iter *iter;
325
3b793060 326 iter = it;
fb6958a5 327 if (!is_blacklisted(iter->ssid, iter->devno))
678a395b
CH
328 /* Not blacklisted, nothing to output. */
329 return 0;
330 if (!iter->in_range) {
331 /* First device in range. */
332 if ((iter->devno == __MAX_SUBCHANNEL) ||
c2f0b61d 333 !is_blacklisted(iter->ssid, iter->devno + 1)) {
678a395b 334 /* Singular device. */
c2f0b61d
JP
335 seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
336 return 0;
337 }
678a395b 338 iter->in_range = 1;
c2f0b61d
JP
339 seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
340 return 0;
678a395b
CH
341 }
342 if ((iter->devno == __MAX_SUBCHANNEL) ||
fb6958a5 343 !is_blacklisted(iter->ssid, iter->devno + 1)) {
678a395b
CH
344 /* Last device in range. */
345 iter->in_range = 0;
c2f0b61d 346 seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
678a395b
CH
347 }
348 return 0;
349}
350
351static ssize_t
352cio_ignore_write(struct file *file, const char __user *user_buf,
353 size_t user_len, loff_t *offset)
1da177e4
LT
354{
355 char *buf;
94cbc203 356 ssize_t rc, ret, i;
1da177e4 357
678a395b
CH
358 if (*offset)
359 return -EINVAL;
1da177e4
LT
360 if (user_len > 65536)
361 user_len = 65536;
dc8a5c99 362 buf = vzalloc(user_len + 1); /* maybe better use the stack? */
1da177e4
LT
363 if (buf == NULL)
364 return -ENOMEM;
5b890987 365
1da177e4 366 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
5b890987
ME
367 rc = -EFAULT;
368 goto out_free;
1da177e4 369 }
1da177e4 370
5b890987
ME
371 i = user_len - 1;
372 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
373 buf[i] = '\0';
374 i--;
375 }
376 ret = blacklist_parse_proc_parameters(buf);
377 if (ret)
ecf5d9ef 378 rc = ret;
5b890987
ME
379 else
380 rc = user_len;
1da177e4 381
5b890987 382out_free:
1da177e4 383 vfree (buf);
5b890987 384 return rc;
1da177e4
LT
385}
386
5c81cdbe 387static const struct seq_operations cio_ignore_proc_seq_ops = {
678a395b
CH
388 .start = cio_ignore_proc_seq_start,
389 .stop = cio_ignore_proc_seq_stop,
390 .next = cio_ignore_proc_seq_next,
391 .show = cio_ignore_proc_seq_show,
392};
393
394static int
395cio_ignore_proc_open(struct inode *inode, struct file *file)
396{
05d419b1
CB
397 return seq_open_private(file, &cio_ignore_proc_seq_ops,
398 sizeof(struct ccwdev_iter));
678a395b
CH
399}
400
d54b1fdb 401static const struct file_operations cio_ignore_proc_fops = {
678a395b
CH
402 .open = cio_ignore_proc_open,
403 .read = seq_read,
404 .llseek = seq_lseek,
05d419b1 405 .release = seq_release_private,
678a395b
CH
406 .write = cio_ignore_write,
407};
408
1da177e4
LT
409static int
410cio_ignore_proc_init (void)
411{
412 struct proc_dir_entry *entry;
413
8b594007
DL
414 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
415 &cio_ignore_proc_fops);
1da177e4 416 if (!entry)
a7fbf6bb 417 return -ENOENT;
a7fbf6bb 418 return 0;
1da177e4
LT
419}
420
421__initcall (cio_ignore_proc_init);
422
423#endif /* CONFIG_PROC_FS */