日期:2014-05-16  浏览次数:20897 次

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