Linux scsi disk driver

sd.c

static void sd_config_discard(struct scsi_disk *, unsigned int);
static void sd_config_write_same(struct scsi_disk *);
static int  sd_revalidate_disk(struct gendisk *);
static void sd_unlock_native_capacity(struct gendisk *disk);
static int  sd_probe(struct device *);
static int  sd_remove(struct device *);
static void sd_shutdown(struct device *);
static int sd_suspend(struct device *);
static int sd_resume(struct device *);
static void sd_rescan(struct device *);
static int sd_init_command(struct scsi_cmnd *SCpnt);
static void sd_uninit_command(struct scsi_cmnd *SCpnt);
static int sd_done(struct scsi_cmnd *);
static int sd_eh_action(struct scsi_cmnd *, int);
static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
static void scsi_disk_release(struct device *cdev);
static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
static void sd_print_result(struct scsi_disk *, int);
module_init(init_sd);

/**
 *	init_sd - entry point for this driver (both when built in or when
 *	a module).
 *
 *	Note: this function registers this driver with the scsi mid-level.
 **/
static int __init init_sd(void)
{
	int majors = 0, i, err;

	SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));

	for (i = 0; i < SD_MAJORS; i++)
	        注册驱动
		if (register_blkdev(sd_major(i), "sd") == 0)
			majors++;

	if (!majors)
		return -ENODEV;

	err = class_register(&sd_disk_class);
	if (err)
		goto err_out;

	sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
					 0, 0, NULL);
	if (!sd_cdb_cache) {
		printk(KERN_ERR "sd: can‘t init extended cdb cache\n");
		goto err_out_class;
	}

	sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
	if (!sd_cdb_pool) {
		printk(KERN_ERR "sd: can‘t init extended cdb pool\n");
		goto err_out_cache;
	}
        在scsi总线上注册驱动
	err = scsi_register_driver(&sd_template.gendrv);
	if (err)
		goto err_out_driver;

	return 0;

err_out_driver:
	mempool_destroy(sd_cdb_pool);

err_out_cache:
	kmem_cache_destroy(sd_cdb_cache);

err_out_class:
	class_unregister(&sd_disk_class);
err_out:
	for (i = 0; i < SD_MAJORS; i++)
		unregister_blkdev(sd_major(i), "sd");
	return err;
}
int scsi_register_driver(struct device_driver *drv)
{
drv->bus = &scsi_bus_type;
return driver_register(drv);
}

sd_probe->async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);

sd_probe_async->add_disk

                sd_revalidate_disk


可以从gendisk获取scsi_disk和scsi_device

        struct gendisk *disk

struct scsi_disk *sdkp = scsi_disk(disk);

struct scsi_device *sdp = sdkp->device;


一些接口函数:

static struct scsi_disk *scsi_disk_get(struct gendisk *disk);

static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev);





scsi_scan.c

scsi_rescan_device

scsi_sysfs.c

static ssize_t
store_rescan_field (struct device *dev, struct device_attribute *attr,
		    const char *buf, size_t count)
{
	scsi_rescan_device(dev);
	return count;
}

scsi.c

subsys_initcall(init_scsi);

static int __init init_scsi(void)
{
	int error;

	error = scsi_init_queue();
	if (error)
		return error;
	error = scsi_init_procfs();
	if (error)
		goto cleanup_queue;
	error = scsi_init_devinfo();
	if (error)
		goto cleanup_procfs;
	error = scsi_init_hosts();
	if (error)
		goto cleanup_devlist;
	error = scsi_init_sysctl();
	if (error)
		goto cleanup_hosts;
	error = scsi_sysfs_register();
	if (error)
		goto cleanup_sysctl;

	scsi_netlink_init();

	printk(KERN_NOTICE "SCSI subsystem initialized\n");
	return 0;

cleanup_sysctl:
	scsi_exit_sysctl();
cleanup_hosts:
	scsi_exit_hosts();
cleanup_devlist:
	scsi_exit_devinfo();
cleanup_procfs:
	scsi_exit_procfs();
cleanup_queue:
	scsi_exit_queue();
	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
	       -error);
	return error;
}

接口函数:

int scsi_device_get(struct scsi_device *sdev)


scsi_sysfs.c

scsi_scan->scsi_scan_host_selected->scsi_scan_channel->__scsi_scan_target->scsi_probe_and_add_lun->scsi_probe_lun->scsi_execute_req->scsi_execute_req_flags->scsi_execute->blk_execute_rq->blk_execute_rq_nowait->

__elv_add_request(q, rq, where);

__blk_run_queue(q);->


