Example #1
0
/**
 *	sd_init_command - build a scsi (read or write) command from
 *	information in the request structure.
 *	@SCpnt: pointer to mid-level's per scsi command structure that
 *	contains request and into which the scsi command is written
 *
 *	Returns 1 if successful and 0 if error (or cannot be done now).
 **/
static int sd_init_command(struct scsi_cmnd * SCpnt)
{
	unsigned int this_count, timeout;
	struct gendisk *disk;
	sector_t block;
	struct scsi_device *sdp = SCpnt->device;
	struct request *rq = SCpnt->request;

	timeout = sdp->timeout;

	/*
	 * SG_IO from block layer already setup, just copy cdb basically
	 */
	if (blk_pc_request(rq)) {
		scsi_setup_blk_pc_cmnd(SCpnt, SD_PASSTHROUGH_RETRIES);
		if (rq->timeout)
			timeout = rq->timeout;

		goto queue;
	}

	/*
	 * we only do REQ_CMD and REQ_BLOCK_PC
	 */
	if (!blk_fs_request(rq))
		return 0;

	disk = rq->rq_disk;
	block = rq->sector;
	this_count = SCpnt->request_bufflen >> 9;

	SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
			    "count=%d\n", disk->disk_name,
			 (unsigned long long)block, this_count));

	if (!sdp || !scsi_device_online(sdp) ||
 	    block + rq->nr_sectors > get_capacity(disk)) {
		SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", 
				 rq->nr_sectors));
		SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
		return 0;
	}

	if (sdp->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"); */
		return 0;
	}
	SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
				   disk->disk_name, (unsigned long long)block));

	/*
	 * If we have a 1K hardware sectorsize, prevent access to single
	 * 512 byte sectors.  In theory we could handle this - in fact
	 * the scsi cdrom driver must be able to handle this because
	 * we typically use 1K blocksizes, and cdroms typically have
	 * 2K hardware sectorsizes.  Of course, things are simpler
	 * with the cdrom, since it is read-only.  For performance
	 * reasons, the filesystems should be able to handle this
	 * and not force the scsi disk driver to use bounce buffers
	 * for this.
	 */
	if (sdp->sector_size == 1024) {
		if ((block & 1) || (rq->nr_sectors & 1)) {
			printk(KERN_ERR "sd: Bad block number requested");
			return 0;
		} else {
			block = block >> 1;
			this_count = this_count >> 1;
		}
	}
	if (sdp->sector_size == 2048) {
		if ((block & 3) || (rq->nr_sectors & 3)) {
			printk(KERN_ERR "sd: Bad block number requested");
			return 0;
		} else {
			block = block >> 2;
			this_count = this_count >> 2;
		}
	}
	if (sdp->sector_size == 4096) {
		if ((block & 7) || (rq->nr_sectors & 7)) {
			printk(KERN_ERR "sd: Bad block number requested");
			return 0;
		} else {
			block = block >> 3;
			this_count = this_count >> 3;
		}
	}
	if (rq_data_dir(rq) == WRITE) {
		if (!sdp->writeable) {
			return 0;
		}
		SCpnt->cmnd[0] = WRITE_6;
		SCpnt->sc_data_direction = DMA_TO_DEVICE;
	} else if (rq_data_dir(rq) == READ) {
		SCpnt->cmnd[0] = READ_6;
		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
	} else {
		printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
/* overkill 	panic("Unknown sd command %lx\n", rq->flags); */
		return 0;
	}

	SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", 
		disk->disk_name, (rq_data_dir(rq) == WRITE) ? 
		"writing" : "reading", this_count, rq->nr_sectors));

	SCpnt->cmnd[1] = 0;
	
	if (block > 0xffffffff) {
		SCpnt->cmnd[0] += READ_16 - READ_6;
		SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
		SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
		SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
		SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
		SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
		SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
		SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
		SCpnt->cmnd[9] = (unsigned char) block & 0xff;
		SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
		SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
		SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
		SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
		SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
	} else if ((this_count > 0xff) || (block > 0x1fffff) ||
//static int sm_init_command(struct scsi_cmnd* pcmnd)
static int sm_prep_fn(struct request_queue *q, struct request *rq)
{
	//LogPath();
	struct scsi_device* sdp = q->queuedata;
//	struct request* rq = pcmnd->request;
	struct gendisk* disk = rq->rq_disk;
	sector_t 	 block = blk_rq_pos(rq);
	unsigned int this_count = blk_rq_sectors(rq);
	int ret = 0;
	//[OS]=[sm_prep_fn:193]=block:32640,this_count:8
//	Log("block:%d,this_count:%d",block,this_count);
	if(rq->cmd_type == REQ_TYPE_BLOCK_PC)
	{
		ret = scsi_setup_blk_pc_cmnd(sdp,rq);	
		goto out;
	}
	else if(rq->cmd_type != REQ_TYPE_FS)
	{
		ret = BLKPREP_KILL;
		goto out;
	}
	ret = scsi_setup_fs_cmnd(sdp,rq);
	if(ret != BLKPREP_OK)
	{
		goto out;
	}
	struct scsi_cmnd* pcmnd = rq->special;
//	unsigned int timeout = sdp->timeout;

	if(!sdp || !scsi_device_online(sdp) || 
		block + blk_rq_sectors(rq) > get_capacity(disk))
	{
		Log("[Error] commnad.");
		return 0;
	}

	if(rq_data_dir(rq) == WRITE)
	{
		//LogPath();
		pcmnd->cmnd[0] = WRITE_6;
		pcmnd->sc_data_direction = DMA_TO_DEVICE;
	}
	else
	{
		//LogPath();
		pcmnd->cmnd[0] = READ_6;
		pcmnd->sc_data_direction = DMA_FROM_DEVICE;
	}
	//LogPath();
	pcmnd->cmnd[1] = 0;

	if(block > 0xFFFFFFFF)
	{
		pcmnd->cmnd[0] += READ_16 - READ_6;
		pcmnd->cmnd[1] |= ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
		pcmnd->cmnd[2] = sizeof(block) > 4 ? (unsigned char)(block >> 56) & 0xFF : 0;
		pcmnd->cmnd[3] = sizeof(block) > 4 ? (unsigned char)(block >> 48) & 0xFF : 0;
		pcmnd->cmnd[4] = sizeof(block) > 4 ? (unsigned char)(block >> 40) & 0xFF : 0;
		pcmnd->cmnd[5] = sizeof(block) > 4 ? (unsigned char)(block >> 30) & 0xFF : 0;
		pcmnd->cmnd[6] = (unsigned char)(block >> 24) & 0xFF;
		pcmnd->cmnd[7] = (unsigned char)(block >> 16) & 0xFF;
		pcmnd->cmnd[8] = (unsigned char)(block >> 8) & 0xFF;
		pcmnd->cmnd[9] = (unsigned char)block & 0xFF;

		pcmnd->cmnd[10] = (unsigned char)(this_count >> 24) & 0xFF;
		pcmnd->cmnd[11] = (unsigned char)(this_count >> 16) & 0xFF;
		pcmnd->cmnd[12] = (unsigned char)(this_count >> 8)  & 0xFF;
		pcmnd->cmnd[13] = (unsigned char)(this_count)		&0xFF;

		pcmnd->cmnd[14] = pcmnd->cmnd[15] = 0;
 	}