iop-adma.c 40 KB

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