blob: f77f4d7f6a34f76b7c651ae22ccd21452736c962 [file] [log] [blame]
Uma Krishnan76ebe012018-03-26 11:30:51 -05001/*
2 * CXL Flash Device Driver
3 *
4 * Written by: Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
5 * Uma Krishnan <ukrishn@linux.vnet.ibm.com>, IBM Corporation
6 *
7 * Copyright (C) 2018 IBM Corporation
8 *
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.
13 */
14
Uma Krishnan926a62f2018-03-26 11:32:09 -050015#include <linux/file.h>
Uma Krishnan429ebfa2018-03-26 11:32:00 -050016#include <linux/idr.h>
Uma Krishnan926a62f2018-03-26 11:32:09 -050017#include <linux/module.h>
18#include <linux/mount.h>
Uma Krishnan56f1db12018-03-26 11:34:03 -050019#include <linux/poll.h>
Uma Krishnan03aa9c52018-03-26 11:34:12 -050020#include <linux/sched/signal.h>
Uma Krishnan429ebfa2018-03-26 11:32:00 -050021
Uma Krishnan76ebe012018-03-26 11:30:51 -050022#include <misc/ocxl.h>
23
Uma Krishnan03aa9c52018-03-26 11:34:12 -050024#include <uapi/misc/cxl.h>
25
Uma Krishnan76ebe012018-03-26 11:30:51 -050026#include "backend.h"
Uma Krishnan48e077d2018-03-26 11:31:01 -050027#include "ocxl_hw.h"
28
Uma Krishnan926a62f2018-03-26 11:32:09 -050029/*
30 * Pseudo-filesystem to allocate inodes.
31 */
32
33#define OCXLFLASH_FS_MAGIC 0x1697698f
34
35static int ocxlflash_fs_cnt;
36static struct vfsmount *ocxlflash_vfs_mount;
37
38static const struct dentry_operations ocxlflash_fs_dops = {
39 .d_dname = simple_dname,
40};
41
42/*
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.
48 *
49 * Return: pointer to the directory entry structure
50 */
51static struct dentry *ocxlflash_fs_mount(struct file_system_type *fs_type,
52 int flags, const char *dev_name,
53 void *data)
54{
55 return mount_pseudo(fs_type, "ocxlflash:", NULL, &ocxlflash_fs_dops,
56 OCXLFLASH_FS_MAGIC);
57}
58
59static struct file_system_type ocxlflash_fs_type = {
60 .name = "ocxlflash",
61 .owner = THIS_MODULE,
62 .mount = ocxlflash_fs_mount,
63 .kill_sb = kill_anon_super,
64};
65
66/*
67 * ocxlflash_release_mapping() - release the memory mapping
68 * @ctx: Context whose mapping is to be released.
69 */
70static void ocxlflash_release_mapping(struct ocxlflash_context *ctx)
71{
72 if (ctx->mapping)
73 simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
74 ctx->mapping = NULL;
75}
76
77/*
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.
84 *
85 * Return: pointer to the file on success, ERR_PTR on failure
86 */
87static struct file *ocxlflash_getfile(struct device *dev, const char *name,
88 const struct file_operations *fops,
89 void *priv, int flags)
90{
91 struct qstr this;
92 struct path path;
93 struct file *file;
94 struct inode *inode = NULL;
95 int rc;
96
97 if (fops->owner && !try_module_get(fops->owner)) {
98 dev_err(dev, "%s: Owner does not exist\n", __func__);
99 rc = -ENOENT;
100 goto err1;
101 }
102
103 rc = simple_pin_fs(&ocxlflash_fs_type, &ocxlflash_vfs_mount,
104 &ocxlflash_fs_cnt);
105 if (unlikely(rc < 0)) {
106 dev_err(dev, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
107 __func__, rc);
108 goto err2;
109 }
110
111 inode = alloc_anon_inode(ocxlflash_vfs_mount->mnt_sb);
112 if (IS_ERR(inode)) {
113 rc = PTR_ERR(inode);
114 dev_err(dev, "%s: alloc_anon_inode failed rc=%d\n",
115 __func__, rc);
116 goto err3;
117 }
118
119 this.name = name;
120 this.len = strlen(name);
121 this.hash = 0;
122 path.dentry = d_alloc_pseudo(ocxlflash_vfs_mount->mnt_sb, &this);
123 if (!path.dentry) {
124 dev_err(dev, "%s: d_alloc_pseudo failed\n", __func__);
125 rc = -ENOMEM;
126 goto err4;
127 }
128
129 path.mnt = mntget(ocxlflash_vfs_mount);
130 d_instantiate(path.dentry, inode);
131
132 file = alloc_file(&path, OPEN_FMODE(flags), fops);
133 if (IS_ERR(file)) {
134 rc = PTR_ERR(file);
135 dev_err(dev, "%s: alloc_file failed rc=%d\n",
136 __func__, rc);
137 goto err5;
138 }
139
140 file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
141 file->private_data = priv;
142out:
143 return file;
144err5:
145 path_put(&path);
146err4:
147 iput(inode);
148err3:
149 simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
150err2:
151 module_put(fops->owner);
152err1:
153 file = ERR_PTR(rc);
154 goto out;
155}
156
Uma Krishnan48e077d2018-03-26 11:31:01 -0500157/**
Uma Krishnan012f3942018-03-26 11:32:56 -0500158 * ocxlflash_psa_map() - map the process specific MMIO space
159 * @ctx_cookie: Adapter context for which the mapping needs to be done.
160 *
161 * Return: MMIO pointer of the mapped region
162 */
163static void __iomem *ocxlflash_psa_map(void *ctx_cookie)
164{
165 struct ocxlflash_context *ctx = ctx_cookie;
166
167 return ioremap(ctx->psn_phys, ctx->psn_size);
168}
169
170/**
171 * ocxlflash_psa_unmap() - unmap the process specific MMIO space
172 * @addr: MMIO pointer to unmap.
173 */
174static void ocxlflash_psa_unmap(void __iomem *addr)
175{
176 iounmap(addr);
177}
178
179/**
Uma Krishnanb18718c2018-03-26 11:32:20 -0500180 * ocxlflash_process_element() - get process element of the adapter context
181 * @ctx_cookie: Adapter context associated with the process element.
182 *
183 * Return: process element of the adapter context
184 */
185static int ocxlflash_process_element(void *ctx_cookie)
186{
187 struct ocxlflash_context *ctx = ctx_cookie;
188
189 return ctx->pe;
190}
191
192/**
Uma Krishnana06b1cf2018-03-26 11:33:48 -0500193 * afu_map_irq() - map the interrupt of the adapter context
194 * @flags: Flags.
195 * @ctx: Adapter context.
196 * @num: Per-context AFU interrupt number.
197 * @handler: Interrupt handler to register.
198 * @cookie: Interrupt handler private data.
199 * @name: Name of the interrupt.
200 *
201 * Return: 0 on success, -errno on failure
202 */
203static int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
204 irq_handler_t handler, void *cookie, char *name)
205{
206 struct ocxl_hw_afu *afu = ctx->hw_afu;
207 struct device *dev = afu->dev;
208 struct ocxlflash_irqs *irq;
209 void __iomem *vtrig;
210 u32 virq;
211 int rc = 0;
212
213 if (num < 0 || num >= ctx->num_irqs) {
214 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
215 rc = -ENOENT;
216 goto out;
217 }
218
219 irq = &ctx->irqs[num];
220 virq = irq_create_mapping(NULL, irq->hwirq);
221 if (unlikely(!virq)) {
222 dev_err(dev, "%s: irq_create_mapping failed\n", __func__);
223 rc = -ENOMEM;
224 goto out;
225 }
226
227 rc = request_irq(virq, handler, 0, name, cookie);
228 if (unlikely(rc)) {
229 dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
230 goto err1;
231 }
232
233 vtrig = ioremap(irq->ptrig, PAGE_SIZE);
234 if (unlikely(!vtrig)) {
235 dev_err(dev, "%s: Trigger page mapping failed\n", __func__);
236 rc = -ENOMEM;
237 goto err2;
238 }
239
240 irq->virq = virq;
241 irq->vtrig = vtrig;
242out:
243 return rc;
244err2:
245 free_irq(virq, cookie);
246err1:
247 irq_dispose_mapping(virq);
248 goto out;
249}
250
251/**
252 * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
253 * @ctx_cookie: Adapter context.
254 * @num: Per-context AFU interrupt number.
255 * @handler: Interrupt handler to register.
256 * @cookie: Interrupt handler private data.
257 * @name: Name of the interrupt.
258 *
259 * Return: 0 on success, -errno on failure
260 */
261static int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
262 irq_handler_t handler, void *cookie,
263 char *name)
264{
265 return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
266}
267
268/**
269 * afu_unmap_irq() - unmap the interrupt
270 * @flags: Flags.
271 * @ctx: Adapter context.
272 * @num: Per-context AFU interrupt number.
273 * @cookie: Interrupt handler private data.
274 */
275static void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
276 void *cookie)
277{
278 struct ocxl_hw_afu *afu = ctx->hw_afu;
279 struct device *dev = afu->dev;
280 struct ocxlflash_irqs *irq;
281
282 if (num < 0 || num >= ctx->num_irqs) {
283 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
284 return;
285 }
286
287 irq = &ctx->irqs[num];
288 if (irq->vtrig)
289 iounmap(irq->vtrig);
290
291 if (irq_find_mapping(NULL, irq->hwirq)) {
292 free_irq(irq->virq, cookie);
293 irq_dispose_mapping(irq->virq);
294 }
295
296 memset(irq, 0, sizeof(*irq));
297}
298
299/**
300 * ocxlflash_unmap_afu_irq() - unmap the interrupt
301 * @ctx_cookie: Adapter context.
302 * @num: Per-context AFU interrupt number.
303 * @cookie: Interrupt handler private data.
304 */
305static void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
306{
307 return afu_unmap_irq(0, ctx_cookie, num, cookie);
308}
309
310/**
Uma Krishnan402a55e2018-03-26 11:34:35 -0500311 * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
312 * @ctx_cookie: Context associated with the interrupt.
313 * @irq: Interrupt number.
314 *
315 * Return: effective address of the mapped region
316 */
317static u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
318{
319 struct ocxlflash_context *ctx = ctx_cookie;
320
321 if (irq < 0 || irq >= ctx->num_irqs)
322 return 0;
323
324 return (__force u64)ctx->irqs[irq].vtrig;
325}
326
327/**
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500328 * start_context() - local routine to start a context
329 * @ctx: Adapter context to be started.
330 *
Uma Krishnanc207b572018-03-26 11:33:35 -0500331 * Assign the context specific MMIO space, add and enable the PE.
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500332 *
333 * Return: 0 on success, -errno on failure
334 */
335static int start_context(struct ocxlflash_context *ctx)
336{
337 struct ocxl_hw_afu *afu = ctx->hw_afu;
338 struct ocxl_afu_config *acfg = &afu->acfg;
Uma Krishnanc207b572018-03-26 11:33:35 -0500339 void *link_token = afu->link_token;
340 struct device *dev = afu->dev;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500341 bool master = ctx->master;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500342 struct mm_struct *mm;
Uma Krishnanc207b572018-03-26 11:33:35 -0500343 int rc = 0;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500344 u32 pid;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500345
346 if (master) {
347 ctx->psn_size = acfg->global_mmio_size;
348 ctx->psn_phys = afu->gmmio_phys;
349 } else {
350 ctx->psn_size = acfg->pp_mmio_stride;
351 ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
352 }
353
Uma Krishnan762c7e92018-03-26 11:33:55 -0500354 /* pid and mm not set for master contexts */
355 if (master) {
356 pid = 0;
357 mm = NULL;
358 } else {
359 pid = current->mm->context.id;
360 mm = current->mm;
361 }
Uma Krishnanc207b572018-03-26 11:33:35 -0500362
Uma Krishnan762c7e92018-03-26 11:33:55 -0500363 rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0, mm, NULL, NULL);
Uma Krishnanc207b572018-03-26 11:33:35 -0500364 if (unlikely(rc)) {
365 dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
366 __func__, rc);
367 goto out;
368 }
369out:
370 return rc;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500371}
372
373/**
374 * ocxlflash_start_context() - start a kernel context
375 * @ctx_cookie: Adapter context to be started.
376 *
377 * Return: 0 on success, -errno on failure
378 */
379static int ocxlflash_start_context(void *ctx_cookie)
380{
381 struct ocxlflash_context *ctx = ctx_cookie;
382
383 return start_context(ctx);
384}
385
386/**
Uma Krishnanc207b572018-03-26 11:33:35 -0500387 * ocxlflash_stop_context() - stop a context
388 * @ctx_cookie: Adapter context to be stopped.
389 *
390 * Return: 0 on success, -errno on failure
391 */
392static int ocxlflash_stop_context(void *ctx_cookie)
393{
394 struct ocxlflash_context *ctx = ctx_cookie;
395 struct ocxl_hw_afu *afu = ctx->hw_afu;
396 struct ocxl_afu_config *acfg = &afu->acfg;
397 struct pci_dev *pdev = afu->pdev;
398 struct device *dev = afu->dev;
399 int rc;
400
401 rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
402 ctx->pe);
403 if (unlikely(rc)) {
404 dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
405 __func__, rc);
406 /* If EBUSY, PE could be referenced in future by the AFU */
407 if (rc == -EBUSY)
408 goto out;
409 }
410
411 rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
412 if (unlikely(rc)) {
413 dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
414 __func__, rc);
415 goto out;
416 }
417out:
418 return rc;
419}
420
421/**
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500422 * ocxlflash_set_master() - sets the context as master
423 * @ctx_cookie: Adapter context to set as master.
424 */
425static void ocxlflash_set_master(void *ctx_cookie)
426{
427 struct ocxlflash_context *ctx = ctx_cookie;
428
429 ctx->master = true;
430}
431
432/**
433 * ocxlflash_get_context() - obtains the context associated with the host
434 * @pdev: PCI device associated with the host.
435 * @afu_cookie: Hardware AFU associated with the host.
436 *
437 * Return: returns the pointer to host adapter context
438 */
439static void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
440{
441 struct ocxl_hw_afu *afu = afu_cookie;
442
443 return afu->ocxl_ctx;
444}
445
446/**
447 * ocxlflash_dev_context_init() - allocate and initialize an adapter context
448 * @pdev: PCI device associated with the host.
449 * @afu_cookie: Hardware AFU associated with the host.
450 *
451 * Return: returns the adapter context on success, ERR_PTR on failure
452 */
453static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
454{
455 struct ocxl_hw_afu *afu = afu_cookie;
456 struct device *dev = afu->dev;
457 struct ocxlflash_context *ctx;
458 int rc;
459
460 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
461 if (unlikely(!ctx)) {
462 dev_err(dev, "%s: Context allocation failed\n", __func__);
463 rc = -ENOMEM;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500464 goto err1;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500465 }
466
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500467 idr_preload(GFP_KERNEL);
468 rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
469 idr_preload_end();
470 if (unlikely(rc < 0)) {
471 dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
472 goto err2;
473 }
474
Uma Krishnan762c7e92018-03-26 11:33:55 -0500475 spin_lock_init(&ctx->slock);
Uma Krishnan56f1db12018-03-26 11:34:03 -0500476 init_waitqueue_head(&ctx->wq);
Uma Krishnan762c7e92018-03-26 11:33:55 -0500477
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500478 ctx->pe = rc;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500479 ctx->master = false;
Uma Krishnan926a62f2018-03-26 11:32:09 -0500480 ctx->mapping = NULL;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500481 ctx->hw_afu = afu;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500482 ctx->irq_bitmap = 0;
483 ctx->pending_irq = false;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500484out:
485 return ctx;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500486err2:
487 kfree(ctx);
488err1:
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500489 ctx = ERR_PTR(rc);
490 goto out;
491}
492
493/**
494 * ocxlflash_release_context() - releases an adapter context
495 * @ctx_cookie: Adapter context to be released.
496 *
497 * Return: 0 on success, -errno on failure
498 */
499static int ocxlflash_release_context(void *ctx_cookie)
500{
501 struct ocxlflash_context *ctx = ctx_cookie;
502 int rc = 0;
503
504 if (!ctx)
505 goto out;
506
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500507 idr_remove(&ctx->hw_afu->idr, ctx->pe);
Uma Krishnan926a62f2018-03-26 11:32:09 -0500508 ocxlflash_release_mapping(ctx);
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500509 kfree(ctx);
510out:
511 return rc;
512}
513
514/**
Uma Krishnan8b7a5522018-03-26 11:32:29 -0500515 * ocxlflash_perst_reloads_same_image() - sets the image reload policy
516 * @afu_cookie: Hardware AFU associated with the host.
517 * @image: Whether to load the same image on PERST.
518 */
519static void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
520{
521 struct ocxl_hw_afu *afu = afu_cookie;
522
523 afu->perst_same_image = image;
524}
525
526/**
Uma Krishnan119c9202018-03-26 11:33:14 -0500527 * ocxlflash_read_adapter_vpd() - reads the adapter VPD
528 * @pdev: PCI device associated with the host.
529 * @buf: Buffer to get the VPD data.
530 * @count: Size of buffer (maximum bytes that can be read).
531 *
532 * Return: size of VPD on success, -errno on failure
533 */
534static ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
535 size_t count)
536{
537 return pci_read_vpd(pdev, 0, count, buf);
538}
539
540/**
Uma Krishnanbc65c1c2018-03-26 11:33:41 -0500541 * free_afu_irqs() - internal service to free interrupts
542 * @ctx: Adapter context.
543 */
544static void free_afu_irqs(struct ocxlflash_context *ctx)
545{
546 struct ocxl_hw_afu *afu = ctx->hw_afu;
547 struct device *dev = afu->dev;
548 int i;
549
550 if (!ctx->irqs) {
551 dev_err(dev, "%s: Interrupts not allocated\n", __func__);
552 return;
553 }
554
555 for (i = ctx->num_irqs; i >= 0; i--)
556 ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
557
558 kfree(ctx->irqs);
559 ctx->irqs = NULL;
560}
561
562/**
563 * alloc_afu_irqs() - internal service to allocate interrupts
564 * @ctx: Context associated with the request.
565 * @num: Number of interrupts requested.
566 *
567 * Return: 0 on success, -errno on failure
568 */
569static int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
570{
571 struct ocxl_hw_afu *afu = ctx->hw_afu;
572 struct device *dev = afu->dev;
573 struct ocxlflash_irqs *irqs;
574 u64 addr;
575 int rc = 0;
576 int hwirq;
577 int i;
578
579 if (ctx->irqs) {
580 dev_err(dev, "%s: Interrupts already allocated\n", __func__);
581 rc = -EEXIST;
582 goto out;
583 }
584
585 if (num > OCXL_MAX_IRQS) {
586 dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
587 rc = -EINVAL;
588 goto out;
589 }
590
591 irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
592 if (unlikely(!irqs)) {
593 dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
594 rc = -ENOMEM;
595 goto out;
596 }
597
598 for (i = 0; i < num; i++) {
599 rc = ocxl_link_irq_alloc(afu->link_token, &hwirq, &addr);
600 if (unlikely(rc)) {
601 dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
602 __func__, rc);
603 goto err;
604 }
605
606 irqs[i].hwirq = hwirq;
607 irqs[i].ptrig = addr;
608 }
609
610 ctx->irqs = irqs;
611 ctx->num_irqs = num;
612out:
613 return rc;
614err:
615 for (i = i-1; i >= 0; i--)
616 ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
617 kfree(irqs);
618 goto out;
619}
620
621/**
622 * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
623 * @ctx_cookie: Context associated with the request.
624 * @num: Number of interrupts requested.
625 *
626 * Return: 0 on success, -errno on failure
627 */
628static int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
629{
630 return alloc_afu_irqs(ctx_cookie, num);
631}
632
633/**
634 * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
635 * @ctx_cookie: Adapter context.
636 */
637static void ocxlflash_free_afu_irqs(void *ctx_cookie)
638{
639 free_afu_irqs(ctx_cookie);
640}
641
642/**
Uma Krishnan54370502018-03-26 11:32:37 -0500643 * ocxlflash_unconfig_afu() - unconfigure the AFU
644 * @afu: AFU associated with the host.
645 */
646static void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
647{
648 if (afu->gmmio_virt) {
649 iounmap(afu->gmmio_virt);
650 afu->gmmio_virt = NULL;
651 }
652}
653
654/**
Uma Krishnan48e077d2018-03-26 11:31:01 -0500655 * ocxlflash_destroy_afu() - destroy the AFU structure
656 * @afu_cookie: AFU to be freed.
657 */
658static void ocxlflash_destroy_afu(void *afu_cookie)
659{
660 struct ocxl_hw_afu *afu = afu_cookie;
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500661 int pos;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500662
663 if (!afu)
664 return;
665
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500666 ocxlflash_release_context(afu->ocxl_ctx);
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500667 idr_destroy(&afu->idr);
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500668
669 /* Disable the AFU */
670 pos = afu->acfg.dvsec_afu_control_pos;
671 ocxl_config_set_afu_state(afu->pdev, pos, 0);
672
Uma Krishnan54370502018-03-26 11:32:37 -0500673 ocxlflash_unconfig_afu(afu);
Uma Krishnan48e077d2018-03-26 11:31:01 -0500674 kfree(afu);
675}
676
677/**
Uma Krishnane9dfced2018-03-26 11:31:09 -0500678 * ocxlflash_config_fn() - configure the host function
679 * @pdev: PCI device associated with the host.
680 * @afu: AFU associated with the host.
681 *
682 * Return: 0 on success, -errno on failure
683 */
684static int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
685{
686 struct ocxl_fn_config *fcfg = &afu->fcfg;
687 struct device *dev = &pdev->dev;
Uma Krishnan2e222772018-03-26 11:31:21 -0500688 u16 base, enabled, supported;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500689 int rc = 0;
690
691 /* Read DVSEC config of the function */
692 rc = ocxl_config_read_function(pdev, fcfg);
693 if (unlikely(rc)) {
694 dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
695 __func__, rc);
696 goto out;
697 }
698
699 /* Check if function has AFUs defined, only 1 per function supported */
700 if (fcfg->max_afu_index >= 0) {
701 afu->is_present = true;
702 if (fcfg->max_afu_index != 0)
703 dev_warn(dev, "%s: Unexpected AFU index value %d\n",
704 __func__, fcfg->max_afu_index);
705 }
Uma Krishnan2e222772018-03-26 11:31:21 -0500706
707 rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
708 if (unlikely(rc)) {
709 dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
710 __func__, rc);
711 goto out;
712 }
713
714 afu->fn_actag_base = base;
715 afu->fn_actag_enabled = enabled;
716
717 ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
718 dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
719 __func__, base, enabled);
Uma Krishnan73904822018-03-26 11:33:21 -0500720
721 rc = ocxl_link_setup(pdev, 0, &afu->link_token);
722 if (unlikely(rc)) {
723 dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
724 __func__, rc);
725 goto out;
726 }
Uma Krishnanc52bf5b2018-03-26 11:33:28 -0500727
728 rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
729 if (unlikely(rc)) {
730 dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
731 __func__, rc);
732 goto err;
733 }
Uma Krishnane9dfced2018-03-26 11:31:09 -0500734out:
735 return rc;
Uma Krishnanc52bf5b2018-03-26 11:33:28 -0500736err:
737 ocxl_link_release(pdev, afu->link_token);
738 goto out;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500739}
740
741/**
Uma Krishnan73904822018-03-26 11:33:21 -0500742 * ocxlflash_unconfig_fn() - unconfigure the host function
743 * @pdev: PCI device associated with the host.
744 * @afu: AFU associated with the host.
745 */
746static void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
747{
748 ocxl_link_release(pdev, afu->link_token);
749}
750
751/**
Uma Krishnan54370502018-03-26 11:32:37 -0500752 * ocxlflash_map_mmio() - map the AFU MMIO space
753 * @afu: AFU associated with the host.
754 *
755 * Return: 0 on success, -errno on failure
756 */
757static int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
758{
759 struct ocxl_afu_config *acfg = &afu->acfg;
760 struct pci_dev *pdev = afu->pdev;
761 struct device *dev = afu->dev;
762 phys_addr_t gmmio, ppmmio;
763 int rc = 0;
764
765 rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
766 if (unlikely(rc)) {
767 dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
768 __func__, rc);
769 goto out;
770 }
771 gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
772 gmmio += acfg->global_mmio_offset;
773
774 rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
775 if (unlikely(rc)) {
776 dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
777 __func__, rc);
778 goto err1;
779 }
780 ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
781 ppmmio += acfg->pp_mmio_offset;
782
783 afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
784 if (unlikely(!afu->gmmio_virt)) {
785 dev_err(dev, "%s: MMIO mapping failed\n", __func__);
786 rc = -ENOMEM;
787 goto err2;
788 }
789
790 afu->gmmio_phys = gmmio;
791 afu->ppmmio_phys = ppmmio;
792out:
793 return rc;
794err2:
795 pci_release_region(pdev, acfg->pp_mmio_bar);
796err1:
797 pci_release_region(pdev, acfg->global_mmio_bar);
798 goto out;
799}
800
801/**
Uma Krishnan9cc84292018-03-26 11:31:29 -0500802 * ocxlflash_config_afu() - configure the host AFU
803 * @pdev: PCI device associated with the host.
804 * @afu: AFU associated with the host.
805 *
806 * Must be called _after_ host function configuration.
807 *
808 * Return: 0 on success, -errno on failure
809 */
810static int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
811{
812 struct ocxl_afu_config *acfg = &afu->acfg;
813 struct ocxl_fn_config *fcfg = &afu->fcfg;
814 struct device *dev = &pdev->dev;
Uma Krishnand926519e2018-03-26 11:31:36 -0500815 int count;
816 int base;
817 int pos;
Uma Krishnan9cc84292018-03-26 11:31:29 -0500818 int rc = 0;
819
820 /* This HW AFU function does not have any AFUs defined */
821 if (!afu->is_present)
822 goto out;
823
824 /* Read AFU config at index 0 */
825 rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
826 if (unlikely(rc)) {
827 dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
828 __func__, rc);
829 goto out;
830 }
Uma Krishnand926519e2018-03-26 11:31:36 -0500831
832 /* Only one AFU per function is supported, so actag_base is same */
833 base = afu->fn_actag_base;
834 count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
835 pos = acfg->dvsec_afu_control_pos;
836
837 ocxl_config_set_afu_actag(pdev, pos, base, count);
838 dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
839 afu->afu_actag_base = base;
840 afu->afu_actag_enabled = count;
Uma Krishnan41df40d2018-03-26 11:31:44 -0500841 afu->max_pasid = 1 << acfg->pasid_supported_log;
842
843 ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
Uma Krishnan54370502018-03-26 11:32:37 -0500844
845 rc = ocxlflash_map_mmio(afu);
846 if (unlikely(rc)) {
847 dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
848 __func__, rc);
849 goto out;
850 }
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500851
852 /* Enable the AFU */
853 ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
Uma Krishnan9cc84292018-03-26 11:31:29 -0500854out:
855 return rc;
856}
857
858/**
Uma Krishnan48e077d2018-03-26 11:31:01 -0500859 * ocxlflash_create_afu() - create the AFU for OCXL
860 * @pdev: PCI device associated with the host.
861 *
862 * Return: AFU on success, NULL on failure
863 */
864static void *ocxlflash_create_afu(struct pci_dev *pdev)
865{
866 struct device *dev = &pdev->dev;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500867 struct ocxlflash_context *ctx;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500868 struct ocxl_hw_afu *afu;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500869 int rc;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500870
871 afu = kzalloc(sizeof(*afu), GFP_KERNEL);
872 if (unlikely(!afu)) {
873 dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
874 goto out;
875 }
876
877 afu->pdev = pdev;
878 afu->dev = dev;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500879 idr_init(&afu->idr);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500880
881 rc = ocxlflash_config_fn(pdev, afu);
882 if (unlikely(rc)) {
883 dev_err(dev, "%s: Function configuration failed rc=%d\n",
884 __func__, rc);
885 goto err1;
886 }
Uma Krishnan9cc84292018-03-26 11:31:29 -0500887
888 rc = ocxlflash_config_afu(pdev, afu);
889 if (unlikely(rc)) {
890 dev_err(dev, "%s: AFU configuration failed rc=%d\n",
891 __func__, rc);
Uma Krishnan73904822018-03-26 11:33:21 -0500892 goto err2;
Uma Krishnan9cc84292018-03-26 11:31:29 -0500893 }
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500894
895 ctx = ocxlflash_dev_context_init(pdev, afu);
896 if (IS_ERR(ctx)) {
897 rc = PTR_ERR(ctx);
898 dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
899 __func__, rc);
Uma Krishnan73904822018-03-26 11:33:21 -0500900 goto err3;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500901 }
902
903 afu->ocxl_ctx = ctx;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500904out:
905 return afu;
Uma Krishnan73904822018-03-26 11:33:21 -0500906err3:
Uma Krishnan54370502018-03-26 11:32:37 -0500907 ocxlflash_unconfig_afu(afu);
Uma Krishnan73904822018-03-26 11:33:21 -0500908err2:
909 ocxlflash_unconfig_fn(pdev, afu);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500910err1:
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500911 idr_destroy(&afu->idr);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500912 kfree(afu);
913 afu = NULL;
914 goto out;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500915}
Uma Krishnan76ebe012018-03-26 11:30:51 -0500916
Uma Krishnan56f1db12018-03-26 11:34:03 -0500917/**
918 * ctx_event_pending() - check for any event pending on the context
919 * @ctx: Context to be checked.
920 *
921 * Return: true if there is an event pending, false if none pending
922 */
923static inline bool ctx_event_pending(struct ocxlflash_context *ctx)
924{
925 if (ctx->pending_irq)
926 return true;
927
928 return false;
929}
930
931/**
932 * afu_poll() - poll the AFU for events on the context
933 * @file: File associated with the adapter context.
934 * @poll: Poll structure from the user.
935 *
936 * Return: poll mask
937 */
938static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
939{
940 struct ocxlflash_context *ctx = file->private_data;
941 struct device *dev = ctx->hw_afu->dev;
942 ulong lock_flags;
943 int mask = 0;
944
945 poll_wait(file, &ctx->wq, poll);
946
947 spin_lock_irqsave(&ctx->slock, lock_flags);
948 if (ctx_event_pending(ctx))
949 mask |= POLLIN | POLLRDNORM;
950 else
951 mask |= POLLERR;
952 spin_unlock_irqrestore(&ctx->slock, lock_flags);
953
954 dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
955 __func__, ctx->pe, mask);
956
957 return mask;
958}
959
Uma Krishnan03aa9c52018-03-26 11:34:12 -0500960/**
961 * afu_read() - perform a read on the context for any event
962 * @file: File associated with the adapter context.
963 * @buf: Buffer to receive the data.
964 * @count: Size of buffer (maximum bytes that can be read).
965 * @off: Offset.
966 *
967 * Return: size of the data read on success, -errno on failure
968 */
969static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
970 loff_t *off)
971{
972 struct ocxlflash_context *ctx = file->private_data;
973 struct device *dev = ctx->hw_afu->dev;
974 struct cxl_event event;
975 ulong lock_flags;
976 ssize_t esize;
977 ssize_t rc;
978 int bit;
979 DEFINE_WAIT(event_wait);
980
981 if (*off != 0) {
982 dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
983 __func__, *off);
984 rc = -EINVAL;
985 goto out;
986 }
987
988 spin_lock_irqsave(&ctx->slock, lock_flags);
989
990 for (;;) {
991 prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
992
993 if (ctx_event_pending(ctx))
994 break;
995
996 if (file->f_flags & O_NONBLOCK) {
997 dev_err(dev, "%s: File cannot be blocked on I/O\n",
998 __func__);
999 rc = -EAGAIN;
1000 goto err;
1001 }
1002
1003 if (signal_pending(current)) {
1004 dev_err(dev, "%s: Signal pending on the process\n",
1005 __func__);
1006 rc = -ERESTARTSYS;
1007 goto err;
1008 }
1009
1010 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1011 schedule();
1012 spin_lock_irqsave(&ctx->slock, lock_flags);
1013 }
1014
1015 finish_wait(&ctx->wq, &event_wait);
1016
1017 memset(&event, 0, sizeof(event));
1018 event.header.process_element = ctx->pe;
1019 event.header.size = sizeof(struct cxl_event_header);
1020 if (ctx->pending_irq) {
1021 esize = sizeof(struct cxl_event_afu_interrupt);
1022 event.header.size += esize;
1023 event.header.type = CXL_EVENT_AFU_INTERRUPT;
1024
1025 bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
1026 clear_bit(bit, &ctx->irq_bitmap);
1027 event.irq.irq = bit + 1;
1028 if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
1029 ctx->pending_irq = false;
1030 }
1031
1032 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1033
1034 if (copy_to_user(buf, &event, event.header.size)) {
1035 dev_err(dev, "%s: copy_to_user failed\n", __func__);
1036 rc = -EFAULT;
1037 goto out;
1038 }
1039
1040 rc = event.header.size;
1041out:
1042 return rc;
1043err:
1044 finish_wait(&ctx->wq, &event_wait);
1045 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1046 goto out;
1047}
1048
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001049/**
1050 * afu_release() - release and free the context
1051 * @inode: File inode pointer.
1052 * @file: File associated with the context.
1053 *
1054 * Return: 0 on success, -errno on failure
1055 */
1056static int afu_release(struct inode *inode, struct file *file)
1057{
1058 struct ocxlflash_context *ctx = file->private_data;
1059 int i;
1060
1061 /* Unmap and free the interrupts associated with the context */
1062 for (i = ctx->num_irqs; i >= 0; i--)
1063 afu_unmap_irq(0, ctx, i, ctx);
1064 free_afu_irqs(ctx);
1065
1066 return ocxlflash_release_context(ctx);
1067}
1068
1069/**
1070 * ocxlflash_mmap_fault() - mmap fault handler
1071 * @vmf: VM fault associated with current fault.
1072 *
1073 * Return: 0 on success, -errno on failure
1074 */
1075static int ocxlflash_mmap_fault(struct vm_fault *vmf)
1076{
1077 struct vm_area_struct *vma = vmf->vma;
1078 struct ocxlflash_context *ctx = vma->vm_file->private_data;
1079 u64 mmio_area, offset;
1080
1081 offset = vmf->pgoff << PAGE_SHIFT;
1082 if (offset >= ctx->psn_size)
1083 return VM_FAULT_SIGBUS;
1084
1085 mmio_area = ctx->psn_phys;
1086 mmio_area += offset;
1087
1088 vm_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
1089 return VM_FAULT_NOPAGE;
1090}
1091
1092static const struct vm_operations_struct ocxlflash_vmops = {
1093 .fault = ocxlflash_mmap_fault,
1094};
1095
1096/**
1097 * afu_mmap() - map the fault handler operations
1098 * @file: File associated with the context.
1099 * @vma: VM area associated with mapping.
1100 *
1101 * Return: 0 on success, -errno on failure
1102 */
1103static int afu_mmap(struct file *file, struct vm_area_struct *vma)
1104{
1105 struct ocxlflash_context *ctx = file->private_data;
1106
1107 if ((vma_pages(vma) + vma->vm_pgoff) >
1108 (ctx->psn_size >> PAGE_SHIFT))
1109 return -EINVAL;
1110
1111 vma->vm_flags |= VM_IO | VM_PFNMAP;
1112 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1113 vma->vm_ops = &ocxlflash_vmops;
1114 return 0;
1115}
1116
Uma Krishnan926a62f2018-03-26 11:32:09 -05001117static const struct file_operations ocxl_afu_fops = {
1118 .owner = THIS_MODULE,
Uma Krishnan56f1db12018-03-26 11:34:03 -05001119 .poll = afu_poll,
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001120 .read = afu_read,
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001121 .release = afu_release,
1122 .mmap = afu_mmap,
Uma Krishnan926a62f2018-03-26 11:32:09 -05001123};
1124
Uma Krishnan56f1db12018-03-26 11:34:03 -05001125#define PATCH_FOPS(NAME) \
1126 do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1127
Uma Krishnan926a62f2018-03-26 11:32:09 -05001128/**
1129 * ocxlflash_get_fd() - get file descriptor for an adapter context
1130 * @ctx_cookie: Adapter context.
1131 * @fops: File operations to be associated.
1132 * @fd: File descriptor to be returned back.
1133 *
1134 * Return: pointer to the file on success, ERR_PTR on failure
1135 */
1136static struct file *ocxlflash_get_fd(void *ctx_cookie,
1137 struct file_operations *fops, int *fd)
1138{
1139 struct ocxlflash_context *ctx = ctx_cookie;
1140 struct device *dev = ctx->hw_afu->dev;
1141 struct file *file;
1142 int flags, fdtmp;
1143 int rc = 0;
1144 char *name = NULL;
1145
1146 /* Only allow one fd per context */
1147 if (ctx->mapping) {
1148 dev_err(dev, "%s: Context is already mapped to an fd\n",
1149 __func__);
1150 rc = -EEXIST;
1151 goto err1;
1152 }
1153
1154 flags = O_RDWR | O_CLOEXEC;
1155
1156 /* This code is similar to anon_inode_getfd() */
1157 rc = get_unused_fd_flags(flags);
1158 if (unlikely(rc < 0)) {
1159 dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
1160 __func__, rc);
1161 goto err1;
1162 }
1163 fdtmp = rc;
1164
Uma Krishnan56f1db12018-03-26 11:34:03 -05001165 /* Patch the file ops that are not defined */
1166 if (fops) {
1167 PATCH_FOPS(poll);
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001168 PATCH_FOPS(read);
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001169 PATCH_FOPS(release);
1170 PATCH_FOPS(mmap);
Uma Krishnan56f1db12018-03-26 11:34:03 -05001171 } else /* Use default ops */
Uma Krishnan926a62f2018-03-26 11:32:09 -05001172 fops = (struct file_operations *)&ocxl_afu_fops;
1173
1174 name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
1175 file = ocxlflash_getfile(dev, name, fops, ctx, flags);
1176 kfree(name);
1177 if (IS_ERR(file)) {
1178 rc = PTR_ERR(file);
1179 dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
1180 __func__, rc);
1181 goto err2;
1182 }
1183
1184 ctx->mapping = file->f_mapping;
1185 *fd = fdtmp;
1186out:
1187 return file;
1188err2:
1189 put_unused_fd(fdtmp);
1190err1:
1191 file = ERR_PTR(rc);
1192 goto out;
1193}
1194
Uma Krishnanb18718c2018-03-26 11:32:20 -05001195/**
1196 * ocxlflash_fops_get_context() - get the context associated with the file
1197 * @file: File associated with the adapter context.
1198 *
1199 * Return: pointer to the context
1200 */
1201static void *ocxlflash_fops_get_context(struct file *file)
1202{
1203 return file->private_data;
1204}
1205
Uma Krishnan762c7e92018-03-26 11:33:55 -05001206/**
1207 * ocxlflash_afu_irq() - interrupt handler for user contexts
1208 * @irq: Interrupt number.
1209 * @data: Private data provided at interrupt registration, the context.
1210 *
1211 * Return: Always return IRQ_HANDLED.
1212 */
1213static irqreturn_t ocxlflash_afu_irq(int irq, void *data)
1214{
1215 struct ocxlflash_context *ctx = data;
1216 struct device *dev = ctx->hw_afu->dev;
1217 int i;
1218
1219 dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
1220 __func__, ctx->pe, irq);
1221
1222 for (i = 0; i < ctx->num_irqs; i++) {
1223 if (ctx->irqs[i].virq == irq)
1224 break;
1225 }
1226 if (unlikely(i >= ctx->num_irqs)) {
1227 dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
1228 goto out;
1229 }
1230
1231 spin_lock(&ctx->slock);
1232 set_bit(i - 1, &ctx->irq_bitmap);
1233 ctx->pending_irq = true;
1234 spin_unlock(&ctx->slock);
Uma Krishnan56f1db12018-03-26 11:34:03 -05001235
1236 wake_up_all(&ctx->wq);
Uma Krishnan762c7e92018-03-26 11:33:55 -05001237out:
1238 return IRQ_HANDLED;
1239}
1240
1241/**
1242 * ocxlflash_start_work() - start a user context
1243 * @ctx_cookie: Context to be started.
1244 * @num_irqs: Number of interrupts requested.
1245 *
1246 * Return: 0 on success, -errno on failure
1247 */
1248static int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
1249{
1250 struct ocxlflash_context *ctx = ctx_cookie;
1251 struct ocxl_hw_afu *afu = ctx->hw_afu;
1252 struct device *dev = afu->dev;
1253 char *name;
1254 int rc = 0;
1255 int i;
1256
1257 rc = alloc_afu_irqs(ctx, num_irqs);
1258 if (unlikely(rc < 0)) {
1259 dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
1260 goto out;
1261 }
1262
1263 for (i = 0; i < num_irqs; i++) {
1264 name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
1265 dev_name(dev), ctx->pe, i);
1266 rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
1267 kfree(name);
1268 if (unlikely(rc < 0)) {
1269 dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
1270 __func__, rc);
1271 goto err;
1272 }
1273 }
1274
1275 rc = start_context(ctx);
1276 if (unlikely(rc)) {
1277 dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
1278 goto err;
1279 }
1280out:
1281 return rc;
1282err:
1283 for (i = i-1; i >= 0; i--)
1284 afu_unmap_irq(0, ctx, i, ctx);
1285 free_afu_irqs(ctx);
1286 goto out;
Uma Krishnane117c3c2018-03-26 11:34:27 -05001287};
1288
1289/**
1290 * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
1291 * @file: File installed with adapter file descriptor.
1292 * @vma: VM area associated with mapping.
1293 *
1294 * Return: 0 on success, -errno on failure
1295 */
1296static int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
1297{
1298 return afu_mmap(file, vma);
1299}
1300
1301/**
1302 * ocxlflash_fd_release() - release the context associated with the file
1303 * @inode: File inode pointer.
1304 * @file: File associated with the adapter context.
1305 *
1306 * Return: 0 on success, -errno on failure
1307 */
1308static int ocxlflash_fd_release(struct inode *inode, struct file *file)
1309{
1310 return afu_release(inode, file);
Uma Krishnan762c7e92018-03-26 11:33:55 -05001311}
1312
Uma Krishnan76ebe012018-03-26 11:30:51 -05001313/* Backend ops to ocxlflash services */
1314const struct cxlflash_backend_ops cxlflash_ocxl_ops = {
1315 .module = THIS_MODULE,
Uma Krishnan012f3942018-03-26 11:32:56 -05001316 .psa_map = ocxlflash_psa_map,
1317 .psa_unmap = ocxlflash_psa_unmap,
Uma Krishnanb18718c2018-03-26 11:32:20 -05001318 .process_element = ocxlflash_process_element,
Uma Krishnana06b1cf2018-03-26 11:33:48 -05001319 .map_afu_irq = ocxlflash_map_afu_irq,
1320 .unmap_afu_irq = ocxlflash_unmap_afu_irq,
Uma Krishnan402a55e2018-03-26 11:34:35 -05001321 .get_irq_objhndl = ocxlflash_get_irq_objhndl,
Uma Krishnan6b938ac2018-03-26 11:32:48 -05001322 .start_context = ocxlflash_start_context,
Uma Krishnanc207b572018-03-26 11:33:35 -05001323 .stop_context = ocxlflash_stop_context,
Uma Krishnanf6b4557c2018-03-26 11:31:53 -05001324 .set_master = ocxlflash_set_master,
1325 .get_context = ocxlflash_get_context,
1326 .dev_context_init = ocxlflash_dev_context_init,
1327 .release_context = ocxlflash_release_context,
Uma Krishnan8b7a5522018-03-26 11:32:29 -05001328 .perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
Uma Krishnan119c9202018-03-26 11:33:14 -05001329 .read_adapter_vpd = ocxlflash_read_adapter_vpd,
Uma Krishnanbc65c1c2018-03-26 11:33:41 -05001330 .allocate_afu_irqs = ocxlflash_allocate_afu_irqs,
1331 .free_afu_irqs = ocxlflash_free_afu_irqs,
Uma Krishnan48e077d2018-03-26 11:31:01 -05001332 .create_afu = ocxlflash_create_afu,
1333 .destroy_afu = ocxlflash_destroy_afu,
Uma Krishnan926a62f2018-03-26 11:32:09 -05001334 .get_fd = ocxlflash_get_fd,
Uma Krishnanb18718c2018-03-26 11:32:20 -05001335 .fops_get_context = ocxlflash_fops_get_context,
Uma Krishnan762c7e92018-03-26 11:33:55 -05001336 .start_work = ocxlflash_start_work,
Uma Krishnane117c3c2018-03-26 11:34:27 -05001337 .fd_mmap = ocxlflash_fd_mmap,
1338 .fd_release = ocxlflash_fd_release,
Uma Krishnan76ebe012018-03-26 11:30:51 -05001339};