scsi_proc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * linux/drivers/scsi/scsi_proc.c
  3. *
  4. * The functions in this file provide an interface between
  5. * the PROC file system and the SCSI device drivers
  6. * It is mainly used for debugging, statistics and to pass
  7. * information directly to the lowlevel driver.
  8. *
  9. * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
  10. * Version: 0.99.8 last change: 95/09/13
  11. *
  12. * generic command parser provided by:
  13. * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
  14. *
  15. * generic_proc_info() support of xxxx_info() by:
  16. * Michael A. Griffith <grif@acm.org>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/errno.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/mutex.h>
  27. #include <linux/gfp.h>
  28. #include <asm/uaccess.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport.h>
  33. #include "scsi_priv.h"
  34. #include "scsi_logging.h"
  35. /* 4K page size, but our output routines, use some slack for overruns */
  36. #define PROC_BLOCK_SIZE (3*1024)
  37. static struct proc_dir_entry *proc_scsi;
  38. /* Protect sht->present and sht->proc_dir */
  39. static DEFINE_MUTEX(global_host_template_mutex);
  40. /**
  41. * proc_scsi_read - handle read from /proc by calling host's proc_info() command
  42. * @buffer: passed to proc_info
  43. * @start: passed to proc_info
  44. * @offset: passed to proc_info
  45. * @length: passed to proc_info
  46. * @eof: returns whether length read was less than requested
  47. * @data: pointer to a &struct Scsi_Host
  48. */
  49. static int proc_scsi_read(char *buffer, char **start, off_t offset,
  50. int length, int *eof, void *data)
  51. {
  52. struct Scsi_Host *shost = data;
  53. int n;
  54. n = shost->hostt->proc_info(shost, buffer, start, offset, length, 0);
  55. *eof = (n < length);
  56. return n;
  57. }
  58. /**
  59. * proc_scsi_write_proc - Handle write to /proc by calling host's proc_info()
  60. * @file: not used
  61. * @buf: source of data to write.
  62. * @count: number of bytes (at most PROC_BLOCK_SIZE) to write.
  63. * @data: pointer to &struct Scsi_Host
  64. */
  65. static int proc_scsi_write_proc(struct file *file, const char __user *buf,
  66. unsigned long count, void *data)
  67. {
  68. struct Scsi_Host *shost = data;
  69. ssize_t ret = -ENOMEM;
  70. char *page;
  71. char *start;
  72. if (count > PROC_BLOCK_SIZE)
  73. return -EOVERFLOW;
  74. page = (char *)__get_free_page(GFP_KERNEL);
  75. if (page) {
  76. ret = -EFAULT;
  77. if (copy_from_user(page, buf, count))
  78. goto out;
  79. ret = shost->hostt->proc_info(shost, page, &start, 0, count, 1);
  80. }
  81. out:
  82. free_page((unsigned long)page);
  83. return ret;
  84. }
  85. static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. struct Scsi_Host *shost = PDE(file_inode(file))->data;
  89. ssize_t ret = -ENOMEM;
  90. char *page;
  91. if (count > PROC_BLOCK_SIZE)
  92. return -EOVERFLOW;
  93. if (!shost->hostt->write_info)
  94. return -EINVAL;
  95. page = (char *)__get_free_page(GFP_KERNEL);
  96. if (page) {
  97. ret = -EFAULT;
  98. if (copy_from_user(page, buf, count))
  99. goto out;
  100. ret = shost->hostt->write_info(shost, page, count);
  101. }
  102. out:
  103. free_page((unsigned long)page);
  104. return ret;
  105. }
  106. static int proc_scsi_show(struct seq_file *m, void *v)
  107. {
  108. struct Scsi_Host *shost = m->private;
  109. return shost->hostt->show_info(m, shost);
  110. }
  111. static int proc_scsi_host_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, proc_scsi_show, PDE(inode)->data);
  114. }
  115. static const struct file_operations proc_scsi_fops = {
  116. .open = proc_scsi_host_open,
  117. .read = seq_read,
  118. .llseek = seq_lseek,
  119. .write = proc_scsi_host_write
  120. };
  121. /**
  122. * scsi_proc_hostdir_add - Create directory in /proc for a scsi host
  123. * @sht: owner of this directory
  124. *
  125. * Sets sht->proc_dir to the new directory.
  126. */
  127. void scsi_proc_hostdir_add(struct scsi_host_template *sht)
  128. {
  129. if (!sht->proc_info && !sht->show_info)
  130. return;
  131. mutex_lock(&global_host_template_mutex);
  132. if (!sht->present++) {
  133. sht->proc_dir = proc_mkdir(sht->proc_name, proc_scsi);
  134. if (!sht->proc_dir)
  135. printk(KERN_ERR "%s: proc_mkdir failed for %s\n",
  136. __func__, sht->proc_name);
  137. }
  138. mutex_unlock(&global_host_template_mutex);
  139. }
  140. /**
  141. * scsi_proc_hostdir_rm - remove directory in /proc for a scsi host
  142. * @sht: owner of directory
  143. */
  144. void scsi_proc_hostdir_rm(struct scsi_host_template *sht)
  145. {
  146. if (!sht->proc_info && !sht->show_info)
  147. return;
  148. mutex_lock(&global_host_template_mutex);
  149. if (!--sht->present && sht->proc_dir) {
  150. remove_proc_entry(sht->proc_name, proc_scsi);
  151. sht->proc_dir = NULL;
  152. }
  153. mutex_unlock(&global_host_template_mutex);
  154. }
  155. /**
  156. * scsi_proc_host_add - Add entry for this host to appropriate /proc dir
  157. * @shost: host to add
  158. */
  159. void scsi_proc_host_add(struct Scsi_Host *shost)
  160. {
  161. struct scsi_host_template *sht = shost->hostt;
  162. struct proc_dir_entry *p;
  163. char name[10];
  164. if (!sht->proc_dir)
  165. return;
  166. sprintf(name,"%d", shost->host_no);
  167. if (sht->show_info) {
  168. p = proc_create_data(name, S_IRUGO | S_IWUSR,
  169. sht->proc_dir, &proc_scsi_fops, shost);
  170. if (!p)
  171. goto Fail;
  172. return;
  173. }
  174. p = create_proc_read_entry(name, S_IFREG | S_IRUGO | S_IWUSR,
  175. sht->proc_dir, proc_scsi_read, shost);
  176. if (p) {
  177. p->write_proc = proc_scsi_write_proc;
  178. return;
  179. }
  180. Fail:
  181. printk(KERN_ERR "%s: Failed to register host %d in"
  182. "%s\n", __func__, shost->host_no,
  183. sht->proc_name);
  184. }
  185. /**
  186. * scsi_proc_host_rm - remove this host's entry from /proc
  187. * @shost: which host
  188. */
  189. void scsi_proc_host_rm(struct Scsi_Host *shost)
  190. {
  191. char name[10];
  192. if (!shost->hostt->proc_dir)
  193. return;
  194. sprintf(name,"%d", shost->host_no);
  195. remove_proc_entry(name, shost->hostt->proc_dir);
  196. }
  197. /**
  198. * proc_print_scsidevice - return data about this host
  199. * @dev: A scsi device
  200. * @data: &struct seq_file to output to.
  201. *
  202. * Description: prints Host, Channel, Id, Lun, Vendor, Model, Rev, Type,
  203. * and revision.
  204. */
  205. static int proc_print_scsidevice(struct device *dev, void *data)
  206. {
  207. struct scsi_device *sdev;
  208. struct seq_file *s = data;
  209. int i;
  210. if (!scsi_is_sdev_device(dev))
  211. goto out;
  212. sdev = to_scsi_device(dev);
  213. seq_printf(s,
  214. "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n Vendor: ",
  215. sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
  216. for (i = 0; i < 8; i++) {
  217. if (sdev->vendor[i] >= 0x20)
  218. seq_printf(s, "%c", sdev->vendor[i]);
  219. else
  220. seq_printf(s, " ");
  221. }
  222. seq_printf(s, " Model: ");
  223. for (i = 0; i < 16; i++) {
  224. if (sdev->model[i] >= 0x20)
  225. seq_printf(s, "%c", sdev->model[i]);
  226. else
  227. seq_printf(s, " ");
  228. }
  229. seq_printf(s, " Rev: ");
  230. for (i = 0; i < 4; i++) {
  231. if (sdev->rev[i] >= 0x20)
  232. seq_printf(s, "%c", sdev->rev[i]);
  233. else
  234. seq_printf(s, " ");
  235. }
  236. seq_printf(s, "\n");
  237. seq_printf(s, " Type: %s ", scsi_device_type(sdev->type));
  238. seq_printf(s, " ANSI SCSI revision: %02x",
  239. sdev->scsi_level - (sdev->scsi_level > 1));
  240. if (sdev->scsi_level == 2)
  241. seq_printf(s, " CCS\n");
  242. else
  243. seq_printf(s, "\n");
  244. out:
  245. return 0;
  246. }
  247. /**
  248. * scsi_add_single_device - Respond to user request to probe for/add device
  249. * @host: user-supplied decimal integer
  250. * @channel: user-supplied decimal integer
  251. * @id: user-supplied decimal integer
  252. * @lun: user-supplied decimal integer
  253. *
  254. * Description: called by writing "scsi add-single-device" to /proc/scsi/scsi.
  255. *
  256. * does scsi_host_lookup() and either user_scan() if that transport
  257. * type supports it, or else scsi_scan_host_selected()
  258. *
  259. * Note: this seems to be aimed exclusively at SCSI parallel busses.
  260. */
  261. static int scsi_add_single_device(uint host, uint channel, uint id, uint lun)
  262. {
  263. struct Scsi_Host *shost;
  264. int error = -ENXIO;
  265. shost = scsi_host_lookup(host);
  266. if (!shost)
  267. return error;
  268. if (shost->transportt->user_scan)
  269. error = shost->transportt->user_scan(shost, channel, id, lun);
  270. else
  271. error = scsi_scan_host_selected(shost, channel, id, lun, 1);
  272. scsi_host_put(shost);
  273. return error;
  274. }
  275. /**
  276. * scsi_remove_single_device - Respond to user request to remove a device
  277. * @host: user-supplied decimal integer
  278. * @channel: user-supplied decimal integer
  279. * @id: user-supplied decimal integer
  280. * @lun: user-supplied decimal integer
  281. *
  282. * Description: called by writing "scsi remove-single-device" to
  283. * /proc/scsi/scsi. Does a scsi_device_lookup() and scsi_remove_device()
  284. */
  285. static int scsi_remove_single_device(uint host, uint channel, uint id, uint lun)
  286. {
  287. struct scsi_device *sdev;
  288. struct Scsi_Host *shost;
  289. int error = -ENXIO;
  290. shost = scsi_host_lookup(host);
  291. if (!shost)
  292. return error;
  293. sdev = scsi_device_lookup(shost, channel, id, lun);
  294. if (sdev) {
  295. scsi_remove_device(sdev);
  296. scsi_device_put(sdev);
  297. error = 0;
  298. }
  299. scsi_host_put(shost);
  300. return error;
  301. }
  302. /**
  303. * proc_scsi_write - handle writes to /proc/scsi/scsi
  304. * @file: not used
  305. * @buf: buffer to write
  306. * @length: length of buf, at most PAGE_SIZE
  307. * @ppos: not used
  308. *
  309. * Description: this provides a legacy mechanism to add or remove devices by
  310. * Host, Channel, ID, and Lun. To use,
  311. * "echo 'scsi add-single-device 0 1 2 3' > /proc/scsi/scsi" or
  312. * "echo 'scsi remove-single-device 0 1 2 3' > /proc/scsi/scsi" with
  313. * "0 1 2 3" replaced by the Host, Channel, Id, and Lun.
  314. *
  315. * Note: this seems to be aimed at parallel SCSI. Most modern busses (USB,
  316. * SATA, Firewire, Fibre Channel, etc) dynamically assign these values to
  317. * provide a unique identifier and nothing more.
  318. */
  319. static ssize_t proc_scsi_write(struct file *file, const char __user *buf,
  320. size_t length, loff_t *ppos)
  321. {
  322. int host, channel, id, lun;
  323. char *buffer, *p;
  324. int err;
  325. if (!buf || length > PAGE_SIZE)
  326. return -EINVAL;
  327. buffer = (char *)__get_free_page(GFP_KERNEL);
  328. if (!buffer)
  329. return -ENOMEM;
  330. err = -EFAULT;
  331. if (copy_from_user(buffer, buf, length))
  332. goto out;
  333. err = -EINVAL;
  334. if (length < PAGE_SIZE)
  335. buffer[length] = '\0';
  336. else if (buffer[PAGE_SIZE-1])
  337. goto out;
  338. /*
  339. * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
  340. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  341. */
  342. if (!strncmp("scsi add-single-device", buffer, 22)) {
  343. p = buffer + 23;
  344. host = simple_strtoul(p, &p, 0);
  345. channel = simple_strtoul(p + 1, &p, 0);
  346. id = simple_strtoul(p + 1, &p, 0);
  347. lun = simple_strtoul(p + 1, &p, 0);
  348. err = scsi_add_single_device(host, channel, id, lun);
  349. /*
  350. * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
  351. * with "0 1 2 3" replaced by your "Host Channel Id Lun".
  352. */
  353. } else if (!strncmp("scsi remove-single-device", buffer, 25)) {
  354. p = buffer + 26;
  355. host = simple_strtoul(p, &p, 0);
  356. channel = simple_strtoul(p + 1, &p, 0);
  357. id = simple_strtoul(p + 1, &p, 0);
  358. lun = simple_strtoul(p + 1, &p, 0);
  359. err = scsi_remove_single_device(host, channel, id, lun);
  360. }
  361. /*
  362. * convert success returns so that we return the
  363. * number of bytes consumed.
  364. */
  365. if (!err)
  366. err = length;
  367. out:
  368. free_page((unsigned long)buffer);
  369. return err;
  370. }
  371. static int always_match(struct device *dev, void *data)
  372. {
  373. return 1;
  374. }
  375. static inline struct device *next_scsi_device(struct device *start)
  376. {
  377. struct device *next = bus_find_device(&scsi_bus_type, start, NULL,
  378. always_match);
  379. put_device(start);
  380. return next;
  381. }
  382. static void *scsi_seq_start(struct seq_file *sfile, loff_t *pos)
  383. {
  384. struct device *dev = NULL;
  385. loff_t n = *pos;
  386. while ((dev = next_scsi_device(dev))) {
  387. if (!n--)
  388. break;
  389. sfile->private++;
  390. }
  391. return dev;
  392. }
  393. static void *scsi_seq_next(struct seq_file *sfile, void *v, loff_t *pos)
  394. {
  395. (*pos)++;
  396. sfile->private++;
  397. return next_scsi_device(v);
  398. }
  399. static void scsi_seq_stop(struct seq_file *sfile, void *v)
  400. {
  401. put_device(v);
  402. }
  403. static int scsi_seq_show(struct seq_file *sfile, void *dev)
  404. {
  405. if (!sfile->private)
  406. seq_puts(sfile, "Attached devices:\n");
  407. return proc_print_scsidevice(dev, sfile);
  408. }
  409. static const struct seq_operations scsi_seq_ops = {
  410. .start = scsi_seq_start,
  411. .next = scsi_seq_next,
  412. .stop = scsi_seq_stop,
  413. .show = scsi_seq_show
  414. };
  415. /**
  416. * proc_scsi_open - glue function
  417. * @inode: not used
  418. * @file: passed to single_open()
  419. *
  420. * Associates proc_scsi_show with this file
  421. */
  422. static int proc_scsi_open(struct inode *inode, struct file *file)
  423. {
  424. /*
  425. * We don't really need this for the write case but it doesn't
  426. * harm either.
  427. */
  428. return seq_open(file, &scsi_seq_ops);
  429. }
  430. static const struct file_operations proc_scsi_operations = {
  431. .owner = THIS_MODULE,
  432. .open = proc_scsi_open,
  433. .read = seq_read,
  434. .write = proc_scsi_write,
  435. .llseek = seq_lseek,
  436. .release = seq_release,
  437. };
  438. /**
  439. * scsi_init_procfs - create scsi and scsi/scsi in procfs
  440. */
  441. int __init scsi_init_procfs(void)
  442. {
  443. struct proc_dir_entry *pde;
  444. proc_scsi = proc_mkdir("scsi", NULL);
  445. if (!proc_scsi)
  446. goto err1;
  447. pde = proc_create("scsi/scsi", 0, NULL, &proc_scsi_operations);
  448. if (!pde)
  449. goto err2;
  450. return 0;
  451. err2:
  452. remove_proc_entry("scsi", NULL);
  453. err1:
  454. return -ENOMEM;
  455. }
  456. /**
  457. * scsi_exit_procfs - Remove scsi/scsi and scsi from procfs
  458. */
  459. void scsi_exit_procfs(void)
  460. {
  461. remove_proc_entry("scsi/scsi", NULL);
  462. remove_proc_entry("scsi", NULL);
  463. }