dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2015 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. * The full GNU General Public License is included in this distribution in
  15. * the file called "COPYING".
  16. *
  17. */
  18. /*
  19. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  20. * copy operations.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/prefetch.h>
  32. #include "dma.h"
  33. #include "registers.h"
  34. #include "hw.h"
  35. #include "../dmaengine.h"
  36. static void ioat_eh(struct ioatdma_chan *ioat_chan);
  37. /**
  38. * ioat_dma_do_interrupt - handler used for single vector interrupt mode
  39. * @irq: interrupt id
  40. * @data: interrupt data
  41. */
  42. irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
  43. {
  44. struct ioatdma_device *instance = data;
  45. struct ioatdma_chan *ioat_chan;
  46. unsigned long attnstatus;
  47. int bit;
  48. u8 intrctrl;
  49. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  50. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  51. return IRQ_NONE;
  52. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  53. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  54. return IRQ_NONE;
  55. }
  56. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  57. for_each_set_bit(bit, &attnstatus, BITS_PER_LONG) {
  58. ioat_chan = ioat_chan_by_index(instance, bit);
  59. if (test_bit(IOAT_RUN, &ioat_chan->state))
  60. tasklet_schedule(&ioat_chan->cleanup_task);
  61. }
  62. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  63. return IRQ_HANDLED;
  64. }
  65. /**
  66. * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
  67. * @irq: interrupt id
  68. * @data: interrupt data
  69. */
  70. irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
  71. {
  72. struct ioatdma_chan *ioat_chan = data;
  73. if (test_bit(IOAT_RUN, &ioat_chan->state))
  74. tasklet_schedule(&ioat_chan->cleanup_task);
  75. return IRQ_HANDLED;
  76. }
  77. void ioat_stop(struct ioatdma_chan *ioat_chan)
  78. {
  79. struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
  80. struct pci_dev *pdev = ioat_dma->pdev;
  81. int chan_id = chan_num(ioat_chan);
  82. struct msix_entry *msix;
  83. /* 1/ stop irq from firing tasklets
  84. * 2/ stop the tasklet from re-arming irqs
  85. */
  86. clear_bit(IOAT_RUN, &ioat_chan->state);
  87. /* flush inflight interrupts */
  88. switch (ioat_dma->irq_mode) {
  89. case IOAT_MSIX:
  90. msix = &ioat_dma->msix_entries[chan_id];
  91. synchronize_irq(msix->vector);
  92. break;
  93. case IOAT_MSI:
  94. case IOAT_INTX:
  95. synchronize_irq(pdev->irq);
  96. break;
  97. default:
  98. break;
  99. }
  100. /* flush inflight timers */
  101. del_timer_sync(&ioat_chan->timer);
  102. /* flush inflight tasklet runs */
  103. tasklet_kill(&ioat_chan->cleanup_task);
  104. /* final cleanup now that everything is quiesced and can't re-arm */
  105. ioat_cleanup_event((unsigned long)&ioat_chan->dma_chan);
  106. }
  107. static void __ioat_issue_pending(struct ioatdma_chan *ioat_chan)
  108. {
  109. ioat_chan->dmacount += ioat_ring_pending(ioat_chan);
  110. ioat_chan->issued = ioat_chan->head;
  111. writew(ioat_chan->dmacount,
  112. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  113. dev_dbg(to_dev(ioat_chan),
  114. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  115. __func__, ioat_chan->head, ioat_chan->tail,
  116. ioat_chan->issued, ioat_chan->dmacount);
  117. }
  118. void ioat_issue_pending(struct dma_chan *c)
  119. {
  120. struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
  121. if (ioat_ring_pending(ioat_chan)) {
  122. spin_lock_bh(&ioat_chan->prep_lock);
  123. __ioat_issue_pending(ioat_chan);
  124. spin_unlock_bh(&ioat_chan->prep_lock);
  125. }
  126. }
  127. /**
  128. * ioat_update_pending - log pending descriptors
  129. * @ioat: ioat+ channel
  130. *
  131. * Check if the number of unsubmitted descriptors has exceeded the
  132. * watermark. Called with prep_lock held
  133. */
  134. static void ioat_update_pending(struct ioatdma_chan *ioat_chan)
  135. {
  136. if (ioat_ring_pending(ioat_chan) > ioat_pending_level)
  137. __ioat_issue_pending(ioat_chan);
  138. }
  139. static void __ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
  140. {
  141. struct ioat_ring_ent *desc;
  142. struct ioat_dma_descriptor *hw;
  143. if (ioat_ring_space(ioat_chan) < 1) {
  144. dev_err(to_dev(ioat_chan),
  145. "Unable to start null desc - ring full\n");
  146. return;
  147. }
  148. dev_dbg(to_dev(ioat_chan),
  149. "%s: head: %#x tail: %#x issued: %#x\n",
  150. __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
  151. desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head);
  152. hw = desc->hw;
  153. hw->ctl = 0;
  154. hw->ctl_f.null = 1;
  155. hw->ctl_f.int_en = 1;
  156. hw->ctl_f.compl_write = 1;
  157. /* set size to non-zero value (channel returns error when size is 0) */
  158. hw->size = NULL_DESC_BUFFER_SIZE;
  159. hw->src_addr = 0;
  160. hw->dst_addr = 0;
  161. async_tx_ack(&desc->txd);
  162. ioat_set_chainaddr(ioat_chan, desc->txd.phys);
  163. dump_desc_dbg(ioat_chan, desc);
  164. /* make sure descriptors are written before we submit */
  165. wmb();
  166. ioat_chan->head += 1;
  167. __ioat_issue_pending(ioat_chan);
  168. }
  169. void ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
  170. {
  171. spin_lock_bh(&ioat_chan->prep_lock);
  172. __ioat_start_null_desc(ioat_chan);
  173. spin_unlock_bh(&ioat_chan->prep_lock);
  174. }
  175. static void __ioat_restart_chan(struct ioatdma_chan *ioat_chan)
  176. {
  177. /* set the tail to be re-issued */
  178. ioat_chan->issued = ioat_chan->tail;
  179. ioat_chan->dmacount = 0;
  180. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  181. dev_dbg(to_dev(ioat_chan),
  182. "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
  183. __func__, ioat_chan->head, ioat_chan->tail,
  184. ioat_chan->issued, ioat_chan->dmacount);
  185. if (ioat_ring_pending(ioat_chan)) {
  186. struct ioat_ring_ent *desc;
  187. desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
  188. ioat_set_chainaddr(ioat_chan, desc->txd.phys);
  189. __ioat_issue_pending(ioat_chan);
  190. } else
  191. __ioat_start_null_desc(ioat_chan);
  192. }
  193. static int ioat_quiesce(struct ioatdma_chan *ioat_chan, unsigned long tmo)
  194. {
  195. unsigned long end = jiffies + tmo;
  196. int err = 0;
  197. u32 status;
  198. status = ioat_chansts(ioat_chan);
  199. if (is_ioat_active(status) || is_ioat_idle(status))
  200. ioat_suspend(ioat_chan);
  201. while (is_ioat_active(status) || is_ioat_idle(status)) {
  202. if (tmo && time_after(jiffies, end)) {
  203. err = -ETIMEDOUT;
  204. break;
  205. }
  206. status = ioat_chansts(ioat_chan);
  207. cpu_relax();
  208. }
  209. return err;
  210. }
  211. static int ioat_reset_sync(struct ioatdma_chan *ioat_chan, unsigned long tmo)
  212. {
  213. unsigned long end = jiffies + tmo;
  214. int err = 0;
  215. ioat_reset(ioat_chan);
  216. while (ioat_reset_pending(ioat_chan)) {
  217. if (end && time_after(jiffies, end)) {
  218. err = -ETIMEDOUT;
  219. break;
  220. }
  221. cpu_relax();
  222. }
  223. return err;
  224. }
  225. static dma_cookie_t ioat_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
  226. __releases(&ioat_chan->prep_lock)
  227. {
  228. struct dma_chan *c = tx->chan;
  229. struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
  230. dma_cookie_t cookie;
  231. cookie = dma_cookie_assign(tx);
  232. dev_dbg(to_dev(ioat_chan), "%s: cookie: %d\n", __func__, cookie);
  233. if (!test_and_set_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
  234. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  235. /* make descriptor updates visible before advancing ioat->head,
  236. * this is purposefully not smp_wmb() since we are also
  237. * publishing the descriptor updates to a dma device
  238. */
  239. wmb();
  240. ioat_chan->head += ioat_chan->produce;
  241. ioat_update_pending(ioat_chan);
  242. spin_unlock_bh(&ioat_chan->prep_lock);
  243. return cookie;
  244. }
  245. static struct ioat_ring_ent *
  246. ioat_alloc_ring_ent(struct dma_chan *chan, gfp_t flags)
  247. {
  248. struct ioat_dma_descriptor *hw;
  249. struct ioat_ring_ent *desc;
  250. struct ioatdma_device *ioat_dma;
  251. dma_addr_t phys;
  252. ioat_dma = to_ioatdma_device(chan->device);
  253. hw = pci_pool_alloc(ioat_dma->dma_pool, flags, &phys);
  254. if (!hw)
  255. return NULL;
  256. memset(hw, 0, sizeof(*hw));
  257. desc = kmem_cache_zalloc(ioat_cache, flags);
  258. if (!desc) {
  259. pci_pool_free(ioat_dma->dma_pool, hw, phys);
  260. return NULL;
  261. }
  262. dma_async_tx_descriptor_init(&desc->txd, chan);
  263. desc->txd.tx_submit = ioat_tx_submit_unlock;
  264. desc->hw = hw;
  265. desc->txd.phys = phys;
  266. return desc;
  267. }
  268. void ioat_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
  269. {
  270. struct ioatdma_device *ioat_dma;
  271. ioat_dma = to_ioatdma_device(chan->device);
  272. pci_pool_free(ioat_dma->dma_pool, desc->hw, desc->txd.phys);
  273. kmem_cache_free(ioat_cache, desc);
  274. }
  275. struct ioat_ring_ent **
  276. ioat_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
  277. {
  278. struct ioat_ring_ent **ring;
  279. int descs = 1 << order;
  280. int i;
  281. if (order > ioat_get_max_alloc_order())
  282. return NULL;
  283. /* allocate the array to hold the software ring */
  284. ring = kcalloc(descs, sizeof(*ring), flags);
  285. if (!ring)
  286. return NULL;
  287. for (i = 0; i < descs; i++) {
  288. ring[i] = ioat_alloc_ring_ent(c, flags);
  289. if (!ring[i]) {
  290. while (i--)
  291. ioat_free_ring_ent(ring[i], c);
  292. kfree(ring);
  293. return NULL;
  294. }
  295. set_desc_id(ring[i], i);
  296. }
  297. /* link descs */
  298. for (i = 0; i < descs-1; i++) {
  299. struct ioat_ring_ent *next = ring[i+1];
  300. struct ioat_dma_descriptor *hw = ring[i]->hw;
  301. hw->next = next->txd.phys;
  302. }
  303. ring[i]->hw->next = ring[0]->txd.phys;
  304. return ring;
  305. }
  306. static bool reshape_ring(struct ioatdma_chan *ioat_chan, int order)
  307. {
  308. /* reshape differs from normal ring allocation in that we want
  309. * to allocate a new software ring while only
  310. * extending/truncating the hardware ring
  311. */
  312. struct dma_chan *c = &ioat_chan->dma_chan;
  313. const u32 curr_size = ioat_ring_size(ioat_chan);
  314. const u16 active = ioat_ring_active(ioat_chan);
  315. const u32 new_size = 1 << order;
  316. struct ioat_ring_ent **ring;
  317. u32 i;
  318. if (order > ioat_get_max_alloc_order())
  319. return false;
  320. /* double check that we have at least 1 free descriptor */
  321. if (active == curr_size)
  322. return false;
  323. /* when shrinking, verify that we can hold the current active
  324. * set in the new ring
  325. */
  326. if (active >= new_size)
  327. return false;
  328. /* allocate the array to hold the software ring */
  329. ring = kcalloc(new_size, sizeof(*ring), GFP_NOWAIT);
  330. if (!ring)
  331. return false;
  332. /* allocate/trim descriptors as needed */
  333. if (new_size > curr_size) {
  334. /* copy current descriptors to the new ring */
  335. for (i = 0; i < curr_size; i++) {
  336. u16 curr_idx = (ioat_chan->tail+i) & (curr_size-1);
  337. u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
  338. ring[new_idx] = ioat_chan->ring[curr_idx];
  339. set_desc_id(ring[new_idx], new_idx);
  340. }
  341. /* add new descriptors to the ring */
  342. for (i = curr_size; i < new_size; i++) {
  343. u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
  344. ring[new_idx] = ioat_alloc_ring_ent(c, GFP_NOWAIT);
  345. if (!ring[new_idx]) {
  346. while (i--) {
  347. u16 new_idx = (ioat_chan->tail+i) &
  348. (new_size-1);
  349. ioat_free_ring_ent(ring[new_idx], c);
  350. }
  351. kfree(ring);
  352. return false;
  353. }
  354. set_desc_id(ring[new_idx], new_idx);
  355. }
  356. /* hw link new descriptors */
  357. for (i = curr_size-1; i < new_size; i++) {
  358. u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
  359. struct ioat_ring_ent *next =
  360. ring[(new_idx+1) & (new_size-1)];
  361. struct ioat_dma_descriptor *hw = ring[new_idx]->hw;
  362. hw->next = next->txd.phys;
  363. }
  364. } else {
  365. struct ioat_dma_descriptor *hw;
  366. struct ioat_ring_ent *next;
  367. /* copy current descriptors to the new ring, dropping the
  368. * removed descriptors
  369. */
  370. for (i = 0; i < new_size; i++) {
  371. u16 curr_idx = (ioat_chan->tail+i) & (curr_size-1);
  372. u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
  373. ring[new_idx] = ioat_chan->ring[curr_idx];
  374. set_desc_id(ring[new_idx], new_idx);
  375. }
  376. /* free deleted descriptors */
  377. for (i = new_size; i < curr_size; i++) {
  378. struct ioat_ring_ent *ent;
  379. ent = ioat_get_ring_ent(ioat_chan, ioat_chan->tail+i);
  380. ioat_free_ring_ent(ent, c);
  381. }
  382. /* fix up hardware ring */
  383. hw = ring[(ioat_chan->tail+new_size-1) & (new_size-1)]->hw;
  384. next = ring[(ioat_chan->tail+new_size) & (new_size-1)];
  385. hw->next = next->txd.phys;
  386. }
  387. dev_dbg(to_dev(ioat_chan), "%s: allocated %d descriptors\n",
  388. __func__, new_size);
  389. kfree(ioat_chan->ring);
  390. ioat_chan->ring = ring;
  391. ioat_chan->alloc_order = order;
  392. return true;
  393. }
  394. /**
  395. * ioat_check_space_lock - verify space and grab ring producer lock
  396. * @ioat: ioat,3 channel (ring) to operate on
  397. * @num_descs: allocation length
  398. */
  399. int ioat_check_space_lock(struct ioatdma_chan *ioat_chan, int num_descs)
  400. __acquires(&ioat_chan->prep_lock)
  401. {
  402. bool retry;
  403. retry:
  404. spin_lock_bh(&ioat_chan->prep_lock);
  405. /* never allow the last descriptor to be consumed, we need at
  406. * least one free at all times to allow for on-the-fly ring
  407. * resizing.
  408. */
  409. if (likely(ioat_ring_space(ioat_chan) > num_descs)) {
  410. dev_dbg(to_dev(ioat_chan), "%s: num_descs: %d (%x:%x:%x)\n",
  411. __func__, num_descs, ioat_chan->head,
  412. ioat_chan->tail, ioat_chan->issued);
  413. ioat_chan->produce = num_descs;
  414. return 0; /* with ioat->prep_lock held */
  415. }
  416. retry = test_and_set_bit(IOAT_RESHAPE_PENDING, &ioat_chan->state);
  417. spin_unlock_bh(&ioat_chan->prep_lock);
  418. /* is another cpu already trying to expand the ring? */
  419. if (retry)
  420. goto retry;
  421. spin_lock_bh(&ioat_chan->cleanup_lock);
  422. spin_lock_bh(&ioat_chan->prep_lock);
  423. retry = reshape_ring(ioat_chan, ioat_chan->alloc_order + 1);
  424. clear_bit(IOAT_RESHAPE_PENDING, &ioat_chan->state);
  425. spin_unlock_bh(&ioat_chan->prep_lock);
  426. spin_unlock_bh(&ioat_chan->cleanup_lock);
  427. /* if we were able to expand the ring retry the allocation */
  428. if (retry)
  429. goto retry;
  430. dev_dbg_ratelimited(to_dev(ioat_chan),
  431. "%s: ring full! num_descs: %d (%x:%x:%x)\n",
  432. __func__, num_descs, ioat_chan->head,
  433. ioat_chan->tail, ioat_chan->issued);
  434. /* progress reclaim in the allocation failure case we may be
  435. * called under bh_disabled so we need to trigger the timer
  436. * event directly
  437. */
  438. if (time_is_before_jiffies(ioat_chan->timer.expires)
  439. && timer_pending(&ioat_chan->timer)) {
  440. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  441. ioat_timer_event((unsigned long)ioat_chan);
  442. }
  443. return -ENOMEM;
  444. }
  445. static bool desc_has_ext(struct ioat_ring_ent *desc)
  446. {
  447. struct ioat_dma_descriptor *hw = desc->hw;
  448. if (hw->ctl_f.op == IOAT_OP_XOR ||
  449. hw->ctl_f.op == IOAT_OP_XOR_VAL) {
  450. struct ioat_xor_descriptor *xor = desc->xor;
  451. if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
  452. return true;
  453. } else if (hw->ctl_f.op == IOAT_OP_PQ ||
  454. hw->ctl_f.op == IOAT_OP_PQ_VAL) {
  455. struct ioat_pq_descriptor *pq = desc->pq;
  456. if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
  457. return true;
  458. }
  459. return false;
  460. }
  461. static void
  462. ioat_free_sed(struct ioatdma_device *ioat_dma, struct ioat_sed_ent *sed)
  463. {
  464. if (!sed)
  465. return;
  466. dma_pool_free(ioat_dma->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
  467. kmem_cache_free(ioat_sed_cache, sed);
  468. }
  469. static u64 ioat_get_current_completion(struct ioatdma_chan *ioat_chan)
  470. {
  471. u64 phys_complete;
  472. u64 completion;
  473. completion = *ioat_chan->completion;
  474. phys_complete = ioat_chansts_to_addr(completion);
  475. dev_dbg(to_dev(ioat_chan), "%s: phys_complete: %#llx\n", __func__,
  476. (unsigned long long) phys_complete);
  477. return phys_complete;
  478. }
  479. static bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
  480. u64 *phys_complete)
  481. {
  482. *phys_complete = ioat_get_current_completion(ioat_chan);
  483. if (*phys_complete == ioat_chan->last_completion)
  484. return false;
  485. clear_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
  486. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  487. return true;
  488. }
  489. static void
  490. desc_get_errstat(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc)
  491. {
  492. struct ioat_dma_descriptor *hw = desc->hw;
  493. switch (hw->ctl_f.op) {
  494. case IOAT_OP_PQ_VAL:
  495. case IOAT_OP_PQ_VAL_16S:
  496. {
  497. struct ioat_pq_descriptor *pq = desc->pq;
  498. /* check if there's error written */
  499. if (!pq->dwbes_f.wbes)
  500. return;
  501. /* need to set a chanerr var for checking to clear later */
  502. if (pq->dwbes_f.p_val_err)
  503. *desc->result |= SUM_CHECK_P_RESULT;
  504. if (pq->dwbes_f.q_val_err)
  505. *desc->result |= SUM_CHECK_Q_RESULT;
  506. return;
  507. }
  508. default:
  509. return;
  510. }
  511. }
  512. /**
  513. * __cleanup - reclaim used descriptors
  514. * @ioat: channel (ring) to clean
  515. */
  516. static void __cleanup(struct ioatdma_chan *ioat_chan, dma_addr_t phys_complete)
  517. {
  518. struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
  519. struct ioat_ring_ent *desc;
  520. bool seen_current = false;
  521. int idx = ioat_chan->tail, i;
  522. u16 active;
  523. dev_dbg(to_dev(ioat_chan), "%s: head: %#x tail: %#x issued: %#x\n",
  524. __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
  525. /*
  526. * At restart of the channel, the completion address and the
  527. * channel status will be 0 due to starting a new chain. Since
  528. * it's new chain and the first descriptor "fails", there is
  529. * nothing to clean up. We do not want to reap the entire submitted
  530. * chain due to this 0 address value and then BUG.
  531. */
  532. if (!phys_complete)
  533. return;
  534. active = ioat_ring_active(ioat_chan);
  535. for (i = 0; i < active && !seen_current; i++) {
  536. struct dma_async_tx_descriptor *tx;
  537. smp_read_barrier_depends();
  538. prefetch(ioat_get_ring_ent(ioat_chan, idx + i + 1));
  539. desc = ioat_get_ring_ent(ioat_chan, idx + i);
  540. dump_desc_dbg(ioat_chan, desc);
  541. /* set err stat if we are using dwbes */
  542. if (ioat_dma->cap & IOAT_CAP_DWBES)
  543. desc_get_errstat(ioat_chan, desc);
  544. tx = &desc->txd;
  545. if (tx->cookie) {
  546. dma_cookie_complete(tx);
  547. dma_descriptor_unmap(tx);
  548. if (tx->callback) {
  549. tx->callback(tx->callback_param);
  550. tx->callback = NULL;
  551. }
  552. }
  553. if (tx->phys == phys_complete)
  554. seen_current = true;
  555. /* skip extended descriptors */
  556. if (desc_has_ext(desc)) {
  557. BUG_ON(i + 1 >= active);
  558. i++;
  559. }
  560. /* cleanup super extended descriptors */
  561. if (desc->sed) {
  562. ioat_free_sed(ioat_dma, desc->sed);
  563. desc->sed = NULL;
  564. }
  565. }
  566. /* finish all descriptor reads before incrementing tail */
  567. smp_mb();
  568. ioat_chan->tail = idx + i;
  569. /* no active descs have written a completion? */
  570. BUG_ON(active && !seen_current);
  571. ioat_chan->last_completion = phys_complete;
  572. if (active - i == 0) {
  573. dev_dbg(to_dev(ioat_chan), "%s: cancel completion timeout\n",
  574. __func__);
  575. mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
  576. }
  577. /* 5 microsecond delay per pending descriptor */
  578. writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
  579. ioat_chan->ioat_dma->reg_base + IOAT_INTRDELAY_OFFSET);
  580. }
  581. static void ioat_cleanup(struct ioatdma_chan *ioat_chan)
  582. {
  583. u64 phys_complete;
  584. spin_lock_bh(&ioat_chan->cleanup_lock);
  585. if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
  586. __cleanup(ioat_chan, phys_complete);
  587. if (is_ioat_halted(*ioat_chan->completion)) {
  588. u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  589. if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
  590. mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
  591. ioat_eh(ioat_chan);
  592. }
  593. }
  594. spin_unlock_bh(&ioat_chan->cleanup_lock);
  595. }
  596. void ioat_cleanup_event(unsigned long data)
  597. {
  598. struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
  599. ioat_cleanup(ioat_chan);
  600. if (!test_bit(IOAT_RUN, &ioat_chan->state))
  601. return;
  602. writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  603. }
  604. static void ioat_restart_channel(struct ioatdma_chan *ioat_chan)
  605. {
  606. u64 phys_complete;
  607. ioat_quiesce(ioat_chan, 0);
  608. if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
  609. __cleanup(ioat_chan, phys_complete);
  610. __ioat_restart_chan(ioat_chan);
  611. }
  612. static void ioat_eh(struct ioatdma_chan *ioat_chan)
  613. {
  614. struct pci_dev *pdev = to_pdev(ioat_chan);
  615. struct ioat_dma_descriptor *hw;
  616. struct dma_async_tx_descriptor *tx;
  617. u64 phys_complete;
  618. struct ioat_ring_ent *desc;
  619. u32 err_handled = 0;
  620. u32 chanerr_int;
  621. u32 chanerr;
  622. /* cleanup so tail points to descriptor that caused the error */
  623. if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
  624. __cleanup(ioat_chan, phys_complete);
  625. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  626. pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
  627. dev_dbg(to_dev(ioat_chan), "%s: error = %x:%x\n",
  628. __func__, chanerr, chanerr_int);
  629. desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
  630. hw = desc->hw;
  631. dump_desc_dbg(ioat_chan, desc);
  632. switch (hw->ctl_f.op) {
  633. case IOAT_OP_XOR_VAL:
  634. if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
  635. *desc->result |= SUM_CHECK_P_RESULT;
  636. err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
  637. }
  638. break;
  639. case IOAT_OP_PQ_VAL:
  640. case IOAT_OP_PQ_VAL_16S:
  641. if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
  642. *desc->result |= SUM_CHECK_P_RESULT;
  643. err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
  644. }
  645. if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
  646. *desc->result |= SUM_CHECK_Q_RESULT;
  647. err_handled |= IOAT_CHANERR_XOR_Q_ERR;
  648. }
  649. break;
  650. }
  651. /* fault on unhandled error or spurious halt */
  652. if (chanerr ^ err_handled || chanerr == 0) {
  653. dev_err(to_dev(ioat_chan), "%s: fatal error (%x:%x)\n",
  654. __func__, chanerr, err_handled);
  655. BUG();
  656. } else { /* cleanup the faulty descriptor */
  657. tx = &desc->txd;
  658. if (tx->cookie) {
  659. dma_cookie_complete(tx);
  660. dma_descriptor_unmap(tx);
  661. if (tx->callback) {
  662. tx->callback(tx->callback_param);
  663. tx->callback = NULL;
  664. }
  665. }
  666. }
  667. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  668. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
  669. /* mark faulting descriptor as complete */
  670. *ioat_chan->completion = desc->txd.phys;
  671. spin_lock_bh(&ioat_chan->prep_lock);
  672. ioat_restart_channel(ioat_chan);
  673. spin_unlock_bh(&ioat_chan->prep_lock);
  674. }
  675. static void check_active(struct ioatdma_chan *ioat_chan)
  676. {
  677. if (ioat_ring_active(ioat_chan)) {
  678. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  679. return;
  680. }
  681. if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
  682. mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
  683. else if (ioat_chan->alloc_order > ioat_get_alloc_order()) {
  684. /* if the ring is idle, empty, and oversized try to step
  685. * down the size
  686. */
  687. reshape_ring(ioat_chan, ioat_chan->alloc_order - 1);
  688. /* keep shrinking until we get back to our minimum
  689. * default size
  690. */
  691. if (ioat_chan->alloc_order > ioat_get_alloc_order())
  692. mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
  693. }
  694. }
  695. void ioat_timer_event(unsigned long data)
  696. {
  697. struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
  698. dma_addr_t phys_complete;
  699. u64 status;
  700. status = ioat_chansts(ioat_chan);
  701. /* when halted due to errors check for channel
  702. * programming errors before advancing the completion state
  703. */
  704. if (is_ioat_halted(status)) {
  705. u32 chanerr;
  706. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  707. dev_err(to_dev(ioat_chan), "%s: Channel halted (%x)\n",
  708. __func__, chanerr);
  709. if (test_bit(IOAT_RUN, &ioat_chan->state))
  710. BUG_ON(is_ioat_bug(chanerr));
  711. else /* we never got off the ground */
  712. return;
  713. }
  714. /* if we haven't made progress and we have already
  715. * acknowledged a pending completion once, then be more
  716. * forceful with a restart
  717. */
  718. spin_lock_bh(&ioat_chan->cleanup_lock);
  719. if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
  720. __cleanup(ioat_chan, phys_complete);
  721. else if (test_bit(IOAT_COMPLETION_ACK, &ioat_chan->state)) {
  722. spin_lock_bh(&ioat_chan->prep_lock);
  723. ioat_restart_channel(ioat_chan);
  724. spin_unlock_bh(&ioat_chan->prep_lock);
  725. spin_unlock_bh(&ioat_chan->cleanup_lock);
  726. return;
  727. } else {
  728. set_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
  729. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  730. }
  731. if (ioat_ring_active(ioat_chan))
  732. mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
  733. else {
  734. spin_lock_bh(&ioat_chan->prep_lock);
  735. check_active(ioat_chan);
  736. spin_unlock_bh(&ioat_chan->prep_lock);
  737. }
  738. spin_unlock_bh(&ioat_chan->cleanup_lock);
  739. }
  740. enum dma_status
  741. ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  742. struct dma_tx_state *txstate)
  743. {
  744. struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
  745. enum dma_status ret;
  746. ret = dma_cookie_status(c, cookie, txstate);
  747. if (ret == DMA_COMPLETE)
  748. return ret;
  749. ioat_cleanup(ioat_chan);
  750. return dma_cookie_status(c, cookie, txstate);
  751. }
  752. static int ioat_irq_reinit(struct ioatdma_device *ioat_dma)
  753. {
  754. struct pci_dev *pdev = ioat_dma->pdev;
  755. int irq = pdev->irq, i;
  756. if (!is_bwd_ioat(pdev))
  757. return 0;
  758. switch (ioat_dma->irq_mode) {
  759. case IOAT_MSIX:
  760. for (i = 0; i < ioat_dma->dma_dev.chancnt; i++) {
  761. struct msix_entry *msix = &ioat_dma->msix_entries[i];
  762. struct ioatdma_chan *ioat_chan;
  763. ioat_chan = ioat_chan_by_index(ioat_dma, i);
  764. devm_free_irq(&pdev->dev, msix->vector, ioat_chan);
  765. }
  766. pci_disable_msix(pdev);
  767. break;
  768. case IOAT_MSI:
  769. pci_disable_msi(pdev);
  770. /* fall through */
  771. case IOAT_INTX:
  772. devm_free_irq(&pdev->dev, irq, ioat_dma);
  773. break;
  774. default:
  775. return 0;
  776. }
  777. ioat_dma->irq_mode = IOAT_NOIRQ;
  778. return ioat_dma_setup_interrupts(ioat_dma);
  779. }
  780. int ioat_reset_hw(struct ioatdma_chan *ioat_chan)
  781. {
  782. /* throw away whatever the channel was doing and get it
  783. * initialized, with ioat3 specific workarounds
  784. */
  785. struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
  786. struct pci_dev *pdev = ioat_dma->pdev;
  787. u32 chanerr;
  788. u16 dev_id;
  789. int err;
  790. ioat_quiesce(ioat_chan, msecs_to_jiffies(100));
  791. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  792. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  793. if (ioat_dma->version < IOAT_VER_3_3) {
  794. /* clear any pending errors */
  795. err = pci_read_config_dword(pdev,
  796. IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  797. if (err) {
  798. dev_err(&pdev->dev,
  799. "channel error register unreachable\n");
  800. return err;
  801. }
  802. pci_write_config_dword(pdev,
  803. IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  804. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  805. * (workaround for spurious config parity error after restart)
  806. */
  807. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  808. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
  809. pci_write_config_dword(pdev,
  810. IOAT_PCI_DMAUNCERRSTS_OFFSET,
  811. 0x10);
  812. }
  813. }
  814. err = ioat_reset_sync(ioat_chan, msecs_to_jiffies(200));
  815. if (!err)
  816. err = ioat_irq_reinit(ioat_dma);
  817. if (err)
  818. dev_err(&pdev->dev, "Failed to reset: %d\n", err);
  819. return err;
  820. }