dma_v2.c 21 KB

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