2 * CXL Flash Device Driver
4 * Written by: Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
5 * Uma Krishnan <ukrishn@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) 2018 IBM Corporation
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/file.h>
16 #include <linux/idr.h>
17 #include <linux/module.h>
18 #include <linux/mount.h>
19 #include <linux/poll.h>
20 #include <linux/sched/signal.h>
22 #include <misc/ocxl.h>
24 #include <uapi/misc/cxl.h>
30 * Pseudo-filesystem to allocate inodes.
33 #define OCXLFLASH_FS_MAGIC 0x1697698f
35 static int ocxlflash_fs_cnt
;
36 static struct vfsmount
*ocxlflash_vfs_mount
;
38 static const struct dentry_operations ocxlflash_fs_dops
= {
39 .d_dname
= simple_dname
,
43 * ocxlflash_fs_mount() - mount the pseudo-filesystem
44 * @fs_type: File system type.
45 * @flags: Flags for the filesystem.
46 * @dev_name: Device name associated with the filesystem.
47 * @data: Data pointer.
49 * Return: pointer to the directory entry structure
51 static struct dentry
*ocxlflash_fs_mount(struct file_system_type
*fs_type
,
52 int flags
, const char *dev_name
,
55 return mount_pseudo(fs_type
, "ocxlflash:", NULL
, &ocxlflash_fs_dops
,
59 static struct file_system_type ocxlflash_fs_type
= {
62 .mount
= ocxlflash_fs_mount
,
63 .kill_sb
= kill_anon_super
,
67 * ocxlflash_release_mapping() - release the memory mapping
68 * @ctx: Context whose mapping is to be released.
70 static void ocxlflash_release_mapping(struct ocxlflash_context
*ctx
)
73 simple_release_fs(&ocxlflash_vfs_mount
, &ocxlflash_fs_cnt
);
78 * ocxlflash_getfile() - allocate pseudo filesystem, inode, and the file
79 * @dev: Generic device of the host.
80 * @name: Name of the pseudo filesystem.
81 * @fops: File operations.
82 * @priv: Private data.
83 * @flags: Flags for the file.
85 * Return: pointer to the file on success, ERR_PTR on failure
87 static struct file
*ocxlflash_getfile(struct device
*dev
, const char *name
,
88 const struct file_operations
*fops
,
89 void *priv
, int flags
)
94 struct inode
*inode
= NULL
;
97 if (fops
->owner
&& !try_module_get(fops
->owner
)) {
98 dev_err(dev
, "%s: Owner does not exist\n", __func__
);
103 rc
= simple_pin_fs(&ocxlflash_fs_type
, &ocxlflash_vfs_mount
,
105 if (unlikely(rc
< 0)) {
106 dev_err(dev
, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
111 inode
= alloc_anon_inode(ocxlflash_vfs_mount
->mnt_sb
);
114 dev_err(dev
, "%s: alloc_anon_inode failed rc=%d\n",
120 this.len
= strlen(name
);
122 path
.dentry
= d_alloc_pseudo(ocxlflash_vfs_mount
->mnt_sb
, &this);
124 dev_err(dev
, "%s: d_alloc_pseudo failed\n", __func__
);
129 path
.mnt
= mntget(ocxlflash_vfs_mount
);
130 d_instantiate(path
.dentry
, inode
);
132 file
= alloc_file(&path
, OPEN_FMODE(flags
), fops
);
135 dev_err(dev
, "%s: alloc_file failed rc=%d\n",
140 file
->f_flags
= flags
& (O_ACCMODE
| O_NONBLOCK
);
141 file
->private_data
= priv
;
149 simple_release_fs(&ocxlflash_vfs_mount
, &ocxlflash_fs_cnt
);
151 module_put(fops
->owner
);
158 * ocxlflash_psa_map() - map the process specific MMIO space
159 * @ctx_cookie: Adapter context for which the mapping needs to be done.
161 * Return: MMIO pointer of the mapped region
163 static void __iomem
*ocxlflash_psa_map(void *ctx_cookie
)
165 struct ocxlflash_context
*ctx
= ctx_cookie
;
166 struct device
*dev
= ctx
->hw_afu
->dev
;
168 mutex_lock(&ctx
->state_mutex
);
169 if (ctx
->state
!= STARTED
) {
170 dev_err(dev
, "%s: Context not started, state=%d\n", __func__
,
172 mutex_unlock(&ctx
->state_mutex
);
175 mutex_unlock(&ctx
->state_mutex
);
177 return ioremap(ctx
->psn_phys
, ctx
->psn_size
);
181 * ocxlflash_psa_unmap() - unmap the process specific MMIO space
182 * @addr: MMIO pointer to unmap.
184 static void ocxlflash_psa_unmap(void __iomem
*addr
)
190 * ocxlflash_process_element() - get process element of the adapter context
191 * @ctx_cookie: Adapter context associated with the process element.
193 * Return: process element of the adapter context
195 static int ocxlflash_process_element(void *ctx_cookie
)
197 struct ocxlflash_context
*ctx
= ctx_cookie
;
203 * afu_map_irq() - map the interrupt of the adapter context
205 * @ctx: Adapter context.
206 * @num: Per-context AFU interrupt number.
207 * @handler: Interrupt handler to register.
208 * @cookie: Interrupt handler private data.
209 * @name: Name of the interrupt.
211 * Return: 0 on success, -errno on failure
213 static int afu_map_irq(u64 flags
, struct ocxlflash_context
*ctx
, int num
,
214 irq_handler_t handler
, void *cookie
, char *name
)
216 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
217 struct device
*dev
= afu
->dev
;
218 struct ocxlflash_irqs
*irq
;
223 if (num
< 0 || num
>= ctx
->num_irqs
) {
224 dev_err(dev
, "%s: Interrupt %d not allocated\n", __func__
, num
);
229 irq
= &ctx
->irqs
[num
];
230 virq
= irq_create_mapping(NULL
, irq
->hwirq
);
231 if (unlikely(!virq
)) {
232 dev_err(dev
, "%s: irq_create_mapping failed\n", __func__
);
237 rc
= request_irq(virq
, handler
, 0, name
, cookie
);
239 dev_err(dev
, "%s: request_irq failed rc=%d\n", __func__
, rc
);
243 vtrig
= ioremap(irq
->ptrig
, PAGE_SIZE
);
244 if (unlikely(!vtrig
)) {
245 dev_err(dev
, "%s: Trigger page mapping failed\n", __func__
);
255 free_irq(virq
, cookie
);
257 irq_dispose_mapping(virq
);
262 * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
263 * @ctx_cookie: Adapter context.
264 * @num: Per-context AFU interrupt number.
265 * @handler: Interrupt handler to register.
266 * @cookie: Interrupt handler private data.
267 * @name: Name of the interrupt.
269 * Return: 0 on success, -errno on failure
271 static int ocxlflash_map_afu_irq(void *ctx_cookie
, int num
,
272 irq_handler_t handler
, void *cookie
,
275 return afu_map_irq(0, ctx_cookie
, num
, handler
, cookie
, name
);
279 * afu_unmap_irq() - unmap the interrupt
281 * @ctx: Adapter context.
282 * @num: Per-context AFU interrupt number.
283 * @cookie: Interrupt handler private data.
285 static void afu_unmap_irq(u64 flags
, struct ocxlflash_context
*ctx
, int num
,
288 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
289 struct device
*dev
= afu
->dev
;
290 struct ocxlflash_irqs
*irq
;
292 if (num
< 0 || num
>= ctx
->num_irqs
) {
293 dev_err(dev
, "%s: Interrupt %d not allocated\n", __func__
, num
);
297 irq
= &ctx
->irqs
[num
];
301 if (irq_find_mapping(NULL
, irq
->hwirq
)) {
302 free_irq(irq
->virq
, cookie
);
303 irq_dispose_mapping(irq
->virq
);
306 memset(irq
, 0, sizeof(*irq
));
310 * ocxlflash_unmap_afu_irq() - unmap the interrupt
311 * @ctx_cookie: Adapter context.
312 * @num: Per-context AFU interrupt number.
313 * @cookie: Interrupt handler private data.
315 static void ocxlflash_unmap_afu_irq(void *ctx_cookie
, int num
, void *cookie
)
317 return afu_unmap_irq(0, ctx_cookie
, num
, cookie
);
321 * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
322 * @ctx_cookie: Context associated with the interrupt.
323 * @irq: Interrupt number.
325 * Return: effective address of the mapped region
327 static u64
ocxlflash_get_irq_objhndl(void *ctx_cookie
, int irq
)
329 struct ocxlflash_context
*ctx
= ctx_cookie
;
331 if (irq
< 0 || irq
>= ctx
->num_irqs
)
334 return (__force u64
)ctx
->irqs
[irq
].vtrig
;
338 * ocxlflash_xsl_fault() - callback when translation error is triggered
339 * @data: Private data provided at callback registration, the context.
340 * @addr: Address that triggered the error.
341 * @dsisr: Value of dsisr register.
343 static void ocxlflash_xsl_fault(void *data
, u64 addr
, u64 dsisr
)
345 struct ocxlflash_context
*ctx
= data
;
347 spin_lock(&ctx
->slock
);
348 ctx
->fault_addr
= addr
;
349 ctx
->fault_dsisr
= dsisr
;
350 ctx
->pending_fault
= true;
351 spin_unlock(&ctx
->slock
);
353 wake_up_all(&ctx
->wq
);
357 * start_context() - local routine to start a context
358 * @ctx: Adapter context to be started.
360 * Assign the context specific MMIO space, add and enable the PE.
362 * Return: 0 on success, -errno on failure
364 static int start_context(struct ocxlflash_context
*ctx
)
366 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
367 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
368 void *link_token
= afu
->link_token
;
369 struct device
*dev
= afu
->dev
;
370 bool master
= ctx
->master
;
371 struct mm_struct
*mm
;
375 mutex_lock(&ctx
->state_mutex
);
376 if (ctx
->state
!= OPENED
) {
377 dev_err(dev
, "%s: Context state invalid, state=%d\n",
378 __func__
, ctx
->state
);
384 ctx
->psn_size
= acfg
->global_mmio_size
;
385 ctx
->psn_phys
= afu
->gmmio_phys
;
387 ctx
->psn_size
= acfg
->pp_mmio_stride
;
388 ctx
->psn_phys
= afu
->ppmmio_phys
+ (ctx
->pe
* ctx
->psn_size
);
391 /* pid and mm not set for master contexts */
396 pid
= current
->mm
->context
.id
;
400 rc
= ocxl_link_add_pe(link_token
, ctx
->pe
, pid
, 0, 0, mm
,
401 ocxlflash_xsl_fault
, ctx
);
403 dev_err(dev
, "%s: ocxl_link_add_pe failed rc=%d\n",
408 ctx
->state
= STARTED
;
410 mutex_unlock(&ctx
->state_mutex
);
415 * ocxlflash_start_context() - start a kernel context
416 * @ctx_cookie: Adapter context to be started.
418 * Return: 0 on success, -errno on failure
420 static int ocxlflash_start_context(void *ctx_cookie
)
422 struct ocxlflash_context
*ctx
= ctx_cookie
;
424 return start_context(ctx
);
428 * ocxlflash_stop_context() - stop a context
429 * @ctx_cookie: Adapter context to be stopped.
431 * Return: 0 on success, -errno on failure
433 static int ocxlflash_stop_context(void *ctx_cookie
)
435 struct ocxlflash_context
*ctx
= ctx_cookie
;
436 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
437 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
438 struct pci_dev
*pdev
= afu
->pdev
;
439 struct device
*dev
= afu
->dev
;
440 enum ocxlflash_ctx_state state
;
443 mutex_lock(&ctx
->state_mutex
);
446 mutex_unlock(&ctx
->state_mutex
);
447 if (state
!= STARTED
)
450 rc
= ocxl_config_terminate_pasid(pdev
, acfg
->dvsec_afu_control_pos
,
453 dev_err(dev
, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
455 /* If EBUSY, PE could be referenced in future by the AFU */
460 rc
= ocxl_link_remove_pe(afu
->link_token
, ctx
->pe
);
462 dev_err(dev
, "%s: ocxl_link_remove_pe failed rc=%d\n",
471 * ocxlflash_afu_reset() - reset the AFU
472 * @ctx_cookie: Adapter context.
474 static int ocxlflash_afu_reset(void *ctx_cookie
)
476 struct ocxlflash_context
*ctx
= ctx_cookie
;
477 struct device
*dev
= ctx
->hw_afu
->dev
;
479 /* Pending implementation from OCXL transport services */
480 dev_err_once(dev
, "%s: afu_reset() fop not supported\n", __func__
);
482 /* Silently return success until it is implemented */
487 * ocxlflash_set_master() - sets the context as master
488 * @ctx_cookie: Adapter context to set as master.
490 static void ocxlflash_set_master(void *ctx_cookie
)
492 struct ocxlflash_context
*ctx
= ctx_cookie
;
498 * ocxlflash_get_context() - obtains the context associated with the host
499 * @pdev: PCI device associated with the host.
500 * @afu_cookie: Hardware AFU associated with the host.
502 * Return: returns the pointer to host adapter context
504 static void *ocxlflash_get_context(struct pci_dev
*pdev
, void *afu_cookie
)
506 struct ocxl_hw_afu
*afu
= afu_cookie
;
508 return afu
->ocxl_ctx
;
512 * ocxlflash_dev_context_init() - allocate and initialize an adapter context
513 * @pdev: PCI device associated with the host.
514 * @afu_cookie: Hardware AFU associated with the host.
516 * Return: returns the adapter context on success, ERR_PTR on failure
518 static void *ocxlflash_dev_context_init(struct pci_dev
*pdev
, void *afu_cookie
)
520 struct ocxl_hw_afu
*afu
= afu_cookie
;
521 struct device
*dev
= afu
->dev
;
522 struct ocxlflash_context
*ctx
;
525 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
526 if (unlikely(!ctx
)) {
527 dev_err(dev
, "%s: Context allocation failed\n", __func__
);
532 idr_preload(GFP_KERNEL
);
533 rc
= idr_alloc(&afu
->idr
, ctx
, 0, afu
->max_pasid
, GFP_NOWAIT
);
535 if (unlikely(rc
< 0)) {
536 dev_err(dev
, "%s: idr_alloc failed rc=%d\n", __func__
, rc
);
540 spin_lock_init(&ctx
->slock
);
541 init_waitqueue_head(&ctx
->wq
);
542 mutex_init(&ctx
->state_mutex
);
550 ctx
->pending_irq
= false;
551 ctx
->pending_fault
= false;
562 * ocxlflash_release_context() - releases an adapter context
563 * @ctx_cookie: Adapter context to be released.
565 * Return: 0 on success, -errno on failure
567 static int ocxlflash_release_context(void *ctx_cookie
)
569 struct ocxlflash_context
*ctx
= ctx_cookie
;
576 dev
= ctx
->hw_afu
->dev
;
577 mutex_lock(&ctx
->state_mutex
);
578 if (ctx
->state
>= STARTED
) {
579 dev_err(dev
, "%s: Context in use, state=%d\n", __func__
,
581 mutex_unlock(&ctx
->state_mutex
);
585 mutex_unlock(&ctx
->state_mutex
);
587 idr_remove(&ctx
->hw_afu
->idr
, ctx
->pe
);
588 ocxlflash_release_mapping(ctx
);
595 * ocxlflash_perst_reloads_same_image() - sets the image reload policy
596 * @afu_cookie: Hardware AFU associated with the host.
597 * @image: Whether to load the same image on PERST.
599 static void ocxlflash_perst_reloads_same_image(void *afu_cookie
, bool image
)
601 struct ocxl_hw_afu
*afu
= afu_cookie
;
603 afu
->perst_same_image
= image
;
607 * ocxlflash_read_adapter_vpd() - reads the adapter VPD
608 * @pdev: PCI device associated with the host.
609 * @buf: Buffer to get the VPD data.
610 * @count: Size of buffer (maximum bytes that can be read).
612 * Return: size of VPD on success, -errno on failure
614 static ssize_t
ocxlflash_read_adapter_vpd(struct pci_dev
*pdev
, void *buf
,
617 return pci_read_vpd(pdev
, 0, count
, buf
);
621 * free_afu_irqs() - internal service to free interrupts
622 * @ctx: Adapter context.
624 static void free_afu_irqs(struct ocxlflash_context
*ctx
)
626 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
627 struct device
*dev
= afu
->dev
;
631 dev_err(dev
, "%s: Interrupts not allocated\n", __func__
);
635 for (i
= ctx
->num_irqs
; i
>= 0; i
--)
636 ocxl_link_free_irq(afu
->link_token
, ctx
->irqs
[i
].hwirq
);
643 * alloc_afu_irqs() - internal service to allocate interrupts
644 * @ctx: Context associated with the request.
645 * @num: Number of interrupts requested.
647 * Return: 0 on success, -errno on failure
649 static int alloc_afu_irqs(struct ocxlflash_context
*ctx
, int num
)
651 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
652 struct device
*dev
= afu
->dev
;
653 struct ocxlflash_irqs
*irqs
;
660 dev_err(dev
, "%s: Interrupts already allocated\n", __func__
);
665 if (num
> OCXL_MAX_IRQS
) {
666 dev_err(dev
, "%s: Too many interrupts num=%d\n", __func__
, num
);
671 irqs
= kcalloc(num
, sizeof(*irqs
), GFP_KERNEL
);
672 if (unlikely(!irqs
)) {
673 dev_err(dev
, "%s: Context irqs allocation failed\n", __func__
);
678 for (i
= 0; i
< num
; i
++) {
679 rc
= ocxl_link_irq_alloc(afu
->link_token
, &hwirq
, &addr
);
681 dev_err(dev
, "%s: ocxl_link_irq_alloc failed rc=%d\n",
686 irqs
[i
].hwirq
= hwirq
;
687 irqs
[i
].ptrig
= addr
;
695 for (i
= i
-1; i
>= 0; i
--)
696 ocxl_link_free_irq(afu
->link_token
, irqs
[i
].hwirq
);
702 * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
703 * @ctx_cookie: Context associated with the request.
704 * @num: Number of interrupts requested.
706 * Return: 0 on success, -errno on failure
708 static int ocxlflash_allocate_afu_irqs(void *ctx_cookie
, int num
)
710 return alloc_afu_irqs(ctx_cookie
, num
);
714 * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
715 * @ctx_cookie: Adapter context.
717 static void ocxlflash_free_afu_irqs(void *ctx_cookie
)
719 free_afu_irqs(ctx_cookie
);
723 * ocxlflash_unconfig_afu() - unconfigure the AFU
724 * @afu: AFU associated with the host.
726 static void ocxlflash_unconfig_afu(struct ocxl_hw_afu
*afu
)
728 if (afu
->gmmio_virt
) {
729 iounmap(afu
->gmmio_virt
);
730 afu
->gmmio_virt
= NULL
;
735 * ocxlflash_destroy_afu() - destroy the AFU structure
736 * @afu_cookie: AFU to be freed.
738 static void ocxlflash_destroy_afu(void *afu_cookie
)
740 struct ocxl_hw_afu
*afu
= afu_cookie
;
746 ocxlflash_release_context(afu
->ocxl_ctx
);
747 idr_destroy(&afu
->idr
);
749 /* Disable the AFU */
750 pos
= afu
->acfg
.dvsec_afu_control_pos
;
751 ocxl_config_set_afu_state(afu
->pdev
, pos
, 0);
753 ocxlflash_unconfig_afu(afu
);
758 * ocxlflash_config_fn() - configure the host function
759 * @pdev: PCI device associated with the host.
760 * @afu: AFU associated with the host.
762 * Return: 0 on success, -errno on failure
764 static int ocxlflash_config_fn(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
766 struct ocxl_fn_config
*fcfg
= &afu
->fcfg
;
767 struct device
*dev
= &pdev
->dev
;
768 u16 base
, enabled
, supported
;
771 /* Read DVSEC config of the function */
772 rc
= ocxl_config_read_function(pdev
, fcfg
);
774 dev_err(dev
, "%s: ocxl_config_read_function failed rc=%d\n",
779 /* Check if function has AFUs defined, only 1 per function supported */
780 if (fcfg
->max_afu_index
>= 0) {
781 afu
->is_present
= true;
782 if (fcfg
->max_afu_index
!= 0)
783 dev_warn(dev
, "%s: Unexpected AFU index value %d\n",
784 __func__
, fcfg
->max_afu_index
);
787 rc
= ocxl_config_get_actag_info(pdev
, &base
, &enabled
, &supported
);
789 dev_err(dev
, "%s: ocxl_config_get_actag_info failed rc=%d\n",
794 afu
->fn_actag_base
= base
;
795 afu
->fn_actag_enabled
= enabled
;
797 ocxl_config_set_actag(pdev
, fcfg
->dvsec_function_pos
, base
, enabled
);
798 dev_dbg(dev
, "%s: Function acTag range base=%u enabled=%u\n",
799 __func__
, base
, enabled
);
801 rc
= ocxl_link_setup(pdev
, 0, &afu
->link_token
);
803 dev_err(dev
, "%s: ocxl_link_setup failed rc=%d\n",
808 rc
= ocxl_config_set_TL(pdev
, fcfg
->dvsec_tl_pos
);
810 dev_err(dev
, "%s: ocxl_config_set_TL failed rc=%d\n",
817 ocxl_link_release(pdev
, afu
->link_token
);
822 * ocxlflash_unconfig_fn() - unconfigure the host function
823 * @pdev: PCI device associated with the host.
824 * @afu: AFU associated with the host.
826 static void ocxlflash_unconfig_fn(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
828 ocxl_link_release(pdev
, afu
->link_token
);
832 * ocxlflash_map_mmio() - map the AFU MMIO space
833 * @afu: AFU associated with the host.
835 * Return: 0 on success, -errno on failure
837 static int ocxlflash_map_mmio(struct ocxl_hw_afu
*afu
)
839 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
840 struct pci_dev
*pdev
= afu
->pdev
;
841 struct device
*dev
= afu
->dev
;
842 phys_addr_t gmmio
, ppmmio
;
845 rc
= pci_request_region(pdev
, acfg
->global_mmio_bar
, "ocxlflash");
847 dev_err(dev
, "%s: pci_request_region for global failed rc=%d\n",
851 gmmio
= pci_resource_start(pdev
, acfg
->global_mmio_bar
);
852 gmmio
+= acfg
->global_mmio_offset
;
854 rc
= pci_request_region(pdev
, acfg
->pp_mmio_bar
, "ocxlflash");
856 dev_err(dev
, "%s: pci_request_region for pp bar failed rc=%d\n",
860 ppmmio
= pci_resource_start(pdev
, acfg
->pp_mmio_bar
);
861 ppmmio
+= acfg
->pp_mmio_offset
;
863 afu
->gmmio_virt
= ioremap(gmmio
, acfg
->global_mmio_size
);
864 if (unlikely(!afu
->gmmio_virt
)) {
865 dev_err(dev
, "%s: MMIO mapping failed\n", __func__
);
870 afu
->gmmio_phys
= gmmio
;
871 afu
->ppmmio_phys
= ppmmio
;
875 pci_release_region(pdev
, acfg
->pp_mmio_bar
);
877 pci_release_region(pdev
, acfg
->global_mmio_bar
);
882 * ocxlflash_config_afu() - configure the host AFU
883 * @pdev: PCI device associated with the host.
884 * @afu: AFU associated with the host.
886 * Must be called _after_ host function configuration.
888 * Return: 0 on success, -errno on failure
890 static int ocxlflash_config_afu(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
892 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
893 struct ocxl_fn_config
*fcfg
= &afu
->fcfg
;
894 struct device
*dev
= &pdev
->dev
;
900 /* This HW AFU function does not have any AFUs defined */
901 if (!afu
->is_present
)
904 /* Read AFU config at index 0 */
905 rc
= ocxl_config_read_afu(pdev
, fcfg
, acfg
, 0);
907 dev_err(dev
, "%s: ocxl_config_read_afu failed rc=%d\n",
912 /* Only one AFU per function is supported, so actag_base is same */
913 base
= afu
->fn_actag_base
;
914 count
= min_t(int, acfg
->actag_supported
, afu
->fn_actag_enabled
);
915 pos
= acfg
->dvsec_afu_control_pos
;
917 ocxl_config_set_afu_actag(pdev
, pos
, base
, count
);
918 dev_dbg(dev
, "%s: acTag base=%d enabled=%d\n", __func__
, base
, count
);
919 afu
->afu_actag_base
= base
;
920 afu
->afu_actag_enabled
= count
;
921 afu
->max_pasid
= 1 << acfg
->pasid_supported_log
;
923 ocxl_config_set_afu_pasid(pdev
, pos
, 0, acfg
->pasid_supported_log
);
925 rc
= ocxlflash_map_mmio(afu
);
927 dev_err(dev
, "%s: ocxlflash_map_mmio failed rc=%d\n",
933 ocxl_config_set_afu_state(pdev
, acfg
->dvsec_afu_control_pos
, 1);
939 * ocxlflash_create_afu() - create the AFU for OCXL
940 * @pdev: PCI device associated with the host.
942 * Return: AFU on success, NULL on failure
944 static void *ocxlflash_create_afu(struct pci_dev
*pdev
)
946 struct device
*dev
= &pdev
->dev
;
947 struct ocxlflash_context
*ctx
;
948 struct ocxl_hw_afu
*afu
;
951 afu
= kzalloc(sizeof(*afu
), GFP_KERNEL
);
952 if (unlikely(!afu
)) {
953 dev_err(dev
, "%s: HW AFU allocation failed\n", __func__
);
961 rc
= ocxlflash_config_fn(pdev
, afu
);
963 dev_err(dev
, "%s: Function configuration failed rc=%d\n",
968 rc
= ocxlflash_config_afu(pdev
, afu
);
970 dev_err(dev
, "%s: AFU configuration failed rc=%d\n",
975 ctx
= ocxlflash_dev_context_init(pdev
, afu
);
978 dev_err(dev
, "%s: ocxlflash_dev_context_init failed rc=%d\n",
987 ocxlflash_unconfig_afu(afu
);
989 ocxlflash_unconfig_fn(pdev
, afu
);
991 idr_destroy(&afu
->idr
);
998 * ctx_event_pending() - check for any event pending on the context
999 * @ctx: Context to be checked.
1001 * Return: true if there is an event pending, false if none pending
1003 static inline bool ctx_event_pending(struct ocxlflash_context
*ctx
)
1005 if (ctx
->pending_irq
|| ctx
->pending_fault
)
1012 * afu_poll() - poll the AFU for events on the context
1013 * @file: File associated with the adapter context.
1014 * @poll: Poll structure from the user.
1018 static unsigned int afu_poll(struct file
*file
, struct poll_table_struct
*poll
)
1020 struct ocxlflash_context
*ctx
= file
->private_data
;
1021 struct device
*dev
= ctx
->hw_afu
->dev
;
1025 poll_wait(file
, &ctx
->wq
, poll
);
1027 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1028 if (ctx_event_pending(ctx
))
1029 mask
|= POLLIN
| POLLRDNORM
;
1030 else if (ctx
->state
== CLOSED
)
1032 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1034 dev_dbg(dev
, "%s: Poll wait completed for pe %i mask %i\n",
1035 __func__
, ctx
->pe
, mask
);
1041 * afu_read() - perform a read on the context for any event
1042 * @file: File associated with the adapter context.
1043 * @buf: Buffer to receive the data.
1044 * @count: Size of buffer (maximum bytes that can be read).
1047 * Return: size of the data read on success, -errno on failure
1049 static ssize_t
afu_read(struct file
*file
, char __user
*buf
, size_t count
,
1052 struct ocxlflash_context
*ctx
= file
->private_data
;
1053 struct device
*dev
= ctx
->hw_afu
->dev
;
1054 struct cxl_event event
;
1059 DEFINE_WAIT(event_wait
);
1062 dev_err(dev
, "%s: Non-zero offset not supported, off=%lld\n",
1068 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1071 prepare_to_wait(&ctx
->wq
, &event_wait
, TASK_INTERRUPTIBLE
);
1073 if (ctx_event_pending(ctx
) || (ctx
->state
== CLOSED
))
1076 if (file
->f_flags
& O_NONBLOCK
) {
1077 dev_err(dev
, "%s: File cannot be blocked on I/O\n",
1083 if (signal_pending(current
)) {
1084 dev_err(dev
, "%s: Signal pending on the process\n",
1090 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1092 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1095 finish_wait(&ctx
->wq
, &event_wait
);
1097 memset(&event
, 0, sizeof(event
));
1098 event
.header
.process_element
= ctx
->pe
;
1099 event
.header
.size
= sizeof(struct cxl_event_header
);
1100 if (ctx
->pending_irq
) {
1101 esize
= sizeof(struct cxl_event_afu_interrupt
);
1102 event
.header
.size
+= esize
;
1103 event
.header
.type
= CXL_EVENT_AFU_INTERRUPT
;
1105 bit
= find_first_bit(&ctx
->irq_bitmap
, ctx
->num_irqs
);
1106 clear_bit(bit
, &ctx
->irq_bitmap
);
1107 event
.irq
.irq
= bit
+ 1;
1108 if (bitmap_empty(&ctx
->irq_bitmap
, ctx
->num_irqs
))
1109 ctx
->pending_irq
= false;
1110 } else if (ctx
->pending_fault
) {
1111 event
.header
.size
+= sizeof(struct cxl_event_data_storage
);
1112 event
.header
.type
= CXL_EVENT_DATA_STORAGE
;
1113 event
.fault
.addr
= ctx
->fault_addr
;
1114 event
.fault
.dsisr
= ctx
->fault_dsisr
;
1115 ctx
->pending_fault
= false;
1118 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1120 if (copy_to_user(buf
, &event
, event
.header
.size
)) {
1121 dev_err(dev
, "%s: copy_to_user failed\n", __func__
);
1126 rc
= event
.header
.size
;
1130 finish_wait(&ctx
->wq
, &event_wait
);
1131 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1136 * afu_release() - release and free the context
1137 * @inode: File inode pointer.
1138 * @file: File associated with the context.
1140 * Return: 0 on success, -errno on failure
1142 static int afu_release(struct inode
*inode
, struct file
*file
)
1144 struct ocxlflash_context
*ctx
= file
->private_data
;
1147 /* Unmap and free the interrupts associated with the context */
1148 for (i
= ctx
->num_irqs
; i
>= 0; i
--)
1149 afu_unmap_irq(0, ctx
, i
, ctx
);
1152 return ocxlflash_release_context(ctx
);
1156 * ocxlflash_mmap_fault() - mmap fault handler
1157 * @vmf: VM fault associated with current fault.
1159 * Return: 0 on success, -errno on failure
1161 static int ocxlflash_mmap_fault(struct vm_fault
*vmf
)
1163 struct vm_area_struct
*vma
= vmf
->vma
;
1164 struct ocxlflash_context
*ctx
= vma
->vm_file
->private_data
;
1165 struct device
*dev
= ctx
->hw_afu
->dev
;
1166 u64 mmio_area
, offset
;
1168 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1169 if (offset
>= ctx
->psn_size
)
1170 return VM_FAULT_SIGBUS
;
1172 mutex_lock(&ctx
->state_mutex
);
1173 if (ctx
->state
!= STARTED
) {
1174 dev_err(dev
, "%s: Context not started, state=%d\n",
1175 __func__
, ctx
->state
);
1176 mutex_unlock(&ctx
->state_mutex
);
1177 return VM_FAULT_SIGBUS
;
1179 mutex_unlock(&ctx
->state_mutex
);
1181 mmio_area
= ctx
->psn_phys
;
1182 mmio_area
+= offset
;
1184 vm_insert_pfn(vma
, vmf
->address
, mmio_area
>> PAGE_SHIFT
);
1185 return VM_FAULT_NOPAGE
;
1188 static const struct vm_operations_struct ocxlflash_vmops
= {
1189 .fault
= ocxlflash_mmap_fault
,
1193 * afu_mmap() - map the fault handler operations
1194 * @file: File associated with the context.
1195 * @vma: VM area associated with mapping.
1197 * Return: 0 on success, -errno on failure
1199 static int afu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1201 struct ocxlflash_context
*ctx
= file
->private_data
;
1203 if ((vma_pages(vma
) + vma
->vm_pgoff
) >
1204 (ctx
->psn_size
>> PAGE_SHIFT
))
1207 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
;
1208 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1209 vma
->vm_ops
= &ocxlflash_vmops
;
1213 static const struct file_operations ocxl_afu_fops
= {
1214 .owner
= THIS_MODULE
,
1217 .release
= afu_release
,
1221 #define PATCH_FOPS(NAME) \
1222 do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1225 * ocxlflash_get_fd() - get file descriptor for an adapter context
1226 * @ctx_cookie: Adapter context.
1227 * @fops: File operations to be associated.
1228 * @fd: File descriptor to be returned back.
1230 * Return: pointer to the file on success, ERR_PTR on failure
1232 static struct file
*ocxlflash_get_fd(void *ctx_cookie
,
1233 struct file_operations
*fops
, int *fd
)
1235 struct ocxlflash_context
*ctx
= ctx_cookie
;
1236 struct device
*dev
= ctx
->hw_afu
->dev
;
1242 /* Only allow one fd per context */
1244 dev_err(dev
, "%s: Context is already mapped to an fd\n",
1250 flags
= O_RDWR
| O_CLOEXEC
;
1252 /* This code is similar to anon_inode_getfd() */
1253 rc
= get_unused_fd_flags(flags
);
1254 if (unlikely(rc
< 0)) {
1255 dev_err(dev
, "%s: get_unused_fd_flags failed rc=%d\n",
1261 /* Patch the file ops that are not defined */
1265 PATCH_FOPS(release
);
1267 } else /* Use default ops */
1268 fops
= (struct file_operations
*)&ocxl_afu_fops
;
1270 name
= kasprintf(GFP_KERNEL
, "ocxlflash:%d", ctx
->pe
);
1271 file
= ocxlflash_getfile(dev
, name
, fops
, ctx
, flags
);
1275 dev_err(dev
, "%s: ocxlflash_getfile failed rc=%d\n",
1280 ctx
->mapping
= file
->f_mapping
;
1285 put_unused_fd(fdtmp
);
1292 * ocxlflash_fops_get_context() - get the context associated with the file
1293 * @file: File associated with the adapter context.
1295 * Return: pointer to the context
1297 static void *ocxlflash_fops_get_context(struct file
*file
)
1299 return file
->private_data
;
1303 * ocxlflash_afu_irq() - interrupt handler for user contexts
1304 * @irq: Interrupt number.
1305 * @data: Private data provided at interrupt registration, the context.
1307 * Return: Always return IRQ_HANDLED.
1309 static irqreturn_t
ocxlflash_afu_irq(int irq
, void *data
)
1311 struct ocxlflash_context
*ctx
= data
;
1312 struct device
*dev
= ctx
->hw_afu
->dev
;
1315 dev_dbg(dev
, "%s: Interrupt raised for pe %i virq %i\n",
1316 __func__
, ctx
->pe
, irq
);
1318 for (i
= 0; i
< ctx
->num_irqs
; i
++) {
1319 if (ctx
->irqs
[i
].virq
== irq
)
1322 if (unlikely(i
>= ctx
->num_irqs
)) {
1323 dev_err(dev
, "%s: Received AFU IRQ out of range\n", __func__
);
1327 spin_lock(&ctx
->slock
);
1328 set_bit(i
- 1, &ctx
->irq_bitmap
);
1329 ctx
->pending_irq
= true;
1330 spin_unlock(&ctx
->slock
);
1332 wake_up_all(&ctx
->wq
);
1338 * ocxlflash_start_work() - start a user context
1339 * @ctx_cookie: Context to be started.
1340 * @num_irqs: Number of interrupts requested.
1342 * Return: 0 on success, -errno on failure
1344 static int ocxlflash_start_work(void *ctx_cookie
, u64 num_irqs
)
1346 struct ocxlflash_context
*ctx
= ctx_cookie
;
1347 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
1348 struct device
*dev
= afu
->dev
;
1353 rc
= alloc_afu_irqs(ctx
, num_irqs
);
1354 if (unlikely(rc
< 0)) {
1355 dev_err(dev
, "%s: alloc_afu_irqs failed rc=%d\n", __func__
, rc
);
1359 for (i
= 0; i
< num_irqs
; i
++) {
1360 name
= kasprintf(GFP_KERNEL
, "ocxlflash-%s-pe%i-%i",
1361 dev_name(dev
), ctx
->pe
, i
);
1362 rc
= afu_map_irq(0, ctx
, i
, ocxlflash_afu_irq
, ctx
, name
);
1364 if (unlikely(rc
< 0)) {
1365 dev_err(dev
, "%s: afu_map_irq failed rc=%d\n",
1371 rc
= start_context(ctx
);
1373 dev_err(dev
, "%s: start_context failed rc=%d\n", __func__
, rc
);
1379 for (i
= i
-1; i
>= 0; i
--)
1380 afu_unmap_irq(0, ctx
, i
, ctx
);
1386 * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
1387 * @file: File installed with adapter file descriptor.
1388 * @vma: VM area associated with mapping.
1390 * Return: 0 on success, -errno on failure
1392 static int ocxlflash_fd_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1394 return afu_mmap(file
, vma
);
1398 * ocxlflash_fd_release() - release the context associated with the file
1399 * @inode: File inode pointer.
1400 * @file: File associated with the adapter context.
1402 * Return: 0 on success, -errno on failure
1404 static int ocxlflash_fd_release(struct inode
*inode
, struct file
*file
)
1406 return afu_release(inode
, file
);
1409 /* Backend ops to ocxlflash services */
1410 const struct cxlflash_backend_ops cxlflash_ocxl_ops
= {
1411 .module
= THIS_MODULE
,
1412 .psa_map
= ocxlflash_psa_map
,
1413 .psa_unmap
= ocxlflash_psa_unmap
,
1414 .process_element
= ocxlflash_process_element
,
1415 .map_afu_irq
= ocxlflash_map_afu_irq
,
1416 .unmap_afu_irq
= ocxlflash_unmap_afu_irq
,
1417 .get_irq_objhndl
= ocxlflash_get_irq_objhndl
,
1418 .start_context
= ocxlflash_start_context
,
1419 .stop_context
= ocxlflash_stop_context
,
1420 .afu_reset
= ocxlflash_afu_reset
,
1421 .set_master
= ocxlflash_set_master
,
1422 .get_context
= ocxlflash_get_context
,
1423 .dev_context_init
= ocxlflash_dev_context_init
,
1424 .release_context
= ocxlflash_release_context
,
1425 .perst_reloads_same_image
= ocxlflash_perst_reloads_same_image
,
1426 .read_adapter_vpd
= ocxlflash_read_adapter_vpd
,
1427 .allocate_afu_irqs
= ocxlflash_allocate_afu_irqs
,
1428 .free_afu_irqs
= ocxlflash_free_afu_irqs
,
1429 .create_afu
= ocxlflash_create_afu
,
1430 .destroy_afu
= ocxlflash_destroy_afu
,
1431 .get_fd
= ocxlflash_get_fd
,
1432 .fops_get_context
= ocxlflash_fops_get_context
,
1433 .start_work
= ocxlflash_start_work
,
1434 .fd_mmap
= ocxlflash_fd_mmap
,
1435 .fd_release
= ocxlflash_fd_release
,