ioat_dma.c 47 KB

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