blacklist.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * drivers/s390/cio/blacklist.c
  3. * S/390 common I/O routines -- blacklisting of specific devices
  4. *
  5. * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Ingo Adlung (adlung@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. * Arnd Bergmann (arndb@de.ibm.com)
  10. */
  11. #define KMSG_COMPONENT "cio"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/ctype.h>
  18. #include <linux/device.h>
  19. #include <asm/cio.h>
  20. #include <asm/uaccess.h>
  21. #include "blacklist.h"
  22. #include "cio.h"
  23. #include "cio_debug.h"
  24. #include "css.h"
  25. #include "device.h"
  26. /*
  27. * "Blacklisting" of certain devices:
  28. * Device numbers given in the commandline as cio_ignore=... won't be known
  29. * to Linux.
  30. *
  31. * These can be single devices or ranges of devices
  32. */
  33. /* 65536 bits for each set to indicate if a devno is blacklisted or not */
  34. #define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  35. (8*sizeof(long)))
  36. static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
  37. typedef enum {add, free} range_action;
  38. /*
  39. * Function: blacklist_range
  40. * (Un-)blacklist the devices from-to
  41. */
  42. static int blacklist_range(range_action action, unsigned int from_ssid,
  43. unsigned int to_ssid, unsigned int from,
  44. unsigned int to, int msgtrigger)
  45. {
  46. if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
  47. if (msgtrigger)
  48. pr_warning("0.%x.%04x to 0.%x.%04x is not a valid "
  49. "range for cio_ignore\n", from_ssid, from,
  50. to_ssid, to);
  51. return 1;
  52. }
  53. while ((from_ssid < to_ssid) || ((from_ssid == to_ssid) &&
  54. (from <= to))) {
  55. if (action == add)
  56. set_bit(from, bl_dev[from_ssid]);
  57. else
  58. clear_bit(from, bl_dev[from_ssid]);
  59. from++;
  60. if (from > __MAX_SUBCHANNEL) {
  61. from_ssid++;
  62. from = 0;
  63. }
  64. }
  65. return 0;
  66. }
  67. static int pure_hex(char **cp, unsigned int *val, int min_digit,
  68. int max_digit, int max_val)
  69. {
  70. int diff;
  71. diff = 0;
  72. *val = 0;
  73. while (diff <= max_digit) {
  74. int value = hex_to_bin(**cp);
  75. if (value < 0)
  76. break;
  77. *val = *val * 16 + value;
  78. (*cp)++;
  79. diff++;
  80. }
  81. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  82. return 1;
  83. return 0;
  84. }
  85. static int parse_busid(char *str, unsigned int *cssid, unsigned int *ssid,
  86. unsigned int *devno, int msgtrigger)
  87. {
  88. char *str_work;
  89. int val, rc, ret;
  90. rc = 1;
  91. if (*str == '\0')
  92. goto out;
  93. /* old style */
  94. str_work = str;
  95. val = simple_strtoul(str, &str_work, 16);
  96. if (*str_work == '\0') {
  97. if (val <= __MAX_SUBCHANNEL) {
  98. *devno = val;
  99. *ssid = 0;
  100. *cssid = 0;
  101. rc = 0;
  102. }
  103. goto out;
  104. }
  105. /* new style */
  106. str_work = str;
  107. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  108. if (ret || (str_work[0] != '.'))
  109. goto out;
  110. str_work++;
  111. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  112. if (ret || (str_work[0] != '.'))
  113. goto out;
  114. str_work++;
  115. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  116. if (ret || (str_work[0] != '\0'))
  117. goto out;
  118. rc = 0;
  119. out:
  120. if (rc && msgtrigger)
  121. pr_warning("%s is not a valid device for the cio_ignore "
  122. "kernel parameter\n", str);
  123. return rc;
  124. }
  125. static int blacklist_parse_parameters(char *str, range_action action,
  126. int msgtrigger)
  127. {
  128. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  129. int rc, totalrc;
  130. char *parm;
  131. range_action ra;
  132. totalrc = 0;
  133. while ((parm = strsep(&str, ","))) {
  134. rc = 0;
  135. ra = action;
  136. if (*parm == '!') {
  137. if (ra == add)
  138. ra = free;
  139. else
  140. ra = add;
  141. parm++;
  142. }
  143. if (strcmp(parm, "all") == 0) {
  144. from_cssid = 0;
  145. from_ssid = 0;
  146. from = 0;
  147. to_cssid = __MAX_CSSID;
  148. to_ssid = __MAX_SSID;
  149. to = __MAX_SUBCHANNEL;
  150. } else {
  151. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  152. &from_ssid, &from, msgtrigger);
  153. if (!rc) {
  154. if (parm != NULL)
  155. rc = parse_busid(parm, &to_cssid,
  156. &to_ssid, &to,
  157. msgtrigger);
  158. else {
  159. to_cssid = from_cssid;
  160. to_ssid = from_ssid;
  161. to = from;
  162. }
  163. }
  164. }
  165. if (!rc) {
  166. rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
  167. msgtrigger);
  168. if (rc)
  169. totalrc = -EINVAL;
  170. } else
  171. totalrc = -EINVAL;
  172. }
  173. return totalrc;
  174. }
  175. static int __init
  176. blacklist_setup (char *str)
  177. {
  178. CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
  179. if (blacklist_parse_parameters(str, add, 1))
  180. return 0;
  181. return 1;
  182. }
  183. __setup ("cio_ignore=", blacklist_setup);
  184. /* Checking if devices are blacklisted */
  185. /*
  186. * Function: is_blacklisted
  187. * Returns 1 if the given devicenumber can be found in the blacklist,
  188. * otherwise 0.
  189. * Used by validate_subchannel()
  190. */
  191. int
  192. is_blacklisted (int ssid, int devno)
  193. {
  194. return test_bit (devno, bl_dev[ssid]);
  195. }
  196. #ifdef CONFIG_PROC_FS
  197. /*
  198. * Function: blacklist_parse_proc_parameters
  199. * parse the stuff which is piped to /proc/cio_ignore
  200. */
  201. static int blacklist_parse_proc_parameters(char *buf)
  202. {
  203. int rc;
  204. char *parm;
  205. parm = strsep(&buf, " ");
  206. if (strcmp("free", parm) == 0)
  207. rc = blacklist_parse_parameters(buf, free, 0);
  208. else if (strcmp("add", parm) == 0)
  209. rc = blacklist_parse_parameters(buf, add, 0);
  210. else if (strcmp("purge", parm) == 0)
  211. return ccw_purge_blacklisted();
  212. else
  213. return -EINVAL;
  214. css_schedule_reprobe();
  215. return rc;
  216. }
  217. /* Iterator struct for all devices. */
  218. struct ccwdev_iter {
  219. int devno;
  220. int ssid;
  221. int in_range;
  222. };
  223. static void *
  224. cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
  225. {
  226. struct ccwdev_iter *iter = s->private;
  227. if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
  228. return NULL;
  229. memset(iter, 0, sizeof(*iter));
  230. iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
  231. iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
  232. return iter;
  233. }
  234. static void
  235. cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
  236. {
  237. }
  238. static void *
  239. cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
  240. {
  241. struct ccwdev_iter *iter;
  242. if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
  243. return NULL;
  244. iter = it;
  245. if (iter->devno == __MAX_SUBCHANNEL) {
  246. iter->devno = 0;
  247. iter->ssid++;
  248. if (iter->ssid > __MAX_SSID)
  249. return NULL;
  250. } else
  251. iter->devno++;
  252. (*offset)++;
  253. return iter;
  254. }
  255. static int
  256. cio_ignore_proc_seq_show(struct seq_file *s, void *it)
  257. {
  258. struct ccwdev_iter *iter;
  259. iter = it;
  260. if (!is_blacklisted(iter->ssid, iter->devno))
  261. /* Not blacklisted, nothing to output. */
  262. return 0;
  263. if (!iter->in_range) {
  264. /* First device in range. */
  265. if ((iter->devno == __MAX_SUBCHANNEL) ||
  266. !is_blacklisted(iter->ssid, iter->devno + 1))
  267. /* Singular device. */
  268. return seq_printf(s, "0.%x.%04x\n",
  269. iter->ssid, iter->devno);
  270. iter->in_range = 1;
  271. return seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
  272. }
  273. if ((iter->devno == __MAX_SUBCHANNEL) ||
  274. !is_blacklisted(iter->ssid, iter->devno + 1)) {
  275. /* Last device in range. */
  276. iter->in_range = 0;
  277. return seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
  278. }
  279. return 0;
  280. }
  281. static ssize_t
  282. cio_ignore_write(struct file *file, const char __user *user_buf,
  283. size_t user_len, loff_t *offset)
  284. {
  285. char *buf;
  286. ssize_t rc, ret, i;
  287. if (*offset)
  288. return -EINVAL;
  289. if (user_len > 65536)
  290. user_len = 65536;
  291. buf = vzalloc(user_len + 1); /* maybe better use the stack? */
  292. if (buf == NULL)
  293. return -ENOMEM;
  294. if (strncpy_from_user (buf, user_buf, user_len) < 0) {
  295. rc = -EFAULT;
  296. goto out_free;
  297. }
  298. i = user_len - 1;
  299. while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
  300. buf[i] = '\0';
  301. i--;
  302. }
  303. ret = blacklist_parse_proc_parameters(buf);
  304. if (ret)
  305. rc = ret;
  306. else
  307. rc = user_len;
  308. out_free:
  309. vfree (buf);
  310. return rc;
  311. }
  312. static const struct seq_operations cio_ignore_proc_seq_ops = {
  313. .start = cio_ignore_proc_seq_start,
  314. .stop = cio_ignore_proc_seq_stop,
  315. .next = cio_ignore_proc_seq_next,
  316. .show = cio_ignore_proc_seq_show,
  317. };
  318. static int
  319. cio_ignore_proc_open(struct inode *inode, struct file *file)
  320. {
  321. return seq_open_private(file, &cio_ignore_proc_seq_ops,
  322. sizeof(struct ccwdev_iter));
  323. }
  324. static const struct file_operations cio_ignore_proc_fops = {
  325. .open = cio_ignore_proc_open,
  326. .read = seq_read,
  327. .llseek = seq_lseek,
  328. .release = seq_release_private,
  329. .write = cio_ignore_write,
  330. };
  331. static int
  332. cio_ignore_proc_init (void)
  333. {
  334. struct proc_dir_entry *entry;
  335. entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
  336. &cio_ignore_proc_fops);
  337. if (!entry)
  338. return -ENOENT;
  339. return 0;
  340. }
  341. __initcall (cio_ignore_proc_init);
  342. #endif /* CONFIG_PROC_FS */