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