114baf4d9SChristophe Lombard /* 214baf4d9SChristophe Lombard * Copyright 2015 IBM Corp. 314baf4d9SChristophe Lombard * 414baf4d9SChristophe Lombard * This program is free software; you can redistribute it and/or 514baf4d9SChristophe Lombard * modify it under the terms of the GNU General Public License 614baf4d9SChristophe Lombard * as published by the Free Software Foundation; either version 714baf4d9SChristophe Lombard * 2 of the License, or (at your option) any later version. 814baf4d9SChristophe Lombard */ 914baf4d9SChristophe Lombard 1014baf4d9SChristophe Lombard #include <linux/spinlock.h> 1114baf4d9SChristophe Lombard #include <linux/uaccess.h> 1214baf4d9SChristophe Lombard #include <linux/delay.h> 1314baf4d9SChristophe Lombard 1414baf4d9SChristophe Lombard #include "cxl.h" 1514baf4d9SChristophe Lombard #include "hcalls.h" 1614baf4d9SChristophe Lombard #include "trace.h" 1714baf4d9SChristophe Lombard 180d400f77SChristophe Lombard #define CXL_ERROR_DETECTED_EVENT 1 190d400f77SChristophe Lombard #define CXL_SLOT_RESET_EVENT 2 200d400f77SChristophe Lombard #define CXL_RESUME_EVENT 3 210d400f77SChristophe Lombard 220d400f77SChristophe Lombard static void pci_error_handlers(struct cxl_afu *afu, 230d400f77SChristophe Lombard int bus_error_event, 240d400f77SChristophe Lombard pci_channel_state_t state) 250d400f77SChristophe Lombard { 260d400f77SChristophe Lombard struct pci_dev *afu_dev; 270d400f77SChristophe Lombard 280d400f77SChristophe Lombard if (afu->phb == NULL) 290d400f77SChristophe Lombard return; 300d400f77SChristophe Lombard 310d400f77SChristophe Lombard list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) { 320d400f77SChristophe Lombard if (!afu_dev->driver) 330d400f77SChristophe Lombard continue; 340d400f77SChristophe Lombard 350d400f77SChristophe Lombard switch (bus_error_event) { 360d400f77SChristophe Lombard case CXL_ERROR_DETECTED_EVENT: 370d400f77SChristophe Lombard afu_dev->error_state = state; 380d400f77SChristophe Lombard 390d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 400d400f77SChristophe Lombard afu_dev->driver->err_handler->error_detected) 410d400f77SChristophe Lombard afu_dev->driver->err_handler->error_detected(afu_dev, state); 420d400f77SChristophe Lombard break; 430d400f77SChristophe Lombard case CXL_SLOT_RESET_EVENT: 440d400f77SChristophe Lombard afu_dev->error_state = state; 450d400f77SChristophe Lombard 460d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 470d400f77SChristophe Lombard afu_dev->driver->err_handler->slot_reset) 480d400f77SChristophe Lombard afu_dev->driver->err_handler->slot_reset(afu_dev); 490d400f77SChristophe Lombard break; 500d400f77SChristophe Lombard case CXL_RESUME_EVENT: 510d400f77SChristophe Lombard if (afu_dev->driver->err_handler && 520d400f77SChristophe Lombard afu_dev->driver->err_handler->resume) 530d400f77SChristophe Lombard afu_dev->driver->err_handler->resume(afu_dev); 540d400f77SChristophe Lombard break; 550d400f77SChristophe Lombard } 560d400f77SChristophe Lombard } 570d400f77SChristophe Lombard } 5814baf4d9SChristophe Lombard 5914baf4d9SChristophe Lombard static irqreturn_t guest_handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr, 6014baf4d9SChristophe Lombard u64 errstat) 6114baf4d9SChristophe Lombard { 6214baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 6314baf4d9SChristophe Lombard dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat); 6414baf4d9SChristophe Lombard 6514baf4d9SChristophe Lombard return cxl_ops->ack_irq(ctx, 0, errstat); 6614baf4d9SChristophe Lombard } 6714baf4d9SChristophe Lombard 6814baf4d9SChristophe Lombard static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu *afu, 6914baf4d9SChristophe Lombard void *buf, size_t len) 7014baf4d9SChristophe Lombard { 7114baf4d9SChristophe Lombard unsigned int entries, mod; 7214baf4d9SChristophe Lombard unsigned long **vpd_buf = NULL; 7314baf4d9SChristophe Lombard struct sg_list *le; 7414baf4d9SChristophe Lombard int rc = 0, i, tocopy; 7514baf4d9SChristophe Lombard u64 out = 0; 7614baf4d9SChristophe Lombard 7714baf4d9SChristophe Lombard if (buf == NULL) 7814baf4d9SChristophe Lombard return -EINVAL; 7914baf4d9SChristophe Lombard 8014baf4d9SChristophe Lombard /* number of entries in the list */ 8114baf4d9SChristophe Lombard entries = len / SG_BUFFER_SIZE; 8214baf4d9SChristophe Lombard mod = len % SG_BUFFER_SIZE; 8314baf4d9SChristophe Lombard if (mod) 8414baf4d9SChristophe Lombard entries++; 8514baf4d9SChristophe Lombard 8614baf4d9SChristophe Lombard if (entries > SG_MAX_ENTRIES) { 8714baf4d9SChristophe Lombard entries = SG_MAX_ENTRIES; 8814baf4d9SChristophe Lombard len = SG_MAX_ENTRIES * SG_BUFFER_SIZE; 8914baf4d9SChristophe Lombard mod = 0; 9014baf4d9SChristophe Lombard } 9114baf4d9SChristophe Lombard 9214baf4d9SChristophe Lombard vpd_buf = kzalloc(entries * sizeof(unsigned long *), GFP_KERNEL); 9314baf4d9SChristophe Lombard if (!vpd_buf) 9414baf4d9SChristophe Lombard return -ENOMEM; 9514baf4d9SChristophe Lombard 9614baf4d9SChristophe Lombard le = (struct sg_list *)get_zeroed_page(GFP_KERNEL); 9714baf4d9SChristophe Lombard if (!le) { 9814baf4d9SChristophe Lombard rc = -ENOMEM; 9914baf4d9SChristophe Lombard goto err1; 10014baf4d9SChristophe Lombard } 10114baf4d9SChristophe Lombard 10214baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 10314baf4d9SChristophe Lombard vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL); 10414baf4d9SChristophe Lombard if (!vpd_buf[i]) { 10514baf4d9SChristophe Lombard rc = -ENOMEM; 10614baf4d9SChristophe Lombard goto err2; 10714baf4d9SChristophe Lombard } 10814baf4d9SChristophe Lombard le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i])); 10914baf4d9SChristophe Lombard le[i].len = cpu_to_be64(SG_BUFFER_SIZE); 11014baf4d9SChristophe Lombard if ((i == (entries - 1)) && mod) 11114baf4d9SChristophe Lombard le[i].len = cpu_to_be64(mod); 11214baf4d9SChristophe Lombard } 11314baf4d9SChristophe Lombard 11414baf4d9SChristophe Lombard if (adapter) 11514baf4d9SChristophe Lombard rc = cxl_h_collect_vpd_adapter(adapter->guest->handle, 11614baf4d9SChristophe Lombard virt_to_phys(le), entries, &out); 11714baf4d9SChristophe Lombard else 11814baf4d9SChristophe Lombard rc = cxl_h_collect_vpd(afu->guest->handle, 0, 11914baf4d9SChristophe Lombard virt_to_phys(le), entries, &out); 12014baf4d9SChristophe Lombard pr_devel("length of available (entries: %i), vpd: %#llx\n", 12114baf4d9SChristophe Lombard entries, out); 12214baf4d9SChristophe Lombard 12314baf4d9SChristophe Lombard if (!rc) { 12414baf4d9SChristophe Lombard /* 12514baf4d9SChristophe Lombard * hcall returns in 'out' the size of available VPDs. 12614baf4d9SChristophe Lombard * It fills the buffer with as much data as possible. 12714baf4d9SChristophe Lombard */ 12814baf4d9SChristophe Lombard if (out < len) 12914baf4d9SChristophe Lombard len = out; 13014baf4d9SChristophe Lombard rc = len; 13114baf4d9SChristophe Lombard if (out) { 13214baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 13314baf4d9SChristophe Lombard if (len < SG_BUFFER_SIZE) 13414baf4d9SChristophe Lombard tocopy = len; 13514baf4d9SChristophe Lombard else 13614baf4d9SChristophe Lombard tocopy = SG_BUFFER_SIZE; 13714baf4d9SChristophe Lombard memcpy(buf, vpd_buf[i], tocopy); 13814baf4d9SChristophe Lombard buf += tocopy; 13914baf4d9SChristophe Lombard len -= tocopy; 14014baf4d9SChristophe Lombard } 14114baf4d9SChristophe Lombard } 14214baf4d9SChristophe Lombard } 14314baf4d9SChristophe Lombard err2: 14414baf4d9SChristophe Lombard for (i = 0; i < entries; i++) { 14514baf4d9SChristophe Lombard if (vpd_buf[i]) 14614baf4d9SChristophe Lombard free_page((unsigned long) vpd_buf[i]); 14714baf4d9SChristophe Lombard } 14814baf4d9SChristophe Lombard free_page((unsigned long) le); 14914baf4d9SChristophe Lombard err1: 15014baf4d9SChristophe Lombard kfree(vpd_buf); 15114baf4d9SChristophe Lombard return rc; 15214baf4d9SChristophe Lombard } 15314baf4d9SChristophe Lombard 15414baf4d9SChristophe Lombard static int guest_get_irq_info(struct cxl_context *ctx, struct cxl_irq_info *info) 15514baf4d9SChristophe Lombard { 15614baf4d9SChristophe Lombard return cxl_h_collect_int_info(ctx->afu->guest->handle, ctx->process_token, info); 15714baf4d9SChristophe Lombard } 15814baf4d9SChristophe Lombard 15914baf4d9SChristophe Lombard static irqreturn_t guest_psl_irq(int irq, void *data) 16014baf4d9SChristophe Lombard { 16114baf4d9SChristophe Lombard struct cxl_context *ctx = data; 16214baf4d9SChristophe Lombard struct cxl_irq_info irq_info; 16314baf4d9SChristophe Lombard int rc; 16414baf4d9SChristophe Lombard 16514baf4d9SChristophe Lombard pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq); 16614baf4d9SChristophe Lombard rc = guest_get_irq_info(ctx, &irq_info); 16714baf4d9SChristophe Lombard if (rc) { 16814baf4d9SChristophe Lombard WARN(1, "Unable to get IRQ info: %i\n", rc); 16914baf4d9SChristophe Lombard return IRQ_HANDLED; 17014baf4d9SChristophe Lombard } 17114baf4d9SChristophe Lombard 17214baf4d9SChristophe Lombard rc = cxl_irq(irq, ctx, &irq_info); 17314baf4d9SChristophe Lombard return rc; 17414baf4d9SChristophe Lombard } 17514baf4d9SChristophe Lombard 1760d400f77SChristophe Lombard static int afu_read_error_state(struct cxl_afu *afu, int *state_out) 1770d400f77SChristophe Lombard { 1780d400f77SChristophe Lombard u64 state; 1790d400f77SChristophe Lombard int rc = 0; 1800d400f77SChristophe Lombard 1810d400f77SChristophe Lombard rc = cxl_h_read_error_state(afu->guest->handle, &state); 1820d400f77SChristophe Lombard if (!rc) { 1830d400f77SChristophe Lombard WARN_ON(state != H_STATE_NORMAL && 1840d400f77SChristophe Lombard state != H_STATE_DISABLE && 1850d400f77SChristophe Lombard state != H_STATE_TEMP_UNAVAILABLE && 1860d400f77SChristophe Lombard state != H_STATE_PERM_UNAVAILABLE); 1870d400f77SChristophe Lombard *state_out = state & 0xffffffff; 1880d400f77SChristophe Lombard } 1890d400f77SChristophe Lombard return rc; 1900d400f77SChristophe Lombard } 1910d400f77SChristophe Lombard 19214baf4d9SChristophe Lombard static irqreturn_t guest_slice_irq_err(int irq, void *data) 19314baf4d9SChristophe Lombard { 19414baf4d9SChristophe Lombard struct cxl_afu *afu = data; 19514baf4d9SChristophe Lombard int rc; 19614baf4d9SChristophe Lombard u64 serr; 19714baf4d9SChristophe Lombard 19814baf4d9SChristophe Lombard WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq); 19914baf4d9SChristophe Lombard rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr); 20014baf4d9SChristophe Lombard if (rc) { 20114baf4d9SChristophe Lombard dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc); 20214baf4d9SChristophe Lombard return IRQ_HANDLED; 20314baf4d9SChristophe Lombard } 20414baf4d9SChristophe Lombard dev_crit(&afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr); 20514baf4d9SChristophe Lombard 20614baf4d9SChristophe Lombard rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr); 20714baf4d9SChristophe Lombard if (rc) 20814baf4d9SChristophe Lombard dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n", 20914baf4d9SChristophe Lombard rc); 21014baf4d9SChristophe Lombard 21114baf4d9SChristophe Lombard return IRQ_HANDLED; 21214baf4d9SChristophe Lombard } 21314baf4d9SChristophe Lombard 21414baf4d9SChristophe Lombard 21514baf4d9SChristophe Lombard static int irq_alloc_range(struct cxl *adapter, int len, int *irq) 21614baf4d9SChristophe Lombard { 21714baf4d9SChristophe Lombard int i, n; 21814baf4d9SChristophe Lombard struct irq_avail *cur; 21914baf4d9SChristophe Lombard 22014baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 22114baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 22214baf4d9SChristophe Lombard n = bitmap_find_next_zero_area(cur->bitmap, cur->range, 22314baf4d9SChristophe Lombard 0, len, 0); 22414baf4d9SChristophe Lombard if (n < cur->range) { 22514baf4d9SChristophe Lombard bitmap_set(cur->bitmap, n, len); 22614baf4d9SChristophe Lombard *irq = cur->offset + n; 22714baf4d9SChristophe Lombard pr_devel("guest: allocate IRQs %#x->%#x\n", 22814baf4d9SChristophe Lombard *irq, *irq + len - 1); 22914baf4d9SChristophe Lombard 23014baf4d9SChristophe Lombard return 0; 23114baf4d9SChristophe Lombard } 23214baf4d9SChristophe Lombard } 23314baf4d9SChristophe Lombard return -ENOSPC; 23414baf4d9SChristophe Lombard } 23514baf4d9SChristophe Lombard 23614baf4d9SChristophe Lombard static int irq_free_range(struct cxl *adapter, int irq, int len) 23714baf4d9SChristophe Lombard { 23814baf4d9SChristophe Lombard int i, n; 23914baf4d9SChristophe Lombard struct irq_avail *cur; 24014baf4d9SChristophe Lombard 24114baf4d9SChristophe Lombard if (len == 0) 24214baf4d9SChristophe Lombard return -ENOENT; 24314baf4d9SChristophe Lombard 24414baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 24514baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 24614baf4d9SChristophe Lombard if (irq >= cur->offset && 24714baf4d9SChristophe Lombard (irq + len) <= (cur->offset + cur->range)) { 24814baf4d9SChristophe Lombard n = irq - cur->offset; 24914baf4d9SChristophe Lombard bitmap_clear(cur->bitmap, n, len); 25014baf4d9SChristophe Lombard pr_devel("guest: release IRQs %#x->%#x\n", 25114baf4d9SChristophe Lombard irq, irq + len - 1); 25214baf4d9SChristophe Lombard return 0; 25314baf4d9SChristophe Lombard } 25414baf4d9SChristophe Lombard } 25514baf4d9SChristophe Lombard return -ENOENT; 25614baf4d9SChristophe Lombard } 25714baf4d9SChristophe Lombard 25814baf4d9SChristophe Lombard static int guest_reset(struct cxl *adapter) 25914baf4d9SChristophe Lombard { 2600d400f77SChristophe Lombard struct cxl_afu *afu = NULL; 2610d400f77SChristophe Lombard int i, rc; 26214baf4d9SChristophe Lombard 26314baf4d9SChristophe Lombard pr_devel("Adapter reset request\n"); 2640d400f77SChristophe Lombard for (i = 0; i < adapter->slices; i++) { 2650d400f77SChristophe Lombard if ((afu = adapter->afu[i])) { 2660d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 2670d400f77SChristophe Lombard pci_channel_io_frozen); 2680d400f77SChristophe Lombard cxl_context_detach_all(afu); 2690d400f77SChristophe Lombard } 2700d400f77SChristophe Lombard } 2710d400f77SChristophe Lombard 27214baf4d9SChristophe Lombard rc = cxl_h_reset_adapter(adapter->guest->handle); 2730d400f77SChristophe Lombard for (i = 0; i < adapter->slices; i++) { 2740d400f77SChristophe Lombard if (!rc && (afu = adapter->afu[i])) { 2750d400f77SChristophe Lombard pci_error_handlers(afu, CXL_SLOT_RESET_EVENT, 2760d400f77SChristophe Lombard pci_channel_io_normal); 2770d400f77SChristophe Lombard pci_error_handlers(afu, CXL_RESUME_EVENT, 0); 2780d400f77SChristophe Lombard } 2790d400f77SChristophe Lombard } 28014baf4d9SChristophe Lombard return rc; 28114baf4d9SChristophe Lombard } 28214baf4d9SChristophe Lombard 28314baf4d9SChristophe Lombard static int guest_alloc_one_irq(struct cxl *adapter) 28414baf4d9SChristophe Lombard { 28514baf4d9SChristophe Lombard int irq; 28614baf4d9SChristophe Lombard 28714baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 28814baf4d9SChristophe Lombard if (irq_alloc_range(adapter, 1, &irq)) 28914baf4d9SChristophe Lombard irq = -ENOSPC; 29014baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 29114baf4d9SChristophe Lombard return irq; 29214baf4d9SChristophe Lombard } 29314baf4d9SChristophe Lombard 29414baf4d9SChristophe Lombard static void guest_release_one_irq(struct cxl *adapter, int irq) 29514baf4d9SChristophe Lombard { 29614baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 29714baf4d9SChristophe Lombard irq_free_range(adapter, irq, 1); 29814baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 29914baf4d9SChristophe Lombard } 30014baf4d9SChristophe Lombard 30114baf4d9SChristophe Lombard static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs, 30214baf4d9SChristophe Lombard struct cxl *adapter, unsigned int num) 30314baf4d9SChristophe Lombard { 30414baf4d9SChristophe Lombard int i, try, irq; 30514baf4d9SChristophe Lombard 30614baf4d9SChristophe Lombard memset(irqs, 0, sizeof(struct cxl_irq_ranges)); 30714baf4d9SChristophe Lombard 30814baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 30914baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES && num; i++) { 31014baf4d9SChristophe Lombard try = num; 31114baf4d9SChristophe Lombard while (try) { 31214baf4d9SChristophe Lombard if (irq_alloc_range(adapter, try, &irq) == 0) 31314baf4d9SChristophe Lombard break; 31414baf4d9SChristophe Lombard try /= 2; 31514baf4d9SChristophe Lombard } 31614baf4d9SChristophe Lombard if (!try) 31714baf4d9SChristophe Lombard goto error; 31814baf4d9SChristophe Lombard irqs->offset[i] = irq; 31914baf4d9SChristophe Lombard irqs->range[i] = try; 32014baf4d9SChristophe Lombard num -= try; 32114baf4d9SChristophe Lombard } 32214baf4d9SChristophe Lombard if (num) 32314baf4d9SChristophe Lombard goto error; 32414baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 32514baf4d9SChristophe Lombard return 0; 32614baf4d9SChristophe Lombard 32714baf4d9SChristophe Lombard error: 32814baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES; i++) 32914baf4d9SChristophe Lombard irq_free_range(adapter, irqs->offset[i], irqs->range[i]); 33014baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 33114baf4d9SChristophe Lombard return -ENOSPC; 33214baf4d9SChristophe Lombard } 33314baf4d9SChristophe Lombard 33414baf4d9SChristophe Lombard static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs, 33514baf4d9SChristophe Lombard struct cxl *adapter) 33614baf4d9SChristophe Lombard { 33714baf4d9SChristophe Lombard int i; 33814baf4d9SChristophe Lombard 33914baf4d9SChristophe Lombard spin_lock(&adapter->guest->irq_alloc_lock); 34014baf4d9SChristophe Lombard for (i = 0; i < CXL_IRQ_RANGES; i++) 34114baf4d9SChristophe Lombard irq_free_range(adapter, irqs->offset[i], irqs->range[i]); 34214baf4d9SChristophe Lombard spin_unlock(&adapter->guest->irq_alloc_lock); 34314baf4d9SChristophe Lombard } 34414baf4d9SChristophe Lombard 34514baf4d9SChristophe Lombard static int guest_register_serr_irq(struct cxl_afu *afu) 34614baf4d9SChristophe Lombard { 34714baf4d9SChristophe Lombard afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err", 34814baf4d9SChristophe Lombard dev_name(&afu->dev)); 34914baf4d9SChristophe Lombard if (!afu->err_irq_name) 35014baf4d9SChristophe Lombard return -ENOMEM; 35114baf4d9SChristophe Lombard 35214baf4d9SChristophe Lombard if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq, 35314baf4d9SChristophe Lombard guest_slice_irq_err, afu, afu->err_irq_name))) { 35414baf4d9SChristophe Lombard kfree(afu->err_irq_name); 35514baf4d9SChristophe Lombard afu->err_irq_name = NULL; 35614baf4d9SChristophe Lombard return -ENOMEM; 35714baf4d9SChristophe Lombard } 35814baf4d9SChristophe Lombard 35914baf4d9SChristophe Lombard return 0; 36014baf4d9SChristophe Lombard } 36114baf4d9SChristophe Lombard 36214baf4d9SChristophe Lombard static void guest_release_serr_irq(struct cxl_afu *afu) 36314baf4d9SChristophe Lombard { 36414baf4d9SChristophe Lombard cxl_unmap_irq(afu->serr_virq, afu); 36514baf4d9SChristophe Lombard cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq); 36614baf4d9SChristophe Lombard kfree(afu->err_irq_name); 36714baf4d9SChristophe Lombard } 36814baf4d9SChristophe Lombard 36914baf4d9SChristophe Lombard static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask) 37014baf4d9SChristophe Lombard { 37114baf4d9SChristophe Lombard return cxl_h_control_faults(ctx->afu->guest->handle, ctx->process_token, 37214baf4d9SChristophe Lombard tfc >> 32, (psl_reset_mask != 0)); 37314baf4d9SChristophe Lombard } 37414baf4d9SChristophe Lombard 37514baf4d9SChristophe Lombard static void disable_afu_irqs(struct cxl_context *ctx) 37614baf4d9SChristophe Lombard { 37714baf4d9SChristophe Lombard irq_hw_number_t hwirq; 37814baf4d9SChristophe Lombard unsigned int virq; 37914baf4d9SChristophe Lombard int r, i; 38014baf4d9SChristophe Lombard 38114baf4d9SChristophe Lombard pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice); 38214baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 38314baf4d9SChristophe Lombard hwirq = ctx->irqs.offset[r]; 38414baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 38514baf4d9SChristophe Lombard virq = irq_find_mapping(NULL, hwirq); 38614baf4d9SChristophe Lombard disable_irq(virq); 38714baf4d9SChristophe Lombard } 38814baf4d9SChristophe Lombard } 38914baf4d9SChristophe Lombard } 39014baf4d9SChristophe Lombard 39114baf4d9SChristophe Lombard static void enable_afu_irqs(struct cxl_context *ctx) 39214baf4d9SChristophe Lombard { 39314baf4d9SChristophe Lombard irq_hw_number_t hwirq; 39414baf4d9SChristophe Lombard unsigned int virq; 39514baf4d9SChristophe Lombard int r, i; 39614baf4d9SChristophe Lombard 39714baf4d9SChristophe Lombard pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice); 39814baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 39914baf4d9SChristophe Lombard hwirq = ctx->irqs.offset[r]; 40014baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) { 40114baf4d9SChristophe Lombard virq = irq_find_mapping(NULL, hwirq); 40214baf4d9SChristophe Lombard enable_irq(virq); 40314baf4d9SChristophe Lombard } 40414baf4d9SChristophe Lombard } 40514baf4d9SChristophe Lombard } 40614baf4d9SChristophe Lombard 40714baf4d9SChristophe Lombard static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx, 40814baf4d9SChristophe Lombard u64 offset, u64 *val) 40914baf4d9SChristophe Lombard { 41014baf4d9SChristophe Lombard unsigned long cr; 41114baf4d9SChristophe Lombard char c; 41214baf4d9SChristophe Lombard int rc = 0; 41314baf4d9SChristophe Lombard 41414baf4d9SChristophe Lombard if (afu->crs_len < sz) 41514baf4d9SChristophe Lombard return -ENOENT; 41614baf4d9SChristophe Lombard 41714baf4d9SChristophe Lombard if (unlikely(offset >= afu->crs_len)) 41814baf4d9SChristophe Lombard return -ERANGE; 41914baf4d9SChristophe Lombard 42014baf4d9SChristophe Lombard cr = get_zeroed_page(GFP_KERNEL); 42114baf4d9SChristophe Lombard if (!cr) 42214baf4d9SChristophe Lombard return -ENOMEM; 42314baf4d9SChristophe Lombard 42414baf4d9SChristophe Lombard rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset, 42514baf4d9SChristophe Lombard virt_to_phys((void *)cr), sz); 42614baf4d9SChristophe Lombard if (rc) 42714baf4d9SChristophe Lombard goto err; 42814baf4d9SChristophe Lombard 42914baf4d9SChristophe Lombard switch (sz) { 43014baf4d9SChristophe Lombard case 1: 43114baf4d9SChristophe Lombard c = *((char *) cr); 43214baf4d9SChristophe Lombard *val = c; 43314baf4d9SChristophe Lombard break; 43414baf4d9SChristophe Lombard case 2: 43514baf4d9SChristophe Lombard *val = in_le16((u16 *)cr); 43614baf4d9SChristophe Lombard break; 43714baf4d9SChristophe Lombard case 4: 43814baf4d9SChristophe Lombard *val = in_le32((unsigned *)cr); 43914baf4d9SChristophe Lombard break; 44014baf4d9SChristophe Lombard case 8: 44114baf4d9SChristophe Lombard *val = in_le64((u64 *)cr); 44214baf4d9SChristophe Lombard break; 44314baf4d9SChristophe Lombard default: 44414baf4d9SChristophe Lombard WARN_ON(1); 44514baf4d9SChristophe Lombard } 44614baf4d9SChristophe Lombard err: 44714baf4d9SChristophe Lombard free_page(cr); 44814baf4d9SChristophe Lombard return rc; 44914baf4d9SChristophe Lombard } 45014baf4d9SChristophe Lombard 45114baf4d9SChristophe Lombard static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx, u64 offset, 45214baf4d9SChristophe Lombard u32 *out) 45314baf4d9SChristophe Lombard { 45414baf4d9SChristophe Lombard int rc; 45514baf4d9SChristophe Lombard u64 val; 45614baf4d9SChristophe Lombard 45714baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val); 45814baf4d9SChristophe Lombard if (!rc) 45914baf4d9SChristophe Lombard *out = (u32) val; 46014baf4d9SChristophe Lombard return rc; 46114baf4d9SChristophe Lombard } 46214baf4d9SChristophe Lombard 46314baf4d9SChristophe Lombard static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx, u64 offset, 46414baf4d9SChristophe Lombard u16 *out) 46514baf4d9SChristophe Lombard { 46614baf4d9SChristophe Lombard int rc; 46714baf4d9SChristophe Lombard u64 val; 46814baf4d9SChristophe Lombard 46914baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val); 47014baf4d9SChristophe Lombard if (!rc) 47114baf4d9SChristophe Lombard *out = (u16) val; 47214baf4d9SChristophe Lombard return rc; 47314baf4d9SChristophe Lombard } 47414baf4d9SChristophe Lombard 47514baf4d9SChristophe Lombard static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset, 47614baf4d9SChristophe Lombard u8 *out) 47714baf4d9SChristophe Lombard { 47814baf4d9SChristophe Lombard int rc; 47914baf4d9SChristophe Lombard u64 val; 48014baf4d9SChristophe Lombard 48114baf4d9SChristophe Lombard rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val); 48214baf4d9SChristophe Lombard if (!rc) 48314baf4d9SChristophe Lombard *out = (u8) val; 48414baf4d9SChristophe Lombard return rc; 48514baf4d9SChristophe Lombard } 48614baf4d9SChristophe Lombard 48714baf4d9SChristophe Lombard static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx, u64 offset, 48814baf4d9SChristophe Lombard u64 *out) 48914baf4d9SChristophe Lombard { 49014baf4d9SChristophe Lombard return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out); 49114baf4d9SChristophe Lombard } 49214baf4d9SChristophe Lombard 493d601ea91SFrederic Barrat static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in) 494d601ea91SFrederic Barrat { 495d601ea91SFrederic Barrat /* config record is not writable from guest */ 496d601ea91SFrederic Barrat return -EPERM; 497d601ea91SFrederic Barrat } 498d601ea91SFrederic Barrat 499d601ea91SFrederic Barrat static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in) 500d601ea91SFrederic Barrat { 501d601ea91SFrederic Barrat /* config record is not writable from guest */ 502d601ea91SFrederic Barrat return -EPERM; 503d601ea91SFrederic Barrat } 504d601ea91SFrederic Barrat 505d601ea91SFrederic Barrat static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in) 506d601ea91SFrederic Barrat { 507d601ea91SFrederic Barrat /* config record is not writable from guest */ 508d601ea91SFrederic Barrat return -EPERM; 509d601ea91SFrederic Barrat } 510d601ea91SFrederic Barrat 51114baf4d9SChristophe Lombard static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr) 51214baf4d9SChristophe Lombard { 51314baf4d9SChristophe Lombard struct cxl_process_element_hcall *elem; 51414baf4d9SChristophe Lombard struct cxl *adapter = ctx->afu->adapter; 51514baf4d9SChristophe Lombard const struct cred *cred; 51614baf4d9SChristophe Lombard u32 pid, idx; 51714baf4d9SChristophe Lombard int rc, r, i; 51814baf4d9SChristophe Lombard u64 mmio_addr, mmio_size; 51914baf4d9SChristophe Lombard __be64 flags = 0; 52014baf4d9SChristophe Lombard 52114baf4d9SChristophe Lombard /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */ 52214baf4d9SChristophe Lombard if (!(elem = (struct cxl_process_element_hcall *) 52314baf4d9SChristophe Lombard get_zeroed_page(GFP_KERNEL))) 52414baf4d9SChristophe Lombard return -ENOMEM; 52514baf4d9SChristophe Lombard 52614baf4d9SChristophe Lombard elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION); 52714baf4d9SChristophe Lombard if (ctx->kernel) { 52814baf4d9SChristophe Lombard pid = 0; 52914baf4d9SChristophe Lombard flags |= CXL_PE_TRANSLATION_ENABLED; 53014baf4d9SChristophe Lombard flags |= CXL_PE_PRIVILEGED_PROCESS; 53114baf4d9SChristophe Lombard if (mfmsr() & MSR_SF) 53214baf4d9SChristophe Lombard flags |= CXL_PE_64_BIT; 53314baf4d9SChristophe Lombard } else { 53414baf4d9SChristophe Lombard pid = current->pid; 53514baf4d9SChristophe Lombard flags |= CXL_PE_PROBLEM_STATE; 53614baf4d9SChristophe Lombard flags |= CXL_PE_TRANSLATION_ENABLED; 53714baf4d9SChristophe Lombard if (!test_tsk_thread_flag(current, TIF_32BIT)) 53814baf4d9SChristophe Lombard flags |= CXL_PE_64_BIT; 53914baf4d9SChristophe Lombard cred = get_current_cred(); 54014baf4d9SChristophe Lombard if (uid_eq(cred->euid, GLOBAL_ROOT_UID)) 54114baf4d9SChristophe Lombard flags |= CXL_PE_PRIVILEGED_PROCESS; 54214baf4d9SChristophe Lombard put_cred(cred); 54314baf4d9SChristophe Lombard } 54414baf4d9SChristophe Lombard elem->flags = cpu_to_be64(flags); 54514baf4d9SChristophe Lombard elem->common.tid = cpu_to_be32(0); /* Unused */ 54614baf4d9SChristophe Lombard elem->common.pid = cpu_to_be32(pid); 54714baf4d9SChristophe Lombard elem->common.csrp = cpu_to_be64(0); /* disable */ 54814baf4d9SChristophe Lombard elem->common.aurp0 = cpu_to_be64(0); /* disable */ 54914baf4d9SChristophe Lombard elem->common.aurp1 = cpu_to_be64(0); /* disable */ 55014baf4d9SChristophe Lombard 55114baf4d9SChristophe Lombard cxl_prefault(ctx, wed); 55214baf4d9SChristophe Lombard 55314baf4d9SChristophe Lombard elem->common.sstp0 = cpu_to_be64(ctx->sstp0); 55414baf4d9SChristophe Lombard elem->common.sstp1 = cpu_to_be64(ctx->sstp1); 5553c206fa7SIan Munsie 5563c206fa7SIan Munsie /* 5573c206fa7SIan Munsie * Ensure we have at least one interrupt allocated to take faults for 5583c206fa7SIan Munsie * kernel contexts that may not have allocated any AFU IRQs at all: 5593c206fa7SIan Munsie */ 5603c206fa7SIan Munsie if (ctx->irqs.range[0] == 0) { 5613c206fa7SIan Munsie rc = afu_register_irqs(ctx, 0); 5623c206fa7SIan Munsie if (rc) 5633c206fa7SIan Munsie goto out_free; 5643c206fa7SIan Munsie } 5653c206fa7SIan Munsie 56614baf4d9SChristophe Lombard for (r = 0; r < CXL_IRQ_RANGES; r++) { 56714baf4d9SChristophe Lombard for (i = 0; i < ctx->irqs.range[r]; i++) { 56814baf4d9SChristophe Lombard if (r == 0 && i == 0) { 56914baf4d9SChristophe Lombard elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]); 57014baf4d9SChristophe Lombard } else { 57114baf4d9SChristophe Lombard idx = ctx->irqs.offset[r] + i - adapter->guest->irq_base_offset; 57214baf4d9SChristophe Lombard elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >> (idx % 8); 57314baf4d9SChristophe Lombard } 57414baf4d9SChristophe Lombard } 57514baf4d9SChristophe Lombard } 57614baf4d9SChristophe Lombard elem->common.amr = cpu_to_be64(amr); 57714baf4d9SChristophe Lombard elem->common.wed = cpu_to_be64(wed); 57814baf4d9SChristophe Lombard 57914baf4d9SChristophe Lombard disable_afu_irqs(ctx); 58014baf4d9SChristophe Lombard 58114baf4d9SChristophe Lombard rc = cxl_h_attach_process(ctx->afu->guest->handle, elem, 58214baf4d9SChristophe Lombard &ctx->process_token, &mmio_addr, &mmio_size); 58314baf4d9SChristophe Lombard if (rc == H_SUCCESS) { 58414baf4d9SChristophe Lombard if (ctx->master || !ctx->afu->pp_psa) { 58514baf4d9SChristophe Lombard ctx->psn_phys = ctx->afu->psn_phys; 58614baf4d9SChristophe Lombard ctx->psn_size = ctx->afu->adapter->ps_size; 58714baf4d9SChristophe Lombard } else { 58814baf4d9SChristophe Lombard ctx->psn_phys = mmio_addr; 58914baf4d9SChristophe Lombard ctx->psn_size = mmio_size; 59014baf4d9SChristophe Lombard } 59114baf4d9SChristophe Lombard if (ctx->afu->pp_psa && mmio_size && 59214baf4d9SChristophe Lombard ctx->afu->pp_size == 0) { 59314baf4d9SChristophe Lombard /* 59414baf4d9SChristophe Lombard * There's no property in the device tree to read the 59514baf4d9SChristophe Lombard * pp_size. We only find out at the 1st attach. 59614baf4d9SChristophe Lombard * Compared to bare-metal, it is too late and we 59714baf4d9SChristophe Lombard * should really lock here. However, on powerVM, 59814baf4d9SChristophe Lombard * pp_size is really only used to display in /sys. 59914baf4d9SChristophe Lombard * Being discussed with pHyp for their next release. 60014baf4d9SChristophe Lombard */ 60114baf4d9SChristophe Lombard ctx->afu->pp_size = mmio_size; 60214baf4d9SChristophe Lombard } 60314baf4d9SChristophe Lombard /* from PAPR: process element is bytes 4-7 of process token */ 60414baf4d9SChristophe Lombard ctx->external_pe = ctx->process_token & 0xFFFFFFFF; 60514baf4d9SChristophe Lombard pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx", 60614baf4d9SChristophe Lombard ctx->pe, ctx->external_pe, ctx->psn_size); 60714baf4d9SChristophe Lombard ctx->pe_inserted = true; 60814baf4d9SChristophe Lombard enable_afu_irqs(ctx); 60914baf4d9SChristophe Lombard } 61014baf4d9SChristophe Lombard 6113c206fa7SIan Munsie out_free: 61214baf4d9SChristophe Lombard free_page((u64)elem); 61314baf4d9SChristophe Lombard return rc; 61414baf4d9SChristophe Lombard } 61514baf4d9SChristophe Lombard 61614baf4d9SChristophe Lombard static int guest_attach_process(struct cxl_context *ctx, bool kernel, u64 wed, u64 amr) 61714baf4d9SChristophe Lombard { 61814baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 61914baf4d9SChristophe Lombard 620*7a0d85d3SIan Munsie if (ctx->real_mode) 621*7a0d85d3SIan Munsie return -EPERM; 622*7a0d85d3SIan Munsie 62314baf4d9SChristophe Lombard ctx->kernel = kernel; 62414baf4d9SChristophe Lombard if (ctx->afu->current_mode == CXL_MODE_DIRECTED) 62514baf4d9SChristophe Lombard return attach_afu_directed(ctx, wed, amr); 62614baf4d9SChristophe Lombard 62714baf4d9SChristophe Lombard /* dedicated mode not supported on FW840 */ 62814baf4d9SChristophe Lombard 62914baf4d9SChristophe Lombard return -EINVAL; 63014baf4d9SChristophe Lombard } 63114baf4d9SChristophe Lombard 63214baf4d9SChristophe Lombard static int detach_afu_directed(struct cxl_context *ctx) 63314baf4d9SChristophe Lombard { 63414baf4d9SChristophe Lombard if (!ctx->pe_inserted) 63514baf4d9SChristophe Lombard return 0; 63614baf4d9SChristophe Lombard if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token)) 63714baf4d9SChristophe Lombard return -1; 63814baf4d9SChristophe Lombard return 0; 63914baf4d9SChristophe Lombard } 64014baf4d9SChristophe Lombard 64114baf4d9SChristophe Lombard static int guest_detach_process(struct cxl_context *ctx) 64214baf4d9SChristophe Lombard { 64314baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 64414baf4d9SChristophe Lombard trace_cxl_detach(ctx); 64514baf4d9SChristophe Lombard 6460d400f77SChristophe Lombard if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) 64714baf4d9SChristophe Lombard return -EIO; 64814baf4d9SChristophe Lombard 64914baf4d9SChristophe Lombard if (ctx->afu->current_mode == CXL_MODE_DIRECTED) 65014baf4d9SChristophe Lombard return detach_afu_directed(ctx); 65114baf4d9SChristophe Lombard 65214baf4d9SChristophe Lombard return -EINVAL; 65314baf4d9SChristophe Lombard } 65414baf4d9SChristophe Lombard 65514baf4d9SChristophe Lombard static void guest_release_afu(struct device *dev) 65614baf4d9SChristophe Lombard { 65714baf4d9SChristophe Lombard struct cxl_afu *afu = to_cxl_afu(dev); 65814baf4d9SChristophe Lombard 65914baf4d9SChristophe Lombard pr_devel("%s\n", __func__); 66014baf4d9SChristophe Lombard 66114baf4d9SChristophe Lombard idr_destroy(&afu->contexts_idr); 66214baf4d9SChristophe Lombard 66314baf4d9SChristophe Lombard kfree(afu->guest); 66414baf4d9SChristophe Lombard kfree(afu); 66514baf4d9SChristophe Lombard } 66614baf4d9SChristophe Lombard 66714baf4d9SChristophe Lombard ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len) 66814baf4d9SChristophe Lombard { 66914baf4d9SChristophe Lombard return guest_collect_vpd(NULL, afu, buf, len); 67014baf4d9SChristophe Lombard } 67114baf4d9SChristophe Lombard 67214baf4d9SChristophe Lombard #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE 67314baf4d9SChristophe Lombard static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf, 67414baf4d9SChristophe Lombard loff_t off, size_t count) 67514baf4d9SChristophe Lombard { 67614baf4d9SChristophe Lombard void *tbuf = NULL; 67714baf4d9SChristophe Lombard int rc = 0; 67814baf4d9SChristophe Lombard 67914baf4d9SChristophe Lombard tbuf = (void *) get_zeroed_page(GFP_KERNEL); 68014baf4d9SChristophe Lombard if (!tbuf) 68114baf4d9SChristophe Lombard return -ENOMEM; 68214baf4d9SChristophe Lombard 68314baf4d9SChristophe Lombard rc = cxl_h_get_afu_err(afu->guest->handle, 68414baf4d9SChristophe Lombard off & 0x7, 68514baf4d9SChristophe Lombard virt_to_phys(tbuf), 68614baf4d9SChristophe Lombard count); 68714baf4d9SChristophe Lombard if (rc) 68814baf4d9SChristophe Lombard goto err; 68914baf4d9SChristophe Lombard 69014baf4d9SChristophe Lombard if (count > ERR_BUFF_MAX_COPY_SIZE) 69114baf4d9SChristophe Lombard count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7); 69214baf4d9SChristophe Lombard memcpy(buf, tbuf, count); 69314baf4d9SChristophe Lombard err: 69414baf4d9SChristophe Lombard free_page((u64)tbuf); 69514baf4d9SChristophe Lombard 69614baf4d9SChristophe Lombard return rc; 69714baf4d9SChristophe Lombard } 69814baf4d9SChristophe Lombard 69914baf4d9SChristophe Lombard static int guest_afu_check_and_enable(struct cxl_afu *afu) 70014baf4d9SChristophe Lombard { 70114baf4d9SChristophe Lombard return 0; 70214baf4d9SChristophe Lombard } 70314baf4d9SChristophe Lombard 7044752876cSChristophe Lombard static bool guest_support_attributes(const char *attr_name, 7054752876cSChristophe Lombard enum cxl_attrs type) 7064752876cSChristophe Lombard { 7074752876cSChristophe Lombard switch (type) { 7084752876cSChristophe Lombard case CXL_ADAPTER_ATTRS: 7094752876cSChristophe Lombard if ((strcmp(attr_name, "base_image") == 0) || 7104752876cSChristophe Lombard (strcmp(attr_name, "load_image_on_perst") == 0) || 7114752876cSChristophe Lombard (strcmp(attr_name, "perst_reloads_same_image") == 0) || 7124752876cSChristophe Lombard (strcmp(attr_name, "image_loaded") == 0)) 7134752876cSChristophe Lombard return false; 7144752876cSChristophe Lombard break; 7154752876cSChristophe Lombard case CXL_AFU_MASTER_ATTRS: 7164752876cSChristophe Lombard if ((strcmp(attr_name, "pp_mmio_off") == 0)) 7174752876cSChristophe Lombard return false; 7184752876cSChristophe Lombard break; 7194752876cSChristophe Lombard case CXL_AFU_ATTRS: 7204752876cSChristophe Lombard break; 7214752876cSChristophe Lombard default: 7224752876cSChristophe Lombard break; 7234752876cSChristophe Lombard } 7244752876cSChristophe Lombard 7254752876cSChristophe Lombard return true; 7264752876cSChristophe Lombard } 7274752876cSChristophe Lombard 72814baf4d9SChristophe Lombard static int activate_afu_directed(struct cxl_afu *afu) 72914baf4d9SChristophe Lombard { 73014baf4d9SChristophe Lombard int rc; 73114baf4d9SChristophe Lombard 73214baf4d9SChristophe Lombard dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice); 73314baf4d9SChristophe Lombard 73414baf4d9SChristophe Lombard afu->current_mode = CXL_MODE_DIRECTED; 73514baf4d9SChristophe Lombard 73614baf4d9SChristophe Lombard afu->num_procs = afu->max_procs_virtualised; 73714baf4d9SChristophe Lombard 73814baf4d9SChristophe Lombard if ((rc = cxl_chardev_m_afu_add(afu))) 73914baf4d9SChristophe Lombard return rc; 74014baf4d9SChristophe Lombard 74114baf4d9SChristophe Lombard if ((rc = cxl_sysfs_afu_m_add(afu))) 74214baf4d9SChristophe Lombard goto err; 74314baf4d9SChristophe Lombard 74414baf4d9SChristophe Lombard if ((rc = cxl_chardev_s_afu_add(afu))) 74514baf4d9SChristophe Lombard goto err1; 74614baf4d9SChristophe Lombard 74714baf4d9SChristophe Lombard return 0; 74814baf4d9SChristophe Lombard err1: 74914baf4d9SChristophe Lombard cxl_sysfs_afu_m_remove(afu); 75014baf4d9SChristophe Lombard err: 75114baf4d9SChristophe Lombard cxl_chardev_afu_remove(afu); 75214baf4d9SChristophe Lombard return rc; 75314baf4d9SChristophe Lombard } 75414baf4d9SChristophe Lombard 75514baf4d9SChristophe Lombard static int guest_afu_activate_mode(struct cxl_afu *afu, int mode) 75614baf4d9SChristophe Lombard { 75714baf4d9SChristophe Lombard if (!mode) 75814baf4d9SChristophe Lombard return 0; 75914baf4d9SChristophe Lombard if (!(mode & afu->modes_supported)) 76014baf4d9SChristophe Lombard return -EINVAL; 76114baf4d9SChristophe Lombard 76214baf4d9SChristophe Lombard if (mode == CXL_MODE_DIRECTED) 76314baf4d9SChristophe Lombard return activate_afu_directed(afu); 76414baf4d9SChristophe Lombard 76514baf4d9SChristophe Lombard if (mode == CXL_MODE_DEDICATED) 76614baf4d9SChristophe Lombard dev_err(&afu->dev, "Dedicated mode not supported\n"); 76714baf4d9SChristophe Lombard 76814baf4d9SChristophe Lombard return -EINVAL; 76914baf4d9SChristophe Lombard } 77014baf4d9SChristophe Lombard 77114baf4d9SChristophe Lombard static int deactivate_afu_directed(struct cxl_afu *afu) 77214baf4d9SChristophe Lombard { 77314baf4d9SChristophe Lombard dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n", afu->slice); 77414baf4d9SChristophe Lombard 77514baf4d9SChristophe Lombard afu->current_mode = 0; 77614baf4d9SChristophe Lombard afu->num_procs = 0; 77714baf4d9SChristophe Lombard 77814baf4d9SChristophe Lombard cxl_sysfs_afu_m_remove(afu); 77914baf4d9SChristophe Lombard cxl_chardev_afu_remove(afu); 78014baf4d9SChristophe Lombard 78114baf4d9SChristophe Lombard cxl_ops->afu_reset(afu); 78214baf4d9SChristophe Lombard 78314baf4d9SChristophe Lombard return 0; 78414baf4d9SChristophe Lombard } 78514baf4d9SChristophe Lombard 78614baf4d9SChristophe Lombard static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode) 78714baf4d9SChristophe Lombard { 78814baf4d9SChristophe Lombard if (!mode) 78914baf4d9SChristophe Lombard return 0; 79014baf4d9SChristophe Lombard if (!(mode & afu->modes_supported)) 79114baf4d9SChristophe Lombard return -EINVAL; 79214baf4d9SChristophe Lombard 79314baf4d9SChristophe Lombard if (mode == CXL_MODE_DIRECTED) 79414baf4d9SChristophe Lombard return deactivate_afu_directed(afu); 79514baf4d9SChristophe Lombard return 0; 79614baf4d9SChristophe Lombard } 79714baf4d9SChristophe Lombard 79814baf4d9SChristophe Lombard static int guest_afu_reset(struct cxl_afu *afu) 79914baf4d9SChristophe Lombard { 80014baf4d9SChristophe Lombard pr_devel("AFU(%d) reset request\n", afu->slice); 80114baf4d9SChristophe Lombard return cxl_h_reset_afu(afu->guest->handle); 80214baf4d9SChristophe Lombard } 80314baf4d9SChristophe Lombard 80414baf4d9SChristophe Lombard static int guest_map_slice_regs(struct cxl_afu *afu) 80514baf4d9SChristophe Lombard { 80614baf4d9SChristophe Lombard if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys, afu->guest->p2n_size))) { 80714baf4d9SChristophe Lombard dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n", 80814baf4d9SChristophe Lombard afu->slice); 80914baf4d9SChristophe Lombard return -ENOMEM; 81014baf4d9SChristophe Lombard } 81114baf4d9SChristophe Lombard return 0; 81214baf4d9SChristophe Lombard } 81314baf4d9SChristophe Lombard 81414baf4d9SChristophe Lombard static void guest_unmap_slice_regs(struct cxl_afu *afu) 81514baf4d9SChristophe Lombard { 81614baf4d9SChristophe Lombard if (afu->p2n_mmio) 81714baf4d9SChristophe Lombard iounmap(afu->p2n_mmio); 81814baf4d9SChristophe Lombard } 81914baf4d9SChristophe Lombard 8200d400f77SChristophe Lombard static int afu_update_state(struct cxl_afu *afu) 82114baf4d9SChristophe Lombard { 8220d400f77SChristophe Lombard int rc, cur_state; 8230d400f77SChristophe Lombard 8240d400f77SChristophe Lombard rc = afu_read_error_state(afu, &cur_state); 8250d400f77SChristophe Lombard if (rc) 8260d400f77SChristophe Lombard return rc; 8270d400f77SChristophe Lombard 8280d400f77SChristophe Lombard if (afu->guest->previous_state == cur_state) 8290d400f77SChristophe Lombard return 0; 8300d400f77SChristophe Lombard 8310d400f77SChristophe Lombard pr_devel("AFU(%d) update state to %#x\n", afu->slice, cur_state); 8320d400f77SChristophe Lombard 8330d400f77SChristophe Lombard switch (cur_state) { 8340d400f77SChristophe Lombard case H_STATE_NORMAL: 8350d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8360d400f77SChristophe Lombard rc = 1; 8370d400f77SChristophe Lombard break; 8380d400f77SChristophe Lombard 8390d400f77SChristophe Lombard case H_STATE_DISABLE: 8400d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 8410d400f77SChristophe Lombard pci_channel_io_frozen); 8420d400f77SChristophe Lombard 8430d400f77SChristophe Lombard cxl_context_detach_all(afu); 8440d400f77SChristophe Lombard if ((rc = cxl_ops->afu_reset(afu))) 8450d400f77SChristophe Lombard pr_devel("reset hcall failed %d\n", rc); 8460d400f77SChristophe Lombard 8470d400f77SChristophe Lombard rc = afu_read_error_state(afu, &cur_state); 8480d400f77SChristophe Lombard if (!rc && cur_state == H_STATE_NORMAL) { 8490d400f77SChristophe Lombard pci_error_handlers(afu, CXL_SLOT_RESET_EVENT, 8500d400f77SChristophe Lombard pci_channel_io_normal); 8510d400f77SChristophe Lombard pci_error_handlers(afu, CXL_RESUME_EVENT, 0); 8520d400f77SChristophe Lombard rc = 1; 8530d400f77SChristophe Lombard } 8540d400f77SChristophe Lombard afu->guest->previous_state = 0; 8550d400f77SChristophe Lombard break; 8560d400f77SChristophe Lombard 8570d400f77SChristophe Lombard case H_STATE_TEMP_UNAVAILABLE: 8580d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8590d400f77SChristophe Lombard break; 8600d400f77SChristophe Lombard 8610d400f77SChristophe Lombard case H_STATE_PERM_UNAVAILABLE: 8620d400f77SChristophe Lombard dev_err(&afu->dev, "AFU is in permanent error state\n"); 8630d400f77SChristophe Lombard pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT, 8640d400f77SChristophe Lombard pci_channel_io_perm_failure); 8650d400f77SChristophe Lombard afu->guest->previous_state = cur_state; 8660d400f77SChristophe Lombard break; 8670d400f77SChristophe Lombard 8680d400f77SChristophe Lombard default: 8690d400f77SChristophe Lombard pr_err("Unexpected AFU(%d) error state: %#x\n", 8700d400f77SChristophe Lombard afu->slice, cur_state); 8710d400f77SChristophe Lombard return -EINVAL; 8720d400f77SChristophe Lombard } 8730d400f77SChristophe Lombard 8740d400f77SChristophe Lombard return rc; 8750d400f77SChristophe Lombard } 8760d400f77SChristophe Lombard 8770d400f77SChristophe Lombard static int afu_do_recovery(struct cxl_afu *afu) 8780d400f77SChristophe Lombard { 8790d400f77SChristophe Lombard int rc; 8800d400f77SChristophe Lombard 8810d400f77SChristophe Lombard /* many threads can arrive here, in case of detach_all for example. 8820d400f77SChristophe Lombard * Only one needs to drive the recovery 8830d400f77SChristophe Lombard */ 8840d400f77SChristophe Lombard if (mutex_trylock(&afu->guest->recovery_lock)) { 8850d400f77SChristophe Lombard rc = afu_update_state(afu); 8860d400f77SChristophe Lombard mutex_unlock(&afu->guest->recovery_lock); 8870d400f77SChristophe Lombard return rc; 8880d400f77SChristophe Lombard } 8890d400f77SChristophe Lombard return 0; 8900d400f77SChristophe Lombard } 8910d400f77SChristophe Lombard 8920d400f77SChristophe Lombard static bool guest_link_ok(struct cxl *cxl, struct cxl_afu *afu) 8930d400f77SChristophe Lombard { 8940d400f77SChristophe Lombard int state; 8950d400f77SChristophe Lombard 8960d400f77SChristophe Lombard if (afu) { 8970d400f77SChristophe Lombard if (afu_read_error_state(afu, &state) || 8980d400f77SChristophe Lombard state != H_STATE_NORMAL) { 8990d400f77SChristophe Lombard if (afu_do_recovery(afu) > 0) { 9000d400f77SChristophe Lombard /* check again in case we've just fixed it */ 9010d400f77SChristophe Lombard if (!afu_read_error_state(afu, &state) && 9020d400f77SChristophe Lombard state == H_STATE_NORMAL) 9030d400f77SChristophe Lombard return true; 9040d400f77SChristophe Lombard } 9050d400f77SChristophe Lombard return false; 9060d400f77SChristophe Lombard } 9070d400f77SChristophe Lombard } 9080d400f77SChristophe Lombard 90914baf4d9SChristophe Lombard return true; 91014baf4d9SChristophe Lombard } 91114baf4d9SChristophe Lombard 91214baf4d9SChristophe Lombard static int afu_properties_look_ok(struct cxl_afu *afu) 91314baf4d9SChristophe Lombard { 91414baf4d9SChristophe Lombard if (afu->pp_irqs < 0) { 91514baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected per-process minimum interrupt value\n"); 91614baf4d9SChristophe Lombard return -EINVAL; 91714baf4d9SChristophe Lombard } 91814baf4d9SChristophe Lombard 91914baf4d9SChristophe Lombard if (afu->max_procs_virtualised < 1) { 92014baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected max number of processes virtualised value\n"); 92114baf4d9SChristophe Lombard return -EINVAL; 92214baf4d9SChristophe Lombard } 92314baf4d9SChristophe Lombard 92414baf4d9SChristophe Lombard if (afu->crs_len < 0) { 92514baf4d9SChristophe Lombard dev_err(&afu->dev, "Unexpected configuration record size value\n"); 92614baf4d9SChristophe Lombard return -EINVAL; 92714baf4d9SChristophe Lombard } 92814baf4d9SChristophe Lombard 92914baf4d9SChristophe Lombard return 0; 93014baf4d9SChristophe Lombard } 93114baf4d9SChristophe Lombard 93214baf4d9SChristophe Lombard int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np) 93314baf4d9SChristophe Lombard { 93414baf4d9SChristophe Lombard struct cxl_afu *afu; 93514baf4d9SChristophe Lombard bool free = true; 93614baf4d9SChristophe Lombard int rc; 93714baf4d9SChristophe Lombard 93814baf4d9SChristophe Lombard pr_devel("in %s - AFU(%d)\n", __func__, slice); 93914baf4d9SChristophe Lombard if (!(afu = cxl_alloc_afu(adapter, slice))) 94014baf4d9SChristophe Lombard return -ENOMEM; 94114baf4d9SChristophe Lombard 94214baf4d9SChristophe Lombard if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest), GFP_KERNEL))) { 94314baf4d9SChristophe Lombard kfree(afu); 94414baf4d9SChristophe Lombard return -ENOMEM; 94514baf4d9SChristophe Lombard } 94614baf4d9SChristophe Lombard 9470d400f77SChristophe Lombard mutex_init(&afu->guest->recovery_lock); 9480d400f77SChristophe Lombard 94914baf4d9SChristophe Lombard if ((rc = dev_set_name(&afu->dev, "afu%i.%i", 95014baf4d9SChristophe Lombard adapter->adapter_num, 95114baf4d9SChristophe Lombard slice))) 95214baf4d9SChristophe Lombard goto err1; 95314baf4d9SChristophe Lombard 95414baf4d9SChristophe Lombard adapter->slices++; 95514baf4d9SChristophe Lombard 95614baf4d9SChristophe Lombard if ((rc = cxl_of_read_afu_handle(afu, afu_np))) 95714baf4d9SChristophe Lombard goto err1; 95814baf4d9SChristophe Lombard 95914baf4d9SChristophe Lombard if ((rc = cxl_ops->afu_reset(afu))) 96014baf4d9SChristophe Lombard goto err1; 96114baf4d9SChristophe Lombard 96214baf4d9SChristophe Lombard if ((rc = cxl_of_read_afu_properties(afu, afu_np))) 96314baf4d9SChristophe Lombard goto err1; 96414baf4d9SChristophe Lombard 96514baf4d9SChristophe Lombard if ((rc = afu_properties_look_ok(afu))) 96614baf4d9SChristophe Lombard goto err1; 96714baf4d9SChristophe Lombard 96814baf4d9SChristophe Lombard if ((rc = guest_map_slice_regs(afu))) 96914baf4d9SChristophe Lombard goto err1; 97014baf4d9SChristophe Lombard 97114baf4d9SChristophe Lombard if ((rc = guest_register_serr_irq(afu))) 97214baf4d9SChristophe Lombard goto err2; 97314baf4d9SChristophe Lombard 97414baf4d9SChristophe Lombard /* 97514baf4d9SChristophe Lombard * After we call this function we must not free the afu directly, even 97614baf4d9SChristophe Lombard * if it returns an error! 97714baf4d9SChristophe Lombard */ 97814baf4d9SChristophe Lombard if ((rc = cxl_register_afu(afu))) 97914baf4d9SChristophe Lombard goto err_put1; 98014baf4d9SChristophe Lombard 98114baf4d9SChristophe Lombard if ((rc = cxl_sysfs_afu_add(afu))) 98214baf4d9SChristophe Lombard goto err_put1; 98314baf4d9SChristophe Lombard 98414baf4d9SChristophe Lombard /* 98514baf4d9SChristophe Lombard * pHyp doesn't expose the programming models supported by the 98614baf4d9SChristophe Lombard * AFU. pHyp currently only supports directed mode. If it adds 98714baf4d9SChristophe Lombard * dedicated mode later, this version of cxl has no way to 98814baf4d9SChristophe Lombard * detect it. So we'll initialize the driver, but the first 98914baf4d9SChristophe Lombard * attach will fail. 99014baf4d9SChristophe Lombard * Being discussed with pHyp to do better (likely new property) 99114baf4d9SChristophe Lombard */ 99214baf4d9SChristophe Lombard if (afu->max_procs_virtualised == 1) 99314baf4d9SChristophe Lombard afu->modes_supported = CXL_MODE_DEDICATED; 99414baf4d9SChristophe Lombard else 99514baf4d9SChristophe Lombard afu->modes_supported = CXL_MODE_DIRECTED; 99614baf4d9SChristophe Lombard 99714baf4d9SChristophe Lombard if ((rc = cxl_afu_select_best_mode(afu))) 99814baf4d9SChristophe Lombard goto err_put2; 99914baf4d9SChristophe Lombard 100014baf4d9SChristophe Lombard adapter->afu[afu->slice] = afu; 100114baf4d9SChristophe Lombard 100214baf4d9SChristophe Lombard afu->enabled = true; 100314baf4d9SChristophe Lombard 1004d601ea91SFrederic Barrat if ((rc = cxl_pci_vphb_add(afu))) 1005d601ea91SFrederic Barrat dev_info(&afu->dev, "Can't register vPHB\n"); 1006d601ea91SFrederic Barrat 100714baf4d9SChristophe Lombard return 0; 100814baf4d9SChristophe Lombard 100914baf4d9SChristophe Lombard err_put2: 101014baf4d9SChristophe Lombard cxl_sysfs_afu_remove(afu); 101114baf4d9SChristophe Lombard err_put1: 101214baf4d9SChristophe Lombard device_unregister(&afu->dev); 101314baf4d9SChristophe Lombard free = false; 101414baf4d9SChristophe Lombard guest_release_serr_irq(afu); 101514baf4d9SChristophe Lombard err2: 101614baf4d9SChristophe Lombard guest_unmap_slice_regs(afu); 101714baf4d9SChristophe Lombard err1: 101814baf4d9SChristophe Lombard if (free) { 101914baf4d9SChristophe Lombard kfree(afu->guest); 102014baf4d9SChristophe Lombard kfree(afu); 102114baf4d9SChristophe Lombard } 102214baf4d9SChristophe Lombard return rc; 102314baf4d9SChristophe Lombard } 102414baf4d9SChristophe Lombard 102514baf4d9SChristophe Lombard void cxl_guest_remove_afu(struct cxl_afu *afu) 102614baf4d9SChristophe Lombard { 102714baf4d9SChristophe Lombard pr_devel("in %s - AFU(%d)\n", __func__, afu->slice); 102814baf4d9SChristophe Lombard 102914baf4d9SChristophe Lombard if (!afu) 103014baf4d9SChristophe Lombard return; 103114baf4d9SChristophe Lombard 1032d601ea91SFrederic Barrat cxl_pci_vphb_remove(afu); 103314baf4d9SChristophe Lombard cxl_sysfs_afu_remove(afu); 103414baf4d9SChristophe Lombard 103514baf4d9SChristophe Lombard spin_lock(&afu->adapter->afu_list_lock); 103614baf4d9SChristophe Lombard afu->adapter->afu[afu->slice] = NULL; 103714baf4d9SChristophe Lombard spin_unlock(&afu->adapter->afu_list_lock); 103814baf4d9SChristophe Lombard 103914baf4d9SChristophe Lombard cxl_context_detach_all(afu); 104014baf4d9SChristophe Lombard cxl_ops->afu_deactivate_mode(afu, afu->current_mode); 104114baf4d9SChristophe Lombard guest_release_serr_irq(afu); 104214baf4d9SChristophe Lombard guest_unmap_slice_regs(afu); 104314baf4d9SChristophe Lombard 104414baf4d9SChristophe Lombard device_unregister(&afu->dev); 104514baf4d9SChristophe Lombard } 104614baf4d9SChristophe Lombard 104714baf4d9SChristophe Lombard static void free_adapter(struct cxl *adapter) 104814baf4d9SChristophe Lombard { 104914baf4d9SChristophe Lombard struct irq_avail *cur; 105014baf4d9SChristophe Lombard int i; 105114baf4d9SChristophe Lombard 105214baf4d9SChristophe Lombard if (adapter->guest->irq_avail) { 105314baf4d9SChristophe Lombard for (i = 0; i < adapter->guest->irq_nranges; i++) { 105414baf4d9SChristophe Lombard cur = &adapter->guest->irq_avail[i]; 105514baf4d9SChristophe Lombard kfree(cur->bitmap); 105614baf4d9SChristophe Lombard } 105714baf4d9SChristophe Lombard kfree(adapter->guest->irq_avail); 105814baf4d9SChristophe Lombard } 105914baf4d9SChristophe Lombard kfree(adapter->guest->status); 106014baf4d9SChristophe Lombard cxl_remove_adapter_nr(adapter); 106114baf4d9SChristophe Lombard kfree(adapter->guest); 106214baf4d9SChristophe Lombard kfree(adapter); 106314baf4d9SChristophe Lombard } 106414baf4d9SChristophe Lombard 106514baf4d9SChristophe Lombard static int properties_look_ok(struct cxl *adapter) 106614baf4d9SChristophe Lombard { 106714baf4d9SChristophe Lombard /* The absence of this property means that the operational 106814baf4d9SChristophe Lombard * status is unknown or okay 106914baf4d9SChristophe Lombard */ 107014baf4d9SChristophe Lombard if (strlen(adapter->guest->status) && 107114baf4d9SChristophe Lombard strcmp(adapter->guest->status, "okay")) { 107214baf4d9SChristophe Lombard pr_err("ABORTING:Bad operational status of the device\n"); 107314baf4d9SChristophe Lombard return -EINVAL; 107414baf4d9SChristophe Lombard } 107514baf4d9SChristophe Lombard 107614baf4d9SChristophe Lombard return 0; 107714baf4d9SChristophe Lombard } 107814baf4d9SChristophe Lombard 107914baf4d9SChristophe Lombard ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len) 108014baf4d9SChristophe Lombard { 108114baf4d9SChristophe Lombard return guest_collect_vpd(adapter, NULL, buf, len); 108214baf4d9SChristophe Lombard } 108314baf4d9SChristophe Lombard 108414baf4d9SChristophe Lombard void cxl_guest_remove_adapter(struct cxl *adapter) 108514baf4d9SChristophe Lombard { 108614baf4d9SChristophe Lombard pr_devel("in %s\n", __func__); 108714baf4d9SChristophe Lombard 108814baf4d9SChristophe Lombard cxl_sysfs_adapter_remove(adapter); 108914baf4d9SChristophe Lombard 1090594ff7d0SChristophe Lombard cxl_guest_remove_chardev(adapter); 109114baf4d9SChristophe Lombard device_unregister(&adapter->dev); 109214baf4d9SChristophe Lombard } 109314baf4d9SChristophe Lombard 109414baf4d9SChristophe Lombard static void release_adapter(struct device *dev) 109514baf4d9SChristophe Lombard { 109614baf4d9SChristophe Lombard free_adapter(to_cxl_adapter(dev)); 109714baf4d9SChristophe Lombard } 109814baf4d9SChristophe Lombard 109914baf4d9SChristophe Lombard struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *pdev) 110014baf4d9SChristophe Lombard { 110114baf4d9SChristophe Lombard struct cxl *adapter; 110214baf4d9SChristophe Lombard bool free = true; 110314baf4d9SChristophe Lombard int rc; 110414baf4d9SChristophe Lombard 110514baf4d9SChristophe Lombard if (!(adapter = cxl_alloc_adapter())) 110614baf4d9SChristophe Lombard return ERR_PTR(-ENOMEM); 110714baf4d9SChristophe Lombard 110814baf4d9SChristophe Lombard if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest), GFP_KERNEL))) { 110914baf4d9SChristophe Lombard free_adapter(adapter); 111014baf4d9SChristophe Lombard return ERR_PTR(-ENOMEM); 111114baf4d9SChristophe Lombard } 111214baf4d9SChristophe Lombard 111314baf4d9SChristophe Lombard adapter->slices = 0; 111414baf4d9SChristophe Lombard adapter->guest->pdev = pdev; 111514baf4d9SChristophe Lombard adapter->dev.parent = &pdev->dev; 111614baf4d9SChristophe Lombard adapter->dev.release = release_adapter; 111714baf4d9SChristophe Lombard dev_set_drvdata(&pdev->dev, adapter); 111814baf4d9SChristophe Lombard 1119e009a7e8SFrederic Barrat /* 1120e009a7e8SFrederic Barrat * Hypervisor controls PSL timebase initialization (p1 register). 1121e009a7e8SFrederic Barrat * On FW840, PSL is initialized. 1122e009a7e8SFrederic Barrat */ 1123e009a7e8SFrederic Barrat adapter->psl_timebase_synced = true; 1124e009a7e8SFrederic Barrat 112514baf4d9SChristophe Lombard if ((rc = cxl_of_read_adapter_handle(adapter, np))) 112614baf4d9SChristophe Lombard goto err1; 112714baf4d9SChristophe Lombard 112814baf4d9SChristophe Lombard if ((rc = cxl_of_read_adapter_properties(adapter, np))) 112914baf4d9SChristophe Lombard goto err1; 113014baf4d9SChristophe Lombard 113114baf4d9SChristophe Lombard if ((rc = properties_look_ok(adapter))) 113214baf4d9SChristophe Lombard goto err1; 113314baf4d9SChristophe Lombard 1134594ff7d0SChristophe Lombard if ((rc = cxl_guest_add_chardev(adapter))) 1135594ff7d0SChristophe Lombard goto err1; 1136594ff7d0SChristophe Lombard 113714baf4d9SChristophe Lombard /* 113814baf4d9SChristophe Lombard * After we call this function we must not free the adapter directly, 113914baf4d9SChristophe Lombard * even if it returns an error! 114014baf4d9SChristophe Lombard */ 114114baf4d9SChristophe Lombard if ((rc = cxl_register_adapter(adapter))) 114214baf4d9SChristophe Lombard goto err_put1; 114314baf4d9SChristophe Lombard 114414baf4d9SChristophe Lombard if ((rc = cxl_sysfs_adapter_add(adapter))) 114514baf4d9SChristophe Lombard goto err_put1; 114614baf4d9SChristophe Lombard 114714baf4d9SChristophe Lombard return adapter; 114814baf4d9SChristophe Lombard 114914baf4d9SChristophe Lombard err_put1: 115014baf4d9SChristophe Lombard device_unregister(&adapter->dev); 115114baf4d9SChristophe Lombard free = false; 1152594ff7d0SChristophe Lombard cxl_guest_remove_chardev(adapter); 115314baf4d9SChristophe Lombard err1: 115414baf4d9SChristophe Lombard if (free) 115514baf4d9SChristophe Lombard free_adapter(adapter); 115614baf4d9SChristophe Lombard return ERR_PTR(rc); 115714baf4d9SChristophe Lombard } 115814baf4d9SChristophe Lombard 1159594ff7d0SChristophe Lombard void cxl_guest_reload_module(struct cxl *adapter) 1160594ff7d0SChristophe Lombard { 1161594ff7d0SChristophe Lombard struct platform_device *pdev; 1162594ff7d0SChristophe Lombard 1163594ff7d0SChristophe Lombard pdev = adapter->guest->pdev; 1164594ff7d0SChristophe Lombard cxl_guest_remove_adapter(adapter); 1165594ff7d0SChristophe Lombard 1166594ff7d0SChristophe Lombard cxl_of_probe(pdev); 1167594ff7d0SChristophe Lombard } 1168594ff7d0SChristophe Lombard 116914baf4d9SChristophe Lombard const struct cxl_backend_ops cxl_guest_ops = { 117014baf4d9SChristophe Lombard .module = THIS_MODULE, 117114baf4d9SChristophe Lombard .adapter_reset = guest_reset, 117214baf4d9SChristophe Lombard .alloc_one_irq = guest_alloc_one_irq, 117314baf4d9SChristophe Lombard .release_one_irq = guest_release_one_irq, 117414baf4d9SChristophe Lombard .alloc_irq_ranges = guest_alloc_irq_ranges, 117514baf4d9SChristophe Lombard .release_irq_ranges = guest_release_irq_ranges, 117614baf4d9SChristophe Lombard .setup_irq = NULL, 117714baf4d9SChristophe Lombard .handle_psl_slice_error = guest_handle_psl_slice_error, 117814baf4d9SChristophe Lombard .psl_interrupt = guest_psl_irq, 117914baf4d9SChristophe Lombard .ack_irq = guest_ack_irq, 118014baf4d9SChristophe Lombard .attach_process = guest_attach_process, 118114baf4d9SChristophe Lombard .detach_process = guest_detach_process, 11824752876cSChristophe Lombard .support_attributes = guest_support_attributes, 118314baf4d9SChristophe Lombard .link_ok = guest_link_ok, 118414baf4d9SChristophe Lombard .release_afu = guest_release_afu, 118514baf4d9SChristophe Lombard .afu_read_err_buffer = guest_afu_read_err_buffer, 118614baf4d9SChristophe Lombard .afu_check_and_enable = guest_afu_check_and_enable, 118714baf4d9SChristophe Lombard .afu_activate_mode = guest_afu_activate_mode, 118814baf4d9SChristophe Lombard .afu_deactivate_mode = guest_afu_deactivate_mode, 118914baf4d9SChristophe Lombard .afu_reset = guest_afu_reset, 119014baf4d9SChristophe Lombard .afu_cr_read8 = guest_afu_cr_read8, 119114baf4d9SChristophe Lombard .afu_cr_read16 = guest_afu_cr_read16, 119214baf4d9SChristophe Lombard .afu_cr_read32 = guest_afu_cr_read32, 119314baf4d9SChristophe Lombard .afu_cr_read64 = guest_afu_cr_read64, 1194d601ea91SFrederic Barrat .afu_cr_write8 = guest_afu_cr_write8, 1195d601ea91SFrederic Barrat .afu_cr_write16 = guest_afu_cr_write16, 1196d601ea91SFrederic Barrat .afu_cr_write32 = guest_afu_cr_write32, 1197d601ea91SFrederic Barrat .read_adapter_vpd = cxl_guest_read_adapter_vpd, 119814baf4d9SChristophe Lombard }; 1199