ioat_dma.c 45 KB

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