iop-adma.c 44 KB

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