blob: b3f791b2c1f8994de2287052275fc8853b3b946a [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)
Michael Ernste6d5a422008-12-25 13:39:36 +010054 pr_warning("0.%x.%04x to 0.%x.%04x is not a valid "
55 "range for cio_ignore\n", from_ssid, from,
56 to_ssid, to);
57
Michael Ernst5b890982008-05-07 09:22:55 +020058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Michael Ernst5b890982008-05-07 09:22:55 +020060
61 while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
62 (from <= to))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (action == add)
Michael Ernst5b890982008-05-07 09:22:55 +020064 set_bit(from, bl_dev[from_ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 else
Michael Ernst5b890982008-05-07 09:22:55 +020066 clear_bit(from, bl_dev[from_ssid]);
67 from++;
68 if (from > __MAX_SUBCHANNEL) {
69 from_ssid++;
70 from = 0;
71 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Michael Ernst5b890982008-05-07 09:22:55 +020077static int pure_hex(char **cp, unsigned int *val, int min_digit,
78 int max_digit, int max_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Michael Ernst5b890982008-05-07 09:22:55 +020080 int diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Michael Ernst5b890982008-05-07 09:22:55 +020082 diff = 0;
83 *val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Andy Shevchenkof2777072010-10-25 16:10:25 +020085 while (diff <= max_digit) {
86 int value = hex_to_bin(**cp);
Cornelia Huckfb6958a2006-01-06 00:19:25 -080087
Andy Shevchenkof2777072010-10-25 16:10:25 +020088 if (value < 0)
89 break;
Michael Ernst5b890982008-05-07 09:22:55 +020090 *val = *val * 16 + value;
91 (*cp)++;
92 diff++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Michael Ernst5b890982008-05-07 09:22:55 +020094
95 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
96 return 1;
97
98 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Cornelia Huck12829122008-06-10 10:03:19 +0200101static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
102 unsigned int *devno, int msgtrigger)
Michael Ernst5b890982008-05-07 09:22:55 +0200103{
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;
122 }
123 goto out;
124 }
125
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)
Michael Ernste6d5a422008-12-25 13:39:36 +0100143 pr_warning("%s is not a valid device for the cio_ignore "
144 "kernel parameter\n", str);
Michael Ernst5b890982008-05-07 09:22:55 +0200145
146 return rc;
147}
148
149static int blacklist_parse_parameters(char *str, range_action action,
150 int msgtrigger)
151{
Cornelia Huck12829122008-06-10 10:03:19 +0200152 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
Michael Ernst5b890982008-05-07 09:22:55 +0200153 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;
Sebastian Ott0e6c83d2013-04-30 17:16:17 +0200176 } 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;
Sebastian Ottd1eb16e2013-04-30 17:17:34 +0200192 } 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;
Michael Ernst5b890982008-05-07 09:22:55 +0200199 } 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 }
212 }
213 }
214 if (!rc) {
215 rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
216 msgtrigger);
217 if (rc)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200218 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200219 } else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200220 totalrc = -EINVAL;
Michael Ernst5b890982008-05-07 09:22:55 +0200221 }
222
223 return totalrc;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int __init
227blacklist_setup (char *str)
228{
Michael Ernst5b890982008-05-07 09:22:55 +0200229 CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
230 if (blacklist_parse_parameters(str, add, 1))
231 return 0;
232 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
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
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800246is_blacklisted (int ssid, int devno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800248 return test_bit (devno, bl_dev[ssid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * Function: blacklist_parse_proc_parameters
254 * parse the stuff which is piped to /proc/cio_ignore
255 */
Michael Ernst5b890982008-05-07 09:22:55 +0200256static int blacklist_parse_proc_parameters(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Michael Ernst5b890982008-05-07 09:22:55 +0200258 int rc;
259 char *parm;
260
261 parm = strsep(&buf, " ");
262
Peter Oberparleitere6b25512013-11-26 15:00:37 +0100263 if (strcmp("free", parm) == 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200264 rc = blacklist_parse_parameters(buf, free, 0);
Peter Oberparleitere6b25512013-11-26 15:00:37 +0100265 css_schedule_eval_all_unreg(0);
266 } else if (strcmp("add", parm) == 0)
Michael Ernst5b890982008-05-07 09:22:55 +0200267 rc = blacklist_parse_parameters(buf, add, 0);
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200268 else if (strcmp("purge", parm) == 0)
269 return ccw_purge_blacklisted();
Michael Ernst5b890982008-05-07 09:22:55 +0200270 else
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200271 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Michael Ernst5b890982008-05-07 09:22:55 +0200273
274 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Cornelia Huck678a3952006-01-06 00:19:24 -0800277/* Iterator struct for all devices. */
278struct ccwdev_iter {
279 int devno;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800280 int ssid;
Cornelia Huck678a3952006-01-06 00:19:24 -0800281 int in_range;
282};
283
284static void *
285cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200287 struct ccwdev_iter *iter = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800289 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800290 return NULL;
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200291 memset(iter, 0, sizeof(*iter));
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800292 iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
293 iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
Cornelia Huck678a3952006-01-06 00:19:24 -0800294 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Cornelia Huck678a3952006-01-06 00:19:24 -0800297static void
298cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
299{
Cornelia Huck678a3952006-01-06 00:19:24 -0800300}
301
302static void *
303cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
304{
305 struct ccwdev_iter *iter;
306
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800307 if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800308 return NULL;
Cornelia Huck3b793062006-01-06 00:19:26 -0800309 iter = it;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800310 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++;
Cornelia Huck678a3952006-01-06 00:19:24 -0800317 (*offset)++;
318 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) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800333 !is_blacklisted(iter->ssid, iter->devno + 1))
Cornelia Huck678a3952006-01-06 00:19:24 -0800334 /* Singular device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800335 return seq_printf(s, "0.%x.%04x\n",
336 iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800337 iter->in_range = 1;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800338 return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800339 }
340 if ((iter->devno == __MAX_SUBCHANNEL) ||
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800341 !is_blacklisted(iter->ssid, iter->devno + 1)) {
Cornelia Huck678a3952006-01-06 00:19:24 -0800342 /* Last device in range. */
343 iter->in_range = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800344 return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
Cornelia Huck678a3952006-01-06 00:19:24 -0800345 }
346 return 0;
347}
348
349static ssize_t
350cio_ignore_write(struct file *file, const char __user *user_buf,
351 size_t user_len, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 char *buf;
Sebastian Ott94cbc202009-03-26 15:24:16 +0100354 ssize_t rc, ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Cornelia Huck678a3952006-01-06 00:19:24 -0800356 if (*offset)
357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (user_len > 65536)
359 user_len = 65536;
Joe Perchesdc8a5c92011-05-28 10:36:21 -0700360 buf = vzalloc(user_len + 1); /* maybe better use the stack? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (buf == NULL)
362 return -ENOMEM;
Michael Ernst5b890982008-05-07 09:22:55 +0200363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (strncpy_from_user (buf, user_buf, user_len) < 0) {
Michael Ernst5b890982008-05-07 09:22:55 +0200365 rc = -EFAULT;
366 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Michael Ernst5b890982008-05-07 09:22:55 +0200369 i = user_len - 1;
370 while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
371 buf[i] = '\0';
372 i--;
373 }
374 ret = blacklist_parse_proc_parameters(buf);
375 if (ret)
Peter Oberparleiterecf5d9e2008-10-10 21:33:06 +0200376 rc = ret;
Michael Ernst5b890982008-05-07 09:22:55 +0200377 else
378 rc = user_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Michael Ernst5b890982008-05-07 09:22:55 +0200380out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 vfree (buf);
Michael Ernst5b890982008-05-07 09:22:55 +0200382 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100385static const struct seq_operations cio_ignore_proc_seq_ops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800386 .start = cio_ignore_proc_seq_start,
387 .stop = cio_ignore_proc_seq_stop,
388 .next = cio_ignore_proc_seq_next,
389 .show = cio_ignore_proc_seq_show,
390};
391
392static int
393cio_ignore_proc_open(struct inode *inode, struct file *file)
394{
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200395 return seq_open_private(file, &cio_ignore_proc_seq_ops,
396 sizeof(struct ccwdev_iter));
Cornelia Huck678a3952006-01-06 00:19:24 -0800397}
398
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800399static const struct file_operations cio_ignore_proc_fops = {
Cornelia Huck678a3952006-01-06 00:19:24 -0800400 .open = cio_ignore_proc_open,
401 .read = seq_read,
402 .llseek = seq_lseek,
Christian Borntraeger05d419b2009-10-06 10:34:00 +0200403 .release = seq_release_private,
Cornelia Huck678a3952006-01-06 00:19:24 -0800404 .write = cio_ignore_write,
405};
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static int
408cio_ignore_proc_init (void)
409{
410 struct proc_dir_entry *entry;
411
Denis V. Lunev8b594002008-04-29 01:02:20 -0700412 entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
413 &cio_ignore_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!entry)
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700415 return -ENOENT;
Cornelia Hucka7fbf6b2006-04-10 22:53:45 -0700416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419__initcall (cio_ignore_proc_init);
420
421#endif /* CONFIG_PROC_FS */