ccp-dmaengine.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Gary R Hook <gary.hook@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mutex.h>
  16. #include <linux/ccp.h>
  17. #include "ccp-dev.h"
  18. #include "../../dma/dmaengine.h"
  19. #define CCP_DMA_WIDTH(_mask) \
  20. ({ \
  21. u64 mask = _mask + 1; \
  22. (mask == 0) ? 64 : fls64(mask); \
  23. })
  24. static void ccp_free_cmd_resources(struct ccp_device *ccp,
  25. struct list_head *list)
  26. {
  27. struct ccp_dma_cmd *cmd, *ctmp;
  28. list_for_each_entry_safe(cmd, ctmp, list, entry) {
  29. list_del(&cmd->entry);
  30. kmem_cache_free(ccp->dma_cmd_cache, cmd);
  31. }
  32. }
  33. static void ccp_free_desc_resources(struct ccp_device *ccp,
  34. struct list_head *list)
  35. {
  36. struct ccp_dma_desc *desc, *dtmp;
  37. list_for_each_entry_safe(desc, dtmp, list, entry) {
  38. ccp_free_cmd_resources(ccp, &desc->active);
  39. ccp_free_cmd_resources(ccp, &desc->pending);
  40. list_del(&desc->entry);
  41. kmem_cache_free(ccp->dma_desc_cache, desc);
  42. }
  43. }
  44. static void ccp_free_chan_resources(struct dma_chan *dma_chan)
  45. {
  46. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  47. dma_chan);
  48. unsigned long flags;
  49. dev_dbg(chan->ccp->dev, "%s - chan=%p\n", __func__, chan);
  50. spin_lock_irqsave(&chan->lock, flags);
  51. ccp_free_desc_resources(chan->ccp, &chan->complete);
  52. ccp_free_desc_resources(chan->ccp, &chan->active);
  53. ccp_free_desc_resources(chan->ccp, &chan->pending);
  54. spin_unlock_irqrestore(&chan->lock, flags);
  55. }
  56. static void ccp_cleanup_desc_resources(struct ccp_device *ccp,
  57. struct list_head *list)
  58. {
  59. struct ccp_dma_desc *desc, *dtmp;
  60. list_for_each_entry_safe_reverse(desc, dtmp, list, entry) {
  61. if (!async_tx_test_ack(&desc->tx_desc))
  62. continue;
  63. dev_dbg(ccp->dev, "%s - desc=%p\n", __func__, desc);
  64. ccp_free_cmd_resources(ccp, &desc->active);
  65. ccp_free_cmd_resources(ccp, &desc->pending);
  66. list_del(&desc->entry);
  67. kmem_cache_free(ccp->dma_desc_cache, desc);
  68. }
  69. }
  70. static void ccp_do_cleanup(unsigned long data)
  71. {
  72. struct ccp_dma_chan *chan = (struct ccp_dma_chan *)data;
  73. unsigned long flags;
  74. dev_dbg(chan->ccp->dev, "%s - chan=%s\n", __func__,
  75. dma_chan_name(&chan->dma_chan));
  76. spin_lock_irqsave(&chan->lock, flags);
  77. ccp_cleanup_desc_resources(chan->ccp, &chan->complete);
  78. spin_unlock_irqrestore(&chan->lock, flags);
  79. }
  80. static int ccp_issue_next_cmd(struct ccp_dma_desc *desc)
  81. {
  82. struct ccp_dma_cmd *cmd;
  83. int ret;
  84. cmd = list_first_entry(&desc->pending, struct ccp_dma_cmd, entry);
  85. list_move(&cmd->entry, &desc->active);
  86. dev_dbg(desc->ccp->dev, "%s - tx %d, cmd=%p\n", __func__,
  87. desc->tx_desc.cookie, cmd);
  88. ret = ccp_enqueue_cmd(&cmd->ccp_cmd);
  89. if (!ret || (ret == -EINPROGRESS) || (ret == -EBUSY))
  90. return 0;
  91. dev_dbg(desc->ccp->dev, "%s - error: ret=%d, tx %d, cmd=%p\n", __func__,
  92. ret, desc->tx_desc.cookie, cmd);
  93. return ret;
  94. }
  95. static void ccp_free_active_cmd(struct ccp_dma_desc *desc)
  96. {
  97. struct ccp_dma_cmd *cmd;
  98. cmd = list_first_entry_or_null(&desc->active, struct ccp_dma_cmd,
  99. entry);
  100. if (!cmd)
  101. return;
  102. dev_dbg(desc->ccp->dev, "%s - freeing tx %d cmd=%p\n",
  103. __func__, desc->tx_desc.cookie, cmd);
  104. list_del(&cmd->entry);
  105. kmem_cache_free(desc->ccp->dma_cmd_cache, cmd);
  106. }
  107. static struct ccp_dma_desc *__ccp_next_dma_desc(struct ccp_dma_chan *chan,
  108. struct ccp_dma_desc *desc)
  109. {
  110. /* Move current DMA descriptor to the complete list */
  111. if (desc)
  112. list_move(&desc->entry, &chan->complete);
  113. /* Get the next DMA descriptor on the active list */
  114. desc = list_first_entry_or_null(&chan->active, struct ccp_dma_desc,
  115. entry);
  116. return desc;
  117. }
  118. static struct ccp_dma_desc *ccp_handle_active_desc(struct ccp_dma_chan *chan,
  119. struct ccp_dma_desc *desc)
  120. {
  121. struct dma_async_tx_descriptor *tx_desc;
  122. unsigned long flags;
  123. /* Loop over descriptors until one is found with commands */
  124. do {
  125. if (desc) {
  126. /* Remove the DMA command from the list and free it */
  127. ccp_free_active_cmd(desc);
  128. if (!list_empty(&desc->pending)) {
  129. /* No errors, keep going */
  130. if (desc->status != DMA_ERROR)
  131. return desc;
  132. /* Error, free remaining commands and move on */
  133. ccp_free_cmd_resources(desc->ccp,
  134. &desc->pending);
  135. }
  136. tx_desc = &desc->tx_desc;
  137. } else {
  138. tx_desc = NULL;
  139. }
  140. spin_lock_irqsave(&chan->lock, flags);
  141. if (desc) {
  142. if (desc->status != DMA_ERROR)
  143. desc->status = DMA_COMPLETE;
  144. dev_dbg(desc->ccp->dev,
  145. "%s - tx %d complete, status=%u\n", __func__,
  146. desc->tx_desc.cookie, desc->status);
  147. dma_cookie_complete(tx_desc);
  148. }
  149. desc = __ccp_next_dma_desc(chan, desc);
  150. spin_unlock_irqrestore(&chan->lock, flags);
  151. if (tx_desc) {
  152. if (tx_desc->callback &&
  153. (tx_desc->flags & DMA_PREP_INTERRUPT))
  154. tx_desc->callback(tx_desc->callback_param);
  155. dma_run_dependencies(tx_desc);
  156. }
  157. } while (desc);
  158. return NULL;
  159. }
  160. static struct ccp_dma_desc *__ccp_pending_to_active(struct ccp_dma_chan *chan)
  161. {
  162. struct ccp_dma_desc *desc;
  163. if (list_empty(&chan->pending))
  164. return NULL;
  165. desc = list_empty(&chan->active)
  166. ? list_first_entry(&chan->pending, struct ccp_dma_desc, entry)
  167. : NULL;
  168. list_splice_tail_init(&chan->pending, &chan->active);
  169. return desc;
  170. }
  171. static void ccp_cmd_callback(void *data, int err)
  172. {
  173. struct ccp_dma_desc *desc = data;
  174. struct ccp_dma_chan *chan;
  175. int ret;
  176. if (err == -EINPROGRESS)
  177. return;
  178. chan = container_of(desc->tx_desc.chan, struct ccp_dma_chan,
  179. dma_chan);
  180. dev_dbg(chan->ccp->dev, "%s - tx %d callback, err=%d\n",
  181. __func__, desc->tx_desc.cookie, err);
  182. if (err)
  183. desc->status = DMA_ERROR;
  184. while (true) {
  185. /* Check for DMA descriptor completion */
  186. desc = ccp_handle_active_desc(chan, desc);
  187. /* Don't submit cmd if no descriptor or DMA is paused */
  188. if (!desc || (chan->status == DMA_PAUSED))
  189. break;
  190. ret = ccp_issue_next_cmd(desc);
  191. if (!ret)
  192. break;
  193. desc->status = DMA_ERROR;
  194. }
  195. tasklet_schedule(&chan->cleanup_tasklet);
  196. }
  197. static dma_cookie_t ccp_tx_submit(struct dma_async_tx_descriptor *tx_desc)
  198. {
  199. struct ccp_dma_desc *desc = container_of(tx_desc, struct ccp_dma_desc,
  200. tx_desc);
  201. struct ccp_dma_chan *chan;
  202. dma_cookie_t cookie;
  203. unsigned long flags;
  204. chan = container_of(tx_desc->chan, struct ccp_dma_chan, dma_chan);
  205. spin_lock_irqsave(&chan->lock, flags);
  206. cookie = dma_cookie_assign(tx_desc);
  207. list_add_tail(&desc->entry, &chan->pending);
  208. spin_unlock_irqrestore(&chan->lock, flags);
  209. dev_dbg(chan->ccp->dev, "%s - added tx descriptor %d to pending list\n",
  210. __func__, cookie);
  211. return cookie;
  212. }
  213. static struct ccp_dma_cmd *ccp_alloc_dma_cmd(struct ccp_dma_chan *chan)
  214. {
  215. struct ccp_dma_cmd *cmd;
  216. cmd = kmem_cache_alloc(chan->ccp->dma_cmd_cache, GFP_NOWAIT);
  217. if (cmd)
  218. memset(cmd, 0, sizeof(*cmd));
  219. return cmd;
  220. }
  221. static struct ccp_dma_desc *ccp_alloc_dma_desc(struct ccp_dma_chan *chan,
  222. unsigned long flags)
  223. {
  224. struct ccp_dma_desc *desc;
  225. desc = kmem_cache_zalloc(chan->ccp->dma_desc_cache, GFP_NOWAIT);
  226. if (!desc)
  227. return NULL;
  228. dma_async_tx_descriptor_init(&desc->tx_desc, &chan->dma_chan);
  229. desc->tx_desc.flags = flags;
  230. desc->tx_desc.tx_submit = ccp_tx_submit;
  231. desc->ccp = chan->ccp;
  232. INIT_LIST_HEAD(&desc->pending);
  233. INIT_LIST_HEAD(&desc->active);
  234. desc->status = DMA_IN_PROGRESS;
  235. return desc;
  236. }
  237. static struct ccp_dma_desc *ccp_create_desc(struct dma_chan *dma_chan,
  238. struct scatterlist *dst_sg,
  239. unsigned int dst_nents,
  240. struct scatterlist *src_sg,
  241. unsigned int src_nents,
  242. unsigned long flags)
  243. {
  244. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  245. dma_chan);
  246. struct ccp_device *ccp = chan->ccp;
  247. struct ccp_dma_desc *desc;
  248. struct ccp_dma_cmd *cmd;
  249. struct ccp_cmd *ccp_cmd;
  250. struct ccp_passthru_nomap_engine *ccp_pt;
  251. unsigned int src_offset, src_len;
  252. unsigned int dst_offset, dst_len;
  253. unsigned int len;
  254. unsigned long sflags;
  255. size_t total_len;
  256. if (!dst_sg || !src_sg)
  257. return NULL;
  258. if (!dst_nents || !src_nents)
  259. return NULL;
  260. desc = ccp_alloc_dma_desc(chan, flags);
  261. if (!desc)
  262. return NULL;
  263. total_len = 0;
  264. src_len = sg_dma_len(src_sg);
  265. src_offset = 0;
  266. dst_len = sg_dma_len(dst_sg);
  267. dst_offset = 0;
  268. while (true) {
  269. if (!src_len) {
  270. src_nents--;
  271. if (!src_nents)
  272. break;
  273. src_sg = sg_next(src_sg);
  274. if (!src_sg)
  275. break;
  276. src_len = sg_dma_len(src_sg);
  277. src_offset = 0;
  278. continue;
  279. }
  280. if (!dst_len) {
  281. dst_nents--;
  282. if (!dst_nents)
  283. break;
  284. dst_sg = sg_next(dst_sg);
  285. if (!dst_sg)
  286. break;
  287. dst_len = sg_dma_len(dst_sg);
  288. dst_offset = 0;
  289. continue;
  290. }
  291. len = min(dst_len, src_len);
  292. cmd = ccp_alloc_dma_cmd(chan);
  293. if (!cmd)
  294. goto err;
  295. ccp_cmd = &cmd->ccp_cmd;
  296. ccp_pt = &ccp_cmd->u.passthru_nomap;
  297. ccp_cmd->flags = CCP_CMD_MAY_BACKLOG;
  298. ccp_cmd->flags |= CCP_CMD_PASSTHRU_NO_DMA_MAP;
  299. ccp_cmd->engine = CCP_ENGINE_PASSTHRU;
  300. ccp_pt->bit_mod = CCP_PASSTHRU_BITWISE_NOOP;
  301. ccp_pt->byte_swap = CCP_PASSTHRU_BYTESWAP_NOOP;
  302. ccp_pt->src_dma = sg_dma_address(src_sg) + src_offset;
  303. ccp_pt->dst_dma = sg_dma_address(dst_sg) + dst_offset;
  304. ccp_pt->src_len = len;
  305. ccp_pt->final = 1;
  306. ccp_cmd->callback = ccp_cmd_callback;
  307. ccp_cmd->data = desc;
  308. list_add_tail(&cmd->entry, &desc->pending);
  309. dev_dbg(ccp->dev,
  310. "%s - cmd=%p, src=%pad, dst=%pad, len=%llu\n", __func__,
  311. cmd, &ccp_pt->src_dma,
  312. &ccp_pt->dst_dma, ccp_pt->src_len);
  313. total_len += len;
  314. src_len -= len;
  315. src_offset += len;
  316. dst_len -= len;
  317. dst_offset += len;
  318. }
  319. desc->len = total_len;
  320. if (list_empty(&desc->pending))
  321. goto err;
  322. dev_dbg(ccp->dev, "%s - desc=%p\n", __func__, desc);
  323. spin_lock_irqsave(&chan->lock, sflags);
  324. list_add_tail(&desc->entry, &chan->pending);
  325. spin_unlock_irqrestore(&chan->lock, sflags);
  326. return desc;
  327. err:
  328. ccp_free_cmd_resources(ccp, &desc->pending);
  329. kmem_cache_free(ccp->dma_desc_cache, desc);
  330. return NULL;
  331. }
  332. static struct dma_async_tx_descriptor *ccp_prep_dma_memcpy(
  333. struct dma_chan *dma_chan, dma_addr_t dst, dma_addr_t src, size_t len,
  334. unsigned long flags)
  335. {
  336. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  337. dma_chan);
  338. struct ccp_dma_desc *desc;
  339. struct scatterlist dst_sg, src_sg;
  340. dev_dbg(chan->ccp->dev,
  341. "%s - src=%pad, dst=%pad, len=%zu, flags=%#lx\n",
  342. __func__, &src, &dst, len, flags);
  343. sg_init_table(&dst_sg, 1);
  344. sg_dma_address(&dst_sg) = dst;
  345. sg_dma_len(&dst_sg) = len;
  346. sg_init_table(&src_sg, 1);
  347. sg_dma_address(&src_sg) = src;
  348. sg_dma_len(&src_sg) = len;
  349. desc = ccp_create_desc(dma_chan, &dst_sg, 1, &src_sg, 1, flags);
  350. if (!desc)
  351. return NULL;
  352. return &desc->tx_desc;
  353. }
  354. static struct dma_async_tx_descriptor *ccp_prep_dma_sg(
  355. struct dma_chan *dma_chan, struct scatterlist *dst_sg,
  356. unsigned int dst_nents, struct scatterlist *src_sg,
  357. unsigned int src_nents, unsigned long flags)
  358. {
  359. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  360. dma_chan);
  361. struct ccp_dma_desc *desc;
  362. dev_dbg(chan->ccp->dev,
  363. "%s - src=%p, src_nents=%u dst=%p, dst_nents=%u, flags=%#lx\n",
  364. __func__, src_sg, src_nents, dst_sg, dst_nents, flags);
  365. desc = ccp_create_desc(dma_chan, dst_sg, dst_nents, src_sg, src_nents,
  366. flags);
  367. if (!desc)
  368. return NULL;
  369. return &desc->tx_desc;
  370. }
  371. static struct dma_async_tx_descriptor *ccp_prep_dma_interrupt(
  372. struct dma_chan *dma_chan, unsigned long flags)
  373. {
  374. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  375. dma_chan);
  376. struct ccp_dma_desc *desc;
  377. desc = ccp_alloc_dma_desc(chan, flags);
  378. if (!desc)
  379. return NULL;
  380. return &desc->tx_desc;
  381. }
  382. static void ccp_issue_pending(struct dma_chan *dma_chan)
  383. {
  384. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  385. dma_chan);
  386. struct ccp_dma_desc *desc;
  387. unsigned long flags;
  388. dev_dbg(chan->ccp->dev, "%s\n", __func__);
  389. spin_lock_irqsave(&chan->lock, flags);
  390. desc = __ccp_pending_to_active(chan);
  391. spin_unlock_irqrestore(&chan->lock, flags);
  392. /* If there was nothing active, start processing */
  393. if (desc)
  394. ccp_cmd_callback(desc, 0);
  395. }
  396. static enum dma_status ccp_tx_status(struct dma_chan *dma_chan,
  397. dma_cookie_t cookie,
  398. struct dma_tx_state *state)
  399. {
  400. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  401. dma_chan);
  402. struct ccp_dma_desc *desc;
  403. enum dma_status ret;
  404. unsigned long flags;
  405. if (chan->status == DMA_PAUSED) {
  406. ret = DMA_PAUSED;
  407. goto out;
  408. }
  409. ret = dma_cookie_status(dma_chan, cookie, state);
  410. if (ret == DMA_COMPLETE) {
  411. spin_lock_irqsave(&chan->lock, flags);
  412. /* Get status from complete chain, if still there */
  413. list_for_each_entry(desc, &chan->complete, entry) {
  414. if (desc->tx_desc.cookie != cookie)
  415. continue;
  416. ret = desc->status;
  417. break;
  418. }
  419. spin_unlock_irqrestore(&chan->lock, flags);
  420. }
  421. out:
  422. dev_dbg(chan->ccp->dev, "%s - %u\n", __func__, ret);
  423. return ret;
  424. }
  425. static int ccp_pause(struct dma_chan *dma_chan)
  426. {
  427. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  428. dma_chan);
  429. chan->status = DMA_PAUSED;
  430. /*TODO: Wait for active DMA to complete before returning? */
  431. return 0;
  432. }
  433. static int ccp_resume(struct dma_chan *dma_chan)
  434. {
  435. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  436. dma_chan);
  437. struct ccp_dma_desc *desc;
  438. unsigned long flags;
  439. spin_lock_irqsave(&chan->lock, flags);
  440. desc = list_first_entry_or_null(&chan->active, struct ccp_dma_desc,
  441. entry);
  442. spin_unlock_irqrestore(&chan->lock, flags);
  443. /* Indicate the channel is running again */
  444. chan->status = DMA_IN_PROGRESS;
  445. /* If there was something active, re-start */
  446. if (desc)
  447. ccp_cmd_callback(desc, 0);
  448. return 0;
  449. }
  450. static int ccp_terminate_all(struct dma_chan *dma_chan)
  451. {
  452. struct ccp_dma_chan *chan = container_of(dma_chan, struct ccp_dma_chan,
  453. dma_chan);
  454. unsigned long flags;
  455. dev_dbg(chan->ccp->dev, "%s\n", __func__);
  456. /*TODO: Wait for active DMA to complete before continuing */
  457. spin_lock_irqsave(&chan->lock, flags);
  458. /*TODO: Purge the complete list? */
  459. ccp_free_desc_resources(chan->ccp, &chan->active);
  460. ccp_free_desc_resources(chan->ccp, &chan->pending);
  461. spin_unlock_irqrestore(&chan->lock, flags);
  462. return 0;
  463. }
  464. int ccp_dmaengine_register(struct ccp_device *ccp)
  465. {
  466. struct ccp_dma_chan *chan;
  467. struct dma_device *dma_dev = &ccp->dma_dev;
  468. struct dma_chan *dma_chan;
  469. char *dma_cmd_cache_name;
  470. char *dma_desc_cache_name;
  471. unsigned int i;
  472. int ret;
  473. ccp->ccp_dma_chan = devm_kcalloc(ccp->dev, ccp->cmd_q_count,
  474. sizeof(*(ccp->ccp_dma_chan)),
  475. GFP_KERNEL);
  476. if (!ccp->ccp_dma_chan)
  477. return -ENOMEM;
  478. dma_cmd_cache_name = devm_kasprintf(ccp->dev, GFP_KERNEL,
  479. "%s-dmaengine-cmd-cache",
  480. ccp->name);
  481. if (!dma_cmd_cache_name)
  482. return -ENOMEM;
  483. ccp->dma_cmd_cache = kmem_cache_create(dma_cmd_cache_name,
  484. sizeof(struct ccp_dma_cmd),
  485. sizeof(void *),
  486. SLAB_HWCACHE_ALIGN, NULL);
  487. if (!ccp->dma_cmd_cache)
  488. return -ENOMEM;
  489. dma_desc_cache_name = devm_kasprintf(ccp->dev, GFP_KERNEL,
  490. "%s-dmaengine-desc-cache",
  491. ccp->name);
  492. if (!dma_desc_cache_name) {
  493. ret = -ENOMEM;
  494. goto err_cache;
  495. }
  496. ccp->dma_desc_cache = kmem_cache_create(dma_desc_cache_name,
  497. sizeof(struct ccp_dma_desc),
  498. sizeof(void *),
  499. SLAB_HWCACHE_ALIGN, NULL);
  500. if (!ccp->dma_desc_cache) {
  501. ret = -ENOMEM;
  502. goto err_cache;
  503. }
  504. dma_dev->dev = ccp->dev;
  505. dma_dev->src_addr_widths = CCP_DMA_WIDTH(dma_get_mask(ccp->dev));
  506. dma_dev->dst_addr_widths = CCP_DMA_WIDTH(dma_get_mask(ccp->dev));
  507. dma_dev->directions = DMA_MEM_TO_MEM;
  508. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  509. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  510. dma_cap_set(DMA_SG, dma_dev->cap_mask);
  511. dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
  512. INIT_LIST_HEAD(&dma_dev->channels);
  513. for (i = 0; i < ccp->cmd_q_count; i++) {
  514. chan = ccp->ccp_dma_chan + i;
  515. dma_chan = &chan->dma_chan;
  516. chan->ccp = ccp;
  517. spin_lock_init(&chan->lock);
  518. INIT_LIST_HEAD(&chan->pending);
  519. INIT_LIST_HEAD(&chan->active);
  520. INIT_LIST_HEAD(&chan->complete);
  521. tasklet_init(&chan->cleanup_tasklet, ccp_do_cleanup,
  522. (unsigned long)chan);
  523. dma_chan->device = dma_dev;
  524. dma_cookie_init(dma_chan);
  525. list_add_tail(&dma_chan->device_node, &dma_dev->channels);
  526. }
  527. dma_dev->device_free_chan_resources = ccp_free_chan_resources;
  528. dma_dev->device_prep_dma_memcpy = ccp_prep_dma_memcpy;
  529. dma_dev->device_prep_dma_sg = ccp_prep_dma_sg;
  530. dma_dev->device_prep_dma_interrupt = ccp_prep_dma_interrupt;
  531. dma_dev->device_issue_pending = ccp_issue_pending;
  532. dma_dev->device_tx_status = ccp_tx_status;
  533. dma_dev->device_pause = ccp_pause;
  534. dma_dev->device_resume = ccp_resume;
  535. dma_dev->device_terminate_all = ccp_terminate_all;
  536. ret = dma_async_device_register(dma_dev);
  537. if (ret)
  538. goto err_reg;
  539. return 0;
  540. err_reg:
  541. kmem_cache_destroy(ccp->dma_desc_cache);
  542. err_cache:
  543. kmem_cache_destroy(ccp->dma_cmd_cache);
  544. return ret;
  545. }
  546. void ccp_dmaengine_unregister(struct ccp_device *ccp)
  547. {
  548. struct dma_device *dma_dev = &ccp->dma_dev;
  549. dma_async_device_unregister(dma_dev);
  550. kmem_cache_destroy(ccp->dma_desc_cache);
  551. kmem_cache_destroy(ccp->dma_cmd_cache);
  552. }