staging: fsl-mc/dpio: Propagate error code
[linux-2.6/btrfs-unstable.git] / drivers / staging / fsl-mc / bus / dpio / dpio-service.c
blobf8096828f5b7110076d996282d07958f98961724
1 /*
2 * Copyright 2014-2016 Freescale Semiconductor Inc.
3 * Copyright 2016 NXP
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Freescale Semiconductor nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
19 * later version.
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <linux/types.h>
33 #include "../../include/mc.h"
34 #include "../../include/dpaa2-io.h"
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/platform_device.h>
38 #include <linux/interrupt.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/slab.h>
42 #include "dpio.h"
43 #include "qbman-portal.h"
45 struct dpaa2_io {
46 atomic_t refs;
47 struct dpaa2_io_desc dpio_desc;
48 struct qbman_swp_desc swp_desc;
49 struct qbman_swp *swp;
50 struct list_head node;
51 /* protect against multiple management commands */
52 spinlock_t lock_mgmt_cmd;
53 /* protect notifications list */
54 spinlock_t lock_notifications;
55 struct list_head notifications;
58 struct dpaa2_io_store {
59 unsigned int max;
60 dma_addr_t paddr;
61 struct dpaa2_dq *vaddr;
62 void *alloced_addr; /* unaligned value from kmalloc() */
63 unsigned int idx; /* position of the next-to-be-returned entry */
64 struct qbman_swp *swp; /* portal used to issue VDQCR */
65 struct device *dev; /* device used for DMA mapping */
68 /* keep a per cpu array of DPIOs for fast access */
69 static struct dpaa2_io *dpio_by_cpu[NR_CPUS];
70 static struct list_head dpio_list = LIST_HEAD_INIT(dpio_list);
71 static DEFINE_SPINLOCK(dpio_list_lock);
73 static inline struct dpaa2_io *service_select_by_cpu(struct dpaa2_io *d,
74 int cpu)
76 if (d)
77 return d;
79 if (unlikely(cpu >= num_possible_cpus()))
80 return NULL;
83 * If cpu == -1, choose the current cpu, with no guarantees about
84 * potentially being migrated away.
86 if (unlikely(cpu < 0))
87 cpu = smp_processor_id();
89 /* If a specific cpu was requested, pick it up immediately */
90 return dpio_by_cpu[cpu];
93 static inline struct dpaa2_io *service_select(struct dpaa2_io *d)
95 if (d)
96 return d;
98 spin_lock(&dpio_list_lock);
99 d = list_entry(dpio_list.next, struct dpaa2_io, node);
100 list_del(&d->node);
101 list_add_tail(&d->node, &dpio_list);
102 spin_unlock(&dpio_list_lock);
104 return d;
108 * dpaa2_io_create() - create a dpaa2_io object.
109 * @desc: the dpaa2_io descriptor
111 * Activates a "struct dpaa2_io" corresponding to the given config of an actual
112 * DPIO object.
114 * Return a valid dpaa2_io object for success, or NULL for failure.
116 struct dpaa2_io *dpaa2_io_create(const struct dpaa2_io_desc *desc)
118 struct dpaa2_io *obj = kmalloc(sizeof(*obj), GFP_KERNEL);
120 if (!obj)
121 return NULL;
123 /* check if CPU is out of range (-1 means any cpu) */
124 if (desc->cpu >= num_possible_cpus()) {
125 kfree(obj);
126 return NULL;
129 atomic_set(&obj->refs, 1);
130 obj->dpio_desc = *desc;
131 obj->swp_desc.cena_bar = obj->dpio_desc.regs_cena;
132 obj->swp_desc.cinh_bar = obj->dpio_desc.regs_cinh;
133 obj->swp_desc.qman_version = obj->dpio_desc.qman_version;
134 obj->swp = qbman_swp_init(&obj->swp_desc);
136 if (!obj->swp) {
137 kfree(obj);
138 return NULL;
141 INIT_LIST_HEAD(&obj->node);
142 spin_lock_init(&obj->lock_mgmt_cmd);
143 spin_lock_init(&obj->lock_notifications);
144 INIT_LIST_HEAD(&obj->notifications);
146 /* For now only enable DQRR interrupts */
147 qbman_swp_interrupt_set_trigger(obj->swp,
148 QBMAN_SWP_INTERRUPT_DQRI);
149 qbman_swp_interrupt_clear_status(obj->swp, 0xffffffff);
150 if (obj->dpio_desc.receives_notifications)
151 qbman_swp_push_set(obj->swp, 0, 1);
153 spin_lock(&dpio_list_lock);
154 list_add_tail(&obj->node, &dpio_list);
155 if (desc->cpu >= 0 && !dpio_by_cpu[desc->cpu])
156 dpio_by_cpu[desc->cpu] = obj;
157 spin_unlock(&dpio_list_lock);
159 return obj;
161 EXPORT_SYMBOL(dpaa2_io_create);
164 * dpaa2_io_down() - release the dpaa2_io object.
165 * @d: the dpaa2_io object to be released.
167 * The "struct dpaa2_io" type can represent an individual DPIO object (as
168 * described by "struct dpaa2_io_desc") or an instance of a "DPIO service",
169 * which can be used to group/encapsulate multiple DPIO objects. In all cases,
170 * each handle obtained should be released using this function.
172 void dpaa2_io_down(struct dpaa2_io *d)
174 if (!atomic_dec_and_test(&d->refs))
175 return;
176 kfree(d);
178 EXPORT_SYMBOL(dpaa2_io_down);
180 #define DPAA_POLL_MAX 32
183 * dpaa2_io_irq() - ISR for DPIO interrupts
185 * @obj: the given DPIO object.
187 * Return IRQ_HANDLED for success or IRQ_NONE if there
188 * were no pending interrupts.
190 irqreturn_t dpaa2_io_irq(struct dpaa2_io *obj)
192 const struct dpaa2_dq *dq;
193 int max = 0;
194 struct qbman_swp *swp;
195 u32 status;
197 swp = obj->swp;
198 status = qbman_swp_interrupt_read_status(swp);
199 if (!status)
200 return IRQ_NONE;
202 dq = qbman_swp_dqrr_next(swp);
203 while (dq) {
204 if (qbman_result_is_SCN(dq)) {
205 struct dpaa2_io_notification_ctx *ctx;
206 u64 q64;
208 q64 = qbman_result_SCN_ctx(dq);
209 ctx = (void *)q64;
210 ctx->cb(ctx);
211 } else {
212 pr_crit("fsl-mc-dpio: Unrecognised/ignored DQRR entry\n");
214 qbman_swp_dqrr_consume(swp, dq);
215 ++max;
216 if (max > DPAA_POLL_MAX)
217 goto done;
218 dq = qbman_swp_dqrr_next(swp);
220 done:
221 qbman_swp_interrupt_clear_status(swp, status);
222 qbman_swp_interrupt_set_inhibit(swp, 0);
223 return IRQ_HANDLED;
225 EXPORT_SYMBOL(dpaa2_io_irq);
228 * dpaa2_io_service_register() - Prepare for servicing of FQDAN or CDAN
229 * notifications on the given DPIO service.
230 * @d: the given DPIO service.
231 * @ctx: the notification context.
233 * The caller should make the MC command to attach a DPAA2 object to
234 * a DPIO after this function completes successfully. In that way:
235 * (a) The DPIO service is "ready" to handle a notification arrival
236 * (which might happen before the "attach" command to MC has
237 * returned control of execution back to the caller)
238 * (b) The DPIO service can provide back to the caller the 'dpio_id' and
239 * 'qman64' parameters that it should pass along in the MC command
240 * in order for the object to be configured to produce the right
241 * notification fields to the DPIO service.
243 * Return 0 for success, or -ENODEV for failure.
245 int dpaa2_io_service_register(struct dpaa2_io *d,
246 struct dpaa2_io_notification_ctx *ctx)
248 unsigned long irqflags;
250 d = service_select_by_cpu(d, ctx->desired_cpu);
251 if (!d)
252 return -ENODEV;
254 ctx->dpio_id = d->dpio_desc.dpio_id;
255 ctx->qman64 = (u64)ctx;
256 ctx->dpio_private = d;
257 spin_lock_irqsave(&d->lock_notifications, irqflags);
258 list_add(&ctx->node, &d->notifications);
259 spin_unlock_irqrestore(&d->lock_notifications, irqflags);
261 /* Enable the generation of CDAN notifications */
262 if (ctx->is_cdan)
263 return qbman_swp_CDAN_set_context_enable(d->swp,
264 (u16)ctx->id,
265 ctx->qman64);
266 return 0;
268 EXPORT_SYMBOL(dpaa2_io_service_register);
271 * dpaa2_io_service_deregister - The opposite of 'register'.
272 * @service: the given DPIO service.
273 * @ctx: the notification context.
275 * This function should be called only after sending the MC command to
276 * to detach the notification-producing device from the DPIO.
278 void dpaa2_io_service_deregister(struct dpaa2_io *service,
279 struct dpaa2_io_notification_ctx *ctx)
281 struct dpaa2_io *d = ctx->dpio_private;
282 unsigned long irqflags;
284 if (ctx->is_cdan)
285 qbman_swp_CDAN_disable(d->swp, (u16)ctx->id);
287 spin_lock_irqsave(&d->lock_notifications, irqflags);
288 list_del(&ctx->node);
289 spin_unlock_irqrestore(&d->lock_notifications, irqflags);
291 EXPORT_SYMBOL(dpaa2_io_service_deregister);
294 * dpaa2_io_service_rearm() - Rearm the notification for the given DPIO service.
295 * @d: the given DPIO service.
296 * @ctx: the notification context.
298 * Once a FQDAN/CDAN has been produced, the corresponding FQ/channel is
299 * considered "disarmed". Ie. the user can issue pull dequeue operations on that
300 * traffic source for as long as it likes. Eventually it may wish to "rearm"
301 * that source to allow it to produce another FQDAN/CDAN, that's what this
302 * function achieves.
304 * Return 0 for success.
306 int dpaa2_io_service_rearm(struct dpaa2_io *d,
307 struct dpaa2_io_notification_ctx *ctx)
309 unsigned long irqflags;
310 int err;
312 d = service_select_by_cpu(d, ctx->desired_cpu);
313 if (!unlikely(d))
314 return -ENODEV;
316 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
317 if (ctx->is_cdan)
318 err = qbman_swp_CDAN_enable(d->swp, (u16)ctx->id);
319 else
320 err = qbman_swp_fq_schedule(d->swp, ctx->id);
321 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
323 return err;
325 EXPORT_SYMBOL(dpaa2_io_service_rearm);
328 * dpaa2_io_service_pull_fq() - pull dequeue functions from a fq.
329 * @d: the given DPIO service.
330 * @fqid: the given frame queue id.
331 * @s: the dpaa2_io_store object for the result.
333 * Return 0 for success, or error code for failure.
335 int dpaa2_io_service_pull_fq(struct dpaa2_io *d, u32 fqid,
336 struct dpaa2_io_store *s)
338 struct qbman_pull_desc pd;
339 int err;
341 qbman_pull_desc_clear(&pd);
342 qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
343 qbman_pull_desc_set_numframes(&pd, (u8)s->max);
344 qbman_pull_desc_set_fq(&pd, fqid);
346 d = service_select(d);
347 if (!d)
348 return -ENODEV;
349 s->swp = d->swp;
350 err = qbman_swp_pull(d->swp, &pd);
351 if (err)
352 s->swp = NULL;
354 return err;
356 EXPORT_SYMBOL(dpaa2_io_service_pull_fq);
359 * dpaa2_io_service_pull_channel() - pull dequeue functions from a channel.
360 * @d: the given DPIO service.
361 * @channelid: the given channel id.
362 * @s: the dpaa2_io_store object for the result.
364 * Return 0 for success, or error code for failure.
366 int dpaa2_io_service_pull_channel(struct dpaa2_io *d, u32 channelid,
367 struct dpaa2_io_store *s)
369 struct qbman_pull_desc pd;
370 int err;
372 qbman_pull_desc_clear(&pd);
373 qbman_pull_desc_set_storage(&pd, s->vaddr, s->paddr, 1);
374 qbman_pull_desc_set_numframes(&pd, (u8)s->max);
375 qbman_pull_desc_set_channel(&pd, channelid, qbman_pull_type_prio);
377 d = service_select(d);
378 if (!d)
379 return -ENODEV;
381 s->swp = d->swp;
382 err = qbman_swp_pull(d->swp, &pd);
383 if (err)
384 s->swp = NULL;
386 return err;
388 EXPORT_SYMBOL(dpaa2_io_service_pull_channel);
391 * dpaa2_io_service_enqueue_fq() - Enqueue a frame to a frame queue.
392 * @d: the given DPIO service.
393 * @fqid: the given frame queue id.
394 * @fd: the frame descriptor which is enqueued.
396 * Return 0 for successful enqueue, -EBUSY if the enqueue ring is not ready,
397 * or -ENODEV if there is no dpio service.
399 int dpaa2_io_service_enqueue_fq(struct dpaa2_io *d,
400 u32 fqid,
401 const struct dpaa2_fd *fd)
403 struct qbman_eq_desc ed;
405 d = service_select(d);
406 if (!d)
407 return -ENODEV;
409 qbman_eq_desc_clear(&ed);
410 qbman_eq_desc_set_no_orp(&ed, 0);
411 qbman_eq_desc_set_fq(&ed, fqid);
413 return qbman_swp_enqueue(d->swp, &ed, fd);
415 EXPORT_SYMBOL(dpaa2_io_service_enqueue_fq);
418 * dpaa2_io_service_enqueue_qd() - Enqueue a frame to a QD.
419 * @d: the given DPIO service.
420 * @qdid: the given queuing destination id.
421 * @prio: the given queuing priority.
422 * @qdbin: the given queuing destination bin.
423 * @fd: the frame descriptor which is enqueued.
425 * Return 0 for successful enqueue, or -EBUSY if the enqueue ring is not ready,
426 * or -ENODEV if there is no dpio service.
428 int dpaa2_io_service_enqueue_qd(struct dpaa2_io *d,
429 u32 qdid, u8 prio, u16 qdbin,
430 const struct dpaa2_fd *fd)
432 struct qbman_eq_desc ed;
434 d = service_select(d);
435 if (!d)
436 return -ENODEV;
438 qbman_eq_desc_clear(&ed);
439 qbman_eq_desc_set_no_orp(&ed, 0);
440 qbman_eq_desc_set_qd(&ed, qdid, qdbin, prio);
442 return qbman_swp_enqueue(d->swp, &ed, fd);
444 EXPORT_SYMBOL(dpaa2_io_service_enqueue_qd);
447 * dpaa2_io_service_release() - Release buffers to a buffer pool.
448 * @d: the given DPIO object.
449 * @bpid: the buffer pool id.
450 * @buffers: the buffers to be released.
451 * @num_buffers: the number of the buffers to be released.
453 * Return 0 for success, and negative error code for failure.
455 int dpaa2_io_service_release(struct dpaa2_io *d,
456 u32 bpid,
457 const u64 *buffers,
458 unsigned int num_buffers)
460 struct qbman_release_desc rd;
462 d = service_select(d);
463 if (!d)
464 return -ENODEV;
466 qbman_release_desc_clear(&rd);
467 qbman_release_desc_set_bpid(&rd, bpid);
469 return qbman_swp_release(d->swp, &rd, buffers, num_buffers);
471 EXPORT_SYMBOL(dpaa2_io_service_release);
474 * dpaa2_io_service_acquire() - Acquire buffers from a buffer pool.
475 * @d: the given DPIO object.
476 * @bpid: the buffer pool id.
477 * @buffers: the buffer addresses for acquired buffers.
478 * @num_buffers: the expected number of the buffers to acquire.
480 * Return a negative error code if the command failed, otherwise it returns
481 * the number of buffers acquired, which may be less than the number requested.
482 * Eg. if the buffer pool is empty, this will return zero.
484 int dpaa2_io_service_acquire(struct dpaa2_io *d,
485 u32 bpid,
486 u64 *buffers,
487 unsigned int num_buffers)
489 unsigned long irqflags;
490 int err;
492 d = service_select(d);
493 if (!d)
494 return -ENODEV;
496 spin_lock_irqsave(&d->lock_mgmt_cmd, irqflags);
497 err = qbman_swp_acquire(d->swp, bpid, buffers, num_buffers);
498 spin_unlock_irqrestore(&d->lock_mgmt_cmd, irqflags);
500 return err;
502 EXPORT_SYMBOL(dpaa2_io_service_acquire);
505 * 'Stores' are reusable memory blocks for holding dequeue results, and to
506 * assist with parsing those results.
510 * dpaa2_io_store_create() - Create the dma memory storage for dequeue result.
511 * @max_frames: the maximum number of dequeued result for frames, must be <= 16.
512 * @dev: the device to allow mapping/unmapping the DMAable region.
514 * The size of the storage is "max_frames*sizeof(struct dpaa2_dq)".
515 * The 'dpaa2_io_store' returned is a DPIO service managed object.
517 * Return pointer to dpaa2_io_store struct for successfully created storage
518 * memory, or NULL on error.
520 struct dpaa2_io_store *dpaa2_io_store_create(unsigned int max_frames,
521 struct device *dev)
523 struct dpaa2_io_store *ret;
524 size_t size;
526 if (!max_frames || (max_frames > 16))
527 return NULL;
529 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
530 if (!ret)
531 return NULL;
533 ret->max = max_frames;
534 size = max_frames * sizeof(struct dpaa2_dq) + 64;
535 ret->alloced_addr = kzalloc(size, GFP_KERNEL);
536 if (!ret->alloced_addr) {
537 kfree(ret);
538 return NULL;
541 ret->vaddr = PTR_ALIGN(ret->alloced_addr, 64);
542 ret->paddr = dma_map_single(dev, ret->vaddr,
543 sizeof(struct dpaa2_dq) * max_frames,
544 DMA_FROM_DEVICE);
545 if (dma_mapping_error(dev, ret->paddr)) {
546 kfree(ret->alloced_addr);
547 kfree(ret);
548 return NULL;
551 ret->idx = 0;
552 ret->dev = dev;
554 return ret;
556 EXPORT_SYMBOL(dpaa2_io_store_create);
559 * dpaa2_io_store_destroy() - Frees the dma memory storage for dequeue
560 * result.
561 * @s: the storage memory to be destroyed.
563 void dpaa2_io_store_destroy(struct dpaa2_io_store *s)
565 dma_unmap_single(s->dev, s->paddr, sizeof(struct dpaa2_dq) * s->max,
566 DMA_FROM_DEVICE);
567 kfree(s->alloced_addr);
568 kfree(s);
570 EXPORT_SYMBOL(dpaa2_io_store_destroy);
573 * dpaa2_io_store_next() - Determine when the next dequeue result is available.
574 * @s: the dpaa2_io_store object.
575 * @is_last: indicate whether this is the last frame in the pull command.
577 * When an object driver performs dequeues to a dpaa2_io_store, this function
578 * can be used to determine when the next frame result is available. Once
579 * this function returns non-NULL, a subsequent call to it will try to find
580 * the next dequeue result.
582 * Note that if a pull-dequeue has a NULL result because the target FQ/channel
583 * was empty, then this function will also return NULL (rather than expecting
584 * the caller to always check for this. As such, "is_last" can be used to
585 * differentiate between "end-of-empty-dequeue" and "still-waiting".
587 * Return dequeue result for a valid dequeue result, or NULL for empty dequeue.
589 struct dpaa2_dq *dpaa2_io_store_next(struct dpaa2_io_store *s, int *is_last)
591 int match;
592 struct dpaa2_dq *ret = &s->vaddr[s->idx];
594 match = qbman_result_has_new_result(s->swp, ret);
595 if (!match) {
596 *is_last = 0;
597 return NULL;
600 s->idx++;
602 if (dpaa2_dq_is_pull_complete(ret)) {
603 *is_last = 1;
604 s->idx = 0;
606 * If we get an empty dequeue result to terminate a zero-results
607 * vdqcr, return NULL to the caller rather than expecting him to
608 * check non-NULL results every time.
610 if (!(dpaa2_dq_flags(ret) & DPAA2_DQ_STAT_VALIDFRAME))
611 ret = NULL;
612 } else {
613 *is_last = 0;
616 return ret;
618 EXPORT_SYMBOL(dpaa2_io_store_next);