blob: 0b5cb6cf91a054fbc11e55b529a20cd1a2249a6c [file] [log] [blame]
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001/*
2 * Copyright 2014 IBM Corp.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include <linux/pci.h>
11#include <linux/slab.h>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100012#include <linux/file.h>
13#include <misc/cxl.h>
Ian Munsiea2f67d52016-07-14 07:17:10 +100014#include <linux/msi.h>
Frederic Barratbdecf762016-11-18 23:00:31 +110015#include <linux/module.h>
16#include <linux/mount.h>
Christophe Lombard6dd2d232017-04-07 16:11:55 +020017#include <linux/sched/mm.h>
Frederic Barrat03b8abe2017-09-03 20:15:13 +020018#include <linux/mmu_context.h>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100019
20#include "cxl.h"
21
Frederic Barratbdecf762016-11-18 23:00:31 +110022/*
23 * Since we want to track memory mappings to be able to force-unmap
24 * when the AFU is no longer reachable, we need an inode. For devices
25 * opened through the cxl user API, this is not a problem, but a
26 * userland process can also get a cxl fd through the cxl_get_fd()
27 * API, which is used by the cxlflash driver.
28 *
29 * Therefore we implement our own simple pseudo-filesystem and inode
30 * allocator. We don't use the anonymous inode, as we need the
31 * meta-data associated with it (address_space) and it is shared by
32 * other drivers/processes, so it could lead to cxl unmapping VMAs
33 * from random processes.
34 */
35
36#define CXL_PSEUDO_FS_MAGIC 0x1697697f
37
38static int cxl_fs_cnt;
39static struct vfsmount *cxl_vfs_mount;
40
41static const struct dentry_operations cxl_fs_dops = {
42 .d_dname = simple_dname,
43};
44
45static struct dentry *cxl_fs_mount(struct file_system_type *fs_type, int flags,
46 const char *dev_name, void *data)
47{
48 return mount_pseudo(fs_type, "cxl:", NULL, &cxl_fs_dops,
49 CXL_PSEUDO_FS_MAGIC);
50}
51
52static struct file_system_type cxl_fs_type = {
53 .name = "cxl",
54 .owner = THIS_MODULE,
55 .mount = cxl_fs_mount,
56 .kill_sb = kill_anon_super,
57};
58
59
60void cxl_release_mapping(struct cxl_context *ctx)
61{
62 if (ctx->kernelapi && ctx->mapping)
63 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
64}
65
66static struct file *cxl_getfile(const char *name,
67 const struct file_operations *fops,
68 void *priv, int flags)
69{
70 struct qstr this;
71 struct path path;
72 struct file *file;
73 struct inode *inode = NULL;
74 int rc;
75
76 /* strongly inspired by anon_inode_getfile() */
77
78 if (fops->owner && !try_module_get(fops->owner))
79 return ERR_PTR(-ENOENT);
80
81 rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
82 if (rc < 0) {
83 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
84 file = ERR_PTR(rc);
85 goto err_module;
86 }
87
88 inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
89 if (IS_ERR(inode)) {
90 file = ERR_CAST(inode);
91 goto err_fs;
92 }
93
94 file = ERR_PTR(-ENOMEM);
95 this.name = name;
96 this.len = strlen(name);
97 this.hash = 0;
98 path.dentry = d_alloc_pseudo(cxl_vfs_mount->mnt_sb, &this);
99 if (!path.dentry)
100 goto err_inode;
101
102 path.mnt = mntget(cxl_vfs_mount);
103 d_instantiate(path.dentry, inode);
104
Al Viroc9c554f2018-07-11 14:19:04 -0400105 file = alloc_file(&path, flags & (O_ACCMODE | O_NONBLOCK), fops);
Al Virod2027972018-06-09 09:43:13 -0400106 if (IS_ERR(file)) {
107 path_put(&path);
108 goto err_fs;
109 }
Frederic Barratbdecf762016-11-18 23:00:31 +1100110 file->private_data = priv;
111
112 return file;
113
Frederic Barratbdecf762016-11-18 23:00:31 +1100114err_inode:
115 iput(inode);
116err_fs:
117 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
118err_module:
119 module_put(fops->owner);
120 return file;
121}
122
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000123struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
124{
125 struct cxl_afu *afu;
126 struct cxl_context *ctx;
127 int rc;
128
129 afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000130 if (IS_ERR(afu))
131 return ERR_CAST(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000132
133 ctx = cxl_context_alloc();
Christophe Jaillet5cd4f5c2016-10-30 20:35:57 +0100134 if (!ctx)
135 return ERR_PTR(-ENOMEM);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000136
Ian Munsie55e07662015-08-27 19:50:19 +1000137 ctx->kernelapi = true;
138
Ian Munsie55e07662015-08-27 19:50:19 +1000139 /* Make it a slave context. We can promote it later? */
Frederic Barratbdecf762016-11-18 23:00:31 +1100140 rc = cxl_context_init(ctx, afu, false);
Ian Munsie55e07662015-08-27 19:50:19 +1000141 if (rc)
Frederic Barratbdecf762016-11-18 23:00:31 +1100142 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +1000143
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000144 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +1000145
146err_ctx:
147 kfree(ctx);
Ian Munsieaf2a50b2015-08-27 19:50:18 +1000148 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000149}
150EXPORT_SYMBOL_GPL(cxl_dev_context_init);
151
152struct cxl_context *cxl_get_context(struct pci_dev *dev)
153{
154 return dev->dev.archdata.cxl_ctx;
155}
156EXPORT_SYMBOL_GPL(cxl_get_context);
157
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000158int cxl_release_context(struct cxl_context *ctx)
159{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +1000160 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000161 return -EBUSY;
162
163 cxl_context_free(ctx);
164
165 return 0;
166}
167EXPORT_SYMBOL_GPL(cxl_release_context);
168
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000169static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
170{
171 __u16 range;
172 int r;
173
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000174 for (r = 0; r < CXL_IRQ_RANGES; r++) {
175 range = ctx->irqs.range[r];
176 if (num < range) {
177 return ctx->irqs.offset[r] + num;
178 }
179 num -= range;
180 }
181 return 0;
182}
183
Ian Munsiecbce0912016-07-14 07:17:09 +1000184int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
185{
186 if (*ctx == NULL || *afu_irq == 0) {
187 *afu_irq = 1;
188 *ctx = cxl_get_context(pdev);
189 } else {
190 (*afu_irq)++;
191 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
192 *ctx = list_next_entry(*ctx, extra_irq_contexts);
193 *afu_irq = 1;
194 }
195 }
196 return cxl_find_afu_irq(*ctx, *afu_irq);
197}
198/* Exported via cxl_base */
Michael Neulingad42de82016-06-24 08:47:07 +0200199
200int cxl_set_priv(struct cxl_context *ctx, void *priv)
201{
202 if (!ctx)
203 return -EINVAL;
204
205 ctx->priv = priv;
206
207 return 0;
208}
209EXPORT_SYMBOL_GPL(cxl_set_priv);
210
211void *cxl_get_priv(struct cxl_context *ctx)
212{
213 if (!ctx)
214 return ERR_PTR(-EINVAL);
215
216 return ctx->priv;
217}
218EXPORT_SYMBOL_GPL(cxl_get_priv);
219
Frederic Barratd601ea92016-03-04 12:26:40 +0100220int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
221{
222 int res;
223 irq_hw_number_t hwirq;
224
225 if (num == 0)
226 num = ctx->afu->pp_irqs;
227 res = afu_allocate_irqs(ctx, num);
Ian Munsie292841b2016-05-24 02:14:05 +1000228 if (res)
229 return res;
230
231 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
Frederic Barratd601ea92016-03-04 12:26:40 +0100232 /* In a guest, the PSL interrupt is not multiplexed. It was
233 * allocated above, and we need to set its handler
234 */
235 hwirq = cxl_find_afu_irq(ctx, 0);
236 if (hwirq)
237 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
238 }
Ian Munsie292841b2016-05-24 02:14:05 +1000239
240 if (ctx->status == STARTED) {
241 if (cxl_ops->update_ivtes)
242 cxl_ops->update_ivtes(ctx);
243 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
244 }
245
Frederic Barratd601ea92016-03-04 12:26:40 +0100246 return res;
247}
248EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
249
250void cxl_free_afu_irqs(struct cxl_context *ctx)
251{
252 irq_hw_number_t hwirq;
253 unsigned int virq;
254
255 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
256 hwirq = cxl_find_afu_irq(ctx, 0);
257 if (hwirq) {
258 virq = irq_find_mapping(NULL, hwirq);
259 if (virq)
260 cxl_unmap_irq(virq, ctx);
261 }
262 }
263 afu_irq_name_free(ctx);
264 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
265}
266EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
267
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000268int cxl_map_afu_irq(struct cxl_context *ctx, int num,
269 irq_handler_t handler, void *cookie, char *name)
270{
271 irq_hw_number_t hwirq;
272
273 /*
274 * Find interrupt we are to register.
275 */
276 hwirq = cxl_find_afu_irq(ctx, num);
277 if (!hwirq)
278 return -ENOENT;
279
280 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
281}
282EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
283
284void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
285{
286 irq_hw_number_t hwirq;
287 unsigned int virq;
288
289 hwirq = cxl_find_afu_irq(ctx, num);
290 if (!hwirq)
291 return;
292
293 virq = irq_find_mapping(NULL, hwirq);
294 if (virq)
295 cxl_unmap_irq(virq, cookie);
296}
297EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
298
299/*
300 * Start a context
301 * Code here similar to afu_ioctl_start_work().
302 */
303int cxl_start_context(struct cxl_context *ctx, u64 wed,
304 struct task_struct *task)
305{
306 int rc = 0;
307 bool kernel = true;
308
309 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
310
311 mutex_lock(&ctx->status_mutex);
312 if (ctx->status == STARTED)
313 goto out; /* already started */
314
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530315 /*
316 * Increment the mapped context count for adapter. This also checks
317 * if adapter_context_lock is taken.
318 */
319 rc = cxl_adapter_context_get(ctx->afu->adapter);
320 if (rc)
321 goto out;
322
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000323 if (task) {
324 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000325 kernel = false;
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000326 ctx->real_mode = false;
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200327
328 /* acquire a reference to the task's mm */
329 ctx->mm = get_task_mm(current);
330
331 /* ensure this mm_struct can't be freed */
332 cxl_context_mm_count_get(ctx);
333
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200334 if (ctx->mm) {
335 /* decrement the use count from above */
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200336 mmput(ctx->mm);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200337 /* make TLBIs for this context global */
338 mm_context_add_copro(ctx->mm);
339 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000340 }
341
Frederic Barrat197267d2017-08-30 12:15:49 +0200342 /*
343 * Increment driver use count. Enables global TLBIs for hash
344 * and callbacks to handle the segment table
345 */
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000346 cxl_ctx_get();
347
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200348 /* See the comment in afu_ioctl_start_work() */
349 smp_mb();
350
Frederic Barrat5be587b2016-03-04 12:26:28 +0100351 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000352 put_pid(ctx->pid);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200353 ctx->pid = NULL;
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530354 cxl_adapter_context_put(ctx->afu->adapter);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000355 cxl_ctx_put();
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200356 if (task) {
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200357 cxl_context_mm_count_put(ctx);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200358 if (ctx->mm)
359 mm_context_remove_copro(ctx->mm);
360 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000361 goto out;
362 }
363
364 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000365out:
366 mutex_unlock(&ctx->status_mutex);
367 return rc;
368}
369EXPORT_SYMBOL_GPL(cxl_start_context);
370
371int cxl_process_element(struct cxl_context *ctx)
372{
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100373 return ctx->external_pe;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000374}
375EXPORT_SYMBOL_GPL(cxl_process_element);
376
377/* Stop a context. Returns 0 on success, otherwise -Errno */
378int cxl_stop_context(struct cxl_context *ctx)
379{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000380 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000381}
382EXPORT_SYMBOL_GPL(cxl_stop_context);
383
384void cxl_set_master(struct cxl_context *ctx)
385{
386 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000387}
388EXPORT_SYMBOL_GPL(cxl_set_master);
389
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000390int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
391{
392 if (ctx->status == STARTED) {
393 /*
394 * We could potentially update the PE and issue an update LLCMD
395 * to support this, but it doesn't seem to have a good use case
396 * since it's trivial to just create a second kernel context
397 * with different translation modes, so until someone convinces
398 * me otherwise:
399 */
400 return -EBUSY;
401 }
402
403 ctx->real_mode = real_mode;
404 return 0;
405}
406EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
407
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000408/* wrappers around afu_* file ops which are EXPORTED */
409int cxl_fd_open(struct inode *inode, struct file *file)
410{
411 return afu_open(inode, file);
412}
413EXPORT_SYMBOL_GPL(cxl_fd_open);
414int cxl_fd_release(struct inode *inode, struct file *file)
415{
416 return afu_release(inode, file);
417}
418EXPORT_SYMBOL_GPL(cxl_fd_release);
419long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
420{
421 return afu_ioctl(file, cmd, arg);
422}
423EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
424int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
425{
426 return afu_mmap(file, vm);
427}
428EXPORT_SYMBOL_GPL(cxl_fd_mmap);
Al Viroafc9a422017-07-03 06:39:46 -0400429__poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000430{
431 return afu_poll(file, poll);
432}
433EXPORT_SYMBOL_GPL(cxl_fd_poll);
434ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
435 loff_t *off)
436{
437 return afu_read(file, buf, count, off);
438}
439EXPORT_SYMBOL_GPL(cxl_fd_read);
440
441#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
442
443/* Get a struct file and fd for a context and attach the ops */
444struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
445 int *fd)
446{
447 struct file *file;
448 int rc, flags, fdtmp;
Frederic Barratbdecf762016-11-18 23:00:31 +1100449 char *name = NULL;
450
451 /* only allow one per context */
452 if (ctx->mapping)
453 return ERR_PTR(-EEXIST);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000454
455 flags = O_RDWR | O_CLOEXEC;
456
457 /* This code is similar to anon_inode_getfd() */
458 rc = get_unused_fd_flags(flags);
459 if (rc < 0)
460 return ERR_PTR(rc);
461 fdtmp = rc;
462
463 /*
464 * Patch the file ops. Needs to be careful that this is rentrant safe.
465 */
466 if (fops) {
467 PATCH_FOPS(open);
468 PATCH_FOPS(poll);
469 PATCH_FOPS(read);
470 PATCH_FOPS(release);
471 PATCH_FOPS(unlocked_ioctl);
472 PATCH_FOPS(compat_ioctl);
473 PATCH_FOPS(mmap);
474 } else /* use default ops */
475 fops = (struct file_operations *)&afu_fops;
476
Frederic Barratbdecf762016-11-18 23:00:31 +1100477 name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
478 file = cxl_getfile(name, fops, ctx, flags);
479 kfree(name);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000480 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000481 goto err_fd;
482
Frederic Barratbdecf762016-11-18 23:00:31 +1100483 cxl_context_set_mapping(ctx, file->f_mapping);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000484 *fd = fdtmp;
485 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000486
487err_fd:
488 put_unused_fd(fdtmp);
489 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000490}
491EXPORT_SYMBOL_GPL(cxl_get_fd);
492
493struct cxl_context *cxl_fops_get_context(struct file *file)
494{
495 return file->private_data;
496}
497EXPORT_SYMBOL_GPL(cxl_fops_get_context);
498
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200499void cxl_set_driver_ops(struct cxl_context *ctx,
500 struct cxl_afu_driver_ops *ops)
501{
502 WARN_ON(!ops->fetch_event || !ops->event_delivered);
503 atomic_set(&ctx->afu_driver_events, 0);
504 ctx->afu_driver_ops = ops;
505}
506EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
507
508void cxl_context_events_pending(struct cxl_context *ctx,
509 unsigned int new_events)
510{
511 atomic_add(new_events, &ctx->afu_driver_events);
512 wake_up_all(&ctx->wq);
513}
514EXPORT_SYMBOL_GPL(cxl_context_events_pending);
515
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000516int cxl_start_work(struct cxl_context *ctx,
517 struct cxl_ioctl_start_work *work)
518{
519 int rc;
520
521 /* code taken from afu_ioctl_start_work */
522 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
523 work->num_interrupts = ctx->afu->pp_irqs;
524 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
525 (work->num_interrupts > ctx->afu->irqs_max)) {
526 return -EINVAL;
527 }
528
529 rc = afu_register_irqs(ctx, work->num_interrupts);
530 if (rc)
531 return rc;
532
533 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
534 if (rc < 0) {
535 afu_release_irqs(ctx, ctx);
536 return rc;
537 }
538
539 return 0;
540}
541EXPORT_SYMBOL_GPL(cxl_start_work);
542
543void __iomem *cxl_psa_map(struct cxl_context *ctx)
544{
Frederic Barratcca44c02016-03-04 12:26:27 +0100545 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000546 return NULL;
547
548 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100549 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000550 return ioremap(ctx->psn_phys, ctx->psn_size);
551}
552EXPORT_SYMBOL_GPL(cxl_psa_map);
553
554void cxl_psa_unmap(void __iomem *addr)
555{
556 iounmap(addr);
557}
558EXPORT_SYMBOL_GPL(cxl_psa_unmap);
559
560int cxl_afu_reset(struct cxl_context *ctx)
561{
562 struct cxl_afu *afu = ctx->afu;
563 int rc;
564
Frederic Barrat5be587b2016-03-04 12:26:28 +0100565 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000566 if (rc)
567 return rc;
568
Frederic Barrat5be587b2016-03-04 12:26:28 +0100569 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000570}
571EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000572
573void cxl_perst_reloads_same_image(struct cxl_afu *afu,
574 bool perst_reloads_same_image)
575{
576 afu->adapter->perst_same_image = perst_reloads_same_image;
577}
578EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
Frederic Barratd601ea92016-03-04 12:26:40 +0100579
580ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
581{
582 struct cxl_afu *afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000583 if (IS_ERR(afu))
584 return -ENODEV;
Frederic Barratd601ea92016-03-04 12:26:40 +0100585
586 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
587}
588EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
Ian Munsie79384e42016-07-14 07:17:08 +1000589
590int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
591{
592 struct cxl_afu *afu = cxl_pci_to_afu(dev);
593 if (IS_ERR(afu))
594 return -ENODEV;
595
596 if (irqs > afu->adapter->user_irqs)
597 return -EINVAL;
598
599 /* Limit user_irqs to prevent the user increasing this via sysfs */
600 afu->adapter->user_irqs = irqs;
601 afu->irqs_max = irqs;
602
603 return 0;
604}
605EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
606
607int cxl_get_max_irqs_per_process(struct pci_dev *dev)
608{
609 struct cxl_afu *afu = cxl_pci_to_afu(dev);
610 if (IS_ERR(afu))
611 return -ENODEV;
612
613 return afu->irqs_max;
614}
615EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
Ian Munsiea2f67d52016-07-14 07:17:10 +1000616
617/*
618 * This is a special interrupt allocation routine called from the PHB's MSI
619 * setup function. When capi interrupts are allocated in this manner they must
620 * still be associated with a running context, but since the MSI APIs have no
621 * way to specify this we use the default context associated with the device.
622 *
623 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
624 * interrupt number, so in order to overcome this their driver informs us of
625 * the restriction by setting the maximum interrupts per context, and we
626 * allocate additional contexts as necessary so that we can keep the AFU
627 * interrupt number within the supported range.
628 */
629int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
630{
631 struct cxl_context *ctx, *new_ctx, *default_ctx;
632 int remaining;
633 int rc;
634
635 ctx = default_ctx = cxl_get_context(pdev);
636 if (WARN_ON(!default_ctx))
637 return -ENODEV;
638
639 remaining = nvec;
640 while (remaining > 0) {
641 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
642 if (rc) {
643 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
644 return rc;
645 }
646 remaining -= ctx->afu->irqs_max;
647
648 if (ctx != default_ctx && default_ctx->status == STARTED) {
649 WARN_ON(cxl_start_context(ctx,
650 be64_to_cpu(default_ctx->elem->common.wed),
651 NULL));
652 }
653
654 if (remaining > 0) {
655 new_ctx = cxl_dev_context_init(pdev);
Christophe Jaillet28e323e2016-10-30 22:34:51 +0100656 if (IS_ERR(new_ctx)) {
Ian Munsiea2f67d52016-07-14 07:17:10 +1000657 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
658 return -ENOSPC;
659 }
660 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
661 ctx = new_ctx;
662 }
663 }
664
665 return 0;
666}
667/* Exported via cxl_base */
668
669void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
670{
671 struct cxl_context *ctx, *pos, *tmp;
672
673 ctx = cxl_get_context(pdev);
674 if (WARN_ON(!ctx))
675 return;
676
677 cxl_free_afu_irqs(ctx);
678 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
679 cxl_stop_context(pos);
680 cxl_free_afu_irqs(pos);
681 list_del(&pos->extra_irq_contexts);
682 cxl_release_context(pos);
683 }
684}
685/* Exported via cxl_base */