|
|
1.1 root 1: /*
2: * sd.c Copyright (C) 1992 Drew Eckhardt
3: * Linux scsi disk driver by
4: * Drew Eckhardt
5: *
6: * <[email protected]>
7: */
8:
9: #include <linux/config.h>
10:
11: #ifdef CONFIG_BLK_DEV_SD
12: #include <linux/string.h>
13: #include <linux/fs.h>
14: #include <linux/kernel.h>
15: #include <linux/sched.h>
16: #include "scsi.h"
17: #include "sd.h"
18:
19: #define MAJOR_NR 8
20:
21: #include "../blk.h"
22:
23: /*
24: static const char RCSid[] = "$Header:";
25: */
26:
27: #define MAX_RETRIES 5
28:
29: /*
30: * Time out in seconds
31: */
32:
33: #define SD_TIMEOUT 100
34:
35: Partition scsi_disks[MAX_SD << 4];
36: int NR_SD=0;
37: Scsi_Disk rscsi_disks[MAX_SD];
38: static int sd_sizes[MAX_SD << 4];
39: static int this_count;
40: static int the_result;
41:
42: extern int sd_ioctl(struct inode *, struct file *, unsigned long, unsigned long);
43:
44: static void sd_release(struct inode * inode, struct file * file)
45: {
46: sync_dev(inode->i_rdev);
47: }
48:
49: static struct file_operations sd_fops = {
50: NULL, /* lseek - default */
51: block_read, /* read - general block-dev read */
52: block_write, /* write - general block-dev write */
53: NULL, /* readdir - bad */
54: NULL, /* select */
55: sd_ioctl, /* ioctl */
56: NULL, /* no special open code */
57: sd_release /* release */
58: };
59:
60: /*
61: The sense_buffer is where we put data for all mode sense commands
62: performed.
63: */
64:
65: static unsigned char sense_buffer[255];
66:
67: /*
68: rw_intr is the interrupt routine for the device driver. It will
69: be notified on the end of a SCSI read / write, and
70: will take on of several actions based on success or failure.
71: */
72:
73: static void rw_intr (int host, int result)
74: {
75: if (HOST != host)
76: panic ("sd.o : rw_intr() recieving interrupt for different host.");
77:
78: /*
79: First case : we assume that the command succeeded. One of two things will
80: happen here. Either we will be finished, or there will be more
81: sectors that we were unable to read last time.
82: */
83:
84: if (!result)
85: if (!(CURRENT->nr_sectors -= this_count)) {
86: end_request(1);
87: do_sd_request();
88: } else {
89: CURRENT->nr_sectors -= this_count;
90: /*
91: The CURRENT->nr_sectors field is always done in 512 byte sectors,
92: even if this really isn't the case.
93: */
94:
95: (char *) CURRENT->buffer += this_count << 9;
96: CURRENT->sector += this_count;
97: CURRENT->errors = 0;
98: do_sd_request();
99: }
100:
101: /*
102: Now, if we were good little boys and girls, Santa left us a request
103: sense buffer. We can extract information from this, so we
104: can choose a block to remap, etc.
105: */
106:
107: else if (driver_byte(result) & DRIVER_SENSE) {
108: if (sugestion(result) == SUGGEST_REMAP) {
109: #ifdef REMAP
110: /*
111: Not yet implemented. A read will fail after being remapped,
112: a write will call the strategy routine again.
113: */
114:
115: if rscsi_disks[DEVICE_NR(CURRENT->dev)].remap
116: {
117: result = 0;
118: }
119: else
120:
121: #endif
122: }
123: /*
124: If we had an ILLEGAL REQUEST returned, then we may have performed
125: an unsupported command. The only thing this should be would be a ten
126: byte read where only a six byte read was supportted. Also, on a
127: system where READ CAPACITY failed, we mave have read past the end of the
128: disk.
129: */
130:
131: else if (sense_buffer[7] == ILLEGAL_REQUEST) {
132: if (rscsi_disks[DEVICE_NR(CURRENT->dev)].ten) {
133: rscsi_disks[DEVICE_NR(CURRENT->dev)].ten = 0;
134: do_sd_request();
135: result = 0;
136: } else {
137: }
138: }
139: }
140: if (result) {
141: printk("SCSI disk error : host %d id %d lun %d return code = %03x\n",
142: rscsi_disks[DEVICE_NR(CURRENT->dev)].device->host_no,
143: rscsi_disks[DEVICE_NR(CURRENT->dev)].device->id,
144: rscsi_disks[DEVICE_NR(CURRENT->dev)].device->lun);
145:
146: if (driver_byte(result) & DRIVER_SENSE)
147: printk("\tSense class %x, sense error %x, extended sense %x\n",
148: sense_class(sense_buffer[0]),
149: sense_error(sense_buffer[0]),
150: sense_buffer[2] & 0xf);
151:
152: end_request(0);
153: }
154: }
155:
156: /*
157: do_sd_request() is the request handler function for the sd driver.
158: Its function in life is to take block device requests, and translate
159: them to SCSI commands.
160: */
161:
162: void do_sd_request (void)
163: {
164: int dev, block;
165: unsigned char cmd[10];
166:
167: INIT_REQUEST;
168: dev = MINOR(CURRENT->dev);
169: block = CURRENT->sector;
170:
171: #ifdef DEBUG
172: printk("Doing sd request, dev = %d, block = %d\n", dev, block);
173: #endif
174:
175: if (dev >= (NR_SD << 4) || block + 2 > scsi_disks[dev].nr_sects ||
176: (dev % 16) > 5)
177: {
178: end_request(0);
179: goto repeat;
180: }
181:
182: block += scsi_disks[dev].start_sect;
183: dev = DEVICE_NR(dev);
184:
185: #ifdef DEBUG
186: printk("Real dev = %d, block = %d\n", dev, block);
187: #endif
188:
189: if (!rscsi_disks[dev].use)
190: {
191: end_request(0);
192: goto repeat;
193: }
194:
195: this_count = CURRENT->nr_sectors;
196: switch (CURRENT->cmd)
197: {
198: case WRITE :
199: if (!rscsi_disks[dev].device->writeable)
200: {
201: end_request(0);
202: goto repeat;
203: }
204: cmd[0] = WRITE_6;
205: break;
206: case READ :
207: cmd[0] = READ_6;
208: break;
209: default :
210: printk ("Unknown sd command %d\r\n", CURRENT->cmd);
211: panic("");
212: }
213:
214: cmd[1] = (LUN << 5) & 0xe0;
215:
216: if (((this_count > 0xff) || (block > 0x1fffff)) && rscsi_disks[dev].ten)
217: {
218: if (this_count > 0xffff)
219: this_count = 0xffff;
220:
221: cmd[0] += READ_10 - READ_6 ;
222: cmd[2] = (unsigned char) (block >> 24) & 0xff;
223: cmd[3] = (unsigned char) (block >> 16) & 0xff;
224: cmd[4] = (unsigned char) (block >> 8) & 0xff;
225: cmd[5] = (unsigned char) block & 0xff;
226: cmd[6] = cmd[9] = 0;
227: cmd[7] = (unsigned char) (this_count >> 8) & 0xff;
228: cmd[8] = (unsigned char) this_count & 0xff;
229: }
230: else
231: {
232: if (this_count > 0xff)
233: this_count = 0xff;
234:
235: cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
236: cmd[2] = (unsigned char) ((block >> 8) & 0xff);
237: cmd[3] = (unsigned char) block & 0xff;
238: cmd[4] = (unsigned char) this_count;
239: cmd[5] = 0;
240: }
241:
242: scsi_do_cmd (HOST, ID, (void *) cmd, CURRENT->buffer, this_count << 9,
243: rw_intr, SD_TIMEOUT, sense_buffer, MAX_RETRIES);
244: }
245:
246: static void sd_init_done (int host, int result)
247: {
248: the_result = result;
249: }
250:
251: /*
252: The sd_init() function looks at all SCSI drives present, determines
253: their size, and reads partition table entries for them.
254: */
255:
256: void sd_init(void)
257: {
258: int i,j,k;
259: unsigned char cmd[10];
260: unsigned char buffer[513];
261:
262: Partition *p;
263:
264:
265: for (i = 0; i < NR_SD; ++i)
266: {
267: cmd[0] = READ_CAPACITY;
268: rscsi_disks[i].use = 1;
269: cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
270: memset ((void *) &cmd[2], 0, 8);
271: the_result = -1;
272: #ifdef DEBUG
273: printk("Read capacity, disk %d at host = %d, id = %d\n", i,
274: rscsi_disks[i].device->host_no, rscsi_disks[i].device->id);
275: #endif
276: scsi_do_cmd (rscsi_disks[i].device->host_no ,
277: rscsi_disks[i].device->id,
278: (void *) cmd, (void *) buffer,
279: 512, sd_init_done, SD_TIMEOUT, sense_buffer,
280: MAX_RETRIES);
281:
282: while(the_result < 0);
283: /*
284: The SCSI standard says "READ CAPACITY is necessary for self confuring software"
285: While not mandatory, support of READ CAPACITY is strongly encouraged.
286:
287: We used to die if we couldn't successfully do a READ CAPACITY.
288: But, now we go on about our way. The side effects of this are
289:
290: 1. We can't know block size with certainty. I have said "512 bytes is it"
291: as this is most common.
292:
293: 2. Recovery from when some one attempts to read past the end of the raw device will
294: be slower.
295: */
296:
297: if (the_result)
298: {
299: printk ("Warning : SCSI device at host %d, id %d, lun %d failed READ CAPACITY.\n"
300: "status = %x, message = %02x, host = %02x, driver = %02x \n",
301: rscsi_disks[i].device->host_no, rscsi_disks[i].device->id,
302: rscsi_disks[i].device->lun,
303: status_byte(the_result),
304: msg_byte(the_result),
305: host_byte(the_result),
306: driver_byte(the_result)
307: );
308: if (driver_byte(the_result) & DRIVER_SENSE)
309: printk("Extended sense code = %1x \n", sense_buffer[2] & 0xf);
310: else
311: printk("Sense not available. \n");
312:
313: printk("Block size assumed to be 512 bytes, disk size 1GB. \n");
314: rscsi_disks[i].capacity = 0x1fffff;
315: rscsi_disks[i].sector_size = 512;
316: }
317: else
318: {
319: rscsi_disks[i].capacity = (buffer[0] << 24) |
320: (buffer[1] << 16) |
321: (buffer[2] << 8) |
322: buffer[3];
323:
324: if ((rscsi_disks[i].sector_size = (buffer[4] << 24) |
325: (buffer[5] << 16) |
326: (buffer[6] << 8) |
327: buffer[7]) != 512)
328: {
329: printk ("Unsupported sector size %d for sd %d",
330: rscsi_disks[i].sector_size, i);
331: rscsi_disks[i].use = 0;
332: }
333: }
334:
335: if (rscsi_disks[i].use)
336: {
337: scsi_disks[j = (i << 4)].start_sect = 0;
338:
339: sd_sizes[j]=(scsi_disks[j].nr_sects = rscsi_disks[i].capacity)>>1;
340: #ifdef DEBUG
341: printk("/dev/sd%1d size = %d\n", j, sd_sizes[j]);
342: #endif
343: cmd[0] = READ_6;
344: cmd[2] = cmd[3] = cmd[5] = 0;
345: cmd[4] = 1;
346: the_result = -1;
347:
348: scsi_do_cmd (rscsi_disks[i].device->host_no , rscsi_disks[i].device->id,
349: (void *) cmd, (void *) buffer, 512, sd_init_done, SD_TIMEOUT,
350: sense_buffer, MAX_RETRIES);
351:
352: while (the_result < 0);
353:
354:
355: if (the_result || (0xaa55 != *(unsigned short *)(buffer + 510)))
356: {
357: printk ("Cannot read partition table for sd %d"
358: "\n\r",i);
359: rscsi_disks[i].use = 0;
360: }
361: else
362: for (++j, k=j+4, p=(Partition *) (buffer + 0x1be); j < k; ++j, ++p)
363: {
364: memcpy ((void *) &scsi_disks[j], (void *) p, sizeof(Partition));
365: sd_sizes[j]=(scsi_disks[j].nr_sects)>>1;
366: #ifdef DEBUG
367: printk("/dev/sd%1d size = %d (%d blocks), offset = %d\n", j, scsi_disks[j].nr_sects, sd_sizes[j], scsi_disks[j].start_sect);
368: #endif
369: }
370:
371: rscsi_disks[i].ten = 1;
372: rscsi_disks[i].remap = 1;
373: }
374: }
375: blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
376: blk_size[MAJOR_NR] = sd_sizes;
377: blkdev_fops[MAJOR_NR] = &sd_fops;
378: }
379: #endif
380:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.