blob: 4e9f794176d3511fc461af23b00aea5b174fb6c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S/390 common I/O routines -- blacklisting of specific devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ott0e6c83d2013-04-30 17:16:17 +02004 * Copyright IBM Corp. 1999, 2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Ingo Adlung (adlung@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08006 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Arnd Bergmann (arndb@de.ibm.com)
8 */
9
Michael Ernste6d5a422008-12-25 13:39:36 +010010#define KMSG_COMPONENT "cio"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
Sebastian Ott0e6c83d2013-04-30 17:16:17 +020021#include <asm/cio.h>
22#include <asm/ipl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "blacklist.h"
25#include "cio.h"
26#include "cio_debug.h"
27#include "css.h"
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +020028#include "device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * "Blacklisting" of certain devices:
32 * Device numbers given in the commandline as cio_ignore=... won't be known
33 * to Linux.
34 *
35 * These can be single devices or ranges of devices
36 */
37
Cornelia Huckfb6958a2006-01-06 00:19:25 -080038/* 65536 bits for each set to indicate if a devno is blacklisted or not */
Cornelia Hucka8237fc2006-01-06 00:19:21 -080039#define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 (8*sizeof(long)))
Cornelia Huckfb6958a2006-01-06 00:19:25 -080041static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042typedef enum {add, free} range_action;
43
44/*
45 * Function: blacklist_range
46 * (Un-)blacklist the devices from-to
47 */
Michael Ernst5b890982008-05-07 09:22:55 +020048static int blacklist_range(range_action action, unsigned int from_ssid,
49 unsigned int to_ssid, unsigned int from,
50 unsigned int to, int msgtrigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Michael Ernst5b890982008-05-07 09:22:55 +020052 if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
53 if (msgtrigger)
Joe Perchesbaebc702016-03-03 20:49:57 -080054 pr_warn("0.%x.%04x to 0.%x.%04x is not a valid range for cio_ignore\n",
55 from_ssid, from, to_ssid, to);
Michael Ernste6d5a422008-12-25 13:39:36 +010056
Michael Ernst5b890982008-05-07 09:22:55 +020057 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Michael Ernst5b890982008-05-07 09:22:55 +020059
60 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
61 (from <= to))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (action == add)
Michael Ernst5b890982008-05-07 09:22:55 +020063 set_bit(from, bl_dev[from_ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 else
Michael Ernst5b890982008-05-07 09:22:55 +020065 clear_bit(from, bl_dev[from_ssid]);
66 from++;
67 if (from > __MAX_SUBCHANNEL) {
68 from_ssid++;
69 from = 0;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Michael Ernst5b890982008-05-07 09:22:55 +020076static int pure_hex(char **cp, unsigned int *val, int min_digit,
77 int max_digit, int max_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Michael Ernst5b890982008-05-07 09:22:55 +020079 int diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Michael Ernst5b890982008-05-07 09:22:55 +020081 diff = 0;
82 *val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Andy Shevchenkof2777072010-10-25 16:10:25 +020084 while (diff <= max_digit) {
85 int value = hex_to_bin(**cp);
Cornelia Huckfb6958a2006-01-06 00:19:25 -080086
Andy Shevchenkof2777072010-10-25 16:10:25 +020087 if (value < 0)
88 break;
Michael Ernst5b890982008-05-07 09:22:55 +020089 *val = *val * 16 + value;
90 (*cp)++;
91 diff++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Michael Ernst5b890982008-05-07 09:22:55 +020093
94 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
95 return 1;
96
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Cornelia Huck12829122008-06-10 10:03:19 +0200100static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
101 unsigned int *devno, int msgtrigger)
Michael Ernst5b890982008-05-07 09:22:55 +0200102{
103 char *str_work;
104 int val, rc, ret;
105
106 rc = 1;
107
108 if (*str == '\0')
109 goto out;
110
111 /* old style */
112 str_work = str;
113 val = simple_strtoul(str, &str_work, 16);
114
115 if (*str_work == '\0') {
116 if (val <= __MAX_SUBCHANNEL) {
117 *devno = val;
118 *ssid = 0;
119 *cssid = 0;
120 rc = 0;
121 }
122 goto out;
123 }
124
125 /* new style */
126 str_work = str;
127 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
128 if (ret || (str_work[0] != '.'))
129 goto out;
130 str_work++;
131 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
132 if (ret || (str_work[0] != '.'))
133 goto out;
134 str_work++;
135 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
136 if (ret || (str_work[0] != '\0'))
137 goto out;
138
139 rc = 0;
140out:
141 if (rc && msgtrigger)
Joe Perchesbaebc702016-03-03 20:49:57 -0800142 pr_warn("%s is not a valid device for the cio_ignore kernel parameter\n",
143 str);
Michael Ernst5b890982008-05-07 09:22:55 +0200144
145 return rc;
146}
147
148static int blacklist_parse_parameters(char *str, range_action action,
149 int msgtrigger)
150{
Cornelia Huck12829122008-06-10 10:03:19 +0200151 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
Michael Ernst5b890982008-05-07 09:22:55 +0200152 int rc, totalrc;
153 char *parm;
154 range_action ra;
155
156 totalrc = 0;
157
158 while ((parm = strsep(&str, ","))) {
159 rc = 0;
160 ra = action;
161 if (*parm == '!') {
162 if (ra == add)
163 ra = free;
164 else
165 ra = add;
166 parm++;
167 }
168 if (strcmp(parm, "all") == 0) {
169 from_cssid = 0;
170 from_ssid = 0;
171 from = 0;
172 to_cssid = __MAX_CSSID;
173 to_ssid = __MAX_SSID;
174 to = __MAX_SUBCHANNEL;
Sebastian Ott0e6c83d2013-04-30 17:16:17 +0200175 } else if (strcmp(parm, "ipldev") == 0) {
176 if (ipl_info.type == IPL_TYPE_CCW) {
177 from_cssid = 0;
178 from_ssid = ipl_info.data.ccw.dev_id.ssid;
179 from = ipl_info.data.ccw.dev_id.devno;
180 } else if (ipl_info.type == IPL_TYPE_FCP ||
181 ipl_info.type == IPL_TYPE_FCP_DUMP) {
182 from_cssid = 0;
183 from_ssid = ipl_info.data.fcp.dev_id.ssid;
184 from = ipl_info.data.fcp.dev_id.devno;
185 } else {
186 continue;
187 }
188 to_cssid = from_cssid;
189 to_ssid = from_ssid;
190 to = from;
Sebastian Ottd1eb16e2013-04-30 17:17:34 +0200191 } else if (strcmp(parm, "condev") == 0) {
192 if (console_devno == -1)
193 continue;
194
195 from_cssid = to_cssid = 0;
196 from_ssid = to_ssid = 0;
197 from = to = console_devno;
Michael Ernst5b890982008-05-07 09:22:55 +0200198 } else {
199 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
200 &from_ssid, &from, msgtrigger);
201 if (!rc) {
202 if (parm != NULL)
203 rc = parse_busid(parm, &to_cssid,
204 &to_ssid, &to,
205 msgtrigger);
206 else {
207 to_cssid = from_cssid;
208 to_ssid = from_ssid;
209 to = from;
210 }
211 }
212 }
213 if (!rc) {
214 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
215 msgtrigger);
216 if (rc)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200217 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200218 } else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200219 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200220 }
221
222 return totalrc;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int __init
226blacklist_setup (char *str)
227{
Michael Ernst5b890982008-05-07 09:22:55 +0200228 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
229 if (blacklist_parse_parameters(str, add, 1))
230 return 0;
231 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234__setup ("cio_ignore=", blacklist_setup);
235
236/* Checking if devices are blacklisted */
237
238/*
239 * Function: is_blacklisted
240 * Returns 1 if the given devicenumber can be found in the blacklist,
241 * otherwise 0.
242 * Used by validate_subchannel()
243 */
244int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800245is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800247 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Function: blacklist_parse_proc_parameters
253 * parse the stuff which is piped to /proc/cio_ignore
254 */
Michael Ernst5b890982008-05-07 09:22:55 +0200255static int blacklist_parse_proc_parameters(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Michael Ernst5b890982008-05-07 09:22:55 +0200257 int rc;
258 char *parm;
259
260 parm = strsep(&buf, " ");
261
Peter Oberparleitere6b25512013-11-26 15:00:37 +0100262 if (strcmp("free", parm) == 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200263 rc = blacklist_parse_parameters(buf, free, 0);
Peter Oberparleitere6b25512013-11-26 15:00:37 +0100264 css_schedule_eval_all_unreg(0);
265 } else if (strcmp("add", parm) == 0)
Michael Ernst5b890982008-05-07 09:22:55 +0200266 rc = blacklist_parse_parameters(buf, add, 0);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200267 else if (strcmp("purge", parm) == 0)
268 return ccw_purge_blacklisted();
Michael Ernst5b890982008-05-07 09:22:55 +0200269 else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Michael Ernst5b890982008-05-07 09:22:55 +0200272
273 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Cornelia Huck678a3952006-01-06 00:19:24 -0800276/* Iterator struct for all devices. */
277struct ccwdev_iter {
278 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800279 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800280 int in_range;
281};
282
283static void *
284cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200286 struct ccwdev_iter *iter = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800288 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800289 return NULL;
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200290 memset(iter, 0, sizeof(*iter));
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800291 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
292 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800293 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Cornelia Huck678a3952006-01-06 00:19:24 -0800296static void
297cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
298{
Cornelia Huck678a3952006-01-06 00:19:24 -0800299}
300
301static void *
302cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
303{
304 struct ccwdev_iter *iter;
Vasily Averincfbd8af2020-01-24 08:48:55 +0300305 loff_t p = *offset;
Cornelia Huck678a3952006-01-06 00:19:24 -0800306
Vasily Averincfbd8af2020-01-24 08:48:55 +0300307 (*offset)++;
308 if (p >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800309 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800310 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800311 if (iter->devno == __MAX_SUBCHANNEL) {
312 iter->devno = 0;
313 iter->ssid++;
314 if (iter->ssid > __MAX_SSID)
315 return NULL;
316 } else
317 iter->devno++;
Cornelia Huck678a3952006-01-06 00:19:24 -0800318 return iter;
319}
320
321static int
322cio_ignore_proc_seq_show(struct seq_file *s, void *it)
323{
324 struct ccwdev_iter *iter;
325
Cornelia Huck3b793062006-01-06 00:19:26 -0800326 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800327 if (!is_blacklisted(iter->ssid, iter->devno))
Cornelia Huck678a3952006-01-06 00:19:24 -0800328 /* Not blacklisted, nothing to output. */
329 return 0;
330 if (!iter->in_range) {
331 /* First device in range. */
332 if ((iter->devno == __MAX_SUBCHANNEL) ||
Joe Perchesc2f0b612015-04-15 16:18:14 -0700333 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800334 /* Singular device. */
Joe Perchesc2f0b612015-04-15 16:18:14 -0700335 seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
336 return 0;
337 }
Cornelia Huck678a3952006-01-06 00:19:24 -0800338 iter->in_range = 1;
Joe Perchesc2f0b612015-04-15 16:18:14 -0700339 seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
340 return 0;
Cornelia Huck678a3952006-01-06 00:19:24 -0800341 }
342 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800343 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800344 /* Last device in range. */
345 iter->in_range = 0;
Joe Perchesc2f0b612015-04-15 16:18:14 -0700346 seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800347 }
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 char *buf;
Sebastian Ott94cbc202009-03-26 15:24:16 +0100356 ssize_t rc, ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Cornelia Huck678a3952006-01-06 00:19:24 -0800358 if (*offset)
359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (user_len > 65536)
361 user_len = 65536;
Joe Perchesdc8a5c92011-05-28 10:36:21 -0700362 buf = vzalloc(user_len + 1); /* maybe better use the stack? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (buf == NULL)
364 return -ENOMEM;
Michael Ernst5b890982008-05-07 09:22:55 +0200365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200367 rc = -EFAULT;
368 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Michael Ernst5b890982008-05-07 09:22:55 +0200371 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)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200378 rc = ret;
Michael Ernst5b890982008-05-07 09:22:55 +0200379 else
380 rc = user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Michael Ernst5b890982008-05-07 09:22:55 +0200382out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 vfree (buf);
Michael Ernst5b890982008-05-07 09:22:55 +0200384 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100387static const struct seq_operations cio_ignore_proc_seq_ops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800388 .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{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200397 return seq_open_private(file, &cio_ignore_proc_seq_ops,
398 sizeof(struct ccwdev_iter));
Cornelia Huck678a3952006-01-06 00:19:24 -0800399}
400
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800401static const struct file_operations cio_ignore_proc_fops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800402 .open = cio_ignore_proc_open,
403 .read = seq_read,
404 .llseek = seq_lseek,
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200405 .release = seq_release_private,
Cornelia Huck678a3952006-01-06 00:19:24 -0800406 .write = cio_ignore_write,
407};
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static int
410cio_ignore_proc_init (void)
411{
412 struct proc_dir_entry *entry;
413
Denis V. Lunev8b594002008-04-29 01:02:20 -0700414 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
415 &cio_ignore_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700417 return -ENOENT;
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421__initcall (cio_ignore_proc_init);
422
423#endif /* CONFIG_PROC_FS */