dma.c 27 KB

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