?? sd.c
字號:
/* * sd.c Copyright (C) 1992 Drew Eckhardt * Linux scsi disk driver by * Drew Eckhardt * * <drew@colorado.edu> * * Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to * add scatter-gather, multiple outstanding request, and other * enhancements. */#include <linux/fs.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/errno.h>#include <asm/system.h>#define MAJOR_NR SCSI_DISK_MAJOR#include "../block/blk.h"#include "scsi.h"#include "hosts.h"#include "sd.h"#include "scsi_ioctl.h"#include "constants.h"#include <linux/genhd.h>/*static const char RCSid[] = "$Header:";*/#define MAX_RETRIES 5/* * Time out in seconds */#define SD_TIMEOUT 300struct hd_struct * sd;int NR_SD=0;int MAX_SD=0;Scsi_Disk * rscsi_disks;static int * sd_sizes;static int * sd_blocksizes;extern int sd_ioctl(struct inode *, struct file *, unsigned int, unsigned long);static sd_init_onedisk(int);static void requeue_sd_request (Scsi_Cmnd * SCpnt);static int sd_open(struct inode * inode, struct file * filp){ int target; target = DEVICE_NR(MINOR(inode->i_rdev)); if(target >= NR_SD || !rscsi_disks[target].device) return -ENODEV; /* No such device */ /* Make sure that only one process can do a check_change_disk at one time. This is also used to lock out further access when the partition table is being re-read. */ while (rscsi_disks[target].device->busy); if(rscsi_disks[target].device->removable) { check_disk_change(inode->i_rdev); if(!rscsi_disks[target].device->access_count) sd_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0); }; rscsi_disks[target].device->access_count++; return 0;}static void sd_release(struct inode * inode, struct file * file){ int target; sync_dev(inode->i_rdev); target = DEVICE_NR(MINOR(inode->i_rdev)); rscsi_disks[target].device->access_count--; if(rscsi_disks[target].device->removable) { if(!rscsi_disks[target].device->access_count) sd_ioctl(inode, NULL, SCSI_IOCTL_DOORUNLOCK, 0); };}static void sd_geninit(void);static struct file_operations sd_fops = { NULL, /* lseek - default */ block_read, /* read - general block-dev read */ block_write, /* write - general block-dev write */ NULL, /* readdir - bad */ NULL, /* select */ sd_ioctl, /* ioctl */ NULL, /* mmap */ sd_open, /* open code */ sd_release, /* release */ block_fsync /* fsync */};static struct gendisk sd_gendisk = { MAJOR_NR, /* Major number */ "sd", /* Major name */ 4, /* Bits to shift to get real from partition */ 1 << 4, /* Number of partitions per real */ 0, /* maximum number of real */ sd_geninit, /* init function */ NULL, /* hd struct */ NULL, /* block sizes */ 0, /* number */ NULL, /* internal */ NULL /* next */};static void sd_geninit (void){ int i; for (i = 0; i < NR_SD; ++i) sd[i << 4].nr_sects = rscsi_disks[i].capacity; sd_gendisk.nr_real = NR_SD;}/* rw_intr is the interrupt routine for the device driver. It will be notified on the end of a SCSI read / write, and will take on of several actions based on success or failure.*/static void rw_intr (Scsi_Cmnd *SCpnt){ int result = SCpnt->result; int this_count = SCpnt->bufflen >> 9;#ifdef DEBUG printk("sd%d : rw_intr(%d, %d)\n", MINOR(SCpnt->request.dev), SCpnt->host->host_no, result);#endif/* First case : we assume that the command succeeded. One of two things will happen here. Either we will be finished, or there will be more sectors that we were unable to read last time.*/ if (!result) {#ifdef DEBUG printk("sd%d : %d sectors remain.\n", MINOR(SCpnt->request.dev), SCpnt->request.nr_sectors); printk("use_sg is %d\n ",SCpnt->use_sg);#endif if (SCpnt->use_sg) { struct scatterlist * sgpnt; int i; sgpnt = (struct scatterlist *) SCpnt->buffer; for(i=0; i<SCpnt->use_sg; i++) {#ifdef DEBUG printk(":%x %x %d\n",sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length);#endif if (sgpnt[i].alt_address) { if (SCpnt->request.cmd == READ) memcpy(sgpnt[i].alt_address, sgpnt[i].address, sgpnt[i].length); scsi_free(sgpnt[i].address, sgpnt[i].length); }; }; scsi_free(SCpnt->buffer, SCpnt->sglist_len); /* Free list of scatter-gather pointers */ } else { if (SCpnt->buffer != SCpnt->request.buffer) {#ifdef DEBUG printk("nosg: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer, SCpnt->bufflen);#endif if (SCpnt->request.cmd == READ) memcpy(SCpnt->request.buffer, SCpnt->buffer, SCpnt->bufflen); scsi_free(SCpnt->buffer, SCpnt->bufflen); }; };/* * If multiple sectors are requested in one buffer, then * they will have been finished off by the first command. If * not, then we have a multi-buffer command. */ if (SCpnt->request.nr_sectors > this_count) { SCpnt->request.errors = 0; if (!SCpnt->request.bh) {#ifdef DEBUG printk("sd%d : handling page request, no buffer\n", MINOR(SCpnt->request.dev));#endif/* The SCpnt->request.nr_sectors field is always done in 512 byte sectors, even if this really isn't the case.*/ panic("sd.c: linked page request (%lx %x)", SCpnt->request.sector, this_count); } } end_scsi_request(SCpnt, 1, this_count); requeue_sd_request(SCpnt); return; }/* Free up any indirection buffers we allocated for DMA purposes. */ if (SCpnt->use_sg) { struct scatterlist * sgpnt; int i; sgpnt = (struct scatterlist *) SCpnt->buffer; for(i=0; i<SCpnt->use_sg; i++) {#ifdef DEBUG printk("err: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer, SCpnt->bufflen);#endif if (sgpnt[i].alt_address) { scsi_free(sgpnt[i].address, sgpnt[i].length); }; }; scsi_free(SCpnt->buffer, SCpnt->sglist_len); /* Free list of scatter-gather pointers */ } else {#ifdef DEBUG printk("nosgerr: %x %x %d\n",SCpnt->request.buffer, SCpnt->buffer, SCpnt->bufflen);#endif if (SCpnt->buffer != SCpnt->request.buffer) scsi_free(SCpnt->buffer, SCpnt->bufflen); };/* Now, if we were good little boys and girls, Santa left us a request sense buffer. We can extract information from this, so we can choose a block to remap, etc.*/ if (driver_byte(result) != 0) { if (sugestion(result) == SUGGEST_REMAP) {#ifdef REMAP/* Not yet implemented. A read will fail after being remapped, a write will call the strategy routine again.*/ if rscsi_disks[DEVICE_NR(SCpnt->request.dev)].remap { result = 0; } else #endif } if ((SCpnt->sense_buffer[0] & 0x7f) == 0x70) { if ((SCpnt->sense_buffer[2] & 0xf) == UNIT_ATTENTION) { if(rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->removable) { /* detected disc change. set a bit and quietly refuse */ /* further access. */ rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->changed = 1; end_scsi_request(SCpnt, 0, this_count); requeue_sd_request(SCpnt); return; } } } /* If we had an ILLEGAL REQUEST returned, then we may haveperformed an unsupported command. The only thing this should be wouldbe a ten byte read where only a six byte read was supportted. Also,on a system where READ CAPACITY failed, we mave have read past the endof the disk. */ if (SCpnt->sense_buffer[2] == ILLEGAL_REQUEST) { if (rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten) { rscsi_disks[DEVICE_NR(SCpnt->request.dev)].ten = 0; requeue_sd_request(SCpnt); result = 0; } else { } } } /* driver byte != 0 */ if (result) { printk("SCSI disk error : host %d id %d lun %d return code = %x\n", rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->host->host_no, rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->id, rscsi_disks[DEVICE_NR(SCpnt->request.dev)].device->lun, result); if (driver_byte(result) & DRIVER_SENSE) print_sense("sd", SCpnt); end_scsi_request(SCpnt, 0, SCpnt->request.current_nr_sectors); requeue_sd_request(SCpnt); return; }}/* requeue_sd_request() is the request handler function for the sd driver. Its function in life is to take block device requests, and translate them to SCSI commands.*/static void do_sd_request (void){ Scsi_Cmnd * SCpnt = NULL; struct request * req = NULL; int flag = 0; while (1==1){ cli(); if (CURRENT != NULL && CURRENT->dev == -1) { sti(); return; }; INIT_SCSI_REQUEST;/* We have to be careful here. allocate_device will get a free pointer, but there is no guarantee that it is queueable. In normal usage, we want to call this, because other types of devices may have the host all tied up, and we want to make sure that we have at least one request pending for this type of device. We can also come through here while servicing an interrupt, because of the need to start another command. If we call allocate_device more than once, then the system can wedge if the command is not queueable. The request_queueable function is safe because it checks to make sure that the host is able to take another command before it returns a pointer. */ if (flag++ == 0) SCpnt = allocate_device(&CURRENT, rscsi_disks[DEVICE_NR(MINOR(CURRENT->dev))].device->index, 0); else SCpnt = NULL; sti();/* This is a performance enhancement. We dig down into the request list and try and find a queueable request (i.e. device not busy, and host able to accept another command. If we find one, then we queue it. This can make a big difference on systems with more than one disk drive. We want to have the interrupts off when monkeying with the request list, because otherwise the kernel might try and slip in a request inbetween somewhere. */ if (!SCpnt && NR_SD > 1){ struct request *req1; req1 = NULL; cli(); req = CURRENT; while(req){ SCpnt = request_queueable(req, rscsi_disks[DEVICE_NR(MINOR(req->dev))].device->index); if(SCpnt) break; req1 = req; req = req->next; }; if (SCpnt && req->dev == -1) { if (req == CURRENT) CURRENT = CURRENT->next; else req1->next = req->next; }; sti(); }; if (!SCpnt) return; /* Could not find anything to do */ wake_up(&wait_for_request); /* Queue command */ requeue_sd_request(SCpnt); }; /* While */} static void requeue_sd_request (Scsi_Cmnd * SCpnt){ int dev, block, this_count; unsigned char cmd[10]; char * buff;repeat: if(SCpnt->request.dev <= 0) { do_sd_request(); return; } dev = MINOR(SCpnt->request.dev); block = SCpnt->request.sector; this_count = 0;#ifdef DEBUG printk("Doing sd request, dev = %d, block = %d\n", dev, block);#endif if (dev >= (NR_SD << 4) || block + SCpnt->request.nr_sectors > sd[dev].nr_sects) { end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors); goto repeat; } block += sd[dev].start_sect; dev = DEVICE_NR(dev); if (rscsi_disks[dev].device->changed) {/* * quietly refuse to do anything to a changed disc until the changed bit has been reset */ /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors); goto repeat; }#ifdef DEBUG printk("sd%d : real dev = /dev/sd%d, block = %d\n", MINOR(SCpnt->request.dev), dev, block);#endif switch (SCpnt->request.cmd) { case WRITE : if (!rscsi_disks[dev].device->writeable) { end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors); goto repeat; } cmd[0] = WRITE_6; break; case READ : cmd[0] = READ_6; break; default : panic ("Unknown sd command %d\n", SCpnt->request.cmd); } SCpnt->this_count = 0; if (!SCpnt->request.bh || (SCpnt->request.nr_sectors == SCpnt->request.current_nr_sectors)) { /* case of page request (i.e. raw device), or unlinked buffer */ this_count = SCpnt->request.nr_sectors; buff = SCpnt->request.buffer; SCpnt->use_sg = 0; } else if (SCpnt->host->sg_tablesize == 0 || (need_isa_buffer && dma_free_sectors < 10)) { /* Case of host adapter that cannot scatter-gather. We also come here if we are running low on DMA buffer memory. We set a threshold higher than that we would need for this request so we leave room for other requests. Even though we would not need it all, we need to be conservative, because if we run low enough we have no choice but to panic. */ if (SCpnt->host->sg_tablesize != 0 && need_isa_buffer && dma_free_sectors < 10) printk("Warning: SCSI DMA buffer space running low. Using non scatter-gather I/O.\n"); this_count = SCpnt->request.current_nr_sectors; buff = SCpnt->request.buffer; SCpnt->use_sg = 0; } else { /* Scatter-gather capable host adapter */ struct buffer_head * bh; struct scatterlist * sgpnt; int count, this_count_max; bh = SCpnt->request.bh; this_count = 0; this_count_max = (rscsi_disks[dev].ten ? 0xffff : 0xff); count = 0; while(bh && count < SCpnt->host->sg_tablesize) { if ((this_count + (bh->b_size >> 9)) > this_count_max) break; this_count += (bh->b_size >> 9); count++; bh = bh->b_reqnext; };
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -