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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/semaphore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <asm/rtas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "cxl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "hcalls.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define DOWNLOAD_IMAGE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define VALIDATE_IMAGE 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) struct ai_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	u16 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	u8  reserved0[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u16 vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	u16 device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	u16 subsystem_vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	u16 subsystem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u64 image_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	u64 image_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	u8  reserved1[96];
^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 struct semaphore sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static unsigned long *buffer[CXL_AI_MAX_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static struct sg_list *le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static u64 continue_token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static unsigned int transfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct update_props_workarea {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	__be32 phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	__be32 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	__be64 reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	__be32 nprops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) struct update_nodes_workarea {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	__be32 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	__be64 unit_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	__be32 reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define DEVICE_SCOPE 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define NODE_ACTION_MASK	0xff000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define NODE_COUNT_MASK		0x00ffffff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define OPCODE_DELETE	0x01000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define OPCODE_UPDATE	0x02000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define OPCODE_ADD	0x03000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int rcall(int token, char *buf, s32 scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	spin_lock(&rtas_data_buf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	spin_unlock(&rtas_data_buf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int update_property(struct device_node *dn, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			   u32 vd, char *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct property *new_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	u32 *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (!new_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	new_prop->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (!new_prop->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		kfree(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	new_prop->length = vd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (!new_prop->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		kfree(new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		kfree(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	memcpy(new_prop->value, value, vd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	val = (u32 *)new_prop->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	rc = cxl_update_properties(dn, new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	pr_devel("%pOFn: update property (%s, length: %i, value: %#x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		  dn, name, vd, be32_to_cpu(*val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		kfree(new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		kfree(new_prop->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		kfree(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int update_node(__be32 phandle, s32 scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct update_props_workarea *upwa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct device_node *dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int i, rc, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	char *prop_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	u32 nprops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u32 vd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	token = rtas_token("ibm,update-properties");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (token == RTAS_UNKNOWN_SERVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!dn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	upwa = (struct update_props_workarea *)&buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	upwa->phandle = phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		rc = rcall(token, buf, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		prop_data = buf + sizeof(*upwa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		nprops = be32_to_cpu(upwa->nprops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (*prop_data == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			prop_data++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			vd = be32_to_cpu(*(__be32 *)prop_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			prop_data += vd + sizeof(vd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			nprops--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		for (i = 0; i < nprops; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			char *prop_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			prop_name = prop_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			prop_data += strlen(prop_name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			vd = be32_to_cpu(*(__be32 *)prop_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			prop_data += sizeof(vd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			if ((vd != 0x00000000) && (vd != 0x80000000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				ret = update_property(dn, prop_name, vd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 						prop_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 					pr_err("cxl: Could not update property %s - %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 					       prop_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				prop_data += vd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	} while (rc == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	of_node_put(dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int update_devicetree(struct cxl *adapter, s32 scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct update_nodes_workarea *unwa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	u32 action, node_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int token, rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	__be32 *data, phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	token = rtas_token("ibm,update-nodes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (token == RTAS_UNKNOWN_SERVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	unwa = (struct update_nodes_workarea *)&buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	unwa->unit_address = cpu_to_be64(adapter->guest->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		rc = rcall(token, buf, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (rc && rc != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		data = (__be32 *)buf + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			action = be32_to_cpu(*data) & NODE_ACTION_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			pr_devel("device reconfiguration - action: %#x, nodes: %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				 action, node_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			data++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			for (i = 0; i < node_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				phandle = *data++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 				switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				case OPCODE_DELETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 					/* nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				case OPCODE_UPDATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 					update_node(phandle, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				case OPCODE_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 					/* nothing to do, just move pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 					data++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	} while (rc == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return 0;
^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 int handle_image(struct cxl *adapter, int operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			long (*fct)(u64, u64, u64, u64 *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			struct cxl_adapter_image *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	size_t mod, s_copy, len_chunk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct ai_header *header = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	unsigned int entries = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	void *dest, *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int rc = 0, need_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	/* base adapter image header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	need_header = (ai->flags & CXL_AI_NEED_HEADER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (need_header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		header = kzalloc(sizeof(struct ai_header), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (!header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		header->version = cpu_to_be16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		header->vendor = cpu_to_be16(adapter->guest->vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		header->device = cpu_to_be16(adapter->guest->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		header->subsystem_vendor = cpu_to_be16(adapter->guest->subsystem_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		header->subsystem = cpu_to_be16(adapter->guest->subsystem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		header->image_offset = cpu_to_be64(CXL_AI_HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		header->image_length = cpu_to_be64(ai->len_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/* number of entries in the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	len_chunk = ai->len_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (need_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		len_chunk += CXL_AI_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	entries = len_chunk / CXL_AI_BUFFER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mod = len_chunk % CXL_AI_BUFFER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (entries > CXL_AI_MAX_ENTRIES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/*          < -- MAX_CHUNK_SIZE = 4096 * 256 = 1048576 bytes -->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * chunk 0  ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 *          | header   |  data                                 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 *          ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 * chunk 1  ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	 *          | data                                             |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 *          ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 * ....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 * chunk n  ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 *          | data                                             |
^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) 	from = (void *) ai->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	for (i = 0; i < entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		dest = buffer[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		s_copy = CXL_AI_BUFFER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if ((need_header) && (i == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			/* add adapter image header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			memcpy(buffer[i], header, sizeof(struct ai_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			s_copy = CXL_AI_BUFFER_SIZE - CXL_AI_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			dest += CXL_AI_HEADER_SIZE; /* image offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		if ((i == (entries - 1)) && mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			s_copy = mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		/* copy data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		if (copy_from_user(dest, from, s_copy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		/* fill in the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		le[i].phys_addr = cpu_to_be64(virt_to_phys(buffer[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		le[i].len = cpu_to_be64(CXL_AI_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if ((i == (entries - 1)) && mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			le[i].len = cpu_to_be64(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		from += s_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	pr_devel("%s (op: %i, need header: %i, entries: %i, token: %#llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		 __func__, operation, need_header, entries, continue_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	 * download/validate the adapter image to the coherent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 * platform facility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	rc = fct(adapter->guest->handle, virt_to_phys(le), entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		&continue_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (rc == 0) /* success of download/validation operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		continue_token = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	kfree(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int transfer_image(struct cxl *adapter, int operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			struct cxl_adapter_image *ai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	switch (operation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	case DOWNLOAD_IMAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		rc = handle_image(adapter, operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				&cxl_h_download_adapter_image, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			pr_devel("resetting adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			cxl_h_reset_adapter(adapter->guest->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	case VALIDATE_IMAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		rc = handle_image(adapter, operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				&cxl_h_validate_adapter_image, ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			pr_devel("resetting adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			cxl_h_reset_adapter(adapter->guest->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			pr_devel("remove current afu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			for (afu = 0; afu < adapter->slices; afu++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				cxl_guest_remove_afu(adapter->afu[afu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			pr_devel("resetting adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			cxl_h_reset_adapter(adapter->guest->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			/* The entire image has now been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			 * downloaded and the validation has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			 * been successfully performed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			 * After that, the partition should call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			 * ibm,update-nodes and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			 * ibm,update-properties to receive the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			 * current configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			rc = update_devicetree(adapter, DEVICE_SCOPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			transfer = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static long ioctl_transfer_image(struct cxl *adapter, int operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 				struct cxl_adapter_image __user *uai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct cxl_adapter_image ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	pr_devel("%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (copy_from_user(&ai, uai, sizeof(struct cxl_adapter_image)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 * Make sure reserved fields and bits are set to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (ai.reserved1 || ai.reserved2 || ai.reserved3 || ai.reserved4 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		(ai.flags & ~CXL_AI_ALL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	return transfer_image(adapter, operation, &ai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static int device_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	struct cxl *adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	int rc = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	pr_devel("in %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	BUG_ON(sizeof(struct ai_header) != CXL_AI_HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/* Allows one process to open the device by using a semaphore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (down_interruptible(&sem) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (!(adapter = get_cxl_adapter(adapter_num))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	file->private_data = adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	continue_token = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	transfer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	for (i = 0; i < CXL_AI_MAX_ENTRIES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		buffer[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/* aligned buffer containing list entries which describes up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 * 1 megabyte of data (256 entries of 4096 bytes each)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	 *  Logical real address of buffer 0  -  Buffer 0 length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 *  Logical real address of buffer 1  -  Buffer 1 length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	 *  Logical real address of buffer 2  -  Buffer 2 length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	 *  ....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	 *  ....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 *  Logical real address of buffer N  -  Buffer N length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (!le) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		goto err;
^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) 	for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		buffer[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		if (!buffer[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			goto err1;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (buffer[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			free_page((unsigned long) buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		free_page((unsigned long) le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	put_device(&adapter->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	up(&sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	struct cxl *adapter = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	pr_devel("in %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (cmd == CXL_IOCTL_DOWNLOAD_IMAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		return ioctl_transfer_image(adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 					DOWNLOAD_IMAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 					(struct cxl_adapter_image __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	else if (cmd == CXL_IOCTL_VALIDATE_IMAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		return ioctl_transfer_image(adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 					VALIDATE_IMAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 					(struct cxl_adapter_image __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int device_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct cxl *adapter = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	pr_devel("in %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		if (buffer[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			free_page((unsigned long) buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (le)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		free_page((unsigned long) le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	up(&sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	put_device(&adapter->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	continue_token = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	/* reload the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (transfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		cxl_guest_reload_module(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		pr_devel("resetting adapter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		cxl_h_reset_adapter(adapter->guest->handle);
^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) 	transfer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static const struct file_operations fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	.open		= device_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	.unlocked_ioctl	= device_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	.release	= device_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) void cxl_guest_remove_chardev(struct cxl *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	cdev_del(&adapter->guest->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) int cxl_guest_add_chardev(struct cxl *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	dev_t devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	devt = MKDEV(MAJOR(cxl_get_dev()), CXL_CARD_MINOR(adapter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	cdev_init(&adapter->guest->cdev, &fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if ((rc = cdev_add(&adapter->guest->cdev, devt, 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		dev_err(&adapter->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			"Unable to add chardev on adapter (card%i): %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			adapter->adapter_num, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	adapter->dev.devt = devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	sema_init(&sem, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }