libata分析

来源:互联网 发布:nginx 指定tomcat目录 编辑:程序博客网 时间:2024/06/05 04:20

转自:http://blog.csdn.net/dianhuiren/article/details/7162063

[cpp] view plaincopy
  1. 进来分析libata模块,颇有所感,记录如下,希望能对大家有所帮助,同时也对自己的理解进一步深入。  
  2. linux版本:linux-2.6.24.3  
  3. 注:因完全是个人理解,理解不当难免,恳请批评指正!!!!  
  4.   
  5. 大家知道驱动程序在初始化sata controller后, 并初始化ata_host结构体后,会调用函数ata_host_activate进入libata的初始化,我们从这里开始分析。  
  6.   
  7. 下面是freescale  mpc8315平台的sata驱动代码。  
  8.   
  9. linux/driver/ata/sata_fsl.c  
  10.   
  11. static int sata_fsl_probe(struct of_device *ofdev,  
  12.             const struct of_device_id *match)  
  13. {  
  14.     host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);  
  15.     if (!host_priv)  
  16.         goto error_exit_with_cleanup;  
  17.   
  18.     irq = irq_of_parse_and_map(ofdev->node, 0);  
  19.     if (irq < 0) {  
  20.         dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");  
  21.         goto error_exit_with_cleanup;  
  22.     }  
  23.     host_priv->irq = irq;  
  24.   
  25.     /* allocate host structure */  
  26.     host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);  
  27.   
  28.     /* host->iomap is not used currently */  
  29.     host->private_data = host_priv;  
  30.   
  31.     /* initialize host controller */  
  32.     sata_fsl_init_controller(host);  
  33.   
  34.     /* 
  35.      * Now, register with libATA core, this will also initiate the 
  36.      * device discovery process, invoking our port_start() handler & 
  37.      * error_handler() to execute a dummy Softreset EH session 
  38.      */  
  39.     ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,  
  40.               &sata_fsl_sht);  
  41.   
  42.     dev_set_drvdata(&ofdev->dev, host);  
  43.   
  44.     return 0;  
  45. }  
  46.   
  47. 函数ata_host_activate申请了中断,并调用ata_host_register函数注册host  
  48.   
  49. linux/driver/ata/libata-core.c  
  50.   
  51. /** 
  52.  *    ata_host_activate - start host, request IRQ and register it 
  53.  *    @host: target ATA host 
  54.  *    @irq: IRQ to request 
  55.  *    @irq_handler: irq_handler used when requesting IRQ 
  56.  *    @irq_flags: irq_flags used when requesting IRQ 
  57.  *    @sht: scsi_host_template to use when registering the host 
  58.  * 
  59.  *    After allocating an ATA host and initializing it, most libata 
  60.  *    LLDs perform three steps to activate the host - start host, 
  61.  *    request IRQ and register it.  This helper takes necessasry 
  62.  *    arguments and performs the three steps in one go. 
  63.  * 
  64.  *    An invalid IRQ skips the IRQ registration and expects the host to 
  65.  *    have set polling mode on the port. In this case, @irq_handler 
  66.  *    should be NULL. 
  67.  * 
  68.  *    LOCKING: 
  69.  *    Inherited from calling layer (may sleep). 
  70.  * 
  71.  *    RETURNS: 
  72.  *    0 on success, -errno otherwise. 
  73.  */  
  74. int ata_host_activate(struct ata_host *host, int irq,  
  75.               irq_handler_t irq_handler, unsigned long irq_flags,  
  76.               struct scsi_host_template *sht)  
  77. {  
  78.     int i, rc;  
  79.   
  80.     rc = ata_host_start(host);  
  81.     if (rc)  
  82.         return rc;  
  83.   
  84.     /* Special case for polling mode */  
  85.     if (!irq) {  
  86.         WARN_ON(irq_handler);  
  87.         return ata_host_register(host, sht);  
  88.     }  
  89.   
  90.     rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,  
  91.                   dev_driver_string(host->dev), host);  
  92.     if (rc)  
  93.         return rc;  
  94.   
  95.     for (i = 0; i < host->n_ports; i++)  
  96.         ata_port_desc(host->ports[i], "irq %d", irq);  
  97.   
  98.     rc = ata_host_register(host, sht);  
  99.     /* if failed, just free the IRQ and leave ports alone */  
  100.     if (rc)  
  101.         devm_free_irq(host->dev, irq, host);  
  102.   
  103.     return rc;  
  104. }  
  105.   
  106.   
  107. linux/driver/ata/libata-core.c  
  108.   
  109. /** 
  110.  *    ata_host_register - register initialized ATA host 
  111.  *    @host: ATA host to register 
  112.  *    @sht: template for SCSI host 
  113.  * 
  114.  *    Register initialized ATA host.  @host is allocated using 
  115.  *    ata_host_alloc() and fully initialized by LLD.  This function 
  116.  *    starts ports, registers @host with ATA and SCSI layers and 
  117.  *    probe registered devices. 
  118.  * 
  119.  *    LOCKING: 
  120.  *    Inherited from calling layer (may sleep). 
  121.  * 
  122.  *    RETURNS: 
  123.  *    0 on success, -errno otherwise. 
  124.  */  
  125. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)  
  126. {  
  127.     int i, rc;  
  128.   
  129.     /* host must have been started */  
  130.     if (!(host->flags & ATA_HOST_STARTED)) {  
  131.         dev_printk(KERN_ERR, host->dev,  
  132.                "BUG: trying to register unstarted host\n");  
  133.         WARN_ON(1);  
  134.         return -EINVAL;  
  135.     }  
  136.   
  137.     /* Blow away unused ports.  This happens when LLD can't 
  138.      * determine the exact number of ports to allocate at 
  139.      * allocation time. 
  140.      */  
  141.     for (i = host->n_ports; host->ports[i]; i++)  
  142.         kfree(host->ports[i]);  
  143.   
  144.     /* give ports names and add SCSI hosts */  
  145.     for (i = 0; i < host->n_ports; i++)  
  146.         host->ports[i]->print_id = ata_print_id++;  
  147.   
  148.     rc = ata_scsi_add_hosts(host, sht);  
  149.     if (rc)  
  150.         return rc;  
  151.   
  152.     /* associate with ACPI nodes */  
  153.     ata_acpi_associate(host);  
  154.   
  155.     /* set cable, sata_spd_limit and report */  
  156.     for (i = 0; i < host->n_ports; i++) {  
  157.         struct ata_port *ap = host->ports[i];  
  158.         unsigned long xfer_mask;  
  159.   
  160.         /* set SATA cable type if still unset */  
  161.         if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))  
  162.             ap->cbl = ATA_CBL_SATA;  
  163.   
  164.         /* init sata_spd_limit to the current value */  
  165.         sata_link_init_spd(&ap->link);  
  166.   
  167.         /* print per-port info to dmesg */  
  168.         xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,  
  169.                           ap->udma_mask);  
  170.   
  171.         if (!ata_port_is_dummy(ap)) {  
  172.             ata_port_printk(ap, KERN_INFO,  
  173.                     "%cATA max %s %s\n",  
  174.                     (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',  
  175.                     ata_mode_string(xfer_mask),  
  176.                     ap->link.eh_info.desc);  
  177.             ata_ehi_clear_desc(&ap->link.eh_info);  
  178.         } else  
  179.             ata_port_printk(ap, KERN_INFO, "DUMMY\n");  
  180.     }  
  181.   
  182.     /* perform each probe synchronously */  
  183.     DPRINTK("probe begin\n");  
  184.     for (i = 0; i < host->n_ports; i++) {  
  185.         struct ata_port *ap = host->ports[i];  
  186.         int rc;  
  187.   
  188.         /* probe */  
  189.         if (ap->ops->error_handler) {  
  190.             struct ata_eh_info *ehi = &ap->link.eh_info;  
  191.             unsigned long flags;  
  192.   
  193.             ata_port_probe(ap);  
  194.   
  195.             /* kick EH for boot probing */  
  196.             spin_lock_irqsave(ap->lock, flags);  
  197.   
  198.             ehi->probe_mask =  
  199.                 (1 << ata_link_max_devices(&ap->link)) - 1;  
  200.             ehi->action |= ATA_EH_SOFTRESET;  
  201.             ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;  
  202.   
  203.             ap->pflags &= ~ATA_PFLAG_INITIALIZING;  
  204.             ap->pflags |= ATA_PFLAG_LOADING;  
  205.             ata_port_schedule_eh(ap);  
  206.   
  207.             spin_unlock_irqrestore(ap->lock, flags);  
  208.   
  209.             /* wait for EH to finish */  
  210.             ata_port_wait_eh(ap);  
  211.         } else {  
  212.             DPRINTK("ata%u: bus probe begin\n", ap->print_id);  
  213.             rc = ata_bus_probe(ap);  
  214.             DPRINTK("ata%u: bus probe end\n", ap->print_id);  
  215.   
  216.             if (rc) {  
  217.                 /* FIXME: do something useful here? 
  218.                  * Current libata behavior will 
  219.                  * tear down everything when 
  220.                  * the module is removed 
  221.                  * or the h/w is unplugged. 
  222.                  */  
  223.             }  
  224.         }  
  225.     }  
  226.   
  227.     /* probes are done, now scan each port's disk(s) */  
  228.     for (i = 0; i < host->n_ports; i++) {  
  229.         struct ata_port *ap = host->ports[i];  
  230.   
  231.         ata_scsi_scan_host(ap, 1);  
  232.         ata_lpm_schedule(ap, ap->pm_policy);  
  233.     }  
  234.   
  235.     return 0;  
  236. }  
  237. 在ata_scsi_add_hosts函数启动了error_handler内核线程,之后会在红色第二部分代码执行该线程,直到初始华完毕,第三部分主要初始化每个硬盘设备(包括分配硬盘设备节点等)。  
  238.   
  239. linux/driver/ata/libata-scsi.c  
  240. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)  
  241. {  
  242.     int i, rc;  
  243.   
  244.     for (i = 0; i < host->n_ports; i++) {  
  245.         struct ata_port *ap = host->ports[i];  
  246.         struct Scsi_Host *shost;  
  247.   
  248.         rc = -ENOMEM;  
  249.         shost = scsi_host_alloc(sht, sizeof(struct ata_port *));  
  250.         if (!shost)  
  251.             goto err_alloc;  
  252.   
  253.         *(struct ata_port **)&shost->hostdata[0] = ap;  
  254.         ap->scsi_host = shost;  
  255.   
  256.         shost->transportt = &ata_scsi_transport_template;  
  257.         shost->unique_id = ap->print_id;  
  258.         shost->max_id = 16;  
  259.         shost->max_lun = 1;  
  260.         shost->max_channel = 1;  
  261.         shost->max_cmd_len = 16;  
  262.   
  263.         /* Schedule policy is determined by ->qc_defer() 
  264.          * callback and it needs to see every deferred qc. 
  265.          * Set host_blocked to 1 to prevent SCSI midlayer from 
  266.          * automatically deferring requests. 
  267.          */  
  268.         shost->max_host_blocked = 1;  
  269.   
  270.         rc = scsi_add_host(ap->scsi_host, ap->host->dev);  
  271.         if (rc)  
  272.             goto err_add;  
  273.     }  
  274.   
  275.     return 0;  
  276.   
  277.  err_add:  
  278.     scsi_host_put(host->ports[i]->scsi_host);  
  279.  err_alloc:  
  280.     while (--i >= 0) {  
  281.         struct Scsi_Host *shost = host->ports[i]->scsi_host;  
  282.   
  283.         scsi_remove_host(shost);  
  284.         scsi_host_put(shost);  
  285.     }  
  286.     return rc;  
  287. }  
  288. ata_scsi_add_hosts主要初始化scsi层需要的结构,然后注册到scsi模块,完成scsi与ata的连接。  
  289.   
  290. linux/driver/scsi/hosts.c  
  291.   
  292. /** 
  293.  * scsi_host_alloc - register a scsi host adapter instance. 
  294.  * @sht:    pointer to scsi host template 
  295.  * @privsize:    extra bytes to allocate for driver 
  296.  * 
  297.  * Note: 
  298.  *     Allocate a new Scsi_Host and perform basic initialization. 
  299.  *     The host is not published to the scsi midlayer until scsi_add_host 
  300.  *     is called. 
  301.  * 
  302.  * Return value: 
  303.  *     Pointer to a new Scsi_Host 
  304.  **/  
  305. struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)  
  306. {  
  307.     struct Scsi_Host *shost;  
  308.     gfp_t gfp_mask = GFP_KERNEL;  
  309.     int rval;  
  310.   
  311.     if (sht->unchecked_isa_dma && privsize)  
  312.         gfp_mask |= __GFP_DMA;  
  313.   
  314.     shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);  
  315.     if (!shost)  
  316.         return NULL;  
  317.   
  318.     shost->host_lock = &shost->default_lock;  
  319.     spin_lock_init(shost->host_lock);  
  320.     shost->shost_state = SHOST_CREATED;  
  321.     INIT_LIST_HEAD(&shost->__devices);  
  322.     INIT_LIST_HEAD(&shost->__targets);  
  323.     INIT_LIST_HEAD(&shost->eh_cmd_q);  
  324.     INIT_LIST_HEAD(&shost->starved_list);  
  325.     init_waitqueue_head(&shost->host_wait);  
  326.   
  327.     mutex_init(&shost->scan_mutex);  
  328.   
  329.     shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */  
  330.     shost->dma_channel = 0xff;  
  331.   
  332.     /* These three are default values which can be overridden */  
  333.     shost->max_channel = 0;  
  334.     shost->max_id = 8;  
  335.     shost->max_lun = 8;  
  336.   
  337.     /* Give each shost a default transportt */  
  338.     shost->transportt = &blank_transport_template;  
  339.   
  340.     /* 
  341.      * All drivers right now should be able to handle 12 byte 
  342.      * commands.  Every so often there are requests for 16 byte 
  343.      * commands, but individual low-level drivers need to certify that 
  344.      * they actually do something sensible with such commands. 
  345.      */  
  346.     shost->max_cmd_len = 12;  
  347.     shost->hostt = sht;  
  348.     shost->this_id = sht->this_id;  
  349.     shost->can_queue = sht->can_queue;  
  350.     shost->sg_tablesize = sht->sg_tablesize;  
  351.     shost->cmd_per_lun = sht->cmd_per_lun;  
  352.     shost->unchecked_isa_dma = sht->unchecked_isa_dma;  
  353.     shost->use_clustering = sht->use_clustering;  
  354.     shost->ordered_tag = sht->ordered_tag;  
  355.     shost->active_mode = sht->supported_mode;  
  356.     shost->use_sg_chaining = sht->use_sg_chaining;  
  357.   
  358.     if (sht->supported_mode == MODE_UNKNOWN)  
  359.         /* means we didn't set it ... default to INITIATOR */  
  360.         shost->active_mode = MODE_INITIATOR;  
  361.     else  
  362.         shost->active_mode = sht->supported_mode;  
  363.   
  364.     if (sht->max_host_blocked)  
  365.         shost->max_host_blocked = sht->max_host_blocked;  
  366.     else  
  367.         shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;  
  368.   
  369.     /* 
  370.      * If the driver imposes no hard sector transfer limit, start at 
  371.      * machine infinity initially. 
  372.      */  
  373.     if (sht->max_sectors)  
  374.         shost->max_sectors = sht->max_sectors;  
  375.     else  
  376.         shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;  
  377.   
  378.     /* 
  379.      * assume a 4GB boundary, if not set 
  380.      */  
  381.     if (sht->dma_boundary)  
  382.         shost->dma_boundary = sht->dma_boundary;  
  383.     else  
  384.         shost->dma_boundary = 0xffffffff;  
  385.   
  386.     rval = scsi_setup_command_freelist(shost);  
  387.     if (rval)  
  388.         goto fail_kfree;  
  389.   
  390.     device_initialize(&shost->shost_gendev);  
  391.     snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",  
  392.         shost->host_no);  
  393.     shost->shost_gendev.release = scsi_host_dev_release;  
  394.   
  395.     class_device_initialize(&shost->shost_classdev);  
  396.     shost->shost_classdev.dev = &shost->shost_gendev;  
  397.     shost->shost_classdev.class = &shost_class;  
  398.     snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",  
  399.           shost->host_no);  
  400.   
  401.     shost->ehandler = kthread_run(scsi_error_handler, shost,  
  402.             "scsi_eh_%d", shost->host_no);  
  403.     if (IS_ERR(shost->ehandler)) {  
  404.         rval = PTR_ERR(shost->ehandler);  
  405.         goto fail_destroy_freelist;  
  406.     }  
  407.   
  408.     scsi_proc_hostdir_add(shost->hostt);  
  409.     return shost;  
  410.   
  411.  fail_destroy_freelist:  
  412.     scsi_destroy_command_freelist(shost);  
  413.  fail_kfree:  
  414.     kfree(shost);  
  415.     return NULL;  
  416. }  
  417. EXPORT_SYMBOL(scsi_host_alloc);  
  418. scsi_alloc_hosts执行完,内核即多了一个线程执行scsi_error_handler, ata_scsi_add_hosts继续初始话scsi_host结构体,其中:  
  419.         shost->transportt = &ata_scsi_transport_template;会在scsi_error_handler调用。  
  420.   
  421. /** 
  422.  * scsi_error_handler - SCSI error handler thread 
  423.  * @data:    Host for which we are running. 
  424.  * 
  425.  * Notes: 
  426.  *    This is the main error handling loop.  This is run as a kernel thread 
  427.  *    for every SCSI host and handles all error handling activity. 
  428.  **/  
  429. int scsi_error_handler(void *data)  
  430. {  
  431.     struct Scsi_Host *shost = data;  
  432.   
  433.     /* 
  434.      * We use TASK_INTERRUPTIBLE so that the thread is not 
  435.      * counted against the load average as a running process. 
  436.      * We never actually get interrupted because kthread_run 
  437.      * disables singal delivery for the created thread. 
  438.      */  
  439.     set_current_state(TASK_INTERRUPTIBLE);  
  440.     while (!kthread_should_stop()) {  
  441.         if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||  
  442.             shost->host_failed != shost->host_busy) {  
  443.             SCSI_LOG_ERROR_RECOVERY(1,  
  444.                 printk("Error handler scsi_eh_%d sleeping\n",  
  445.                     shost->host_no));  
  446.             schedule();  
  447.             set_current_state(TASK_INTERRUPTIBLE);  
  448.             continue;  
  449.         }  
  450.   
  451.         __set_current_state(TASK_RUNNING);  
  452.         SCSI_LOG_ERROR_RECOVERY(1,  
  453.             printk("Error handler scsi_eh_%d waking up\n",  
  454.                 shost->host_no));  
  455.   
  456.         /* 
  457.          * We have a host that is failing for some reason.  Figure out 
  458.          * what we need to do to get it up and online again (if we can). 
  459.          * If we fail, we end up taking the thing offline. 
  460.          */  
  461.         if (shost->transportt->eh_strategy_handler)  
  462.             shost->transportt->eh_strategy_handler(shost);  
  463.         else  
  464.             scsi_unjam_host(shost);  
  465.   
  466.         /* 
  467.          * Note - if the above fails completely, the action is to take 
  468.          * individual devices offline and flush the queue of any 
  469.          * outstanding requests that may have been pending.  When we 
  470.          * restart, we restart any I/O to any other devices on the bus 
  471.          * which are still online. 
  472.          */  
  473.         scsi_restart_operations(shost);  
  474.         set_current_state(TASK_INTERRUPTIBLE);  
  475.     }  
  476.     __set_current_state(TASK_RUNNING);  
  477.   
  478.     SCSI_LOG_ERROR_RECOVERY(1,  
  479.         printk("Error handler scsi_eh_%d exiting\n", shost->host_no));  
  480.     shost->ehandler = NULL;  
  481.     return 0;  
  482. }  
  483.   
  484.   
  485. 上次写到系统在运行scsi_error_handle线程之前的初始化过程,系统会在libata-core.c 函数ata_host_register中等待该线程的执行,现在我们从这个线程开始执行:  
  486.   
  487. linux/driver/scsi/scsi_error.c  
  488. /** 
  489.  * scsi_error_handler - SCSI error handler thread 
  490.  * @data:    Host for which we are running. 
  491.  * 
  492.  * Notes: 
  493.  *    This is the main error handling loop.  This is run as a kernel thread 
  494.  *    for every SCSI host and handles all error handling activity. 
  495.  **/  
  496. int scsi_error_handler(void *data)  
  497. {  
  498.     struct Scsi_Host *shost = data;  
  499.   
  500.     /* 
  501.      * We use TASK_INTERRUPTIBLE so that the thread is not 
  502.      * counted against the load average as a running process. 
  503.      * We never actually get interrupted because kthread_run 
  504.      * disables singal delivery for the created thread. 
  505.      */  
  506.     set_current_state(TASK_INTERRUPTIBLE);  
  507.     while (!kthread_should_stop()) {  
  508.         if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||  
  509.             shost->host_failed != shost->host_busy) {  
  510.             SCSI_LOG_ERROR_RECOVERY(1,  
  511.                 printk("Error handler scsi_eh_%d sleeping\n",  
  512.                     shost->host_no));  
  513.             schedule();  
  514.             set_current_state(TASK_INTERRUPTIBLE);  
  515.             continue;  
  516.         }  
  517.   
  518.         __set_current_state(TASK_RUNNING);  
  519.         SCSI_LOG_ERROR_RECOVERY(1,  
  520.             printk("Error handler scsi_eh_%d waking up\n",  
  521.                 shost->host_no));  
  522.   
  523.         /* 
  524.          * We have a host that is failing for some reason.  Figure out 
  525.          * what we need to do to get it up and online again (if we can). 
  526.          * If we fail, we end up taking the thing offline. 
  527.          */  
  528.         if (shost->transportt->eh_strategy_handler)  
  529.             shost->transportt->eh_strategy_handler(shost);  
  530.         else  
  531.             scsi_unjam_host(shost);  
  532.   
  533.         /* 
  534.          * Note - if the above fails completely, the action is to take 
  535.          * individual devices offline and flush the queue of any 
  536.          * outstanding requests that may have been pending.  When we 
  537.          * restart, we restart any I/O to any other devices on the bus 
  538.          * which are still online. 
  539.          */  
  540.         scsi_restart_operations(shost);  
  541.         set_current_state(TASK_INTERRUPTIBLE);  
  542.     }  
  543.     __set_current_state(TASK_RUNNING);  
  544.   
  545.     SCSI_LOG_ERROR_RECOVERY(1,  
  546.         printk("Error handler scsi_eh_%d exiting\n", shost->host_no));  
  547.     shost->ehandler = NULL;  
  548.     return 0;  
  549. }  
  550.   
  551. shost->transportt->eh_strategy_handler(shost); 此处是一个函数指针调用,在之前的文章我已经详细说了这个指针的由来,现在我们看一下这个指针的赋值:  
  552.   
  553. linux/driver/ata/libata-scsi.c  
  554.   
  555. /* 
  556.  * libata transport template.  libata doesn't do real transport stuff. 
  557.  * It just needs the eh_timed_out hook. 
  558.  */  
  559. static struct scsi_transport_template ata_scsi_transport_template = {  
  560.     .eh_strategy_handler    = ata_scsi_error,  
  561.     .eh_timed_out        = ata_scsi_timed_out,  
  562.     .user_scan        = ata_scsi_user_scan,  
  563. };  
  564.   
  565. 该结构在ata_scsi_add_host函数中被传递到上面,上一篇文章对此有提及,可以参考该系列的上一篇文章。  
  566.   
  567. 下面我们看一下ata_scsi_error的由来:  
  568.   
  569. linux/driver/ata/libata-eh.c  
  570. /** 
  571.  *    ata_scsi_error - SCSI layer error handler callback 
  572.  *    @host: SCSI host on which error occurred 
  573.  * 
  574.  *    Handles SCSI-layer-thrown error events. 
  575.  * 
  576.  *    LOCKING: 
  577.  *    Inherited from SCSI layer (none, can sleep) 
  578.  * 
  579.  *    RETURNS: 
  580.  *    Zero. 
  581.  */  
  582. void ata_scsi_error(struct Scsi_Host *host)  
  583. {  
  584.     struct ata_port *ap = ata_shost_to_port(host);  
  585.     int i;  
  586.     unsigned long flags;  
  587.   
  588.     DPRINTK("ENTER\n");  
  589.   
  590.     /* synchronize with port task */  
  591.     ata_port_flush_task(ap);  
  592.   
  593.     /* synchronize with host lock and sort out timeouts */  
  594.   
  595.     /* For new EH, all qcs are finished in one of three ways - 
  596.      * normal completion, error completion, and SCSI timeout. 
  597.      * Both cmpletions can race against SCSI timeout.  When normal 
  598.      * completion wins, the qc never reaches EH.  When error 
  599.      * completion wins, the qc has ATA_QCFLAG_FAILED set. 
  600.      * 
  601.      * When SCSI timeout wins, things are a bit more complex. 
  602.      * Normal or error completion can occur after the timeout but 
  603.      * before this point.  In such cases, both types of 
  604.      * completions are honored.  A scmd is determined to have 
  605.      * timed out iff its associated qc is active and not failed. 
  606.      */  
  607.     if (ap->ops->error_handler) {  
  608.         struct scsi_cmnd *scmd, *tmp;  
  609.         int nr_timedout = 0;  
  610.   
  611.         spin_lock_irqsave(ap->lock, flags);  
  612.   
  613.         list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {  
  614.             struct ata_queued_cmd *qc;  
  615.   
  616.             for (i = 0; i < ATA_MAX_QUEUE; i++) {  
  617.                 qc = __ata_qc_from_tag(ap, i);  
  618.                 if (qc->flags & ATA_QCFLAG_ACTIVE &&  
  619.                     qc->scsicmd == scmd)  
  620.                     break;  
  621.             }  
  622.   
  623.             if (i < ATA_MAX_QUEUE) {  
  624.                 /* the scmd has an associated qc */  
  625.                 if (!(qc->flags & ATA_QCFLAG_FAILED)) {  
  626.                     /* which hasn't failed yet, timeout */  
  627.                     qc->err_mask |= AC_ERR_TIMEOUT;  
  628.                     qc->flags |= ATA_QCFLAG_FAILED;  
  629.                     nr_timedout++;  
  630.                 }  
  631.             } else {  
  632.                 /* Normal completion occurred after 
  633.                  * SCSI timeout but before this point. 
  634.                  * Successfully complete it. 
  635.                  */  
  636.                 scmd->retries = scmd->allowed;  
  637.                 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);  
  638.             }  
  639.         }  
  640.   
  641.         /* If we have timed out qcs.  They belong to EH from 
  642.          * this point but the state of the controller is 
  643.          * unknown.  Freeze the port to make sure the IRQ 
  644.          * handler doesn't diddle with those qcs.  This must 
  645.          * be done atomically w.r.t. setting QCFLAG_FAILED. 
  646.          */  
  647.         if (nr_timedout)  
  648.             __ata_port_freeze(ap);  
  649.   
  650.         spin_unlock_irqrestore(ap->lock, flags);  
  651.   
  652.         /* initialize eh_tries */  
  653.         ap->eh_tries = ATA_EH_MAX_TRIES;  
  654.     } else  
  655.         spin_unlock_wait(ap->lock);  
  656.   
  657.  repeat:  
  658.     /* invoke error handler */  
  659.     if (ap->ops->error_handler) {  
  660.         struct ata_link *link;  
  661.   
  662.         /* kill fast drain timer */  
  663.         del_timer_sync(&ap->fastdrain_timer);  
  664.   
  665.         /* process port resume request */  
  666.         ata_eh_handle_port_resume(ap);  
  667.   
  668.         /* fetch & clear EH info */  
  669.         spin_lock_irqsave(ap->lock, flags);  
  670.   
  671.         __ata_port_for_each_link(link, ap) {  
  672.             memset(&link->eh_context, 0, sizeof(link->eh_context));  
  673.             link->eh_context.i = link->eh_info;  
  674.             memset(&link->eh_info, 0, sizeof(link->eh_info));  
  675.         }  
  676.   
  677.         ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;  
  678.         ap->pflags &= ~ATA_PFLAG_EH_PENDING;  
  679.         ap->excl_link = NULL;    /* don't maintain exclusion over EH */  
  680.   
  681.         spin_unlock_irqrestore(ap->lock, flags);  
  682.   
  683.         /* invoke EH, skip if unloading or suspended */  
  684.         if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))  
  685.             ap->ops->error_handler(ap);  
  686.         else  
  687.             ata_eh_finish(ap);  
  688.   
  689.         /* process port suspend request */  
  690.         ata_eh_handle_port_suspend(ap);  
  691.   
  692.         /* Exception might have happend after ->error_handler 
  693.          * recovered the port but before this point.  Repeat 
  694.          * EH in such case. 
  695.          */  
  696.         spin_lock_irqsave(ap->lock, flags);  
  697.   
  698.         if (ap->pflags & ATA_PFLAG_EH_PENDING) {  
  699.             if (--ap->eh_tries) {  
  700.                 spin_unlock_irqrestore(ap->lock, flags);  
  701.                 goto repeat;  
  702.             }  
  703.             ata_port_printk(ap, KERN_ERR, "EH pending after %d "  
  704.                     "tries, giving up\n", ATA_EH_MAX_TRIES);  
  705.             ap->pflags &= ~ATA_PFLAG_EH_PENDING;  
  706.         }  
  707.   
  708.         /* this run is complete, make sure EH info is clear */  
  709.         __ata_port_for_each_link(link, ap)  
  710.             memset(&link->eh_info, 0, sizeof(link->eh_info));  
  711.   
  712.         /* Clear host_eh_scheduled while holding ap->lock such 
  713.          * that if exception occurs after this point but 
  714.          * before EH completion, SCSI midlayer will 
  715.          * re-initiate EH. 
  716.          */  
  717.         host->host_eh_scheduled = 0;  
  718.   
  719.         spin_unlock_irqrestore(ap->lock, flags);  
  720.     } else {  
  721.         WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);  
  722.         ap->ops->eng_timeout(ap);  
  723.     }  
  724.   
  725.     /* finish or retry handled scmd's and clean up */  
  726.     WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));  
  727.   
  728.     scsi_eh_flush_done_q(&ap->eh_done_q);  
  729.   
  730.     /* clean up */  
  731.     spin_lock_irqsave(ap->lock, flags);  
  732.   
  733.     if (ap->pflags & ATA_PFLAG_LOADING)  
  734.         ap->pflags &= ~ATA_PFLAG_LOADING;  
  735.     else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)  
  736.         queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);  
  737.   
  738.     if (ap->pflags & ATA_PFLAG_RECOVERED)  
  739.         ata_port_printk(ap, KERN_INFO, "EH complete\n");  
  740.   
  741.     ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);  
  742.   
  743.     /* tell wait_eh that we're done */  
  744.     ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;  
  745.     wake_up_all(&ap->eh_wait_q);  
  746.   
  747.     spin_unlock_irqrestore(ap->lock, flags);  
  748.   
  749.     DPRINTK("EXIT\n");  
  750. }  
  751. 注意在这个函数的后面唤醒了之前我们强制等待的初始化主线程,这个函数执行完后这个内核线程执行完了一个周期,等待下一次的唤醒。 我们继续分析这个函数中是如何开始扫描每一个sata口的。  
  752.   
  753. linux/driver/ata/sata_fsl.c  
  754.   
  755. static const struct ata_port_info sata_fsl_port_info[] = {  
  756.     {  
  757.      .flags = SATA_FSL_HOST_FLAGS,  
  758.      .link_flags = SATA_FSL_HOST_LFLAGS,  
  759.      .pio_mask = 0x1f,    /* pio 0-4 */  
  760.      .udma_mask = 0x7f,    /* udma 0-6 */  
  761.      .port_ops = &sata_fsl_ops,  
  762.      },  
  763. };  
  764.   
  765. static const struct ata_port_operations sata_fsl_ops = {  
  766.     .check_status = sata_fsl_check_status,  
  767.     .check_altstatus = sata_fsl_check_status,  
  768.     .dev_select = ata_noop_dev_select,  
  769.   
  770.     .tf_read = sata_fsl_tf_read,  
  771.   
  772.     .qc_prep = sata_fsl_qc_prep,  
  773.     .qc_issue = sata_fsl_qc_issue,  
  774.     .irq_clear = sata_fsl_irq_clear,  
  775.   
  776.     .scr_read = sata_fsl_scr_read,  
  777.     .scr_write = sata_fsl_scr_write,  
  778.   
  779.     .freeze = sata_fsl_freeze,  
  780.     .thaw = sata_fsl_thaw,  
  781.     .error_handler = sata_fsl_error_handler,  
  782.     .post_internal_cmd = sata_fsl_post_internal_cmd,  
  783.   
  784.     .port_start = sata_fsl_port_start,  
  785.     .port_stop = sata_fsl_port_stop,  
  786.   
  787.     .pmp_attach = sata_fsl_pmp_attach,  
  788.     .pmp_detach = sata_fsl_pmp_detach,  
  789. };  
  790.   
  791. static void sata_fsl_error_handler(struct ata_port *ap)  
  792. {  
  793.   
  794.     DPRINTK("in xx_error_handler\n");  
  795.   
  796.     /* perform recovery */  
  797.     sata_pmp_do_eh(ap, ata_std_prereset, sata_fsl_softreset,  
  798.                sata_std_hardreset, ata_std_postreset,  
  799.                sata_pmp_std_prereset, sata_fsl_pmp_softreset,  
  800.                sata_pmp_std_hardreset, sata_pmp_std_postreset);  
  801. }  
  802. 这个函数比较简单,是在在驱动中提供了sata_pmp_do_eh的一个接口, 因为mpc8315 CPU SATA 接口支持PM功能,所以这里使用pmp模块的函数处理。  
  803.   
  804. /linux/driver/libata-pmp.c  
  805.   
  806. /** 
  807.  *    sata_pmp_do_eh - do standard error handling for PMP-enabled host 
  808.  *    @ap: host port to handle error for 
  809.  *    @prereset: prereset method (can be NULL) 
  810.  *    @softreset: softreset method 
  811.  *    @hardreset: hardreset method 
  812.  *    @postreset: postreset method (can be NULL) 
  813.  *    @pmp_prereset: PMP prereset method (can be NULL) 
  814.  *    @pmp_softreset: PMP softreset method (can be NULL) 
  815.  *    @pmp_hardreset: PMP hardreset method (can be NULL) 
  816.  *    @pmp_postreset: PMP postreset method (can be NULL) 
  817.  * 
  818.  *    Perform standard error handling sequence for PMP-enabled host 
  819.  *    @ap. 
  820.  * 
  821.  *    LOCKING: 
  822.  *    Kernel thread context (may sleep). 
  823.  */  
  824. void sata_pmp_do_eh(struct ata_port *ap,  
  825.         ata_prereset_fn_t prereset, ata_reset_fn_t softreset,  
  826.         ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,  
  827.         ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,  
  828.         ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)  
  829. {  
  830.     DPRINTK("ENTER\n");  
  831.     ata_eh_autopsy(ap);  
  832.     ata_eh_report(ap);  
  833.     sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,  
  834.                 pmp_prereset, pmp_softreset, pmp_hardreset,  
  835.                 pmp_postreset);  
  836.     ata_eh_finish(ap);  
  837.     DPRINTK("EXIT\n");  
  838. }  
  839. 到这里往下的部分已经很简单了,读者可以自己查找代码进一步的阅读,我们主要分析函数sata_pmp_eh_recover.  
  840.   
  841.   
  842. /** 
  843.  *    sata_pmp_eh_recover - recover PMP-enabled port 
  844.  *    @ap: ATA port to recover 
  845.  *    @prereset: prereset method (can be NULL) 
  846.  *    @softreset: softreset method 
  847.  *    @hardreset: hardreset method 
  848.  *    @postreset: postreset method (can be NULL) 
  849.  *    @pmp_prereset: PMP prereset method (can be NULL) 
  850.  *    @pmp_softreset: PMP softreset method (can be NULL) 
  851.  *    @pmp_hardreset: PMP hardreset method (can be NULL) 
  852.  *    @pmp_postreset: PMP postreset method (can be NULL) 
  853.  * 
  854.  *    Drive EH recovery operation for PMP enabled port @ap.  This 
  855.  *    function recovers host and PMP ports with proper retrials and 
  856.  *    fallbacks.  Actual recovery operations are performed using 
  857.  *    ata_eh_recover() and sata_pmp_eh_recover_pmp(). 
  858.  * 
  859.  *    LOCKING: 
  860.  *    Kernel thread context (may sleep). 
  861.  * 
  862.  *    RETURNS: 
  863.  *    0 on success, -errno on failure. 
  864.  */  
  865. static int sata_pmp_eh_recover(struct ata_port *ap,  
  866.         ata_prereset_fn_t prereset, ata_reset_fn_t softreset,  
  867.         ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,  
  868.         ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,  
  869.         ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)  
  870. {  
  871.     int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];  
  872.     struct ata_link *pmp_link = &ap->link;  
  873.     struct ata_device *pmp_dev = pmp_link->device;  
  874.     struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;  
  875.     struct ata_link *link;  
  876.     struct ata_device *dev;  
  877.     unsigned int err_mask;  
  878.     u32 gscr_error, sntf;  
  879.     int cnt, rc;  
  880.   
  881.     pmp_tries = ATA_EH_PMP_TRIES;  
  882.     ata_port_for_each_link(link, ap)  
  883.         link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;  
  884.   
  885.  retry:  
  886.     /* PMP attached? */  
  887.     if (!ap->nr_pmp_links) {  
  888.         rc = ata_eh_recover(ap, prereset, softreset, hardreset,  
  889.                     postreset, NULL);  
  890.         if (rc) {  
  891.             ata_link_for_each_dev(dev, &ap->link)  
  892.                 ata_dev_disable(dev);  
  893.             return rc;  
  894.         }  
  895.   
  896.         if (pmp_dev->class != ATA_DEV_PMP)  
  897.             return 0;  
  898.   
  899.         /* new PMP online */  
  900.         ata_port_for_each_link(link, ap)  
  901.             link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;  
  902.   
  903.         /* fall through */  
  904.     }  
  905.   
  906.     /* recover pmp */  
  907.     rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,  
  908.                      postreset);  
  909.     if (rc)  
  910.         goto pmp_fail;  
  911.   
  912.     /* handle disabled links */  
  913.     rc = sata_pmp_eh_handle_disabled_links(ap);  
  914.     if (rc)  
  915.         goto pmp_fail;  
  916.   
  917.     /* recover links */  
  918.     rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,  
  919.                 pmp_postreset, &link);  
  920.     if (rc)  
  921.         goto link_fail;  
  922.   
  923.     /* Connection status might have changed while resetting other 
  924.      * links, check SATA_PMP_GSCR_ERROR before returning. 
  925.      */  
  926.   
  927.     /* clear SNotification */  
  928.     rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);  
  929.     if (rc == 0)  
  930.         sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);  
  931.   
  932.     /* enable notification */  
  933.     if (pmp_dev->flags & ATA_DFLAG_AN) {  
  934.         pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;  
  935.   
  936.         err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,  
  937.                       pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);  
  938.         if (err_mask) {  
  939.             ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "  
  940.                        "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);  
  941.             rc = -EIO;  
  942.             goto pmp_fail;  
  943.         }  
  944.     }  
  945.   
  946.     /* check GSCR_ERROR */  
  947.     err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);  
  948.     if (err_mask) {  
  949.         ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "  
  950.                    "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);  
  951.         rc = -EIO;  
  952.         goto pmp_fail;  
  953.     }  
  954.   
  955.     cnt = 0;  
  956.     ata_port_for_each_link(link, ap) {  
  957.         if (!(gscr_error & (1 << link->pmp)))  
  958.             continue;  
  959.   
  960.         if (sata_pmp_handle_link_fail(link, link_tries)) {  
  961.             ata_ehi_hotplugged(&link->eh_context.i);  
  962.             cnt++;  
  963.         } else {  
  964.             ata_link_printk(link, KERN_WARNING,  
  965.                 "PHY status changed but maxed out on retries, "  
  966.                 "giving up\n");  
  967.             ata_link_printk(link, KERN_WARNING,  
  968.                 "Manully issue scan to resume this link\n");  
  969.         }  
  970.     }  
  971.   
  972.     if (cnt) {  
  973.         ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "  
  974.                 "ports, repeating recovery\n");  
  975.         goto retry;  
  976.     }  
  977.   
  978.     return 0;  
  979.   
  980.  link_fail:  
  981.     if (sata_pmp_handle_link_fail(link, link_tries)) {  
  982.         pmp_ehc->i.action |= ATA_EH_HARDRESET;  
  983.         goto retry;  
  984.     }  
  985.   
  986.     /* fall through */  
  987.  pmp_fail:  
  988.     /* Control always ends up here after detaching PMP.  Shut up 
  989.      * and return if we're unloading. 
  990.      */  
  991.     if (ap->pflags & ATA_PFLAG_UNLOADING)  
  992.         return rc;  
  993.   
  994.     if (!ap->nr_pmp_links)  
  995.         goto retry;  
  996.   
  997.     if (--pmp_tries) {  
  998.         ata_port_printk(ap, KERN_WARNING,  
  999.                 "failed to recover PMP, retrying in 5 secs\n");  
  1000.         pmp_ehc->i.action |= ATA_EH_HARDRESET;  
  1001.         ssleep(5);  
  1002.         goto retry;  
  1003.     }  
  1004.   
  1005.     ata_port_printk(ap, KERN_ERR,  
  1006.             "failed to recover PMP after %d tries, giving up\n",  
  1007.             ATA_EH_PMP_TRIES);  
  1008.     sata_pmp_detach(pmp_dev);  
  1009.     ata_dev_disable(pmp_dev);  
  1010.   
  1011.     return rc;  
  1012. }  
  1013.   
  1014. /linux/driver/ata/libata_eh.c  
  1015. /** 
  1016.  *    ata_eh_recover - recover host port after error 
  1017.  *    @ap: host port to recover 
  1018.  *    @prereset: prereset method (can be NULL) 
  1019.  *    @softreset: softreset method (can be NULL) 
  1020.  *    @hardreset: hardreset method (can be NULL) 
  1021.  *    @postreset: postreset method (can be NULL) 
  1022.  *    @r_failed_link: out parameter for failed link 
  1023.  * 
  1024.  *    This is the alpha and omega, eum and yang, heart and soul of 
  1025.  *    libata exception handling.  On entry, actions required to 
  1026.  *    recover each link and hotplug requests are recorded in the 
  1027.  *    link's eh_context.  This function executes all the operations 
  1028.  *    with appropriate retrials and fallbacks to resurrect failed 
  1029.  *    devices, detach goners and greet newcomers. 
  1030.  * 
  1031.  *    LOCKING: 
  1032.  *    Kernel thread context (may sleep). 
  1033.  * 
  1034.  *    RETURNS: 
  1035.  *    0 on success, -errno on failure. 
  1036.  */  
  1037. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,  
  1038.            ata_reset_fn_t softreset, ata_reset_fn_t hardreset,  
  1039.            ata_postreset_fn_t postreset,  
  1040.            struct ata_link **r_failed_link)  
  1041. {  
  1042.     struct ata_link *link;  
  1043.     struct ata_device *dev;  
  1044.     int nr_failed_devs, nr_disabled_devs;  
  1045.     int reset, rc;  
  1046.     unsigned long flags;  
  1047.   
  1048.     DPRINTK("ENTER\n");  
  1049.   
  1050.     /* prep for recovery */  
  1051.     ata_port_for_each_link(link, ap) {  
  1052.         struct ata_eh_context *ehc = &link->eh_context;  
  1053.   
  1054.         /* re-enable link? */  
  1055.         if (ehc->i.action & ATA_EH_ENABLE_LINK) {  
  1056.             ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);  
  1057.             spin_lock_irqsave(ap->lock, flags);  
  1058.             link->flags &= ~ATA_LFLAG_DISABLED;  
  1059.             spin_unlock_irqrestore(ap->lock, flags);  
  1060.             ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);  
  1061.         }  
  1062.   
  1063.         ata_link_for_each_dev(dev, link) {  
  1064.             if (link->flags & ATA_LFLAG_NO_RETRY)  
  1065.                 ehc->tries[dev->devno] = 1;  
  1066.             else  
  1067.                 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;  
  1068.   
  1069.             /* collect port action mask recorded in dev actions */  
  1070.             ehc->i.action |= ehc->i.dev_action[dev->devno] &  
  1071.                      ~ATA_EH_PERDEV_MASK;  
  1072.             ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;  
  1073.   
  1074.             /* process hotplug request */  
  1075.             if (dev->flags & ATA_DFLAG_DETACH)  
  1076.                 ata_eh_detach_dev(dev);  
  1077.   
  1078.             if (!ata_dev_enabled(dev) &&  
  1079.                 ((ehc->i.probe_mask & (1 << dev->devno)) &&  
  1080.                  !(ehc->did_probe_mask & (1 << dev->devno)))) {  
  1081.                 ata_eh_detach_dev(dev);  
  1082.                 ata_dev_init(dev);  
  1083.                 ehc->did_probe_mask |= (1 << dev->devno);  
  1084.                 ehc->i.action |= ATA_EH_SOFTRESET;  
  1085.             }  
  1086.         }  
  1087.     }  
  1088.   
  1089.  retry:  
  1090.     rc = 0;  
  1091.     nr_failed_devs = 0;  
  1092.     nr_disabled_devs = 0;  
  1093.     reset = 0;  
  1094.   
  1095.     /* if UNLOADING, finish immediately */  
  1096.     if (ap->pflags & ATA_PFLAG_UNLOADING)  
  1097.         goto out;  
  1098.   
  1099.     /* prep for EH */  
  1100.     ata_port_for_each_link(link, ap) {  
  1101.         struct ata_eh_context *ehc = &link->eh_context;  
  1102.   
  1103.         /* skip EH if possible. */  
  1104.         if (ata_eh_skip_recovery(link))  
  1105.             ehc->i.action = 0;  
  1106.   
  1107.         /* do we need to reset? */  
  1108.         if (ehc->i.action & ATA_EH_RESET_MASK)  
  1109.             reset = 1;  
  1110.   
  1111.         ata_link_for_each_dev(dev, link)  
  1112.             ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;  
  1113.     }  
  1114.   
  1115.     /* reset */  
  1116.     if (reset) {  
  1117.         /* if PMP is attached, this function only deals with 
  1118.          * downstream links, port should stay thawed. 
  1119.          */  
  1120.         if (!ap->nr_pmp_links)  
  1121.             ata_eh_freeze_port(ap);  
  1122.   
  1123.         ata_port_for_each_link(link, ap) {  
  1124.             struct ata_eh_context *ehc = &link->eh_context;  
  1125.   
  1126.             if (!(ehc->i.action & ATA_EH_RESET_MASK))  
  1127.                 continue;  
  1128.   
  1129.             rc = ata_eh_reset(link, ata_link_nr_vacant(link),  
  1130.                       prereset, softreset, hardreset,  
  1131.                       postreset);  
  1132.             if (rc) {  
  1133.                 ata_link_printk(link, KERN_ERR,  
  1134.                         "reset failed, giving up\n");  
  1135.                 goto out;  
  1136.             }  
  1137.         }  
  1138.   
  1139.         if (!ap->nr_pmp_links)  
  1140.             ata_eh_thaw_port(ap);  
  1141.     }  
  1142.   
  1143.     /* the rest */  
  1144.     ata_port_for_each_link(link, ap) {  
  1145.         struct ata_eh_context *ehc = &link->eh_context;  
  1146.   
  1147.         /* revalidate existing devices and attach new ones */  
  1148.         rc = ata_eh_revalidate_and_attach(link, &dev);  
  1149.         if (rc)  
  1150.             goto dev_fail;  
  1151.   
  1152.         /* if PMP got attached, return, pmp EH will take care of it */  
  1153.         if (link->device->class == ATA_DEV_PMP) {  
  1154.             ehc->i.action = 0;  
  1155.             return 0;  
  1156.         }  
  1157.   
  1158.         /* configure transfer mode if necessary */  
  1159.         if (ehc->i.flags & ATA_EHI_SETMODE) {  
  1160.             rc = ata_set_mode(link, &dev);  
  1161.             if (rc)  
  1162.                 goto dev_fail;  
  1163.             ehc->i.flags &= ~ATA_EHI_SETMODE;  
  1164.         }  
  1165.   
  1166.         if (ehc->i.action & ATA_EHI_LPM)  
  1167.             ata_link_for_each_dev(dev, link)  
  1168.                 ata_dev_enable_pm(dev, ap->pm_policy);  
  1169.   
  1170.         /* this link is okay now */  
  1171.         ehc->i.flags = 0;  
  1172.         continue;  
  1173.   
  1174. dev_fail:  
  1175.         nr_failed_devs++;  
  1176.         if (ata_eh_handle_dev_fail(dev, rc))  
  1177.             nr_disabled_devs++;  
  1178.   
  1179.         if (ap->pflags & ATA_PFLAG_FROZEN) {  
  1180.             /* PMP reset requires working host port. 
  1181.              * Can't retry if it's frozen. 
  1182.              */  
  1183.             if (ap->nr_pmp_links)  
  1184.                 goto out;  
  1185.             break;  
  1186.         }  
  1187.     }  
  1188.   
  1189.     if (nr_failed_devs) {  
  1190.         if (nr_failed_devs != nr_disabled_devs) {  
  1191.             ata_port_printk(ap, KERN_WARNING, "failed to recover "  
  1192.                     "some devices, retrying in 5 secs\n");  
  1193.             ssleep(5);  
  1194.         } else {  
  1195.             /* no device left to recover, repeat fast */  
  1196.             msleep(500);  
  1197.         }  
  1198.   
  1199.         goto retry;  
  1200.     }  
  1201.   
  1202.  out:  
  1203.     if (rc && r_failed_link)  
  1204.         *r_failed_link = link;  
  1205.   
  1206.     DPRINTK("EXIT, rc=%d\n", rc);  
  1207.     return rc;  
  1208. }  
  1209.   
  1210. int ata_eh_reset(struct ata_link *link, int classify,  
  1211.          ata_prereset_fn_t prereset, ata_reset_fn_t softreset,  
  1212.          ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)  
  1213. {  
  1214.     const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);  
  1215.     struct ata_port *ap = link->ap;  
  1216.     struct ata_eh_context *ehc = &link->eh_context;  
  1217.     unsigned int *classes = ehc->classes;  
  1218.     unsigned int lflags = link->flags;  
  1219.     int verbose = !(ehc->i.flags & ATA_EHI_QUIET);  
  1220.     int try = 0;  
  1221.     struct ata_device *dev;  
  1222.     unsigned long deadline, now;  
  1223.     unsigned int tmp_action;  
  1224.     ata_reset_fn_t reset;  
  1225.     unsigned long flags;  
  1226.     u32 sstatus;  
  1227.     int rc;  
  1228.   
  1229.     /* about to reset */  
  1230.     spin_lock_irqsave(ap->lock, flags);  
  1231.     ap->pflags |= ATA_PFLAG_RESETTING;  
  1232.     spin_unlock_irqrestore(ap->lock, flags);  
  1233.   
  1234.     ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);  
  1235.   
  1236.     ata_link_for_each_dev(dev, link) {  
  1237.         /* If we issue an SRST then an ATA drive (not ATAPI) 
  1238.          * may change configuration and be in PIO0 timing. If 
  1239.          * we do a hard reset (or are coming from power on) 
  1240.          * this is true for ATA or ATAPI. Until we've set a 
  1241.          * suitable controller mode we should not touch the 
  1242.          * bus as we may be talking too fast. 
  1243.          */  
  1244.         dev->pio_mode = XFER_PIO_0;  
  1245.   
  1246.         /* If the controller has a pio mode setup function 
  1247.          * then use it to set the chipset to rights. Don't 
  1248.          * touch the DMA setup as that will be dealt with when 
  1249.          * configuring devices. 
  1250.          */  
  1251.         if (ap->ops->set_piomode)  
  1252.             ap->ops->set_piomode(ap, dev);  
  1253.     }  
  1254.   
  1255.     /* Determine which reset to use and record in ehc->i.action. 
  1256.      * prereset() may examine and modify it. 
  1257.      */  
  1258.     if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&  
  1259.                      !sata_set_spd_needed(link) &&  
  1260.                      !(ehc->i.action & ATA_EH_HARDRESET))))  
  1261.         tmp_action = ATA_EH_SOFTRESET;  
  1262.     else  
  1263.         tmp_action = ATA_EH_HARDRESET;  
  1264.   
  1265.     ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;  
  1266.   
  1267.     if (prereset) {  
  1268.         rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);  
  1269.         if (rc) {  
  1270.             if (rc == -ENOENT) {  
  1271.                 ata_link_printk(link, KERN_DEBUG,  
  1272.                         "port disabled. ignoring.\n");  
  1273.                 ehc->i.action &= ~ATA_EH_RESET_MASK;  
  1274.   
  1275.                 ata_link_for_each_dev(dev, link)  
  1276.                     classes[dev->devno] = ATA_DEV_NONE;  
  1277.   
  1278.                 rc = 0;  
  1279.             } else  
  1280.                 ata_link_printk(link, KERN_ERR,  
  1281.                     "prereset failed (errno=%d)\n", rc);  
  1282.             goto out;  
  1283.         }  
  1284.     }  
  1285.   
  1286.     /* prereset() might have modified ehc->i.action */  
  1287.     if (ehc->i.action & ATA_EH_HARDRESET)  
  1288.         reset = hardreset;  
  1289.     else if (ehc->i.action & ATA_EH_SOFTRESET)  
  1290.         reset = softreset;  
  1291.     else {  
  1292.         /* prereset told us not to reset, bang classes and return */  
  1293.         ata_link_for_each_dev(dev, link)  
  1294.             classes[dev->devno] = ATA_DEV_NONE;  
  1295.         rc = 0;  
  1296.         goto out;  
  1297.     }  
  1298.   
  1299.     /* did prereset() screw up?  if so, fix up to avoid oopsing */  
  1300.     if (!reset) {  
  1301.         if (softreset)  
  1302.             reset = softreset;  
  1303.         else  
  1304.             reset = hardreset;  
  1305.     }  
  1306.   
  1307.  retry:  
  1308.     deadline = jiffies + ata_eh_reset_timeouts[try++];  
  1309.   
  1310.     /* shut up during boot probing */  
  1311.     if (verbose)  
  1312.         ata_link_printk(link, KERN_INFO, "%s resetting link\n",  
  1313.                 reset == softreset ? "soft" : "hard");  
  1314.   
  1315.     /* mark that this EH session started with reset */  
  1316.     if (reset == hardreset)  
  1317.         ehc->i.flags |= ATA_EHI_DID_HARDRESET;  
  1318.     else  
  1319.         ehc->i.flags |= ATA_EHI_DID_SOFTRESET;  
  1320.   
  1321.     rc = ata_do_reset(link, reset, classes, deadline);  
  1322.   
  1323.     if (reset == hardreset &&  
  1324.         ata_eh_followup_srst_needed(link, rc, classify, classes)) {  
  1325.         /* okay, let's do follow-up softreset */  
  1326.         reset = softreset;  
  1327.   
  1328.         if (!reset) {  
  1329.             ata_link_printk(link, KERN_ERR,  
  1330.                     "follow-up softreset required "  
  1331.                     "but no softreset avaliable\n");  
  1332.             rc = -EINVAL;  
  1333.             goto fail;  
  1334.         }  
  1335.   
  1336.         ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);  
  1337.         rc = ata_do_reset(link, reset, classes, deadline);  
  1338.     }  
  1339.   
  1340.     /* -EAGAIN can happen if we skipped followup SRST */  
  1341.     if (rc && rc != -EAGAIN)  
  1342.         goto fail;  
  1343.   
  1344.     /* was classification successful? */  
  1345.     if (classify && classes[0] == ATA_DEV_UNKNOWN &&  
  1346.         !(lflags & ATA_LFLAG_ASSUME_CLASS)) {  
  1347.         if (try < max_tries) {  
  1348.             ata_link_printk(link, KERN_WARNING,  
  1349.                     "classification failed\n");  
  1350.             rc = -EINVAL;  
  1351.             goto fail;  
  1352.         }  
  1353.   
  1354.         ata_link_printk(link, KERN_WARNING,  
  1355.                 "classfication failed, assuming ATA\n");  
  1356.         lflags |= ATA_LFLAG_ASSUME_ATA;  
  1357.     }  
  1358.   
  1359.     ata_link_for_each_dev(dev, link) {  
  1360.         /* After the reset, the device state is PIO 0 and the 
  1361.          * controller state is undefined.  Reset also wakes up 
  1362.          * drives from sleeping mode. 
  1363.          */  
  1364.         dev->pio_mode = XFER_PIO_0;  
  1365.         dev->flags &= ~ATA_DFLAG_SLEEPING;  
  1366.   
  1367.         if (ata_link_offline(link))  
  1368.             continue;  
  1369.   
  1370.         /* apply class override */  
  1371.         if (lflags & ATA_LFLAG_ASSUME_ATA)  
  1372.             classes[dev->devno] = ATA_DEV_ATA;  
  1373.         else if (lflags & ATA_LFLAG_ASSUME_SEMB)  
  1374.             classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */  
  1375.     }  
  1376.   
  1377.     /* record current link speed */  
  1378.     if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)  
  1379.         link->sata_spd = (sstatus >> 4) & 0xf;  
  1380.   
  1381.     if (postreset)  
  1382.         postreset(link, classes);  
  1383.   
  1384.     /* reset successful, schedule revalidation */  
  1385.     ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);  
  1386.     ehc->i.action |= ATA_EH_REVALIDATE;  
  1387.   
  1388.     rc = 0;  
  1389.  out:  
  1390.     /* clear hotplug flag */  
  1391.     ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;  
  1392.   
  1393.     spin_lock_irqsave(ap->lock, flags);  
  1394.     ap->pflags &= ~ATA_PFLAG_RESETTING;  
  1395.     spin_unlock_irqrestore(ap->lock, flags);  
  1396.   
  1397.     return rc;  
  1398.   
  1399.  fail:  
  1400.     if (rc == -ERESTART || try >= max_tries)  
  1401.         goto out;  
  1402.   
  1403.     now = jiffies;  
  1404.     if (time_before(now, deadline)) {  
  1405.         unsigned long delta = deadline - now;  
  1406.   
  1407.         ata_link_printk(link, KERN_WARNING, "reset failed "  
  1408.                 "(errno=%d), retrying in %u secs\n",  
  1409.                 rc, (jiffies_to_msecs(delta) + 999) / 1000);  
  1410.   
  1411.         while (delta)  
  1412.             delta = schedule_timeout_uninterruptible(delta);  
  1413.     }  
  1414.   
  1415.     if (rc == -EPIPE || try == max_tries - 1)  
  1416.         sata_down_spd_limit(link);  
  1417.     if (hardreset)  
  1418.         reset = hardreset;  
  1419.     goto retry;  
  1420. }  
  1421.   
  1422. 这个函数比较复杂,读者可以自己分析下, 由于篇幅所限就不一一分析下去了。  
  1423.   
  1424. 因为本文讨论的大部分都是scsi模块的内容,所以将名字更改,但是这是之前两篇文章的继续, libata是scsi的子系统。  
  1425.   
  1426. scsi部分主要是重要的结构的建立, 启动层上其下的作用。  
  1427.   
  1428. linux/driver/scsi/sd.c  
  1429. 这个模块初始化早于sata驱动的初始化,我们看一下该模块的初始化函数。  
  1430.   
  1431. /** 
  1432.  *    init_sd - entry point for this driver (both when built in or when 
  1433.  *    a module). 
  1434.  * 
  1435.  *    Note: this function registers this driver with the scsi mid-level. 
  1436.  **/  
  1437. static int __init init_sd(void)  
  1438. {  
  1439.     int majors = 0, i, err;  
  1440.   
  1441.     SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));  
  1442.   
  1443.     for (i = 0; i < SD_MAJORS; i++)  
  1444.         if (register_blkdev(sd_major(i), "sd") == 0)  
  1445.             majors++;  
  1446.   
  1447.     if (!majors)  
  1448.         return -ENODEV;  
  1449.   
  1450.     err = class_register(&sd_disk_class);  
  1451.     if (err)  
  1452.         goto err_out;  
  1453.   
  1454.     err = scsi_register_driver(&sd_template.gendrv);  
  1455.     if (err)  
  1456.         goto err_out_class;  
  1457.   
  1458.     return 0;  
  1459.   
  1460. err_out_class:  
  1461.     class_unregister(&sd_disk_class);  
  1462. err_out:  
  1463.     for (i = 0; i < SD_MAJORS; i++)  
  1464.         unregister_blkdev(sd_major(i), "sd");  
  1465.     return err;  
  1466. }  
  1467.   
  1468. static struct scsi_driver sd_template = {  
  1469.     .owner            = THIS_MODULE,  
  1470.     .gendrv = {  
  1471.         .name        = "sd",  
  1472.         .probe        = sd_probe,  
  1473.         .remove        = sd_remove,  
  1474.         .suspend    = sd_suspend,  
  1475.         .resume        = sd_resume,  
  1476.         .shutdown    = sd_shutdown,  
  1477.     },  
  1478.     .rescan            = sd_rescan,  
  1479.     .done            = sd_done,  
  1480. };  
  1481.   
  1482. 一般我们认为系统在执行完scs_register_driver系统会根据总线上的设备情况,执行进一步的处理 , 如执行驱动提供的probe函数,但是因为这个模块早于sata驱动的初始化,系统没有其他的硬盘设备,导致这里并没有在总线上发现设备。知道sata初始化完并向scsi层注册设备后,才会手动执行这里probe函数,进而完成sd模块的初始化,如分配设备号等。  
  1483.   
  1484. 下面我们继续分析之前两篇文章剩下的一点部分,引入今天我们要分析的内容。  
  1485.   
  1486. 之前我们看到(第一篇文章)系统在执行到ata_host_register会等待一个内核线程的执行,并在第二篇文章看到了内核线程的执行流程以及该线程结束后会唤醒等待的主初始化线程,现在我们从等待后面继续往下看。  
  1487.   
  1488. ata_host_register函数剩下的部分比较简单,只有几行代码,我们主要看他调用的一个函数,如下:  
  1489.   
  1490. linux/driver/ata/libata_scsi.c  
  1491.   
  1492. void ata_scsi_scan_host(struct ata_port *ap, int sync)  
  1493. {  
  1494.     int tries = 5;  
  1495.     struct ata_device *last_failed_dev = NULL;  
  1496.     struct ata_link *link;  
  1497.     struct ata_device *dev;  
  1498.   
  1499.     if (ap->flags & ATA_FLAG_DISABLED)  
  1500.         return;  
  1501.   
  1502.  repeat:  
  1503.     ata_port_for_each_link(link, ap) {  
  1504.         ata_link_for_each_dev(dev, link) {  
  1505.             struct scsi_device *sdev;  
  1506.             int channel = 0, id = 0;  
  1507.   
  1508.             if (!ata_dev_enabled(dev) || dev->sdev)  
  1509.                 continue;  
  1510.   
  1511.             if (ata_is_host_link(link))  
  1512.                 id = dev->devno;  
  1513.             else  
  1514.                 channel = link->pmp;  
  1515.   
  1516.             sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,  
  1517.                          NULL);  
  1518.             if (!IS_ERR(sdev)) {  
  1519.                 dev->sdev = sdev;  
  1520.                 scsi_device_put(sdev);  
  1521.             }  
  1522.         }  
  1523.     }  
  1524.   
  1525.     /* If we scanned while EH was in progress or allocation 
  1526.      * failure occurred, scan would have failed silently.  Check 
  1527.      * whether all devices are attached. 
  1528.      */  
  1529.     ata_port_for_each_link(link, ap) {  
  1530.         ata_link_for_each_dev(dev, link) {  
  1531.             if (ata_dev_enabled(dev) && !dev->sdev)  
  1532.                 goto exit_loop;  
  1533.         }  
  1534.     }  
  1535.  exit_loop:  
  1536.     if (!link)  
  1537.         return;  
  1538.   
  1539.     /* we're missing some SCSI devices */  
  1540.     if (sync) {  
  1541.         /* If caller requested synchrnous scan && we've made 
  1542.          * any progress, sleep briefly and repeat. 
  1543.          */  
  1544.         if (dev != last_failed_dev) {  
  1545.             msleep(100);  
  1546.             last_failed_dev = dev;  
  1547.             goto repeat;  
  1548.         }  
  1549.   
  1550.         /* We might be failing to detect boot device, give it 
  1551.          * a few more chances. 
  1552.          */  
  1553.         if (--tries) {  
  1554.             msleep(100);  
  1555.             goto repeat;  
  1556.         }  
  1557.   
  1558.         ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "  
  1559.                 "failed without making any progress,\n"  
  1560.                 "                  switching to async\n");  
  1561.     }  
  1562.   
  1563.     queue_delayed_work(ata_aux_wq, &ap->hotplug_task,  
  1564.                round_jiffies_relative(HZ));  
  1565. }  
  1566.   
  1567. linux/dirver/scsi/scsi_scan.c  
  1568.   
  1569. struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,  
  1570.                       uint id, uint lun, void *hostdata)  
  1571. {  
  1572.     struct scsi_device *sdev = ERR_PTR(-ENODEV);  
  1573.     struct device *parent = &shost->shost_gendev;  
  1574.     struct scsi_target *starget;  
  1575.   
  1576.     if (strncmp(scsi_scan_type, "none", 4) == 0)  
  1577.         return ERR_PTR(-ENODEV);  
  1578.   
  1579.     starget = scsi_alloc_target(parent, channel, id);  
  1580.     if (!starget)  
  1581.         return ERR_PTR(-ENOMEM);  
  1582.   
  1583.     mutex_lock(&shost->scan_mutex);  
  1584.     if (!shost->async_scan)  
  1585.         scsi_complete_async_scans();  
  1586.   
  1587.     if (scsi_host_scan_allowed(shost))  
  1588.         scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);  
  1589.     mutex_unlock(&shost->scan_mutex);  
  1590.     scsi_target_reap(starget);  
  1591.     put_device(&starget->dev);  
  1592.   
  1593.     return sdev;  
  1594. }  
  1595.   
  1596. /** 
  1597.  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 
  1598.  * @starget:    pointer to target device structure 
  1599.  * @lun:    LUN of target device 
  1600.  * @sdevscan:    probe the LUN corresponding to this scsi_device 
  1601.  * @sdevnew:    store the value of any new scsi_device allocated 
  1602.  * @bflagsp:    store bflags here if not NULL 
  1603.  * 
  1604.  * Description: 
  1605.  *     Call scsi_probe_lun, if a LUN with an attached device is found, 
  1606.  *     allocate and set it up by calling scsi_add_lun. 
  1607.  * 
  1608.  * Return: 
  1609.  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 
  1610.  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 
  1611.  *         attached at the LUN 
  1612.  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 
  1613.  **/  
  1614. static int scsi_probe_and_add_lun(struct scsi_target *starget,  
  1615.                   uint lun, int *bflagsp,  
  1616.                   struct scsi_device **sdevp, int rescan,  
  1617.                   void *hostdata)  
  1618. {  
  1619.     struct scsi_device *sdev;  
  1620.     unsigned char *result;  
  1621.     int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;  
  1622.     struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);  
  1623.   
  1624.     /* 
  1625.      * The rescan flag is used as an optimization, the first scan of a 
  1626.      * host adapter calls into here with rescan == 0. 
  1627.      */  
  1628.     sdev = scsi_device_lookup_by_target(starget, lun);  
  1629.     if (sdev) {  
  1630.         if (rescan || sdev->sdev_state != SDEV_CREATED) {  
  1631.             SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO  
  1632.                 "scsi scan: device exists on %s\n",  
  1633.                 sdev->sdev_gendev.bus_id));  
  1634.             if (sdevp)  
  1635.                 *sdevp = sdev;  
  1636.             else  
  1637.                 scsi_device_put(sdev);  
  1638.   
  1639.             if (bflagsp)  
  1640.                 *bflagsp = scsi_get_device_flags(sdev,  
  1641.                                  sdev->vendor,  
  1642.                                  sdev->model);  
  1643.             return SCSI_SCAN_LUN_PRESENT;  
  1644.         }  
  1645.         scsi_device_put(sdev);  
  1646.     } else  
  1647.         sdev = scsi_alloc_sdev(starget, lun, hostdata);  
  1648.     if (!sdev)  
  1649.         goto out;  
  1650.   
  1651.     result = kmalloc(result_len, GFP_ATOMIC |  
  1652.             ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));  
  1653.     if (!result)  
  1654.         goto out_free_sdev;  
  1655.   
  1656.     if (scsi_probe_lun(sdev, result, result_len, &bflags))  
  1657.         goto out_free_result;  
  1658.   
  1659.     if (bflagsp)  
  1660.         *bflagsp = bflags;  
  1661.     /* 
  1662.      * result contains valid SCSI INQUIRY data. 
  1663.      */  
  1664.     if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {  
  1665.         /* 
  1666.          * For a Peripheral qualifier 3 (011b), the SCSI 
  1667.          * spec says: The device server is not capable of 
  1668.          * supporting a physical device on this logical 
  1669.          * unit. 
  1670.          * 
  1671.          * For disks, this implies that there is no 
  1672.          * logical disk configured at sdev->lun, but there 
  1673.          * is a target id responding. 
  1674.          */  
  1675.         SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"  
  1676.                    " peripheral qualifier of 3, device not"  
  1677.                    " added\n"))  
  1678.         if (lun == 0) {  
  1679.             SCSI_LOG_SCAN_BUS(1, {  
  1680.                 unsigned char vend[9];  
  1681.                 unsigned char mod[17];  
  1682.   
  1683.                 sdev_printk(KERN_INFO, sdev,  
  1684.                     "scsi scan: consider passing scsi_mod."  
  1685.                     "dev_flags=%s:%s:0x240 or 0x1000240\n",  
  1686.                     scsi_inq_str(vend, result, 8, 16),  
  1687.                     scsi_inq_str(mod, result, 16, 32));  
  1688.             });  
  1689.         }  
  1690.           
  1691.         res = SCSI_SCAN_TARGET_PRESENT;  
  1692.         goto out_free_result;  
  1693.     }  
  1694.   
  1695.     /* 
  1696.      * Some targets may set slight variations of PQ and PDT to signal 
  1697.      * that no LUN is present, so don't add sdev in these cases. 
  1698.      * Two specific examples are: 
  1699.      * 1) NetApp targets: return PQ=1, PDT=0x1f 
  1700.      * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 
  1701.      *    in the UFI 1.0 spec (we cannot rely on reserved bits). 
  1702.      * 
  1703.      * References: 
  1704.      * 1) SCSI SPC-3, pp. 145-146 
  1705.      * PQ=1: "A peripheral device having the specified peripheral 
  1706.      * device type is not connected to this logical unit. However, the 
  1707.      * device server is capable of supporting the specified peripheral 
  1708.      * device type on this logical unit." 
  1709.      * PDT=0x1f: "Unknown or no device type" 
  1710.      * 2) USB UFI 1.0, p. 20 
  1711.      * PDT=00h Direct-access device (floppy) 
  1712.      * PDT=1Fh none (no FDD connected to the requested logical unit) 
  1713.      */  
  1714.     if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&  
  1715.          (result[0] & 0x1f) == 0x1f) {  
  1716.         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO  
  1717.                     "scsi scan: peripheral device type"  
  1718.                     " of 31, no device added\n"));  
  1719.         res = SCSI_SCAN_TARGET_PRESENT;  
  1720.         goto out_free_result;  
  1721.     }  
  1722.   
  1723.     res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);  
  1724.     if (res == SCSI_SCAN_LUN_PRESENT) {  
  1725.         if (bflags & BLIST_KEY) {  
  1726.             sdev->lockable = 0;  
  1727.             scsi_unlock_floptical(sdev, result);  
  1728.         }  
  1729.     }  
  1730.   
  1731.  out_free_result:  
  1732.     kfree(result);  
  1733.  out_free_sdev:  
  1734.     if (res == SCSI_SCAN_LUN_PRESENT) {  
  1735.         if (sdevp) {  
  1736.             if (scsi_device_get(sdev) == 0) {  
  1737.                 *sdevp = sdev;  
  1738.             } else {  
  1739.                 __scsi_remove_device(sdev);  
  1740.                 res = SCSI_SCAN_NO_RESPONSE;  
  1741.             }  
  1742.         }  
  1743.     } else  
  1744.         scsi_destroy_sdev(sdev);  
  1745.  out:  
  1746.     return res;  
  1747. }  
  1748.   
  1749. /** 
  1750.  * scsi_add_lun - allocate and fully initialze a scsi_device 
  1751.  * @sdev:    holds information to be stored in the new scsi_device 
  1752.  * @inq_result:    holds the result of a previous INQUIRY to the LUN 
  1753.  * @bflags:    black/white list flag 
  1754.  * @async:    1 if this device is being scanned asynchronously 
  1755.  * 
  1756.  * Description: 
  1757.  *     Initialize the scsi_device @sdev.  Optionally set fields based 
  1758.  *     on values in *@bflags. 
  1759.  * 
  1760.  * Return: 
  1761.  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 
  1762.  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 
  1763.  **/  
  1764. static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,  
  1765.         int *bflags, int async)  
  1766. {  
  1767.     /* 
  1768.      * XXX do not save the inquiry, since it can change underneath us, 
  1769.      * save just vendor/model/rev. 
  1770.      * 
  1771.      * Rather than save it and have an ioctl that retrieves the saved 
  1772.      * value, have an ioctl that executes the same INQUIRY code used 
  1773.      * in scsi_probe_lun, let user level programs doing INQUIRY 
  1774.      * scanning run at their own risk, or supply a user level program 
  1775.      * that can correctly scan. 
  1776.      */  
  1777.   
  1778.     /* 
  1779.      * Copy at least 36 bytes of INQUIRY data, so that we don't 
  1780.      * dereference unallocated memory when accessing the Vendor, 
  1781.      * Product, and Revision strings.  Badly behaved devices may set 
  1782.      * the INQUIRY Additional Length byte to a small value, indicating 
  1783.      * these strings are invalid, but often they contain plausible data 
  1784.      * nonetheless.  It doesn't matter if the device sent < 36 bytes 
  1785.      * total, since scsi_probe_lun() initializes inq_result with 0s. 
  1786.      */  
  1787.     sdev->inquiry = kmemdup(inq_result,  
  1788.                 max_t(size_t, sdev->inquiry_len, 36),  
  1789.                 GFP_ATOMIC);  
  1790.     if (sdev->inquiry == NULL)  
  1791.         return SCSI_SCAN_NO_RESPONSE;  
  1792.   
  1793.     sdev->vendor = (char *) (sdev->inquiry + 8);  
  1794.     sdev->model = (char *) (sdev->inquiry + 16);  
  1795.     sdev->rev = (char *) (sdev->inquiry + 32);  
  1796.   
  1797.     if (*bflags & BLIST_ISROM) {  
  1798.         sdev->type = TYPE_ROM;  
  1799.         sdev->removable = 1;  
  1800.     } else {  
  1801.         sdev->type = (inq_result[0] & 0x1f);  
  1802.         sdev->removable = (inq_result[1] & 0x80) >> 7;  
  1803.     }  
  1804.   
  1805.     switch (sdev->type) {  
  1806.     case TYPE_RBC:  
  1807.     case TYPE_TAPE:  
  1808.     case TYPE_DISK:  
  1809.     case TYPE_PRINTER:  
  1810.     case TYPE_MOD:  
  1811.     case TYPE_PROCESSOR:  
  1812.     case TYPE_SCANNER:  
  1813.     case TYPE_MEDIUM_CHANGER:  
  1814.     case TYPE_ENCLOSURE:  
  1815.     case TYPE_COMM:  
  1816.     case TYPE_RAID:  
  1817.         sdev->writeable = 1;  
  1818.         break;  
  1819.     case TYPE_ROM:  
  1820.     case TYPE_WORM:  
  1821.         sdev->writeable = 0;  
  1822.         break;  
  1823.     default:  
  1824.         printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);  
  1825.     }  
  1826.   
  1827.     if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {  
  1828.         /* RBC and MMC devices can return SCSI-3 compliance and yet 
  1829.          * still not support REPORT LUNS, so make them act as 
  1830.          * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 
  1831.          * specifically set */  
  1832.         if ((*bflags & BLIST_REPORTLUN2) == 0)  
  1833.             *bflags |= BLIST_NOREPORTLUN;  
  1834.     }  
  1835.   
  1836.     /* 
  1837.      * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 
  1838.      * spec says: The device server is capable of supporting the 
  1839.      * specified peripheral device type on this logical unit. However, 
  1840.      * the physical device is not currently connected to this logical 
  1841.      * unit. 
  1842.      * 
  1843.      * The above is vague, as it implies that we could treat 001 and 
  1844.      * 011 the same. Stay compatible with previous code, and create a 
  1845.      * scsi_device for a PQ of 1 
  1846.      * 
  1847.      * Don't set the device offline here; rather let the upper 
  1848.      * level drivers eval the PQ to decide whether they should 
  1849.      * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 
  1850.      */   
  1851.   
  1852.     sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;  
  1853.     sdev->lockable = sdev->removable;  
  1854.     sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);  
  1855.   
  1856.     if (sdev->scsi_level >= SCSI_3 ||  
  1857.             (sdev->inquiry_len > 56 && inq_result[56] & 0x04))  
  1858.         sdev->ppr = 1;  
  1859.     if (inq_result[7] & 0x60)  
  1860.         sdev->wdtr = 1;  
  1861.     if (inq_result[7] & 0x10)  
  1862.         sdev->sdtr = 1;  
  1863.   
  1864.     sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "  
  1865.             "ANSI: %d%s\n", scsi_device_type(sdev->type),  
  1866.             sdev->vendor, sdev->model, sdev->rev,  
  1867.             sdev->inq_periph_qual, inq_result[2] & 0x07,  
  1868.             (inq_result[3] & 0x0f) == 1 ? " CCS" : "");  
  1869.   
  1870.     if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&  
  1871.         !(*bflags & BLIST_NOTQ))  
  1872.         sdev->tagged_supported = 1;  
  1873.   
  1874.     /* 
  1875.      * Some devices (Texel CD ROM drives) have handshaking problems 
  1876.      * when used with the Seagate controllers. borken is initialized 
  1877.      * to 1, and then set it to 0 here. 
  1878.      */  
  1879.     if ((*bflags & BLIST_BORKEN) == 0)  
  1880.         sdev->borken = 0;  
  1881.   
  1882.     if (*bflags & BLIST_NO_ULD_ATTACH)  
  1883.         sdev->no_uld_attach = 1;  
  1884.   
  1885.     /* 
  1886.      * Apparently some really broken devices (contrary to the SCSI 
  1887.      * standards) need to be selected without asserting ATN 
  1888.      */  
  1889.     if (*bflags & BLIST_SELECT_NO_ATN)  
  1890.         sdev->select_no_atn = 1;  
  1891.   
  1892.     /* 
  1893.      * Maximum 512 sector transfer length 
  1894.      * broken RA4x00 Compaq Disk Array 
  1895.      */  
  1896.     if (*bflags & BLIST_MAX_512)  
  1897.         blk_queue_max_sectors(sdev->request_queue, 512);  
  1898.   
  1899.     /* 
  1900.      * Some devices may not want to have a start command automatically 
  1901.      * issued when a device is added. 
  1902.      */  
  1903.     if (*bflags & BLIST_NOSTARTONADD)  
  1904.         sdev->no_start_on_add = 1;  
  1905.   
  1906.     if (*bflags & BLIST_SINGLELUN)  
  1907.         sdev->single_lun = 1;  
  1908.   
  1909.     sdev->use_10_for_rw = 1;  
  1910.   
  1911.     if (*bflags & BLIST_MS_SKIP_PAGE_08)  
  1912.         sdev->skip_ms_page_8 = 1;  
  1913.   
  1914.     if (*bflags & BLIST_MS_SKIP_PAGE_3F)  
  1915.         sdev->skip_ms_page_3f = 1;  
  1916.   
  1917.     if (*bflags & BLIST_USE_10_BYTE_MS)  
  1918.         sdev->use_10_for_ms = 1;  
  1919.   
  1920.     /* set the device running here so that slave configure 
  1921.      * may do I/O */  
  1922.     scsi_device_set_state(sdev, SDEV_RUNNING);  
  1923.   
  1924.     if (*bflags & BLIST_MS_192_BYTES_FOR_3F)  
  1925.         sdev->use_192_bytes_for_3f = 1;  
  1926.   
  1927.     if (*bflags & BLIST_NOT_LOCKABLE)  
  1928.         sdev->lockable = 0;  
  1929.   
  1930.     if (*bflags & BLIST_RETRY_HWERROR)  
  1931.         sdev->retry_hwerror = 1;  
  1932.   
  1933.     transport_configure_device(&sdev->sdev_gendev);  
  1934.   
  1935.     if (sdev->host->hostt->slave_configure) {  
  1936.         int ret = sdev->host->hostt->slave_configure(sdev);  
  1937.         if (ret) {  
  1938.             /* 
  1939.              * if LLDD reports slave not present, don't clutter 
  1940.              * console with alloc failure messages 
  1941.              */  
  1942.             if (ret != -ENXIO) {  
  1943.                 sdev_printk(KERN_ERR, sdev,  
  1944.                     "failed to configure device\n");  
  1945.             }  
  1946.             return SCSI_SCAN_NO_RESPONSE;  
  1947.         }  
  1948.     }  
  1949.   
  1950.     /* 
  1951.      * Ok, the device is now all set up, we can 
  1952.      * register it and tell the rest of the kernel 
  1953.      * about it. 
  1954.      */  
  1955.     if (!async && scsi_sysfs_add_sdev(sdev) != 0)  
  1956.         return SCSI_SCAN_NO_RESPONSE;  
  1957.   
  1958.     return SCSI_SCAN_LUN_PRESENT;  
  1959. }  
  1960.   
  1961. linux/driver/scsi/scsi_sysfs.c  
  1962.   
  1963. /** 
  1964.  * scsi_sysfs_add_sdev - add scsi device to sysfs 
  1965.  * @sdev:    scsi_device to add 
  1966.  * 
  1967.  * Return value: 
  1968.  *     0 on Success / non-zero on Failure 
  1969.  **/  
  1970. int scsi_sysfs_add_sdev(struct scsi_device *sdev)  
  1971. {  
  1972.     int error, i;  
  1973.     struct request_queue *rq = sdev->request_queue;  
  1974.   
  1975.     if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)  
  1976.         return error;  
  1977.   
  1978.     error = device_add(&sdev->sdev_gendev);  
  1979.     if (error) {  
  1980.         put_device(sdev->sdev_gendev.parent);  
  1981.         printk(KERN_INFO "error 1\n");  
  1982.         return error;  
  1983.     }  
  1984.     error = class_device_add(&sdev->sdev_classdev);  
  1985.     if (error) {  
  1986.         printk(KERN_INFO "error 2\n");  
  1987.         goto clean_device;  
  1988.     }  
  1989.   
  1990.     /* take a reference for the sdev_classdev; this is 
  1991.      * released by the sdev_class .release */  
  1992.     get_device(&sdev->sdev_gendev);  
  1993.   
  1994.     /* create queue files, which may be writable, depending on the host */  
  1995.     if (sdev->host->hostt->change_queue_depth)  
  1996.         error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);  
  1997.     else  
  1998.         error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);  
  1999.     if (error) {  
  2000.         __scsi_remove_device(sdev);  
  2001.         goto out;  
  2002.     }  
  2003.     if (sdev->host->hostt->change_queue_type)  
  2004.         error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);  
  2005.     else  
  2006.         error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);  
  2007.     if (error) {  
  2008.         __scsi_remove_device(sdev);  
  2009.         goto out;  
  2010.     }  
  2011.   
  2012.     error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);  
  2013.   
  2014.     if (error)  
  2015.         sdev_printk(KERN_INFO, sdev,  
  2016.                 "Failed to register bsg queue, errno=%d\n", error);  
  2017.   
  2018.     /* we're treating error on bsg register as non-fatal, so pretend 
  2019.      * nothing went wrong */  
  2020.     error = 0;  
  2021.   
  2022.     /* add additional host specific attributes */  
  2023.     if (sdev->host->hostt->sdev_attrs) {  
  2024.         for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {  
  2025.             error = device_create_file(&sdev->sdev_gendev,  
  2026.                     sdev->host->hostt->sdev_attrs[i]);  
  2027.             if (error) {  
  2028.                 __scsi_remove_device(sdev);  
  2029.                 goto out;  
  2030.             }  
  2031.         }  
  2032.     }  
  2033.   
  2034.     transport_add_device(&sdev->sdev_gendev);  
  2035.  out:  
  2036.     return error;  
  2037.   
  2038.  clean_device:  
  2039.     scsi_device_set_state(sdev, SDEV_CANCEL);  
  2040.   
  2041.     device_del(&sdev->sdev_gendev);  
  2042.     transport_destroy_device(&sdev->sdev_gendev);  
  2043.     put_device(&sdev->sdev_gendev);  
  2044.   
  2045.     return error;  
  2046. }  
  2047.   
  2048. 上面的add_device可以执行本文章开头的probe函数,因为篇幅原因,就不继续深入分析,我们直接分析sd模块的probe函数:  
  2049.   
  2050.   
  2051. /** 
  2052.  *    sd_probe - called during driver initialization and whenever a 
  2053.  *    new scsi device is attached to the system. It is called once 
  2054.  *    for each scsi device (not just disks) present. 
  2055.  *    @dev: pointer to device object 
  2056.  * 
  2057.  *    Returns 0 if successful (or not interested in this scsi device  
  2058.  *    (e.g. scanner)); 1 when there is an error. 
  2059.  * 
  2060.  *    Note: this function is invoked from the scsi mid-level. 
  2061.  *    This function sets up the mapping between a given  
  2062.  *    <host,channel,id,lun> (found in sdp) and new device name  
  2063.  *    (e.g. /dev/sda). More precisely it is the block device major  
  2064.  *    and minor number that is chosen here. 
  2065.  * 
  2066.  *    Assume sd_attach is not re-entrant (for time being) 
  2067.  *    Also think about sd_attach() and sd_remove() running coincidentally. 
  2068.  **/  
  2069. static int sd_probe(struct device *dev)  
  2070. {  
  2071.     struct scsi_device *sdp = to_scsi_device(dev);  
  2072.     struct scsi_disk *sdkp;  
  2073.     struct gendisk *gd;  
  2074.     u32 index;  
  2075.     int error;  
  2076.   
  2077.     error = -ENODEV;  
  2078.     if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)  
  2079.         goto out;  
  2080.   
  2081.     SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,  
  2082.                     "sd_attach\n"));  
  2083.   
  2084.     error = -ENOMEM;  
  2085.     sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);  
  2086.     if (!sdkp)  
  2087.         goto out;  
  2088.   
  2089.     gd = alloc_disk(16);  
  2090.     if (!gd)  
  2091.         goto out_free;  
  2092.   
  2093.     if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))  
  2094.         goto out_put;  
  2095.   
  2096.     spin_lock(&sd_index_lock);  
  2097.     error = idr_get_new(&sd_index_idr, NULL, &index);  
  2098.     spin_unlock(&sd_index_lock);  
  2099.   
  2100.     if (index >= SD_MAX_DISKS)  
  2101.         error = -EBUSY;  
  2102.     if (error)  
  2103.         goto out_put;  
  2104.   
  2105.     sdkp->device = sdp;  
  2106.     sdkp->driver = &sd_template;  
  2107.     sdkp->disk = gd;  
  2108.     sdkp->index = index;  
  2109.     sdkp->openers = 0;  
  2110.   
  2111.     if (!sdp->timeout) {  
  2112.         if (sdp->type != TYPE_MOD)  
  2113.             sdp->timeout = SD_TIMEOUT;  
  2114.         else  
  2115.             sdp->timeout = SD_MOD_TIMEOUT;  
  2116.     }  
  2117.   
  2118.     class_device_initialize(&sdkp->cdev);  
  2119.     sdkp->cdev.dev = &sdp->sdev_gendev;  
  2120.     sdkp->cdev.class = &sd_disk_class;  
  2121.     strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);  
  2122.   
  2123.     if (class_device_add(&sdkp->cdev))  
  2124.         goto out_put;  
  2125.   
  2126.     get_device(&sdp->sdev_gendev);  
  2127.   
  2128.     gd->major = sd_major((index & 0xf0) >> 4);  
  2129.     gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);  
  2130.     gd->minors = 16;  
  2131.     gd->fops = &sd_fops;  
  2132.   
  2133.     if (index < 26) {  
  2134.         sprintf(gd->disk_name, "sd%c"'a' + index % 26);  
  2135.     } else if (index < (26 + 1) * 26) {  
  2136.         sprintf(gd->disk_name, "sd%c%c",  
  2137.             'a' + index / 26 - 1,'a' + index % 26);  
  2138.     } else {  
  2139.         const unsigned int m1 = (index / 26 - 1) / 26 - 1;  
  2140.         const unsigned int m2 = (index / 26 - 1) % 26;  
  2141.         const unsigned int m3 =  index % 26;  
  2142.         sprintf(gd->disk_name, "sd%c%c%c",  
  2143.             'a' + m1, 'a' + m2, 'a' + m3);  
  2144.     }  
  2145.   
  2146.     gd->private_data = &sdkp->driver;  
  2147.     gd->queue = sdkp->device->request_queue;  
  2148.   
  2149.     sd_revalidate_disk(gd);  
  2150.   
  2151.     blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);  
  2152.   
  2153.     gd->driverfs_dev = &sdp->sdev_gendev;  
  2154.     gd->flags = GENHD_FL_DRIVERFS;  
  2155.     if (sdp->removable)  
  2156.         gd->flags |= GENHD_FL_REMOVABLE;  
  2157.   
  2158.     dev_set_drvdata(dev, sdkp);  
  2159.     add_disk(gd);  
  2160.   
  2161.     sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",  
  2162.           sdp->removable ? "removable " : "");  
  2163.   
  2164.     return 0;  
  2165.   
  2166.  out_put:  
  2167.     put_disk(gd);  
  2168.  out_free:  
  2169.     kfree(sdkp);  
  2170.  out:  
  2171.     return error;  
  2172. }  
  2173. 这个函数分配了盘符等必须的信息,但是还是没有完成与用户空间的接口设置,在函数执行快结束的地方我们能看见函数add_disk, 这个函数并不是属于scsi层的函数,而是linux/block的函数,我们知道硬盘等都是块设备,而我们从最底层一直到现在还没有接触任何与此相关的内容,linux/driver/block 和 linux/block即是与块设备的操作逻辑,而scsi层与libata只是为block层提供具体操作的接口。后续文章我们将继续深入分析。   
  2174.   
  2175. 块设备初始化分析(libata初始化分析 4)  
  2176.   
  2177. 我们先将前面的内容简单的回顾下, 整个初始化由sata驱动模块开始,通过对sata设备的注册,初始化libata层的结构,在初始化的过程中,初始化了scsi层需要的结构,并开启一个错误处理线程,该线程负责处理在操作中出现异常/错误的处理,并负责确定是否对设备是否重新连接。因为现在整个系统刚刚初始化,还没有连接设备,所以通过该线程进行对设备的reset 以及连接等。  
  2178. 在此过程中,初始化线程处于等待状态,在错误处理线程执行一个周期后,初始化线程继续执行,并由此开始初始化总线上的设备,手动触发sd.c模块进行probe处理,这里的probe我们有必要详细分析下,因为这里有一些重要的数据结构,这些结构对于我们后续的理解有重大的帮助。  
  2179.   
  2180. linux/driver/scsi/sd.c  
  2181. /** 
  2182.  *    sd_probe - called during driver initialization and whenever a 
  2183.  *    new scsi device is attached to the system. It is called once 
  2184.  *    for each scsi device (not just disks) present. 
  2185.  *    @dev: pointer to device object 
  2186.  * 
  2187.  *    Returns 0 if successful (or not interested in this scsi device  
  2188.  *    (e.g. scanner)); 1 when there is an error. 
  2189.  * 
  2190.  *    Note: this function is invoked from the scsi mid-level. 
  2191.  *    This function sets up the mapping between a given  
  2192.  *    <host,channel,id,lun> (found in sdp) and new device name  
  2193.  *    (e.g. /dev/sda). More precisely it is the block device major  
  2194.  *    and minor number that is chosen here. 
  2195.  * 
  2196.  *    Assume sd_attach is not re-entrant (for time being) 
  2197.  *    Also think about sd_attach() and sd_remove() running coincidentally. 
  2198.  **/  
  2199. static int sd_probe(struct device *dev)  
  2200. {  
  2201.     struct scsi_device *sdp = to_scsi_device(dev);    //(1)  
  2202.     struct scsi_disk *sdkp;  
  2203.     struct gendisk *gd;  
  2204.     u32 index;  
  2205.     int error;  
  2206.   
  2207.     error = -ENODEV;  
  2208.     if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)  
  2209.         goto out;  
  2210.   
  2211.     SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,  
  2212.                     "sd_attach\n"));  
  2213.   
  2214.     error = -ENOMEM;  
  2215.     sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);  
  2216.     if (!sdkp)  
  2217.         goto out;  
  2218.   
  2219.     gd = alloc_disk(16);  
  2220.     if (!gd)  
  2221.         goto out_free;  
  2222.   
  2223.     if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))  
  2224.         goto out_put;  
  2225.   
  2226.     spin_lock(&sd_index_lock);  
  2227.     error = idr_get_new(&sd_index_idr, NULL, &index);  
  2228.     spin_unlock(&sd_index_lock);  
  2229.   
  2230.     if (index >= SD_MAX_DISKS)  
  2231.         error = -EBUSY;  
  2232.     if (error)  
  2233.         goto out_put;  
  2234.   
  2235.     sdkp->device = sdp;  
  2236.     sdkp->driver = &sd_template;  
  2237.     sdkp->disk = gd;  
  2238.     sdkp->index = index;  
  2239.     sdkp->openers = 0;  // (2)  
  2240.   
  2241.     if (!sdp->timeout) {  
  2242.         if (sdp->type != TYPE_MOD)  
  2243.             sdp->timeout = SD_TIMEOUT;  
  2244.         else  
  2245.             sdp->timeout = SD_MOD_TIMEOUT;  
  2246.     }  
  2247.   
  2248.     class_device_initialize(&sdkp->cdev);  
  2249.     sdkp->cdev.dev = &sdp->sdev_gendev;  
  2250.     sdkp->cdev.class = &sd_disk_class;  
  2251.     strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);  
  2252.   
  2253.     if (class_device_add(&sdkp->cdev))  
  2254.         goto out_put;  
  2255.   
  2256.     get_device(&sdp->sdev_gendev);  
  2257.   
  2258.     gd->major = sd_major((index & 0xf0) >> 4);  
  2259.     gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);  
  2260.     gd->minors = 16;  
  2261.     gd->fops = &sd_fops;   //(3)  
  2262.   
  2263.     if (index < 26) {  
  2264.         sprintf(gd->disk_name, "sd%c"'a' + index % 26);  
  2265.     } else if (index < (26 + 1) * 26) {  
  2266.         sprintf(gd->disk_name, "sd%c%c",  
  2267.             'a' + index / 26 - 1,'a' + index % 26);  
  2268.     } else {  
  2269.         const unsigned int m1 = (index / 26 - 1) / 26 - 1;  
  2270.         const unsigned int m2 = (index / 26 - 1) % 26;  
  2271.         const unsigned int m3 =  index % 26;  
  2272.         sprintf(gd->disk_name, "sd%c%c%c",  
  2273.             'a' + m1, 'a' + m2, 'a' + m3);  
  2274.     }  
  2275.   
  2276.     gd->private_data = &sdkp->driver;  
  2277.     gd->queue = sdkp->device->request_queue;  //(4)  
  2278.   
  2279.     sd_revalidate_disk(gd);  
  2280.   
  2281.     blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);  
  2282.   
  2283.     gd->driverfs_dev = &sdp->sdev_gendev;  
  2284.     gd->flags = GENHD_FL_DRIVERFS;  
  2285.     if (sdp->removable)  
  2286.         gd->flags |= GENHD_FL_REMOVABLE;  
  2287.   
  2288.     dev_set_drvdata(dev, sdkp);  
  2289.     add_disk(gd);  
  2290.   
  2291.     sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",  
  2292.           sdp->removable ? "removable " : "");  
  2293.   
  2294.     return 0;  
  2295.   
  2296.  out_put:  
  2297.     put_disk(gd);  
  2298.  out_free:  
  2299.     kfree(sdkp);  
  2300.  out:  
  2301.     return error;  
  2302. }  
  2303.   
  2304. 我们先说明下第三部分,第三部分初始化了struct gendisk的设备号和一个重要的指针( gd->fops = &sd_fops;),我们先来看下这个结构体的初始化:  
  2305.   
  2306. static struct block_device_operations sd_fops = {  
  2307.     .owner            = THIS_MODULE,  
  2308.     .open            = sd_open,  
  2309.     .release        = sd_release,  
  2310.     .ioctl            = sd_ioctl,  
  2311.     .getgeo            = sd_getgeo,  
  2312. #ifdef CONFIG_COMPAT  
  2313.     .compat_ioctl        = sd_compat_ioctl,  
  2314. #endif  
  2315.     .media_changed        = sd_media_changed,  
  2316.     .revalidate_disk    = sd_revalidate_disk,  
  2317. };  
  2318.   
  2319. 这个结构提很像字符设备中的file_operation,同样的有open, release函数,其实这个就是块设备的接口函数,我们打开一个块设备,系统最终会运行这里的open函数,至于这个函数的生效过程我们一会再分析。  
  2320.   
  2321. 第1 2 4部分要一起看,其实这里有一个块设备重要的操作函数,我们在上面的块设备操作函数没有看到read, write函数,因为块的读写是通过一个特殊的函数request函数实现的, 系统在文件系统层将需要的操作写入到相应设备的queue中,而这个queue中就包含这个request函数的指针,方便系统的最后调用,我们可以看到这里的gd->queue来自于probe函数的传入参数,我们可以看下之前的分析过程中的一个函数:  
  2322.   
  2323. /linux/driver/scsi/scsi_scan.c  
  2324.   
  2325. /** 
  2326.  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 
  2327.  * @starget:    pointer to target device structure 
  2328.  * @lun:    LUN of target device 
  2329.  * @sdevscan:    probe the LUN corresponding to this scsi_device 
  2330.  * @sdevnew:    store the value of any new scsi_device allocated 
  2331.  * @bflagsp:    store bflags here if not NULL 
  2332.  * 
  2333.  * Description: 
  2334.  *     Call scsi_probe_lun, if a LUN with an attached device is found, 
  2335.  *     allocate and set it up by calling scsi_add_lun. 
  2336.  * 
  2337.  * Return: 
  2338.  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 
  2339.  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 
  2340.  *         attached at the LUN 
  2341.  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 
  2342.  **/  
  2343. static int scsi_probe_and_add_lun(struct scsi_target *starget,  
  2344.                   uint lun, int *bflagsp,  
  2345.                   struct scsi_device **sdevp, int rescan,  
  2346.                   void *hostdata)  
  2347. {  
  2348.     struct scsi_device *sdev;  
  2349.     unsigned char *result;  
  2350.     int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;  
  2351.     struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);  
  2352.   
  2353.     /* 
  2354.      * The rescan flag is used as an optimization, the first scan of a 
  2355.      * host adapter calls into here with rescan == 0. 
  2356.      */  
  2357.     sdev = scsi_device_lookup_by_target(starget, lun);  
  2358.     if (sdev) {  
  2359.         if (rescan || sdev->sdev_state != SDEV_CREATED) {  
  2360.             SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO  
  2361.                 "scsi scan: device exists on %s\n",  
  2362.                 sdev->sdev_gendev.bus_id));  
  2363.             if (sdevp)  
  2364.                 *sdevp = sdev;  
  2365.             else  
  2366.                 scsi_device_put(sdev);  
  2367.   
  2368.             if (bflagsp)  
  2369.                 *bflagsp = scsi_get_device_flags(sdev,  
  2370.                                  sdev->vendor,  
  2371.                                  sdev->model);  
  2372.             return SCSI_SCAN_LUN_PRESENT;  
  2373.         }  
  2374.         scsi_device_put(sdev);  
  2375.     } else  
  2376.         sdev = scsi_alloc_sdev(starget, lun, hostdata);  
  2377.     if (!sdev)  
  2378.         goto out;  
  2379.   
  2380.     result = kmalloc(result_len, GFP_ATOMIC |  
  2381.             ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));  
  2382.     if (!result)  
  2383.         goto out_free_sdev;  
  2384.   
  2385.     if (scsi_probe_lun(sdev, result, result_len, &bflags))  
  2386.         goto out_free_result;  
  2387.   
  2388.     if (bflagsp)  
  2389.         *bflagsp = bflags;  
  2390.     /* 
  2391.      * result contains valid SCSI INQUIRY data. 
  2392.      */  
  2393.     if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {  
  2394.         /* 
  2395.          * For a Peripheral qualifier 3 (011b), the SCSI 
  2396.          * spec says: The device server is not capable of 
  2397.          * supporting a physical device on this logical 
  2398.          * unit. 
  2399.          * 
  2400.          * For disks, this implies that there is no 
  2401.          * logical disk configured at sdev->lun, but there 
  2402.          * is a target id responding. 
  2403.          */  
  2404.         SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"  
  2405.                    " peripheral qualifier of 3, device not"  
  2406.                    " added\n"))  
  2407.         if (lun == 0) {  
  2408.             SCSI_LOG_SCAN_BUS(1, {  
  2409.                 unsigned char vend[9];  
  2410.                 unsigned char mod[17];  
  2411.   
  2412.                 sdev_printk(KERN_INFO, sdev,  
  2413.                     "scsi scan: consider passing scsi_mod."  
  2414.                     "dev_flags=%s:%s:0x240 or 0x1000240\n",  
  2415.                     scsi_inq_str(vend, result, 8, 16),  
  2416.                     scsi_inq_str(mod, result, 16, 32));  
  2417.             });  
  2418.         }  
  2419.           
  2420.         res = SCSI_SCAN_TARGET_PRESENT;  
  2421.         goto out_free_result;  
  2422.     }  
  2423. 这个函数在前面的文章曾经运行过一次,其中这里有相关queue的操作。  
  2424.   
  2425.   
  2426. /** 
  2427.  * scsi_alloc_sdev - allocate and setup a scsi_Device 
  2428.  * 
  2429.  * Description: 
  2430.  *     Allocate, initialize for io, and return a pointer to a scsi_Device. 
  2431.  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and 
  2432.  *     adds scsi_Device to the appropriate list. 
  2433.  * 
  2434.  * Return value: 
  2435.  *     scsi_Device pointer, or NULL on failure. 
  2436.  **/  
  2437. static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,  
  2438.                        unsigned int lun, void *hostdata)  
  2439. {  
  2440.     struct scsi_device *sdev;  
  2441.     int display_failure_msg = 1, ret;  
  2442.     struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);  
  2443.     extern void scsi_evt_thread(struct work_struct *work);  
  2444.   
  2445.     sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,  
  2446.                GFP_ATOMIC);  
  2447.     if (!sdev)  
  2448.         goto out;  
  2449.   
  2450.     sdev->vendor = scsi_null_device_strs;  
  2451.     sdev->model = scsi_null_device_strs;  
  2452.     sdev->rev = scsi_null_device_strs;  
  2453.     sdev->host = shost;  
  2454.     sdev->id = starget->id;  
  2455.     sdev->lun = lun;  
  2456.     sdev->channel = starget->channel;  
  2457.     sdev->sdev_state = SDEV_CREATED;  
  2458.     INIT_LIST_HEAD(&sdev->siblings);  
  2459.     INIT_LIST_HEAD(&sdev->same_target_siblings);  
  2460.     INIT_LIST_HEAD(&sdev->cmd_list);  
  2461.     INIT_LIST_HEAD(&sdev->starved_entry);  
  2462.     INIT_LIST_HEAD(&sdev->event_list);  
  2463.     spin_lock_init(&sdev->list_lock);  
  2464.     INIT_WORK(&sdev->event_work, scsi_evt_thread);  
  2465.   
  2466.     sdev->sdev_gendev.parent = get_device(&starget->dev);  
  2467.     sdev->sdev_target = starget;  
  2468.   
  2469.     /* usually NULL and set by ->slave_alloc instead */  
  2470.     sdev->hostdata = hostdata;  
  2471.   
  2472.     /* if the device needs this changing, it may do so in the 
  2473.      * slave_configure function */  
  2474.     sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;  
  2475.   
  2476.     /* 
  2477.      * Some low level driver could use device->type 
  2478.      */  
  2479.     sdev->type = -1;  
  2480.   
  2481.     /* 
  2482.      * Assume that the device will have handshaking problems, 
  2483.      * and then fix this field later if it turns out it 
  2484.      * doesn't 
  2485.      */  
  2486.     sdev->borken = 1;  
  2487.   
  2488.     sdev->request_queue = scsi_alloc_queue(sdev);  
  2489.     if (!sdev->request_queue) {  
  2490.         /* release fn is set up in scsi_sysfs_device_initialise, so 
  2491.          * have to free and put manually here */  
  2492.         put_device(&starget->dev);  
  2493.         kfree(sdev);  
  2494.         goto out;  
  2495.     }  
  2496.   
  2497.     sdev->request_queue->queuedata = sdev;  
  2498.     scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);  
  2499.   
  2500.     scsi_sysfs_device_initialize(sdev);  
  2501.   
  2502.     if (shost->hostt->slave_alloc) {  
  2503.         ret = shost->hostt->slave_alloc(sdev);  
  2504.         if (ret) {  
  2505.             /* 
  2506.              * if LLDD reports slave not present, don't clutter 
  2507.              * console with alloc failure messages 
  2508.              */  
  2509.             if (ret == -ENXIO)  
  2510.                 display_failure_msg = 0;  
  2511.             goto out_device_destroy;  
  2512.         }  
  2513.     }  
  2514.   
  2515.     return sdev;  
  2516.   
  2517. out_device_destroy:  
  2518.     transport_destroy_device(&sdev->sdev_gendev);  
  2519.     put_device(&sdev->sdev_gendev);  
  2520. out:  
  2521.     if (display_failure_msg)  
  2522.         printk(ALLOC_FAILURE_MSG, __FUNCTION__);  
  2523.     return NULL;  
  2524. }  
  2525. 这里初始化的sdev就是传入文章开始probe函数的参数,我们继续看queue的初始化。  
  2526.   
  2527. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)  
  2528. {  
  2529.     struct request_queue *q;  
  2530.   
  2531.     q = __scsi_alloc_queue(sdev->host, scsi_request_fn);  
  2532.     if (!q)  
  2533.         return NULL;  
  2534.   
  2535.     blk_queue_prep_rq(q, scsi_prep_fn);  
  2536.     blk_queue_softirq_done(q, scsi_softirq_done);  
  2537.     return q;  
  2538. }  
  2539.   
  2540. 这里的传入__scsi_alloc_queue函数的第二个参数就是我们要找的request函数,系统在读写设备会最终运行这个函数。  
  2541.   
  2542.   
  2543. struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,  
  2544.                      request_fn_proc *request_fn)  
  2545. {  
  2546.     struct request_queue *q;  
  2547.   
  2548.     q = blk_init_queue(request_fn, NULL);  
  2549.     if (!q)  
  2550.         return NULL;  
  2551.   
  2552.     /* 
  2553.      * this limit is imposed by hardware restrictions 
  2554.      */  
  2555.     blk_queue_max_hw_segments(q, shost->sg_tablesize);  
  2556.   
  2557.     /* 
  2558.      * In the future, sg chaining support will be mandatory and this 
  2559.      * ifdef can then go away. Right now we don't have all archs 
  2560.      * converted, so better keep it safe. 
  2561.      */  
  2562. #ifdef ARCH_HAS_SG_CHAIN  
  2563.     if (shost->use_sg_chaining)  
  2564.         blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);  
  2565.     else  
  2566.         blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);  
  2567. #else  
  2568.     blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);  
  2569. #endif  
  2570.   
  2571.     blk_queue_max_sectors(q, shost->max_sectors);  
  2572.     blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));  
  2573.     blk_queue_segment_boundary(q, shost->dma_boundary);  
  2574.   
  2575.     if (!shost->use_clustering)  
  2576.         clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);  
  2577.     return q;  
  2578. }  
  2579. EXPORT_SYMBOL(__scsi_alloc_queue);  
  2580. 都这里关于queue与request的由来,以及快设备的操作结构体我们就分析到这里,读者可以继续看看下面的具体实现。  
  2581.   
  2582. 下面我们要进入probe为之准备万分的一个重要函数: add_disk  
  2583.   
  2584. /** 
  2585.  * add_disk - add partitioning information to kernel list 
  2586.  * @disk: per-device partitioning information 
  2587.  * 
  2588.  * This function registers the partitioning information in @disk 
  2589.  * with the kernel. 
  2590.  */  
  2591. void add_disk(struct gendisk *disk)  
  2592. {  
  2593.     disk->flags |= GENHD_FL_UP;  
  2594.     blk_register_region(MKDEV(disk->major, disk->first_minor),  
  2595.                 disk->minors, NULL, exact_match, exact_lock, disk);  
  2596.     register_disk(disk);  
  2597.     blk_register_queue(disk);  
  2598. }  
  2599.   
  2600. EXPORT_SYMBOL(add_disk);  
  2601. 关于这个函数我在LDD3上看到这样的描述:  
  2602.   
  2603. “一旦调用了add_disk,磁盘设备将被激活(可以理解为已经初始化完毕),并随时会调用它提供的方法。“从这里可以看出来add_disk是块设备生效的关键的最后一步。  
  2604.   
  2605. linux/fs/partitions/check.c  可以看出这个函数已经是文件系统层的。  
  2606.   
  2607. /* Not exported, helper to add_disk(). */  
  2608. void register_disk(struct gendisk *disk)  
  2609. {  
  2610.     struct block_device *bdev;  
  2611.     char *s;  
  2612.     int i;  
  2613.     struct hd_struct *p;  
  2614.     int err;  
  2615.   
  2616.     kobject_set_name(&disk->kobj, "%s", disk->disk_name);  
  2617.     /* ewww... some of these buggers have / in name... */  
  2618.     s = strchr(disk->kobj.k_name, '/');  
  2619.     if (s)  
  2620.         *s = '!';  
  2621.     if ((err = kobject_add(&disk->kobj)))  
  2622.         return;  
  2623.     err = disk_sysfs_symlinks(disk);  
  2624.     if (err) {  
  2625.         kobject_del(&disk->kobj);  
  2626.         return;  
  2627.     }  
  2628.      disk_sysfs_add_subdirs(disk);  
  2629.   
  2630.     /* No minors to use for partitions */  
  2631.     if (disk->minors == 1)  
  2632.         goto exit;  
  2633.   
  2634.     /* No such device (e.g., media were just removed) */  
  2635.     if (!get_capacity(disk))  
  2636.         goto exit;  
  2637.   
  2638.     bdev = bdget_disk(disk, 0);  
  2639.     if (!bdev)  
  2640.         goto exit;  
  2641.   
  2642.     /* scan partition table, but suppress uevents */  
  2643.     bdev->bd_invalidated = 1;  
  2644.     disk->part_uevent_suppress = 1;  
  2645.     err = blkdev_get(bdev, FMODE_READ, 0);  
  2646.     disk->part_uevent_suppress = 0;  
  2647.     if (err < 0)  
  2648.         goto exit;  
  2649.     blkdev_put(bdev);  
  2650.   
  2651. exit:  
  2652.     /* announce disk after possible partitions are already created */  
  2653.     kobject_uevent(&disk->kobj, KOBJ_ADD);  
  2654.   
  2655.     /* announce possible partitions */  
  2656.     for (i = 1; i < disk->minors; i++) {  
  2657.         p = disk->part[i-1];  
  2658.         if (!p || !p->nr_sects)  
  2659.             continue;  
  2660.         kobject_uevent(&p->kobj, KOBJ_ADD);  
  2661.     }  
  2662. }  
  2663. 由于篇幅和时间问题,本系列文章就分析这么多,这里我只是分析了初始化的大体流程,读者可以选择性的阅读相关代码了解详细的流程, 至于关于块设备的读写流程,我在晚上发现了一篇很不错的文章,读者可以参考下,这里就不使用源码分析了。

原创粉丝点击