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