ioat_dma.c 47 KB

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