dma_v3.c 36 KB

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