dma_v2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2009 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. /*
  23. * This driver supports an Intel I/OAT DMA engine (versions >= 2), which
  24. * does asynchronous data movement and checksumming operations.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/i7300_idle.h>
  35. #include "dma.h"
  36. #include "dma_v2.h"
  37. #include "registers.h"
  38. #include "hw.h"
  39. static int ioat_ring_alloc_order = 8;
  40. module_param(ioat_ring_alloc_order, int, 0644);
  41. MODULE_PARM_DESC(ioat_ring_alloc_order,
  42. "ioat2+: allocate 2^n descriptors per channel (default: n=8)");
  43. static void __ioat2_issue_pending(struct ioat2_dma_chan *ioat)
  44. {
  45. void * __iomem reg_base = ioat->base.reg_base;
  46. ioat->pending = 0;
  47. ioat->dmacount += ioat2_ring_pending(ioat);;
  48. ioat->issued = ioat->head;
  49. /* make descriptor updates globally visible before notifying channel */
  50. wmb();
  51. writew(ioat->dmacount, reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  52. dev_dbg(to_dev(&ioat->base),
  53. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  54. __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
  55. }
  56. static void ioat2_issue_pending(struct dma_chan *chan)
  57. {
  58. struct ioat2_dma_chan *ioat = to_ioat2_chan(chan);
  59. spin_lock_bh(&ioat->ring_lock);
  60. if (ioat->pending == 1)
  61. __ioat2_issue_pending(ioat);
  62. spin_unlock_bh(&ioat->ring_lock);
  63. }
  64. /**
  65. * ioat2_update_pending - log pending descriptors
  66. * @ioat: ioat2+ channel
  67. *
  68. * set pending to '1' unless pending is already set to '2', pending == 2
  69. * indicates that submission is temporarily blocked due to an in-flight
  70. * reset. If we are already above the ioat_pending_level threshold then
  71. * just issue pending.
  72. *
  73. * called with ring_lock held
  74. */
  75. static void ioat2_update_pending(struct ioat2_dma_chan *ioat)
  76. {
  77. if (unlikely(ioat->pending == 2))
  78. return;
  79. else if (ioat2_ring_pending(ioat) > ioat_pending_level)
  80. __ioat2_issue_pending(ioat);
  81. else
  82. ioat->pending = 1;
  83. }
  84. static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  85. {
  86. struct ioat_ring_ent *desc;
  87. struct ioat_dma_descriptor *hw;
  88. int idx;
  89. if (ioat2_ring_space(ioat) < 1) {
  90. dev_err(to_dev(&ioat->base),
  91. "Unable to start null desc - ring full\n");
  92. return;
  93. }
  94. dev_dbg(to_dev(&ioat->base), "%s: head: %#x tail: %#x issued: %#x\n",
  95. __func__, ioat->head, ioat->tail, ioat->issued);
  96. idx = ioat2_desc_alloc(ioat, 1);
  97. desc = ioat2_get_ring_ent(ioat, idx);
  98. hw = desc->hw;
  99. hw->ctl = 0;
  100. hw->ctl_f.null = 1;
  101. hw->ctl_f.int_en = 1;
  102. hw->ctl_f.compl_write = 1;
  103. /* set size to non-zero value (channel returns error when size is 0) */
  104. hw->size = NULL_DESC_BUFFER_SIZE;
  105. hw->src_addr = 0;
  106. hw->dst_addr = 0;
  107. async_tx_ack(&desc->txd);
  108. ioat2_set_chainaddr(ioat, desc->txd.phys);
  109. dump_desc_dbg(ioat, desc);
  110. __ioat2_issue_pending(ioat);
  111. }
  112. static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  113. {
  114. spin_lock_bh(&ioat->ring_lock);
  115. __ioat2_start_null_desc(ioat);
  116. spin_unlock_bh(&ioat->ring_lock);
  117. }
  118. static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
  119. {
  120. struct ioat_chan_common *chan = &ioat->base;
  121. struct dma_async_tx_descriptor *tx;
  122. struct ioat_ring_ent *desc;
  123. bool seen_current = false;
  124. u16 active;
  125. int i;
  126. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  127. __func__, ioat->head, ioat->tail, ioat->issued);
  128. active = ioat2_ring_active(ioat);
  129. for (i = 0; i < active && !seen_current; i++) {
  130. prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
  131. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  132. tx = &desc->txd;
  133. dump_desc_dbg(ioat, desc);
  134. if (tx->cookie) {
  135. ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
  136. chan->completed_cookie = tx->cookie;
  137. tx->cookie = 0;
  138. if (tx->callback) {
  139. tx->callback(tx->callback_param);
  140. tx->callback = NULL;
  141. }
  142. }
  143. if (tx->phys == phys_complete)
  144. seen_current = true;
  145. }
  146. ioat->tail += i;
  147. BUG_ON(!seen_current); /* no active descs have written a completion? */
  148. chan->last_completion = phys_complete;
  149. if (ioat->head == ioat->tail) {
  150. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  151. __func__);
  152. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  153. }
  154. }
  155. /**
  156. * ioat2_cleanup - clean finished descriptors (advance tail pointer)
  157. * @chan: ioat channel to be cleaned up
  158. */
  159. static void ioat2_cleanup(struct ioat2_dma_chan *ioat)
  160. {
  161. struct ioat_chan_common *chan = &ioat->base;
  162. unsigned long phys_complete;
  163. prefetch(chan->completion);
  164. if (!spin_trylock_bh(&chan->cleanup_lock))
  165. return;
  166. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  167. spin_unlock_bh(&chan->cleanup_lock);
  168. return;
  169. }
  170. if (!spin_trylock_bh(&ioat->ring_lock)) {
  171. spin_unlock_bh(&chan->cleanup_lock);
  172. return;
  173. }
  174. __cleanup(ioat, phys_complete);
  175. spin_unlock_bh(&ioat->ring_lock);
  176. spin_unlock_bh(&chan->cleanup_lock);
  177. }
  178. static void ioat2_cleanup_tasklet(unsigned long data)
  179. {
  180. struct ioat2_dma_chan *ioat = (void *) data;
  181. ioat2_cleanup(ioat);
  182. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  183. }
  184. static void __restart_chan(struct ioat2_dma_chan *ioat)
  185. {
  186. struct ioat_chan_common *chan = &ioat->base;
  187. /* set the tail to be re-issued */
  188. ioat->issued = ioat->tail;
  189. ioat->dmacount = 0;
  190. set_bit(IOAT_COMPLETION_PENDING, &chan->state);
  191. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  192. dev_dbg(to_dev(chan),
  193. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  194. __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
  195. if (ioat2_ring_pending(ioat)) {
  196. struct ioat_ring_ent *desc;
  197. desc = ioat2_get_ring_ent(ioat, ioat->tail);
  198. ioat2_set_chainaddr(ioat, desc->txd.phys);
  199. __ioat2_issue_pending(ioat);
  200. } else
  201. __ioat2_start_null_desc(ioat);
  202. }
  203. static void ioat2_restart_channel(struct ioat2_dma_chan *ioat)
  204. {
  205. struct ioat_chan_common *chan = &ioat->base;
  206. unsigned long phys_complete;
  207. u32 status;
  208. status = ioat_chansts(chan);
  209. if (is_ioat_active(status) || is_ioat_idle(status))
  210. ioat_suspend(chan);
  211. while (is_ioat_active(status) || is_ioat_idle(status)) {
  212. status = ioat_chansts(chan);
  213. cpu_relax();
  214. }
  215. if (ioat_cleanup_preamble(chan, &phys_complete))
  216. __cleanup(ioat, phys_complete);
  217. __restart_chan(ioat);
  218. }
  219. static void ioat2_timer_event(unsigned long data)
  220. {
  221. struct ioat2_dma_chan *ioat = (void *) data;
  222. struct ioat_chan_common *chan = &ioat->base;
  223. spin_lock_bh(&chan->cleanup_lock);
  224. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  225. unsigned long phys_complete;
  226. u64 status;
  227. spin_lock_bh(&ioat->ring_lock);
  228. status = ioat_chansts(chan);
  229. /* when halted due to errors check for channel
  230. * programming errors before advancing the completion state
  231. */
  232. if (is_ioat_halted(status)) {
  233. u32 chanerr;
  234. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  235. BUG_ON(is_ioat_bug(chanerr));
  236. }
  237. /* if we haven't made progress and we have already
  238. * acknowledged a pending completion once, then be more
  239. * forceful with a restart
  240. */
  241. if (ioat_cleanup_preamble(chan, &phys_complete))
  242. __cleanup(ioat, phys_complete);
  243. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
  244. ioat2_restart_channel(ioat);
  245. else {
  246. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  247. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  248. }
  249. spin_unlock_bh(&ioat->ring_lock);
  250. }
  251. spin_unlock_bh(&chan->cleanup_lock);
  252. }
  253. /**
  254. * ioat2_enumerate_channels - find and initialize the device's channels
  255. * @device: the device to be enumerated
  256. */
  257. static int ioat2_enumerate_channels(struct ioatdma_device *device)
  258. {
  259. struct ioat2_dma_chan *ioat;
  260. struct device *dev = &device->pdev->dev;
  261. struct dma_device *dma = &device->common;
  262. u8 xfercap_log;
  263. int i;
  264. INIT_LIST_HEAD(&dma->channels);
  265. dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  266. dma->chancnt &= 0x1f; /* bits [4:0] valid */
  267. if (dma->chancnt > ARRAY_SIZE(device->idx)) {
  268. dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
  269. dma->chancnt, ARRAY_SIZE(device->idx));
  270. dma->chancnt = ARRAY_SIZE(device->idx);
  271. }
  272. xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  273. xfercap_log &= 0x1f; /* bits [4:0] valid */
  274. if (xfercap_log == 0)
  275. return 0;
  276. dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
  277. /* FIXME which i/oat version is i7300? */
  278. #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
  279. if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
  280. dma->chancnt--;
  281. #endif
  282. for (i = 0; i < dma->chancnt; i++) {
  283. ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
  284. if (!ioat)
  285. break;
  286. ioat_init_channel(device, &ioat->base, i,
  287. ioat2_timer_event,
  288. ioat2_cleanup_tasklet,
  289. (unsigned long) ioat);
  290. ioat->xfercap_log = xfercap_log;
  291. spin_lock_init(&ioat->ring_lock);
  292. }
  293. dma->chancnt = i;
  294. return i;
  295. }
  296. static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  297. {
  298. struct dma_chan *c = tx->chan;
  299. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  300. struct ioat_chan_common *chan = &ioat->base;
  301. dma_cookie_t cookie = c->cookie;
  302. cookie++;
  303. if (cookie < 0)
  304. cookie = 1;
  305. tx->cookie = cookie;
  306. c->cookie = cookie;
  307. dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
  308. if (!test_and_set_bit(IOAT_COMPLETION_PENDING, &chan->state))
  309. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  310. ioat2_update_pending(ioat);
  311. spin_unlock_bh(&ioat->ring_lock);
  312. return cookie;
  313. }
  314. static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan)
  315. {
  316. struct ioat_dma_descriptor *hw;
  317. struct ioat_ring_ent *desc;
  318. struct ioatdma_device *dma;
  319. dma_addr_t phys;
  320. dma = to_ioatdma_device(chan->device);
  321. hw = pci_pool_alloc(dma->dma_pool, GFP_KERNEL, &phys);
  322. if (!hw)
  323. return NULL;
  324. memset(hw, 0, sizeof(*hw));
  325. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  326. if (!desc) {
  327. pci_pool_free(dma->dma_pool, hw, phys);
  328. return NULL;
  329. }
  330. dma_async_tx_descriptor_init(&desc->txd, chan);
  331. desc->txd.tx_submit = ioat2_tx_submit_unlock;
  332. desc->hw = hw;
  333. desc->txd.phys = phys;
  334. return desc;
  335. }
  336. static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
  337. {
  338. struct ioatdma_device *dma;
  339. dma = to_ioatdma_device(chan->device);
  340. pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
  341. kfree(desc);
  342. }
  343. /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
  344. * @chan: channel to be initialized
  345. */
  346. static int ioat2_alloc_chan_resources(struct dma_chan *c)
  347. {
  348. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  349. struct ioat_chan_common *chan = &ioat->base;
  350. struct ioat_ring_ent **ring;
  351. u32 chanerr;
  352. int descs;
  353. int i;
  354. /* have we already been set up? */
  355. if (ioat->ring)
  356. return 1 << ioat->alloc_order;
  357. /* Setup register to interrupt and write completion status on error */
  358. writew(IOAT_CHANCTRL_RUN, chan->reg_base + IOAT_CHANCTRL_OFFSET);
  359. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  360. if (chanerr) {
  361. dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
  362. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  363. }
  364. /* allocate a completion writeback area */
  365. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  366. chan->completion = pci_pool_alloc(chan->device->completion_pool,
  367. GFP_KERNEL, &chan->completion_dma);
  368. if (!chan->completion)
  369. return -ENOMEM;
  370. memset(chan->completion, 0, sizeof(*chan->completion));
  371. writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
  372. chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  373. writel(((u64) chan->completion_dma) >> 32,
  374. chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  375. ioat->alloc_order = ioat_get_alloc_order();
  376. descs = 1 << ioat->alloc_order;
  377. /* allocate the array to hold the software ring */
  378. ring = kcalloc(descs, sizeof(*ring), GFP_KERNEL);
  379. if (!ring)
  380. return -ENOMEM;
  381. for (i = 0; i < descs; i++) {
  382. ring[i] = ioat2_alloc_ring_ent(c);
  383. if (!ring[i]) {
  384. while (i--)
  385. ioat2_free_ring_ent(ring[i], c);
  386. kfree(ring);
  387. return -ENOMEM;
  388. }
  389. set_desc_id(ring[i], i);
  390. }
  391. /* link descs */
  392. for (i = 0; i < descs-1; i++) {
  393. struct ioat_ring_ent *next = ring[i+1];
  394. struct ioat_dma_descriptor *hw = ring[i]->hw;
  395. hw->next = next->txd.phys;
  396. }
  397. ring[i]->hw->next = ring[0]->txd.phys;
  398. spin_lock_bh(&ioat->ring_lock);
  399. ioat->ring = ring;
  400. ioat->head = 0;
  401. ioat->issued = 0;
  402. ioat->tail = 0;
  403. ioat->pending = 0;
  404. spin_unlock_bh(&ioat->ring_lock);
  405. tasklet_enable(&chan->cleanup_task);
  406. ioat2_start_null_desc(ioat);
  407. return descs;
  408. }
  409. /**
  410. * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
  411. * @idx: gets starting descriptor index on successful allocation
  412. * @ioat: ioat2,3 channel (ring) to operate on
  413. * @num_descs: allocation length
  414. */
  415. static int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
  416. {
  417. struct ioat_chan_common *chan = &ioat->base;
  418. spin_lock_bh(&ioat->ring_lock);
  419. if (unlikely(ioat2_ring_space(ioat) < num_descs)) {
  420. if (printk_ratelimit())
  421. dev_dbg(to_dev(chan),
  422. "%s: ring full! num_descs: %d (%x:%x:%x)\n",
  423. __func__, num_descs, ioat->head, ioat->tail,
  424. ioat->issued);
  425. spin_unlock_bh(&ioat->ring_lock);
  426. /* progress reclaim in the allocation failure case we
  427. * may be called under bh_disabled so we need to trigger
  428. * the timer event directly
  429. */
  430. spin_lock_bh(&chan->cleanup_lock);
  431. if (jiffies > chan->timer.expires &&
  432. timer_pending(&chan->timer)) {
  433. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  434. spin_unlock_bh(&chan->cleanup_lock);
  435. ioat2_timer_event((unsigned long) ioat);
  436. } else
  437. spin_unlock_bh(&chan->cleanup_lock);
  438. return -ENOMEM;
  439. }
  440. dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
  441. __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
  442. *idx = ioat2_desc_alloc(ioat, num_descs);
  443. return 0; /* with ioat->ring_lock held */
  444. }
  445. static struct dma_async_tx_descriptor *
  446. ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
  447. dma_addr_t dma_src, size_t len, unsigned long flags)
  448. {
  449. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  450. struct ioat_dma_descriptor *hw;
  451. struct ioat_ring_ent *desc;
  452. dma_addr_t dst = dma_dest;
  453. dma_addr_t src = dma_src;
  454. size_t total_len = len;
  455. int num_descs;
  456. u16 idx;
  457. int i;
  458. num_descs = ioat2_xferlen_to_descs(ioat, len);
  459. if (likely(num_descs) &&
  460. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  461. /* pass */;
  462. else
  463. return NULL;
  464. for (i = 0; i < num_descs; i++) {
  465. size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
  466. desc = ioat2_get_ring_ent(ioat, idx + i);
  467. hw = desc->hw;
  468. hw->size = copy;
  469. hw->ctl = 0;
  470. hw->src_addr = src;
  471. hw->dst_addr = dst;
  472. len -= copy;
  473. dst += copy;
  474. src += copy;
  475. dump_desc_dbg(ioat, desc);
  476. }
  477. desc->txd.flags = flags;
  478. desc->len = total_len;
  479. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  480. hw->ctl_f.compl_write = 1;
  481. dump_desc_dbg(ioat, desc);
  482. /* we leave the channel locked to ensure in order submission */
  483. return &desc->txd;
  484. }
  485. /**
  486. * ioat2_free_chan_resources - release all the descriptors
  487. * @chan: the channel to be cleaned
  488. */
  489. static void ioat2_free_chan_resources(struct dma_chan *c)
  490. {
  491. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  492. struct ioat_chan_common *chan = &ioat->base;
  493. struct ioatdma_device *ioatdma_device = chan->device;
  494. struct ioat_ring_ent *desc;
  495. const u16 total_descs = 1 << ioat->alloc_order;
  496. int descs;
  497. int i;
  498. /* Before freeing channel resources first check
  499. * if they have been previously allocated for this channel.
  500. */
  501. if (!ioat->ring)
  502. return;
  503. tasklet_disable(&chan->cleanup_task);
  504. del_timer_sync(&chan->timer);
  505. ioat2_cleanup(ioat);
  506. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  507. * before removing DMA descriptor resources.
  508. */
  509. writeb(IOAT_CHANCMD_RESET,
  510. chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
  511. mdelay(100);
  512. spin_lock_bh(&ioat->ring_lock);
  513. descs = ioat2_ring_space(ioat);
  514. dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs);
  515. for (i = 0; i < descs; i++) {
  516. desc = ioat2_get_ring_ent(ioat, ioat->head + i);
  517. ioat2_free_ring_ent(desc, c);
  518. }
  519. if (descs < total_descs)
  520. dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
  521. total_descs - descs);
  522. for (i = 0; i < total_descs - descs; i++) {
  523. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  524. dump_desc_dbg(ioat, desc);
  525. ioat2_free_ring_ent(desc, c);
  526. }
  527. kfree(ioat->ring);
  528. ioat->ring = NULL;
  529. ioat->alloc_order = 0;
  530. pci_pool_free(ioatdma_device->completion_pool,
  531. chan->completion,
  532. chan->completion_dma);
  533. spin_unlock_bh(&ioat->ring_lock);
  534. chan->last_completion = 0;
  535. chan->completion_dma = 0;
  536. ioat->pending = 0;
  537. ioat->dmacount = 0;
  538. }
  539. static enum dma_status
  540. ioat2_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  541. dma_cookie_t *done, dma_cookie_t *used)
  542. {
  543. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  544. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  545. return DMA_SUCCESS;
  546. ioat2_cleanup(ioat);
  547. return ioat_is_complete(c, cookie, done, used);
  548. }
  549. int __devinit ioat2_dma_probe(struct ioatdma_device *device, int dca)
  550. {
  551. struct pci_dev *pdev = device->pdev;
  552. struct dma_device *dma;
  553. struct dma_chan *c;
  554. struct ioat_chan_common *chan;
  555. int err;
  556. device->enumerate_channels = ioat2_enumerate_channels;
  557. dma = &device->common;
  558. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  559. dma->device_issue_pending = ioat2_issue_pending;
  560. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  561. dma->device_free_chan_resources = ioat2_free_chan_resources;
  562. dma->device_is_tx_complete = ioat2_is_complete;
  563. err = ioat_probe(device);
  564. if (err)
  565. return err;
  566. ioat_set_tcp_copy_break(2048);
  567. list_for_each_entry(c, &dma->channels, device_node) {
  568. chan = to_chan_common(c);
  569. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
  570. chan->reg_base + IOAT_DCACTRL_OFFSET);
  571. }
  572. err = ioat_register(device);
  573. if (err)
  574. return err;
  575. if (dca)
  576. device->dca = ioat2_dca_init(pdev, device->reg_base);
  577. return err;
  578. }
  579. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  580. {
  581. struct pci_dev *pdev = device->pdev;
  582. struct dma_device *dma;
  583. struct dma_chan *c;
  584. struct ioat_chan_common *chan;
  585. int err;
  586. u16 dev_id;
  587. device->enumerate_channels = ioat2_enumerate_channels;
  588. dma = &device->common;
  589. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  590. dma->device_issue_pending = ioat2_issue_pending;
  591. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  592. dma->device_free_chan_resources = ioat2_free_chan_resources;
  593. dma->device_is_tx_complete = ioat2_is_complete;
  594. /* -= IOAT ver.3 workarounds =- */
  595. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  596. * that can cause stability issues for IOAT ver.3
  597. */
  598. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  599. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  600. * (workaround for spurious config parity error after restart)
  601. */
  602. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  603. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  604. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  605. err = ioat_probe(device);
  606. if (err)
  607. return err;
  608. ioat_set_tcp_copy_break(262144);
  609. list_for_each_entry(c, &dma->channels, device_node) {
  610. chan = to_chan_common(c);
  611. writel(IOAT_DMA_DCA_ANY_CPU,
  612. chan->reg_base + IOAT_DCACTRL_OFFSET);
  613. }
  614. err = ioat_register(device);
  615. if (err)
  616. return err;
  617. if (dca)
  618. device->dca = ioat3_dca_init(pdev, device->reg_base);
  619. return err;
  620. }