This source file includes following definitions.
- sd_release
- sd_geninit
- rw_intr
- do_sd_request
- sd_init_done
- sd_init
1
2
3
4
5
6
7
8
9 #include <linux/config.h>
10
11 #ifdef CONFIG_BLK_DEV_SD
12 #include <linux/fs.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/string.h>
16
17 #include "scsi.h"
18 #include "sd.h"
19
20 #define MAJOR_NR 8
21
22 #include "../blk.h"
23 #include <linux/genhd.h>
24
25
26
27
28
29 #define MAX_RETRIES 5
30
31
32
33
34
35 #define SD_TIMEOUT 200
36
37 struct hd_struct sd[MAX_SD << 4];
38
39 int NR_SD=0;
40 Scsi_Disk rscsi_disks[MAX_SD];
41 static int sd_sizes[MAX_SD << 4] = {0, };
42 static int this_count, total_count = 0;
43 static int the_result;
44
45 static char sense_buffer[255];
46
47 extern int sd_ioctl(struct inode *, struct file *, unsigned long, unsigned long);
48
49 static void sd_release(struct inode * inode, struct file * file)
50 {
51 sync_dev(inode->i_rdev);
52 }
53
54 static struct gendisk sd_gendisk;
55
56 static void sd_geninit (void) {
57 int i;
58 for (i = 0; i < NR_SD; ++i)
59 sd[i << 4].nr_sects = rscsi_disks[i].capacity;
60 sd_gendisk.nr_real = NR_SD;
61 }
62
63 static struct file_operations sd_fops = {
64 NULL,
65 block_read,
66 block_write,
67 NULL,
68 NULL,
69 sd_ioctl,
70 NULL,
71 sd_release
72 };
73
74 static struct gendisk sd_gendisk = {
75 MAJOR_NR,
76 "sd",
77 4,
78 1 << 4,
79 MAX_SD,
80 sd_geninit,
81 sd,
82 sd_sizes,
83 0,
84 (void *) rscsi_disks,
85 NULL
86 };
87
88
89
90
91
92
93
94 static void rw_intr (int host, int result)
95 {
96 if (HOST != host)
97 panic ("sd.o : rw_intr() recieving interrupt for different host.");
98
99 #ifdef DEBUG
100 printk("sd%d : rw_intr(%d, %x)\n", MINOR(CURRENT->dev), host, result);
101 #endif
102
103
104
105
106
107
108
109 if (!result) {
110 CURRENT->nr_sectors -= this_count;
111 total_count -= this_count;
112 if(total_count){
113 CURRENT->sector += this_count;
114 CURRENT->buffer += (this_count << 9);
115 do_sd_request();
116 return;
117 };
118
119 #ifdef DEBUG
120 printk("sd%d : %d sectors remain.\n", MINOR(CURRENT->dev), CURRENT->nr_sectors);
121 #endif
122
123
124
125
126
127
128 if (CURRENT->nr_sectors)
129 {
130 CURRENT->sector += this_count;
131 CURRENT->errors = 0;
132
133 if (!CURRENT->bh)
134 {
135 #ifdef DEBUG
136 printk("sd%d : handling page request, no buffer\n",
137 MINOR(CURRENT->dev));
138 #endif
139
140
141
142
143
144 (char *) CURRENT->buffer += this_count << 9;
145 }
146 else
147 {
148 #ifdef DEBUG
149 printk("sd%d : handling linked buffer request\n", MINOR(CURRENT->dev));
150 #endif
151 end_request(1);
152 }
153 }
154 else
155 end_request(1);
156 do_sd_request();
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171 else if (driver_byte(result) & DRIVER_SENSE) {
172 if (sugestion(result) == SUGGEST_REMAP) {
173 #ifdef REMAP
174
175
176
177
178
179 if rscsi_disks[DEVICE_NR(CURRENT->dev)].remap
180 {
181 result = 0;
182 }
183 else
184
185 #endif
186 }
187
188
189
190
191
192
193
194
195 else if (sense_buffer[7] == ILLEGAL_REQUEST) {
196 if (rscsi_disks[DEVICE_NR(CURRENT->dev)].ten) {
197 rscsi_disks[DEVICE_NR(CURRENT->dev)].ten = 0;
198 do_sd_request();
199 result = 0;
200 } else {
201 }
202 }
203 }
204 if (result) {
205 printk("SCSI disk error : host %d id %d lun %d return code = %03x\n",
206 rscsi_disks[DEVICE_NR(CURRENT->dev)].device->host_no,
207 rscsi_disks[DEVICE_NR(CURRENT->dev)].device->id,
208 rscsi_disks[DEVICE_NR(CURRENT->dev)].device->lun);
209
210 if (driver_byte(result) & DRIVER_SENSE)
211 printk("\tSense class %x, sense error %x, extended sense %x\n",
212 sense_class(sense_buffer[0]),
213 sense_error(sense_buffer[0]),
214 sense_buffer[2] & 0xf);
215
216 end_request(0);
217 }
218 }
219
220
221
222
223
224
225
226 static void do_sd_request (void)
227 {
228 int dev, block;
229 unsigned char cmd[10];
230
231 repeat:
232 INIT_REQUEST;
233 dev = MINOR(CURRENT->dev);
234 block = CURRENT->sector;
235
236 #ifdef DEBUG
237 printk("Doing sd request, dev = %d, block = %d\n", dev, block);
238 #endif
239
240 if (dev >= (NR_SD << 4) || block + CURRENT->nr_sectors > sd[dev].nr_sects)
241 {
242 end_request(0);
243 goto repeat;
244 }
245
246 block += sd[dev].start_sect;
247 dev = DEVICE_NR(dev);
248
249 #ifdef DEBUG
250 printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(CURRENT->dev), dev, block);
251 #endif
252
253
254 if (!CURRENT->bh)
255 this_count = CURRENT->nr_sectors;
256 else
257 this_count = (BLOCK_SIZE / 512);
258
259 if(total_count == 0)
260 total_count = this_count;
261 this_count = 1;
262
263 #ifdef DEBUG
264 printk("sd%d : %s %d/%d 512 byte blocks.\n", MINOR(CURRENT->dev),
265 (CURRENT->cmd == WRITE) ? "writing" : "reading",
266 this_count, CURRENT->nr_sectors);
267 #endif
268
269 switch (CURRENT->cmd)
270 {
271 case WRITE :
272 if (!rscsi_disks[dev].device->writeable)
273 {
274 end_request(0);
275 goto repeat;
276 }
277 cmd[0] = WRITE_6;
278 break;
279 case READ :
280 cmd[0] = READ_6;
281 break;
282 default :
283 printk ("Unknown sd command %d\r\n", CURRENT->cmd);
284 panic("");
285 }
286
287 cmd[1] = (LUN << 5) & 0xe0;
288
289 if (((this_count > 0xff) || (block > 0x1fffff)) && rscsi_disks[dev].ten)
290 {
291 if (this_count > 0xffff)
292 this_count = 0xffff;
293
294 cmd[0] += READ_10 - READ_6 ;
295 cmd[2] = (unsigned char) (block >> 24) & 0xff;
296 cmd[3] = (unsigned char) (block >> 16) & 0xff;
297 cmd[4] = (unsigned char) (block >> 8) & 0xff;
298 cmd[5] = (unsigned char) block & 0xff;
299 cmd[6] = cmd[9] = 0;
300 cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
301 cmd[8] = (unsigned char) this_count & 0xff;
302 }
303 else
304 {
305 if (this_count > 0xff)
306 this_count = 0xff;
307
308 cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
309 cmd[2] = (unsigned char) ((block >> 8) & 0xff);
310 cmd[3] = (unsigned char) block & 0xff;
311 cmd[4] = (unsigned char) this_count;
312 cmd[5] = 0;
313 }
314
315 scsi_do_cmd (HOST, ID, (void *) cmd, CURRENT->buffer, this_count << 9,
316 rw_intr, SD_TIMEOUT, sense_buffer, MAX_RETRIES);
317 }
318
319 static void sd_init_done (int host, int result)
320 {
321 the_result = result;
322 }
323
324
325
326
327
328
329 void sd_init(void)
330 {
331 int i,j;
332 unsigned char cmd[10];
333 unsigned char buffer[513];
334 int try_again;
335
336
337 for (i = 0; i < NR_SD; ++i)
338 {
339 try_again=2;
340 cmd[0] = READ_CAPACITY;
341 cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
342 memset ((void *) &cmd[2], 0, 8);
343
344
345
346
347
348
349
350
351
352 do {
353 the_result = -1;
354 #ifdef DEBUG
355 printk("sd%d : READ CAPACITY\n ", i);
356 #endif
357 scsi_do_cmd (rscsi_disks[i].device->host_no ,
358 rscsi_disks[i].device->id,
359 (void *) cmd, (void *) buffer,
360 512, sd_init_done, SD_TIMEOUT, sense_buffer,
361 MAX_RETRIES);
362
363 while(the_result < 0);
364 } while (try_again && the_result);
365
366
367
368
369
370
371
372
373
374
375
376
377
378 if (the_result)
379 {
380 printk ("sd%d : READ CAPACITY failed.\n"
381 "sd%d : status = %x, message = %02x, host = %02x, driver = %02x \n",
382 i,i,
383 rscsi_disks[i].device->host_no, rscsi_disks[i].device->id,
384 rscsi_disks[i].device->lun,
385 status_byte(the_result),
386 msg_byte(the_result),
387 host_byte(the_result),
388 driver_byte(the_result)
389 );
390 if (driver_byte(the_result) & DRIVER_SENSE)
391 printk("sd%d : extended sense code = %1x \n", i, sense_buffer[2] & 0xf);
392 else
393 printk("sd%d : sense not available. \n", i);
394
395 printk("sd%d : block size assumed to be 512 bytes, disk size 1GB. \n", i);
396 rscsi_disks[i].capacity = 0x1fffff;
397 rscsi_disks[i].sector_size = 512;
398 }
399 else
400 {
401 rscsi_disks[i].capacity = (buffer[0] << 24) |
402 (buffer[1] << 16) |
403 (buffer[2] << 8) |
404 buffer[3];
405
406 if ((rscsi_disks[i].sector_size = (buffer[4] << 24) |
407 (buffer[5] << 16) |
408 (buffer[6] << 8) |
409 buffer[7]) != 512)
410 {
411 printk ("sd%d : unsupported sector size %d.\n",
412 i, rscsi_disks[i].sector_size);
413 printk ("scsi : deleting disk entry.\n");
414 for (j=i; j < NR_SD;)
415 rscsi_disks[j] = rscsi_disks[++j];
416 --i;
417 continue;
418 }
419 }
420
421 rscsi_disks[i].ten = 1;
422 rscsi_disks[i].remap = 1;
423 }
424
425 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
426 blkdev_fops[MAJOR_NR] = &sd_fops;
427 sd_gendisk.next = gendisk_head;
428 gendisk_head = &sd_gendisk;
429 }
430 #endif