Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2014 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/pci_regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "cxl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define to_afu_chardev_m(d) dev_get_drvdata(d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*********  Adapter attributes  **********************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static ssize_t caia_version_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	return scnprintf(buf, PAGE_SIZE, "%i.%i\n", adapter->caia_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			 adapter->caia_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static ssize_t psl_revision_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static ssize_t base_image_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->base_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static ssize_t image_loaded_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (adapter->user_image_loaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return scnprintf(buf, PAGE_SIZE, "user\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return scnprintf(buf, PAGE_SIZE, "factory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static ssize_t psl_timebase_synced_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	u64 psl_tb, delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	/* Recompute the status only in native mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		psl_tb = adapter->native->sl_ops->timebase_read(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		delta = abs(mftb() - psl_tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		/* CORE TB and PSL TB difference <= 16usecs ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		adapter->psl_timebase_synced = (tb_to_ns(delta) < 16000) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		pr_devel("PSL timebase %s - delta: 0x%016llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			 (tb_to_ns(delta) < 16000) ? "synchronized" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			 "not synchronized", tb_to_ns(delta));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->psl_timebase_synced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static ssize_t tunneled_ops_supported_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->tunneled_ops_supported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static ssize_t reset_adapter_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	rc = sscanf(buf, "%i", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if ((rc != 1) || (val != 1 && val != -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * See if we can lock the context mapping that's only allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * when there are no contexts attached to the adapter. Once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * taken this will also prevent any context from getting activated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (val == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		rc =  cxl_adapter_context_lock(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		rc = cxl_ops->adapter_reset(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		/* In case reset failed release context lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			cxl_adapter_context_unlock(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	} else if (val == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		/* Perform a forced adapter reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		rc = cxl_ops->adapter_reset(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static ssize_t load_image_on_perst_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (!adapter->perst_loads_image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return scnprintf(buf, PAGE_SIZE, "none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (adapter->perst_select_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return scnprintf(buf, PAGE_SIZE, "user\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return scnprintf(buf, PAGE_SIZE, "factory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static ssize_t load_image_on_perst_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!strncmp(buf, "none", 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		adapter->perst_loads_image = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	else if (!strncmp(buf, "user", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		adapter->perst_select_user = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		adapter->perst_loads_image = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	} else if (!strncmp(buf, "factory", 7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		adapter->perst_select_user = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		adapter->perst_loads_image = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if ((rc = cxl_update_image_control(adapter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static ssize_t perst_reloads_same_image_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return scnprintf(buf, PAGE_SIZE, "%i\n", adapter->perst_same_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static ssize_t perst_reloads_same_image_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct cxl *adapter = to_cxl_adapter(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	rc = sscanf(buf, "%i", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if ((rc != 1) || !(val == 1 || val == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	adapter->perst_same_image = (val == 1 ? true : false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static struct device_attribute adapter_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	__ATTR_RO(caia_version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	__ATTR_RO(psl_revision),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	__ATTR_RO(base_image),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	__ATTR_RO(image_loaded),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	__ATTR_RO(psl_timebase_synced),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	__ATTR_RO(tunneled_ops_supported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	__ATTR_RW(load_image_on_perst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	__ATTR_RW(perst_reloads_same_image),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	__ATTR(reset, S_IWUSR, NULL, reset_adapter_store),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*********  AFU master specific attributes  **********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static ssize_t mmio_size_show_master(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	struct cxl_afu *afu = to_afu_chardev_m(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static ssize_t pp_mmio_off_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct cxl_afu *afu = to_afu_chardev_m(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->native->pp_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static ssize_t pp_mmio_len_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct cxl_afu *afu = to_afu_chardev_m(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static struct device_attribute afu_master_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	__ATTR(mmio_size, S_IRUGO, mmio_size_show_master, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	__ATTR_RO(pp_mmio_off),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	__ATTR_RO(pp_mmio_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*********  AFU attributes  **************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static ssize_t mmio_size_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (afu->pp_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->adapter->ps_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static ssize_t reset_store_afu(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* Not safe to reset if it is currently in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	mutex_lock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!idr_is_empty(&afu->contexts_idr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if ((rc = cxl_ops->afu_reset(afu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	rc = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	mutex_unlock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static ssize_t irqs_min_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return scnprintf(buf, PAGE_SIZE, "%i\n", afu->pp_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static ssize_t irqs_max_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return scnprintf(buf, PAGE_SIZE, "%i\n", afu->irqs_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static ssize_t irqs_max_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int irqs_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	ret = sscanf(buf, "%i", &irqs_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (irqs_max < afu->pp_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (irqs_max > afu->adapter->user_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		/* pHyp sets a per-AFU limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if (irqs_max > afu->guest->max_ints)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	afu->irqs_max = irqs_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static ssize_t modes_supported_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				    struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	char *p = buf, *end = buf + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (afu->modes_supported & CXL_MODE_DEDICATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		p += scnprintf(p, end - p, "dedicated_process\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (afu->modes_supported & CXL_MODE_DIRECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		p += scnprintf(p, end - p, "afu_directed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return (p - buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static ssize_t prefault_mode_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	switch (afu->prefault_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	case CXL_PREFAULT_WED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return scnprintf(buf, PAGE_SIZE, "work_element_descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	case CXL_PREFAULT_ALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return scnprintf(buf, PAGE_SIZE, "all\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		return scnprintf(buf, PAGE_SIZE, "none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static ssize_t prefault_mode_store(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	enum prefault_modes mode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!strncmp(buf, "none", 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		mode = CXL_PREFAULT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		if (!radix_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			/* only allowed when not in radix mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			if (!strncmp(buf, "work_element_descriptor", 23))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				mode = CXL_PREFAULT_WED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			if (!strncmp(buf, "all", 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				mode = CXL_PREFAULT_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			dev_err(device, "Cannot prefault with radix enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (mode == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	afu->prefault_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static ssize_t mode_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (afu->current_mode == CXL_MODE_DEDICATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		return scnprintf(buf, PAGE_SIZE, "dedicated_process\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (afu->current_mode == CXL_MODE_DIRECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		return scnprintf(buf, PAGE_SIZE, "afu_directed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	return scnprintf(buf, PAGE_SIZE, "none\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static ssize_t mode_store(struct device *device, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct cxl_afu *afu = to_cxl_afu(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int old_mode, mode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	/* can't change this if we have a user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	mutex_lock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!idr_is_empty(&afu->contexts_idr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (!strncmp(buf, "dedicated_process", 17))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		mode = CXL_MODE_DEDICATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!strncmp(buf, "afu_directed", 12))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		mode = CXL_MODE_DIRECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (!strncmp(buf, "none", 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (mode == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	 * afu_deactivate_mode needs to be done outside the lock, prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	 * other contexts coming in before we are ready:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	old_mode = afu->current_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	afu->current_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	afu->num_procs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	mutex_unlock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	mutex_unlock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static ssize_t api_version_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 				struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static ssize_t api_version_compatible_show(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 					   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 					   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return scnprintf(buf, PAGE_SIZE, "%i\n", CXL_API_VERSION_COMPATIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			       struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			       loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static struct device_attribute afu_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	__ATTR_RO(mmio_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	__ATTR_RO(irqs_min),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	__ATTR_RW(irqs_max),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	__ATTR_RO(modes_supported),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	__ATTR_RW(mode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	__ATTR_RW(prefault_mode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	__ATTR_RO(api_version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	__ATTR_RO(api_version_compatible),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	__ATTR(reset, S_IWUSR, NULL, reset_store_afu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) int cxl_sysfs_adapter_add(struct cxl *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		dev_attr = &adapter_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 						CXL_ADAPTER_ATTRS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			if ((rc = device_create_file(&adapter->dev, dev_attr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	for (i--; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		dev_attr = &adapter_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 						CXL_ADAPTER_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			device_remove_file(&adapter->dev, dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) void cxl_sysfs_adapter_remove(struct cxl *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		dev_attr = &adapter_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 						CXL_ADAPTER_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			device_remove_file(&adapter->dev, dev_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct afu_config_record {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	struct kobject kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct bin_attribute config_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	int cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	u16 device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	u16 vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	u32 class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) #define to_cr(obj) container_of(obj, struct afu_config_record, kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) static ssize_t vendor_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			   struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	struct afu_config_record *cr = to_cr(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static ssize_t device_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			   struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct afu_config_record *cr = to_cr(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	return scnprintf(buf, PAGE_SIZE, "0x%.4x\n", cr->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static ssize_t class_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			  struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	struct afu_config_record *cr = to_cr(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	return scnprintf(buf, PAGE_SIZE, "0x%.6x\n", cr->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			       struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			       loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	struct afu_config_record *cr = to_cr(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	struct cxl_afu *afu = to_cxl_afu(kobj_to_dev(kobj->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	u64 i, j, val, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	for (i = 0; i < count;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			val = ~0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			buf[i] = (val >> (j * 8)) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static struct kobj_attribute vendor_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	__ATTR_RO(vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) static struct kobj_attribute device_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	__ATTR_RO(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static struct kobj_attribute class_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	__ATTR_RO(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static struct attribute *afu_cr_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	&vendor_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	&device_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	&class_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static void release_afu_config_record(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	struct afu_config_record *cr = to_cr(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	kfree(cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static struct kobj_type afu_config_record_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	.sysfs_ops = &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	.release = release_afu_config_record,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	.default_attrs = afu_cr_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int cr_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	struct afu_config_record *cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	cr = kzalloc(sizeof(struct afu_config_record), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (!cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	cr->cr = cr_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	cr->class >>= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	 * Export raw AFU PCIe like config record. For now this is read only by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	 * root - we can expand that later to be readable by non-root and maybe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	 * even writable provided we have a good use-case. Once we support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	 * exposing AFUs through a virtual PHB they will get that for free from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	 * Linux' PCI infrastructure, but until then it's not clear that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	 * need it for anything since the main use case is just identifying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	 * AFUs, which can be done via the vendor, device and class attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	sysfs_bin_attr_init(&cr->config_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	cr->config_attr.attr.name = "config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	cr->config_attr.attr.mode = S_IRUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	cr->config_attr.size = afu->crs_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	cr->config_attr.read = afu_read_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	rc = kobject_init_and_add(&cr->kobj, &afu_config_record_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 				  &afu->dev.kobj, "cr%i", cr->cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	rc = sysfs_create_bin_file(&cr->kobj, &cr->config_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	rc = kobject_uevent(&cr->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	return cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	kobject_put(&cr->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	kfree(cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) void cxl_sysfs_afu_remove(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	struct afu_config_record *cr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	/* remove the err buffer bin attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	if (afu->eb_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		device_remove_bin_file(&afu->dev, &afu->attr_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		dev_attr = &afu_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 						CXL_AFU_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			device_remove_file(&afu->dev, &afu_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	list_for_each_entry_safe(cr, tmp, &afu->crs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		sysfs_remove_bin_file(&cr->kobj, &cr->config_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		kobject_put(&cr->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) int cxl_sysfs_afu_add(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	struct afu_config_record *cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	INIT_LIST_HEAD(&afu->crs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	for (i = 0; i < ARRAY_SIZE(afu_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		dev_attr = &afu_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 						CXL_AFU_ATTRS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 			if ((rc = device_create_file(&afu->dev, &afu_attrs[i])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	/* conditionally create the add the binary file for error info buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (afu->eb_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		sysfs_attr_init(&afu->attr_eb.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		afu->attr_eb.attr.name = "afu_err_buff";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		afu->attr_eb.attr.mode = S_IRUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		afu->attr_eb.size = afu->eb_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		afu->attr_eb.read = afu_eb_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		rc = device_create_bin_file(&afu->dev, &afu->attr_eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			dev_err(&afu->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 				"Unable to create eb attr for the afu. Err(%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 				rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	for (i = 0; i < afu->crs_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		cr = cxl_sysfs_afu_new_cr(afu, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		if (IS_ERR(cr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			rc = PTR_ERR(cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		list_add(&cr->list, &afu->crs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	cxl_sysfs_afu_remove(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	/* reset the eb_len as we havent created the bin attr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	afu->eb_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	for (i--; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		dev_attr = &afu_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 						CXL_AFU_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		device_remove_file(&afu->dev, &afu_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) int cxl_sysfs_afu_m_add(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		dev_attr = &afu_master_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 						CXL_AFU_MASTER_ATTRS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 			if ((rc = device_create_file(afu->chardev_m, &afu_master_attrs[i])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	for (i--; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		dev_attr = &afu_master_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 						CXL_AFU_MASTER_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 			device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) void cxl_sysfs_afu_m_remove(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	struct device_attribute *dev_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	for (i = 0; i < ARRAY_SIZE(afu_master_attrs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		dev_attr = &afu_master_attrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		if (cxl_ops->support_attributes(dev_attr->attr.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 						CXL_AFU_MASTER_ATTRS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 			device_remove_file(afu->chardev_m, &afu_master_attrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }