dma_v3.c 37 KB

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