summaryrefslogtreecommitdiff
path: root/linux/src/drivers/scsi/scsi_proc.c
blob: d1fa28d0f5027cfce69fefcd1d43e3d36c0c4ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * linux/drivers/scsi/scsi_proc.c
 *
 * The functions in this file provide an interface between
 * the PROC file system and the SCSI device drivers
 * It is mainly used for debugging, statistics and to pass 
 * information directly to the lowlevel driver.
 *
 * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de 
 * Version: 0.99.8   last change: 95/09/13
 * 
 * generic command parser provided by: 
 * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
 */

/*
 * Don't import our own symbols, as this would severely mess up our
 * symbol tables.
 */
#define _SCSI_SYMS_VER_
#define __NO_VERSION__
#include <linux/module.h>

#include <linux/string.h>
#include <linux/mm.h>
#include <linux/malloc.h>
#include <linux/proc_fs.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/blk.h>
#include "scsi.h"
#include "hosts.h"

#ifndef TRUE
#define TRUE  1
#define FALSE 0
#endif

extern int scsi_proc_info(char *, char **, off_t, int, int, int);
 
struct scsi_dir {
    struct proc_dir_entry entry;
    char name[4];
};


/* generic_proc_info
 * Used if the driver currently has no own support for /proc/scsi
 */
int generic_proc_info(char *buffer, char **start, off_t offset, 
		     int length, int inode, int inout)
{
    int len, pos, begin;

    if(inout == TRUE)
	return(-ENOSYS);  /* This is a no-op */
    
    begin = 0;
    pos = len = sprintf(buffer, 
			"The driver does not yet support the proc-fs\n");
    if(pos < offset) {
	len = 0;
	begin = pos;
    }
    
    *start = buffer + (offset - begin);   /* Start of wanted data */
    len -= (offset - begin);
    if(len > length)
	len = length;
    
    return(len);
}

/* dispatch_scsi_info is the central dispatcher 
 * It is the interface between the proc-fs and the SCSI subsystem code
 */
extern int dispatch_scsi_info(int ino, char *buffer, char **start, 
			      off_t offset, int length, int func)
{
    struct Scsi_Host *hpnt = scsi_hostlist;
    
    if(ino == PROC_SCSI_SCSI) {            
        /*
         * This is for the scsi core, rather than any specific
         * lowlevel driver.
         */
        return(scsi_proc_info(buffer, start, offset, length, 0, func));
    }
    
    while(hpnt) {
        if (ino == (hpnt->host_no + PROC_SCSI_FILE)) {
            if(hpnt->hostt->proc_info == NULL)
                return generic_proc_info(buffer, start, offset, length, 
                                         hpnt->host_no, func);
            else
                return(hpnt->hostt->proc_info(buffer, start, offset, 
                                              length, hpnt->host_no, func));
        }
        hpnt = hpnt->next;
    }
    return(-EBADF);
}

void build_proc_dir_entries(Scsi_Host_Template *tpnt)
{
    struct Scsi_Host *hpnt;

    struct scsi_dir *scsi_hba_dir;

    proc_scsi_register(0, tpnt->proc_dir);
    
    hpnt = scsi_hostlist;
    while (hpnt) {
        if (tpnt == hpnt->hostt) {
            scsi_hba_dir = scsi_init_malloc(sizeof(struct scsi_dir), GFP_KERNEL);
            if(scsi_hba_dir == NULL)
                panic("Not enough memory to register SCSI HBA in /proc/scsi !\n");
            memset(scsi_hba_dir, 0, sizeof(struct scsi_dir));
            scsi_hba_dir->entry.low_ino = PROC_SCSI_FILE + hpnt->host_no;
            scsi_hba_dir->entry.namelen = sprintf(scsi_hba_dir->name,"%d",
                                                    hpnt->host_no);
            scsi_hba_dir->entry.name = scsi_hba_dir->name;
            scsi_hba_dir->entry.mode = S_IFREG | S_IRUGO | S_IWUSR;
            proc_scsi_register(tpnt->proc_dir, &scsi_hba_dir->entry);
        }
        hpnt = hpnt->next;
    }
}

/*
 *  parseHandle *parseInit(char *buf, char *cmdList, int cmdNum); 
 *	 	gets a pointer to a null terminated data buffer
 *		and a list of commands with blanks as delimiter 
 *      in between. 
 *      The commands have to be alphanumerically sorted. 
 *      cmdNum has to contain the number of commands.
 *		On success, a pointer to a handle structure
 *		is returned, NULL on failure
 *
 *	int parseOpt(parseHandle *handle, char **param);
 *		processes the next parameter. On success, the
 *		index of the appropriate command in the cmdList
 *		is returned, starting with zero.
 *		param points to the null terminated parameter string.
 *		On failure, -1 is returned.
 *
 *	The databuffer buf may only contain pairs of commands
 *	    options, separated by blanks:
 *		<Command> <Parameter> [<Command> <Parameter>]*
 */

