dma_v3.c 36 KB

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