dma_v3.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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 ioat3_timer_event(unsigned long data)
  300. {
  301. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  302. struct ioat_chan_common *chan = &ioat->base;
  303. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  304. dma_addr_t phys_complete;
  305. u64 status;
  306. status = ioat_chansts(chan);
  307. /* when halted due to errors check for channel
  308. * programming errors before advancing the completion state
  309. */
  310. if (is_ioat_halted(status)) {
  311. u32 chanerr;
  312. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  313. dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
  314. __func__, chanerr);
  315. if (test_bit(IOAT_RUN, &chan->state))
  316. BUG_ON(is_ioat_bug(chanerr));
  317. else /* we never got off the ground */
  318. return;
  319. }
  320. /* if we haven't made progress and we have already
  321. * acknowledged a pending completion once, then be more
  322. * forceful with a restart
  323. */
  324. spin_lock_bh(&chan->cleanup_lock);
  325. if (ioat_cleanup_preamble(chan, &phys_complete))
  326. __cleanup(ioat, phys_complete);
  327. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
  328. spin_lock_bh(&ioat->prep_lock);
  329. ioat3_restart_channel(ioat);
  330. spin_unlock_bh(&ioat->prep_lock);
  331. } else {
  332. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  333. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  334. }
  335. spin_unlock_bh(&chan->cleanup_lock);
  336. } else {
  337. u16 active;
  338. /* if the ring is idle, empty, and oversized try to step
  339. * down the size
  340. */
  341. spin_lock_bh(&chan->cleanup_lock);
  342. spin_lock_bh(&ioat->prep_lock);
  343. active = ioat2_ring_active(ioat);
  344. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  345. reshape_ring(ioat, ioat->alloc_order-1);
  346. spin_unlock_bh(&ioat->prep_lock);
  347. spin_unlock_bh(&chan->cleanup_lock);
  348. /* keep shrinking until we get back to our minimum
  349. * default size
  350. */
  351. if (ioat->alloc_order > ioat_get_alloc_order())
  352. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  353. }
  354. }
  355. static enum dma_status
  356. ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  357. struct dma_tx_state *txstate)
  358. {
  359. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  360. enum dma_status ret;
  361. ret = dma_cookie_status(c, cookie, txstate);
  362. if (ret == DMA_SUCCESS)
  363. return ret;
  364. ioat3_cleanup(ioat);
  365. return dma_cookie_status(c, cookie, txstate);
  366. }
  367. static struct dma_async_tx_descriptor *
  368. ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
  369. size_t len, unsigned long flags)
  370. {
  371. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  372. struct ioat_ring_ent *desc;
  373. size_t total_len = len;
  374. struct ioat_fill_descriptor *fill;
  375. u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
  376. int num_descs, idx, i;
  377. num_descs = ioat2_xferlen_to_descs(ioat, len);
  378. if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0)
  379. idx = ioat->head;
  380. else
  381. return NULL;
  382. i = 0;
  383. do {
  384. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  385. desc = ioat2_get_ring_ent(ioat, idx + i);
  386. fill = desc->fill;
  387. fill->size = xfer_size;
  388. fill->src_data = src_data;
  389. fill->dst_addr = dest;
  390. fill->ctl = 0;
  391. fill->ctl_f.op = IOAT_OP_FILL;
  392. len -= xfer_size;
  393. dest += xfer_size;
  394. dump_desc_dbg(ioat, desc);
  395. } while (++i < num_descs);
  396. desc->txd.flags = flags;
  397. desc->len = total_len;
  398. fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  399. fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  400. fill->ctl_f.compl_write = 1;
  401. dump_desc_dbg(ioat, desc);
  402. /* we leave the channel locked to ensure in order submission */
  403. return &desc->txd;
  404. }
  405. static struct dma_async_tx_descriptor *
  406. __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
  407. dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
  408. size_t len, unsigned long flags)
  409. {
  410. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  411. struct ioat_ring_ent *compl_desc;
  412. struct ioat_ring_ent *desc;
  413. struct ioat_ring_ent *ext;
  414. size_t total_len = len;
  415. struct ioat_xor_descriptor *xor;
  416. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  417. struct ioat_dma_descriptor *hw;
  418. int num_descs, with_ext, idx, i;
  419. u32 offset = 0;
  420. u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
  421. BUG_ON(src_cnt < 2);
  422. num_descs = ioat2_xferlen_to_descs(ioat, len);
  423. /* we need 2x the number of descriptors to cover greater than 5
  424. * sources
  425. */
  426. if (src_cnt > 5) {
  427. with_ext = 1;
  428. num_descs *= 2;
  429. } else
  430. with_ext = 0;
  431. /* completion writes from the raid engine may pass completion
  432. * writes from the legacy engine, so we need one extra null
  433. * (legacy) descriptor to ensure all completion writes arrive in
  434. * order.
  435. */
  436. if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
  437. idx = ioat->head;
  438. else
  439. return NULL;
  440. i = 0;
  441. do {
  442. struct ioat_raw_descriptor *descs[2];
  443. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  444. int s;
  445. desc = ioat2_get_ring_ent(ioat, idx + i);
  446. xor = desc->xor;
  447. /* save a branch by unconditionally retrieving the
  448. * extended descriptor xor_set_src() knows to not write
  449. * to it in the single descriptor case
  450. */
  451. ext = ioat2_get_ring_ent(ioat, idx + i + 1);
  452. xor_ex = ext->xor_ex;
  453. descs[0] = (struct ioat_raw_descriptor *) xor;
  454. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  455. for (s = 0; s < src_cnt; s++)
  456. xor_set_src(descs, src[s], offset, s);
  457. xor->size = xfer_size;
  458. xor->dst_addr = dest + offset;
  459. xor->ctl = 0;
  460. xor->ctl_f.op = op;
  461. xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
  462. len -= xfer_size;
  463. offset += xfer_size;
  464. dump_desc_dbg(ioat, desc);
  465. } while ((i += 1 + with_ext) < num_descs);
  466. /* last xor descriptor carries the unmap parameters and fence bit */
  467. desc->txd.flags = flags;
  468. desc->len = total_len;
  469. if (result)
  470. desc->result = result;
  471. xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  472. /* completion descriptor carries interrupt bit */
  473. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  474. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  475. hw = compl_desc->hw;
  476. hw->ctl = 0;
  477. hw->ctl_f.null = 1;
  478. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  479. hw->ctl_f.compl_write = 1;
  480. hw->size = NULL_DESC_BUFFER_SIZE;
  481. dump_desc_dbg(ioat, compl_desc);
  482. /* we leave the channel locked to ensure in order submission */
  483. return &compl_desc->txd;
  484. }
  485. static struct dma_async_tx_descriptor *
  486. ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  487. unsigned int src_cnt, size_t len, unsigned long flags)
  488. {
  489. return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
  490. }
  491. struct dma_async_tx_descriptor *
  492. ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  493. unsigned int src_cnt, size_t len,
  494. enum sum_check_flags *result, unsigned long flags)
  495. {
  496. /* the cleanup routine only sets bits on validate failure, it
  497. * does not clear bits on validate success... so clear it here
  498. */
  499. *result = 0;
  500. return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
  501. src_cnt - 1, len, flags);
  502. }
  503. static void
  504. dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
  505. {
  506. struct device *dev = to_dev(&ioat->base);
  507. struct ioat_pq_descriptor *pq = desc->pq;
  508. struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
  509. struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
  510. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  511. int i;
  512. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  513. " sz: %#x ctl: %#x (op: %d int: %d compl: %d pq: '%s%s' src_cnt: %d)\n",
  514. desc_id(desc), (unsigned long long) desc->txd.phys,
  515. (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
  516. desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
  517. pq->ctl_f.compl_write,
  518. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  519. pq->ctl_f.src_cnt);
  520. for (i = 0; i < src_cnt; i++)
  521. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  522. (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
  523. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  524. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  525. }
  526. static struct dma_async_tx_descriptor *
  527. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  528. const dma_addr_t *dst, const dma_addr_t *src,
  529. unsigned int src_cnt, const unsigned char *scf,
  530. size_t len, unsigned long flags)
  531. {
  532. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  533. struct ioat_chan_common *chan = &ioat->base;
  534. struct ioat_ring_ent *compl_desc;
  535. struct ioat_ring_ent *desc;
  536. struct ioat_ring_ent *ext;
  537. size_t total_len = len;
  538. struct ioat_pq_descriptor *pq;
  539. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  540. struct ioat_dma_descriptor *hw;
  541. u32 offset = 0;
  542. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  543. int i, s, idx, with_ext, num_descs;
  544. dev_dbg(to_dev(chan), "%s\n", __func__);
  545. /* the engine requires at least two sources (we provide
  546. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  547. */
  548. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  549. num_descs = ioat2_xferlen_to_descs(ioat, len);
  550. /* we need 2x the number of descriptors to cover greater than 3
  551. * sources (we need 1 extra source in the q-only continuation
  552. * case and 3 extra sources in the p+q continuation case.
  553. */
  554. if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
  555. (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
  556. with_ext = 1;
  557. num_descs *= 2;
  558. } else
  559. with_ext = 0;
  560. /* completion writes from the raid engine may pass completion
  561. * writes from the legacy engine, so we need one extra null
  562. * (legacy) descriptor to ensure all completion writes arrive in
  563. * order.
  564. */
  565. if (likely(num_descs) &&
  566. ioat2_check_space_lock(ioat, num_descs+1) == 0)
  567. idx = ioat->head;
  568. else
  569. return NULL;
  570. i = 0;
  571. do {
  572. struct ioat_raw_descriptor *descs[2];
  573. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  574. desc = ioat2_get_ring_ent(ioat, idx + i);
  575. pq = desc->pq;
  576. /* save a branch by unconditionally retrieving the
  577. * extended descriptor pq_set_src() knows to not write
  578. * to it in the single descriptor case
  579. */
  580. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  581. pq_ex = ext->pq_ex;
  582. descs[0] = (struct ioat_raw_descriptor *) pq;
  583. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  584. for (s = 0; s < src_cnt; s++)
  585. pq_set_src(descs, src[s], offset, scf[s], s);
  586. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  587. if (dmaf_p_disabled_continue(flags))
  588. pq_set_src(descs, dst[1], offset, 1, s++);
  589. else if (dmaf_continue(flags)) {
  590. pq_set_src(descs, dst[0], offset, 0, s++);
  591. pq_set_src(descs, dst[1], offset, 1, s++);
  592. pq_set_src(descs, dst[1], offset, 0, s++);
  593. }
  594. pq->size = xfer_size;
  595. pq->p_addr = dst[0] + offset;
  596. pq->q_addr = dst[1] + offset;
  597. pq->ctl = 0;
  598. pq->ctl_f.op = op;
  599. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  600. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  601. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  602. len -= xfer_size;
  603. offset += xfer_size;
  604. } while ((i += 1 + with_ext) < num_descs);
  605. /* last pq descriptor carries the unmap parameters and fence bit */
  606. desc->txd.flags = flags;
  607. desc->len = total_len;
  608. if (result)
  609. desc->result = result;
  610. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  611. dump_pq_desc_dbg(ioat, desc, ext);
  612. /* completion descriptor carries interrupt bit */
  613. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  614. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  615. hw = compl_desc->hw;
  616. hw->ctl = 0;
  617. hw->ctl_f.null = 1;
  618. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  619. hw->ctl_f.compl_write = 1;
  620. hw->size = NULL_DESC_BUFFER_SIZE;
  621. dump_desc_dbg(ioat, compl_desc);
  622. /* we leave the channel locked to ensure in order submission */
  623. return &compl_desc->txd;
  624. }
  625. static struct dma_async_tx_descriptor *
  626. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  627. unsigned int src_cnt, const unsigned char *scf, size_t len,
  628. unsigned long flags)
  629. {
  630. /* specify valid address for disabled result */
  631. if (flags & DMA_PREP_PQ_DISABLE_P)
  632. dst[0] = dst[1];
  633. if (flags & DMA_PREP_PQ_DISABLE_Q)
  634. dst[1] = dst[0];
  635. /* handle the single source multiply case from the raid6
  636. * recovery path
  637. */
  638. if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
  639. dma_addr_t single_source[2];
  640. unsigned char single_source_coef[2];
  641. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  642. single_source[0] = src[0];
  643. single_source[1] = src[0];
  644. single_source_coef[0] = scf[0];
  645. single_source_coef[1] = 0;
  646. return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  647. single_source_coef, len, flags);
  648. } else
  649. return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
  650. len, flags);
  651. }
  652. struct dma_async_tx_descriptor *
  653. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  654. unsigned int src_cnt, const unsigned char *scf, size_t len,
  655. enum sum_check_flags *pqres, unsigned long flags)
  656. {
  657. /* specify valid address for disabled result */
  658. if (flags & DMA_PREP_PQ_DISABLE_P)
  659. pq[0] = pq[1];
  660. if (flags & DMA_PREP_PQ_DISABLE_Q)
  661. pq[1] = pq[0];
  662. /* the cleanup routine only sets bits on validate failure, it
  663. * does not clear bits on validate success... so clear it here
  664. */
  665. *pqres = 0;
  666. return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  667. flags);
  668. }
  669. static struct dma_async_tx_descriptor *
  670. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  671. unsigned int src_cnt, size_t len, unsigned long flags)
  672. {
  673. unsigned char scf[src_cnt];
  674. dma_addr_t pq[2];
  675. memset(scf, 0, src_cnt);
  676. pq[0] = dst;
  677. flags |= DMA_PREP_PQ_DISABLE_Q;
  678. pq[1] = dst; /* specify valid address for disabled result */
  679. return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  680. flags);
  681. }
  682. struct dma_async_tx_descriptor *
  683. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  684. unsigned int src_cnt, size_t len,
  685. enum sum_check_flags *result, unsigned long flags)
  686. {
  687. unsigned char scf[src_cnt];
  688. dma_addr_t pq[2];
  689. /* the cleanup routine only sets bits on validate failure, it
  690. * does not clear bits on validate success... so clear it here
  691. */
  692. *result = 0;
  693. memset(scf, 0, src_cnt);
  694. pq[0] = src[0];
  695. flags |= DMA_PREP_PQ_DISABLE_Q;
  696. pq[1] = pq[0]; /* specify valid address for disabled result */
  697. return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
  698. len, flags);
  699. }
  700. static struct dma_async_tx_descriptor *
  701. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  702. {
  703. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  704. struct ioat_ring_ent *desc;
  705. struct ioat_dma_descriptor *hw;
  706. if (ioat2_check_space_lock(ioat, 1) == 0)
  707. desc = ioat2_get_ring_ent(ioat, ioat->head);
  708. else
  709. return NULL;
  710. hw = desc->hw;
  711. hw->ctl = 0;
  712. hw->ctl_f.null = 1;
  713. hw->ctl_f.int_en = 1;
  714. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  715. hw->ctl_f.compl_write = 1;
  716. hw->size = NULL_DESC_BUFFER_SIZE;
  717. hw->src_addr = 0;
  718. hw->dst_addr = 0;
  719. desc->txd.flags = flags;
  720. desc->len = 1;
  721. dump_desc_dbg(ioat, desc);
  722. /* we leave the channel locked to ensure in order submission */
  723. return &desc->txd;
  724. }
  725. static void __devinit ioat3_dma_test_callback(void *dma_async_param)
  726. {
  727. struct completion *cmp = dma_async_param;
  728. complete(cmp);
  729. }
  730. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  731. static int __devinit ioat_xor_val_self_test(struct ioatdma_device *device)
  732. {
  733. int i, src_idx;
  734. struct page *dest;
  735. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  736. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  737. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  738. dma_addr_t dma_addr, dest_dma;
  739. struct dma_async_tx_descriptor *tx;
  740. struct dma_chan *dma_chan;
  741. dma_cookie_t cookie;
  742. u8 cmp_byte = 0;
  743. u32 cmp_word;
  744. u32 xor_val_result;
  745. int err = 0;
  746. struct completion cmp;
  747. unsigned long tmo;
  748. struct device *dev = &device->pdev->dev;
  749. struct dma_device *dma = &device->common;
  750. u8 op = 0;
  751. dev_dbg(dev, "%s\n", __func__);
  752. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  753. return 0;
  754. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  755. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  756. if (!xor_srcs[src_idx]) {
  757. while (src_idx--)
  758. __free_page(xor_srcs[src_idx]);
  759. return -ENOMEM;
  760. }
  761. }
  762. dest = alloc_page(GFP_KERNEL);
  763. if (!dest) {
  764. while (src_idx--)
  765. __free_page(xor_srcs[src_idx]);
  766. return -ENOMEM;
  767. }
  768. /* Fill in src buffers */
  769. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  770. u8 *ptr = page_address(xor_srcs[src_idx]);
  771. for (i = 0; i < PAGE_SIZE; i++)
  772. ptr[i] = (1 << src_idx);
  773. }
  774. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  775. cmp_byte ^= (u8) (1 << src_idx);
  776. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  777. (cmp_byte << 8) | cmp_byte;
  778. memset(page_address(dest), 0, PAGE_SIZE);
  779. dma_chan = container_of(dma->channels.next, struct dma_chan,
  780. device_node);
  781. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  782. err = -ENODEV;
  783. goto out;
  784. }
  785. /* test xor */
  786. op = IOAT_OP_XOR;
  787. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  788. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  789. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  790. DMA_TO_DEVICE);
  791. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  792. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  793. DMA_PREP_INTERRUPT |
  794. DMA_COMPL_SKIP_SRC_UNMAP |
  795. DMA_COMPL_SKIP_DEST_UNMAP);
  796. if (!tx) {
  797. dev_err(dev, "Self-test xor prep failed\n");
  798. err = -ENODEV;
  799. goto dma_unmap;
  800. }
  801. async_tx_ack(tx);
  802. init_completion(&cmp);
  803. tx->callback = ioat3_dma_test_callback;
  804. tx->callback_param = &cmp;
  805. cookie = tx->tx_submit(tx);
  806. if (cookie < 0) {
  807. dev_err(dev, "Self-test xor setup failed\n");
  808. err = -ENODEV;
  809. goto dma_unmap;
  810. }
  811. dma->device_issue_pending(dma_chan);
  812. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  813. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  814. dev_err(dev, "Self-test xor timed out\n");
  815. err = -ENODEV;
  816. goto dma_unmap;
  817. }
  818. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  819. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  820. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  821. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  822. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  823. u32 *ptr = page_address(dest);
  824. if (ptr[i] != cmp_word) {
  825. dev_err(dev, "Self-test xor failed compare\n");
  826. err = -ENODEV;
  827. goto free_resources;
  828. }
  829. }
  830. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_TO_DEVICE);
  831. /* skip validate if the capability is not present */
  832. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  833. goto free_resources;
  834. op = IOAT_OP_XOR_VAL;
  835. /* validate the sources with the destintation page */
  836. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  837. xor_val_srcs[i] = xor_srcs[i];
  838. xor_val_srcs[i] = dest;
  839. xor_val_result = 1;
  840. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  841. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  842. DMA_TO_DEVICE);
  843. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  844. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  845. &xor_val_result, DMA_PREP_INTERRUPT |
  846. DMA_COMPL_SKIP_SRC_UNMAP |
  847. DMA_COMPL_SKIP_DEST_UNMAP);
  848. if (!tx) {
  849. dev_err(dev, "Self-test zero prep failed\n");
  850. err = -ENODEV;
  851. goto dma_unmap;
  852. }
  853. async_tx_ack(tx);
  854. init_completion(&cmp);
  855. tx->callback = ioat3_dma_test_callback;
  856. tx->callback_param = &cmp;
  857. cookie = tx->tx_submit(tx);
  858. if (cookie < 0) {
  859. dev_err(dev, "Self-test zero setup failed\n");
  860. err = -ENODEV;
  861. goto dma_unmap;
  862. }
  863. dma->device_issue_pending(dma_chan);
  864. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  865. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  866. dev_err(dev, "Self-test validate timed out\n");
  867. err = -ENODEV;
  868. goto dma_unmap;
  869. }
  870. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  871. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  872. if (xor_val_result != 0) {
  873. dev_err(dev, "Self-test validate failed compare\n");
  874. err = -ENODEV;
  875. goto free_resources;
  876. }
  877. /* skip memset if the capability is not present */
  878. if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
  879. goto free_resources;
  880. /* test memset */
  881. op = IOAT_OP_FILL;
  882. dma_addr = dma_map_page(dev, dest, 0,
  883. PAGE_SIZE, DMA_FROM_DEVICE);
  884. tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  885. DMA_PREP_INTERRUPT |
  886. DMA_COMPL_SKIP_SRC_UNMAP |
  887. DMA_COMPL_SKIP_DEST_UNMAP);
  888. if (!tx) {
  889. dev_err(dev, "Self-test memset prep failed\n");
  890. err = -ENODEV;
  891. goto dma_unmap;
  892. }
  893. async_tx_ack(tx);
  894. init_completion(&cmp);
  895. tx->callback = ioat3_dma_test_callback;
  896. tx->callback_param = &cmp;
  897. cookie = tx->tx_submit(tx);
  898. if (cookie < 0) {
  899. dev_err(dev, "Self-test memset setup failed\n");
  900. err = -ENODEV;
  901. goto dma_unmap;
  902. }
  903. dma->device_issue_pending(dma_chan);
  904. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  905. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  906. dev_err(dev, "Self-test memset timed out\n");
  907. err = -ENODEV;
  908. goto dma_unmap;
  909. }
  910. dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
  911. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  912. u32 *ptr = page_address(dest);
  913. if (ptr[i]) {
  914. dev_err(dev, "Self-test memset failed compare\n");
  915. err = -ENODEV;
  916. goto free_resources;
  917. }
  918. }
  919. /* test for non-zero parity sum */
  920. op = IOAT_OP_XOR_VAL;
  921. xor_val_result = 0;
  922. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  923. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  924. DMA_TO_DEVICE);
  925. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  926. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  927. &xor_val_result, DMA_PREP_INTERRUPT |
  928. DMA_COMPL_SKIP_SRC_UNMAP |
  929. DMA_COMPL_SKIP_DEST_UNMAP);
  930. if (!tx) {
  931. dev_err(dev, "Self-test 2nd zero prep failed\n");
  932. err = -ENODEV;
  933. goto dma_unmap;
  934. }
  935. async_tx_ack(tx);
  936. init_completion(&cmp);
  937. tx->callback = ioat3_dma_test_callback;
  938. tx->callback_param = &cmp;
  939. cookie = tx->tx_submit(tx);
  940. if (cookie < 0) {
  941. dev_err(dev, "Self-test 2nd zero setup failed\n");
  942. err = -ENODEV;
  943. goto dma_unmap;
  944. }
  945. dma->device_issue_pending(dma_chan);
  946. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  947. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  948. dev_err(dev, "Self-test 2nd validate timed out\n");
  949. err = -ENODEV;
  950. goto dma_unmap;
  951. }
  952. if (xor_val_result != SUM_CHECK_P_RESULT) {
  953. dev_err(dev, "Self-test validate failed compare\n");
  954. err = -ENODEV;
  955. goto dma_unmap;
  956. }
  957. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  958. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  959. goto free_resources;
  960. dma_unmap:
  961. if (op == IOAT_OP_XOR) {
  962. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  963. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  964. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  965. DMA_TO_DEVICE);
  966. } else if (op == IOAT_OP_XOR_VAL) {
  967. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  968. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  969. DMA_TO_DEVICE);
  970. } else if (op == IOAT_OP_FILL)
  971. dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
  972. free_resources:
  973. dma->device_free_chan_resources(dma_chan);
  974. out:
  975. src_idx = IOAT_NUM_SRC_TEST;
  976. while (src_idx--)
  977. __free_page(xor_srcs[src_idx]);
  978. __free_page(dest);
  979. return err;
  980. }
  981. static int __devinit ioat3_dma_self_test(struct ioatdma_device *device)
  982. {
  983. int rc = ioat_dma_self_test(device);
  984. if (rc)
  985. return rc;
  986. rc = ioat_xor_val_self_test(device);
  987. if (rc)
  988. return rc;
  989. return 0;
  990. }
  991. static int ioat3_reset_hw(struct ioat_chan_common *chan)
  992. {
  993. /* throw away whatever the channel was doing and get it
  994. * initialized, with ioat3 specific workarounds
  995. */
  996. struct ioatdma_device *device = chan->device;
  997. struct pci_dev *pdev = device->pdev;
  998. u32 chanerr;
  999. u16 dev_id;
  1000. int err;
  1001. ioat2_quiesce(chan, msecs_to_jiffies(100));
  1002. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  1003. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  1004. /* clear any pending errors */
  1005. err = pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  1006. if (err) {
  1007. dev_err(&pdev->dev, "channel error register unreachable\n");
  1008. return err;
  1009. }
  1010. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  1011. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1012. * (workaround for spurious config parity error after restart)
  1013. */
  1014. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1015. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  1016. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  1017. return ioat2_reset_sync(chan, msecs_to_jiffies(200));
  1018. }
  1019. static bool is_jf_ioat(struct pci_dev *pdev)
  1020. {
  1021. switch (pdev->device) {
  1022. case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
  1023. case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
  1024. case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
  1025. case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
  1026. case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
  1027. case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
  1028. case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
  1029. case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
  1030. case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
  1031. case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
  1032. return true;
  1033. default:
  1034. return false;
  1035. }
  1036. }
  1037. static bool is_snb_ioat(struct pci_dev *pdev)
  1038. {
  1039. switch (pdev->device) {
  1040. case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
  1041. case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
  1042. case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
  1043. case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
  1044. case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
  1045. case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
  1046. case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
  1047. case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
  1048. case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
  1049. case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
  1050. return true;
  1051. default:
  1052. return false;
  1053. }
  1054. }
  1055. static bool is_ivb_ioat(struct pci_dev *pdev)
  1056. {
  1057. switch (pdev->device) {
  1058. case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
  1059. case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
  1060. case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
  1061. case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
  1062. case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
  1063. case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
  1064. case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
  1065. case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
  1066. case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
  1067. case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
  1068. return true;
  1069. default:
  1070. return false;
  1071. }
  1072. }
  1073. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  1074. {
  1075. struct pci_dev *pdev = device->pdev;
  1076. int dca_en = system_has_dca_enabled(pdev);
  1077. struct dma_device *dma;
  1078. struct dma_chan *c;
  1079. struct ioat_chan_common *chan;
  1080. bool is_raid_device = false;
  1081. int err;
  1082. u32 cap;
  1083. device->enumerate_channels = ioat2_enumerate_channels;
  1084. device->reset_hw = ioat3_reset_hw;
  1085. device->self_test = ioat3_dma_self_test;
  1086. dma = &device->common;
  1087. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  1088. dma->device_issue_pending = ioat2_issue_pending;
  1089. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  1090. dma->device_free_chan_resources = ioat2_free_chan_resources;
  1091. if (is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev))
  1092. dma->copy_align = 6;
  1093. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  1094. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  1095. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  1096. /* dca is incompatible with raid operations */
  1097. if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  1098. cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  1099. if (cap & IOAT_CAP_XOR) {
  1100. is_raid_device = true;
  1101. dma->max_xor = 8;
  1102. dma->xor_align = 6;
  1103. dma_cap_set(DMA_XOR, dma->cap_mask);
  1104. dma->device_prep_dma_xor = ioat3_prep_xor;
  1105. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1106. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  1107. }
  1108. if (cap & IOAT_CAP_PQ) {
  1109. is_raid_device = true;
  1110. dma_set_maxpq(dma, 8, 0);
  1111. dma->pq_align = 6;
  1112. dma_cap_set(DMA_PQ, dma->cap_mask);
  1113. dma->device_prep_dma_pq = ioat3_prep_pq;
  1114. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  1115. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  1116. if (!(cap & IOAT_CAP_XOR)) {
  1117. dma->max_xor = 8;
  1118. dma->xor_align = 6;
  1119. dma_cap_set(DMA_XOR, dma->cap_mask);
  1120. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1121. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1122. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1123. }
  1124. }
  1125. if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
  1126. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  1127. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  1128. }
  1129. if (is_raid_device) {
  1130. dma->device_tx_status = ioat3_tx_status;
  1131. device->cleanup_fn = ioat3_cleanup_event;
  1132. device->timer_fn = ioat3_timer_event;
  1133. } else {
  1134. dma->device_tx_status = ioat_dma_tx_status;
  1135. device->cleanup_fn = ioat2_cleanup_event;
  1136. device->timer_fn = ioat2_timer_event;
  1137. }
  1138. #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  1139. dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
  1140. dma->device_prep_dma_pq_val = NULL;
  1141. #endif
  1142. #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  1143. dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
  1144. dma->device_prep_dma_xor_val = NULL;
  1145. #endif
  1146. err = ioat_probe(device);
  1147. if (err)
  1148. return err;
  1149. ioat_set_tcp_copy_break(262144);
  1150. list_for_each_entry(c, &dma->channels, device_node) {
  1151. chan = to_chan_common(c);
  1152. writel(IOAT_DMA_DCA_ANY_CPU,
  1153. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1154. }
  1155. err = ioat_register(device);
  1156. if (err)
  1157. return err;
  1158. ioat_kobject_add(device, &ioat2_ktype);
  1159. if (dca)
  1160. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1161. return 0;
  1162. }