dasd_devmap.c

来自「linux 内核源代码」· C语言 代码 · 共 1,152 行 · 第 1/2 页

C
1,152
字号
	}	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);	cdev->dev.driver_data = device;	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);	return device;}/* * Wait queue for dasd_delete_device waits. */static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);/* * Remove a dasd device structure. The passed referenced * is destroyed. */voiddasd_delete_device(struct dasd_device *device){	struct ccw_device *cdev;	struct dasd_devmap *devmap;	unsigned long flags;	/* First remove device pointer from devmap. */	devmap = dasd_find_busid(device->cdev->dev.bus_id);	BUG_ON(IS_ERR(devmap));	spin_lock(&dasd_devmap_lock);	if (devmap->device != device) {		spin_unlock(&dasd_devmap_lock);		dasd_put_device(device);		return;	}	devmap->device = NULL;	spin_unlock(&dasd_devmap_lock);	/* Disconnect dasd_device structure from ccw_device structure. */	spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);	device->cdev->dev.driver_data = NULL;	spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);	/*	 * Drop ref_count by 3, one for the devmap reference, one for	 * the cdev reference and one for the passed reference.	 */	atomic_sub(3, &device->ref_count);	/* Wait for reference counter to drop to zero. */	wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);	/* Disconnect dasd_device structure from ccw_device structure. */	cdev = device->cdev;	device->cdev = NULL;	/* Put ccw_device structure. */	put_device(&cdev->dev);	/* Now the device structure can be freed. */	dasd_free_device(device);}/* * Reference counter dropped to zero. Wake up waiter * in dasd_delete_device. */voiddasd_put_device_wake(struct dasd_device *device){	wake_up(&dasd_delete_wq);}/* * Return dasd_device structure associated with cdev. * This function needs to be called with the ccw device * lock held. It can be used from interrupt context. */struct dasd_device *dasd_device_from_cdev_locked(struct ccw_device *cdev){	struct dasd_device *device = cdev->dev.driver_data;	if (!device)		return ERR_PTR(-ENODEV);	dasd_get_device(device);	return device;}/* * Return dasd_device structure associated with cdev. */struct dasd_device *dasd_device_from_cdev(struct ccw_device *cdev){	struct dasd_device *device;	unsigned long flags;	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);	device = dasd_device_from_cdev_locked(cdev);	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);	return device;}/* * SECTION: files in sysfs *//* * readonly controls the readonly status of a dasd */static ssize_tdasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	int ro_flag;	devmap = dasd_find_busid(dev->bus_id);	if (!IS_ERR(devmap))		ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;	else		ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;	return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");}static ssize_tdasd_ro_store(struct device *dev, struct device_attribute *attr,	      const char *buf, size_t count){	struct dasd_devmap *devmap;	int val;	char *endp;	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));	if (IS_ERR(devmap))		return PTR_ERR(devmap);	val = simple_strtoul(buf, &endp, 0);	if (((endp + 1) < (buf + count)) || (val > 1))		return -EINVAL;	spin_lock(&dasd_devmap_lock);	if (val)		devmap->features |= DASD_FEATURE_READONLY;	else		devmap->features &= ~DASD_FEATURE_READONLY;	if (devmap->device)		devmap->device->features = devmap->features;	if (devmap->device && devmap->device->gdp)		set_disk_ro(devmap->device->gdp, val);	spin_unlock(&dasd_devmap_lock);	return count;}static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);/* * erplog controls the logging of ERP related data * (e.g. failing channel programs). */static ssize_tdasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	int erplog;	devmap = dasd_find_busid(dev->bus_id);	if (!IS_ERR(devmap))		erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;	else		erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;	return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");}static ssize_tdasd_erplog_store(struct device *dev, struct device_attribute *attr,	      const char *buf, size_t count){	struct dasd_devmap *devmap;	int val;	char *endp;	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));	if (IS_ERR(devmap))		return PTR_ERR(devmap);	val = simple_strtoul(buf, &endp, 0);	if (((endp + 1) < (buf + count)) || (val > 1))		return -EINVAL;	spin_lock(&dasd_devmap_lock);	if (val)		devmap->features |= DASD_FEATURE_ERPLOG;	else		devmap->features &= ~DASD_FEATURE_ERPLOG;	if (devmap->device)		devmap->device->features = devmap->features;	spin_unlock(&dasd_devmap_lock);	return count;}static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);/* * use_diag controls whether the driver should use diag rather than ssch * to talk to the device */static ssize_tdasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	int use_diag;	devmap = dasd_find_busid(dev->bus_id);	if (!IS_ERR(devmap))		use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;	else		use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;	return sprintf(buf, use_diag ? "1\n" : "0\n");}static ssize_tdasd_use_diag_store(struct device *dev, struct device_attribute *attr,		    const char *buf, size_t count){	struct dasd_devmap *devmap;	ssize_t rc;	int val;	char *endp;	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));	if (IS_ERR(devmap))		return PTR_ERR(devmap);	val = simple_strtoul(buf, &endp, 0);	if (((endp + 1) < (buf + count)) || (val > 1))		return -EINVAL;	spin_lock(&dasd_devmap_lock);	/* Changing diag discipline flag is only allowed in offline state. */	rc = count;	if (!devmap->device) {		if (val)			devmap->features |= DASD_FEATURE_USEDIAG;		else			devmap->features &= ~DASD_FEATURE_USEDIAG;	} else		rc = -EPERM;	spin_unlock(&dasd_devmap_lock);	return rc;}static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);static ssize_tdasd_discipline_show(struct device *dev, struct device_attribute *attr,		     char *buf){	struct dasd_device *device;	ssize_t len;	device = dasd_device_from_cdev(to_ccwdev(dev));	if (!IS_ERR(device) && device->discipline) {		len = snprintf(buf, PAGE_SIZE, "%s\n",			       device->discipline->name);		dasd_put_device(device);	} else		len = snprintf(buf, PAGE_SIZE, "none\n");	return len;}static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);static ssize_tdasd_device_status_show(struct device *dev, struct device_attribute *attr,		     char *buf){	struct dasd_device *device;	ssize_t len;	device = dasd_device_from_cdev(to_ccwdev(dev));	if (!IS_ERR(device)) {		switch (device->state) {		case DASD_STATE_NEW:			len = snprintf(buf, PAGE_SIZE, "new\n");			break;		case DASD_STATE_KNOWN:			len = snprintf(buf, PAGE_SIZE, "detected\n");			break;		case DASD_STATE_BASIC:			len = snprintf(buf, PAGE_SIZE, "basic\n");			break;		case DASD_STATE_UNFMT:			len = snprintf(buf, PAGE_SIZE, "unformatted\n");			break;		case DASD_STATE_READY:			len = snprintf(buf, PAGE_SIZE, "ready\n");			break;		case DASD_STATE_ONLINE:			len = snprintf(buf, PAGE_SIZE, "online\n");			break;		default:			len = snprintf(buf, PAGE_SIZE, "no stat\n");			break;		}		dasd_put_device(device);	} else		len = snprintf(buf, PAGE_SIZE, "unknown\n");	return len;}static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);static ssize_tdasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	int alias;	devmap = dasd_find_busid(dev->bus_id);	spin_lock(&dasd_devmap_lock);	if (!IS_ERR(devmap))		alias = devmap->uid.alias;	else		alias = 0;	spin_unlock(&dasd_devmap_lock);	return sprintf(buf, alias ? "1\n" : "0\n");}static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);static ssize_tdasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	char *vendor;	devmap = dasd_find_busid(dev->bus_id);	spin_lock(&dasd_devmap_lock);	if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)		vendor = devmap->uid.vendor;	else		vendor = "";	spin_unlock(&dasd_devmap_lock);	return snprintf(buf, PAGE_SIZE, "%s\n", vendor);}static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);#define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial    */ 14 + 1 +\		     /* SSID   */ 4 + 1 + /* unit addr */ 2 + 1)static ssize_tdasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	char uid[UID_STRLEN];	devmap = dasd_find_busid(dev->bus_id);	spin_lock(&dasd_devmap_lock);	if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)		snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x",			 devmap->uid.vendor, devmap->uid.serial,			 devmap->uid.ssid, devmap->uid.unit_addr);	else		uid[0] = 0;	spin_unlock(&dasd_devmap_lock);	return snprintf(buf, PAGE_SIZE, "%s\n", uid);}static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);/* * extended error-reporting */static ssize_tdasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf){	struct dasd_devmap *devmap;	int eer_flag;	devmap = dasd_find_busid(dev->bus_id);	if (!IS_ERR(devmap) && devmap->device)		eer_flag = dasd_eer_enabled(devmap->device);	else		eer_flag = 0;	return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");}static ssize_tdasd_eer_store(struct device *dev, struct device_attribute *attr,	       const char *buf, size_t count){	struct dasd_devmap *devmap;	int val, rc;	char *endp;	devmap = dasd_devmap_from_cdev(to_ccwdev(dev));	if (IS_ERR(devmap))		return PTR_ERR(devmap);	if (!devmap->device)		return -ENODEV;	val = simple_strtoul(buf, &endp, 0);	if (((endp + 1) < (buf + count)) || (val > 1))		return -EINVAL;	if (val) {		rc = dasd_eer_enable(devmap->device);		if (rc)			return rc;	} else		dasd_eer_disable(devmap->device);	return count;}static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);static struct attribute * dasd_attrs[] = {	&dev_attr_readonly.attr,	&dev_attr_discipline.attr,	&dev_attr_status.attr,	&dev_attr_alias.attr,	&dev_attr_vendor.attr,	&dev_attr_uid.attr,	&dev_attr_use_diag.attr,	&dev_attr_eer_enabled.attr,	&dev_attr_erplog.attr,	NULL,};static struct attribute_group dasd_attr_group = {	.attrs = dasd_attrs,};/* * Return copy of the device unique identifier. */intdasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid){	struct dasd_devmap *devmap;	devmap = dasd_find_busid(cdev->dev.bus_id);	if (IS_ERR(devmap))		return PTR_ERR(devmap);	spin_lock(&dasd_devmap_lock);	*uid = devmap->uid;	spin_unlock(&dasd_devmap_lock);	return 0;}/* * Register the given device unique identifier into devmap struct. * In addition check if the related storage server subsystem ID is already * contained in the dasd_server_ssid_list. If subsystem ID is not contained, * create new entry. * Return 0 if server was already in serverlist, *	  1 if the server was added successful *	 <0 in case of error. */intdasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid){	struct dasd_devmap *devmap;	struct dasd_server_ssid_map *srv, *tmp;	devmap = dasd_find_busid(cdev->dev.bus_id);	if (IS_ERR(devmap))		return PTR_ERR(devmap);	/* generate entry for server_ssid_map */	srv = (struct dasd_server_ssid_map *)		kzalloc(sizeof(struct dasd_server_ssid_map), GFP_KERNEL);	if (!srv)		return -ENOMEM;	strncpy(srv->sid.vendor, uid->vendor, sizeof(srv->sid.vendor) - 1);	strncpy(srv->sid.serial, uid->serial, sizeof(srv->sid.serial) - 1);	srv->sid.ssid = uid->ssid;	/* server is already contained ? */	spin_lock(&dasd_devmap_lock);	devmap->uid = *uid;	list_for_each_entry(tmp, &dasd_server_ssid_list, list) {		if (!memcmp(&srv->sid, &tmp->sid,			    sizeof(struct system_id))) {			kfree(srv);			srv = NULL;			break;		}	}	/* add servermap to serverlist */	if (srv)		list_add(&srv->list, &dasd_server_ssid_list);	spin_unlock(&dasd_devmap_lock);	return (srv ? 1 : 0);}EXPORT_SYMBOL_GPL(dasd_set_uid);/* * Return value of the specified feature. */intdasd_get_feature(struct ccw_device *cdev, int feature){	struct dasd_devmap *devmap;	devmap = dasd_find_busid(cdev->dev.bus_id);	if (IS_ERR(devmap))		return PTR_ERR(devmap);	return ((devmap->features & feature) != 0);}/* * Set / reset given feature. * Flag indicates wether to set (!=0) or the reset (=0) the feature. */intdasd_set_feature(struct ccw_device *cdev, int feature, int flag){	struct dasd_devmap *devmap;	devmap = dasd_find_busid(cdev->dev.bus_id);	if (IS_ERR(devmap))		return PTR_ERR(devmap);	spin_lock(&dasd_devmap_lock);	if (flag)		devmap->features |= feature;	else		devmap->features &= ~feature;	if (devmap->device)		devmap->device->features = devmap->features;	spin_unlock(&dasd_devmap_lock);	return 0;}intdasd_add_sysfs_files(struct ccw_device *cdev){	return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);}voiddasd_remove_sysfs_files(struct ccw_device *cdev){	sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);}intdasd_devmap_init(void){	int i;	/* Initialize devmap structures. */	dasd_max_devindex = 0;	for (i = 0; i < 256; i++)		INIT_LIST_HEAD(&dasd_hashlists[i]);	/* Initialize servermap structure. */	INIT_LIST_HEAD(&dasd_server_ssid_list);	return 0;}voiddasd_devmap_exit(void){	dasd_forget_ranges();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?