dma_v3.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * The full GNU General Public License is included in this distribution in
  23. * the file called "COPYING".
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in
  36. * the documentation and/or other materials provided with the
  37. * distribution.
  38. * * Neither the name of Intel Corporation nor the names of its
  39. * contributors may be used to endorse or promote products derived
  40. * from this software without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  46. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  47. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  48. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  49. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  50. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  52. * POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. /*
  55. * Support routines for v3+ hardware
  56. */
  57. #include <linux/pci.h>
  58. #include <linux/gfp.h>
  59. #include <linux/dmaengine.h>
  60. #include <linux/dma-mapping.h>
  61. #include "registers.h"
  62. #include "hw.h"
  63. #include "dma.h"
  64. #include "dma_v2.h"
  65. /* ioat hardware assumes at least two sources for raid operations */
  66. #define src_cnt_to_sw(x) ((x) + 2)
  67. #define src_cnt_to_hw(x) ((x) - 2)
  68. /* provide a lookup table for setting the source address in the base or
  69. * extended descriptor of an xor or pq descriptor
  70. */
  71. static const u8 xor_idx_to_desc __read_mostly = 0xd0;
  72. static const u8 xor_idx_to_field[] __read_mostly = { 1, 4, 5, 6, 7, 0, 1, 2 };
  73. static const u8 pq_idx_to_desc __read_mostly = 0xf8;
  74. static const u8 pq_idx_to_field[] __read_mostly = { 1, 4, 5, 0, 1, 2, 4, 5 };
  75. static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  76. {
  77. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  78. return raw->field[xor_idx_to_field[idx]];
  79. }
  80. static void xor_set_src(struct ioat_raw_descriptor *descs[2],
  81. dma_addr_t addr, u32 offset, int idx)
  82. {
  83. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  84. raw->field[xor_idx_to_field[idx]] = addr + offset;
  85. }
  86. static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  87. {
  88. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  89. return raw->field[pq_idx_to_field[idx]];
  90. }
  91. static void pq_set_src(struct ioat_raw_descriptor *descs[2],
  92. dma_addr_t addr, u32 offset, u8 coef, int idx)
  93. {
  94. struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
  95. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  96. raw->field[pq_idx_to_field[idx]] = addr + offset;
  97. pq->coef[idx] = coef;
  98. }
  99. static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
  100. struct ioat_ring_ent *desc, int idx)
  101. {
  102. struct ioat_chan_common *chan = &ioat->base;
  103. struct pci_dev *pdev = chan->device->pdev;
  104. size_t len = desc->len;
  105. size_t offset = len - desc->hw->size;
  106. struct dma_async_tx_descriptor *tx = &desc->txd;
  107. enum dma_ctrl_flags flags = tx->flags;
  108. switch (desc->hw->ctl_f.op) {
  109. case IOAT_OP_COPY:
  110. if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
  111. ioat_dma_unmap(chan, flags, len, desc->hw);
  112. break;
  113. case IOAT_OP_FILL: {
  114. struct ioat_fill_descriptor *hw = desc->fill;
  115. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  116. ioat_unmap(pdev, hw->dst_addr - offset, len,
  117. PCI_DMA_FROMDEVICE, flags, 1);
  118. break;
  119. }
  120. case IOAT_OP_XOR_VAL:
  121. case IOAT_OP_XOR: {
  122. struct ioat_xor_descriptor *xor = desc->xor;
  123. struct ioat_ring_ent *ext;
  124. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  125. int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
  126. struct ioat_raw_descriptor *descs[2];
  127. int i;
  128. if (src_cnt > 5) {
  129. ext = ioat2_get_ring_ent(ioat, idx + 1);
  130. xor_ex = ext->xor_ex;
  131. }
  132. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  133. descs[0] = (struct ioat_raw_descriptor *) xor;
  134. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  135. for (i = 0; i < src_cnt; i++) {
  136. dma_addr_t src = xor_get_src(descs, i);
  137. ioat_unmap(pdev, src - offset, len,
  138. PCI_DMA_TODEVICE, flags, 0);
  139. }
  140. /* dest is a source in xor validate operations */
  141. if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
  142. ioat_unmap(pdev, xor->dst_addr - offset, len,
  143. PCI_DMA_TODEVICE, flags, 1);
  144. break;
  145. }
  146. }
  147. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  148. ioat_unmap(pdev, xor->dst_addr - offset, len,
  149. PCI_DMA_FROMDEVICE, flags, 1);
  150. break;
  151. }
  152. case IOAT_OP_PQ_VAL:
  153. case IOAT_OP_PQ: {
  154. struct ioat_pq_descriptor *pq = desc->pq;
  155. struct ioat_ring_ent *ext;
  156. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  157. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  158. struct ioat_raw_descriptor *descs[2];
  159. int i;
  160. if (src_cnt > 3) {
  161. ext = ioat2_get_ring_ent(ioat, idx + 1);
  162. pq_ex = ext->pq_ex;
  163. }
  164. /* in the 'continue' case don't unmap the dests as sources */
  165. if (dmaf_p_disabled_continue(flags))
  166. src_cnt--;
  167. else if (dmaf_continue(flags))
  168. src_cnt -= 3;
  169. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  170. descs[0] = (struct ioat_raw_descriptor *) pq;
  171. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  172. for (i = 0; i < src_cnt; i++) {
  173. dma_addr_t src = pq_get_src(descs, i);
  174. ioat_unmap(pdev, src - offset, len,
  175. PCI_DMA_TODEVICE, flags, 0);
  176. }
  177. /* the dests are sources in pq validate operations */
  178. if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
  179. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  180. ioat_unmap(pdev, pq->p_addr - offset,
  181. len, PCI_DMA_TODEVICE, flags, 0);
  182. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  183. ioat_unmap(pdev, pq->q_addr - offset,
  184. len, PCI_DMA_TODEVICE, flags, 0);
  185. break;
  186. }
  187. }
  188. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  189. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  190. ioat_unmap(pdev, pq->p_addr - offset, len,
  191. PCI_DMA_BIDIRECTIONAL, flags, 1);
  192. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  193. ioat_unmap(pdev, pq->q_addr - offset, len,
  194. PCI_DMA_BIDIRECTIONAL, flags, 1);
  195. }
  196. break;
  197. }
  198. default:
  199. dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
  200. __func__, desc->hw->ctl_f.op);
  201. }
  202. }
  203. static bool desc_has_ext(struct ioat_ring_ent *desc)
  204. {
  205. struct ioat_dma_descriptor *hw = desc->hw;
  206. if (hw->ctl_f.op == IOAT_OP_XOR ||
  207. hw->ctl_f.op == IOAT_OP_XOR_VAL) {
  208. struct ioat_xor_descriptor *xor = desc->xor;
  209. if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
  210. return true;
  211. } else if (hw->ctl_f.op == IOAT_OP_PQ ||
  212. hw->ctl_f.op == IOAT_OP_PQ_VAL) {
  213. struct ioat_pq_descriptor *pq = desc->pq;
  214. if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. * __cleanup - reclaim used descriptors
  221. * @ioat: channel (ring) to clean
  222. *
  223. * The difference from the dma_v2.c __cleanup() is that this routine
  224. * handles extended descriptors and dma-unmapping raid operations.
  225. */
  226. static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
  227. {
  228. struct ioat_chan_common *chan = &ioat->base;
  229. struct ioat_ring_ent *desc;
  230. bool seen_current = false;
  231. int idx = ioat->tail, i;
  232. u16 active;
  233. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  234. __func__, ioat->head, ioat->tail, ioat->issued);
  235. active = ioat2_ring_active(ioat);
  236. for (i = 0; i < active && !seen_current; i++) {
  237. struct dma_async_tx_descriptor *tx;
  238. smp_read_barrier_depends();
  239. prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
  240. desc = ioat2_get_ring_ent(ioat, idx + i);
  241. dump_desc_dbg(ioat, desc);
  242. tx = &desc->txd;
  243. if (tx->cookie) {
  244. chan->completed_cookie = tx->cookie;
  245. ioat3_dma_unmap(ioat, desc, idx + i);
  246. tx->cookie = 0;
  247. if (tx->callback) {
  248. tx->callback(tx->callback_param);
  249. tx->callback = NULL;
  250. }
  251. }
  252. if (tx->phys == phys_complete)
  253. seen_current = true;
  254. /* skip extended descriptors */
  255. if (desc_has_ext(desc)) {
  256. BUG_ON(i + 1 >= active);
  257. i++;
  258. }
  259. }
  260. smp_mb(); /* finish all descriptor reads before incrementing tail */
  261. ioat->tail = idx + i;
  262. BUG_ON(active && !seen_current); /* no active descs have written a completion? */
  263. chan->last_completion = phys_complete;
  264. if (active - i == 0) {
  265. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  266. __func__);
  267. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  268. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  269. }
  270. /* 5 microsecond delay per pending descriptor */
  271. writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
  272. chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
  273. }
  274. static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
  275. {
  276. struct ioat_chan_common *chan = &ioat->base;
  277. unsigned long phys_complete;
  278. spin_lock_bh(&chan->cleanup_lock);
  279. if (ioat_cleanup_preamble(chan, &phys_complete))
  280. __cleanup(ioat, phys_complete);
  281. spin_unlock_bh(&chan->cleanup_lock);
  282. }
  283. static void ioat3_cleanup_event(unsigned long data)
  284. {
  285. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  286. ioat3_cleanup(ioat);
  287. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  288. }
  289. static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
  290. {
  291. struct ioat_chan_common *chan = &ioat->base;
  292. unsigned long phys_complete;
  293. ioat2_quiesce(chan, 0);
  294. if (ioat_cleanup_preamble(chan, &phys_complete))
  295. __cleanup(ioat, phys_complete);
  296. __ioat2_restart_chan(ioat);
  297. }
  298. static void ioat3_timer_event(unsigned long data)
  299. {
  300. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  301. struct ioat_chan_common *chan = &ioat->base;
  302. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  303. unsigned long phys_complete;
  304. u64 status;
  305. status = ioat_chansts(chan);
  306. /* when halted due to errors check for channel
  307. * programming errors before advancing the completion state
  308. */
  309. if (is_ioat_halted(status)) {
  310. u32 chanerr;
  311. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  312. dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
  313. __func__, chanerr);
  314. if (test_bit(IOAT_RUN, &chan->state))
  315. BUG_ON(is_ioat_bug(chanerr));
  316. else /* we never got off the ground */
  317. return;
  318. }
  319. /* if we haven't made progress and we have already
  320. * acknowledged a pending completion once, then be more
  321. * forceful with a restart
  322. */
  323. spin_lock_bh(&chan->cleanup_lock);
  324. if (ioat_cleanup_preamble(chan, &phys_complete))
  325. __cleanup(ioat, phys_complete);
  326. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
  327. spin_lock_bh(&ioat->prep_lock);
  328. ioat3_restart_channel(ioat);
  329. spin_unlock_bh(&ioat->prep_lock);
  330. } else {
  331. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  332. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  333. }
  334. spin_unlock_bh(&chan->cleanup_lock);
  335. } else {
  336. u16 active;
  337. /* if the ring is idle, empty, and oversized try to step
  338. * down the size
  339. */
  340. spin_lock_bh(&chan->cleanup_lock);
  341. spin_lock_bh(&ioat->prep_lock);
  342. active = ioat2_ring_active(ioat);
  343. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  344. reshape_ring(ioat, ioat->alloc_order-1);
  345. spin_unlock_bh(&ioat->prep_lock);
  346. spin_unlock_bh(&chan->cleanup_lock);
  347. /* keep shrinking until we get back to our minimum
  348. * default size
  349. */
  350. if (ioat->alloc_order > ioat_get_alloc_order())
  351. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  352. }
  353. }
  354. static enum dma_status
  355. ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  356. struct dma_tx_state *txstate)
  357. {
  358. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  359. if (ioat_tx_status(c, cookie, txstate) == DMA_SUCCESS)
  360. return DMA_SUCCESS;
  361. ioat3_cleanup(ioat);
  362. return ioat_tx_status(c, cookie, txstate);
  363. }
  364. static struct dma_async_tx_descriptor *
  365. ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
  366. size_t len, unsigned long flags)
  367. {
  368. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  369. struct ioat_ring_ent *desc;
  370. size_t total_len = len;
  371. struct ioat_fill_descriptor *fill;
  372. u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
  373. int num_descs, idx, i;
  374. num_descs = ioat2_xferlen_to_descs(ioat, len);
  375. if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0)
  376. idx = ioat->head;
  377. else
  378. return NULL;
  379. i = 0;
  380. do {
  381. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  382. desc = ioat2_get_ring_ent(ioat, idx + i);
  383. fill = desc->fill;
  384. fill->size = xfer_size;
  385. fill->src_data = src_data;
  386. fill->dst_addr = dest;
  387. fill->ctl = 0;
  388. fill->ctl_f.op = IOAT_OP_FILL;
  389. len -= xfer_size;
  390. dest += xfer_size;
  391. dump_desc_dbg(ioat, desc);
  392. } while (++i < num_descs);
  393. desc->txd.flags = flags;
  394. desc->len = total_len;
  395. fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  396. fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  397. fill->ctl_f.compl_write = 1;
  398. dump_desc_dbg(ioat, desc);
  399. /* we leave the channel locked to ensure in order submission */
  400. return &desc->txd;
  401. }
  402. static struct dma_async_tx_descriptor *
  403. __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
  404. dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
  405. size_t len, unsigned long flags)
  406. {
  407. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  408. struct ioat_ring_ent *compl_desc;
  409. struct ioat_ring_ent *desc;
  410. struct ioat_ring_ent *ext;
  411. size_t total_len = len;
  412. struct ioat_xor_descriptor *xor;
  413. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  414. struct ioat_dma_descriptor *hw;
  415. int num_descs, with_ext, idx, i;
  416. u32 offset = 0;
  417. u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
  418. BUG_ON(src_cnt < 2);
  419. num_descs = ioat2_xferlen_to_descs(ioat, len);
  420. /* we need 2x the number of descriptors to cover greater than 5
  421. * sources
  422. */
  423. if (src_cnt > 5) {
  424. with_ext = 1;
  425. num_descs *= 2;
  426. } else
  427. with_ext = 0;
  428. /* completion writes from the raid engine may pass completion
  429. * writes from the legacy engine, so we need one extra null
  430. * (legacy) descriptor to ensure all completion writes arrive in
  431. * order.
  432. */
  433. if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
  434. idx = ioat->head;
  435. else
  436. return NULL;
  437. i = 0;
  438. do {
  439. struct ioat_raw_descriptor *descs[2];
  440. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  441. int s;
  442. desc = ioat2_get_ring_ent(ioat, idx + i);
  443. xor = desc->xor;
  444. /* save a branch by unconditionally retrieving the
  445. * extended descriptor xor_set_src() knows to not write
  446. * to it in the single descriptor case
  447. */
  448. ext = ioat2_get_ring_ent(ioat, idx + i + 1);
  449. xor_ex = ext->xor_ex;
  450. descs[0] = (struct ioat_raw_descriptor *) xor;
  451. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  452. for (s = 0; s < src_cnt; s++)
  453. xor_set_src(descs, src[s], offset, s);
  454. xor->size = xfer_size;
  455. xor->dst_addr = dest + offset;
  456. xor->ctl = 0;
  457. xor->ctl_f.op = op;
  458. xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
  459. len -= xfer_size;
  460. offset += xfer_size;
  461. dump_desc_dbg(ioat, desc);
  462. } while ((i += 1 + with_ext) < num_descs);
  463. /* last xor descriptor carries the unmap parameters and fence bit */
  464. desc->txd.flags = flags;
  465. desc->len = total_len;
  466. if (result)
  467. desc->result = result;
  468. xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  469. /* completion descriptor carries interrupt bit */
  470. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  471. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  472. hw = compl_desc->hw;
  473. hw->ctl = 0;
  474. hw->ctl_f.null = 1;
  475. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  476. hw->ctl_f.compl_write = 1;
  477. hw->size = NULL_DESC_BUFFER_SIZE;
  478. dump_desc_dbg(ioat, compl_desc);
  479. /* we leave the channel locked to ensure in order submission */
  480. return &compl_desc->txd;
  481. }
  482. static struct dma_async_tx_descriptor *
  483. ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  484. unsigned int src_cnt, size_t len, unsigned long flags)
  485. {
  486. return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
  487. }
  488. struct dma_async_tx_descriptor *
  489. ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  490. unsigned int src_cnt, size_t len,
  491. enum sum_check_flags *result, unsigned long flags)
  492. {
  493. /* the cleanup routine only sets bits on validate failure, it
  494. * does not clear bits on validate success... so clear it here
  495. */
  496. *result = 0;
  497. return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
  498. src_cnt - 1, len, flags);
  499. }
  500. static void
  501. dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
  502. {
  503. struct device *dev = to_dev(&ioat->base);
  504. struct ioat_pq_descriptor *pq = desc->pq;
  505. struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
  506. struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
  507. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  508. int i;
  509. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  510. " sz: %#x ctl: %#x (op: %d int: %d compl: %d pq: '%s%s' src_cnt: %d)\n",
  511. desc_id(desc), (unsigned long long) desc->txd.phys,
  512. (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
  513. desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
  514. pq->ctl_f.compl_write,
  515. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  516. pq->ctl_f.src_cnt);
  517. for (i = 0; i < src_cnt; i++)
  518. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  519. (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
  520. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  521. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  522. }
  523. static struct dma_async_tx_descriptor *
  524. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  525. const dma_addr_t *dst, const dma_addr_t *src,
  526. unsigned int src_cnt, const unsigned char *scf,
  527. size_t len, unsigned long flags)
  528. {
  529. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  530. struct ioat_chan_common *chan = &ioat->base;
  531. struct ioat_ring_ent *compl_desc;
  532. struct ioat_ring_ent *desc;
  533. struct ioat_ring_ent *ext;
  534. size_t total_len = len;
  535. struct ioat_pq_descriptor *pq;
  536. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  537. struct ioat_dma_descriptor *hw;
  538. u32 offset = 0;
  539. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  540. int i, s, idx, with_ext, num_descs;
  541. dev_dbg(to_dev(chan), "%s\n", __func__);
  542. /* the engine requires at least two sources (we provide
  543. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  544. */
  545. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  546. num_descs = ioat2_xferlen_to_descs(ioat, len);
  547. /* we need 2x the number of descriptors to cover greater than 3
  548. * sources (we need 1 extra source in the q-only continuation
  549. * case and 3 extra sources in the p+q continuation case.
  550. */
  551. if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
  552. (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
  553. with_ext = 1;
  554. num_descs *= 2;
  555. } else
  556. with_ext = 0;
  557. /* completion writes from the raid engine may pass completion
  558. * writes from the legacy engine, so we need one extra null
  559. * (legacy) descriptor to ensure all completion writes arrive in
  560. * order.
  561. */
  562. if (likely(num_descs) &&
  563. ioat2_check_space_lock(ioat, num_descs+1) == 0)
  564. idx = ioat->head;
  565. else
  566. return NULL;
  567. i = 0;
  568. do {
  569. struct ioat_raw_descriptor *descs[2];
  570. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  571. desc = ioat2_get_ring_ent(ioat, idx + i);
  572. pq = desc->pq;
  573. /* save a branch by unconditionally retrieving the
  574. * extended descriptor pq_set_src() knows to not write
  575. * to it in the single descriptor case
  576. */
  577. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  578. pq_ex = ext->pq_ex;
  579. descs[0] = (struct ioat_raw_descriptor *) pq;
  580. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  581. for (s = 0; s < src_cnt; s++)
  582. pq_set_src(descs, src[s], offset, scf[s], s);
  583. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  584. if (dmaf_p_disabled_continue(flags))
  585. pq_set_src(descs, dst[1], offset, 1, s++);
  586. else if (dmaf_continue(flags)) {
  587. pq_set_src(descs, dst[0], offset, 0, s++);
  588. pq_set_src(descs, dst[1], offset, 1, s++);
  589. pq_set_src(descs, dst[1], offset, 0, s++);
  590. }
  591. pq->size = xfer_size;
  592. pq->p_addr = dst[0] + offset;
  593. pq->q_addr = dst[1] + offset;
  594. pq->ctl = 0;
  595. pq->ctl_f.op = op;
  596. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  597. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  598. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  599. len -= xfer_size;
  600. offset += xfer_size;
  601. } while ((i += 1 + with_ext) < num_descs);
  602. /* last pq descriptor carries the unmap parameters and fence bit */
  603. desc->txd.flags = flags;
  604. desc->len = total_len;
  605. if (result)
  606. desc->result = result;
  607. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  608. dump_pq_desc_dbg(ioat, desc, ext);
  609. /* completion descriptor carries interrupt bit */
  610. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  611. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  612. hw = compl_desc->hw;
  613. hw->ctl = 0;
  614. hw->ctl_f.null = 1;
  615. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  616. hw->ctl_f.compl_write = 1;
  617. hw->size = NULL_DESC_BUFFER_SIZE;
  618. dump_desc_dbg(ioat, compl_desc);
  619. /* we leave the channel locked to ensure in order submission */
  620. return &compl_desc->txd;
  621. }
  622. static struct dma_async_tx_descriptor *
  623. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  624. unsigned int src_cnt, const unsigned char *scf, size_t len,
  625. unsigned long flags)
  626. {
  627. /* specify valid address for disabled result */
  628. if (flags & DMA_PREP_PQ_DISABLE_P)
  629. dst[0] = dst[1];
  630. if (flags & DMA_PREP_PQ_DISABLE_Q)
  631. dst[1] = dst[0];
  632. /* handle the single source multiply case from the raid6
  633. * recovery path
  634. */
  635. if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
  636. dma_addr_t single_source[2];
  637. unsigned char single_source_coef[2];
  638. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  639. single_source[0] = src[0];
  640. single_source[1] = src[0];
  641. single_source_coef[0] = scf[0];
  642. single_source_coef[1] = 0;
  643. return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  644. single_source_coef, len, flags);
  645. } else
  646. return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
  647. len, flags);
  648. }
  649. struct dma_async_tx_descriptor *
  650. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  651. unsigned int src_cnt, const unsigned char *scf, size_t len,
  652. enum sum_check_flags *pqres, unsigned long flags)
  653. {
  654. /* specify valid address for disabled result */
  655. if (flags & DMA_PREP_PQ_DISABLE_P)
  656. pq[0] = pq[1];
  657. if (flags & DMA_PREP_PQ_DISABLE_Q)
  658. pq[1] = pq[0];
  659. /* the cleanup routine only sets bits on validate failure, it
  660. * does not clear bits on validate success... so clear it here
  661. */
  662. *pqres = 0;
  663. return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  664. flags);
  665. }
  666. static struct dma_async_tx_descriptor *
  667. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  668. unsigned int src_cnt, size_t len, unsigned long flags)
  669. {
  670. unsigned char scf[src_cnt];
  671. dma_addr_t pq[2];
  672. memset(scf, 0, src_cnt);
  673. pq[0] = dst;
  674. flags |= DMA_PREP_PQ_DISABLE_Q;
  675. pq[1] = dst; /* specify valid address for disabled result */
  676. return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  677. flags);
  678. }
  679. struct dma_async_tx_descriptor *
  680. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  681. unsigned int src_cnt, size_t len,
  682. enum sum_check_flags *result, unsigned long flags)
  683. {
  684. unsigned char scf[src_cnt];
  685. dma_addr_t pq[2];
  686. /* the cleanup routine only sets bits on validate failure, it
  687. * does not clear bits on validate success... so clear it here
  688. */
  689. *result = 0;
  690. memset(scf, 0, src_cnt);
  691. pq[0] = src[0];
  692. flags |= DMA_PREP_PQ_DISABLE_Q;
  693. pq[1] = pq[0]; /* specify valid address for disabled result */
  694. return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
  695. len, flags);
  696. }
  697. static struct dma_async_tx_descriptor *
  698. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  699. {
  700. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  701. struct ioat_ring_ent *desc;
  702. struct ioat_dma_descriptor *hw;
  703. if (ioat2_check_space_lock(ioat, 1) == 0)
  704. desc = ioat2_get_ring_ent(ioat, ioat->head);
  705. else
  706. return NULL;
  707. hw = desc->hw;
  708. hw->ctl = 0;
  709. hw->ctl_f.null = 1;
  710. hw->ctl_f.int_en = 1;
  711. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  712. hw->ctl_f.compl_write = 1;
  713. hw->size = NULL_DESC_BUFFER_SIZE;
  714. hw->src_addr = 0;
  715. hw->dst_addr = 0;
  716. desc->txd.flags = flags;
  717. desc->len = 1;
  718. dump_desc_dbg(ioat, desc);
  719. /* we leave the channel locked to ensure in order submission */
  720. return &desc->txd;
  721. }
  722. static void __devinit ioat3_dma_test_callback(void *dma_async_param)
  723. {
  724. struct completion *cmp = dma_async_param;
  725. complete(cmp);
  726. }
  727. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  728. static int __devinit ioat_xor_val_self_test(struct ioatdma_device *device)
  729. {
  730. int i, src_idx;
  731. struct page *dest;
  732. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  733. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  734. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  735. dma_addr_t dma_addr, dest_dma;
  736. struct dma_async_tx_descriptor *tx;
  737. struct dma_chan *dma_chan;
  738. dma_cookie_t cookie;
  739. u8 cmp_byte = 0;
  740. u32 cmp_word;
  741. u32 xor_val_result;
  742. int err = 0;
  743. struct completion cmp;
  744. unsigned long tmo;
  745. struct device *dev = &device->pdev->dev;
  746. struct dma_device *dma = &device->common;
  747. dev_dbg(dev, "%s\n", __func__);
  748. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  749. return 0;
  750. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  751. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  752. if (!xor_srcs[src_idx]) {
  753. while (src_idx--)
  754. __free_page(xor_srcs[src_idx]);
  755. return -ENOMEM;
  756. }
  757. }
  758. dest = alloc_page(GFP_KERNEL);
  759. if (!dest) {
  760. while (src_idx--)
  761. __free_page(xor_srcs[src_idx]);
  762. return -ENOMEM;
  763. }
  764. /* Fill in src buffers */
  765. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  766. u8 *ptr = page_address(xor_srcs[src_idx]);
  767. for (i = 0; i < PAGE_SIZE; i++)
  768. ptr[i] = (1 << src_idx);
  769. }
  770. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  771. cmp_byte ^= (u8) (1 << src_idx);
  772. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  773. (cmp_byte << 8) | cmp_byte;
  774. memset(page_address(dest), 0, PAGE_SIZE);
  775. dma_chan = container_of(dma->channels.next, struct dma_chan,
  776. device_node);
  777. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  778. err = -ENODEV;
  779. goto out;
  780. }
  781. /* test xor */
  782. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  783. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  784. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  785. DMA_TO_DEVICE);
  786. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  787. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  788. DMA_PREP_INTERRUPT);
  789. if (!tx) {
  790. dev_err(dev, "Self-test xor prep failed\n");
  791. err = -ENODEV;
  792. goto free_resources;
  793. }
  794. async_tx_ack(tx);
  795. init_completion(&cmp);
  796. tx->callback = ioat3_dma_test_callback;
  797. tx->callback_param = &cmp;
  798. cookie = tx->tx_submit(tx);
  799. if (cookie < 0) {
  800. dev_err(dev, "Self-test xor setup failed\n");
  801. err = -ENODEV;
  802. goto free_resources;
  803. }
  804. dma->device_issue_pending(dma_chan);
  805. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  806. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  807. dev_err(dev, "Self-test xor timed out\n");
  808. err = -ENODEV;
  809. goto free_resources;
  810. }
  811. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  812. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  813. u32 *ptr = page_address(dest);
  814. if (ptr[i] != cmp_word) {
  815. dev_err(dev, "Self-test xor failed compare\n");
  816. err = -ENODEV;
  817. goto free_resources;
  818. }
  819. }
  820. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_TO_DEVICE);
  821. /* skip validate if the capability is not present */
  822. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  823. goto free_resources;
  824. /* validate the sources with the destintation page */
  825. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  826. xor_val_srcs[i] = xor_srcs[i];
  827. xor_val_srcs[i] = dest;
  828. xor_val_result = 1;
  829. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  830. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  831. DMA_TO_DEVICE);
  832. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  833. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  834. &xor_val_result, DMA_PREP_INTERRUPT);
  835. if (!tx) {
  836. dev_err(dev, "Self-test zero prep failed\n");
  837. err = -ENODEV;
  838. goto free_resources;
  839. }
  840. async_tx_ack(tx);
  841. init_completion(&cmp);
  842. tx->callback = ioat3_dma_test_callback;
  843. tx->callback_param = &cmp;
  844. cookie = tx->tx_submit(tx);
  845. if (cookie < 0) {
  846. dev_err(dev, "Self-test zero setup failed\n");
  847. err = -ENODEV;
  848. goto free_resources;
  849. }
  850. dma->device_issue_pending(dma_chan);
  851. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  852. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  853. dev_err(dev, "Self-test validate timed out\n");
  854. err = -ENODEV;
  855. goto free_resources;
  856. }
  857. if (xor_val_result != 0) {
  858. dev_err(dev, "Self-test validate failed compare\n");
  859. err = -ENODEV;
  860. goto free_resources;
  861. }
  862. /* skip memset if the capability is not present */
  863. if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
  864. goto free_resources;
  865. /* test memset */
  866. dma_addr = dma_map_page(dev, dest, 0,
  867. PAGE_SIZE, DMA_FROM_DEVICE);
  868. tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  869. DMA_PREP_INTERRUPT);
  870. if (!tx) {
  871. dev_err(dev, "Self-test memset prep failed\n");
  872. err = -ENODEV;
  873. goto free_resources;
  874. }
  875. async_tx_ack(tx);
  876. init_completion(&cmp);
  877. tx->callback = ioat3_dma_test_callback;
  878. tx->callback_param = &cmp;
  879. cookie = tx->tx_submit(tx);
  880. if (cookie < 0) {
  881. dev_err(dev, "Self-test memset setup failed\n");
  882. err = -ENODEV;
  883. goto free_resources;
  884. }
  885. dma->device_issue_pending(dma_chan);
  886. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  887. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  888. dev_err(dev, "Self-test memset timed out\n");
  889. err = -ENODEV;
  890. goto free_resources;
  891. }
  892. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  893. u32 *ptr = page_address(dest);
  894. if (ptr[i]) {
  895. dev_err(dev, "Self-test memset failed compare\n");
  896. err = -ENODEV;
  897. goto free_resources;
  898. }
  899. }
  900. /* test for non-zero parity sum */
  901. xor_val_result = 0;
  902. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  903. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  904. DMA_TO_DEVICE);
  905. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  906. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  907. &xor_val_result, DMA_PREP_INTERRUPT);
  908. if (!tx) {
  909. dev_err(dev, "Self-test 2nd zero prep failed\n");
  910. err = -ENODEV;
  911. goto free_resources;
  912. }
  913. async_tx_ack(tx);
  914. init_completion(&cmp);
  915. tx->callback = ioat3_dma_test_callback;
  916. tx->callback_param = &cmp;
  917. cookie = tx->tx_submit(tx);
  918. if (cookie < 0) {
  919. dev_err(dev, "Self-test 2nd zero setup failed\n");
  920. err = -ENODEV;
  921. goto free_resources;
  922. }
  923. dma->device_issue_pending(dma_chan);
  924. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  925. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  926. dev_err(dev, "Self-test 2nd validate timed out\n");
  927. err = -ENODEV;
  928. goto free_resources;
  929. }
  930. if (xor_val_result != SUM_CHECK_P_RESULT) {
  931. dev_err(dev, "Self-test validate failed compare\n");
  932. err = -ENODEV;
  933. goto free_resources;
  934. }
  935. free_resources:
  936. dma->device_free_chan_resources(dma_chan);
  937. out:
  938. src_idx = IOAT_NUM_SRC_TEST;
  939. while (src_idx--)
  940. __free_page(xor_srcs[src_idx]);
  941. __free_page(dest);
  942. return err;
  943. }
  944. static int __devinit ioat3_dma_self_test(struct ioatdma_device *device)
  945. {
  946. int rc = ioat_dma_self_test(device);
  947. if (rc)
  948. return rc;
  949. rc = ioat_xor_val_self_test(device);
  950. if (rc)
  951. return rc;
  952. return 0;
  953. }
  954. static int ioat3_reset_hw(struct ioat_chan_common *chan)
  955. {
  956. /* throw away whatever the channel was doing and get it
  957. * initialized, with ioat3 specific workarounds
  958. */
  959. struct ioatdma_device *device = chan->device;
  960. struct pci_dev *pdev = device->pdev;
  961. u32 chanerr;
  962. u16 dev_id;
  963. int err;
  964. ioat2_quiesce(chan, msecs_to_jiffies(100));
  965. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  966. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  967. /* -= IOAT ver.3 workarounds =- */
  968. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  969. * that can cause stability issues for IOAT ver.3, and clear any
  970. * pending errors
  971. */
  972. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  973. err = pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  974. if (err) {
  975. dev_err(&pdev->dev, "channel error register unreachable\n");
  976. return err;
  977. }
  978. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  979. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  980. * (workaround for spurious config parity error after restart)
  981. */
  982. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  983. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  984. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  985. return ioat2_reset_sync(chan, msecs_to_jiffies(200));
  986. }
  987. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  988. {
  989. struct pci_dev *pdev = device->pdev;
  990. int dca_en = system_has_dca_enabled(pdev);
  991. struct dma_device *dma;
  992. struct dma_chan *c;
  993. struct ioat_chan_common *chan;
  994. bool is_raid_device = false;
  995. int err;
  996. u32 cap;
  997. device->enumerate_channels = ioat2_enumerate_channels;
  998. device->reset_hw = ioat3_reset_hw;
  999. device->self_test = ioat3_dma_self_test;
  1000. dma = &device->common;
  1001. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  1002. dma->device_issue_pending = ioat2_issue_pending;
  1003. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  1004. dma->device_free_chan_resources = ioat2_free_chan_resources;
  1005. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  1006. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  1007. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  1008. /* dca is incompatible with raid operations */
  1009. if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  1010. cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  1011. if (cap & IOAT_CAP_XOR) {
  1012. is_raid_device = true;
  1013. dma->max_xor = 8;
  1014. dma->xor_align = 6;
  1015. dma_cap_set(DMA_XOR, dma->cap_mask);
  1016. dma->device_prep_dma_xor = ioat3_prep_xor;
  1017. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1018. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  1019. }
  1020. if (cap & IOAT_CAP_PQ) {
  1021. is_raid_device = true;
  1022. dma_set_maxpq(dma, 8, 0);
  1023. dma->pq_align = 6;
  1024. dma_cap_set(DMA_PQ, dma->cap_mask);
  1025. dma->device_prep_dma_pq = ioat3_prep_pq;
  1026. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  1027. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  1028. if (!(cap & IOAT_CAP_XOR)) {
  1029. dma->max_xor = 8;
  1030. dma->xor_align = 6;
  1031. dma_cap_set(DMA_XOR, dma->cap_mask);
  1032. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1033. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1034. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1035. }
  1036. }
  1037. if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
  1038. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  1039. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  1040. }
  1041. if (is_raid_device) {
  1042. dma->device_tx_status = ioat3_tx_status;
  1043. device->cleanup_fn = ioat3_cleanup_event;
  1044. device->timer_fn = ioat3_timer_event;
  1045. } else {
  1046. dma->device_tx_status = ioat_dma_tx_status;
  1047. device->cleanup_fn = ioat2_cleanup_event;
  1048. device->timer_fn = ioat2_timer_event;
  1049. }
  1050. #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  1051. dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
  1052. dma->device_prep_dma_pq_val = NULL;
  1053. #endif
  1054. #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  1055. dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
  1056. dma->device_prep_dma_xor_val = NULL;
  1057. #endif
  1058. err = ioat_probe(device);
  1059. if (err)
  1060. return err;
  1061. ioat_set_tcp_copy_break(262144);
  1062. list_for_each_entry(c, &dma->channels, device_node) {
  1063. chan = to_chan_common(c);
  1064. writel(IOAT_DMA_DCA_ANY_CPU,
  1065. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1066. }
  1067. err = ioat_register(device);
  1068. if (err)
  1069. return err;
  1070. ioat_kobject_add(device, &ioat2_ktype);
  1071. if (dca)
  1072. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1073. return 0;
  1074. }