dma_v3.c 36 KB

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