iop-adma.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*
  2. * offload engine driver for the Intel Xscale series of i/o processors
  3. * Copyright © 2006, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. /*
  16. * This driver supports the asynchrounous DMA copy and RAID engines available
  17. * on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/memory.h>
  27. #include <linux/ioport.h>
  28. #include <linux/raid/pq.h>
  29. #include <linux/slab.h>
  30. #include <mach/adma.h>
  31. #include "dmaengine.h"
  32. #define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
  33. #define to_iop_adma_device(dev) \
  34. container_of(dev, struct iop_adma_device, common)
  35. #define tx_to_iop_adma_slot(tx) \
  36. container_of(tx, struct iop_adma_desc_slot, async_tx)
  37. /**
  38. * iop_adma_free_slots - flags descriptor slots for reuse
  39. * @slot: Slot to free
  40. * Caller must hold &iop_chan->lock while calling this function
  41. */
  42. static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
  43. {
  44. int stride = slot->slots_per_op;
  45. while (stride--) {
  46. slot->slots_per_op = 0;
  47. slot = list_entry(slot->slot_node.next,
  48. struct iop_adma_desc_slot,
  49. slot_node);
  50. }
  51. }
  52. static dma_cookie_t
  53. iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
  54. struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
  55. {
  56. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  57. BUG_ON(tx->cookie < 0);
  58. if (tx->cookie > 0) {
  59. cookie = tx->cookie;
  60. tx->cookie = 0;
  61. /* call the callback (must not sleep or submit new
  62. * operations to this channel)
  63. */
  64. if (tx->callback)
  65. tx->callback(tx->callback_param);
  66. dma_descriptor_unmap(tx);
  67. if (desc->group_head)
  68. desc->group_head = NULL;
  69. }
  70. /* run dependent operations */
  71. dma_run_dependencies(tx);
  72. return cookie;
  73. }
  74. static int
  75. iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
  76. struct iop_adma_chan *iop_chan)
  77. {
  78. /* the client is allowed to attach dependent operations
  79. * until 'ack' is set
  80. */
  81. if (!async_tx_test_ack(&desc->async_tx))
  82. return 0;
  83. /* leave the last descriptor in the chain
  84. * so we can append to it
  85. */
  86. if (desc->chain_node.next == &iop_chan->chain)
  87. return 1;
  88. dev_dbg(iop_chan->device->common.dev,
  89. "\tfree slot: %d slots_per_op: %d\n",
  90. desc->idx, desc->slots_per_op);
  91. list_del(&desc->chain_node);
  92. iop_adma_free_slots(desc);
  93. return 0;
  94. }
  95. static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  96. {
  97. struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
  98. dma_cookie_t cookie = 0;
  99. u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
  100. int busy = iop_chan_is_busy(iop_chan);
  101. int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
  102. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  103. /* free completed slots from the chain starting with
  104. * the oldest descriptor
  105. */
  106. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  107. chain_node) {
  108. pr_debug("\tcookie: %d slot: %d busy: %d "
  109. "this_desc: %#x next_desc: %#x ack: %d\n",
  110. iter->async_tx.cookie, iter->idx, busy,
  111. iter->async_tx.phys, iop_desc_get_next_desc(iter),
  112. async_tx_test_ack(&iter->async_tx));
  113. prefetch(_iter);
  114. prefetch(&_iter->async_tx);
  115. /* do not advance past the current descriptor loaded into the
  116. * hardware channel, subsequent descriptors are either in
  117. * process or have not been submitted
  118. */
  119. if (seen_current)
  120. break;
  121. /* stop the search if we reach the current descriptor and the
  122. * channel is busy, or if it appears that the current descriptor
  123. * needs to be re-read (i.e. has been appended to)
  124. */
  125. if (iter->async_tx.phys == current_desc) {
  126. BUG_ON(seen_current++);
  127. if (busy || iop_desc_get_next_desc(iter))
  128. break;
  129. }
  130. /* detect the start of a group transaction */
  131. if (!slot_cnt && !slots_per_op) {
  132. slot_cnt = iter->slot_cnt;
  133. slots_per_op = iter->slots_per_op;
  134. if (slot_cnt <= slots_per_op) {
  135. slot_cnt = 0;
  136. slots_per_op = 0;
  137. }
  138. }
  139. if (slot_cnt) {
  140. pr_debug("\tgroup++\n");
  141. if (!grp_start)
  142. grp_start = iter;
  143. slot_cnt -= slots_per_op;
  144. }
  145. /* all the members of a group are complete */
  146. if (slots_per_op != 0 && slot_cnt == 0) {
  147. struct iop_adma_desc_slot *grp_iter, *_grp_iter;
  148. int end_of_chain = 0;
  149. pr_debug("\tgroup end\n");
  150. /* collect the total results */
  151. if (grp_start->xor_check_result) {
  152. u32 zero_sum_result = 0;
  153. slot_cnt = grp_start->slot_cnt;
  154. grp_iter = grp_start;
  155. list_for_each_entry_from(grp_iter,
  156. &iop_chan->chain, chain_node) {
  157. zero_sum_result |=
  158. iop_desc_get_zero_result(grp_iter);
  159. pr_debug("\titer%d result: %d\n",
  160. grp_iter->idx, zero_sum_result);
  161. slot_cnt -= slots_per_op;
  162. if (slot_cnt == 0)
  163. break;
  164. }
  165. pr_debug("\tgrp_start->xor_check_result: %p\n",
  166. grp_start->xor_check_result);
  167. *grp_start->xor_check_result = zero_sum_result;
  168. }
  169. /* clean up the group */
  170. slot_cnt = grp_start->slot_cnt;
  171. grp_iter = grp_start;
  172. list_for_each_entry_safe_from(grp_iter, _grp_iter,
  173. &iop_chan->chain, chain_node) {
  174. cookie = iop_adma_run_tx_complete_actions(
  175. grp_iter, iop_chan, cookie);
  176. slot_cnt -= slots_per_op;
  177. end_of_chain = iop_adma_clean_slot(grp_iter,
  178. iop_chan);
  179. if (slot_cnt == 0 || end_of_chain)
  180. break;
  181. }
  182. /* the group should be complete at this point */
  183. BUG_ON(slot_cnt);
  184. slots_per_op = 0;
  185. grp_start = NULL;
  186. if (end_of_chain)
  187. break;
  188. else
  189. continue;
  190. } else if (slots_per_op) /* wait for group completion */
  191. continue;
  192. /* write back zero sum results (single descriptor case) */
  193. if (iter->xor_check_result && iter->async_tx.cookie)
  194. *iter->xor_check_result =
  195. iop_desc_get_zero_result(iter);
  196. cookie = iop_adma_run_tx_complete_actions(
  197. iter, iop_chan, cookie);
  198. if (iop_adma_clean_slot(iter, iop_chan))
  199. break;
  200. }
  201. if (cookie > 0) {
  202. iop_chan->common.completed_cookie = cookie;
  203. pr_debug("\tcompleted cookie %d\n", cookie);
  204. }
  205. }
  206. static void
  207. iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  208. {
  209. spin_lock_bh(&iop_chan->lock);
  210. __iop_adma_slot_cleanup(iop_chan);
  211. spin_unlock_bh(&iop_chan->lock);
  212. }
  213. static void iop_adma_tasklet(unsigned long data)
  214. {
  215. struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
  216. /* lockdep will flag depedency submissions as potentially
  217. * recursive locking, this is not the case as a dependency
  218. * submission will never recurse a channels submit routine.
  219. * There are checks in async_tx.c to prevent this.
  220. */
  221. spin_lock_nested(&iop_chan->lock, SINGLE_DEPTH_NESTING);
  222. __iop_adma_slot_cleanup(iop_chan);
  223. spin_unlock(&iop_chan->lock);
  224. }
  225. static struct iop_adma_desc_slot *
  226. iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
  227. int slots_per_op)
  228. {
  229. struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
  230. LIST_HEAD(chain);
  231. int slots_found, retry = 0;
  232. /* start search from the last allocated descrtiptor
  233. * if a contiguous allocation can not be found start searching
  234. * from the beginning of the list
  235. */
  236. retry:
  237. slots_found = 0;
  238. if (retry == 0)
  239. iter = iop_chan->last_used;
  240. else
  241. iter = list_entry(&iop_chan->all_slots,
  242. struct iop_adma_desc_slot,
  243. slot_node);
  244. list_for_each_entry_safe_continue(
  245. iter, _iter, &iop_chan->all_slots, slot_node) {
  246. prefetch(_iter);
  247. prefetch(&_iter->async_tx);
  248. if (iter->slots_per_op) {
  249. /* give up after finding the first busy slot
  250. * on the second pass through the list
  251. */
  252. if (retry)
  253. break;
  254. slots_found = 0;
  255. continue;
  256. }
  257. /* start the allocation if the slot is correctly aligned */
  258. if (!slots_found++) {
  259. if (iop_desc_is_aligned(iter, slots_per_op))
  260. alloc_start = iter;
  261. else {
  262. slots_found = 0;
  263. continue;
  264. }
  265. }
  266. if (slots_found == num_slots) {
  267. struct iop_adma_desc_slot *alloc_tail = NULL;
  268. struct iop_adma_desc_slot *last_used = NULL;
  269. iter = alloc_start;
  270. while (num_slots) {
  271. int i;
  272. dev_dbg(iop_chan->device->common.dev,
  273. "allocated slot: %d "
  274. "(desc %p phys: %#x) slots_per_op %d\n",
  275. iter->idx, iter->hw_desc,
  276. iter->async_tx.phys, slots_per_op);
  277. /* pre-ack all but the last descriptor */
  278. if (num_slots != slots_per_op)
  279. async_tx_ack(&iter->async_tx);
  280. list_add_tail(&iter->chain_node, &chain);
  281. alloc_tail = iter;
  282. iter->async_tx.cookie = 0;
  283. iter->slot_cnt = num_slots;
  284. iter->xor_check_result = NULL;
  285. for (i = 0; i < slots_per_op; i++) {
  286. iter->slots_per_op = slots_per_op - i;
  287. last_used = iter;
  288. iter = list_entry(iter->slot_node.next,
  289. struct iop_adma_desc_slot,
  290. slot_node);
  291. }
  292. num_slots -= slots_per_op;
  293. }
  294. alloc_tail->group_head = alloc_start;
  295. alloc_tail->async_tx.cookie = -EBUSY;
  296. list_splice(&chain, &alloc_tail->tx_list);
  297. iop_chan->last_used = last_used;
  298. iop_desc_clear_next_desc(alloc_start);
  299. iop_desc_clear_next_desc(alloc_tail);
  300. return alloc_tail;
  301. }
  302. }
  303. if (!retry++)
  304. goto retry;
  305. /* perform direct reclaim if the allocation fails */
  306. __iop_adma_slot_cleanup(iop_chan);
  307. return NULL;
  308. }
  309. static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
  310. {
  311. dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
  312. iop_chan->pending);
  313. if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
  314. iop_chan->pending = 0;
  315. iop_chan_append(iop_chan);
  316. }
  317. }
  318. static dma_cookie_t
  319. iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
  320. {
  321. struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
  322. struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
  323. struct iop_adma_desc_slot *grp_start, *old_chain_tail;
  324. int slot_cnt;
  325. int slots_per_op;
  326. dma_cookie_t cookie;
  327. dma_addr_t next_dma;
  328. grp_start = sw_desc->group_head;
  329. slot_cnt = grp_start->slot_cnt;
  330. slots_per_op = grp_start->slots_per_op;
  331. spin_lock_bh(&iop_chan->lock);
  332. cookie = dma_cookie_assign(tx);
  333. old_chain_tail = list_entry(iop_chan->chain.prev,
  334. struct iop_adma_desc_slot, chain_node);
  335. list_splice_init(&sw_desc->tx_list,
  336. &old_chain_tail->chain_node);
  337. /* fix up the hardware chain */
  338. next_dma = grp_start->async_tx.phys;
  339. iop_desc_set_next_desc(old_chain_tail, next_dma);
  340. BUG_ON(iop_desc_get_next_desc(old_chain_tail) != next_dma); /* flush */
  341. /* check for pre-chained descriptors */
  342. iop_paranoia(iop_desc_get_next_desc(sw_desc));
  343. /* increment the pending count by the number of slots
  344. * memcpy operations have a 1:1 (slot:operation) relation
  345. * other operations are heavier and will pop the threshold
  346. * more often.
  347. */
  348. iop_chan->pending += slot_cnt;
  349. iop_adma_check_threshold(iop_chan);
  350. spin_unlock_bh(&iop_chan->lock);
  351. dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
  352. __func__, sw_desc->async_tx.cookie, sw_desc->idx);
  353. return cookie;
  354. }
  355. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
  356. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
  357. /**
  358. * iop_adma_alloc_chan_resources - returns the number of allocated descriptors
  359. * @chan - allocate descriptor resources for this channel
  360. * @client - current client requesting the channel be ready for requests
  361. *
  362. * Note: We keep the slots for 1 operation on iop_chan->chain at all times. To
  363. * avoid deadlock, via async_xor, num_descs_in_pool must at a minimum be
  364. * greater than 2x the number slots needed to satisfy a device->max_xor
  365. * request.
  366. * */
  367. static int iop_adma_alloc_chan_resources(struct dma_chan *chan)
  368. {
  369. char *hw_desc;
  370. int idx;
  371. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  372. struct iop_adma_desc_slot *slot = NULL;
  373. int init = iop_chan->slots_allocated ? 0 : 1;
  374. struct iop_adma_platform_data *plat_data =
  375. dev_get_platdata(&iop_chan->device->pdev->dev);
  376. int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
  377. /* Allocate descriptor slots */
  378. do {
  379. idx = iop_chan->slots_allocated;
  380. if (idx == num_descs_in_pool)
  381. break;
  382. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  383. if (!slot) {
  384. printk(KERN_INFO "IOP ADMA Channel only initialized"
  385. " %d descriptor slots", idx);
  386. break;
  387. }
  388. hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
  389. slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
  390. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  391. slot->async_tx.tx_submit = iop_adma_tx_submit;
  392. INIT_LIST_HEAD(&slot->tx_list);
  393. INIT_LIST_HEAD(&slot->chain_node);
  394. INIT_LIST_HEAD(&slot->slot_node);
  395. hw_desc = (char *) iop_chan->device->dma_desc_pool;
  396. slot->async_tx.phys =
  397. (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
  398. slot->idx = idx;
  399. spin_lock_bh(&iop_chan->lock);
  400. iop_chan->slots_allocated++;
  401. list_add_tail(&slot->slot_node, &iop_chan->all_slots);
  402. spin_unlock_bh(&iop_chan->lock);
  403. } while (iop_chan->slots_allocated < num_descs_in_pool);
  404. if (idx && !iop_chan->last_used)
  405. iop_chan->last_used = list_entry(iop_chan->all_slots.next,
  406. struct iop_adma_desc_slot,
  407. slot_node);
  408. dev_dbg(iop_chan->device->common.dev,
  409. "allocated %d descriptor slots last_used: %p\n",
  410. iop_chan->slots_allocated, iop_chan->last_used);
  411. /* initialize the channel and the chain with a null operation */
  412. if (init) {
  413. if (dma_has_cap(DMA_MEMCPY,
  414. iop_chan->device->common.cap_mask))
  415. iop_chan_start_null_memcpy(iop_chan);
  416. else if (dma_has_cap(DMA_XOR,
  417. iop_chan->device->common.cap_mask))
  418. iop_chan_start_null_xor(iop_chan);
  419. else
  420. BUG();
  421. }
  422. return (idx > 0) ? idx : -ENOMEM;
  423. }
  424. static struct dma_async_tx_descriptor *
  425. iop_adma_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
  426. {
  427. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  428. struct iop_adma_desc_slot *sw_desc, *grp_start;
  429. int slot_cnt, slots_per_op;
  430. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  431. spin_lock_bh(&iop_chan->lock);
  432. slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
  433. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  434. if (sw_desc) {
  435. grp_start = sw_desc->group_head;
  436. iop_desc_init_interrupt(grp_start, iop_chan);
  437. sw_desc->async_tx.flags = flags;
  438. }
  439. spin_unlock_bh(&iop_chan->lock);
  440. return sw_desc ? &sw_desc->async_tx : NULL;
  441. }
  442. static struct dma_async_tx_descriptor *
  443. iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  444. dma_addr_t dma_src, size_t len, unsigned long flags)
  445. {
  446. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  447. struct iop_adma_desc_slot *sw_desc, *grp_start;
  448. int slot_cnt, slots_per_op;
  449. if (unlikely(!len))
  450. return NULL;
  451. BUG_ON(len > IOP_ADMA_MAX_BYTE_COUNT);
  452. dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
  453. __func__, len);
  454. spin_lock_bh(&iop_chan->lock);
  455. slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
  456. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  457. if (sw_desc) {
  458. grp_start = sw_desc->group_head;
  459. iop_desc_init_memcpy(grp_start, flags);
  460. iop_desc_set_byte_count(grp_start, iop_chan, len);
  461. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  462. iop_desc_set_memcpy_src_addr(grp_start, dma_src);
  463. sw_desc->async_tx.flags = flags;
  464. }
  465. spin_unlock_bh(&iop_chan->lock);
  466. return sw_desc ? &sw_desc->async_tx : NULL;
  467. }
  468. static struct dma_async_tx_descriptor *
  469. iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
  470. dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
  471. unsigned long flags)
  472. {
  473. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  474. struct iop_adma_desc_slot *sw_desc, *grp_start;
  475. int slot_cnt, slots_per_op;
  476. if (unlikely(!len))
  477. return NULL;
  478. BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
  479. dev_dbg(iop_chan->device->common.dev,
  480. "%s src_cnt: %d len: %u flags: %lx\n",
  481. __func__, src_cnt, len, flags);
  482. spin_lock_bh(&iop_chan->lock);
  483. slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
  484. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  485. if (sw_desc) {
  486. grp_start = sw_desc->group_head;
  487. iop_desc_init_xor(grp_start, src_cnt, flags);
  488. iop_desc_set_byte_count(grp_start, iop_chan, len);
  489. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  490. sw_desc->async_tx.flags = flags;
  491. while (src_cnt--)
  492. iop_desc_set_xor_src_addr(grp_start, src_cnt,
  493. dma_src[src_cnt]);
  494. }
  495. spin_unlock_bh(&iop_chan->lock);
  496. return sw_desc ? &sw_desc->async_tx : NULL;
  497. }
  498. static struct dma_async_tx_descriptor *
  499. iop_adma_prep_dma_xor_val(struct dma_chan *chan, dma_addr_t *dma_src,
  500. unsigned int src_cnt, size_t len, u32 *result,
  501. unsigned long flags)
  502. {
  503. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  504. struct iop_adma_desc_slot *sw_desc, *grp_start;
  505. int slot_cnt, slots_per_op;
  506. if (unlikely(!len))
  507. return NULL;
  508. dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
  509. __func__, src_cnt, len);
  510. spin_lock_bh(&iop_chan->lock);
  511. slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
  512. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  513. if (sw_desc) {
  514. grp_start = sw_desc->group_head;
  515. iop_desc_init_zero_sum(grp_start, src_cnt, flags);
  516. iop_desc_set_zero_sum_byte_count(grp_start, len);
  517. grp_start->xor_check_result = result;
  518. pr_debug("\t%s: grp_start->xor_check_result: %p\n",
  519. __func__, grp_start->xor_check_result);
  520. sw_desc->async_tx.flags = flags;
  521. while (src_cnt--)
  522. iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
  523. dma_src[src_cnt]);
  524. }
  525. spin_unlock_bh(&iop_chan->lock);
  526. return sw_desc ? &sw_desc->async_tx : NULL;
  527. }
  528. static struct dma_async_tx_descriptor *
  529. iop_adma_prep_dma_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  530. unsigned int src_cnt, const unsigned char *scf, size_t len,
  531. unsigned long flags)
  532. {
  533. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  534. struct iop_adma_desc_slot *sw_desc, *g;
  535. int slot_cnt, slots_per_op;
  536. int continue_srcs;
  537. if (unlikely(!len))
  538. return NULL;
  539. BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
  540. dev_dbg(iop_chan->device->common.dev,
  541. "%s src_cnt: %d len: %u flags: %lx\n",
  542. __func__, src_cnt, len, flags);
  543. if (dmaf_p_disabled_continue(flags))
  544. continue_srcs = 1+src_cnt;
  545. else if (dmaf_continue(flags))
  546. continue_srcs = 3+src_cnt;
  547. else
  548. continue_srcs = 0+src_cnt;
  549. spin_lock_bh(&iop_chan->lock);
  550. slot_cnt = iop_chan_pq_slot_count(len, continue_srcs, &slots_per_op);
  551. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  552. if (sw_desc) {
  553. int i;
  554. g = sw_desc->group_head;
  555. iop_desc_set_byte_count(g, iop_chan, len);
  556. /* even if P is disabled its destination address (bits
  557. * [3:0]) must match Q. It is ok if P points to an
  558. * invalid address, it won't be written.
  559. */
  560. if (flags & DMA_PREP_PQ_DISABLE_P)
  561. dst[0] = dst[1] & 0x7;
  562. iop_desc_set_pq_addr(g, dst);
  563. sw_desc->async_tx.flags = flags;
  564. for (i = 0; i < src_cnt; i++)
  565. iop_desc_set_pq_src_addr(g, i, src[i], scf[i]);
  566. /* if we are continuing a previous operation factor in
  567. * the old p and q values, see the comment for dma_maxpq
  568. * in include/linux/dmaengine.h
  569. */
  570. if (dmaf_p_disabled_continue(flags))
  571. iop_desc_set_pq_src_addr(g, i++, dst[1], 1);
  572. else if (dmaf_continue(flags)) {
  573. iop_desc_set_pq_src_addr(g, i++, dst[0], 0);
  574. iop_desc_set_pq_src_addr(g, i++, dst[1], 1);
  575. iop_desc_set_pq_src_addr(g, i++, dst[1], 0);
  576. }
  577. iop_desc_init_pq(g, i, flags);
  578. }
  579. spin_unlock_bh(&iop_chan->lock);
  580. return sw_desc ? &sw_desc->async_tx : NULL;
  581. }
  582. static struct dma_async_tx_descriptor *
  583. iop_adma_prep_dma_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  584. unsigned int src_cnt, const unsigned char *scf,
  585. size_t len, enum sum_check_flags *pqres,
  586. unsigned long flags)
  587. {
  588. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  589. struct iop_adma_desc_slot *sw_desc, *g;
  590. int slot_cnt, slots_per_op;
  591. if (unlikely(!len))
  592. return NULL;
  593. BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
  594. dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
  595. __func__, src_cnt, len);
  596. spin_lock_bh(&iop_chan->lock);
  597. slot_cnt = iop_chan_pq_zero_sum_slot_count(len, src_cnt + 2, &slots_per_op);
  598. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  599. if (sw_desc) {
  600. /* for validate operations p and q are tagged onto the
  601. * end of the source list
  602. */
  603. int pq_idx = src_cnt;
  604. g = sw_desc->group_head;
  605. iop_desc_init_pq_zero_sum(g, src_cnt+2, flags);
  606. iop_desc_set_pq_zero_sum_byte_count(g, len);
  607. g->pq_check_result = pqres;
  608. pr_debug("\t%s: g->pq_check_result: %p\n",
  609. __func__, g->pq_check_result);
  610. sw_desc->async_tx.flags = flags;
  611. while (src_cnt--)
  612. iop_desc_set_pq_zero_sum_src_addr(g, src_cnt,
  613. src[src_cnt],
  614. scf[src_cnt]);
  615. iop_desc_set_pq_zero_sum_addr(g, pq_idx, src);
  616. }
  617. spin_unlock_bh(&iop_chan->lock);
  618. return sw_desc ? &sw_desc->async_tx : NULL;
  619. }
  620. static void iop_adma_free_chan_resources(struct dma_chan *chan)
  621. {
  622. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  623. struct iop_adma_desc_slot *iter, *_iter;
  624. int in_use_descs = 0;
  625. iop_adma_slot_cleanup(iop_chan);
  626. spin_lock_bh(&iop_chan->lock);
  627. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  628. chain_node) {
  629. in_use_descs++;
  630. list_del(&iter->chain_node);
  631. }
  632. list_for_each_entry_safe_reverse(
  633. iter, _iter, &iop_chan->all_slots, slot_node) {
  634. list_del(&iter->slot_node);
  635. kfree(iter);
  636. iop_chan->slots_allocated--;
  637. }
  638. iop_chan->last_used = NULL;
  639. dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
  640. __func__, iop_chan->slots_allocated);
  641. spin_unlock_bh(&iop_chan->lock);
  642. /* one is ok since we left it on there on purpose */
  643. if (in_use_descs > 1)
  644. printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
  645. in_use_descs - 1);
  646. }
  647. /**
  648. * iop_adma_status - poll the status of an ADMA transaction
  649. * @chan: ADMA channel handle
  650. * @cookie: ADMA transaction identifier
  651. * @txstate: a holder for the current state of the channel or NULL
  652. */
  653. static enum dma_status iop_adma_status(struct dma_chan *chan,
  654. dma_cookie_t cookie,
  655. struct dma_tx_state *txstate)
  656. {
  657. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  658. int ret;
  659. ret = dma_cookie_status(chan, cookie, txstate);
  660. if (ret == DMA_COMPLETE)
  661. return ret;
  662. iop_adma_slot_cleanup(iop_chan);
  663. return dma_cookie_status(chan, cookie, txstate);
  664. }
  665. static irqreturn_t iop_adma_eot_handler(int irq, void *data)
  666. {
  667. struct iop_adma_chan *chan = data;
  668. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  669. tasklet_schedule(&chan->irq_tasklet);
  670. iop_adma_device_clear_eot_status(chan);
  671. return IRQ_HANDLED;
  672. }
  673. static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
  674. {
  675. struct iop_adma_chan *chan = data;
  676. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  677. tasklet_schedule(&chan->irq_tasklet);
  678. iop_adma_device_clear_eoc_status(chan);
  679. return IRQ_HANDLED;
  680. }
  681. static irqreturn_t iop_adma_err_handler(int irq, void *data)
  682. {
  683. struct iop_adma_chan *chan = data;
  684. unsigned long status = iop_chan_get_status(chan);
  685. dev_err(chan->device->common.dev,
  686. "error ( %s%s%s%s%s%s%s)\n",
  687. iop_is_err_int_parity(status, chan) ? "int_parity " : "",
  688. iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
  689. iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
  690. iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
  691. iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
  692. iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
  693. iop_is_err_split_tx(status, chan) ? "split_tx " : "");
  694. iop_adma_device_clear_err_status(chan);
  695. BUG();
  696. return IRQ_HANDLED;
  697. }
  698. static void iop_adma_issue_pending(struct dma_chan *chan)
  699. {
  700. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  701. if (iop_chan->pending) {
  702. iop_chan->pending = 0;
  703. iop_chan_append(iop_chan);
  704. }
  705. }
  706. /*
  707. * Perform a transaction to verify the HW works.
  708. */
  709. #define IOP_ADMA_TEST_SIZE 2000
  710. static int iop_adma_memcpy_self_test(struct iop_adma_device *device)
  711. {
  712. int i;
  713. void *src, *dest;
  714. dma_addr_t src_dma, dest_dma;
  715. struct dma_chan *dma_chan;
  716. dma_cookie_t cookie;
  717. struct dma_async_tx_descriptor *tx;
  718. int err = 0;
  719. struct iop_adma_chan *iop_chan;
  720. dev_dbg(device->common.dev, "%s\n", __func__);
  721. src = kmalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  722. if (!src)
  723. return -ENOMEM;
  724. dest = kzalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  725. if (!dest) {
  726. kfree(src);
  727. return -ENOMEM;
  728. }
  729. /* Fill in src buffer */
  730. for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
  731. ((u8 *) src)[i] = (u8)i;
  732. /* Start copy, using first DMA channel */
  733. dma_chan = container_of(device->common.channels.next,
  734. struct dma_chan,
  735. device_node);
  736. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  737. err = -ENODEV;
  738. goto out;
  739. }
  740. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  741. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  742. src_dma = dma_map_single(dma_chan->device->dev, src,
  743. IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
  744. tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  745. IOP_ADMA_TEST_SIZE,
  746. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  747. cookie = iop_adma_tx_submit(tx);
  748. iop_adma_issue_pending(dma_chan);
  749. msleep(1);
  750. if (iop_adma_status(dma_chan, cookie, NULL) !=
  751. DMA_COMPLETE) {
  752. dev_err(dma_chan->device->dev,
  753. "Self-test copy timed out, disabling\n");
  754. err = -ENODEV;
  755. goto free_resources;
  756. }
  757. iop_chan = to_iop_adma_chan(dma_chan);
  758. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  759. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  760. if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
  761. dev_err(dma_chan->device->dev,
  762. "Self-test copy failed compare, disabling\n");
  763. err = -ENODEV;
  764. goto free_resources;
  765. }
  766. free_resources:
  767. iop_adma_free_chan_resources(dma_chan);
  768. out:
  769. kfree(src);
  770. kfree(dest);
  771. return err;
  772. }
  773. #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
  774. static int
  775. iop_adma_xor_val_self_test(struct iop_adma_device *device)
  776. {
  777. int i, src_idx;
  778. struct page *dest;
  779. struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
  780. struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  781. dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  782. dma_addr_t dest_dma;
  783. struct dma_async_tx_descriptor *tx;
  784. struct dma_chan *dma_chan;
  785. dma_cookie_t cookie;
  786. u8 cmp_byte = 0;
  787. u32 cmp_word;
  788. u32 zero_sum_result;
  789. int err = 0;
  790. struct iop_adma_chan *iop_chan;
  791. dev_dbg(device->common.dev, "%s\n", __func__);
  792. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  793. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  794. if (!xor_srcs[src_idx]) {
  795. while (src_idx--)
  796. __free_page(xor_srcs[src_idx]);
  797. return -ENOMEM;
  798. }
  799. }
  800. dest = alloc_page(GFP_KERNEL);
  801. if (!dest) {
  802. while (src_idx--)
  803. __free_page(xor_srcs[src_idx]);
  804. return -ENOMEM;
  805. }
  806. /* Fill in src buffers */
  807. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  808. u8 *ptr = page_address(xor_srcs[src_idx]);
  809. for (i = 0; i < PAGE_SIZE; i++)
  810. ptr[i] = (1 << src_idx);
  811. }
  812. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
  813. cmp_byte ^= (u8) (1 << src_idx);
  814. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  815. (cmp_byte << 8) | cmp_byte;
  816. memset(page_address(dest), 0, PAGE_SIZE);
  817. dma_chan = container_of(device->common.channels.next,
  818. struct dma_chan,
  819. device_node);
  820. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  821. err = -ENODEV;
  822. goto out;
  823. }
  824. /* test xor */
  825. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
  826. PAGE_SIZE, DMA_FROM_DEVICE);
  827. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  828. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  829. 0, PAGE_SIZE, DMA_TO_DEVICE);
  830. tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  831. IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
  832. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  833. cookie = iop_adma_tx_submit(tx);
  834. iop_adma_issue_pending(dma_chan);
  835. msleep(8);
  836. if (iop_adma_status(dma_chan, cookie, NULL) !=
  837. DMA_COMPLETE) {
  838. dev_err(dma_chan->device->dev,
  839. "Self-test xor timed out, disabling\n");
  840. err = -ENODEV;
  841. goto free_resources;
  842. }
  843. iop_chan = to_iop_adma_chan(dma_chan);
  844. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  845. PAGE_SIZE, DMA_FROM_DEVICE);
  846. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  847. u32 *ptr = page_address(dest);
  848. if (ptr[i] != cmp_word) {
  849. dev_err(dma_chan->device->dev,
  850. "Self-test xor failed compare, disabling\n");
  851. err = -ENODEV;
  852. goto free_resources;
  853. }
  854. }
  855. dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
  856. PAGE_SIZE, DMA_TO_DEVICE);
  857. /* skip zero sum if the capability is not present */
  858. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  859. goto free_resources;
  860. /* zero sum the sources with the destintation page */
  861. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  862. zero_sum_srcs[i] = xor_srcs[i];
  863. zero_sum_srcs[i] = dest;
  864. zero_sum_result = 1;
  865. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  866. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  867. zero_sum_srcs[i], 0, PAGE_SIZE,
  868. DMA_TO_DEVICE);
  869. tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
  870. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  871. &zero_sum_result,
  872. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  873. cookie = iop_adma_tx_submit(tx);
  874. iop_adma_issue_pending(dma_chan);
  875. msleep(8);
  876. if (iop_adma_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
  877. dev_err(dma_chan->device->dev,
  878. "Self-test zero sum timed out, disabling\n");
  879. err = -ENODEV;
  880. goto free_resources;
  881. }
  882. if (zero_sum_result != 0) {
  883. dev_err(dma_chan->device->dev,
  884. "Self-test zero sum failed compare, disabling\n");
  885. err = -ENODEV;
  886. goto free_resources;
  887. }
  888. /* test for non-zero parity sum */
  889. zero_sum_result = 0;
  890. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  891. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  892. zero_sum_srcs[i], 0, PAGE_SIZE,
  893. DMA_TO_DEVICE);
  894. tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
  895. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  896. &zero_sum_result,
  897. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  898. cookie = iop_adma_tx_submit(tx);
  899. iop_adma_issue_pending(dma_chan);
  900. msleep(8);
  901. if (iop_adma_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
  902. dev_err(dma_chan->device->dev,
  903. "Self-test non-zero sum timed out, disabling\n");
  904. err = -ENODEV;
  905. goto free_resources;
  906. }
  907. if (zero_sum_result != 1) {
  908. dev_err(dma_chan->device->dev,
  909. "Self-test non-zero sum failed compare, disabling\n");
  910. err = -ENODEV;
  911. goto free_resources;
  912. }
  913. free_resources:
  914. iop_adma_free_chan_resources(dma_chan);
  915. out:
  916. src_idx = IOP_ADMA_NUM_SRC_TEST;
  917. while (src_idx--)
  918. __free_page(xor_srcs[src_idx]);
  919. __free_page(dest);
  920. return err;
  921. }
  922. #ifdef CONFIG_RAID6_PQ
  923. static int
  924. iop_adma_pq_zero_sum_self_test(struct iop_adma_device *device)
  925. {
  926. /* combined sources, software pq results, and extra hw pq results */
  927. struct page *pq[IOP_ADMA_NUM_SRC_TEST+2+2];
  928. /* ptr to the extra hw pq buffers defined above */
  929. struct page **pq_hw = &pq[IOP_ADMA_NUM_SRC_TEST+2];
  930. /* address conversion buffers (dma_map / page_address) */
  931. void *pq_sw[IOP_ADMA_NUM_SRC_TEST+2];
  932. dma_addr_t pq_src[IOP_ADMA_NUM_SRC_TEST+2];
  933. dma_addr_t *pq_dest = &pq_src[IOP_ADMA_NUM_SRC_TEST];
  934. int i;
  935. struct dma_async_tx_descriptor *tx;
  936. struct dma_chan *dma_chan;
  937. dma_cookie_t cookie;
  938. u32 zero_sum_result;
  939. int err = 0;
  940. struct device *dev;
  941. dev_dbg(device->common.dev, "%s\n", __func__);
  942. for (i = 0; i < ARRAY_SIZE(pq); i++) {
  943. pq[i] = alloc_page(GFP_KERNEL);
  944. if (!pq[i]) {
  945. while (i--)
  946. __free_page(pq[i]);
  947. return -ENOMEM;
  948. }
  949. }
  950. /* Fill in src buffers */
  951. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++) {
  952. pq_sw[i] = page_address(pq[i]);
  953. memset(pq_sw[i], 0x11111111 * (1<<i), PAGE_SIZE);
  954. }
  955. pq_sw[i] = page_address(pq[i]);
  956. pq_sw[i+1] = page_address(pq[i+1]);
  957. dma_chan = container_of(device->common.channels.next,
  958. struct dma_chan,
  959. device_node);
  960. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  961. err = -ENODEV;
  962. goto out;
  963. }
  964. dev = dma_chan->device->dev;
  965. /* initialize the dests */
  966. memset(page_address(pq_hw[0]), 0 , PAGE_SIZE);
  967. memset(page_address(pq_hw[1]), 0 , PAGE_SIZE);
  968. /* test pq */
  969. pq_dest[0] = dma_map_page(dev, pq_hw[0], 0, PAGE_SIZE, DMA_FROM_DEVICE);
  970. pq_dest[1] = dma_map_page(dev, pq_hw[1], 0, PAGE_SIZE, DMA_FROM_DEVICE);
  971. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  972. pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
  973. DMA_TO_DEVICE);
  974. tx = iop_adma_prep_dma_pq(dma_chan, pq_dest, pq_src,
  975. IOP_ADMA_NUM_SRC_TEST, (u8 *)raid6_gfexp,
  976. PAGE_SIZE,
  977. DMA_PREP_INTERRUPT |
  978. DMA_CTRL_ACK);
  979. cookie = iop_adma_tx_submit(tx);
  980. iop_adma_issue_pending(dma_chan);
  981. msleep(8);
  982. if (iop_adma_status(dma_chan, cookie, NULL) !=
  983. DMA_COMPLETE) {
  984. dev_err(dev, "Self-test pq timed out, disabling\n");
  985. err = -ENODEV;
  986. goto free_resources;
  987. }
  988. raid6_call.gen_syndrome(IOP_ADMA_NUM_SRC_TEST+2, PAGE_SIZE, pq_sw);
  989. if (memcmp(pq_sw[IOP_ADMA_NUM_SRC_TEST],
  990. page_address(pq_hw[0]), PAGE_SIZE) != 0) {
  991. dev_err(dev, "Self-test p failed compare, disabling\n");
  992. err = -ENODEV;
  993. goto free_resources;
  994. }
  995. if (memcmp(pq_sw[IOP_ADMA_NUM_SRC_TEST+1],
  996. page_address(pq_hw[1]), PAGE_SIZE) != 0) {
  997. dev_err(dev, "Self-test q failed compare, disabling\n");
  998. err = -ENODEV;
  999. goto free_resources;
  1000. }
  1001. /* test correct zero sum using the software generated pq values */
  1002. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 2; i++)
  1003. pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
  1004. DMA_TO_DEVICE);
  1005. zero_sum_result = ~0;
  1006. tx = iop_adma_prep_dma_pq_val(dma_chan, &pq_src[IOP_ADMA_NUM_SRC_TEST],
  1007. pq_src, IOP_ADMA_NUM_SRC_TEST,
  1008. raid6_gfexp, PAGE_SIZE, &zero_sum_result,
  1009. DMA_PREP_INTERRUPT|DMA_CTRL_ACK);
  1010. cookie = iop_adma_tx_submit(tx);
  1011. iop_adma_issue_pending(dma_chan);
  1012. msleep(8);
  1013. if (iop_adma_status(dma_chan, cookie, NULL) !=
  1014. DMA_COMPLETE) {
  1015. dev_err(dev, "Self-test pq-zero-sum timed out, disabling\n");
  1016. err = -ENODEV;
  1017. goto free_resources;
  1018. }
  1019. if (zero_sum_result != 0) {
  1020. dev_err(dev, "Self-test pq-zero-sum failed to validate: %x\n",
  1021. zero_sum_result);
  1022. err = -ENODEV;
  1023. goto free_resources;
  1024. }
  1025. /* test incorrect zero sum */
  1026. i = IOP_ADMA_NUM_SRC_TEST;
  1027. memset(pq_sw[i] + 100, 0, 100);
  1028. memset(pq_sw[i+1] + 200, 0, 200);
  1029. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 2; i++)
  1030. pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
  1031. DMA_TO_DEVICE);
  1032. zero_sum_result = 0;
  1033. tx = iop_adma_prep_dma_pq_val(dma_chan, &pq_src[IOP_ADMA_NUM_SRC_TEST],
  1034. pq_src, IOP_ADMA_NUM_SRC_TEST,
  1035. raid6_gfexp, PAGE_SIZE, &zero_sum_result,
  1036. DMA_PREP_INTERRUPT|DMA_CTRL_ACK);
  1037. cookie = iop_adma_tx_submit(tx);
  1038. iop_adma_issue_pending(dma_chan);
  1039. msleep(8);
  1040. if (iop_adma_status(dma_chan, cookie, NULL) !=
  1041. DMA_COMPLETE) {
  1042. dev_err(dev, "Self-test !pq-zero-sum timed out, disabling\n");
  1043. err = -ENODEV;
  1044. goto free_resources;
  1045. }
  1046. if (zero_sum_result != (SUM_CHECK_P_RESULT | SUM_CHECK_Q_RESULT)) {
  1047. dev_err(dev, "Self-test !pq-zero-sum failed to validate: %x\n",
  1048. zero_sum_result);
  1049. err = -ENODEV;
  1050. goto free_resources;
  1051. }
  1052. free_resources:
  1053. iop_adma_free_chan_resources(dma_chan);
  1054. out:
  1055. i = ARRAY_SIZE(pq);
  1056. while (i--)
  1057. __free_page(pq[i]);
  1058. return err;
  1059. }
  1060. #endif
  1061. static int iop_adma_remove(struct platform_device *dev)
  1062. {
  1063. struct iop_adma_device *device = platform_get_drvdata(dev);
  1064. struct dma_chan *chan, *_chan;
  1065. struct iop_adma_chan *iop_chan;
  1066. struct iop_adma_platform_data *plat_data = dev_get_platdata(&dev->dev);
  1067. dma_async_device_unregister(&device->common);
  1068. dma_free_coherent(&dev->dev, plat_data->pool_size,
  1069. device->dma_desc_pool_virt, device->dma_desc_pool);
  1070. list_for_each_entry_safe(chan, _chan, &device->common.channels,
  1071. device_node) {
  1072. iop_chan = to_iop_adma_chan(chan);
  1073. list_del(&chan->device_node);
  1074. kfree(iop_chan);
  1075. }
  1076. kfree(device);
  1077. return 0;
  1078. }
  1079. static int iop_adma_probe(struct platform_device *pdev)
  1080. {
  1081. struct resource *res;
  1082. int ret = 0, i;
  1083. struct iop_adma_device *adev;
  1084. struct iop_adma_chan *iop_chan;
  1085. struct dma_device *dma_dev;
  1086. struct iop_adma_platform_data *plat_data = dev_get_platdata(&pdev->dev);
  1087. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1088. if (!res)
  1089. return -ENODEV;
  1090. if (!devm_request_mem_region(&pdev->dev, res->start,
  1091. resource_size(res), pdev->name))
  1092. return -EBUSY;
  1093. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  1094. if (!adev)
  1095. return -ENOMEM;
  1096. dma_dev = &adev->common;
  1097. /* allocate coherent memory for hardware descriptors
  1098. * note: writecombine gives slightly better performance, but
  1099. * requires that we explicitly flush the writes
  1100. */
  1101. adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
  1102. plat_data->pool_size,
  1103. &adev->dma_desc_pool,
  1104. GFP_KERNEL);
  1105. if (!adev->dma_desc_pool_virt) {
  1106. ret = -ENOMEM;
  1107. goto err_free_adev;
  1108. }
  1109. dev_dbg(&pdev->dev, "%s: allocated descriptor pool virt %p phys %p\n",
  1110. __func__, adev->dma_desc_pool_virt,
  1111. (void *) adev->dma_desc_pool);
  1112. adev->id = plat_data->hw_id;
  1113. /* discover transaction capabilites from the platform data */
  1114. dma_dev->cap_mask = plat_data->cap_mask;
  1115. adev->pdev = pdev;
  1116. platform_set_drvdata(pdev, adev);
  1117. INIT_LIST_HEAD(&dma_dev->channels);
  1118. /* set base routines */
  1119. dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
  1120. dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
  1121. dma_dev->device_tx_status = iop_adma_status;
  1122. dma_dev->device_issue_pending = iop_adma_issue_pending;
  1123. dma_dev->dev = &pdev->dev;
  1124. /* set prep routines based on capability */
  1125. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  1126. dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
  1127. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  1128. dma_dev->max_xor = iop_adma_get_max_xor();
  1129. dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
  1130. }
  1131. if (dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask))
  1132. dma_dev->device_prep_dma_xor_val =
  1133. iop_adma_prep_dma_xor_val;
  1134. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  1135. dma_set_maxpq(dma_dev, iop_adma_get_max_pq(), 0);
  1136. dma_dev->device_prep_dma_pq = iop_adma_prep_dma_pq;
  1137. }
  1138. if (dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask))
  1139. dma_dev->device_prep_dma_pq_val =
  1140. iop_adma_prep_dma_pq_val;
  1141. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  1142. dma_dev->device_prep_dma_interrupt =
  1143. iop_adma_prep_dma_interrupt;
  1144. iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
  1145. if (!iop_chan) {
  1146. ret = -ENOMEM;
  1147. goto err_free_dma;
  1148. }
  1149. iop_chan->device = adev;
  1150. iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
  1151. resource_size(res));
  1152. if (!iop_chan->mmr_base) {
  1153. ret = -ENOMEM;
  1154. goto err_free_iop_chan;
  1155. }
  1156. tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
  1157. iop_chan);
  1158. /* clear errors before enabling interrupts */
  1159. iop_adma_device_clear_err_status(iop_chan);
  1160. for (i = 0; i < 3; i++) {
  1161. irq_handler_t handler[] = { iop_adma_eot_handler,
  1162. iop_adma_eoc_handler,
  1163. iop_adma_err_handler };
  1164. int irq = platform_get_irq(pdev, i);
  1165. if (irq < 0) {
  1166. ret = -ENXIO;
  1167. goto err_free_iop_chan;
  1168. } else {
  1169. ret = devm_request_irq(&pdev->dev, irq,
  1170. handler[i], 0, pdev->name, iop_chan);
  1171. if (ret)
  1172. goto err_free_iop_chan;
  1173. }
  1174. }
  1175. spin_lock_init(&iop_chan->lock);
  1176. INIT_LIST_HEAD(&iop_chan->chain);
  1177. INIT_LIST_HEAD(&iop_chan->all_slots);
  1178. iop_chan->common.device = dma_dev;
  1179. dma_cookie_init(&iop_chan->common);
  1180. list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
  1181. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  1182. ret = iop_adma_memcpy_self_test(adev);
  1183. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  1184. if (ret)
  1185. goto err_free_iop_chan;
  1186. }
  1187. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  1188. ret = iop_adma_xor_val_self_test(adev);
  1189. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  1190. if (ret)
  1191. goto err_free_iop_chan;
  1192. }
  1193. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask) &&
  1194. dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask)) {
  1195. #ifdef CONFIG_RAID6_PQ
  1196. ret = iop_adma_pq_zero_sum_self_test(adev);
  1197. dev_dbg(&pdev->dev, "pq self test returned %d\n", ret);
  1198. #else
  1199. /* can not test raid6, so do not publish capability */
  1200. dma_cap_clear(DMA_PQ, dma_dev->cap_mask);
  1201. dma_cap_clear(DMA_PQ_VAL, dma_dev->cap_mask);
  1202. ret = 0;
  1203. #endif
  1204. if (ret)
  1205. goto err_free_iop_chan;
  1206. }
  1207. dev_info(&pdev->dev, "Intel(R) IOP: ( %s%s%s%s%s%s)\n",
  1208. dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "pq " : "",
  1209. dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask) ? "pq_val " : "",
  1210. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  1211. dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask) ? "xor_val " : "",
  1212. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  1213. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  1214. dma_async_device_register(dma_dev);
  1215. goto out;
  1216. err_free_iop_chan:
  1217. kfree(iop_chan);
  1218. err_free_dma:
  1219. dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
  1220. adev->dma_desc_pool_virt, adev->dma_desc_pool);
  1221. err_free_adev:
  1222. kfree(adev);
  1223. out:
  1224. return ret;
  1225. }
  1226. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
  1227. {
  1228. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1229. dma_cookie_t cookie;
  1230. int slot_cnt, slots_per_op;
  1231. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1232. spin_lock_bh(&iop_chan->lock);
  1233. slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
  1234. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1235. if (sw_desc) {
  1236. grp_start = sw_desc->group_head;
  1237. list_splice_init(&sw_desc->tx_list, &iop_chan->chain);
  1238. async_tx_ack(&sw_desc->async_tx);
  1239. iop_desc_init_memcpy(grp_start, 0);
  1240. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1241. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1242. iop_desc_set_memcpy_src_addr(grp_start, 0);
  1243. cookie = dma_cookie_assign(&sw_desc->async_tx);
  1244. /* initialize the completed cookie to be less than
  1245. * the most recently used cookie
  1246. */
  1247. iop_chan->common.completed_cookie = cookie - 1;
  1248. /* channel should not be busy */
  1249. BUG_ON(iop_chan_is_busy(iop_chan));
  1250. /* clear any prior error-status bits */
  1251. iop_adma_device_clear_err_status(iop_chan);
  1252. /* disable operation */
  1253. iop_chan_disable(iop_chan);
  1254. /* set the descriptor address */
  1255. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1256. /* 1/ don't add pre-chained descriptors
  1257. * 2/ dummy read to flush next_desc write
  1258. */
  1259. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1260. /* run the descriptor */
  1261. iop_chan_enable(iop_chan);
  1262. } else
  1263. dev_err(iop_chan->device->common.dev,
  1264. "failed to allocate null descriptor\n");
  1265. spin_unlock_bh(&iop_chan->lock);
  1266. }
  1267. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
  1268. {
  1269. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1270. dma_cookie_t cookie;
  1271. int slot_cnt, slots_per_op;
  1272. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1273. spin_lock_bh(&iop_chan->lock);
  1274. slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
  1275. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1276. if (sw_desc) {
  1277. grp_start = sw_desc->group_head;
  1278. list_splice_init(&sw_desc->tx_list, &iop_chan->chain);
  1279. async_tx_ack(&sw_desc->async_tx);
  1280. iop_desc_init_null_xor(grp_start, 2, 0);
  1281. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1282. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1283. iop_desc_set_xor_src_addr(grp_start, 0, 0);
  1284. iop_desc_set_xor_src_addr(grp_start, 1, 0);
  1285. cookie = dma_cookie_assign(&sw_desc->async_tx);
  1286. /* initialize the completed cookie to be less than
  1287. * the most recently used cookie
  1288. */
  1289. iop_chan->common.completed_cookie = cookie - 1;
  1290. /* channel should not be busy */
  1291. BUG_ON(iop_chan_is_busy(iop_chan));
  1292. /* clear any prior error-status bits */
  1293. iop_adma_device_clear_err_status(iop_chan);
  1294. /* disable operation */
  1295. iop_chan_disable(iop_chan);
  1296. /* set the descriptor address */
  1297. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1298. /* 1/ don't add pre-chained descriptors
  1299. * 2/ dummy read to flush next_desc write
  1300. */
  1301. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1302. /* run the descriptor */
  1303. iop_chan_enable(iop_chan);
  1304. } else
  1305. dev_err(iop_chan->device->common.dev,
  1306. "failed to allocate null descriptor\n");
  1307. spin_unlock_bh(&iop_chan->lock);
  1308. }
  1309. static struct platform_driver iop_adma_driver = {
  1310. .probe = iop_adma_probe,
  1311. .remove = iop_adma_remove,
  1312. .driver = {
  1313. .name = "iop-adma",
  1314. },
  1315. };
  1316. module_platform_driver(iop_adma_driver);
  1317. MODULE_AUTHOR("Intel Corporation");
  1318. MODULE_DESCRIPTION("IOP ADMA Engine Driver");
  1319. MODULE_LICENSE("GPL");
  1320. MODULE_ALIAS("platform:iop-adma");