qi.c 20 KB

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