This source file includes following definitions.
- sg_ioctl
- sg_open
- sg_close
- sg_malloc
- sg_free
- sg_read
- sg_command_done
- sg_write
- sg_detect
- sg_init
- sg_attach
1
2
3
4
5
6
7
8
9
10 #include <linux/fs.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/mtio.h>
16 #include <linux/ioctl.h>
17 #include <linux/fcntl.h>
18 #include <asm/io.h>
19 #include <asm/segment.h>
20 #include <asm/system.h>
21
22 #include "../block/blk.h"
23 #include "scsi.h"
24 #include "hosts.h"
25 #include "scsi_ioctl.h"
26 #include "sg.h"
27
28 static void sg_init(void);
29 static void sg_attach(Scsi_Device *);
30 static int sg_detect(Scsi_Device *);
31
32
33 struct Scsi_Device_Template sg_template = {NULL, NULL, "sg", 0xff,
34 SCSI_GENERIC_MAJOR, 0, 0, 0, 0,
35 sg_detect, sg_init,
36 NULL, sg_attach, NULL};
37
38 #ifdef SG_BIG_BUFF
39 static char *big_buff;
40 static struct wait_queue *big_wait;
41 static int big_inuse=0;
42 #endif
43
44 struct scsi_generic
45 {
46 Scsi_Device *device;
47 int users;
48 struct wait_queue *generic_wait;
49 struct wait_queue *read_wait;
50 struct wait_queue *write_wait;
51 int timeout;
52 int buff_len;
53 char *buff;
54 struct sg_header header;
55 char exclude;
56 char pending;
57 char complete;
58 };
59
60 static struct scsi_generic *scsi_generics=NULL;
61
62 static int sg_ioctl(struct inode * inode,struct file * file,
63 unsigned int cmd_in, unsigned long arg)
64 {
65 int dev = MINOR(inode->i_rdev);
66 if ((dev<0) || (dev>=sg_template.dev_max))
67 return -ENXIO;
68 switch(cmd_in)
69 {
70 case SG_SET_TIMEOUT:
71 scsi_generics[dev].timeout=get_fs_long((int *) arg);
72 return 0;
73 case SG_GET_TIMEOUT:
74 return scsi_generics[dev].timeout;
75 default:
76 return scsi_ioctl(scsi_generics[dev].device, cmd_in, (void *) arg);
77 }
78 }
79
80 static int sg_open(struct inode * inode, struct file * filp)
81 {
82 int dev=MINOR(inode->i_rdev);
83 int flags=filp->f_flags;
84 if (dev>=sg_template.dev_max || !scsi_generics[dev].device)
85 return -ENXIO;
86 if (O_RDWR!=(flags & O_ACCMODE))
87 return -EACCES;
88 if (flags & O_EXCL)
89 {
90 while(scsi_generics[dev].users)
91 {
92 if (flags & O_NONBLOCK)
93 return -EBUSY;
94 interruptible_sleep_on(&scsi_generics[dev].generic_wait);
95 if (current->signal & ~current->blocked)
96 return -ERESTARTSYS;
97 }
98 scsi_generics[dev].exclude=1;
99 }
100 else
101 while(scsi_generics[dev].exclude)
102 {
103 if (flags & O_NONBLOCK)
104 return -EBUSY;
105 interruptible_sleep_on(&scsi_generics[dev].generic_wait);
106 if (current->signal & ~current->blocked)
107 return -ERESTARTSYS;
108 }
109 if (!scsi_generics[dev].users && scsi_generics[dev].pending && scsi_generics[dev].complete)
110 {
111 scsi_free(scsi_generics[dev].buff,scsi_generics[dev].buff_len);
112 scsi_generics[dev].pending=0;
113 }
114 if (!scsi_generics[dev].users)
115 scsi_generics[dev].timeout=SG_DEFAULT_TIMEOUT;
116 scsi_generics[dev].users++;
117 return 0;
118 }
119
120 static void sg_close(struct inode * inode, struct file * filp)
121 {
122 int dev=MINOR(inode->i_rdev);
123 scsi_generics[dev].users--;
124 scsi_generics[dev].exclude=0;
125 wake_up(&scsi_generics[dev].generic_wait);
126 }
127
128 static char *sg_malloc(int size)
129 {
130 if (size<=4096)
131 return (char *) scsi_malloc(size);
132 #ifdef SG_BIG_BUFF
133 if (size<SG_BIG_BUFF)
134 {
135 while(big_inuse)
136 {
137 interruptible_sleep_on(&big_wait);
138 if (current->signal & ~current->blocked)
139 return NULL;
140 }
141 big_inuse=1;
142 return big_buff;
143 }
144 #endif
145 return NULL;
146 }
147
148 static void sg_free(char *buff,int size)
149 {
150 #ifdef SG_BIG_BUFF
151 if (buff==big_buff)
152 {
153 big_inuse=0;
154 wake_up(&big_wait);
155 return;
156 }
157 #endif
158 scsi_free(buff,size);
159 }
160
161 static int sg_read(struct inode *inode,struct file *filp,char *buf,int count)
162 {
163 int dev=MINOR(inode->i_rdev);
164 int i;
165 struct scsi_generic *device=&scsi_generics[dev];
166 if ((i=verify_area(VERIFY_WRITE,buf,count)))
167 return i;
168 while(!device->pending || !device->complete)
169 {
170 if (filp->f_flags & O_NONBLOCK)
171 return -EWOULDBLOCK;
172 interruptible_sleep_on(&device->read_wait);
173 if (current->signal & ~current->blocked)
174 return -ERESTARTSYS;
175 }
176 device->header.pack_len=device->header.reply_len;
177 device->header.result=0;
178 if (count>=sizeof(struct sg_header))
179 {
180 memcpy_tofs(buf,&device->header,sizeof(struct sg_header));
181 buf+=sizeof(struct sg_header);
182 if (count>device->header.pack_len)
183 count=device->header.pack_len;
184 if (count > sizeof(struct sg_header)) {
185 memcpy_tofs(buf,device->buff,count-sizeof(struct sg_header));
186 }
187 }
188 else
189 count=0;
190 sg_free(device->buff,device->buff_len);
191 device->pending=0;
192 wake_up(&device->write_wait);
193 return count;
194 }
195
196 static void sg_command_done(Scsi_Cmnd * SCpnt)
197 {
198 int dev=SCpnt->request.dev;
199 struct scsi_generic *device=&scsi_generics[dev];
200 if (!device->pending)
201 {
202 printk("unexpected done for sg %d\n",dev);
203 SCpnt->request.dev=-1;
204 return;
205 }
206 memcpy(device->header.sense_buffer, SCpnt->sense_buffer, sizeof(SCpnt->sense_buffer));
207 if (SCpnt->sense_buffer[0])
208 {
209 device->header.result=EIO;
210 }
211 else
212 device->header.result=SCpnt->result;
213 device->complete=1;
214 SCpnt->request.dev=-1;
215 wake_up(&scsi_generics[dev].read_wait);
216 }
217
218 static int sg_write(struct inode *inode,struct file *filp,char *buf,int count)
219 {
220 int dev=MINOR(inode->i_rdev);
221 Scsi_Cmnd *SCpnt;
222 int bsize,size,amt,i;
223 unsigned char cmnd[MAX_COMMAND_SIZE];
224 struct scsi_generic *device=&scsi_generics[dev];
225
226 if ((i=verify_area(VERIFY_READ,buf,count)))
227 return i;
228 if (count<sizeof(struct sg_header))
229 return -EIO;
230
231 while(device->pending)
232 {
233 if (filp->f_flags & O_NONBLOCK)
234 return -EWOULDBLOCK;
235 #ifdef DEBUG
236 printk("sg_write: sleeping on pending request\n");
237 #endif
238 interruptible_sleep_on(&device->write_wait);
239 if (current->signal & ~current->blocked)
240 return -ERESTARTSYS;
241 }
242 device->pending=1;
243 device->complete=0;
244 memcpy_fromfs(&device->header,buf,sizeof(struct sg_header));
245
246 device->header.pack_len=count;
247 buf+=sizeof(struct sg_header);
248 bsize=(device->header.pack_len>device->header.reply_len) ? device->header.pack_len : device->header.reply_len;
249 bsize-=sizeof(struct sg_header);
250 amt=bsize;
251 if (!bsize)
252 bsize++;
253 bsize=(bsize+511) & ~511;
254 if ((bsize<0) || !(device->buff=sg_malloc(device->buff_len=bsize)))
255 {
256 device->pending=0;
257 wake_up(&device->write_wait);
258 return -ENOMEM;
259 }
260 #ifdef DEBUG
261 printk("allocating device\n");
262 #endif
263 if (!(SCpnt=allocate_device(NULL,device->device, !(filp->f_flags & O_NONBLOCK))))
264 {
265 device->pending=0;
266 wake_up(&device->write_wait);
267 sg_free(device->buff,device->buff_len);
268 return -EWOULDBLOCK;
269 }
270 #ifdef DEBUG
271 printk("device allocated\n");
272 #endif
273
274 SCpnt->request.dev=dev;
275 SCpnt->sense_buffer[0]=0;
276 size=COMMAND_SIZE(get_fs_byte(buf));
277 memcpy_fromfs(cmnd,buf,size);
278 buf+=size;
279 memcpy_fromfs(device->buff,buf,device->header.pack_len-size-sizeof(struct sg_header));
280 cmnd[1]=(cmnd[1] & 0x1f) | (device->device->lun<<5);
281 #ifdef DEBUG
282 printk("do cmd\n");
283 #endif
284 scsi_do_cmd (SCpnt,(void *) cmnd,
285 (void *) device->buff,amt,sg_command_done,device->timeout,SG_DEFAULT_RETRIES);
286 #ifdef DEBUG
287 printk("done cmd\n");
288 #endif
289 return count;
290 }
291
292 static struct file_operations sg_fops = {
293 NULL,
294 sg_read,
295 sg_write,
296 NULL,
297 NULL,
298 sg_ioctl,
299 NULL,
300 sg_open,
301 sg_close,
302 NULL
303 };
304
305
306 static int sg_detect(Scsi_Device * SDp){
307
308 if(scsi_loadable_module_flag) return 0;
309
310 ++sg_template.dev_noticed;
311 return 1;
312 }
313
314
315 static void sg_init()
316 {
317 static int sg_registered = 0;
318
319 if (sg_template.dev_noticed == 0) return;
320
321 if(!sg_registered) {
322 if (register_chrdev(SCSI_GENERIC_MAJOR,"sg",&sg_fops))
323 {
324 printk("Unable to get major %d for generic SCSI device\n",
325 SCSI_GENERIC_MAJOR);
326 return;
327 }
328 sg_registered++;
329 }
330
331
332 if(scsi_loadable_module_flag) return;
333 #ifdef DEBUG
334 printk("sg: Init generic device.\n");
335 #endif
336
337 #ifdef SG_BIG_BUFF
338 big_buff= (char *) scsi_init_malloc(SG_BIG_BUFF);
339 #endif
340
341 scsi_generics = (struct scsi_generic *)
342 scsi_init_malloc(sg_template.dev_noticed * sizeof(struct scsi_generic));
343 memset(scsi_generics, 0, sg_template.dev_noticed * sizeof(struct scsi_generic));
344
345 sg_template.dev_max = sg_template.dev_noticed;
346 }
347
348 static void sg_attach(Scsi_Device * SDp)
349 {
350 struct scsi_generic * gpnt;
351 int i;
352
353
354 if(scsi_loadable_module_flag) return;
355
356 if(sg_template.nr_dev >= sg_template.dev_max)
357 panic ("scsi_devices corrupt (sg)");
358
359 for(gpnt = scsi_generics, i=0; i<sg_template.dev_max; i++, gpnt++)
360 if(!gpnt->device) break;
361
362 if(i >= sg_template.dev_max) panic ("scsi_devices corrupt (sg)");
363
364 scsi_generics[i].device=SDp;
365 scsi_generics[i].users=0;
366 scsi_generics[i].generic_wait=NULL;
367 scsi_generics[i].read_wait=NULL;
368 scsi_generics[i].write_wait=NULL;
369 scsi_generics[i].exclude=0;
370 scsi_generics[i].pending=0;
371 scsi_generics[i].timeout=SG_DEFAULT_TIMEOUT;
372 sg_template.nr_dev++;
373 };