iop-adma.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. .owner = THIS_MODULE,
  1317. .name = "iop-adma",
  1318. },
  1319. };
  1320. module_platform_driver(iop_adma_driver);
  1321. MODULE_AUTHOR("Intel Corporation");
  1322. MODULE_DESCRIPTION("IOP ADMA Engine Driver");
  1323. MODULE_LICENSE("GPL");
  1324. MODULE_ALIAS("platform:iop-adma");