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
)
95 if (fops
->owner
&& !try_module_get(fops
->owner
)) {
96 dev_err(dev
, "%s: Owner does not exist\n", __func__
);
101 rc
= simple_pin_fs(&ocxlflash_fs_type
, &ocxlflash_vfs_mount
,
103 if (unlikely(rc
< 0)) {
104 dev_err(dev
, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
109 inode
= alloc_anon_inode(ocxlflash_vfs_mount
->mnt_sb
);
112 dev_err(dev
, "%s: alloc_anon_inode failed rc=%d\n",
117 file
= alloc_file_pseudo(inode
, ocxlflash_vfs_mount
, name
,
118 flags
& (O_ACCMODE
| O_NONBLOCK
), fops
);
121 dev_err(dev
, "%s: alloc_file failed rc=%d\n",
126 file
->private_data
= priv
;
132 simple_release_fs(&ocxlflash_vfs_mount
, &ocxlflash_fs_cnt
);
134 module_put(fops
->owner
);
141 * ocxlflash_psa_map() - map the process specific MMIO space
142 * @ctx_cookie: Adapter context for which the mapping needs to be done.
144 * Return: MMIO pointer of the mapped region
146 static void __iomem
*ocxlflash_psa_map(void *ctx_cookie
)
148 struct ocxlflash_context
*ctx
= ctx_cookie
;
149 struct device
*dev
= ctx
->hw_afu
->dev
;
151 mutex_lock(&ctx
->state_mutex
);
152 if (ctx
->state
!= STARTED
) {
153 dev_err(dev
, "%s: Context not started, state=%d\n", __func__
,
155 mutex_unlock(&ctx
->state_mutex
);
158 mutex_unlock(&ctx
->state_mutex
);
160 return ioremap(ctx
->psn_phys
, ctx
->psn_size
);
164 * ocxlflash_psa_unmap() - unmap the process specific MMIO space
165 * @addr: MMIO pointer to unmap.
167 static void ocxlflash_psa_unmap(void __iomem
*addr
)
173 * ocxlflash_process_element() - get process element of the adapter context
174 * @ctx_cookie: Adapter context associated with the process element.
176 * Return: process element of the adapter context
178 static int ocxlflash_process_element(void *ctx_cookie
)
180 struct ocxlflash_context
*ctx
= ctx_cookie
;
186 * afu_map_irq() - map the interrupt of the adapter context
188 * @ctx: Adapter context.
189 * @num: Per-context AFU interrupt number.
190 * @handler: Interrupt handler to register.
191 * @cookie: Interrupt handler private data.
192 * @name: Name of the interrupt.
194 * Return: 0 on success, -errno on failure
196 static int afu_map_irq(u64 flags
, struct ocxlflash_context
*ctx
, int num
,
197 irq_handler_t handler
, void *cookie
, char *name
)
199 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
200 struct device
*dev
= afu
->dev
;
201 struct ocxlflash_irqs
*irq
;
206 if (num
< 0 || num
>= ctx
->num_irqs
) {
207 dev_err(dev
, "%s: Interrupt %d not allocated\n", __func__
, num
);
212 irq
= &ctx
->irqs
[num
];
213 virq
= irq_create_mapping(NULL
, irq
->hwirq
);
214 if (unlikely(!virq
)) {
215 dev_err(dev
, "%s: irq_create_mapping failed\n", __func__
);
220 rc
= request_irq(virq
, handler
, 0, name
, cookie
);
222 dev_err(dev
, "%s: request_irq failed rc=%d\n", __func__
, rc
);
226 vtrig
= ioremap(irq
->ptrig
, PAGE_SIZE
);
227 if (unlikely(!vtrig
)) {
228 dev_err(dev
, "%s: Trigger page mapping failed\n", __func__
);
238 free_irq(virq
, cookie
);
240 irq_dispose_mapping(virq
);
245 * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
246 * @ctx_cookie: Adapter context.
247 * @num: Per-context AFU interrupt number.
248 * @handler: Interrupt handler to register.
249 * @cookie: Interrupt handler private data.
250 * @name: Name of the interrupt.
252 * Return: 0 on success, -errno on failure
254 static int ocxlflash_map_afu_irq(void *ctx_cookie
, int num
,
255 irq_handler_t handler
, void *cookie
,
258 return afu_map_irq(0, ctx_cookie
, num
, handler
, cookie
, name
);
262 * afu_unmap_irq() - unmap the interrupt
264 * @ctx: Adapter context.
265 * @num: Per-context AFU interrupt number.
266 * @cookie: Interrupt handler private data.
268 static void afu_unmap_irq(u64 flags
, struct ocxlflash_context
*ctx
, int num
,
271 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
272 struct device
*dev
= afu
->dev
;
273 struct ocxlflash_irqs
*irq
;
275 if (num
< 0 || num
>= ctx
->num_irqs
) {
276 dev_err(dev
, "%s: Interrupt %d not allocated\n", __func__
, num
);
280 irq
= &ctx
->irqs
[num
];
284 if (irq_find_mapping(NULL
, irq
->hwirq
)) {
285 free_irq(irq
->virq
, cookie
);
286 irq_dispose_mapping(irq
->virq
);
289 memset(irq
, 0, sizeof(*irq
));
293 * ocxlflash_unmap_afu_irq() - unmap the interrupt
294 * @ctx_cookie: Adapter context.
295 * @num: Per-context AFU interrupt number.
296 * @cookie: Interrupt handler private data.
298 static void ocxlflash_unmap_afu_irq(void *ctx_cookie
, int num
, void *cookie
)
300 return afu_unmap_irq(0, ctx_cookie
, num
, cookie
);
304 * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
305 * @ctx_cookie: Context associated with the interrupt.
306 * @irq: Interrupt number.
308 * Return: effective address of the mapped region
310 static u64
ocxlflash_get_irq_objhndl(void *ctx_cookie
, int irq
)
312 struct ocxlflash_context
*ctx
= ctx_cookie
;
314 if (irq
< 0 || irq
>= ctx
->num_irqs
)
317 return (__force u64
)ctx
->irqs
[irq
].vtrig
;
321 * ocxlflash_xsl_fault() - callback when translation error is triggered
322 * @data: Private data provided at callback registration, the context.
323 * @addr: Address that triggered the error.
324 * @dsisr: Value of dsisr register.
326 static void ocxlflash_xsl_fault(void *data
, u64 addr
, u64 dsisr
)
328 struct ocxlflash_context
*ctx
= data
;
330 spin_lock(&ctx
->slock
);
331 ctx
->fault_addr
= addr
;
332 ctx
->fault_dsisr
= dsisr
;
333 ctx
->pending_fault
= true;
334 spin_unlock(&ctx
->slock
);
336 wake_up_all(&ctx
->wq
);
340 * start_context() - local routine to start a context
341 * @ctx: Adapter context to be started.
343 * Assign the context specific MMIO space, add and enable the PE.
345 * Return: 0 on success, -errno on failure
347 static int start_context(struct ocxlflash_context
*ctx
)
349 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
350 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
351 void *link_token
= afu
->link_token
;
352 struct device
*dev
= afu
->dev
;
353 bool master
= ctx
->master
;
354 struct mm_struct
*mm
;
358 mutex_lock(&ctx
->state_mutex
);
359 if (ctx
->state
!= OPENED
) {
360 dev_err(dev
, "%s: Context state invalid, state=%d\n",
361 __func__
, ctx
->state
);
367 ctx
->psn_size
= acfg
->global_mmio_size
;
368 ctx
->psn_phys
= afu
->gmmio_phys
;
370 ctx
->psn_size
= acfg
->pp_mmio_stride
;
371 ctx
->psn_phys
= afu
->ppmmio_phys
+ (ctx
->pe
* ctx
->psn_size
);
374 /* pid and mm not set for master contexts */
379 pid
= current
->mm
->context
.id
;
383 rc
= ocxl_link_add_pe(link_token
, ctx
->pe
, pid
, 0, 0, mm
,
384 ocxlflash_xsl_fault
, ctx
);
386 dev_err(dev
, "%s: ocxl_link_add_pe failed rc=%d\n",
391 ctx
->state
= STARTED
;
393 mutex_unlock(&ctx
->state_mutex
);
398 * ocxlflash_start_context() - start a kernel context
399 * @ctx_cookie: Adapter context to be started.
401 * Return: 0 on success, -errno on failure
403 static int ocxlflash_start_context(void *ctx_cookie
)
405 struct ocxlflash_context
*ctx
= ctx_cookie
;
407 return start_context(ctx
);
411 * ocxlflash_stop_context() - stop a context
412 * @ctx_cookie: Adapter context to be stopped.
414 * Return: 0 on success, -errno on failure
416 static int ocxlflash_stop_context(void *ctx_cookie
)
418 struct ocxlflash_context
*ctx
= ctx_cookie
;
419 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
420 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
421 struct pci_dev
*pdev
= afu
->pdev
;
422 struct device
*dev
= afu
->dev
;
423 enum ocxlflash_ctx_state state
;
426 mutex_lock(&ctx
->state_mutex
);
429 mutex_unlock(&ctx
->state_mutex
);
430 if (state
!= STARTED
)
433 rc
= ocxl_config_terminate_pasid(pdev
, acfg
->dvsec_afu_control_pos
,
436 dev_err(dev
, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
438 /* If EBUSY, PE could be referenced in future by the AFU */
443 rc
= ocxl_link_remove_pe(afu
->link_token
, ctx
->pe
);
445 dev_err(dev
, "%s: ocxl_link_remove_pe failed rc=%d\n",
454 * ocxlflash_afu_reset() - reset the AFU
455 * @ctx_cookie: Adapter context.
457 static int ocxlflash_afu_reset(void *ctx_cookie
)
459 struct ocxlflash_context
*ctx
= ctx_cookie
;
460 struct device
*dev
= ctx
->hw_afu
->dev
;
462 /* Pending implementation from OCXL transport services */
463 dev_err_once(dev
, "%s: afu_reset() fop not supported\n", __func__
);
465 /* Silently return success until it is implemented */
470 * ocxlflash_set_master() - sets the context as master
471 * @ctx_cookie: Adapter context to set as master.
473 static void ocxlflash_set_master(void *ctx_cookie
)
475 struct ocxlflash_context
*ctx
= ctx_cookie
;
481 * ocxlflash_get_context() - obtains the context associated with the host
482 * @pdev: PCI device associated with the host.
483 * @afu_cookie: Hardware AFU associated with the host.
485 * Return: returns the pointer to host adapter context
487 static void *ocxlflash_get_context(struct pci_dev
*pdev
, void *afu_cookie
)
489 struct ocxl_hw_afu
*afu
= afu_cookie
;
491 return afu
->ocxl_ctx
;
495 * ocxlflash_dev_context_init() - allocate and initialize an adapter context
496 * @pdev: PCI device associated with the host.
497 * @afu_cookie: Hardware AFU associated with the host.
499 * Return: returns the adapter context on success, ERR_PTR on failure
501 static void *ocxlflash_dev_context_init(struct pci_dev
*pdev
, void *afu_cookie
)
503 struct ocxl_hw_afu
*afu
= afu_cookie
;
504 struct device
*dev
= afu
->dev
;
505 struct ocxlflash_context
*ctx
;
508 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
509 if (unlikely(!ctx
)) {
510 dev_err(dev
, "%s: Context allocation failed\n", __func__
);
515 idr_preload(GFP_KERNEL
);
516 rc
= idr_alloc(&afu
->idr
, ctx
, 0, afu
->max_pasid
, GFP_NOWAIT
);
518 if (unlikely(rc
< 0)) {
519 dev_err(dev
, "%s: idr_alloc failed rc=%d\n", __func__
, rc
);
523 spin_lock_init(&ctx
->slock
);
524 init_waitqueue_head(&ctx
->wq
);
525 mutex_init(&ctx
->state_mutex
);
533 ctx
->pending_irq
= false;
534 ctx
->pending_fault
= false;
545 * ocxlflash_release_context() - releases an adapter context
546 * @ctx_cookie: Adapter context to be released.
548 * Return: 0 on success, -errno on failure
550 static int ocxlflash_release_context(void *ctx_cookie
)
552 struct ocxlflash_context
*ctx
= ctx_cookie
;
559 dev
= ctx
->hw_afu
->dev
;
560 mutex_lock(&ctx
->state_mutex
);
561 if (ctx
->state
>= STARTED
) {
562 dev_err(dev
, "%s: Context in use, state=%d\n", __func__
,
564 mutex_unlock(&ctx
->state_mutex
);
568 mutex_unlock(&ctx
->state_mutex
);
570 idr_remove(&ctx
->hw_afu
->idr
, ctx
->pe
);
571 ocxlflash_release_mapping(ctx
);
578 * ocxlflash_perst_reloads_same_image() - sets the image reload policy
579 * @afu_cookie: Hardware AFU associated with the host.
580 * @image: Whether to load the same image on PERST.
582 static void ocxlflash_perst_reloads_same_image(void *afu_cookie
, bool image
)
584 struct ocxl_hw_afu
*afu
= afu_cookie
;
586 afu
->perst_same_image
= image
;
590 * ocxlflash_read_adapter_vpd() - reads the adapter VPD
591 * @pdev: PCI device associated with the host.
592 * @buf: Buffer to get the VPD data.
593 * @count: Size of buffer (maximum bytes that can be read).
595 * Return: size of VPD on success, -errno on failure
597 static ssize_t
ocxlflash_read_adapter_vpd(struct pci_dev
*pdev
, void *buf
,
600 return pci_read_vpd(pdev
, 0, count
, buf
);
604 * free_afu_irqs() - internal service to free interrupts
605 * @ctx: Adapter context.
607 static void free_afu_irqs(struct ocxlflash_context
*ctx
)
609 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
610 struct device
*dev
= afu
->dev
;
614 dev_err(dev
, "%s: Interrupts not allocated\n", __func__
);
618 for (i
= ctx
->num_irqs
; i
>= 0; i
--)
619 ocxl_link_free_irq(afu
->link_token
, ctx
->irqs
[i
].hwirq
);
626 * alloc_afu_irqs() - internal service to allocate interrupts
627 * @ctx: Context associated with the request.
628 * @num: Number of interrupts requested.
630 * Return: 0 on success, -errno on failure
632 static int alloc_afu_irqs(struct ocxlflash_context
*ctx
, int num
)
634 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
635 struct device
*dev
= afu
->dev
;
636 struct ocxlflash_irqs
*irqs
;
643 dev_err(dev
, "%s: Interrupts already allocated\n", __func__
);
648 if (num
> OCXL_MAX_IRQS
) {
649 dev_err(dev
, "%s: Too many interrupts num=%d\n", __func__
, num
);
654 irqs
= kcalloc(num
, sizeof(*irqs
), GFP_KERNEL
);
655 if (unlikely(!irqs
)) {
656 dev_err(dev
, "%s: Context irqs allocation failed\n", __func__
);
661 for (i
= 0; i
< num
; i
++) {
662 rc
= ocxl_link_irq_alloc(afu
->link_token
, &hwirq
, &addr
);
664 dev_err(dev
, "%s: ocxl_link_irq_alloc failed rc=%d\n",
669 irqs
[i
].hwirq
= hwirq
;
670 irqs
[i
].ptrig
= addr
;
678 for (i
= i
-1; i
>= 0; i
--)
679 ocxl_link_free_irq(afu
->link_token
, irqs
[i
].hwirq
);
685 * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
686 * @ctx_cookie: Context associated with the request.
687 * @num: Number of interrupts requested.
689 * Return: 0 on success, -errno on failure
691 static int ocxlflash_allocate_afu_irqs(void *ctx_cookie
, int num
)
693 return alloc_afu_irqs(ctx_cookie
, num
);
697 * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
698 * @ctx_cookie: Adapter context.
700 static void ocxlflash_free_afu_irqs(void *ctx_cookie
)
702 free_afu_irqs(ctx_cookie
);
706 * ocxlflash_unconfig_afu() - unconfigure the AFU
707 * @afu: AFU associated with the host.
709 static void ocxlflash_unconfig_afu(struct ocxl_hw_afu
*afu
)
711 if (afu
->gmmio_virt
) {
712 iounmap(afu
->gmmio_virt
);
713 afu
->gmmio_virt
= NULL
;
718 * ocxlflash_destroy_afu() - destroy the AFU structure
719 * @afu_cookie: AFU to be freed.
721 static void ocxlflash_destroy_afu(void *afu_cookie
)
723 struct ocxl_hw_afu
*afu
= afu_cookie
;
729 ocxlflash_release_context(afu
->ocxl_ctx
);
730 idr_destroy(&afu
->idr
);
732 /* Disable the AFU */
733 pos
= afu
->acfg
.dvsec_afu_control_pos
;
734 ocxl_config_set_afu_state(afu
->pdev
, pos
, 0);
736 ocxlflash_unconfig_afu(afu
);
741 * ocxlflash_config_fn() - configure the host function
742 * @pdev: PCI device associated with the host.
743 * @afu: AFU associated with the host.
745 * Return: 0 on success, -errno on failure
747 static int ocxlflash_config_fn(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
749 struct ocxl_fn_config
*fcfg
= &afu
->fcfg
;
750 struct device
*dev
= &pdev
->dev
;
751 u16 base
, enabled
, supported
;
754 /* Read DVSEC config of the function */
755 rc
= ocxl_config_read_function(pdev
, fcfg
);
757 dev_err(dev
, "%s: ocxl_config_read_function failed rc=%d\n",
762 /* Check if function has AFUs defined, only 1 per function supported */
763 if (fcfg
->max_afu_index
>= 0) {
764 afu
->is_present
= true;
765 if (fcfg
->max_afu_index
!= 0)
766 dev_warn(dev
, "%s: Unexpected AFU index value %d\n",
767 __func__
, fcfg
->max_afu_index
);
770 rc
= ocxl_config_get_actag_info(pdev
, &base
, &enabled
, &supported
);
772 dev_err(dev
, "%s: ocxl_config_get_actag_info failed rc=%d\n",
777 afu
->fn_actag_base
= base
;
778 afu
->fn_actag_enabled
= enabled
;
780 ocxl_config_set_actag(pdev
, fcfg
->dvsec_function_pos
, base
, enabled
);
781 dev_dbg(dev
, "%s: Function acTag range base=%u enabled=%u\n",
782 __func__
, base
, enabled
);
784 rc
= ocxl_link_setup(pdev
, 0, &afu
->link_token
);
786 dev_err(dev
, "%s: ocxl_link_setup failed rc=%d\n",
791 rc
= ocxl_config_set_TL(pdev
, fcfg
->dvsec_tl_pos
);
793 dev_err(dev
, "%s: ocxl_config_set_TL failed rc=%d\n",
800 ocxl_link_release(pdev
, afu
->link_token
);
805 * ocxlflash_unconfig_fn() - unconfigure the host function
806 * @pdev: PCI device associated with the host.
807 * @afu: AFU associated with the host.
809 static void ocxlflash_unconfig_fn(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
811 ocxl_link_release(pdev
, afu
->link_token
);
815 * ocxlflash_map_mmio() - map the AFU MMIO space
816 * @afu: AFU associated with the host.
818 * Return: 0 on success, -errno on failure
820 static int ocxlflash_map_mmio(struct ocxl_hw_afu
*afu
)
822 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
823 struct pci_dev
*pdev
= afu
->pdev
;
824 struct device
*dev
= afu
->dev
;
825 phys_addr_t gmmio
, ppmmio
;
828 rc
= pci_request_region(pdev
, acfg
->global_mmio_bar
, "ocxlflash");
830 dev_err(dev
, "%s: pci_request_region for global failed rc=%d\n",
834 gmmio
= pci_resource_start(pdev
, acfg
->global_mmio_bar
);
835 gmmio
+= acfg
->global_mmio_offset
;
837 rc
= pci_request_region(pdev
, acfg
->pp_mmio_bar
, "ocxlflash");
839 dev_err(dev
, "%s: pci_request_region for pp bar failed rc=%d\n",
843 ppmmio
= pci_resource_start(pdev
, acfg
->pp_mmio_bar
);
844 ppmmio
+= acfg
->pp_mmio_offset
;
846 afu
->gmmio_virt
= ioremap(gmmio
, acfg
->global_mmio_size
);
847 if (unlikely(!afu
->gmmio_virt
)) {
848 dev_err(dev
, "%s: MMIO mapping failed\n", __func__
);
853 afu
->gmmio_phys
= gmmio
;
854 afu
->ppmmio_phys
= ppmmio
;
858 pci_release_region(pdev
, acfg
->pp_mmio_bar
);
860 pci_release_region(pdev
, acfg
->global_mmio_bar
);
865 * ocxlflash_config_afu() - configure the host AFU
866 * @pdev: PCI device associated with the host.
867 * @afu: AFU associated with the host.
869 * Must be called _after_ host function configuration.
871 * Return: 0 on success, -errno on failure
873 static int ocxlflash_config_afu(struct pci_dev
*pdev
, struct ocxl_hw_afu
*afu
)
875 struct ocxl_afu_config
*acfg
= &afu
->acfg
;
876 struct ocxl_fn_config
*fcfg
= &afu
->fcfg
;
877 struct device
*dev
= &pdev
->dev
;
883 /* This HW AFU function does not have any AFUs defined */
884 if (!afu
->is_present
)
887 /* Read AFU config at index 0 */
888 rc
= ocxl_config_read_afu(pdev
, fcfg
, acfg
, 0);
890 dev_err(dev
, "%s: ocxl_config_read_afu failed rc=%d\n",
895 /* Only one AFU per function is supported, so actag_base is same */
896 base
= afu
->fn_actag_base
;
897 count
= min_t(int, acfg
->actag_supported
, afu
->fn_actag_enabled
);
898 pos
= acfg
->dvsec_afu_control_pos
;
900 ocxl_config_set_afu_actag(pdev
, pos
, base
, count
);
901 dev_dbg(dev
, "%s: acTag base=%d enabled=%d\n", __func__
, base
, count
);
902 afu
->afu_actag_base
= base
;
903 afu
->afu_actag_enabled
= count
;
904 afu
->max_pasid
= 1 << acfg
->pasid_supported_log
;
906 ocxl_config_set_afu_pasid(pdev
, pos
, 0, acfg
->pasid_supported_log
);
908 rc
= ocxlflash_map_mmio(afu
);
910 dev_err(dev
, "%s: ocxlflash_map_mmio failed rc=%d\n",
916 ocxl_config_set_afu_state(pdev
, acfg
->dvsec_afu_control_pos
, 1);
922 * ocxlflash_create_afu() - create the AFU for OCXL
923 * @pdev: PCI device associated with the host.
925 * Return: AFU on success, NULL on failure
927 static void *ocxlflash_create_afu(struct pci_dev
*pdev
)
929 struct device
*dev
= &pdev
->dev
;
930 struct ocxlflash_context
*ctx
;
931 struct ocxl_hw_afu
*afu
;
934 afu
= kzalloc(sizeof(*afu
), GFP_KERNEL
);
935 if (unlikely(!afu
)) {
936 dev_err(dev
, "%s: HW AFU allocation failed\n", __func__
);
944 rc
= ocxlflash_config_fn(pdev
, afu
);
946 dev_err(dev
, "%s: Function configuration failed rc=%d\n",
951 rc
= ocxlflash_config_afu(pdev
, afu
);
953 dev_err(dev
, "%s: AFU configuration failed rc=%d\n",
958 ctx
= ocxlflash_dev_context_init(pdev
, afu
);
961 dev_err(dev
, "%s: ocxlflash_dev_context_init failed rc=%d\n",
970 ocxlflash_unconfig_afu(afu
);
972 ocxlflash_unconfig_fn(pdev
, afu
);
974 idr_destroy(&afu
->idr
);
981 * ctx_event_pending() - check for any event pending on the context
982 * @ctx: Context to be checked.
984 * Return: true if there is an event pending, false if none pending
986 static inline bool ctx_event_pending(struct ocxlflash_context
*ctx
)
988 if (ctx
->pending_irq
|| ctx
->pending_fault
)
995 * afu_poll() - poll the AFU for events on the context
996 * @file: File associated with the adapter context.
997 * @poll: Poll structure from the user.
1001 static unsigned int afu_poll(struct file
*file
, struct poll_table_struct
*poll
)
1003 struct ocxlflash_context
*ctx
= file
->private_data
;
1004 struct device
*dev
= ctx
->hw_afu
->dev
;
1008 poll_wait(file
, &ctx
->wq
, poll
);
1010 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1011 if (ctx_event_pending(ctx
))
1012 mask
|= POLLIN
| POLLRDNORM
;
1013 else if (ctx
->state
== CLOSED
)
1015 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1017 dev_dbg(dev
, "%s: Poll wait completed for pe %i mask %i\n",
1018 __func__
, ctx
->pe
, mask
);
1024 * afu_read() - perform a read on the context for any event
1025 * @file: File associated with the adapter context.
1026 * @buf: Buffer to receive the data.
1027 * @count: Size of buffer (maximum bytes that can be read).
1030 * Return: size of the data read on success, -errno on failure
1032 static ssize_t
afu_read(struct file
*file
, char __user
*buf
, size_t count
,
1035 struct ocxlflash_context
*ctx
= file
->private_data
;
1036 struct device
*dev
= ctx
->hw_afu
->dev
;
1037 struct cxl_event event
;
1042 DEFINE_WAIT(event_wait
);
1045 dev_err(dev
, "%s: Non-zero offset not supported, off=%lld\n",
1051 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1054 prepare_to_wait(&ctx
->wq
, &event_wait
, TASK_INTERRUPTIBLE
);
1056 if (ctx_event_pending(ctx
) || (ctx
->state
== CLOSED
))
1059 if (file
->f_flags
& O_NONBLOCK
) {
1060 dev_err(dev
, "%s: File cannot be blocked on I/O\n",
1066 if (signal_pending(current
)) {
1067 dev_err(dev
, "%s: Signal pending on the process\n",
1073 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1075 spin_lock_irqsave(&ctx
->slock
, lock_flags
);
1078 finish_wait(&ctx
->wq
, &event_wait
);
1080 memset(&event
, 0, sizeof(event
));
1081 event
.header
.process_element
= ctx
->pe
;
1082 event
.header
.size
= sizeof(struct cxl_event_header
);
1083 if (ctx
->pending_irq
) {
1084 esize
= sizeof(struct cxl_event_afu_interrupt
);
1085 event
.header
.size
+= esize
;
1086 event
.header
.type
= CXL_EVENT_AFU_INTERRUPT
;
1088 bit
= find_first_bit(&ctx
->irq_bitmap
, ctx
->num_irqs
);
1089 clear_bit(bit
, &ctx
->irq_bitmap
);
1090 event
.irq
.irq
= bit
+ 1;
1091 if (bitmap_empty(&ctx
->irq_bitmap
, ctx
->num_irqs
))
1092 ctx
->pending_irq
= false;
1093 } else if (ctx
->pending_fault
) {
1094 event
.header
.size
+= sizeof(struct cxl_event_data_storage
);
1095 event
.header
.type
= CXL_EVENT_DATA_STORAGE
;
1096 event
.fault
.addr
= ctx
->fault_addr
;
1097 event
.fault
.dsisr
= ctx
->fault_dsisr
;
1098 ctx
->pending_fault
= false;
1101 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1103 if (copy_to_user(buf
, &event
, event
.header
.size
)) {
1104 dev_err(dev
, "%s: copy_to_user failed\n", __func__
);
1109 rc
= event
.header
.size
;
1113 finish_wait(&ctx
->wq
, &event_wait
);
1114 spin_unlock_irqrestore(&ctx
->slock
, lock_flags
);
1119 * afu_release() - release and free the context
1120 * @inode: File inode pointer.
1121 * @file: File associated with the context.
1123 * Return: 0 on success, -errno on failure
1125 static int afu_release(struct inode
*inode
, struct file
*file
)
1127 struct ocxlflash_context
*ctx
= file
->private_data
;
1130 /* Unmap and free the interrupts associated with the context */
1131 for (i
= ctx
->num_irqs
; i
>= 0; i
--)
1132 afu_unmap_irq(0, ctx
, i
, ctx
);
1135 return ocxlflash_release_context(ctx
);
1139 * ocxlflash_mmap_fault() - mmap fault handler
1140 * @vmf: VM fault associated with current fault.
1142 * Return: 0 on success, -errno on failure
1144 static vm_fault_t
ocxlflash_mmap_fault(struct vm_fault
*vmf
)
1146 struct vm_area_struct
*vma
= vmf
->vma
;
1147 struct ocxlflash_context
*ctx
= vma
->vm_file
->private_data
;
1148 struct device
*dev
= ctx
->hw_afu
->dev
;
1149 u64 mmio_area
, offset
;
1151 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
1152 if (offset
>= ctx
->psn_size
)
1153 return VM_FAULT_SIGBUS
;
1155 mutex_lock(&ctx
->state_mutex
);
1156 if (ctx
->state
!= STARTED
) {
1157 dev_err(dev
, "%s: Context not started, state=%d\n",
1158 __func__
, ctx
->state
);
1159 mutex_unlock(&ctx
->state_mutex
);
1160 return VM_FAULT_SIGBUS
;
1162 mutex_unlock(&ctx
->state_mutex
);
1164 mmio_area
= ctx
->psn_phys
;
1165 mmio_area
+= offset
;
1167 return vmf_insert_pfn(vma
, vmf
->address
, mmio_area
>> PAGE_SHIFT
);
1170 static const struct vm_operations_struct ocxlflash_vmops
= {
1171 .fault
= ocxlflash_mmap_fault
,
1175 * afu_mmap() - map the fault handler operations
1176 * @file: File associated with the context.
1177 * @vma: VM area associated with mapping.
1179 * Return: 0 on success, -errno on failure
1181 static int afu_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1183 struct ocxlflash_context
*ctx
= file
->private_data
;
1185 if ((vma_pages(vma
) + vma
->vm_pgoff
) >
1186 (ctx
->psn_size
>> PAGE_SHIFT
))
1189 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
;
1190 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1191 vma
->vm_ops
= &ocxlflash_vmops
;
1195 static const struct file_operations ocxl_afu_fops
= {
1196 .owner
= THIS_MODULE
,
1199 .release
= afu_release
,
1203 #define PATCH_FOPS(NAME) \
1204 do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1207 * ocxlflash_get_fd() - get file descriptor for an adapter context
1208 * @ctx_cookie: Adapter context.
1209 * @fops: File operations to be associated.
1210 * @fd: File descriptor to be returned back.
1212 * Return: pointer to the file on success, ERR_PTR on failure
1214 static struct file
*ocxlflash_get_fd(void *ctx_cookie
,
1215 struct file_operations
*fops
, int *fd
)
1217 struct ocxlflash_context
*ctx
= ctx_cookie
;
1218 struct device
*dev
= ctx
->hw_afu
->dev
;
1224 /* Only allow one fd per context */
1226 dev_err(dev
, "%s: Context is already mapped to an fd\n",
1232 flags
= O_RDWR
| O_CLOEXEC
;
1234 /* This code is similar to anon_inode_getfd() */
1235 rc
= get_unused_fd_flags(flags
);
1236 if (unlikely(rc
< 0)) {
1237 dev_err(dev
, "%s: get_unused_fd_flags failed rc=%d\n",
1243 /* Patch the file ops that are not defined */
1247 PATCH_FOPS(release
);
1249 } else /* Use default ops */
1250 fops
= (struct file_operations
*)&ocxl_afu_fops
;
1252 name
= kasprintf(GFP_KERNEL
, "ocxlflash:%d", ctx
->pe
);
1253 file
= ocxlflash_getfile(dev
, name
, fops
, ctx
, flags
);
1257 dev_err(dev
, "%s: ocxlflash_getfile failed rc=%d\n",
1262 ctx
->mapping
= file
->f_mapping
;
1267 put_unused_fd(fdtmp
);
1274 * ocxlflash_fops_get_context() - get the context associated with the file
1275 * @file: File associated with the adapter context.
1277 * Return: pointer to the context
1279 static void *ocxlflash_fops_get_context(struct file
*file
)
1281 return file
->private_data
;
1285 * ocxlflash_afu_irq() - interrupt handler for user contexts
1286 * @irq: Interrupt number.
1287 * @data: Private data provided at interrupt registration, the context.
1289 * Return: Always return IRQ_HANDLED.
1291 static irqreturn_t
ocxlflash_afu_irq(int irq
, void *data
)
1293 struct ocxlflash_context
*ctx
= data
;
1294 struct device
*dev
= ctx
->hw_afu
->dev
;
1297 dev_dbg(dev
, "%s: Interrupt raised for pe %i virq %i\n",
1298 __func__
, ctx
->pe
, irq
);
1300 for (i
= 0; i
< ctx
->num_irqs
; i
++) {
1301 if (ctx
->irqs
[i
].virq
== irq
)
1304 if (unlikely(i
>= ctx
->num_irqs
)) {
1305 dev_err(dev
, "%s: Received AFU IRQ out of range\n", __func__
);
1309 spin_lock(&ctx
->slock
);
1310 set_bit(i
- 1, &ctx
->irq_bitmap
);
1311 ctx
->pending_irq
= true;
1312 spin_unlock(&ctx
->slock
);
1314 wake_up_all(&ctx
->wq
);
1320 * ocxlflash_start_work() - start a user context
1321 * @ctx_cookie: Context to be started.
1322 * @num_irqs: Number of interrupts requested.
1324 * Return: 0 on success, -errno on failure
1326 static int ocxlflash_start_work(void *ctx_cookie
, u64 num_irqs
)
1328 struct ocxlflash_context
*ctx
= ctx_cookie
;
1329 struct ocxl_hw_afu
*afu
= ctx
->hw_afu
;
1330 struct device
*dev
= afu
->dev
;
1335 rc
= alloc_afu_irqs(ctx
, num_irqs
);
1336 if (unlikely(rc
< 0)) {
1337 dev_err(dev
, "%s: alloc_afu_irqs failed rc=%d\n", __func__
, rc
);
1341 for (i
= 0; i
< num_irqs
; i
++) {
1342 name
= kasprintf(GFP_KERNEL
, "ocxlflash-%s-pe%i-%i",
1343 dev_name(dev
), ctx
->pe
, i
);
1344 rc
= afu_map_irq(0, ctx
, i
, ocxlflash_afu_irq
, ctx
, name
);
1346 if (unlikely(rc
< 0)) {
1347 dev_err(dev
, "%s: afu_map_irq failed rc=%d\n",
1353 rc
= start_context(ctx
);
1355 dev_err(dev
, "%s: start_context failed rc=%d\n", __func__
, rc
);
1361 for (i
= i
-1; i
>= 0; i
--)
1362 afu_unmap_irq(0, ctx
, i
, ctx
);
1368 * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
1369 * @file: File installed with adapter file descriptor.
1370 * @vma: VM area associated with mapping.
1372 * Return: 0 on success, -errno on failure
1374 static int ocxlflash_fd_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1376 return afu_mmap(file
, vma
);
1380 * ocxlflash_fd_release() - release the context associated with the file
1381 * @inode: File inode pointer.
1382 * @file: File associated with the adapter context.
1384 * Return: 0 on success, -errno on failure
1386 static int ocxlflash_fd_release(struct inode
*inode
, struct file
*file
)
1388 return afu_release(inode
, file
);
1391 /* Backend ops to ocxlflash services */
1392 const struct cxlflash_backend_ops cxlflash_ocxl_ops
= {
1393 .module
= THIS_MODULE
,
1394 .psa_map
= ocxlflash_psa_map
,
1395 .psa_unmap
= ocxlflash_psa_unmap
,
1396 .process_element
= ocxlflash_process_element
,
1397 .map_afu_irq
= ocxlflash_map_afu_irq
,
1398 .unmap_afu_irq
= ocxlflash_unmap_afu_irq
,
1399 .get_irq_objhndl
= ocxlflash_get_irq_objhndl
,
1400 .start_context
= ocxlflash_start_context
,
1401 .stop_context
= ocxlflash_stop_context
,
1402 .afu_reset
= ocxlflash_afu_reset
,
1403 .set_master
= ocxlflash_set_master
,
1404 .get_context
= ocxlflash_get_context
,
1405 .dev_context_init
= ocxlflash_dev_context_init
,
1406 .release_context
= ocxlflash_release_context
,
1407 .perst_reloads_same_image
= ocxlflash_perst_reloads_same_image
,
1408 .read_adapter_vpd
= ocxlflash_read_adapter_vpd
,
1409 .allocate_afu_irqs
= ocxlflash_allocate_afu_irqs
,
1410 .free_afu_irqs
= ocxlflash_free_afu_irqs
,
1411 .create_afu
= ocxlflash_create_afu
,
1412 .destroy_afu
= ocxlflash_destroy_afu
,
1413 .get_fd
= ocxlflash_get_fd
,
1414 .fops_get_context
= ocxlflash_fops_get_context
,
1415 .start_work
= ocxlflash_start_work
,
1416 .fd_mmap
= ocxlflash_fd_mmap
,
1417 .fd_release
= ocxlflash_fd_release
,