blacklist.c 8.2 KB

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