blob: 5b5565d6572e8f3aa4e96357829055dc80a64e42 [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;
Uma Krishnanf81face2018-03-26 11:35:00 -0500166 struct device *dev = ctx->hw_afu->dev;
167
168 mutex_lock(&ctx->state_mutex);
169 if (ctx->state != STARTED) {
170 dev_err(dev, "%s: Context not started, state=%d\n", __func__,
171 ctx->state);
172 mutex_unlock(&ctx->state_mutex);
173 return NULL;
174 }
175 mutex_unlock(&ctx->state_mutex);
Uma Krishnan012f3942018-03-26 11:32:56 -0500176
177 return ioremap(ctx->psn_phys, ctx->psn_size);
178}
179
180/**
181 * ocxlflash_psa_unmap() - unmap the process specific MMIO space
182 * @addr: MMIO pointer to unmap.
183 */
184static void ocxlflash_psa_unmap(void __iomem *addr)
185{
186 iounmap(addr);
187}
188
189/**
Uma Krishnanb18718c2018-03-26 11:32:20 -0500190 * ocxlflash_process_element() - get process element of the adapter context
191 * @ctx_cookie: Adapter context associated with the process element.
192 *
193 * Return: process element of the adapter context
194 */
195static int ocxlflash_process_element(void *ctx_cookie)
196{
197 struct ocxlflash_context *ctx = ctx_cookie;
198
199 return ctx->pe;
200}
201
202/**
Uma Krishnana06b1cf2018-03-26 11:33:48 -0500203 * afu_map_irq() - map the interrupt of the adapter context
204 * @flags: Flags.
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.
210 *
211 * Return: 0 on success, -errno on failure
212 */
213static int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
214 irq_handler_t handler, void *cookie, char *name)
215{
216 struct ocxl_hw_afu *afu = ctx->hw_afu;
217 struct device *dev = afu->dev;
218 struct ocxlflash_irqs *irq;
219 void __iomem *vtrig;
220 u32 virq;
221 int rc = 0;
222
223 if (num < 0 || num >= ctx->num_irqs) {
224 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
225 rc = -ENOENT;
226 goto out;
227 }
228
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__);
233 rc = -ENOMEM;
234 goto out;
235 }
236
237 rc = request_irq(virq, handler, 0, name, cookie);
238 if (unlikely(rc)) {
239 dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
240 goto err1;
241 }
242
243 vtrig = ioremap(irq->ptrig, PAGE_SIZE);
244 if (unlikely(!vtrig)) {
245 dev_err(dev, "%s: Trigger page mapping failed\n", __func__);
246 rc = -ENOMEM;
247 goto err2;
248 }
249
250 irq->virq = virq;
251 irq->vtrig = vtrig;
252out:
253 return rc;
254err2:
255 free_irq(virq, cookie);
256err1:
257 irq_dispose_mapping(virq);
258 goto out;
259}
260
261/**
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.
268 *
269 * Return: 0 on success, -errno on failure
270 */
271static int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
272 irq_handler_t handler, void *cookie,
273 char *name)
274{
275 return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
276}
277
278/**
279 * afu_unmap_irq() - unmap the interrupt
280 * @flags: Flags.
281 * @ctx: Adapter context.
282 * @num: Per-context AFU interrupt number.
283 * @cookie: Interrupt handler private data.
284 */
285static void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
286 void *cookie)
287{
288 struct ocxl_hw_afu *afu = ctx->hw_afu;
289 struct device *dev = afu->dev;
290 struct ocxlflash_irqs *irq;
291
292 if (num < 0 || num >= ctx->num_irqs) {
293 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
294 return;
295 }
296
297 irq = &ctx->irqs[num];
298 if (irq->vtrig)
299 iounmap(irq->vtrig);
300
301 if (irq_find_mapping(NULL, irq->hwirq)) {
302 free_irq(irq->virq, cookie);
303 irq_dispose_mapping(irq->virq);
304 }
305
306 memset(irq, 0, sizeof(*irq));
307}
308
309/**
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.
314 */
315static void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
316{
317 return afu_unmap_irq(0, ctx_cookie, num, cookie);
318}
319
320/**
Uma Krishnan402a55e2018-03-26 11:34:35 -0500321 * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
322 * @ctx_cookie: Context associated with the interrupt.
323 * @irq: Interrupt number.
324 *
325 * Return: effective address of the mapped region
326 */
327static u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
328{
329 struct ocxlflash_context *ctx = ctx_cookie;
330
331 if (irq < 0 || irq >= ctx->num_irqs)
332 return 0;
333
334 return (__force u64)ctx->irqs[irq].vtrig;
335}
336
337/**
Uma Krishnan66ae6442018-03-26 11:35:07 -0500338 * 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.
342 */
343static void ocxlflash_xsl_fault(void *data, u64 addr, u64 dsisr)
344{
345 struct ocxlflash_context *ctx = data;
346
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);
352
353 wake_up_all(&ctx->wq);
354}
355
356/**
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500357 * start_context() - local routine to start a context
358 * @ctx: Adapter context to be started.
359 *
Uma Krishnanc207b572018-03-26 11:33:35 -0500360 * Assign the context specific MMIO space, add and enable the PE.
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500361 *
362 * Return: 0 on success, -errno on failure
363 */
364static int start_context(struct ocxlflash_context *ctx)
365{
366 struct ocxl_hw_afu *afu = ctx->hw_afu;
367 struct ocxl_afu_config *acfg = &afu->acfg;
Uma Krishnanc207b572018-03-26 11:33:35 -0500368 void *link_token = afu->link_token;
369 struct device *dev = afu->dev;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500370 bool master = ctx->master;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500371 struct mm_struct *mm;
Uma Krishnanc207b572018-03-26 11:33:35 -0500372 int rc = 0;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500373 u32 pid;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500374
Uma Krishnanf81face2018-03-26 11:35:00 -0500375 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);
379 rc = -EINVAL;
380 goto out;
381 }
382
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500383 if (master) {
384 ctx->psn_size = acfg->global_mmio_size;
385 ctx->psn_phys = afu->gmmio_phys;
386 } else {
387 ctx->psn_size = acfg->pp_mmio_stride;
388 ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
389 }
390
Uma Krishnan762c7e92018-03-26 11:33:55 -0500391 /* pid and mm not set for master contexts */
392 if (master) {
393 pid = 0;
394 mm = NULL;
395 } else {
396 pid = current->mm->context.id;
397 mm = current->mm;
398 }
Uma Krishnanc207b572018-03-26 11:33:35 -0500399
Uma Krishnan66ae6442018-03-26 11:35:07 -0500400 rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0, mm,
401 ocxlflash_xsl_fault, ctx);
Uma Krishnanc207b572018-03-26 11:33:35 -0500402 if (unlikely(rc)) {
403 dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
404 __func__, rc);
405 goto out;
406 }
Uma Krishnanf81face2018-03-26 11:35:00 -0500407
408 ctx->state = STARTED;
Uma Krishnanc207b572018-03-26 11:33:35 -0500409out:
Uma Krishnanf81face2018-03-26 11:35:00 -0500410 mutex_unlock(&ctx->state_mutex);
Uma Krishnanc207b572018-03-26 11:33:35 -0500411 return rc;
Uma Krishnan6b938ac2018-03-26 11:32:48 -0500412}
413
414/**
415 * ocxlflash_start_context() - start a kernel context
416 * @ctx_cookie: Adapter context to be started.
417 *
418 * Return: 0 on success, -errno on failure
419 */
420static int ocxlflash_start_context(void *ctx_cookie)
421{
422 struct ocxlflash_context *ctx = ctx_cookie;
423
424 return start_context(ctx);
425}
426
427/**
Uma Krishnanc207b572018-03-26 11:33:35 -0500428 * ocxlflash_stop_context() - stop a context
429 * @ctx_cookie: Adapter context to be stopped.
430 *
431 * Return: 0 on success, -errno on failure
432 */
433static int ocxlflash_stop_context(void *ctx_cookie)
434{
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;
Uma Krishnanf81face2018-03-26 11:35:00 -0500440 enum ocxlflash_ctx_state state;
441 int rc = 0;
442
443 mutex_lock(&ctx->state_mutex);
444 state = ctx->state;
445 ctx->state = CLOSED;
446 mutex_unlock(&ctx->state_mutex);
447 if (state != STARTED)
448 goto out;
Uma Krishnanc207b572018-03-26 11:33:35 -0500449
450 rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
451 ctx->pe);
452 if (unlikely(rc)) {
453 dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
454 __func__, rc);
455 /* If EBUSY, PE could be referenced in future by the AFU */
456 if (rc == -EBUSY)
457 goto out;
458 }
459
460 rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
461 if (unlikely(rc)) {
462 dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
463 __func__, rc);
464 goto out;
465 }
466out:
467 return rc;
468}
469
470/**
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500471 * ocxlflash_set_master() - sets the context as master
472 * @ctx_cookie: Adapter context to set as master.
473 */
474static void ocxlflash_set_master(void *ctx_cookie)
475{
476 struct ocxlflash_context *ctx = ctx_cookie;
477
478 ctx->master = true;
479}
480
481/**
482 * ocxlflash_get_context() - obtains the context associated with the host
483 * @pdev: PCI device associated with the host.
484 * @afu_cookie: Hardware AFU associated with the host.
485 *
486 * Return: returns the pointer to host adapter context
487 */
488static void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
489{
490 struct ocxl_hw_afu *afu = afu_cookie;
491
492 return afu->ocxl_ctx;
493}
494
495/**
496 * ocxlflash_dev_context_init() - allocate and initialize an adapter context
497 * @pdev: PCI device associated with the host.
498 * @afu_cookie: Hardware AFU associated with the host.
499 *
500 * Return: returns the adapter context on success, ERR_PTR on failure
501 */
502static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
503{
504 struct ocxl_hw_afu *afu = afu_cookie;
505 struct device *dev = afu->dev;
506 struct ocxlflash_context *ctx;
507 int rc;
508
509 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
510 if (unlikely(!ctx)) {
511 dev_err(dev, "%s: Context allocation failed\n", __func__);
512 rc = -ENOMEM;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500513 goto err1;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500514 }
515
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500516 idr_preload(GFP_KERNEL);
517 rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
518 idr_preload_end();
519 if (unlikely(rc < 0)) {
520 dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
521 goto err2;
522 }
523
Uma Krishnan762c7e92018-03-26 11:33:55 -0500524 spin_lock_init(&ctx->slock);
Uma Krishnan56f1db12018-03-26 11:34:03 -0500525 init_waitqueue_head(&ctx->wq);
Uma Krishnanf81face2018-03-26 11:35:00 -0500526 mutex_init(&ctx->state_mutex);
Uma Krishnan762c7e92018-03-26 11:33:55 -0500527
Uma Krishnanf81face2018-03-26 11:35:00 -0500528 ctx->state = OPENED;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500529 ctx->pe = rc;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500530 ctx->master = false;
Uma Krishnan926a62f2018-03-26 11:32:09 -0500531 ctx->mapping = NULL;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500532 ctx->hw_afu = afu;
Uma Krishnan762c7e92018-03-26 11:33:55 -0500533 ctx->irq_bitmap = 0;
534 ctx->pending_irq = false;
Uma Krishnan66ae6442018-03-26 11:35:07 -0500535 ctx->pending_fault = false;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500536out:
537 return ctx;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500538err2:
539 kfree(ctx);
540err1:
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500541 ctx = ERR_PTR(rc);
542 goto out;
543}
544
545/**
546 * ocxlflash_release_context() - releases an adapter context
547 * @ctx_cookie: Adapter context to be released.
548 *
549 * Return: 0 on success, -errno on failure
550 */
551static int ocxlflash_release_context(void *ctx_cookie)
552{
553 struct ocxlflash_context *ctx = ctx_cookie;
Uma Krishnanf81face2018-03-26 11:35:00 -0500554 struct device *dev;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500555 int rc = 0;
556
557 if (!ctx)
558 goto out;
559
Uma Krishnanf81face2018-03-26 11:35:00 -0500560 dev = ctx->hw_afu->dev;
561 mutex_lock(&ctx->state_mutex);
562 if (ctx->state >= STARTED) {
563 dev_err(dev, "%s: Context in use, state=%d\n", __func__,
564 ctx->state);
565 mutex_unlock(&ctx->state_mutex);
566 rc = -EBUSY;
567 goto out;
568 }
569 mutex_unlock(&ctx->state_mutex);
570
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500571 idr_remove(&ctx->hw_afu->idr, ctx->pe);
Uma Krishnan926a62f2018-03-26 11:32:09 -0500572 ocxlflash_release_mapping(ctx);
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500573 kfree(ctx);
574out:
575 return rc;
576}
577
578/**
Uma Krishnan8b7a5522018-03-26 11:32:29 -0500579 * ocxlflash_perst_reloads_same_image() - sets the image reload policy
580 * @afu_cookie: Hardware AFU associated with the host.
581 * @image: Whether to load the same image on PERST.
582 */
583static void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
584{
585 struct ocxl_hw_afu *afu = afu_cookie;
586
587 afu->perst_same_image = image;
588}
589
590/**
Uma Krishnan119c9202018-03-26 11:33:14 -0500591 * ocxlflash_read_adapter_vpd() - reads the adapter VPD
592 * @pdev: PCI device associated with the host.
593 * @buf: Buffer to get the VPD data.
594 * @count: Size of buffer (maximum bytes that can be read).
595 *
596 * Return: size of VPD on success, -errno on failure
597 */
598static ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
599 size_t count)
600{
601 return pci_read_vpd(pdev, 0, count, buf);
602}
603
604/**
Uma Krishnanbc65c1c2018-03-26 11:33:41 -0500605 * free_afu_irqs() - internal service to free interrupts
606 * @ctx: Adapter context.
607 */
608static void free_afu_irqs(struct ocxlflash_context *ctx)
609{
610 struct ocxl_hw_afu *afu = ctx->hw_afu;
611 struct device *dev = afu->dev;
612 int i;
613
614 if (!ctx->irqs) {
615 dev_err(dev, "%s: Interrupts not allocated\n", __func__);
616 return;
617 }
618
619 for (i = ctx->num_irqs; i >= 0; i--)
620 ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
621
622 kfree(ctx->irqs);
623 ctx->irqs = NULL;
624}
625
626/**
627 * alloc_afu_irqs() - internal service to allocate interrupts
628 * @ctx: Context associated with the request.
629 * @num: Number of interrupts requested.
630 *
631 * Return: 0 on success, -errno on failure
632 */
633static int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
634{
635 struct ocxl_hw_afu *afu = ctx->hw_afu;
636 struct device *dev = afu->dev;
637 struct ocxlflash_irqs *irqs;
638 u64 addr;
639 int rc = 0;
640 int hwirq;
641 int i;
642
643 if (ctx->irqs) {
644 dev_err(dev, "%s: Interrupts already allocated\n", __func__);
645 rc = -EEXIST;
646 goto out;
647 }
648
649 if (num > OCXL_MAX_IRQS) {
650 dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
651 rc = -EINVAL;
652 goto out;
653 }
654
655 irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
656 if (unlikely(!irqs)) {
657 dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
658 rc = -ENOMEM;
659 goto out;
660 }
661
662 for (i = 0; i < num; i++) {
663 rc = ocxl_link_irq_alloc(afu->link_token, &hwirq, &addr);
664 if (unlikely(rc)) {
665 dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
666 __func__, rc);
667 goto err;
668 }
669
670 irqs[i].hwirq = hwirq;
671 irqs[i].ptrig = addr;
672 }
673
674 ctx->irqs = irqs;
675 ctx->num_irqs = num;
676out:
677 return rc;
678err:
679 for (i = i-1; i >= 0; i--)
680 ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
681 kfree(irqs);
682 goto out;
683}
684
685/**
686 * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
687 * @ctx_cookie: Context associated with the request.
688 * @num: Number of interrupts requested.
689 *
690 * Return: 0 on success, -errno on failure
691 */
692static int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
693{
694 return alloc_afu_irqs(ctx_cookie, num);
695}
696
697/**
698 * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
699 * @ctx_cookie: Adapter context.
700 */
701static void ocxlflash_free_afu_irqs(void *ctx_cookie)
702{
703 free_afu_irqs(ctx_cookie);
704}
705
706/**
Uma Krishnan54370502018-03-26 11:32:37 -0500707 * ocxlflash_unconfig_afu() - unconfigure the AFU
708 * @afu: AFU associated with the host.
709 */
710static void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
711{
712 if (afu->gmmio_virt) {
713 iounmap(afu->gmmio_virt);
714 afu->gmmio_virt = NULL;
715 }
716}
717
718/**
Uma Krishnan48e077d2018-03-26 11:31:01 -0500719 * ocxlflash_destroy_afu() - destroy the AFU structure
720 * @afu_cookie: AFU to be freed.
721 */
722static void ocxlflash_destroy_afu(void *afu_cookie)
723{
724 struct ocxl_hw_afu *afu = afu_cookie;
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500725 int pos;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500726
727 if (!afu)
728 return;
729
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500730 ocxlflash_release_context(afu->ocxl_ctx);
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500731 idr_destroy(&afu->idr);
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500732
733 /* Disable the AFU */
734 pos = afu->acfg.dvsec_afu_control_pos;
735 ocxl_config_set_afu_state(afu->pdev, pos, 0);
736
Uma Krishnan54370502018-03-26 11:32:37 -0500737 ocxlflash_unconfig_afu(afu);
Uma Krishnan48e077d2018-03-26 11:31:01 -0500738 kfree(afu);
739}
740
741/**
Uma Krishnane9dfced2018-03-26 11:31:09 -0500742 * ocxlflash_config_fn() - configure the host function
743 * @pdev: PCI device associated with the host.
744 * @afu: AFU associated with the host.
745 *
746 * Return: 0 on success, -errno on failure
747 */
748static int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
749{
750 struct ocxl_fn_config *fcfg = &afu->fcfg;
751 struct device *dev = &pdev->dev;
Uma Krishnan2e222772018-03-26 11:31:21 -0500752 u16 base, enabled, supported;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500753 int rc = 0;
754
755 /* Read DVSEC config of the function */
756 rc = ocxl_config_read_function(pdev, fcfg);
757 if (unlikely(rc)) {
758 dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
759 __func__, rc);
760 goto out;
761 }
762
763 /* Check if function has AFUs defined, only 1 per function supported */
764 if (fcfg->max_afu_index >= 0) {
765 afu->is_present = true;
766 if (fcfg->max_afu_index != 0)
767 dev_warn(dev, "%s: Unexpected AFU index value %d\n",
768 __func__, fcfg->max_afu_index);
769 }
Uma Krishnan2e222772018-03-26 11:31:21 -0500770
771 rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
772 if (unlikely(rc)) {
773 dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
774 __func__, rc);
775 goto out;
776 }
777
778 afu->fn_actag_base = base;
779 afu->fn_actag_enabled = enabled;
780
781 ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
782 dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
783 __func__, base, enabled);
Uma Krishnan73904822018-03-26 11:33:21 -0500784
785 rc = ocxl_link_setup(pdev, 0, &afu->link_token);
786 if (unlikely(rc)) {
787 dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
788 __func__, rc);
789 goto out;
790 }
Uma Krishnanc52bf5b2018-03-26 11:33:28 -0500791
792 rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
793 if (unlikely(rc)) {
794 dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
795 __func__, rc);
796 goto err;
797 }
Uma Krishnane9dfced2018-03-26 11:31:09 -0500798out:
799 return rc;
Uma Krishnanc52bf5b2018-03-26 11:33:28 -0500800err:
801 ocxl_link_release(pdev, afu->link_token);
802 goto out;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500803}
804
805/**
Uma Krishnan73904822018-03-26 11:33:21 -0500806 * ocxlflash_unconfig_fn() - unconfigure the host function
807 * @pdev: PCI device associated with the host.
808 * @afu: AFU associated with the host.
809 */
810static void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
811{
812 ocxl_link_release(pdev, afu->link_token);
813}
814
815/**
Uma Krishnan54370502018-03-26 11:32:37 -0500816 * ocxlflash_map_mmio() - map the AFU MMIO space
817 * @afu: AFU associated with the host.
818 *
819 * Return: 0 on success, -errno on failure
820 */
821static int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
822{
823 struct ocxl_afu_config *acfg = &afu->acfg;
824 struct pci_dev *pdev = afu->pdev;
825 struct device *dev = afu->dev;
826 phys_addr_t gmmio, ppmmio;
827 int rc = 0;
828
829 rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
830 if (unlikely(rc)) {
831 dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
832 __func__, rc);
833 goto out;
834 }
835 gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
836 gmmio += acfg->global_mmio_offset;
837
838 rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
839 if (unlikely(rc)) {
840 dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
841 __func__, rc);
842 goto err1;
843 }
844 ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
845 ppmmio += acfg->pp_mmio_offset;
846
847 afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
848 if (unlikely(!afu->gmmio_virt)) {
849 dev_err(dev, "%s: MMIO mapping failed\n", __func__);
850 rc = -ENOMEM;
851 goto err2;
852 }
853
854 afu->gmmio_phys = gmmio;
855 afu->ppmmio_phys = ppmmio;
856out:
857 return rc;
858err2:
859 pci_release_region(pdev, acfg->pp_mmio_bar);
860err1:
861 pci_release_region(pdev, acfg->global_mmio_bar);
862 goto out;
863}
864
865/**
Uma Krishnan9cc84292018-03-26 11:31:29 -0500866 * ocxlflash_config_afu() - configure the host AFU
867 * @pdev: PCI device associated with the host.
868 * @afu: AFU associated with the host.
869 *
870 * Must be called _after_ host function configuration.
871 *
872 * Return: 0 on success, -errno on failure
873 */
874static int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
875{
876 struct ocxl_afu_config *acfg = &afu->acfg;
877 struct ocxl_fn_config *fcfg = &afu->fcfg;
878 struct device *dev = &pdev->dev;
Uma Krishnand926519e2018-03-26 11:31:36 -0500879 int count;
880 int base;
881 int pos;
Uma Krishnan9cc84292018-03-26 11:31:29 -0500882 int rc = 0;
883
884 /* This HW AFU function does not have any AFUs defined */
885 if (!afu->is_present)
886 goto out;
887
888 /* Read AFU config at index 0 */
889 rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
890 if (unlikely(rc)) {
891 dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
892 __func__, rc);
893 goto out;
894 }
Uma Krishnand926519e2018-03-26 11:31:36 -0500895
896 /* Only one AFU per function is supported, so actag_base is same */
897 base = afu->fn_actag_base;
898 count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
899 pos = acfg->dvsec_afu_control_pos;
900
901 ocxl_config_set_afu_actag(pdev, pos, base, count);
902 dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
903 afu->afu_actag_base = base;
904 afu->afu_actag_enabled = count;
Uma Krishnan41df40d2018-03-26 11:31:44 -0500905 afu->max_pasid = 1 << acfg->pasid_supported_log;
906
907 ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
Uma Krishnan54370502018-03-26 11:32:37 -0500908
909 rc = ocxlflash_map_mmio(afu);
910 if (unlikely(rc)) {
911 dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
912 __func__, rc);
913 goto out;
914 }
Uma Krishnan3351e4f2018-03-26 11:33:05 -0500915
916 /* Enable the AFU */
917 ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
Uma Krishnan9cc84292018-03-26 11:31:29 -0500918out:
919 return rc;
920}
921
922/**
Uma Krishnan48e077d2018-03-26 11:31:01 -0500923 * ocxlflash_create_afu() - create the AFU for OCXL
924 * @pdev: PCI device associated with the host.
925 *
926 * Return: AFU on success, NULL on failure
927 */
928static void *ocxlflash_create_afu(struct pci_dev *pdev)
929{
930 struct device *dev = &pdev->dev;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500931 struct ocxlflash_context *ctx;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500932 struct ocxl_hw_afu *afu;
Uma Krishnane9dfced2018-03-26 11:31:09 -0500933 int rc;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500934
935 afu = kzalloc(sizeof(*afu), GFP_KERNEL);
936 if (unlikely(!afu)) {
937 dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
938 goto out;
939 }
940
941 afu->pdev = pdev;
942 afu->dev = dev;
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500943 idr_init(&afu->idr);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500944
945 rc = ocxlflash_config_fn(pdev, afu);
946 if (unlikely(rc)) {
947 dev_err(dev, "%s: Function configuration failed rc=%d\n",
948 __func__, rc);
949 goto err1;
950 }
Uma Krishnan9cc84292018-03-26 11:31:29 -0500951
952 rc = ocxlflash_config_afu(pdev, afu);
953 if (unlikely(rc)) {
954 dev_err(dev, "%s: AFU configuration failed rc=%d\n",
955 __func__, rc);
Uma Krishnan73904822018-03-26 11:33:21 -0500956 goto err2;
Uma Krishnan9cc84292018-03-26 11:31:29 -0500957 }
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500958
959 ctx = ocxlflash_dev_context_init(pdev, afu);
960 if (IS_ERR(ctx)) {
961 rc = PTR_ERR(ctx);
962 dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
963 __func__, rc);
Uma Krishnan73904822018-03-26 11:33:21 -0500964 goto err3;
Uma Krishnanf6b4557c2018-03-26 11:31:53 -0500965 }
966
967 afu->ocxl_ctx = ctx;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500968out:
969 return afu;
Uma Krishnan73904822018-03-26 11:33:21 -0500970err3:
Uma Krishnan54370502018-03-26 11:32:37 -0500971 ocxlflash_unconfig_afu(afu);
Uma Krishnan73904822018-03-26 11:33:21 -0500972err2:
973 ocxlflash_unconfig_fn(pdev, afu);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500974err1:
Uma Krishnan429ebfa2018-03-26 11:32:00 -0500975 idr_destroy(&afu->idr);
Uma Krishnane9dfced2018-03-26 11:31:09 -0500976 kfree(afu);
977 afu = NULL;
978 goto out;
Uma Krishnan48e077d2018-03-26 11:31:01 -0500979}
Uma Krishnan76ebe012018-03-26 11:30:51 -0500980
Uma Krishnan56f1db12018-03-26 11:34:03 -0500981/**
982 * ctx_event_pending() - check for any event pending on the context
983 * @ctx: Context to be checked.
984 *
985 * Return: true if there is an event pending, false if none pending
986 */
987static inline bool ctx_event_pending(struct ocxlflash_context *ctx)
988{
Uma Krishnan66ae6442018-03-26 11:35:07 -0500989 if (ctx->pending_irq || ctx->pending_fault)
Uma Krishnan56f1db12018-03-26 11:34:03 -0500990 return true;
991
992 return false;
993}
994
995/**
996 * afu_poll() - poll the AFU for events on the context
997 * @file: File associated with the adapter context.
998 * @poll: Poll structure from the user.
999 *
1000 * Return: poll mask
1001 */
1002static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
1003{
1004 struct ocxlflash_context *ctx = file->private_data;
1005 struct device *dev = ctx->hw_afu->dev;
1006 ulong lock_flags;
1007 int mask = 0;
1008
1009 poll_wait(file, &ctx->wq, poll);
1010
1011 spin_lock_irqsave(&ctx->slock, lock_flags);
1012 if (ctx_event_pending(ctx))
1013 mask |= POLLIN | POLLRDNORM;
Uma Krishnanf81face2018-03-26 11:35:00 -05001014 else if (ctx->state == CLOSED)
Uma Krishnan56f1db12018-03-26 11:34:03 -05001015 mask |= POLLERR;
1016 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1017
1018 dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
1019 __func__, ctx->pe, mask);
1020
1021 return mask;
1022}
1023
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001024/**
1025 * afu_read() - perform a read on the context for any event
1026 * @file: File associated with the adapter context.
1027 * @buf: Buffer to receive the data.
1028 * @count: Size of buffer (maximum bytes that can be read).
1029 * @off: Offset.
1030 *
1031 * Return: size of the data read on success, -errno on failure
1032 */
1033static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
1034 loff_t *off)
1035{
1036 struct ocxlflash_context *ctx = file->private_data;
1037 struct device *dev = ctx->hw_afu->dev;
1038 struct cxl_event event;
1039 ulong lock_flags;
1040 ssize_t esize;
1041 ssize_t rc;
1042 int bit;
1043 DEFINE_WAIT(event_wait);
1044
1045 if (*off != 0) {
1046 dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
1047 __func__, *off);
1048 rc = -EINVAL;
1049 goto out;
1050 }
1051
1052 spin_lock_irqsave(&ctx->slock, lock_flags);
1053
1054 for (;;) {
1055 prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
1056
Uma Krishnanf81face2018-03-26 11:35:00 -05001057 if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001058 break;
1059
1060 if (file->f_flags & O_NONBLOCK) {
1061 dev_err(dev, "%s: File cannot be blocked on I/O\n",
1062 __func__);
1063 rc = -EAGAIN;
1064 goto err;
1065 }
1066
1067 if (signal_pending(current)) {
1068 dev_err(dev, "%s: Signal pending on the process\n",
1069 __func__);
1070 rc = -ERESTARTSYS;
1071 goto err;
1072 }
1073
1074 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1075 schedule();
1076 spin_lock_irqsave(&ctx->slock, lock_flags);
1077 }
1078
1079 finish_wait(&ctx->wq, &event_wait);
1080
1081 memset(&event, 0, sizeof(event));
1082 event.header.process_element = ctx->pe;
1083 event.header.size = sizeof(struct cxl_event_header);
1084 if (ctx->pending_irq) {
1085 esize = sizeof(struct cxl_event_afu_interrupt);
1086 event.header.size += esize;
1087 event.header.type = CXL_EVENT_AFU_INTERRUPT;
1088
1089 bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
1090 clear_bit(bit, &ctx->irq_bitmap);
1091 event.irq.irq = bit + 1;
1092 if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
1093 ctx->pending_irq = false;
Uma Krishnan66ae6442018-03-26 11:35:07 -05001094 } else if (ctx->pending_fault) {
1095 event.header.size += sizeof(struct cxl_event_data_storage);
1096 event.header.type = CXL_EVENT_DATA_STORAGE;
1097 event.fault.addr = ctx->fault_addr;
1098 event.fault.dsisr = ctx->fault_dsisr;
1099 ctx->pending_fault = false;
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001100 }
1101
1102 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1103
1104 if (copy_to_user(buf, &event, event.header.size)) {
1105 dev_err(dev, "%s: copy_to_user failed\n", __func__);
1106 rc = -EFAULT;
1107 goto out;
1108 }
1109
1110 rc = event.header.size;
1111out:
1112 return rc;
1113err:
1114 finish_wait(&ctx->wq, &event_wait);
1115 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1116 goto out;
1117}
1118
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001119/**
1120 * afu_release() - release and free the context
1121 * @inode: File inode pointer.
1122 * @file: File associated with the context.
1123 *
1124 * Return: 0 on success, -errno on failure
1125 */
1126static int afu_release(struct inode *inode, struct file *file)
1127{
1128 struct ocxlflash_context *ctx = file->private_data;
1129 int i;
1130
1131 /* Unmap and free the interrupts associated with the context */
1132 for (i = ctx->num_irqs; i >= 0; i--)
1133 afu_unmap_irq(0, ctx, i, ctx);
1134 free_afu_irqs(ctx);
1135
1136 return ocxlflash_release_context(ctx);
1137}
1138
1139/**
1140 * ocxlflash_mmap_fault() - mmap fault handler
1141 * @vmf: VM fault associated with current fault.
1142 *
1143 * Return: 0 on success, -errno on failure
1144 */
1145static int ocxlflash_mmap_fault(struct vm_fault *vmf)
1146{
1147 struct vm_area_struct *vma = vmf->vma;
1148 struct ocxlflash_context *ctx = vma->vm_file->private_data;
Uma Krishnanf81face2018-03-26 11:35:00 -05001149 struct device *dev = ctx->hw_afu->dev;
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001150 u64 mmio_area, offset;
1151
1152 offset = vmf->pgoff << PAGE_SHIFT;
1153 if (offset >= ctx->psn_size)
1154 return VM_FAULT_SIGBUS;
1155
Uma Krishnanf81face2018-03-26 11:35:00 -05001156 mutex_lock(&ctx->state_mutex);
1157 if (ctx->state != STARTED) {
1158 dev_err(dev, "%s: Context not started, state=%d\n",
1159 __func__, ctx->state);
1160 mutex_unlock(&ctx->state_mutex);
1161 return VM_FAULT_SIGBUS;
1162 }
1163 mutex_unlock(&ctx->state_mutex);
1164
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001165 mmio_area = ctx->psn_phys;
1166 mmio_area += offset;
1167
1168 vm_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
1169 return VM_FAULT_NOPAGE;
1170}
1171
1172static const struct vm_operations_struct ocxlflash_vmops = {
1173 .fault = ocxlflash_mmap_fault,
1174};
1175
1176/**
1177 * afu_mmap() - map the fault handler operations
1178 * @file: File associated with the context.
1179 * @vma: VM area associated with mapping.
1180 *
1181 * Return: 0 on success, -errno on failure
1182 */
1183static int afu_mmap(struct file *file, struct vm_area_struct *vma)
1184{
1185 struct ocxlflash_context *ctx = file->private_data;
1186
1187 if ((vma_pages(vma) + vma->vm_pgoff) >
1188 (ctx->psn_size >> PAGE_SHIFT))
1189 return -EINVAL;
1190
1191 vma->vm_flags |= VM_IO | VM_PFNMAP;
1192 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1193 vma->vm_ops = &ocxlflash_vmops;
1194 return 0;
1195}
1196
Uma Krishnan926a62f2018-03-26 11:32:09 -05001197static const struct file_operations ocxl_afu_fops = {
1198 .owner = THIS_MODULE,
Uma Krishnan56f1db12018-03-26 11:34:03 -05001199 .poll = afu_poll,
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001200 .read = afu_read,
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001201 .release = afu_release,
1202 .mmap = afu_mmap,
Uma Krishnan926a62f2018-03-26 11:32:09 -05001203};
1204
Uma Krishnan56f1db12018-03-26 11:34:03 -05001205#define PATCH_FOPS(NAME) \
1206 do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1207
Uma Krishnan926a62f2018-03-26 11:32:09 -05001208/**
1209 * ocxlflash_get_fd() - get file descriptor for an adapter context
1210 * @ctx_cookie: Adapter context.
1211 * @fops: File operations to be associated.
1212 * @fd: File descriptor to be returned back.
1213 *
1214 * Return: pointer to the file on success, ERR_PTR on failure
1215 */
1216static struct file *ocxlflash_get_fd(void *ctx_cookie,
1217 struct file_operations *fops, int *fd)
1218{
1219 struct ocxlflash_context *ctx = ctx_cookie;
1220 struct device *dev = ctx->hw_afu->dev;
1221 struct file *file;
1222 int flags, fdtmp;
1223 int rc = 0;
1224 char *name = NULL;
1225
1226 /* Only allow one fd per context */
1227 if (ctx->mapping) {
1228 dev_err(dev, "%s: Context is already mapped to an fd\n",
1229 __func__);
1230 rc = -EEXIST;
1231 goto err1;
1232 }
1233
1234 flags = O_RDWR | O_CLOEXEC;
1235
1236 /* This code is similar to anon_inode_getfd() */
1237 rc = get_unused_fd_flags(flags);
1238 if (unlikely(rc < 0)) {
1239 dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
1240 __func__, rc);
1241 goto err1;
1242 }
1243 fdtmp = rc;
1244
Uma Krishnan56f1db12018-03-26 11:34:03 -05001245 /* Patch the file ops that are not defined */
1246 if (fops) {
1247 PATCH_FOPS(poll);
Uma Krishnan03aa9c52018-03-26 11:34:12 -05001248 PATCH_FOPS(read);
Uma Krishnan93b8f8d2018-03-26 11:34:20 -05001249 PATCH_FOPS(release);
1250 PATCH_FOPS(mmap);
Uma Krishnan56f1db12018-03-26 11:34:03 -05001251 } else /* Use default ops */
Uma Krishnan926a62f2018-03-26 11:32:09 -05001252 fops = (struct file_operations *)&ocxl_afu_fops;
1253
1254 name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
1255 file = ocxlflash_getfile(dev, name, fops, ctx, flags);
1256 kfree(name);
1257 if (IS_ERR(file)) {
1258 rc = PTR_ERR(file);
1259 dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
1260 __func__, rc);
1261 goto err2;
1262 }
1263
1264 ctx->mapping = file->f_mapping;
1265 *fd = fdtmp;
1266out:
1267 return file;
1268err2:
1269 put_unused_fd(fdtmp);
1270err1:
1271 file = ERR_PTR(rc);
1272 goto out;
1273}
1274
Uma Krishnanb18718c2018-03-26 11:32:20 -05001275/**
1276 * ocxlflash_fops_get_context() - get the context associated with the file
1277 * @file: File associated with the adapter context.
1278 *
1279 * Return: pointer to the context
1280 */
1281static void *ocxlflash_fops_get_context(struct file *file)
1282{
1283 return file->private_data;
1284}
1285
Uma Krishnan762c7e92018-03-26 11:33:55 -05001286/**
1287 * ocxlflash_afu_irq() - interrupt handler for user contexts
1288 * @irq: Interrupt number.
1289 * @data: Private data provided at interrupt registration, the context.
1290 *
1291 * Return: Always return IRQ_HANDLED.
1292 */
1293static irqreturn_t ocxlflash_afu_irq(int irq, void *data)
1294{
1295 struct ocxlflash_context *ctx = data;
1296 struct device *dev = ctx->hw_afu->dev;
1297 int i;
1298
1299 dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
1300 __func__, ctx->pe, irq);
1301
1302 for (i = 0; i < ctx->num_irqs; i++) {
1303 if (ctx->irqs[i].virq == irq)
1304 break;
1305 }
1306 if (unlikely(i >= ctx->num_irqs)) {
1307 dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
1308 goto out;
1309 }
1310
1311 spin_lock(&ctx->slock);
1312 set_bit(i - 1, &ctx->irq_bitmap);
1313 ctx->pending_irq = true;
1314 spin_unlock(&ctx->slock);
Uma Krishnan56f1db12018-03-26 11:34:03 -05001315
1316 wake_up_all(&ctx->wq);
Uma Krishnan762c7e92018-03-26 11:33:55 -05001317out:
1318 return IRQ_HANDLED;
1319}
1320
1321/**
1322 * ocxlflash_start_work() - start a user context
1323 * @ctx_cookie: Context to be started.
1324 * @num_irqs: Number of interrupts requested.
1325 *
1326 * Return: 0 on success, -errno on failure
1327 */
1328static int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
1329{
1330 struct ocxlflash_context *ctx = ctx_cookie;
1331 struct ocxl_hw_afu *afu = ctx->hw_afu;
1332 struct device *dev = afu->dev;
1333 char *name;
1334 int rc = 0;
1335 int i;
1336
1337 rc = alloc_afu_irqs(ctx, num_irqs);
1338 if (unlikely(rc < 0)) {
1339 dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
1340 goto out;
1341 }
1342
1343 for (i = 0; i < num_irqs; i++) {
1344 name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
1345 dev_name(dev), ctx->pe, i);
1346 rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
1347 kfree(name);
1348 if (unlikely(rc < 0)) {
1349 dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
1350 __func__, rc);
1351 goto err;
1352 }
1353 }
1354
1355 rc = start_context(ctx);
1356 if (unlikely(rc)) {
1357 dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
1358 goto err;
1359 }
1360out:
1361 return rc;
1362err:
1363 for (i = i-1; i >= 0; i--)
1364 afu_unmap_irq(0, ctx, i, ctx);
1365 free_afu_irqs(ctx);
1366 goto out;
Uma Krishnane117c3c2018-03-26 11:34:27 -05001367};
1368
1369/**
1370 * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
1371 * @file: File installed with adapter file descriptor.
1372 * @vma: VM area associated with mapping.
1373 *
1374 * Return: 0 on success, -errno on failure
1375 */
1376static int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
1377{
1378 return afu_mmap(file, vma);
1379}
1380
1381/**
1382 * ocxlflash_fd_release() - release the context associated with the file
1383 * @inode: File inode pointer.
1384 * @file: File associated with the adapter context.
1385 *
1386 * Return: 0 on success, -errno on failure
1387 */
1388static int ocxlflash_fd_release(struct inode *inode, struct file *file)
1389{
1390 return afu_release(inode, file);
Uma Krishnan762c7e92018-03-26 11:33:55 -05001391}
1392
Uma Krishnan76ebe012018-03-26 11:30:51 -05001393/* Backend ops to ocxlflash services */
1394const struct cxlflash_backend_ops cxlflash_ocxl_ops = {
1395 .module = THIS_MODULE,
Uma Krishnan012f3942018-03-26 11:32:56 -05001396 .psa_map = ocxlflash_psa_map,
1397 .psa_unmap = ocxlflash_psa_unmap,
Uma Krishnanb18718c2018-03-26 11:32:20 -05001398 .process_element = ocxlflash_process_element,
Uma Krishnana06b1cf2018-03-26 11:33:48 -05001399 .map_afu_irq = ocxlflash_map_afu_irq,
1400 .unmap_afu_irq = ocxlflash_unmap_afu_irq,
Uma Krishnan402a55e2018-03-26 11:34:35 -05001401 .get_irq_objhndl = ocxlflash_get_irq_objhndl,
Uma Krishnan6b938ac2018-03-26 11:32:48 -05001402 .start_context = ocxlflash_start_context,
Uma Krishnanc207b572018-03-26 11:33:35 -05001403 .stop_context = ocxlflash_stop_context,
Uma Krishnanf6b4557c2018-03-26 11:31:53 -05001404 .set_master = ocxlflash_set_master,
1405 .get_context = ocxlflash_get_context,
1406 .dev_context_init = ocxlflash_dev_context_init,
1407 .release_context = ocxlflash_release_context,
Uma Krishnan8b7a5522018-03-26 11:32:29 -05001408 .perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
Uma Krishnan119c9202018-03-26 11:33:14 -05001409 .read_adapter_vpd = ocxlflash_read_adapter_vpd,
Uma Krishnanbc65c1c2018-03-26 11:33:41 -05001410 .allocate_afu_irqs = ocxlflash_allocate_afu_irqs,
1411 .free_afu_irqs = ocxlflash_free_afu_irqs,
Uma Krishnan48e077d2018-03-26 11:31:01 -05001412 .create_afu = ocxlflash_create_afu,
1413 .destroy_afu = ocxlflash_destroy_afu,
Uma Krishnan926a62f2018-03-26 11:32:09 -05001414 .get_fd = ocxlflash_get_fd,
Uma Krishnanb18718c2018-03-26 11:32:20 -05001415 .fops_get_context = ocxlflash_fops_get_context,
Uma Krishnan762c7e92018-03-26 11:33:55 -05001416 .start_work = ocxlflash_start_work,
Uma Krishnane117c3c2018-03-26 11:34:27 -05001417 .fd_mmap = ocxlflash_fd_mmap,
1418 .fd_release = ocxlflash_fd_release,
Uma Krishnan76ebe012018-03-26 11:30:51 -05001419};