From: Somasundaram Krishnasamy <somasundaram.krishnasamy@lsi.com>
Subject: Kernel oops during bootup in ses_intf_add()
References: bnc#670816
Patch-Mainline: Submitted to scsi-misc

During device discovery (bootup), scsi mid layer sends INQUIRY
command to LUN 0. If the LUN 0 is not mapped to host, it will
create a temporary scsi_device with LUN id 0 and send REPORT_LUNS
command to it. After the REPORT_LUNS succeeds, it walks through
the LUN table returned by REPORT_LUNS and adds each LUN found to
sysfs. At the end of REPORT_LUNS lun table scan, it will delete
the temporary scsi_device of LUN 0.

When scsi devices are added to sysfs, it calls add_dev function
of all the registered class interfaces. If ses driver is registered,
it will call ses_intf_add(). This function in
ses_intf_add()->scsi_device_enclosure() path
will try to access the inquiry data for the temporary lun 0
scsi_device. 

static inline int scsi_device_enclosure(struct scsi_device *sdev)
{
        return sdev->inquiry[6] & (1<<6);
}

Since sdev->inquiry was not allocated for temporary LUN 0 scsi_device,
it will cause NULL pointer exception.

Signed-off-by: Hannes Reinecke <hare@suse.de>

diff --git a/drivers/scsi/ses.c b/drivers/scsi/ses.c
index 0d9d6f7..89b6c13 100644
--- a/drivers/scsi/ses.c
+++ b/drivers/scsi/ses.c
@@ -617,7 +617,9 @@ static int ses_intf_add(struct device *cdev,
 	/* see if there are any devices matching before
 	 * we found the enclosure */
 	shost_for_each_device(tmp_sdev, sdev->host) {
-		if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
+		if (tmp_sdev->lun != 0 ||
+		    tmp_sdev->sdev_state != SDEV_RUNNING ||
+		    scsi_device_enclosure(tmp_sdev))
 			continue;
 		ses_match_to_enclosure(edev, tmp_sdev);
 	}
