fsl_raid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * drivers/dma/fsl_raid.c
  3. *
  4. * Freescale RAID Engine device driver
  5. *
  6. * Author:
  7. * Harninder Rai <harninder.rai@freescale.com>
  8. * Naveen Burmi <naveenburmi@freescale.com>
  9. *
  10. * Rewrite:
  11. * Xuelin Shi <xuelin.shi@freescale.com>
  12. *
  13. * Copyright (c) 2010-2014 Freescale Semiconductor, Inc.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * Neither the name of Freescale Semiconductor nor the
  23. * names of its contributors may be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * ALTERNATIVELY, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") as published by the Free Software
  28. * Foundation, either version 2 of that License or (at your option) any
  29. * later version.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  32. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  35. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  38. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  40. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * Theory of operation:
  43. *
  44. * General capabilities:
  45. * RAID Engine (RE) block is capable of offloading XOR, memcpy and P/Q
  46. * calculations required in RAID5 and RAID6 operations. RE driver
  47. * registers with Linux's ASYNC layer as dma driver. RE hardware
  48. * maintains strict ordering of the requests through chained
  49. * command queueing.
  50. *
  51. * Data flow:
  52. * Software RAID layer of Linux (MD layer) maintains RAID partitions,
  53. * strips, stripes etc. It sends requests to the underlying ASYNC layer
  54. * which further passes it to RE driver. ASYNC layer decides which request
  55. * goes to which job ring of RE hardware. For every request processed by
  56. * RAID Engine, driver gets an interrupt unless coalescing is set. The
  57. * per job ring interrupt handler checks the status register for errors,
  58. * clears the interrupt and leave the post interrupt processing to the irq
  59. * thread.
  60. */
  61. #include <linux/interrupt.h>
  62. #include <linux/module.h>
  63. #include <linux/of_irq.h>
  64. #include <linux/of_address.h>
  65. #include <linux/of_platform.h>
  66. #include <linux/dma-mapping.h>
  67. #include <linux/dmapool.h>
  68. #include <linux/dmaengine.h>
  69. #include <linux/io.h>
  70. #include <linux/spinlock.h>
  71. #include <linux/slab.h>
  72. #include "dmaengine.h"
  73. #include "fsl_raid.h"
  74. #define FSL_RE_MAX_XOR_SRCS 16
  75. #define FSL_RE_MAX_PQ_SRCS 16
  76. #define FSL_RE_MIN_DESCS 256
  77. #define FSL_RE_MAX_DESCS (4 * FSL_RE_MIN_DESCS)
  78. #define FSL_RE_FRAME_FORMAT 0x1
  79. #define FSL_RE_MAX_DATA_LEN (1024*1024)
  80. #define to_fsl_re_dma_desc(tx) container_of(tx, struct fsl_re_desc, async_tx)
  81. /* Add descriptors into per chan software queue - submit_q */
  82. static dma_cookie_t fsl_re_tx_submit(struct dma_async_tx_descriptor *tx)
  83. {
  84. struct fsl_re_desc *desc;
  85. struct fsl_re_chan *re_chan;
  86. dma_cookie_t cookie;
  87. unsigned long flags;
  88. desc = to_fsl_re_dma_desc(tx);
  89. re_chan = container_of(tx->chan, struct fsl_re_chan, chan);
  90. spin_lock_irqsave(&re_chan->desc_lock, flags);
  91. cookie = dma_cookie_assign(tx);
  92. list_add_tail(&desc->node, &re_chan->submit_q);
  93. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  94. return cookie;
  95. }
  96. /* Copy descriptor from per chan software queue into hardware job ring */
  97. static void fsl_re_issue_pending(struct dma_chan *chan)
  98. {
  99. struct fsl_re_chan *re_chan;
  100. int avail;
  101. struct fsl_re_desc *desc, *_desc;
  102. unsigned long flags;
  103. re_chan = container_of(chan, struct fsl_re_chan, chan);
  104. spin_lock_irqsave(&re_chan->desc_lock, flags);
  105. avail = FSL_RE_SLOT_AVAIL(
  106. in_be32(&re_chan->jrregs->inbring_slot_avail));
  107. list_for_each_entry_safe(desc, _desc, &re_chan->submit_q, node) {
  108. if (!avail)
  109. break;
  110. list_move_tail(&desc->node, &re_chan->active_q);
  111. memcpy(&re_chan->inb_ring_virt_addr[re_chan->inb_count],
  112. &desc->hwdesc, sizeof(struct fsl_re_hw_desc));
  113. re_chan->inb_count = (re_chan->inb_count + 1) &
  114. FSL_RE_RING_SIZE_MASK;
  115. out_be32(&re_chan->jrregs->inbring_add_job, FSL_RE_ADD_JOB(1));
  116. avail--;
  117. }
  118. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  119. }
  120. static void fsl_re_desc_done(struct fsl_re_desc *desc)
  121. {
  122. dma_cookie_complete(&desc->async_tx);
  123. dma_descriptor_unmap(&desc->async_tx);
  124. dmaengine_desc_get_callback_invoke(&desc->async_tx, NULL);
  125. }
  126. static void fsl_re_cleanup_descs(struct fsl_re_chan *re_chan)
  127. {
  128. struct fsl_re_desc *desc, *_desc;
  129. unsigned long flags;
  130. spin_lock_irqsave(&re_chan->desc_lock, flags);
  131. list_for_each_entry_safe(desc, _desc, &re_chan->ack_q, node) {
  132. if (async_tx_test_ack(&desc->async_tx))
  133. list_move_tail(&desc->node, &re_chan->free_q);
  134. }
  135. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  136. fsl_re_issue_pending(&re_chan->chan);
  137. }
  138. static void fsl_re_dequeue(unsigned long data)
  139. {
  140. struct fsl_re_chan *re_chan;
  141. struct fsl_re_desc *desc, *_desc;
  142. struct fsl_re_hw_desc *hwdesc;
  143. unsigned long flags;
  144. unsigned int count, oub_count;
  145. int found;
  146. re_chan = dev_get_drvdata((struct device *)data);
  147. fsl_re_cleanup_descs(re_chan);
  148. spin_lock_irqsave(&re_chan->desc_lock, flags);
  149. count = FSL_RE_SLOT_FULL(in_be32(&re_chan->jrregs->oubring_slot_full));
  150. while (count--) {
  151. found = 0;
  152. hwdesc = &re_chan->oub_ring_virt_addr[re_chan->oub_count];
  153. list_for_each_entry_safe(desc, _desc, &re_chan->active_q,
  154. node) {
  155. /* compare the hw dma addr to find the completed */
  156. if (desc->hwdesc.lbea32 == hwdesc->lbea32 &&
  157. desc->hwdesc.addr_low == hwdesc->addr_low) {
  158. found = 1;
  159. break;
  160. }
  161. }
  162. if (found) {
  163. fsl_re_desc_done(desc);
  164. list_move_tail(&desc->node, &re_chan->ack_q);
  165. } else {
  166. dev_err(re_chan->dev,
  167. "found hwdesc not in sw queue, discard it\n");
  168. }
  169. oub_count = (re_chan->oub_count + 1) & FSL_RE_RING_SIZE_MASK;
  170. re_chan->oub_count = oub_count;
  171. out_be32(&re_chan->jrregs->oubring_job_rmvd,
  172. FSL_RE_RMVD_JOB(1));
  173. }
  174. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  175. }
  176. /* Per Job Ring interrupt handler */
  177. static irqreturn_t fsl_re_isr(int irq, void *data)
  178. {
  179. struct fsl_re_chan *re_chan;
  180. u32 irqstate, status;
  181. re_chan = dev_get_drvdata((struct device *)data);
  182. irqstate = in_be32(&re_chan->jrregs->jr_interrupt_status);
  183. if (!irqstate)
  184. return IRQ_NONE;
  185. /*
  186. * There's no way in upper layer (read MD layer) to recover from
  187. * error conditions except restart everything. In long term we
  188. * need to do something more than just crashing
  189. */
  190. if (irqstate & FSL_RE_ERROR) {
  191. status = in_be32(&re_chan->jrregs->jr_status);
  192. dev_err(re_chan->dev, "chan error irqstate: %x, status: %x\n",
  193. irqstate, status);
  194. }
  195. /* Clear interrupt */
  196. out_be32(&re_chan->jrregs->jr_interrupt_status, FSL_RE_CLR_INTR);
  197. tasklet_schedule(&re_chan->irqtask);
  198. return IRQ_HANDLED;
  199. }
  200. static enum dma_status fsl_re_tx_status(struct dma_chan *chan,
  201. dma_cookie_t cookie,
  202. struct dma_tx_state *txstate)
  203. {
  204. return dma_cookie_status(chan, cookie, txstate);
  205. }
  206. static void fill_cfd_frame(struct fsl_re_cmpnd_frame *cf, u8 index,
  207. size_t length, dma_addr_t addr, bool final)
  208. {
  209. u32 efrl = length & FSL_RE_CF_LENGTH_MASK;
  210. efrl |= final << FSL_RE_CF_FINAL_SHIFT;
  211. cf[index].efrl32 = efrl;
  212. cf[index].addr_high = upper_32_bits(addr);
  213. cf[index].addr_low = lower_32_bits(addr);
  214. }
  215. static struct fsl_re_desc *fsl_re_init_desc(struct fsl_re_chan *re_chan,
  216. struct fsl_re_desc *desc,
  217. void *cf, dma_addr_t paddr)
  218. {
  219. desc->re_chan = re_chan;
  220. desc->async_tx.tx_submit = fsl_re_tx_submit;
  221. dma_async_tx_descriptor_init(&desc->async_tx, &re_chan->chan);
  222. INIT_LIST_HEAD(&desc->node);
  223. desc->hwdesc.fmt32 = FSL_RE_FRAME_FORMAT << FSL_RE_HWDESC_FMT_SHIFT;
  224. desc->hwdesc.lbea32 = upper_32_bits(paddr);
  225. desc->hwdesc.addr_low = lower_32_bits(paddr);
  226. desc->cf_addr = cf;
  227. desc->cf_paddr = paddr;
  228. desc->cdb_addr = (void *)(cf + FSL_RE_CF_DESC_SIZE);
  229. desc->cdb_paddr = paddr + FSL_RE_CF_DESC_SIZE;
  230. return desc;
  231. }
  232. static struct fsl_re_desc *fsl_re_chan_alloc_desc(struct fsl_re_chan *re_chan,
  233. unsigned long flags)
  234. {
  235. struct fsl_re_desc *desc = NULL;
  236. void *cf;
  237. dma_addr_t paddr;
  238. unsigned long lock_flag;
  239. fsl_re_cleanup_descs(re_chan);
  240. spin_lock_irqsave(&re_chan->desc_lock, lock_flag);
  241. if (!list_empty(&re_chan->free_q)) {
  242. /* take one desc from free_q */
  243. desc = list_first_entry(&re_chan->free_q,
  244. struct fsl_re_desc, node);
  245. list_del(&desc->node);
  246. desc->async_tx.flags = flags;
  247. }
  248. spin_unlock_irqrestore(&re_chan->desc_lock, lock_flag);
  249. if (!desc) {
  250. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  251. if (!desc)
  252. return NULL;
  253. cf = dma_pool_alloc(re_chan->re_dev->cf_desc_pool, GFP_NOWAIT,
  254. &paddr);
  255. if (!cf) {
  256. kfree(desc);
  257. return NULL;
  258. }
  259. desc = fsl_re_init_desc(re_chan, desc, cf, paddr);
  260. desc->async_tx.flags = flags;
  261. spin_lock_irqsave(&re_chan->desc_lock, lock_flag);
  262. re_chan->alloc_count++;
  263. spin_unlock_irqrestore(&re_chan->desc_lock, lock_flag);
  264. }
  265. return desc;
  266. }
  267. static struct dma_async_tx_descriptor *fsl_re_prep_dma_genq(
  268. struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  269. unsigned int src_cnt, const unsigned char *scf, size_t len,
  270. unsigned long flags)
  271. {
  272. struct fsl_re_chan *re_chan;
  273. struct fsl_re_desc *desc;
  274. struct fsl_re_xor_cdb *xor;
  275. struct fsl_re_cmpnd_frame *cf;
  276. u32 cdb;
  277. unsigned int i, j;
  278. unsigned int save_src_cnt = src_cnt;
  279. int cont_q = 0;
  280. re_chan = container_of(chan, struct fsl_re_chan, chan);
  281. if (len > FSL_RE_MAX_DATA_LEN) {
  282. dev_err(re_chan->dev, "genq tx length %zu, max length %d\n",
  283. len, FSL_RE_MAX_DATA_LEN);
  284. return NULL;
  285. }
  286. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  287. if (desc <= 0)
  288. return NULL;
  289. if (scf && (flags & DMA_PREP_CONTINUE)) {
  290. cont_q = 1;
  291. src_cnt += 1;
  292. }
  293. /* Filling xor CDB */
  294. cdb = FSL_RE_XOR_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  295. cdb |= (src_cnt - 1) << FSL_RE_CDB_NRCS_SHIFT;
  296. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  297. cdb |= FSL_RE_INTR_ON_ERROR << FSL_RE_CDB_ERROR_SHIFT;
  298. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  299. xor = desc->cdb_addr;
  300. xor->cdb32 = cdb;
  301. if (scf) {
  302. /* compute q = src0*coef0^src1*coef1^..., * is GF(8) mult */
  303. for (i = 0; i < save_src_cnt; i++)
  304. xor->gfm[i] = scf[i];
  305. if (cont_q)
  306. xor->gfm[i++] = 1;
  307. } else {
  308. /* compute P, that is XOR all srcs */
  309. for (i = 0; i < src_cnt; i++)
  310. xor->gfm[i] = 1;
  311. }
  312. /* Filling frame 0 of compound frame descriptor with CDB */
  313. cf = desc->cf_addr;
  314. fill_cfd_frame(cf, 0, sizeof(*xor), desc->cdb_paddr, 0);
  315. /* Fill CFD's 1st frame with dest buffer */
  316. fill_cfd_frame(cf, 1, len, dest, 0);
  317. /* Fill CFD's rest of the frames with source buffers */
  318. for (i = 2, j = 0; j < save_src_cnt; i++, j++)
  319. fill_cfd_frame(cf, i, len, src[j], 0);
  320. if (cont_q)
  321. fill_cfd_frame(cf, i++, len, dest, 0);
  322. /* Setting the final bit in the last source buffer frame in CFD */
  323. cf[i - 1].efrl32 |= 1 << FSL_RE_CF_FINAL_SHIFT;
  324. return &desc->async_tx;
  325. }
  326. /*
  327. * Prep function for P parity calculation.In RAID Engine terminology,
  328. * XOR calculation is called GenQ calculation done through GenQ command
  329. */
  330. static struct dma_async_tx_descriptor *fsl_re_prep_dma_xor(
  331. struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  332. unsigned int src_cnt, size_t len, unsigned long flags)
  333. {
  334. /* NULL let genq take all coef as 1 */
  335. return fsl_re_prep_dma_genq(chan, dest, src, src_cnt, NULL, len, flags);
  336. }
  337. /*
  338. * Prep function for P/Q parity calculation.In RAID Engine terminology,
  339. * P/Q calculation is called GenQQ done through GenQQ command
  340. */
  341. static struct dma_async_tx_descriptor *fsl_re_prep_dma_pq(
  342. struct dma_chan *chan, dma_addr_t *dest, dma_addr_t *src,
  343. unsigned int src_cnt, const unsigned char *scf, size_t len,
  344. unsigned long flags)
  345. {
  346. struct fsl_re_chan *re_chan;
  347. struct fsl_re_desc *desc;
  348. struct fsl_re_pq_cdb *pq;
  349. struct fsl_re_cmpnd_frame *cf;
  350. u32 cdb;
  351. u8 *p;
  352. int gfmq_len, i, j;
  353. unsigned int save_src_cnt = src_cnt;
  354. re_chan = container_of(chan, struct fsl_re_chan, chan);
  355. if (len > FSL_RE_MAX_DATA_LEN) {
  356. dev_err(re_chan->dev, "pq tx length is %zu, max length is %d\n",
  357. len, FSL_RE_MAX_DATA_LEN);
  358. return NULL;
  359. }
  360. /*
  361. * RE requires at least 2 sources, if given only one source, we pass the
  362. * second source same as the first one.
  363. * With only one source, generating P is meaningless, only generate Q.
  364. */
  365. if (src_cnt == 1) {
  366. struct dma_async_tx_descriptor *tx;
  367. dma_addr_t dma_src[2];
  368. unsigned char coef[2];
  369. dma_src[0] = *src;
  370. coef[0] = *scf;
  371. dma_src[1] = *src;
  372. coef[1] = 0;
  373. tx = fsl_re_prep_dma_genq(chan, dest[1], dma_src, 2, coef, len,
  374. flags);
  375. if (tx)
  376. desc = to_fsl_re_dma_desc(tx);
  377. return tx;
  378. }
  379. /*
  380. * During RAID6 array creation, Linux's MD layer gets P and Q
  381. * calculated separately in two steps. But our RAID Engine has
  382. * the capability to calculate both P and Q with a single command
  383. * Hence to merge well with MD layer, we need to provide a hook
  384. * here and call re_jq_prep_dma_genq() function
  385. */
  386. if (flags & DMA_PREP_PQ_DISABLE_P)
  387. return fsl_re_prep_dma_genq(chan, dest[1], src, src_cnt,
  388. scf, len, flags);
  389. if (flags & DMA_PREP_CONTINUE)
  390. src_cnt += 3;
  391. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  392. if (desc <= 0)
  393. return NULL;
  394. /* Filling GenQQ CDB */
  395. cdb = FSL_RE_PQ_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  396. cdb |= (src_cnt - 1) << FSL_RE_CDB_NRCS_SHIFT;
  397. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  398. cdb |= FSL_RE_BUFFER_OUTPUT << FSL_RE_CDB_BUFFER_SHIFT;
  399. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  400. pq = desc->cdb_addr;
  401. pq->cdb32 = cdb;
  402. p = pq->gfm_q1;
  403. /* Init gfm_q1[] */
  404. for (i = 0; i < src_cnt; i++)
  405. p[i] = 1;
  406. /* Align gfm[] to 32bit */
  407. gfmq_len = ALIGN(src_cnt, 4);
  408. /* Init gfm_q2[] */
  409. p += gfmq_len;
  410. for (i = 0; i < src_cnt; i++)
  411. p[i] = scf[i];
  412. /* Filling frame 0 of compound frame descriptor with CDB */
  413. cf = desc->cf_addr;
  414. fill_cfd_frame(cf, 0, sizeof(struct fsl_re_pq_cdb), desc->cdb_paddr, 0);
  415. /* Fill CFD's 1st & 2nd frame with dest buffers */
  416. for (i = 1, j = 0; i < 3; i++, j++)
  417. fill_cfd_frame(cf, i, len, dest[j], 0);
  418. /* Fill CFD's rest of the frames with source buffers */
  419. for (i = 3, j = 0; j < save_src_cnt; i++, j++)
  420. fill_cfd_frame(cf, i, len, src[j], 0);
  421. /* PQ computation continuation */
  422. if (flags & DMA_PREP_CONTINUE) {
  423. if (src_cnt - save_src_cnt == 3) {
  424. p[save_src_cnt] = 0;
  425. p[save_src_cnt + 1] = 0;
  426. p[save_src_cnt + 2] = 1;
  427. fill_cfd_frame(cf, i++, len, dest[0], 0);
  428. fill_cfd_frame(cf, i++, len, dest[1], 0);
  429. fill_cfd_frame(cf, i++, len, dest[1], 0);
  430. } else {
  431. dev_err(re_chan->dev, "PQ tx continuation error!\n");
  432. return NULL;
  433. }
  434. }
  435. /* Setting the final bit in the last source buffer frame in CFD */
  436. cf[i - 1].efrl32 |= 1 << FSL_RE_CF_FINAL_SHIFT;
  437. return &desc->async_tx;
  438. }
  439. /*
  440. * Prep function for memcpy. In RAID Engine, memcpy is done through MOVE
  441. * command. Logic of this function will need to be modified once multipage
  442. * support is added in Linux's MD/ASYNC Layer
  443. */
  444. static struct dma_async_tx_descriptor *fsl_re_prep_dma_memcpy(
  445. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  446. size_t len, unsigned long flags)
  447. {
  448. struct fsl_re_chan *re_chan;
  449. struct fsl_re_desc *desc;
  450. size_t length;
  451. struct fsl_re_cmpnd_frame *cf;
  452. struct fsl_re_move_cdb *move;
  453. u32 cdb;
  454. re_chan = container_of(chan, struct fsl_re_chan, chan);
  455. if (len > FSL_RE_MAX_DATA_LEN) {
  456. dev_err(re_chan->dev, "cp tx length is %zu, max length is %d\n",
  457. len, FSL_RE_MAX_DATA_LEN);
  458. return NULL;
  459. }
  460. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  461. if (desc <= 0)
  462. return NULL;
  463. /* Filling move CDB */
  464. cdb = FSL_RE_MOVE_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  465. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  466. cdb |= FSL_RE_INTR_ON_ERROR << FSL_RE_CDB_ERROR_SHIFT;
  467. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  468. move = desc->cdb_addr;
  469. move->cdb32 = cdb;
  470. /* Filling frame 0 of CFD with move CDB */
  471. cf = desc->cf_addr;
  472. fill_cfd_frame(cf, 0, sizeof(*move), desc->cdb_paddr, 0);
  473. length = min_t(size_t, len, FSL_RE_MAX_DATA_LEN);
  474. /* Fill CFD's 1st frame with dest buffer */
  475. fill_cfd_frame(cf, 1, length, dest, 0);
  476. /* Fill CFD's 2nd frame with src buffer */
  477. fill_cfd_frame(cf, 2, length, src, 1);
  478. return &desc->async_tx;
  479. }
  480. static int fsl_re_alloc_chan_resources(struct dma_chan *chan)
  481. {
  482. struct fsl_re_chan *re_chan;
  483. struct fsl_re_desc *desc;
  484. void *cf;
  485. dma_addr_t paddr;
  486. int i;
  487. re_chan = container_of(chan, struct fsl_re_chan, chan);
  488. for (i = 0; i < FSL_RE_MIN_DESCS; i++) {
  489. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  490. if (!desc)
  491. break;
  492. cf = dma_pool_alloc(re_chan->re_dev->cf_desc_pool, GFP_KERNEL,
  493. &paddr);
  494. if (!cf) {
  495. kfree(desc);
  496. break;
  497. }
  498. INIT_LIST_HEAD(&desc->node);
  499. fsl_re_init_desc(re_chan, desc, cf, paddr);
  500. list_add_tail(&desc->node, &re_chan->free_q);
  501. re_chan->alloc_count++;
  502. }
  503. return re_chan->alloc_count;
  504. }
  505. static void fsl_re_free_chan_resources(struct dma_chan *chan)
  506. {
  507. struct fsl_re_chan *re_chan;
  508. struct fsl_re_desc *desc;
  509. re_chan = container_of(chan, struct fsl_re_chan, chan);
  510. while (re_chan->alloc_count--) {
  511. desc = list_first_entry(&re_chan->free_q,
  512. struct fsl_re_desc,
  513. node);
  514. list_del(&desc->node);
  515. dma_pool_free(re_chan->re_dev->cf_desc_pool, desc->cf_addr,
  516. desc->cf_paddr);
  517. kfree(desc);
  518. }
  519. if (!list_empty(&re_chan->free_q))
  520. dev_err(re_chan->dev, "chan resource cannot be cleaned!\n");
  521. }
  522. static int fsl_re_chan_probe(struct platform_device *ofdev,
  523. struct device_node *np, u8 q, u32 off)
  524. {
  525. struct device *dev, *chandev;
  526. struct fsl_re_drv_private *re_priv;
  527. struct fsl_re_chan *chan;
  528. struct dma_device *dma_dev;
  529. u32 ptr;
  530. u32 status;
  531. int ret = 0, rc;
  532. struct platform_device *chan_ofdev;
  533. dev = &ofdev->dev;
  534. re_priv = dev_get_drvdata(dev);
  535. dma_dev = &re_priv->dma_dev;
  536. chan = devm_kzalloc(dev, sizeof(*chan), GFP_KERNEL);
  537. if (!chan)
  538. return -ENOMEM;
  539. /* create platform device for chan node */
  540. chan_ofdev = of_platform_device_create(np, NULL, dev);
  541. if (!chan_ofdev) {
  542. dev_err(dev, "Not able to create ofdev for jr %d\n", q);
  543. ret = -EINVAL;
  544. goto err_free;
  545. }
  546. /* read reg property from dts */
  547. rc = of_property_read_u32(np, "reg", &ptr);
  548. if (rc) {
  549. dev_err(dev, "Reg property not found in jr %d\n", q);
  550. ret = -ENODEV;
  551. goto err_free;
  552. }
  553. chan->jrregs = (struct fsl_re_chan_cfg *)((u8 *)re_priv->re_regs +
  554. off + ptr);
  555. /* read irq property from dts */
  556. chan->irq = irq_of_parse_and_map(np, 0);
  557. if (!chan->irq) {
  558. dev_err(dev, "No IRQ defined for JR %d\n", q);
  559. ret = -ENODEV;
  560. goto err_free;
  561. }
  562. snprintf(chan->name, sizeof(chan->name), "re_jr%02d", q);
  563. chandev = &chan_ofdev->dev;
  564. tasklet_init(&chan->irqtask, fsl_re_dequeue, (unsigned long)chandev);
  565. ret = request_irq(chan->irq, fsl_re_isr, 0, chan->name, chandev);
  566. if (ret) {
  567. dev_err(dev, "Unable to register interrupt for JR %d\n", q);
  568. ret = -EINVAL;
  569. goto err_free;
  570. }
  571. re_priv->re_jrs[q] = chan;
  572. chan->chan.device = dma_dev;
  573. chan->chan.private = chan;
  574. chan->dev = chandev;
  575. chan->re_dev = re_priv;
  576. spin_lock_init(&chan->desc_lock);
  577. INIT_LIST_HEAD(&chan->ack_q);
  578. INIT_LIST_HEAD(&chan->active_q);
  579. INIT_LIST_HEAD(&chan->submit_q);
  580. INIT_LIST_HEAD(&chan->free_q);
  581. chan->inb_ring_virt_addr = dma_pool_alloc(chan->re_dev->hw_desc_pool,
  582. GFP_KERNEL, &chan->inb_phys_addr);
  583. if (!chan->inb_ring_virt_addr) {
  584. dev_err(dev, "No dma memory for inb_ring_virt_addr\n");
  585. ret = -ENOMEM;
  586. goto err_free;
  587. }
  588. chan->oub_ring_virt_addr = dma_pool_alloc(chan->re_dev->hw_desc_pool,
  589. GFP_KERNEL, &chan->oub_phys_addr);
  590. if (!chan->oub_ring_virt_addr) {
  591. dev_err(dev, "No dma memory for oub_ring_virt_addr\n");
  592. ret = -ENOMEM;
  593. goto err_free_1;
  594. }
  595. /* Program the Inbound/Outbound ring base addresses and size */
  596. out_be32(&chan->jrregs->inbring_base_h,
  597. chan->inb_phys_addr & FSL_RE_ADDR_BIT_MASK);
  598. out_be32(&chan->jrregs->oubring_base_h,
  599. chan->oub_phys_addr & FSL_RE_ADDR_BIT_MASK);
  600. out_be32(&chan->jrregs->inbring_base_l,
  601. chan->inb_phys_addr >> FSL_RE_ADDR_BIT_SHIFT);
  602. out_be32(&chan->jrregs->oubring_base_l,
  603. chan->oub_phys_addr >> FSL_RE_ADDR_BIT_SHIFT);
  604. out_be32(&chan->jrregs->inbring_size,
  605. FSL_RE_RING_SIZE << FSL_RE_RING_SIZE_SHIFT);
  606. out_be32(&chan->jrregs->oubring_size,
  607. FSL_RE_RING_SIZE << FSL_RE_RING_SIZE_SHIFT);
  608. /* Read LIODN value from u-boot */
  609. status = in_be32(&chan->jrregs->jr_config_1) & FSL_RE_REG_LIODN_MASK;
  610. /* Program the CFG reg */
  611. out_be32(&chan->jrregs->jr_config_1,
  612. FSL_RE_CFG1_CBSI | FSL_RE_CFG1_CBS0 | status);
  613. dev_set_drvdata(chandev, chan);
  614. /* Enable RE/CHAN */
  615. out_be32(&chan->jrregs->jr_command, FSL_RE_ENABLE);
  616. return 0;
  617. err_free_1:
  618. dma_pool_free(chan->re_dev->hw_desc_pool, chan->inb_ring_virt_addr,
  619. chan->inb_phys_addr);
  620. err_free:
  621. return ret;
  622. }
  623. /* Probe function for RAID Engine */
  624. static int fsl_re_probe(struct platform_device *ofdev)
  625. {
  626. struct fsl_re_drv_private *re_priv;
  627. struct device_node *np;
  628. struct device_node *child;
  629. u32 off;
  630. u8 ridx = 0;
  631. struct dma_device *dma_dev;
  632. struct resource *res;
  633. int rc;
  634. struct device *dev = &ofdev->dev;
  635. re_priv = devm_kzalloc(dev, sizeof(*re_priv), GFP_KERNEL);
  636. if (!re_priv)
  637. return -ENOMEM;
  638. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  639. if (!res)
  640. return -ENODEV;
  641. /* IOMAP the entire RAID Engine region */
  642. re_priv->re_regs = devm_ioremap(dev, res->start, resource_size(res));
  643. if (!re_priv->re_regs)
  644. return -EBUSY;
  645. /* Program the RE mode */
  646. out_be32(&re_priv->re_regs->global_config, FSL_RE_NON_DPAA_MODE);
  647. /* Program Galois Field polynomial */
  648. out_be32(&re_priv->re_regs->galois_field_config, FSL_RE_GFM_POLY);
  649. dev_info(dev, "version %x, mode %x, gfp %x\n",
  650. in_be32(&re_priv->re_regs->re_version_id),
  651. in_be32(&re_priv->re_regs->global_config),
  652. in_be32(&re_priv->re_regs->galois_field_config));
  653. dma_dev = &re_priv->dma_dev;
  654. dma_dev->dev = dev;
  655. INIT_LIST_HEAD(&dma_dev->channels);
  656. dma_set_mask(dev, DMA_BIT_MASK(40));
  657. dma_dev->device_alloc_chan_resources = fsl_re_alloc_chan_resources;
  658. dma_dev->device_tx_status = fsl_re_tx_status;
  659. dma_dev->device_issue_pending = fsl_re_issue_pending;
  660. dma_dev->max_xor = FSL_RE_MAX_XOR_SRCS;
  661. dma_dev->device_prep_dma_xor = fsl_re_prep_dma_xor;
  662. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  663. dma_dev->max_pq = FSL_RE_MAX_PQ_SRCS;
  664. dma_dev->device_prep_dma_pq = fsl_re_prep_dma_pq;
  665. dma_cap_set(DMA_PQ, dma_dev->cap_mask);
  666. dma_dev->device_prep_dma_memcpy = fsl_re_prep_dma_memcpy;
  667. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  668. dma_dev->device_free_chan_resources = fsl_re_free_chan_resources;
  669. re_priv->total_chans = 0;
  670. re_priv->cf_desc_pool = dmam_pool_create("fsl_re_cf_desc_pool", dev,
  671. FSL_RE_CF_CDB_SIZE,
  672. FSL_RE_CF_CDB_ALIGN, 0);
  673. if (!re_priv->cf_desc_pool) {
  674. dev_err(dev, "No memory for fsl re_cf desc pool\n");
  675. return -ENOMEM;
  676. }
  677. re_priv->hw_desc_pool = dmam_pool_create("fsl_re_hw_desc_pool", dev,
  678. sizeof(struct fsl_re_hw_desc) * FSL_RE_RING_SIZE,
  679. FSL_RE_FRAME_ALIGN, 0);
  680. if (!re_priv->hw_desc_pool) {
  681. dev_err(dev, "No memory for fsl re_hw desc pool\n");
  682. return -ENOMEM;
  683. }
  684. dev_set_drvdata(dev, re_priv);
  685. /* Parse Device tree to find out the total number of JQs present */
  686. for_each_compatible_node(np, NULL, "fsl,raideng-v1.0-job-queue") {
  687. rc = of_property_read_u32(np, "reg", &off);
  688. if (rc) {
  689. dev_err(dev, "Reg property not found in JQ node\n");
  690. of_node_put(np);
  691. return -ENODEV;
  692. }
  693. /* Find out the Job Rings present under each JQ */
  694. for_each_child_of_node(np, child) {
  695. rc = of_device_is_compatible(child,
  696. "fsl,raideng-v1.0-job-ring");
  697. if (rc) {
  698. fsl_re_chan_probe(ofdev, child, ridx++, off);
  699. re_priv->total_chans++;
  700. }
  701. }
  702. }
  703. dma_async_device_register(dma_dev);
  704. return 0;
  705. }
  706. static void fsl_re_remove_chan(struct fsl_re_chan *chan)
  707. {
  708. tasklet_kill(&chan->irqtask);
  709. dma_pool_free(chan->re_dev->hw_desc_pool, chan->inb_ring_virt_addr,
  710. chan->inb_phys_addr);
  711. dma_pool_free(chan->re_dev->hw_desc_pool, chan->oub_ring_virt_addr,
  712. chan->oub_phys_addr);
  713. }
  714. static int fsl_re_remove(struct platform_device *ofdev)
  715. {
  716. struct fsl_re_drv_private *re_priv;
  717. struct device *dev;
  718. int i;
  719. dev = &ofdev->dev;
  720. re_priv = dev_get_drvdata(dev);
  721. /* Cleanup chan related memory areas */
  722. for (i = 0; i < re_priv->total_chans; i++)
  723. fsl_re_remove_chan(re_priv->re_jrs[i]);
  724. /* Unregister the driver */
  725. dma_async_device_unregister(&re_priv->dma_dev);
  726. return 0;
  727. }
  728. static struct of_device_id fsl_re_ids[] = {
  729. { .compatible = "fsl,raideng-v1.0", },
  730. {}
  731. };
  732. static struct platform_driver fsl_re_driver = {
  733. .driver = {
  734. .name = "fsl-raideng",
  735. .of_match_table = fsl_re_ids,
  736. },
  737. .probe = fsl_re_probe,
  738. .remove = fsl_re_remove,
  739. };
  740. module_platform_driver(fsl_re_driver);
  741. MODULE_AUTHOR("Harninder Rai <harninder.rai@freescale.com>");
  742. MODULE_LICENSE("GPL v2");
  743. MODULE_DESCRIPTION("Freescale RAID Engine Device Driver");