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