iop-adma.c 45 KB

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