xref: /OK3568_Linux_fs/kernel/drivers/scsi/cxlflash/ocxl_hw.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * CXL Flash Device Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Written by: Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
6*4882a593Smuzhiyun  *             Uma Krishnan <ukrishn@linux.vnet.ibm.com>, IBM Corporation
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2018 IBM Corporation
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/file.h>
12*4882a593Smuzhiyun #include <linux/idr.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/mount.h>
15*4882a593Smuzhiyun #include <linux/pseudo_fs.h>
16*4882a593Smuzhiyun #include <linux/poll.h>
17*4882a593Smuzhiyun #include <linux/sched/signal.h>
18*4882a593Smuzhiyun #include <linux/interrupt.h>
19*4882a593Smuzhiyun #include <asm/xive.h>
20*4882a593Smuzhiyun #include <misc/ocxl.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <uapi/misc/cxl.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include "backend.h"
25*4882a593Smuzhiyun #include "ocxl_hw.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * Pseudo-filesystem to allocate inodes.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define OCXLFLASH_FS_MAGIC      0x1697698f
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static int ocxlflash_fs_cnt;
34*4882a593Smuzhiyun static struct vfsmount *ocxlflash_vfs_mount;
35*4882a593Smuzhiyun 
ocxlflash_fs_init_fs_context(struct fs_context * fc)36*4882a593Smuzhiyun static int ocxlflash_fs_init_fs_context(struct fs_context *fc)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	return init_pseudo(fc, OCXLFLASH_FS_MAGIC) ? 0 : -ENOMEM;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static struct file_system_type ocxlflash_fs_type = {
42*4882a593Smuzhiyun 	.name		= "ocxlflash",
43*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
44*4882a593Smuzhiyun 	.init_fs_context = ocxlflash_fs_init_fs_context,
45*4882a593Smuzhiyun 	.kill_sb	= kill_anon_super,
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * ocxlflash_release_mapping() - release the memory mapping
50*4882a593Smuzhiyun  * @ctx:	Context whose mapping is to be released.
51*4882a593Smuzhiyun  */
ocxlflash_release_mapping(struct ocxlflash_context * ctx)52*4882a593Smuzhiyun static void ocxlflash_release_mapping(struct ocxlflash_context *ctx)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	if (ctx->mapping)
55*4882a593Smuzhiyun 		simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
56*4882a593Smuzhiyun 	ctx->mapping = NULL;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * ocxlflash_getfile() - allocate pseudo filesystem, inode, and the file
61*4882a593Smuzhiyun  * @dev:	Generic device of the host.
62*4882a593Smuzhiyun  * @name:	Name of the pseudo filesystem.
63*4882a593Smuzhiyun  * @fops:	File operations.
64*4882a593Smuzhiyun  * @priv:	Private data.
65*4882a593Smuzhiyun  * @flags:	Flags for the file.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * Return: pointer to the file on success, ERR_PTR on failure
68*4882a593Smuzhiyun  */
ocxlflash_getfile(struct device * dev,const char * name,const struct file_operations * fops,void * priv,int flags)69*4882a593Smuzhiyun static struct file *ocxlflash_getfile(struct device *dev, const char *name,
70*4882a593Smuzhiyun 				      const struct file_operations *fops,
71*4882a593Smuzhiyun 				      void *priv, int flags)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	struct file *file;
74*4882a593Smuzhiyun 	struct inode *inode;
75*4882a593Smuzhiyun 	int rc;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (fops->owner && !try_module_get(fops->owner)) {
78*4882a593Smuzhiyun 		dev_err(dev, "%s: Owner does not exist\n", __func__);
79*4882a593Smuzhiyun 		rc = -ENOENT;
80*4882a593Smuzhiyun 		goto err1;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	rc = simple_pin_fs(&ocxlflash_fs_type, &ocxlflash_vfs_mount,
84*4882a593Smuzhiyun 			   &ocxlflash_fs_cnt);
85*4882a593Smuzhiyun 	if (unlikely(rc < 0)) {
86*4882a593Smuzhiyun 		dev_err(dev, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
87*4882a593Smuzhiyun 			__func__, rc);
88*4882a593Smuzhiyun 		goto err2;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	inode = alloc_anon_inode(ocxlflash_vfs_mount->mnt_sb);
92*4882a593Smuzhiyun 	if (IS_ERR(inode)) {
93*4882a593Smuzhiyun 		rc = PTR_ERR(inode);
94*4882a593Smuzhiyun 		dev_err(dev, "%s: alloc_anon_inode failed rc=%d\n",
95*4882a593Smuzhiyun 			__func__, rc);
96*4882a593Smuzhiyun 		goto err3;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	file = alloc_file_pseudo(inode, ocxlflash_vfs_mount, name,
100*4882a593Smuzhiyun 				 flags & (O_ACCMODE | O_NONBLOCK), fops);
101*4882a593Smuzhiyun 	if (IS_ERR(file)) {
102*4882a593Smuzhiyun 		rc = PTR_ERR(file);
103*4882a593Smuzhiyun 		dev_err(dev, "%s: alloc_file failed rc=%d\n",
104*4882a593Smuzhiyun 			__func__, rc);
105*4882a593Smuzhiyun 		goto err4;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	file->private_data = priv;
109*4882a593Smuzhiyun out:
110*4882a593Smuzhiyun 	return file;
111*4882a593Smuzhiyun err4:
112*4882a593Smuzhiyun 	iput(inode);
113*4882a593Smuzhiyun err3:
114*4882a593Smuzhiyun 	simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
115*4882a593Smuzhiyun err2:
116*4882a593Smuzhiyun 	module_put(fops->owner);
117*4882a593Smuzhiyun err1:
118*4882a593Smuzhiyun 	file = ERR_PTR(rc);
119*4882a593Smuzhiyun 	goto out;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun  * ocxlflash_psa_map() - map the process specific MMIO space
124*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context for which the mapping needs to be done.
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * Return: MMIO pointer of the mapped region
127*4882a593Smuzhiyun  */
ocxlflash_psa_map(void * ctx_cookie)128*4882a593Smuzhiyun static void __iomem *ocxlflash_psa_map(void *ctx_cookie)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
131*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	mutex_lock(&ctx->state_mutex);
134*4882a593Smuzhiyun 	if (ctx->state != STARTED) {
135*4882a593Smuzhiyun 		dev_err(dev, "%s: Context not started, state=%d\n", __func__,
136*4882a593Smuzhiyun 			ctx->state);
137*4882a593Smuzhiyun 		mutex_unlock(&ctx->state_mutex);
138*4882a593Smuzhiyun 		return NULL;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 	mutex_unlock(&ctx->state_mutex);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return ioremap(ctx->psn_phys, ctx->psn_size);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun  * ocxlflash_psa_unmap() - unmap the process specific MMIO space
147*4882a593Smuzhiyun  * @addr:	MMIO pointer to unmap.
148*4882a593Smuzhiyun  */
ocxlflash_psa_unmap(void __iomem * addr)149*4882a593Smuzhiyun static void ocxlflash_psa_unmap(void __iomem *addr)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	iounmap(addr);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun  * ocxlflash_process_element() - get process element of the adapter context
156*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context associated with the process element.
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * Return: process element of the adapter context
159*4882a593Smuzhiyun  */
ocxlflash_process_element(void * ctx_cookie)160*4882a593Smuzhiyun static int ocxlflash_process_element(void *ctx_cookie)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return ctx->pe;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * afu_map_irq() - map the interrupt of the adapter context
169*4882a593Smuzhiyun  * @flags:	Flags.
170*4882a593Smuzhiyun  * @ctx:	Adapter context.
171*4882a593Smuzhiyun  * @num:	Per-context AFU interrupt number.
172*4882a593Smuzhiyun  * @handler:	Interrupt handler to register.
173*4882a593Smuzhiyun  * @cookie:	Interrupt handler private data.
174*4882a593Smuzhiyun  * @name:	Name of the interrupt.
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
177*4882a593Smuzhiyun  */
afu_map_irq(u64 flags,struct ocxlflash_context * ctx,int num,irq_handler_t handler,void * cookie,char * name)178*4882a593Smuzhiyun static int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
179*4882a593Smuzhiyun 		       irq_handler_t handler, void *cookie, char *name)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
182*4882a593Smuzhiyun 	struct device *dev = afu->dev;
183*4882a593Smuzhiyun 	struct ocxlflash_irqs *irq;
184*4882a593Smuzhiyun 	struct xive_irq_data *xd;
185*4882a593Smuzhiyun 	u32 virq;
186*4882a593Smuzhiyun 	int rc = 0;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (num < 0 || num >= ctx->num_irqs) {
189*4882a593Smuzhiyun 		dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
190*4882a593Smuzhiyun 		rc = -ENOENT;
191*4882a593Smuzhiyun 		goto out;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	irq = &ctx->irqs[num];
195*4882a593Smuzhiyun 	virq = irq_create_mapping(NULL, irq->hwirq);
196*4882a593Smuzhiyun 	if (unlikely(!virq)) {
197*4882a593Smuzhiyun 		dev_err(dev, "%s: irq_create_mapping failed\n", __func__);
198*4882a593Smuzhiyun 		rc = -ENOMEM;
199*4882a593Smuzhiyun 		goto out;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	rc = request_irq(virq, handler, 0, name, cookie);
203*4882a593Smuzhiyun 	if (unlikely(rc)) {
204*4882a593Smuzhiyun 		dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
205*4882a593Smuzhiyun 		goto err1;
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	xd = irq_get_handler_data(virq);
209*4882a593Smuzhiyun 	if (unlikely(!xd)) {
210*4882a593Smuzhiyun 		dev_err(dev, "%s: Can't get interrupt data\n", __func__);
211*4882a593Smuzhiyun 		rc = -ENXIO;
212*4882a593Smuzhiyun 		goto err2;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	irq->virq = virq;
216*4882a593Smuzhiyun 	irq->vtrig = xd->trig_mmio;
217*4882a593Smuzhiyun out:
218*4882a593Smuzhiyun 	return rc;
219*4882a593Smuzhiyun err2:
220*4882a593Smuzhiyun 	free_irq(virq, cookie);
221*4882a593Smuzhiyun err1:
222*4882a593Smuzhiyun 	irq_dispose_mapping(virq);
223*4882a593Smuzhiyun 	goto out;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /**
227*4882a593Smuzhiyun  * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
228*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context.
229*4882a593Smuzhiyun  * @num:	Per-context AFU interrupt number.
230*4882a593Smuzhiyun  * @handler:	Interrupt handler to register.
231*4882a593Smuzhiyun  * @cookie:	Interrupt handler private data.
232*4882a593Smuzhiyun  * @name:	Name of the interrupt.
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
235*4882a593Smuzhiyun  */
ocxlflash_map_afu_irq(void * ctx_cookie,int num,irq_handler_t handler,void * cookie,char * name)236*4882a593Smuzhiyun static int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
237*4882a593Smuzhiyun 				 irq_handler_t handler, void *cookie,
238*4882a593Smuzhiyun 				 char *name)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun  * afu_unmap_irq() - unmap the interrupt
245*4882a593Smuzhiyun  * @flags:	Flags.
246*4882a593Smuzhiyun  * @ctx:	Adapter context.
247*4882a593Smuzhiyun  * @num:	Per-context AFU interrupt number.
248*4882a593Smuzhiyun  * @cookie:	Interrupt handler private data.
249*4882a593Smuzhiyun  */
afu_unmap_irq(u64 flags,struct ocxlflash_context * ctx,int num,void * cookie)250*4882a593Smuzhiyun static void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
251*4882a593Smuzhiyun 			  void *cookie)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
254*4882a593Smuzhiyun 	struct device *dev = afu->dev;
255*4882a593Smuzhiyun 	struct ocxlflash_irqs *irq;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	if (num < 0 || num >= ctx->num_irqs) {
258*4882a593Smuzhiyun 		dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
259*4882a593Smuzhiyun 		return;
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	irq = &ctx->irqs[num];
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (irq_find_mapping(NULL, irq->hwirq)) {
265*4882a593Smuzhiyun 		free_irq(irq->virq, cookie);
266*4882a593Smuzhiyun 		irq_dispose_mapping(irq->virq);
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	memset(irq, 0, sizeof(*irq));
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun  * ocxlflash_unmap_afu_irq() - unmap the interrupt
274*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context.
275*4882a593Smuzhiyun  * @num:	Per-context AFU interrupt number.
276*4882a593Smuzhiyun  * @cookie:	Interrupt handler private data.
277*4882a593Smuzhiyun  */
ocxlflash_unmap_afu_irq(void * ctx_cookie,int num,void * cookie)278*4882a593Smuzhiyun static void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	return afu_unmap_irq(0, ctx_cookie, num, cookie);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /**
284*4882a593Smuzhiyun  * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
285*4882a593Smuzhiyun  * @ctx_cookie:	Context associated with the interrupt.
286*4882a593Smuzhiyun  * @irq:	Interrupt number.
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * Return: effective address of the mapped region
289*4882a593Smuzhiyun  */
ocxlflash_get_irq_objhndl(void * ctx_cookie,int irq)290*4882a593Smuzhiyun static u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	if (irq < 0 || irq >= ctx->num_irqs)
295*4882a593Smuzhiyun 		return 0;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	return (__force u64)ctx->irqs[irq].vtrig;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun  * ocxlflash_xsl_fault() - callback when translation error is triggered
302*4882a593Smuzhiyun  * @data:	Private data provided at callback registration, the context.
303*4882a593Smuzhiyun  * @addr:	Address that triggered the error.
304*4882a593Smuzhiyun  * @dsisr:	Value of dsisr register.
305*4882a593Smuzhiyun  */
ocxlflash_xsl_fault(void * data,u64 addr,u64 dsisr)306*4882a593Smuzhiyun static void ocxlflash_xsl_fault(void *data, u64 addr, u64 dsisr)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = data;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	spin_lock(&ctx->slock);
311*4882a593Smuzhiyun 	ctx->fault_addr = addr;
312*4882a593Smuzhiyun 	ctx->fault_dsisr = dsisr;
313*4882a593Smuzhiyun 	ctx->pending_fault = true;
314*4882a593Smuzhiyun 	spin_unlock(&ctx->slock);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	wake_up_all(&ctx->wq);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun /**
320*4882a593Smuzhiyun  * start_context() - local routine to start a context
321*4882a593Smuzhiyun  * @ctx:	Adapter context to be started.
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  * Assign the context specific MMIO space, add and enable the PE.
324*4882a593Smuzhiyun  *
325*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
326*4882a593Smuzhiyun  */
start_context(struct ocxlflash_context * ctx)327*4882a593Smuzhiyun static int start_context(struct ocxlflash_context *ctx)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
330*4882a593Smuzhiyun 	struct ocxl_afu_config *acfg = &afu->acfg;
331*4882a593Smuzhiyun 	void *link_token = afu->link_token;
332*4882a593Smuzhiyun 	struct device *dev = afu->dev;
333*4882a593Smuzhiyun 	bool master = ctx->master;
334*4882a593Smuzhiyun 	struct mm_struct *mm;
335*4882a593Smuzhiyun 	int rc = 0;
336*4882a593Smuzhiyun 	u32 pid;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	mutex_lock(&ctx->state_mutex);
339*4882a593Smuzhiyun 	if (ctx->state != OPENED) {
340*4882a593Smuzhiyun 		dev_err(dev, "%s: Context state invalid, state=%d\n",
341*4882a593Smuzhiyun 			__func__, ctx->state);
342*4882a593Smuzhiyun 		rc = -EINVAL;
343*4882a593Smuzhiyun 		goto out;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	if (master) {
347*4882a593Smuzhiyun 		ctx->psn_size = acfg->global_mmio_size;
348*4882a593Smuzhiyun 		ctx->psn_phys = afu->gmmio_phys;
349*4882a593Smuzhiyun 	} else {
350*4882a593Smuzhiyun 		ctx->psn_size = acfg->pp_mmio_stride;
351*4882a593Smuzhiyun 		ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* pid and mm not set for master contexts */
355*4882a593Smuzhiyun 	if (master) {
356*4882a593Smuzhiyun 		pid = 0;
357*4882a593Smuzhiyun 		mm = NULL;
358*4882a593Smuzhiyun 	} else {
359*4882a593Smuzhiyun 		pid = current->mm->context.id;
360*4882a593Smuzhiyun 		mm = current->mm;
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0, mm,
364*4882a593Smuzhiyun 			      ocxlflash_xsl_fault, ctx);
365*4882a593Smuzhiyun 	if (unlikely(rc)) {
366*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
367*4882a593Smuzhiyun 			__func__, rc);
368*4882a593Smuzhiyun 		goto out;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	ctx->state = STARTED;
372*4882a593Smuzhiyun out:
373*4882a593Smuzhiyun 	mutex_unlock(&ctx->state_mutex);
374*4882a593Smuzhiyun 	return rc;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun /**
378*4882a593Smuzhiyun  * ocxlflash_start_context() - start a kernel context
379*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context to be started.
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
382*4882a593Smuzhiyun  */
ocxlflash_start_context(void * ctx_cookie)383*4882a593Smuzhiyun static int ocxlflash_start_context(void *ctx_cookie)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	return start_context(ctx);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun /**
391*4882a593Smuzhiyun  * ocxlflash_stop_context() - stop a context
392*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context to be stopped.
393*4882a593Smuzhiyun  *
394*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
395*4882a593Smuzhiyun  */
ocxlflash_stop_context(void * ctx_cookie)396*4882a593Smuzhiyun static int ocxlflash_stop_context(void *ctx_cookie)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
399*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
400*4882a593Smuzhiyun 	struct ocxl_afu_config *acfg = &afu->acfg;
401*4882a593Smuzhiyun 	struct pci_dev *pdev = afu->pdev;
402*4882a593Smuzhiyun 	struct device *dev = afu->dev;
403*4882a593Smuzhiyun 	enum ocxlflash_ctx_state state;
404*4882a593Smuzhiyun 	int rc = 0;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	mutex_lock(&ctx->state_mutex);
407*4882a593Smuzhiyun 	state = ctx->state;
408*4882a593Smuzhiyun 	ctx->state = CLOSED;
409*4882a593Smuzhiyun 	mutex_unlock(&ctx->state_mutex);
410*4882a593Smuzhiyun 	if (state != STARTED)
411*4882a593Smuzhiyun 		goto out;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
414*4882a593Smuzhiyun 					 ctx->pe);
415*4882a593Smuzhiyun 	if (unlikely(rc)) {
416*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
417*4882a593Smuzhiyun 			__func__, rc);
418*4882a593Smuzhiyun 		/* If EBUSY, PE could be referenced in future by the AFU */
419*4882a593Smuzhiyun 		if (rc == -EBUSY)
420*4882a593Smuzhiyun 			goto out;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
424*4882a593Smuzhiyun 	if (unlikely(rc)) {
425*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
426*4882a593Smuzhiyun 			__func__, rc);
427*4882a593Smuzhiyun 		goto out;
428*4882a593Smuzhiyun 	}
429*4882a593Smuzhiyun out:
430*4882a593Smuzhiyun 	return rc;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun  * ocxlflash_afu_reset() - reset the AFU
435*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context.
436*4882a593Smuzhiyun  */
ocxlflash_afu_reset(void * ctx_cookie)437*4882a593Smuzhiyun static int ocxlflash_afu_reset(void *ctx_cookie)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
440*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/* Pending implementation from OCXL transport services */
443*4882a593Smuzhiyun 	dev_err_once(dev, "%s: afu_reset() fop not supported\n", __func__);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	/* Silently return success until it is implemented */
446*4882a593Smuzhiyun 	return 0;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun  * ocxlflash_set_master() - sets the context as master
451*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context to set as master.
452*4882a593Smuzhiyun  */
ocxlflash_set_master(void * ctx_cookie)453*4882a593Smuzhiyun static void ocxlflash_set_master(void *ctx_cookie)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ctx->master = true;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun /**
461*4882a593Smuzhiyun  * ocxlflash_get_context() - obtains the context associated with the host
462*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
463*4882a593Smuzhiyun  * @afu_cookie:	Hardware AFU associated with the host.
464*4882a593Smuzhiyun  *
465*4882a593Smuzhiyun  * Return: returns the pointer to host adapter context
466*4882a593Smuzhiyun  */
ocxlflash_get_context(struct pci_dev * pdev,void * afu_cookie)467*4882a593Smuzhiyun static void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = afu_cookie;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	return afu->ocxl_ctx;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun /**
475*4882a593Smuzhiyun  * ocxlflash_dev_context_init() - allocate and initialize an adapter context
476*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
477*4882a593Smuzhiyun  * @afu_cookie:	Hardware AFU associated with the host.
478*4882a593Smuzhiyun  *
479*4882a593Smuzhiyun  * Return: returns the adapter context on success, ERR_PTR on failure
480*4882a593Smuzhiyun  */
ocxlflash_dev_context_init(struct pci_dev * pdev,void * afu_cookie)481*4882a593Smuzhiyun static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = afu_cookie;
484*4882a593Smuzhiyun 	struct device *dev = afu->dev;
485*4882a593Smuzhiyun 	struct ocxlflash_context *ctx;
486*4882a593Smuzhiyun 	int rc;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
489*4882a593Smuzhiyun 	if (unlikely(!ctx)) {
490*4882a593Smuzhiyun 		dev_err(dev, "%s: Context allocation failed\n", __func__);
491*4882a593Smuzhiyun 		rc = -ENOMEM;
492*4882a593Smuzhiyun 		goto err1;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	idr_preload(GFP_KERNEL);
496*4882a593Smuzhiyun 	rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
497*4882a593Smuzhiyun 	idr_preload_end();
498*4882a593Smuzhiyun 	if (unlikely(rc < 0)) {
499*4882a593Smuzhiyun 		dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
500*4882a593Smuzhiyun 		goto err2;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	spin_lock_init(&ctx->slock);
504*4882a593Smuzhiyun 	init_waitqueue_head(&ctx->wq);
505*4882a593Smuzhiyun 	mutex_init(&ctx->state_mutex);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	ctx->state = OPENED;
508*4882a593Smuzhiyun 	ctx->pe = rc;
509*4882a593Smuzhiyun 	ctx->master = false;
510*4882a593Smuzhiyun 	ctx->mapping = NULL;
511*4882a593Smuzhiyun 	ctx->hw_afu = afu;
512*4882a593Smuzhiyun 	ctx->irq_bitmap = 0;
513*4882a593Smuzhiyun 	ctx->pending_irq = false;
514*4882a593Smuzhiyun 	ctx->pending_fault = false;
515*4882a593Smuzhiyun out:
516*4882a593Smuzhiyun 	return ctx;
517*4882a593Smuzhiyun err2:
518*4882a593Smuzhiyun 	kfree(ctx);
519*4882a593Smuzhiyun err1:
520*4882a593Smuzhiyun 	ctx = ERR_PTR(rc);
521*4882a593Smuzhiyun 	goto out;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /**
525*4882a593Smuzhiyun  * ocxlflash_release_context() - releases an adapter context
526*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context to be released.
527*4882a593Smuzhiyun  *
528*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
529*4882a593Smuzhiyun  */
ocxlflash_release_context(void * ctx_cookie)530*4882a593Smuzhiyun static int ocxlflash_release_context(void *ctx_cookie)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
533*4882a593Smuzhiyun 	struct device *dev;
534*4882a593Smuzhiyun 	int rc = 0;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (!ctx)
537*4882a593Smuzhiyun 		goto out;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	dev = ctx->hw_afu->dev;
540*4882a593Smuzhiyun 	mutex_lock(&ctx->state_mutex);
541*4882a593Smuzhiyun 	if (ctx->state >= STARTED) {
542*4882a593Smuzhiyun 		dev_err(dev, "%s: Context in use, state=%d\n", __func__,
543*4882a593Smuzhiyun 			ctx->state);
544*4882a593Smuzhiyun 		mutex_unlock(&ctx->state_mutex);
545*4882a593Smuzhiyun 		rc = -EBUSY;
546*4882a593Smuzhiyun 		goto out;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 	mutex_unlock(&ctx->state_mutex);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	idr_remove(&ctx->hw_afu->idr, ctx->pe);
551*4882a593Smuzhiyun 	ocxlflash_release_mapping(ctx);
552*4882a593Smuzhiyun 	kfree(ctx);
553*4882a593Smuzhiyun out:
554*4882a593Smuzhiyun 	return rc;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /**
558*4882a593Smuzhiyun  * ocxlflash_perst_reloads_same_image() - sets the image reload policy
559*4882a593Smuzhiyun  * @afu_cookie:	Hardware AFU associated with the host.
560*4882a593Smuzhiyun  * @image:	Whether to load the same image on PERST.
561*4882a593Smuzhiyun  */
ocxlflash_perst_reloads_same_image(void * afu_cookie,bool image)562*4882a593Smuzhiyun static void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = afu_cookie;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	afu->perst_same_image = image;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun  * ocxlflash_read_adapter_vpd() - reads the adapter VPD
571*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
572*4882a593Smuzhiyun  * @buf:	Buffer to get the VPD data.
573*4882a593Smuzhiyun  * @count:	Size of buffer (maximum bytes that can be read).
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * Return: size of VPD on success, -errno on failure
576*4882a593Smuzhiyun  */
ocxlflash_read_adapter_vpd(struct pci_dev * pdev,void * buf,size_t count)577*4882a593Smuzhiyun static ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
578*4882a593Smuzhiyun 					  size_t count)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	return pci_read_vpd(pdev, 0, count, buf);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /**
584*4882a593Smuzhiyun  * free_afu_irqs() - internal service to free interrupts
585*4882a593Smuzhiyun  * @ctx:	Adapter context.
586*4882a593Smuzhiyun  */
free_afu_irqs(struct ocxlflash_context * ctx)587*4882a593Smuzhiyun static void free_afu_irqs(struct ocxlflash_context *ctx)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
590*4882a593Smuzhiyun 	struct device *dev = afu->dev;
591*4882a593Smuzhiyun 	int i;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	if (!ctx->irqs) {
594*4882a593Smuzhiyun 		dev_err(dev, "%s: Interrupts not allocated\n", __func__);
595*4882a593Smuzhiyun 		return;
596*4882a593Smuzhiyun 	}
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	for (i = ctx->num_irqs; i >= 0; i--)
599*4882a593Smuzhiyun 		ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	kfree(ctx->irqs);
602*4882a593Smuzhiyun 	ctx->irqs = NULL;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun /**
606*4882a593Smuzhiyun  * alloc_afu_irqs() - internal service to allocate interrupts
607*4882a593Smuzhiyun  * @ctx:	Context associated with the request.
608*4882a593Smuzhiyun  * @num:	Number of interrupts requested.
609*4882a593Smuzhiyun  *
610*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
611*4882a593Smuzhiyun  */
alloc_afu_irqs(struct ocxlflash_context * ctx,int num)612*4882a593Smuzhiyun static int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
615*4882a593Smuzhiyun 	struct device *dev = afu->dev;
616*4882a593Smuzhiyun 	struct ocxlflash_irqs *irqs;
617*4882a593Smuzhiyun 	int rc = 0;
618*4882a593Smuzhiyun 	int hwirq;
619*4882a593Smuzhiyun 	int i;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	if (ctx->irqs) {
622*4882a593Smuzhiyun 		dev_err(dev, "%s: Interrupts already allocated\n", __func__);
623*4882a593Smuzhiyun 		rc = -EEXIST;
624*4882a593Smuzhiyun 		goto out;
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	if (num > OCXL_MAX_IRQS) {
628*4882a593Smuzhiyun 		dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
629*4882a593Smuzhiyun 		rc = -EINVAL;
630*4882a593Smuzhiyun 		goto out;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
634*4882a593Smuzhiyun 	if (unlikely(!irqs)) {
635*4882a593Smuzhiyun 		dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
636*4882a593Smuzhiyun 		rc = -ENOMEM;
637*4882a593Smuzhiyun 		goto out;
638*4882a593Smuzhiyun 	}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	for (i = 0; i < num; i++) {
641*4882a593Smuzhiyun 		rc = ocxl_link_irq_alloc(afu->link_token, &hwirq);
642*4882a593Smuzhiyun 		if (unlikely(rc)) {
643*4882a593Smuzhiyun 			dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
644*4882a593Smuzhiyun 				__func__, rc);
645*4882a593Smuzhiyun 			goto err;
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		irqs[i].hwirq = hwirq;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	ctx->irqs = irqs;
652*4882a593Smuzhiyun 	ctx->num_irqs = num;
653*4882a593Smuzhiyun out:
654*4882a593Smuzhiyun 	return rc;
655*4882a593Smuzhiyun err:
656*4882a593Smuzhiyun 	for (i = i-1; i >= 0; i--)
657*4882a593Smuzhiyun 		ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
658*4882a593Smuzhiyun 	kfree(irqs);
659*4882a593Smuzhiyun 	goto out;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun /**
663*4882a593Smuzhiyun  * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
664*4882a593Smuzhiyun  * @ctx_cookie:	Context associated with the request.
665*4882a593Smuzhiyun  * @num:	Number of interrupts requested.
666*4882a593Smuzhiyun  *
667*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
668*4882a593Smuzhiyun  */
ocxlflash_allocate_afu_irqs(void * ctx_cookie,int num)669*4882a593Smuzhiyun static int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	return alloc_afu_irqs(ctx_cookie, num);
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun /**
675*4882a593Smuzhiyun  * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
676*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context.
677*4882a593Smuzhiyun  */
ocxlflash_free_afu_irqs(void * ctx_cookie)678*4882a593Smuzhiyun static void ocxlflash_free_afu_irqs(void *ctx_cookie)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	free_afu_irqs(ctx_cookie);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun /**
684*4882a593Smuzhiyun  * ocxlflash_unconfig_afu() - unconfigure the AFU
685*4882a593Smuzhiyun  * @afu: AFU associated with the host.
686*4882a593Smuzhiyun  */
ocxlflash_unconfig_afu(struct ocxl_hw_afu * afu)687*4882a593Smuzhiyun static void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	if (afu->gmmio_virt) {
690*4882a593Smuzhiyun 		iounmap(afu->gmmio_virt);
691*4882a593Smuzhiyun 		afu->gmmio_virt = NULL;
692*4882a593Smuzhiyun 	}
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun /**
696*4882a593Smuzhiyun  * ocxlflash_destroy_afu() - destroy the AFU structure
697*4882a593Smuzhiyun  * @afu_cookie:	AFU to be freed.
698*4882a593Smuzhiyun  */
ocxlflash_destroy_afu(void * afu_cookie)699*4882a593Smuzhiyun static void ocxlflash_destroy_afu(void *afu_cookie)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = afu_cookie;
702*4882a593Smuzhiyun 	int pos;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	if (!afu)
705*4882a593Smuzhiyun 		return;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	ocxlflash_release_context(afu->ocxl_ctx);
708*4882a593Smuzhiyun 	idr_destroy(&afu->idr);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* Disable the AFU */
711*4882a593Smuzhiyun 	pos = afu->acfg.dvsec_afu_control_pos;
712*4882a593Smuzhiyun 	ocxl_config_set_afu_state(afu->pdev, pos, 0);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	ocxlflash_unconfig_afu(afu);
715*4882a593Smuzhiyun 	kfree(afu);
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun /**
719*4882a593Smuzhiyun  * ocxlflash_config_fn() - configure the host function
720*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
721*4882a593Smuzhiyun  * @afu:	AFU associated with the host.
722*4882a593Smuzhiyun  *
723*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
724*4882a593Smuzhiyun  */
ocxlflash_config_fn(struct pci_dev * pdev,struct ocxl_hw_afu * afu)725*4882a593Smuzhiyun static int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	struct ocxl_fn_config *fcfg = &afu->fcfg;
728*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
729*4882a593Smuzhiyun 	u16 base, enabled, supported;
730*4882a593Smuzhiyun 	int rc = 0;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	/* Read DVSEC config of the function */
733*4882a593Smuzhiyun 	rc = ocxl_config_read_function(pdev, fcfg);
734*4882a593Smuzhiyun 	if (unlikely(rc)) {
735*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
736*4882a593Smuzhiyun 			__func__, rc);
737*4882a593Smuzhiyun 		goto out;
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	/* Check if function has AFUs defined, only 1 per function supported */
741*4882a593Smuzhiyun 	if (fcfg->max_afu_index >= 0) {
742*4882a593Smuzhiyun 		afu->is_present = true;
743*4882a593Smuzhiyun 		if (fcfg->max_afu_index != 0)
744*4882a593Smuzhiyun 			dev_warn(dev, "%s: Unexpected AFU index value %d\n",
745*4882a593Smuzhiyun 				 __func__, fcfg->max_afu_index);
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
749*4882a593Smuzhiyun 	if (unlikely(rc)) {
750*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
751*4882a593Smuzhiyun 			__func__, rc);
752*4882a593Smuzhiyun 		goto out;
753*4882a593Smuzhiyun 	}
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	afu->fn_actag_base = base;
756*4882a593Smuzhiyun 	afu->fn_actag_enabled = enabled;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
759*4882a593Smuzhiyun 	dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
760*4882a593Smuzhiyun 		__func__, base, enabled);
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	rc = ocxl_link_setup(pdev, 0, &afu->link_token);
763*4882a593Smuzhiyun 	if (unlikely(rc)) {
764*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
765*4882a593Smuzhiyun 			__func__, rc);
766*4882a593Smuzhiyun 		goto out;
767*4882a593Smuzhiyun 	}
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
770*4882a593Smuzhiyun 	if (unlikely(rc)) {
771*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
772*4882a593Smuzhiyun 			__func__, rc);
773*4882a593Smuzhiyun 		goto err;
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun out:
776*4882a593Smuzhiyun 	return rc;
777*4882a593Smuzhiyun err:
778*4882a593Smuzhiyun 	ocxl_link_release(pdev, afu->link_token);
779*4882a593Smuzhiyun 	goto out;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun /**
783*4882a593Smuzhiyun  * ocxlflash_unconfig_fn() - unconfigure the host function
784*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
785*4882a593Smuzhiyun  * @afu:	AFU associated with the host.
786*4882a593Smuzhiyun  */
ocxlflash_unconfig_fn(struct pci_dev * pdev,struct ocxl_hw_afu * afu)787*4882a593Smuzhiyun static void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun 	ocxl_link_release(pdev, afu->link_token);
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /**
793*4882a593Smuzhiyun  * ocxlflash_map_mmio() - map the AFU MMIO space
794*4882a593Smuzhiyun  * @afu: AFU associated with the host.
795*4882a593Smuzhiyun  *
796*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
797*4882a593Smuzhiyun  */
ocxlflash_map_mmio(struct ocxl_hw_afu * afu)798*4882a593Smuzhiyun static int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct ocxl_afu_config *acfg = &afu->acfg;
801*4882a593Smuzhiyun 	struct pci_dev *pdev = afu->pdev;
802*4882a593Smuzhiyun 	struct device *dev = afu->dev;
803*4882a593Smuzhiyun 	phys_addr_t gmmio, ppmmio;
804*4882a593Smuzhiyun 	int rc = 0;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
807*4882a593Smuzhiyun 	if (unlikely(rc)) {
808*4882a593Smuzhiyun 		dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
809*4882a593Smuzhiyun 			__func__, rc);
810*4882a593Smuzhiyun 		goto out;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 	gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
813*4882a593Smuzhiyun 	gmmio += acfg->global_mmio_offset;
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
816*4882a593Smuzhiyun 	if (unlikely(rc)) {
817*4882a593Smuzhiyun 		dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
818*4882a593Smuzhiyun 			__func__, rc);
819*4882a593Smuzhiyun 		goto err1;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 	ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
822*4882a593Smuzhiyun 	ppmmio += acfg->pp_mmio_offset;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
825*4882a593Smuzhiyun 	if (unlikely(!afu->gmmio_virt)) {
826*4882a593Smuzhiyun 		dev_err(dev, "%s: MMIO mapping failed\n", __func__);
827*4882a593Smuzhiyun 		rc = -ENOMEM;
828*4882a593Smuzhiyun 		goto err2;
829*4882a593Smuzhiyun 	}
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	afu->gmmio_phys = gmmio;
832*4882a593Smuzhiyun 	afu->ppmmio_phys = ppmmio;
833*4882a593Smuzhiyun out:
834*4882a593Smuzhiyun 	return rc;
835*4882a593Smuzhiyun err2:
836*4882a593Smuzhiyun 	pci_release_region(pdev, acfg->pp_mmio_bar);
837*4882a593Smuzhiyun err1:
838*4882a593Smuzhiyun 	pci_release_region(pdev, acfg->global_mmio_bar);
839*4882a593Smuzhiyun 	goto out;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun /**
843*4882a593Smuzhiyun  * ocxlflash_config_afu() - configure the host AFU
844*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
845*4882a593Smuzhiyun  * @afu:	AFU associated with the host.
846*4882a593Smuzhiyun  *
847*4882a593Smuzhiyun  * Must be called _after_ host function configuration.
848*4882a593Smuzhiyun  *
849*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
850*4882a593Smuzhiyun  */
ocxlflash_config_afu(struct pci_dev * pdev,struct ocxl_hw_afu * afu)851*4882a593Smuzhiyun static int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	struct ocxl_afu_config *acfg = &afu->acfg;
854*4882a593Smuzhiyun 	struct ocxl_fn_config *fcfg = &afu->fcfg;
855*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
856*4882a593Smuzhiyun 	int count;
857*4882a593Smuzhiyun 	int base;
858*4882a593Smuzhiyun 	int pos;
859*4882a593Smuzhiyun 	int rc = 0;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	/* This HW AFU function does not have any AFUs defined */
862*4882a593Smuzhiyun 	if (!afu->is_present)
863*4882a593Smuzhiyun 		goto out;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	/* Read AFU config at index 0 */
866*4882a593Smuzhiyun 	rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
867*4882a593Smuzhiyun 	if (unlikely(rc)) {
868*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
869*4882a593Smuzhiyun 			__func__, rc);
870*4882a593Smuzhiyun 		goto out;
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	/* Only one AFU per function is supported, so actag_base is same */
874*4882a593Smuzhiyun 	base = afu->fn_actag_base;
875*4882a593Smuzhiyun 	count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
876*4882a593Smuzhiyun 	pos = acfg->dvsec_afu_control_pos;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	ocxl_config_set_afu_actag(pdev, pos, base, count);
879*4882a593Smuzhiyun 	dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
880*4882a593Smuzhiyun 	afu->afu_actag_base = base;
881*4882a593Smuzhiyun 	afu->afu_actag_enabled = count;
882*4882a593Smuzhiyun 	afu->max_pasid = 1 << acfg->pasid_supported_log;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	rc = ocxlflash_map_mmio(afu);
887*4882a593Smuzhiyun 	if (unlikely(rc)) {
888*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
889*4882a593Smuzhiyun 			__func__, rc);
890*4882a593Smuzhiyun 		goto out;
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	/* Enable the AFU */
894*4882a593Smuzhiyun 	ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
895*4882a593Smuzhiyun out:
896*4882a593Smuzhiyun 	return rc;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /**
900*4882a593Smuzhiyun  * ocxlflash_create_afu() - create the AFU for OCXL
901*4882a593Smuzhiyun  * @pdev:	PCI device associated with the host.
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  * Return: AFU on success, NULL on failure
904*4882a593Smuzhiyun  */
ocxlflash_create_afu(struct pci_dev * pdev)905*4882a593Smuzhiyun static void *ocxlflash_create_afu(struct pci_dev *pdev)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
908*4882a593Smuzhiyun 	struct ocxlflash_context *ctx;
909*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu;
910*4882a593Smuzhiyun 	int rc;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	afu = kzalloc(sizeof(*afu), GFP_KERNEL);
913*4882a593Smuzhiyun 	if (unlikely(!afu)) {
914*4882a593Smuzhiyun 		dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
915*4882a593Smuzhiyun 		goto out;
916*4882a593Smuzhiyun 	}
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	afu->pdev = pdev;
919*4882a593Smuzhiyun 	afu->dev = dev;
920*4882a593Smuzhiyun 	idr_init(&afu->idr);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	rc = ocxlflash_config_fn(pdev, afu);
923*4882a593Smuzhiyun 	if (unlikely(rc)) {
924*4882a593Smuzhiyun 		dev_err(dev, "%s: Function configuration failed rc=%d\n",
925*4882a593Smuzhiyun 			__func__, rc);
926*4882a593Smuzhiyun 		goto err1;
927*4882a593Smuzhiyun 	}
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	rc = ocxlflash_config_afu(pdev, afu);
930*4882a593Smuzhiyun 	if (unlikely(rc)) {
931*4882a593Smuzhiyun 		dev_err(dev, "%s: AFU configuration failed rc=%d\n",
932*4882a593Smuzhiyun 			__func__, rc);
933*4882a593Smuzhiyun 		goto err2;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	ctx = ocxlflash_dev_context_init(pdev, afu);
937*4882a593Smuzhiyun 	if (IS_ERR(ctx)) {
938*4882a593Smuzhiyun 		rc = PTR_ERR(ctx);
939*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
940*4882a593Smuzhiyun 			__func__, rc);
941*4882a593Smuzhiyun 		goto err3;
942*4882a593Smuzhiyun 	}
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	afu->ocxl_ctx = ctx;
945*4882a593Smuzhiyun out:
946*4882a593Smuzhiyun 	return afu;
947*4882a593Smuzhiyun err3:
948*4882a593Smuzhiyun 	ocxlflash_unconfig_afu(afu);
949*4882a593Smuzhiyun err2:
950*4882a593Smuzhiyun 	ocxlflash_unconfig_fn(pdev, afu);
951*4882a593Smuzhiyun err1:
952*4882a593Smuzhiyun 	idr_destroy(&afu->idr);
953*4882a593Smuzhiyun 	kfree(afu);
954*4882a593Smuzhiyun 	afu = NULL;
955*4882a593Smuzhiyun 	goto out;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun /**
959*4882a593Smuzhiyun  * ctx_event_pending() - check for any event pending on the context
960*4882a593Smuzhiyun  * @ctx:	Context to be checked.
961*4882a593Smuzhiyun  *
962*4882a593Smuzhiyun  * Return: true if there is an event pending, false if none pending
963*4882a593Smuzhiyun  */
ctx_event_pending(struct ocxlflash_context * ctx)964*4882a593Smuzhiyun static inline bool ctx_event_pending(struct ocxlflash_context *ctx)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	if (ctx->pending_irq || ctx->pending_fault)
967*4882a593Smuzhiyun 		return true;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	return false;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun /**
973*4882a593Smuzhiyun  * afu_poll() - poll the AFU for events on the context
974*4882a593Smuzhiyun  * @file:	File associated with the adapter context.
975*4882a593Smuzhiyun  * @poll:	Poll structure from the user.
976*4882a593Smuzhiyun  *
977*4882a593Smuzhiyun  * Return: poll mask
978*4882a593Smuzhiyun  */
afu_poll(struct file * file,struct poll_table_struct * poll)979*4882a593Smuzhiyun static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = file->private_data;
982*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
983*4882a593Smuzhiyun 	ulong lock_flags;
984*4882a593Smuzhiyun 	int mask = 0;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	poll_wait(file, &ctx->wq, poll);
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	spin_lock_irqsave(&ctx->slock, lock_flags);
989*4882a593Smuzhiyun 	if (ctx_event_pending(ctx))
990*4882a593Smuzhiyun 		mask |= POLLIN | POLLRDNORM;
991*4882a593Smuzhiyun 	else if (ctx->state == CLOSED)
992*4882a593Smuzhiyun 		mask |= POLLERR;
993*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ctx->slock, lock_flags);
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
996*4882a593Smuzhiyun 		__func__, ctx->pe, mask);
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	return mask;
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun /**
1002*4882a593Smuzhiyun  * afu_read() - perform a read on the context for any event
1003*4882a593Smuzhiyun  * @file:	File associated with the adapter context.
1004*4882a593Smuzhiyun  * @buf:	Buffer to receive the data.
1005*4882a593Smuzhiyun  * @count:	Size of buffer (maximum bytes that can be read).
1006*4882a593Smuzhiyun  * @off:	Offset.
1007*4882a593Smuzhiyun  *
1008*4882a593Smuzhiyun  * Return: size of the data read on success, -errno on failure
1009*4882a593Smuzhiyun  */
afu_read(struct file * file,char __user * buf,size_t count,loff_t * off)1010*4882a593Smuzhiyun static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
1011*4882a593Smuzhiyun 			loff_t *off)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = file->private_data;
1014*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
1015*4882a593Smuzhiyun 	struct cxl_event event;
1016*4882a593Smuzhiyun 	ulong lock_flags;
1017*4882a593Smuzhiyun 	ssize_t esize;
1018*4882a593Smuzhiyun 	ssize_t rc;
1019*4882a593Smuzhiyun 	int bit;
1020*4882a593Smuzhiyun 	DEFINE_WAIT(event_wait);
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	if (*off != 0) {
1023*4882a593Smuzhiyun 		dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
1024*4882a593Smuzhiyun 			__func__, *off);
1025*4882a593Smuzhiyun 		rc = -EINVAL;
1026*4882a593Smuzhiyun 		goto out;
1027*4882a593Smuzhiyun 	}
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	spin_lock_irqsave(&ctx->slock, lock_flags);
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	for (;;) {
1032*4882a593Smuzhiyun 		prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 		if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
1035*4882a593Smuzhiyun 			break;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 		if (file->f_flags & O_NONBLOCK) {
1038*4882a593Smuzhiyun 			dev_err(dev, "%s: File cannot be blocked on I/O\n",
1039*4882a593Smuzhiyun 				__func__);
1040*4882a593Smuzhiyun 			rc = -EAGAIN;
1041*4882a593Smuzhiyun 			goto err;
1042*4882a593Smuzhiyun 		}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 		if (signal_pending(current)) {
1045*4882a593Smuzhiyun 			dev_err(dev, "%s: Signal pending on the process\n",
1046*4882a593Smuzhiyun 				__func__);
1047*4882a593Smuzhiyun 			rc = -ERESTARTSYS;
1048*4882a593Smuzhiyun 			goto err;
1049*4882a593Smuzhiyun 		}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ctx->slock, lock_flags);
1052*4882a593Smuzhiyun 		schedule();
1053*4882a593Smuzhiyun 		spin_lock_irqsave(&ctx->slock, lock_flags);
1054*4882a593Smuzhiyun 	}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	finish_wait(&ctx->wq, &event_wait);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	memset(&event, 0, sizeof(event));
1059*4882a593Smuzhiyun 	event.header.process_element = ctx->pe;
1060*4882a593Smuzhiyun 	event.header.size = sizeof(struct cxl_event_header);
1061*4882a593Smuzhiyun 	if (ctx->pending_irq) {
1062*4882a593Smuzhiyun 		esize = sizeof(struct cxl_event_afu_interrupt);
1063*4882a593Smuzhiyun 		event.header.size += esize;
1064*4882a593Smuzhiyun 		event.header.type = CXL_EVENT_AFU_INTERRUPT;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 		bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
1067*4882a593Smuzhiyun 		clear_bit(bit, &ctx->irq_bitmap);
1068*4882a593Smuzhiyun 		event.irq.irq = bit + 1;
1069*4882a593Smuzhiyun 		if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
1070*4882a593Smuzhiyun 			ctx->pending_irq = false;
1071*4882a593Smuzhiyun 	} else if (ctx->pending_fault) {
1072*4882a593Smuzhiyun 		event.header.size += sizeof(struct cxl_event_data_storage);
1073*4882a593Smuzhiyun 		event.header.type = CXL_EVENT_DATA_STORAGE;
1074*4882a593Smuzhiyun 		event.fault.addr = ctx->fault_addr;
1075*4882a593Smuzhiyun 		event.fault.dsisr = ctx->fault_dsisr;
1076*4882a593Smuzhiyun 		ctx->pending_fault = false;
1077*4882a593Smuzhiyun 	}
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ctx->slock, lock_flags);
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	if (copy_to_user(buf, &event, event.header.size)) {
1082*4882a593Smuzhiyun 		dev_err(dev, "%s: copy_to_user failed\n", __func__);
1083*4882a593Smuzhiyun 		rc = -EFAULT;
1084*4882a593Smuzhiyun 		goto out;
1085*4882a593Smuzhiyun 	}
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	rc = event.header.size;
1088*4882a593Smuzhiyun out:
1089*4882a593Smuzhiyun 	return rc;
1090*4882a593Smuzhiyun err:
1091*4882a593Smuzhiyun 	finish_wait(&ctx->wq, &event_wait);
1092*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ctx->slock, lock_flags);
1093*4882a593Smuzhiyun 	goto out;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun /**
1097*4882a593Smuzhiyun  * afu_release() - release and free the context
1098*4882a593Smuzhiyun  * @inode:	File inode pointer.
1099*4882a593Smuzhiyun  * @file:	File associated with the context.
1100*4882a593Smuzhiyun  *
1101*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1102*4882a593Smuzhiyun  */
afu_release(struct inode * inode,struct file * file)1103*4882a593Smuzhiyun static int afu_release(struct inode *inode, struct file *file)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = file->private_data;
1106*4882a593Smuzhiyun 	int i;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	/* Unmap and free the interrupts associated with the context */
1109*4882a593Smuzhiyun 	for (i = ctx->num_irqs; i >= 0; i--)
1110*4882a593Smuzhiyun 		afu_unmap_irq(0, ctx, i, ctx);
1111*4882a593Smuzhiyun 	free_afu_irqs(ctx);
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	return ocxlflash_release_context(ctx);
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun /**
1117*4882a593Smuzhiyun  * ocxlflash_mmap_fault() - mmap fault handler
1118*4882a593Smuzhiyun  * @vmf:	VM fault associated with current fault.
1119*4882a593Smuzhiyun  *
1120*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1121*4882a593Smuzhiyun  */
ocxlflash_mmap_fault(struct vm_fault * vmf)1122*4882a593Smuzhiyun static vm_fault_t ocxlflash_mmap_fault(struct vm_fault *vmf)
1123*4882a593Smuzhiyun {
1124*4882a593Smuzhiyun 	struct vm_area_struct *vma = vmf->vma;
1125*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = vma->vm_file->private_data;
1126*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
1127*4882a593Smuzhiyun 	u64 mmio_area, offset;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	offset = vmf->pgoff << PAGE_SHIFT;
1130*4882a593Smuzhiyun 	if (offset >= ctx->psn_size)
1131*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	mutex_lock(&ctx->state_mutex);
1134*4882a593Smuzhiyun 	if (ctx->state != STARTED) {
1135*4882a593Smuzhiyun 		dev_err(dev, "%s: Context not started, state=%d\n",
1136*4882a593Smuzhiyun 			__func__, ctx->state);
1137*4882a593Smuzhiyun 		mutex_unlock(&ctx->state_mutex);
1138*4882a593Smuzhiyun 		return VM_FAULT_SIGBUS;
1139*4882a593Smuzhiyun 	}
1140*4882a593Smuzhiyun 	mutex_unlock(&ctx->state_mutex);
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	mmio_area = ctx->psn_phys;
1143*4882a593Smuzhiyun 	mmio_area += offset;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	return vmf_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun static const struct vm_operations_struct ocxlflash_vmops = {
1149*4882a593Smuzhiyun 	.fault = ocxlflash_mmap_fault,
1150*4882a593Smuzhiyun };
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun /**
1153*4882a593Smuzhiyun  * afu_mmap() - map the fault handler operations
1154*4882a593Smuzhiyun  * @file:	File associated with the context.
1155*4882a593Smuzhiyun  * @vma:	VM area associated with mapping.
1156*4882a593Smuzhiyun  *
1157*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1158*4882a593Smuzhiyun  */
afu_mmap(struct file * file,struct vm_area_struct * vma)1159*4882a593Smuzhiyun static int afu_mmap(struct file *file, struct vm_area_struct *vma)
1160*4882a593Smuzhiyun {
1161*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = file->private_data;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	if ((vma_pages(vma) + vma->vm_pgoff) >
1164*4882a593Smuzhiyun 	    (ctx->psn_size >> PAGE_SHIFT))
1165*4882a593Smuzhiyun 		return -EINVAL;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	vma->vm_flags |= VM_IO | VM_PFNMAP;
1168*4882a593Smuzhiyun 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1169*4882a593Smuzhiyun 	vma->vm_ops = &ocxlflash_vmops;
1170*4882a593Smuzhiyun 	return 0;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun static const struct file_operations ocxl_afu_fops = {
1174*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1175*4882a593Smuzhiyun 	.poll		= afu_poll,
1176*4882a593Smuzhiyun 	.read		= afu_read,
1177*4882a593Smuzhiyun 	.release	= afu_release,
1178*4882a593Smuzhiyun 	.mmap		= afu_mmap,
1179*4882a593Smuzhiyun };
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun #define PATCH_FOPS(NAME)						\
1182*4882a593Smuzhiyun 	do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun /**
1185*4882a593Smuzhiyun  * ocxlflash_get_fd() - get file descriptor for an adapter context
1186*4882a593Smuzhiyun  * @ctx_cookie:	Adapter context.
1187*4882a593Smuzhiyun  * @fops:	File operations to be associated.
1188*4882a593Smuzhiyun  * @fd:		File descriptor to be returned back.
1189*4882a593Smuzhiyun  *
1190*4882a593Smuzhiyun  * Return: pointer to the file on success, ERR_PTR on failure
1191*4882a593Smuzhiyun  */
ocxlflash_get_fd(void * ctx_cookie,struct file_operations * fops,int * fd)1192*4882a593Smuzhiyun static struct file *ocxlflash_get_fd(void *ctx_cookie,
1193*4882a593Smuzhiyun 				     struct file_operations *fops, int *fd)
1194*4882a593Smuzhiyun {
1195*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
1196*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
1197*4882a593Smuzhiyun 	struct file *file;
1198*4882a593Smuzhiyun 	int flags, fdtmp;
1199*4882a593Smuzhiyun 	int rc = 0;
1200*4882a593Smuzhiyun 	char *name = NULL;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	/* Only allow one fd per context */
1203*4882a593Smuzhiyun 	if (ctx->mapping) {
1204*4882a593Smuzhiyun 		dev_err(dev, "%s: Context is already mapped to an fd\n",
1205*4882a593Smuzhiyun 			__func__);
1206*4882a593Smuzhiyun 		rc = -EEXIST;
1207*4882a593Smuzhiyun 		goto err1;
1208*4882a593Smuzhiyun 	}
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	flags = O_RDWR | O_CLOEXEC;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	/* This code is similar to anon_inode_getfd() */
1213*4882a593Smuzhiyun 	rc = get_unused_fd_flags(flags);
1214*4882a593Smuzhiyun 	if (unlikely(rc < 0)) {
1215*4882a593Smuzhiyun 		dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
1216*4882a593Smuzhiyun 			__func__, rc);
1217*4882a593Smuzhiyun 		goto err1;
1218*4882a593Smuzhiyun 	}
1219*4882a593Smuzhiyun 	fdtmp = rc;
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	/* Patch the file ops that are not defined */
1222*4882a593Smuzhiyun 	if (fops) {
1223*4882a593Smuzhiyun 		PATCH_FOPS(poll);
1224*4882a593Smuzhiyun 		PATCH_FOPS(read);
1225*4882a593Smuzhiyun 		PATCH_FOPS(release);
1226*4882a593Smuzhiyun 		PATCH_FOPS(mmap);
1227*4882a593Smuzhiyun 	} else /* Use default ops */
1228*4882a593Smuzhiyun 		fops = (struct file_operations *)&ocxl_afu_fops;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
1231*4882a593Smuzhiyun 	file = ocxlflash_getfile(dev, name, fops, ctx, flags);
1232*4882a593Smuzhiyun 	kfree(name);
1233*4882a593Smuzhiyun 	if (IS_ERR(file)) {
1234*4882a593Smuzhiyun 		rc = PTR_ERR(file);
1235*4882a593Smuzhiyun 		dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
1236*4882a593Smuzhiyun 			__func__, rc);
1237*4882a593Smuzhiyun 		goto err2;
1238*4882a593Smuzhiyun 	}
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	ctx->mapping = file->f_mapping;
1241*4882a593Smuzhiyun 	*fd = fdtmp;
1242*4882a593Smuzhiyun out:
1243*4882a593Smuzhiyun 	return file;
1244*4882a593Smuzhiyun err2:
1245*4882a593Smuzhiyun 	put_unused_fd(fdtmp);
1246*4882a593Smuzhiyun err1:
1247*4882a593Smuzhiyun 	file = ERR_PTR(rc);
1248*4882a593Smuzhiyun 	goto out;
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun /**
1252*4882a593Smuzhiyun  * ocxlflash_fops_get_context() - get the context associated with the file
1253*4882a593Smuzhiyun  * @file:	File associated with the adapter context.
1254*4882a593Smuzhiyun  *
1255*4882a593Smuzhiyun  * Return: pointer to the context
1256*4882a593Smuzhiyun  */
ocxlflash_fops_get_context(struct file * file)1257*4882a593Smuzhiyun static void *ocxlflash_fops_get_context(struct file *file)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun 	return file->private_data;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun /**
1263*4882a593Smuzhiyun  * ocxlflash_afu_irq() - interrupt handler for user contexts
1264*4882a593Smuzhiyun  * @irq:	Interrupt number.
1265*4882a593Smuzhiyun  * @data:	Private data provided at interrupt registration, the context.
1266*4882a593Smuzhiyun  *
1267*4882a593Smuzhiyun  * Return: Always return IRQ_HANDLED.
1268*4882a593Smuzhiyun  */
ocxlflash_afu_irq(int irq,void * data)1269*4882a593Smuzhiyun static irqreturn_t ocxlflash_afu_irq(int irq, void *data)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = data;
1272*4882a593Smuzhiyun 	struct device *dev = ctx->hw_afu->dev;
1273*4882a593Smuzhiyun 	int i;
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
1276*4882a593Smuzhiyun 		__func__, ctx->pe, irq);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	for (i = 0; i < ctx->num_irqs; i++) {
1279*4882a593Smuzhiyun 		if (ctx->irqs[i].virq == irq)
1280*4882a593Smuzhiyun 			break;
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun 	if (unlikely(i >= ctx->num_irqs)) {
1283*4882a593Smuzhiyun 		dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
1284*4882a593Smuzhiyun 		goto out;
1285*4882a593Smuzhiyun 	}
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	spin_lock(&ctx->slock);
1288*4882a593Smuzhiyun 	set_bit(i - 1, &ctx->irq_bitmap);
1289*4882a593Smuzhiyun 	ctx->pending_irq = true;
1290*4882a593Smuzhiyun 	spin_unlock(&ctx->slock);
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	wake_up_all(&ctx->wq);
1293*4882a593Smuzhiyun out:
1294*4882a593Smuzhiyun 	return IRQ_HANDLED;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun /**
1298*4882a593Smuzhiyun  * ocxlflash_start_work() - start a user context
1299*4882a593Smuzhiyun  * @ctx_cookie:	Context to be started.
1300*4882a593Smuzhiyun  * @num_irqs:	Number of interrupts requested.
1301*4882a593Smuzhiyun  *
1302*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1303*4882a593Smuzhiyun  */
ocxlflash_start_work(void * ctx_cookie,u64 num_irqs)1304*4882a593Smuzhiyun static int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun 	struct ocxlflash_context *ctx = ctx_cookie;
1307*4882a593Smuzhiyun 	struct ocxl_hw_afu *afu = ctx->hw_afu;
1308*4882a593Smuzhiyun 	struct device *dev = afu->dev;
1309*4882a593Smuzhiyun 	char *name;
1310*4882a593Smuzhiyun 	int rc = 0;
1311*4882a593Smuzhiyun 	int i;
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	rc = alloc_afu_irqs(ctx, num_irqs);
1314*4882a593Smuzhiyun 	if (unlikely(rc < 0)) {
1315*4882a593Smuzhiyun 		dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
1316*4882a593Smuzhiyun 		goto out;
1317*4882a593Smuzhiyun 	}
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	for (i = 0; i < num_irqs; i++) {
1320*4882a593Smuzhiyun 		name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
1321*4882a593Smuzhiyun 				 dev_name(dev), ctx->pe, i);
1322*4882a593Smuzhiyun 		rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
1323*4882a593Smuzhiyun 		kfree(name);
1324*4882a593Smuzhiyun 		if (unlikely(rc < 0)) {
1325*4882a593Smuzhiyun 			dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
1326*4882a593Smuzhiyun 				__func__, rc);
1327*4882a593Smuzhiyun 			goto err;
1328*4882a593Smuzhiyun 		}
1329*4882a593Smuzhiyun 	}
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	rc = start_context(ctx);
1332*4882a593Smuzhiyun 	if (unlikely(rc)) {
1333*4882a593Smuzhiyun 		dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
1334*4882a593Smuzhiyun 		goto err;
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun out:
1337*4882a593Smuzhiyun 	return rc;
1338*4882a593Smuzhiyun err:
1339*4882a593Smuzhiyun 	for (i = i-1; i >= 0; i--)
1340*4882a593Smuzhiyun 		afu_unmap_irq(0, ctx, i, ctx);
1341*4882a593Smuzhiyun 	free_afu_irqs(ctx);
1342*4882a593Smuzhiyun 	goto out;
1343*4882a593Smuzhiyun };
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun /**
1346*4882a593Smuzhiyun  * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
1347*4882a593Smuzhiyun  * @file:	File installed with adapter file descriptor.
1348*4882a593Smuzhiyun  * @vma:	VM area associated with mapping.
1349*4882a593Smuzhiyun  *
1350*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1351*4882a593Smuzhiyun  */
ocxlflash_fd_mmap(struct file * file,struct vm_area_struct * vma)1352*4882a593Smuzhiyun static int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	return afu_mmap(file, vma);
1355*4882a593Smuzhiyun }
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun /**
1358*4882a593Smuzhiyun  * ocxlflash_fd_release() - release the context associated with the file
1359*4882a593Smuzhiyun  * @inode:	File inode pointer.
1360*4882a593Smuzhiyun  * @file:	File associated with the adapter context.
1361*4882a593Smuzhiyun  *
1362*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
1363*4882a593Smuzhiyun  */
ocxlflash_fd_release(struct inode * inode,struct file * file)1364*4882a593Smuzhiyun static int ocxlflash_fd_release(struct inode *inode, struct file *file)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun 	return afu_release(inode, file);
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun /* Backend ops to ocxlflash services */
1370*4882a593Smuzhiyun const struct cxlflash_backend_ops cxlflash_ocxl_ops = {
1371*4882a593Smuzhiyun 	.module			= THIS_MODULE,
1372*4882a593Smuzhiyun 	.psa_map		= ocxlflash_psa_map,
1373*4882a593Smuzhiyun 	.psa_unmap		= ocxlflash_psa_unmap,
1374*4882a593Smuzhiyun 	.process_element	= ocxlflash_process_element,
1375*4882a593Smuzhiyun 	.map_afu_irq		= ocxlflash_map_afu_irq,
1376*4882a593Smuzhiyun 	.unmap_afu_irq		= ocxlflash_unmap_afu_irq,
1377*4882a593Smuzhiyun 	.get_irq_objhndl	= ocxlflash_get_irq_objhndl,
1378*4882a593Smuzhiyun 	.start_context		= ocxlflash_start_context,
1379*4882a593Smuzhiyun 	.stop_context		= ocxlflash_stop_context,
1380*4882a593Smuzhiyun 	.afu_reset		= ocxlflash_afu_reset,
1381*4882a593Smuzhiyun 	.set_master		= ocxlflash_set_master,
1382*4882a593Smuzhiyun 	.get_context		= ocxlflash_get_context,
1383*4882a593Smuzhiyun 	.dev_context_init	= ocxlflash_dev_context_init,
1384*4882a593Smuzhiyun 	.release_context	= ocxlflash_release_context,
1385*4882a593Smuzhiyun 	.perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
1386*4882a593Smuzhiyun 	.read_adapter_vpd	= ocxlflash_read_adapter_vpd,
1387*4882a593Smuzhiyun 	.allocate_afu_irqs	= ocxlflash_allocate_afu_irqs,
1388*4882a593Smuzhiyun 	.free_afu_irqs		= ocxlflash_free_afu_irqs,
1389*4882a593Smuzhiyun 	.create_afu		= ocxlflash_create_afu,
1390*4882a593Smuzhiyun 	.destroy_afu		= ocxlflash_destroy_afu,
1391*4882a593Smuzhiyun 	.get_fd			= ocxlflash_get_fd,
1392*4882a593Smuzhiyun 	.fops_get_context	= ocxlflash_fops_get_context,
1393*4882a593Smuzhiyun 	.start_work		= ocxlflash_start_work,
1394*4882a593Smuzhiyun 	.fd_mmap		= ocxlflash_fd_mmap,
1395*4882a593Smuzhiyun 	.fd_release		= ocxlflash_fd_release,
1396*4882a593Smuzhiyun };
1397