/**

 * __blk_run_queue_uncond - run a queue whether or not it has been stopped

 * @q: The queue to run

 *

 * Description:

 *    Invoke request handling on a queue if there are any pending requests.

 *    May be used to restart request handling after a request has completed.

 *    This variant runs the queue whether or not the queue has been

 *    stopped. Must be called with the queue lock held and interrupts

 *    disabled. See also @blk_run_queue.

 */

inline void __blk_run_queue_uncond(struct request_queue *q)

{

if (unlikely(blk_queue_dead(q)))

return;


/*

* Some request_fn implementations, e.g. scsi_request_fn(), unlock

* the queue lock internally. As a result multiple threads may be

* running such a request function concurrently. Keep track of the

* number of active request_fn invocations such that blk_drain_queue()

* can wait until all these request_fn calls have finished.

*/

q->request_fn_active++;

q->request_fn(q);

q->request_fn_active--;

}


scsi_request_fn->scsi_dispatch_cmd->


/**

 * scsi_dispatch_command - Dispatch a command to the low-level driver.

 * @cmd: command block we are dispatching.

 *

 * Return: nonzero return request was rejected and device‘s queue needs to be

 * plugged.

 */

int scsi_dispatch_cmd(struct scsi_cmnd *cmd)

{

struct Scsi_Host *host = cmd->device->host;

int rtn = 0;


atomic_inc(&cmd->device->iorequest_cnt);


/* check if the device is still usable */

if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {

/* in SDEV_DEL we error all commands. DID_NO_CONNECT

* returns an immediate error upwards, and signals

* that the device is no longer present */

cmd->result = DID_NO_CONNECT << 16;

scsi_done(cmd);

/* return 0 (because the command has been processed) */

goto out;

}


/* Check to see if the scsi lld made this device blocked. */

if (unlikely(scsi_device_blocked(cmd->device))) {

/* 

* in blocked state, the command is just put back on

* the device queue.  The suspend state has already

* blocked the queue so future requests should not

* occur until the device transitions out of the

* suspend state.

*/


scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);


SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));


/*

* NOTE: rtn is still zero here because we don‘t need the

* queue to be plugged on return (it‘s already stopped)

*/

goto out;

}


/* 

* If SCSI-2 or lower, store the LUN value in cmnd.

*/

if (cmd->device->scsi_level <= SCSI_2 &&

   cmd->device->scsi_level != SCSI_UNKNOWN) {

cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |

      (cmd->device->lun << 5 & 0xe0);

}


scsi_log_send(cmd);


/*

* Before we queue this command, check if the command

* length exceeds what the host adapter can handle.

*/

if (cmd->cmd_len > cmd->device->host->max_cmd_len) {

SCSI_LOG_MLQUEUE(3,

printk("queuecommand : command too long. "

      "cdb_size=%d host->max_cmd_len=%d\n",

      cmd->cmd_len, cmd->device->host->max_cmd_len));

cmd->result = (DID_ABORT << 16);


scsi_done(cmd);

goto out;

}


if (unlikely(host->shost_state == SHOST_DEL)) {

cmd->result = (DID_NO_CONNECT << 16);

scsi_done(cmd);

} else {

trace_scsi_dispatch_cmd_start(cmd);

cmd->scsi_done = scsi_done;

rtn = host->hostt->queuecommand(host, cmd);

              通过host将cmd传送出去。如FC、IB(srp)

}


if (rtn) {

trace_scsi_dispatch_cmd_error(cmd, rtn);

if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&

   rtn != SCSI_MLQUEUE_TARGET_BUSY)

rtn = SCSI_MLQUEUE_HOST_BUSY;


scsi_queue_insert(cmd, rtn);


SCSI_LOG_MLQUEUE(3,

   printk("queuecommand : request rejected\n"));

}


 out:

SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));

return rtn;

}



/*
 * Create the actual show/store functions and data structures.
 */

static ssize_t
store_scan(struct device *dev, struct device_attribute *attr,
	   const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	int res;

	res = scsi_scan(shost, buf);
	if (res == 0)
		res = count;
	return res;
};

static int scsi_scan(struct Scsi_Host *shost, const char *str)
{
	char s1[15], s2[15], s3[15], junk;
	unsigned int channel, id, lun;
	int res;

	res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
	if (res != 3)
		return -EINVAL;
	if (check_set(&channel, s1))
		return -EINVAL;
	if (check_set(&id, s2))
		return -EINVAL;
	if (check_set(&lun, s3))
		return -EINVAL;
	if (shost->transportt->user_scan)
		res = shost->transportt->user_scan(shost, channel, id, lun);
	else
		res = scsi_scan_host_selected(shost, channel, id, lun, 1);
	return res;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。