iop-adma.c 40 KB

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