]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/misc/cxl/api.c
scsi_dh: don't try to load a device handler during async probing
[karo-tx-linux.git] / drivers / misc / cxl / api.c
1 /*
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>
12 #include <linux/anon_inodes.h>
13 #include <linux/file.h>
14 #include <misc/cxl.h>
15 #include <linux/fs.h>
16
17 #include "cxl.h"
18
19 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
20 {
21         struct address_space *mapping;
22         struct cxl_afu *afu;
23         struct cxl_context  *ctx;
24         int rc;
25
26         afu = cxl_pci_to_afu(dev);
27
28         get_device(&afu->dev);
29         ctx = cxl_context_alloc();
30         if (IS_ERR(ctx)) {
31                 rc = PTR_ERR(ctx);
32                 goto err_dev;
33         }
34
35         ctx->kernelapi = true;
36
37         /*
38          * Make our own address space since we won't have one from the
39          * filesystem like the user api has, and even if we do associate a file
40          * with this context we don't want to use the global anonymous inode's
41          * address space as that can invalidate unrelated users:
42          */
43         mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
44         if (!mapping) {
45                 rc = -ENOMEM;
46                 goto err_ctx;
47         }
48         address_space_init_once(mapping);
49
50         /* Make it a slave context.  We can promote it later? */
51         rc = cxl_context_init(ctx, afu, false, mapping);
52         if (rc)
53                 goto err_mapping;
54
55         cxl_assign_psn_space(ctx);
56
57         return ctx;
58
59 err_mapping:
60         kfree(mapping);
61 err_ctx:
62         kfree(ctx);
63 err_dev:
64         put_device(&afu->dev);
65         return ERR_PTR(rc);
66 }
67 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
68
69 struct cxl_context *cxl_get_context(struct pci_dev *dev)
70 {
71         return dev->dev.archdata.cxl_ctx;
72 }
73 EXPORT_SYMBOL_GPL(cxl_get_context);
74
75 struct device *cxl_get_phys_dev(struct pci_dev *dev)
76 {
77         struct cxl_afu *afu;
78
79         afu = cxl_pci_to_afu(dev);
80
81         return afu->adapter->dev.parent;
82 }
83 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
84
85 int cxl_release_context(struct cxl_context *ctx)
86 {
87         if (ctx->status >= STARTED)
88                 return -EBUSY;
89
90         put_device(&ctx->afu->dev);
91
92         cxl_context_free(ctx);
93
94         return 0;
95 }
96 EXPORT_SYMBOL_GPL(cxl_release_context);
97
98 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
99 {
100         if (num == 0)
101                 num = ctx->afu->pp_irqs;
102         return afu_allocate_irqs(ctx, num);
103 }
104 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
105
106 void cxl_free_afu_irqs(struct cxl_context *ctx)
107 {
108         cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
109 }
110 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
111
112 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
113 {
114         __u16 range;
115         int r;
116
117         WARN_ON(num == 0);
118
119         for (r = 0; r < CXL_IRQ_RANGES; r++) {
120                 range = ctx->irqs.range[r];
121                 if (num < range) {
122                         return ctx->irqs.offset[r] + num;
123                 }
124                 num -= range;
125         }
126         return 0;
127 }
128
129 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
130                     irq_handler_t handler, void *cookie, char *name)
131 {
132         irq_hw_number_t hwirq;
133
134         /*
135          * Find interrupt we are to register.
136          */
137         hwirq = cxl_find_afu_irq(ctx, num);
138         if (!hwirq)
139                 return -ENOENT;
140
141         return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
142 }
143 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
144
145 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
146 {
147         irq_hw_number_t hwirq;
148         unsigned int virq;
149
150         hwirq = cxl_find_afu_irq(ctx, num);
151         if (!hwirq)
152                 return;
153
154         virq = irq_find_mapping(NULL, hwirq);
155         if (virq)
156                 cxl_unmap_irq(virq, cookie);
157 }
158 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
159
160 /*
161  * Start a context
162  * Code here similar to afu_ioctl_start_work().
163  */
164 int cxl_start_context(struct cxl_context *ctx, u64 wed,
165                       struct task_struct *task)
166 {
167         int rc = 0;
168         bool kernel = true;
169
170         pr_devel("%s: pe: %i\n", __func__, ctx->pe);
171
172         mutex_lock(&ctx->status_mutex);
173         if (ctx->status == STARTED)
174                 goto out; /* already started */
175
176         if (task) {
177                 ctx->pid = get_task_pid(task, PIDTYPE_PID);
178                 get_pid(ctx->pid);
179                 kernel = false;
180         }
181
182         cxl_ctx_get();
183
184         if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
185                 put_pid(ctx->pid);
186                 cxl_ctx_put();
187                 goto out;
188         }
189
190         ctx->status = STARTED;
191 out:
192         mutex_unlock(&ctx->status_mutex);
193         return rc;
194 }
195 EXPORT_SYMBOL_GPL(cxl_start_context);
196
197 int cxl_process_element(struct cxl_context *ctx)
198 {
199         return ctx->pe;
200 }
201 EXPORT_SYMBOL_GPL(cxl_process_element);
202
203 /* Stop a context.  Returns 0 on success, otherwise -Errno */
204 int cxl_stop_context(struct cxl_context *ctx)
205 {
206         return __detach_context(ctx);
207 }
208 EXPORT_SYMBOL_GPL(cxl_stop_context);
209
210 void cxl_set_master(struct cxl_context *ctx)
211 {
212         ctx->master = true;
213         cxl_assign_psn_space(ctx);
214 }
215 EXPORT_SYMBOL_GPL(cxl_set_master);
216
217 /* wrappers around afu_* file ops which are EXPORTED */
218 int cxl_fd_open(struct inode *inode, struct file *file)
219 {
220         return afu_open(inode, file);
221 }
222 EXPORT_SYMBOL_GPL(cxl_fd_open);
223 int cxl_fd_release(struct inode *inode, struct file *file)
224 {
225         return afu_release(inode, file);
226 }
227 EXPORT_SYMBOL_GPL(cxl_fd_release);
228 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
229 {
230         return afu_ioctl(file, cmd, arg);
231 }
232 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
233 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
234 {
235         return afu_mmap(file, vm);
236 }
237 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
238 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
239 {
240         return afu_poll(file, poll);
241 }
242 EXPORT_SYMBOL_GPL(cxl_fd_poll);
243 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
244                         loff_t *off)
245 {
246         return afu_read(file, buf, count, off);
247 }
248 EXPORT_SYMBOL_GPL(cxl_fd_read);
249
250 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
251
252 /* Get a struct file and fd for a context and attach the ops */
253 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
254                         int *fd)
255 {
256         struct file *file;
257         int rc, flags, fdtmp;
258
259         flags = O_RDWR | O_CLOEXEC;
260
261         /* This code is similar to anon_inode_getfd() */
262         rc = get_unused_fd_flags(flags);
263         if (rc < 0)
264                 return ERR_PTR(rc);
265         fdtmp = rc;
266
267         /*
268          * Patch the file ops.  Needs to be careful that this is rentrant safe.
269          */
270         if (fops) {
271                 PATCH_FOPS(open);
272                 PATCH_FOPS(poll);
273                 PATCH_FOPS(read);
274                 PATCH_FOPS(release);
275                 PATCH_FOPS(unlocked_ioctl);
276                 PATCH_FOPS(compat_ioctl);
277                 PATCH_FOPS(mmap);
278         } else /* use default ops */
279                 fops = (struct file_operations *)&afu_fops;
280
281         file = anon_inode_getfile("cxl", fops, ctx, flags);
282         if (IS_ERR(file))
283                 goto err_fd;
284
285         file->f_mapping = ctx->mapping;
286
287         *fd = fdtmp;
288         return file;
289
290 err_fd:
291         put_unused_fd(fdtmp);
292         return NULL;
293 }
294 EXPORT_SYMBOL_GPL(cxl_get_fd);
295
296 struct cxl_context *cxl_fops_get_context(struct file *file)
297 {
298         return file->private_data;
299 }
300 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
301
302 int cxl_start_work(struct cxl_context *ctx,
303                    struct cxl_ioctl_start_work *work)
304 {
305         int rc;
306
307         /* code taken from afu_ioctl_start_work */
308         if (!(work->flags & CXL_START_WORK_NUM_IRQS))
309                 work->num_interrupts = ctx->afu->pp_irqs;
310         else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
311                  (work->num_interrupts > ctx->afu->irqs_max)) {
312                 return -EINVAL;
313         }
314
315         rc = afu_register_irqs(ctx, work->num_interrupts);
316         if (rc)
317                 return rc;
318
319         rc = cxl_start_context(ctx, work->work_element_descriptor, current);
320         if (rc < 0) {
321                 afu_release_irqs(ctx, ctx);
322                 return rc;
323         }
324
325         return 0;
326 }
327 EXPORT_SYMBOL_GPL(cxl_start_work);
328
329 void __iomem *cxl_psa_map(struct cxl_context *ctx)
330 {
331         struct cxl_afu *afu = ctx->afu;
332         int rc;
333
334         rc = cxl_afu_check_and_enable(afu);
335         if (rc)
336                 return NULL;
337
338         pr_devel("%s: psn_phys%llx size:%llx\n",
339                  __func__, afu->psn_phys, afu->adapter->ps_size);
340         return ioremap(ctx->psn_phys, ctx->psn_size);
341 }
342 EXPORT_SYMBOL_GPL(cxl_psa_map);
343
344 void cxl_psa_unmap(void __iomem *addr)
345 {
346         iounmap(addr);
347 }
348 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
349
350 int cxl_afu_reset(struct cxl_context *ctx)
351 {
352         struct cxl_afu *afu = ctx->afu;
353         int rc;
354
355         rc = __cxl_afu_reset(afu);
356         if (rc)
357                 return rc;
358
359         return cxl_afu_check_and_enable(afu);
360 }
361 EXPORT_SYMBOL_GPL(cxl_afu_reset);
362
363 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
364                                   bool perst_reloads_same_image)
365 {
366         afu->adapter->perst_same_image = perst_reloads_same_image;
367 }
368 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);