手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux-2.6平台下SCSI子系统之添加适配器到系统

[日期:2013-01-14] 来源:Linux社区  作者:yuesichiu [字体: ]

SCSI低层驱动是面向主机适配器的,低层驱动被加载时,首先要添加主机适配器。主机适配器可以在PCI子系统完成ID匹配时添加,或者通过手动添加。所有基于硬件PCI接口的主机适配器都采用前一种方式,而UNH iSCSI启动器采用的是后一种方式。

添加主机适配器包括两部分内容:为主机适配器分配数据结构,将主机适配器添加到系统。

SCSI中间层为此提供了两个公共函数:scsi_host_alloc和scsi_add_host

/**
* scsi_host_alloc - register a scsi host adapter instance.
* @sht: pointer to scsi host template
* @privsize: extra bytes to allocate for driver
*
* Note:
* Allocate a new Scsi_Host and perform basic initialization.
* The host is not published to the scsi midlayer until scsi_add_host
* is called.
*
* Return value:
* Pointer to a new Scsi_Host
**/

 

struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
{
struct Scsi_Host *shost;
gfp_t gfp_mask = GFP_KERNEL;
int rval;

if (sht->unchecked_isa_dma && privsize)
gfp_mask |= __GFP_DMA; //RAM的低16M作为DMA地址空间

shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask); //SCSI_Host的空间一次性分配,包括公有部分和私有部分。
if (!shost)
return NULL;

shost->host_lock = &shost->default_lock;
spin_lock_init(shost->host_lock);
shost->shost_state = SHOST_CREATED;
INIT_LIST_HEAD(&shost->__devices);
INIT_LIST_HEAD(&shost->__targets);
INIT_LIST_HEAD(&shost->eh_cmd_q);
INIT_LIST_HEAD(&shost->starved_list);
init_waitqueue_head(&shost->host_wait);

mutex_init(&shost->scan_mutex);

/*
* subtract one because we increment first then return, but we need to
* know what the next host number was before increment
*/
shost->host_no = atomic_inc_return(&scsi_host_next_hn) - 1; //host_no主机适配器编号,scsi_host_next_hn表示下一个新的主机适配器的编号。它的初始值为0,每次发现一个新的主机适配器,将全局变量scsi_host_next_hn的值赋给host_no域,然后再递增。
shost->dma_channel = 0xff;

/* These three are default values which can be overridden */
shost->max_channel = 0;
shost->max_id = 8;
shost->max_lun = 8;

/* Give each shost a default transportt */
shost->transportt = &blank_transport_template;

/*
* All drivers right now should be able to handle 12 byte
* commands. Every so often there are requests for 16 byte
* commands, but individual low-level drivers need to certify that
* they actually do something sensible with such commands.
*/
shost->max_cmd_len = 12;
shost->hostt = sht;
shost->this_id = sht->this_id;
shost->can_queue = sht->can_queue;
shost->sg_tablesize = sht->sg_tablesize;
shost->cmd_per_lun = sht->cmd_per_lun;
shost->unchecked_isa_dma = sht->unchecked_isa_dma;
shost->use_clustering = sht->use_clustering;
shost->ordered_tag = sht->ordered_tag;

if (sht->supported_mode == MODE_UNKNOWN)
/* means we didn't set it ... default to INITIATOR */
shost->active_mode = MODE_INITIATOR;
else
shost->active_mode = sht->supported_mode;

if (sht->max_host_blocked)
shost->max_host_blocked = sht->max_host_blocked;
else
shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;

/*
* If the driver imposes no hard sector transfer limit, start at
* machine infinity initially.
*/
if (sht->max_sectors)
shost->max_sectors = sht->max_sectors;
else
shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;

/*
* assume a 4GB boundary, if not set
*/
if (sht->dma_boundary)
shost->dma_boundary = sht->dma_boundary;
else
shost->dma_boundary = 0xffffffff;

device_initialize(&shost->shost_gendev); //shost_gendev域内嵌通用设备,初始化;是内嵌类设备的辅设备。

dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
#ifndef CONFIG_SYSFS_DEPRECATED
shost->shost_gendev.bus = &scsi_bus_type;
#endif
shost->shost_gendev.type = &scsi_host_type;

device_initialize(&shost->shost_dev); //shost_dev域是内嵌类设备,初始化该类设备,
shost->shost_dev.parent = &shost->shost_gendev;
shost->shost_dev.class = &shost_class;
dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;

shost->ehandler = kthread_run(scsi_error_handler, shost, //SCSI错误恢复
"scsi_eh_%d", shost->host_no);
if (IS_ERR(shost->ehandler)) {
rval = PTR_ERR(shost->ehandler);
goto fail_kfree;
}

scsi_proc_hostdir_add(shost->hostt); //在proc文件系统中为这个主机适配器添加一个目录
return shost;

fail_kfree:
kfree(shost);
return NULL;
}

linux
相关资讯       Linux SCSI  Linux子系统 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款