dma_v3.c 37 KB

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