fsl_raid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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_async_tx_callback callback;
  123. void *callback_param;
  124. dma_cookie_complete(&desc->async_tx);
  125. callback = desc->async_tx.callback;
  126. callback_param = desc->async_tx.callback_param;
  127. if (callback)
  128. callback(callback_param);
  129. dma_descriptor_unmap(&desc->async_tx);
  130. }
  131. static void fsl_re_cleanup_descs(struct fsl_re_chan *re_chan)
  132. {
  133. struct fsl_re_desc *desc, *_desc;
  134. unsigned long flags;
  135. spin_lock_irqsave(&re_chan->desc_lock, flags);
  136. list_for_each_entry_safe(desc, _desc, &re_chan->ack_q, node) {
  137. if (async_tx_test_ack(&desc->async_tx))
  138. list_move_tail(&desc->node, &re_chan->free_q);
  139. }
  140. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  141. fsl_re_issue_pending(&re_chan->chan);
  142. }
  143. static void fsl_re_dequeue(unsigned long data)
  144. {
  145. struct fsl_re_chan *re_chan;
  146. struct fsl_re_desc *desc, *_desc;
  147. struct fsl_re_hw_desc *hwdesc;
  148. unsigned long flags;
  149. unsigned int count, oub_count;
  150. int found;
  151. re_chan = dev_get_drvdata((struct device *)data);
  152. fsl_re_cleanup_descs(re_chan);
  153. spin_lock_irqsave(&re_chan->desc_lock, flags);
  154. count = FSL_RE_SLOT_FULL(in_be32(&re_chan->jrregs->oubring_slot_full));
  155. while (count--) {
  156. found = 0;
  157. hwdesc = &re_chan->oub_ring_virt_addr[re_chan->oub_count];
  158. list_for_each_entry_safe(desc, _desc, &re_chan->active_q,
  159. node) {
  160. /* compare the hw dma addr to find the completed */
  161. if (desc->hwdesc.lbea32 == hwdesc->lbea32 &&
  162. desc->hwdesc.addr_low == hwdesc->addr_low) {
  163. found = 1;
  164. break;
  165. }
  166. }
  167. if (found) {
  168. fsl_re_desc_done(desc);
  169. list_move_tail(&desc->node, &re_chan->ack_q);
  170. } else {
  171. dev_err(re_chan->dev,
  172. "found hwdesc not in sw queue, discard it\n");
  173. }
  174. oub_count = (re_chan->oub_count + 1) & FSL_RE_RING_SIZE_MASK;
  175. re_chan->oub_count = oub_count;
  176. out_be32(&re_chan->jrregs->oubring_job_rmvd,
  177. FSL_RE_RMVD_JOB(1));
  178. }
  179. spin_unlock_irqrestore(&re_chan->desc_lock, flags);
  180. }
  181. /* Per Job Ring interrupt handler */
  182. static irqreturn_t fsl_re_isr(int irq, void *data)
  183. {
  184. struct fsl_re_chan *re_chan;
  185. u32 irqstate, status;
  186. re_chan = dev_get_drvdata((struct device *)data);
  187. irqstate = in_be32(&re_chan->jrregs->jr_interrupt_status);
  188. if (!irqstate)
  189. return IRQ_NONE;
  190. /*
  191. * There's no way in upper layer (read MD layer) to recover from
  192. * error conditions except restart everything. In long term we
  193. * need to do something more than just crashing
  194. */
  195. if (irqstate & FSL_RE_ERROR) {
  196. status = in_be32(&re_chan->jrregs->jr_status);
  197. dev_err(re_chan->dev, "chan error irqstate: %x, status: %x\n",
  198. irqstate, status);
  199. }
  200. /* Clear interrupt */
  201. out_be32(&re_chan->jrregs->jr_interrupt_status, FSL_RE_CLR_INTR);
  202. tasklet_schedule(&re_chan->irqtask);
  203. return IRQ_HANDLED;
  204. }
  205. static enum dma_status fsl_re_tx_status(struct dma_chan *chan,
  206. dma_cookie_t cookie,
  207. struct dma_tx_state *txstate)
  208. {
  209. return dma_cookie_status(chan, cookie, txstate);
  210. }
  211. static void fill_cfd_frame(struct fsl_re_cmpnd_frame *cf, u8 index,
  212. size_t length, dma_addr_t addr, bool final)
  213. {
  214. u32 efrl = length & FSL_RE_CF_LENGTH_MASK;
  215. efrl |= final << FSL_RE_CF_FINAL_SHIFT;
  216. cf[index].efrl32 = efrl;
  217. cf[index].addr_high = upper_32_bits(addr);
  218. cf[index].addr_low = lower_32_bits(addr);
  219. }
  220. static struct fsl_re_desc *fsl_re_init_desc(struct fsl_re_chan *re_chan,
  221. struct fsl_re_desc *desc,
  222. void *cf, dma_addr_t paddr)
  223. {
  224. desc->re_chan = re_chan;
  225. desc->async_tx.tx_submit = fsl_re_tx_submit;
  226. dma_async_tx_descriptor_init(&desc->async_tx, &re_chan->chan);
  227. INIT_LIST_HEAD(&desc->node);
  228. desc->hwdesc.fmt32 = FSL_RE_FRAME_FORMAT << FSL_RE_HWDESC_FMT_SHIFT;
  229. desc->hwdesc.lbea32 = upper_32_bits(paddr);
  230. desc->hwdesc.addr_low = lower_32_bits(paddr);
  231. desc->cf_addr = cf;
  232. desc->cf_paddr = paddr;
  233. desc->cdb_addr = (void *)(cf + FSL_RE_CF_DESC_SIZE);
  234. desc->cdb_paddr = paddr + FSL_RE_CF_DESC_SIZE;
  235. return desc;
  236. }
  237. static struct fsl_re_desc *fsl_re_chan_alloc_desc(struct fsl_re_chan *re_chan,
  238. unsigned long flags)
  239. {
  240. struct fsl_re_desc *desc = NULL;
  241. void *cf;
  242. dma_addr_t paddr;
  243. unsigned long lock_flag;
  244. fsl_re_cleanup_descs(re_chan);
  245. spin_lock_irqsave(&re_chan->desc_lock, lock_flag);
  246. if (!list_empty(&re_chan->free_q)) {
  247. /* take one desc from free_q */
  248. desc = list_first_entry(&re_chan->free_q,
  249. struct fsl_re_desc, node);
  250. list_del(&desc->node);
  251. desc->async_tx.flags = flags;
  252. }
  253. spin_unlock_irqrestore(&re_chan->desc_lock, lock_flag);
  254. if (!desc) {
  255. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  256. if (!desc)
  257. return NULL;
  258. cf = dma_pool_alloc(re_chan->re_dev->cf_desc_pool, GFP_NOWAIT,
  259. &paddr);
  260. if (!cf) {
  261. kfree(desc);
  262. return NULL;
  263. }
  264. desc = fsl_re_init_desc(re_chan, desc, cf, paddr);
  265. desc->async_tx.flags = flags;
  266. spin_lock_irqsave(&re_chan->desc_lock, lock_flag);
  267. re_chan->alloc_count++;
  268. spin_unlock_irqrestore(&re_chan->desc_lock, lock_flag);
  269. }
  270. return desc;
  271. }
  272. static struct dma_async_tx_descriptor *fsl_re_prep_dma_genq(
  273. struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  274. unsigned int src_cnt, const unsigned char *scf, size_t len,
  275. unsigned long flags)
  276. {
  277. struct fsl_re_chan *re_chan;
  278. struct fsl_re_desc *desc;
  279. struct fsl_re_xor_cdb *xor;
  280. struct fsl_re_cmpnd_frame *cf;
  281. u32 cdb;
  282. unsigned int i, j;
  283. unsigned int save_src_cnt = src_cnt;
  284. int cont_q = 0;
  285. re_chan = container_of(chan, struct fsl_re_chan, chan);
  286. if (len > FSL_RE_MAX_DATA_LEN) {
  287. dev_err(re_chan->dev, "genq tx length %lu, max length %d\n",
  288. len, FSL_RE_MAX_DATA_LEN);
  289. return NULL;
  290. }
  291. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  292. if (desc <= 0)
  293. return NULL;
  294. if (scf && (flags & DMA_PREP_CONTINUE)) {
  295. cont_q = 1;
  296. src_cnt += 1;
  297. }
  298. /* Filling xor CDB */
  299. cdb = FSL_RE_XOR_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  300. cdb |= (src_cnt - 1) << FSL_RE_CDB_NRCS_SHIFT;
  301. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  302. cdb |= FSL_RE_INTR_ON_ERROR << FSL_RE_CDB_ERROR_SHIFT;
  303. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  304. xor = desc->cdb_addr;
  305. xor->cdb32 = cdb;
  306. if (scf) {
  307. /* compute q = src0*coef0^src1*coef1^..., * is GF(8) mult */
  308. for (i = 0; i < save_src_cnt; i++)
  309. xor->gfm[i] = scf[i];
  310. if (cont_q)
  311. xor->gfm[i++] = 1;
  312. } else {
  313. /* compute P, that is XOR all srcs */
  314. for (i = 0; i < src_cnt; i++)
  315. xor->gfm[i] = 1;
  316. }
  317. /* Filling frame 0 of compound frame descriptor with CDB */
  318. cf = desc->cf_addr;
  319. fill_cfd_frame(cf, 0, sizeof(*xor), desc->cdb_paddr, 0);
  320. /* Fill CFD's 1st frame with dest buffer */
  321. fill_cfd_frame(cf, 1, len, dest, 0);
  322. /* Fill CFD's rest of the frames with source buffers */
  323. for (i = 2, j = 0; j < save_src_cnt; i++, j++)
  324. fill_cfd_frame(cf, i, len, src[j], 0);
  325. if (cont_q)
  326. fill_cfd_frame(cf, i++, len, dest, 0);
  327. /* Setting the final bit in the last source buffer frame in CFD */
  328. cf[i - 1].efrl32 |= 1 << FSL_RE_CF_FINAL_SHIFT;
  329. return &desc->async_tx;
  330. }
  331. /*
  332. * Prep function for P parity calculation.In RAID Engine terminology,
  333. * XOR calculation is called GenQ calculation done through GenQ command
  334. */
  335. static struct dma_async_tx_descriptor *fsl_re_prep_dma_xor(
  336. struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  337. unsigned int src_cnt, size_t len, unsigned long flags)
  338. {
  339. /* NULL let genq take all coef as 1 */
  340. return fsl_re_prep_dma_genq(chan, dest, src, src_cnt, NULL, len, flags);
  341. }
  342. /*
  343. * Prep function for P/Q parity calculation.In RAID Engine terminology,
  344. * P/Q calculation is called GenQQ done through GenQQ command
  345. */
  346. static struct dma_async_tx_descriptor *fsl_re_prep_dma_pq(
  347. struct dma_chan *chan, dma_addr_t *dest, dma_addr_t *src,
  348. unsigned int src_cnt, const unsigned char *scf, size_t len,
  349. unsigned long flags)
  350. {
  351. struct fsl_re_chan *re_chan;
  352. struct fsl_re_desc *desc;
  353. struct fsl_re_pq_cdb *pq;
  354. struct fsl_re_cmpnd_frame *cf;
  355. u32 cdb;
  356. u8 *p;
  357. int gfmq_len, i, j;
  358. unsigned int save_src_cnt = src_cnt;
  359. re_chan = container_of(chan, struct fsl_re_chan, chan);
  360. if (len > FSL_RE_MAX_DATA_LEN) {
  361. dev_err(re_chan->dev, "pq tx length is %lu, max length is %d\n",
  362. len, FSL_RE_MAX_DATA_LEN);
  363. return NULL;
  364. }
  365. /*
  366. * RE requires at least 2 sources, if given only one source, we pass the
  367. * second source same as the first one.
  368. * With only one source, generating P is meaningless, only generate Q.
  369. */
  370. if (src_cnt == 1) {
  371. struct dma_async_tx_descriptor *tx;
  372. dma_addr_t dma_src[2];
  373. unsigned char coef[2];
  374. dma_src[0] = *src;
  375. coef[0] = *scf;
  376. dma_src[1] = *src;
  377. coef[1] = 0;
  378. tx = fsl_re_prep_dma_genq(chan, dest[1], dma_src, 2, coef, len,
  379. flags);
  380. if (tx)
  381. desc = to_fsl_re_dma_desc(tx);
  382. return tx;
  383. }
  384. /*
  385. * During RAID6 array creation, Linux's MD layer gets P and Q
  386. * calculated separately in two steps. But our RAID Engine has
  387. * the capability to calculate both P and Q with a single command
  388. * Hence to merge well with MD layer, we need to provide a hook
  389. * here and call re_jq_prep_dma_genq() function
  390. */
  391. if (flags & DMA_PREP_PQ_DISABLE_P)
  392. return fsl_re_prep_dma_genq(chan, dest[1], src, src_cnt,
  393. scf, len, flags);
  394. if (flags & DMA_PREP_CONTINUE)
  395. src_cnt += 3;
  396. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  397. if (desc <= 0)
  398. return NULL;
  399. /* Filling GenQQ CDB */
  400. cdb = FSL_RE_PQ_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  401. cdb |= (src_cnt - 1) << FSL_RE_CDB_NRCS_SHIFT;
  402. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  403. cdb |= FSL_RE_BUFFER_OUTPUT << FSL_RE_CDB_BUFFER_SHIFT;
  404. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  405. pq = desc->cdb_addr;
  406. pq->cdb32 = cdb;
  407. p = pq->gfm_q1;
  408. /* Init gfm_q1[] */
  409. for (i = 0; i < src_cnt; i++)
  410. p[i] = 1;
  411. /* Align gfm[] to 32bit */
  412. gfmq_len = ALIGN(src_cnt, 4);
  413. /* Init gfm_q2[] */
  414. p += gfmq_len;
  415. for (i = 0; i < src_cnt; i++)
  416. p[i] = scf[i];
  417. /* Filling frame 0 of compound frame descriptor with CDB */
  418. cf = desc->cf_addr;
  419. fill_cfd_frame(cf, 0, sizeof(struct fsl_re_pq_cdb), desc->cdb_paddr, 0);
  420. /* Fill CFD's 1st & 2nd frame with dest buffers */
  421. for (i = 1, j = 0; i < 3; i++, j++)
  422. fill_cfd_frame(cf, i, len, dest[j], 0);
  423. /* Fill CFD's rest of the frames with source buffers */
  424. for (i = 3, j = 0; j < save_src_cnt; i++, j++)
  425. fill_cfd_frame(cf, i, len, src[j], 0);
  426. /* PQ computation continuation */
  427. if (flags & DMA_PREP_CONTINUE) {
  428. if (src_cnt - save_src_cnt == 3) {
  429. p[save_src_cnt] = 0;
  430. p[save_src_cnt + 1] = 0;
  431. p[save_src_cnt + 2] = 1;
  432. fill_cfd_frame(cf, i++, len, dest[0], 0);
  433. fill_cfd_frame(cf, i++, len, dest[1], 0);
  434. fill_cfd_frame(cf, i++, len, dest[1], 0);
  435. } else {
  436. dev_err(re_chan->dev, "PQ tx continuation error!\n");
  437. return NULL;
  438. }
  439. }
  440. /* Setting the final bit in the last source buffer frame in CFD */
  441. cf[i - 1].efrl32 |= 1 << FSL_RE_CF_FINAL_SHIFT;
  442. return &desc->async_tx;
  443. }
  444. /*
  445. * Prep function for memcpy. In RAID Engine, memcpy is done through MOVE
  446. * command. Logic of this function will need to be modified once multipage
  447. * support is added in Linux's MD/ASYNC Layer
  448. */
  449. static struct dma_async_tx_descriptor *fsl_re_prep_dma_memcpy(
  450. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  451. size_t len, unsigned long flags)
  452. {
  453. struct fsl_re_chan *re_chan;
  454. struct fsl_re_desc *desc;
  455. size_t length;
  456. struct fsl_re_cmpnd_frame *cf;
  457. struct fsl_re_move_cdb *move;
  458. u32 cdb;
  459. re_chan = container_of(chan, struct fsl_re_chan, chan);
  460. if (len > FSL_RE_MAX_DATA_LEN) {
  461. dev_err(re_chan->dev, "cp tx length is %lu, max length is %d\n",
  462. len, FSL_RE_MAX_DATA_LEN);
  463. return NULL;
  464. }
  465. desc = fsl_re_chan_alloc_desc(re_chan, flags);
  466. if (desc <= 0)
  467. return NULL;
  468. /* Filling move CDB */
  469. cdb = FSL_RE_MOVE_OPCODE << FSL_RE_CDB_OPCODE_SHIFT;
  470. cdb |= FSL_RE_BLOCK_SIZE << FSL_RE_CDB_BLKSIZE_SHIFT;
  471. cdb |= FSL_RE_INTR_ON_ERROR << FSL_RE_CDB_ERROR_SHIFT;
  472. cdb |= FSL_RE_DATA_DEP << FSL_RE_CDB_DEPEND_SHIFT;
  473. move = desc->cdb_addr;
  474. move->cdb32 = cdb;
  475. /* Filling frame 0 of CFD with move CDB */
  476. cf = desc->cf_addr;
  477. fill_cfd_frame(cf, 0, sizeof(*move), desc->cdb_paddr, 0);
  478. length = min_t(size_t, len, FSL_RE_MAX_DATA_LEN);
  479. /* Fill CFD's 1st frame with dest buffer */
  480. fill_cfd_frame(cf, 1, length, dest, 0);
  481. /* Fill CFD's 2nd frame with src buffer */
  482. fill_cfd_frame(cf, 2, length, src, 1);
  483. return &desc->async_tx;
  484. }
  485. static int fsl_re_alloc_chan_resources(struct dma_chan *chan)
  486. {
  487. struct fsl_re_chan *re_chan;
  488. struct fsl_re_desc *desc;
  489. void *cf;
  490. dma_addr_t paddr;
  491. int i;
  492. re_chan = container_of(chan, struct fsl_re_chan, chan);
  493. for (i = 0; i < FSL_RE_MIN_DESCS; i++) {
  494. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  495. if (!desc)
  496. break;
  497. cf = dma_pool_alloc(re_chan->re_dev->cf_desc_pool, GFP_KERNEL,
  498. &paddr);
  499. if (!cf) {
  500. kfree(desc);
  501. break;
  502. }
  503. INIT_LIST_HEAD(&desc->node);
  504. fsl_re_init_desc(re_chan, desc, cf, paddr);
  505. list_add_tail(&desc->node, &re_chan->free_q);
  506. re_chan->alloc_count++;
  507. }
  508. return re_chan->alloc_count;
  509. }
  510. static void fsl_re_free_chan_resources(struct dma_chan *chan)
  511. {
  512. struct fsl_re_chan *re_chan;
  513. struct fsl_re_desc *desc;
  514. re_chan = container_of(chan, struct fsl_re_chan, chan);
  515. while (re_chan->alloc_count--) {
  516. desc = list_first_entry(&re_chan->free_q,
  517. struct fsl_re_desc,
  518. node);
  519. list_del(&desc->node);
  520. dma_pool_free(re_chan->re_dev->cf_desc_pool, desc->cf_addr,
  521. desc->cf_paddr);
  522. kfree(desc);
  523. }
  524. if (!list_empty(&re_chan->free_q))
  525. dev_err(re_chan->dev, "chan resource cannot be cleaned!\n");
  526. }
  527. static int fsl_re_chan_probe(struct platform_device *ofdev,
  528. struct device_node *np, u8 q, u32 off)
  529. {
  530. struct device *dev, *chandev;
  531. struct fsl_re_drv_private *re_priv;
  532. struct fsl_re_chan *chan;
  533. struct dma_device *dma_dev;
  534. u32 ptr;
  535. u32 status;
  536. int ret = 0, rc;
  537. struct platform_device *chan_ofdev;
  538. dev = &ofdev->dev;
  539. re_priv = dev_get_drvdata(dev);
  540. dma_dev = &re_priv->dma_dev;
  541. chan = devm_kzalloc(dev, sizeof(*chan), GFP_KERNEL);
  542. if (!chan)
  543. return -ENOMEM;
  544. /* create platform device for chan node */
  545. chan_ofdev = of_platform_device_create(np, NULL, dev);
  546. if (!chan_ofdev) {
  547. dev_err(dev, "Not able to create ofdev for jr %d\n", q);
  548. ret = -EINVAL;
  549. goto err_free;
  550. }
  551. /* read reg property from dts */
  552. rc = of_property_read_u32(np, "reg", &ptr);
  553. if (rc) {
  554. dev_err(dev, "Reg property not found in jr %d\n", q);
  555. ret = -ENODEV;
  556. goto err_free;
  557. }
  558. chan->jrregs = (struct fsl_re_chan_cfg *)((u8 *)re_priv->re_regs +
  559. off + ptr);
  560. /* read irq property from dts */
  561. chan->irq = irq_of_parse_and_map(np, 0);
  562. if (chan->irq == NO_IRQ) {
  563. dev_err(dev, "No IRQ defined for JR %d\n", q);
  564. ret = -ENODEV;
  565. goto err_free;
  566. }
  567. snprintf(chan->name, sizeof(chan->name), "re_jr%02d", q);
  568. chandev = &chan_ofdev->dev;
  569. tasklet_init(&chan->irqtask, fsl_re_dequeue, (unsigned long)chandev);
  570. ret = request_irq(chan->irq, fsl_re_isr, 0, chan->name, chandev);
  571. if (ret) {
  572. dev_err(dev, "Unable to register interrupt for JR %d\n", q);
  573. ret = -EINVAL;
  574. goto err_free;
  575. }
  576. re_priv->re_jrs[q] = chan;
  577. chan->chan.device = dma_dev;
  578. chan->chan.private = chan;
  579. chan->dev = chandev;
  580. chan->re_dev = re_priv;
  581. spin_lock_init(&chan->desc_lock);
  582. INIT_LIST_HEAD(&chan->ack_q);
  583. INIT_LIST_HEAD(&chan->active_q);
  584. INIT_LIST_HEAD(&chan->submit_q);
  585. INIT_LIST_HEAD(&chan->free_q);
  586. chan->inb_ring_virt_addr = dma_pool_alloc(chan->re_dev->hw_desc_pool,
  587. GFP_KERNEL, &chan->inb_phys_addr);
  588. if (!chan->inb_ring_virt_addr) {
  589. dev_err(dev, "No dma memory for inb_ring_virt_addr\n");
  590. ret = -ENOMEM;
  591. goto err_free;
  592. }
  593. chan->oub_ring_virt_addr = dma_pool_alloc(chan->re_dev->hw_desc_pool,
  594. GFP_KERNEL, &chan->oub_phys_addr);
  595. if (!chan->oub_ring_virt_addr) {
  596. dev_err(dev, "No dma memory for oub_ring_virt_addr\n");
  597. ret = -ENOMEM;
  598. goto err_free_1;
  599. }
  600. /* Program the Inbound/Outbound ring base addresses and size */
  601. out_be32(&chan->jrregs->inbring_base_h,
  602. chan->inb_phys_addr & FSL_RE_ADDR_BIT_MASK);
  603. out_be32(&chan->jrregs->oubring_base_h,
  604. chan->oub_phys_addr & FSL_RE_ADDR_BIT_MASK);
  605. out_be32(&chan->jrregs->inbring_base_l,
  606. chan->inb_phys_addr >> FSL_RE_ADDR_BIT_SHIFT);
  607. out_be32(&chan->jrregs->oubring_base_l,
  608. chan->oub_phys_addr >> FSL_RE_ADDR_BIT_SHIFT);
  609. out_be32(&chan->jrregs->inbring_size,
  610. FSL_RE_RING_SIZE << FSL_RE_RING_SIZE_SHIFT);
  611. out_be32(&chan->jrregs->oubring_size,
  612. FSL_RE_RING_SIZE << FSL_RE_RING_SIZE_SHIFT);
  613. /* Read LIODN value from u-boot */
  614. status = in_be32(&chan->jrregs->jr_config_1) & FSL_RE_REG_LIODN_MASK;
  615. /* Program the CFG reg */
  616. out_be32(&chan->jrregs->jr_config_1,
  617. FSL_RE_CFG1_CBSI | FSL_RE_CFG1_CBS0 | status);
  618. dev_set_drvdata(chandev, chan);
  619. /* Enable RE/CHAN */
  620. out_be32(&chan->jrregs->jr_command, FSL_RE_ENABLE);
  621. return 0;
  622. err_free_1:
  623. dma_pool_free(chan->re_dev->hw_desc_pool, chan->inb_ring_virt_addr,
  624. chan->inb_phys_addr);
  625. err_free:
  626. return ret;
  627. }
  628. /* Probe function for RAID Engine */
  629. static int fsl_re_probe(struct platform_device *ofdev)
  630. {
  631. struct fsl_re_drv_private *re_priv;
  632. struct device_node *np;
  633. struct device_node *child;
  634. u32 off;
  635. u8 ridx = 0;
  636. struct dma_device *dma_dev;
  637. struct resource *res;
  638. int rc;
  639. struct device *dev = &ofdev->dev;
  640. re_priv = devm_kzalloc(dev, sizeof(*re_priv), GFP_KERNEL);
  641. if (!re_priv)
  642. return -ENOMEM;
  643. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  644. if (!res)
  645. return -ENODEV;
  646. /* IOMAP the entire RAID Engine region */
  647. re_priv->re_regs = devm_ioremap(dev, res->start, resource_size(res));
  648. if (!re_priv->re_regs)
  649. return -EBUSY;
  650. /* Program the RE mode */
  651. out_be32(&re_priv->re_regs->global_config, FSL_RE_NON_DPAA_MODE);
  652. /* Program Galois Field polynomial */
  653. out_be32(&re_priv->re_regs->galois_field_config, FSL_RE_GFM_POLY);
  654. dev_info(dev, "version %x, mode %x, gfp %x\n",
  655. in_be32(&re_priv->re_regs->re_version_id),
  656. in_be32(&re_priv->re_regs->global_config),
  657. in_be32(&re_priv->re_regs->galois_field_config));
  658. dma_dev = &re_priv->dma_dev;
  659. dma_dev->dev = dev;
  660. INIT_LIST_HEAD(&dma_dev->channels);
  661. dma_set_mask(dev, DMA_BIT_MASK(40));
  662. dma_dev->device_alloc_chan_resources = fsl_re_alloc_chan_resources;
  663. dma_dev->device_tx_status = fsl_re_tx_status;
  664. dma_dev->device_issue_pending = fsl_re_issue_pending;
  665. dma_dev->max_xor = FSL_RE_MAX_XOR_SRCS;
  666. dma_dev->device_prep_dma_xor = fsl_re_prep_dma_xor;
  667. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  668. dma_dev->max_pq = FSL_RE_MAX_PQ_SRCS;
  669. dma_dev->device_prep_dma_pq = fsl_re_prep_dma_pq;
  670. dma_cap_set(DMA_PQ, dma_dev->cap_mask);
  671. dma_dev->device_prep_dma_memcpy = fsl_re_prep_dma_memcpy;
  672. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  673. dma_dev->device_free_chan_resources = fsl_re_free_chan_resources;
  674. re_priv->total_chans = 0;
  675. re_priv->cf_desc_pool = dmam_pool_create("fsl_re_cf_desc_pool", dev,
  676. FSL_RE_CF_CDB_SIZE,
  677. FSL_RE_CF_CDB_ALIGN, 0);
  678. if (!re_priv->cf_desc_pool) {
  679. dev_err(dev, "No memory for fsl re_cf desc pool\n");
  680. return -ENOMEM;
  681. }
  682. re_priv->hw_desc_pool = dmam_pool_create("fsl_re_hw_desc_pool", dev,
  683. sizeof(struct fsl_re_hw_desc) * FSL_RE_RING_SIZE,
  684. FSL_RE_FRAME_ALIGN, 0);
  685. if (!re_priv->hw_desc_pool) {
  686. dev_err(dev, "No memory for fsl re_hw desc pool\n");
  687. return -ENOMEM;
  688. }
  689. dev_set_drvdata(dev, re_priv);
  690. /* Parse Device tree to find out the total number of JQs present */
  691. for_each_compatible_node(np, NULL, "fsl,raideng-v1.0-job-queue") {
  692. rc = of_property_read_u32(np, "reg", &off);
  693. if (rc) {
  694. dev_err(dev, "Reg property not found in JQ node\n");
  695. return -ENODEV;
  696. }
  697. /* Find out the Job Rings present under each JQ */
  698. for_each_child_of_node(np, child) {
  699. rc = of_device_is_compatible(child,
  700. "fsl,raideng-v1.0-job-ring");
  701. if (rc) {
  702. fsl_re_chan_probe(ofdev, child, ridx++, off);
  703. re_priv->total_chans++;
  704. }
  705. }
  706. }
  707. dma_async_device_register(dma_dev);
  708. return 0;
  709. }
  710. static void fsl_re_remove_chan(struct fsl_re_chan *chan)
  711. {
  712. dma_pool_free(chan->re_dev->hw_desc_pool, chan->inb_ring_virt_addr,
  713. chan->inb_phys_addr);
  714. dma_pool_free(chan->re_dev->hw_desc_pool, chan->oub_ring_virt_addr,
  715. chan->oub_phys_addr);
  716. }
  717. static int fsl_re_remove(struct platform_device *ofdev)
  718. {
  719. struct fsl_re_drv_private *re_priv;
  720. struct device *dev;
  721. int i;
  722. dev = &ofdev->dev;
  723. re_priv = dev_get_drvdata(dev);
  724. /* Cleanup chan related memory areas */
  725. for (i = 0; i < re_priv->total_chans; i++)
  726. fsl_re_remove_chan(re_priv->re_jrs[i]);
  727. /* Unregister the driver */
  728. dma_async_device_unregister(&re_priv->dma_dev);
  729. return 0;
  730. }
  731. static struct of_device_id fsl_re_ids[] = {
  732. { .compatible = "fsl,raideng-v1.0", },
  733. {}
  734. };
  735. static struct platform_driver fsl_re_driver = {
  736. .driver = {
  737. .name = "fsl-raideng",
  738. .owner = THIS_MODULE,
  739. .of_match_table = fsl_re_ids,
  740. },
  741. .probe = fsl_re_probe,
  742. .remove = fsl_re_remove,
  743. };
  744. module_platform_driver(fsl_re_driver);
  745. MODULE_AUTHOR("Harninder Rai <harninder.rai@freescale.com>");
  746. MODULE_LICENSE("GPL v2");
  747. MODULE_DESCRIPTION("Freescale RAID Engine Device Driver");