1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
4 #include <linux/semaphore.h>
5 #include <linux/slab.h>
6 #include <linux/uaccess.h>
13 #define DOWNLOAD_IMAGE 1
14 #define VALIDATE_IMAGE 2
28 static struct semaphore sem;
29 static unsigned long *buffer[CXL_AI_MAX_ENTRIES];
30 static struct sg_list *le;
31 static u64 continue_token;
32 static unsigned int transfer;
34 struct update_props_workarea {
41 struct update_nodes_workarea {
47 #define DEVICE_SCOPE 3
48 #define NODE_ACTION_MASK 0xff000000
49 #define NODE_COUNT_MASK 0x00ffffff
50 #define OPCODE_DELETE 0x01000000
51 #define OPCODE_UPDATE 0x02000000
52 #define OPCODE_ADD 0x03000000
54 static int rcall(int token, char *buf, s32 scope)
58 spin_lock(&rtas_data_buf_lock);
60 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
61 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
62 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
64 spin_unlock(&rtas_data_buf_lock);
68 static int update_property(struct device_node *dn, const char *name,
71 struct property *new_prop;
75 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
79 new_prop->name = kstrdup(name, GFP_KERNEL);
80 if (!new_prop->name) {
85 new_prop->length = vd;
86 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
87 if (!new_prop->value) {
88 kfree(new_prop->name);
92 memcpy(new_prop->value, value, vd);
94 val = (u32 *)new_prop->value;
95 rc = cxl_update_properties(dn, new_prop);
96 pr_devel("%pOFn: update property (%s, length: %i, value: %#x)\n",
97 dn, name, vd, be32_to_cpu(*val));
100 kfree(new_prop->name);
101 kfree(new_prop->value);
107 static int update_node(__be32 phandle, s32 scope)
109 struct update_props_workarea *upwa;
110 struct device_node *dn;
118 token = rtas_token("ibm,update-properties");
119 if (token == RTAS_UNKNOWN_SERVICE)
122 buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
126 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
132 upwa = (struct update_props_workarea *)&buf[0];
133 upwa->phandle = phandle;
135 rc = rcall(token, buf, scope);
139 prop_data = buf + sizeof(*upwa);
140 nprops = be32_to_cpu(upwa->nprops);
142 if (*prop_data == 0) {
144 vd = be32_to_cpu(*(__be32 *)prop_data);
145 prop_data += vd + sizeof(vd);
149 for (i = 0; i < nprops; i++) {
152 prop_name = prop_data;
153 prop_data += strlen(prop_name) + 1;
154 vd = be32_to_cpu(*(__be32 *)prop_data);
155 prop_data += sizeof(vd);
157 if ((vd != 0x00000000) && (vd != 0x80000000)) {
158 ret = update_property(dn, prop_name, vd,
161 pr_err("cxl: Could not update property %s - %i\n",
174 static int update_devicetree(struct cxl *adapter, s32 scope)
176 struct update_nodes_workarea *unwa;
177 u32 action, node_count;
179 __be32 *data, phandle;
182 token = rtas_token("ibm,update-nodes");
183 if (token == RTAS_UNKNOWN_SERVICE)
186 buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
190 unwa = (struct update_nodes_workarea *)&buf[0];
191 unwa->unit_address = cpu_to_be64(adapter->guest->handle);
193 rc = rcall(token, buf, scope);
197 data = (__be32 *)buf + 4;
198 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
199 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
200 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
201 pr_devel("device reconfiguration - action: %#x, nodes: %#x\n",
205 for (i = 0; i < node_count; i++) {
213 update_node(phandle, scope);
216 /* nothing to do, just move pointer */
228 static int handle_image(struct cxl *adapter, int operation,
229 long (*fct)(u64, u64, u64, u64 *),
230 struct cxl_adapter_image *ai)
232 size_t mod, s_copy, len_chunk = 0;
233 struct ai_header *header = NULL;
234 unsigned int entries = 0, i;
236 int rc = 0, need_header;
238 /* base adapter image header */
239 need_header = (ai->flags & CXL_AI_NEED_HEADER);
241 header = kzalloc(sizeof(struct ai_header), GFP_KERNEL);
244 header->version = cpu_to_be16(1);
245 header->vendor = cpu_to_be16(adapter->guest->vendor);
246 header->device = cpu_to_be16(adapter->guest->device);
247 header->subsystem_vendor = cpu_to_be16(adapter->guest->subsystem_vendor);
248 header->subsystem = cpu_to_be16(adapter->guest->subsystem);
249 header->image_offset = cpu_to_be64(CXL_AI_HEADER_SIZE);
250 header->image_length = cpu_to_be64(ai->len_image);
253 /* number of entries in the list */
254 len_chunk = ai->len_data;
256 len_chunk += CXL_AI_HEADER_SIZE;
258 entries = len_chunk / CXL_AI_BUFFER_SIZE;
259 mod = len_chunk % CXL_AI_BUFFER_SIZE;
263 if (entries > CXL_AI_MAX_ENTRIES) {
268 /* < -- MAX_CHUNK_SIZE = 4096 * 256 = 1048576 bytes -->
269 * chunk 0 ----------------------------------------------------
271 * ----------------------------------------------------
272 * chunk 1 ----------------------------------------------------
274 * ----------------------------------------------------
276 * chunk n ----------------------------------------------------
278 * ----------------------------------------------------
280 from = (void *) ai->data;
281 for (i = 0; i < entries; i++) {
283 s_copy = CXL_AI_BUFFER_SIZE;
285 if ((need_header) && (i == 0)) {
286 /* add adapter image header */
287 memcpy(buffer[i], header, sizeof(struct ai_header));
288 s_copy = CXL_AI_BUFFER_SIZE - CXL_AI_HEADER_SIZE;
289 dest += CXL_AI_HEADER_SIZE; /* image offset */
291 if ((i == (entries - 1)) && mod)
295 if (copy_from_user(dest, from, s_copy))
298 /* fill in the list */
299 le[i].phys_addr = cpu_to_be64(virt_to_phys(buffer[i]));
300 le[i].len = cpu_to_be64(CXL_AI_BUFFER_SIZE);
301 if ((i == (entries - 1)) && mod)
302 le[i].len = cpu_to_be64(mod);
305 pr_devel("%s (op: %i, need header: %i, entries: %i, token: %#llx)\n",
306 __func__, operation, need_header, entries, continue_token);
309 * download/validate the adapter image to the coherent
312 rc = fct(adapter->guest->handle, virt_to_phys(le), entries,
314 if (rc == 0) /* success of download/validation operation */
323 static int transfer_image(struct cxl *adapter, int operation,
324 struct cxl_adapter_image *ai)
331 rc = handle_image(adapter, operation,
332 &cxl_h_download_adapter_image, ai);
334 pr_devel("resetting adapter\n");
335 cxl_h_reset_adapter(adapter->guest->handle);
340 rc = handle_image(adapter, operation,
341 &cxl_h_validate_adapter_image, ai);
343 pr_devel("resetting adapter\n");
344 cxl_h_reset_adapter(adapter->guest->handle);
348 pr_devel("remove current afu\n");
349 for (afu = 0; afu < adapter->slices; afu++)
350 cxl_guest_remove_afu(adapter->afu[afu]);
352 pr_devel("resetting adapter\n");
353 cxl_h_reset_adapter(adapter->guest->handle);
355 /* The entire image has now been
356 * downloaded and the validation has
357 * been successfully performed.
358 * After that, the partition should call
359 * ibm,update-nodes and
360 * ibm,update-properties to receive the
361 * current configuration
363 rc = update_devicetree(adapter, DEVICE_SCOPE);
372 static long ioctl_transfer_image(struct cxl *adapter, int operation,
373 struct cxl_adapter_image __user *uai)
375 struct cxl_adapter_image ai;
377 pr_devel("%s\n", __func__);
379 if (copy_from_user(&ai, uai, sizeof(struct cxl_adapter_image)))
383 * Make sure reserved fields and bits are set to 0
385 if (ai.reserved1 || ai.reserved2 || ai.reserved3 || ai.reserved4 ||
386 (ai.flags & ~CXL_AI_ALL))
389 return transfer_image(adapter, operation, &ai);
392 static int device_open(struct inode *inode, struct file *file)
394 int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
398 pr_devel("in %s\n", __func__);
400 BUG_ON(sizeof(struct ai_header) != CXL_AI_HEADER_SIZE);
402 /* Allows one process to open the device by using a semaphore */
403 if (down_interruptible(&sem) != 0)
406 if (!(adapter = get_cxl_adapter(adapter_num))) {
411 file->private_data = adapter;
415 for (i = 0; i < CXL_AI_MAX_ENTRIES; i++)
418 /* aligned buffer containing list entries which describes up to
419 * 1 megabyte of data (256 entries of 4096 bytes each)
420 * Logical real address of buffer 0 - Buffer 0 length in bytes
421 * Logical real address of buffer 1 - Buffer 1 length in bytes
422 * Logical real address of buffer 2 - Buffer 2 length in bytes
425 * Logical real address of buffer N - Buffer N length in bytes
427 le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
433 for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
434 buffer[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
444 for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
446 free_page((unsigned long) buffer[i]);
450 free_page((unsigned long) le);
452 put_device(&adapter->dev);
459 static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
461 struct cxl *adapter = file->private_data;
463 pr_devel("in %s\n", __func__);
465 if (cmd == CXL_IOCTL_DOWNLOAD_IMAGE)
466 return ioctl_transfer_image(adapter,
468 (struct cxl_adapter_image __user *)arg);
469 else if (cmd == CXL_IOCTL_VALIDATE_IMAGE)
470 return ioctl_transfer_image(adapter,
472 (struct cxl_adapter_image __user *)arg);
477 static int device_close(struct inode *inode, struct file *file)
479 struct cxl *adapter = file->private_data;
482 pr_devel("in %s\n", __func__);
484 for (i = 0; i < CXL_AI_MAX_ENTRIES; i++) {
486 free_page((unsigned long) buffer[i]);
490 free_page((unsigned long) le);
493 put_device(&adapter->dev);
496 /* reload the module */
498 cxl_guest_reload_module(adapter);
500 pr_devel("resetting adapter\n");
501 cxl_h_reset_adapter(adapter->guest->handle);
508 static const struct file_operations fops = {
509 .owner = THIS_MODULE,
511 .unlocked_ioctl = device_ioctl,
512 .compat_ioctl = compat_ptr_ioctl,
513 .release = device_close,
516 void cxl_guest_remove_chardev(struct cxl *adapter)
518 cdev_del(&adapter->guest->cdev);
521 int cxl_guest_add_chardev(struct cxl *adapter)
526 devt = MKDEV(MAJOR(cxl_get_dev()), CXL_CARD_MINOR(adapter));
527 cdev_init(&adapter->guest->cdev, &fops);
528 if ((rc = cdev_add(&adapter->guest->cdev, devt, 1))) {
529 dev_err(&adapter->dev,
530 "Unable to add chardev on adapter (card%i): %i\n",
531 adapter->adapter_num, rc);
534 adapter->dev.devt = devt;