iop-adma.c 40 KB

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