|
|
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/fs.h>
13: #include <linux/kernel.h>
14: #include <linux/sched.h>
1.1.1.2 root 15: #include <linux/string.h>
16:
1.1 root 17: #include "scsi.h"
18: #include "sd.h"
19:
20: #define MAJOR_NR 8
21:
22: #include "../blk.h"
1.1.1.2 root 23: #include <linux/genhd.h>
1.1 root 24:
25: /*
26: static const char RCSid[] = "$Header:";
27: */
28:
29: #define MAX_RETRIES 5
30:
31: /*
32: * Time out in seconds
33: */
34:
1.1.1.2 root 35: #define SD_TIMEOUT 200
1.1 root 36:
1.1.1.2 root 37: struct hd_struct sd[MAX_SD << 4];
38:
1.1 root 39: int NR_SD=0;
40: Scsi_Disk rscsi_disks[MAX_SD];
1.1.1.3 ! root 41: static int sd_sizes[MAX_SD << 4] = {0, };
! 42: static int this_count, total_count = 0;
1.1 root 43: static int the_result;
44:
1.1.1.2 root 45: static char sense_buffer[255];
46:
1.1 root 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:
1.1.1.2 root 54: static struct gendisk sd_gendisk;
55:
56: static void sd_geninit (void) {
57: int i;
58: for (i = 0; i < NR_SD; ++i)
1.1.1.3 ! root 59: sd[i << 4].nr_sects = rscsi_disks[i].capacity;
1.1.1.2 root 60: sd_gendisk.nr_real = NR_SD;
61: }
62:
1.1 root 63: static struct file_operations sd_fops = {
64: NULL, /* lseek - default */
65: block_read, /* read - general block-dev read */
66: block_write, /* write - general block-dev write */
67: NULL, /* readdir - bad */
68: NULL, /* select */
69: sd_ioctl, /* ioctl */
70: NULL, /* no special open code */
71: sd_release /* release */
72: };
73:
1.1.1.2 root 74: static struct gendisk sd_gendisk = {
75: MAJOR_NR, /* Major number */
76: "sd", /* Major name */
77: 4, /* Bits to shift to get real from partition */
78: 1 << 4, /* Number of partitions per real */
79: MAX_SD, /* maximum number of real */
80: sd_geninit, /* init function */
81: sd, /* hd struct */
82: sd_sizes, /* block sizes */
83: 0, /* number */
84: (void *) rscsi_disks, /* internal */
85: NULL /* next */
86: };
1.1 root 87:
88: /*
89: rw_intr is the interrupt routine for the device driver. It will
90: be notified on the end of a SCSI read / write, and
91: will take on of several actions based on success or failure.
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:
1.1.1.2 root 99: #ifdef DEBUG
100: printk("sd%d : rw_intr(%d, %x)\n", MINOR(CURRENT->dev), host, result);
101: #endif
102:
1.1 root 103: /*
104: First case : we assume that the command succeeded. One of two things will
105: happen here. Either we will be finished, or there will be more
106: sectors that we were unable to read last time.
107: */
108:
1.1.1.2 root 109: if (!result) {
110: CURRENT->nr_sectors -= this_count;
1.1.1.3 ! root 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: };
1.1.1.2 root 118:
119: #ifdef DEBUG
120: printk("sd%d : %d sectors remain.\n", MINOR(CURRENT->dev), CURRENT->nr_sectors);
121: #endif
122:
123: /*
124: * If multiple sectors are requested in one buffer, then
125: * they will have been finished off by the first command. If
126: * not, then we have a multi-buffer command.
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:
1.1 root 140: /*
141: The CURRENT->nr_sectors field is always done in 512 byte sectors,
142: even if this really isn't the case.
143: */
1.1.1.2 root 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();
1.1 root 157: }
158:
159: /*
1.1.1.2 root 160: * Of course, the error handling code is a little Fubar down in scsi.c.
161: * Version 2 of the drivers will fix that, and we will *really* recover
162: * from errors.
163: */
164:
165: /*
1.1 root 166: Now, if we were good little boys and girls, Santa left us a request
167: sense buffer. We can extract information from this, so we
168: can choose a block to remap, etc.
169: */
170:
171: else if (driver_byte(result) & DRIVER_SENSE) {
172: if (sugestion(result) == SUGGEST_REMAP) {
173: #ifdef REMAP
174: /*
175: Not yet implemented. A read will fail after being remapped,
176: a write will call the strategy routine again.
177: */
178:
179: if rscsi_disks[DEVICE_NR(CURRENT->dev)].remap
180: {
181: result = 0;
182: }
183: else
184:
185: #endif
186: }
187: /*
188: If we had an ILLEGAL REQUEST returned, then we may have performed
189: an unsupported command. The only thing this should be would be a ten
190: byte read where only a six byte read was supportted. Also, on a
191: system where READ CAPACITY failed, we mave have read past the end of the
192: disk.
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);
1.1.1.2 root 215:
1.1 root 216: end_request(0);
217: }
218: }
219:
220: /*
221: do_sd_request() is the request handler function for the sd driver.
222: Its function in life is to take block device requests, and translate
223: them to SCSI commands.
224: */
225:
1.1.1.2 root 226: static void do_sd_request (void)
1.1 root 227: {
228: int dev, block;
229: unsigned char cmd[10];
230:
1.1.1.2 root 231: repeat:
1.1 root 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:
1.1.1.2 root 240: if (dev >= (NR_SD << 4) || block + CURRENT->nr_sectors > sd[dev].nr_sects)
1.1 root 241: {
242: end_request(0);
243: goto repeat;
244: }
245:
1.1.1.2 root 246: block += sd[dev].start_sect;
1.1 root 247: dev = DEVICE_NR(dev);
248:
249: #ifdef DEBUG
1.1.1.2 root 250: printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(CURRENT->dev), dev, block);
1.1 root 251: #endif
252:
1.1.1.2 root 253:
254: if (!CURRENT->bh)
255: this_count = CURRENT->nr_sectors;
256: else
257: this_count = (BLOCK_SIZE / 512);
1.1.1.3 ! root 258: /* This is a temporary hack for the AHA1742. */
! 259: if(total_count == 0)
! 260: total_count = this_count;
! 261: this_count = 1; /* Take only 512 bytes at a time */
1.1.1.2 root 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
1.1 root 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: }
1.1.1.2 root 286:
1.1 root 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: The sd_init() function looks at all SCSI drives present, determines
326: their size, and reads partition table entries for them.
327: */
328:
329: void sd_init(void)
330: {
1.1.1.2 root 331: int i,j;
1.1 root 332: unsigned char cmd[10];
333: unsigned char buffer[513];
1.1.1.2 root 334: int try_again;
1.1 root 335:
336:
337: for (i = 0; i < NR_SD; ++i)
338: {
1.1.1.2 root 339: try_again=2;
1.1 root 340: cmd[0] = READ_CAPACITY;
341: cmd[1] = (rscsi_disks[i].device->lun << 5) & 0xe0;
1.1.1.2 root 342: memset ((void *) &cmd[2], 0, 8);
343:
344: /*
345: * Super Kludge - since the midlevel error handling code doesn't work
346: * Version 2 will - it's under development 8^)
347: *
348: * We manually retry
349: */
350:
351:
352: do {
1.1 root 353: the_result = -1;
354: #ifdef DEBUG
1.1.1.2 root 355: printk("sd%d : READ CAPACITY\n ", i);
1.1 root 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);
1.1.1.2 root 364: } while (try_again && the_result);
1.1 root 365: /*
1.1.1.2 root 366: * The SCSI standard says "READ CAPACITY is necessary for self confuring software"
367: * While not mandatory, support of READ CAPACITY is strongly encouraged.
368: * We used to die if we couldn't successfully do a READ CAPACITY.
369: * But, now we go on about our way. The side effects of this are
370: *
371: * 1. We can't know block size with certainty. I have said "512 bytes is it"
372: * as this is most common.
373: *
374: * 2. Recovery from when some one attempts to read past the end of the raw device will
375: * be slower.
376: */
377:
1.1 root 378: if (the_result)
379: {
1.1.1.2 root 380: printk ("sd%d : READ CAPACITY failed.\n"
381: "sd%d : status = %x, message = %02x, host = %02x, driver = %02x \n",
382: i,i,
1.1 root 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)
1.1.1.2 root 391: printk("sd%d : extended sense code = %1x \n", i, sense_buffer[2] & 0xf);
1.1 root 392: else
1.1.1.2 root 393: printk("sd%d : sense not available. \n", i);
1.1 root 394:
1.1.1.2 root 395: printk("sd%d : block size assumed to be 512 bytes, disk size 1GB. \n", i);
1.1 root 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: {
1.1.1.2 root 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;
1.1 root 418: }
419: }
420:
1.1.1.2 root 421: rscsi_disks[i].ten = 1;
422: rscsi_disks[i].remap = 1;
1.1 root 423: }
1.1.1.2 root 424:
1.1 root 425: blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1.1.1.3 ! root 426: blkdev_fops[MAJOR_NR] = &sd_fops;
1.1.1.2 root 427: sd_gendisk.next = gendisk_head;
428: gendisk_head = &sd_gendisk;
1.1.1.3 ! root 429: }
1.1 root 430: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.