dma.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2009 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 that 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. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. /*
  23. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  24. * copy operations.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/i7300_idle.h>
  35. #include "dma.h"
  36. #include "registers.h"
  37. #include "hw.h"
  38. static int ioat_pending_level = 4;
  39. module_param(ioat_pending_level, int, 0644);
  40. MODULE_PARM_DESC(ioat_pending_level,
  41. "high-water mark for pushing ioat descriptors (default: 4)");
  42. static void ioat_dma_chan_reset_part2(struct work_struct *work);
  43. static void ioat_dma_chan_watchdog(struct work_struct *work);
  44. /* internal functions */
  45. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
  46. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  47. static struct ioat_desc_sw *
  48. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
  49. static struct ioat_desc_sw *
  50. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
  51. static inline struct ioat_dma_chan *
  52. ioat_chan_by_index(struct ioatdma_device *device, int index)
  53. {
  54. return device->idx[index];
  55. }
  56. /**
  57. * ioat_dma_do_interrupt - handler used for single vector interrupt mode
  58. * @irq: interrupt id
  59. * @data: interrupt data
  60. */
  61. static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
  62. {
  63. struct ioatdma_device *instance = data;
  64. struct ioat_dma_chan *ioat_chan;
  65. unsigned long attnstatus;
  66. int bit;
  67. u8 intrctrl;
  68. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  69. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  70. return IRQ_NONE;
  71. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  72. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  73. return IRQ_NONE;
  74. }
  75. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  76. for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
  77. ioat_chan = ioat_chan_by_index(instance, bit);
  78. tasklet_schedule(&ioat_chan->cleanup_task);
  79. }
  80. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  81. return IRQ_HANDLED;
  82. }
  83. /**
  84. * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
  85. * @irq: interrupt id
  86. * @data: interrupt data
  87. */
  88. static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
  89. {
  90. struct ioat_dma_chan *ioat_chan = data;
  91. tasklet_schedule(&ioat_chan->cleanup_task);
  92. return IRQ_HANDLED;
  93. }
  94. static void ioat_dma_cleanup_tasklet(unsigned long data);
  95. /**
  96. * ioat_dma_enumerate_channels - find and initialize the device's channels
  97. * @device: the device to be enumerated
  98. */
  99. static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
  100. {
  101. u8 xfercap_scale;
  102. u32 xfercap;
  103. int i;
  104. struct ioat_dma_chan *ioat_chan;
  105. struct device *dev = &device->pdev->dev;
  106. struct dma_device *dma = &device->common;
  107. INIT_LIST_HEAD(&dma->channels);
  108. dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  109. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  110. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  111. #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
  112. if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
  113. dma->chancnt--;
  114. #endif
  115. for (i = 0; i < dma->chancnt; i++) {
  116. ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL);
  117. if (!ioat_chan) {
  118. dma->chancnt = i;
  119. break;
  120. }
  121. ioat_chan->device = device;
  122. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  123. ioat_chan->xfercap = xfercap;
  124. ioat_chan->desccount = 0;
  125. INIT_DELAYED_WORK(&ioat_chan->work, ioat_dma_chan_reset_part2);
  126. spin_lock_init(&ioat_chan->cleanup_lock);
  127. spin_lock_init(&ioat_chan->desc_lock);
  128. INIT_LIST_HEAD(&ioat_chan->free_desc);
  129. INIT_LIST_HEAD(&ioat_chan->used_desc);
  130. /* This should be made common somewhere in dmaengine.c */
  131. ioat_chan->common.device = &device->common;
  132. list_add_tail(&ioat_chan->common.device_node, &dma->channels);
  133. device->idx[i] = ioat_chan;
  134. tasklet_init(&ioat_chan->cleanup_task,
  135. ioat_dma_cleanup_tasklet,
  136. (unsigned long) ioat_chan);
  137. tasklet_disable(&ioat_chan->cleanup_task);
  138. }
  139. return dma->chancnt;
  140. }
  141. /**
  142. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  143. * descriptors to hw
  144. * @chan: DMA channel handle
  145. */
  146. static inline void
  147. __ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
  148. {
  149. ioat_chan->pending = 0;
  150. writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
  151. }
  152. static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
  153. {
  154. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  155. if (ioat_chan->pending > 0) {
  156. spin_lock_bh(&ioat_chan->desc_lock);
  157. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  158. spin_unlock_bh(&ioat_chan->desc_lock);
  159. }
  160. }
  161. static inline void
  162. __ioat2_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
  163. {
  164. ioat_chan->pending = 0;
  165. writew(ioat_chan->dmacount,
  166. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  167. }
  168. static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
  169. {
  170. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  171. if (ioat_chan->pending > 0) {
  172. spin_lock_bh(&ioat_chan->desc_lock);
  173. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  174. spin_unlock_bh(&ioat_chan->desc_lock);
  175. }
  176. }
  177. /**
  178. * ioat_dma_chan_reset_part2 - reinit the channel after a reset
  179. */
  180. static void ioat_dma_chan_reset_part2(struct work_struct *work)
  181. {
  182. struct ioat_dma_chan *ioat_chan =
  183. container_of(work, struct ioat_dma_chan, work.work);
  184. struct ioat_desc_sw *desc;
  185. spin_lock_bh(&ioat_chan->cleanup_lock);
  186. spin_lock_bh(&ioat_chan->desc_lock);
  187. ioat_chan->completion_virt->low = 0;
  188. ioat_chan->completion_virt->high = 0;
  189. ioat_chan->pending = 0;
  190. /*
  191. * count the descriptors waiting, and be sure to do it
  192. * right for both the CB1 line and the CB2 ring
  193. */
  194. ioat_chan->dmacount = 0;
  195. if (ioat_chan->used_desc.prev) {
  196. desc = to_ioat_desc(ioat_chan->used_desc.prev);
  197. do {
  198. ioat_chan->dmacount++;
  199. desc = to_ioat_desc(desc->node.next);
  200. } while (&desc->node != ioat_chan->used_desc.next);
  201. }
  202. /*
  203. * write the new starting descriptor address
  204. * this puts channel engine into ARMED state
  205. */
  206. desc = to_ioat_desc(ioat_chan->used_desc.prev);
  207. switch (ioat_chan->device->version) {
  208. case IOAT_VER_1_2:
  209. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  210. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  211. writel(((u64) desc->txd.phys) >> 32,
  212. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  213. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  214. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  215. break;
  216. case IOAT_VER_2_0:
  217. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  218. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  219. writel(((u64) desc->txd.phys) >> 32,
  220. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  221. /* tell the engine to go with what's left to be done */
  222. writew(ioat_chan->dmacount,
  223. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  224. break;
  225. }
  226. dev_err(to_dev(ioat_chan),
  227. "chan%d reset - %d descs waiting, %d total desc\n",
  228. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  229. spin_unlock_bh(&ioat_chan->desc_lock);
  230. spin_unlock_bh(&ioat_chan->cleanup_lock);
  231. }
  232. /**
  233. * ioat_dma_reset_channel - restart a channel
  234. * @ioat_chan: IOAT DMA channel handle
  235. */
  236. static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat_chan)
  237. {
  238. u32 chansts, chanerr;
  239. if (!ioat_chan->used_desc.prev)
  240. return;
  241. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  242. chansts = (ioat_chan->completion_virt->low
  243. & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
  244. if (chanerr) {
  245. dev_err(to_dev(ioat_chan),
  246. "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
  247. chan_num(ioat_chan), chansts, chanerr);
  248. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  249. }
  250. /*
  251. * whack it upside the head with a reset
  252. * and wait for things to settle out.
  253. * force the pending count to a really big negative
  254. * to make sure no one forces an issue_pending
  255. * while we're waiting.
  256. */
  257. spin_lock_bh(&ioat_chan->desc_lock);
  258. ioat_chan->pending = INT_MIN;
  259. writeb(IOAT_CHANCMD_RESET,
  260. ioat_chan->reg_base
  261. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  262. spin_unlock_bh(&ioat_chan->desc_lock);
  263. /* schedule the 2nd half instead of sleeping a long time */
  264. schedule_delayed_work(&ioat_chan->work, RESET_DELAY);
  265. }
  266. /**
  267. * ioat_dma_chan_watchdog - watch for stuck channels
  268. */
  269. static void ioat_dma_chan_watchdog(struct work_struct *work)
  270. {
  271. struct ioatdma_device *device =
  272. container_of(work, struct ioatdma_device, work.work);
  273. struct ioat_dma_chan *ioat_chan;
  274. int i;
  275. union {
  276. u64 full;
  277. struct {
  278. u32 low;
  279. u32 high;
  280. };
  281. } completion_hw;
  282. unsigned long compl_desc_addr_hw;
  283. for (i = 0; i < device->common.chancnt; i++) {
  284. ioat_chan = ioat_chan_by_index(device, i);
  285. if (ioat_chan->device->version == IOAT_VER_1_2
  286. /* have we started processing anything yet */
  287. && ioat_chan->last_completion
  288. /* have we completed any since last watchdog cycle? */
  289. && (ioat_chan->last_completion ==
  290. ioat_chan->watchdog_completion)
  291. /* has TCP stuck on one cookie since last watchdog? */
  292. && (ioat_chan->watchdog_tcp_cookie ==
  293. ioat_chan->watchdog_last_tcp_cookie)
  294. && (ioat_chan->watchdog_tcp_cookie !=
  295. ioat_chan->completed_cookie)
  296. /* is there something in the chain to be processed? */
  297. /* CB1 chain always has at least the last one processed */
  298. && (ioat_chan->used_desc.prev != ioat_chan->used_desc.next)
  299. && ioat_chan->pending == 0) {
  300. /*
  301. * check CHANSTS register for completed
  302. * descriptor address.
  303. * if it is different than completion writeback,
  304. * it is not zero
  305. * and it has changed since the last watchdog
  306. * we can assume that channel
  307. * is still working correctly
  308. * and the problem is in completion writeback.
  309. * update completion writeback
  310. * with actual CHANSTS value
  311. * else
  312. * try resetting the channel
  313. */
  314. completion_hw.low = readl(ioat_chan->reg_base +
  315. IOAT_CHANSTS_OFFSET_LOW(ioat_chan->device->version));
  316. completion_hw.high = readl(ioat_chan->reg_base +
  317. IOAT_CHANSTS_OFFSET_HIGH(ioat_chan->device->version));
  318. #if (BITS_PER_LONG == 64)
  319. compl_desc_addr_hw =
  320. completion_hw.full
  321. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  322. #else
  323. compl_desc_addr_hw =
  324. completion_hw.low & IOAT_LOW_COMPLETION_MASK;
  325. #endif
  326. if ((compl_desc_addr_hw != 0)
  327. && (compl_desc_addr_hw != ioat_chan->watchdog_completion)
  328. && (compl_desc_addr_hw != ioat_chan->last_compl_desc_addr_hw)) {
  329. ioat_chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
  330. ioat_chan->completion_virt->low = completion_hw.low;
  331. ioat_chan->completion_virt->high = completion_hw.high;
  332. } else {
  333. ioat_dma_reset_channel(ioat_chan);
  334. ioat_chan->watchdog_completion = 0;
  335. ioat_chan->last_compl_desc_addr_hw = 0;
  336. }
  337. /*
  338. * for version 2.0 if there are descriptors yet to be processed
  339. * and the last completed hasn't changed since the last watchdog
  340. * if they haven't hit the pending level
  341. * issue the pending to push them through
  342. * else
  343. * try resetting the channel
  344. */
  345. } else if (ioat_chan->device->version == IOAT_VER_2_0
  346. && ioat_chan->used_desc.prev
  347. && ioat_chan->last_completion
  348. && ioat_chan->last_completion == ioat_chan->watchdog_completion) {
  349. if (ioat_chan->pending < ioat_pending_level)
  350. ioat2_dma_memcpy_issue_pending(&ioat_chan->common);
  351. else {
  352. ioat_dma_reset_channel(ioat_chan);
  353. ioat_chan->watchdog_completion = 0;
  354. }
  355. } else {
  356. ioat_chan->last_compl_desc_addr_hw = 0;
  357. ioat_chan->watchdog_completion
  358. = ioat_chan->last_completion;
  359. }
  360. ioat_chan->watchdog_last_tcp_cookie =
  361. ioat_chan->watchdog_tcp_cookie;
  362. }
  363. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  364. }
  365. static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
  366. {
  367. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  368. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  369. struct ioat_desc_sw *prev, *new;
  370. struct ioat_dma_descriptor *hw;
  371. dma_cookie_t cookie;
  372. LIST_HEAD(new_chain);
  373. u32 copy;
  374. size_t len;
  375. dma_addr_t src, dst;
  376. unsigned long orig_flags;
  377. unsigned int desc_count = 0;
  378. /* src and dest and len are stored in the initial descriptor */
  379. len = first->len;
  380. src = first->src;
  381. dst = first->dst;
  382. orig_flags = first->txd.flags;
  383. new = first;
  384. spin_lock_bh(&ioat_chan->desc_lock);
  385. prev = to_ioat_desc(ioat_chan->used_desc.prev);
  386. prefetch(prev->hw);
  387. do {
  388. copy = min_t(size_t, len, ioat_chan->xfercap);
  389. async_tx_ack(&new->txd);
  390. hw = new->hw;
  391. hw->size = copy;
  392. hw->ctl = 0;
  393. hw->src_addr = src;
  394. hw->dst_addr = dst;
  395. hw->next = 0;
  396. /* chain together the physical address list for the HW */
  397. wmb();
  398. prev->hw->next = (u64) new->txd.phys;
  399. len -= copy;
  400. dst += copy;
  401. src += copy;
  402. list_add_tail(&new->node, &new_chain);
  403. desc_count++;
  404. prev = new;
  405. } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
  406. if (!new) {
  407. dev_err(to_dev(ioat_chan), "tx submit failed\n");
  408. spin_unlock_bh(&ioat_chan->desc_lock);
  409. return -ENOMEM;
  410. }
  411. hw->ctl_f.compl_write = 1;
  412. if (first->txd.callback) {
  413. hw->ctl_f.int_en = 1;
  414. if (first != new) {
  415. /* move callback into to last desc */
  416. new->txd.callback = first->txd.callback;
  417. new->txd.callback_param
  418. = first->txd.callback_param;
  419. first->txd.callback = NULL;
  420. first->txd.callback_param = NULL;
  421. }
  422. }
  423. new->tx_cnt = desc_count;
  424. new->txd.flags = orig_flags; /* client is in control of this ack */
  425. /* store the original values for use in later cleanup */
  426. if (new != first) {
  427. new->src = first->src;
  428. new->dst = first->dst;
  429. new->len = first->len;
  430. }
  431. /* cookie incr and addition to used_list must be atomic */
  432. cookie = ioat_chan->common.cookie;
  433. cookie++;
  434. if (cookie < 0)
  435. cookie = 1;
  436. ioat_chan->common.cookie = new->txd.cookie = cookie;
  437. /* write address into NextDescriptor field of last desc in chain */
  438. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  439. first->txd.phys;
  440. list_splice_tail(&new_chain, &ioat_chan->used_desc);
  441. ioat_chan->dmacount += desc_count;
  442. ioat_chan->pending += desc_count;
  443. if (ioat_chan->pending >= ioat_pending_level)
  444. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  445. spin_unlock_bh(&ioat_chan->desc_lock);
  446. return cookie;
  447. }
  448. static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
  449. {
  450. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  451. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  452. struct ioat_desc_sw *new;
  453. struct ioat_dma_descriptor *hw;
  454. dma_cookie_t cookie;
  455. u32 copy;
  456. size_t len;
  457. dma_addr_t src, dst;
  458. unsigned long orig_flags;
  459. unsigned int desc_count = 0;
  460. /* src and dest and len are stored in the initial descriptor */
  461. len = first->len;
  462. src = first->src;
  463. dst = first->dst;
  464. orig_flags = first->txd.flags;
  465. new = first;
  466. /*
  467. * ioat_chan->desc_lock is still in force in version 2 path
  468. * it gets unlocked at end of this function
  469. */
  470. do {
  471. copy = min_t(size_t, len, ioat_chan->xfercap);
  472. async_tx_ack(&new->txd);
  473. hw = new->hw;
  474. hw->size = copy;
  475. hw->ctl = 0;
  476. hw->src_addr = src;
  477. hw->dst_addr = dst;
  478. len -= copy;
  479. dst += copy;
  480. src += copy;
  481. desc_count++;
  482. } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
  483. if (!new) {
  484. dev_err(to_dev(ioat_chan), "tx submit failed\n");
  485. spin_unlock_bh(&ioat_chan->desc_lock);
  486. return -ENOMEM;
  487. }
  488. hw->ctl_f.compl_write = 1;
  489. if (first->txd.callback) {
  490. hw->ctl_f.int_en = 1;
  491. if (first != new) {
  492. /* move callback into to last desc */
  493. new->txd.callback = first->txd.callback;
  494. new->txd.callback_param
  495. = first->txd.callback_param;
  496. first->txd.callback = NULL;
  497. first->txd.callback_param = NULL;
  498. }
  499. }
  500. new->tx_cnt = desc_count;
  501. new->txd.flags = orig_flags; /* client is in control of this ack */
  502. /* store the original values for use in later cleanup */
  503. if (new != first) {
  504. new->src = first->src;
  505. new->dst = first->dst;
  506. new->len = first->len;
  507. }
  508. /* cookie incr and addition to used_list must be atomic */
  509. cookie = ioat_chan->common.cookie;
  510. cookie++;
  511. if (cookie < 0)
  512. cookie = 1;
  513. ioat_chan->common.cookie = new->txd.cookie = cookie;
  514. ioat_chan->dmacount += desc_count;
  515. ioat_chan->pending += desc_count;
  516. if (ioat_chan->pending >= ioat_pending_level)
  517. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  518. spin_unlock_bh(&ioat_chan->desc_lock);
  519. return cookie;
  520. }
  521. /**
  522. * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
  523. * @ioat_chan: the channel supplying the memory pool for the descriptors
  524. * @flags: allocation flags
  525. */
  526. static struct ioat_desc_sw *
  527. ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat_chan, gfp_t flags)
  528. {
  529. struct ioat_dma_descriptor *desc;
  530. struct ioat_desc_sw *desc_sw;
  531. struct ioatdma_device *ioatdma_device;
  532. dma_addr_t phys;
  533. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  534. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  535. if (unlikely(!desc))
  536. return NULL;
  537. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  538. if (unlikely(!desc_sw)) {
  539. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  540. return NULL;
  541. }
  542. memset(desc, 0, sizeof(*desc));
  543. dma_async_tx_descriptor_init(&desc_sw->txd, &ioat_chan->common);
  544. switch (ioat_chan->device->version) {
  545. case IOAT_VER_1_2:
  546. desc_sw->txd.tx_submit = ioat1_tx_submit;
  547. break;
  548. case IOAT_VER_2_0:
  549. case IOAT_VER_3_0:
  550. desc_sw->txd.tx_submit = ioat2_tx_submit;
  551. break;
  552. }
  553. desc_sw->hw = desc;
  554. desc_sw->txd.phys = phys;
  555. return desc_sw;
  556. }
  557. static int ioat_initial_desc_count = 256;
  558. module_param(ioat_initial_desc_count, int, 0644);
  559. MODULE_PARM_DESC(ioat_initial_desc_count,
  560. "initial descriptors per channel (default: 256)");
  561. /**
  562. * ioat2_dma_massage_chan_desc - link the descriptors into a circle
  563. * @ioat_chan: the channel to be massaged
  564. */
  565. static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
  566. {
  567. struct ioat_desc_sw *desc, *_desc;
  568. /* setup used_desc */
  569. ioat_chan->used_desc.next = ioat_chan->free_desc.next;
  570. ioat_chan->used_desc.prev = NULL;
  571. /* pull free_desc out of the circle so that every node is a hw
  572. * descriptor, but leave it pointing to the list
  573. */
  574. ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
  575. ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
  576. /* circle link the hw descriptors */
  577. desc = to_ioat_desc(ioat_chan->free_desc.next);
  578. desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
  579. list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
  580. desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
  581. }
  582. }
  583. /**
  584. * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
  585. * @chan: the channel to be filled out
  586. */
  587. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  588. {
  589. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  590. struct ioat_desc_sw *desc;
  591. u16 chanctrl;
  592. u32 chanerr;
  593. int i;
  594. LIST_HEAD(tmp_list);
  595. /* have we already been set up? */
  596. if (!list_empty(&ioat_chan->free_desc))
  597. return ioat_chan->desccount;
  598. /* Setup register to interrupt and write completion status on error */
  599. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  600. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  601. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  602. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  603. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  604. if (chanerr) {
  605. dev_err(to_dev(ioat_chan), "CHANERR = %x, clearing\n", chanerr);
  606. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  607. }
  608. /* Allocate descriptors */
  609. for (i = 0; i < ioat_initial_desc_count; i++) {
  610. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  611. if (!desc) {
  612. dev_err(to_dev(ioat_chan),
  613. "Only %d initial descriptors\n", i);
  614. break;
  615. }
  616. list_add_tail(&desc->node, &tmp_list);
  617. }
  618. spin_lock_bh(&ioat_chan->desc_lock);
  619. ioat_chan->desccount = i;
  620. list_splice(&tmp_list, &ioat_chan->free_desc);
  621. if (ioat_chan->device->version != IOAT_VER_1_2)
  622. ioat2_dma_massage_chan_desc(ioat_chan);
  623. spin_unlock_bh(&ioat_chan->desc_lock);
  624. /* allocate a completion writeback area */
  625. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  626. ioat_chan->completion_virt =
  627. pci_pool_alloc(ioat_chan->device->completion_pool,
  628. GFP_KERNEL,
  629. &ioat_chan->completion_addr);
  630. memset(ioat_chan->completion_virt, 0,
  631. sizeof(*ioat_chan->completion_virt));
  632. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  633. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  634. writel(((u64) ioat_chan->completion_addr) >> 32,
  635. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  636. tasklet_enable(&ioat_chan->cleanup_task);
  637. ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
  638. return ioat_chan->desccount;
  639. }
  640. /**
  641. * ioat_dma_free_chan_resources - release all the descriptors
  642. * @chan: the channel to be cleaned
  643. */
  644. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  645. {
  646. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  647. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  648. struct ioat_desc_sw *desc, *_desc;
  649. int in_use_descs = 0;
  650. /* Before freeing channel resources first check
  651. * if they have been previously allocated for this channel.
  652. */
  653. if (ioat_chan->desccount == 0)
  654. return;
  655. tasklet_disable(&ioat_chan->cleanup_task);
  656. ioat_dma_memcpy_cleanup(ioat_chan);
  657. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  658. * before removing DMA descriptor resources.
  659. */
  660. writeb(IOAT_CHANCMD_RESET,
  661. ioat_chan->reg_base
  662. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  663. mdelay(100);
  664. spin_lock_bh(&ioat_chan->desc_lock);
  665. switch (ioat_chan->device->version) {
  666. case IOAT_VER_1_2:
  667. list_for_each_entry_safe(desc, _desc,
  668. &ioat_chan->used_desc, node) {
  669. in_use_descs++;
  670. list_del(&desc->node);
  671. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  672. desc->txd.phys);
  673. kfree(desc);
  674. }
  675. list_for_each_entry_safe(desc, _desc,
  676. &ioat_chan->free_desc, node) {
  677. list_del(&desc->node);
  678. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  679. desc->txd.phys);
  680. kfree(desc);
  681. }
  682. break;
  683. case IOAT_VER_2_0:
  684. case IOAT_VER_3_0:
  685. list_for_each_entry_safe(desc, _desc,
  686. ioat_chan->free_desc.next, node) {
  687. list_del(&desc->node);
  688. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  689. desc->txd.phys);
  690. kfree(desc);
  691. }
  692. desc = to_ioat_desc(ioat_chan->free_desc.next);
  693. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  694. desc->txd.phys);
  695. kfree(desc);
  696. INIT_LIST_HEAD(&ioat_chan->free_desc);
  697. INIT_LIST_HEAD(&ioat_chan->used_desc);
  698. break;
  699. }
  700. spin_unlock_bh(&ioat_chan->desc_lock);
  701. pci_pool_free(ioatdma_device->completion_pool,
  702. ioat_chan->completion_virt,
  703. ioat_chan->completion_addr);
  704. /* one is ok since we left it on there on purpose */
  705. if (in_use_descs > 1)
  706. dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
  707. in_use_descs - 1);
  708. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  709. ioat_chan->pending = 0;
  710. ioat_chan->dmacount = 0;
  711. ioat_chan->desccount = 0;
  712. ioat_chan->watchdog_completion = 0;
  713. ioat_chan->last_compl_desc_addr_hw = 0;
  714. ioat_chan->watchdog_tcp_cookie =
  715. ioat_chan->watchdog_last_tcp_cookie = 0;
  716. }
  717. /**
  718. * ioat_dma_get_next_descriptor - return the next available descriptor
  719. * @ioat_chan: IOAT DMA channel handle
  720. *
  721. * Gets the next descriptor from the chain, and must be called with the
  722. * channel's desc_lock held. Allocates more descriptors if the channel
  723. * has run out.
  724. */
  725. static struct ioat_desc_sw *
  726. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  727. {
  728. struct ioat_desc_sw *new;
  729. if (!list_empty(&ioat_chan->free_desc)) {
  730. new = to_ioat_desc(ioat_chan->free_desc.next);
  731. list_del(&new->node);
  732. } else {
  733. /* try to get another desc */
  734. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  735. if (!new) {
  736. dev_err(to_dev(ioat_chan), "alloc failed\n");
  737. return NULL;
  738. }
  739. }
  740. prefetch(new->hw);
  741. return new;
  742. }
  743. static struct ioat_desc_sw *
  744. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  745. {
  746. struct ioat_desc_sw *new;
  747. /*
  748. * used.prev points to where to start processing
  749. * used.next points to next free descriptor
  750. * if used.prev == NULL, there are none waiting to be processed
  751. * if used.next == used.prev.prev, there is only one free descriptor,
  752. * and we need to use it to as a noop descriptor before
  753. * linking in a new set of descriptors, since the device
  754. * has probably already read the pointer to it
  755. */
  756. if (ioat_chan->used_desc.prev &&
  757. ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
  758. struct ioat_desc_sw *desc;
  759. struct ioat_desc_sw *noop_desc;
  760. int i;
  761. /* set up the noop descriptor */
  762. noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
  763. /* set size to non-zero value (channel returns error when size is 0) */
  764. noop_desc->hw->size = NULL_DESC_BUFFER_SIZE;
  765. noop_desc->hw->ctl = 0;
  766. noop_desc->hw->ctl_f.null = 1;
  767. noop_desc->hw->src_addr = 0;
  768. noop_desc->hw->dst_addr = 0;
  769. ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
  770. ioat_chan->pending++;
  771. ioat_chan->dmacount++;
  772. /* try to get a few more descriptors */
  773. for (i = 16; i; i--) {
  774. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  775. if (!desc) {
  776. dev_err(to_dev(ioat_chan), "alloc failed\n");
  777. break;
  778. }
  779. list_add_tail(&desc->node, ioat_chan->used_desc.next);
  780. desc->hw->next
  781. = to_ioat_desc(desc->node.next)->txd.phys;
  782. to_ioat_desc(desc->node.prev)->hw->next
  783. = desc->txd.phys;
  784. ioat_chan->desccount++;
  785. }
  786. ioat_chan->used_desc.next = noop_desc->node.next;
  787. }
  788. new = to_ioat_desc(ioat_chan->used_desc.next);
  789. prefetch(new);
  790. ioat_chan->used_desc.next = new->node.next;
  791. if (ioat_chan->used_desc.prev == NULL)
  792. ioat_chan->used_desc.prev = &new->node;
  793. prefetch(new->hw);
  794. return new;
  795. }
  796. static struct ioat_desc_sw *
  797. ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  798. {
  799. if (!ioat_chan)
  800. return NULL;
  801. switch (ioat_chan->device->version) {
  802. case IOAT_VER_1_2:
  803. return ioat1_dma_get_next_descriptor(ioat_chan);
  804. case IOAT_VER_2_0:
  805. case IOAT_VER_3_0:
  806. return ioat2_dma_get_next_descriptor(ioat_chan);
  807. }
  808. return NULL;
  809. }
  810. static struct dma_async_tx_descriptor *
  811. ioat1_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  812. dma_addr_t dma_src, size_t len, unsigned long flags)
  813. {
  814. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  815. struct ioat_desc_sw *new;
  816. spin_lock_bh(&ioat_chan->desc_lock);
  817. new = ioat_dma_get_next_descriptor(ioat_chan);
  818. spin_unlock_bh(&ioat_chan->desc_lock);
  819. if (new) {
  820. new->len = len;
  821. new->dst = dma_dest;
  822. new->src = dma_src;
  823. new->txd.flags = flags;
  824. return &new->txd;
  825. } else {
  826. dev_err(to_dev(ioat_chan),
  827. "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
  828. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  829. return NULL;
  830. }
  831. }
  832. static struct dma_async_tx_descriptor *
  833. ioat2_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  834. dma_addr_t dma_src, size_t len, unsigned long flags)
  835. {
  836. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  837. struct ioat_desc_sw *new;
  838. spin_lock_bh(&ioat_chan->desc_lock);
  839. new = ioat2_dma_get_next_descriptor(ioat_chan);
  840. /*
  841. * leave ioat_chan->desc_lock set in ioat 2 path
  842. * it will get unlocked at end of tx_submit
  843. */
  844. if (new) {
  845. new->len = len;
  846. new->dst = dma_dest;
  847. new->src = dma_src;
  848. new->txd.flags = flags;
  849. return &new->txd;
  850. } else {
  851. spin_unlock_bh(&ioat_chan->desc_lock);
  852. dev_err(to_dev(ioat_chan),
  853. "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
  854. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  855. return NULL;
  856. }
  857. }
  858. static void ioat_dma_cleanup_tasklet(unsigned long data)
  859. {
  860. struct ioat_dma_chan *chan = (void *)data;
  861. ioat_dma_memcpy_cleanup(chan);
  862. writew(IOAT_CHANCTRL_INT_DISABLE,
  863. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  864. }
  865. static void
  866. ioat_dma_unmap(struct ioat_dma_chan *ioat_chan, struct ioat_desc_sw *desc)
  867. {
  868. if (!(desc->txd.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  869. if (desc->txd.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  870. pci_unmap_single(ioat_chan->device->pdev,
  871. pci_unmap_addr(desc, dst),
  872. pci_unmap_len(desc, len),
  873. PCI_DMA_FROMDEVICE);
  874. else
  875. pci_unmap_page(ioat_chan->device->pdev,
  876. pci_unmap_addr(desc, dst),
  877. pci_unmap_len(desc, len),
  878. PCI_DMA_FROMDEVICE);
  879. }
  880. if (!(desc->txd.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  881. if (desc->txd.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  882. pci_unmap_single(ioat_chan->device->pdev,
  883. pci_unmap_addr(desc, src),
  884. pci_unmap_len(desc, len),
  885. PCI_DMA_TODEVICE);
  886. else
  887. pci_unmap_page(ioat_chan->device->pdev,
  888. pci_unmap_addr(desc, src),
  889. pci_unmap_len(desc, len),
  890. PCI_DMA_TODEVICE);
  891. }
  892. }
  893. /**
  894. * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
  895. * @chan: ioat channel to be cleaned up
  896. */
  897. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  898. {
  899. unsigned long phys_complete;
  900. struct ioat_desc_sw *desc, *_desc;
  901. dma_cookie_t cookie = 0;
  902. unsigned long desc_phys;
  903. struct ioat_desc_sw *latest_desc;
  904. struct dma_async_tx_descriptor *tx;
  905. prefetch(ioat_chan->completion_virt);
  906. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  907. return;
  908. /* The completion writeback can happen at any time,
  909. so reads by the driver need to be atomic operations
  910. The descriptor physical addresses are limited to 32-bits
  911. when the CPU can only do a 32-bit mov */
  912. #if (BITS_PER_LONG == 64)
  913. phys_complete =
  914. ioat_chan->completion_virt->full
  915. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  916. #else
  917. phys_complete =
  918. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  919. #endif
  920. if ((ioat_chan->completion_virt->full
  921. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  922. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  923. dev_err(to_dev(ioat_chan), "Channel halted, chanerr = %x\n",
  924. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  925. /* TODO do something to salvage the situation */
  926. }
  927. if (phys_complete == ioat_chan->last_completion) {
  928. spin_unlock_bh(&ioat_chan->cleanup_lock);
  929. /*
  930. * perhaps we're stuck so hard that the watchdog can't go off?
  931. * try to catch it after 2 seconds
  932. */
  933. if (ioat_chan->device->version != IOAT_VER_3_0) {
  934. if (time_after(jiffies,
  935. ioat_chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
  936. ioat_dma_chan_watchdog(&(ioat_chan->device->work.work));
  937. ioat_chan->last_completion_time = jiffies;
  938. }
  939. }
  940. return;
  941. }
  942. ioat_chan->last_completion_time = jiffies;
  943. cookie = 0;
  944. if (!spin_trylock_bh(&ioat_chan->desc_lock)) {
  945. spin_unlock_bh(&ioat_chan->cleanup_lock);
  946. return;
  947. }
  948. switch (ioat_chan->device->version) {
  949. case IOAT_VER_1_2:
  950. list_for_each_entry_safe(desc, _desc,
  951. &ioat_chan->used_desc, node) {
  952. tx = &desc->txd;
  953. /*
  954. * Incoming DMA requests may use multiple descriptors,
  955. * due to exceeding xfercap, perhaps. If so, only the
  956. * last one will have a cookie, and require unmapping.
  957. */
  958. if (tx->cookie) {
  959. cookie = tx->cookie;
  960. ioat_dma_unmap(ioat_chan, desc);
  961. if (tx->callback) {
  962. tx->callback(tx->callback_param);
  963. tx->callback = NULL;
  964. }
  965. }
  966. if (tx->phys != phys_complete) {
  967. /*
  968. * a completed entry, but not the last, so clean
  969. * up if the client is done with the descriptor
  970. */
  971. if (async_tx_test_ack(tx)) {
  972. list_move_tail(&desc->node,
  973. &ioat_chan->free_desc);
  974. } else
  975. tx->cookie = 0;
  976. } else {
  977. /*
  978. * last used desc. Do not remove, so we can
  979. * append from it, but don't look at it next
  980. * time, either
  981. */
  982. tx->cookie = 0;
  983. /* TODO check status bits? */
  984. break;
  985. }
  986. }
  987. break;
  988. case IOAT_VER_2_0:
  989. case IOAT_VER_3_0:
  990. /* has some other thread has already cleaned up? */
  991. if (ioat_chan->used_desc.prev == NULL)
  992. break;
  993. /* work backwards to find latest finished desc */
  994. desc = to_ioat_desc(ioat_chan->used_desc.next);
  995. tx = &desc->txd;
  996. latest_desc = NULL;
  997. do {
  998. desc = to_ioat_desc(desc->node.prev);
  999. desc_phys = (unsigned long)tx->phys
  1000. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  1001. if (desc_phys == phys_complete) {
  1002. latest_desc = desc;
  1003. break;
  1004. }
  1005. } while (&desc->node != ioat_chan->used_desc.prev);
  1006. if (latest_desc != NULL) {
  1007. /* work forwards to clear finished descriptors */
  1008. for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
  1009. &desc->node != latest_desc->node.next &&
  1010. &desc->node != ioat_chan->used_desc.next;
  1011. desc = to_ioat_desc(desc->node.next)) {
  1012. if (tx->cookie) {
  1013. cookie = tx->cookie;
  1014. tx->cookie = 0;
  1015. ioat_dma_unmap(ioat_chan, desc);
  1016. if (tx->callback) {
  1017. tx->callback(tx->callback_param);
  1018. tx->callback = NULL;
  1019. }
  1020. }
  1021. }
  1022. /* move used.prev up beyond those that are finished */
  1023. if (&desc->node == ioat_chan->used_desc.next)
  1024. ioat_chan->used_desc.prev = NULL;
  1025. else
  1026. ioat_chan->used_desc.prev = &desc->node;
  1027. }
  1028. break;
  1029. }
  1030. spin_unlock_bh(&ioat_chan->desc_lock);
  1031. ioat_chan->last_completion = phys_complete;
  1032. if (cookie != 0)
  1033. ioat_chan->completed_cookie = cookie;
  1034. spin_unlock_bh(&ioat_chan->cleanup_lock);
  1035. }
  1036. /**
  1037. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  1038. * @chan: IOAT DMA channel handle
  1039. * @cookie: DMA transaction identifier
  1040. * @done: if not %NULL, updated with last completed transaction
  1041. * @used: if not %NULL, updated with last used transaction
  1042. */
  1043. static enum dma_status
  1044. ioat_dma_is_complete(struct dma_chan *chan, dma_cookie_t cookie,
  1045. dma_cookie_t *done, dma_cookie_t *used)
  1046. {
  1047. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  1048. dma_cookie_t last_used;
  1049. dma_cookie_t last_complete;
  1050. enum dma_status ret;
  1051. last_used = chan->cookie;
  1052. last_complete = ioat_chan->completed_cookie;
  1053. ioat_chan->watchdog_tcp_cookie = cookie;
  1054. if (done)
  1055. *done = last_complete;
  1056. if (used)
  1057. *used = last_used;
  1058. ret = dma_async_is_complete(cookie, last_complete, last_used);
  1059. if (ret == DMA_SUCCESS)
  1060. return ret;
  1061. ioat_dma_memcpy_cleanup(ioat_chan);
  1062. last_used = chan->cookie;
  1063. last_complete = ioat_chan->completed_cookie;
  1064. if (done)
  1065. *done = last_complete;
  1066. if (used)
  1067. *used = last_used;
  1068. return dma_async_is_complete(cookie, last_complete, last_used);
  1069. }
  1070. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  1071. {
  1072. struct ioat_desc_sw *desc;
  1073. struct ioat_dma_descriptor *hw;
  1074. spin_lock_bh(&ioat_chan->desc_lock);
  1075. desc = ioat_dma_get_next_descriptor(ioat_chan);
  1076. if (!desc) {
  1077. dev_err(to_dev(ioat_chan),
  1078. "Unable to start null desc - get next desc failed\n");
  1079. spin_unlock_bh(&ioat_chan->desc_lock);
  1080. return;
  1081. }
  1082. hw = desc->hw;
  1083. hw->ctl = 0;
  1084. hw->ctl_f.null = 1;
  1085. hw->ctl_f.int_en = 1;
  1086. hw->ctl_f.compl_write = 1;
  1087. /* set size to non-zero value (channel returns error when size is 0) */
  1088. hw->size = NULL_DESC_BUFFER_SIZE;
  1089. hw->src_addr = 0;
  1090. hw->dst_addr = 0;
  1091. async_tx_ack(&desc->txd);
  1092. switch (ioat_chan->device->version) {
  1093. case IOAT_VER_1_2:
  1094. hw->next = 0;
  1095. list_add_tail(&desc->node, &ioat_chan->used_desc);
  1096. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  1097. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  1098. writel(((u64) desc->txd.phys) >> 32,
  1099. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  1100. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  1101. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  1102. break;
  1103. case IOAT_VER_2_0:
  1104. case IOAT_VER_3_0:
  1105. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  1106. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  1107. writel(((u64) desc->txd.phys) >> 32,
  1108. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  1109. ioat_chan->dmacount++;
  1110. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  1111. break;
  1112. }
  1113. spin_unlock_bh(&ioat_chan->desc_lock);
  1114. }
  1115. /*
  1116. * Perform a IOAT transaction to verify the HW works.
  1117. */
  1118. #define IOAT_TEST_SIZE 2000
  1119. static void ioat_dma_test_callback(void *dma_async_param)
  1120. {
  1121. struct completion *cmp = dma_async_param;
  1122. complete(cmp);
  1123. }
  1124. /**
  1125. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  1126. * @device: device to be tested
  1127. */
  1128. static int ioat_dma_self_test(struct ioatdma_device *device)
  1129. {
  1130. int i;
  1131. u8 *src;
  1132. u8 *dest;
  1133. struct dma_device *dma = &device->common;
  1134. struct device *dev = &device->pdev->dev;
  1135. struct dma_chan *dma_chan;
  1136. struct dma_async_tx_descriptor *tx;
  1137. dma_addr_t dma_dest, dma_src;
  1138. dma_cookie_t cookie;
  1139. int err = 0;
  1140. struct completion cmp;
  1141. unsigned long tmo;
  1142. unsigned long flags;
  1143. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  1144. if (!src)
  1145. return -ENOMEM;
  1146. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  1147. if (!dest) {
  1148. kfree(src);
  1149. return -ENOMEM;
  1150. }
  1151. /* Fill in src buffer */
  1152. for (i = 0; i < IOAT_TEST_SIZE; i++)
  1153. src[i] = (u8)i;
  1154. /* Start copy, using first DMA channel */
  1155. dma_chan = container_of(dma->channels.next, struct dma_chan,
  1156. device_node);
  1157. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  1158. dev_err(dev, "selftest cannot allocate chan resource\n");
  1159. err = -ENODEV;
  1160. goto out;
  1161. }
  1162. dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
  1163. dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
  1164. flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE;
  1165. tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
  1166. IOAT_TEST_SIZE, flags);
  1167. if (!tx) {
  1168. dev_err(dev, "Self-test prep failed, disabling\n");
  1169. err = -ENODEV;
  1170. goto free_resources;
  1171. }
  1172. async_tx_ack(tx);
  1173. init_completion(&cmp);
  1174. tx->callback = ioat_dma_test_callback;
  1175. tx->callback_param = &cmp;
  1176. cookie = tx->tx_submit(tx);
  1177. if (cookie < 0) {
  1178. dev_err(dev, "Self-test setup failed, disabling\n");
  1179. err = -ENODEV;
  1180. goto free_resources;
  1181. }
  1182. dma->device_issue_pending(dma_chan);
  1183. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  1184. if (tmo == 0 ||
  1185. dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
  1186. != DMA_SUCCESS) {
  1187. dev_err(dev, "Self-test copy timed out, disabling\n");
  1188. err = -ENODEV;
  1189. goto free_resources;
  1190. }
  1191. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  1192. dev_err(dev, "Self-test copy failed compare, disabling\n");
  1193. err = -ENODEV;
  1194. goto free_resources;
  1195. }
  1196. free_resources:
  1197. dma->device_free_chan_resources(dma_chan);
  1198. out:
  1199. kfree(src);
  1200. kfree(dest);
  1201. return err;
  1202. }
  1203. static char ioat_interrupt_style[32] = "msix";
  1204. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  1205. sizeof(ioat_interrupt_style), 0644);
  1206. MODULE_PARM_DESC(ioat_interrupt_style,
  1207. "set ioat interrupt style: msix (default), "
  1208. "msix-single-vector, msi, intx)");
  1209. /**
  1210. * ioat_dma_setup_interrupts - setup interrupt handler
  1211. * @device: ioat device
  1212. */
  1213. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  1214. {
  1215. struct ioat_dma_chan *ioat_chan;
  1216. struct pci_dev *pdev = device->pdev;
  1217. struct device *dev = &pdev->dev;
  1218. struct msix_entry *msix;
  1219. int i, j, msixcnt;
  1220. int err = -EINVAL;
  1221. u8 intrctrl = 0;
  1222. if (!strcmp(ioat_interrupt_style, "msix"))
  1223. goto msix;
  1224. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  1225. goto msix_single_vector;
  1226. if (!strcmp(ioat_interrupt_style, "msi"))
  1227. goto msi;
  1228. if (!strcmp(ioat_interrupt_style, "intx"))
  1229. goto intx;
  1230. dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
  1231. goto err_no_irq;
  1232. msix:
  1233. /* The number of MSI-X vectors should equal the number of channels */
  1234. msixcnt = device->common.chancnt;
  1235. for (i = 0; i < msixcnt; i++)
  1236. device->msix_entries[i].entry = i;
  1237. err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
  1238. if (err < 0)
  1239. goto msi;
  1240. if (err > 0)
  1241. goto msix_single_vector;
  1242. for (i = 0; i < msixcnt; i++) {
  1243. msix = &device->msix_entries[i];
  1244. ioat_chan = ioat_chan_by_index(device, i);
  1245. err = devm_request_irq(dev, msix->vector,
  1246. ioat_dma_do_interrupt_msix, 0,
  1247. "ioat-msix", ioat_chan);
  1248. if (err) {
  1249. for (j = 0; j < i; j++) {
  1250. msix = &device->msix_entries[j];
  1251. ioat_chan = ioat_chan_by_index(device, j);
  1252. devm_free_irq(dev, msix->vector, ioat_chan);
  1253. }
  1254. goto msix_single_vector;
  1255. }
  1256. }
  1257. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  1258. goto done;
  1259. msix_single_vector:
  1260. msix = &device->msix_entries[0];
  1261. msix->entry = 0;
  1262. err = pci_enable_msix(pdev, device->msix_entries, 1);
  1263. if (err)
  1264. goto msi;
  1265. err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
  1266. "ioat-msix", device);
  1267. if (err) {
  1268. pci_disable_msix(pdev);
  1269. goto msi;
  1270. }
  1271. goto done;
  1272. msi:
  1273. err = pci_enable_msi(pdev);
  1274. if (err)
  1275. goto intx;
  1276. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
  1277. "ioat-msi", device);
  1278. if (err) {
  1279. pci_disable_msi(pdev);
  1280. goto intx;
  1281. }
  1282. goto done;
  1283. intx:
  1284. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
  1285. IRQF_SHARED, "ioat-intx", device);
  1286. if (err)
  1287. goto err_no_irq;
  1288. done:
  1289. if (device->intr_quirk)
  1290. device->intr_quirk(device);
  1291. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  1292. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1293. return 0;
  1294. err_no_irq:
  1295. /* Disable all interrupt generation */
  1296. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1297. dev_err(dev, "no usable interrupts\n");
  1298. return err;
  1299. }
  1300. static void ioat_disable_interrupts(struct ioatdma_device *device)
  1301. {
  1302. /* Disable all interrupt generation */
  1303. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1304. }
  1305. static int ioat_probe(struct ioatdma_device *device)
  1306. {
  1307. int err = -ENODEV;
  1308. struct dma_device *dma = &device->common;
  1309. struct pci_dev *pdev = device->pdev;
  1310. struct device *dev = &pdev->dev;
  1311. /* DMA coherent memory pool for DMA descriptor allocations */
  1312. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  1313. sizeof(struct ioat_dma_descriptor),
  1314. 64, 0);
  1315. if (!device->dma_pool) {
  1316. err = -ENOMEM;
  1317. goto err_dma_pool;
  1318. }
  1319. device->completion_pool = pci_pool_create("completion_pool", pdev,
  1320. sizeof(u64), SMP_CACHE_BYTES,
  1321. SMP_CACHE_BYTES);
  1322. if (!device->completion_pool) {
  1323. err = -ENOMEM;
  1324. goto err_completion_pool;
  1325. }
  1326. ioat_dma_enumerate_channels(device);
  1327. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  1328. dma->device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
  1329. dma->device_free_chan_resources = ioat_dma_free_chan_resources;
  1330. dma->device_is_tx_complete = ioat_dma_is_complete;
  1331. dma->dev = &pdev->dev;
  1332. dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
  1333. " %d channels, device version 0x%02x, driver version %s\n",
  1334. dma->chancnt, device->version, IOAT_DMA_VERSION);
  1335. if (!dma->chancnt) {
  1336. dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
  1337. "zero channels detected\n");
  1338. goto err_setup_interrupts;
  1339. }
  1340. err = ioat_dma_setup_interrupts(device);
  1341. if (err)
  1342. goto err_setup_interrupts;
  1343. err = ioat_dma_self_test(device);
  1344. if (err)
  1345. goto err_self_test;
  1346. return 0;
  1347. err_self_test:
  1348. ioat_disable_interrupts(device);
  1349. err_setup_interrupts:
  1350. pci_pool_destroy(device->completion_pool);
  1351. err_completion_pool:
  1352. pci_pool_destroy(device->dma_pool);
  1353. err_dma_pool:
  1354. return err;
  1355. }
  1356. static int ioat_register(struct ioatdma_device *device)
  1357. {
  1358. int err = dma_async_device_register(&device->common);
  1359. if (err) {
  1360. ioat_disable_interrupts(device);
  1361. pci_pool_destroy(device->completion_pool);
  1362. pci_pool_destroy(device->dma_pool);
  1363. }
  1364. return err;
  1365. }
  1366. /* ioat1_intr_quirk - fix up dma ctrl register to enable / disable msi */
  1367. static void ioat1_intr_quirk(struct ioatdma_device *device)
  1368. {
  1369. struct pci_dev *pdev = device->pdev;
  1370. u32 dmactrl;
  1371. pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  1372. if (pdev->msi_enabled)
  1373. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  1374. else
  1375. dmactrl &= ~IOAT_PCI_DMACTRL_MSI_EN;
  1376. pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  1377. }
  1378. int ioat1_dma_probe(struct ioatdma_device *device, int dca)
  1379. {
  1380. struct pci_dev *pdev = device->pdev;
  1381. struct dma_device *dma;
  1382. int err;
  1383. device->intr_quirk = ioat1_intr_quirk;
  1384. dma = &device->common;
  1385. dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
  1386. dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
  1387. err = ioat_probe(device);
  1388. if (err)
  1389. return err;
  1390. ioat_set_tcp_copy_break(4096);
  1391. err = ioat_register(device);
  1392. if (err)
  1393. return err;
  1394. if (dca)
  1395. device->dca = ioat_dca_init(pdev, device->reg_base);
  1396. INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
  1397. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  1398. return err;
  1399. }
  1400. int ioat2_dma_probe(struct ioatdma_device *device, int dca)
  1401. {
  1402. struct pci_dev *pdev = device->pdev;
  1403. struct dma_device *dma;
  1404. struct dma_chan *chan;
  1405. struct ioat_dma_chan *ioat_chan;
  1406. int err;
  1407. dma = &device->common;
  1408. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1409. dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
  1410. err = ioat_probe(device);
  1411. if (err)
  1412. return err;
  1413. ioat_set_tcp_copy_break(2048);
  1414. list_for_each_entry(chan, &dma->channels, device_node) {
  1415. ioat_chan = to_ioat_chan(chan);
  1416. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
  1417. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  1418. }
  1419. err = ioat_register(device);
  1420. if (err)
  1421. return err;
  1422. if (dca)
  1423. device->dca = ioat2_dca_init(pdev, device->reg_base);
  1424. INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
  1425. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  1426. return err;
  1427. }
  1428. int ioat3_dma_probe(struct ioatdma_device *device, int dca)
  1429. {
  1430. struct pci_dev *pdev = device->pdev;
  1431. struct dma_device *dma;
  1432. struct dma_chan *chan;
  1433. struct ioat_dma_chan *ioat_chan;
  1434. int err;
  1435. u16 dev_id;
  1436. dma = &device->common;
  1437. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1438. dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
  1439. /* -= IOAT ver.3 workarounds =- */
  1440. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  1441. * that can cause stability issues for IOAT ver.3
  1442. */
  1443. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  1444. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1445. * (workaround for spurious config parity error after restart)
  1446. */
  1447. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1448. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  1449. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  1450. err = ioat_probe(device);
  1451. if (err)
  1452. return err;
  1453. ioat_set_tcp_copy_break(262144);
  1454. list_for_each_entry(chan, &dma->channels, device_node) {
  1455. ioat_chan = to_ioat_chan(chan);
  1456. writel(IOAT_DMA_DCA_ANY_CPU,
  1457. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  1458. }
  1459. err = ioat_register(device);
  1460. if (err)
  1461. return err;
  1462. if (dca)
  1463. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1464. return err;
  1465. }
  1466. void ioat_dma_remove(struct ioatdma_device *device)
  1467. {
  1468. struct dma_chan *chan, *_chan;
  1469. struct ioat_dma_chan *ioat_chan;
  1470. struct dma_device *dma = &device->common;
  1471. if (device->version != IOAT_VER_3_0)
  1472. cancel_delayed_work(&device->work);
  1473. ioat_disable_interrupts(device);
  1474. dma_async_device_unregister(dma);
  1475. pci_pool_destroy(device->dma_pool);
  1476. pci_pool_destroy(device->completion_pool);
  1477. list_for_each_entry_safe(chan, _chan, &dma->channels, device_node) {
  1478. ioat_chan = to_ioat_chan(chan);
  1479. list_del(&chan->device_node);
  1480. }
  1481. }