dma_v2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. }
  53. static void ioat2_issue_pending(struct dma_chan *chan)
  54. {
  55. struct ioat2_dma_chan *ioat = to_ioat2_chan(chan);
  56. spin_lock_bh(&ioat->ring_lock);
  57. if (ioat->pending == 1)
  58. __ioat2_issue_pending(ioat);
  59. spin_unlock_bh(&ioat->ring_lock);
  60. }
  61. /**
  62. * ioat2_update_pending - log pending descriptors
  63. * @ioat: ioat2+ channel
  64. *
  65. * set pending to '1' unless pending is already set to '2', pending == 2
  66. * indicates that submission is temporarily blocked due to an in-flight
  67. * reset. If we are already above the ioat_pending_level threshold then
  68. * just issue pending.
  69. *
  70. * called with ring_lock held
  71. */
  72. static void ioat2_update_pending(struct ioat2_dma_chan *ioat)
  73. {
  74. if (unlikely(ioat->pending == 2))
  75. return;
  76. else if (ioat2_ring_pending(ioat) > ioat_pending_level)
  77. __ioat2_issue_pending(ioat);
  78. else
  79. ioat->pending = 1;
  80. }
  81. static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  82. {
  83. void __iomem *reg_base = ioat->base.reg_base;
  84. struct ioat_ring_ent *desc;
  85. struct ioat_dma_descriptor *hw;
  86. int idx;
  87. if (ioat2_ring_space(ioat) < 1) {
  88. dev_err(to_dev(&ioat->base),
  89. "Unable to start null desc - ring full\n");
  90. return;
  91. }
  92. idx = ioat2_desc_alloc(ioat, 1);
  93. desc = ioat2_get_ring_ent(ioat, idx);
  94. hw = desc->hw;
  95. hw->ctl = 0;
  96. hw->ctl_f.null = 1;
  97. hw->ctl_f.int_en = 1;
  98. hw->ctl_f.compl_write = 1;
  99. /* set size to non-zero value (channel returns error when size is 0) */
  100. hw->size = NULL_DESC_BUFFER_SIZE;
  101. hw->src_addr = 0;
  102. hw->dst_addr = 0;
  103. async_tx_ack(&desc->txd);
  104. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  105. reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  106. writel(((u64) desc->txd.phys) >> 32,
  107. reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  108. __ioat2_issue_pending(ioat);
  109. }
  110. static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
  111. {
  112. spin_lock_bh(&ioat->ring_lock);
  113. __ioat2_start_null_desc(ioat);
  114. spin_unlock_bh(&ioat->ring_lock);
  115. }
  116. static void ioat2_cleanup(struct ioat2_dma_chan *ioat);
  117. /**
  118. * ioat2_reset_part2 - reinit the channel after a reset
  119. */
  120. static void ioat2_reset_part2(struct work_struct *work)
  121. {
  122. struct ioat_chan_common *chan;
  123. struct ioat2_dma_chan *ioat;
  124. chan = container_of(work, struct ioat_chan_common, work.work);
  125. ioat = container_of(chan, struct ioat2_dma_chan, base);
  126. /* ensure that ->tail points to the stalled descriptor
  127. * (ioat->pending is set to 2 at this point so no new
  128. * descriptors will be issued while we perform this cleanup)
  129. */
  130. ioat2_cleanup(ioat);
  131. spin_lock_bh(&chan->cleanup_lock);
  132. spin_lock_bh(&ioat->ring_lock);
  133. /* set the tail to be re-issued */
  134. ioat->issued = ioat->tail;
  135. ioat->dmacount = 0;
  136. if (ioat2_ring_pending(ioat)) {
  137. struct ioat_ring_ent *desc;
  138. desc = ioat2_get_ring_ent(ioat, ioat->tail);
  139. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  140. chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  141. writel(((u64) desc->txd.phys) >> 32,
  142. chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  143. __ioat2_issue_pending(ioat);
  144. } else
  145. __ioat2_start_null_desc(ioat);
  146. spin_unlock_bh(&ioat->ring_lock);
  147. spin_unlock_bh(&chan->cleanup_lock);
  148. dev_info(to_dev(chan),
  149. "chan%d reset - %d descs waiting, %d total desc\n",
  150. chan_num(chan), ioat->dmacount, 1 << ioat->alloc_order);
  151. }
  152. /**
  153. * ioat2_reset_channel - restart a channel
  154. * @ioat: IOAT DMA channel handle
  155. */
  156. static void ioat2_reset_channel(struct ioat2_dma_chan *ioat)
  157. {
  158. u32 chansts, chanerr;
  159. struct ioat_chan_common *chan = &ioat->base;
  160. u16 active;
  161. spin_lock_bh(&ioat->ring_lock);
  162. active = ioat2_ring_active(ioat);
  163. spin_unlock_bh(&ioat->ring_lock);
  164. if (!active)
  165. return;
  166. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  167. chansts = (chan->completion_virt->low
  168. & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
  169. if (chanerr) {
  170. dev_err(to_dev(chan),
  171. "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
  172. chan_num(chan), chansts, chanerr);
  173. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  174. }
  175. spin_lock_bh(&ioat->ring_lock);
  176. ioat->pending = 2;
  177. writeb(IOAT_CHANCMD_RESET,
  178. chan->reg_base
  179. + IOAT_CHANCMD_OFFSET(chan->device->version));
  180. spin_unlock_bh(&ioat->ring_lock);
  181. schedule_delayed_work(&chan->work, RESET_DELAY);
  182. }
  183. /**
  184. * ioat2_chan_watchdog - watch for stuck channels
  185. */
  186. static void ioat2_chan_watchdog(struct work_struct *work)
  187. {
  188. struct ioatdma_device *device =
  189. container_of(work, struct ioatdma_device, work.work);
  190. struct ioat2_dma_chan *ioat;
  191. struct ioat_chan_common *chan;
  192. u16 active;
  193. int i;
  194. for (i = 0; i < device->common.chancnt; i++) {
  195. chan = ioat_chan_by_index(device, i);
  196. ioat = container_of(chan, struct ioat2_dma_chan, base);
  197. /*
  198. * for version 2.0 if there are descriptors yet to be processed
  199. * and the last completed hasn't changed since the last watchdog
  200. * if they haven't hit the pending level
  201. * issue the pending to push them through
  202. * else
  203. * try resetting the channel
  204. */
  205. spin_lock_bh(&ioat->ring_lock);
  206. active = ioat2_ring_active(ioat);
  207. spin_unlock_bh(&ioat->ring_lock);
  208. if (active &&
  209. chan->last_completion &&
  210. chan->last_completion == chan->watchdog_completion) {
  211. if (ioat->pending == 1)
  212. ioat2_issue_pending(&chan->common);
  213. else {
  214. ioat2_reset_channel(ioat);
  215. chan->watchdog_completion = 0;
  216. }
  217. } else {
  218. chan->last_compl_desc_addr_hw = 0;
  219. chan->watchdog_completion = chan->last_completion;
  220. }
  221. chan->watchdog_last_tcp_cookie = chan->watchdog_tcp_cookie;
  222. }
  223. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  224. }
  225. /**
  226. * ioat2_cleanup - clean finished descriptors (advance tail pointer)
  227. * @chan: ioat channel to be cleaned up
  228. */
  229. static void ioat2_cleanup(struct ioat2_dma_chan *ioat)
  230. {
  231. struct ioat_chan_common *chan = &ioat->base;
  232. unsigned long phys_complete;
  233. struct ioat_ring_ent *desc;
  234. bool seen_current = false;
  235. u16 active;
  236. int i;
  237. struct dma_async_tx_descriptor *tx;
  238. prefetch(chan->completion_virt);
  239. spin_lock_bh(&chan->cleanup_lock);
  240. phys_complete = ioat_get_current_completion(chan);
  241. if (phys_complete == chan->last_completion) {
  242. spin_unlock_bh(&chan->cleanup_lock);
  243. /*
  244. * perhaps we're stuck so hard that the watchdog can't go off?
  245. * try to catch it after WATCHDOG_DELAY seconds
  246. */
  247. if (chan->device->version < IOAT_VER_3_0) {
  248. unsigned long tmo;
  249. tmo = chan->last_completion_time + HZ*WATCHDOG_DELAY;
  250. if (time_after(jiffies, tmo)) {
  251. ioat2_chan_watchdog(&(chan->device->work.work));
  252. chan->last_completion_time = jiffies;
  253. }
  254. }
  255. return;
  256. }
  257. chan->last_completion_time = jiffies;
  258. spin_lock_bh(&ioat->ring_lock);
  259. active = ioat2_ring_active(ioat);
  260. for (i = 0; i < active && !seen_current; i++) {
  261. prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
  262. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  263. tx = &desc->txd;
  264. if (tx->cookie) {
  265. ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
  266. chan->completed_cookie = tx->cookie;
  267. tx->cookie = 0;
  268. if (tx->callback) {
  269. tx->callback(tx->callback_param);
  270. tx->callback = NULL;
  271. }
  272. }
  273. if (tx->phys == phys_complete)
  274. seen_current = true;
  275. }
  276. ioat->tail += i;
  277. BUG_ON(!seen_current); /* no active descs have written a completion? */
  278. spin_unlock_bh(&ioat->ring_lock);
  279. chan->last_completion = phys_complete;
  280. spin_unlock_bh(&chan->cleanup_lock);
  281. }
  282. static void ioat2_cleanup_tasklet(unsigned long data)
  283. {
  284. struct ioat2_dma_chan *ioat = (void *) data;
  285. ioat2_cleanup(ioat);
  286. writew(IOAT_CHANCTRL_INT_DISABLE,
  287. ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  288. }
  289. /**
  290. * ioat2_enumerate_channels - find and initialize the device's channels
  291. * @device: the device to be enumerated
  292. */
  293. static int ioat2_enumerate_channels(struct ioatdma_device *device)
  294. {
  295. struct ioat2_dma_chan *ioat;
  296. struct device *dev = &device->pdev->dev;
  297. struct dma_device *dma = &device->common;
  298. u8 xfercap_log;
  299. int i;
  300. INIT_LIST_HEAD(&dma->channels);
  301. dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  302. xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  303. if (xfercap_log == 0)
  304. return 0;
  305. /* FIXME which i/oat version is i7300? */
  306. #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
  307. if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
  308. dma->chancnt--;
  309. #endif
  310. for (i = 0; i < dma->chancnt; i++) {
  311. ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
  312. if (!ioat)
  313. break;
  314. ioat_init_channel(device, &ioat->base, i,
  315. ioat2_reset_part2,
  316. ioat2_cleanup_tasklet,
  317. (unsigned long) ioat);
  318. ioat->xfercap_log = xfercap_log;
  319. spin_lock_init(&ioat->ring_lock);
  320. }
  321. dma->chancnt = i;
  322. return i;
  323. }
  324. static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  325. {
  326. struct dma_chan *c = tx->chan;
  327. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  328. dma_cookie_t cookie = c->cookie;
  329. cookie++;
  330. if (cookie < 0)
  331. cookie = 1;
  332. tx->cookie = cookie;
  333. c->cookie = cookie;
  334. ioat2_update_pending(ioat);
  335. spin_unlock_bh(&ioat->ring_lock);
  336. return cookie;
  337. }
  338. static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan)
  339. {
  340. struct ioat_dma_descriptor *hw;
  341. struct ioat_ring_ent *desc;
  342. struct ioatdma_device *dma;
  343. dma_addr_t phys;
  344. dma = to_ioatdma_device(chan->device);
  345. hw = pci_pool_alloc(dma->dma_pool, GFP_KERNEL, &phys);
  346. if (!hw)
  347. return NULL;
  348. memset(hw, 0, sizeof(*hw));
  349. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  350. if (!desc) {
  351. pci_pool_free(dma->dma_pool, hw, phys);
  352. return NULL;
  353. }
  354. dma_async_tx_descriptor_init(&desc->txd, chan);
  355. desc->txd.tx_submit = ioat2_tx_submit_unlock;
  356. desc->hw = hw;
  357. desc->txd.phys = phys;
  358. return desc;
  359. }
  360. static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
  361. {
  362. struct ioatdma_device *dma;
  363. dma = to_ioatdma_device(chan->device);
  364. pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
  365. kfree(desc);
  366. }
  367. /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
  368. * @chan: channel to be initialized
  369. */
  370. static int ioat2_alloc_chan_resources(struct dma_chan *c)
  371. {
  372. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  373. struct ioat_chan_common *chan = &ioat->base;
  374. struct ioat_ring_ent **ring;
  375. u16 chanctrl;
  376. u32 chanerr;
  377. int descs;
  378. int i;
  379. /* have we already been set up? */
  380. if (ioat->ring)
  381. return 1 << ioat->alloc_order;
  382. /* Setup register to interrupt and write completion status on error */
  383. chanctrl = IOAT_CHANCTRL_ERR_INT_EN | IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  384. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  385. writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
  386. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  387. if (chanerr) {
  388. dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
  389. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  390. }
  391. /* allocate a completion writeback area */
  392. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  393. chan->completion_virt = pci_pool_alloc(chan->device->completion_pool,
  394. GFP_KERNEL,
  395. &chan->completion_addr);
  396. if (!chan->completion_virt)
  397. return -ENOMEM;
  398. memset(chan->completion_virt, 0,
  399. sizeof(*chan->completion_virt));
  400. writel(((u64) chan->completion_addr) & 0x00000000FFFFFFFF,
  401. chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  402. writel(((u64) chan->completion_addr) >> 32,
  403. chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  404. ioat->alloc_order = ioat_get_alloc_order();
  405. descs = 1 << ioat->alloc_order;
  406. /* allocate the array to hold the software ring */
  407. ring = kcalloc(descs, sizeof(*ring), GFP_KERNEL);
  408. if (!ring)
  409. return -ENOMEM;
  410. for (i = 0; i < descs; i++) {
  411. ring[i] = ioat2_alloc_ring_ent(c);
  412. if (!ring[i]) {
  413. while (i--)
  414. ioat2_free_ring_ent(ring[i], c);
  415. kfree(ring);
  416. return -ENOMEM;
  417. }
  418. }
  419. /* link descs */
  420. for (i = 0; i < descs-1; i++) {
  421. struct ioat_ring_ent *next = ring[i+1];
  422. struct ioat_dma_descriptor *hw = ring[i]->hw;
  423. hw->next = next->txd.phys;
  424. }
  425. ring[i]->hw->next = ring[0]->txd.phys;
  426. spin_lock_bh(&ioat->ring_lock);
  427. ioat->ring = ring;
  428. ioat->head = 0;
  429. ioat->issued = 0;
  430. ioat->tail = 0;
  431. ioat->pending = 0;
  432. spin_unlock_bh(&ioat->ring_lock);
  433. tasklet_enable(&chan->cleanup_task);
  434. ioat2_start_null_desc(ioat);
  435. return descs;
  436. }
  437. /**
  438. * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
  439. * @idx: gets starting descriptor index on successful allocation
  440. * @ioat: ioat2,3 channel (ring) to operate on
  441. * @num_descs: allocation length
  442. */
  443. static int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
  444. {
  445. struct ioat_chan_common *chan = &ioat->base;
  446. spin_lock_bh(&ioat->ring_lock);
  447. if (unlikely(ioat2_ring_space(ioat) < num_descs)) {
  448. if (printk_ratelimit())
  449. dev_dbg(to_dev(chan),
  450. "%s: ring full! num_descs: %d (%x:%x:%x)\n",
  451. __func__, num_descs, ioat->head, ioat->tail,
  452. ioat->issued);
  453. spin_unlock_bh(&ioat->ring_lock);
  454. /* do direct reclaim in the allocation failure case */
  455. ioat2_cleanup(ioat);
  456. return -ENOMEM;
  457. }
  458. dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
  459. __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
  460. *idx = ioat2_desc_alloc(ioat, num_descs);
  461. return 0; /* with ioat->ring_lock held */
  462. }
  463. static struct dma_async_tx_descriptor *
  464. ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
  465. dma_addr_t dma_src, size_t len, unsigned long flags)
  466. {
  467. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  468. struct ioat_dma_descriptor *hw;
  469. struct ioat_ring_ent *desc;
  470. dma_addr_t dst = dma_dest;
  471. dma_addr_t src = dma_src;
  472. size_t total_len = len;
  473. int num_descs;
  474. u16 idx;
  475. int i;
  476. num_descs = ioat2_xferlen_to_descs(ioat, len);
  477. if (likely(num_descs) &&
  478. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  479. /* pass */;
  480. else
  481. return NULL;
  482. for (i = 0; i < num_descs; i++) {
  483. size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
  484. desc = ioat2_get_ring_ent(ioat, idx + i);
  485. hw = desc->hw;
  486. hw->size = copy;
  487. hw->ctl = 0;
  488. hw->src_addr = src;
  489. hw->dst_addr = dst;
  490. len -= copy;
  491. dst += copy;
  492. src += copy;
  493. }
  494. desc->txd.flags = flags;
  495. desc->len = total_len;
  496. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  497. hw->ctl_f.compl_write = 1;
  498. /* we leave the channel locked to ensure in order submission */
  499. return &desc->txd;
  500. }
  501. /**
  502. * ioat2_free_chan_resources - release all the descriptors
  503. * @chan: the channel to be cleaned
  504. */
  505. static void ioat2_free_chan_resources(struct dma_chan *c)
  506. {
  507. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  508. struct ioat_chan_common *chan = &ioat->base;
  509. struct ioatdma_device *ioatdma_device = chan->device;
  510. struct ioat_ring_ent *desc;
  511. const u16 total_descs = 1 << ioat->alloc_order;
  512. int descs;
  513. int i;
  514. /* Before freeing channel resources first check
  515. * if they have been previously allocated for this channel.
  516. */
  517. if (!ioat->ring)
  518. return;
  519. tasklet_disable(&chan->cleanup_task);
  520. ioat2_cleanup(ioat);
  521. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  522. * before removing DMA descriptor resources.
  523. */
  524. writeb(IOAT_CHANCMD_RESET,
  525. chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
  526. mdelay(100);
  527. spin_lock_bh(&ioat->ring_lock);
  528. descs = ioat2_ring_space(ioat);
  529. for (i = 0; i < descs; i++) {
  530. desc = ioat2_get_ring_ent(ioat, ioat->head + i);
  531. ioat2_free_ring_ent(desc, c);
  532. }
  533. if (descs < total_descs)
  534. dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
  535. total_descs - descs);
  536. for (i = 0; i < total_descs - descs; i++) {
  537. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  538. ioat2_free_ring_ent(desc, c);
  539. }
  540. kfree(ioat->ring);
  541. ioat->ring = NULL;
  542. ioat->alloc_order = 0;
  543. pci_pool_free(ioatdma_device->completion_pool,
  544. chan->completion_virt,
  545. chan->completion_addr);
  546. spin_unlock_bh(&ioat->ring_lock);
  547. chan->last_completion = 0;
  548. chan->completion_addr = 0;
  549. ioat->pending = 0;
  550. ioat->dmacount = 0;
  551. chan->watchdog_completion = 0;
  552. chan->last_compl_desc_addr_hw = 0;
  553. chan->watchdog_tcp_cookie = 0;
  554. chan->watchdog_last_tcp_cookie = 0;
  555. }
  556. static enum dma_status
  557. ioat2_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  558. dma_cookie_t *done, dma_cookie_t *used)
  559. {
  560. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  561. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  562. return DMA_SUCCESS;
  563. ioat2_cleanup(ioat);
  564. return ioat_is_complete(c, cookie, done, used);
  565. }
  566. int ioat2_dma_probe(struct ioatdma_device *device, int dca)
  567. {
  568. struct pci_dev *pdev = device->pdev;
  569. struct dma_device *dma;
  570. struct dma_chan *c;
  571. struct ioat_chan_common *chan;
  572. int err;
  573. device->enumerate_channels = ioat2_enumerate_channels;
  574. dma = &device->common;
  575. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  576. dma->device_issue_pending = ioat2_issue_pending;
  577. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  578. dma->device_free_chan_resources = ioat2_free_chan_resources;
  579. dma->device_is_tx_complete = ioat2_is_complete;
  580. err = ioat_probe(device);
  581. if (err)
  582. return err;
  583. ioat_set_tcp_copy_break(2048);
  584. list_for_each_entry(c, &dma->channels, device_node) {
  585. chan = to_chan_common(c);
  586. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
  587. chan->reg_base + IOAT_DCACTRL_OFFSET);
  588. }
  589. err = ioat_register(device);
  590. if (err)
  591. return err;
  592. if (dca)
  593. device->dca = ioat2_dca_init(pdev, device->reg_base);
  594. INIT_DELAYED_WORK(&device->work, ioat2_chan_watchdog);
  595. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  596. return err;
  597. }
  598. int ioat3_dma_probe(struct ioatdma_device *device, int dca)
  599. {
  600. struct pci_dev *pdev = device->pdev;
  601. struct dma_device *dma;
  602. struct dma_chan *c;
  603. struct ioat_chan_common *chan;
  604. int err;
  605. u16 dev_id;
  606. device->enumerate_channels = ioat2_enumerate_channels;
  607. dma = &device->common;
  608. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  609. dma->device_issue_pending = ioat2_issue_pending;
  610. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  611. dma->device_free_chan_resources = ioat2_free_chan_resources;
  612. dma->device_is_tx_complete = ioat2_is_complete;
  613. /* -= IOAT ver.3 workarounds =- */
  614. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  615. * that can cause stability issues for IOAT ver.3
  616. */
  617. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  618. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  619. * (workaround for spurious config parity error after restart)
  620. */
  621. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  622. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  623. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  624. err = ioat_probe(device);
  625. if (err)
  626. return err;
  627. ioat_set_tcp_copy_break(262144);
  628. list_for_each_entry(c, &dma->channels, device_node) {
  629. chan = to_chan_common(c);
  630. writel(IOAT_DMA_DCA_ANY_CPU,
  631. chan->reg_base + IOAT_DCACTRL_OFFSET);
  632. }
  633. err = ioat_register(device);
  634. if (err)
  635. return err;
  636. if (dca)
  637. device->dca = ioat3_dca_init(pdev, device->reg_base);
  638. return err;
  639. }