scsi_scan.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,352 行 · 第 1/3 页
C
1,352 行
* 32, the lookup of the device flags (above) could be invalid, * and it would be possible to take an incorrect action - we do * not want to hang because of a short INQUIRY. On the flip side, * if the device is spun down or becoming ready (and so it gives a * short INQUIRY), an abort here prevents any further use of the * device, including spin up. * * Related to the above issue: * * XXX Devices (disk or all?) should be sent a TEST UNIT READY, * and if not ready, sent a START_STOP to start (maybe spin up) and * then send the INQUIRY again, since the INQUIRY can change after * a device is initialized. * * Ideally, start a device if explicitly asked to do so. This * assumes that a device is spun up on power on, spun down on * request, and then spun up on request. */ /* * The scanning code needs to know the scsi_level, even if no * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so * non-zero LUNs can be scanned. */ sdev->scsi_level = inq_result[2] & 0x07; if (sdev->scsi_level >= 2 || (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) sdev->scsi_level++; return;}/** * scsi_add_lun - allocate and fully initialze a Scsi_Device * @sdevscan: holds information to be stored in the new Scsi_Device * @sdevnew: store the address of the newly allocated Scsi_Device * @inq_result: holds the result of a previous INQUIRY to the LUN * @bflags: black/white list flag * * Description: * Allocate and initialize a Scsi_Device matching sdevscan. Optionally * set fields based on values in *@bflags. If @sdevnew is not * NULL, store the address of the new Scsi_Device in *@sdevnew (needed * when scanning a particular LUN). * * Return: * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized **/static int scsi_add_lun(struct scsi_device *sdev, char *inq_result, int *bflags){ struct scsi_device *sdev_sibling; struct scsi_target *starget; unsigned long flags; /* * XXX do not save the inquiry, since it can change underneath us, * save just vendor/model/rev. * * Rather than save it and have an ioctl that retrieves the saved * value, have an ioctl that executes the same INQUIRY code used * in scsi_probe_lun, let user level programs doing INQUIRY * scanning run at their own risk, or supply a user level program * that can correctly scan. */ sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC); if (sdev->inquiry == NULL) { return SCSI_SCAN_NO_RESPONSE; } memcpy(sdev->inquiry, inq_result, sdev->inquiry_len); sdev->vendor = (char *) (sdev->inquiry + 8); sdev->model = (char *) (sdev->inquiry + 16); sdev->rev = (char *) (sdev->inquiry + 32); if (*bflags & BLIST_ISROM) { /* * It would be better to modify sdev->type, and set * sdev->removable, but then the print_inquiry() output * would not show TYPE_ROM; if print_inquiry() is removed * the issue goes away. */ inq_result[0] = TYPE_ROM; inq_result[1] |= 0x80; /* removable */ } switch (sdev->type = (inq_result[0] & 0x1f)) { case TYPE_TAPE: case TYPE_DISK: case TYPE_PRINTER: case TYPE_MOD: case TYPE_PROCESSOR: case TYPE_SCANNER: case TYPE_MEDIUM_CHANGER: case TYPE_ENCLOSURE: case TYPE_COMM: sdev->writeable = 1; break; case TYPE_WORM: case TYPE_ROM: sdev->writeable = 0; break; default: printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type); } print_inquiry(inq_result); /* * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI * spec says: The device server is capable of supporting the * specified peripheral device type on this logical unit. However, * the physical device is not currently connected to this logical * unit. * * The above is vague, as it implies that we could treat 001 and * 011 the same. Stay compatible with previous code, and create a * Scsi_Device for a PQ of 1 * * Don't set the device offline here; rather let the upper * level drivers eval the PQ to decide whether they should * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. */ sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; sdev->removable = (0x80 & inq_result[1]) >> 7; sdev->lockable = sdev->removable; sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) sdev->ppr = 1; if (inq_result[7] & 0x60) sdev->wdtr = 1; if (inq_result[7] & 0x10) sdev->sdtr = 1; sprintf(sdev->devfs_name, "scsi/host%d/bus%d/target%d/lun%d", sdev->host->host_no, sdev->channel, sdev->id, sdev->lun); /* * End driverfs/devfs code. */ if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && !(*bflags & BLIST_NOTQ)) sdev->tagged_supported = 1; /* * Some devices (Texel CD ROM drives) have handshaking problems * when used with the Seagate controllers. borken is initialized * to 1, and then set it to 0 here. */ if ((*bflags & BLIST_BORKEN) == 0) sdev->borken = 0; /* * Some devices may not want to have a start command automatically * issued when a device is added. */ if (*bflags & BLIST_NOSTARTONADD) sdev->no_start_on_add = 1; /* * If we need to allow I/O to only one of the luns attached to * this target id at a time set single_lun, and allocate or modify * sdev_target. */ if (*bflags & BLIST_SINGLELUN) { sdev->single_lun = 1; spin_lock_irqsave(sdev->host->host_lock, flags); starget = NULL; /* * Search for an existing target for this sdev. */ list_for_each_entry(sdev_sibling, &sdev->same_target_siblings, same_target_siblings) { if (sdev_sibling->sdev_target != NULL) { starget = sdev_sibling->sdev_target; break; } } if (!starget) { starget = kmalloc(sizeof(*starget), GFP_ATOMIC); if (!starget) { printk(ALLOC_FAILURE_MSG, __FUNCTION__); spin_unlock_irqrestore(sdev->host->host_lock, flags); return SCSI_SCAN_NO_RESPONSE; } starget->starget_refcnt = 0; starget->starget_sdev_user = NULL; } starget->starget_refcnt++; sdev->sdev_target = starget; spin_unlock_irqrestore(sdev->host->host_lock, flags); } sdev->use_10_for_rw = 1; if (*bflags & BLIST_MS_SKIP_PAGE_08) sdev->skip_ms_page_8 = 1; if (*bflags & BLIST_MS_SKIP_PAGE_3F) sdev->skip_ms_page_3f = 1; if (*bflags & BLIST_USE_10_BYTE_MS) sdev->use_10_for_ms = 1; /* set the device running here so that slave configure * may do I/O */ scsi_device_set_state(sdev, SDEV_RUNNING); if (*bflags & BLIST_MS_192_BYTES_FOR_3F) sdev->use_192_bytes_for_3f = 1; if (*bflags & BLIST_NOT_LOCKABLE) sdev->lockable = 0; if(sdev->host->hostt->slave_configure) sdev->host->hostt->slave_configure(sdev); /* * Ok, the device is now all set up, we can * register it and tell the rest of the kernel * about it. */ scsi_sysfs_add_sdev(sdev); return SCSI_SCAN_LUN_PRESENT;}/** * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it * @sdevscan: probe the LUN corresponding to this Scsi_Device * @sdevnew: store the value of any new Scsi_Device allocated * @bflagsp: store bflags here if not NULL * * Description: * Call scsi_probe_lun, if a LUN with an attached device is found, * allocate and set it up by calling scsi_add_lun. * * Return: * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is * attached at the LUN * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized **/static int scsi_probe_and_add_lun(struct Scsi_Host *host, uint channel, uint id, uint lun, int *bflagsp, struct scsi_device **sdevp, int rescan, void *hostdata){ struct scsi_device *sdev; struct scsi_request *sreq; unsigned char *result; int bflags, res = SCSI_SCAN_NO_RESPONSE; /* * The rescan flag is used as an optimization, the first scan of a * host adapter calls into here with rescan == 0. */ if (rescan) { sdev = scsi_device_lookup(host, channel, id, lun); if (sdev) { SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: device exists on <%d:%d:%d:%d>\n", host->host_no, channel, id, lun)); if (sdevp) *sdevp = sdev; if (bflagsp) *bflagsp = scsi_get_device_flags(sdev, sdev->vendor, sdev->model); /* XXX: bandaid until callers do refcounting */ scsi_device_put(sdev); return SCSI_SCAN_LUN_PRESENT; } } sdev = scsi_alloc_sdev(host, channel, id, lun, hostdata); if (!sdev) goto out; sreq = scsi_allocate_request(sdev, GFP_ATOMIC); if (!sreq) goto out_free_sdev; result = kmalloc(256, GFP_ATOMIC | (host->unchecked_isa_dma) ? __GFP_DMA : 0); if (!result) goto out_free_sreq; scsi_probe_lun(sreq, result, &bflags); if (sreq->sr_result) goto out_free_result; /* * result contains valid SCSI INQUIRY data. */ if ((result[0] >> 5) == 3) { /* * For a Peripheral qualifier 3 (011b), the SCSI * spec says: The device server is not capable of * supporting a physical device on this logical * unit. * * For disks, this implies that there is no * logical disk configured at sdev->lun, but there * is a target id responding. */ SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: peripheral qualifier of 3," " no device added\n")); res = SCSI_SCAN_TARGET_PRESENT; goto out_free_result; } res = scsi_add_lun(sdev, result, &bflags); if (res == SCSI_SCAN_LUN_PRESENT) { if (bflags & BLIST_KEY) { sdev->lockable = 0; scsi_unlock_floptical(sreq, result); } if (bflagsp) *bflagsp = bflags; } out_free_result: kfree(result); out_free_sreq: scsi_release_request(sreq); out_free_sdev: if (res == SCSI_SCAN_LUN_PRESENT) { if (sdevp) *sdevp = sdev; } else { if (sdev->host->hostt->slave_destroy) sdev->host->hostt->slave_destroy(sdev); if (sdev->host->transportt->cleanup) sdev->host->transportt->cleanup(sdev); put_device(&sdev->sdev_gendev); } out: return res;}/** * scsi_sequential_lun_scan - sequentially scan a SCSI target * @sdevscan: scan the host, channel, and id of this Scsi_Device * @bflags: black/white list flag for LUN 0 * @lun0_res: result of scanning LUN 0 * * Description: * Generally, scan from LUN 1 (LUN 0 is assumed to already have been * scanned) to some maximum lun until a LUN is found with no device * attached. Use the bflags to figure out any oddities. * * Modifies sdevscan->lun. **/static void scsi_sequential_lun_scan(struct Scsi_Host *shost, uint channel, uint id, int bflags, int lun0_res, int scsi_level, int rescan){ unsigned int sparse_lun, lun, max_dev_lun; SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of" " host %d channel %d id %d\n", shost->host_no, channel, id)); max_dev_lun = min(max_scsi_luns, shost->max_lun); /* * If this device is known to support sparse multiple units, * override the other settings, and scan all of them. Normally, * SCSI-3 devices should be scanned via the REPORT LUNS. */ if (bflags & BLIST_SPARSELUN) { max_dev_lun = shost->max_lun; sparse_lun = 1; } else sparse_lun = 0; /* * If not sparse lun and no device attached at LUN 0 do not scan * any further. */ if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT)) return; /* * If less than SCSI_1_CSS, and no special lun scaning, stop * scanning; this matches 2.4 behaviour, but could just be a bug * (to continue scanning a SCSI_1_CSS device). * * This test is broken. We might not have any device on lun0 for * a sparselun device, and if that's the case then how would we * know the real scsi_level, eh? It might make sense to just not * scan any SCSI_1 device for non-0 luns, but that check would best * go into scsi_alloc_sdev() and just have it return null when asked * to alloc an sdev for lun > 0 on an already found SCSI_1 device. * if ((sdevscan->scsi_level < SCSI_1_CCS) && ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) == 0)) return; */ /* * If this device is known to support multiple units, override * the other settings, and scan all of them. */ if (bflags & BLIST_FORCELUN) max_dev_lun = shost->max_lun; /* * REGAL CDC-4X: avoid hang after LUN 4 */ if (bflags & BLIST_MAX5LUN) max_dev_lun = min(5U, max_dev_lun); /* * Do not scan SCSI-2 or lower device past LUN 7, unless * BLIST_LARGELUN. */ if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) max_dev_lun = min(8U, max_dev_lun); /* * We have already scanned LUN 0, so start at LUN 1. Keep scanning * until we reach the max, or no LUN is found and we are not * sparse_lun. */ for (lun = 1; lun < max_dev_lun; ++lun) if ((scsi_probe_and_add_lun(shost, channel, id, lun, NULL, NULL, rescan, NULL) != SCSI_SCAN_LUN_PRESENT) && !sparse_lun) return;}/** * scsilun_to_int: convert a scsi_lun to an int * @scsilun: struct scsi_lun to be converted. * * Description: * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered * integer, and return the result. The caller must check for * truncation before using this function. * * Notes: * The struct scsi_lun is assumed to be four levels, with each level * effectively containing a SCSI byte-ordered (big endian) short; the * addressing bits of each level are ignored (the highest two bits). * For a description of the LUN format, post SCSI-3 see the SCSI * Architecture Model, for SCSI-3 see the SCSI Controller Commands. * * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns * the integer: 0x0b030a04 **/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?