typedef struct
{
    char *buf,				   /* command buffer  */
	 *cmdList,                         /* command list    */
	 *bufPos,                          /* actual position */
	 **cmdPos,                         /* cmdList index   */
	 cmdNum;                           /* cmd number      */
} parseHandle;
	

inline int parseFree (parseHandle *handle)               /* free memory     */
{
    kfree (handle->cmdPos);
    kfree (handle);
    
    return(-1);
}

	
parseHandle *parseInit(char *buf, char *cmdList, int cmdNum)
{
    char        *ptr;                               /* temp pointer    */
    parseHandle *handle;                            /* new handle      */
    
    if (!buf || !cmdList)                           /* bad input ?     */
	return(NULL);
    if ((handle = (parseHandle*) kmalloc(sizeof(parseHandle), GFP_KERNEL)) == 0)
	return(NULL);                               /* out of memory   */
    if ((handle->cmdPos = (char**) kmalloc(sizeof(int) * cmdNum, GFP_KERNEL)) == 0) {
	kfree(handle);
	return(NULL);                               /* out of memory   */
    }
    
    handle->buf     = handle->bufPos = buf;         /* init handle     */
    handle->cmdList = cmdList;
    handle->cmdNum  = cmdNum;
    
    handle->cmdPos[cmdNum = 0] = cmdList;
    for (ptr = cmdList; *ptr; ptr++) {          /* scan command string */
	if(*ptr == ' ') {                       /* and insert zeroes   */
	    *ptr++ = 0;
	    handle->cmdPos[++cmdNum] = ptr++;
	} 
    }
    return(handle);
}


int parseOpt(parseHandle *handle, char **param)
{
    int  cmdIndex = 0, 
	 cmdLen = 0;
    char *startPos;
    
    if (!handle)                                    /* invalid handle  */
	return(parseFree(handle));
    /* skip spaces     */  
    for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
    if (!*(handle->bufPos))
	return(parseFree(handle));                  /* end of data     */
    
    startPos = handle->bufPos;                      /* store cmd start */
    for (; handle->cmdPos[cmdIndex][cmdLen] && *(handle->bufPos); handle->bufPos++)
    {                                               /* no string end?  */
	for (;;)
	{
	    if (*(handle->bufPos) == handle->cmdPos[cmdIndex][cmdLen])
		break;                              /* char matches ?  */
	    else
		if (memcmp(startPos, (char*)(handle->cmdPos[++cmdIndex]), cmdLen))
		    return(parseFree(handle));      /* unknown command */
	    
	    if (cmdIndex >= handle->cmdNum)
		return(parseFree(handle));          /* unknown command */     
	}
	
	cmdLen++;                                   /* next char       */
    }
    
    /* Get param. First skip all blanks, then insert zero after param  */
    
    for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
    *param = handle->bufPos; 
    
    for (; *(handle->bufPos) && *(handle->bufPos) != ' '; handle->bufPos++);
    *(handle->bufPos++) = 0;
    
    return(cmdIndex);
}

void proc_print_scsidevice(Scsi_Device *scd, char *buffer, int *size, int len)
{	    
    int x, y = *size;
    
    y = sprintf(buffer + len, 
		    "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n  Vendor: ",
		    scd->host->host_no, scd->channel, scd->id, scd->lun);
    for (x = 0; x < 8; x++) {
	if (scd->vendor[x] >= 0x20)
	    y += sprintf(buffer + len + y, "%c", scd->vendor[x]);
	else
	    y += sprintf(buffer + len + y," ");
    }
    y += sprintf(buffer + len + y, " Model: ");
    for (x = 0; x < 16; x++) {
	if (scd->model[x] >= 0x20)
	    y +=  sprintf(buffer + len + y, "%c", scd->model[x]);
	else
	    y += sprintf(buffer + len + y, " ");
    }
    y += sprintf(buffer + len + y, " Rev: ");
    for (x = 0; x < 4; x++) {
	if (scd->rev[x] >= 0x20)
	    y += sprintf(buffer + len + y, "%c", scd->rev[x]);
	else
	    y += sprintf(buffer + len + y, " ");
    }
    y += sprintf(buffer + len + y, "\n");
    
    y += sprintf(buffer + len + y, "  Type:   %s ",
		     scd->type < MAX_SCSI_DEVICE_CODE ? 
		     scsi_device_types[(int)scd->type] : "Unknown          " );
    y += sprintf(buffer + len + y, "               ANSI"
		     " SCSI revision: %02x", (scd->scsi_level < 3)?1:2);
    if (scd->scsi_level == 2)
	y += sprintf(buffer + len + y, " CCS\n");
    else
	y += sprintf(buffer + len + y, "\n");

    *size = y; 
    return;
}

/*
 * Overrides for Emacs so that we get a uniform tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-indent-level: 4
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -4
 * c-argdecl-indent: 4
 * c-label-offset: -4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: 0
 * indent-tabs-mode: nil
 * tab-width: 8
 * End:
 */