xref: /openbmc/linux/drivers/crypto/caam/qi.c (revision 1c2dd16a)
1 /*
2  * CAAM/SEC 4.x QI transport/backend driver
3  * Queue Interface backend functionality
4  *
5  * Copyright 2013-2016 Freescale Semiconductor, Inc.
6  * Copyright 2016-2017 NXP
7  */
8 
9 #include <linux/cpumask.h>
10 #include <linux/kthread.h>
11 #include <soc/fsl/qman.h>
12 
13 #include "regs.h"
14 #include "qi.h"
15 #include "desc.h"
16 #include "intern.h"
17 #include "desc_constr.h"
18 
19 #define PREHDR_RSLS_SHIFT	31
20 
21 /*
22  * Use a reasonable backlog of frames (per CPU) as congestion threshold,
23  * so that resources used by the in-flight buffers do not become a memory hog.
24  */
25 #define MAX_RSP_FQ_BACKLOG_PER_CPU	256
26 
27 /* Length of a single buffer in the QI driver memory cache */
28 #define CAAM_QI_MEMCACHE_SIZE	512
29 
30 #define CAAM_QI_ENQUEUE_RETRIES	10000
31 
32 #define CAAM_NAPI_WEIGHT	63
33 
34 /*
35  * caam_napi - struct holding CAAM NAPI-related params
36  * @irqtask: IRQ task for QI backend
37  * @p: QMan portal
38  */
39 struct caam_napi {
40 	struct napi_struct irqtask;
41 	struct qman_portal *p;
42 };
43 
44 /*
45  * caam_qi_pcpu_priv - percpu private data structure to main list of pending
46  *                     responses expected on each cpu.
47  * @caam_napi: CAAM NAPI params
48  * @net_dev: netdev used by NAPI
49  * @rsp_fq: response FQ from CAAM
50  */
51 struct caam_qi_pcpu_priv {
52 	struct caam_napi caam_napi;
53 	struct net_device net_dev;
54 	struct qman_fq *rsp_fq;
55 } ____cacheline_aligned;
56 
57 static DEFINE_PER_CPU(struct caam_qi_pcpu_priv, pcpu_qipriv);
58 
59 /*
60  * caam_qi_priv - CAAM QI backend private params
61  * @cgr: QMan congestion group
62  * @qi_pdev: platform device for QI backend
63  */
64 struct caam_qi_priv {
65 	struct qman_cgr cgr;
66 	struct platform_device *qi_pdev;
67 };
68 
69 static struct caam_qi_priv qipriv ____cacheline_aligned;
70 
71 /*
72  * This is written by only one core - the one that initialized the CGR - and
73  * read by multiple cores (all the others).
74  */
75 bool caam_congested __read_mostly;
76 EXPORT_SYMBOL(caam_congested);
77 
78 #ifdef CONFIG_DEBUG_FS
79 /*
80  * This is a counter for the number of times the congestion group (where all
81  * the request and response queueus are) reached congestion. Incremented
82  * each time the congestion callback is called with congested == true.
83  */
84 static u64 times_congested;
85 #endif
86 
87 /*
88  * CPU from where the module initialised. This is required because QMan driver
89  * requires CGRs to be removed from same CPU from where they were originally
90  * allocated.
91  */
92 static int mod_init_cpu;
93 
94 /*
95  * This is a a cache of buffers, from which the users of CAAM QI driver
96  * can allocate short (CAAM_QI_MEMCACHE_SIZE) buffers. It's faster than
97  * doing malloc on the hotpath.
98  * NOTE: A more elegant solution would be to have some headroom in the frames
99  *       being processed. This could be added by the dpaa-ethernet driver.
100  *       This would pose a problem for userspace application processing which
101  *       cannot know of this limitation. So for now, this will work.
102  * NOTE: The memcache is SMP-safe. No need to handle spinlocks in-here
103  */
104 static struct kmem_cache *qi_cache;
105 
106 int caam_qi_enqueue(struct device *qidev, struct caam_drv_req *req)
107 {
108 	struct qm_fd fd;
109 	dma_addr_t addr;
110 	int ret;
111 	int num_retries = 0;
112 
113 	qm_fd_clear_fd(&fd);
114 	qm_fd_set_compound(&fd, qm_sg_entry_get_len(&req->fd_sgt[1]));
115 
116 	addr = dma_map_single(qidev, req->fd_sgt, sizeof(req->fd_sgt),
117 			      DMA_BIDIRECTIONAL);
118 	if (dma_mapping_error(qidev, addr)) {
119 		dev_err(qidev, "DMA mapping error for QI enqueue request\n");
120 		return -EIO;
121 	}
122 	qm_fd_addr_set64(&fd, addr);
123 
124 	do {
125 		ret = qman_enqueue(req->drv_ctx->req_fq, &fd);
126 		if (likely(!ret))
127 			return 0;
128 
129 		if (ret != -EBUSY)
130 			break;
131 		num_retries++;
132 	} while (num_retries < CAAM_QI_ENQUEUE_RETRIES);
133 
134 	dev_err(qidev, "qman_enqueue failed: %d\n", ret);
135 
136 	return ret;
137 }
138 EXPORT_SYMBOL(caam_qi_enqueue);
139 
140 static void caam_fq_ern_cb(struct qman_portal *qm, struct qman_fq *fq,
141 			   const union qm_mr_entry *msg)
142 {
143 	const struct qm_fd *fd;
144 	struct caam_drv_req *drv_req;
145 	struct device *qidev = &(raw_cpu_ptr(&pcpu_qipriv)->net_dev.dev);
146 
147 	fd = &msg->ern.fd;
148 
149 	if (qm_fd_get_format(fd) != qm_fd_compound) {
150 		dev_err(qidev, "Non-compound FD from CAAM\n");
151 		return;
152 	}
153 
154 	drv_req = (struct caam_drv_req *)phys_to_virt(qm_fd_addr_get64(fd));
155 	if (!drv_req) {
156 		dev_err(qidev,
157 			"Can't find original request for CAAM response\n");
158 		return;
159 	}
160 
161 	dma_unmap_single(drv_req->drv_ctx->qidev, qm_fd_addr(fd),
162 			 sizeof(drv_req->fd_sgt), DMA_BIDIRECTIONAL);
163 
164 	drv_req->cbk(drv_req, -EIO);
165 }
166 
167 static struct qman_fq *create_caam_req_fq(struct device *qidev,
168 					  struct qman_fq *rsp_fq,
169 					  dma_addr_t hwdesc,
170 					  int fq_sched_flag)
171 {
172 	int ret;
173 	struct qman_fq *req_fq;
174 	struct qm_mcc_initfq opts;
175 
176 	req_fq = kzalloc(sizeof(*req_fq), GFP_ATOMIC);
177 	if (!req_fq)
178 		return ERR_PTR(-ENOMEM);
179 
180 	req_fq->cb.ern = caam_fq_ern_cb;
181 	req_fq->cb.fqs = NULL;
182 
183 	ret = qman_create_fq(0, QMAN_FQ_FLAG_DYNAMIC_FQID |
184 				QMAN_FQ_FLAG_TO_DCPORTAL, req_fq);
185 	if (ret) {
186 		dev_err(qidev, "Failed to create session req FQ\n");
187 		goto create_req_fq_fail;
188 	}
189 
190 	memset(&opts, 0, sizeof(opts));
191 	opts.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL | QM_INITFQ_WE_DESTWQ |
192 				   QM_INITFQ_WE_CONTEXTB |
193 				   QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CGID);
194 	opts.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_CPCSTASH | QM_FQCTRL_CGE);
195 	qm_fqd_set_destwq(&opts.fqd, qm_channel_caam, 2);
196 	opts.fqd.context_b = cpu_to_be32(qman_fq_fqid(rsp_fq));
197 	qm_fqd_context_a_set64(&opts.fqd, hwdesc);
198 	opts.fqd.cgid = qipriv.cgr.cgrid;
199 
200 	ret = qman_init_fq(req_fq, fq_sched_flag, &opts);
201 	if (ret) {
202 		dev_err(qidev, "Failed to init session req FQ\n");
203 		goto init_req_fq_fail;
204 	}
205 
206 	dev_info(qidev, "Allocated request FQ %u for CPU %u\n", req_fq->fqid,
207 		 smp_processor_id());
208 	return req_fq;
209 
210 init_req_fq_fail:
211 	qman_destroy_fq(req_fq);
212 create_req_fq_fail:
213 	kfree(req_fq);
214 	return ERR_PTR(ret);
215 }
216 
217 static int empty_retired_fq(struct device *qidev, struct qman_fq *fq)
218 {
219 	int ret;
220 
221 	ret = qman_volatile_dequeue(fq, QMAN_VOLATILE_FLAG_WAIT_INT |
222 				    QMAN_VOLATILE_FLAG_FINISH,
223 				    QM_VDQCR_PRECEDENCE_VDQCR |
224 				    QM_VDQCR_NUMFRAMES_TILLEMPTY);
225 	if (ret) {
226 		dev_err(qidev, "Volatile dequeue fail for FQ: %u\n", fq->fqid);
227 		return ret;
228 	}
229 
230 	do {
231 		struct qman_portal *p;
232 
233 		p = qman_get_affine_portal(smp_processor_id());
234 		qman_p_poll_dqrr(p, 16);
235 	} while (fq->flags & QMAN_FQ_STATE_NE);
236 
237 	return 0;
238 }
239 
240 static int kill_fq(struct device *qidev, struct qman_fq *fq)
241 {
242 	u32 flags;
243 	int ret;
244 
245 	ret = qman_retire_fq(fq, &flags);
246 	if (ret < 0) {
247 		dev_err(qidev, "qman_retire_fq failed: %d\n", ret);
248 		return ret;
249 	}
250 
251 	if (!ret)
252 		goto empty_fq;
253 
254 	/* Async FQ retirement condition */
255 	if (ret == 1) {
256 		/* Retry till FQ gets in retired state */
257 		do {
258 			msleep(20);
259 		} while (fq->state != qman_fq_state_retired);
260 
261 		WARN_ON(fq->flags & QMAN_FQ_STATE_BLOCKOOS);
262 		WARN_ON(fq->flags & QMAN_FQ_STATE_ORL);
263 	}
264 
265 empty_fq:
266 	if (fq->flags & QMAN_FQ_STATE_NE) {
267 		ret = empty_retired_fq(qidev, fq);
268 		if (ret) {
269 			dev_err(qidev, "empty_retired_fq fail for FQ: %u\n",
270 				fq->fqid);
271 			return ret;
272 		}
273 	}
274 
275 	ret = qman_oos_fq(fq);
276 	if (ret)
277 		dev_err(qidev, "OOS of FQID: %u failed\n", fq->fqid);
278 
279 	qman_destroy_fq(fq);
280 
281 	return ret;
282 }
283 
284 static int empty_caam_fq(struct qman_fq *fq)
285 {
286 	int ret;
287 	struct qm_mcr_queryfq_np np;
288 
289 	/* Wait till the older CAAM FQ get empty */
290 	do {
291 		ret = qman_query_fq_np(fq, &np);
292 		if (ret)
293 			return ret;
294 
295 		if (!qm_mcr_np_get(&np, frm_cnt))
296 			break;
297 
298 		msleep(20);
299 	} while (1);
300 
301 	/*
302 	 * Give extra time for pending jobs from this FQ in holding tanks
303 	 * to get processed
304 	 */
305 	msleep(20);
306 	return 0;
307 }
308 
309 int caam_drv_ctx_update(struct caam_drv_ctx *drv_ctx, u32 *sh_desc)
310 {
311 	int ret;
312 	u32 num_words;
313 	struct qman_fq *new_fq, *old_fq;
314 	struct device *qidev = drv_ctx->qidev;
315 
316 	num_words = desc_len(sh_desc);
317 	if (num_words > MAX_SDLEN) {
318 		dev_err(qidev, "Invalid descriptor len: %d words\n", num_words);
319 		return -EINVAL;
320 	}
321 
322 	/* Note down older req FQ */
323 	old_fq = drv_ctx->req_fq;
324 
325 	/* Create a new req FQ in parked state */
326 	new_fq = create_caam_req_fq(drv_ctx->qidev, drv_ctx->rsp_fq,
327 				    drv_ctx->context_a, 0);
328 	if (unlikely(IS_ERR_OR_NULL(new_fq))) {
329 		dev_err(qidev, "FQ allocation for shdesc update failed\n");
330 		return PTR_ERR(new_fq);
331 	}
332 
333 	/* Hook up new FQ to context so that new requests keep queuing */
334 	drv_ctx->req_fq = new_fq;
335 
336 	/* Empty and remove the older FQ */
337 	ret = empty_caam_fq(old_fq);
338 	if (ret) {
339 		dev_err(qidev, "Old CAAM FQ empty failed: %d\n", ret);
340 
341 		/* We can revert to older FQ */
342 		drv_ctx->req_fq = old_fq;
343 
344 		if (kill_fq(qidev, new_fq))
345 			dev_warn(qidev, "New CAAM FQ: %u kill failed\n",
346 				 new_fq->fqid);
347 
348 		return ret;
349 	}
350 
351 	/*
352 	 * Re-initialise pre-header. Set RSLS and SDLEN.
353 	 * Update the shared descriptor for driver context.
354 	 */
355 	drv_ctx->prehdr[0] = cpu_to_caam32((1 << PREHDR_RSLS_SHIFT) |
356 					   num_words);
357 	memcpy(drv_ctx->sh_desc, sh_desc, desc_bytes(sh_desc));
358 	dma_sync_single_for_device(qidev, drv_ctx->context_a,
359 				   sizeof(drv_ctx->sh_desc) +
360 				   sizeof(drv_ctx->prehdr),
361 				   DMA_BIDIRECTIONAL);
362 
363 	/* Put the new FQ in scheduled state */
364 	ret = qman_schedule_fq(new_fq);
365 	if (ret) {
366 		dev_err(qidev, "Fail to sched new CAAM FQ, ecode = %d\n", ret);
367 
368 		/*
369 		 * We can kill new FQ and revert to old FQ.
370 		 * Since the desc is already modified, it is success case
371 		 */
372 
373 		drv_ctx->req_fq = old_fq;
374 
375 		if (kill_fq(qidev, new_fq))
376 			dev_warn(qidev, "New CAAM FQ: %u kill failed\n",
377 				 new_fq->fqid);
378 	} else if (kill_fq(qidev, old_fq)) {
379 		dev_warn(qidev, "Old CAAM FQ: %u kill failed\n", old_fq->fqid);
380 	}
381 
382 	return 0;
383 }
384 EXPORT_SYMBOL(caam_drv_ctx_update);
385 
386 struct caam_drv_ctx *caam_drv_ctx_init(struct device *qidev,
387 				       int *cpu,
388 				       u32 *sh_desc)
389 {
390 	size_t size;
391 	u32 num_words;
392 	dma_addr_t hwdesc;
393 	struct caam_drv_ctx *drv_ctx;
394 	const cpumask_t *cpus = qman_affine_cpus();
395 	static DEFINE_PER_CPU(int, last_cpu);
396 
397 	num_words = desc_len(sh_desc);
398 	if (num_words > MAX_SDLEN) {
399 		dev_err(qidev, "Invalid descriptor len: %d words\n",
400 			num_words);
401 		return ERR_PTR(-EINVAL);
402 	}
403 
404 	drv_ctx = kzalloc(sizeof(*drv_ctx), GFP_ATOMIC);
405 	if (!drv_ctx)
406 		return ERR_PTR(-ENOMEM);
407 
408 	/*
409 	 * Initialise pre-header - set RSLS and SDLEN - and shared descriptor
410 	 * and dma-map them.
411 	 */
412 	drv_ctx->prehdr[0] = cpu_to_caam32((1 << PREHDR_RSLS_SHIFT) |
413 					   num_words);
414 	memcpy(drv_ctx->sh_desc, sh_desc, desc_bytes(sh_desc));
415 	size = sizeof(drv_ctx->prehdr) + sizeof(drv_ctx->sh_desc);
416 	hwdesc = dma_map_single(qidev, drv_ctx->prehdr, size,
417 				DMA_BIDIRECTIONAL);
418 	if (dma_mapping_error(qidev, hwdesc)) {
419 		dev_err(qidev, "DMA map error for preheader + shdesc\n");
420 		kfree(drv_ctx);
421 		return ERR_PTR(-ENOMEM);
422 	}
423 	drv_ctx->context_a = hwdesc;
424 
425 	/* If given CPU does not own the portal, choose another one that does */
426 	if (!cpumask_test_cpu(*cpu, cpus)) {
427 		int *pcpu = &get_cpu_var(last_cpu);
428 
429 		*pcpu = cpumask_next(*pcpu, cpus);
430 		if (*pcpu >= nr_cpu_ids)
431 			*pcpu = cpumask_first(cpus);
432 		*cpu = *pcpu;
433 
434 		put_cpu_var(last_cpu);
435 	}
436 	drv_ctx->cpu = *cpu;
437 
438 	/* Find response FQ hooked with this CPU */
439 	drv_ctx->rsp_fq = per_cpu(pcpu_qipriv.rsp_fq, drv_ctx->cpu);
440 
441 	/* Attach request FQ */
442 	drv_ctx->req_fq = create_caam_req_fq(qidev, drv_ctx->rsp_fq, hwdesc,
443 					     QMAN_INITFQ_FLAG_SCHED);
444 	if (unlikely(IS_ERR_OR_NULL(drv_ctx->req_fq))) {
445 		dev_err(qidev, "create_caam_req_fq failed\n");
446 		dma_unmap_single(qidev, hwdesc, size, DMA_BIDIRECTIONAL);
447 		kfree(drv_ctx);
448 		return ERR_PTR(-ENOMEM);
449 	}
450 
451 	drv_ctx->qidev = qidev;
452 	return drv_ctx;
453 }
454 EXPORT_SYMBOL(caam_drv_ctx_init);
455 
456 void *qi_cache_alloc(gfp_t flags)
457 {
458 	return kmem_cache_alloc(qi_cache, flags);
459 }
460 EXPORT_SYMBOL(qi_cache_alloc);
461 
462 void qi_cache_free(void *obj)
463 {
464 	kmem_cache_free(qi_cache, obj);
465 }
466 EXPORT_SYMBOL(qi_cache_free);
467 
468 static int caam_qi_poll(struct napi_struct *napi, int budget)
469 {
470 	struct caam_napi *np = container_of(napi, struct caam_napi, irqtask);
471 
472 	int cleaned = qman_p_poll_dqrr(np->p, budget);
473 
474 	if (cleaned < budget) {
475 		napi_complete(napi);
476 		qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
477 	}
478 
479 	return cleaned;
480 }
481 
482 void caam_drv_ctx_rel(struct caam_drv_ctx *drv_ctx)
483 {
484 	if (IS_ERR_OR_NULL(drv_ctx))
485 		return;
486 
487 	/* Remove request FQ */
488 	if (kill_fq(drv_ctx->qidev, drv_ctx->req_fq))
489 		dev_err(drv_ctx->qidev, "Crypto session req FQ kill failed\n");
490 
491 	dma_unmap_single(drv_ctx->qidev, drv_ctx->context_a,
492 			 sizeof(drv_ctx->sh_desc) + sizeof(drv_ctx->prehdr),
493 			 DMA_BIDIRECTIONAL);
494 	kfree(drv_ctx);
495 }
496 EXPORT_SYMBOL(caam_drv_ctx_rel);
497 
498 int caam_qi_shutdown(struct device *qidev)
499 {
500 	int i, ret;
501 	struct caam_qi_priv *priv = dev_get_drvdata(qidev);
502 	const cpumask_t *cpus = qman_affine_cpus();
503 	struct cpumask old_cpumask = current->cpus_allowed;
504 
505 	for_each_cpu(i, cpus) {
506 		struct napi_struct *irqtask;
507 
508 		irqtask = &per_cpu_ptr(&pcpu_qipriv.caam_napi, i)->irqtask;
509 		napi_disable(irqtask);
510 		netif_napi_del(irqtask);
511 
512 		if (kill_fq(qidev, per_cpu(pcpu_qipriv.rsp_fq, i)))
513 			dev_err(qidev, "Rsp FQ kill failed, cpu: %d\n", i);
514 		kfree(per_cpu(pcpu_qipriv.rsp_fq, i));
515 	}
516 
517 	/*
518 	 * QMan driver requires CGRs to be deleted from same CPU from where they
519 	 * were instantiated. Hence we get the module removal execute from the
520 	 * same CPU from where it was originally inserted.
521 	 */
522 	set_cpus_allowed_ptr(current, get_cpu_mask(mod_init_cpu));
523 
524 	ret = qman_delete_cgr(&priv->cgr);
525 	if (ret)
526 		dev_err(qidev, "Deletion of CGR failed: %d\n", ret);
527 	else
528 		qman_release_cgrid(priv->cgr.cgrid);
529 
530 	kmem_cache_destroy(qi_cache);
531 
532 	/* Now that we're done with the CGRs, restore the cpus allowed mask */
533 	set_cpus_allowed_ptr(current, &old_cpumask);
534 
535 	platform_device_unregister(priv->qi_pdev);
536 	return ret;
537 }
538 
539 static void cgr_cb(struct qman_portal *qm, struct qman_cgr *cgr, int congested)
540 {
541 	caam_congested = congested;
542 
543 	if (congested) {
544 #ifdef CONFIG_DEBUG_FS
545 		times_congested++;
546 #endif
547 		pr_debug_ratelimited("CAAM entered congestion\n");
548 
549 	} else {
550 		pr_debug_ratelimited("CAAM exited congestion\n");
551 	}
552 }
553 
554 static int caam_qi_napi_schedule(struct qman_portal *p, struct caam_napi *np)
555 {
556 	/*
557 	 * In case of threaded ISR, for RT kernels in_irq() does not return
558 	 * appropriate value, so use in_serving_softirq to distinguish between
559 	 * softirq and irq contexts.
560 	 */
561 	if (unlikely(in_irq() || !in_serving_softirq())) {
562 		/* Disable QMan IRQ source and invoke NAPI */
563 		qman_p_irqsource_remove(p, QM_PIRQ_DQRI);
564 		np->p = p;
565 		napi_schedule(&np->irqtask);
566 		return 1;
567 	}
568 	return 0;
569 }
570 
571 static enum qman_cb_dqrr_result caam_rsp_fq_dqrr_cb(struct qman_portal *p,
572 						    struct qman_fq *rsp_fq,
573 						    const struct qm_dqrr_entry *dqrr)
574 {
575 	struct caam_napi *caam_napi = raw_cpu_ptr(&pcpu_qipriv.caam_napi);
576 	struct caam_drv_req *drv_req;
577 	const struct qm_fd *fd;
578 	struct device *qidev = &(raw_cpu_ptr(&pcpu_qipriv)->net_dev.dev);
579 	u32 status;
580 
581 	if (caam_qi_napi_schedule(p, caam_napi))
582 		return qman_cb_dqrr_stop;
583 
584 	fd = &dqrr->fd;
585 	status = be32_to_cpu(fd->status);
586 	if (unlikely(status))
587 		dev_err(qidev, "Error: %#x in CAAM response FD\n", status);
588 
589 	if (unlikely(qm_fd_get_format(fd) != qm_fd_compound)) {
590 		dev_err(qidev, "Non-compound FD from CAAM\n");
591 		return qman_cb_dqrr_consume;
592 	}
593 
594 	drv_req = (struct caam_drv_req *)phys_to_virt(qm_fd_addr_get64(fd));
595 	if (unlikely(!drv_req)) {
596 		dev_err(qidev,
597 			"Can't find original request for caam response\n");
598 		return qman_cb_dqrr_consume;
599 	}
600 
601 	dma_unmap_single(drv_req->drv_ctx->qidev, qm_fd_addr(fd),
602 			 sizeof(drv_req->fd_sgt), DMA_BIDIRECTIONAL);
603 
604 	drv_req->cbk(drv_req, status);
605 	return qman_cb_dqrr_consume;
606 }
607 
608 static int alloc_rsp_fq_cpu(struct device *qidev, unsigned int cpu)
609 {
610 	struct qm_mcc_initfq opts;
611 	struct qman_fq *fq;
612 	int ret;
613 
614 	fq = kzalloc(sizeof(*fq), GFP_KERNEL | GFP_DMA);
615 	if (!fq)
616 		return -ENOMEM;
617 
618 	fq->cb.dqrr = caam_rsp_fq_dqrr_cb;
619 
620 	ret = qman_create_fq(0, QMAN_FQ_FLAG_NO_ENQUEUE |
621 			     QMAN_FQ_FLAG_DYNAMIC_FQID, fq);
622 	if (ret) {
623 		dev_err(qidev, "Rsp FQ create failed\n");
624 		kfree(fq);
625 		return -ENODEV;
626 	}
627 
628 	memset(&opts, 0, sizeof(opts));
629 	opts.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL | QM_INITFQ_WE_DESTWQ |
630 				   QM_INITFQ_WE_CONTEXTB |
631 				   QM_INITFQ_WE_CONTEXTA | QM_INITFQ_WE_CGID);
632 	opts.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_CTXASTASHING |
633 				       QM_FQCTRL_CPCSTASH | QM_FQCTRL_CGE);
634 	qm_fqd_set_destwq(&opts.fqd, qman_affine_channel(cpu), 3);
635 	opts.fqd.cgid = qipriv.cgr.cgrid;
636 	opts.fqd.context_a.stashing.exclusive =	QM_STASHING_EXCL_CTX |
637 						QM_STASHING_EXCL_DATA;
638 	qm_fqd_set_stashing(&opts.fqd, 0, 1, 1);
639 
640 	ret = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &opts);
641 	if (ret) {
642 		dev_err(qidev, "Rsp FQ init failed\n");
643 		kfree(fq);
644 		return -ENODEV;
645 	}
646 
647 	per_cpu(pcpu_qipriv.rsp_fq, cpu) = fq;
648 
649 	dev_info(qidev, "Allocated response FQ %u for CPU %u", fq->fqid, cpu);
650 	return 0;
651 }
652 
653 static int init_cgr(struct device *qidev)
654 {
655 	int ret;
656 	struct qm_mcc_initcgr opts;
657 	const u64 cpus = *(u64 *)qman_affine_cpus();
658 	const int num_cpus = hweight64(cpus);
659 	const u64 val = num_cpus * MAX_RSP_FQ_BACKLOG_PER_CPU;
660 
661 	ret = qman_alloc_cgrid(&qipriv.cgr.cgrid);
662 	if (ret) {
663 		dev_err(qidev, "CGR alloc failed for rsp FQs: %d\n", ret);
664 		return ret;
665 	}
666 
667 	qipriv.cgr.cb = cgr_cb;
668 	memset(&opts, 0, sizeof(opts));
669 	opts.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES |
670 				   QM_CGR_WE_MODE);
671 	opts.cgr.cscn_en = QM_CGR_EN;
672 	opts.cgr.mode = QMAN_CGR_MODE_FRAME;
673 	qm_cgr_cs_thres_set64(&opts.cgr.cs_thres, val, 1);
674 
675 	ret = qman_create_cgr(&qipriv.cgr, QMAN_CGR_FLAG_USE_INIT, &opts);
676 	if (ret) {
677 		dev_err(qidev, "Error %d creating CAAM CGRID: %u\n", ret,
678 			qipriv.cgr.cgrid);
679 		return ret;
680 	}
681 
682 	dev_info(qidev, "Congestion threshold set to %llu\n", val);
683 	return 0;
684 }
685 
686 static int alloc_rsp_fqs(struct device *qidev)
687 {
688 	int ret, i;
689 	const cpumask_t *cpus = qman_affine_cpus();
690 
691 	/*Now create response FQs*/
692 	for_each_cpu(i, cpus) {
693 		ret = alloc_rsp_fq_cpu(qidev, i);
694 		if (ret) {
695 			dev_err(qidev, "CAAM rsp FQ alloc failed, cpu: %u", i);
696 			return ret;
697 		}
698 	}
699 
700 	return 0;
701 }
702 
703 static void free_rsp_fqs(void)
704 {
705 	int i;
706 	const cpumask_t *cpus = qman_affine_cpus();
707 
708 	for_each_cpu(i, cpus)
709 		kfree(per_cpu(pcpu_qipriv.rsp_fq, i));
710 }
711 
712 int caam_qi_init(struct platform_device *caam_pdev)
713 {
714 	int err, i;
715 	struct platform_device *qi_pdev;
716 	struct device *ctrldev = &caam_pdev->dev, *qidev;
717 	struct caam_drv_private *ctrlpriv;
718 	const cpumask_t *cpus = qman_affine_cpus();
719 	struct cpumask old_cpumask = current->cpus_allowed;
720 	static struct platform_device_info qi_pdev_info = {
721 		.name = "caam_qi",
722 		.id = PLATFORM_DEVID_NONE
723 	};
724 
725 	/*
726 	 * QMAN requires CGRs to be removed from same CPU+portal from where it
727 	 * was originally allocated. Hence we need to note down the
728 	 * initialisation CPU and use the same CPU for module exit.
729 	 * We select the first CPU to from the list of portal owning CPUs.
730 	 * Then we pin module init to this CPU.
731 	 */
732 	mod_init_cpu = cpumask_first(cpus);
733 	set_cpus_allowed_ptr(current, get_cpu_mask(mod_init_cpu));
734 
735 	qi_pdev_info.parent = ctrldev;
736 	qi_pdev_info.dma_mask = dma_get_mask(ctrldev);
737 	qi_pdev = platform_device_register_full(&qi_pdev_info);
738 	if (IS_ERR(qi_pdev))
739 		return PTR_ERR(qi_pdev);
740 
741 	ctrlpriv = dev_get_drvdata(ctrldev);
742 	qidev = &qi_pdev->dev;
743 
744 	qipriv.qi_pdev = qi_pdev;
745 	dev_set_drvdata(qidev, &qipriv);
746 
747 	/* Initialize the congestion detection */
748 	err = init_cgr(qidev);
749 	if (err) {
750 		dev_err(qidev, "CGR initialization failed: %d\n", err);
751 		platform_device_unregister(qi_pdev);
752 		return err;
753 	}
754 
755 	/* Initialise response FQs */
756 	err = alloc_rsp_fqs(qidev);
757 	if (err) {
758 		dev_err(qidev, "Can't allocate CAAM response FQs: %d\n", err);
759 		free_rsp_fqs();
760 		platform_device_unregister(qi_pdev);
761 		return err;
762 	}
763 
764 	/*
765 	 * Enable the NAPI contexts on each of the core which has an affine
766 	 * portal.
767 	 */
768 	for_each_cpu(i, cpus) {
769 		struct caam_qi_pcpu_priv *priv = per_cpu_ptr(&pcpu_qipriv, i);
770 		struct caam_napi *caam_napi = &priv->caam_napi;
771 		struct napi_struct *irqtask = &caam_napi->irqtask;
772 		struct net_device *net_dev = &priv->net_dev;
773 
774 		net_dev->dev = *qidev;
775 		INIT_LIST_HEAD(&net_dev->napi_list);
776 
777 		netif_napi_add(net_dev, irqtask, caam_qi_poll,
778 			       CAAM_NAPI_WEIGHT);
779 
780 		napi_enable(irqtask);
781 	}
782 
783 	/* Hook up QI device to parent controlling caam device */
784 	ctrlpriv->qidev = qidev;
785 
786 	qi_cache = kmem_cache_create("caamqicache", CAAM_QI_MEMCACHE_SIZE, 0,
787 				     SLAB_CACHE_DMA, NULL);
788 	if (!qi_cache) {
789 		dev_err(qidev, "Can't allocate CAAM cache\n");
790 		free_rsp_fqs();
791 		platform_device_unregister(qi_pdev);
792 		return -ENOMEM;
793 	}
794 
795 	/* Done with the CGRs; restore the cpus allowed mask */
796 	set_cpus_allowed_ptr(current, &old_cpumask);
797 #ifdef CONFIG_DEBUG_FS
798 	ctrlpriv->qi_congested = debugfs_create_file("qi_congested", 0444,
799 						     ctrlpriv->ctl,
800 						     &times_congested,
801 						     &caam_fops_u64_ro);
802 #endif
803 	dev_info(qidev, "Linux CAAM Queue I/F driver initialised\n");
804 	return 0;
805 }
806