dma_v2.c 21 KB

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