blacklist.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * S/390 common I/O routines -- blacklisting of specific devices
  3. *
  4. * Copyright IBM Corp. 1999, 2013
  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 <linux/uaccess.h>
  18. #include <asm/cio.h>
  19. #include <asm/ipl.h>
  20. #include "blacklist.h"
  21. #include "cio.h"
  22. #include "cio_debug.h"
  23. #include "css.h"
  24. #include "device.h"
  25. /*
  26. * "Blacklisting" of certain devices:
  27. * Device numbers given in the commandline as cio_ignore=... won't be known
  28. * to Linux.
  29. *
  30. * These can be single devices or ranges of devices
  31. */
  32. /* 65536 bits for each set to indicate if a devno is blacklisted or not */
  33. #define __BL_DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  34. (8*sizeof(long)))
  35. static unsigned long bl_dev[__MAX_SSID + 1][__BL_DEV_WORDS];
  36. typedef enum {add, free} range_action;
  37. /*
  38. * Function: blacklist_range
  39. * (Un-)blacklist the devices from-to
  40. */
  41. static int blacklist_range(range_action action, unsigned int from_ssid,
  42. unsigned int to_ssid, unsigned int from,
  43. unsigned int to, int msgtrigger)
  44. {
  45. if ((from_ssid > to_ssid) || ((from_ssid == to_ssid) && (from > to))) {
  46. if (msgtrigger)
  47. pr_warn("0.%x.%04x to 0.%x.%04x is not a valid range for cio_ignore\n",
  48. from_ssid, from, 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_warn("%s is not a valid device for the cio_ignore kernel parameter\n",
  120. 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 if (strcmp(parm, "ipldev") == 0) {
  149. if (ipl_info.type == IPL_TYPE_CCW) {
  150. from_cssid = 0;
  151. from_ssid = ipl_info.data.ccw.dev_id.ssid;
  152. from = ipl_info.data.ccw.dev_id.devno;
  153. } else if (ipl_info.type == IPL_TYPE_FCP ||
  154. ipl_info.type == IPL_TYPE_FCP_DUMP) {
  155. from_cssid = 0;
  156. from_ssid = ipl_info.data.fcp.dev_id.ssid;
  157. from = ipl_info.data.fcp.dev_id.devno;
  158. } else {
  159. continue;
  160. }
  161. to_cssid = from_cssid;
  162. to_ssid = from_ssid;
  163. to = from;
  164. } else if (strcmp(parm, "condev") == 0) {
  165. if (console_devno == -1)
  166. continue;
  167. from_cssid = to_cssid = 0;
  168. from_ssid = to_ssid = 0;
  169. from = to = console_devno;
  170. } else {
  171. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  172. &from_ssid, &from, msgtrigger);
  173. if (!rc) {
  174. if (parm != NULL)
  175. rc = parse_busid(parm, &to_cssid,
  176. &to_ssid, &to,
  177. msgtrigger);
  178. else {
  179. to_cssid = from_cssid;
  180. to_ssid = from_ssid;
  181. to = from;
  182. }
  183. }
  184. }
  185. if (!rc) {
  186. rc = blacklist_range(ra, from_ssid, to_ssid, from, to,
  187. msgtrigger);
  188. if (rc)
  189. totalrc = -EINVAL;
  190. } else
  191. totalrc = -EINVAL;
  192. }
  193. return totalrc;
  194. }
  195. static int __init
  196. blacklist_setup (char *str)
  197. {
  198. CIO_MSG_EVENT(6, "Reading blacklist parameters\n");
  199. if (blacklist_parse_parameters(str, add, 1))
  200. return 0;
  201. return 1;
  202. }
  203. __setup ("cio_ignore=", blacklist_setup);
  204. /* Checking if devices are blacklisted */
  205. /*
  206. * Function: is_blacklisted
  207. * Returns 1 if the given devicenumber can be found in the blacklist,
  208. * otherwise 0.
  209. * Used by validate_subchannel()
  210. */
  211. int
  212. is_blacklisted (int ssid, int devno)
  213. {
  214. return test_bit (devno, bl_dev[ssid]);
  215. }
  216. #ifdef CONFIG_PROC_FS
  217. /*
  218. * Function: blacklist_parse_proc_parameters
  219. * parse the stuff which is piped to /proc/cio_ignore
  220. */
  221. static int blacklist_parse_proc_parameters(char *buf)
  222. {
  223. int rc;
  224. char *parm;
  225. parm = strsep(&buf, " ");
  226. if (strcmp("free", parm) == 0) {
  227. rc = blacklist_parse_parameters(buf, free, 0);
  228. css_schedule_eval_all_unreg(0);
  229. } else if (strcmp("add", parm) == 0)
  230. rc = blacklist_parse_parameters(buf, add, 0);
  231. else if (strcmp("purge", parm) == 0)
  232. return ccw_purge_blacklisted();
  233. else
  234. return -EINVAL;
  235. return rc;
  236. }
  237. /* Iterator struct for all devices. */
  238. struct ccwdev_iter {
  239. int devno;
  240. int ssid;
  241. int in_range;
  242. };
  243. static void *
  244. cio_ignore_proc_seq_start(struct seq_file *s, loff_t *offset)
  245. {
  246. struct ccwdev_iter *iter = s->private;
  247. if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
  248. return NULL;
  249. memset(iter, 0, sizeof(*iter));
  250. iter->ssid = *offset / (__MAX_SUBCHANNEL + 1);
  251. iter->devno = *offset % (__MAX_SUBCHANNEL + 1);
  252. return iter;
  253. }
  254. static void
  255. cio_ignore_proc_seq_stop(struct seq_file *s, void *it)
  256. {
  257. }
  258. static void *
  259. cio_ignore_proc_seq_next(struct seq_file *s, void *it, loff_t *offset)
  260. {
  261. struct ccwdev_iter *iter;
  262. if (*offset >= (__MAX_SUBCHANNEL + 1) * (__MAX_SSID + 1))
  263. return NULL;
  264. iter = it;
  265. if (iter->devno == __MAX_SUBCHANNEL) {
  266. iter->devno = 0;
  267. iter->ssid++;
  268. if (iter->ssid > __MAX_SSID)
  269. return NULL;
  270. } else
  271. iter->devno++;
  272. (*offset)++;
  273. return iter;
  274. }
  275. static int
  276. cio_ignore_proc_seq_show(struct seq_file *s, void *it)
  277. {
  278. struct ccwdev_iter *iter;
  279. iter = it;
  280. if (!is_blacklisted(iter->ssid, iter->devno))
  281. /* Not blacklisted, nothing to output. */
  282. return 0;
  283. if (!iter->in_range) {
  284. /* First device in range. */
  285. if ((iter->devno == __MAX_SUBCHANNEL) ||
  286. !is_blacklisted(iter->ssid, iter->devno + 1)) {
  287. /* Singular device. */
  288. seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
  289. return 0;
  290. }
  291. iter->in_range = 1;
  292. seq_printf(s, "0.%x.%04x-", iter->ssid, iter->devno);
  293. return 0;
  294. }
  295. if ((iter->devno == __MAX_SUBCHANNEL) ||
  296. !is_blacklisted(iter->ssid, iter->devno + 1)) {
  297. /* Last device in range. */
  298. iter->in_range = 0;
  299. seq_printf(s, "0.%x.%04x\n", iter->ssid, iter->devno);
  300. }
  301. return 0;
  302. }
  303. static ssize_t
  304. cio_ignore_write(struct file *file, const char __user *user_buf,
  305. size_t user_len, loff_t *offset)
  306. {
  307. char *buf;
  308. ssize_t rc, ret, i;
  309. if (*offset)
  310. return -EINVAL;
  311. if (user_len > 65536)
  312. user_len = 65536;
  313. buf = vzalloc(user_len + 1); /* maybe better use the stack? */
  314. if (buf == NULL)
  315. return -ENOMEM;
  316. if (strncpy_from_user (buf, user_buf, user_len) < 0) {
  317. rc = -EFAULT;
  318. goto out_free;
  319. }
  320. i = user_len - 1;
  321. while ((i >= 0) && (isspace(buf[i]) || (buf[i] == 0))) {
  322. buf[i] = '\0';
  323. i--;
  324. }
  325. ret = blacklist_parse_proc_parameters(buf);
  326. if (ret)
  327. rc = ret;
  328. else
  329. rc = user_len;
  330. out_free:
  331. vfree (buf);
  332. return rc;
  333. }
  334. static const struct seq_operations cio_ignore_proc_seq_ops = {
  335. .start = cio_ignore_proc_seq_start,
  336. .stop = cio_ignore_proc_seq_stop,
  337. .next = cio_ignore_proc_seq_next,
  338. .show = cio_ignore_proc_seq_show,
  339. };
  340. static int
  341. cio_ignore_proc_open(struct inode *inode, struct file *file)
  342. {
  343. return seq_open_private(file, &cio_ignore_proc_seq_ops,
  344. sizeof(struct ccwdev_iter));
  345. }
  346. static const struct file_operations cio_ignore_proc_fops = {
  347. .open = cio_ignore_proc_open,
  348. .read = seq_read,
  349. .llseek = seq_lseek,
  350. .release = seq_release_private,
  351. .write = cio_ignore_write,
  352. };
  353. static int
  354. cio_ignore_proc_init (void)
  355. {
  356. struct proc_dir_entry *entry;
  357. entry = proc_create("cio_ignore", S_IFREG | S_IRUGO | S_IWUSR, NULL,
  358. &cio_ignore_proc_fops);
  359. if (!entry)
  360. return -ENOENT;
  361. return 0;
  362. }
  363. __initcall (cio_ignore_proc_init);
  364. #endif /* CONFIG_PROC_FS */