dma_v3.c 39 KB

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