dma_v3.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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: %#10.8x ctl: %#x (op: %#x 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. dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
  537. }
  538. static struct dma_async_tx_descriptor *
  539. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  540. const dma_addr_t *dst, const dma_addr_t *src,
  541. unsigned int src_cnt, const unsigned char *scf,
  542. size_t len, unsigned long flags)
  543. {
  544. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  545. struct ioat_chan_common *chan = &ioat->base;
  546. struct ioat_ring_ent *compl_desc;
  547. struct ioat_ring_ent *desc;
  548. struct ioat_ring_ent *ext;
  549. size_t total_len = len;
  550. struct ioat_pq_descriptor *pq;
  551. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  552. struct ioat_dma_descriptor *hw;
  553. u32 offset = 0;
  554. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  555. int i, s, idx, with_ext, num_descs;
  556. dev_dbg(to_dev(chan), "%s\n", __func__);
  557. /* the engine requires at least two sources (we provide
  558. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  559. */
  560. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  561. num_descs = ioat2_xferlen_to_descs(ioat, len);
  562. /* we need 2x the number of descriptors to cover greater than 3
  563. * sources (we need 1 extra source in the q-only continuation
  564. * case and 3 extra sources in the p+q continuation case.
  565. */
  566. if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
  567. (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
  568. with_ext = 1;
  569. num_descs *= 2;
  570. } else
  571. with_ext = 0;
  572. /* completion writes from the raid engine may pass completion
  573. * writes from the legacy engine, so we need one extra null
  574. * (legacy) descriptor to ensure all completion writes arrive in
  575. * order.
  576. */
  577. if (likely(num_descs) &&
  578. ioat2_check_space_lock(ioat, num_descs+1) == 0)
  579. idx = ioat->head;
  580. else
  581. return NULL;
  582. i = 0;
  583. do {
  584. struct ioat_raw_descriptor *descs[2];
  585. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  586. desc = ioat2_get_ring_ent(ioat, idx + i);
  587. pq = desc->pq;
  588. /* save a branch by unconditionally retrieving the
  589. * extended descriptor pq_set_src() knows to not write
  590. * to it in the single descriptor case
  591. */
  592. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  593. pq_ex = ext->pq_ex;
  594. descs[0] = (struct ioat_raw_descriptor *) pq;
  595. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  596. for (s = 0; s < src_cnt; s++)
  597. pq_set_src(descs, src[s], offset, scf[s], s);
  598. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  599. if (dmaf_p_disabled_continue(flags))
  600. pq_set_src(descs, dst[1], offset, 1, s++);
  601. else if (dmaf_continue(flags)) {
  602. pq_set_src(descs, dst[0], offset, 0, s++);
  603. pq_set_src(descs, dst[1], offset, 1, s++);
  604. pq_set_src(descs, dst[1], offset, 0, s++);
  605. }
  606. pq->size = xfer_size;
  607. pq->p_addr = dst[0] + offset;
  608. pq->q_addr = dst[1] + offset;
  609. pq->ctl = 0;
  610. pq->ctl_f.op = op;
  611. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  612. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  613. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  614. len -= xfer_size;
  615. offset += xfer_size;
  616. } while ((i += 1 + with_ext) < num_descs);
  617. /* last pq descriptor carries the unmap parameters and fence bit */
  618. desc->txd.flags = flags;
  619. desc->len = total_len;
  620. if (result)
  621. desc->result = result;
  622. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  623. dump_pq_desc_dbg(ioat, desc, ext);
  624. /* completion descriptor carries interrupt bit */
  625. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  626. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  627. hw = compl_desc->hw;
  628. hw->ctl = 0;
  629. hw->ctl_f.null = 1;
  630. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  631. hw->ctl_f.compl_write = 1;
  632. hw->size = NULL_DESC_BUFFER_SIZE;
  633. dump_desc_dbg(ioat, compl_desc);
  634. /* we leave the channel locked to ensure in order submission */
  635. return &compl_desc->txd;
  636. }
  637. static struct dma_async_tx_descriptor *
  638. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  639. unsigned int src_cnt, const unsigned char *scf, size_t len,
  640. unsigned long flags)
  641. {
  642. /* specify valid address for disabled result */
  643. if (flags & DMA_PREP_PQ_DISABLE_P)
  644. dst[0] = dst[1];
  645. if (flags & DMA_PREP_PQ_DISABLE_Q)
  646. dst[1] = dst[0];
  647. /* handle the single source multiply case from the raid6
  648. * recovery path
  649. */
  650. if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
  651. dma_addr_t single_source[2];
  652. unsigned char single_source_coef[2];
  653. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  654. single_source[0] = src[0];
  655. single_source[1] = src[0];
  656. single_source_coef[0] = scf[0];
  657. single_source_coef[1] = 0;
  658. return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  659. single_source_coef, len, flags);
  660. } else
  661. return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
  662. len, flags);
  663. }
  664. struct dma_async_tx_descriptor *
  665. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  666. unsigned int src_cnt, const unsigned char *scf, size_t len,
  667. enum sum_check_flags *pqres, unsigned long flags)
  668. {
  669. /* specify valid address for disabled result */
  670. if (flags & DMA_PREP_PQ_DISABLE_P)
  671. pq[0] = pq[1];
  672. if (flags & DMA_PREP_PQ_DISABLE_Q)
  673. pq[1] = pq[0];
  674. /* the cleanup routine only sets bits on validate failure, it
  675. * does not clear bits on validate success... so clear it here
  676. */
  677. *pqres = 0;
  678. return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  679. flags);
  680. }
  681. static struct dma_async_tx_descriptor *
  682. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  683. unsigned int src_cnt, size_t len, unsigned long flags)
  684. {
  685. unsigned char scf[src_cnt];
  686. dma_addr_t pq[2];
  687. memset(scf, 0, src_cnt);
  688. pq[0] = dst;
  689. flags |= DMA_PREP_PQ_DISABLE_Q;
  690. pq[1] = dst; /* specify valid address for disabled result */
  691. return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  692. flags);
  693. }
  694. struct dma_async_tx_descriptor *
  695. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  696. unsigned int src_cnt, size_t len,
  697. enum sum_check_flags *result, unsigned long flags)
  698. {
  699. unsigned char scf[src_cnt];
  700. dma_addr_t pq[2];
  701. /* the cleanup routine only sets bits on validate failure, it
  702. * does not clear bits on validate success... so clear it here
  703. */
  704. *result = 0;
  705. memset(scf, 0, src_cnt);
  706. pq[0] = src[0];
  707. flags |= DMA_PREP_PQ_DISABLE_Q;
  708. pq[1] = pq[0]; /* specify valid address for disabled result */
  709. return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
  710. len, flags);
  711. }
  712. static struct dma_async_tx_descriptor *
  713. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  714. {
  715. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  716. struct ioat_ring_ent *desc;
  717. struct ioat_dma_descriptor *hw;
  718. if (ioat2_check_space_lock(ioat, 1) == 0)
  719. desc = ioat2_get_ring_ent(ioat, ioat->head);
  720. else
  721. return NULL;
  722. hw = desc->hw;
  723. hw->ctl = 0;
  724. hw->ctl_f.null = 1;
  725. hw->ctl_f.int_en = 1;
  726. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  727. hw->ctl_f.compl_write = 1;
  728. hw->size = NULL_DESC_BUFFER_SIZE;
  729. hw->src_addr = 0;
  730. hw->dst_addr = 0;
  731. desc->txd.flags = flags;
  732. desc->len = 1;
  733. dump_desc_dbg(ioat, desc);
  734. /* we leave the channel locked to ensure in order submission */
  735. return &desc->txd;
  736. }
  737. static void ioat3_dma_test_callback(void *dma_async_param)
  738. {
  739. struct completion *cmp = dma_async_param;
  740. complete(cmp);
  741. }
  742. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  743. static int ioat_xor_val_self_test(struct ioatdma_device *device)
  744. {
  745. int i, src_idx;
  746. struct page *dest;
  747. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  748. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  749. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  750. dma_addr_t dma_addr, dest_dma;
  751. struct dma_async_tx_descriptor *tx;
  752. struct dma_chan *dma_chan;
  753. dma_cookie_t cookie;
  754. u8 cmp_byte = 0;
  755. u32 cmp_word;
  756. u32 xor_val_result;
  757. int err = 0;
  758. struct completion cmp;
  759. unsigned long tmo;
  760. struct device *dev = &device->pdev->dev;
  761. struct dma_device *dma = &device->common;
  762. u8 op = 0;
  763. dev_dbg(dev, "%s\n", __func__);
  764. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  765. return 0;
  766. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  767. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  768. if (!xor_srcs[src_idx]) {
  769. while (src_idx--)
  770. __free_page(xor_srcs[src_idx]);
  771. return -ENOMEM;
  772. }
  773. }
  774. dest = alloc_page(GFP_KERNEL);
  775. if (!dest) {
  776. while (src_idx--)
  777. __free_page(xor_srcs[src_idx]);
  778. return -ENOMEM;
  779. }
  780. /* Fill in src buffers */
  781. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  782. u8 *ptr = page_address(xor_srcs[src_idx]);
  783. for (i = 0; i < PAGE_SIZE; i++)
  784. ptr[i] = (1 << src_idx);
  785. }
  786. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  787. cmp_byte ^= (u8) (1 << src_idx);
  788. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  789. (cmp_byte << 8) | cmp_byte;
  790. memset(page_address(dest), 0, PAGE_SIZE);
  791. dma_chan = container_of(dma->channels.next, struct dma_chan,
  792. device_node);
  793. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  794. err = -ENODEV;
  795. goto out;
  796. }
  797. /* test xor */
  798. op = IOAT_OP_XOR;
  799. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  800. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  801. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  802. DMA_TO_DEVICE);
  803. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  804. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  805. DMA_PREP_INTERRUPT |
  806. DMA_COMPL_SKIP_SRC_UNMAP |
  807. DMA_COMPL_SKIP_DEST_UNMAP);
  808. if (!tx) {
  809. dev_err(dev, "Self-test xor prep failed\n");
  810. err = -ENODEV;
  811. goto dma_unmap;
  812. }
  813. async_tx_ack(tx);
  814. init_completion(&cmp);
  815. tx->callback = ioat3_dma_test_callback;
  816. tx->callback_param = &cmp;
  817. cookie = tx->tx_submit(tx);
  818. if (cookie < 0) {
  819. dev_err(dev, "Self-test xor setup failed\n");
  820. err = -ENODEV;
  821. goto dma_unmap;
  822. }
  823. dma->device_issue_pending(dma_chan);
  824. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  825. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  826. dev_err(dev, "Self-test xor timed out\n");
  827. err = -ENODEV;
  828. goto dma_unmap;
  829. }
  830. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  831. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  832. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  833. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  834. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  835. u32 *ptr = page_address(dest);
  836. if (ptr[i] != cmp_word) {
  837. dev_err(dev, "Self-test xor failed compare\n");
  838. err = -ENODEV;
  839. goto free_resources;
  840. }
  841. }
  842. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  843. /* skip validate if the capability is not present */
  844. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  845. goto free_resources;
  846. op = IOAT_OP_XOR_VAL;
  847. /* validate the sources with the destintation page */
  848. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  849. xor_val_srcs[i] = xor_srcs[i];
  850. xor_val_srcs[i] = dest;
  851. xor_val_result = 1;
  852. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  853. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  854. DMA_TO_DEVICE);
  855. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  856. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  857. &xor_val_result, DMA_PREP_INTERRUPT |
  858. DMA_COMPL_SKIP_SRC_UNMAP |
  859. DMA_COMPL_SKIP_DEST_UNMAP);
  860. if (!tx) {
  861. dev_err(dev, "Self-test zero prep failed\n");
  862. err = -ENODEV;
  863. goto dma_unmap;
  864. }
  865. async_tx_ack(tx);
  866. init_completion(&cmp);
  867. tx->callback = ioat3_dma_test_callback;
  868. tx->callback_param = &cmp;
  869. cookie = tx->tx_submit(tx);
  870. if (cookie < 0) {
  871. dev_err(dev, "Self-test zero setup failed\n");
  872. err = -ENODEV;
  873. goto dma_unmap;
  874. }
  875. dma->device_issue_pending(dma_chan);
  876. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  877. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  878. dev_err(dev, "Self-test validate timed out\n");
  879. err = -ENODEV;
  880. goto dma_unmap;
  881. }
  882. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  883. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  884. if (xor_val_result != 0) {
  885. dev_err(dev, "Self-test validate failed compare\n");
  886. err = -ENODEV;
  887. goto free_resources;
  888. }
  889. /* skip memset if the capability is not present */
  890. if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
  891. goto free_resources;
  892. /* test memset */
  893. op = IOAT_OP_FILL;
  894. dma_addr = dma_map_page(dev, dest, 0,
  895. PAGE_SIZE, DMA_FROM_DEVICE);
  896. tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  897. DMA_PREP_INTERRUPT |
  898. DMA_COMPL_SKIP_SRC_UNMAP |
  899. DMA_COMPL_SKIP_DEST_UNMAP);
  900. if (!tx) {
  901. dev_err(dev, "Self-test memset prep failed\n");
  902. err = -ENODEV;
  903. goto dma_unmap;
  904. }
  905. async_tx_ack(tx);
  906. init_completion(&cmp);
  907. tx->callback = ioat3_dma_test_callback;
  908. tx->callback_param = &cmp;
  909. cookie = tx->tx_submit(tx);
  910. if (cookie < 0) {
  911. dev_err(dev, "Self-test memset setup failed\n");
  912. err = -ENODEV;
  913. goto dma_unmap;
  914. }
  915. dma->device_issue_pending(dma_chan);
  916. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  917. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  918. dev_err(dev, "Self-test memset timed out\n");
  919. err = -ENODEV;
  920. goto dma_unmap;
  921. }
  922. dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
  923. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  924. u32 *ptr = page_address(dest);
  925. if (ptr[i]) {
  926. dev_err(dev, "Self-test memset failed compare\n");
  927. err = -ENODEV;
  928. goto free_resources;
  929. }
  930. }
  931. /* test for non-zero parity sum */
  932. op = IOAT_OP_XOR_VAL;
  933. xor_val_result = 0;
  934. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  935. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  936. DMA_TO_DEVICE);
  937. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  938. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  939. &xor_val_result, DMA_PREP_INTERRUPT |
  940. DMA_COMPL_SKIP_SRC_UNMAP |
  941. DMA_COMPL_SKIP_DEST_UNMAP);
  942. if (!tx) {
  943. dev_err(dev, "Self-test 2nd zero prep failed\n");
  944. err = -ENODEV;
  945. goto dma_unmap;
  946. }
  947. async_tx_ack(tx);
  948. init_completion(&cmp);
  949. tx->callback = ioat3_dma_test_callback;
  950. tx->callback_param = &cmp;
  951. cookie = tx->tx_submit(tx);
  952. if (cookie < 0) {
  953. dev_err(dev, "Self-test 2nd zero setup failed\n");
  954. err = -ENODEV;
  955. goto dma_unmap;
  956. }
  957. dma->device_issue_pending(dma_chan);
  958. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  959. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  960. dev_err(dev, "Self-test 2nd validate timed out\n");
  961. err = -ENODEV;
  962. goto dma_unmap;
  963. }
  964. if (xor_val_result != SUM_CHECK_P_RESULT) {
  965. dev_err(dev, "Self-test validate failed compare\n");
  966. err = -ENODEV;
  967. goto dma_unmap;
  968. }
  969. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  970. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  971. goto free_resources;
  972. dma_unmap:
  973. if (op == IOAT_OP_XOR) {
  974. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  975. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  976. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  977. DMA_TO_DEVICE);
  978. } else if (op == IOAT_OP_XOR_VAL) {
  979. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  980. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  981. DMA_TO_DEVICE);
  982. } else if (op == IOAT_OP_FILL)
  983. dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
  984. free_resources:
  985. dma->device_free_chan_resources(dma_chan);
  986. out:
  987. src_idx = IOAT_NUM_SRC_TEST;
  988. while (src_idx--)
  989. __free_page(xor_srcs[src_idx]);
  990. __free_page(dest);
  991. return err;
  992. }
  993. static int ioat3_dma_self_test(struct ioatdma_device *device)
  994. {
  995. int rc = ioat_dma_self_test(device);
  996. if (rc)
  997. return rc;
  998. rc = ioat_xor_val_self_test(device);
  999. if (rc)
  1000. return rc;
  1001. return 0;
  1002. }
  1003. static int ioat3_reset_hw(struct ioat_chan_common *chan)
  1004. {
  1005. /* throw away whatever the channel was doing and get it
  1006. * initialized, with ioat3 specific workarounds
  1007. */
  1008. struct ioatdma_device *device = chan->device;
  1009. struct pci_dev *pdev = device->pdev;
  1010. u32 chanerr;
  1011. u16 dev_id;
  1012. int err;
  1013. ioat2_quiesce(chan, msecs_to_jiffies(100));
  1014. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  1015. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  1016. /* clear any pending errors */
  1017. err = pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  1018. if (err) {
  1019. dev_err(&pdev->dev, "channel error register unreachable\n");
  1020. return err;
  1021. }
  1022. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  1023. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1024. * (workaround for spurious config parity error after restart)
  1025. */
  1026. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1027. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  1028. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  1029. return ioat2_reset_sync(chan, msecs_to_jiffies(200));
  1030. }
  1031. static bool is_jf_ioat(struct pci_dev *pdev)
  1032. {
  1033. switch (pdev->device) {
  1034. case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
  1035. case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
  1036. case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
  1037. case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
  1038. case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
  1039. case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
  1040. case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
  1041. case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
  1042. case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
  1043. case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
  1044. return true;
  1045. default:
  1046. return false;
  1047. }
  1048. }
  1049. static bool is_snb_ioat(struct pci_dev *pdev)
  1050. {
  1051. switch (pdev->device) {
  1052. case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
  1053. case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
  1054. case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
  1055. case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
  1056. case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
  1057. case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
  1058. case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
  1059. case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
  1060. case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
  1061. case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
  1062. return true;
  1063. default:
  1064. return false;
  1065. }
  1066. }
  1067. static bool is_ivb_ioat(struct pci_dev *pdev)
  1068. {
  1069. switch (pdev->device) {
  1070. case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
  1071. case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
  1072. case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
  1073. case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
  1074. case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
  1075. case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
  1076. case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
  1077. case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
  1078. case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
  1079. case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
  1080. return true;
  1081. default:
  1082. return false;
  1083. }
  1084. }
  1085. static bool is_hsw_ioat(struct pci_dev *pdev)
  1086. {
  1087. switch (pdev->device) {
  1088. case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
  1089. case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
  1090. case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
  1091. case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
  1092. case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
  1093. case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
  1094. case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
  1095. case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
  1096. case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
  1097. case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
  1098. return true;
  1099. default:
  1100. return false;
  1101. }
  1102. }
  1103. static bool is_xeon_cb32(struct pci_dev *pdev)
  1104. {
  1105. return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
  1106. is_hsw_ioat(pdev);
  1107. }
  1108. int ioat3_dma_probe(struct ioatdma_device *device, int dca)
  1109. {
  1110. struct pci_dev *pdev = device->pdev;
  1111. int dca_en = system_has_dca_enabled(pdev);
  1112. struct dma_device *dma;
  1113. struct dma_chan *c;
  1114. struct ioat_chan_common *chan;
  1115. bool is_raid_device = false;
  1116. int err;
  1117. u32 cap;
  1118. device->enumerate_channels = ioat2_enumerate_channels;
  1119. device->reset_hw = ioat3_reset_hw;
  1120. device->self_test = ioat3_dma_self_test;
  1121. dma = &device->common;
  1122. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  1123. dma->device_issue_pending = ioat2_issue_pending;
  1124. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  1125. dma->device_free_chan_resources = ioat2_free_chan_resources;
  1126. if (is_xeon_cb32(pdev))
  1127. dma->copy_align = 6;
  1128. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  1129. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  1130. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  1131. /* dca is incompatible with raid operations */
  1132. if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  1133. cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  1134. if (cap & IOAT_CAP_XOR) {
  1135. is_raid_device = true;
  1136. dma->max_xor = 8;
  1137. dma->xor_align = 6;
  1138. dma_cap_set(DMA_XOR, dma->cap_mask);
  1139. dma->device_prep_dma_xor = ioat3_prep_xor;
  1140. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1141. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  1142. }
  1143. if (cap & IOAT_CAP_PQ) {
  1144. is_raid_device = true;
  1145. dma_set_maxpq(dma, 8, 0);
  1146. dma->pq_align = 6;
  1147. dma_cap_set(DMA_PQ, dma->cap_mask);
  1148. dma->device_prep_dma_pq = ioat3_prep_pq;
  1149. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  1150. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  1151. if (!(cap & IOAT_CAP_XOR)) {
  1152. dma->max_xor = 8;
  1153. dma->xor_align = 6;
  1154. dma_cap_set(DMA_XOR, dma->cap_mask);
  1155. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1156. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1157. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1158. }
  1159. }
  1160. if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
  1161. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  1162. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  1163. }
  1164. dma->device_tx_status = ioat3_tx_status;
  1165. device->cleanup_fn = ioat3_cleanup_event;
  1166. device->timer_fn = ioat3_timer_event;
  1167. #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  1168. dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
  1169. dma->device_prep_dma_pq_val = NULL;
  1170. #endif
  1171. #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  1172. dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
  1173. dma->device_prep_dma_xor_val = NULL;
  1174. #endif
  1175. err = ioat_probe(device);
  1176. if (err)
  1177. return err;
  1178. ioat_set_tcp_copy_break(262144);
  1179. list_for_each_entry(c, &dma->channels, device_node) {
  1180. chan = to_chan_common(c);
  1181. writel(IOAT_DMA_DCA_ANY_CPU,
  1182. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1183. }
  1184. err = ioat_register(device);
  1185. if (err)
  1186. return err;
  1187. ioat_kobject_add(device, &ioat2_ktype);
  1188. if (dca)
  1189. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1190. return 0;
  1191. }