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