dma_v3.c 41 KB

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