感谢支持
我们一直在努力

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

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;
}

在scsi_host_alloc函数被调用后,主机适配器还不会被公开给SCSI中间层,直到scsi_add_host函数被调用。

static inline int __must_check scsi_add_host(struct Scsi_Host *host,
struct device *dev)
{
return scsi_add_host_with_dma(host, dev, dev);
}

int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, //第一个为指向主机适配器描述符的指针,第二个指向这个SCSI主机适配器驱动模型中的父设备的device描述符
struct device *dma_dev)
{
struct scsi_host_template *sht = shost->hostt;
int error = -EINVAL;

printk(KERN_INFO “scsi%d : %s\n”, shost->host_no,
sht->info ? sht->info(shost) : sht->name);

if (!shost->can_queue) { //can_queue为主机适配器可以同时接受的命令数
printk(KERN_ERR “%s: can_queue = 0 no longer supported\n”,
sht->name);
goto fail;
}

error = scsi_setup_command_freelist(shost);
if (error)
goto fail;

if (!shost->shost_gendev.parent) //确定主机适配器在sysfs中的位置
shost->shost_gendev.parent = dev ? dev : &platform_bus;
shost->dma_dev = dma_dev;

error = device_add(&shost->shost_gendev);
if (error)
goto out;

scsi_host_set_state(shost, SHOST_RUNNING);
get_device(shost->shost_gendev.parent);

error = device_add(&shost->shost_dev);
if (error)
goto out_del_gendev;

get_device(&shost->shost_gendev);

if (shost->transportt->host_size) {
shost->shost_data = kzalloc(shost->transportt->host_size,
GFP_KERNEL);
if (shost->shost_data == NULL) {
error = -ENOMEM;
goto out_del_dev;
}
}

if (shost->transportt->create_work_queue) {
snprintf(shost->work_q_name, sizeof(shost->work_q_name),
“scsi_wq_%d”, shost->host_no);
shost->work_q = create_singlethread_workqueue(
shost->work_q_name);
if (!shost->work_q) {
error = -EINVAL;
goto out_free_shost_data;
}
}

error = scsi_sysfs_add_host(shost);
if (error)
goto out_destroy_host;

scsi_proc_host_add(shost);
return error;

out_destroy_host:
if (shost->work_q)
destroy_workqueue(shost->work_q);
out_free_shost_data:
kfree(shost->shost_data);
out_del_dev:
device_del(&shost->shost_dev);
out_del_gendev:
device_del(&shost->shost_gendev);
out:
scsi_destroy_command_freelist(shost);
fail:
return error;
}

 

 

补充:

sysfs 是 Linux 内核中设计较新的一种虚拟的基于内存的文件系统,它的作用与 proc 有些类似,但除了与 proc 相同的具有查看和设定内核参数功能之外,还有为 Linux 统一设备模型作为管理之用。相比于 proc 文件系统,使用 sysfs 导出内核数据的方式更为统一,并且组织的方式更好,它的设计从 proc 中吸取了很多教训。本文就 sysfs 的挂载点 /sys 目录结构、其与 Linux 统一设备模型的关系、常见属性文件的用法等方面对 sysfs 作入门介绍,并且就内核编程方面,以具体的例子来展示如何添加 sysfs 支持。

赞(0) 打赏
转载请注明出处:服务器评测 » Linux-2.6平台下SCSI子系统之添加适配器到系统
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